MachineVerifier.cpp 43 KB

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