blob: f798c1346e0ea06063319f9bf7e4959bce7924a6 [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 Olesen48872e02009-05-16 00:33:53 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman2dbc4c82009-10-07 17:36:00 +000033#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
35#include "llvm/CodeGen/Passes.h"
Bill Wendlingd29052b2011-05-04 22:54:05 +000036#include "llvm/MC/MCAsmInfo.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000037#include "llvm/Target/TargetMachine.h"
38#include "llvm/Target/TargetRegisterInfo.h"
39#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnercf143a42009-08-23 03:13:20 +000040#include "llvm/ADT/DenseSet.h"
41#include "llvm/ADT/SetOperations.h"
42#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000043#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000044#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000046using namespace llvm;
47
48namespace {
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000049 struct MachineVerifier {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000050
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000051 MachineVerifier(Pass *pass, const char *b) :
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000052 PASS(pass),
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000053 Banner(b),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000054 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000055 {}
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000056
57 bool runOnMachineFunction(MachineFunction &MF);
58
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +000059 Pass *const PASS;
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +000060 const char *Banner;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000061 const char *const OutFileName;
Chris Lattner17e9edc2009-08-23 02:51:22 +000062 raw_ostream *OS;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000063 const MachineFunction *MF;
64 const TargetMachine *TM;
Evan Cheng15993f82011-06-27 21:26:13 +000065 const TargetInstrInfo *TII;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000066 const TargetRegisterInfo *TRI;
67 const MachineRegisterInfo *MRI;
68
69 unsigned foundErrors;
70
71 typedef SmallVector<unsigned, 16> RegVector;
72 typedef DenseSet<unsigned> RegSet;
73 typedef DenseMap<unsigned, const MachineInstr*> RegMap;
74
75 BitVector regsReserved;
76 RegSet regsLive;
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +000077 RegVector regsDefined, regsDead, regsKilled;
78 RegSet regsLiveInButUnused;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000079
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +000080 SlotIndex lastIndex;
81
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000082 // Add Reg and any sub-registers to RV
83 void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
84 RV.push_back(Reg);
85 if (TargetRegisterInfo::isPhysicalRegister(Reg))
86 for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
87 RV.push_back(*R);
88 }
89
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000090 struct BBInfo {
91 // Is this MBB reachable from the MF entry point?
92 bool reachable;
93
94 // Vregs that must be live in because they are used without being
95 // defined. Map value is the user.
96 RegMap vregsLiveIn;
97
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000098 // Regs killed in MBB. They may be defined again, and will then be in both
99 // regsKilled and regsLiveOut.
100 RegSet regsKilled;
101
102 // Regs defined in MBB and live out. Note that vregs passing through may
103 // be live out without being mentioned here.
104 RegSet regsLiveOut;
105
106 // Vregs that pass through MBB untouched. This set is disjoint from
107 // regsKilled and regsLiveOut.
108 RegSet vregsPassed;
109
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000110 // Vregs that must pass through MBB because they are needed by a successor
111 // block. This set is disjoint from regsLiveOut.
112 RegSet vregsRequired;
113
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000114 BBInfo() : reachable(false) {}
115
116 // Add register to vregsPassed if it belongs there. Return true if
117 // anything changed.
118 bool addPassed(unsigned Reg) {
119 if (!TargetRegisterInfo::isVirtualRegister(Reg))
120 return false;
121 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
122 return false;
123 return vregsPassed.insert(Reg).second;
124 }
125
126 // Same for a full set.
127 bool addPassed(const RegSet &RS) {
128 bool changed = false;
129 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
130 if (addPassed(*I))
131 changed = true;
132 return changed;
133 }
134
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000135 // Add register to vregsRequired if it belongs there. Return true if
136 // anything changed.
137 bool addRequired(unsigned Reg) {
138 if (!TargetRegisterInfo::isVirtualRegister(Reg))
139 return false;
140 if (regsLiveOut.count(Reg))
141 return false;
142 return vregsRequired.insert(Reg).second;
143 }
144
145 // Same for a full set.
146 bool addRequired(const RegSet &RS) {
147 bool changed = false;
148 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
149 if (addRequired(*I))
150 changed = true;
151 return changed;
152 }
153
154 // Same for a full map.
155 bool addRequired(const RegMap &RM) {
156 bool changed = false;
157 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
158 if (addRequired(I->first))
159 changed = true;
160 return changed;
161 }
162
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000163 // Live-out registers are either in regsLiveOut or vregsPassed.
164 bool isLiveOut(unsigned Reg) const {
165 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
166 }
167 };
168
169 // Extra register info per MBB.
170 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
171
172 bool isReserved(unsigned Reg) {
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000173 return Reg < regsReserved.size() && regsReserved.test(Reg);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000174 }
175
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000176 // Analysis information if available
177 LiveVariables *LiveVars;
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +0000178 LiveIntervals *LiveInts;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000179 LiveStacks *LiveStks;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000180 SlotIndexes *Indexes;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000181
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000182 void visitMachineFunctionBefore();
183 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
184 void visitMachineInstrBefore(const MachineInstr *MI);
185 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
186 void visitMachineInstrAfter(const MachineInstr *MI);
187 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
188 void visitMachineFunctionAfter();
189
190 void report(const char *msg, const MachineFunction *MF);
191 void report(const char *msg, const MachineBasicBlock *MBB);
192 void report(const char *msg, const MachineInstr *MI);
193 void report(const char *msg, const MachineOperand *MO, unsigned MONum);
194
195 void markReachable(const MachineBasicBlock *MBB);
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000196 void calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000197 void checkPHIOps(const MachineBasicBlock *MBB);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000198
199 void calcRegsRequired();
200 void verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000201 void verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000202 };
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000203
204 struct MachineVerifierPass : public MachineFunctionPass {
205 static char ID; // Pass ID, replacement for typeid
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000206 const char *const Banner;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000207
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000208 MachineVerifierPass(const char *b = 0)
209 : MachineFunctionPass(ID), Banner(b) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000210 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
211 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000212
213 void getAnalysisUsage(AnalysisUsage &AU) const {
214 AU.setPreservesAll();
215 MachineFunctionPass::getAnalysisUsage(AU);
216 }
217
218 bool runOnMachineFunction(MachineFunction &MF) {
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000219 MF.verify(this, Banner);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000220 return false;
221 }
222 };
223
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000224}
225
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000226char MachineVerifierPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000227INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
Owen Andersonce665bd2010-10-07 22:25:06 +0000228 "Verify generated machine code", false, false)
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000229
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000230FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
231 return new MachineVerifierPass(Banner);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000232}
233
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000234void MachineFunction::verify(Pass *p, const char *Banner) const {
235 MachineVerifier(p, Banner)
236 .runOnMachineFunction(const_cast<MachineFunction&>(*this));
Jakob Stoklund Olesence727d02009-11-13 21:56:09 +0000237}
238
Chris Lattner17e9edc2009-08-23 02:51:22 +0000239bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
240 raw_ostream *OutFile = 0;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000241 if (OutFileName) {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000242 std::string ErrorInfo;
243 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
244 raw_fd_ostream::F_Append);
245 if (!ErrorInfo.empty()) {
246 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
247 exit(1);
248 }
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000249
Chris Lattner17e9edc2009-08-23 02:51:22 +0000250 OS = OutFile;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000251 } else {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000252 OS = &errs();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000253 }
254
255 foundErrors = 0;
256
257 this->MF = &MF;
258 TM = &MF.getTarget();
Evan Cheng15993f82011-06-27 21:26:13 +0000259 TII = TM->getInstrInfo();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000260 TRI = TM->getRegisterInfo();
261 MRI = &MF.getRegInfo();
262
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000263 LiveVars = NULL;
264 LiveInts = NULL;
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000265 LiveStks = NULL;
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000266 Indexes = NULL;
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000267 if (PASS) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000268 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000269 // We don't want to verify LiveVariables if LiveIntervals is available.
270 if (!LiveInts)
271 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000272 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000273 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000274 }
275
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000276 visitMachineFunctionBefore();
277 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
278 MFI!=MFE; ++MFI) {
279 visitMachineBasicBlockBefore(MFI);
280 for (MachineBasicBlock::const_iterator MBBI = MFI->begin(),
281 MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
Jakob Stoklund Olesen7bd46da2011-01-12 21:27:41 +0000282 if (MBBI->getParent() != MFI) {
283 report("Bad instruction parent pointer", MFI);
284 *OS << "Instruction: " << *MBBI;
285 continue;
286 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000287 visitMachineInstrBefore(MBBI);
288 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
289 visitMachineOperand(&MBBI->getOperand(I), I);
290 visitMachineInstrAfter(MBBI);
291 }
292 visitMachineBasicBlockAfter(MFI);
293 }
294 visitMachineFunctionAfter();
295
Chris Lattner17e9edc2009-08-23 02:51:22 +0000296 if (OutFile)
297 delete OutFile;
298 else if (foundErrors)
Chris Lattner75361b62010-04-07 22:58:41 +0000299 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000300
Jakob Stoklund Olesen63496682009-08-08 15:34:50 +0000301 // Clean up.
302 regsLive.clear();
303 regsDefined.clear();
304 regsDead.clear();
305 regsKilled.clear();
306 regsLiveInButUnused.clear();
307 MBBInfoMap.clear();
308
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000309 return false; // no changes
310}
311
Chris Lattner372fefe2009-08-23 01:03:30 +0000312void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000313 assert(MF);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000314 *OS << '\n';
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000315 if (!foundErrors++) {
316 if (Banner)
317 *OS << "# " << Banner << '\n';
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000318 MF->print(*OS, Indexes);
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000319 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000320 *OS << "*** Bad machine code: " << msg << " ***\n"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000321 << "- function: " << MF->getFunction()->getNameStr() << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000322}
323
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000324void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000325 assert(MBB);
326 report(msg, MBB->getParent());
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000327 *OS << "- basic block: " << MBB->getName()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000328 << " " << (void*)MBB
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000329 << " (BB#" << MBB->getNumber() << ")";
330 if (Indexes)
331 *OS << " [" << Indexes->getMBBStartIdx(MBB)
332 << ';' << Indexes->getMBBEndIdx(MBB) << ')';
333 *OS << '\n';
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000334}
335
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000336void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000337 assert(MI);
338 report(msg, MI->getParent());
339 *OS << "- instruction: ";
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000340 if (Indexes && Indexes->hasIndex(MI))
341 *OS << Indexes->getInstructionIndex(MI) << '\t';
Chris Lattner705e07f2009-08-23 03:41:05 +0000342 MI->print(*OS, TM);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000343}
344
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000345void MachineVerifier::report(const char *msg,
346 const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000347 assert(MO);
348 report(msg, MO->getParent());
349 *OS << "- operand " << MONum << ": ";
350 MO->print(*OS, TM);
351 *OS << "\n";
352}
353
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000354void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000355 BBInfo &MInfo = MBBInfoMap[MBB];
356 if (!MInfo.reachable) {
357 MInfo.reachable = true;
358 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
359 SuE = MBB->succ_end(); SuI != SuE; ++SuI)
360 markReachable(*SuI);
361 }
362}
363
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000364void MachineVerifier::visitMachineFunctionBefore() {
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000365 lastIndex = SlotIndex();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000366 regsReserved = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesend37bc5a2009-08-04 19:18:01 +0000367
368 // A sub-register of a reserved register is also reserved
369 for (int Reg = regsReserved.find_first(); Reg>=0;
370 Reg = regsReserved.find_next(Reg)) {
371 for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
372 // FIXME: This should probably be:
373 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
374 regsReserved.set(*Sub);
375 }
376 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000377 markReachable(&MF->front());
378}
379
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000380// Does iterator point to a and b as the first two elements?
Dan Gohmanb3579832010-04-15 17:08:50 +0000381static bool matchPair(MachineBasicBlock::const_succ_iterator i,
382 const MachineBasicBlock *a, const MachineBasicBlock *b) {
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000383 if (*i == a)
384 return *++i == b;
385 if (*i == b)
386 return *++i == a;
387 return false;
388}
389
390void
391MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000392 // Count the number of landing pad successors.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000393 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000394 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
Cameron Zwarich2100d212010-12-20 04:19:48 +0000395 E = MBB->succ_end(); I != E; ++I) {
396 if ((*I)->isLandingPad())
397 LandingPadSuccs.insert(*I);
398 }
Bill Wendlingd29052b2011-05-04 22:54:05 +0000399
400 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
401 const BasicBlock *BB = MBB->getBasicBlock();
402 if (LandingPadSuccs.size() > 1 &&
403 !(AsmInfo &&
404 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
405 BB && isa<SwitchInst>(BB->getTerminator())))
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000406 report("MBB has more than one landing pad successor", MBB);
407
Dan Gohman27920592009-08-27 02:43:49 +0000408 // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
409 MachineBasicBlock *TBB = 0, *FBB = 0;
410 SmallVector<MachineOperand, 4> Cond;
411 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
412 TBB, FBB, Cond)) {
413 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
414 // check whether its answers match up with reality.
415 if (!TBB && !FBB) {
416 // Block falls through to its successor.
417 MachineFunction::const_iterator MBBI = MBB;
418 ++MBBI;
419 if (MBBI == MF->end()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000420 // It's possible that the block legitimately ends with a noreturn
421 // call or an unreachable, in which case it won't actually fall
422 // out the bottom of the function.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000423 } else if (MBB->succ_size() == LandingPadSuccs.size()) {
Dan Gohmana01a80f2009-08-27 18:14:26 +0000424 // It's possible that the block legitimately ends with a noreturn
425 // call or an unreachable, in which case it won't actuall fall
426 // out of the block.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000427 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000428 report("MBB exits via unconditional fall-through but doesn't have "
429 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000430 } else if (!MBB->isSuccessor(MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000431 report("MBB exits via unconditional fall-through but its successor "
432 "differs from its CFG successor!", MBB);
433 }
Evan Cheng86050dc2010-06-18 23:09:54 +0000434 if (!MBB->empty() && MBB->back().getDesc().isBarrier() &&
435 !TII->isPredicated(&MBB->back())) {
Dan Gohman27920592009-08-27 02:43:49 +0000436 report("MBB exits via unconditional fall-through but ends with a "
437 "barrier instruction!", MBB);
438 }
439 if (!Cond.empty()) {
440 report("MBB exits via unconditional fall-through but has a condition!",
441 MBB);
442 }
443 } else if (TBB && !FBB && Cond.empty()) {
444 // Block unconditionally branches somewhere.
Cameron Zwarich2100d212010-12-20 04:19:48 +0000445 if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
Dan Gohman27920592009-08-27 02:43:49 +0000446 report("MBB exits via unconditional branch but doesn't have "
447 "exactly one CFG successor!", MBB);
Jakob Stoklund Olesen0a7bbcb2010-10-21 18:47:06 +0000448 } else if (!MBB->isSuccessor(TBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000449 report("MBB exits via unconditional branch but the CFG "
450 "successor doesn't match the actual successor!", MBB);
451 }
452 if (MBB->empty()) {
453 report("MBB exits via unconditional branch but doesn't contain "
454 "any instructions!", MBB);
455 } else if (!MBB->back().getDesc().isBarrier()) {
456 report("MBB exits via unconditional branch but doesn't end with a "
457 "barrier instruction!", MBB);
458 } else if (!MBB->back().getDesc().isTerminator()) {
459 report("MBB exits via unconditional branch but the branch isn't a "
460 "terminator instruction!", MBB);
461 }
462 } else if (TBB && !FBB && !Cond.empty()) {
463 // Block conditionally branches somewhere, otherwise falls through.
464 MachineFunction::const_iterator MBBI = MBB;
465 ++MBBI;
466 if (MBBI == MF->end()) {
467 report("MBB conditionally falls through out of function!", MBB);
468 } if (MBB->succ_size() != 2) {
469 report("MBB exits via conditional branch/fall-through but doesn't have "
470 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000471 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
Dan Gohman27920592009-08-27 02:43:49 +0000472 report("MBB exits via conditional branch/fall-through but the CFG "
473 "successors don't match the actual successors!", MBB);
474 }
475 if (MBB->empty()) {
476 report("MBB exits via conditional branch/fall-through but doesn't "
477 "contain any instructions!", MBB);
478 } else if (MBB->back().getDesc().isBarrier()) {
479 report("MBB exits via conditional branch/fall-through but ends with a "
480 "barrier instruction!", MBB);
481 } else if (!MBB->back().getDesc().isTerminator()) {
482 report("MBB exits via conditional branch/fall-through but the branch "
483 "isn't a terminator instruction!", MBB);
484 }
485 } else if (TBB && FBB) {
486 // Block conditionally branches somewhere, otherwise branches
487 // somewhere else.
488 if (MBB->succ_size() != 2) {
489 report("MBB exits via conditional branch/branch but doesn't have "
490 "exactly two CFG successors!", MBB);
Jakob Stoklund Olesen1dc0fcb2009-11-13 21:55:54 +0000491 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
Dan Gohman27920592009-08-27 02:43:49 +0000492 report("MBB exits via conditional branch/branch but the CFG "
493 "successors don't match the actual successors!", MBB);
494 }
495 if (MBB->empty()) {
496 report("MBB exits via conditional branch/branch but doesn't "
497 "contain any instructions!", MBB);
498 } else if (!MBB->back().getDesc().isBarrier()) {
499 report("MBB exits via conditional branch/branch but doesn't end with a "
500 "barrier instruction!", MBB);
501 } else if (!MBB->back().getDesc().isTerminator()) {
502 report("MBB exits via conditional branch/branch but the branch "
503 "isn't a terminator instruction!", MBB);
504 }
505 if (Cond.empty()) {
506 report("MBB exits via conditinal branch/branch but there's no "
507 "condition!", MBB);
508 }
509 } else {
510 report("AnalyzeBranch returned invalid data!", MBB);
511 }
512 }
513
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000514 regsLive.clear();
Dan Gohman81bf03e2010-04-13 16:57:55 +0000515 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000516 E = MBB->livein_end(); I != E; ++I) {
517 if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
518 report("MBB live-in list contains non-physical register", MBB);
519 continue;
520 }
521 regsLive.insert(*I);
522 for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
523 regsLive.insert(*R);
524 }
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000525 regsLiveInButUnused = regsLive;
Jakob Stoklund Olesena6b677d2009-08-13 16:19:51 +0000526
527 const MachineFrameInfo *MFI = MF->getFrameInfo();
528 assert(MFI && "Function has no frame info");
529 BitVector PR = MFI->getPristineRegs(MBB);
530 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
531 regsLive.insert(I);
532 for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
533 regsLive.insert(*R);
534 }
535
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000536 regsKilled.clear();
537 regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000538
539 if (Indexes)
540 lastIndex = Indexes->getMBBStartIdx(MBB);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000541}
542
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000543void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
Evan Chenge837dea2011-06-28 19:10:37 +0000544 const MCInstrDesc &MCID = MI->getDesc();
545 if (MI->getNumOperands() < MCID.getNumOperands()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000546 report("Too few operands", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000547 *OS << MCID.getNumOperands() << " operands expected, but "
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000548 << MI->getNumExplicitOperands() << " given.\n";
549 }
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000550
551 // Check the MachineMemOperands for basic consistency.
552 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
553 E = MI->memoperands_end(); I != E; ++I) {
Evan Chenge837dea2011-06-28 19:10:37 +0000554 if ((*I)->isLoad() && !MCID.mayLoad())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000555 report("Missing mayLoad flag", MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000556 if ((*I)->isStore() && !MCID.mayStore())
Dan Gohman2dbc4c82009-10-07 17:36:00 +0000557 report("Missing mayStore flag", MI);
558 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000559
560 // Debug values must not have a slot index.
561 // Other instructions must have one.
562 if (LiveInts) {
563 bool mapped = !LiveInts->isNotInMIMap(MI);
564 if (MI->isDebugValue()) {
565 if (mapped)
566 report("Debug instruction has a slot index", MI);
567 } else {
568 if (!mapped)
569 report("Missing slot index", MI);
570 }
571 }
572
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000573}
574
575void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000576MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000577 const MachineInstr *MI = MO->getParent();
Evan Chenge837dea2011-06-28 19:10:37 +0000578 const MCInstrDesc &MCID = MI->getDesc();
579 const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000580
Evan Chenge837dea2011-06-28 19:10:37 +0000581 // The first MCID.NumDefs operands must be explicit register defines
582 if (MONum < MCID.getNumDefs()) {
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000583 if (!MO->isReg())
584 report("Explicit definition must be a register", MO, MONum);
585 else if (!MO->isDef())
586 report("Explicit definition marked as use", MO, MONum);
587 else if (MO->isImplicit())
588 report("Explicit definition marked as implicit", MO, MONum);
Evan Chenge837dea2011-06-28 19:10:37 +0000589 } else if (MONum < MCID.getNumOperands()) {
Eric Christopher113a06c2010-11-17 00:55:36 +0000590 // Don't check if it's the last operand in a variadic instruction. See,
591 // e.g., LDM_RET in the arm back end.
Evan Chenge837dea2011-06-28 19:10:37 +0000592 if (MO->isReg() &&
593 !(MCID.isVariadic() && MONum == MCID.getNumOperands()-1)) {
594 if (MO->isDef() && !MCOI.isOptionalDef())
Cameron Zwarich22d67cf2010-12-19 21:37:23 +0000595 report("Explicit operand marked as def", MO, MONum);
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000596 if (MO->isImplicit())
597 report("Explicit operand marked as implicit", MO, MONum);
598 }
599 } else {
Jakob Stoklund Olesen57115642009-12-22 21:48:20 +0000600 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
Evan Chenge837dea2011-06-28 19:10:37 +0000601 if (MO->isReg() && !MO->isImplicit() && !MCID.isVariadic() && MO->getReg())
Jakob Stoklund Olesen39523e22009-09-23 20:57:55 +0000602 report("Extra explicit operand on non-variadic instruction", MO, MONum);
Jakob Stoklund Olesen44b27e52009-05-16 07:25:20 +0000603 }
604
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000605 switch (MO->getType()) {
606 case MachineOperand::MO_Register: {
607 const unsigned Reg = MO->getReg();
608 if (!Reg)
609 return;
610
611 // Check Live Variables.
Cameron Zwarich8ec88ba2010-12-20 00:08:10 +0000612 if (MI->isDebugValue()) {
613 // Liveness checks are not valid for debug values.
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000614 } else if (MO->isUse() && !MO->isUndef()) {
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000615 regsLiveInButUnused.erase(Reg);
616
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000617 bool isKill = false;
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000618 unsigned defIdx;
619 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
620 // A two-addr use counts as a kill if use and def are the same.
621 unsigned DefReg = MI->getOperand(defIdx).getReg();
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000622 if (Reg == DefReg)
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000623 isKill = true;
Jakob Stoklund Olesen02ae9f22011-03-31 17:52:41 +0000624 else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesen1b2c7612010-05-14 20:28:32 +0000625 report("Two-address instruction operands must be identical",
626 MO, MONum);
627 }
628 } else
629 isKill = MO->isKill();
630
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000631 if (isKill)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000632 addRegWithSubRegs(regsKilled, Reg);
633
Jakob Stoklund Olesenc910c8d2010-08-05 23:51:26 +0000634 // Check that LiveVars knows this kill.
635 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
636 MO->isKill()) {
637 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
638 if (std::find(VI.Kills.begin(),
639 VI.Kills.end(), MI) == VI.Kills.end())
640 report("Kill missing from LiveVariables", MO, MONum);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000641 }
642
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000643 // Check LiveInts liveness and kill.
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000644 if (TargetRegisterInfo::isVirtualRegister(Reg) &&
645 LiveInts && !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000646 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getUseIndex();
647 if (LiveInts->hasInterval(Reg)) {
648 const LiveInterval &LI = LiveInts->getInterval(Reg);
649 if (!LI.liveAt(UseIdx)) {
650 report("No live range at use", MO, MONum);
651 *OS << UseIdx << " is not live in " << LI << '\n';
652 }
Jakob Stoklund Olesena7b586b2011-02-04 00:39:18 +0000653 // Check for extra kill flags.
654 // Note that we allow missing kill flags for now.
655 if (MO->isKill() && !LI.killedAt(UseIdx.getDefIndex())) {
656 report("Live range continues after kill flag", MO, MONum);
657 *OS << "Live range: " << LI << '\n';
Jakob Stoklund Olesen1c163d22010-11-01 21:51:31 +0000658 }
Jakob Stoklund Olesenab566472010-10-30 01:26:11 +0000659 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000660 report("Virtual register has no Live interval", MO, MONum);
661 }
662 }
663
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000664 // Use of a dead register.
665 if (!regsLive.count(Reg)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000666 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Oleseneeb57c72011-07-29 23:36:21 +0000667 // Reserved registers may be used even when 'dead', but allocatable
668 // registers can't.
669 // We track the liveness of unreserved, unallocatable registers while
670 // the machine function is still in SSA form. That lets us check for
671 // bad EFLAGS uses. After register allocation, the unallocatable
672 // registers are probably quite wrong. For example, the x87 ST0-ST7
673 // registers don't track liveness at all.
674 if (!isReserved(Reg) &&
675 (MRI->isSSA() || TRI->isInAllocatableClass(Reg)))
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000676 report("Using an undefined physical register", MO, MONum);
677 } else {
678 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
679 // We don't know which virtual registers are live in, so only complain
680 // if vreg was killed in this MBB. Otherwise keep track of vregs that
681 // must be live in. PHI instructions are handled separately.
682 if (MInfo.regsKilled.count(Reg))
683 report("Using a killed virtual register", MO, MONum);
Chris Lattner518bb532010-02-09 19:54:29 +0000684 else if (!MI->isPHI())
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000685 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
686 }
Duncan Sandse5567202009-05-16 03:28:54 +0000687 }
Jakob Stoklund Olesen8e53aca2011-03-31 17:23:25 +0000688 } else if (MO->isDef()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000689 // Register defined.
690 // TODO: verify that earlyclobber ops are not used.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000691 if (MO->isDead())
692 addRegWithSubRegs(regsDead, Reg);
Jakob Stoklund Olesen710b13b2009-08-08 13:19:25 +0000693 else
694 addRegWithSubRegs(regsDefined, Reg);
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000695
Jakob Stoklund Olesen93e6f022011-07-29 23:02:48 +0000696 // Verify SSA form.
697 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
698 llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
699 report("Multiple virtual register defs in SSA form", MO, MONum);
700
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000701 // Check LiveInts for a live range, but only for virtual registers.
702 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
703 !LiveInts->isNotInMIMap(MI)) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000704 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getDefIndex();
705 if (LiveInts->hasInterval(Reg)) {
706 const LiveInterval &LI = LiveInts->getInterval(Reg);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000707 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
708 assert(VNI && "NULL valno is not allowed");
Cameron Zwarich1b031dd2010-12-19 23:50:53 +0000709 if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000710 report("Inconsistent valno->def", MO, MONum);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +0000711 *OS << "Valno " << VNI->id << " is not defined at "
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000712 << DefIdx << " in " << LI << '\n';
713 }
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000714 } else {
715 report("No live range at def", MO, MONum);
716 *OS << DefIdx << " is not live in " << LI << '\n';
717 }
Jakob Stoklund Olesen775aa222010-08-06 18:04:14 +0000718 } else {
Jakob Stoklund Olesen1fe9c342010-08-05 22:32:21 +0000719 report("Virtual register has no Live interval", MO, MONum);
720 }
721 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000722 }
723
724 // Check register classes.
Evan Chenge837dea2011-06-28 19:10:37 +0000725 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000726 unsigned SubIdx = MO->getSubReg();
727
728 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
729 unsigned sr = Reg;
730 if (SubIdx) {
731 unsigned s = TRI->getSubReg(Reg, SubIdx);
732 if (!s) {
733 report("Invalid subregister index for physical register",
734 MO, MONum);
735 return;
736 }
737 sr = s;
738 }
Evan Chenge837dea2011-06-28 19:10:37 +0000739 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000740 if (!DRC->contains(sr)) {
741 report("Illegal physical register for instruction", MO, MONum);
742 *OS << TRI->getName(sr) << " is not a "
743 << DRC->getName() << " register.\n";
744 }
745 }
746 } else {
747 // Virtual register.
748 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
749 if (SubIdx) {
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000750 const TargetRegisterClass *SRC = RC->getSubRegisterRegClass(SubIdx);
751 if (!SRC) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000752 report("Invalid subregister index for virtual register", MO, MONum);
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000753 *OS << "Register class " << RC->getName()
754 << " does not support subreg index " << SubIdx << "\n";
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000755 return;
756 }
Jakob Stoklund Olesen6a8d2c62010-05-18 17:31:12 +0000757 RC = SRC;
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000758 }
Evan Chenge837dea2011-06-28 19:10:37 +0000759 if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000760 if (!RC->hasSuperClassEq(DRC)) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000761 report("Illegal virtual register for instruction", MO, MONum);
762 *OS << "Expected a " << DRC->getName() << " register, but got a "
763 << RC->getName() << " register\n";
764 }
765 }
766 }
767 }
768 break;
769 }
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000770
771 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner518bb532010-02-09 19:54:29 +0000772 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
773 report("PHI operand is not in the CFG", MO, MONum);
Jakob Stoklund Olesena5ba07c2009-09-21 07:19:08 +0000774 break;
775
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000776 case MachineOperand::MO_FrameIndex:
777 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
778 LiveInts && !LiveInts->isNotInMIMap(MI)) {
779 LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
780 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
Evan Chenge837dea2011-06-28 19:10:37 +0000781 if (MCID.mayLoad() && !LI.liveAt(Idx.getUseIndex())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000782 report("Instruction loads from dead spill slot", MO, MONum);
783 *OS << "Live stack: " << LI << '\n';
784 }
Evan Chenge837dea2011-06-28 19:10:37 +0000785 if (MCID.mayStore() && !LI.liveAt(Idx.getDefIndex())) {
Jakob Stoklund Olesene8f08232010-11-01 19:49:52 +0000786 report("Instruction stores to dead spill slot", MO, MONum);
787 *OS << "Live stack: " << LI << '\n';
788 }
789 }
790 break;
791
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000792 default:
793 break;
794 }
795}
796
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000797void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000798 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
799 set_union(MInfo.regsKilled, regsKilled);
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000800 set_subtract(regsLive, regsKilled); regsKilled.clear();
801 set_subtract(regsLive, regsDead); regsDead.clear();
802 set_union(regsLive, regsDefined); regsDefined.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000803
804 if (Indexes && Indexes->hasIndex(MI)) {
805 SlotIndex idx = Indexes->getInstructionIndex(MI);
806 if (!(idx > lastIndex)) {
807 report("Instruction index out of order", MI);
808 *OS << "Last instruction was at " << lastIndex << '\n';
809 }
810 lastIndex = idx;
811 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000812}
813
814void
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000815MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000816 MBBInfoMap[MBB].regsLiveOut = regsLive;
817 regsLive.clear();
Jakob Stoklund Olesenfc69c372011-01-12 21:27:48 +0000818
819 if (Indexes) {
820 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
821 if (!(stop > lastIndex)) {
822 report("Block ends before last instruction index", MBB);
823 *OS << "Block ends at " << stop
824 << " last instruction was at " << lastIndex << '\n';
825 }
826 lastIndex = stop;
827 }
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000828}
829
830// Calculate the largest possible vregsPassed sets. These are the registers that
831// can pass through an MBB live, but may not be live every time. It is assumed
832// that all vregsPassed sets are empty before the call.
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000833void MachineVerifier::calcRegsPassed() {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000834 // First push live-out regs to successors' vregsPassed. Remember the MBBs that
835 // have any vregsPassed.
836 DenseSet<const MachineBasicBlock*> todo;
837 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
838 MFI != MFE; ++MFI) {
839 const MachineBasicBlock &MBB(*MFI);
840 BBInfo &MInfo = MBBInfoMap[&MBB];
841 if (!MInfo.reachable)
842 continue;
843 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
844 SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
845 BBInfo &SInfo = MBBInfoMap[*SuI];
846 if (SInfo.addPassed(MInfo.regsLiveOut))
847 todo.insert(*SuI);
848 }
849 }
850
851 // Iteratively push vregsPassed to successors. This will converge to the same
852 // final state regardless of DenseSet iteration order.
853 while (!todo.empty()) {
854 const MachineBasicBlock *MBB = *todo.begin();
855 todo.erase(MBB);
856 BBInfo &MInfo = MBBInfoMap[MBB];
857 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
858 SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
859 if (*SuI == MBB)
860 continue;
861 BBInfo &SInfo = MBBInfoMap[*SuI];
862 if (SInfo.addPassed(MInfo.vregsPassed))
863 todo.insert(*SuI);
864 }
865 }
866}
867
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000868// Calculate the set of virtual registers that must be passed through each basic
869// block in order to satisfy the requirements of successor blocks. This is very
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000870// similar to calcRegsPassed, only backwards.
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000871void MachineVerifier::calcRegsRequired() {
872 // First push live-in regs to predecessors' vregsRequired.
873 DenseSet<const MachineBasicBlock*> todo;
874 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
875 MFI != MFE; ++MFI) {
876 const MachineBasicBlock &MBB(*MFI);
877 BBInfo &MInfo = MBBInfoMap[&MBB];
878 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
879 PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
880 BBInfo &PInfo = MBBInfoMap[*PrI];
881 if (PInfo.addRequired(MInfo.vregsLiveIn))
882 todo.insert(*PrI);
883 }
884 }
885
886 // Iteratively push vregsRequired to predecessors. This will converge to the
887 // same final state regardless of DenseSet iteration order.
888 while (!todo.empty()) {
889 const MachineBasicBlock *MBB = *todo.begin();
890 todo.erase(MBB);
891 BBInfo &MInfo = MBBInfoMap[MBB];
892 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
893 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
894 if (*PrI == MBB)
895 continue;
896 BBInfo &SInfo = MBBInfoMap[*PrI];
897 if (SInfo.addRequired(MInfo.vregsRequired))
898 todo.insert(*PrI);
899 }
900 }
901}
902
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000903// Check PHI instructions at the beginning of MBB. It is assumed that
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000904// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000905void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000906 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
Chris Lattner518bb532010-02-09 19:54:29 +0000907 BBI != BBE && BBI->isPHI(); ++BBI) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000908 DenseSet<const MachineBasicBlock*> seen;
909
910 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
911 unsigned Reg = BBI->getOperand(i).getReg();
912 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
913 if (!Pre->isSuccessor(MBB))
914 continue;
915 seen.insert(Pre);
916 BBInfo &PrInfo = MBBInfoMap[Pre];
917 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
918 report("PHI operand is not live-out from predecessor",
919 &BBI->getOperand(i), i);
920 }
921
922 // Did we see all predecessors?
923 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
924 PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
925 if (!seen.count(*PrI)) {
926 report("Missing PHI operand", BBI);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000927 *OS << "BB#" << (*PrI)->getNumber()
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000928 << " is a predecessor according to the CFG.\n";
929 }
930 }
931 }
932}
933
Jakob Stoklund Olesenb44fad72009-10-04 18:18:39 +0000934void MachineVerifier::visitMachineFunctionAfter() {
Jakob Stoklund Olesenb31defe2010-01-05 20:59:36 +0000935 calcRegsPassed();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000936
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000937 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
938 MFI != MFE; ++MFI) {
939 BBInfo &MInfo = MBBInfoMap[MFI];
940
941 // Skip unreachable MBBs.
942 if (!MInfo.reachable)
943 continue;
944
945 checkPHIOps(MFI);
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000946 }
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000947
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000948 // Now check liveness info if available
949 if (LiveVars || LiveInts)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000950 calcRegsRequired();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000951 if (LiveVars)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000952 verifyLiveVariables();
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000953 if (LiveInts)
954 verifyLiveIntervals();
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000955}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000956
957void MachineVerifier::verifyLiveVariables() {
958 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
Jakob Stoklund Olesen98c54762011-01-08 23:11:02 +0000959 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
960 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000961 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
962 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
963 MFI != MFE; ++MFI) {
964 BBInfo &MInfo = MBBInfoMap[MFI];
965
966 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
967 if (MInfo.vregsRequired.count(Reg)) {
968 if (!VI.AliveBlocks.test(MFI->getNumber())) {
969 report("LiveVariables: Block missing from AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000970 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000971 << " must be live through the block.\n";
972 }
973 } else {
974 if (VI.AliveBlocks.test(MFI->getNumber())) {
975 report("LiveVariables: Block should not be in AliveBlocks", MFI);
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +0000976 *OS << "Virtual register " << PrintReg(Reg)
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +0000977 << " is not needed live through the block.\n";
978 }
979 }
980 }
981 }
982}
983
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000984void MachineVerifier::verifyLiveIntervals() {
985 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
986 for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
987 LVE = LiveInts->end(); LVI != LVE; ++LVI) {
988 const LiveInterval &LI = *LVI->second;
Jakob Stoklund Olesen893ab5d2010-10-06 23:54:35 +0000989
990 // Spilling and splitting may leave unused registers around. Skip them.
991 if (MRI->use_empty(LI.reg))
992 continue;
993
Jakob Stoklund Olesen8c456422010-10-28 20:44:22 +0000994 // Physical registers have much weirdness going on, mostly from coalescing.
995 // We should probably fix it, but for now just ignore them.
996 if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
997 continue;
998
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +0000999 assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
1000
1001 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
1002 I!=E; ++I) {
1003 VNInfo *VNI = *I;
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001004 const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001005
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001006 if (!DefVNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001007 if (!VNI->isUnused()) {
1008 report("Valno not live at def and not marked unused", MF);
1009 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1010 }
1011 continue;
1012 }
1013
1014 if (VNI->isUnused())
1015 continue;
1016
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001017 if (DefVNI != VNI) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001018 report("Live range at def has different valno", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001019 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001020 << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001021 continue;
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001022 }
1023
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001024 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1025 if (!MBB) {
1026 report("Invalid definition index", MF);
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001027 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1028 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001029 continue;
1030 }
1031
1032 if (VNI->isPHIDef()) {
1033 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1034 report("PHIDef value is not defined at MBB start", MF);
1035 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
Jakob Stoklund Olesendbcc2e12010-10-26 20:21:43 +00001036 << ", not at the beginning of BB#" << MBB->getNumber()
1037 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001038 }
1039 } else {
1040 // Non-PHI def.
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001041 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1042 if (!MI) {
1043 report("No instruction at def index", MF);
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001044 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1045 << " in " << LI << '\n';
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001046 } else if (!MI->modifiesRegister(LI.reg, TRI)) {
1047 report("Defining instruction does not modify register", MI);
1048 *OS << "Valno #" << VNI->id << " in " << LI << '\n';
1049 }
Cameron Zwarich0b13d7d2010-12-20 03:15:20 +00001050
1051 bool isEarlyClobber = false;
1052 if (MI) {
1053 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1054 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1055 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() &&
1056 MOI->isEarlyClobber()) {
1057 isEarlyClobber = true;
1058 break;
1059 }
1060 }
1061 }
1062
1063 // Early clobber defs begin at USE slots, but other defs must begin at
1064 // DEF slots.
1065 if (isEarlyClobber) {
1066 if (!VNI->def.isUse()) {
1067 report("Early clobber def must be at a USE slot", MF);
1068 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1069 << " in " << LI << '\n';
1070 }
1071 } else if (!VNI->def.isDef()) {
1072 report("Non-PHI, non-early clobber def must be at a DEF slot", MF);
1073 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
1074 << " in " << LI << '\n';
1075 }
Jakob Stoklund Olesen3bf7cf92010-10-22 22:48:58 +00001076 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001077 }
1078
1079 for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001080 const VNInfo *VNI = I->valno;
1081 assert(VNI && "Live range has no valno");
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001082
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001083 if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001084 report("Foreign valno in live range", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001085 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001086 *OS << " has a valno not in " << LI << '\n';
1087 }
1088
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001089 if (VNI->isUnused()) {
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001090 report("Live range valno is marked unused", MF);
Jakob Stoklund Olesened826352010-10-02 05:24:46 +00001091 I->print(*OS);
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001092 *OS << " in " << LI << '\n';
1093 }
1094
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001095 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
1096 if (!MBB) {
1097 report("Bad start of live segment, no basic block", MF);
1098 I->print(*OS);
1099 *OS << " in " << LI << '\n';
1100 continue;
1101 }
1102 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1103 if (I->start != MBBStartIdx && I->start != VNI->def) {
1104 report("Live segment must begin at MBB entry or valno def", MBB);
1105 I->print(*OS);
1106 *OS << " in " << LI << '\n' << "Basic block starts at "
1107 << MBBStartIdx << '\n';
1108 }
1109
1110 const MachineBasicBlock *EndMBB =
1111 LiveInts->getMBBFromIndex(I->end.getPrevSlot());
1112 if (!EndMBB) {
1113 report("Bad end of live segment, no basic block", MF);
1114 I->print(*OS);
1115 *OS << " in " << LI << '\n';
1116 continue;
1117 }
1118 if (I->end != LiveInts->getMBBEndIdx(EndMBB)) {
1119 // The live segment is ending inside EndMBB
1120 const MachineInstr *MI =
1121 LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
1122 if (!MI) {
1123 report("Live segment doesn't end at a valid instruction", EndMBB);
1124 I->print(*OS);
1125 *OS << " in " << LI << '\n' << "Basic block starts at "
1126 << MBBStartIdx << '\n';
1127 } else if (TargetRegisterInfo::isVirtualRegister(LI.reg) &&
1128 !MI->readsVirtualRegister(LI.reg)) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001129 // A live range can end with either a redefinition, a kill flag on a
1130 // use, or a dead flag on a def.
1131 // FIXME: Should we check for each of these?
1132 bool hasDeadDef = false;
1133 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1134 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
Cameron Zwarich5e61f992010-12-20 02:59:51 +00001135 if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() && MOI->isDead()) {
Cameron Zwarich636f15f2010-12-20 01:22:37 +00001136 hasDeadDef = true;
1137 break;
1138 }
1139 }
1140
1141 if (!hasDeadDef) {
1142 report("Instruction killing live segment neither defines nor reads "
1143 "register", MI);
1144 I->print(*OS);
1145 *OS << " in " << LI << '\n';
1146 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001147 }
1148 }
1149
1150 // Now check all the basic blocks in this live segment.
1151 MachineFunction::const_iterator MFI = MBB;
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001152 // Is this live range the beginning of a non-PHIDef VN?
1153 if (I->start == VNI->def && !VNI->isPHIDef()) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001154 // Not live-in to any blocks.
1155 if (MBB == EndMBB)
1156 continue;
1157 // Skip this block.
1158 ++MFI;
1159 }
1160 for (;;) {
1161 assert(LiveInts->isLiveInToMBB(LI, MFI));
Jakob Stoklund Olesene459d552010-10-26 16:49:23 +00001162 // We don't know how to track physregs into a landing pad.
1163 if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
1164 MFI->isLandingPad()) {
1165 if (&*MFI == EndMBB)
1166 break;
1167 ++MFI;
1168 continue;
1169 }
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001170 // Check that VNI is live-out of all predecessors.
1171 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1172 PE = MFI->pred_end(); PI != PE; ++PI) {
1173 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI).getPrevSlot();
1174 const VNInfo *PVNI = LI.getVNInfoAt(PEnd);
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001175
1176 if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI)) {
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001177 if (PVNI && !PVNI->hasPHIKill()) {
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001178 report("Value live out of predecessor doesn't have PHIKill", MF);
1179 *OS << "Valno #" << PVNI->id << " live out of BB#"
1180 << (*PI)->getNumber() << '@' << PEnd
1181 << " doesn't have PHIKill, but Valno #" << VNI->id
1182 << " is PHIDef and defined at the beginning of BB#"
1183 << MFI->getNumber() << '@' << LiveInts->getMBBStartIdx(MFI)
1184 << " in " << LI << '\n';
1185 }
1186 continue;
1187 }
1188
Cameron Zwarichcb584d02010-12-28 23:45:38 +00001189 if (!PVNI) {
1190 report("Register not marked live out of predecessor", *PI);
1191 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
1192 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live at "
1193 << PEnd << " in " << LI << '\n';
1194 continue;
1195 }
1196
Cameron Zwarich4eee42c2010-12-27 05:17:23 +00001197 if (PVNI != VNI) {
Jakob Stoklund Olesen78716872010-10-23 00:49:09 +00001198 report("Different value live out of predecessor", *PI);
1199 *OS << "Valno #" << PVNI->id << " live out of BB#"
1200 << (*PI)->getNumber() << '@' << PEnd
1201 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
1202 << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
1203 }
1204 }
1205 if (&*MFI == EndMBB)
1206 break;
1207 ++MFI;
1208 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001209 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001210
1211 // Check the LI only has one connected component.
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001212 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
1213 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1214 unsigned NumComp = ConEQ.Classify(&LI);
1215 if (NumComp > 1) {
1216 report("Multiple connected components in live interval", MF);
1217 *OS << NumComp << " components in " << LI << '\n';
Jakob Stoklund Olesencb367772010-10-29 00:40:57 +00001218 for (unsigned comp = 0; comp != NumComp; ++comp) {
1219 *OS << comp << ": valnos";
1220 for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1221 E = LI.vni_end(); I!=E; ++I)
1222 if (comp == ConEQ.getEqClass(*I))
1223 *OS << ' ' << (*I)->id;
1224 *OS << '\n';
1225 }
Jakob Stoklund Olesen8c593f92010-10-27 00:39:01 +00001226 }
Jakob Stoklund Olesen501dc422010-10-26 22:36:07 +00001227 }
Jakob Stoklund Olesen58e12482010-08-06 18:04:19 +00001228 }
1229}
Jakob Stoklund Olesen8f16e022009-11-18 20:36:57 +00001230