- add support for statistics and merge debug info
[libfirm] / ir / ana / height.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Compute heights of nodes inside basic blocks
23  * @author   Sebastian Hack
24  * @date     19.04.2006
25  * @version  $Id$
26  */
27 #include "config.h"
28
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #include "list.h"
33
34 #include "irdump.h"
35 #include "irgwalk.h"
36 #include "irtools.h"
37 #include "irphase_t.h"
38 #include "iredges_t.h"
39
40 typedef struct _heights_t heights_t;
41
42 struct _heights_t {
43         ir_phase ph;
44         unsigned visited;
45         void *dump_handle;
46 };
47
48 typedef struct {
49         unsigned height;
50         unsigned visited;
51 } irn_height_t;
52
53 static void *irn_height_init(ir_phase *ph, const ir_node *irn, void *data)
54 {
55         irn_height_t *h = data ? data : phase_alloc(ph, sizeof(h[0]));
56         (void)irn;
57         memset(h, 0, sizeof(h[0]));
58         return h;
59 }
60
61 static void height_dump_cb(void *data, FILE *f, const ir_node *irn)
62 {
63         heights_t *heights = data;
64         irn_height_t *h    = phase_get_irn_data(&heights->ph, irn);
65
66         if(h)
67                 fprintf(f, "height: %u\n", h->height);
68 }
69
70 /**
71  * Check, if we can reach a target node from a given node inside one basic block.
72  * @param h    The heights object.
73  * @param curr The current node from which we tried to reach the other one.
74  * @param tgt  The node we try to reach.
75  * @return     1, one of tgt can be reached from curr, 0 else.
76  */
77 static int search(heights_t *h, const ir_node *curr, const ir_node *tgt)
78 {
79         irn_height_t *h_curr;
80         irn_height_t *h_tgt;
81         int i, n;
82
83         /* if the current node is the one we were looking for, we're done. */
84         if(curr == tgt)
85                 return 1;
86
87         /* If we are in another block or at a phi we won't find our target. */
88         if(get_nodes_block(curr) != get_nodes_block(tgt))
89                 return 0;
90         if(is_Phi(curr))
91                 return 0;
92
93         /* Check, if we have already been here. Coming more often won't help :-) */
94         h_curr = phase_get_irn_data(&h->ph, curr);
95         if(h_curr->visited >= h->visited)
96                 return 0;
97
98         /* If we are too deep into the DAG we won't find the target either. */
99         h_tgt = phase_get_irn_data(&h->ph, tgt);
100         if(h_curr->height > h_tgt->height)
101                 return 0;
102
103         /* Mark this place as visited. */
104         h_curr->visited = h->visited;
105
106         /* Start a search from this node. */
107         for(i = 0, n = get_irn_ins_or_deps(curr); i < n; ++i) {
108                 ir_node *op = get_irn_in_or_dep(curr, i);
109                 if(search(h, op, tgt))
110                         return 1;
111         }
112
113         return 0;
114 }
115
116 /**
117  * Check, if one node can be reached from another one, according to data dependence.
118  */
119 int heights_reachable_in_block(heights_t *h, const ir_node *n, const ir_node *m)
120 {
121         int res          = 0;
122         irn_height_t *hn = phase_get_irn_data(&h->ph, n);
123         irn_height_t *hm = phase_get_irn_data(&h->ph, m);
124
125         assert(get_nodes_block(n) == get_nodes_block(m));
126         assert(hn != NULL && hm != NULL);
127
128         if(hn->height <= hm->height) {
129                 h->visited++;
130                 res = search(h, n, m);
131         }
132
133         return res;
134 }
135
136 /**
137  * Compute the height of a node in a block.
138  * @param h   The heights object.
139  * @param irn The node.
140  * @param bl  The block.
141  */
142 static unsigned compute_height(heights_t *h, ir_node *irn, const ir_node *bl)
143 {
144         irn_height_t *ih = phase_get_or_set_irn_data(&h->ph, irn);
145
146         const ir_edge_t *edge;
147
148         /* bail out if we already visited that node. */
149         if(ih->visited >= h->visited)
150                 return ih->height;
151
152         ih->visited = h->visited;
153         ih->height  = 0;
154
155         foreach_out_edge(irn, edge) {
156                 ir_node *dep = get_edge_src_irn(edge);
157
158                 if(!is_Block(dep) && !is_Phi(dep) && get_nodes_block(dep) == bl) {
159                         unsigned dep_height = compute_height(h, dep, bl);
160                         ih->height          = MAX(ih->height, dep_height);
161                 }
162
163                 ih->height++;
164         }
165
166         foreach_out_edge_kind(irn, edge, EDGE_KIND_DEP) {
167                 ir_node *dep = get_edge_src_irn(edge);
168
169                 assert(!is_Phi(dep));
170                 if(!is_Block(dep) && get_nodes_block(dep) == bl) {
171                         unsigned dep_height = compute_height(h, dep, bl);
172                         ih->height          = MAX(ih->height, dep_height);
173                 }
174
175                 ih->height++;
176         }
177
178         return ih->height;
179 }
180
181 static unsigned compute_heights_in_block(ir_node *bl, heights_t *h)
182 {
183         int             max_height = -1;
184         const ir_edge_t *edge;
185
186         h->visited++;
187
188         foreach_out_edge(bl, edge) {
189                 ir_node *dep = get_edge_src_irn(edge);
190                 int     curh = compute_height(h, dep, bl);
191
192                 max_height = MAX(curh, max_height);
193         }
194
195         foreach_out_edge_kind(bl, edge, EDGE_KIND_DEP) {
196                 ir_node *dep = get_edge_src_irn(edge);
197                 int     curh = compute_height(h, dep, bl);
198
199                 max_height = MAX(curh, max_height);
200         }
201
202         return max_height;
203 }
204
205 static void compute_heights_in_block_walker(ir_node *block, void *data)
206 {
207         heights_t *h = data;
208         compute_heights_in_block(block, h);
209 }
210
211 unsigned get_irn_height(heights_t *heights, const ir_node *irn)
212 {
213         irn_height_t *h = phase_get_irn_data(&heights->ph, irn);
214         assert(h && "No height information for node");
215         return h->height;
216 }
217
218 unsigned heights_recompute_block(heights_t *h, ir_node *block)
219 {
220         const ir_edge_t *edge;
221
222         edges_assure(phase_get_irg(&h->ph));
223
224         /* reset phase data for all nodes in the block */
225         foreach_out_edge(block, edge) {
226                 ir_node      *irn = get_edge_src_irn(edge);
227                 irn_height_t *ih  = phase_get_irn_data(&h->ph, irn);
228
229                 irn_height_init(&h->ph, irn, ih);
230         }
231
232         h->visited = 0;
233         return compute_heights_in_block(block, h);
234 }
235
236 void heights_recompute(heights_t *h)
237 {
238         edges_assure(phase_get_irg(&h->ph));
239         phase_reinit_irn_data(&h->ph);
240         h->visited = 0;
241         irg_block_walk_graph(phase_get_irg(&h->ph), compute_heights_in_block_walker, NULL, h);
242 }
243
244 heights_t *heights_new(ir_graph *irg)
245 {
246         heights_t *res = XMALLOC(heights_t);
247         phase_init(&res->ph, "heights", irg, PHASE_DEFAULT_GROWTH, irn_height_init, NULL);
248         res->dump_handle = dump_add_node_info_callback(height_dump_cb, res);
249         heights_recompute(res);
250
251         return res;
252 }
253
254 void heights_free(heights_t *h)
255 {
256         phase_free(&h->ph);
257         dump_remv_node_info_callback(h->dump_handle);
258         xfree(h);
259 }