MachineVerifier.cpp 43 KB

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