blob: 6b28026264563e021df31f71948a407110a6be95 [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"
Benjamin Kramer012b1512015-02-27 23:13:13 +000016#include "llvm/ADT/SmallBitVector.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "llvm/CodeGen/Passes.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
Chad Rosiere2185fd2016-06-24 17:15:04 +000022// Always verify dominfo if expensive checking is enabled.
Chad Rosierfd3428082016-06-24 13:32:22 +000023#ifdef EXPENSIVE_CHECKS
Chad Rosiere2185fd2016-06-24 17:15:04 +000024static bool VerifyMachineDomInfo = true;
Chad Rosierfd3428082016-06-24 13:32:22 +000025#else
26static bool VerifyMachineDomInfo = false;
27#endif
28static cl::opt<bool, true> VerifyMachineDomInfoX(
Zachary Turner8065f0b2017-12-01 00:53:10 +000029 "verify-machine-dom-info", cl::location(VerifyMachineDomInfo), cl::Hidden,
Chad Rosierfd3428082016-06-24 13:32:22 +000030 cl::desc("Verify machine dominator info (time consuming)"));
31
John McCall323c30c2009-12-16 00:13:24 +000032namespace llvm {
Benjamin Kramera667d1a2015-07-13 17:21:31 +000033template class DomTreeNodeBase<MachineBasicBlock>;
Jakub Kuderskib292c222017-07-14 18:26:09 +000034template class DominatorTreeBase<MachineBasicBlock, false>; // DomTreeBase
John McCall323c30c2009-12-16 00:13:24 +000035}
Owen Anderson9b8f34f2007-10-31 03:30:14 +000036
Chris Lattner647e61a2008-01-05 20:15:42 +000037char MachineDominatorTree::ID = 0;
38
Owen Andersond31d82d2010-08-23 17:52:01 +000039INITIALIZE_PASS(MachineDominatorTree, "machinedomtree",
Owen Andersondf7a4f22010-10-07 22:25:06 +000040 "MachineDominator Tree Construction", true, true)
Bill Wendling0c209432008-01-04 20:54:55 +000041
Owen Andersona7aed182010-08-06 18:33:48 +000042char &llvm::MachineDominatorsID = MachineDominatorTree::ID;
Dan Gohman906152a2009-01-05 17:59:02 +000043
44void MachineDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.setPreservesAll();
46 MachineFunctionPass::getAnalysisUsage(AU);
47}
48
49bool MachineDominatorTree::runOnMachineFunction(MachineFunction &F) {
Quentin Colombetabea99f2014-08-13 21:00:07 +000050 CriticalEdgesToSplit.clear();
51 NewBBs.clear();
Jakub Kuderskib292c222017-07-14 18:26:09 +000052 DT.reset(new DomTreeBase<MachineBasicBlock>());
Dan Gohman906152a2009-01-05 17:59:02 +000053 DT->recalculate(F);
Dan Gohman906152a2009-01-05 17:59:02 +000054 return false;
55}
56
57MachineDominatorTree::MachineDominatorTree()
Owen Andersona7aed182010-08-06 18:33:48 +000058 : MachineFunctionPass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000059 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
Dan Gohman906152a2009-01-05 17:59:02 +000060}
61
62void MachineDominatorTree::releaseMemory() {
Serge Pavlove2bf6972017-03-02 12:00:10 +000063 CriticalEdgesToSplit.clear();
64 DT.reset(nullptr);
Dan Gohman906152a2009-01-05 17:59:02 +000065}
Chris Lattnerb1d782b2009-08-23 05:17:37 +000066
Chad Rosierfd3428082016-06-24 13:32:22 +000067void MachineDominatorTree::verifyAnalysis() const {
David Green7c35de12018-02-28 11:00:08 +000068 if (DT && VerifyMachineDomInfo) {
69 MachineFunction &F = *getRoot()->getParent();
70
71 DomTreeBase<MachineBasicBlock> OtherDT;
72 OtherDT.recalculate(F);
73 if (getRootNode()->getBlock() != OtherDT.getRootNode()->getBlock() ||
74 DT->compare(OtherDT)) {
75 errs() << "MachineDominatorTree for function " << F.getName()
76 << " is not up to date!\nComputed:\n";
77 DT->print(errs());
78 errs() << "\nActual:\n";
79 OtherDT.print(errs());
80 abort();
81 }
82 }
Chad Rosierfd3428082016-06-24 13:32:22 +000083}
84
Chris Lattner13626022009-08-23 06:03:38 +000085void MachineDominatorTree::print(raw_ostream &OS, const Module*) const {
Serge Pavlove2bf6972017-03-02 12:00:10 +000086 if (DT)
87 DT->print(OS);
Chris Lattnerb1d782b2009-08-23 05:17:37 +000088}
Benjamin Kramer012b1512015-02-27 23:13:13 +000089
90void MachineDominatorTree::applySplitCriticalEdges() const {
91 // Bail out early if there is nothing to do.
92 if (CriticalEdgesToSplit.empty())
93 return;
94
95 // For each element in CriticalEdgesToSplit, remember whether or not element
96 // is the new immediate domminator of its successor. The mapping is done by
97 // index, i.e., the information for the ith element of CriticalEdgesToSplit is
98 // the ith element of IsNewIDom.
99 SmallBitVector IsNewIDom(CriticalEdgesToSplit.size(), true);
100 size_t Idx = 0;
101
102 // Collect all the dominance properties info, before invalidating
103 // the underlying DT.
104 for (CriticalEdge &Edge : CriticalEdgesToSplit) {
105 // Update dominator information.
106 MachineBasicBlock *Succ = Edge.ToBB;
107 MachineDomTreeNode *SuccDTNode = DT->getNode(Succ);
108
109 for (MachineBasicBlock *PredBB : Succ->predecessors()) {
110 if (PredBB == Edge.NewBB)
111 continue;
112 // If we are in this situation:
113 // FromBB1 FromBB2
114 // + +
115 // + + + +
116 // + + + +
117 // ... Split1 Split2 ...
118 // + +
119 // + +
120 // +
121 // Succ
122 // Instead of checking the domiance property with Split2, we check it with
123 // FromBB2 since Split2 is still unknown of the underlying DT structure.
124 if (NewBBs.count(PredBB)) {
125 assert(PredBB->pred_size() == 1 && "A basic block resulting from a "
126 "critical edge split has more "
127 "than one predecessor!");
128 PredBB = *PredBB->pred_begin();
129 }
130 if (!DT->dominates(SuccDTNode, DT->getNode(PredBB))) {
131 IsNewIDom[Idx] = false;
132 break;
133 }
134 }
135 ++Idx;
136 }
137
138 // Now, update DT with the collected dominance properties info.
139 Idx = 0;
140 for (CriticalEdge &Edge : CriticalEdgesToSplit) {
141 // We know FromBB dominates NewBB.
142 MachineDomTreeNode *NewDTNode = DT->addNewBlock(Edge.NewBB, Edge.FromBB);
143
144 // If all the other predecessors of "Succ" are dominated by "Succ" itself
145 // then the new block is the new immediate dominator of "Succ". Otherwise,
146 // the new block doesn't dominate anything.
147 if (IsNewIDom[Idx])
148 DT->changeImmediateDominator(DT->getNode(Edge.ToBB), NewDTNode);
149 ++Idx;
150 }
151 NewBBs.clear();
152 CriticalEdgesToSplit.clear();
153}