blob: 467a2e4eb428c56a35a4ec545756710c25533254 [file] [log] [blame]
Owen Anderson9b8f34f2007-10-31 03:30:14 +00001//===- MachineDominators.cpp - Machine Dominator Calculation --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Owen Anderson9b8f34f2007-10-31 03:30:14 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements simple dominator construction algorithms for finding
11// forward dominators on machine functions.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/MachineDominators.h"
Bill Wendling0c209432008-01-04 20:54:55 +000016#include "llvm/CodeGen/Passes.h"
Benjamin Kramer012b1512015-02-27 23:13:13 +000017#include "llvm/ADT/SmallBitVector.h"
Owen Anderson9b8f34f2007-10-31 03:30:14 +000018
19using namespace llvm;
20
John McCall323c30c2009-12-16 00:13:24 +000021namespace llvm {
Owen Anderson9b8f34f2007-10-31 03:30:14 +000022TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
23TEMPLATE_INSTANTIATION(class DominatorTreeBase<MachineBasicBlock>);
John McCall323c30c2009-12-16 00:13:24 +000024}
Owen Anderson9b8f34f2007-10-31 03:30:14 +000025
Chris Lattner647e61a2008-01-05 20:15:42 +000026char MachineDominatorTree::ID = 0;
27
Owen Andersond31d82d2010-08-23 17:52:01 +000028INITIALIZE_PASS(MachineDominatorTree, "machinedomtree",
Owen Andersondf7a4f22010-10-07 22:25:06 +000029 "MachineDominator Tree Construction", true, true)
Bill Wendling0c209432008-01-04 20:54:55 +000030
Owen Andersona7aed182010-08-06 18:33:48 +000031char &llvm::MachineDominatorsID = MachineDominatorTree::ID;
Dan Gohman906152a2009-01-05 17:59:02 +000032
33void MachineDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const {
34 AU.setPreservesAll();
35 MachineFunctionPass::getAnalysisUsage(AU);
36}
37
38bool MachineDominatorTree::runOnMachineFunction(MachineFunction &F) {
Quentin Colombetabea99f2014-08-13 21:00:07 +000039 CriticalEdgesToSplit.clear();
40 NewBBs.clear();
Dan Gohman906152a2009-01-05 17:59:02 +000041 DT->recalculate(F);
42
43 return false;
44}
45
46MachineDominatorTree::MachineDominatorTree()
Owen Andersona7aed182010-08-06 18:33:48 +000047 : MachineFunctionPass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000048 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
Dan Gohman906152a2009-01-05 17:59:02 +000049 DT = new DominatorTreeBase<MachineBasicBlock>(false);
50}
51
52MachineDominatorTree::~MachineDominatorTree() {
Dan Gohman906152a2009-01-05 17:59:02 +000053 delete DT;
54}
55
56void MachineDominatorTree::releaseMemory() {
57 DT->releaseMemory();
58}
Chris Lattnerb1d782b2009-08-23 05:17:37 +000059
Chris Lattner13626022009-08-23 06:03:38 +000060void MachineDominatorTree::print(raw_ostream &OS, const Module*) const {
61 DT->print(OS);
Chris Lattnerb1d782b2009-08-23 05:17:37 +000062}
Benjamin Kramer012b1512015-02-27 23:13:13 +000063
64void MachineDominatorTree::applySplitCriticalEdges() const {
65 // Bail out early if there is nothing to do.
66 if (CriticalEdgesToSplit.empty())
67 return;
68
69 // For each element in CriticalEdgesToSplit, remember whether or not element
70 // is the new immediate domminator of its successor. The mapping is done by
71 // index, i.e., the information for the ith element of CriticalEdgesToSplit is
72 // the ith element of IsNewIDom.
73 SmallBitVector IsNewIDom(CriticalEdgesToSplit.size(), true);
74 size_t Idx = 0;
75
76 // Collect all the dominance properties info, before invalidating
77 // the underlying DT.
78 for (CriticalEdge &Edge : CriticalEdgesToSplit) {
79 // Update dominator information.
80 MachineBasicBlock *Succ = Edge.ToBB;
81 MachineDomTreeNode *SuccDTNode = DT->getNode(Succ);
82
83 for (MachineBasicBlock *PredBB : Succ->predecessors()) {
84 if (PredBB == Edge.NewBB)
85 continue;
86 // If we are in this situation:
87 // FromBB1 FromBB2
88 // + +
89 // + + + +
90 // + + + +
91 // ... Split1 Split2 ...
92 // + +
93 // + +
94 // +
95 // Succ
96 // Instead of checking the domiance property with Split2, we check it with
97 // FromBB2 since Split2 is still unknown of the underlying DT structure.
98 if (NewBBs.count(PredBB)) {
99 assert(PredBB->pred_size() == 1 && "A basic block resulting from a "
100 "critical edge split has more "
101 "than one predecessor!");
102 PredBB = *PredBB->pred_begin();
103 }
104 if (!DT->dominates(SuccDTNode, DT->getNode(PredBB))) {
105 IsNewIDom[Idx] = false;
106 break;
107 }
108 }
109 ++Idx;
110 }
111
112 // Now, update DT with the collected dominance properties info.
113 Idx = 0;
114 for (CriticalEdge &Edge : CriticalEdgesToSplit) {
115 // We know FromBB dominates NewBB.
116 MachineDomTreeNode *NewDTNode = DT->addNewBlock(Edge.NewBB, Edge.FromBB);
117
118 // If all the other predecessors of "Succ" are dominated by "Succ" itself
119 // then the new block is the new immediate dominator of "Succ". Otherwise,
120 // the new block doesn't dominate anything.
121 if (IsNewIDom[Idx])
122 DT->changeImmediateDominator(DT->getNode(Edge.ToBB), NewDTNode);
123 ++Idx;
124 }
125 NewBBs.clear();
126 CriticalEdgesToSplit.clear();
127}