MachineDominators.cpp 4.9 KB

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