7133b8ea9ef1c27a815129ecb1a26982a759d703
[libfirm] / ir / ir / irgraph_t.h
1 /*
2  * Copyright (C) 1995-2008 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    Entry point to the representation of procedure code -- internal header.
23  * @author   Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  */
25 #ifndef FIRM_IR_IRGRAPH_T_H
26 #define FIRM_IR_IRGRAPH_T_H
27
28 #include "firm_types.h"
29 #include "irgraph.h"
30
31 #include "irtypes.h"
32 #include "irprog.h"
33 #include "type_t.h"
34 #include "entity_t.h"
35 #include "iredgekinds.h"
36
37 #include "irloop.h"
38
39 #include "obst.h"
40 #include "pset.h"
41 #include "set.h"
42
43 /** Suffix that is added to every frame type. */
44 #define FRAME_TP_SUFFIX "frame_tp"
45 #define is_ir_graph(thing)                    is_ir_graph_(thing)
46 #define get_irg_start_block(irg)              get_irg_start_block_(irg)
47 #define set_irg_start_block(irg, node)        set_irg_start_block_(irg, node)
48 #define get_irg_start(irg)                    get_irg_start_(irg)
49 #define set_irg_start(irg, node)              set_irg_start_(irg, node)
50 #define get_irg_end_block(irg)                get_irg_end_block_(irg)
51 #define set_irg_end_block(irg, node)          set_irg_end_block_(irg, node)
52 #define get_irg_end(irg)                      get_irg_end_(irg)
53 #define set_irg_end(irg, node)                set_irg_end_(irg, node)
54 #define get_irg_initial_exec(irg)             get_irg_initial_exec_(irg)
55 #define set_irg_initial_exec(irg, node)       set_irg_initial_exec_(irg, node)
56 #define get_irg_frame(irg)                    get_irg_frame_(irg)
57 #define set_irg_frame(irg, node)              set_irg_frame_(irg, node)
58 #define get_irg_initial_mem(irg)              get_irg_initial_mem_(irg)
59 #define set_irg_initial_mem(irg, node)        set_irg_initial_mem_(irg, node)
60 #define get_irg_args(irg)                     get_irg_args_(irg)
61 #define set_irg_args(irg, node)               set_irg_args_(irg, node)
62 #define get_irg_no_mem(irg)                   get_irg_no_mem_(irg)
63 #define set_irn_no_mem(irg, node)             set_irn_no_mem_(irg, node)
64 #define get_irg_entity(irg)                   get_irg_entity_(irg)
65 #define set_irg_entity(irg, ent)              set_irg_entity_(irg, ent)
66 #define get_irg_frame_type(irg)               get_irg_frame_type_(irg)
67 #define set_irg_frame_type(irg, ftp)          set_irg_frame_type_(irg, ftp)
68 #define get_irg_pinned(irg)                   get_irg_pinned_(irg)
69 #define get_irg_callee_info_state(irg)        get_irg_callee_info_state_(irg)
70 #define set_irg_callee_info_state(irg, s)     set_irg_callee_info_state_(irg, s)
71 #define get_irg_additional_properties(irg)    get_irg_additional_properties_(irg)
72 #define set_irg_additional_properties(irg, m) set_irg_additional_properties_(irg, m)
73 #define set_irg_additional_property(irg, f)   set_irg_additional_property_(irg, f)
74 #define set_irg_link(irg, thing)              set_irg_link_(irg, thing)
75 #define get_irg_link(irg)                     get_irg_link_(irg)
76 #define get_irg_visited(irg)                  get_irg_visited_(irg)
77 #define get_irg_block_visited(irg)            get_irg_block_visited_(irg)
78 #define set_irg_block_visited(irg, v)         set_irg_block_visited_(irg, v)
79 #define inc_irg_block_visited(irg)            inc_irg_block_visited_(irg)
80 #define dec_irg_block_visited(irg)            dec_irg_block_visited_(irg)
81 #define get_irg_estimated_node_cnt(irg)       get_irg_estimated_node_cnt_(irg)
82 #define get_irg_fp_model(irg)                 get_irg_fp_model_(irg)
83 #define get_idx_irn(irg, idx)                 get_idx_irn_(irg, idx)
84 #define irg_is_constrained(irg, constraints)  irg_is_constrained_(irg, constraints)
85 #define add_irg_properties(irg, props)        add_irg_properties_(irg, props)
86 #define clear_irg_properties(irg, props)      clear_irg_properties_(irg, props)
87 #define irg_has_properties(irg, props)        irg_has_properties_(irg, props)
88
89 /**
90  * Initializes the graph construction module.
91  */
92 void firm_init_irgraph(void);
93
94 /**
95  * Set the number of locals for a given graph.
96  *
97  * @param irg    the graph
98  * @param n_loc  number of locals
99  */
100 void irg_set_nloc(ir_graph *res, int n_loc);
101
102 /**
103  * Internal constructor that does not add to irp_irgs or the like.
104  */
105 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc);
106
107 /**
108  * Make a rudimentary ir graph for the constant code.
109  * Must look like a correct irg, spare everything else.
110  */
111 ir_graph *new_const_code_irg(void);
112
113 /**
114  * Create a new graph that is a copy of a given one.
115  * Uses the link fields of the original graphs.
116  *
117  * @param irg  The graph that must be copied.
118  */
119 ir_graph *create_irg_copy(ir_graph *irg);
120
121 /**
122  * Set the op_pin_state_pinned state of a graph.
123  *
124  * @param irg     the IR graph
125  * @param p       new pin state
126  */
127 static inline void set_irg_pinned(ir_graph *irg, op_pin_state p)
128 {
129         irg->irg_pinned_state = p;
130 }
131
132 /** Returns the obstack associated with the graph. */
133 static inline struct obstack *get_irg_obstack(const ir_graph *irg)
134 {
135         return irg->obst;
136 }
137
138
139
140 /**
141  * Returns true if the node n is allocated on the storage of graph irg.
142  *
143  * @param irg   the IR graph
144  * @param n the IR node
145  */
146 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n);
147
148 /*-------------------------------------------------------------------*/
149 /* inline functions for graphs                                       */
150 /*-------------------------------------------------------------------*/
151
152 static inline int is_ir_graph_(const void *thing)
153 {
154         return (get_kind(thing) == k_ir_graph);
155 }
156
157 /** Returns the start block of a graph. */
158 static inline ir_node *get_irg_start_block_(const ir_graph *irg)
159 {
160         return get_irn_n(irg->anchor, anchor_start_block);
161 }
162
163 static inline void set_irg_start_block_(ir_graph *irg, ir_node *node)
164 {
165         set_irn_n(irg->anchor, anchor_start_block, node);
166 }
167
168 static inline ir_node *get_irg_start_(const ir_graph *irg)
169 {
170         return get_irn_n(irg->anchor, anchor_start);
171 }
172
173 static inline void set_irg_start_(ir_graph *irg, ir_node *node)
174 {
175         set_irn_n(irg->anchor, anchor_start, node);
176 }
177
178 static inline ir_node *get_irg_end_block_(const ir_graph *irg)
179 {
180         return get_irn_n(irg->anchor, anchor_end_block);
181 }
182
183 static inline void set_irg_end_block_(ir_graph *irg, ir_node *node)
184 {
185         set_irn_n(irg->anchor, anchor_end_block, node);
186 }
187
188 static inline ir_node *get_irg_end_(const ir_graph *irg)
189 {
190         return get_irn_n(irg->anchor, anchor_end);
191 }
192
193 static inline void set_irg_end_(ir_graph *irg, ir_node *node)
194 {
195         set_irn_n(irg->anchor, anchor_end, node);
196 }
197
198 static inline ir_node *get_irg_initial_exec_(const ir_graph *irg)
199 {
200         return get_irn_n(irg->anchor, anchor_initial_exec);
201 }
202
203 static inline void set_irg_initial_exec_(ir_graph *irg, ir_node *node)
204 {
205         set_irn_n(irg->anchor, anchor_initial_exec, node);
206 }
207
208 static inline ir_node *get_irg_frame_(const ir_graph *irg)
209 {
210         return get_irn_n(irg->anchor, anchor_frame);
211 }
212
213 static inline void set_irg_frame_(ir_graph *irg, ir_node *node)
214 {
215         set_irn_n(irg->anchor, anchor_frame, node);
216 }
217
218 static inline ir_node *get_irg_initial_mem_(const ir_graph *irg)
219 {
220         return get_irn_n(irg->anchor, anchor_initial_mem);
221 }
222
223 static inline void set_irg_initial_mem_(ir_graph *irg, ir_node *node)
224 {
225         set_irn_n(irg->anchor, anchor_initial_mem, node);
226 }
227
228 static inline ir_node *get_irg_args_(const ir_graph *irg)
229 {
230         return get_irn_n(irg->anchor, anchor_args);
231 }
232
233 static inline void set_irg_args_(ir_graph *irg, ir_node *node)
234 {
235         set_irn_n(irg->anchor, anchor_args, node);
236 }
237
238 static inline ir_node *get_irg_no_mem_(const ir_graph *irg)
239 {
240         return get_irn_n(irg->anchor, anchor_no_mem);
241 }
242
243 static inline void set_irg_no_mem_(ir_graph *irg, ir_node *node)
244 {
245         set_irn_n(irg->anchor, anchor_no_mem, node);
246 }
247
248 static inline ir_entity *get_irg_entity_(const ir_graph *irg)
249 {
250         return irg->ent;
251 }
252
253 static inline void set_irg_entity_(ir_graph *irg, ir_entity *ent)
254 {
255         irg->ent = ent;
256 }
257
258 static inline ir_type *get_irg_frame_type_(ir_graph *irg)
259 {
260         assert(irg->frame_type);
261         return irg->frame_type;
262 }
263
264 static inline void set_irg_frame_type_(ir_graph *irg, ir_type *ftp)
265 {
266         assert(is_frame_type(ftp));
267         irg->frame_type = ftp;
268 }
269
270 static inline op_pin_state get_irg_pinned_(const ir_graph *irg)
271 {
272         return irg->irg_pinned_state;
273 }
274
275 static inline irg_callee_info_state get_irg_callee_info_state_(const ir_graph *irg)
276 {
277         return irg->callee_info_state;
278 }
279
280 static inline void set_irg_callee_info_state_(ir_graph *irg, irg_callee_info_state s)
281 {
282         irg_callee_info_state irp_state = get_irp_callee_info_state();
283
284         irg->callee_info_state = s;
285
286         /* I could compare ... but who knows? */
287         if ((irp_state == irg_callee_info_consistent)  ||
288             ((irp_state == irg_callee_info_inconsistent) && (s == irg_callee_info_none)))
289                 set_irp_callee_info_state(s);
290 }
291
292 static inline void set_irg_link_(ir_graph *irg, void *thing)
293 {
294         irg->link = thing;
295 }
296
297 static inline void *get_irg_link_(const ir_graph *irg)
298 {
299         return irg->link;
300 }
301
302 static inline ir_visited_t get_irg_visited_(const ir_graph *irg)
303 {
304         return irg->visited;
305 }
306
307 static inline ir_visited_t get_irg_block_visited_(const ir_graph *irg)
308 {
309         return irg->block_visited;
310 }
311
312 static inline void set_irg_block_visited_(ir_graph *irg, ir_visited_t visited)
313 {
314         irg->block_visited = visited;
315 }
316
317 static inline void inc_irg_block_visited_(ir_graph *irg)
318 {
319         ++irg->block_visited;
320 }
321
322 static inline void dec_irg_block_visited_(ir_graph *irg)
323 {
324         --irg->block_visited;
325 }
326
327 static inline unsigned get_irg_estimated_node_cnt_(const ir_graph *irg)
328 {
329         return irg->estimated_node_count;
330 }
331
332 /* Return the floating point model of this graph. */
333 static inline unsigned get_irg_fp_model_(const ir_graph *irg)
334 {
335         return irg->fp_model;
336 }
337
338 static inline int irg_is_constrained_(const ir_graph *irg,
339                                       ir_graph_constraints_t constraints)
340 {
341         return (irg->constraints & constraints) == constraints;
342 }
343
344 static inline void add_irg_properties_(ir_graph *irg,
345                                        ir_graph_properties_t props)
346 {
347         irg->properties |= props;
348 }
349
350 static inline void clear_irg_properties_(ir_graph *irg,
351                                     ir_graph_properties_t props)
352 {
353         irg->properties &= ~props;
354 }
355
356 static inline int irg_has_properties_(const ir_graph *irg,
357                                       ir_graph_properties_t props)
358 {
359         return (irg->properties & props) == props;
360 }
361
362 /**
363  * Allocates a new idx in the irg for the node and adds the irn to the idx -> irn map.
364  * @param irg The graph.
365  * @param irn The node.
366  * @return    The index allocated for the node.
367  */
368 static inline unsigned irg_register_node_idx(ir_graph *irg, ir_node *irn)
369 {
370         unsigned idx = irg->last_node_idx++;
371         if (idx >= (unsigned)ARR_LEN(irg->idx_irn_map))
372                 ARR_RESIZE(ir_node *, irg->idx_irn_map, idx + 1);
373
374         irg->idx_irn_map[idx] = irn;
375         return idx;
376 }
377
378 /**
379  * Kill a node from the irg. BEWARE: this kills
380  * all later created nodes.
381  */
382 static inline void irg_kill_node(ir_graph *irg, ir_node *n)
383 {
384         unsigned idx = get_irn_idx(n);
385         assert(idx + 1 == irg->last_node_idx);
386
387         if (idx + 1 == irg->last_node_idx)
388                 --irg->last_node_idx;
389         irg->idx_irn_map[idx] = NULL;
390         obstack_free(irg->obst, n);
391 }
392
393 /**
394  * Get the node for an index.
395  * @param irg The graph.
396  * @param idx The index you want the node for.
397  * @return    The node with that index or NULL, if there is no node with that index.
398  * @note      The node you got might be dead.
399  */
400 static inline ir_node *get_idx_irn_(const ir_graph *irg, unsigned idx)
401 {
402         assert(idx < (unsigned) ARR_LEN(irg->idx_irn_map));
403         return irg->idx_irn_map[idx];
404 }
405
406 /**
407  * Return the number of anchors in this graph.
408  */
409 static inline int get_irg_n_anchors(const ir_graph *irg)
410 {
411         return get_irn_arity(irg->anchor);
412 }
413
414 /**
415  * Return anchor for given index
416  */
417 static inline ir_node *get_irg_anchor(const ir_graph *irg, int idx)
418 {
419         return get_irn_n(irg->anchor, idx);
420 }
421
422 /**
423  * Set anchor for given index
424  */
425 static inline void set_irg_anchor(ir_graph *irg, int idx, ir_node *irn)
426 {
427         set_irn_n(irg->anchor, idx, irn);
428 }
429
430 #endif