"Edges private data" was broken. Added allocation of private data again and added...
[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  * Modified by: Andreas Schoesser
7  * Created:     15.01.2005
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2005 Universit�t Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * everlasting outs.
15  * @author Sebastian Hack
16  * @date 15.1.2005
17  */
18
19 #ifndef _FIRM_EDGES_T_H
20 #define _FIRM_EDGES_T_H
21
22 #include "firm_config.h"
23 #include "debug.h"
24
25 #include "set.h"
26 #include "list.h"
27
28 #include "irnode_t.h"
29 #include "irgraph_t.h"
30
31 #include "iredgekinds.h"
32 #include "iredges.h"
33
34 #define DBG_EDGES  "firm.ir.edges"
35
36 /**
37  * An edge.
38  */
39 struct _ir_edge_t {
40   ir_node *src;           /**< The source node of the edge. */
41   int pos;                /**< The position of the edge at @p src. */
42   unsigned invalid : 1;   /**< edges that are removed are marked invalid. */
43   unsigned present : 1;   /**< Used by the verifier. Don't rely on its content. */
44   unsigned kind    : 4;   /**< The kind of the edge. */
45   struct list_head list;  /**< The list head to queue all out edges at a node. */
46 #ifdef DEBUG_libfirm
47   long src_nr;            /**< The node number of the source node. */
48 #endif
49 };
50
51
52 /** Accessor for private irn info. */
53 #define _get_irn_edge_info(irn, kind) (&(((irn)->edge_info)[kind]))
54
55 /** Accessor for private irg info. */
56 #define _get_irg_edge_info(irg, kind) (&(((irg)->edge_info)[kind]))
57
58 /**
59 * Convenience macro to get the outs_head from a irn_edge_info_t
60 * struct.
61 */
62 #define _get_irn_outs_head(irn, kind) (&_get_irn_edge_info(irn, kind)->outs_head)
63
64 /**
65 * Get the first edge pointing to some node.
66 * @note There is no order on out edges. First in this context only
67 * means, that you get some starting point into the list of edges.
68 * @param irn The node.
69 * @return The first out edge that points to this node.
70 */
71 static INLINE const ir_edge_t *_get_irn_out_edge_first_kind(const ir_node *irn, ir_edge_kind_t kind)
72 {
73         const struct list_head *head = _get_irn_outs_head(irn, kind);
74         return list_empty(head) ? NULL : list_entry(head->next, ir_edge_t, list);
75 }
76
77 /**
78 * Get the next edge in the out list of some node.
79 * @param irn The node.
80 * @param last The last out edge you have seen.
81 * @return The next out edge in @p irn 's out list after @p last.
82 */
83 static INLINE const ir_edge_t *_get_irn_out_edge_next(const ir_node *irn, const ir_edge_t *last)
84 {
85         struct list_head *next = last->list.next;
86         return next == _get_irn_outs_head(irn, last->kind) ? NULL : list_entry(next, ir_edge_t, list);
87 }
88
89 /**
90 * Get the number of edges pointing to a node.
91 * @param irn The node.
92 * @return The number of edges pointing to this node.
93 */
94 static INLINE int _get_irn_n_edges_kind(const ir_node *irn, int kind)
95 {
96         /* Perhaps out_count was buggy. This code does it more safely. */
97 #if 1
98         int res = 0;
99         const struct list_head *pos, *head = _get_irn_outs_head(irn, kind);
100         list_for_each(pos, head)
101                 res++;
102         return res;
103 #else
104         return _get_irn_edge_info(irn, kind)->out_count;
105 #endif
106 }
107
108 static INLINE int _edges_activated_kind(const ir_graph *irg, ir_edge_kind_t kind)
109 {
110         return _get_irg_edge_info(irg, kind)->activated;
111 }
112
113 /**
114 * Assure, that the edges information is present for a certain graph.
115 * @param irg The graph.
116 */
117 static INLINE void _edges_assure_kind(ir_graph *irg, int kind)
118 {
119         if(!_edges_activated_kind(irg, kind))
120                 edges_activate_kind(irg, kind);
121 }
122
123 void edges_init_graph_kind(ir_graph *irg, ir_edge_kind_t kind);
124
125 /**
126 * Notify of a edge change.
127 * The edge from (src, pos) -> old_tgt is redirected to tgt
128 */
129 void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_edge_kind_t kind, ir_graph *irg);
130
131 /**
132 * A node is deleted.
133 */
134 void edges_node_deleted(ir_node *old, ir_graph *irg);
135
136 void edges_invalidate_kind(ir_node *irn, ir_edge_kind_t kind, ir_graph *irg);
137
138 /**
139 * Register additional memory in an edge.
140 * This must be called before Firm is initialized.
141 * @param  n Number of bytes you need.
142 * @return A number you have to keep and to pass
143 *         edges_get_private_data()
144 *         to get a pointer to your data.
145 */
146 int edges_register_private_data(size_t n);
147
148 /**
149 * Get a pointer to the private data you registered.
150 * @param  edge The edge.
151 * @param  ofs  The number, you obtained with
152 *              edges_register_private_data().
153 * @return A pointer to the private data.
154 */
155 static INLINE void *_get_edge_private_data(const ir_edge_t *edge, int ofs)
156 {
157         return (void *) ((char *) edge + sizeof(edge[0]) + ofs);
158 }
159
160 static INLINE ir_node *_get_edge_src_irn(const ir_edge_t *edge)
161 {
162         return edge->src;
163 }
164
165 static INLINE int _get_edge_src_pos(const ir_edge_t *edge)
166 {
167         return edge->pos;
168 }
169
170 /**
171 * Initialize the out edges.
172 * This must be called before firm is initialized.
173 */
174 extern void init_edges(void);
175
176 /**
177  * Set dbg information for edges.
178  */
179 void edges_init_dbg(int do_dbg);
180
181 void edges_invalidate_all(ir_node *irn, ir_graph *irg);
182
183 #define get_irn_n_edges_kind(irn, kind)   _get_irn_n_edges_kind(irn, kind)
184 #define get_edge_src_irn(edge)            _get_edge_src_irn(edge)
185 #define get_edge_src_pos(edge)            _get_edge_src_pos(edge)
186 #define get_edge_private_data(edge, ofs)  _get_edge_private_data(edge,ofs)
187 #define get_irn_out_edge_next(irn, last)  _get_irn_out_edge_next(irn, last)
188
189 #ifndef get_irn_n_edges
190 #define get_irn_n_edges(irn)              _get_irn_n_edges_kind(irn, EDGE_KIND_NORMAL)
191 #endif
192
193 #ifndef get_irn_out_edge_first
194 #define get_irn_out_edge_first(irn)       _get_irn_out_edge_first_kind(irn, EDGE_KIND_NORMAL)
195 #endif
196
197 #ifndef get_block_succ_first
198 #define get_block_succ_first(irn)         _get_irn_out_edge_first_kind(irn, EDGE_KIND_BLOCK)
199 #endif
200
201 #ifndef get_block_succ_next
202 #define get_block_succ_next(irn, last)    _get_irn_out_edge_next(irn, last)
203 #endif
204
205
206
207 #endif /* _FIRM_EDGES_T_H */