MachineVerifier.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. //===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
  2. //
  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. #include "llvm/Instructions.h"
  26. #include "llvm/Function.h"
  27. #include "llvm/CodeGen/LiveIntervalAnalysis.h"
  28. #include "llvm/CodeGen/LiveVariables.h"
  29. #include "llvm/CodeGen/LiveStackAnalysis.h"
  30. #include "llvm/CodeGen/MachineFunctionPass.h"
  31. #include "llvm/CodeGen/MachineFrameInfo.h"
  32. #include "llvm/CodeGen/MachineMemOperand.h"
  33. #include "llvm/CodeGen/MachineRegisterInfo.h"
  34. #include "llvm/CodeGen/Passes.h"
  35. #include "llvm/MC/MCAsmInfo.h"
  36. #include "llvm/Target/TargetMachine.h"
  37. #include "llvm/Target/TargetRegisterInfo.h"
  38. #include "llvm/Target/TargetInstrInfo.h"
  39. #include "llvm/ADT/DenseSet.h"
  40. #include "llvm/ADT/SetOperations.h"
  41. #include "llvm/ADT/SmallVector.h"
  42. #include "llvm/Support/Debug.h"
  43. #include "llvm/Support/ErrorHandling.h"
  44. #include "llvm/Support/raw_ostream.h"
  45. using namespace llvm;
  46. namespace {
  47. struct MachineVerifier {
  48. MachineVerifier(Pass *pass, const char *b) :
  49. PASS(pass),
  50. Banner(b),
  51. OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
  52. {}
  53. bool runOnMachineFunction(MachineFunction &MF);
  54. Pass *const PASS;
  55. const char *Banner;
  56. const char *const OutFileName;
  57. raw_ostream *OS;
  58. const MachineFunction *MF;
  59. const TargetMachine *TM;
  60. const TargetInstrInfo *TII;
  61. const TargetRegisterInfo *TRI;
  62. const MachineRegisterInfo *MRI;
  63. unsigned foundErrors;
  64. typedef SmallVector<unsigned, 16> RegVector;
  65. typedef DenseSet<unsigned> RegSet;
  66. typedef DenseMap<unsigned, const MachineInstr*> RegMap;
  67. const MachineInstr *FirstTerminator;
  68. BitVector regsReserved;
  69. RegSet regsLive;
  70. RegVector regsDefined, regsDead, regsKilled;
  71. RegSet regsLiveInButUnused;
  72. SlotIndex lastIndex;
  73. // Add Reg and any sub-registers to RV
  74. void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
  75. RV.push_back(Reg);
  76. if (TargetRegisterInfo::isPhysicalRegister(Reg))
  77. for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
  78. RV.push_back(*R);
  79. }
  80. struct BBInfo {
  81. // Is this MBB reachable from the MF entry point?
  82. bool reachable;
  83. // Vregs that must be live in because they are used without being
  84. // defined. Map value is the user.
  85. RegMap vregsLiveIn;
  86. // Regs killed in MBB. They may be defined again, and will then be in both
  87. // regsKilled and regsLiveOut.
  88. RegSet regsKilled;
  89. // Regs defined in MBB and live out. Note that vregs passing through may
  90. // be live out without being mentioned here.
  91. RegSet regsLiveOut;
  92. // Vregs that pass through MBB untouched. This set is disjoint from
  93. // regsKilled and regsLiveOut.
  94. RegSet vregsPassed;
  95. // Vregs that must pass through MBB because they are needed by a successor
  96. // block. This set is disjoint from regsLiveOut.
  97. RegSet vregsRequired;
  98. BBInfo() : reachable(false) {}
  99. // Add register to vregsPassed if it belongs there. Return true if
  100. // anything changed.
  101. bool addPassed(unsigned Reg) {
  102. if (!TargetRegisterInfo::isVirtualRegister(Reg))
  103. return false;
  104. if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
  105. return false;
  106. return vregsPassed.insert(Reg).second;
  107. }
  108. // Same for a full set.
  109. bool addPassed(const RegSet &RS) {
  110. bool changed = false;
  111. for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
  112. if (addPassed(*I))
  113. changed = true;
  114. return changed;
  115. }
  116. // Add register to vregsRequired if it belongs there. Return true if
  117. // anything changed.
  118. bool addRequired(unsigned Reg) {
  119. if (!TargetRegisterInfo::isVirtualRegister(Reg))
  120. return false;
  121. if (regsLiveOut.count(Reg))
  122. return false;
  123. return vregsRequired.insert(Reg).second;
  124. }
  125. // Same for a full set.
  126. bool addRequired(const RegSet &RS) {
  127. bool changed = false;
  128. for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
  129. if (addRequired(*I))
  130. changed = true;
  131. return changed;
  132. }
  133. // Same for a full map.
  134. bool addRequired(const RegMap &RM) {
  135. bool changed = false;
  136. for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
  137. if (addRequired(I->first))
  138. changed = true;
  139. return changed;
  140. }
  141. // Live-out registers are either in regsLiveOut or vregsPassed.
  142. bool isLiveOut(unsigned Reg) const {
  143. return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
  144. }
  145. };
  146. // Extra register info per MBB.
  147. DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
  148. bool isReserved(unsigned Reg) {
  149. return Reg < regsReserved.size() && regsReserved.test(Reg);
  150. }
  151. // Analysis information if available
  152. LiveVariables *LiveVars;
  153. LiveIntervals *LiveInts;
  154. LiveStacks *LiveStks;
  155. SlotIndexes *Indexes;
  156. void visitMachineFunctionBefore();
  157. void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
  158. void visitMachineInstrBefore(const MachineInstr *MI);
  159. void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
  160. void visitMachineInstrAfter(const MachineInstr *MI);
  161. void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
  162. void visitMachineFunctionAfter();
  163. void report(const char *msg, const MachineFunction *MF);
  164. void report(const char *msg, const MachineBasicBlock *MBB);
  165. void report(const char *msg, const MachineInstr *MI);
  166. void report(const char *msg, const MachineOperand *MO, unsigned MONum);
  167. void markReachable(const MachineBasicBlock *MBB);
  168. void calcRegsPassed();
  169. void checkPHIOps(const MachineBasicBlock *MBB);
  170. void calcRegsRequired();
  171. void verifyLiveVariables();
  172. void verifyLiveIntervals();
  173. };
  174. struct MachineVerifierPass : public MachineFunctionPass {
  175. static char ID; // Pass ID, replacement for typeid
  176. const char *const Banner;
  177. MachineVerifierPass(const char *b = 0)
  178. : MachineFunctionPass(ID), Banner(b) {
  179. initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
  180. }
  181. void getAnalysisUsage(AnalysisUsage &AU) const {
  182. AU.setPreservesAll();
  183. MachineFunctionPass::getAnalysisUsage(AU);
  184. }
  185. bool runOnMachineFunction(MachineFunction &MF) {
  186. MF.verify(this, Banner);
  187. return false;
  188. }
  189. };
  190. }
  191. char MachineVerifierPass::ID = 0;
  192. INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
  193. "Verify generated machine code", false, false)
  194. FunctionPass *llvm::createMachineVerifierPass(const char *Banner) {
  195. return new MachineVerifierPass(Banner);
  196. }
  197. void MachineFunction::verify(Pass *p, const char *Banner) const {
  198. MachineVerifier(p, Banner)
  199. .runOnMachineFunction(const_cast<MachineFunction&>(*this));
  200. }
  201. bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
  202. raw_ostream *OutFile = 0;
  203. if (OutFileName) {
  204. std::string ErrorInfo;
  205. OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
  206. raw_fd_ostream::F_Append);
  207. if (!ErrorInfo.empty()) {
  208. errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
  209. exit(1);
  210. }
  211. OS = OutFile;
  212. } else {
  213. OS = &errs();
  214. }
  215. foundErrors = 0;
  216. this->MF = &MF;
  217. TM = &MF.getTarget();
  218. TII = TM->getInstrInfo();
  219. TRI = TM->getRegisterInfo();
  220. MRI = &MF.getRegInfo();
  221. LiveVars = NULL;
  222. LiveInts = NULL;
  223. LiveStks = NULL;
  224. Indexes = NULL;
  225. if (PASS) {
  226. LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
  227. // We don't want to verify LiveVariables if LiveIntervals is available.
  228. if (!LiveInts)
  229. LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
  230. LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
  231. Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
  232. }
  233. visitMachineFunctionBefore();
  234. for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
  235. MFI!=MFE; ++MFI) {
  236. visitMachineBasicBlockBefore(MFI);
  237. for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
  238. MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
  239. if (MBBI->getParent() != MFI) {
  240. report("Bad instruction parent pointer", MFI);
  241. *OS << "Instruction: " << *MBBI;
  242. continue;
  243. }
  244. // Skip BUNDLE instruction for now. FIXME: We should add code to verify
  245. // the BUNDLE's specifically.
  246. if (MBBI->isBundle())
  247. continue;
  248. visitMachineInstrBefore(MBBI);
  249. for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
  250. visitMachineOperand(&MBBI->getOperand(I), I);
  251. visitMachineInstrAfter(MBBI);
  252. }
  253. visitMachineBasicBlockAfter(MFI);
  254. }
  255. visitMachineFunctionAfter();
  256. if (OutFile)
  257. delete OutFile;
  258. else if (foundErrors)
  259. report_fatal_error("Found "+Twine(foundErrors)+" machine code errors.");
  260. // Clean up.
  261. regsLive.clear();
  262. regsDefined.clear();
  263. regsDead.clear();
  264. regsKilled.clear();
  265. regsLiveInButUnused.clear();
  266. MBBInfoMap.clear();
  267. return false; // no changes
  268. }
  269. void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
  270. assert(MF);
  271. *OS << '\n';
  272. if (!foundErrors++) {
  273. if (Banner)
  274. *OS << "# " << Banner << '\n';
  275. MF->print(*OS, Indexes);
  276. }
  277. *OS << "*** Bad machine code: " << msg << " ***\n"
  278. << "- function: " << MF->getFunction()->getName() << "\n";
  279. }
  280. void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
  281. assert(MBB);
  282. report(msg, MBB->getParent());
  283. *OS << "- basic block: " << MBB->getName()
  284. << " " << (void*)MBB
  285. << " (BB#" << MBB->getNumber() << ")";
  286. if (Indexes)
  287. *OS << " [" << Indexes->getMBBStartIdx(MBB)
  288. << ';' << Indexes->getMBBEndIdx(MBB) << ')';
  289. *OS << '\n';
  290. }
  291. void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
  292. assert(MI);
  293. report(msg, MI->getParent());
  294. *OS << "- instruction: ";
  295. if (Indexes && Indexes->hasIndex(MI))
  296. *OS << Indexes->getInstructionIndex(MI) << '\t';
  297. MI->print(*OS, TM);
  298. }
  299. void MachineVerifier::report(const char *msg,
  300. const MachineOperand *MO, unsigned MONum) {
  301. assert(MO);
  302. report(msg, MO->getParent());
  303. *OS << "- operand " << MONum << ": ";
  304. MO->print(*OS, TM);
  305. *OS << "\n";
  306. }
  307. void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
  308. BBInfo &MInfo = MBBInfoMap[MBB];
  309. if (!MInfo.reachable) {
  310. MInfo.reachable = true;
  311. for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
  312. SuE = MBB->succ_end(); SuI != SuE; ++SuI)
  313. markReachable(*SuI);
  314. }
  315. }
  316. void MachineVerifier::visitMachineFunctionBefore() {
  317. lastIndex = SlotIndex();
  318. regsReserved = TRI->getReservedRegs(*MF);
  319. // A sub-register of a reserved register is also reserved
  320. for (int Reg = regsReserved.find_first(); Reg>=0;
  321. Reg = regsReserved.find_next(Reg)) {
  322. for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
  323. // FIXME: This should probably be:
  324. // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
  325. regsReserved.set(*Sub);
  326. }
  327. }
  328. markReachable(&MF->front());
  329. }
  330. // Does iterator point to a and b as the first two elements?
  331. static bool matchPair(MachineBasicBlock::const_succ_iterator i,
  332. const MachineBasicBlock *a, const MachineBasicBlock *b) {
  333. if (*i == a)
  334. return *++i == b;
  335. if (*i == b)
  336. return *++i == a;
  337. return false;
  338. }
  339. void
  340. MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
  341. FirstTerminator = 0;
  342. // Count the number of landing pad successors.
  343. SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
  344. for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
  345. E = MBB->succ_end(); I != E; ++I) {
  346. if ((*I)->isLandingPad())
  347. LandingPadSuccs.insert(*I);
  348. }
  349. const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
  350. const BasicBlock *BB = MBB->getBasicBlock();
  351. if (LandingPadSuccs.size() > 1 &&
  352. !(AsmInfo &&
  353. AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
  354. BB && isa<SwitchInst>(BB->getTerminator())))
  355. report("MBB has more than one landing pad successor", MBB);
  356. // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
  357. MachineBasicBlock *TBB = 0, *FBB = 0;
  358. SmallVector<MachineOperand, 4> Cond;
  359. if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
  360. TBB, FBB, Cond)) {
  361. // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
  362. // check whether its answers match up with reality.
  363. if (!TBB && !FBB) {
  364. // Block falls through to its successor.
  365. MachineFunction::const_iterator MBBI = MBB;
  366. ++MBBI;
  367. if (MBBI == MF->end()) {
  368. // It's possible that the block legitimately ends with a noreturn
  369. // call or an unreachable, in which case it won't actually fall
  370. // out the bottom of the function.
  371. } else if (MBB->succ_size() == LandingPadSuccs.size()) {
  372. // It's possible that the block legitimately ends with a noreturn
  373. // call or an unreachable, in which case it won't actuall fall
  374. // out of the block.
  375. } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
  376. report("MBB exits via unconditional fall-through but doesn't have "
  377. "exactly one CFG successor!", MBB);
  378. } else if (!MBB->isSuccessor(MBBI)) {
  379. report("MBB exits via unconditional fall-through but its successor "
  380. "differs from its CFG successor!", MBB);
  381. }
  382. if (!MBB->empty() && MBB->back().isBarrier() &&
  383. !TII->isPredicated(&MBB->back())) {
  384. report("MBB exits via unconditional fall-through but ends with a "
  385. "barrier instruction!", MBB);
  386. }
  387. if (!Cond.empty()) {
  388. report("MBB exits via unconditional fall-through but has a condition!",
  389. MBB);
  390. }
  391. } else if (TBB && !FBB && Cond.empty()) {
  392. // Block unconditionally branches somewhere.
  393. if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
  394. report("MBB exits via unconditional branch but doesn't have "
  395. "exactly one CFG successor!", MBB);
  396. } else if (!MBB->isSuccessor(TBB)) {
  397. report("MBB exits via unconditional branch but the CFG "
  398. "successor doesn't match the actual successor!", MBB);
  399. }
  400. if (MBB->empty()) {
  401. report("MBB exits via unconditional branch but doesn't contain "
  402. "any instructions!", MBB);
  403. } else if (!MBB->back().isBarrier()) {
  404. report("MBB exits via unconditional branch but doesn't end with a "
  405. "barrier instruction!", MBB);
  406. } else if (!MBB->back().isTerminator()) {
  407. report("MBB exits via unconditional branch but the branch isn't a "
  408. "terminator instruction!", MBB);
  409. }
  410. } else if (TBB && !FBB && !Cond.empty()) {
  411. // Block conditionally branches somewhere, otherwise falls through.
  412. MachineFunction::const_iterator MBBI = MBB;
  413. ++MBBI;
  414. if (MBBI == MF->end()) {
  415. report("MBB conditionally falls through out of function!", MBB);
  416. } if (MBB->succ_size() != 2) {
  417. report("MBB exits via conditional branch/fall-through but doesn't have "
  418. "exactly two CFG successors!", MBB);
  419. } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) {
  420. report("MBB exits via conditional branch/fall-through but the CFG "
  421. "successors don't match the actual successors!", MBB);
  422. }
  423. if (MBB->empty()) {
  424. report("MBB exits via conditional branch/fall-through but doesn't "
  425. "contain any instructions!", MBB);
  426. } else if (MBB->back().isBarrier()) {
  427. report("MBB exits via conditional branch/fall-through but ends with a "
  428. "barrier instruction!", MBB);
  429. } else if (!MBB->back().isTerminator()) {
  430. report("MBB exits via conditional branch/fall-through but the branch "
  431. "isn't a terminator instruction!", MBB);
  432. }
  433. } else if (TBB && FBB) {
  434. // Block conditionally branches somewhere, otherwise branches
  435. // somewhere else.
  436. if (MBB->succ_size() != 2) {
  437. report("MBB exits via conditional branch/branch but doesn't have "
  438. "exactly two CFG successors!", MBB);
  439. } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
  440. report("MBB exits via conditional branch/branch but the CFG "
  441. "successors don't match the actual successors!", MBB);
  442. }
  443. if (MBB->empty()) {
  444. report("MBB exits via conditional branch/branch but doesn't "
  445. "contain any instructions!", MBB);
  446. } else if (!MBB->back().isBarrier()) {
  447. report("MBB exits via conditional branch/branch but doesn't end with a "
  448. "barrier instruction!", MBB);
  449. } else if (!MBB->back().isTerminator()) {
  450. report("MBB exits via conditional branch/branch but the branch "
  451. "isn't a terminator instruction!", MBB);
  452. }
  453. if (Cond.empty()) {
  454. report("MBB exits via conditinal branch/branch but there's no "
  455. "condition!", MBB);
  456. }
  457. } else {
  458. report("AnalyzeBranch returned invalid data!", MBB);
  459. }
  460. }
  461. regsLive.clear();
  462. for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
  463. E = MBB->livein_end(); I != E; ++I) {
  464. if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
  465. report("MBB live-in list contains non-physical register", MBB);
  466. continue;
  467. }
  468. regsLive.insert(*I);
  469. for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
  470. regsLive.insert(*R);
  471. }
  472. regsLiveInButUnused = regsLive;
  473. const MachineFrameInfo *MFI = MF->getFrameInfo();
  474. assert(MFI && "Function has no frame info");
  475. BitVector PR = MFI->getPristineRegs(MBB);
  476. for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
  477. regsLive.insert(I);
  478. for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
  479. regsLive.insert(*R);
  480. }
  481. regsKilled.clear();
  482. regsDefined.clear();
  483. if (Indexes)
  484. lastIndex = Indexes->getMBBStartIdx(MBB);
  485. }
  486. void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
  487. const MCInstrDesc &MCID = MI->getDesc();
  488. if (MI->getNumOperands() < MCID.getNumOperands()) {
  489. report("Too few operands", MI);
  490. *OS << MCID.getNumOperands() << " operands expected, but "
  491. << MI->getNumExplicitOperands() << " given.\n";
  492. }
  493. // Check the MachineMemOperands for basic consistency.
  494. for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
  495. E = MI->memoperands_end(); I != E; ++I) {
  496. if ((*I)->isLoad() && !MI->mayLoad())
  497. report("Missing mayLoad flag", MI);
  498. if ((*I)->isStore() && !MI->mayStore())
  499. report("Missing mayStore flag", MI);
  500. }
  501. // Debug values must not have a slot index.
  502. // Other instructions must have one.
  503. if (LiveInts) {
  504. bool mapped = !LiveInts->isNotInMIMap(MI);
  505. if (MI->isDebugValue()) {
  506. if (mapped)
  507. report("Debug instruction has a slot index", MI);
  508. } else {
  509. if (!mapped)
  510. report("Missing slot index", MI);
  511. }
  512. }
  513. // Ensure non-terminators don't follow terminators.
  514. if (MI->isTerminator()) {
  515. if (!FirstTerminator)
  516. FirstTerminator = MI;
  517. } else if (FirstTerminator) {
  518. report("Non-terminator instruction after the first terminator", MI);
  519. *OS << "First terminator was:\t" << *FirstTerminator;
  520. }
  521. StringRef ErrorInfo;
  522. if (!TII->verifyInstruction(MI, ErrorInfo))
  523. report(ErrorInfo.data(), MI);
  524. }
  525. void
  526. MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
  527. const MachineInstr *MI = MO->getParent();
  528. const MCInstrDesc &MCID = MI->getDesc();
  529. const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
  530. // The first MCID.NumDefs operands must be explicit register defines
  531. if (MONum < MCID.getNumDefs()) {
  532. if (!MO->isReg())
  533. report("Explicit definition must be a register", MO, MONum);
  534. else if (!MO->isDef())
  535. report("Explicit definition marked as use", MO, MONum);
  536. else if (MO->isImplicit())
  537. report("Explicit definition marked as implicit", MO, MONum);
  538. } else if (MONum < MCID.getNumOperands()) {
  539. // Don't check if it's the last operand in a variadic instruction. See,
  540. // e.g., LDM_RET in the arm back end.
  541. if (MO->isReg() &&
  542. !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
  543. if (MO->isDef() && !MCOI.isOptionalDef())
  544. report("Explicit operand marked as def", MO, MONum);
  545. if (MO->isImplicit())
  546. report("Explicit operand marked as implicit", MO, MONum);
  547. }
  548. } else {
  549. // ARM adds %reg0 operands to indicate predicates. We'll allow that.
  550. if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
  551. report("Extra explicit operand on non-variadic instruction", MO, MONum);
  552. }
  553. switch (MO->getType()) {
  554. case MachineOperand::MO_Register: {
  555. const unsigned Reg = MO->getReg();
  556. if (!Reg)
  557. return;
  558. // Check Live Variables.
  559. if (MI->isDebugValue()) {
  560. // Liveness checks are not valid for debug values.
  561. } else if (MO->isUse() && !MO->isUndef()) {
  562. regsLiveInButUnused.erase(Reg);
  563. bool isKill = false;
  564. unsigned defIdx;
  565. if (MI->isRegTiedToDefOperand(MONum, &defIdx)) {
  566. // A two-addr use counts as a kill if use and def are the same.
  567. unsigned DefReg = MI->getOperand(defIdx).getReg();
  568. if (Reg == DefReg)
  569. isKill = true;
  570. else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
  571. report("Two-address instruction operands must be identical",
  572. MO, MONum);
  573. }
  574. } else
  575. isKill = MO->isKill();
  576. if (isKill)
  577. addRegWithSubRegs(regsKilled, Reg);
  578. // Check that LiveVars knows this kill.
  579. if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
  580. MO->isKill()) {
  581. LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
  582. if (std::find(VI.Kills.begin(),
  583. VI.Kills.end(), MI) == VI.Kills.end())
  584. report("Kill missing from LiveVariables", MO, MONum);
  585. }
  586. // Check LiveInts liveness and kill.
  587. if (TargetRegisterInfo::isVirtualRegister(Reg) &&
  588. LiveInts && !LiveInts->isNotInMIMap(MI)) {
  589. SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getRegSlot(true);
  590. if (LiveInts->hasInterval(Reg)) {
  591. const LiveInterval &LI = LiveInts->getInterval(Reg);
  592. if (!LI.liveAt(UseIdx)) {
  593. report("No live range at use", MO, MONum);
  594. *OS << UseIdx << " is not live in " << LI << '\n';
  595. }
  596. // Check for extra kill flags.
  597. // Note that we allow missing kill flags for now.
  598. if (MO->isKill() && !LI.killedAt(UseIdx.getRegSlot())) {
  599. report("Live range continues after kill flag", MO, MONum);
  600. *OS << "Live range: " << LI << '\n';
  601. }
  602. } else {
  603. report("Virtual register has no Live interval", MO, MONum);
  604. }
  605. }
  606. // Use of a dead register.
  607. if (!regsLive.count(Reg)) {
  608. if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
  609. // Reserved registers may be used even when 'dead'.
  610. if (!isReserved(Reg))
  611. report("Using an undefined physical register", MO, MONum);
  612. } else {
  613. BBInfo &MInfo = MBBInfoMap[MI->getParent()];
  614. // We don't know which virtual registers are live in, so only complain
  615. // if vreg was killed in this MBB. Otherwise keep track of vregs that
  616. // must be live in. PHI instructions are handled separately.
  617. if (MInfo.regsKilled.count(Reg))
  618. report("Using a killed virtual register", MO, MONum);
  619. else if (!MI->isPHI())
  620. MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
  621. }
  622. }
  623. } else if (MO->isDef()) {
  624. // Register defined.
  625. // TODO: verify that earlyclobber ops are not used.
  626. if (MO->isDead())
  627. addRegWithSubRegs(regsDead, Reg);
  628. else
  629. addRegWithSubRegs(regsDefined, Reg);
  630. // Verify SSA form.
  631. if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
  632. llvm::next(MRI->def_begin(Reg)) != MRI->def_end())
  633. report("Multiple virtual register defs in SSA form", MO, MONum);
  634. // Check LiveInts for a live range, but only for virtual registers.
  635. if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) &&
  636. !LiveInts->isNotInMIMap(MI)) {
  637. SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getRegSlot();
  638. if (LiveInts->hasInterval(Reg)) {
  639. const LiveInterval &LI = LiveInts->getInterval(Reg);
  640. if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) {
  641. assert(VNI && "NULL valno is not allowed");
  642. if (VNI->def != DefIdx && !MO->isEarlyClobber()) {
  643. report("Inconsistent valno->def", MO, MONum);
  644. *OS << "Valno " << VNI->id << " is not defined at "
  645. << DefIdx << " in " << LI << '\n';
  646. }
  647. } else {
  648. report("No live range at def", MO, MONum);
  649. *OS << DefIdx << " is not live in " << LI << '\n';
  650. }
  651. } else {
  652. report("Virtual register has no Live interval", MO, MONum);
  653. }
  654. }
  655. }
  656. // Check register classes.
  657. if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
  658. unsigned SubIdx = MO->getSubReg();
  659. if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
  660. if (SubIdx) {
  661. report("Illegal subregister index for physical register", MO, MONum);
  662. return;
  663. }
  664. if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
  665. if (!DRC->contains(Reg)) {
  666. report("Illegal physical register for instruction", MO, MONum);
  667. *OS << TRI->getName(Reg) << " is not a "
  668. << DRC->getName() << " register.\n";
  669. }
  670. }
  671. } else {
  672. // Virtual register.
  673. const TargetRegisterClass *RC = MRI->getRegClass(Reg);
  674. if (SubIdx) {
  675. const TargetRegisterClass *SRC =
  676. TRI->getSubClassWithSubReg(RC, SubIdx);
  677. if (!SRC) {
  678. report("Invalid subregister index for virtual register", MO, MONum);
  679. *OS << "Register class " << RC->getName()
  680. << " does not support subreg index " << SubIdx << "\n";
  681. return;
  682. }
  683. if (RC != SRC) {
  684. report("Invalid register class for subregister index", MO, MONum);
  685. *OS << "Register class " << RC->getName()
  686. << " does not fully support subreg index " << SubIdx << "\n";
  687. return;
  688. }
  689. }
  690. if (const TargetRegisterClass *DRC = TII->getRegClass(MCID,MONum,TRI)) {
  691. if (SubIdx) {
  692. const TargetRegisterClass *SuperRC =
  693. TRI->getLargestLegalSuperClass(RC);
  694. if (!SuperRC) {
  695. report("No largest legal super class exists.", MO, MONum);
  696. return;
  697. }
  698. DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
  699. if (!DRC) {
  700. report("No matching super-reg register class.", MO, MONum);
  701. return;
  702. }
  703. }
  704. if (!RC->hasSuperClassEq(DRC)) {
  705. report("Illegal virtual register for instruction", MO, MONum);
  706. *OS << "Expected a " << DRC->getName() << " register, but got a "
  707. << RC->getName() << " register\n";
  708. }
  709. }
  710. }
  711. }
  712. break;
  713. }
  714. case MachineOperand::MO_MachineBasicBlock:
  715. if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
  716. report("PHI operand is not in the CFG", MO, MONum);
  717. break;
  718. case MachineOperand::MO_FrameIndex:
  719. if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
  720. LiveInts && !LiveInts->isNotInMIMap(MI)) {
  721. LiveInterval &LI = LiveStks->getInterval(MO->getIndex());
  722. SlotIndex Idx = LiveInts->getInstructionIndex(MI);
  723. if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) {
  724. report("Instruction loads from dead spill slot", MO, MONum);
  725. *OS << "Live stack: " << LI << '\n';
  726. }
  727. if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) {
  728. report("Instruction stores to dead spill slot", MO, MONum);
  729. *OS << "Live stack: " << LI << '\n';
  730. }
  731. }
  732. break;
  733. default:
  734. break;
  735. }
  736. }
  737. void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
  738. BBInfo &MInfo = MBBInfoMap[MI->getParent()];
  739. set_union(MInfo.regsKilled, regsKilled);
  740. set_subtract(regsLive, regsKilled); regsKilled.clear();
  741. set_subtract(regsLive, regsDead); regsDead.clear();
  742. set_union(regsLive, regsDefined); regsDefined.clear();
  743. if (Indexes && Indexes->hasIndex(MI)) {
  744. SlotIndex idx = Indexes->getInstructionIndex(MI);
  745. if (!(idx > lastIndex)) {
  746. report("Instruction index out of order", MI);
  747. *OS << "Last instruction was at " << lastIndex << '\n';
  748. }
  749. lastIndex = idx;
  750. }
  751. }
  752. void
  753. MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
  754. MBBInfoMap[MBB].regsLiveOut = regsLive;
  755. regsLive.clear();
  756. if (Indexes) {
  757. SlotIndex stop = Indexes->getMBBEndIdx(MBB);
  758. if (!(stop > lastIndex)) {
  759. report("Block ends before last instruction index", MBB);
  760. *OS << "Block ends at " << stop
  761. << " last instruction was at " << lastIndex << '\n';
  762. }
  763. lastIndex = stop;
  764. }
  765. }
  766. // Calculate the largest possible vregsPassed sets. These are the registers that
  767. // can pass through an MBB live, but may not be live every time. It is assumed
  768. // that all vregsPassed sets are empty before the call.
  769. void MachineVerifier::calcRegsPassed() {
  770. // First push live-out regs to successors' vregsPassed. Remember the MBBs that
  771. // have any vregsPassed.
  772. DenseSet<const MachineBasicBlock*> todo;
  773. for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
  774. MFI != MFE; ++MFI) {
  775. const MachineBasicBlock &MBB(*MFI);
  776. BBInfo &MInfo = MBBInfoMap[&MBB];
  777. if (!MInfo.reachable)
  778. continue;
  779. for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
  780. SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
  781. BBInfo &SInfo = MBBInfoMap[*SuI];
  782. if (SInfo.addPassed(MInfo.regsLiveOut))
  783. todo.insert(*SuI);
  784. }
  785. }
  786. // Iteratively push vregsPassed to successors. This will converge to the same
  787. // final state regardless of DenseSet iteration order.
  788. while (!todo.empty()) {
  789. const MachineBasicBlock *MBB = *todo.begin();
  790. todo.erase(MBB);
  791. BBInfo &MInfo = MBBInfoMap[MBB];
  792. for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
  793. SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
  794. if (*SuI == MBB)
  795. continue;
  796. BBInfo &SInfo = MBBInfoMap[*SuI];
  797. if (SInfo.addPassed(MInfo.vregsPassed))
  798. todo.insert(*SuI);
  799. }
  800. }
  801. }
  802. // Calculate the set of virtual registers that must be passed through each basic
  803. // block in order to satisfy the requirements of successor blocks. This is very
  804. // similar to calcRegsPassed, only backwards.
  805. void MachineVerifier::calcRegsRequired() {
  806. // First push live-in regs to predecessors' vregsRequired.
  807. DenseSet<const MachineBasicBlock*> todo;
  808. for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
  809. MFI != MFE; ++MFI) {
  810. const MachineBasicBlock &MBB(*MFI);
  811. BBInfo &MInfo = MBBInfoMap[&MBB];
  812. for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
  813. PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
  814. BBInfo &PInfo = MBBInfoMap[*PrI];
  815. if (PInfo.addRequired(MInfo.vregsLiveIn))
  816. todo.insert(*PrI);
  817. }
  818. }
  819. // Iteratively push vregsRequired to predecessors. This will converge to the
  820. // same final state regardless of DenseSet iteration order.
  821. while (!todo.empty()) {
  822. const MachineBasicBlock *MBB = *todo.begin();
  823. todo.erase(MBB);
  824. BBInfo &MInfo = MBBInfoMap[MBB];
  825. for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
  826. PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
  827. if (*PrI == MBB)
  828. continue;
  829. BBInfo &SInfo = MBBInfoMap[*PrI];
  830. if (SInfo.addRequired(MInfo.vregsRequired))
  831. todo.insert(*PrI);
  832. }
  833. }
  834. }
  835. // Check PHI instructions at the beginning of MBB. It is assumed that
  836. // calcRegsPassed has been run so BBInfo::isLiveOut is valid.
  837. void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
  838. for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
  839. BBI != BBE && BBI->isPHI(); ++BBI) {
  840. DenseSet<const MachineBasicBlock*> seen;
  841. for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
  842. unsigned Reg = BBI->getOperand(i).getReg();
  843. const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
  844. if (!Pre->isSuccessor(MBB))
  845. continue;
  846. seen.insert(Pre);
  847. BBInfo &PrInfo = MBBInfoMap[Pre];
  848. if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
  849. report("PHI operand is not live-out from predecessor",
  850. &BBI->getOperand(i), i);
  851. }
  852. // Did we see all predecessors?
  853. for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
  854. PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
  855. if (!seen.count(*PrI)) {
  856. report("Missing PHI operand", BBI);
  857. *OS << "BB#" << (*PrI)->getNumber()
  858. << " is a predecessor according to the CFG.\n";
  859. }
  860. }
  861. }
  862. }
  863. void MachineVerifier::visitMachineFunctionAfter() {
  864. calcRegsPassed();
  865. for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
  866. MFI != MFE; ++MFI) {
  867. BBInfo &MInfo = MBBInfoMap[MFI];
  868. // Skip unreachable MBBs.
  869. if (!MInfo.reachable)
  870. continue;
  871. checkPHIOps(MFI);
  872. }
  873. // Now check liveness info if available
  874. if (LiveVars || LiveInts)
  875. calcRegsRequired();
  876. if (LiveVars)
  877. verifyLiveVariables();
  878. if (LiveInts)
  879. verifyLiveIntervals();
  880. }
  881. void MachineVerifier::verifyLiveVariables() {
  882. assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
  883. for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
  884. unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
  885. LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
  886. for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
  887. MFI != MFE; ++MFI) {
  888. BBInfo &MInfo = MBBInfoMap[MFI];
  889. // Our vregsRequired should be identical to LiveVariables' AliveBlocks
  890. if (MInfo.vregsRequired.count(Reg)) {
  891. if (!VI.AliveBlocks.test(MFI->getNumber())) {
  892. report("LiveVariables: Block missing from AliveBlocks", MFI);
  893. *OS << "Virtual register " << PrintReg(Reg)
  894. << " must be live through the block.\n";
  895. }
  896. } else {
  897. if (VI.AliveBlocks.test(MFI->getNumber())) {
  898. report("LiveVariables: Block should not be in AliveBlocks", MFI);
  899. *OS << "Virtual register " << PrintReg(Reg)
  900. << " is not needed live through the block.\n";
  901. }
  902. }
  903. }
  904. }
  905. }
  906. void MachineVerifier::verifyLiveIntervals() {
  907. assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
  908. for (LiveIntervals::const_iterator LVI = LiveInts->begin(),
  909. LVE = LiveInts->end(); LVI != LVE; ++LVI) {
  910. const LiveInterval &LI = *LVI->second;
  911. // Spilling and splitting may leave unused registers around. Skip them.
  912. if (MRI->use_empty(LI.reg))
  913. continue;
  914. // Physical registers have much weirdness going on, mostly from coalescing.
  915. // We should probably fix it, but for now just ignore them.
  916. if (TargetRegisterInfo::isPhysicalRegister(LI.reg))
  917. continue;
  918. assert(LVI->first == LI.reg && "Invalid reg to interval mapping");
  919. for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
  920. I!=E; ++I) {
  921. VNInfo *VNI = *I;
  922. const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def);
  923. if (!DefVNI) {
  924. if (!VNI->isUnused()) {
  925. report("Valno not live at def and not marked unused", MF);
  926. *OS << "Valno #" << VNI->id << " in " << LI << '\n';
  927. }
  928. continue;
  929. }
  930. if (VNI->isUnused())
  931. continue;
  932. if (DefVNI != VNI) {
  933. report("Live range at def has different valno", MF);
  934. *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
  935. << " where valno #" << DefVNI->id << " is live in " << LI << '\n';
  936. continue;
  937. }
  938. const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
  939. if (!MBB) {
  940. report("Invalid definition index", MF);
  941. *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
  942. << " in " << LI << '\n';
  943. continue;
  944. }
  945. if (VNI->isPHIDef()) {
  946. if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
  947. report("PHIDef value is not defined at MBB start", MF);
  948. *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
  949. << ", not at the beginning of BB#" << MBB->getNumber()
  950. << " in " << LI << '\n';
  951. }
  952. } else {
  953. // Non-PHI def.
  954. const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
  955. if (!MI) {
  956. report("No instruction at def index", MF);
  957. *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
  958. << " in " << LI << '\n';
  959. } else if (!MI->modifiesRegister(LI.reg, TRI)) {
  960. report("Defining instruction does not modify register", MI);
  961. *OS << "Valno #" << VNI->id << " in " << LI << '\n';
  962. }
  963. bool isEarlyClobber = false;
  964. if (MI) {
  965. for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
  966. MOE = MI->operands_end(); MOI != MOE; ++MOI) {
  967. if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() &&
  968. MOI->isEarlyClobber()) {
  969. isEarlyClobber = true;
  970. break;
  971. }
  972. }
  973. }
  974. // Early clobber defs begin at USE slots, but other defs must begin at
  975. // DEF slots.
  976. if (isEarlyClobber) {
  977. if (!VNI->def.isEarlyClobber()) {
  978. report("Early clobber def must be at an early-clobber slot", MF);
  979. *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
  980. << " in " << LI << '\n';
  981. }
  982. } else if (!VNI->def.isRegister()) {
  983. report("Non-PHI, non-early clobber def must be at a register slot",
  984. MF);
  985. *OS << "Valno #" << VNI->id << " is defined at " << VNI->def
  986. << " in " << LI << '\n';
  987. }
  988. }
  989. }
  990. for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) {
  991. const VNInfo *VNI = I->valno;
  992. assert(VNI && "Live range has no valno");
  993. if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) {
  994. report("Foreign valno in live range", MF);
  995. I->print(*OS);
  996. *OS << " has a valno not in " << LI << '\n';
  997. }
  998. if (VNI->isUnused()) {
  999. report("Live range valno is marked unused", MF);
  1000. I->print(*OS);
  1001. *OS << " in " << LI << '\n';
  1002. }
  1003. const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start);
  1004. if (!MBB) {
  1005. report("Bad start of live segment, no basic block", MF);
  1006. I->print(*OS);
  1007. *OS << " in " << LI << '\n';
  1008. continue;
  1009. }
  1010. SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
  1011. if (I->start != MBBStartIdx && I->start != VNI->def) {
  1012. report("Live segment must begin at MBB entry or valno def", MBB);
  1013. I->print(*OS);
  1014. *OS << " in " << LI << '\n' << "Basic block starts at "
  1015. << MBBStartIdx << '\n';
  1016. }
  1017. const MachineBasicBlock *EndMBB =
  1018. LiveInts->getMBBFromIndex(I->end.getPrevSlot());
  1019. if (!EndMBB) {
  1020. report("Bad end of live segment, no basic block", MF);
  1021. I->print(*OS);
  1022. *OS << " in " << LI << '\n';
  1023. continue;
  1024. }
  1025. if (I->end != LiveInts->getMBBEndIdx(EndMBB)) {
  1026. // The live segment is ending inside EndMBB
  1027. const MachineInstr *MI =
  1028. LiveInts->getInstructionFromIndex(I->end.getPrevSlot());
  1029. if (!MI) {
  1030. report("Live segment doesn't end at a valid instruction", EndMBB);
  1031. I->print(*OS);
  1032. *OS << " in " << LI << '\n' << "Basic block starts at "
  1033. << MBBStartIdx << '\n';
  1034. } else if (TargetRegisterInfo::isVirtualRegister(LI.reg) &&
  1035. !MI->readsVirtualRegister(LI.reg)) {
  1036. // A live range can end with either a redefinition, a kill flag on a
  1037. // use, or a dead flag on a def.
  1038. // FIXME: Should we check for each of these?
  1039. bool hasDeadDef = false;
  1040. for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
  1041. MOE = MI->operands_end(); MOI != MOE; ++MOI) {
  1042. if (MOI->isReg() && MOI->getReg() == LI.reg && MOI->isDef() && MOI->isDead()) {
  1043. hasDeadDef = true;
  1044. break;
  1045. }
  1046. }
  1047. if (!hasDeadDef) {
  1048. report("Instruction killing live segment neither defines nor reads "
  1049. "register", MI);
  1050. I->print(*OS);
  1051. *OS << " in " << LI << '\n';
  1052. }
  1053. }
  1054. }
  1055. // Now check all the basic blocks in this live segment.
  1056. MachineFunction::const_iterator MFI = MBB;
  1057. // Is this live range the beginning of a non-PHIDef VN?
  1058. if (I->start == VNI->def && !VNI->isPHIDef()) {
  1059. // Not live-in to any blocks.
  1060. if (MBB == EndMBB)
  1061. continue;
  1062. // Skip this block.
  1063. ++MFI;
  1064. }
  1065. for (;;) {
  1066. assert(LiveInts->isLiveInToMBB(LI, MFI));
  1067. // We don't know how to track physregs into a landing pad.
  1068. if (TargetRegisterInfo::isPhysicalRegister(LI.reg) &&
  1069. MFI->isLandingPad()) {
  1070. if (&*MFI == EndMBB)
  1071. break;
  1072. ++MFI;
  1073. continue;
  1074. }
  1075. // Check that VNI is live-out of all predecessors.
  1076. for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
  1077. PE = MFI->pred_end(); PI != PE; ++PI) {
  1078. SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
  1079. const VNInfo *PVNI = LI.getVNInfoBefore(PEnd);
  1080. if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI))
  1081. continue;
  1082. if (!PVNI) {
  1083. report("Register not marked live out of predecessor", *PI);
  1084. *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber()
  1085. << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before "
  1086. << PEnd << " in " << LI << '\n';
  1087. continue;
  1088. }
  1089. if (PVNI != VNI) {
  1090. report("Different value live out of predecessor", *PI);
  1091. *OS << "Valno #" << PVNI->id << " live out of BB#"
  1092. << (*PI)->getNumber() << '@' << PEnd
  1093. << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber()
  1094. << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n';
  1095. }
  1096. }
  1097. if (&*MFI == EndMBB)
  1098. break;
  1099. ++MFI;
  1100. }
  1101. }
  1102. // Check the LI only has one connected component.
  1103. if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
  1104. ConnectedVNInfoEqClasses ConEQ(*LiveInts);
  1105. unsigned NumComp = ConEQ.Classify(&LI);
  1106. if (NumComp > 1) {
  1107. report("Multiple connected components in live interval", MF);
  1108. *OS << NumComp << " components in " << LI << '\n';
  1109. for (unsigned comp = 0; comp != NumComp; ++comp) {
  1110. *OS << comp << ": valnos";
  1111. for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
  1112. E = LI.vni_end(); I!=E; ++I)
  1113. if (comp == ConEQ.getEqClass(*I))
  1114. *OS << ' ' << (*I)->id;
  1115. *OS << '\n';
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }