1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
//===--- Dominance.cpp - SIL basic block dominance analysis ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/Dominance.h"
#include "llvm/Support/GenericDomTreeConstruction.h"
using namespace swift;
template class llvm::DominatorTreeBase<SILBasicBlock, false>;
template class llvm::DominatorTreeBase<SILBasicBlock, true>;
template class llvm::DomTreeNodeBase<SILBasicBlock>;
namespace llvm {
namespace DomTreeBuilder {
template void Calculate<SILDomTree>(SILDomTree &DT);
template void Calculate<SILPostDomTree>(SILPostDomTree &DT);
} // namespace DomTreeBuilder
} // namespace llvm
/// Compute the immediate-dominators map.
DominanceInfo::DominanceInfo(SILFunction *F)
: DominatorTreeBase() {
assert(!F->isExternalDeclaration() &&
"Make sure the function is a definition and not a declaration.");
recalculate(*F);
}
DominanceInfo::~DominanceInfo() {
}
bool DominanceInfo::properlyDominates(SILInstruction *a, SILInstruction *b) {
auto aBlock = a->getParent(), bBlock = b->getParent();
// If the blocks are different, it's as easy as whether A's block
// dominates B's block.
if (aBlock != bBlock)
return properlyDominates(a->getParent(), b->getParent());
// Otherwise, they're in the same block, and we just need to check
// whether B comes after A. This is a non-strict computation.
auto aIter = a->getIterator();
auto bIter = b->getIterator();
auto fIter = aBlock->begin();
while (bIter != fIter) {
--bIter;
if (aIter == bIter)
return true;
}
return false;
}
/// Does value A properly dominate instruction B?
bool DominanceInfo::properlyDominates(SILValue a, SILInstruction *b) {
if (auto *Inst = a->getDefiningInstruction()) {
return properlyDominates(Inst, b);
}
if (auto *Arg = dyn_cast<SILArgument>(a)) {
return dominates(Arg->getParent(), b->getParent());
}
return false;
}
SILBasicBlock *DominanceInfo::getLeastCommonAncestorOfUses(SILValue value) {
SILBasicBlock *lca = nullptr;
for (auto *use : value->getUses()) {
auto *block = use->getParentBlock();
lca = lca ? findNearestCommonDominator(lca, block) : block;
}
return lca;
}
void DominanceInfo::verify() const {
// Recompute.
auto *F = getRoot()->getParent();
DominanceInfo OtherDT(F);
// And compare.
if (errorOccurredOnComparison(OtherDT)) {
llvm::errs() << "DominatorTree is not up to date!\nComputed:\n";
print(llvm::errs());
llvm::errs() << "\nActual:\n";
OtherDT.print(llvm::errs());
abort();
}
}
/// Compute the immediate-post-dominators map.
PostDominanceInfo::PostDominanceInfo(SILFunction *F)
: PostDominatorTreeBase() {
assert(!F->isExternalDeclaration() &&
"Cannot construct a post dominator tree for a declaration");
recalculate(*F);
}
bool
PostDominanceInfo::
properlyDominates(SILInstruction *I1, SILInstruction *I2) {
SILBasicBlock *BB1 = I1->getParent(), *BB2 = I2->getParent();
// If the blocks are different, it's as easy as whether BB1 post dominates
// BB2.
if (BB1 != BB2)
return properlyDominates(BB1, BB2);
// Otherwise, they're in the same block, and we just need to check
// whether A comes after B.
for (auto II = I1->getIterator(), IE = BB1->end(); II != IE; ++II) {
if (&*II == I2) {
return false;
}
}
return true;
}
bool PostDominanceInfo::properlyDominates(SILValue A, SILInstruction *B) {
if (auto *Inst = A->getDefiningInstruction()) {
return properlyDominates(Inst, B);
}
if (auto *Arg = dyn_cast<SILArgument>(A)) {
return dominates(Arg->getParent(), B->getParent());
}
return false;
}
void PostDominanceInfo::verify() const {
// Recompute.
//
// Even though at the SIL level we have "one" return function, we can have
// multiple exits provided by no-return functions.
auto *F = (*root_begin())->getParent();
PostDominanceInfo OtherDT(F);
// And compare.
if (errorOccurredOnComparison(OtherDT)) {
llvm::errs() << "PostDominatorTree is not up to date!\nComputed:\n";
print(llvm::errs());
llvm::errs() << "\nActual:\n";
OtherDT.print(llvm::errs());
abort();
}
}
void swift::computeDominatedBoundaryBlocks(
SILBasicBlock *root, DominanceInfo *domTree,
SmallVectorImpl<SILBasicBlock *> &boundary) {
assert(boundary.empty());
DominanceOrder domOrder(root, domTree);
while (SILBasicBlock *block = domOrder.getNext()) {
DominanceInfoNode *domNode = domTree->getNode(block);
if (!domNode->isLeaf()) {
domOrder.pushChildren(block);
continue;
}
if (block->getNumSuccessors() == 0) {
boundary.push_back(block);
continue;
}
auto *succ = block->getSingleSuccessorBlock();
if (!domTree->properlyDominates(root, succ)) {
boundary.push_back(block);
}
}
}
|