MachineVerifier.cpp 43 KB

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