blob: 8515b0f456d8a94fd48aaed19452c3089aa9a4a7 [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"
Torok Edwin7d696d82009-07-11 13:10:19 +000045#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000046#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetMachine.h"
48#include "llvm/Target/TargetRegisterInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000049using namespace llvm;
50
51namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000052 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000053
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000054 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000055 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000056 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000057 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000058 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000059
60 bool runOnMachineFunction(MachineFunction &MF);
61
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000062 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000063 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000064 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000065 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000066 const MachineFunction *MF;
67 const TargetMachine *TM;
Evan Cheng15993f82011-06-27 21:26:13 +000068 const TargetInstrInfo *TII;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000069 const TargetRegisterInfo *TRI;
70 const MachineRegisterInfo *MRI;
71
72 unsigned foundErrors;
73
74 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000075 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000076 typedef DenseSet<unsigned> RegSet;
77 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +000078 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000079
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000080 const MachineInstr *FirstTerminator;
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +000081 BlockSet FunctionBlocks;
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000082
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000083 BitVector regsReserved;
84 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000085 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000086 RegMaskVector regMasks;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000087 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000088
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000089 SlotIndex lastIndex;
90
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000091 // Add Reg and any sub-registers to RV
92 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
93 RV.push_back(Reg);
94 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +000095 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
96 RV.push_back(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000097 }
98
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000099 struct BBInfo {
100 // Is this MBB reachable from the MF entry point?
101 bool reachable;
102
103 // Vregs that must be live in because they are used without being
104 // defined. Map value is the user.
105 RegMap vregsLiveIn;
106
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000107 // Regs killed in MBB. They may be defined again, and will then be in both
108 // regsKilled and regsLiveOut.
109 RegSet regsKilled;
110
111 // Regs defined in MBB and live out. Note that vregs passing through may
112 // be live out without being mentioned here.
113 RegSet regsLiveOut;
114
115 // Vregs that pass through MBB untouched. This set is disjoint from
116 // regsKilled and regsLiveOut.
117 RegSet vregsPassed;
118
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000119 // Vregs that must pass through MBB because they are needed by a successor
120 // block. This set is disjoint from regsLiveOut.
121 RegSet vregsRequired;
122
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000123 // Set versions of block's predecessor and successor lists.
124 BlockSet Preds, Succs;
125
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000126 BBInfo() : reachable(false) {}
127
128 // Add register to vregsPassed if it belongs there. Return true if
129 // anything changed.
130 bool addPassed(unsigned Reg) {
131 if (!TargetRegisterInfo::isVirtualRegister(Reg))
132 return false;
133 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
134 return false;
135 return vregsPassed.insert(Reg).second;
136 }
137
138 // Same for a full set.
139 bool addPassed(const RegSet &RS) {
140 bool changed = false;
141 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
142 if (addPassed(*I))
143 changed = true;
144 return changed;
145 }
146
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000147 // Add register to vregsRequired if it belongs there. Return true if
148 // anything changed.
149 bool addRequired(unsigned Reg) {
150 if (!TargetRegisterInfo::isVirtualRegister(Reg))
151 return false;
152 if (regsLiveOut.count(Reg))
153 return false;
154 return vregsRequired.insert(Reg).second;
155 }
156
157 // Same for a full set.
158 bool addRequired(const RegSet &RS) {
159 bool changed = false;
160 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
161 if (addRequired(*I))
162 changed = true;
163 return changed;
164 }
165
166 // Same for a full map.
167 bool addRequired(const RegMap &RM) {
168 bool changed = false;
169 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
170 if (addRequired(I->first))
171 changed = true;
172 return changed;
173 }
174
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000175 // Live-out registers are either in regsLiveOut or vregsPassed.
176 bool isLiveOut(unsigned Reg) const {
177 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
178 }
179 };
180
181 // Extra register info per MBB.
182 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
183
184 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000185 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000186 }
187
Lang Hames03698de2012-02-14 19:17:48 +0000188 bool isAllocatable(unsigned Reg) {
Jakob Stoklund Olesenfeab72c2012-10-16 00:05:06 +0000189 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
Lang Hames03698de2012-02-14 19:17:48 +0000190 }
191
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000192 // Analysis information if available
193 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000194 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000195 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000196 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000197
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000198 void visitMachineFunctionBefore();
199 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000200 void visitMachineBundleBefore(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000201 void visitMachineInstrBefore(const MachineInstr *MI);
202 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
203 void visitMachineInstrAfter(const MachineInstr *MI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000204 void visitMachineBundleAfter(const MachineInstr *MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000205 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
206 void visitMachineFunctionAfter();
207
208 void report(const char *msg, const MachineFunction *MF);
209 void report(const char *msg, const MachineBasicBlock *MBB);
210 void report(const char *msg, const MachineInstr *MI);
211 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000212 void report(const char *msg, const MachineFunction *MF,
213 const LiveInterval &LI);
214 void report(const char *msg, const MachineBasicBlock *MBB,
215 const LiveInterval &LI);
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000216 void report(const char *msg, const MachineFunction *MF,
217 const LiveRange &LR);
218 void report(const char *msg, const MachineBasicBlock *MBB,
219 const LiveRange &LR);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000220
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000221 void verifyInlineAsm(const MachineInstr *MI);
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000222
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000223 void checkLiveness(const MachineOperand *MO, unsigned MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000224 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000225 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000226 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000227
228 void calcRegsRequired();
229 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000230 void verifyLiveIntervals();
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +0000231 void verifyLiveInterval(const LiveInterval&);
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000232 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned);
233 void verifyLiveRangeSegment(const LiveRange&,
234 const LiveRange::const_iterator I, unsigned);
235 void verifyLiveRange(const LiveRange&, unsigned);
Manman Ren7310b752013-07-15 21:26:31 +0000236
237 void verifyStackFrame();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000238 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000239
240 struct MachineVerifierPass : public MachineFunctionPass {
241 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000242 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000243
Stephen Hinesdce4a402014-05-29 02:49:00 -0700244 MachineVerifierPass(const char *b = nullptr)
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000245 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000246 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
247 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000248
Stephen Hines36b56882014-04-23 16:57:46 -0700249 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000250 AU.setPreservesAll();
251 MachineFunctionPass::getAnalysisUsage(AU);
252 }
253
Stephen Hines36b56882014-04-23 16:57:46 -0700254 bool runOnMachineFunction(MachineFunction &MF) override {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000255 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000256 return false;
257 }
258 };
259
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000260}
261
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000262char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000263INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000264 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000265
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000266FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
267 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000268}
269
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000270void MachineFunction::verify(Pass *p, const char *Banner) const {
271 MachineVerifier(p, Banner)
272 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000273}
274
Chris Lattner17e9edc2009-08-23 02:51:22 +0000275bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700276 raw_ostream *OutFile = nullptr;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000277 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000278 std::string ErrorInfo;
Stephen Hines36b56882014-04-23 16:57:46 -0700279 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
280 sys::fs::F_Append | sys::fs::F_Text);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000281 if (!ErrorInfo.empty()) {
282 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
283 exit(1);
284 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000285
Chris Lattner17e9edc2009-08-23 02:51:22 +0000286 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000287 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000288 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000289 }
290
291 foundErrors = 0;
292
293 this->MF = &MF;
294 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000295 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000296 TRI = TM->getRegisterInfo();
297 MRI = &MF.getRegInfo();
298
Stephen Hinesdce4a402014-05-29 02:49:00 -0700299 LiveVars = nullptr;
300 LiveInts = nullptr;
301 LiveStks = nullptr;
302 Indexes = nullptr;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000303 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000304 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000305 // We don't want to verify LiveVariables if LiveIntervals is available.
306 if (!LiveInts)
307 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000308 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000309 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000310 }
311
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000312 visitMachineFunctionBefore();
313 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
314 MFI!=MFE; ++MFI) {
315 visitMachineBasicBlockBefore(MFI);
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000316 // Keep track of the current bundle header.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700317 const MachineInstr *CurBundle = nullptr;
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000318 // Do we expect the next instruction to be part of the same bundle?
319 bool InBundle = false;
320
Evan Chengddfd1372011-12-14 02:11:42 +0000321 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
322 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000323 if (MBBI->getParent() != MFI) {
324 report("Bad instruction parent pointer", MFI);
325 *OS << "Instruction: " << *MBBI;
326 continue;
327 }
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000328
329 // Check for consistent bundle flags.
330 if (InBundle && !MBBI->isBundledWithPred())
331 report("Missing BundledPred flag, "
332 "BundledSucc was set on predecessor", MBBI);
333 if (!InBundle && MBBI->isBundledWithPred())
334 report("BundledPred flag is set, "
335 "but BundledSucc not set on predecessor", MBBI);
336
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000337 // Is this a bundle header?
338 if (!MBBI->isInsideBundle()) {
339 if (CurBundle)
340 visitMachineBundleAfter(CurBundle);
341 CurBundle = MBBI;
342 visitMachineBundleBefore(CurBundle);
343 } else if (!CurBundle)
344 report("No bundle header", MBBI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000345 visitMachineInstrBefore(MBBI);
346 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
347 visitMachineOperand(&MBBI->getOperand(I), I);
348 visitMachineInstrAfter(MBBI);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000349
350 // Was this the last bundled instruction?
351 InBundle = MBBI->isBundledWithSucc();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000352 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000353 if (CurBundle)
354 visitMachineBundleAfter(CurBundle);
Jakob Stoklund Olesen9466bde2012-12-18 22:55:07 +0000355 if (InBundle)
356 report("BundledSucc flag set on last instruction in block", &MFI->back());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000357 visitMachineBasicBlockAfter(MFI);
358 }
359 visitMachineFunctionAfter();
360
Chris Lattner17e9edc2009-08-23 02:51:22 +0000361 if (OutFile)
362 delete OutFile;
363 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000364 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000365
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000366 // Clean up.
367 regsLive.clear();
368 regsDefined.clear();
369 regsDead.clear();
370 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000371 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000372 regsLiveInButUnused.clear();
373 MBBInfoMap.clear();
374
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000375 return false; // no changes
376}
377
Chris Lattner372fefe2009-08-23 01:03:30 +0000378void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000379 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000380 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000381 if (!foundErrors++) {
382 if (Banner)
383 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000384 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000385 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000386 *OS << "*** Bad machine code: " << msg << " ***\n"
Craig Topper96601ca2012-08-22 06:07:19 +0000387 << "- function: " << MF->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000388}
389
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000390void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000391 assert(MBB);
392 report(msg, MBB->getParent());
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000393 *OS << "- basic block: BB#" << MBB->getNumber()
394 << ' ' << MBB->getName()
Roman Divacky59324292012-09-05 22:26:57 +0000395 << " (" << (const void*)MBB << ')';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000396 if (Indexes)
397 *OS << " [" << Indexes->getMBBStartIdx(MBB)
398 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
399 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000400}
401
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000402void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000403 assert(MI);
404 report(msg, MI->getParent());
405 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000406 if (Indexes && Indexes->hasIndex(MI))
407 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000408 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000409}
410
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000411void MachineVerifier::report(const char *msg,
412 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000413 assert(MO);
414 report(msg, MO->getParent());
415 *OS << "- operand " << MONum << ": ";
416 MO->print(*OS, TM);
417 *OS << "\n";
418}
419
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000420void MachineVerifier::report(const char *msg, const MachineFunction *MF,
421 const LiveInterval &LI) {
422 report(msg, MF);
Matthias Braun03d96092013-10-10 21:29:05 +0000423 *OS << "- interval: " << LI << '\n';
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000424}
425
426void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
427 const LiveInterval &LI) {
428 report(msg, MBB);
Matthias Braun03d96092013-10-10 21:29:05 +0000429 *OS << "- interval: " << LI << '\n';
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +0000430}
431
Matthias Brauna4aed9a2013-10-10 21:28:54 +0000432void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB,
433 const LiveRange &LR) {
434 report(msg, MBB);
435 *OS << "- liverange: " << LR << "\n";
436}
437
438void MachineVerifier::report(const char *msg, const MachineFunction *MF,
439 const LiveRange &LR) {
440 report(msg, MF);
441 *OS << "- liverange: " << LR << "\n";
442}
443
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000444void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000445 BBInfo &MInfo = MBBInfoMap[MBB];
446 if (!MInfo.reachable) {
447 MInfo.reachable = true;
448 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
449 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
450 markReachable(*SuI);
451 }
452}
453
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000454void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000455 lastIndex = SlotIndex();
Jakob Stoklund Olesenfb9ebbf2012-10-15 21:57:41 +0000456 regsReserved = MRI->getReservedRegs();
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000457
458 // A sub-register of a reserved register is also reserved
459 for (int Reg = regsReserved.find_first(); Reg>=0;
460 Reg = regsReserved.find_next(Reg)) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000461 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000462 // FIXME: This should probably be:
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000463 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
464 regsReserved.set(*SubRegs);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000465 }
466 }
Lang Hames03698de2012-02-14 19:17:48 +0000467
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000468 markReachable(&MF->front());
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000469
470 // Build a set of the basic blocks in the function.
471 FunctionBlocks.clear();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700472 for (const auto &MBB : *MF) {
473 FunctionBlocks.insert(&MBB);
474 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000475
Stephen Hinesdce4a402014-05-29 02:49:00 -0700476 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
477 if (MInfo.Preds.size() != MBB.pred_size())
478 report("MBB has duplicate entries in its predecessor list.", &MBB);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000479
Stephen Hinesdce4a402014-05-29 02:49:00 -0700480 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
481 if (MInfo.Succs.size() != MBB.succ_size())
482 report("MBB has duplicate entries in its successor list.", &MBB);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000483 }
Jakob Stoklund Olesena58d67a2013-04-19 21:40:57 +0000484
485 // Check that the register use lists are sane.
486 MRI->verifyUseLists();
Manman Ren7310b752013-07-15 21:26:31 +0000487
488 verifyStackFrame();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000489}
490
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000491// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000492static bool matchPair(MachineBasicBlock::const_succ_iterator i,
493 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000494 if (*i == a)
495 return *++i == b;
496 if (*i == b)
497 return *++i == a;
498 return false;
499}
500
501void
502MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700503 FirstTerminator = nullptr;
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000504
Lang Hames03698de2012-02-14 19:17:48 +0000505 if (MRI->isSSA()) {
506 // If this block has allocatable physical registers live-in, check that
507 // it is an entry block or landing pad.
508 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
509 LE = MBB->livein_end();
510 LI != LE; ++LI) {
511 unsigned reg = *LI;
512 if (isAllocatable(reg) && !MBB->isLandingPad() &&
513 MBB != MBB->getParent()->begin()) {
514 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
515 }
516 }
517 }
518
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000519 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000520 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000521 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000522 E = MBB->succ_end(); I != E; ++I) {
523 if ((*I)->isLandingPad())
524 LandingPadSuccs.insert(*I);
Jakob Stoklund Olesenb254c6d2012-08-20 20:52:06 +0000525 if (!FunctionBlocks.count(*I))
526 report("MBB has successor that isn't part of the function.", MBB);
527 if (!MBBInfoMap[*I].Preds.count(MBB)) {
528 report("Inconsistent CFG", MBB);
529 *OS << "MBB is not in the predecessor list of the successor BB#"
530 << (*I)->getNumber() << ".\n";
531 }
532 }
533
534 // Check the predecessor list.
535 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
536 E = MBB->pred_end(); I != E; ++I) {
537 if (!FunctionBlocks.count(*I))
538 report("MBB has predecessor that isn't part of the function.", MBB);
539 if (!MBBInfoMap[*I].Succs.count(MBB)) {
540 report("Inconsistent CFG", MBB);
541 *OS << "MBB is not in the successor list of the predecessor BB#"
542 << (*I)->getNumber() << ".\n";
543 }
Cameron Zwarich2100d212010-12-20 04:19:48 +0000544 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000545
546 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
547 const BasicBlock *BB = MBB->getBasicBlock();
548 if (LandingPadSuccs.size() > 1 &&
549 !(AsmInfo &&
550 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
551 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000552 report("MBB has more than one landing pad successor", MBB);
553
Dan Gohman27920592009-08-27 02:43:49 +0000554 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700555 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Dan Gohman27920592009-08-27 02:43:49 +0000556 SmallVector<MachineOperand, 4> Cond;
557 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
558 TBB, FBB, Cond)) {
559 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
560 // check whether its answers match up with reality.
561 if (!TBB && !FBB) {
562 // Block falls through to its successor.
563 MachineFunction::const_iterator MBBI = MBB;
564 ++MBBI;
565 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000566 // It's possible that the block legitimately ends with a noreturn
567 // call or an unreachable, in which case it won't actually fall
568 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000569 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000570 // It's possible that the block legitimately ends with a noreturn
571 // call or an unreachable, in which case it won't actuall fall
572 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000573 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000574 report("MBB exits via unconditional fall-through but doesn't have "
575 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000576 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000577 report("MBB exits via unconditional fall-through but its successor "
578 "differs from its CFG successor!", MBB);
579 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700580 if (!MBB->empty() && MBB->back().isBarrier() &&
581 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000582 report("MBB exits via unconditional fall-through but ends with a "
583 "barrier instruction!", MBB);
584 }
585 if (!Cond.empty()) {
586 report("MBB exits via unconditional fall-through but has a condition!",
587 MBB);
588 }
589 } else if (TBB && !FBB && Cond.empty()) {
590 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000591 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000592 report("MBB exits via unconditional branch but doesn't have "
593 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000594 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000595 report("MBB exits via unconditional branch but the CFG "
596 "successor doesn't match the actual successor!", MBB);
597 }
598 if (MBB->empty()) {
599 report("MBB exits via unconditional branch but doesn't contain "
600 "any instructions!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700601 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000602 report("MBB exits via unconditional branch but doesn't end with a "
603 "barrier instruction!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700604 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000605 report("MBB exits via unconditional branch but the branch isn't a "
606 "terminator instruction!", MBB);
607 }
608 } else if (TBB && !FBB && !Cond.empty()) {
609 // Block conditionally branches somewhere, otherwise falls through.
610 MachineFunction::const_iterator MBBI = MBB;
611 ++MBBI;
612 if (MBBI == MF->end()) {
613 report("MBB conditionally falls through out of function!", MBB);
Dmitri Gribenko344df792012-12-19 22:13:01 +0000614 } else if (MBB->succ_size() == 1) {
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000615 // A conditional branch with only one successor is weird, but allowed.
616 if (&*MBBI != TBB)
617 report("MBB exits via conditional branch/fall-through but only has "
618 "one CFG successor!", MBB);
619 else if (TBB != *MBB->succ_begin())
620 report("MBB exits via conditional branch/fall-through but the CFG "
621 "successor don't match the actual successor!", MBB);
622 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000623 report("MBB exits via conditional branch/fall-through but doesn't have "
624 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000625 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000626 report("MBB exits via conditional branch/fall-through but the CFG "
627 "successors don't match the actual successors!", MBB);
628 }
629 if (MBB->empty()) {
630 report("MBB exits via conditional branch/fall-through but doesn't "
631 "contain any instructions!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700632 } else if (MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000633 report("MBB exits via conditional branch/fall-through but ends with a "
634 "barrier instruction!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700635 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000636 report("MBB exits via conditional branch/fall-through but the branch "
637 "isn't a terminator instruction!", MBB);
638 }
639 } else if (TBB && FBB) {
640 // Block conditionally branches somewhere, otherwise branches
641 // somewhere else.
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +0000642 if (MBB->succ_size() == 1) {
643 // A conditional branch with only one successor is weird, but allowed.
644 if (FBB != TBB)
645 report("MBB exits via conditional branch/branch through but only has "
646 "one CFG successor!", MBB);
647 else if (TBB != *MBB->succ_begin())
648 report("MBB exits via conditional branch/branch through but the CFG "
649 "successor don't match the actual successor!", MBB);
650 } else if (MBB->succ_size() != 2) {
Dan Gohman27920592009-08-27 02:43:49 +0000651 report("MBB exits via conditional branch/branch but doesn't have "
652 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000653 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000654 report("MBB exits via conditional branch/branch but the CFG "
655 "successors don't match the actual successors!", MBB);
656 }
657 if (MBB->empty()) {
658 report("MBB exits via conditional branch/branch but doesn't "
659 "contain any instructions!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700660 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000661 report("MBB exits via conditional branch/branch but doesn't end with a "
662 "barrier instruction!", MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700663 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000664 report("MBB exits via conditional branch/branch but the branch "
665 "isn't a terminator instruction!", MBB);
666 }
667 if (Cond.empty()) {
668 report("MBB exits via conditinal branch/branch but there's no "
669 "condition!", MBB);
670 }
671 } else {
672 report("AnalyzeBranch returned invalid data!", MBB);
673 }
674 }
675
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000676 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000677 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000678 E = MBB->livein_end(); I != E; ++I) {
679 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
680 report("MBB live-in list contains non-physical register", MBB);
681 continue;
682 }
Chad Rosier62c320a2013-05-22 23:17:36 +0000683 for (MCSubRegIterator SubRegs(*I, TRI, /*IncludeSelf=*/true);
684 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000685 regsLive.insert(*SubRegs);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000686 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000687 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000688
689 const MachineFrameInfo *MFI = MF->getFrameInfo();
690 assert(MFI && "Function has no frame info");
691 BitVector PR = MFI->getPristineRegs(MBB);
692 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
Chad Rosier62c320a2013-05-22 23:17:36 +0000693 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
694 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000695 regsLive.insert(*SubRegs);
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000696 }
697
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000698 regsKilled.clear();
699 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000700
701 if (Indexes)
702 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000703}
704
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000705// This function gets called for all bundle headers, including normal
706// stand-alone unbundled instructions.
707void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
708 if (Indexes && Indexes->hasIndex(MI)) {
709 SlotIndex idx = Indexes->getInstructionIndex(MI);
710 if (!(idx > lastIndex)) {
711 report("Instruction index out of order", MI);
712 *OS << "Last instruction was at " << lastIndex << '\n';
713 }
714 lastIndex = idx;
715 }
Pete Cooper83569cb2012-06-07 17:41:39 +0000716
717 // Ensure non-terminators don't follow terminators.
718 // Ignore predicated terminators formed by if conversion.
719 // FIXME: If conversion shouldn't need to violate this rule.
720 if (MI->isTerminator() && !TII->isPredicated(MI)) {
721 if (!FirstTerminator)
722 FirstTerminator = MI;
723 } else if (FirstTerminator) {
724 report("Non-terminator instruction after the first terminator", MI);
725 *OS << "First terminator was:\t" << *FirstTerminator;
726 }
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +0000727}
728
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000729// The operands on an INLINEASM instruction must follow a template.
730// Verify that the flag operands make sense.
731void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
732 // The first two operands on INLINEASM are the asm string and global flags.
733 if (MI->getNumOperands() < 2) {
734 report("Too few operands on inline asm", MI);
735 return;
736 }
737 if (!MI->getOperand(0).isSymbol())
738 report("Asm string must be an external symbol", MI);
739 if (!MI->getOperand(1).isImm())
740 report("Asm flags must be an immediate", MI);
Chad Rosier3d716882012-10-30 19:11:54 +0000741 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
742 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16.
743 if (!isUInt<5>(MI->getOperand(1).getImm()))
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000744 report("Unknown asm flags", &MI->getOperand(1), 1);
745
746 assert(InlineAsm::MIOp_FirstOperand == 2 && "Asm format changed");
747
748 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
749 unsigned NumOps;
750 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
751 const MachineOperand &MO = MI->getOperand(OpNo);
752 // There may be implicit ops after the fixed operands.
753 if (!MO.isImm())
754 break;
755 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
756 }
757
758 if (OpNo > MI->getNumOperands())
759 report("Missing operands in last group", MI);
760
761 // An optional MDNode follows the groups.
762 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
763 ++OpNo;
764
765 // All trailing operands must be implicit registers.
766 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
767 const MachineOperand &MO = MI->getOperand(OpNo);
768 if (!MO.isReg() || !MO.isImplicit())
769 report("Expected implicit register after groups", &MO, OpNo);
770 }
771}
772
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000773void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000774 const MCInstrDesc &MCID = MI->getDesc();
775 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000776 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000777 *OS << MCID.getNumOperands() << " operands expected, but "
Matt Arsenault17d4ac82013-11-15 22:18:19 +0000778 << MI->getNumOperands() << " given.\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000779 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000780
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000781 // Check the tied operands.
Jakob Stoklund Olesen90a4f782012-08-29 18:11:05 +0000782 if (MI->isInlineAsm())
783 verifyInlineAsm(MI);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000784
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000785 // Check the MachineMemOperands for basic consistency.
786 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
787 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000788 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000789 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000790 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000791 report("Missing mayStore flag", MI);
792 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000793
794 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000795 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000796 if (LiveInts) {
797 bool mapped = !LiveInts->isNotInMIMap(MI);
798 if (MI->isDebugValue()) {
799 if (mapped)
800 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000801 } else if (MI->isInsideBundle()) {
802 if (mapped)
803 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000804 } else {
805 if (!mapped)
806 report("Missing slot index", MI);
807 }
808 }
809
Andrew Trick3be654f2011-09-21 02:20:46 +0000810 StringRef ErrorInfo;
811 if (!TII->verifyInstruction(MI, ErrorInfo))
812 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000813}
814
815void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000816MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000817 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000818 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000819
Evan Chenge837dea2011-06-28 19:10:37 +0000820 // The first MCID.NumDefs operands must be explicit register defines
821 if (MONum < MCID.getNumDefs()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000822 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000823 if (!MO->isReg())
824 report("Explicit definition must be a register", MO, MONum);
Evan Chengcac58aa2012-05-29 19:40:44 +0000825 else if (!MO->isDef() && !MCOI.isOptionalDef())
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000826 report("Explicit definition marked as use", MO, MONum);
827 else if (MO->isImplicit())
828 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000829 } else if (MONum < MCID.getNumOperands()) {
Richard Smith11a4fa42012-08-15 01:39:31 +0000830 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Eric Christopher113a06c2010-11-17 00:55:36 +0000831 // Don't check if it's the last operand in a variadic instruction. See,
832 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000833 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000834 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000835 if (MO->isDef() && !MCOI.isOptionalDef())
Matthias Braunb38d9872013-10-04 16:53:00 +0000836 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000837 if (MO->isImplicit())
838 report("Explicit operand marked as implicit", MO, MONum);
839 }
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000840
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000841 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
842 if (TiedTo != -1) {
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000843 if (!MO->isReg())
844 report("Tied use must be a register", MO, MONum);
845 else if (!MO->isTied())
846 report("Operand should be tied", MO, MONum);
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000847 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
848 report("Tied def doesn't match MCInstrDesc", MO, MONum);
Jakob Stoklund Olesenca71c5d2012-08-29 00:38:03 +0000849 } else if (MO->isReg() && MO->isTied())
850 report("Explicit operand should not be tied", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000851 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000852 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000853 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000854 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000855 }
856
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000857 switch (MO->getType()) {
858 case MachineOperand::MO_Register: {
859 const unsigned Reg = MO->getReg();
860 if (!Reg)
861 return;
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000862 if (MRI->tracksLiveness() && !MI->isDebugValue())
863 checkLiveness(MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000864
Jakob Stoklund Olesendaddf072012-09-04 18:38:28 +0000865 // Verify the consistency of tied operands.
866 if (MO->isTied()) {
867 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
868 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
869 if (!OtherMO.isReg())
870 report("Must be tied to a register", MO, MONum);
871 if (!OtherMO.isTied())
872 report("Missing tie flags on tied operand", MO, MONum);
873 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
874 report("Inconsistent tie links", MO, MONum);
875 if (MONum < MCID.getNumDefs()) {
876 if (OtherIdx < MCID.getNumOperands()) {
877 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
878 report("Explicit def tied to explicit use without tie constraint",
879 MO, MONum);
880 } else {
881 if (!OtherMO.isImplicit())
882 report("Explicit def should be tied to implicit use", MO, MONum);
883 }
884 }
885 }
886
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000887 // Verify two-address constraints after leaving SSA form.
888 unsigned DefIdx;
889 if (!MRI->isSSA() && MO->isUse() &&
890 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
891 Reg != MI->getOperand(DefIdx).getReg())
892 report("Two-address instruction operands must be identical", MO, MONum);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000893
894 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000895 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000896 unsigned SubIdx = MO->getSubReg();
897
898 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000899 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000900 report("Illegal subregister index for physical register", MO, MONum);
901 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000902 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000903 if (const TargetRegisterClass *DRC =
904 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000905 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000906 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000907 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000908 << DRC->getName() << " register.\n";
909 }
910 }
911 } else {
912 // Virtual register.
913 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
914 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000915 const TargetRegisterClass *SRC =
916 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000917 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000918 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000919 *OS << "Register class " << RC->getName()
920 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000921 return;
922 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000923 if (RC != SRC) {
924 report("Invalid register class for subregister index", MO, MONum);
925 *OS << "Register class " << RC->getName()
926 << " does not fully support subreg index " << SubIdx << "\n";
927 return;
928 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000929 }
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000930 if (const TargetRegisterClass *DRC =
931 TII->getRegClass(MCID, MONum, TRI, *MF)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000932 if (SubIdx) {
933 const TargetRegisterClass *SuperRC =
934 TRI->getLargestLegalSuperClass(RC);
935 if (!SuperRC) {
936 report("No largest legal super class exists.", MO, MONum);
937 return;
938 }
939 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
940 if (!DRC) {
941 report("No matching super-reg register class.", MO, MONum);
942 return;
943 }
944 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000945 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000946 report("Illegal virtual register for instruction", MO, MONum);
947 *OS << "Expected a " << DRC->getName() << " register, but got a "
948 << RC->getName() << " register\n";
949 }
950 }
951 }
952 }
953 break;
954 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000955
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000956 case MachineOperand::MO_RegisterMask:
957 regMasks.push_back(MO->getRegMask());
958 break;
959
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000960 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000961 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
962 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000963 break;
964
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000965 case MachineOperand::MO_FrameIndex:
966 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
967 LiveInts && !LiveInts->isNotInMIMap(MI)) {
968 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
969 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000970 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000971 report("Instruction loads from dead spill slot", MO, MONum);
972 *OS << "Live stack: " << LI << '\n';
973 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000974 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000975 report("Instruction stores to dead spill slot", MO, MONum);
976 *OS << "Live stack: " << LI << '\n';
977 }
978 }
979 break;
980
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000981 default:
982 break;
983 }
984}
985
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000986void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
987 const MachineInstr *MI = MO->getParent();
988 const unsigned Reg = MO->getReg();
989
990 // Both use and def operands can read a register.
991 if (MO->readsReg()) {
992 regsLiveInButUnused.erase(Reg);
993
Jakob Stoklund Oleseneba2bbb2012-07-25 16:49:11 +0000994 if (MO->isKill())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +0000995 addRegWithSubRegs(regsKilled, Reg);
996
997 // Check that LiveVars knows this kill.
998 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
999 MO->isKill()) {
1000 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1001 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1002 report("Kill missing from LiveVariables", MO, MONum);
1003 }
1004
1005 // Check LiveInts liveness and kill.
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001006 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
1007 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
1008 // Check the cached regunit intervals.
1009 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1010 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001011 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units)) {
1012 LiveQueryResult LRQ = LR->Query(UseIdx);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001013 if (!LRQ.valueIn()) {
Matthias Braun331de112013-10-10 21:28:43 +00001014 report("No live segment at use", MO, MONum);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001015 *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI)
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001016 << ' ' << *LR << '\n';
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001017 }
1018 if (MO->isKill() && !LRQ.isKill()) {
1019 report("Live range continues after kill flag", MO, MONum);
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001020 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LR << '\n';
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001021 }
1022 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001023 }
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001024 }
1025
1026 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1027 if (LiveInts->hasInterval(Reg)) {
1028 // This is a virtual register interval.
1029 const LiveInterval &LI = LiveInts->getInterval(Reg);
Matthias Braun5649e252013-10-10 21:28:52 +00001030 LiveQueryResult LRQ = LI.Query(UseIdx);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001031 if (!LRQ.valueIn()) {
Matthias Braun331de112013-10-10 21:28:43 +00001032 report("No live segment at use", MO, MONum);
Jakob Stoklund Olesena62e1e82012-08-01 23:52:40 +00001033 *OS << UseIdx << " is not live in " << LI << '\n';
1034 }
1035 // Check for extra kill flags.
1036 // Note that we allow missing kill flags for now.
1037 if (MO->isKill() && !LRQ.isKill()) {
1038 report("Live range continues after kill flag", MO, MONum);
1039 *OS << "Live range: " << LI << '\n';
1040 }
1041 } else {
1042 report("Virtual register has no live interval", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001043 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001044 }
1045 }
1046
1047 // Use of a dead register.
1048 if (!regsLive.count(Reg)) {
1049 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1050 // Reserved registers may be used even when 'dead'.
1051 if (!isReserved(Reg))
1052 report("Using an undefined physical register", MO, MONum);
Pete Cooperb97c57a2012-07-19 23:40:38 +00001053 } else if (MRI->def_empty(Reg)) {
1054 report("Reading virtual register without a def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001055 } else {
1056 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1057 // We don't know which virtual registers are live in, so only complain
1058 // if vreg was killed in this MBB. Otherwise keep track of vregs that
1059 // must be live in. PHI instructions are handled separately.
1060 if (MInfo.regsKilled.count(Reg))
1061 report("Using a killed virtual register", MO, MONum);
1062 else if (!MI->isPHI())
1063 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1064 }
1065 }
1066 }
1067
1068 if (MO->isDef()) {
1069 // Register defined.
1070 // TODO: verify that earlyclobber ops are not used.
1071 if (MO->isDead())
1072 addRegWithSubRegs(regsDead, Reg);
1073 else
1074 addRegWithSubRegs(regsDefined, Reg);
1075
1076 // Verify SSA form.
1077 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
Stephen Hines36b56882014-04-23 16:57:46 -07001078 std::next(MRI->def_begin(Reg)) != MRI->def_end())
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001079 report("Multiple virtual register defs in SSA form", MO, MONum);
1080
Matthias Braun331de112013-10-10 21:28:43 +00001081 // Check LiveInts for a live segment, but only for virtual registers.
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001082 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
1083 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001084 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1085 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001086 if (LiveInts->hasInterval(Reg)) {
1087 const LiveInterval &LI = LiveInts->getInterval(Reg);
1088 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
1089 assert(VNI && "NULL valno is not allowed");
Jakob Stoklund Olesenf935e942012-06-22 22:23:58 +00001090 if (VNI->def != DefIdx) {
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001091 report("Inconsistent valno->def", MO, MONum);
1092 *OS << "Valno " << VNI->id << " is not defined at "
1093 << DefIdx << " in " << LI << '\n';
1094 }
1095 } else {
Matthias Braun331de112013-10-10 21:28:43 +00001096 report("No live segment at def", MO, MONum);
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001097 *OS << DefIdx << " is not live in " << LI << '\n';
1098 }
Pedro Artigasd900b112013-11-08 22:46:28 +00001099 // Check that, if the dead def flag is present, LiveInts agree.
1100 if (MO->isDead()) {
1101 LiveQueryResult LRQ = LI.Query(DefIdx);
1102 if (!LRQ.isDeadDef()) {
1103 report("Live range continues after dead def flag", MO, MONum);
1104 *OS << "Live range: " << LI << '\n';
1105 }
1106 }
Jakob Stoklund Olesen948a4442012-03-28 20:47:35 +00001107 } else {
1108 report("Virtual register has no Live interval", MO, MONum);
1109 }
1110 }
1111 }
1112}
1113
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001114void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen1f9c3ec2012-06-06 22:34:30 +00001115}
1116
1117// This function gets called after visiting all instructions in a bundle. The
1118// argument points to the bundle header.
1119// Normal stand-alone instructions are also considered 'bundles', and this
1120// function is called for all of them.
1121void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001122 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1123 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001124 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +00001125 // Kill any masked registers.
1126 while (!regMasks.empty()) {
1127 const uint32_t *Mask = regMasks.pop_back_val();
1128 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1129 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1130 MachineOperand::clobbersPhysReg(Mask, *I))
1131 regsDead.push_back(*I);
1132 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +00001133 set_subtract(regsLive, regsDead); regsDead.clear();
1134 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001135}
1136
1137void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001138MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001139 MBBInfoMap[MBB].regsLiveOut = regsLive;
1140 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +00001141
1142 if (Indexes) {
1143 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1144 if (!(stop > lastIndex)) {
1145 report("Block ends before last instruction index", MBB);
1146 *OS << "Block ends at " << stop
1147 << " last instruction was at " << lastIndex << '\n';
1148 }
1149 lastIndex = stop;
1150 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001151}
1152
1153// Calculate the largest possible vregsPassed sets. These are the registers that
1154// can pass through an MBB live, but may not be live every time. It is assumed
1155// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001156void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001157 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1158 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001159 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001160 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001161 BBInfo &MInfo = MBBInfoMap[&MBB];
1162 if (!MInfo.reachable)
1163 continue;
1164 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1165 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1166 BBInfo &SInfo = MBBInfoMap[*SuI];
1167 if (SInfo.addPassed(MInfo.regsLiveOut))
1168 todo.insert(*SuI);
1169 }
1170 }
1171
1172 // Iteratively push vregsPassed to successors. This will converge to the same
1173 // final state regardless of DenseSet iteration order.
1174 while (!todo.empty()) {
1175 const MachineBasicBlock *MBB = *todo.begin();
1176 todo.erase(MBB);
1177 BBInfo &MInfo = MBBInfoMap[MBB];
1178 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1179 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1180 if (*SuI == MBB)
1181 continue;
1182 BBInfo &SInfo = MBBInfoMap[*SuI];
1183 if (SInfo.addPassed(MInfo.vregsPassed))
1184 todo.insert(*SuI);
1185 }
1186 }
1187}
1188
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001189// Calculate the set of virtual registers that must be passed through each basic
1190// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001191// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001192void MachineVerifier::calcRegsRequired() {
1193 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001194 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001195 for (const auto &MBB : *MF) {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001196 BBInfo &MInfo = MBBInfoMap[&MBB];
1197 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1198 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1199 BBInfo &PInfo = MBBInfoMap[*PrI];
1200 if (PInfo.addRequired(MInfo.vregsLiveIn))
1201 todo.insert(*PrI);
1202 }
1203 }
1204
1205 // Iteratively push vregsRequired to predecessors. This will converge to the
1206 // same final state regardless of DenseSet iteration order.
1207 while (!todo.empty()) {
1208 const MachineBasicBlock *MBB = *todo.begin();
1209 todo.erase(MBB);
1210 BBInfo &MInfo = MBBInfoMap[MBB];
1211 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1212 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1213 if (*PrI == MBB)
1214 continue;
1215 BBInfo &SInfo = MBBInfoMap[*PrI];
1216 if (SInfo.addRequired(MInfo.vregsRequired))
1217 todo.insert(*PrI);
1218 }
1219 }
1220}
1221
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001222// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001223// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001224void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001225 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001226 for (const auto &BBI : *MBB) {
1227 if (!BBI.isPHI())
1228 break;
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +00001229 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001230
Stephen Hinesdce4a402014-05-29 02:49:00 -07001231 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1232 unsigned Reg = BBI.getOperand(i).getReg();
1233 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001234 if (!Pre->isSuccessor(MBB))
1235 continue;
1236 seen.insert(Pre);
1237 BBInfo &PrInfo = MBBInfoMap[Pre];
1238 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1239 report("PHI operand is not live-out from predecessor",
Stephen Hinesdce4a402014-05-29 02:49:00 -07001240 &BBI.getOperand(i), i);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001241 }
1242
1243 // Did we see all predecessors?
1244 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1245 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1246 if (!seen.count(*PrI)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001247 report("Missing PHI operand", &BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +00001248 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001249 << " is a predecessor according to the CFG.\n";
1250 }
1251 }
1252 }
1253}
1254
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001255void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001256 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001257
Stephen Hinesdce4a402014-05-29 02:49:00 -07001258 for (const auto &MBB : *MF) {
1259 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001260
1261 // Skip unreachable MBBs.
1262 if (!MInfo.reachable)
1263 continue;
1264
Stephen Hinesdce4a402014-05-29 02:49:00 -07001265 checkPHIOps(&MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001266 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001267
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001268 // Now check liveness info if available
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001269 calcRegsRequired();
1270
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001271 // Check for killed virtual registers that should be live out.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001272 for (const auto &MBB : *MF) {
1273 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001274 for (RegSet::iterator
1275 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1276 ++I)
1277 if (MInfo.regsKilled.count(*I)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001278 report("Virtual register killed in block, but needed live out.", &MBB);
Bill Wendling96cb1122012-07-19 00:04:14 +00001279 *OS << "Virtual register " << PrintReg(*I)
Jakob Stoklund Olesenbb072162012-06-29 21:00:00 +00001280 << " is used after the block.\n";
1281 }
1282 }
1283
Jakob Stoklund Olesena4e63972012-06-25 18:18:27 +00001284 if (!MF->empty()) {
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001285 BBInfo &MInfo = MBBInfoMap[&MF->front()];
1286 for (RegSet::iterator
1287 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
Jakob Stoklund Olesenff0275e2012-03-10 00:44:11 +00001288 ++I)
1289 report("Virtual register def doesn't dominate all uses.",
1290 MRI->getVRegDef(*I));
Jakob Stoklund Olesen64ffa832012-03-10 00:36:06 +00001291 }
1292
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001293 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001294 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001295 if (LiveInts)
1296 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001297}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001298
1299void MachineVerifier::verifyLiveVariables() {
1300 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001301 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1302 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001303 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001304 for (const auto &MBB : *MF) {
1305 BBInfo &MInfo = MBBInfoMap[&MBB];
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001306
1307 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1308 if (MInfo.vregsRequired.count(Reg)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001309 if (!VI.AliveBlocks.test(MBB.getNumber())) {
1310 report("LiveVariables: Block missing from AliveBlocks", &MBB);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001311 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001312 << " must be live through the block.\n";
1313 }
1314 } else {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001315 if (VI.AliveBlocks.test(MBB.getNumber())) {
1316 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001317 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001318 << " is not needed live through the block.\n";
1319 }
1320 }
1321 }
1322 }
1323}
1324
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001325void MachineVerifier::verifyLiveIntervals() {
1326 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001327 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1328 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001329
1330 // Spilling and splitting may leave unused registers around. Skip them.
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001331 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001332 continue;
1333
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001334 if (!LiveInts->hasInterval(Reg)) {
1335 report("Missing live interval for virtual register", MF);
1336 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n";
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001337 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001338 }
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001339
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +00001340 const LiveInterval &LI = LiveInts->getInterval(Reg);
1341 assert(Reg == LI.reg && "Invalid reg to interval mapping");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001342 verifyLiveInterval(LI);
1343 }
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001344
1345 // Verify all the cached regunit intervals.
1346 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001347 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1348 verifyLiveRange(*LR, i);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001349}
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001350
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001351void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
1352 const VNInfo *VNI,
1353 unsigned Reg) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001354 if (VNI->isUnused())
1355 return;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001356
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001357 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001358
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001359 if (!DefVNI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001360 report("Valno not live at def and not marked unused", MF, LR);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001361 *OS << "Valno #" << VNI->id << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001362 return;
1363 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001364
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001365 if (DefVNI != VNI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001366 report("Live segment at def has different valno", MF, LR);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001367 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001368 << " where valno #" << DefVNI->id << " is live\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001369 return;
1370 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001371
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001372 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1373 if (!MBB) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001374 report("Invalid definition index", MF, LR);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001375 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001376 << " in " << LR << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001377 return;
1378 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001379
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001380 if (VNI->isPHIDef()) {
1381 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001382 report("PHIDef value is not defined at MBB start", MBB, LR);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001383 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001384 << ", not at the beginning of BB#" << MBB->getNumber() << '\n';
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001385 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001386 return;
1387 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001388
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001389 // Non-PHI def.
1390 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1391 if (!MI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001392 report("No instruction at def index", MBB, LR);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001393 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001394 return;
1395 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001396
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001397 if (Reg != 0) {
1398 bool hasDef = false;
1399 bool isEarlyClobber = false;
1400 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
1401 if (!MOI->isReg() || !MOI->isDef())
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001402 continue;
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001403 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1404 if (MOI->getReg() != Reg)
1405 continue;
1406 } else {
1407 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1408 !TRI->hasRegUnit(MOI->getReg(), Reg))
1409 continue;
1410 }
1411 hasDef = true;
1412 if (MOI->isEarlyClobber())
1413 isEarlyClobber = true;
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001414 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001415
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001416 if (!hasDef) {
1417 report("Defining instruction does not modify register", MI);
1418 *OS << "Valno #" << VNI->id << " in " << LR << '\n';
1419 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001420
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001421 // Early clobber defs begin at USE slots, but other defs must begin at
1422 // DEF slots.
1423 if (isEarlyClobber) {
1424 if (!VNI->def.isEarlyClobber()) {
1425 report("Early clobber def must be at an early-clobber slot", MBB, LR);
1426 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
1427 }
1428 } else if (!VNI->def.isRegister()) {
1429 report("Non-PHI, non-early clobber def must be at a register slot",
1430 MBB, LR);
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001431 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001432 }
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001433 }
1434}
1435
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001436void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1437 const LiveRange::const_iterator I,
1438 unsigned Reg) {
1439 const LiveRange::Segment &S = *I;
1440 const VNInfo *VNI = S.valno;
Matthias Braun331de112013-10-10 21:28:43 +00001441 assert(VNI && "Live segment has no valno");
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001442
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001443 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
1444 report("Foreign valno in live segment", MF, LR);
1445 *OS << S << " has a bad valno\n";
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001446 }
1447
1448 if (VNI->isUnused()) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001449 report("Live segment valno is marked unused", MF, LR);
1450 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001451 }
1452
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001453 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001454 if (!MBB) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001455 report("Bad start of live segment, no basic block", MF, LR);
1456 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001457 return;
1458 }
1459 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001460 if (S.start != MBBStartIdx && S.start != VNI->def) {
1461 report("Live segment must begin at MBB entry or valno def", MBB, LR);
1462 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001463 }
1464
1465 const MachineBasicBlock *EndMBB =
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001466 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001467 if (!EndMBB) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001468 report("Bad end of live segment, no basic block", MF, LR);
1469 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001470 return;
1471 }
1472
1473 // No more checks for live-out segments.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001474 if (S.end == LiveInts->getMBBEndIdx(EndMBB))
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001475 return;
1476
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001477 // RegUnit intervals are allowed dead phis.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001478 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1479 S.start == VNI->def && S.end == VNI->def.getDeadSlot())
Jakob Stoklund Olesen80446892012-08-02 16:36:50 +00001480 return;
1481
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001482 // The live segment is ending inside EndMBB
1483 const MachineInstr *MI =
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001484 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001485 if (!MI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001486 report("Live segment doesn't end at a valid instruction", EndMBB, LR);
1487 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001488 return;
1489 }
1490
1491 // The block slot must refer to a basic block boundary.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001492 if (S.end.isBlock()) {
1493 report("Live segment ends at B slot of an instruction", EndMBB, LR);
1494 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001495 }
1496
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001497 if (S.end.isDead()) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001498 // Segment ends on the dead slot.
1499 // That means there must be a dead def.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001500 if (!SlotIndex::isSameInstr(S.start, S.end)) {
1501 report("Live segment ending at dead slot spans instructions", EndMBB, LR);
1502 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001503 }
1504 }
1505
1506 // A live segment can only end at an early-clobber slot if it is being
1507 // redefined by an early-clobber def.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001508 if (S.end.isEarlyClobber()) {
1509 if (I+1 == LR.end() || (I+1)->start != S.end) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001510 report("Live segment ending at early clobber slot must be "
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001511 "redefined by an EC def in the same instruction", EndMBB, LR);
1512 *OS << S << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001513 }
1514 }
1515
1516 // The following checks only apply to virtual registers. Physreg liveness
1517 // is too weird to check.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001518 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braun331de112013-10-10 21:28:43 +00001519 // A live segment can end with either a redefinition, a kill flag on a
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001520 // use, or a dead flag on a def.
1521 bool hasRead = false;
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001522 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001523 if (!MOI->isReg() || MOI->getReg() != Reg)
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001524 continue;
1525 if (MOI->readsReg())
1526 hasRead = true;
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001527 }
Pedro Artigasd900b112013-11-08 22:46:28 +00001528 if (!S.end.isDead()) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001529 if (!hasRead) {
Matthias Braun331de112013-10-10 21:28:43 +00001530 report("Instruction ending live segment doesn't read the register", MI);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001531 *OS << S << " in " << LR << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001532 }
1533 }
1534 }
1535
1536 // Now check all the basic blocks in this live segment.
1537 MachineFunction::const_iterator MFI = MBB;
Matthias Braun331de112013-10-10 21:28:43 +00001538 // Is this live segment the beginning of a non-PHIDef VN?
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001539 if (S.start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001540 // Not live-in to any blocks.
1541 if (MBB == EndMBB)
1542 return;
1543 // Skip this block.
1544 ++MFI;
1545 }
1546 for (;;) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001547 assert(LiveInts->isLiveInToMBB(LR, MFI));
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001548 // We don't know how to track physregs into a landing pad.
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001549 if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001550 MFI->isLandingPad()) {
1551 if (&*MFI == EndMBB)
1552 break;
1553 ++MFI;
1554 continue;
1555 }
1556
1557 // Is VNI a PHI-def in the current block?
1558 bool IsPHI = VNI->isPHIDef() &&
1559 VNI->def == LiveInts->getMBBStartIdx(MFI);
1560
1561 // Check that VNI is live-out of all predecessors.
1562 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1563 PE = MFI->pred_end(); PI != PE; ++PI) {
1564 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001565 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001566
1567 // All predecessors must have a live-out value.
1568 if (!PVNI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001569 report("Register not marked live out of predecessor", *PI, LR);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001570 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1571 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001572 << PEnd << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001573 continue;
1574 }
1575
1576 // Only PHI-defs can take different predecessor values.
1577 if (!IsPHI && PVNI != VNI) {
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001578 report("Different value live out of predecessor", *PI, LR);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001579 *OS << "Valno #" << PVNI->id << " live out of BB#"
1580 << (*PI)->getNumber() << '@' << PEnd
1581 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001582 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n';
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001583 }
1584 }
1585 if (&*MFI == EndMBB)
1586 break;
1587 ++MFI;
1588 }
1589}
1590
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001591void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg) {
1592 for (LiveRange::const_vni_iterator I = LR.vni_begin(), E = LR.vni_end();
1593 I != E; ++I)
1594 verifyLiveRangeValue(LR, *I, Reg);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001595
Matthias Brauna4aed9a2013-10-10 21:28:54 +00001596 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
1597 verifyLiveRangeSegment(LR, I, Reg);
1598}
1599
1600void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1601 verifyLiveRange(LI, LI.reg);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001602
1603 // Check the LI only has one connected component.
1604 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1605 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1606 unsigned NumComp = ConEQ.Classify(&LI);
1607 if (NumComp > 1) {
Jakob Stoklund Olesen79240f952012-08-02 14:31:49 +00001608 report("Multiple connected components in live interval", MF, LI);
Jakob Stoklund Olesene5c79a52012-08-02 00:20:20 +00001609 for (unsigned comp = 0; comp != NumComp; ++comp) {
1610 *OS << comp << ": valnos";
1611 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1612 E = LI.vni_end(); I!=E; ++I)
1613 if (comp == ConEQ.getEqClass(*I))
1614 *OS << ' ' << (*I)->id;
1615 *OS << '\n';
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001616 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001617 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001618 }
1619}
Manman Ren7310b752013-07-15 21:26:31 +00001620
1621namespace {
1622 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1623 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1624 // value is zero.
1625 // We use a bool plus an integer to capture the stack state.
1626 struct StackStateOfBB {
1627 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1628 ExitIsSetup(false) { }
1629 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1630 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1631 ExitIsSetup(ExitSetup) { }
1632 // Can be negative, which means we are setting up a frame.
1633 int EntryValue;
1634 int ExitValue;
1635 bool EntryIsSetup;
1636 bool ExitIsSetup;
1637 };
1638}
1639
1640/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1641/// by a FrameDestroy <n>, stack adjustments are identical on all
1642/// CFG edges to a merge point, and frame is destroyed at end of a return block.
1643void MachineVerifier::verifyStackFrame() {
1644 int FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1645 int FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
1646
1647 SmallVector<StackStateOfBB, 8> SPState;
1648 SPState.resize(MF->getNumBlockIDs());
1649 SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1650
1651 // Visit the MBBs in DFS order.
1652 for (df_ext_iterator<const MachineFunction*,
1653 SmallPtrSet<const MachineBasicBlock*, 8> >
1654 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1655 DFI != DFE; ++DFI) {
1656 const MachineBasicBlock *MBB = *DFI;
1657
1658 StackStateOfBB BBState;
1659 // Check the exit state of the DFS stack predecessor.
1660 if (DFI.getPathLength() >= 2) {
1661 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1662 assert(Reachable.count(StackPred) &&
1663 "DFS stack predecessor is already visited.\n");
1664 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1665 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1666 BBState.ExitValue = BBState.EntryValue;
1667 BBState.ExitIsSetup = BBState.EntryIsSetup;
1668 }
1669
1670 // Update stack state by checking contents of MBB.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001671 for (const auto &I : *MBB) {
1672 if (I.getOpcode() == FrameSetupOpcode) {
Manman Ren7310b752013-07-15 21:26:31 +00001673 // The first operand of a FrameOpcode should be i32.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001674 int Size = I.getOperand(0).getImm();
Manman Ren7310b752013-07-15 21:26:31 +00001675 assert(Size >= 0 &&
1676 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1677
1678 if (BBState.ExitIsSetup)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001679 report("FrameSetup is after another FrameSetup", &I);
Manman Ren7310b752013-07-15 21:26:31 +00001680 BBState.ExitValue -= Size;
1681 BBState.ExitIsSetup = true;
1682 }
1683
Stephen Hinesdce4a402014-05-29 02:49:00 -07001684 if (I.getOpcode() == FrameDestroyOpcode) {
Manman Ren7310b752013-07-15 21:26:31 +00001685 // The first operand of a FrameOpcode should be i32.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001686 int Size = I.getOperand(0).getImm();
Manman Ren7310b752013-07-15 21:26:31 +00001687 assert(Size >= 0 &&
1688 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1689
1690 if (!BBState.ExitIsSetup)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001691 report("FrameDestroy is not after a FrameSetup", &I);
Manman Ren7310b752013-07-15 21:26:31 +00001692 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1693 BBState.ExitValue;
1694 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001695 report("FrameDestroy <n> is after FrameSetup <m>", &I);
Manman Ren7310b752013-07-15 21:26:31 +00001696 *OS << "FrameDestroy <" << Size << "> is after FrameSetup <"
1697 << AbsSPAdj << ">.\n";
1698 }
1699 BBState.ExitValue += Size;
1700 BBState.ExitIsSetup = false;
1701 }
1702 }
1703 SPState[MBB->getNumber()] = BBState;
1704
1705 // Make sure the exit state of any predecessor is consistent with the entry
1706 // state.
1707 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
1708 E = MBB->pred_end(); I != E; ++I) {
1709 if (Reachable.count(*I) &&
1710 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1711 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1712 report("The exit stack state of a predecessor is inconsistent.", MBB);
1713 *OS << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
1714 << SPState[(*I)->getNumber()].ExitValue << ", "
1715 << SPState[(*I)->getNumber()].ExitIsSetup
1716 << "), while BB#" << MBB->getNumber() << " has entry state ("
1717 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
1718 }
1719 }
1720
1721 // Make sure the entry state of any successor is consistent with the exit
1722 // state.
1723 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
1724 E = MBB->succ_end(); I != E; ++I) {
1725 if (Reachable.count(*I) &&
1726 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1727 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1728 report("The entry stack state of a successor is inconsistent.", MBB);
1729 *OS << "Successor BB#" << (*I)->getNumber() << " has entry state ("
1730 << SPState[(*I)->getNumber()].EntryValue << ", "
1731 << SPState[(*I)->getNumber()].EntryIsSetup
1732 << "), while BB#" << MBB->getNumber() << " has exit state ("
1733 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
1734 }
1735 }
1736
1737 // Make sure a basic block with return ends with zero stack adjustment.
1738 if (!MBB->empty() && MBB->back().isReturn()) {
1739 if (BBState.ExitIsSetup)
1740 report("A return block ends with a FrameSetup.", MBB);
1741 if (BBState.ExitValue)
1742 report("A return block ends with a nonzero stack adjustment.", MBB);
1743 }
1744 }
1745}