Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 1 | //===- IteratedDominanceFrontier.cpp - Compute IDF ------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // 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 |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
George Burgess IV | 825a868 | 2016-07-21 20:52:35 +0000 | [diff] [blame] | 9 | // Compute iterated dominance frontiers using a linear time algorithm. |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Analysis/IteratedDominanceFrontier.h" |
| 14 | #include "llvm/IR/CFG.h" |
| 15 | #include "llvm/IR/Dominators.h" |
| 16 | #include <queue> |
| 17 | |
Daniel Berlin | 77fa84e | 2016-04-19 06:13:28 +0000 | [diff] [blame] | 18 | namespace llvm { |
Alina Sbirlea | 0dfe830 | 2018-08-17 17:39:15 +0000 | [diff] [blame] | 19 | |
Jakub Kuderski | b292c22 | 2017-07-14 18:26:09 +0000 | [diff] [blame] | 20 | template <class NodeTy, bool IsPostDom> |
| 21 | void IDFCalculator<NodeTy, IsPostDom>::calculate( |
Daniel Berlin | 77fa84e | 2016-04-19 06:13:28 +0000 | [diff] [blame] | 22 | SmallVectorImpl<BasicBlock *> &PHIBlocks) { |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 23 | // Use a priority queue keyed on dominator tree level so that inserted nodes |
Michael Zolotukhin | 046da97 | 2018-05-12 01:44:32 +0000 | [diff] [blame] | 24 | // are handled from the bottom of the dominator tree upwards. We also augment |
| 25 | // the level with a DFS number to ensure that the blocks are ordered in a |
| 26 | // deterministic way. |
| 27 | typedef std::pair<DomTreeNode *, std::pair<unsigned, unsigned>> |
| 28 | DomTreeNodePair; |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 29 | typedef std::priority_queue<DomTreeNodePair, SmallVector<DomTreeNodePair, 32>, |
| 30 | less_second> IDFPriorityQueue; |
| 31 | IDFPriorityQueue PQ; |
| 32 | |
Michael Zolotukhin | 046da97 | 2018-05-12 01:44:32 +0000 | [diff] [blame] | 33 | DT.updateDFSNumbers(); |
| 34 | |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 35 | for (BasicBlock *BB : *DefBlocks) { |
| 36 | if (DomTreeNode *Node = DT.getNode(BB)) |
Michael Zolotukhin | 046da97 | 2018-05-12 01:44:32 +0000 | [diff] [blame] | 37 | PQ.push({Node, std::make_pair(Node->getLevel(), Node->getDFSNumIn())}); |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | SmallVector<DomTreeNode *, 32> Worklist; |
| 41 | SmallPtrSet<DomTreeNode *, 32> VisitedPQ; |
| 42 | SmallPtrSet<DomTreeNode *, 32> VisitedWorklist; |
| 43 | |
| 44 | while (!PQ.empty()) { |
| 45 | DomTreeNodePair RootPair = PQ.top(); |
| 46 | PQ.pop(); |
| 47 | DomTreeNode *Root = RootPair.first; |
Michael Zolotukhin | 046da97 | 2018-05-12 01:44:32 +0000 | [diff] [blame] | 48 | unsigned RootLevel = RootPair.second.first; |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 49 | |
| 50 | // Walk all dominator tree children of Root, inspecting their CFG edges with |
| 51 | // targets elsewhere on the dominator tree. Only targets whose level is at |
| 52 | // most Root's level are added to the iterated dominance frontier of the |
| 53 | // definition set. |
| 54 | |
| 55 | Worklist.clear(); |
| 56 | Worklist.push_back(Root); |
| 57 | VisitedWorklist.insert(Root); |
| 58 | |
| 59 | while (!Worklist.empty()) { |
| 60 | DomTreeNode *Node = Worklist.pop_back_val(); |
| 61 | BasicBlock *BB = Node->getBlock(); |
Daniel Berlin | 77fa84e | 2016-04-19 06:13:28 +0000 | [diff] [blame] | 62 | // Succ is the successor in the direction we are calculating IDF, so it is |
| 63 | // successor for IDF, and predecessor for Reverse IDF. |
Alina Sbirlea | 0dfe830 | 2018-08-17 17:39:15 +0000 | [diff] [blame] | 64 | auto DoWork = [&](BasicBlock *Succ) { |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 65 | DomTreeNode *SuccNode = DT.getNode(Succ); |
| 66 | |
| 67 | // Quickly skip all CFG edges that are also dominator tree edges instead |
| 68 | // of catching them below. |
| 69 | if (SuccNode->getIDom() == Node) |
Alina Sbirlea | 0dfe830 | 2018-08-17 17:39:15 +0000 | [diff] [blame] | 70 | return; |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 71 | |
Jakub Kuderski | 604a22b | 2017-07-01 00:23:01 +0000 | [diff] [blame] | 72 | const unsigned SuccLevel = SuccNode->getLevel(); |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 73 | if (SuccLevel > RootLevel) |
Alina Sbirlea | 0dfe830 | 2018-08-17 17:39:15 +0000 | [diff] [blame] | 74 | return; |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 75 | |
| 76 | if (!VisitedPQ.insert(SuccNode).second) |
Alina Sbirlea | 0dfe830 | 2018-08-17 17:39:15 +0000 | [diff] [blame] | 77 | return; |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 78 | |
| 79 | BasicBlock *SuccBB = SuccNode->getBlock(); |
| 80 | if (useLiveIn && !LiveInBlocks->count(SuccBB)) |
Alina Sbirlea | 0dfe830 | 2018-08-17 17:39:15 +0000 | [diff] [blame] | 81 | return; |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 82 | |
| 83 | PHIBlocks.emplace_back(SuccBB); |
| 84 | if (!DefBlocks->count(SuccBB)) |
Michael Zolotukhin | 046da97 | 2018-05-12 01:44:32 +0000 | [diff] [blame] | 85 | PQ.push(std::make_pair( |
| 86 | SuccNode, std::make_pair(SuccLevel, SuccNode->getDFSNumIn()))); |
Alina Sbirlea | 0dfe830 | 2018-08-17 17:39:15 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | if (GD) { |
| 90 | for (auto Pair : children< |
| 91 | std::pair<const GraphDiff<BasicBlock *, IsPostDom> *, NodeTy>>( |
| 92 | {GD, BB})) |
| 93 | DoWork(Pair.second); |
| 94 | } else { |
| 95 | for (auto *Succ : children<NodeTy>(BB)) |
| 96 | DoWork(Succ); |
Daniel Berlin | 2372a19 | 2015-04-21 19:13:02 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | for (auto DomChild : *Node) { |
| 100 | if (VisitedWorklist.insert(DomChild).second) |
| 101 | Worklist.push_back(DomChild); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
Daniel Berlin | 77fa84e | 2016-04-19 06:13:28 +0000 | [diff] [blame] | 106 | |
Jakub Kuderski | b292c22 | 2017-07-14 18:26:09 +0000 | [diff] [blame] | 107 | template class IDFCalculator<BasicBlock *, false>; |
| 108 | template class IDFCalculator<Inverse<BasicBlock *>, true>; |
Daniel Berlin | 77fa84e | 2016-04-19 06:13:28 +0000 | [diff] [blame] | 109 | } |