2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Entry point to the representation of procedure code -- internal header.
23 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
26 #ifndef FIRM_IR_IRGRAPH_T_H
27 #define FIRM_IR_IRGRAPH_T_H
29 #include "firm_types.h"
36 #include "iredgekinds.h"
44 /** Suffix that is added to every frame type. */
45 #define FRAME_TP_SUFFIX "frame_tp"
48 * Initializes the graph construction module.
50 void firm_init_irgraph(void);
53 * Set the number of locals for a given graph.
55 * @param irg the graph
56 * @param n_loc number of locals
58 void irg_set_nloc(ir_graph *res, int n_loc);
61 * Internal constructor that does not add to irp_irgs or the like.
63 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc);
66 * Make a rudimentary ir graph for the constant code.
67 * Must look like a correct irg, spare everything else.
69 ir_graph *new_const_code_irg(void);
72 * Create a new graph that is a copy of a given one.
73 * Uses the link fields of the original graphs.
75 * @param irg The graph that must be copied.
77 ir_graph *create_irg_copy(ir_graph *irg);
80 * Set the op_pin_state_pinned state of a graph.
82 * @param irg the IR graph
83 * @param p new pin state
85 void set_irg_pinned(ir_graph *irg, op_pin_state p);
87 /** Returns the obstack associated with the graph. */
88 struct obstack *get_irg_obstack(const ir_graph *irg);
91 * Returns true if the node n is allocated on the storage of graph irg.
93 * @param irg the IR graph
94 * @param n the IR node
96 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n);
98 /*-------------------------------------------------------------------*/
99 /* inline functions for graphs */
100 /*-------------------------------------------------------------------*/
102 static inline int _is_ir_graph(const void *thing)
104 return (get_kind(thing) == k_ir_graph);
107 /** Returns the start block of a graph. */
108 static inline ir_node *_get_irg_start_block(const ir_graph *irg)
110 return get_irn_n(irg->anchor, anchor_start_block);
113 static inline void _set_irg_start_block(ir_graph *irg, ir_node *node)
115 set_irn_n(irg->anchor, anchor_start_block, node);
118 static inline ir_node *_get_irg_start(const ir_graph *irg)
120 return get_irn_n(irg->anchor, anchor_start);
123 static inline void _set_irg_start(ir_graph *irg, ir_node *node)
125 set_irn_n(irg->anchor, anchor_start, node);
128 static inline ir_node *_get_irg_end_block(const ir_graph *irg)
130 return get_irn_n(irg->anchor, anchor_end_block);
133 static inline void _set_irg_end_block(ir_graph *irg, ir_node *node)
135 set_irn_n(irg->anchor, -1, node);
136 set_irn_n(irg->anchor, anchor_end_block, node);
139 static inline ir_node *_get_irg_end(const ir_graph *irg)
141 return get_irn_n(irg->anchor, anchor_end);
144 static inline void _set_irg_end(ir_graph *irg, ir_node *node)
146 set_irn_n(irg->anchor, anchor_end, node);
149 static inline ir_node *_get_irg_initial_exec(const ir_graph *irg)
151 return get_irn_n(irg->anchor, anchor_initial_exec);
154 static inline void _set_irg_initial_exec(ir_graph *irg, ir_node *node)
156 set_irn_n(irg->anchor, anchor_initial_exec, node);
159 static inline ir_node *_get_irg_frame(const ir_graph *irg)
161 return get_irn_n(irg->anchor, anchor_frame);
164 static inline void _set_irg_frame(ir_graph *irg, ir_node *node)
166 set_irn_n(irg->anchor, anchor_frame, node);
169 static inline ir_node *_get_irg_initial_mem(const ir_graph *irg)
171 return get_irn_n(irg->anchor, anchor_initial_mem);
174 static inline void _set_irg_initial_mem(ir_graph *irg, ir_node *node)
176 set_irn_n(irg->anchor, anchor_initial_mem, node);
179 static inline ir_node *_get_irg_args(const ir_graph *irg)
181 return get_irn_n(irg->anchor, anchor_args);
184 static inline void _set_irg_args(ir_graph *irg, ir_node *node)
186 set_irn_n(irg->anchor, anchor_args, node);
189 static inline ir_node * _get_irg_no_mem(const ir_graph *irg)
191 return get_irn_n(irg->anchor, anchor_no_mem);
194 static inline void _set_irg_no_mem(ir_graph *irg, ir_node *node)
196 set_irn_n(irg->anchor, anchor_no_mem, node);
199 static inline ir_entity *_get_irg_entity(const ir_graph *irg)
205 static inline void _set_irg_entity(ir_graph *irg, ir_entity *ent)
210 static inline ir_type *_get_irg_frame_type(ir_graph *irg)
212 assert(irg->frame_type);
213 return irg->frame_type;
216 static inline void _set_irg_frame_type(ir_graph *irg, ir_type *ftp)
218 assert(is_frame_type(ftp));
219 irg->frame_type = ftp;
222 static inline struct obstack *_get_irg_obstack(const ir_graph *irg)
228 static inline irg_phase_state _get_irg_phase_state(const ir_graph *irg)
230 return irg->phase_state;
233 static inline void _set_irg_phase_state(ir_graph *irg, irg_phase_state state)
235 irg->phase_state = state;
238 static inline op_pin_state _get_irg_pinned(const ir_graph *irg)
240 return irg->irg_pinned_state;
243 static inline irg_extblk_info_state _get_irg_extblk_state(const ir_graph *irg)
245 return irg->extblk_state;
248 static inline void _set_irg_extblk_inconsistent(ir_graph *irg)
250 if (irg->extblk_state == ir_extblk_info_valid)
251 irg->extblk_state = ir_extblk_info_invalid;
254 static inline irg_loopinfo_state _get_irg_loopinfo_state(const ir_graph *irg)
256 return irg->loopinfo_state;
259 static inline void _set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s)
261 irg->loopinfo_state = s;
264 static inline void _set_irg_loopinfo_inconsistent(ir_graph *irg)
266 irg->loopinfo_state = (irg_loopinfo_state) (irg->loopinfo_state & ~loopinfo_valid);
267 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO);
270 static inline void _set_irg_pinned(ir_graph *irg, op_pin_state p)
272 irg->irg_pinned_state = p;
275 static inline irg_callee_info_state _get_irg_callee_info_state(const ir_graph *irg)
277 return irg->callee_info_state;
280 static inline void _set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s)
282 irg_callee_info_state irp_state = get_irp_callee_info_state();
284 irg->callee_info_state = s;
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);
292 static inline irg_inline_property _get_irg_inline_property(const ir_graph *irg)
294 return irg->inline_property;
297 static inline void _set_irg_inline_property(ir_graph *irg, irg_inline_property s)
299 irg->inline_property = s;
302 static inline mtp_additional_properties _get_irg_additional_properties(const ir_graph *irg)
304 if (irg->additional_properties & mtp_property_inherited)
305 return get_method_additional_properties(get_entity_type(irg->ent));
306 return irg->additional_properties;
309 static inline void _set_irg_additional_properties(ir_graph *irg, mtp_additional_properties mask)
311 irg->additional_properties = mask & ~mtp_property_inherited;
314 static inline void _add_irg_additional_properties(ir_graph *irg, mtp_additional_properties flag)
316 mtp_additional_properties prop = irg->additional_properties;
318 if (prop & mtp_property_inherited)
319 prop = get_method_additional_properties(get_entity_type(irg->ent));
320 irg->additional_properties = prop | flag;
323 static inline void _set_irg_link(ir_graph *irg, void *thing)
328 static inline void *_get_irg_link(const ir_graph *irg)
333 static inline ir_visited_t _get_irg_visited(const ir_graph *irg)
338 static inline ir_visited_t _get_irg_block_visited(const ir_graph *irg)
340 return irg->block_visited;
343 static inline void _set_irg_block_visited(ir_graph *irg, ir_visited_t visited)
345 irg->block_visited = visited;
348 static inline void _inc_irg_block_visited(ir_graph *irg)
350 ++irg->block_visited;
353 static inline void _dec_irg_block_visited(ir_graph *irg)
355 --irg->block_visited;
358 static inline unsigned _get_irg_estimated_node_cnt(const ir_graph *irg)
360 return irg->estimated_node_count;
363 /* Return the floating point model of this graph. */
364 static inline unsigned _get_irg_fp_model(const ir_graph *irg)
366 return irg->fp_model;
369 static inline void _set_irg_state(ir_graph *irg, ir_graph_state_t state)
371 irg->state = (ir_graph_state_t) (irg->state | state);
374 static inline void _clear_irg_state(ir_graph *irg, ir_graph_state_t state)
376 irg->state = (ir_graph_state_t) (irg->state & ~state);
379 static inline int _is_irg_state(const ir_graph *irg, ir_graph_state_t state)
381 return (irg->state & state) == state;
385 * Allocates a new idx in the irg for the node and adds the irn to the idx -> irn map.
386 * @param irg The graph.
387 * @param irn The node.
388 * @return The index allocated for the node.
390 static inline unsigned irg_register_node_idx(ir_graph *irg, ir_node *irn)
392 unsigned idx = irg->last_node_idx++;
393 if (idx >= (unsigned)ARR_LEN(irg->idx_irn_map))
394 ARR_RESIZE(ir_node *, irg->idx_irn_map, idx + 1);
396 irg->idx_irn_map[idx] = irn;
401 * Kill a node from the irg. BEWARE: this kills
402 * all later created nodes.
404 static inline void irg_kill_node(ir_graph *irg, ir_node *n)
406 unsigned idx = get_irn_idx(n);
407 assert(idx + 1 == irg->last_node_idx);
409 if (idx + 1 == irg->last_node_idx)
410 --irg->last_node_idx;
411 irg->idx_irn_map[idx] = NULL;
412 obstack_free(irg->obst, n);
416 * Get the node for an index.
417 * @param irg The graph.
418 * @param idx The index you want the node for.
419 * @return The node with that index or NULL, if there is no node with that index.
420 * @note The node you got might be dead.
422 static inline ir_node *_get_idx_irn(const ir_graph *irg, unsigned idx)
424 assert(idx < (unsigned) ARR_LEN(irg->idx_irn_map));
425 return irg->idx_irn_map[idx];
429 * Return the number of anchors in this graph.
431 static inline int get_irg_n_anchors(const ir_graph *irg)
433 return get_irn_arity(irg->anchor);
437 * Return anchor for given index
439 static inline ir_node *get_irg_anchor(const ir_graph *irg, int idx)
441 return get_irn_n(irg->anchor, idx);
445 * Set anchor for given index
447 static inline void set_irg_anchor(ir_graph *irg, int idx, ir_node *irn)
449 set_irn_n(irg->anchor, idx, irn);
455 * Register a phase on an irg.
456 * The phase will then be managed by the irg. This means you can easily
457 * access the phase when you only have a graph handle, the memory will be
458 * freed when the graph is freed and some care is taken that the phase data
459 * will be invalidated/preserved on events like dead code elemination and
462 void irg_register_phase(ir_graph *irg, ir_phase_id id, ir_phase *phase);
465 * Frees all phase infos attached to an irg
467 void irg_invalidate_phases(ir_graph *irg);
470 * return phase with given id
472 static inline ir_phase *irg_get_phase(const ir_graph *irg, ir_phase_id id)
474 assert(id <= PHASE_LAST);
475 return irg->phases[id];
479 #define is_ir_graph(thing) _is_ir_graph(thing)
480 #define get_irg_start_block(irg) _get_irg_start_block(irg)
481 #define set_irg_start_block(irg, node) _set_irg_start_block(irg, node)
482 #define get_irg_start(irg) _get_irg_start(irg)
483 #define set_irg_start(irg, node) _set_irg_start(irg, node)
484 #define get_irg_end_block(irg) _get_irg_end_block(irg)
485 #define set_irg_end_block(irg, node) _set_irg_end_block(irg, node)
486 #define get_irg_end(irg) _get_irg_end(irg)
487 #define set_irg_end(irg, node) _set_irg_end(irg, node)
488 #define get_irg_initial_exec(irg) _get_irg_initial_exec(irg)
489 #define set_irg_initial_exec(irg, node) _set_irg_initial_exec(irg, node)
490 #define get_irg_frame(irg) _get_irg_frame(irg)
491 #define set_irg_frame(irg, node) _set_irg_frame(irg, node)
492 #define get_irg_initial_mem(irg) _get_irg_initial_mem(irg)
493 #define set_irg_initial_mem(irg, node) _set_irg_initial_mem(irg, node)
494 #define get_irg_args(irg) _get_irg_args(irg)
495 #define set_irg_args(irg, node) _set_irg_args(irg, node)
496 #define get_irg_no_mem(irg) _get_irg_no_mem(irg)
497 #define set_irn_no_mem(irg, node) _set_irn_no_mem(irg, node)
498 #define get_irg_entity(irg) _get_irg_entity(irg)
499 #define set_irg_entity(irg, ent) _set_irg_entity(irg, ent)
500 #define get_irg_frame_type(irg) _get_irg_frame_type(irg)
501 #define set_irg_frame_type(irg, ftp) _set_irg_frame_type(irg, ftp)
502 #define get_irg_obstack(irg) _get_irg_obstack(irg)
503 #define get_irg_phase_state(irg) _get_irg_phase_state(irg)
504 #define set_irg_phase_state(irg, state) _set_irg_phase_state(irg, state)
505 #define get_irg_pinned(irg) _get_irg_pinned(irg)
506 #define get_irg_extblk_state(irg) _get_irg_extblk_state(irg)
507 #define set_irg_extblk_inconsistent(irg) _set_irg_extblk_inconsistent(irg)
508 #define get_irg_loopinfo_state(irg) _get_irg_loopinfo_state(irg)
509 #define set_irg_loopinfo_state(irg, s) _set_irg_loopinfo_state(irg, s)
510 #define set_irg_loopinfo_inconsistent(irg) _set_irg_loopinfo_inconsistent(irg)
511 #define set_irg_pinned(irg, p) _set_irg_pinned(irg, p)
512 #define get_irg_callee_info_state(irg) _get_irg_callee_info_state(irg)
513 #define set_irg_callee_info_state(irg, s) _set_irg_callee_info_state(irg, s)
514 #define get_irg_inline_property(irg) _get_irg_inline_property(irg)
515 #define set_irg_inline_property(irg, s) _set_irg_inline_property(irg, s)
516 #define get_irg_additional_properties(irg) _get_irg_additional_properties(irg)
517 #define set_irg_additional_properties(irg, m) _set_irg_additional_properties(irg, m)
518 #define set_irg_additional_property(irg, f) _set_irg_additional_property(irg, f)
519 #define set_irg_link(irg, thing) _set_irg_link(irg, thing)
520 #define get_irg_link(irg) _get_irg_link(irg)
521 #define get_irg_visited(irg) _get_irg_visited(irg)
522 #define get_irg_block_visited(irg) _get_irg_block_visited(irg)
523 #define set_irg_block_visited(irg, v) _set_irg_block_visited(irg, v)
524 #define inc_irg_block_visited(irg) _inc_irg_block_visited(irg)
525 #define dec_irg_block_visited(irg) _dec_irg_block_visited(irg)
526 #define get_irg_estimated_node_cnt(irg) _get_irg_estimated_node_cnt(irg)
527 #define get_irg_fp_model(irg) _get_irg_fp_model(irg)
528 #define get_idx_irn(irg, idx) _get_idx_irn(irg, idx)
529 #define set_irg_state(irg, state) _set_irg_state(irg, state)
530 #define clear_irg_state(irg, state) _clear_irg_state(irg, state)
531 #define is_irg_state(irg, state) _is_irg_state(irg, state)