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