00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
00011 #define LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
00012
00013 #include "llvm/Analysis/Dominators.h"
00014 #include "llvm/ADT/SmallPtrSet.h"
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #define BALANCE_IDOM_TREE 0
00032
00033
00034
00035 namespace llvm {
00036
00037 template<class GraphT>
00038 unsigned DFSPass(DominatorTreeBase<typename GraphT::NodeType>& DT,
00039 typename GraphT::NodeType* V, unsigned N) {
00040
00041
00042
00043 #if 0
00044 InfoRec &VInfo = DT.Info[DT.Roots[i]];
00045 VInfo.DFSNum = VInfo.Semi = ++N;
00046 VInfo.Label = V;
00047
00048 Vertex.push_back(V);
00049
00050
00051 VInfo.Size = 1;
00052
00053 for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
00054 InfoRec &SuccVInfo = DT.Info[*SI];
00055 if (SuccVInfo.Semi == 0) {
00056 SuccVInfo.Parent = V;
00057 N = DTDFSPass(DT, *SI, N);
00058 }
00059 }
00060 #else
00061 bool IsChilOfArtificialExit = (N != 0);
00062
00063 std::vector<std::pair<typename GraphT::NodeType*,
00064 typename GraphT::ChildIteratorType> > Worklist;
00065 Worklist.push_back(std::make_pair(V, GraphT::child_begin(V)));
00066 while (!Worklist.empty()) {
00067 typename GraphT::NodeType* BB = Worklist.back().first;
00068 typename GraphT::ChildIteratorType NextSucc = Worklist.back().second;
00069
00070 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
00071 DT.Info[BB];
00072
00073
00074 if (NextSucc == GraphT::child_begin(BB)) {
00075 BBInfo.DFSNum = BBInfo.Semi = ++N;
00076 BBInfo.Label = BB;
00077
00078 DT.Vertex.push_back(BB);
00079
00080
00081 BBInfo.Size = 1;
00082
00083 if (IsChilOfArtificialExit)
00084 BBInfo.Parent = 1;
00085
00086 IsChilOfArtificialExit = false;
00087 }
00088
00089
00090
00091 unsigned BBDFSNum = BBInfo.DFSNum;
00092
00093
00094 if (NextSucc == GraphT::child_end(BB)) {
00095 Worklist.pop_back();
00096 continue;
00097 }
00098
00099
00100 ++Worklist.back().second;
00101
00102
00103 typename GraphT::NodeType* Succ = *NextSucc;
00104
00105 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &SuccVInfo =
00106 DT.Info[Succ];
00107 if (SuccVInfo.Semi == 0) {
00108 SuccVInfo.Parent = BBDFSNum;
00109 Worklist.push_back(std::make_pair(Succ, GraphT::child_begin(Succ)));
00110 }
00111 }
00112 #endif
00113 return N;
00114 }
00115
00116 template<class GraphT>
00117 void Compress(DominatorTreeBase<typename GraphT::NodeType>& DT,
00118 typename GraphT::NodeType *VIn) {
00119 std::vector<typename GraphT::NodeType*> Work;
00120 SmallPtrSet<typename GraphT::NodeType*, 32> Visited;
00121 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInVAInfo =
00122 DT.Info[DT.Vertex[DT.Info[VIn].Ancestor]];
00123
00124 if (VInVAInfo.Ancestor != 0)
00125 Work.push_back(VIn);
00126
00127 while (!Work.empty()) {
00128 typename GraphT::NodeType* V = Work.back();
00129 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
00130 DT.Info[V];
00131 typename GraphT::NodeType* VAncestor = DT.Vertex[VInfo.Ancestor];
00132 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VAInfo =
00133 DT.Info[VAncestor];
00134
00135
00136 if (Visited.insert(VAncestor) &&
00137 VAInfo.Ancestor != 0) {
00138 Work.push_back(VAncestor);
00139 continue;
00140 }
00141 Work.pop_back();
00142
00143
00144 if (VAInfo.Ancestor == 0)
00145 continue;
00146 typename GraphT::NodeType* VAncestorLabel = VAInfo.Label;
00147 typename GraphT::NodeType* VLabel = VInfo.Label;
00148 if (DT.Info[VAncestorLabel].Semi < DT.Info[VLabel].Semi)
00149 VInfo.Label = VAncestorLabel;
00150 VInfo.Ancestor = VAInfo.Ancestor;
00151 }
00152 }
00153
00154 template<class GraphT>
00155 typename GraphT::NodeType* Eval(DominatorTreeBase<typename GraphT::NodeType>& DT,
00156 typename GraphT::NodeType *V) {
00157 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &VInfo =
00158 DT.Info[V];
00159 #if !BALANCE_IDOM_TREE
00160
00161 if (VInfo.Ancestor == 0)
00162 return V;
00163 Compress<GraphT>(DT, V);
00164 return VInfo.Label;
00165 #else
00166
00167 if (VInfo.Ancestor == 0)
00168 return VInfo.Label;
00169 Compress<GraphT>(DT, V);
00170 GraphT::NodeType* VLabel = VInfo.Label;
00171
00172 GraphT::NodeType* VAncestorLabel = DT.Info[VInfo.Ancestor].Label;
00173 if (DT.Info[VAncestorLabel].Semi >= DT.Info[VLabel].Semi)
00174 return VLabel;
00175 else
00176 return VAncestorLabel;
00177 #endif
00178 }
00179
00180 template<class GraphT>
00181 void Link(DominatorTreeBase<typename GraphT::NodeType>& DT,
00182 unsigned DFSNumV, typename GraphT::NodeType* W,
00183 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo) {
00184 #if !BALANCE_IDOM_TREE
00185
00186 WInfo.Ancestor = DFSNumV;
00187 #else
00188
00189 GraphT::NodeType* WLabel = WInfo.Label;
00190 unsigned WLabelSemi = DT.Info[WLabel].Semi;
00191 GraphT::NodeType* S = W;
00192 InfoRec *SInfo = &DT.Info[S];
00193
00194 GraphT::NodeType* SChild = SInfo->Child;
00195 InfoRec *SChildInfo = &DT.Info[SChild];
00196
00197 while (WLabelSemi < DT.Info[SChildInfo->Label].Semi) {
00198 GraphT::NodeType* SChildChild = SChildInfo->Child;
00199 if (SInfo->Size+DT.Info[SChildChild].Size >= 2*SChildInfo->Size) {
00200 SChildInfo->Ancestor = S;
00201 SInfo->Child = SChild = SChildChild;
00202 SChildInfo = &DT.Info[SChild];
00203 } else {
00204 SChildInfo->Size = SInfo->Size;
00205 S = SInfo->Ancestor = SChild;
00206 SInfo = SChildInfo;
00207 SChild = SChildChild;
00208 SChildInfo = &DT.Info[SChild];
00209 }
00210 }
00211
00212 DominatorTreeBase::InfoRec &VInfo = DT.Info[V];
00213 SInfo->Label = WLabel;
00214
00215 assert(V != W && "The optimization here will not work in this case!");
00216 unsigned WSize = WInfo.Size;
00217 unsigned VSize = (VInfo.Size += WSize);
00218
00219 if (VSize < 2*WSize)
00220 std::swap(S, VInfo.Child);
00221
00222 while (S) {
00223 SInfo = &DT.Info[S];
00224 SInfo->Ancestor = V;
00225 S = SInfo->Child;
00226 }
00227 #endif
00228 }
00229
00230 template<class FuncT, class NodeT>
00231 void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
00232 FuncT& F) {
00233 typedef GraphTraits<NodeT> GraphT;
00234
00235 unsigned N = 0;
00236 bool MultipleRoots = (DT.Roots.size() > 1);
00237 if (MultipleRoots) {
00238 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &BBInfo =
00239 DT.Info[NULL];
00240 BBInfo.DFSNum = BBInfo.Semi = ++N;
00241 BBInfo.Label = NULL;
00242
00243 DT.Vertex.push_back(NULL);
00244
00245
00246 BBInfo.Size = 1;
00247 }
00248
00249
00250
00251 for (unsigned i = 0, e = static_cast<unsigned>(DT.Roots.size());
00252 i != e; ++i)
00253 N = DFSPass<GraphT>(DT, DT.Roots[i], N);
00254
00255
00256
00257 MultipleRoots |= (DT.isPostDominator() && N != F.size());
00258
00259 for (unsigned i = N; i >= 2; --i) {
00260 typename GraphT::NodeType* W = DT.Vertex[i];
00261 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo =
00262 DT.Info[W];
00263
00264
00265
00266
00267 WInfo.Semi = WInfo.Parent;
00268 for (typename GraphTraits<Inverse<NodeT> >::ChildIteratorType CI =
00269 GraphTraits<Inverse<NodeT> >::child_begin(W),
00270 E = GraphTraits<Inverse<NodeT> >::child_end(W); CI != E; ++CI)
00271 if (DT.Info.count(*CI)) {
00272 unsigned SemiU = DT.Info[Eval<GraphT>(DT, *CI)].Semi;
00273 if (SemiU < WInfo.Semi)
00274 WInfo.Semi = SemiU;
00275 }
00276
00277 DT.Info[DT.Vertex[WInfo.Semi]].Bucket.push_back(W);
00278
00279 typename GraphT::NodeType* WParent = DT.Vertex[WInfo.Parent];
00280 Link<GraphT>(DT, WInfo.Parent, W, WInfo);
00281
00282
00283 std::vector<typename GraphT::NodeType*> &WParentBucket =
00284 DT.Info[WParent].Bucket;
00285 while (!WParentBucket.empty()) {
00286 typename GraphT::NodeType* V = WParentBucket.back();
00287 WParentBucket.pop_back();
00288 typename GraphT::NodeType* U = Eval<GraphT>(DT, V);
00289 DT.IDoms[V] = DT.Info[U].Semi < DT.Info[V].Semi ? U : WParent;
00290 }
00291 }
00292
00293
00294 for (unsigned i = 2; i <= N; ++i) {
00295 typename GraphT::NodeType* W = DT.Vertex[i];
00296 typename GraphT::NodeType*& WIDom = DT.IDoms[W];
00297 if (WIDom != DT.Vertex[DT.Info[W].Semi])
00298 WIDom = DT.IDoms[WIDom];
00299 }
00300
00301 if (DT.Roots.empty()) return;
00302
00303
00304
00305
00306
00307 typename GraphT::NodeType* Root = !MultipleRoots ? DT.Roots[0] : 0;
00308
00309 DT.DomTreeNodes[Root] = DT.RootNode =
00310 new DomTreeNodeBase<typename GraphT::NodeType>(Root, 0);
00311
00312
00313 for (unsigned i = 2; i <= N; ++i) {
00314 typename GraphT::NodeType* W = DT.Vertex[i];
00315
00316 DomTreeNodeBase<typename GraphT::NodeType> *BBNode = DT.DomTreeNodes[W];
00317 if (BBNode) continue;
00318
00319 typename GraphT::NodeType* ImmDom = DT.getIDom(W);
00320
00321 assert(ImmDom || DT.DomTreeNodes[NULL]);
00322
00323
00324 DomTreeNodeBase<typename GraphT::NodeType> *IDomNode =
00325 DT.getNodeForBlock(ImmDom);
00326
00327
00328
00329 DomTreeNodeBase<typename GraphT::NodeType> *C =
00330 new DomTreeNodeBase<typename GraphT::NodeType>(W, IDomNode);
00331 DT.DomTreeNodes[W] = IDomNode->addChild(C);
00332 }
00333
00334
00335 DT.IDoms.clear();
00336 DT.Info.clear();
00337 std::vector<typename GraphT::NodeType*>().swap(DT.Vertex);
00338
00339 DT.updateDFSNumbers();
00340 }
00341
00342 }
00343
00344 #endif