MachineDominators.cpp 4.1 KB

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