2015-04-22 03:13:02 +08:00
|
|
|
//===- IteratedDominanceFrontier.cpp - Compute IDF ------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2016-07-22 04:52:35 +08:00
|
|
|
// Compute iterated dominance frontiers using a linear time algorithm.
|
2015-04-22 03:13:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/IteratedDominanceFrontier.h"
|
|
|
|
#include "llvm/IR/CFG.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include <queue>
|
|
|
|
|
2016-04-19 14:13:28 +08:00
|
|
|
namespace llvm {
|
2017-07-15 02:26:09 +08:00
|
|
|
template <class NodeTy, bool IsPostDom>
|
|
|
|
void IDFCalculator<NodeTy, IsPostDom>::calculate(
|
2016-04-19 14:13:28 +08:00
|
|
|
SmallVectorImpl<BasicBlock *> &PHIBlocks) {
|
2015-04-22 03:13:02 +08:00
|
|
|
// Use a priority queue keyed on dominator tree level so that inserted nodes
|
2018-05-12 09:44:32 +08:00
|
|
|
// are handled from the bottom of the dominator tree upwards. We also augment
|
|
|
|
// the level with a DFS number to ensure that the blocks are ordered in a
|
|
|
|
// deterministic way.
|
|
|
|
typedef std::pair<DomTreeNode *, std::pair<unsigned, unsigned>>
|
|
|
|
DomTreeNodePair;
|
2015-04-22 03:13:02 +08:00
|
|
|
typedef std::priority_queue<DomTreeNodePair, SmallVector<DomTreeNodePair, 32>,
|
|
|
|
less_second> IDFPriorityQueue;
|
|
|
|
IDFPriorityQueue PQ;
|
|
|
|
|
2018-05-12 09:44:32 +08:00
|
|
|
DT.updateDFSNumbers();
|
|
|
|
|
2015-04-22 03:13:02 +08:00
|
|
|
for (BasicBlock *BB : *DefBlocks) {
|
|
|
|
if (DomTreeNode *Node = DT.getNode(BB))
|
2018-05-12 09:44:32 +08:00
|
|
|
PQ.push({Node, std::make_pair(Node->getLevel(), Node->getDFSNumIn())});
|
2015-04-22 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<DomTreeNode *, 32> Worklist;
|
|
|
|
SmallPtrSet<DomTreeNode *, 32> VisitedPQ;
|
|
|
|
SmallPtrSet<DomTreeNode *, 32> VisitedWorklist;
|
|
|
|
|
|
|
|
while (!PQ.empty()) {
|
|
|
|
DomTreeNodePair RootPair = PQ.top();
|
|
|
|
PQ.pop();
|
|
|
|
DomTreeNode *Root = RootPair.first;
|
2018-05-12 09:44:32 +08:00
|
|
|
unsigned RootLevel = RootPair.second.first;
|
2015-04-22 03:13:02 +08:00
|
|
|
|
|
|
|
// Walk all dominator tree children of Root, inspecting their CFG edges with
|
|
|
|
// targets elsewhere on the dominator tree. Only targets whose level is at
|
|
|
|
// most Root's level are added to the iterated dominance frontier of the
|
|
|
|
// definition set.
|
|
|
|
|
|
|
|
Worklist.clear();
|
|
|
|
Worklist.push_back(Root);
|
|
|
|
VisitedWorklist.insert(Root);
|
|
|
|
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
DomTreeNode *Node = Worklist.pop_back_val();
|
|
|
|
BasicBlock *BB = Node->getBlock();
|
2016-04-19 14:13:28 +08:00
|
|
|
// Succ is the successor in the direction we are calculating IDF, so it is
|
|
|
|
// successor for IDF, and predecessor for Reverse IDF.
|
2017-02-10 04:37:46 +08:00
|
|
|
for (auto *Succ : children<NodeTy>(BB)) {
|
2015-04-22 03:13:02 +08:00
|
|
|
DomTreeNode *SuccNode = DT.getNode(Succ);
|
|
|
|
|
|
|
|
// Quickly skip all CFG edges that are also dominator tree edges instead
|
|
|
|
// of catching them below.
|
|
|
|
if (SuccNode->getIDom() == Node)
|
|
|
|
continue;
|
|
|
|
|
2017-07-01 08:23:01 +08:00
|
|
|
const unsigned SuccLevel = SuccNode->getLevel();
|
2015-04-22 03:13:02 +08:00
|
|
|
if (SuccLevel > RootLevel)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!VisitedPQ.insert(SuccNode).second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BasicBlock *SuccBB = SuccNode->getBlock();
|
|
|
|
if (useLiveIn && !LiveInBlocks->count(SuccBB))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
PHIBlocks.emplace_back(SuccBB);
|
|
|
|
if (!DefBlocks->count(SuccBB))
|
2018-05-12 09:44:32 +08:00
|
|
|
PQ.push(std::make_pair(
|
|
|
|
SuccNode, std::make_pair(SuccLevel, SuccNode->getDFSNumIn())));
|
2015-04-22 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto DomChild : *Node) {
|
|
|
|
if (VisitedWorklist.insert(DomChild).second)
|
|
|
|
Worklist.push_back(DomChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 14:13:28 +08:00
|
|
|
|
2017-07-15 02:26:09 +08:00
|
|
|
template class IDFCalculator<BasicBlock *, false>;
|
|
|
|
template class IDFCalculator<Inverse<BasicBlock *>, true>;
|
2016-04-19 14:13:28 +08:00
|
|
|
}
|