MachineVerifier.cpp 44 KB

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