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
9 * Copyright: (c) 1998-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * ir graph construction.
18 * @author Martin Trapp, Christian Schaefer
25 #include "firm_types.h"
28 #include "firm_common_t.h"
29 #include "irtypeinfo.h"
31 #include "pseudo_irg.h"
35 #include "tr_inheritance.h"
38 #include "execution_frequency.h"
44 #define FRAME_TP_SUFFIX "frame_tp"
47 * Edge info to put into an irg.
49 typedef struct _irg_edge_info_t {
51 unsigned activated : 1;
55 * Index constants for nodes that can be accessed through the graph itself.
58 anchor_start_block = 0, /**< block the start node will belong to */
59 anchor_start, /**< start node of this ir_graph */
60 anchor_end_block, /**< block the end node will belong to */
61 anchor_end, /**< end node of this ir_graph */
62 anchor_end_reg, /**< end node of this ir_graph */
63 anchor_end_except, /**< end node of this ir_graph */
64 anchor_frame, /**< method's frame */
65 anchor_globals, /**< pointer to the data segment containing all
66 globals as well as global procedures. */
67 anchor_tls, /**< pointer to the thread local storage containing all
69 anchor_initial_mem, /**< initial memory of this graph */
70 anchor_args, /**< methods arguments */
71 anchor_bad, /**< bad node of this ir_graph, the one and
73 anchor_no_mem, /**< NoMem node of this ir_graph, the one and only in this graph */
77 /** ir_graph holds all information for a procedure */
79 firm_kind kind; /**< always set to k_ir_graph*/
80 /* -- Basics of the representation -- */
81 entity *ent; /**< The entity of this procedure, i.e.,
82 the type of the procedure and the
83 class it belongs to. */
84 ir_type *frame_type; /**< A class type representing the stack frame.
85 Can include "inner" methods. */
86 ir_node *anchors[anchor_max]; /**< anchor nodes */
87 ir_node **proj_args; /**< projs of the methods arguments */
88 struct obstack *obst; /**< obstack where all of the ir_nodes live */
89 ir_node *current_block; /**< block for newly gen_*()-erated ir_nodes */
90 struct obstack *extbb_obst; /**< obstack for extended basic block info */
92 unsigned last_node_idx; /**< last IR node index for this graph */
94 /* -- Fields for graph properties -- */
95 irg_inline_property inline_property; /**< How to handle inlineing. */
96 unsigned additional_properties; /**< additional graph properties. */
98 /* -- Fields indicating different states of irgraph -- */
99 irg_phase_state phase_state; /**< compiler phase */
100 op_pin_state irg_pinned_state; /**< Flag for status of nodes */
101 irg_outs_state outs_state; /**< Out edges. */
102 irg_dom_state dom_state; /**< Dominator state information */
103 irg_dom_state pdom_state; /**< Post Dominator state information */
104 ir_typeinfo_state typeinfo_state; /**< Validity of type information */
105 irg_callee_info_state callee_info_state; /**< Validity of callee information */
106 irg_loopinfo_state loopinfo_state; /**< state of loop information */
107 exec_freq_state execfreq_state; /**< state of execution frequency information */
108 ir_class_cast_state class_cast_state; /**< kind of cast operations in code. */
109 irg_extblk_info_state extblk_state; /**< state of extended basic block info */
111 /* -- Fields for construction -- */
112 #if USE_EXPLICIT_PHI_IN_STACK
113 struct Phi_in_stack *Phi_in_stack; /**< needed for automatic Phi construction */
115 int n_loc; /**< number of local variable in this
116 procedure including procedure parameters. */
117 void **loc_descriptions; /**< storage for local variable descriptions */
119 /* -- Fields for optimizations / analysis information -- */
120 pset *value_table; /**< hash table for global value numbering (cse)
121 for optimizing use in iropt.c */
122 ir_node **outs; /**< Space for the out arrays. */
124 ir_loop *loop; /**< The outermost loop */
125 void *link; /**< A void* field to link any information to
128 ir_graph **callers; /**< For callgraph analysis. */
129 unsigned char *caller_isbe; /**< For callgraph analysis: set if backedge. */
130 ir_graph **callees; /**< For callgraph analysis. */
131 unsigned char *callee_isbe; /**< For callgraph analysis: set if backedge. */
132 int callgraph_loop_depth; /**< For callgraph analysis */
133 int callgraph_recursion_depth; /**< For callgraph analysis */
134 double method_execution_frequency; /**< For callgraph analysis */
138 /* -- Fields for Walking the graph -- */
139 unsigned long visited; /**< this flag is an identifier for
140 ir walk. it will be incremented
141 every time someone walks through
143 unsigned long block_visited; /**< same as visited, for a complete block */
144 unsigned estimated_node_count; /**< estimated number of nodes in this graph,
145 updated after every walk */
146 irg_edge_info_t edge_info; /**< edge info for automatic outs */
147 ir_node **idx_irn_map; /**< Array mapping node indexes to nodes. */
150 int n_outs; /**< Size wasted for outs */
151 long graph_nr; /**< a unique graph number for each graph to make output
158 * Initializes the graph construction module
160 void firm_init_irgraph(void);
162 /* Internal constructor that does not add to irp_irgs or the like. */
163 ir_graph *new_r_ir_graph (entity *ent, int n_loc);
165 /** Make a rudimentary ir graph for the constant code.
166 Must look like a correct irg, spare everything else. */
167 ir_graph *new_const_code_irg(void);
170 * Set the op_pin_state_pinned state of a graph.
172 * @param irg the IR graph
173 * @param p new pin state
176 set_irg_pinned (ir_graph *irg, op_pin_state p);
178 /** Returns the obstack associated with the graph. */
179 struct obstack *get_irg_obstack(const ir_graph *irg);
182 * Returns true if the node n is allocated on the storage of graph irg.
184 * @param irg the IR graph
185 * @param n the IR node
187 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n);
189 /*-------------------------------------------------------------------*/
190 /* inline functions for graphs */
191 /*-------------------------------------------------------------------*/
193 extern int firm_interprocedural_view;
196 _get_interprocedural_view(void) {
197 return firm_interprocedural_view;
201 _is_ir_graph(const void *thing) {
202 return (get_kind(thing) == k_ir_graph);
205 /** Returns the start block of a graph. */
206 static INLINE ir_node *
207 _get_irg_start_block(const ir_graph *irg) {
208 return irg->anchors[anchor_start_block];
212 _set_irg_start_block(ir_graph *irg, ir_node *node) {
213 irg->anchors[anchor_start_block] = node;
216 static INLINE ir_node *
217 _get_irg_start(const ir_graph *irg) {
218 return irg->anchors[anchor_start];
222 _set_irg_start(ir_graph *irg, ir_node *node) {
223 irg->anchors[anchor_start] = node;
226 static INLINE ir_node *
227 _get_irg_end_block(const ir_graph *irg) {
228 return irg->anchors[anchor_end_block];
232 _set_irg_end_block(ir_graph *irg, ir_node *node) {
233 irg->anchors[anchor_end_block] = node;
236 static INLINE ir_node *
237 _get_irg_end(const ir_graph *irg) {
238 return irg->anchors[anchor_end];
242 _set_irg_end(ir_graph *irg, ir_node *node) {
243 irg->anchors[anchor_end] = node;
246 static INLINE ir_node *
247 _get_irg_end_reg(const ir_graph *irg) {
248 return irg->anchors[anchor_end_reg];
251 static INLINE ir_node *
252 _get_irg_end_except (const ir_graph *irg) {
253 return irg->anchors[anchor_end_except];
256 static INLINE ir_node *
257 _get_irg_frame(const ir_graph *irg) {
258 return irg->anchors[anchor_frame];
262 _set_irg_frame(ir_graph *irg, ir_node *node) {
263 irg->anchors[anchor_frame] = node;
266 static INLINE ir_node *
267 _get_irg_globals(const ir_graph *irg) {
268 return irg->anchors[anchor_globals];
272 _set_irg_globals(ir_graph *irg, ir_node *node) {
273 irg->anchors[anchor_globals] = node;
276 static INLINE ir_node *
277 _get_irg_tls(const ir_graph *irg) {
278 return irg->anchors[anchor_tls];
282 _set_irg_tls(ir_graph *irg, ir_node *node) {
283 irg->anchors[anchor_tls] = node;
286 static INLINE ir_node *
287 _get_irg_initial_mem(const ir_graph *irg) {
288 return irg->anchors[anchor_initial_mem];
292 _set_irg_initial_mem(ir_graph *irg, ir_node *node) {
293 irg->anchors[anchor_initial_mem] = node;
296 static INLINE ir_node *
297 _get_irg_args(const ir_graph *irg) {
298 return irg->anchors[anchor_args];
302 _set_irg_args(ir_graph *irg, ir_node *node) {
303 irg->anchors[anchor_args] = node;
306 static INLINE ir_node **
307 _get_irg_proj_args(const ir_graph *irg) {
308 return irg->proj_args;
312 _set_irg_proj_args(ir_graph *irg, ir_node **nodes) {
313 irg->proj_args = nodes;
316 static INLINE ir_node *
317 _get_irg_bad(const ir_graph *irg) {
318 return irg->anchors[anchor_bad];
322 _set_irg_bad(ir_graph *irg, ir_node *node) {
323 irg->anchors[anchor_bad] = node;
326 static INLINE ir_node *
327 _get_irg_no_mem(const ir_graph *irg) {
328 return irg->anchors[anchor_no_mem];
332 _set_irg_no_mem(ir_graph *irg, ir_node *node) {
333 irg->anchors[anchor_no_mem] = node;
335 static INLINE ir_node *
336 _get_irg_current_block(const ir_graph *irg) {
337 return irg->current_block;
341 _set_irg_current_block(ir_graph *irg, ir_node *node) {
342 irg->current_block = node;
345 static INLINE entity *
346 _get_irg_entity(const ir_graph *irg) {
347 assert(irg && irg->ent);
352 _set_irg_entity(ir_graph *irg, entity *ent) {
356 static INLINE ir_type *
357 _get_irg_frame_type(ir_graph *irg) {
358 assert(irg && irg->frame_type);
359 return irg->frame_type = skip_tid(irg->frame_type);
363 _set_irg_frame_type(ir_graph *irg, ir_type *ftp) {
364 assert(is_frame_type(ftp));
365 irg->frame_type = ftp;
368 static INLINE struct obstack *
369 _get_irg_obstack(const ir_graph *irg) {
374 static INLINE irg_phase_state
375 _get_irg_phase_state(const ir_graph *irg) {
376 return irg->phase_state;
380 _set_irg_phase_state(ir_graph *irg, irg_phase_state state) {
381 irg->phase_state = state;
384 static INLINE op_pin_state
385 _get_irg_pinned(const ir_graph *irg) {
386 return irg->irg_pinned_state;
389 static INLINE irg_outs_state
390 _get_irg_outs_state(const ir_graph *irg) {
391 return irg->outs_state;
395 _set_irg_outs_inconsistent(ir_graph *irg) {
396 if (irg->outs_state == outs_consistent)
397 irg->outs_state = outs_inconsistent;
400 static INLINE irg_extblk_state
401 _get_irg_extblk_state(const ir_graph *irg) {
402 return irg->extblk_state;
406 _set_irg_extblk_inconsistent(ir_graph *irg) {
407 if (irg->extblk_state == extblk_valid)
408 irg->extblk_state = extblk_invalid;
411 static INLINE irg_dom_state
412 _get_irg_dom_state(const ir_graph *irg) {
413 return irg->dom_state;
416 static INLINE irg_dom_state
417 _get_irg_postdom_state(const ir_graph *irg) {
418 return irg->pdom_state;
422 _set_irg_doms_inconsistent(ir_graph *irg) {
423 if (irg->dom_state != dom_none)
424 irg->dom_state = dom_inconsistent;
425 if (irg->pdom_state != dom_none)
426 irg->pdom_state = dom_inconsistent;
429 static INLINE irg_loopinfo_state
430 _get_irg_loopinfo_state(const ir_graph *irg) {
431 return irg->loopinfo_state;
435 _set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
436 irg->loopinfo_state = s;
440 _set_irg_loopinfo_inconsistent(ir_graph *irg) {
441 irg->loopinfo_state &= ~loopinfo_valid;
445 _set_irg_pinned(ir_graph *irg, op_pin_state p) {
446 irg->irg_pinned_state = p;
449 static INLINE irg_callee_info_state
450 _get_irg_callee_info_state(const ir_graph *irg) {
451 return irg->callee_info_state;
455 _set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
456 irg_callee_info_state irp_state = get_irp_callee_info_state();
458 irg->callee_info_state = s;
460 /* I could compare ... but who knows? */
461 if ((irp_state == irg_callee_info_consistent) ||
462 ((irp_state == irg_callee_info_inconsistent) && (s == irg_callee_info_none)))
463 set_irp_callee_info_state(s);
466 static INLINE irg_inline_property
467 _get_irg_inline_property(const ir_graph *irg) {
468 return irg->inline_property;
472 _set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
473 irg->inline_property = s;
476 static INLINE unsigned
477 _get_irg_additional_properties(const ir_graph *irg) {
478 if (irg->additional_properties & mtp_property_inherited)
479 return get_method_additional_properties(get_entity_type(irg->ent));
480 return irg->additional_properties;
484 _set_irg_additional_properties(ir_graph *irg, unsigned mask) {
485 /* do not allow to set the mtp_property_inherited flag or
486 * the automatic inheritance of flags will not work */
487 irg->additional_properties = mask & ~mtp_property_inherited;
491 _set_irg_additional_property(ir_graph *irg, mtp_additional_property flag) {
492 unsigned prop = irg->additional_properties;
494 if (prop & mtp_property_inherited)
495 prop = get_method_additional_properties(get_entity_type(irg->ent));
496 irg->additional_properties = prop | flag;
500 _set_irg_link(ir_graph *irg, void *thing) {
505 _get_irg_link(const ir_graph *irg) {
509 static INLINE unsigned long
510 _get_irg_visited(const ir_graph *irg) {
514 static INLINE unsigned long
515 _get_irg_block_visited(const ir_graph *irg) {
516 return irg->block_visited;
520 _set_irg_block_visited(ir_graph *irg, unsigned long visited) {
521 irg->block_visited = visited;
525 _inc_irg_block_visited(ir_graph *irg) {
526 ++irg->block_visited;
530 _dec_irg_block_visited(ir_graph *irg) {
531 --irg->block_visited;
534 static INLINE unsigned
535 _get_irg_estimated_node_cnt(const ir_graph *irg) {
536 return irg->estimated_node_count;
540 * Allocates a new idx in the irg for the node and adds the irn to the idx -> irn map.
541 * @param irg The graph.
542 * @param irn The node.
543 * @return The index allocated for the node.
545 static INLINE unsigned
546 irg_register_node_idx(ir_graph *irg, ir_node *irn)
548 unsigned idx = irg->last_node_idx++;
549 if(idx >= (unsigned) ARR_LEN(irg->idx_irn_map))
550 ARR_RESIZE(ir_node *, irg->idx_irn_map, idx + 1);
552 irg->idx_irn_map[idx] = irn;
557 * Kill a node from the irg. BEWARE: this kills
558 * all later created nodes.
561 irg_kill_node(ir_graph *irg, ir_node *n) {
562 unsigned idx = get_irn_idx(n);
563 if (idx + 1 == irg->last_node_idx)
564 --irg->last_node_idx;
565 irg->idx_irn_map[idx] = NULL;
566 obstack_free(irg->obst, n);
570 * Get the node for an index.
571 * @param irg The graph.
572 * @param idx The index you want the node for.
573 * @return The node with that index or NULL, if there is no node with that index.
574 * @note The node you got might be dead.
576 static INLINE ir_node *
577 get_idx_irn(ir_graph *irg, unsigned idx) {
578 assert(idx < (unsigned) ARR_LEN(irg->idx_irn_map));
579 return irg->idx_irn_map[idx];
582 #define get_interprocedural_view() _get_interprocedural_view()
583 #define is_ir_graph(thing) _is_ir_graph(thing)
584 #define get_irg_start_block(irg) _get_irg_start_block(irg)
585 #define set_irg_start_block(irg, node) _set_irg_start_block(irg, node)
586 #define get_irg_start(irg) _get_irg_start(irg)
587 #define set_irg_start(irg, node) _set_irg_start(irg, node)
588 #define get_irg_end_block(irg) _get_irg_end_block(irg)
589 #define set_irg_end_block(irg, node) _set_irg_end_block(irg, node)
590 #define get_irg_end(irg) _get_irg_end(irg)
591 #define set_irg_end(irg, node) _set_irg_end(irg, node)
592 #define get_irg_end_reg(irg) _get_irg_end_reg(irg)
593 #define get_irg_end_except(irg) _get_irg_end_except(irg)
594 #define get_irg_frame(irg) _get_irg_frame(irg)
595 #define set_irg_frame(irg, node) _set_irg_frame(irg, node)
596 #define get_irg_globals(irg) _get_irg_globals(irg)
597 #define set_irg_globals(irg, node) _set_irg_globals(irg, node)
598 #define get_irg_tls(irg) _get_irg_tls(irg)
599 #define set_irg_tls(irg, node) _set_irg_tls(irg, node)
600 #define get_irg_initial_mem(irg) _get_irg_initial_mem(irg)
601 #define set_irg_initial_mem(irg, node) _set_irg_initial_mem(irg, node)
602 #define get_irg_args(irg) _get_irg_args(irg)
603 #define set_irg_args(irg, node) _set_irg_args(irg, node)
604 #define get_irg_bad(irg) _get_irg_bad(irg)
605 #define set_irg_bad(irg, node) _set_irg_bad(irg, node)
606 #define get_irg_no_mem(irg) _get_irg_no_mem(irg)
607 #define set_irg_no_mem(irg, node) _set_irg_no_mem(irg, node)
608 #define get_irg_current_block(irg) _get_irg_current_block(irg)
609 #define set_irg_current_block(irg, node) _set_irg_current_block(irg, node)
610 #define get_irg_entity(irg) _get_irg_entity(irg)
611 #define set_irg_entity(irg, ent) _set_irg_entity(irg, ent)
612 #define get_irg_frame_type(irg) _get_irg_frame_type(irg)
613 #define set_irg_frame_type(irg, ftp) _set_irg_frame_type(irg, ftp)
614 #define get_irg_obstack(irg) _get_irg_obstack(irg)
615 #define get_irg_phase_state(irg) _get_irg_phase_state(irg)
616 #define set_irg_phase_state(irg, state) _set_irg_phase_state(irg, state)
617 #define get_irg_pinned(irg) _get_irg_pinned(irg)
618 #define get_irg_outs_state(irg) _get_irg_outs_state(irg)
619 #define set_irg_outs_inconsistent(irg) _set_irg_outs_inconsistent(irg)
620 #define get_irg_extblk_state(irg) _get_irg_extblk_state(irg)
621 #define set_irg_extblk_inconsistent(irg) _set_irg_extblk_inconsistent(irg)
622 #define get_irg_dom_state(irg) _get_irg_dom_state(irg)
623 #define get_irg_postdom_state(irg) _get_irg_postdom_state(irg)
624 #define set_irg_doms_inconsistent(irg) _set_irg_doms_inconsistent(irg)
625 #define get_irg_loopinfo_state(irg) _get_irg_loopinfo_state(irg)
626 #define set_irg_loopinfo_state(irg, s) _set_irg_loopinfo_state(irg, s)
627 #define set_irg_loopinfo_inconsistent(irg) _set_irg_loopinfo_inconsistent(irg)
628 #define set_irg_pinned(irg, p) _set_irg_pinned(irg, p)
629 #define get_irg_callee_info_state(irg) _get_irg_callee_info_state(irg)
630 #define set_irg_callee_info_state(irg, s) _set_irg_callee_info_state(irg, s)
631 #define get_irg_inline_property(irg) _get_irg_inline_property(irg)
632 #define set_irg_inline_property(irg, s) _set_irg_inline_property(irg, s)
633 #define get_irg_additional_properties(irg) _get_irg_additional_properties(irg)
634 #define set_irg_additional_properties(irg, m) _set_irg_additional_properties(irg, m)
635 #define set_irg_additional_property(irg, f) _set_irg_additional_property(irg, f)
636 #define set_irg_link(irg, thing) _set_irg_link(irg, thing)
637 #define get_irg_link(irg) _get_irg_link(irg)
638 #define get_irg_visited(irg) _get_irg_visited(irg)
639 #define get_irg_block_visited(irg) _get_irg_block_visited(irg)
640 #define set_irg_block_visited(irg, v) _set_irg_block_visited(irg, v)
641 #define inc_irg_block_visited(irg) _inc_irg_block_visited(irg)
642 #define dec_irg_block_visited(irg) _dec_irg_block_visited(irg)
643 #define get_irg_estimated_node_cnt(irg) _get_irg_estimated_node_cnt(irg)
645 # endif /* _IRGRAPH_T_H_ */