3 * File name: ir/ir/irgraph.c
4 * Purpose: Entry point to the representation of procedure code.
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.
25 #include "irgraph_t.h"
36 #include "iredges_t.h"
40 * Indicates, whether additional data can be registered to graphs.
41 * If set to 1, this is not possible anymore.
43 static int forbid_new_data = 0;
46 * The amount of additional space for custom data to be allocated upon
47 * creating a new graph.
49 static size_t additional_graph_data_size = 0;
51 ir_graph *current_ir_graph;
52 ir_graph *get_current_ir_graph(void) {
53 return current_ir_graph;
55 void set_current_ir_graph(ir_graph *graph) {
56 current_ir_graph = graph;
60 int firm_interprocedural_view = 0;
62 int (get_interprocedural_view)(void) {
63 return _get_interprocedural_view();
66 void (set_interprocedural_view)(int state) {
67 firm_interprocedural_view = state;
69 /* set function vectors for faster access */
71 _get_irn_arity = _get_irn_inter_arity;
72 _get_irn_n = _get_irn_inter_n;
75 _get_irn_arity = _get_irn_intra_arity;
76 _get_irn_n = _get_irn_intra_n;
80 /** contains the suffix for frame type names */
81 static ident *frame_type_suffix = NULL;
83 /* initialize the IR graph module */
84 void firm_init_irgraph(void) {
85 frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
90 * Allocate a new IR graph.
91 * This function respects the registered graph data. The only reason for
92 * this function is, that there are two locations, where graphs are
93 * allocated (new_r_ir_graph, new_const_code_irg).
94 * @return Memory for a new graph.
96 static ir_graph *alloc_graph(void) {
97 size_t size = sizeof(ir_graph) + additional_graph_data_size;
98 char *ptr = xmalloc(size);
101 return (ir_graph *) (ptr + additional_graph_data_size);
105 * Frees an allocated IR graph
107 static void free_graph(ir_graph *irg) {
108 char *ptr = (char *)irg;
109 free(ptr - additional_graph_data_size);
112 #if USE_EXPLICIT_PHI_IN_STACK
113 /* really defined in ircons.c */
114 typedef struct Phi_in_stack Phi_in_stack;
115 Phi_in_stack *new_Phi_in_stack();
116 void free_Phi_in_stack(Phi_in_stack *s);
119 /* Allocates a list of nodes:
120 - The start block containing a start node and Proj nodes for it's four
121 results (X, M, P, Tuple).
122 - The end block containing an end node. This block is not matured after
123 new_ir_graph as predecessors need to be added to it.
124 - The current block, which is empty and also not matured.
125 Further it allocates several datastructures needed for graph construction
129 new_r_ir_graph (entity *ent, int n_loc)
132 ir_node *first_block;
136 res->kind = k_ir_graph;
138 edges_init_graph(res);
140 /* inform statistics here, as blocks will be already build on this graph */
141 hook_new_graph(res, ent);
143 current_ir_graph = res;
145 /*-- initialized for each graph. --*/
146 if (get_opt_precise_exc_context()) {
147 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
148 dereferenced in this graph plus one for
149 the store plus one for links to fragile
150 operations. n_loc is not the number of
151 parameters to the procedure! */
154 res->n_loc = n_loc + 1; /* number of local variables that are never
155 dereferenced in this graph plus one for
156 the store. This is not the number of parameters
160 /* descriptions will be allocated on demand */
161 res->loc_descriptions = NULL;
163 res->visited = 0; /* visited flag, for the ir walker */
164 res->block_visited = 0; /* visited flag, for the 'block'-walker */
166 #if USE_EXPLICIT_PHI_IN_STACK
167 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
170 res->kind = k_ir_graph;
171 res->obst = xmalloc (sizeof(*res->obst));
172 obstack_init(res->obst);
173 res->extbb_obst = NULL;
175 res->value_table = new_identities (); /* value table for global value
176 numbering for optimizing use in
180 res->inline_property = irg_inline_any;
181 res->additional_properties = mtp_property_inherited; /* inherited from type */
183 res->phase_state = phase_building;
184 res->irg_pinned_state = op_pin_state_pinned;
185 res->outs_state = outs_none;
186 res->dom_state = dom_none;
187 res->pdom_state = dom_none;
188 res->typeinfo_state = ir_typeinfo_none;
189 set_irp_typeinfo_inconsistent(); /* there is a new graph with typeinfo_none. */
190 res->callee_info_state = irg_callee_info_none;
191 res->loopinfo_state = loopinfo_none;
192 res->execfreq_state = exec_freq_none;
193 res->class_cast_state = ir_class_casts_transitive;
194 res->extblk_state = ir_extblk_info_none;
196 /*-- Type information for the procedure of the graph --*/
198 set_entity_irg(ent, res);
200 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
201 res->frame_type = new_type_frame(mangle(get_entity_ident(ent), frame_type_suffix));
203 /*-- Nodes needed in every graph --*/
204 res->end_block = new_immBlock();
205 res->end = new_End();
206 res->end_reg = res->end;
207 res->end_except = res->end;
209 res->start_block = new_immBlock();
210 res->start = new_Start();
211 res->bad = new_ir_node(NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
212 res->no_mem = new_ir_node(NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
214 /* Proj results of start node */
215 projX = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
216 res->frame = new_Proj (res->start, mode_P_data, pn_Start_P_frame_base);
217 res->globals = new_Proj (res->start, mode_P_data, pn_Start_P_globals);
218 res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
219 res->args = new_Proj (res->start, mode_T, pn_Start_T_args);
221 res->graph_nr = get_irp_new_node_nr();
223 res->proj_args = NULL;
225 set_store(res->initial_mem);
227 add_immBlock_pred(res->start_block, projX);
229 * The code generation needs it. leave it in now.
230 * Use of this edge is matter of discussion, unresolved. Also possible:
231 * add_immBlock_pred(res->start_block, res->start_block), but invalid typed.
233 mature_immBlock (res->current_block);
235 /*-- Make a block to start with --*/
236 first_block = new_immBlock();
237 add_immBlock_pred (first_block, projX);
239 res->method_execution_frequency = -1;
240 res->estimated_node_count = 0;
247 new_ir_graph (entity *ent, int n_loc)
249 ir_graph *res = new_r_ir_graph (ent, n_loc);
250 add_irp_irg(res); /* remember this graph global. */
254 /* Make a rudimentary ir graph for the constant code.
255 Must look like a correct irg, spare everything else. */
256 ir_graph *new_const_code_irg(void) {
262 /* inform statistics here, as blocks will be already build on this graph */
263 hook_new_graph(res, NULL);
265 current_ir_graph = res;
266 res->n_loc = 1; /* Only the memory. */
267 res->visited = 0; /* visited flag, for the ir walker */
268 res->block_visited=0; /* visited flag, for the 'block'-walker */
269 #if USE_EXPLICIT_PHI_IN_STACK
270 res->Phi_in_stack = NULL;
272 res->kind = k_ir_graph;
273 res->obst = xmalloc (sizeof(*res->obst));
274 obstack_init (res->obst);
275 res->extbb_obst = NULL;
277 res->phase_state = phase_building;
278 res->irg_pinned_state = op_pin_state_pinned;
279 res->extblk_state = ir_extblk_info_none;
281 res->value_table = new_identities (); /* value table for global value
282 numbering for optimizing use in
285 res->frame_type = NULL;
287 /* -- The end block -- */
288 res->end_block = new_immBlock ();
289 res->end = new_End ();
290 res->end_reg = res->end;
291 res->end_except = res->end;
292 mature_immBlock(get_cur_block()); /* mature the end block */
294 /* -- The start block -- */
295 res->start_block = new_immBlock ();
296 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
297 res->no_mem = new_ir_node (NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
298 res->start = new_Start ();
299 /* Proj results of start node */
300 res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
301 projX = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
302 add_immBlock_pred (res->start_block, projX);
303 mature_immBlock (res->start_block); /* mature the start block */
305 add_immBlock_pred (new_immBlock (), projX);
306 mature_immBlock (get_cur_block()); /* mature the 'body' block for expressions */
308 /* Set the visited flag high enough that the blocks will never be visited. */
309 set_irn_visited(get_cur_block(), -1);
310 set_Block_block_visited(get_cur_block(), -1);
311 set_Block_block_visited(res->start_block, -1);
312 set_irn_visited(res->start_block, -1);
313 set_irn_visited(res->bad, -1);
314 set_irn_visited(res->no_mem, -1);
316 res->phase_state = phase_high;
321 /* Defined in iropt.c */
322 void del_identities (pset *value_table);
324 /* Frees the passed irgraph.
325 Deallocates all nodes in this graph and the ir_graph structure.
326 Sets the field irgraph in the corresponding entity to NULL.
327 Does not remove the irgraph from the list in irprog (requires
328 inefficient search, call remove_irp_irg by hand).
329 Does not free types, entities or modes that are used only by this
330 graph, nor the entity standing for this graph. */
331 void free_ir_graph (ir_graph *irg) {
332 assert(is_ir_graph(irg));
334 hook_free_graph(irg);
335 if (irg->outs_state != outs_none) free_irg_outs(irg);
336 if (irg->frame_type) free_type(irg->frame_type);
337 if (irg->value_table) del_identities(irg->value_table);
339 peculiarity pec = get_entity_peculiarity (irg->ent);
340 set_entity_peculiarity (irg->ent, peculiarity_description);
341 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
342 set_entity_peculiarity (irg->ent, pec);
346 obstack_free(irg->obst,NULL);
348 #if USE_EXPLICIT_PHI_IN_STACK
349 free_Phi_in_stack(irg->Phi_in_stack);
351 if (irg->loc_descriptions)
352 free(irg->loc_descriptions);
357 /* access routines for all ir_graph attributes:
359 {attr type} get_irg_{attribute name} (ir_graph *irg);
360 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
363 (is_ir_graph)(const void *thing) {
364 return _is_ir_graph(thing);
367 /* Outputs a unique number for this node */
370 get_irg_graph_nr(ir_graph *irg) {
373 return irg->graph_nr;
380 (get_irg_start_block)(const ir_graph *irg) {
381 return _get_irg_start_block(irg);
385 (set_irg_start_block)(ir_graph *irg, ir_node *node) {
386 _set_irg_start_block(irg, node);
390 (get_irg_start)(const ir_graph *irg) {
391 return _get_irg_start(irg);
395 (set_irg_start)(ir_graph *irg, ir_node *node) {
396 _set_irg_start(irg, node);
400 (get_irg_end_block)(const ir_graph *irg) {
401 return _get_irg_end_block(irg);
405 (set_irg_end_block)(ir_graph *irg, ir_node *node) {
406 _set_irg_end_block(irg, node);
410 (get_irg_end)(const ir_graph *irg) {
411 return _get_irg_end(irg);
415 (set_irg_end)(ir_graph *irg, ir_node *node) {
416 _set_irg_end(irg, node);
420 (get_irg_end_reg)(const ir_graph *irg) {
421 return _get_irg_end_reg(irg);
424 void set_irg_end_reg (ir_graph *irg, ir_node *node) {
425 assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
430 (get_irg_end_except)(const ir_graph *irg) {
431 return _get_irg_end_except(irg);
434 void set_irg_end_except (ir_graph *irg, ir_node *node) {
435 assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
436 irg->end_except = node;
440 (get_irg_cstore)(const ir_graph *irg) {
441 return _get_irg_cstore(irg);
445 (set_irg_cstore)(ir_graph *irg, ir_node *node) {
446 _set_irg_cstore(irg, node);
450 (get_irg_frame)(const ir_graph *irg) {
451 return _get_irg_frame(irg);
455 (set_irg_frame)(ir_graph *irg, ir_node *node) {
456 _set_irg_frame(irg, node);
460 (get_irg_globals)(const ir_graph *irg) {
461 return _get_irg_globals(irg);
465 (set_irg_globals)(ir_graph *irg, ir_node *node) {
466 _set_irg_globals(irg, node);
470 (get_irg_initial_mem)(const ir_graph *irg) {
471 return _get_irg_initial_mem(irg);
475 (set_irg_initial_mem)(ir_graph *irg, ir_node *node) {
476 _set_irg_initial_mem(irg, node);
480 (get_irg_args)(const ir_graph *irg) {
481 return _get_irg_args(irg);
485 (set_irg_args)(ir_graph *irg, ir_node *node) {
486 _set_irg_args(irg, node);
490 (get_irg_proj_args) (const ir_graph *irg) {
491 return _get_irg_proj_args (irg);
495 (set_irg_proj_args) (ir_graph *irg, ir_node **nodes) {
496 _set_irg_proj_args (irg, nodes);
500 (get_irg_bad)(const ir_graph *irg) {
501 return _get_irg_bad(irg);
505 (set_irg_bad)(ir_graph *irg, ir_node *node) {
506 _set_irg_bad(irg, node);
510 (get_irg_no_mem)(const ir_graph *irg) {
511 return _get_irg_no_mem(irg);
515 (set_irg_no_mem)(ir_graph *irg, ir_node *node) {
516 _set_irg_no_mem(irg, node);
520 (get_irg_current_block)(const ir_graph *irg) {
521 return _get_irg_current_block(irg);
525 (set_irg_current_block)(ir_graph *irg, ir_node *node) {
526 _set_irg_current_block(irg, node);
530 (get_irg_entity)(const ir_graph *irg) {
531 return _get_irg_entity(irg);
535 (set_irg_entity)(ir_graph *irg, entity *ent) {
536 _set_irg_entity(irg, ent);
540 (get_irg_frame_type)(ir_graph *irg) {
541 return _get_irg_frame_type(irg);
545 (set_irg_frame_type)(ir_graph *irg, ir_type *ftp) {
546 _set_irg_frame_type(irg, ftp);
550 get_irg_n_locs (ir_graph *irg)
552 if (get_opt_precise_exc_context())
553 return irg->n_loc - 1 - 1;
555 return irg->n_loc - 1;
559 set_irg_n_loc (ir_graph *irg, int n_loc)
561 if (get_opt_precise_exc_context())
562 irg->n_loc = n_loc + 1 + 1;
564 irg->n_loc = n_loc + 1;
569 /* Returns the obstack associated with the graph. */
571 (get_irg_obstack)(const ir_graph *irg) {
572 return _get_irg_obstack(irg);
576 * Returns true if the node n is allocated on the storage of graph irg.
578 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
580 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
582 struct _obstack_chunk *p;
585 * checks weather the ir_node pointer is on the obstack.
586 * A more sophisticated check would test the "whole" ir_node
588 for (p = irg->obst->chunk; p; p = p->prev) {
589 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
597 (get_irg_phase_state)(const ir_graph *irg) {
598 return _get_irg_phase_state(irg);
602 (set_irg_phase_low)(ir_graph *irg) {
603 _set_irg_phase_low(irg);
607 (get_irg_pinned)(const ir_graph *irg) {
608 return _get_irg_pinned(irg);
612 (get_irg_outs_state)(const ir_graph *irg) {
613 return _get_irg_outs_state(irg);
617 (set_irg_outs_inconsistent)(ir_graph *irg) {
618 _set_irg_outs_inconsistent(irg);
622 (get_irg_dom_state)(const ir_graph *irg) {
623 return _get_irg_dom_state(irg);
627 (get_irg_postdom_state)(const ir_graph *irg) {
628 return _get_irg_postdom_state(irg);
632 (set_irg_doms_inconsistent)(ir_graph *irg) {
633 _set_irg_doms_inconsistent(irg);
637 (get_irg_loopinfo_state)(const ir_graph *irg) {
638 return _get_irg_loopinfo_state(irg);
642 (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s) {
643 _set_irg_loopinfo_state(irg, s);
647 (set_irg_loopinfo_inconsistent)(ir_graph *irg) {
648 _set_irg_loopinfo_inconsistent(irg);
651 void set_irp_loopinfo_inconsistent(void) {
652 int i, n_irgs = get_irp_n_irgs();
653 for (i = 0; i < n_irgs; ++i) {
654 set_irg_loopinfo_inconsistent(get_irp_irg(i));
661 (set_irg_pinned)(ir_graph *irg, op_pin_state p) {
662 _set_irg_pinned(irg, p);
665 irg_callee_info_state
666 (get_irg_callee_info_state)(const ir_graph *irg) {
667 return _get_irg_callee_info_state(irg);
671 (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s) {
672 _set_irg_callee_info_state(irg, s);
676 (get_irg_inline_property)(const ir_graph *irg) {
677 return _get_irg_inline_property(irg);
681 (set_irg_inline_property)(ir_graph *irg, irg_inline_property s) {
682 _set_irg_inline_property(irg, s);
686 (get_irg_additional_properties)(const ir_graph *irg) {
687 return _get_irg_additional_properties(irg);
691 (set_irg_additional_properties)(ir_graph *irg, unsigned property_mask) {
692 _set_irg_additional_properties(irg, property_mask);
696 (set_irg_additional_property)(ir_graph *irg, mtp_additional_property flag) {
697 _set_irg_additional_property(irg, flag);
701 (set_irg_link)(ir_graph *irg, void *thing) {
702 _set_irg_link(irg, thing);
706 (get_irg_link)(const ir_graph *irg) {
707 return _get_irg_link(irg);
710 /** maximum visited flag content of all ir_graph visited fields. */
711 static unsigned long max_irg_visited = 0;
714 (get_irg_visited)(const ir_graph *irg) {
715 return _get_irg_visited(irg);
719 set_irg_visited (ir_graph *irg, unsigned long visited)
721 irg->visited = visited;
722 if (irg->visited > max_irg_visited) {
723 max_irg_visited = irg->visited;
728 inc_irg_visited (ir_graph *irg)
730 if (++irg->visited > max_irg_visited) {
731 max_irg_visited = irg->visited;
736 get_max_irg_visited(void)
740 for(i = 0; i < get_irp_n_irgs(); i++)
741 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
743 return max_irg_visited;
746 void set_max_irg_visited(int val) {
747 max_irg_visited = val;
751 inc_max_irg_visited(void)
755 for(i = 0; i < get_irp_n_irgs(); i++)
756 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
759 return max_irg_visited;
763 (get_irg_block_visited)(const ir_graph *irg) {
764 return _get_irg_block_visited(irg);
768 (set_irg_block_visited)(ir_graph *irg, unsigned long visited) {
769 _set_irg_block_visited(irg, visited);
773 (inc_irg_block_visited)(ir_graph *irg) {
774 _inc_irg_block_visited(irg);
779 * walker Start->End: places Proj nodes into the same block
780 * as it's predecessors
785 static void normalize_proj_walker(ir_node *n, void *env)
788 ir_node *pred = get_Proj_pred(n);
789 ir_node *block = get_nodes_block(pred);
791 set_nodes_block(n, block);
795 /* move Proj nodes into the same block as its predecessors */
796 void normalize_proj_nodes(ir_graph *irg)
798 irg_walk_graph(irg, NULL, normalize_proj_walker, NULL);
799 set_irg_outs_inconsistent(irg);
802 /* set a description for local value n */
803 void set_irg_loc_description(ir_graph *irg, int n, void *description)
805 assert(0 <= n && n < irg->n_loc);
807 if (! irg->loc_descriptions)
808 irg->loc_descriptions = xcalloc(sizeof(*irg->loc_descriptions), irg->n_loc);
810 irg->loc_descriptions[n] = description;
813 /* get the description for local value n */
814 void *get_irg_loc_description(ir_graph *irg, int n)
816 assert(0 <= n && n < irg->n_loc);
817 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
820 /* Returns a estimated node count of the irg. */
821 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg) {
822 return _get_irg_estimated_node_cnt(irg);
825 /* register additional space in an IR graph */
826 size_t register_additional_graph_data(size_t size)
828 assert(!forbid_new_data && "Too late to register additional node data");
833 return additional_graph_data_size += size;