blob: c8845d8382826d7fdf5b0504a61e67c6669a4c61 [file] [log] [blame]
Owen Anderson9b8f34f2007-10-31 03:30:14 +00001//===- MachineDominators.cpp - Machine Dominator Calculation --------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Owen Anderson9b8f34f2007-10-31 03:30:14 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements simple dominator construction algorithms for finding
10// forward dominators on machine functions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MachineDominators.h"
Benjamin Kramer012b1512015-02-27 23:13:13 +000015#include "llvm/ADT/SmallBitVector.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/CodeGen/Passes.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080017#include "llvm/InitializePasses.h"
Chad Rosierfd3428082016-06-24 13:32:22 +000018#include "llvm/Support/CommandLine.h"
Owen Anderson9b8f34f2007-10-31 03:30:14 +000019
20using namespace llvm;
21
Jakub Kuderski56b52a22019-10-01 15:23:27 +000022namespace llvm {
Chad Rosiere2185fd2016-06-24 17:15:04 +000023// Always verify dominfo if expensive checking is enabled.
Chad Rosierfd3428082016-06-24 13:32:22 +000024#ifdef EXPENSIVE_CHECKS
Jakub Kuderski56b52a22019-10-01 15:23:27 +000025bool VerifyMachineDomInfo = true;
Chad Rosierfd3428082016-06-24 13:32:22 +000026#else
Jakub Kuderski56b52a22019-10-01 15:23:27 +000027bool VerifyMachineDomInfo = false;
Chad Rosierfd3428082016-06-24 13:32:22 +000028#endif
Jakub Kuderski56b52a22019-10-01 15:23:27 +000029} // namespace llvm
30
Chad Rosierfd3428082016-06-24 13:32:22 +000031static cl::opt<bool, true> VerifyMachineDomInfoX(
Zachary Turner8065f0b2017-12-01 00:53:10 +000032 "verify-machine-dom-info", cl::location(VerifyMachineDomInfo), cl::Hidden,
Chad Rosierfd3428082016-06-24 13:32:22 +000033 cl::desc("Verify machine dominator info (time consuming)"));
34
John McCall323c30c2009-12-16 00:13:24 +000035namespace llvm {
Benjamin Kramera667d1a2015-07-13 17:21:31 +000036template class DomTreeNodeBase<MachineBasicBlock>;
Jakub Kuderskib292c222017-07-14 18:26:09 +000037template class DominatorTreeBase<MachineBasicBlock, false>; // DomTreeBase
John McCall323c30c2009-12-16 00:13:24 +000038}
Owen Anderson9b8f34f2007-10-31 03:30:14 +000039
Chris Lattner647e61a2008-01-05 20:15:42 +000040char MachineDominatorTree::ID = 0;
41
Owen Andersond31d82d2010-08-23 17:52:01 +000042INITIALIZE_PASS(MachineDominatorTree, "machinedomtree",
Owen Andersondf7a4f22010-10-07 22:25:06 +000043 "MachineDominator Tree Construction", true, true)
Bill Wendling0c209432008-01-04 20:54:55 +000044
Owen Andersona7aed182010-08-06 18:33:48 +000045char &llvm::MachineDominatorsID = MachineDominatorTree::ID;
Dan Gohman906152a2009-01-05 17:59:02 +000046
47void MachineDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.setPreservesAll();
49 MachineFunctionPass::getAnalysisUsage(AU);
50}
51
52bool MachineDominatorTree::runOnMachineFunction(MachineFunction &F) {
Hiroshi Yamauchi75f72f62019-10-28 12:35:34 -070053 calculate(F);
54 return false;
55}
56
57void MachineDominatorTree::calculate(MachineFunction &F) {
Quentin Colombetabea99f2014-08-13 21:00:07 +000058 CriticalEdgesToSplit.clear();
59 NewBBs.clear();
Jakub Kuderskib292c222017-07-14 18:26:09 +000060 DT.reset(new DomTreeBase<MachineBasicBlock>());
Dan Gohman906152a2009-01-05 17:59:02 +000061 DT->recalculate(F);
Dan Gohman906152a2009-01-05 17:59:02 +000062}
63
64MachineDominatorTree::MachineDominatorTree()
Owen Andersona7aed182010-08-06 18:33:48 +000065 : MachineFunctionPass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000066 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
Dan Gohman906152a2009-01-05 17:59:02 +000067}
68
69void MachineDominatorTree::releaseMemory() {
Serge Pavlove2bf6972017-03-02 12:00:10 +000070 CriticalEdgesToSplit.clear();
71 DT.reset(nullptr);
Dan Gohman906152a2009-01-05 17:59:02 +000072}
Chris Lattnerb1d782b2009-08-23 05:17:37 +000073
Chad Rosierfd3428082016-06-24 13:32:22 +000074void MachineDominatorTree::verifyAnalysis() const {
Jakub Kuderski925c2852019-10-01 18:27:14 +000075 if (DT && VerifyMachineDomInfo)
76 if (!DT->verify(DomTreeT::VerificationLevel::Basic)) {
77 errs() << "MachineDominatorTree verification failed\n";
David Green7c35de12018-02-28 11:00:08 +000078 abort();
79 }
Chad Rosierfd3428082016-06-24 13:32:22 +000080}
81
Chris Lattner13626022009-08-23 06:03:38 +000082void MachineDominatorTree::print(raw_ostream &OS, const Module*) const {
Serge Pavlove2bf6972017-03-02 12:00:10 +000083 if (DT)
84 DT->print(OS);
Chris Lattnerb1d782b2009-08-23 05:17:37 +000085}
Benjamin Kramer012b1512015-02-27 23:13:13 +000086
87void MachineDominatorTree::applySplitCriticalEdges() const {
88 // Bail out early if there is nothing to do.
89 if (CriticalEdgesToSplit.empty())
90 return;
91
92 // For each element in CriticalEdgesToSplit, remember whether or not element
93 // is the new immediate domminator of its successor. The mapping is done by
94 // index, i.e., the information for the ith element of CriticalEdgesToSplit is
95 // the ith element of IsNewIDom.
96 SmallBitVector IsNewIDom(CriticalEdgesToSplit.size(), true);
97 size_t Idx = 0;
98
99 // Collect all the dominance properties info, before invalidating
100 // the underlying DT.
101 for (CriticalEdge &Edge : CriticalEdgesToSplit) {
102 // Update dominator information.
103 MachineBasicBlock *Succ = Edge.ToBB;
104 MachineDomTreeNode *SuccDTNode = DT->getNode(Succ);
105
106 for (MachineBasicBlock *PredBB : Succ->predecessors()) {
107 if (PredBB == Edge.NewBB)
108 continue;
109 // If we are in this situation:
110 // FromBB1 FromBB2
111 // + +
112 // + + + +
113 // + + + +
114 // ... Split1 Split2 ...
115 // + +
116 // + +
117 // +
118 // Succ
119 // Instead of checking the domiance property with Split2, we check it with
120 // FromBB2 since Split2 is still unknown of the underlying DT structure.
121 if (NewBBs.count(PredBB)) {
122 assert(PredBB->pred_size() == 1 && "A basic block resulting from a "
123 "critical edge split has more "
124 "than one predecessor!");
125 PredBB = *PredBB->pred_begin();
126 }
127 if (!DT->dominates(SuccDTNode, DT->getNode(PredBB))) {
128 IsNewIDom[Idx] = false;
129 break;
130 }
131 }
132 ++Idx;
133 }
134
135 // Now, update DT with the collected dominance properties info.
136 Idx = 0;
137 for (CriticalEdge &Edge : CriticalEdgesToSplit) {
138 // We know FromBB dominates NewBB.
139 MachineDomTreeNode *NewDTNode = DT->addNewBlock(Edge.NewBB, Edge.FromBB);
140
141 // If all the other predecessors of "Succ" are dominated by "Succ" itself
142 // then the new block is the new immediate dominator of "Succ". Otherwise,
143 // the new block doesn't dominate anything.
144 if (IsNewIDom[Idx])
145 DT->changeImmediateDominator(DT->getNode(Edge.ToBB), NewDTNode);
146 ++Idx;
147 }
148 NewBBs.clear();
149 CriticalEdgesToSplit.clear();
150}