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