get_irn_n_edges: out_count still broken, switched back to recalculation
[libfirm] / ir / ir / iredges_t.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/iredges_t.h
4  * Purpose:     Everlasting outs -- private header.
5  * Author:      Sebastian Hack
6  * Created:     15.01.2005
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 1998-2005 Universit�t Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11
12 /**
13  * everlasting outs.
14  * @author Sebastian Hack
15  * @date 15.1.2005
16  */
17
18 #ifndef _FIRM_EDGES_T_H
19 #define _FIRM_EDGES_T_H
20
21 #include "firm_config.h"
22 #include "debug.h"
23
24 #include "set.h"
25 #include "list.h"
26
27 #include "irnode_t.h"
28 #include "irgraph_t.h"
29
30 #include "iredges.h"
31
32 #define DBG_EDGES  "firm.ir.edges"
33
34 /**
35  * An edge.
36  */
37 struct _ir_edge_t {
38   ir_node *src;           /**< The source node of the edge. */
39   int pos;                /**< The position of the edge at @p src. */
40   unsigned invalid : 1;   /**< edges that are removed are marked invalid. */
41   unsigned present : 1;   /**< Used by the verifier. Don't rely on its content. */
42   struct list_head list;  /**< The list head to queue all out edges at a node. */
43 #ifdef DEBUG_libfirm
44   long src_nr;            /**< The node number of the source node. */
45 #endif
46 };
47
48 /**
49  * A block edge inherits from a normal edge.
50  * They represent edges leading from a block to a control flow node
51  * and are used to quickly find all control flow successors of
52  * a block.
53  */
54 struct _ir_block_edge_t {
55   struct _ir_edge_t edge;      /**< The inherited data. */
56   struct list_head succ_list;  /**< List element listing all
57                                  control flow edges to the
58                                  successors of a block. */
59 };
60
61 /** Accessor for private irn info. */
62 #define _get_irn_edge_info(irn) (&(irn)->edge_info)
63
64 /** Accessor for private irg info. */
65 #define _get_irg_edge_info(irg) (&(irg)->edge_info)
66
67 /**
68  * Convenience macro to get the outs_head from a irn_edge_info_t
69  * struct.
70  */
71 #define _get_irn_outs_head(irn) (&_get_irn_edge_info(irn)->outs_head)
72
73 /**
74  * Convenience macro to get the succ_head from a block_attr
75  * struct.
76  */
77 #define _get_block_succ_head(bl) (&((bl)->attr.block.succ_head))
78
79 /**
80  * Get the first edge pointing to some node.
81  * @note There is no order on out edges. First in this context only
82  * means, that you get some starting point into the list of edges.
83  * @param irn The node.
84  * @return The first out edge that points to this node.
85  */
86 static INLINE const ir_edge_t *_get_irn_out_edge_first(const ir_node *irn)
87 {
88   const struct list_head *head = _get_irn_outs_head(irn);
89   return list_empty(head) ? NULL : list_entry(head->next, ir_edge_t, list);
90 }
91
92 /**
93  * Get the next edge in the out list of some node.
94  * @param irn The node.
95  * @param last The last out edge you have seen.
96  * @return The next out edge in @p irn 's out list after @p last.
97  */
98 static INLINE const ir_edge_t *_get_irn_out_edge_next(const ir_node *irn, const ir_edge_t *last)
99 {
100   struct list_head *next = last->list.next;
101   return next == _get_irn_outs_head(irn) ? NULL : list_entry(next, ir_edge_t, list);
102 }
103
104 /**
105  * Get the first successor edge of a block.
106  * A successor edge is an edge originated from another block, pointing
107  * to a mode_X node in the given block and is thus a control flow
108  * successor edge.
109  * @param irn The block.
110  * @return The first successor edge of the block.
111  */
112 static INLINE const ir_edge_t *_get_block_succ_first(const ir_node *irn)
113 {
114   const struct list_head *head;
115
116   assert(is_Block(irn) && "Node must be a block here");
117   head = _get_block_succ_head(irn);
118   return (ir_edge_t *) (list_empty(head) ? NULL :
119       list_entry(head->next, ir_block_edge_t, succ_list));
120 }
121
122 /**
123  * Get the next block successor edge.
124  * @see See _get_block_succ_first() for details.
125  * @param irn The block.
126  * @param last The last edge.
127  * @return The next edge, or NULL if there is no further.
128  */
129 static INLINE const ir_edge_t *_get_block_succ_next(const ir_node *irn, const ir_edge_t *last)
130 {
131   const ir_block_edge_t *block_edge;
132   struct list_head *next;
133
134   assert(is_Block(irn) && "Node must be a block here");
135   block_edge = (const ir_block_edge_t *) last;
136   next = block_edge->succ_list.next;
137   return (ir_edge_t *) (next == _get_block_succ_head(irn) ? NULL :
138       list_entry(next, ir_block_edge_t, succ_list));
139 }
140
141 /**
142  * Get the source node of an edge.
143  * @param edge The edge.
144  * @return The source node of that edge.
145  */
146 static INLINE  ir_node *_get_edge_src_irn(const ir_edge_t *edge)
147 {
148   return edge ? edge->src : NULL;
149 }
150
151 /**
152  * Get the position of an edge.
153  * @param edge  The edge.
154  * @return The position in the in array of that edges source.
155  */
156 static INLINE int _get_edge_src_pos(const ir_edge_t *edge)
157 {
158   return edge ? edge->pos : -1;
159 }
160
161 /**
162  * Get the number of edges pointing to a node.
163  * @param irn The node.
164  * @return The number of edges pointing to this node.
165  */
166 static INLINE int _get_irn_n_edges(const ir_node *irn)
167 {
168 /* Perhaps out_count was buggy. This code does it more safely. */
169 #if 1
170         int res = 0;
171         const struct list_head *pos, *head = _get_irn_outs_head(irn);
172         list_for_each(pos, head)
173                 res++;
174         return res;
175 #else
176         return _get_irn_edge_info(irn)->out_count;
177 #endif
178 }
179
180 static INLINE int _edges_activated(const ir_graph *irg)
181 {
182   return _get_irg_edge_info(irg)->activated;
183 }
184
185 /**
186  * Assure, that the edges information is present for a certain graph.
187  * @param irg The graph.
188  */
189 static INLINE void _edges_assure(ir_graph *irg)
190 {
191         if(!_edges_activated(irg))
192                 edges_activate(irg);
193 }
194
195 void edges_reroute(ir_node *old, ir_node *nw, ir_graph *irg);
196
197 void edges_init_graph(ir_graph *irg);
198
199 /**
200  * Notify of a edge change.
201  * The edge from (src, pos) -> old_tgt is redirected to tgt
202  */
203 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg);
204
205 /**
206  * A node is deleted.
207  */
208 void edges_node_deleted(ir_node *old, ir_graph *irg);
209
210 void edges_invalidate(ir_node *irn, ir_graph *irg);
211
212 /**
213  * Register additional memory in an edge.
214  * This must be called before Firm is initialized.
215  * @param  n Number of bytes you need.
216  * @return A number you have to keep and to pass
217  *         edges_get_private_data()
218  *         to get a pointer to your data.
219  */
220 int edges_register_private_data(size_t n);
221
222 /**
223  * Get a pointer to the private data you registered.
224  * @param  edge The edge.
225  * @param  ofs  The number, you obtained with
226  *              edges_register_private_data().
227  * @return A pointer to the private data.
228  */
229 static INLINE void *_get_edge_private_data(const ir_edge_t *edge, int ofs)
230 {
231         /* Get the size of the edge. */
232         size_t size =
233                 is_Block(edge->src) ? sizeof(ir_block_edge_t) : sizeof(ir_edge_t);
234
235         return (void *) ((char *) edge + size + ofs);
236 }
237
238 /**
239  * Initialize the out edges.
240  * This must be called before firm is initialized.
241  */
242 extern void init_edges(void);
243
244 #define get_irn_out_edge_first(irn)      _get_irn_out_edge_first(irn)
245 #define get_irn_out_edge_next(irn,last)  _get_irn_out_edge_next(irn, last)
246 #define get_block_succ_first(irn)        _get_block_succ_first(irn)
247 #define get_block_succ_next(irn,last)    _get_block_succ_next(irn, last)
248 #define get_edge_src_irn(edge)           _get_edge_src_irn(edge)
249 #define get_edge_src_pos(edge)           _get_edge_src_pos(edge)
250 #define get_edge_private_data(edge,ofs)  _get_edge_private_data(edge,ofs)
251 #define edges_activated(irg)             _edges_activated(irg)
252 #define edges_assure(irg)                _edges_assure(irg)
253
254 #endif /* _FIRM_EDGES_T_H */