]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Analysis/IteratedDominanceFrontier.cpp
Merge ^/head r317503 through r317807.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Analysis / IteratedDominanceFrontier.cpp
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
14 #include "llvm/Analysis/IteratedDominanceFrontier.h"
15 #include "llvm/IR/CFG.h"
16 #include "llvm/IR/Dominators.h"
17 #include <queue>
18
19 namespace llvm {
20 template <class NodeTy>
21 void IDFCalculator<NodeTy>::calculate(
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
43   SmallVector<DomTreeNode *, 32> Worklist;
44   SmallPtrSet<DomTreeNode *, 32> VisitedPQ;
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 *Succ : children<NodeTy>(BB)) {
68         DomTreeNode *SuccNode = DT.getNode(Succ);
69
70         // Quickly skip all CFG edges that are also dominator tree edges instead
71         // of catching them below.
72         if (SuccNode->getIDom() == Node)
73           continue;
74
75         unsigned SuccLevel = DomLevels.lookup(SuccNode);
76         if (SuccLevel > RootLevel)
77           continue;
78
79         if (!VisitedPQ.insert(SuccNode).second)
80           continue;
81
82         BasicBlock *SuccBB = SuccNode->getBlock();
83         if (useLiveIn && !LiveInBlocks->count(SuccBB))
84           continue;
85
86         PHIBlocks.emplace_back(SuccBB);
87         if (!DefBlocks->count(SuccBB))
88           PQ.push(std::make_pair(SuccNode, SuccLevel));
89       }
90
91       for (auto DomChild : *Node) {
92         if (VisitedWorklist.insert(DomChild).second)
93           Worklist.push_back(DomChild);
94       }
95     }
96   }
97 }
98
99 template class IDFCalculator<BasicBlock *>;
100 template class IDFCalculator<Inverse<BasicBlock *>>;
101 }