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