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