blob: f26886fd20f8fd18cc5b97acb41f01cd242d5b0f [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
Bill Wendlingd29052b2011-05-04 22:54:05 +000026#include "llvm/Instructions.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000027#include "llvm/Function.h"
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +000028#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000029#include "llvm/CodeGen/LiveVariables.h"
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +000030#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +000031#include "llvm/CodeGen/MachineInstrBundle.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +000033#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman2dbc4c82009-10-07 17:36:00 +000034#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
36#include "llvm/CodeGen/Passes.h"
Bill Wendlingd29052b2011-05-04 22:54:05 +000037#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000038#include "llvm/Target/TargetMachine.h"
39#include "llvm/Target/TargetRegisterInfo.h"
40#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000041#include "llvm/ADT/DenseSet.h"
42#include "llvm/ADT/SetOperations.h"
43#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000044#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000045#include "llvm/Support/ErrorHandling.h"
46#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000047using namespace llvm;
48
49namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000050 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000051
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000052 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000053 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000054 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000055 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000056 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000057
58 bool runOnMachineFunction(MachineFunction &MF);
59
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000060 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000061 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000062 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000063 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000064 const MachineFunction *MF;
65 const TargetMachine *TM;
Evan Cheng15993f82011-06-27 21:26:13 +000066 const TargetInstrInfo *TII;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000067 const TargetRegisterInfo *TRI;
68 const MachineRegisterInfo *MRI;
69
70 unsigned foundErrors;
71
72 typedef SmallVector<unsigned, 16> RegVector;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000073 typedef SmallVector<const uint32_t*, 4> RegMaskVector;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000074 typedef DenseSet<unsigned> RegSet;
75 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
76
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +000077 const MachineInstr *FirstTerminator;
78
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000079 BitVector regsReserved;
Lang Hames03698de2012-02-14 19:17:48 +000080 BitVector regsAllocatable;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000081 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000082 RegVector regsDefined, regsDead, regsKilled;
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +000083 RegMaskVector regMasks;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000084 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000085
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000086 SlotIndex lastIndex;
87
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000088 // Add Reg and any sub-registers to RV
89 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
90 RV.push_back(Reg);
91 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topper9ebfbf82012-03-05 05:37:41 +000092 for (const uint16_t *R = TRI->getSubRegisters(Reg); *R; R++)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000093 RV.push_back(*R);
94 }
95
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000096 struct BBInfo {
97 // Is this MBB reachable from the MF entry point?
98 bool reachable;
99
100 // Vregs that must be live in because they are used without being
101 // defined. Map value is the user.
102 RegMap vregsLiveIn;
103
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000104 // Regs killed in MBB. They may be defined again, and will then be in both
105 // regsKilled and regsLiveOut.
106 RegSet regsKilled;
107
108 // Regs defined in MBB and live out. Note that vregs passing through may
109 // be live out without being mentioned here.
110 RegSet regsLiveOut;
111
112 // Vregs that pass through MBB untouched. This set is disjoint from
113 // regsKilled and regsLiveOut.
114 RegSet vregsPassed;
115
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000116 // Vregs that must pass through MBB because they are needed by a successor
117 // block. This set is disjoint from regsLiveOut.
118 RegSet vregsRequired;
119
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000120 BBInfo() : reachable(false) {}
121
122 // Add register to vregsPassed if it belongs there. Return true if
123 // anything changed.
124 bool addPassed(unsigned Reg) {
125 if (!TargetRegisterInfo::isVirtualRegister(Reg))
126 return false;
127 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
128 return false;
129 return vregsPassed.insert(Reg).second;
130 }
131
132 // Same for a full set.
133 bool addPassed(const RegSet &RS) {
134 bool changed = false;
135 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
136 if (addPassed(*I))
137 changed = true;
138 return changed;
139 }
140
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000141 // Add register to vregsRequired if it belongs there. Return true if
142 // anything changed.
143 bool addRequired(unsigned Reg) {
144 if (!TargetRegisterInfo::isVirtualRegister(Reg))
145 return false;
146 if (regsLiveOut.count(Reg))
147 return false;
148 return vregsRequired.insert(Reg).second;
149 }
150
151 // Same for a full set.
152 bool addRequired(const RegSet &RS) {
153 bool changed = false;
154 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
155 if (addRequired(*I))
156 changed = true;
157 return changed;
158 }
159
160 // Same for a full map.
161 bool addRequired(const RegMap &RM) {
162 bool changed = false;
163 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
164 if (addRequired(I->first))
165 changed = true;
166 return changed;
167 }
168
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000169 // Live-out registers are either in regsLiveOut or vregsPassed.
170 bool isLiveOut(unsigned Reg) const {
171 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
172 }
173 };
174
175 // Extra register info per MBB.
176 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
177
178 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000179 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000180 }
181
Lang Hames03698de2012-02-14 19:17:48 +0000182 bool isAllocatable(unsigned Reg) {
183 return Reg < regsAllocatable.size() && regsAllocatable.test(Reg);
184 }
185
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000186 // Analysis information if available
187 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000188 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000189 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000190 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000191
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000192 void visitMachineFunctionBefore();
193 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
194 void visitMachineInstrBefore(const MachineInstr *MI);
195 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
196 void visitMachineInstrAfter(const MachineInstr *MI);
197 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
198 void visitMachineFunctionAfter();
199
200 void report(const char *msg, const MachineFunction *MF);
201 void report(const char *msg, const MachineBasicBlock *MBB);
202 void report(const char *msg, const MachineInstr *MI);
203 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
204
205 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000206 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000207 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000208
209 void calcRegsRequired();
210 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000211 void verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000212 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000213
214 struct MachineVerifierPass : public MachineFunctionPass {
215 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000216 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000217
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000218 MachineVerifierPass(const char *b = 0)
219 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000220 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
221 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000222
223 void getAnalysisUsage(AnalysisUsage &AU) const {
224 AU.setPreservesAll();
225 MachineFunctionPass::getAnalysisUsage(AU);
226 }
227
228 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000229 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000230 return false;
231 }
232 };
233
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000234}
235
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000236char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000237INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000238 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000239
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000240FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
241 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000242}
243
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000244void MachineFunction::verify(Pass *p, const char *Banner) const {
245 MachineVerifier(p, Banner)
246 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000247}
248
Chris Lattner17e9edc2009-08-23 02:51:22 +0000249bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
250 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000251 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000252 std::string ErrorInfo;
253 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
254 raw_fd_ostream::F_Append);
255 if (!ErrorInfo.empty()) {
256 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
257 exit(1);
258 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000259
Chris Lattner17e9edc2009-08-23 02:51:22 +0000260 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000261 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000262 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000263 }
264
265 foundErrors = 0;
266
267 this->MF = &MF;
268 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000269 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000270 TRI = TM->getRegisterInfo();
271 MRI = &MF.getRegInfo();
272
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000273 LiveVars = NULL;
274 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000275 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000276 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000277 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000278 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000279 // We don't want to verify LiveVariables if LiveIntervals is available.
280 if (!LiveInts)
281 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000282 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000283 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000284 }
285
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000286 visitMachineFunctionBefore();
287 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
288 MFI!=MFE; ++MFI) {
289 visitMachineBasicBlockBefore(MFI);
Evan Chengddfd1372011-12-14 02:11:42 +0000290 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
291 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000292 if (MBBI->getParent() != MFI) {
293 report("Bad instruction parent pointer", MFI);
294 *OS << "Instruction: " << *MBBI;
295 continue;
296 }
Evan Chengddfd1372011-12-14 02:11:42 +0000297 // Skip BUNDLE instruction for now. FIXME: We should add code to verify
298 // the BUNDLE's specifically.
299 if (MBBI->isBundle())
300 continue;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000301 visitMachineInstrBefore(MBBI);
302 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
303 visitMachineOperand(&MBBI->getOperand(I), I);
304 visitMachineInstrAfter(MBBI);
305 }
306 visitMachineBasicBlockAfter(MFI);
307 }
308 visitMachineFunctionAfter();
309
Chris Lattner17e9edc2009-08-23 02:51:22 +0000310 if (OutFile)
311 delete OutFile;
312 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000313 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000314
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000315 // Clean up.
316 regsLive.clear();
317 regsDefined.clear();
318 regsDead.clear();
319 regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000320 regMasks.clear();
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000321 regsLiveInButUnused.clear();
322 MBBInfoMap.clear();
323
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000324 return false; // no changes
325}
326
Chris Lattner372fefe2009-08-23 01:03:30 +0000327void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000328 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000329 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000330 if (!foundErrors++) {
331 if (Banner)
332 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000333 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000334 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000335 *OS << "*** Bad machine code: " << msg << " ***\n"
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000336 << "- function: " << MF->getFunction()->getName() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000337}
338
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000339void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000340 assert(MBB);
341 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000342 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000343 << " " << (void*)MBB
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000344 << " (BB#" << MBB->getNumber() << ")";
345 if (Indexes)
346 *OS << " [" << Indexes->getMBBStartIdx(MBB)
347 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
348 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000349}
350
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000351void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000352 assert(MI);
353 report(msg, MI->getParent());
354 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000355 if (Indexes && Indexes->hasIndex(MI))
356 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000357 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000358}
359
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000360void MachineVerifier::report(const char *msg,
361 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000362 assert(MO);
363 report(msg, MO->getParent());
364 *OS << "- operand " << MONum << ": ";
365 MO->print(*OS, TM);
366 *OS << "\n";
367}
368
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000369void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000370 BBInfo &MInfo = MBBInfoMap[MBB];
371 if (!MInfo.reachable) {
372 MInfo.reachable = true;
373 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
374 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
375 markReachable(*SuI);
376 }
377}
378
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000379void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000380 lastIndex = SlotIndex();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000381 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000382
383 // A sub-register of a reserved register is also reserved
384 for (int Reg = regsReserved.find_first(); Reg>=0;
385 Reg = regsReserved.find_next(Reg)) {
Craig Topper9ebfbf82012-03-05 05:37:41 +0000386 for (const uint16_t *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000387 // FIXME: This should probably be:
388 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
389 regsReserved.set(*Sub);
390 }
391 }
Lang Hames03698de2012-02-14 19:17:48 +0000392
393 regsAllocatable = TRI->getAllocatableSet(*MF);
394
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000395 markReachable(&MF->front());
396}
397
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000398// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000399static bool matchPair(MachineBasicBlock::const_succ_iterator i,
400 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000401 if (*i == a)
402 return *++i == b;
403 if (*i == b)
404 return *++i == a;
405 return false;
406}
407
408void
409MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000410 FirstTerminator = 0;
411
Lang Hames03698de2012-02-14 19:17:48 +0000412 if (MRI->isSSA()) {
413 // If this block has allocatable physical registers live-in, check that
414 // it is an entry block or landing pad.
415 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
416 LE = MBB->livein_end();
417 LI != LE; ++LI) {
418 unsigned reg = *LI;
419 if (isAllocatable(reg) && !MBB->isLandingPad() &&
420 MBB != MBB->getParent()->begin()) {
421 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
422 }
423 }
424 }
425
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000426 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000427 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000428 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000429 E = MBB->succ_end(); I != E; ++I) {
430 if ((*I)->isLandingPad())
431 LandingPadSuccs.insert(*I);
432 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000433
434 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
435 const BasicBlock *BB = MBB->getBasicBlock();
436 if (LandingPadSuccs.size() > 1 &&
437 !(AsmInfo &&
438 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
439 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000440 report("MBB has more than one landing pad successor", MBB);
441
Dan Gohman27920592009-08-27 02:43:49 +0000442 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
443 MachineBasicBlock *TBB = 0, *FBB = 0;
444 SmallVector<MachineOperand, 4> Cond;
445 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
446 TBB, FBB, Cond)) {
447 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
448 // check whether its answers match up with reality.
449 if (!TBB && !FBB) {
450 // Block falls through to its successor.
451 MachineFunction::const_iterator MBBI = MBB;
452 ++MBBI;
453 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000454 // It's possible that the block legitimately ends with a noreturn
455 // call or an unreachable, in which case it won't actually fall
456 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000457 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000458 // It's possible that the block legitimately ends with a noreturn
459 // call or an unreachable, in which case it won't actuall fall
460 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000461 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000462 report("MBB exits via unconditional fall-through but doesn't have "
463 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000464 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000465 report("MBB exits via unconditional fall-through but its successor "
466 "differs from its CFG successor!", MBB);
467 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000468 if (!MBB->empty() && MBB->back().isBarrier() &&
Evan Cheng86050dc2010-06-18 23:09:54 +0000469 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000470 report("MBB exits via unconditional fall-through but ends with a "
471 "barrier instruction!", MBB);
472 }
473 if (!Cond.empty()) {
474 report("MBB exits via unconditional fall-through but has a condition!",
475 MBB);
476 }
477 } else if (TBB && !FBB && Cond.empty()) {
478 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000479 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000480 report("MBB exits via unconditional branch but doesn't have "
481 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000482 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000483 report("MBB exits via unconditional branch but the CFG "
484 "successor doesn't match the actual successor!", MBB);
485 }
486 if (MBB->empty()) {
487 report("MBB exits via unconditional branch but doesn't contain "
488 "any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000489 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000490 report("MBB exits via unconditional branch but doesn't end with a "
491 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000492 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000493 report("MBB exits via unconditional branch but the branch isn't a "
494 "terminator instruction!", MBB);
495 }
496 } else if (TBB && !FBB && !Cond.empty()) {
497 // Block conditionally branches somewhere, otherwise falls through.
498 MachineFunction::const_iterator MBBI = MBB;
499 ++MBBI;
500 if (MBBI == MF->end()) {
501 report("MBB conditionally falls through out of function!", MBB);
502 } if (MBB->succ_size() != 2) {
503 report("MBB exits via conditional branch/fall-through but doesn't have "
504 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000505 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000506 report("MBB exits via conditional branch/fall-through but the CFG "
507 "successors don't match the actual successors!", MBB);
508 }
509 if (MBB->empty()) {
510 report("MBB exits via conditional branch/fall-through but doesn't "
511 "contain any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000512 } else if (MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000513 report("MBB exits via conditional branch/fall-through but ends with a "
514 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000515 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000516 report("MBB exits via conditional branch/fall-through but the branch "
517 "isn't a terminator instruction!", MBB);
518 }
519 } else if (TBB && FBB) {
520 // Block conditionally branches somewhere, otherwise branches
521 // somewhere else.
522 if (MBB->succ_size() != 2) {
523 report("MBB exits via conditional branch/branch but doesn't have "
524 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000525 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000526 report("MBB exits via conditional branch/branch but the CFG "
527 "successors don't match the actual successors!", MBB);
528 }
529 if (MBB->empty()) {
530 report("MBB exits via conditional branch/branch but doesn't "
531 "contain any instructions!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000532 } else if (!MBB->back().isBarrier()) {
Dan Gohman27920592009-08-27 02:43:49 +0000533 report("MBB exits via conditional branch/branch but doesn't end with a "
534 "barrier instruction!", MBB);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000535 } else if (!MBB->back().isTerminator()) {
Dan Gohman27920592009-08-27 02:43:49 +0000536 report("MBB exits via conditional branch/branch but the branch "
537 "isn't a terminator instruction!", MBB);
538 }
539 if (Cond.empty()) {
540 report("MBB exits via conditinal branch/branch but there's no "
541 "condition!", MBB);
542 }
543 } else {
544 report("AnalyzeBranch returned invalid data!", MBB);
545 }
546 }
547
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000548 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000549 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000550 E = MBB->livein_end(); I != E; ++I) {
551 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
552 report("MBB live-in list contains non-physical register", MBB);
553 continue;
554 }
555 regsLive.insert(*I);
Craig Topper9ebfbf82012-03-05 05:37:41 +0000556 for (const uint16_t *R = TRI->getSubRegisters(*I); *R; R++)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000557 regsLive.insert(*R);
558 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000559 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000560
561 const MachineFrameInfo *MFI = MF->getFrameInfo();
562 assert(MFI && "Function has no frame info");
563 BitVector PR = MFI->getPristineRegs(MBB);
564 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
565 regsLive.insert(I);
Craig Topper9ebfbf82012-03-05 05:37:41 +0000566 for (const uint16_t *R = TRI->getSubRegisters(I); *R; R++)
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000567 regsLive.insert(*R);
568 }
569
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000570 regsKilled.clear();
571 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000572
573 if (Indexes)
574 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000575}
576
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000577void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000578 const MCInstrDesc &MCID = MI->getDesc();
579 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000580 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000581 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000582 << MI->getNumExplicitOperands() << " given.\n";
583 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000584
585 // Check the MachineMemOperands for basic consistency.
586 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
587 E = MI->memoperands_end(); I != E; ++I) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000588 if ((*I)->isLoad() && !MI->mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000589 report("Missing mayLoad flag", MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000590 if ((*I)->isStore() && !MI->mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000591 report("Missing mayStore flag", MI);
592 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000593
594 // Debug values must not have a slot index.
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000595 // Other instructions must have one, unless they are inside a bundle.
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000596 if (LiveInts) {
597 bool mapped = !LiveInts->isNotInMIMap(MI);
598 if (MI->isDebugValue()) {
599 if (mapped)
600 report("Debug instruction has a slot index", MI);
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +0000601 } else if (MI->isInsideBundle()) {
602 if (mapped)
603 report("Instruction inside bundle has a slot index", MI);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000604 } else {
605 if (!mapped)
606 report("Missing slot index", MI);
607 }
608 }
609
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000610 // Ensure non-terminators don't follow terminators.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000611 if (MI->isTerminator()) {
Jakob Stoklund Olesen5adc07e2011-09-23 22:45:39 +0000612 if (!FirstTerminator)
613 FirstTerminator = MI;
614 } else if (FirstTerminator) {
615 report("Non-terminator instruction after the first terminator", MI);
616 *OS << "First terminator was:\t" << *FirstTerminator;
617 }
618
Andrew Trick3be654f2011-09-21 02:20:46 +0000619 StringRef ErrorInfo;
620 if (!TII->verifyInstruction(MI, ErrorInfo))
621 report(ErrorInfo.data(), MI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000622}
623
624void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000625MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000626 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000627 const MCInstrDesc &MCID = MI->getDesc();
628 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000629
Evan Chenge837dea2011-06-28 19:10:37 +0000630 // The first MCID.NumDefs operands must be explicit register defines
631 if (MONum < MCID.getNumDefs()) {
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000632 if (!MO->isReg())
633 report("Explicit definition must be a register", MO, MONum);
634 else if (!MO->isDef())
635 report("Explicit definition marked as use", MO, MONum);
636 else if (MO->isImplicit())
637 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000638 } else if (MONum < MCID.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000639 // Don't check if it's the last operand in a variadic instruction. See,
640 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000641 if (MO->isReg() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000642 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
Evan Chenge837dea2011-06-28 19:10:37 +0000643 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000644 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000645 if (MO->isImplicit())
646 report("Explicit operand marked as implicit", MO, MONum);
647 }
648 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000649 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000650 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000651 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000652 }
653
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000654 switch (MO->getType()) {
655 case MachineOperand::MO_Register: {
656 const unsigned Reg = MO->getReg();
657 if (!Reg)
658 return;
659
660 // Check Live Variables.
Cameron Zwarich8ec88ba2010-12-20 00:08:10 +0000661 if (MI->isDebugValue()) {
662 // Liveness checks are not valid for debug values.
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000663 } else if (MO->isUse() && !MO->isUndef()) {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000664 regsLiveInButUnused.erase(Reg);
665
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000666 bool isKill = false;
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000667 unsigned defIdx;
668 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
669 // A two-addr use counts as a kill if use and def are the same.
670 unsigned DefReg = MI->getOperand(defIdx).getReg();
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000671 if (Reg == DefReg)
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000672 isKill = true;
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000673 else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000674 report("Two-address instruction operands must be identical",
675 MO, MONum);
676 }
677 } else
678 isKill = MO->isKill();
679
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000680 if (isKill)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000681 addRegWithSubRegs(regsKilled, Reg);
682
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000683 // Check that LiveVars knows this kill.
684 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
685 MO->isKill()) {
686 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
687 if (std::find(VI.Kills.begin(),
688 VI.Kills.end(), MI) == VI.Kills.end())
689 report("Kill missing from LiveVariables", MO, MONum);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000690 }
691
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000692 // Check LiveInts liveness and kill.
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000693 if (TargetRegisterInfo::isVirtualRegister(Reg) &&
694 LiveInts && !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000695 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getRegSlot(true);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000696 if (LiveInts->hasInterval(Reg)) {
697 const LiveInterval &LI = LiveInts->getInterval(Reg);
698 if (!LI.liveAt(UseIdx)) {
699 report("No live range at use", MO, MONum);
700 *OS << UseIdx << " is not live in " << LI << '\n';
701 }
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000702 // Check for extra kill flags.
703 // Note that we allow missing kill flags for now.
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000704 if (MO->isKill() && !LI.killedAt(UseIdx.getRegSlot())) {
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000705 report("Live range continues after kill flag", MO, MONum);
706 *OS << "Live range: " << LI << '\n';
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000707 }
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000708 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000709 report("Virtual register has no Live interval", MO, MONum);
710 }
711 }
712
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000713 // Use of a dead register.
714 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000715 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen4af0f5f2011-07-30 00:57:25 +0000716 // Reserved registers may be used even when 'dead'.
717 if (!isReserved(Reg))
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000718 report("Using an undefined physical register", MO, MONum);
719 } else {
720 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
721 // We don't know which virtual registers are live in, so only complain
722 // if vreg was killed in this MBB. Otherwise keep track of vregs that
723 // must be live in. PHI instructions are handled separately.
724 if (MInfo.regsKilled.count(Reg))
725 report("Using a killed virtual register", MO, MONum);
Chris Lattner518bb532010-02-09 19:54:29 +0000726 else if (!MI->isPHI())
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000727 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
728 }
Duncan Sandse5567202009-05-16 03:28:54 +0000729 }
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000730 } else if (MO->isDef()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000731 // Register defined.
732 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000733 if (MO->isDead())
734 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000735 else
736 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000737
Jakob Stoklund Olesen93e6f022011-07-29 23:02:48 +0000738 // Verify SSA form.
739 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
740 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
741 report("Multiple virtual register defs in SSA form", MO, MONum);
742
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000743 // Check LiveInts for a live range, but only for virtual registers.
744 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
745 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000746 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getRegSlot();
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000747 if (LiveInts->hasInterval(Reg)) {
748 const LiveInterval &LI = LiveInts->getInterval(Reg);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000749 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
750 assert(VNI && "NULL valno is not allowed");
Cameron Zwarich1b031dd2010-12-19 23:50:53 +0000751 if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000752 report("Inconsistent valno->def", MO, MONum);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000753 *OS << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000754 << DefIdx << " in " << LI << '\n';
755 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000756 } else {
757 report("No live range at def", MO, MONum);
758 *OS << DefIdx << " is not live in " << LI << '\n';
759 }
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000760 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000761 report("Virtual register has no Live interval", MO, MONum);
762 }
763 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000764 }
765
766 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000767 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000768 unsigned SubIdx = MO->getSubReg();
769
770 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000771 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000772 report("Illegal subregister index for physical register", MO, MONum);
773 return;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000774 }
Evan Chenge837dea2011-06-28 19:10:37 +0000775 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000776 if (!DRC->contains(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000777 report("Illegal physical register for instruction", MO, MONum);
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000778 *OS << TRI->getName(Reg) << " is not a "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000779 << DRC->getName() << " register.\n";
780 }
781 }
782 } else {
783 // Virtual register.
784 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
785 if (SubIdx) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000786 const TargetRegisterClass *SRC =
787 TRI->getSubClassWithSubReg(RC, SubIdx);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000788 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000789 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000790 *OS << "Register class " << RC->getName()
791 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000792 return;
793 }
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000794 if (RC != SRC) {
795 report("Invalid register class for subregister index", MO, MONum);
796 *OS << "Register class " << RC->getName()
797 << " does not fully support subreg index " << SubIdx << "\n";
798 return;
799 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000800 }
Evan Chenge837dea2011-06-28 19:10:37 +0000801 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenb4a02212011-10-05 22:12:57 +0000802 if (SubIdx) {
803 const TargetRegisterClass *SuperRC =
804 TRI->getLargestLegalSuperClass(RC);
805 if (!SuperRC) {
806 report("No largest legal super class exists.", MO, MONum);
807 return;
808 }
809 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
810 if (!DRC) {
811 report("No matching super-reg register class.", MO, MONum);
812 return;
813 }
814 }
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000815 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000816 report("Illegal virtual register for instruction", MO, MONum);
817 *OS << "Expected a " << DRC->getName() << " register, but got a "
818 << RC->getName() << " register\n";
819 }
820 }
821 }
822 }
823 break;
824 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000825
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000826 case MachineOperand::MO_RegisterMask:
827 regMasks.push_back(MO->getRegMask());
828 break;
829
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000830 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000831 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
832 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000833 break;
834
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000835 case MachineOperand::MO_FrameIndex:
836 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
837 LiveInts && !LiveInts->isNotInMIMap(MI)) {
838 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
839 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000840 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000841 report("Instruction loads from dead spill slot", MO, MONum);
842 *OS << "Live stack: " << LI << '\n';
843 }
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000844 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000845 report("Instruction stores to dead spill slot", MO, MONum);
846 *OS << "Live stack: " << LI << '\n';
847 }
848 }
849 break;
850
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000851 default:
852 break;
853 }
854}
855
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000856void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000857 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
858 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000859 set_subtract(regsLive, regsKilled); regsKilled.clear();
Jakob Stoklund Olesen9ca12d22012-02-28 01:42:41 +0000860 // Kill any masked registers.
861 while (!regMasks.empty()) {
862 const uint32_t *Mask = regMasks.pop_back_val();
863 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
864 if (TargetRegisterInfo::isPhysicalRegister(*I) &&
865 MachineOperand::clobbersPhysReg(Mask, *I))
866 regsDead.push_back(*I);
867 }
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000868 set_subtract(regsLive, regsDead); regsDead.clear();
869 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000870
871 if (Indexes && Indexes->hasIndex(MI)) {
872 SlotIndex idx = Indexes->getInstructionIndex(MI);
873 if (!(idx > lastIndex)) {
874 report("Instruction index out of order", MI);
875 *OS << "Last instruction was at " << lastIndex << '\n';
876 }
877 lastIndex = idx;
878 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000879}
880
881void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000882MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000883 MBBInfoMap[MBB].regsLiveOut = regsLive;
884 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000885
886 if (Indexes) {
887 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
888 if (!(stop > lastIndex)) {
889 report("Block ends before last instruction index", MBB);
890 *OS << "Block ends at " << stop
891 << " last instruction was at " << lastIndex << '\n';
892 }
893 lastIndex = stop;
894 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000895}
896
897// Calculate the largest possible vregsPassed sets. These are the registers that
898// can pass through an MBB live, but may not be live every time. It is assumed
899// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000900void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000901 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
902 // have any vregsPassed.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +0000903 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000904 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
905 MFI != MFE; ++MFI) {
906 const MachineBasicBlock &MBB(*MFI);
907 BBInfo &MInfo = MBBInfoMap[&MBB];
908 if (!MInfo.reachable)
909 continue;
910 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
911 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
912 BBInfo &SInfo = MBBInfoMap[*SuI];
913 if (SInfo.addPassed(MInfo.regsLiveOut))
914 todo.insert(*SuI);
915 }
916 }
917
918 // Iteratively push vregsPassed to successors. This will converge to the same
919 // final state regardless of DenseSet iteration order.
920 while (!todo.empty()) {
921 const MachineBasicBlock *MBB = *todo.begin();
922 todo.erase(MBB);
923 BBInfo &MInfo = MBBInfoMap[MBB];
924 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
925 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
926 if (*SuI == MBB)
927 continue;
928 BBInfo &SInfo = MBBInfoMap[*SuI];
929 if (SInfo.addPassed(MInfo.vregsPassed))
930 todo.insert(*SuI);
931 }
932 }
933}
934
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000935// Calculate the set of virtual registers that must be passed through each basic
936// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000937// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000938void MachineVerifier::calcRegsRequired() {
939 // First push live-in regs to predecessors' vregsRequired.
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +0000940 SmallPtrSet<const MachineBasicBlock*, 8> todo;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000941 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
942 MFI != MFE; ++MFI) {
943 const MachineBasicBlock &MBB(*MFI);
944 BBInfo &MInfo = MBBInfoMap[&MBB];
945 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
946 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
947 BBInfo &PInfo = MBBInfoMap[*PrI];
948 if (PInfo.addRequired(MInfo.vregsLiveIn))
949 todo.insert(*PrI);
950 }
951 }
952
953 // Iteratively push vregsRequired to predecessors. This will converge to the
954 // same final state regardless of DenseSet iteration order.
955 while (!todo.empty()) {
956 const MachineBasicBlock *MBB = *todo.begin();
957 todo.erase(MBB);
958 BBInfo &MInfo = MBBInfoMap[MBB];
959 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
960 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
961 if (*PrI == MBB)
962 continue;
963 BBInfo &SInfo = MBBInfoMap[*PrI];
964 if (SInfo.addRequired(MInfo.vregsRequired))
965 todo.insert(*PrI);
966 }
967 }
968}
969
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000970// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000971// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000972void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +0000973 SmallPtrSet<const MachineBasicBlock*, 8> seen;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000974 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000975 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen1efd6b92012-03-10 00:36:04 +0000976 seen.clear();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000977
978 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
979 unsigned Reg = BBI->getOperand(i).getReg();
980 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
981 if (!Pre->isSuccessor(MBB))
982 continue;
983 seen.insert(Pre);
984 BBInfo &PrInfo = MBBInfoMap[Pre];
985 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
986 report("PHI operand is not live-out from predecessor",
987 &BBI->getOperand(i), i);
988 }
989
990 // Did we see all predecessors?
991 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
992 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
993 if (!seen.count(*PrI)) {
994 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000995 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000996 << " is a predecessor according to the CFG.\n";
997 }
998 }
999 }
1000}
1001
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +00001002void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +00001003 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001004
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001005 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1006 MFI != MFE; ++MFI) {
1007 BBInfo &MInfo = MBBInfoMap[MFI];
1008
1009 // Skip unreachable MBBs.
1010 if (!MInfo.reachable)
1011 continue;
1012
1013 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001014 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001015
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001016 // Now check liveness info if available
1017 if (LiveVars || LiveInts)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001018 calcRegsRequired();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001019 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001020 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001021 if (LiveInts)
1022 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +00001023}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001024
1025void MachineVerifier::verifyLiveVariables() {
1026 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +00001027 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1028 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001029 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1030 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
1031 MFI != MFE; ++MFI) {
1032 BBInfo &MInfo = MBBInfoMap[MFI];
1033
1034 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1035 if (MInfo.vregsRequired.count(Reg)) {
1036 if (!VI.AliveBlocks.test(MFI->getNumber())) {
1037 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001038 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001039 << " must be live through the block.\n";
1040 }
1041 } else {
1042 if (VI.AliveBlocks.test(MFI->getNumber())) {
1043 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +00001044 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001045 << " is not needed live through the block.\n";
1046 }
1047 }
1048 }
1049 }
1050}
1051
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001052void MachineVerifier::verifyLiveIntervals() {
1053 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
1054 for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
1055 LVE = LiveInts->end(); LVI != LVE; ++LVI) {
1056 const LiveInterval &LI = *LVI->second;
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +00001057
1058 // Spilling and splitting may leave unused registers around. Skip them.
1059 if (MRI->use_empty(LI.reg))
1060 continue;
1061
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +00001062 // Physical registers have much weirdness going on, mostly from coalescing.
1063 // We should probably fix it, but for now just ignore them.
1064 if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
1065 continue;
1066
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001067 assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
1068
1069 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1070 I!=E; ++I) {
1071 VNInfo *VNI = *I;
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001072 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001073
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001074 if (!DefVNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001075 if (!VNI->isUnused()) {
1076 report("Valno not live at def and not marked unused", MF);
1077 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1078 }
1079 continue;
1080 }
1081
1082 if (VNI->isUnused())
1083 continue;
1084
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001085 if (DefVNI != VNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001086 report("Live range at def has different valno", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001087 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001088 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001089 continue;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001090 }
1091
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001092 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1093 if (!MBB) {
1094 report("Invalid definition index", MF);
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001095 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1096 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001097 continue;
1098 }
1099
1100 if (VNI->isPHIDef()) {
1101 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1102 report("PHIDef value is not defined at MBB start", MF);
1103 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001104 << ", not at the beginning of BB#" << MBB->getNumber()
1105 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001106 }
1107 } else {
1108 // Non-PHI def.
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +00001109 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001110 if (!MI) {
1111 report("No instruction at def index", MF);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001112 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1113 << " in " << LI << '\n';
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001114 continue;
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001115 }
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001116
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001117 bool hasDef = false;
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001118 bool isEarlyClobber = false;
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +00001119 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001120 if (!MOI->isReg() || !MOI->isDef())
1121 continue;
1122 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1123 if (MOI->getReg() != LI.reg)
1124 continue;
1125 } else {
1126 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1127 !TRI->regsOverlap(LI.reg, MOI->getReg()))
1128 continue;
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001129 }
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001130 hasDef = true;
1131 if (MOI->isEarlyClobber())
1132 isEarlyClobber = true;
1133 }
1134
1135 if (!hasDef) {
1136 report("Defining instruction does not modify register", MI);
1137 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001138 }
1139
1140 // Early clobber defs begin at USE slots, but other defs must begin at
1141 // DEF slots.
1142 if (isEarlyClobber) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001143 if (!VNI->def.isEarlyClobber()) {
1144 report("Early clobber def must be at an early-clobber slot", MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001145 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1146 << " in " << LI << '\n';
1147 }
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001148 } else if (!VNI->def.isRegister()) {
1149 report("Non-PHI, non-early clobber def must be at a register slot",
1150 MF);
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001151 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1152 << " in " << LI << '\n';
1153 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001154 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001155 }
1156
1157 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001158 const VNInfo *VNI = I->valno;
1159 assert(VNI && "Live range has no valno");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001160
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001161 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001162 report("Foreign valno in live range", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001163 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001164 *OS << " has a valno not in " << LI << '\n';
1165 }
1166
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001167 if (VNI->isUnused()) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001168 report("Live range valno is marked unused", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001169 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001170 *OS << " in " << LI << '\n';
1171 }
1172
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001173 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1174 if (!MBB) {
1175 report("Bad start of live segment, no basic block", MF);
1176 I->print(*OS);
1177 *OS << " in " << LI << '\n';
1178 continue;
1179 }
1180 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1181 if (I->start != MBBStartIdx && I->start != VNI->def) {
1182 report("Live segment must begin at MBB entry or valno def", MBB);
1183 I->print(*OS);
1184 *OS << " in " << LI << '\n' << "Basic block starts at "
1185 << MBBStartIdx << '\n';
1186 }
1187
1188 const MachineBasicBlock *EndMBB =
1189 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1190 if (!EndMBB) {
1191 report("Bad end of live segment, no basic block", MF);
1192 I->print(*OS);
1193 *OS << " in " << LI << '\n';
1194 continue;
1195 }
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001196
1197 // No more checks for live-out segments.
1198 if (I->end == LiveInts->getMBBEndIdx(EndMBB))
1199 continue;
1200
1201 // The live segment is ending inside EndMBB
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +00001202 const MachineInstr *MI =
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001203 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1204 if (!MI) {
1205 report("Live segment doesn't end at a valid instruction", EndMBB);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001206 I->print(*OS);
1207 *OS << " in " << LI << '\n' << "Basic block starts at "
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001208 << MBBStartIdx << '\n';
1209 continue;
1210 }
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001211
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001212 // The block slot must refer to a basic block boundary.
1213 if (I->end.isBlock()) {
1214 report("Live segment ends at B slot of an instruction", MI);
1215 I->print(*OS);
1216 *OS << " in " << LI << '\n';
1217 }
1218
1219 if (I->end.isDead()) {
1220 // Segment ends on the dead slot.
1221 // That means there must be a dead def.
1222 if (!SlotIndex::isSameInstr(I->start, I->end)) {
1223 report("Live segment ending at dead slot spans instructions", MI);
1224 I->print(*OS);
1225 *OS << " in " << LI << '\n';
1226 }
1227 }
1228
1229 // A live segment can only end at an early-clobber slot if it is being
1230 // redefined by an early-clobber def.
1231 if (I->end.isEarlyClobber()) {
1232 if (I+1 == E || (I+1)->start != I->end) {
1233 report("Live segment ending at early clobber slot must be "
1234 "redefined by an EC def in the same instruction", MI);
1235 I->print(*OS);
1236 *OS << " in " << LI << '\n';
1237 }
1238 }
1239
1240 // The following checks only apply to virtual registers. Physreg liveness
1241 // is too weird to check.
1242 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1243 // A live range can end with either a redefinition, a kill flag on a
1244 // use, or a dead flag on a def.
1245 bool hasRead = false;
1246 bool hasDeadDef = false;
Jakob Stoklund Olesen30e98a02012-02-29 00:33:41 +00001247 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) {
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001248 if (!MOI->isReg() || MOI->getReg() != LI.reg)
1249 continue;
1250 if (MOI->readsReg())
1251 hasRead = true;
1252 if (MOI->isDef() && MOI->isDead())
1253 hasDeadDef = true;
1254 }
1255
1256 if (I->end.isDead()) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001257 if (!hasDeadDef) {
Jakob Stoklund Olesen121b1792012-02-27 18:24:30 +00001258 report("Instruction doesn't have a dead def operand", MI);
1259 I->print(*OS);
1260 *OS << " in " << LI << '\n';
1261 }
1262 } else {
1263 if (!hasRead) {
1264 report("Instruction ending live range doesn't read the register",
1265 MI);
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001266 I->print(*OS);
1267 *OS << " in " << LI << '\n';
1268 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001269 }
1270 }
1271
1272 // Now check all the basic blocks in this live segment.
1273 MachineFunction::const_iterator MFI = MBB;
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001274 // Is this live range the beginning of a non-PHIDef VN?
1275 if (I->start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001276 // Not live-in to any blocks.
1277 if (MBB == EndMBB)
1278 continue;
1279 // Skip this block.
1280 ++MFI;
1281 }
1282 for (;;) {
1283 assert(LiveInts->isLiveInToMBB(LI, MFI));
Jakob Stoklund Olesene459d552010-10-26 16:49:23 +00001284 // We don't know how to track physregs into a landing pad.
1285 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1286 MFI->isLandingPad()) {
1287 if (&*MFI == EndMBB)
1288 break;
1289 ++MFI;
1290 continue;
1291 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001292 // Check that VNI is live-out of all predecessors.
1293 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1294 PE = MFI->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001295 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1296 const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001297
Jakob Stoklund Olesendf8412c2011-09-15 05:16:30 +00001298 if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI))
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001299 continue;
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001300
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001301 if (!PVNI) {
1302 report("Register not marked live out of predecessor", *PI);
1303 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +00001304 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001305 << PEnd << " in " << LI << '\n';
1306 continue;
1307 }
1308
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001309 if (PVNI != VNI) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001310 report("Different value live out of predecessor", *PI);
1311 *OS << "Valno #" << PVNI->id << " live out of BB#"
1312 << (*PI)->getNumber() << '@' << PEnd
1313 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1314 << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
1315 }
1316 }
1317 if (&*MFI == EndMBB)
1318 break;
1319 ++MFI;
1320 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001321 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001322
1323 // Check the LI only has one connected component.
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001324 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1325 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1326 unsigned NumComp = ConEQ.Classify(&LI);
1327 if (NumComp > 1) {
1328 report("Multiple connected components in live interval", MF);
1329 *OS << NumComp << " components in " << LI << '\n';
Jakob Stoklund Olesencb367772010-10-29 00:40:57 +00001330 for (unsigned comp = 0; comp != NumComp; ++comp) {
1331 *OS << comp << ": valnos";
1332 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1333 E = LI.vni_end(); I!=E; ++I)
1334 if (comp == ConEQ.getEqClass(*I))
1335 *OS << ' ' << (*I)->id;
1336 *OS << '\n';
1337 }
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001338 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001339 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001340 }
1341}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001342