LLVM  4.0.0
MachineDominators.cpp
Go to the documentation of this file.
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 
16 #include "llvm/CodeGen/Passes.h"
19 
20 using namespace llvm;
21 
22 // Always verify dominfo if expensive checking is enabled.
23 #ifdef EXPENSIVE_CHECKS
24 static bool VerifyMachineDomInfo = true;
25 #else
26 static bool VerifyMachineDomInfo = false;
27 #endif
29  "verify-machine-dom-info", cl::location(VerifyMachineDomInfo),
30  cl::desc("Verify machine dominator info (time consuming)"));
31 
32 namespace llvm {
35 }
36 
38 
39 INITIALIZE_PASS(MachineDominatorTree, "machinedomtree",
40  "MachineDominator Tree Construction", true, true)
41 
43 
44 void MachineDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const {
45  AU.setPreservesAll();
47 }
48 
50  CriticalEdgesToSplit.clear();
51  NewBBs.clear();
52  DT->recalculate(F);
53 
54  return false;
55 }
56 
58  : MachineFunctionPass(ID) {
61 }
62 
64  delete DT;
65 }
66 
68  DT->releaseMemory();
69 }
70 
73  verifyDomTree();
74 }
75 
77  DT->print(OS);
78 }
79 
80 void MachineDominatorTree::applySplitCriticalEdges() const {
81  // Bail out early if there is nothing to do.
82  if (CriticalEdgesToSplit.empty())
83  return;
84 
85  // For each element in CriticalEdgesToSplit, remember whether or not element
86  // is the new immediate domminator of its successor. The mapping is done by
87  // index, i.e., the information for the ith element of CriticalEdgesToSplit is
88  // the ith element of IsNewIDom.
89  SmallBitVector IsNewIDom(CriticalEdgesToSplit.size(), true);
90  size_t Idx = 0;
91 
92  // Collect all the dominance properties info, before invalidating
93  // the underlying DT.
94  for (CriticalEdge &Edge : CriticalEdgesToSplit) {
95  // Update dominator information.
96  MachineBasicBlock *Succ = Edge.ToBB;
97  MachineDomTreeNode *SuccDTNode = DT->getNode(Succ);
98 
99  for (MachineBasicBlock *PredBB : Succ->predecessors()) {
100  if (PredBB == Edge.NewBB)
101  continue;
102  // If we are in this situation:
103  // FromBB1 FromBB2
104  // + +
105  // + + + +
106  // + + + +
107  // ... Split1 Split2 ...
108  // + +
109  // + +
110  // +
111  // Succ
112  // Instead of checking the domiance property with Split2, we check it with
113  // FromBB2 since Split2 is still unknown of the underlying DT structure.
114  if (NewBBs.count(PredBB)) {
115  assert(PredBB->pred_size() == 1 && "A basic block resulting from a "
116  "critical edge split has more "
117  "than one predecessor!");
118  PredBB = *PredBB->pred_begin();
119  }
120  if (!DT->dominates(SuccDTNode, DT->getNode(PredBB))) {
121  IsNewIDom[Idx] = false;
122  break;
123  }
124  }
125  ++Idx;
126  }
127 
128  // Now, update DT with the collected dominance properties info.
129  Idx = 0;
130  for (CriticalEdge &Edge : CriticalEdgesToSplit) {
131  // We know FromBB dominates NewBB.
132  MachineDomTreeNode *NewDTNode = DT->addNewBlock(Edge.NewBB, Edge.FromBB);
133 
134  // If all the other predecessors of "Succ" are dominated by "Succ" itself
135  // then the new block is the new immediate dominator of "Succ". Otherwise,
136  // the new block doesn't dominate anything.
137  if (IsNewIDom[Idx])
138  DT->changeImmediateDominator(DT->getNode(Edge.ToBB), NewDTNode);
139  ++Idx;
140  }
141  NewBBs.clear();
142  CriticalEdgesToSplit.clear();
143 }
144 
147 
148  MachineDominatorTree OtherDT;
149  OtherDT.DT->recalculate(F);
150  if (compare(OtherDT)) {
151  errs() << "MachineDominatorTree is not up to date!\nComputed:\n";
152  print(errs(), nullptr);
153  errs() << "\nActual:\n";
154  OtherDT.print(errs(), nullptr);
155  abort();
156  }
157 }
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
static cl::opt< bool, true > VerifyMachineDomInfoX("verify-machine-dom-info", cl::location(VerifyMachineDomInfo), cl::desc("Verify machine dominator info (time consuming)"))
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
MachineBasicBlock * getRoot() const
bool compare(const MachineDominatorTree &Other) const
Returns false if the other dominator tree matches this dominator tree.
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
#define F(x, y, z)
Definition: MD5.cpp:51
Base class for the actual dominator tree node.
Core dominator tree base class.
Definition: LoopInfo.h:59
void initializeMachineDominatorTreePass(PassRegistry &)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
void verifyDomTree() const
Verify the correctness of the domtree by re-computing it.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Represent the analysis usage information of a pass.
iterator_range< pred_iterator > predecessors()
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:36
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool VerifyMachineDomInfo
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
void print(raw_ostream &OS, const Module *) const override
print - Print out the internal state of the pass.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:411