MachineDominators.cpp 5.0 KB

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