3 * File name: ir/ir/irgraph.c
4 * Purpose: Entry point to the representation of procedure code -- internal header.
5 * Author: Martin Trapp, Christian Schaefer
6 * Modified by: Goetz Lindenmaier, Michael Beck
9 * Copyright: (c) 1998-2007 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * IR graph construction.
21 #include "firm_types.h"
24 #include "firm_common_t.h"
25 #include "irtypeinfo.h"
27 #include "pseudo_irg.h"
31 #include "tr_inheritance.h"
32 #include "iredgekinds.h"
36 #include "execution_frequency.h"
42 /** Prefix that is added to every frame type. */
43 #define FRAME_TP_SUFFIX "frame_tp"
46 * Edge info to put into an irg.
48 typedef struct _irg_edge_info_t {
49 set *edges; /**< a set containing all edges of a graph. */
50 unsigned activated : 1; /**< set if edges are activated for the graph. */
53 typedef irg_edge_info_t irg_edges_info_t[EDGE_KIND_LAST];
56 * Index constants for nodes that can be accessed through the graph itself.
59 anchor_start_block = 0, /**< block the start node will belong to */
60 anchor_start, /**< start node of this ir_graph */
61 anchor_end_block, /**< block the end node will belong to */
62 anchor_end, /**< end node of this ir_graph */
63 anchor_end_reg, /**< end node of this ir_graph */
64 anchor_end_except, /**< end node of this ir_graph */
65 anchor_frame, /**< method's frame */
66 anchor_globals, /**< pointer to the data segment containing all
67 globals as well as global procedures. */
68 anchor_tls, /**< pointer to the thread local storage containing all
70 anchor_initial_mem, /**< initial memory of this graph */
71 anchor_args, /**< methods arguments */
72 anchor_value_param_base, /**< method value param base */
73 anchor_bad, /**< bad node of this ir_graph, the one and
75 anchor_no_mem, /**< NoMem node of this ir_graph, the one and only in this graph */
79 /** A callgraph entry for callees. */
80 typedef struct cg_callee_entry {
81 ir_graph *irg; /**< The called irg. */
82 ir_node **call_list; /**< The list of all calls to the irg. */
83 int max_depth; /**< Maximum depth of all Call nodes to irg. */
87 * An ir_graph holds all information for a procedure.
90 firm_kind kind; /**< Always set to k_ir_graph. */
91 /* -- Basics of the representation -- */
92 ir_entity *ent; /**< The entity of this procedure, i.e.,
93 the type of the procedure and the
94 class it belongs to. */
95 ir_type *frame_type; /**< A class type representing the stack frame.
96 Can include "inner" methods. */
97 ir_node *anchors[anchor_max]; /**< List of anchor nodes. */
98 ir_node **proj_args; /**< Projs of the methods arguments. */
99 struct obstack *obst; /**< The obstack where all of the ir_nodes live. */
100 ir_node *current_block; /**< Current block for newly gen_*()-erated ir_nodes. */
101 struct obstack *extbb_obst; /**< The obstack for extended basic block info. */
103 unsigned last_node_idx; /**< The last IR node index for this graph. */
105 /* -- Fields for graph properties -- */
106 irg_inline_property inline_property; /**< How to handle inlineing. */
107 unsigned additional_properties; /**< Additional graph properties. */
109 /* -- Fields indicating different states of irgraph -- */
110 irg_phase_state phase_state; /**< Compiler phase. */
111 op_pin_state irg_pinned_state; /**< Flag for status of nodes. */
112 irg_outs_state outs_state; /**< Out edges. */
113 irg_dom_state dom_state; /**< Dominator state information. */
114 irg_dom_state pdom_state; /**< Post Dominator state information. */
115 ir_typeinfo_state typeinfo_state; /**< Validity of type information. */
116 irg_callee_info_state callee_info_state; /**< Validity of callee information. */
117 irg_loopinfo_state loopinfo_state; /**< State of loop information. */
118 ir_class_cast_state class_cast_state; /**< Kind of cast operations in code. */
119 irg_extblk_info_state extblk_state; /**< State of extended basic block info. */
120 exec_freq_state execfreq_state; /**< Execution frequency state. */
121 ir_address_taken_computed_state adr_taken_state; /**< Address taken state. */
122 unsigned mem_disambig_opt; /**< Options for the memory disambiguator. */
123 unsigned fp_model; /**< floating point model of the graph. */
125 /* -- Fields for construction -- */
126 #if USE_EXPLICIT_PHI_IN_STACK
127 struct Phi_in_stack *Phi_in_stack; /**< Needed for automatic Phi construction. */
129 int n_loc; /**< Number of local variables in this
130 procedure including procedure parameters. */
131 void **loc_descriptions; /**< Storage for local variable descriptions. */
133 /* -- Fields for optimizations / analysis information -- */
134 pset *value_table; /**< Hash table for global value numbering (cse)
135 for optimizing use in iropt.c */
136 ir_node **outs; /**< Space for the out arrays. */
138 ir_loop *loop; /**< The outermost loop for this graph. */
139 void *link; /**< A void* field to link any information to
142 ir_graph **callers; /**< For callgraph analysis: list of caller graphs. */
143 unsigned char *caller_isbe; /**< For callgraph analysis: set if backedge. */
144 cg_callee_entry **callees; /**< For callgraph analysis: list of callee calls */
145 unsigned char *callee_isbe; /**< For callgraph analysis: set if backedge. */
146 int callgraph_loop_depth; /**< For callgraph analysis */
147 int callgraph_recursion_depth; /**< For callgraph analysis */
148 double method_execution_frequency; /**< For callgraph analysis */
150 ir_loop *l; /**< For callgraph analysis. */
152 /* -- Fields for Walking the graph -- */
153 unsigned long visited; /**< this flag is an identifier for
154 ir walk. it will be incremented
155 every time someone walks through
157 unsigned long block_visited; /**< same as visited, for a complete block */
158 unsigned inside_irg_walk : 1; /**< set to 1 if we are currently in an irg walk */
159 unsigned inside_block_walk : 1; /**< set to 1 if we are currently in a block walk */
160 unsigned estimated_node_count; /**< estimated number of nodes in this graph,
161 updated after every walk */
162 irg_edges_info_t edge_info; /**< edge info for automatic outs */
163 ir_node **idx_irn_map; /**< Array mapping node indexes to nodes. */
166 int n_outs; /**< Size wasted for outs */
167 long graph_nr; /**< a unique graph number for each graph to make output
174 * Initializes the graph construction module
176 void firm_init_irgraph(void);
178 /* Internal constructor that does not add to irp_irgs or the like. */
179 ir_graph *new_r_ir_graph (ir_entity *ent, int n_loc);
181 /** Make a rudimentary ir graph for the constant code.
182 Must look like a correct irg, spare everything else. */
183 ir_graph *new_const_code_irg(void);
186 * Set the op_pin_state_pinned state of a graph.
188 * @param irg the IR graph
189 * @param p new pin state
192 set_irg_pinned (ir_graph *irg, op_pin_state p);
194 /** Returns the obstack associated with the graph. */
195 struct obstack *get_irg_obstack(const ir_graph *irg);
198 * Returns true if the node n is allocated on the storage of graph irg.
200 * @param irg the IR graph
201 * @param n the IR node
203 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n);
205 /*-------------------------------------------------------------------*/
206 /* inline functions for graphs */
207 /*-------------------------------------------------------------------*/
209 extern int firm_interprocedural_view;
212 _get_interprocedural_view(void) {
213 return firm_interprocedural_view;
217 _is_ir_graph(const void *thing) {
218 return (get_kind(thing) == k_ir_graph);
221 /** Returns the start block of a graph. */
222 static INLINE ir_node *
223 _get_irg_start_block(const ir_graph *irg) {
224 return irg->anchors[anchor_start_block];
228 _set_irg_start_block(ir_graph *irg, ir_node *node) {
229 irg->anchors[anchor_start_block] = node;
232 static INLINE ir_node *
233 _get_irg_start(const ir_graph *irg) {
234 return irg->anchors[anchor_start];
238 _set_irg_start(ir_graph *irg, ir_node *node) {
239 irg->anchors[anchor_start] = node;
242 static INLINE ir_node *
243 _get_irg_end_block(const ir_graph *irg) {
244 return irg->anchors[anchor_end_block];
248 _set_irg_end_block(ir_graph *irg, ir_node *node) {
249 irg->anchors[anchor_end_block] = node;
252 static INLINE ir_node *
253 _get_irg_end(const ir_graph *irg) {
254 return irg->anchors[anchor_end];
258 _set_irg_end(ir_graph *irg, ir_node *node) {
259 irg->anchors[anchor_end] = node;
262 static INLINE ir_node *
263 _get_irg_end_reg(const ir_graph *irg) {
264 return irg->anchors[anchor_end_reg];
267 static INLINE ir_node *
268 _get_irg_end_except (const ir_graph *irg) {
269 return irg->anchors[anchor_end_except];
272 static INLINE ir_node *
273 _get_irg_frame(const ir_graph *irg) {
274 return irg->anchors[anchor_frame];
278 _set_irg_frame(ir_graph *irg, ir_node *node) {
279 irg->anchors[anchor_frame] = node;
282 static INLINE ir_node *
283 _get_irg_globals(const ir_graph *irg) {
284 return irg->anchors[anchor_globals];
288 _set_irg_globals(ir_graph *irg, ir_node *node) {
289 irg->anchors[anchor_globals] = node;
292 static INLINE ir_node *
293 _get_irg_tls(const ir_graph *irg) {
294 return irg->anchors[anchor_tls];
298 _set_irg_tls(ir_graph *irg, ir_node *node) {
299 irg->anchors[anchor_tls] = node;
302 static INLINE ir_node *
303 _get_irg_initial_mem(const ir_graph *irg) {
304 return irg->anchors[anchor_initial_mem];
308 _set_irg_initial_mem(ir_graph *irg, ir_node *node) {
309 irg->anchors[anchor_initial_mem] = node;
312 static INLINE ir_node *
313 _get_irg_args(const ir_graph *irg) {
314 return irg->anchors[anchor_args];
318 _set_irg_args(ir_graph *irg, ir_node *node) {
319 irg->anchors[anchor_args] = node;
322 static INLINE ir_node *
323 _get_irg_value_param_base(const ir_graph *irg) {
324 return irg->anchors[anchor_value_param_base];
328 _set_irg_value_param_base(ir_graph *irg, ir_node *node) {
329 irg->anchors[anchor_value_param_base] = node;
332 static INLINE ir_node **
333 _get_irg_proj_args(const ir_graph *irg) {
334 return irg->proj_args;
338 _set_irg_proj_args(ir_graph *irg, ir_node **nodes) {
339 irg->proj_args = nodes;
342 static INLINE ir_node *
343 _get_irg_bad(const ir_graph *irg) {
344 return irg->anchors[anchor_bad];
348 _set_irg_bad(ir_graph *irg, ir_node *node) {
349 irg->anchors[anchor_bad] = node;
352 static INLINE ir_node *
353 _get_irg_no_mem(const ir_graph *irg) {
354 return irg->anchors[anchor_no_mem];
358 _set_irg_no_mem(ir_graph *irg, ir_node *node) {
359 irg->anchors[anchor_no_mem] = node;
361 static INLINE ir_node *
362 _get_irg_current_block(const ir_graph *irg) {
363 return irg->current_block;
367 _set_irg_current_block(ir_graph *irg, ir_node *node) {
368 irg->current_block = node;
371 static INLINE ir_entity *
372 _get_irg_entity(const ir_graph *irg) {
373 assert(irg && irg->ent);
378 _set_irg_entity(ir_graph *irg, ir_entity *ent) {
382 static INLINE ir_type *
383 _get_irg_frame_type(ir_graph *irg) {
384 assert(irg && irg->frame_type);
385 return irg->frame_type = skip_tid(irg->frame_type);
389 _set_irg_frame_type(ir_graph *irg, ir_type *ftp) {
390 assert(is_frame_type(ftp));
391 irg->frame_type = ftp;
394 static INLINE struct obstack *
395 _get_irg_obstack(const ir_graph *irg) {
400 static INLINE irg_phase_state
401 _get_irg_phase_state(const ir_graph *irg) {
402 return irg->phase_state;
406 _set_irg_phase_state(ir_graph *irg, irg_phase_state state) {
407 irg->phase_state = state;
410 static INLINE op_pin_state
411 _get_irg_pinned(const ir_graph *irg) {
412 return irg->irg_pinned_state;
415 static INLINE irg_outs_state
416 _get_irg_outs_state(const ir_graph *irg) {
417 return irg->outs_state;
421 _set_irg_outs_inconsistent(ir_graph *irg) {
422 if (irg->outs_state == outs_consistent)
423 irg->outs_state = outs_inconsistent;
426 static INLINE irg_extblk_state
427 _get_irg_extblk_state(const ir_graph *irg) {
428 return irg->extblk_state;
432 _set_irg_extblk_inconsistent(ir_graph *irg) {
433 if (irg->extblk_state == extblk_valid)
434 irg->extblk_state = extblk_invalid;
437 static INLINE irg_dom_state
438 _get_irg_dom_state(const ir_graph *irg) {
439 return irg->dom_state;
442 static INLINE irg_dom_state
443 _get_irg_postdom_state(const ir_graph *irg) {
444 return irg->pdom_state;
448 _set_irg_doms_inconsistent(ir_graph *irg) {
449 if (irg->dom_state != dom_none)
450 irg->dom_state = dom_inconsistent;
451 if (irg->pdom_state != dom_none)
452 irg->pdom_state = dom_inconsistent;
455 static INLINE irg_loopinfo_state
456 _get_irg_loopinfo_state(const ir_graph *irg) {
457 return irg->loopinfo_state;
461 _set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
462 irg->loopinfo_state = s;
466 _set_irg_loopinfo_inconsistent(ir_graph *irg) {
467 irg->loopinfo_state &= ~loopinfo_valid;
471 _set_irg_pinned(ir_graph *irg, op_pin_state p) {
472 irg->irg_pinned_state = p;
475 static INLINE irg_callee_info_state
476 _get_irg_callee_info_state(const ir_graph *irg) {
477 return irg->callee_info_state;
481 _set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
482 irg_callee_info_state irp_state = get_irp_callee_info_state();
484 irg->callee_info_state = s;
486 /* I could compare ... but who knows? */
487 if ((irp_state == irg_callee_info_consistent) ||
488 ((irp_state == irg_callee_info_inconsistent) && (s == irg_callee_info_none)))
489 set_irp_callee_info_state(s);
492 static INLINE irg_inline_property
493 _get_irg_inline_property(const ir_graph *irg) {
494 return irg->inline_property;
498 _set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
499 irg->inline_property = s;
502 static INLINE unsigned
503 _get_irg_additional_properties(const ir_graph *irg) {
504 if (irg->additional_properties & mtp_property_inherited)
505 return get_method_additional_properties(get_entity_type(irg->ent));
506 return irg->additional_properties;
510 _set_irg_additional_properties(ir_graph *irg, unsigned mask) {
511 /* do not allow to set the mtp_property_inherited flag or
512 * the automatic inheritance of flags will not work */
513 irg->additional_properties = mask & ~mtp_property_inherited;
517 _set_irg_additional_property(ir_graph *irg, mtp_additional_property flag) {
518 unsigned prop = irg->additional_properties;
520 if (prop & mtp_property_inherited)
521 prop = get_method_additional_properties(get_entity_type(irg->ent));
522 irg->additional_properties = prop | flag;
526 _set_irg_link(ir_graph *irg, void *thing) {
531 _get_irg_link(const ir_graph *irg) {
535 static INLINE unsigned long
536 _get_irg_visited(const ir_graph *irg) {
540 static INLINE unsigned long
541 _get_irg_block_visited(const ir_graph *irg) {
542 return irg->block_visited;
546 _set_irg_block_visited(ir_graph *irg, unsigned long visited) {
547 irg->block_visited = visited;
551 _inc_irg_block_visited(ir_graph *irg) {
552 ++irg->block_visited;
556 _dec_irg_block_visited(ir_graph *irg) {
557 --irg->block_visited;
561 _set_inside_block_walk(ir_graph *irg) {
562 irg->inside_block_walk = 1;
566 _clear_inside_block_walk(ir_graph *irg) {
567 irg->inside_block_walk = 0;
570 static INLINE unsigned
571 _inside_block_walk(const ir_graph *irg) {
572 return irg->inside_block_walk;
576 _set_inside_irg_walk(ir_graph *irg) {
577 irg->inside_irg_walk = 1;
581 _clear_inside_irg_walk(ir_graph *irg) {
582 irg->inside_irg_walk = 0;
585 static INLINE unsigned
586 _inside_irg_walk(const ir_graph *irg) {
587 return irg->inside_irg_walk;
590 static INLINE unsigned
591 _get_irg_estimated_node_cnt(const ir_graph *irg) {
592 return irg->estimated_node_count;
595 /* Return the floating point model of this graph. */
596 static INLINE unsigned
597 _get_irg_fp_model(const ir_graph *irg) {
598 return irg->fp_model;
602 * Allocates a new idx in the irg for the node and adds the irn to the idx -> irn map.
603 * @param irg The graph.
604 * @param irn The node.
605 * @return The index allocated for the node.
607 static INLINE unsigned
608 irg_register_node_idx(ir_graph *irg, ir_node *irn)
610 unsigned idx = irg->last_node_idx++;
611 if(idx >= (unsigned) ARR_LEN(irg->idx_irn_map))
612 ARR_RESIZE(ir_node *, irg->idx_irn_map, idx + 1);
614 irg->idx_irn_map[idx] = irn;
619 * Kill a node from the irg. BEWARE: this kills
620 * all later created nodes.
623 irg_kill_node(ir_graph *irg, ir_node *n) {
624 unsigned idx = get_irn_idx(n);
625 if (idx + 1 == irg->last_node_idx)
626 --irg->last_node_idx;
627 irg->idx_irn_map[idx] = NULL;
628 obstack_free(irg->obst, n);
632 * Get the node for an index.
633 * @param irg The graph.
634 * @param idx The index you want the node for.
635 * @return The node with that index or NULL, if there is no node with that index.
636 * @note The node you got might be dead.
638 static INLINE ir_node *
639 get_idx_irn(ir_graph *irg, unsigned idx) {
640 assert(idx < (unsigned) ARR_LEN(irg->idx_irn_map));
641 return irg->idx_irn_map[idx];
644 #define get_interprocedural_view() _get_interprocedural_view()
645 #define is_ir_graph(thing) _is_ir_graph(thing)
646 #define get_irg_start_block(irg) _get_irg_start_block(irg)
647 #define set_irg_start_block(irg, node) _set_irg_start_block(irg, node)
648 #define get_irg_start(irg) _get_irg_start(irg)
649 #define set_irg_start(irg, node) _set_irg_start(irg, node)
650 #define get_irg_end_block(irg) _get_irg_end_block(irg)
651 #define set_irg_end_block(irg, node) _set_irg_end_block(irg, node)
652 #define get_irg_end(irg) _get_irg_end(irg)
653 #define set_irg_end(irg, node) _set_irg_end(irg, node)
654 #define get_irg_end_reg(irg) _get_irg_end_reg(irg)
655 #define get_irg_end_except(irg) _get_irg_end_except(irg)
656 #define get_irg_frame(irg) _get_irg_frame(irg)
657 #define set_irg_frame(irg, node) _set_irg_frame(irg, node)
658 #define get_irg_globals(irg) _get_irg_globals(irg)
659 #define set_irg_globals(irg, node) _set_irg_globals(irg, node)
660 #define get_irg_tls(irg) _get_irg_tls(irg)
661 #define set_irg_tls(irg, node) _set_irg_tls(irg, node)
662 #define get_irg_initial_mem(irg) _get_irg_initial_mem(irg)
663 #define set_irg_initial_mem(irg, node) _set_irg_initial_mem(irg, node)
664 #define get_irg_args(irg) _get_irg_args(irg)
665 #define set_irg_args(irg, node) _set_irg_args(irg, node)
666 #define get_irg_value_param_base(irg) _get_irg_value_param_base(irg)
667 #define set_irg_value_param_base(irg, node) _set_irg_value_param_base(irg, node)
668 #define get_irg_bad(irg) _get_irg_bad(irg)
669 #define set_irg_bad(irg, node) _set_irg_bad(irg, node)
670 #define get_irg_no_mem(irg) _get_irg_no_mem(irg)
671 #define set_irg_no_mem(irg, node) _set_irg_no_mem(irg, node)
672 #define get_irg_current_block(irg) _get_irg_current_block(irg)
673 #define set_irg_current_block(irg, node) _set_irg_current_block(irg, node)
674 #define get_irg_entity(irg) _get_irg_entity(irg)
675 #define set_irg_entity(irg, ent) _set_irg_entity(irg, ent)
676 #define get_irg_frame_type(irg) _get_irg_frame_type(irg)
677 #define set_irg_frame_type(irg, ftp) _set_irg_frame_type(irg, ftp)
678 #define get_irg_obstack(irg) _get_irg_obstack(irg)
679 #define get_irg_phase_state(irg) _get_irg_phase_state(irg)
680 #define set_irg_phase_state(irg, state) _set_irg_phase_state(irg, state)
681 #define get_irg_pinned(irg) _get_irg_pinned(irg)
682 #define get_irg_outs_state(irg) _get_irg_outs_state(irg)
683 #define set_irg_outs_inconsistent(irg) _set_irg_outs_inconsistent(irg)
684 #define get_irg_extblk_state(irg) _get_irg_extblk_state(irg)
685 #define set_irg_extblk_inconsistent(irg) _set_irg_extblk_inconsistent(irg)
686 #define get_irg_dom_state(irg) _get_irg_dom_state(irg)
687 #define get_irg_postdom_state(irg) _get_irg_postdom_state(irg)
688 #define set_irg_doms_inconsistent(irg) _set_irg_doms_inconsistent(irg)
689 #define get_irg_loopinfo_state(irg) _get_irg_loopinfo_state(irg)
690 #define set_irg_loopinfo_state(irg, s) _set_irg_loopinfo_state(irg, s)
691 #define set_irg_loopinfo_inconsistent(irg) _set_irg_loopinfo_inconsistent(irg)
692 #define set_irg_pinned(irg, p) _set_irg_pinned(irg, p)
693 #define get_irg_callee_info_state(irg) _get_irg_callee_info_state(irg)
694 #define set_irg_callee_info_state(irg, s) _set_irg_callee_info_state(irg, s)
695 #define get_irg_inline_property(irg) _get_irg_inline_property(irg)
696 #define set_irg_inline_property(irg, s) _set_irg_inline_property(irg, s)
697 #define get_irg_additional_properties(irg) _get_irg_additional_properties(irg)
698 #define set_irg_additional_properties(irg, m) _set_irg_additional_properties(irg, m)
699 #define set_irg_additional_property(irg, f) _set_irg_additional_property(irg, f)
700 #define set_irg_link(irg, thing) _set_irg_link(irg, thing)
701 #define get_irg_link(irg) _get_irg_link(irg)
702 #define get_irg_visited(irg) _get_irg_visited(irg)
703 #define get_irg_block_visited(irg) _get_irg_block_visited(irg)
704 #define set_irg_block_visited(irg, v) _set_irg_block_visited(irg, v)
705 #define inc_irg_block_visited(irg) _inc_irg_block_visited(irg)
706 #define dec_irg_block_visited(irg) _dec_irg_block_visited(irg)
707 #define set_inside_block_walk(irg) _set_inside_block_walk(irg)
708 #define clear_inside_block_walk(irg) _clear_inside_block_walk(irg)
709 #define inside_block_walk(irg) _inside_block_walk(irg)
710 #define set_inside_irg_walk(irg) _set_inside_irg_walk(irg)
711 #define clear_inside_irg_walk(irg) _clear_inside_irg_walk(irg)
712 #define inside_irg_walk(irg) _inside_irg_walk(irg)
713 #define get_irg_estimated_node_cnt(irg) _get_irg_estimated_node_cnt(irg)
714 #define get_irg_fp_model(irg) _get_irg_fp_model(irg)
716 # endif /* _IRGRAPH_T_H_ */