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