LLVM  4.0.0
IteratedDominanceFrontier.cpp
Go to the documentation of this file.
1 //===- IteratedDominanceFrontier.cpp - Compute IDF ------------------------===//
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 // Compute iterated dominance frontiers using a linear time algorithm.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/IR/CFG.h"
16 #include "llvm/IR/Dominators.h"
17 #include <queue>
18 
19 namespace llvm {
20 template <class NodeTy>
22  SmallVectorImpl<BasicBlock *> &PHIBlocks) {
23  // If we haven't computed dominator tree levels, do so now.
24  if (DomLevels.empty()) {
25  for (auto DFI = df_begin(DT.getRootNode()), DFE = df_end(DT.getRootNode());
26  DFI != DFE; ++DFI) {
27  DomLevels[*DFI] = DFI.getPathLength() - 1;
28  }
29  }
30 
31  // Use a priority queue keyed on dominator tree level so that inserted nodes
32  // are handled from the bottom of the dominator tree upwards.
33  typedef std::pair<DomTreeNode *, unsigned> DomTreeNodePair;
34  typedef std::priority_queue<DomTreeNodePair, SmallVector<DomTreeNodePair, 32>,
35  less_second> IDFPriorityQueue;
36  IDFPriorityQueue PQ;
37 
38  for (BasicBlock *BB : *DefBlocks) {
39  if (DomTreeNode *Node = DT.getNode(BB))
40  PQ.push(std::make_pair(Node, DomLevels.lookup(Node)));
41  }
42 
45  SmallPtrSet<DomTreeNode *, 32> VisitedWorklist;
46 
47  while (!PQ.empty()) {
48  DomTreeNodePair RootPair = PQ.top();
49  PQ.pop();
50  DomTreeNode *Root = RootPair.first;
51  unsigned RootLevel = RootPair.second;
52 
53  // Walk all dominator tree children of Root, inspecting their CFG edges with
54  // targets elsewhere on the dominator tree. Only targets whose level is at
55  // most Root's level are added to the iterated dominance frontier of the
56  // definition set.
57 
58  Worklist.clear();
59  Worklist.push_back(Root);
60  VisitedWorklist.insert(Root);
61 
62  while (!Worklist.empty()) {
63  DomTreeNode *Node = Worklist.pop_back_val();
64  BasicBlock *BB = Node->getBlock();
65  // Succ is the successor in the direction we are calculating IDF, so it is
66  // successor for IDF, and predecessor for Reverse IDF.
67  for (auto SuccIter = GraphTraits<NodeTy>::child_begin(BB),
69  SuccIter != End; ++SuccIter) {
70  BasicBlock *Succ = *SuccIter;
71  DomTreeNode *SuccNode = DT.getNode(Succ);
72 
73  // Quickly skip all CFG edges that are also dominator tree edges instead
74  // of catching them below.
75  if (SuccNode->getIDom() == Node)
76  continue;
77 
78  unsigned SuccLevel = DomLevels.lookup(SuccNode);
79  if (SuccLevel > RootLevel)
80  continue;
81 
82  if (!VisitedPQ.insert(SuccNode).second)
83  continue;
84 
85  BasicBlock *SuccBB = SuccNode->getBlock();
86  if (useLiveIn && !LiveInBlocks->count(SuccBB))
87  continue;
88 
89  PHIBlocks.emplace_back(SuccBB);
90  if (!DefBlocks->count(SuccBB))
91  PQ.push(std::make_pair(SuccNode, SuccLevel));
92  }
93 
94  for (auto DomChild : *Node) {
95  if (VisitedWorklist.insert(DomChild).second)
96  Worklist.push_back(DomChild);
97  }
98  }
99  }
100 }
101 
102 template class IDFCalculator<BasicBlock *>;
104 }
Function object to check whether the second component of a std::pair compares less than the second co...
Definition: STLExtras.h:598
void calculate(SmallVectorImpl< BasicBlock * > &IDFBlocks)
Calculate iterated dominance frontiers.
DomTreeNodeBase< NodeT > * getIDom() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
Base class for the actual dominator tree node.
NodeT * getBlock() const
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
df_iterator< T > df_end(const T &G)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:368
static const unsigned End
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:425
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:382
df_iterator< T > df_begin(const T &G)
void emplace_back(ArgTypes &&...Args)
Definition: SmallVector.h:635
Determine the iterated dominance frontier, given a set of defining blocks, and optionally, a set of live-in blocks.