MachineVerifier.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. //===-- MachineVerifier.cpp - Machine Code Verifier -------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Pass to verify generated machine code. The following is checked:
  11. //
  12. // Operand counts: All explicit operands must be present.
  13. //
  14. // Register classes: All physical and virtual register operands must be
  15. // compatible with the register class required by the instruction descriptor.
  16. //
  17. // Register live intervals: Registers must be defined only once, and must be
  18. // defined before use.
  19. //
  20. // The machine code verifier is enabled from LLVMTargetMachine.cpp with the
  21. // command-line option -verify-machineinstrs, or by defining the environment
  22. // variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
  23. // the verifier errors.
  24. //===----------------------------------------------------------------------===//
  25. #include "llvm/Function.h"
  26. #include "llvm/CodeGen/LiveVariables.h"
  27. #include "llvm/CodeGen/MachineFunctionPass.h"
  28. #include "llvm/CodeGen/MachineFrameInfo.h"
  29. #include "llvm/CodeGen/MachineMemOperand.h"
  30. #include "llvm/CodeGen/MachineRegisterInfo.h"
  31. #include "llvm/CodeGen/Passes.h"
  32. #include "llvm/Target/TargetMachine.h"
  33. #include "llvm/Target/TargetRegisterInfo.h"
  34. #include "llvm/Target/TargetInstrInfo.h"
  35. #include "llvm/ADT/DenseSet.h"
  36. #include "llvm/ADT/SetOperations.h"
  37. #include "llvm/ADT/SmallVector.h"
  38. #include "llvm/Support/Debug.h"
  39. #include "llvm/Support/ErrorHandling.h"
  40. #include "llvm/Support/raw_ostream.h"
  41. using namespace llvm;
  42. namespace {
  43. struct MachineVerifier : public MachineFunctionPass {
  44. static char ID; // Pass ID, replacement for typeid
  45. MachineVerifier(bool allowDoubleDefs = false) :
  46. MachineFunctionPass(&ID),
  47. allowVirtDoubleDefs(allowDoubleDefs),
  48. allowPhysDoubleDefs(allowDoubleDefs),
  49. OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS"))
  50. {}
  51. void getAnalysisUsage(AnalysisUsage &AU) const {
  52. AU.setPreservesAll();
  53. MachineFunctionPass::getAnalysisUsage(AU);
  54. }
  55. bool runOnMachineFunction(MachineFunction &MF);
  56. const bool allowVirtDoubleDefs;
  57. const bool allowPhysDoubleDefs;
  58. const char *const OutFileName;
  59. raw_ostream *OS;
  60. const MachineFunction *MF;
  61. const TargetMachine *TM;
  62. const TargetRegisterInfo *TRI;
  63. const MachineRegisterInfo *MRI;
  64. unsigned foundErrors;
  65. typedef SmallVector<unsigned, 16> RegVector;
  66. typedef DenseSet<unsigned> RegSet;
  67. typedef DenseMap<unsigned, const MachineInstr*> RegMap;
  68. BitVector regsReserved;
  69. RegSet regsLive;
  70. RegVector regsDefined, regsDead, regsKilled;
  71. RegSet regsLiveInButUnused;
  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. // Vregs that must be dead in because they are defined without being
  86. // killed first. Map value is the defining instruction.
  87. RegMap vregsDeadIn;
  88. // Regs killed in MBB. They may be defined again, and will then be in both
  89. // regsKilled and regsLiveOut.
  90. RegSet regsKilled;
  91. // Regs defined in MBB and live out. Note that vregs passing through may
  92. // be live out without being mentioned here.
  93. RegSet regsLiveOut;
  94. // Vregs that pass through MBB untouched. This set is disjoint from
  95. // regsKilled and regsLiveOut.
  96. RegSet vregsPassed;
  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. // Live-out registers are either in regsLiveOut or vregsPassed.
  116. bool isLiveOut(unsigned Reg) const {
  117. return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
  118. }
  119. };
  120. // Extra register info per MBB.
  121. DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
  122. bool isReserved(unsigned Reg) {
  123. return Reg < regsReserved.size() && regsReserved.test(Reg);
  124. }
  125. void visitMachineFunctionBefore();
  126. void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
  127. void visitMachineInstrBefore(const MachineInstr *MI);
  128. void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
  129. void visitMachineInstrAfter(const MachineInstr *MI);
  130. void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
  131. void visitMachineFunctionAfter();
  132. void report(const char *msg, const MachineFunction *MF);
  133. void report(const char *msg, const MachineBasicBlock *MBB);
  134. void report(const char *msg, const MachineInstr *MI);
  135. void report(const char *msg, const MachineOperand *MO, unsigned MONum);
  136. void markReachable(const MachineBasicBlock *MBB);
  137. void calcMaxRegsPassed();
  138. void calcMinRegsPassed();
  139. void checkPHIOps(const MachineBasicBlock *MBB);
  140. };
  141. }
  142. char MachineVerifier::ID = 0;
  143. static RegisterPass<MachineVerifier>
  144. MachineVer("machineverifier", "Verify generated machine code");
  145. static const PassInfo *const MachineVerifyID = &MachineVer;
  146. FunctionPass *llvm::createMachineVerifierPass(bool allowPhysDoubleDefs) {
  147. return new MachineVerifier(allowPhysDoubleDefs);
  148. }
  149. bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
  150. raw_ostream *OutFile = 0;
  151. if (OutFileName) {
  152. std::string ErrorInfo;
  153. OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
  154. raw_fd_ostream::F_Append);
  155. if (!ErrorInfo.empty()) {
  156. errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
  157. exit(1);
  158. }
  159. OS = OutFile;
  160. } else {
  161. OS = &errs();
  162. }
  163. foundErrors = 0;
  164. this->MF = &MF;
  165. TM = &MF.getTarget();
  166. TRI = TM->getRegisterInfo();
  167. MRI = &MF.getRegInfo();
  168. visitMachineFunctionBefore();
  169. for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
  170. MFI!=MFE; ++MFI) {
  171. visitMachineBasicBlockBefore(MFI);
  172. for (MachineBasicBlock::const_iterator MBBI = MFI->begin(),
  173. MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
  174. visitMachineInstrBefore(MBBI);
  175. for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I)
  176. visitMachineOperand(&MBBI->getOperand(I), I);
  177. visitMachineInstrAfter(MBBI);
  178. }
  179. visitMachineBasicBlockAfter(MFI);
  180. }
  181. visitMachineFunctionAfter();
  182. if (OutFile)
  183. delete OutFile;
  184. else if (foundErrors)
  185. llvm_report_error("Found "+Twine(foundErrors)+" machine code errors.");
  186. // Clean up.
  187. regsLive.clear();
  188. regsDefined.clear();
  189. regsDead.clear();
  190. regsKilled.clear();
  191. regsLiveInButUnused.clear();
  192. MBBInfoMap.clear();
  193. return false; // no changes
  194. }
  195. void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
  196. assert(MF);
  197. *OS << '\n';
  198. if (!foundErrors++)
  199. MF->print(*OS);
  200. *OS << "*** Bad machine code: " << msg << " ***\n"
  201. << "- function: " << MF->getFunction()->getNameStr() << "\n";
  202. }
  203. void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
  204. assert(MBB);
  205. report(msg, MBB->getParent());
  206. *OS << "- basic block: " << MBB->getBasicBlock()->getNameStr()
  207. << " " << (void*)MBB
  208. << " (#" << MBB->getNumber() << ")\n";
  209. }
  210. void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
  211. assert(MI);
  212. report(msg, MI->getParent());
  213. *OS << "- instruction: ";
  214. MI->print(*OS, TM);
  215. }
  216. void MachineVerifier::report(const char *msg,
  217. const MachineOperand *MO, unsigned MONum) {
  218. assert(MO);
  219. report(msg, MO->getParent());
  220. *OS << "- operand " << MONum << ": ";
  221. MO->print(*OS, TM);
  222. *OS << "\n";
  223. }
  224. void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
  225. BBInfo &MInfo = MBBInfoMap[MBB];
  226. if (!MInfo.reachable) {
  227. MInfo.reachable = true;
  228. for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
  229. SuE = MBB->succ_end(); SuI != SuE; ++SuI)
  230. markReachable(*SuI);
  231. }
  232. }
  233. void MachineVerifier::visitMachineFunctionBefore() {
  234. regsReserved = TRI->getReservedRegs(*MF);
  235. // A sub-register of a reserved register is also reserved
  236. for (int Reg = regsReserved.find_first(); Reg>=0;
  237. Reg = regsReserved.find_next(Reg)) {
  238. for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) {
  239. // FIXME: This should probably be:
  240. // assert(regsReserved.test(*Sub) && "Non-reserved sub-register");
  241. regsReserved.set(*Sub);
  242. }
  243. }
  244. markReachable(&MF->front());
  245. }
  246. void MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
  247. const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
  248. // Start with minimal CFG sanity checks.
  249. MachineFunction::const_iterator MBBI = MBB;
  250. ++MBBI;
  251. if (MBBI != MF->end()) {
  252. // Block is not last in function.
  253. if (!MBB->isSuccessor(MBBI)) {
  254. // Block does not fall through.
  255. if (MBB->empty()) {
  256. report("MBB doesn't fall through but is empty!", MBB);
  257. }
  258. }
  259. if (TII->BlockHasNoFallThrough(*MBB)) {
  260. if (MBB->empty()) {
  261. report("TargetInstrInfo says the block has no fall through, but the "
  262. "block is empty!", MBB);
  263. } else if (!MBB->back().getDesc().isBarrier()) {
  264. report("TargetInstrInfo says the block has no fall through, but the "
  265. "block does not end in a barrier!", MBB);
  266. }
  267. }
  268. } else {
  269. // Block is last in function.
  270. if (MBB->empty()) {
  271. report("MBB is last in function but is empty!", MBB);
  272. }
  273. }
  274. // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
  275. MachineBasicBlock *TBB = 0, *FBB = 0;
  276. SmallVector<MachineOperand, 4> Cond;
  277. if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB),
  278. TBB, FBB, Cond)) {
  279. // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
  280. // check whether its answers match up with reality.
  281. if (!TBB && !FBB) {
  282. // Block falls through to its successor.
  283. MachineFunction::const_iterator MBBI = MBB;
  284. ++MBBI;
  285. if (MBBI == MF->end()) {
  286. // It's possible that the block legitimately ends with a noreturn
  287. // call or an unreachable, in which case it won't actually fall
  288. // out the bottom of the function.
  289. } else if (MBB->succ_empty()) {
  290. // It's possible that the block legitimately ends with a noreturn
  291. // call or an unreachable, in which case it won't actuall fall
  292. // out of the block.
  293. } else if (MBB->succ_size() != 1) {
  294. report("MBB exits via unconditional fall-through but doesn't have "
  295. "exactly one CFG successor!", MBB);
  296. } else if (MBB->succ_begin()[0] != MBBI) {
  297. report("MBB exits via unconditional fall-through but its successor "
  298. "differs from its CFG successor!", MBB);
  299. }
  300. if (!MBB->empty() && MBB->back().getDesc().isBarrier()) {
  301. report("MBB exits via unconditional fall-through but ends with a "
  302. "barrier instruction!", MBB);
  303. }
  304. if (!Cond.empty()) {
  305. report("MBB exits via unconditional fall-through but has a condition!",
  306. MBB);
  307. }
  308. } else if (TBB && !FBB && Cond.empty()) {
  309. // Block unconditionally branches somewhere.
  310. if (MBB->succ_size() != 1) {
  311. report("MBB exits via unconditional branch but doesn't have "
  312. "exactly one CFG successor!", MBB);
  313. } else if (MBB->succ_begin()[0] != TBB) {
  314. report("MBB exits via unconditional branch but the CFG "
  315. "successor doesn't match the actual successor!", MBB);
  316. }
  317. if (MBB->empty()) {
  318. report("MBB exits via unconditional branch but doesn't contain "
  319. "any instructions!", MBB);
  320. } else if (!MBB->back().getDesc().isBarrier()) {
  321. report("MBB exits via unconditional branch but doesn't end with a "
  322. "barrier instruction!", MBB);
  323. } else if (!MBB->back().getDesc().isTerminator()) {
  324. report("MBB exits via unconditional branch but the branch isn't a "
  325. "terminator instruction!", MBB);
  326. }
  327. } else if (TBB && !FBB && !Cond.empty()) {
  328. // Block conditionally branches somewhere, otherwise falls through.
  329. MachineFunction::const_iterator MBBI = MBB;
  330. ++MBBI;
  331. if (MBBI == MF->end()) {
  332. report("MBB conditionally falls through out of function!", MBB);
  333. } if (MBB->succ_size() != 2) {
  334. report("MBB exits via conditional branch/fall-through but doesn't have "
  335. "exactly two CFG successors!", MBB);
  336. } else if ((MBB->succ_begin()[0] == TBB && MBB->succ_end()[1] == MBBI) ||
  337. (MBB->succ_begin()[1] == TBB && MBB->succ_end()[0] == MBBI)) {
  338. report("MBB exits via conditional branch/fall-through but the CFG "
  339. "successors don't match the actual successors!", MBB);
  340. }
  341. if (MBB->empty()) {
  342. report("MBB exits via conditional branch/fall-through but doesn't "
  343. "contain any instructions!", MBB);
  344. } else if (MBB->back().getDesc().isBarrier()) {
  345. report("MBB exits via conditional branch/fall-through but ends with a "
  346. "barrier instruction!", MBB);
  347. } else if (!MBB->back().getDesc().isTerminator()) {
  348. report("MBB exits via conditional branch/fall-through but the branch "
  349. "isn't a terminator instruction!", MBB);
  350. }
  351. } else if (TBB && FBB) {
  352. // Block conditionally branches somewhere, otherwise branches
  353. // somewhere else.
  354. if (MBB->succ_size() != 2) {
  355. report("MBB exits via conditional branch/branch but doesn't have "
  356. "exactly two CFG successors!", MBB);
  357. } else if ((MBB->succ_begin()[0] == TBB && MBB->succ_end()[1] == FBB) ||
  358. (MBB->succ_begin()[1] == TBB && MBB->succ_end()[0] == FBB)) {
  359. report("MBB exits via conditional branch/branch but the CFG "
  360. "successors don't match the actual successors!", MBB);
  361. }
  362. if (MBB->empty()) {
  363. report("MBB exits via conditional branch/branch but doesn't "
  364. "contain any instructions!", MBB);
  365. } else if (!MBB->back().getDesc().isBarrier()) {
  366. report("MBB exits via conditional branch/branch but doesn't end with a "
  367. "barrier instruction!", MBB);
  368. } else if (!MBB->back().getDesc().isTerminator()) {
  369. report("MBB exits via conditional branch/branch but the branch "
  370. "isn't a terminator instruction!", MBB);
  371. }
  372. if (Cond.empty()) {
  373. report("MBB exits via conditinal branch/branch but there's no "
  374. "condition!", MBB);
  375. }
  376. } else {
  377. report("AnalyzeBranch returned invalid data!", MBB);
  378. }
  379. }
  380. regsLive.clear();
  381. for (MachineBasicBlock::const_livein_iterator I = MBB->livein_begin(),
  382. E = MBB->livein_end(); I != E; ++I) {
  383. if (!TargetRegisterInfo::isPhysicalRegister(*I)) {
  384. report("MBB live-in list contains non-physical register", MBB);
  385. continue;
  386. }
  387. regsLive.insert(*I);
  388. for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++)
  389. regsLive.insert(*R);
  390. }
  391. regsLiveInButUnused = regsLive;
  392. const MachineFrameInfo *MFI = MF->getFrameInfo();
  393. assert(MFI && "Function has no frame info");
  394. BitVector PR = MFI->getPristineRegs(MBB);
  395. for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
  396. regsLive.insert(I);
  397. for (const unsigned *R = TRI->getSubRegisters(I); *R; R++)
  398. regsLive.insert(*R);
  399. }
  400. regsKilled.clear();
  401. regsDefined.clear();
  402. }
  403. void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
  404. const TargetInstrDesc &TI = MI->getDesc();
  405. if (MI->getNumOperands() < TI.getNumOperands()) {
  406. report("Too few operands", MI);
  407. *OS << TI.getNumOperands() << " operands expected, but "
  408. << MI->getNumExplicitOperands() << " given.\n";
  409. }
  410. // Check the MachineMemOperands for basic consistency.
  411. for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
  412. E = MI->memoperands_end(); I != E; ++I) {
  413. if ((*I)->isLoad() && !TI.mayLoad())
  414. report("Missing mayLoad flag", MI);
  415. if ((*I)->isStore() && !TI.mayStore())
  416. report("Missing mayStore flag", MI);
  417. }
  418. }
  419. void
  420. MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
  421. const MachineInstr *MI = MO->getParent();
  422. const TargetInstrDesc &TI = MI->getDesc();
  423. // The first TI.NumDefs operands must be explicit register defines
  424. if (MONum < TI.getNumDefs()) {
  425. if (!MO->isReg())
  426. report("Explicit definition must be a register", MO, MONum);
  427. else if (!MO->isDef())
  428. report("Explicit definition marked as use", MO, MONum);
  429. else if (MO->isImplicit())
  430. report("Explicit definition marked as implicit", MO, MONum);
  431. } else if (MONum < TI.getNumOperands()) {
  432. if (MO->isReg()) {
  433. if (MO->isDef())
  434. report("Explicit operand marked as def", MO, MONum);
  435. if (MO->isImplicit())
  436. report("Explicit operand marked as implicit", MO, MONum);
  437. }
  438. } else {
  439. if (MO->isReg() && !MO->isImplicit() && !TI.isVariadic())
  440. report("Extra explicit operand on non-variadic instruction", MO, MONum);
  441. }
  442. switch (MO->getType()) {
  443. case MachineOperand::MO_Register: {
  444. const unsigned Reg = MO->getReg();
  445. if (!Reg)
  446. return;
  447. // Check Live Variables.
  448. if (MO->isUndef()) {
  449. // An <undef> doesn't refer to any register, so just skip it.
  450. } else if (MO->isUse()) {
  451. regsLiveInButUnused.erase(Reg);
  452. if (MO->isKill()) {
  453. addRegWithSubRegs(regsKilled, Reg);
  454. // Tied operands on two-address instuctions MUST NOT have a <kill> flag.
  455. if (MI->isRegTiedToDefOperand(MONum))
  456. report("Illegal kill flag on two-address instruction operand",
  457. MO, MONum);
  458. } else {
  459. // TwoAddress instr modifying a reg is treated as kill+def.
  460. unsigned defIdx;
  461. if (MI->isRegTiedToDefOperand(MONum, &defIdx) &&
  462. MI->getOperand(defIdx).getReg() == Reg)
  463. addRegWithSubRegs(regsKilled, Reg);
  464. }
  465. // Use of a dead register.
  466. if (!regsLive.count(Reg)) {
  467. if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
  468. // Reserved registers may be used even when 'dead'.
  469. if (!isReserved(Reg))
  470. report("Using an undefined physical register", MO, MONum);
  471. } else {
  472. BBInfo &MInfo = MBBInfoMap[MI->getParent()];
  473. // We don't know which virtual registers are live in, so only complain
  474. // if vreg was killed in this MBB. Otherwise keep track of vregs that
  475. // must be live in. PHI instructions are handled separately.
  476. if (MInfo.regsKilled.count(Reg))
  477. report("Using a killed virtual register", MO, MONum);
  478. else if (MI->getOpcode() != TargetInstrInfo::PHI)
  479. MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
  480. }
  481. }
  482. } else {
  483. assert(MO->isDef());
  484. // Register defined.
  485. // TODO: verify that earlyclobber ops are not used.
  486. if (MO->isDead())
  487. addRegWithSubRegs(regsDead, Reg);
  488. else
  489. addRegWithSubRegs(regsDefined, Reg);
  490. }
  491. // Check register classes.
  492. if (MONum < TI.getNumOperands() && !MO->isImplicit()) {
  493. const TargetOperandInfo &TOI = TI.OpInfo[MONum];
  494. unsigned SubIdx = MO->getSubReg();
  495. if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
  496. unsigned sr = Reg;
  497. if (SubIdx) {
  498. unsigned s = TRI->getSubReg(Reg, SubIdx);
  499. if (!s) {
  500. report("Invalid subregister index for physical register",
  501. MO, MONum);
  502. return;
  503. }
  504. sr = s;
  505. }
  506. if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
  507. if (!DRC->contains(sr)) {
  508. report("Illegal physical register for instruction", MO, MONum);
  509. *OS << TRI->getName(sr) << " is not a "
  510. << DRC->getName() << " register.\n";
  511. }
  512. }
  513. } else {
  514. // Virtual register.
  515. const TargetRegisterClass *RC = MRI->getRegClass(Reg);
  516. if (SubIdx) {
  517. if (RC->subregclasses_begin()+SubIdx >= RC->subregclasses_end()) {
  518. report("Invalid subregister index for virtual register", MO, MONum);
  519. return;
  520. }
  521. RC = *(RC->subregclasses_begin()+SubIdx);
  522. }
  523. if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) {
  524. if (RC != DRC && !RC->hasSuperClass(DRC)) {
  525. report("Illegal virtual register for instruction", MO, MONum);
  526. *OS << "Expected a " << DRC->getName() << " register, but got a "
  527. << RC->getName() << " register\n";
  528. }
  529. }
  530. }
  531. }
  532. break;
  533. }
  534. case MachineOperand::MO_MachineBasicBlock:
  535. if (MI->getOpcode() == TargetInstrInfo::PHI) {
  536. if (!MO->getMBB()->isSuccessor(MI->getParent()))
  537. report("PHI operand is not in the CFG", MO, MONum);
  538. }
  539. break;
  540. default:
  541. break;
  542. }
  543. }
  544. void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
  545. BBInfo &MInfo = MBBInfoMap[MI->getParent()];
  546. set_union(MInfo.regsKilled, regsKilled);
  547. set_subtract(regsLive, regsKilled);
  548. regsKilled.clear();
  549. // Verify that both <def> and <def,dead> operands refer to dead registers.
  550. RegVector defs(regsDefined);
  551. defs.append(regsDead.begin(), regsDead.end());
  552. for (RegVector::const_iterator I = defs.begin(), E = defs.end();
  553. I != E; ++I) {
  554. if (regsLive.count(*I)) {
  555. if (TargetRegisterInfo::isPhysicalRegister(*I)) {
  556. if (!allowPhysDoubleDefs && !isReserved(*I) &&
  557. !regsLiveInButUnused.count(*I)) {
  558. report("Redefining a live physical register", MI);
  559. *OS << "Register " << TRI->getName(*I)
  560. << " was defined but already live.\n";
  561. }
  562. } else {
  563. if (!allowVirtDoubleDefs) {
  564. report("Redefining a live virtual register", MI);
  565. *OS << "Virtual register %reg" << *I
  566. << " was defined but already live.\n";
  567. }
  568. }
  569. } else if (TargetRegisterInfo::isVirtualRegister(*I) &&
  570. !MInfo.regsKilled.count(*I)) {
  571. // Virtual register defined without being killed first must be dead on
  572. // entry.
  573. MInfo.vregsDeadIn.insert(std::make_pair(*I, MI));
  574. }
  575. }
  576. set_subtract(regsLive, regsDead); regsDead.clear();
  577. set_union(regsLive, regsDefined); regsDefined.clear();
  578. }
  579. void
  580. MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
  581. MBBInfoMap[MBB].regsLiveOut = regsLive;
  582. regsLive.clear();
  583. }
  584. // Calculate the largest possible vregsPassed sets. These are the registers that
  585. // can pass through an MBB live, but may not be live every time. It is assumed
  586. // that all vregsPassed sets are empty before the call.
  587. void MachineVerifier::calcMaxRegsPassed() {
  588. // First push live-out regs to successors' vregsPassed. Remember the MBBs that
  589. // have any vregsPassed.
  590. DenseSet<const MachineBasicBlock*> todo;
  591. for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
  592. MFI != MFE; ++MFI) {
  593. const MachineBasicBlock &MBB(*MFI);
  594. BBInfo &MInfo = MBBInfoMap[&MBB];
  595. if (!MInfo.reachable)
  596. continue;
  597. for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
  598. SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
  599. BBInfo &SInfo = MBBInfoMap[*SuI];
  600. if (SInfo.addPassed(MInfo.regsLiveOut))
  601. todo.insert(*SuI);
  602. }
  603. }
  604. // Iteratively push vregsPassed to successors. This will converge to the same
  605. // final state regardless of DenseSet iteration order.
  606. while (!todo.empty()) {
  607. const MachineBasicBlock *MBB = *todo.begin();
  608. todo.erase(MBB);
  609. BBInfo &MInfo = MBBInfoMap[MBB];
  610. for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
  611. SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
  612. if (*SuI == MBB)
  613. continue;
  614. BBInfo &SInfo = MBBInfoMap[*SuI];
  615. if (SInfo.addPassed(MInfo.vregsPassed))
  616. todo.insert(*SuI);
  617. }
  618. }
  619. }
  620. // Calculate the minimum vregsPassed set. These are the registers that always
  621. // pass live through an MBB. The calculation assumes that calcMaxRegsPassed has
  622. // been called earlier.
  623. void MachineVerifier::calcMinRegsPassed() {
  624. DenseSet<const MachineBasicBlock*> todo;
  625. for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
  626. MFI != MFE; ++MFI)
  627. todo.insert(MFI);
  628. while (!todo.empty()) {
  629. const MachineBasicBlock *MBB = *todo.begin();
  630. todo.erase(MBB);
  631. BBInfo &MInfo = MBBInfoMap[MBB];
  632. // Remove entries from vRegsPassed that are not live out from all
  633. // reachable predecessors.
  634. RegSet dead;
  635. for (RegSet::iterator I = MInfo.vregsPassed.begin(),
  636. E = MInfo.vregsPassed.end(); I != E; ++I) {
  637. for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
  638. PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
  639. BBInfo &PrInfo = MBBInfoMap[*PrI];
  640. if (PrInfo.reachable && !PrInfo.isLiveOut(*I)) {
  641. dead.insert(*I);
  642. break;
  643. }
  644. }
  645. }
  646. // If any regs removed, we need to recheck successors.
  647. if (!dead.empty()) {
  648. set_subtract(MInfo.vregsPassed, dead);
  649. todo.insert(MBB->succ_begin(), MBB->succ_end());
  650. }
  651. }
  652. }
  653. // Check PHI instructions at the beginning of MBB. It is assumed that
  654. // calcMinRegsPassed has been run so BBInfo::isLiveOut is valid.
  655. void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
  656. for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end();
  657. BBI != BBE && BBI->getOpcode() == TargetInstrInfo::PHI; ++BBI) {
  658. DenseSet<const MachineBasicBlock*> seen;
  659. for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
  660. unsigned Reg = BBI->getOperand(i).getReg();
  661. const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB();
  662. if (!Pre->isSuccessor(MBB))
  663. continue;
  664. seen.insert(Pre);
  665. BBInfo &PrInfo = MBBInfoMap[Pre];
  666. if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
  667. report("PHI operand is not live-out from predecessor",
  668. &BBI->getOperand(i), i);
  669. }
  670. // Did we see all predecessors?
  671. for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
  672. PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
  673. if (!seen.count(*PrI)) {
  674. report("Missing PHI operand", BBI);
  675. *OS << "MBB #" << (*PrI)->getNumber()
  676. << " is a predecessor according to the CFG.\n";
  677. }
  678. }
  679. }
  680. }
  681. void MachineVerifier::visitMachineFunctionAfter() {
  682. calcMaxRegsPassed();
  683. // With the maximal set of vregsPassed we can verify dead-in registers.
  684. for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
  685. MFI != MFE; ++MFI) {
  686. BBInfo &MInfo = MBBInfoMap[MFI];
  687. // Skip unreachable MBBs.
  688. if (!MInfo.reachable)
  689. continue;
  690. for (MachineBasicBlock::const_pred_iterator PrI = MFI->pred_begin(),
  691. PrE = MFI->pred_end(); PrI != PrE; ++PrI) {
  692. BBInfo &PrInfo = MBBInfoMap[*PrI];
  693. if (!PrInfo.reachable)
  694. continue;
  695. // Verify physical live-ins. EH landing pads have magic live-ins so we
  696. // ignore them.
  697. if (!MFI->isLandingPad()) {
  698. for (MachineBasicBlock::const_livein_iterator I = MFI->livein_begin(),
  699. E = MFI->livein_end(); I != E; ++I) {
  700. if (TargetRegisterInfo::isPhysicalRegister(*I) &&
  701. !isReserved (*I) && !PrInfo.isLiveOut(*I)) {
  702. report("Live-in physical register is not live-out from predecessor",
  703. MFI);
  704. *OS << "Register " << TRI->getName(*I)
  705. << " is not live-out from MBB #" << (*PrI)->getNumber()
  706. << ".\n";
  707. }
  708. }
  709. }
  710. // Verify dead-in virtual registers.
  711. if (!allowVirtDoubleDefs) {
  712. for (RegMap::iterator I = MInfo.vregsDeadIn.begin(),
  713. E = MInfo.vregsDeadIn.end(); I != E; ++I) {
  714. // DeadIn register must be in neither regsLiveOut or vregsPassed of
  715. // any predecessor.
  716. if (PrInfo.isLiveOut(I->first)) {
  717. report("Live-in virtual register redefined", I->second);
  718. *OS << "Register %reg" << I->first
  719. << " was live-out from predecessor MBB #"
  720. << (*PrI)->getNumber() << ".\n";
  721. }
  722. }
  723. }
  724. }
  725. }
  726. calcMinRegsPassed();
  727. // With the minimal set of vregsPassed we can verify live-in virtual
  728. // registers, including PHI instructions.
  729. for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
  730. MFI != MFE; ++MFI) {
  731. BBInfo &MInfo = MBBInfoMap[MFI];
  732. // Skip unreachable MBBs.
  733. if (!MInfo.reachable)
  734. continue;
  735. checkPHIOps(MFI);
  736. for (MachineBasicBlock::const_pred_iterator PrI = MFI->pred_begin(),
  737. PrE = MFI->pred_end(); PrI != PrE; ++PrI) {
  738. BBInfo &PrInfo = MBBInfoMap[*PrI];
  739. if (!PrInfo.reachable)
  740. continue;
  741. for (RegMap::iterator I = MInfo.vregsLiveIn.begin(),
  742. E = MInfo.vregsLiveIn.end(); I != E; ++I) {
  743. if (!PrInfo.isLiveOut(I->first)) {
  744. report("Used virtual register is not live-in", I->second);
  745. *OS << "Register %reg" << I->first
  746. << " is not live-out from predecessor MBB #"
  747. << (*PrI)->getNumber()
  748. << ".\n";
  749. }
  750. }
  751. }
  752. }
  753. }