//===- Dominators.cpp - Dominator Calculation -----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements simple dominator construction algorithms for finding
// forward dominators. Postdominators are available in libanalysis, but are not
// included in libvmcore, because it's not needed. Forward dominators are
// needed to support the Verifier pass.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/Dominators.h"
#include "llvm/Support/CFG.h"
#include "llvm/Assembly/Writer.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/SetOperations.h"
#include <algorithm>
#include <iostream>
using namespace llvm;
//===----------------------------------------------------------------------===//
// ImmediateDominators Implementation
//===----------------------------------------------------------------------===//
//
// Immediate Dominators construction - This pass constructs immediate dominator
// information for a flow-graph based on the algorithm described in this
// document:
//
// A Fast Algorithm for Finding Dominators in a Flowgraph
// T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
//
// This implements both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
// LINK, but it turns out that the theoretically slower O(n*log(n))
// implementation is actually faster than the "efficient" algorithm (even for
// large CFGs) because the constant overheads are substantially smaller. The
// lower-complexity version can be enabled with the following #define:
//
#define BALANCE_IDOM_TREE 0
//
//===----------------------------------------------------------------------===//
static RegisterAnalysis<ImmediateDominators>
C("idom", "Immediate Dominators Construction", true);
unsigned ImmediateDominators::DFSPass(BasicBlock *V, InfoRec &VInfo,
unsigned N) {
VInfo.Semi = ++N;
VInfo.Label = V;
Vertex.push_back(V); // Vertex[n] = V;
//Info[V].Ancestor = 0; // Ancestor[n] = 0
//Child[V] = 0; // Child[v] = 0
VInfo.Size = 1; // Size[v] = 1
for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
InfoRec &SuccVInfo = Info[*SI];
if (SuccVInfo.Semi == 0) {
SuccVInfo.Parent = V;
N = DFSPass(*SI, SuccVInfo, N);
}
}
return N;
}
void ImmediateDominators::Compress(BasicBlock *V, InfoRec &VInfo) {
BasicBlock *VAncestor = VInfo.Ancestor;
InfoRec &VAInfo = Info[VAncestor];
if (VAInfo.Ancestor == 0)
return;
Compress(VAncestor, VAInfo);
BasicBlock *VAncestorLabel = VAInfo.Label;
BasicBlock *VLabel = VInfo.Label;
if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
VInfo.Label = VAncestorLabel;
VInfo.Ancestor = VAInfo.Ancestor;
}
BasicBlock *ImmediateDominators::Eval(BasicBlock *V) {
InfoRec &VInfo = Info[V];
#if !BALANCE_IDOM_TREE
// Higher-complexity but faster implementation
if (VInfo.Ancestor == 0)
return V;
Compress(V, VInfo);
return VInfo.Label;
#else
// Lower-complexity but slower implementation
if (VInfo.Ancestor == 0)
return VInfo.Label;
Compress(V, VInfo);
BasicBlock *VLabel = VInfo.Label;
BasicBlock *VAncestorLabel = Info[VInfo.Ancestor].Label;
if (Info[VAncestorLabel].Semi >= Info[VLabel].Semi)
return VLabel;
else
return VAncestorLabel;
#endif
}
void ImmediateDominators::Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo){
#if !BALANCE_IDOM_TREE
// Higher-complexity but faster implementation
WInfo.Ancestor = V;
#else
// Lower-complexity but slower implementation
BasicBlock *WLabel = WInfo.Label;
unsigned WLabelSemi = Info[WLabel].Semi;
BasicBlock *S = W;
InfoRec *SInfo = &Info[S];
BasicBlock *SChild = SInfo->Child;
InfoRec *SChildInfo = &Info[SChild];
while (WLabelSemi < Info[SChildInfo->Label].Semi) {
BasicBlock *SChildChild = SChildInfo->Child;
if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
SChildInfo->Ancestor = S;
SInfo->Child = SChild = SChildChild;
SChildInfo = &Info[SChild];
} else {
SChildInfo->Size = SInfo->Size;
S = SInfo->Ancestor = SChild;
SInfo = SChildInfo;
SChild = SChildChild;
SChildInfo = &Info[SChild];
}
}
InfoRec &VInfo = Info[V];
SInfo->Label = WLabel;
assert(V != W && "The optimization here will not work in this case!");
unsigned WSize = WInfo.Size;
unsigned