del_pset in now called correctly
[libfirm] / ir / ana / irextbb.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/irextbb.c
4  * Purpose:     Extended basis block support.
5  * Author:      Michael Beck
6  * Modified by:
7  * Created:     5.2005
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2002-2005 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * @file irextbb.c
15  *
16  *  Computes extended basic blocks.
17  *
18  *  @author Michael Beck
19  */
20 #include "irextbb_t.h"
21 #include "irgwalk.h"
22 #include "irnode_t.h"
23 #include "xmalloc.h"
24 #include "irprintf.h"
25
26 typedef struct _env {
27   struct obstack *obst;   /**< the obstack where allocations took place */
28   ir_extblk *head;        /**< head of the list of all extended blocks */
29 } env_t;
30
31 /**
32  * allocate a new extended block header.
33  */
34 static void allocate_extblk(ir_node *block, env_t *env)
35 {
36   ir_extblk *extblk = obstack_alloc(env->obst, sizeof(*extblk));
37
38   extblk->kind    = k_ir_extblk;
39   extblk->visited = 1;
40   extblk->blks    = (ir_node **)env->head;
41   extblk->link    = block;
42   env->head       = extblk;
43
44   set_Block_extbb(block, extblk);
45   set_irn_link(block, NULL);
46 }
47
48 /**
49  * add a block to an extended block
50  */
51 static void addto_extblk(ir_extblk *extblk, ir_node *block)
52 {
53   /* link all blocks belonging to this extended block */
54   set_irn_link(block, extblk->link);
55
56   extblk->link = block;
57   extblk->visited++;
58
59   set_Block_extbb(block, extblk);
60 }
61
62
63 /**
64  * Pre block-walker. Calculates the extended block info.
65  */
66 static void pre_walk_calc_extbb(ir_node *block, void *ctx)
67 {
68   int n = get_Block_n_cfgpreds(block);
69   env_t *env = ctx;
70
71   if (n > 1 || block == get_irg_start_block(current_ir_graph)) {
72     /*
73      * block is a JOIN-node ie he control flow from
74      * many other blocks joins here. block is a leader.
75      */
76     allocate_extblk(block, env);
77   }
78   else {
79     ir_node *add_to = get_Block_cfgpred(block, 0);
80
81     if (! is_Bad(add_to))
82       add_to = get_nodes_block(add_to);
83
84     /* blocks with only one BAD predecessors are leaders too */
85     if (is_Bad(add_to)) {
86       allocate_extblk(block, env);
87     }
88     else {
89       /*
90        * Only one control flow predecessor. This block belongs
91        * to the same extended basic block as its predecessor.
92        */
93       set_Block_extbb(block, NULL);
94     }
95   }
96 }
97
98 /**
99  * Post block-walker. Calculates the extended block info.
100  * During construction, we use the (free) block input of all basic blocks
101  * to point to there previous block.
102  */
103 static void post_walk_calc_extbb(ir_node *block, void *env)
104 {
105   ir_extblk *extbb = get_Block_extbb(block);
106
107   if (! extbb) {
108     ir_node *prev = block;
109
110     /* search the leader */
111     do {
112       prev = get_nodes_block(get_Block_cfgpred(prev, 0));
113       extbb = get_Block_extbb(prev);
114     } while (! extbb);
115
116     addto_extblk(extbb, block);
117   }
118 }
119
120 /*
121  * Compute the extended basic blocks for a graph
122  */
123 void compute_extbb(ir_graph *irg) {
124   env_t env;
125   ir_extblk *extbb, *next;
126
127   if (irg->extbb_obst)
128     obstack_free(irg->extbb_obst, NULL);
129   else {
130     irg->extbb_obst = xmalloc(sizeof(*irg->extbb_obst));
131   }
132   obstack_init(irg->extbb_obst);
133
134   env.obst = irg->extbb_obst;
135   env.head = NULL;
136
137   irg_block_walk_graph(irg, pre_walk_calc_extbb, post_walk_calc_extbb, &env);
138
139   /*
140    * Ok, we have now the list of all extended blocks starting with env.head
141    * every extended block "knowns" the number of blocks in visited and
142    * the blocks are linked in link.
143    * Now we can create arrays that hold the blocks, some kind of "out" edges
144    * for the extended block
145    */
146   for (extbb = env.head; extbb; extbb = next) {
147     int i, len = (int)extbb->visited;
148     ir_node *block;
149
150     next = (ir_extblk *)extbb->blks;
151
152     extbb->blks = NEW_ARR_D(ir_node *, env.obst, len);
153
154     for (block = extbb->link, i = 0; i < len; ++i) {
155       ir_node *nblock = get_irn_link(block);
156
157       extbb->blks[i] = block;
158       set_irn_link(block, NULL);
159       block = nblock;
160     }
161
162     extbb->link    = NULL;
163     extbb->visited = 0;
164   }
165
166   irg->extblk_state = ir_extblk_info_valid;
167 }
168
169 /* free all extended block info. */
170 void free_extbb(ir_graph *irg) {
171   if (irg->extbb_obst) {
172     obstack_free(irg->extbb_obst, NULL);
173     xfree(irg->extbb_obst);
174     irg->extbb_obst = NULL;
175   }
176   irg->extblk_state = ir_extblk_info_none;
177 }
178
179 /* Return the extended block of a node. */
180 ir_extblk *get_nodes_extbb(ir_node *node) {
181   ir_node *block = is_Block(node) ? node : get_nodes_block(node);
182   return get_Block_extbb(block);
183 }
184
185 /* Gets the visited counter of an extended block. */
186 unsigned long (get_extbb_visited)(const ir_extblk *blk) {
187   return _get_extbb_visited(blk);
188 }
189
190 /* Sets the visited counter of an extended block. */
191 void (set_extbb_visited)(ir_extblk *blk, unsigned long visited) {
192   _set_extbb_visited(blk, visited);
193 }
194
195 /* Mark an extended block as visited in a graph. */
196 void (mark_extbb_visited)(ir_extblk *blk) {
197   _mark_extbb_visited(blk);
198 }
199
200 /* Returns non-zero if an extended was visited. */
201 int (extbb_visited)(const ir_extblk *blk) {
202   return _extbb_visited(blk);
203 }
204
205 /* Returns non-zero if an extended block was NOT visited. */
206 int (extbb_not_visited)(const ir_extblk *blk) {
207   return _extbb_not_visited(blk);
208 }
209
210 /* Returns the link field of an extended block. */
211 void *(get_extbb_link)(const ir_extblk *blk) {
212   return _get_extbb_link(blk);
213 }
214
215 /* Sets the link field of an extended block. */
216 void (set_extbb_link)(ir_extblk *blk, void *link) {
217   _set_extbb_link(blk, link);
218 }
219
220 /* Return the number of basic blocks of an extended block */
221 int (get_extbb_n_blocks)(const ir_extblk *blk) {
222   return _get_extbb_n_blocks(blk);
223 }
224
225 /* Return the i'th basic block of an extended block */
226 ir_node *(get_extbb_block)(ir_extblk *blk, int pos) {
227   return _get_extbb_block(blk, pos);
228 }