introduce Switch node
[libfirm] / ir / ir / iredges_t.h
1 /*
2  * Copyright (C) 1995-2011 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   Everlasting outs -- private header.
23  * @author  Sebastian Hack, Andreas Schoesser
24  * @date    15.01.2005
25  * @version $Id$
26  */
27 #ifndef FIRM_IR_EDGES_T_H
28 #define FIRM_IR_EDGES_T_H
29
30 #include "debug.h"
31
32 #include "set.h"
33 #include "list.h"
34
35 #include "irnode_t.h"
36 #include "irgraph_t.h"
37
38 #include "iredgekinds.h"
39 #include "iredges.h"
40
41 #define DBG_EDGES  "firm.ir.edges"
42
43 /**
44  * An edge.
45  */
46 struct ir_edge_t {
47         ir_node  *src;          /**< The source node of the edge. */
48         int      pos;           /**< The position of the edge at @p src. */
49         unsigned invalid : 1;   /**< edges that are removed are marked invalid. */
50         unsigned present : 1;   /**< Used by the verifier. Don't rely on its content. */
51         unsigned kind    : 4;   /**< The kind of the edge. */
52         struct list_head list;  /**< The list head to queue all out edges at a node. */
53 };
54
55 /** Accessor for private irn info. */
56 static inline irn_edge_info_t *get_irn_edge_info(ir_node *node,
57                                                  ir_edge_kind_t kind)
58 {
59         return &node->edge_info[kind];
60 }
61
62 static inline const irn_edge_info_t *get_irn_edge_info_const(
63                 const ir_node *node, ir_edge_kind_t kind)
64 {
65         return &node->edge_info[kind];
66 }
67
68 /** Accessor for private irg info. */
69 static inline irg_edge_info_t *get_irg_edge_info(ir_graph *irg,
70                                                  ir_edge_kind_t kind)
71 {
72         return &irg->edge_info[kind];
73 }
74
75 /** Accessor for private irg info. */
76 static inline const irg_edge_info_t *get_irg_edge_info_const(
77                 const ir_graph *irg, ir_edge_kind_t kind)
78 {
79         return &irg->edge_info[kind];
80 }
81
82 /**
83  * Get the first edge pointing to some node.
84  * @note There is no order on out edges. First in this context only
85  * means, that you get some starting point into the list of edges.
86  * @param irn The node.
87  * @return The first out edge that points to this node.
88  */
89 static inline const ir_edge_t *get_irn_out_edge_first_kind_(const ir_node *irn, ir_edge_kind_t kind)
90 {
91         const struct list_head *head;
92         assert(edges_activated_kind(get_irn_irg(irn), kind));
93         head = &get_irn_edge_info_const(irn, kind)->outs_head;
94         return list_empty(head) ? NULL : list_entry(head->next, ir_edge_t, list);
95 }
96
97 /**
98  * Get the next edge in the out list of some node.
99  * @param irn The node.
100  * @param last The last out edge you have seen.
101  * @return The next out edge in @p irn 's out list after @p last.
102  */
103 static inline const ir_edge_t *get_irn_out_edge_next_(const ir_node *irn, const ir_edge_t *last)
104 {
105         struct list_head *next = last->list.next;
106         const struct list_head *head
107                 = &get_irn_edge_info_const(irn, last->kind)->outs_head;
108         return next == head ? NULL : list_entry(next, ir_edge_t, list);
109 }
110
111 /**
112  * Get the number of edges pointing to a node.
113  * @param irn The node.
114  * @return The number of edges pointing to this node.
115  */
116 static inline int get_irn_n_edges_kind_(const ir_node *irn, int kind)
117 {
118         return get_irn_edge_info_const(irn, kind)->out_count;
119 }
120
121 static inline int edges_activated_kind_(const ir_graph *irg, ir_edge_kind_t kind)
122 {
123         return get_irg_edge_info_const(irg, kind)->activated;
124 }
125
126 /**
127  * Assure, that the edges information is present for a certain graph.
128  * @param irg The graph.
129  */
130 static inline void edges_assure_kind_(ir_graph *irg, ir_edge_kind_t kind)
131 {
132         if(!edges_activated_kind_(irg, kind))
133                 edges_activate_kind(irg, kind);
134 }
135
136 void edges_init_graph_kind(ir_graph *irg, ir_edge_kind_t kind);
137
138 /**
139  * A node might be revivaled by CSE.
140  */
141 void edges_node_revival(ir_node *node);
142
143 void edges_invalidate_kind(ir_node *irn, ir_edge_kind_t kind);
144
145 /**
146  * Register additional memory in an edge.
147  * This must be called before Firm is initialized.
148  * @param  n Number of bytes you need.
149  * @return A number you have to keep and to pass
150  *         edges_get_private_data()
151  *         to get a pointer to your data.
152  */
153 size_t edges_register_private_data(size_t n);
154
155 /**
156  * Get a pointer to the private data you registered.
157  * @param  edge The edge.
158  * @param  ofs  The number, you obtained with
159  *              edges_register_private_data().
160  * @return A pointer to the private data.
161  */
162 static inline void *get_edge_private_data_(const ir_edge_t *edge, int ofs)
163 {
164         return (void *) ((char *) edge + sizeof(edge[0]) + ofs);
165 }
166
167 static inline ir_node *get_edge_src_irn_(const ir_edge_t *edge)
168 {
169         return edge->src;
170 }
171
172 static inline int get_edge_src_pos_(const ir_edge_t *edge)
173 {
174         return edge->pos;
175 }
176
177 /**
178  * Initialize the out edges.
179  * This must be called before firm is initialized.
180  */
181 extern void init_edges(void);
182
183 void edges_invalidate_all(ir_node *irn);
184
185 /**
186  * Helper function to dump the edge set of a graph,
187  * unused in normal code.
188  */
189 void edges_dump_kind(ir_graph *irg, ir_edge_kind_t kind);
190
191 /**
192  * Notify normal and block edges.
193  */
194 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt,
195                        ir_node *old_tgt, ir_graph *irg);
196
197 #define get_irn_n_edges_kind(irn, kind)   get_irn_n_edges_kind_(irn, kind)
198 #define get_edge_src_irn(edge)            get_edge_src_irn_(edge)
199 #define get_edge_src_pos(edge)            get_edge_src_pos_(edge)
200 #define get_edge_private_data(edge, ofs)  get_edge_private_data_(edge,ofs)
201 #define get_irn_out_edge_next(irn, last)  get_irn_out_edge_next_(irn, last)
202
203 #ifndef get_irn_n_edges
204 #define get_irn_n_edges(irn)              get_irn_n_edges_kind_(irn, EDGE_KIND_NORMAL)
205 #endif
206
207 #ifndef get_irn_out_edge_first
208 #define get_irn_out_edge_first(irn)       get_irn_out_edge_first_kind_(irn, EDGE_KIND_NORMAL)
209 #endif
210
211 #ifndef get_block_succ_first
212 #define get_block_succ_first(irn)         get_irn_out_edge_first_kind_(irn, EDGE_KIND_BLOCK)
213 #endif
214
215 #ifndef get_block_succ_next
216 #define get_block_succ_next(irn, last)    get_irn_out_edge_next_(irn, last)
217 #endif
218
219 #endif