1 /* src/vm/jit/optimizing/dominators.c - dominators and dominance frontier
3 Copyright (C) 2005, 2006 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Christian Ullrich
32 #include "mm/memory.h"
34 #include "toolbox/bitvector.h"
36 #include "vm/jit/jit.h"
38 #include "vm/jit/optimizing/graph.h"
39 #include "vm/jit/optimizing/dominators.h"
41 /* function prototypes */
42 void dom_Dominators_init(dominatordata *dd, int basicblockcount);
43 #ifdef DOM_DEBUG_CHECK
44 int dom_AncestorWithLowestSemi(dominatordata *dd, int v, int basicblockcount);
45 void dom_Link(dominatordata *dd, int p, int n, int basicblockcount);
46 void dom_DFS(graphdata *gd, dominatordata *dd, int p, int n, int *N,
49 int dom_AncestorWithLowestSemi(dominatordata *dd, int v);
50 void dom_Link(dominatordata *dd, int p, int n);
51 void dom_DFS(graphdata *gd, dominatordata *dd, int p, int n, int *N);
54 /*************************************
56 *************************************/
57 dominatordata *compute_Dominators(graphdata *gd, int basicblockcount) {
58 int i,j,n,N,p,s,s_,v,y;
62 dd = DNEW(dominatordata);
64 dom_Dominators_init(dd, basicblockcount);
68 /* 1 ist the root node of the method */
69 /* 0 is the artificial parent, where locals are set to their parameters */
70 dom_DFS(gd, dd, -1, 0, &N
71 #ifdef DOM_DEBUG_CHECK
76 for(i = N-1; i > 0; i--) {
77 _DOM_CHECK_BOUNDS(i, 0, basicblockcount);
79 _DOM_CHECK_BOUNDS(n, 0, basicblockcount);
82 j = graph_get_first_predecessor(gd, n, &iter);
83 for (; j != -1; j = graph_get_next(&iter)) {
84 _DOM_CHECK_BOUNDS(j, 0, basicblockcount);
85 if (dd->dfnum[j] <= dd->dfnum[n])
88 s_ = dd->semi[dom_AncestorWithLowestSemi(dd, j
89 #ifdef DOM_DEBUG_CHECK
93 _DOM_CHECK_BOUNDS(s_, 0, basicblockcount);
94 _DOM_CHECK_BOUNDS(s, 0, basicblockcount);
95 if (dd->dfnum[s_] < dd->dfnum[s])
99 _DOM_CHECK_BOUNDS(dd->num_bucket[s], 0, basicblockcount);
100 dd->bucket[s][dd->num_bucket[s]] = n;
103 #ifdef DOM_DEBUG_CHECK
107 _DOM_CHECK_BOUNDS(p, 0, basicblockcount);
108 for(j = 0; j < dd->num_bucket[p]; j++) {
109 _DOM_CHECK_BOUNDS(j, 0, basicblockcount);
110 v = dd->bucket[p][j];
111 y = dom_AncestorWithLowestSemi(dd, v
112 #ifdef DOM_DEBUG_CHECK
116 _DOM_CHECK_BOUNDS(y, 0, basicblockcount);
117 _DOM_CHECK_BOUNDS(v, 0, basicblockcount);
118 if (dd->semi[y] == dd->semi[v])
123 dd->num_bucket[p] = 0;
125 for(i = 1; i < N; i++) {
127 _DOM_CHECK_BOUNDS(n, 0, basicblockcount);
128 if (dd->samedom[n] != -1) {
129 _DOM_CHECK_BOUNDS(dd->samedom[n], 0, basicblockcount);
130 dd->idom[n] = dd->idom[dd->samedom[n]];
136 /********************************************
137 compute Dominace Frontier
138 ********************************************/
139 void computeDF(graphdata *gd, dominatordata *dd, int basicblockcount, int n) {
144 _S = DMNEW(bool, basicblockcount);
145 for(i = 0; i < basicblockcount; i++)
147 i = graph_get_first_successor(gd, n, &iter);
148 for (; i != -1; i = graph_get_next(&iter)) {
149 _DOM_CHECK_BOUNDS(i, 0, basicblockcount);
150 if (dd->idom[i] != n)
153 for(c=0; c < basicblockcount; c++) {
154 if (dd->idom[c] == n) {
155 computeDF(gd, dd, basicblockcount, c);
156 for(j=0; j < dd->num_DF[c]; j++) {
157 _DOM_CHECK_BOUNDS(dd->DF[c][j], 0, basicblockcount);
158 if (n != dd->idom[dd->DF[c][j]])
159 /* n does not dominate DF[c][j] -> traverse idom list? */
160 _S[dd->DF[c][j]] = true;
164 for(i = 0; i < basicblockcount; i++)
166 _DOM_CHECK_BOUNDS(dd->num_DF[n], 0, basicblockcount);
167 dd->DF[n][dd->num_DF[n]] = i;
173 void dom_Dominators_init(dominatordata *dd, int basicblockcount) {
176 dd->dfnum = DMNEW(int, basicblockcount);
177 dd->vertex = DMNEW(int, basicblockcount);
178 dd->parent = DMNEW(int, basicblockcount);
179 dd->semi = DMNEW(int, basicblockcount);
180 dd->ancestor = DMNEW(int, basicblockcount);
181 dd->idom = DMNEW(int, basicblockcount);
182 dd->samedom = DMNEW(int, basicblockcount);
183 dd->bucket = DMNEW(int*, basicblockcount);
184 dd->num_bucket = DMNEW(int, basicblockcount);
185 dd->DF = DMNEW(int*, basicblockcount);
186 dd->num_DF = DMNEW(int, basicblockcount);
187 dd->best = DMNEW(int, basicblockcount);
188 for (i=0; i < basicblockcount; i++) {
190 dd->semi[i] = dd->ancestor[i] = dd->idom[i] = dd->samedom[i] = -1;
191 dd->num_bucket[i] = 0;
192 dd->bucket[i] = DMNEW(int, basicblockcount);
194 dd->DF[i] = DMNEW(int, basicblockcount);
198 /**************************************
199 Create Depth First Spanning Tree
200 **************************************/
201 #ifdef DOM_DEBUG_CHECK
202 void dom_DFS(graphdata *gd, dominatordata *dd, int p, int n, int *N,
203 int basicblockcount) {
205 void dom_DFS(graphdata *gd, dominatordata *dd, int p, int n, int *N) {
210 _DOM_CHECK_BOUNDS(n,0,basicblockcount);
211 if (dd->dfnum[n] == -1) { /* not visited till now? */
213 _DOM_CHECK_BOUNDS(*N,0,basicblockcount);
217 i = graph_get_first_successor(gd, n, &iter);
218 for (; i != -1; i = graph_get_next(&iter)) {
219 dom_DFS(gd, dd, n, i, N
220 #ifdef DOM_DEBUG_CHECK
228 #ifdef DOM_DEBUG_CHECK
229 int dom_AncestorWithLowestSemi(dominatordata *dd, int v, int basicblockcount) {
231 int dom_AncestorWithLowestSemi(dominatordata *dd, int v) {
235 _DOM_CHECK_BOUNDS(v, 0, basicblockcount);
237 _DOM_CHECK_BOUNDS(a,0,basicblockcount);
238 if (dd->ancestor[a] != -1) {
239 b = dom_AncestorWithLowestSemi(dd, a
240 #ifdef DOM_DEBUG_CHECK
244 dd->ancestor[v] = dd->ancestor[a];
245 _DOM_CHECK_BOUNDS(b,0,basicblockcount);
246 _DOM_CHECK_BOUNDS(dd->best[v],0,basicblockcount);
247 _DOM_CHECK_BOUNDS(dd->semi[dd->best[v]],0,basicblockcount);
248 if (dd->dfnum[dd->semi[b]] < dd->dfnum[dd->semi[dd->best[v]]])
254 #ifdef DOM_DEBUG_CHECK
255 void dom_Link(dominatordata *dd, int p, int n, int basicblockcount) {
257 void dom_Link(dominatordata *dd, int p, int n) {
259 _DOM_CHECK_BOUNDS(n,0,basicblockcount);
265 * These are local overrides for various environment variables in Emacs.
266 * Please do not remove this and leave it at the end of the file, where
267 * Emacs will automagically detect them.
268 * ---------------------------------------------------------------------
271 * indent-tabs-mode: t