MachineDominators.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //===- MachineDominators.cpp - Machine Dominator Calculation --------------===//
  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. // This file implements simple dominator construction algorithms for finding
  11. // forward dominators on machine functions.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/CodeGen/MachineDominators.h"
  15. #include "llvm/CodeGen/Passes.h"
  16. #include "llvm/ADT/SmallBitVector.h"
  17. #include "llvm/Support/CommandLine.h"
  18. using namespace llvm;
  19. // Always verify dominfo if expensive checking is enabled.
  20. #ifdef EXPENSIVE_CHECKS
  21. static bool VerifyMachineDomInfo = true;
  22. #else
  23. static bool VerifyMachineDomInfo = false;
  24. #endif
  25. static cl::opt<bool, true> VerifyMachineDomInfoX(
  26. "verify-machine-dom-info", cl::location(VerifyMachineDomInfo),
  27. cl::desc("Verify machine dominator info (time consuming)"));
  28. namespace llvm {
  29. template class DomTreeNodeBase<MachineBasicBlock>;
  30. template class DominatorTreeBase<MachineBasicBlock>;
  31. }
  32. char MachineDominatorTree::ID = 0;
  33. INITIALIZE_PASS(MachineDominatorTree, "machinedomtree",
  34. "MachineDominator Tree Construction", true, true)
  35. char &llvm::MachineDominatorsID = MachineDominatorTree::ID;
  36. void MachineDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const {
  37. AU.setPreservesAll();
  38. MachineFunctionPass::getAnalysisUsage(AU);
  39. }
  40. bool MachineDominatorTree::runOnMachineFunction(MachineFunction &F) {
  41. CriticalEdgesToSplit.clear();
  42. NewBBs.clear();
  43. DT->recalculate(F);
  44. return false;
  45. }
  46. MachineDominatorTree::MachineDominatorTree()
  47. : MachineFunctionPass(ID) {
  48. initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
  49. DT = new DominatorTreeBase<MachineBasicBlock>(false);
  50. }
  51. MachineDominatorTree::~MachineDominatorTree() {
  52. delete DT;
  53. }
  54. void MachineDominatorTree::releaseMemory() {
  55. DT->releaseMemory();
  56. }
  57. void MachineDominatorTree::verifyAnalysis() const {
  58. if (VerifyMachineDomInfo && isExecuted())
  59. verifyDomTree();
  60. }
  61. void MachineDominatorTree::print(raw_ostream &OS, const Module*) const {
  62. DT->print(OS);
  63. }
  64. void MachineDominatorTree::applySplitCriticalEdges() const {
  65. // Bail out early if there is nothing to do.
  66. if (CriticalEdgesToSplit.empty())
  67. return;
  68. // For each element in CriticalEdgesToSplit, remember whether or not element
  69. // is the new immediate domminator of its successor. The mapping is done by
  70. // index, i.e., the information for the ith element of CriticalEdgesToSplit is
  71. // the ith element of IsNewIDom.
  72. SmallBitVector IsNewIDom(CriticalEdgesToSplit.size(), true);
  73. size_t Idx = 0;
  74. // Collect all the dominance properties info, before invalidating
  75. // the underlying DT.
  76. for (CriticalEdge &Edge : CriticalEdgesToSplit) {
  77. // Update dominator information.
  78. MachineBasicBlock *Succ = Edge.ToBB;
  79. MachineDomTreeNode *SuccDTNode = DT->getNode(Succ);
  80. for (MachineBasicBlock *PredBB : Succ->predecessors()) {
  81. if (PredBB == Edge.NewBB)
  82. continue;
  83. // If we are in this situation:
  84. // FromBB1 FromBB2
  85. // + +
  86. // + + + +
  87. // + + + +
  88. // ... Split1 Split2 ...
  89. // + +
  90. // + +
  91. // +
  92. // Succ
  93. // Instead of checking the domiance property with Split2, we check it with
  94. // FromBB2 since Split2 is still unknown of the underlying DT structure.
  95. if (NewBBs.count(PredBB)) {
  96. assert(PredBB->pred_size() == 1 && "A basic block resulting from a "
  97. "critical edge split has more "
  98. "than one predecessor!");
  99. PredBB = *PredBB->pred_begin();
  100. }
  101. if (!DT->dominates(SuccDTNode, DT->getNode(PredBB))) {
  102. IsNewIDom[Idx] = false;
  103. break;
  104. }
  105. }
  106. ++Idx;
  107. }
  108. // Now, update DT with the collected dominance properties info.
  109. Idx = 0;
  110. for (CriticalEdge &Edge : CriticalEdgesToSplit) {
  111. // We know FromBB dominates NewBB.
  112. MachineDomTreeNode *NewDTNode = DT->addNewBlock(Edge.NewBB, Edge.FromBB);
  113. // If all the other predecessors of "Succ" are dominated by "Succ" itself
  114. // then the new block is the new immediate dominator of "Succ". Otherwise,
  115. // the new block doesn't dominate anything.
  116. if (IsNewIDom[Idx])
  117. DT->changeImmediateDominator(DT->getNode(Edge.ToBB), NewDTNode);
  118. ++Idx;
  119. }
  120. NewBBs.clear();
  121. CriticalEdgesToSplit.clear();
  122. }
  123. void MachineDominatorTree::verifyDomTree() const {
  124. MachineFunction &F = *getRoot()->getParent();
  125. MachineDominatorTree OtherDT;
  126. OtherDT.DT->recalculate(F);
  127. if (compare(OtherDT)) {
  128. errs() << "MachineDominatorTree is not up to date!\nComputed:\n";
  129. print(errs(), nullptr);
  130. errs() << "\nActual:\n";
  131. OtherDT.print(errs(), nullptr);
  132. abort();
  133. }
  134. }