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, Michael Beck
9 * Copyright: (c) 1998-2007 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
26 #include "irgraph_t.h"
38 #include "iredges_t.h"
42 #define INITIAL_IDX_IRN_MAP_SIZE 1024
45 * Indicates, whether additional data can be registered to graphs.
46 * If set to 1, this is not possible anymore.
48 static int forbid_new_data = 0;
51 * The amount of additional space for custom data to be allocated upon
52 * creating a new graph.
54 static size_t additional_graph_data_size = 0;
56 ir_graph *current_ir_graph;
57 ir_graph *get_current_ir_graph(void) {
58 return current_ir_graph;
60 void set_current_ir_graph(ir_graph *graph) {
61 current_ir_graph = graph;
65 int firm_interprocedural_view = 0;
67 int (get_interprocedural_view)(void) {
68 return _get_interprocedural_view();
71 void (set_interprocedural_view)(int state) {
72 firm_interprocedural_view = state;
74 /* set function vectors for faster access */
76 _get_irn_arity = _get_irn_inter_arity;
77 _get_irn_n = _get_irn_inter_n;
80 _get_irn_arity = _get_irn_intra_arity;
81 _get_irn_n = _get_irn_intra_n;
85 /** contains the suffix for frame type names */
86 static ident *frame_type_suffix = NULL;
88 /* initialize the IR graph module */
89 void firm_init_irgraph(void) {
90 frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
95 * Allocate a new IR graph.
96 * This function respects the registered graph data. The only reason for
97 * this function is, that there are two locations, where graphs are
98 * allocated (new_r_ir_graph, new_const_code_irg).
99 * @return Memory for a new graph.
101 static ir_graph *alloc_graph(void) {
102 size_t size = sizeof(ir_graph) + additional_graph_data_size;
103 char *ptr = xmalloc(size);
104 memset(ptr, 0, size);
106 return (ir_graph *) (ptr + additional_graph_data_size);
110 * Frees an allocated IR graph
112 static void free_graph(ir_graph *irg) {
113 char *ptr = (char *)irg;
114 free(ptr - additional_graph_data_size);
117 #if USE_EXPLICIT_PHI_IN_STACK
118 /* really defined in ircons.c */
119 typedef struct Phi_in_stack Phi_in_stack;
120 Phi_in_stack *new_Phi_in_stack();
121 void free_Phi_in_stack(Phi_in_stack *s);
124 /* Allocates a list of nodes:
125 - The start block containing a start node and Proj nodes for it's four
126 results (X, M, P, Tuple).
127 - The end block containing an end node. This block is not matured after
128 new_ir_graph as predecessors need to be added to it.
129 - The current block, which is empty and also not matured.
130 Further it allocates several datastructures needed for graph construction
134 new_r_ir_graph (ir_entity *ent, int n_loc)
137 ir_node *first_block;
138 ir_node *end, *start, *start_block, *initial_mem, *projX;
141 res->kind = k_ir_graph;
143 //edges_init_graph_kind(res, EDGE_KIND_NORMAL);
144 //edges_init_graph_kind(res, EDGE_KIND_BLOCK);
146 /* initialize the idx->node map. */
147 res->idx_irn_map = NEW_ARR_F(ir_node *, INITIAL_IDX_IRN_MAP_SIZE);
148 memset(res->idx_irn_map, 0, INITIAL_IDX_IRN_MAP_SIZE * sizeof(res->idx_irn_map[0]));
150 /* inform statistics here, as blocks will be already build on this graph */
151 hook_new_graph(res, ent);
153 current_ir_graph = res;
155 /*-- initialized for each graph. --*/
156 if (get_opt_precise_exc_context()) {
157 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
158 dereferenced in this graph plus one for
159 the store plus one for links to fragile
160 operations. n_loc is not the number of
161 parameters to the procedure! */
164 res->n_loc = n_loc + 1; /* number of local variables that are never
165 dereferenced in this graph plus one for
166 the store. This is not the number of parameters
170 /* descriptions will be allocated on demand */
171 res->loc_descriptions = NULL;
173 res->visited = 0; /* visited flag, for the ir walker */
174 res->block_visited = 0; /* visited flag, for the 'block'-walker */
176 #if USE_EXPLICIT_PHI_IN_STACK
177 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
180 res->kind = k_ir_graph;
181 res->obst = xmalloc (sizeof(*res->obst));
182 obstack_init(res->obst);
183 res->extbb_obst = NULL;
185 res->last_node_idx = 0;
187 res->value_table = new_identities (); /* value table for global value
188 numbering for optimizing use in
192 res->inline_property = irg_inline_any;
193 res->additional_properties = mtp_property_inherited; /* inherited from type */
195 res->phase_state = phase_building;
196 res->irg_pinned_state = op_pin_state_pinned;
197 res->outs_state = outs_none;
198 res->dom_state = dom_none;
199 res->pdom_state = dom_none;
200 res->typeinfo_state = ir_typeinfo_none;
201 set_irp_typeinfo_inconsistent(); /* there is a new graph with typeinfo_none. */
202 res->callee_info_state = irg_callee_info_none;
203 res->loopinfo_state = loopinfo_none;
204 res->class_cast_state = ir_class_casts_transitive;
205 res->extblk_state = ir_extblk_info_none;
206 res->execfreq_state = exec_freq_none;
207 res->fp_model = fp_model_precise;
208 res->adr_taken_state = ir_address_taken_not_computed;
210 /*-- Type information for the procedure of the graph --*/
212 set_entity_irg(ent, res);
214 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
215 res->frame_type = new_type_frame(mangle(get_entity_ident(ent), frame_type_suffix));
217 /*-- Nodes needed in every graph --*/
218 set_irg_end_block (res, new_immBlock());
220 set_irg_end (res, end);
221 set_irg_end_reg (res, end);
222 set_irg_end_except(res, end);
224 start_block = new_immBlock();
225 set_irg_start_block(res, start_block);
226 set_irg_bad (res, new_ir_node(NULL, res, start_block, op_Bad, mode_T, 0, NULL));
227 set_irg_no_mem (res, new_ir_node(NULL, res, start_block, op_NoMem, mode_M, 0, NULL));
229 set_irg_start (res, start);
231 /* Proj results of start node */
232 projX = new_Proj(start, mode_X, pn_Start_X_initial_exec);
233 set_irg_frame (res, new_Proj(start, mode_P_data, pn_Start_P_frame_base));
234 set_irg_globals (res, new_Proj(start, mode_P_data, pn_Start_P_globals));
235 set_irg_tls (res, new_Proj(start, mode_P_data, pn_Start_P_tls));
236 set_irg_args (res, new_Proj(start, mode_T, pn_Start_T_args));
237 set_irg_value_param_base(res, new_Proj(start, mode_P_data, pn_Start_P_value_arg_base));
238 initial_mem = new_Proj(start, mode_M, pn_Start_M);
239 set_irg_initial_mem(res, initial_mem);
241 add_immBlock_pred(start_block, projX);
242 set_store(initial_mem);
245 res->graph_nr = get_irp_new_node_nr();
247 res->proj_args = NULL;
250 * The code generation needs it. leave it in now.
251 * Use of this edge is matter of discussion, unresolved. Also possible:
252 * add_immBlock_pred(res->start_block, res->start_block), but invalid typed.
254 mature_immBlock(res->current_block);
256 /*-- Make a block to start with --*/
257 first_block = new_immBlock();
258 add_immBlock_pred(first_block, projX);
260 res->method_execution_frequency = -1;
261 res->estimated_node_count = 0;
268 new_ir_graph(ir_entity *ent, int n_loc)
270 ir_graph *res = new_r_ir_graph(ent, n_loc);
271 add_irp_irg(res); /* remember this graph global. */
275 /* Make a rudimentary IR graph for the constant code.
276 Must look like a correct irg, spare everything else. */
277 ir_graph *new_const_code_irg(void) {
279 ir_node *end, *start_block, *start, *projX;
283 /* initialize the idx->node map. */
284 res->idx_irn_map = NEW_ARR_F(ir_node *, INITIAL_IDX_IRN_MAP_SIZE);
285 memset(res->idx_irn_map, 0, INITIAL_IDX_IRN_MAP_SIZE * sizeof(res->idx_irn_map[0]));
287 /* inform statistics here, as blocks will be already build on this graph */
288 hook_new_graph(res, NULL);
290 current_ir_graph = res;
291 res->n_loc = 1; /* Only the memory. */
292 res->visited = 0; /* visited flag, for the ir walker */
293 res->block_visited = 0; /* visited flag, for the 'block'-walker */
294 #if USE_EXPLICIT_PHI_IN_STACK
295 res->Phi_in_stack = NULL;
297 res->kind = k_ir_graph;
298 res->obst = xmalloc (sizeof(*res->obst));
299 obstack_init (res->obst);
300 res->extbb_obst = NULL;
302 res->last_node_idx = 0;
304 res->phase_state = phase_building;
305 res->irg_pinned_state = op_pin_state_pinned;
306 res->extblk_state = ir_extblk_info_none;
307 res->fp_model = fp_model_precise;
309 res->value_table = new_identities (); /* value table for global value
310 numbering for optimizing use in
313 res->frame_type = NULL;
315 /* -- The end block -- */
316 set_irg_end_block (res, new_immBlock());
318 set_irg_end (res, end);
319 set_irg_end_reg (res, end);
320 set_irg_end_except(res, end);
321 mature_immBlock(get_cur_block()); /* mature the end block */
323 /* -- The start block -- */
324 start_block = new_immBlock();
325 set_irg_start_block(res, start_block);
326 set_irg_bad (res, new_ir_node (NULL, res, start_block, op_Bad, mode_T, 0, NULL));
327 set_irg_no_mem (res, new_ir_node (NULL, res, start_block, op_NoMem, mode_M, 0, NULL));
329 set_irg_start (res, start);
331 /* Proj results of start node */
332 set_irg_initial_mem(res, new_Proj(start, mode_M, pn_Start_M));
333 projX = new_Proj(start, mode_X, pn_Start_X_initial_exec);
334 add_immBlock_pred(start_block, projX);
335 mature_immBlock (start_block); /* mature the start block */
337 add_immBlock_pred(new_immBlock(), projX);
338 mature_immBlock (get_cur_block()); /* mature the 'body' block for expressions */
340 /* Set the visited flag high enough that the blocks will never be visited. */
341 set_irn_visited(get_cur_block(), -1);
342 set_Block_block_visited(get_cur_block(), -1);
343 set_Block_block_visited(start_block, -1);
344 set_irn_visited(start_block, -1);
345 set_irn_visited(get_irg_bad(res), -1);
346 set_irn_visited(get_irg_no_mem(res), -1);
348 res->phase_state = phase_high;
353 /* Defined in iropt.c */
354 void del_identities (pset *value_table);
356 /* Frees the passed irgraph.
357 Deallocates all nodes in this graph and the ir_graph structure.
358 Sets the field irgraph in the corresponding entity to NULL.
359 Does not remove the irgraph from the list in irprog (requires
360 inefficient search, call remove_irp_irg by hand).
361 Does not free types, entities or modes that are used only by this
362 graph, nor the entity standing for this graph. */
363 void free_ir_graph (ir_graph *irg) {
364 assert(is_ir_graph(irg));
366 hook_free_graph(irg);
367 if (irg->outs_state != outs_none) free_irg_outs(irg);
368 if (irg->frame_type) free_type(irg->frame_type);
369 if (irg->value_table) del_identities(irg->value_table);
371 ir_peculiarity pec = get_entity_peculiarity (irg->ent);
372 set_entity_peculiarity (irg->ent, peculiarity_description);
373 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
374 set_entity_peculiarity (irg->ent, pec);
377 free_End(get_irg_end(irg));
378 obstack_free(irg->obst,NULL);
380 #if USE_EXPLICIT_PHI_IN_STACK
381 free_Phi_in_stack(irg->Phi_in_stack);
383 if (irg->loc_descriptions)
384 free(irg->loc_descriptions);
389 /* access routines for all ir_graph attributes:
391 {attr type} get_irg_{attribute name} (ir_graph *irg);
392 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
395 (is_ir_graph)(const void *thing) {
396 return _is_ir_graph(thing);
399 /* Outputs a unique number for this node */
400 long get_irg_graph_nr(ir_graph *irg) {
403 return irg->graph_nr;
405 return (long)PTR_TO_INT(irg);
410 (get_irg_start_block)(const ir_graph *irg) {
411 return _get_irg_start_block(irg);
415 (set_irg_start_block)(ir_graph *irg, ir_node *node) {
416 _set_irg_start_block(irg, node);
420 (get_irg_start)(const ir_graph *irg) {
421 return _get_irg_start(irg);
425 (set_irg_start)(ir_graph *irg, ir_node *node) {
426 _set_irg_start(irg, node);
430 (get_irg_end_block)(const ir_graph *irg) {
431 return _get_irg_end_block(irg);
435 (set_irg_end_block)(ir_graph *irg, ir_node *node) {
436 _set_irg_end_block(irg, node);
440 (get_irg_end)(const ir_graph *irg) {
441 return _get_irg_end(irg);
445 (set_irg_end)(ir_graph *irg, ir_node *node) {
446 _set_irg_end(irg, node);
450 (get_irg_end_reg)(const ir_graph *irg) {
451 return _get_irg_end_reg(irg);
454 void set_irg_end_reg (ir_graph *irg, ir_node *node) {
455 assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
456 irg->anchors[anchor_end_reg] = node;
460 (get_irg_end_except)(const ir_graph *irg) {
461 return _get_irg_end_except(irg);
464 void set_irg_end_except (ir_graph *irg, ir_node *node) {
465 assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
466 irg->anchors[anchor_end_except] = node;
470 (get_irg_frame)(const ir_graph *irg) {
471 return _get_irg_frame(irg);
475 (set_irg_frame)(ir_graph *irg, ir_node *node) {
476 _set_irg_frame(irg, node);
480 (get_irg_globals)(const ir_graph *irg) {
481 return _get_irg_globals(irg);
485 (set_irg_globals)(ir_graph *irg, ir_node *node) {
486 _set_irg_globals(irg, node);
490 (get_irg_tls)(const ir_graph *irg) {
491 return _get_irg_tls(irg);
495 (set_irg_tls)(ir_graph *irg, ir_node *node) {
496 _set_irg_tls(irg, node);
500 (get_irg_initial_mem)(const ir_graph *irg) {
501 return _get_irg_initial_mem(irg);
505 (set_irg_initial_mem)(ir_graph *irg, ir_node *node) {
506 _set_irg_initial_mem(irg, node);
510 (get_irg_args)(const ir_graph *irg) {
511 return _get_irg_args(irg);
515 (set_irg_args)(ir_graph *irg, ir_node *node) {
516 _set_irg_args(irg, node);
520 (get_irg_value_param_base)(const ir_graph *irg) {
521 return _get_irg_value_param_base(irg);
525 (set_irg_value_param_base)(ir_graph *irg, ir_node *node) {
526 _set_irg_value_param_base(irg, node);
530 (get_irg_proj_args) (const ir_graph *irg) {
531 return _get_irg_proj_args (irg);
535 (set_irg_proj_args) (ir_graph *irg, ir_node **nodes) {
536 _set_irg_proj_args (irg, nodes);
540 (get_irg_bad)(const ir_graph *irg) {
541 return _get_irg_bad(irg);
545 (set_irg_bad)(ir_graph *irg, ir_node *node) {
546 _set_irg_bad(irg, node);
550 (get_irg_no_mem)(const ir_graph *irg) {
551 return _get_irg_no_mem(irg);
555 (set_irg_no_mem)(ir_graph *irg, ir_node *node) {
556 _set_irg_no_mem(irg, node);
560 (get_irg_current_block)(const ir_graph *irg) {
561 return _get_irg_current_block(irg);
565 (set_irg_current_block)(ir_graph *irg, ir_node *node) {
566 _set_irg_current_block(irg, node);
570 (get_irg_entity)(const ir_graph *irg) {
571 return _get_irg_entity(irg);
575 (set_irg_entity)(ir_graph *irg, ir_entity *ent) {
576 _set_irg_entity(irg, ent);
580 (get_irg_frame_type)(ir_graph *irg) {
581 return _get_irg_frame_type(irg);
585 (set_irg_frame_type)(ir_graph *irg, ir_type *ftp) {
586 _set_irg_frame_type(irg, ftp);
590 get_irg_n_locs (ir_graph *irg)
592 if (get_opt_precise_exc_context())
593 return irg->n_loc - 1 - 1;
595 return irg->n_loc - 1;
599 set_irg_n_loc (ir_graph *irg, int n_loc)
601 if (get_opt_precise_exc_context())
602 irg->n_loc = n_loc + 1 + 1;
604 irg->n_loc = n_loc + 1;
609 /* Returns the obstack associated with the graph. */
611 (get_irg_obstack)(const ir_graph *irg) {
612 return _get_irg_obstack(irg);
616 * Returns true if the node n is allocated on the storage of graph irg.
618 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
620 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
622 struct _obstack_chunk *p;
625 * checks weather the ir_node pointer is on the obstack.
626 * A more sophisticated check would test the "whole" ir_node
628 for (p = irg->obst->chunk; p; p = p->prev) {
629 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
637 (get_irg_phase_state)(const ir_graph *irg) {
638 return _get_irg_phase_state(irg);
642 (set_irg_phase_state)(ir_graph *irg, irg_phase_state state) {
643 _set_irg_phase_state(irg, state);
647 (get_irg_pinned)(const ir_graph *irg) {
648 return _get_irg_pinned(irg);
652 (get_irg_outs_state)(const ir_graph *irg) {
653 return _get_irg_outs_state(irg);
657 (set_irg_outs_inconsistent)(ir_graph *irg) {
658 _set_irg_outs_inconsistent(irg);
662 (get_irg_extblk_state)(const ir_graph *irg) {
663 return _get_irg_extblk_state(irg);
667 (set_irg_extblk_inconsistent)(ir_graph *irg) {
668 _set_irg_extblk_inconsistent(irg);
672 (get_irg_dom_state)(const ir_graph *irg) {
673 return _get_irg_dom_state(irg);
677 (get_irg_postdom_state)(const ir_graph *irg) {
678 return _get_irg_postdom_state(irg);
682 (set_irg_doms_inconsistent)(ir_graph *irg) {
683 _set_irg_doms_inconsistent(irg);
687 (get_irg_loopinfo_state)(const ir_graph *irg) {
688 return _get_irg_loopinfo_state(irg);
692 (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s) {
693 _set_irg_loopinfo_state(irg, s);
697 (set_irg_loopinfo_inconsistent)(ir_graph *irg) {
698 _set_irg_loopinfo_inconsistent(irg);
701 void set_irp_loopinfo_inconsistent(void) {
702 int i, n_irgs = get_irp_n_irgs();
703 for (i = 0; i < n_irgs; ++i) {
704 set_irg_loopinfo_inconsistent(get_irp_irg(i));
711 (set_irg_pinned)(ir_graph *irg, op_pin_state p) {
712 _set_irg_pinned(irg, p);
715 irg_callee_info_state
716 (get_irg_callee_info_state)(const ir_graph *irg) {
717 return _get_irg_callee_info_state(irg);
721 (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s) {
722 _set_irg_callee_info_state(irg, s);
726 (get_irg_inline_property)(const ir_graph *irg) {
727 return _get_irg_inline_property(irg);
731 (set_irg_inline_property)(ir_graph *irg, irg_inline_property s) {
732 _set_irg_inline_property(irg, s);
736 (get_irg_additional_properties)(const ir_graph *irg) {
737 return _get_irg_additional_properties(irg);
741 (set_irg_additional_properties)(ir_graph *irg, unsigned property_mask) {
742 _set_irg_additional_properties(irg, property_mask);
746 (set_irg_additional_property)(ir_graph *irg, mtp_additional_property flag) {
747 _set_irg_additional_property(irg, flag);
751 (set_irg_link)(ir_graph *irg, void *thing) {
752 _set_irg_link(irg, thing);
756 (get_irg_link)(const ir_graph *irg) {
757 return _get_irg_link(irg);
760 /** maximum visited flag content of all ir_graph visited fields. */
761 static unsigned long max_irg_visited = 0;
764 (get_irg_visited)(const ir_graph *irg) {
765 return _get_irg_visited(irg);
769 set_irg_visited (ir_graph *irg, unsigned long visited)
771 irg->visited = visited;
772 if (irg->visited > max_irg_visited) {
773 max_irg_visited = irg->visited;
778 inc_irg_visited (ir_graph *irg)
780 if (++irg->visited > max_irg_visited) {
781 max_irg_visited = irg->visited;
786 get_max_irg_visited(void)
790 for(i = 0; i < get_irp_n_irgs(); i++)
791 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
793 return max_irg_visited;
796 void set_max_irg_visited(int val) {
797 max_irg_visited = val;
801 inc_max_irg_visited(void)
805 for(i = 0; i < get_irp_n_irgs(); i++)
806 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
809 return max_irg_visited;
813 (get_irg_block_visited)(const ir_graph *irg) {
814 return _get_irg_block_visited(irg);
818 (set_irg_block_visited)(ir_graph *irg, unsigned long visited) {
819 _set_irg_block_visited(irg, visited);
823 (inc_irg_block_visited)(ir_graph *irg) {
824 _inc_irg_block_visited(irg);
827 /* Return the floating point model of this graph. */
828 unsigned (get_irg_fp_model)(const ir_graph *irg) {
829 return _get_irg_fp_model(irg);
832 /* Sets the floating point model for this graph. */
833 void set_irg_fp_model(ir_graph *irg, unsigned model) {
834 irg->fp_model = model;
838 * walker Start->End: places Proj nodes into the same block
839 * as it's predecessors
844 static void normalize_proj_walker(ir_node *n, void *env){
846 ir_node *pred = get_Proj_pred(n);
847 ir_node *block = get_nodes_block(pred);
849 set_nodes_block(n, block);
853 /* move Proj nodes into the same block as its predecessors */
854 void normalize_proj_nodes(ir_graph *irg) {
855 irg_walk_graph(irg, NULL, normalize_proj_walker, NULL);
856 set_irg_outs_inconsistent(irg);
859 /* set a description for local value n */
860 void set_irg_loc_description(ir_graph *irg, int n, void *description) {
861 assert(0 <= n && n < irg->n_loc);
863 if (! irg->loc_descriptions)
864 irg->loc_descriptions = xcalloc(sizeof(*irg->loc_descriptions), irg->n_loc);
866 irg->loc_descriptions[n] = description;
869 /* get the description for local value n */
870 void *get_irg_loc_description(ir_graph *irg, int n) {
871 assert(0 <= n && n < irg->n_loc);
872 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
875 /* Returns a estimated node count of the irg. */
876 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg) {
877 return _get_irg_estimated_node_cnt(irg);
880 /* Returns the last irn index for this graph. */
881 unsigned get_irg_last_idx(const ir_graph *irg) {
882 return irg->last_node_idx;
885 /* register additional space in an IR graph */
886 size_t register_additional_graph_data(size_t size)
888 assert(!forbid_new_data && "Too late to register additional node data");
893 return additional_graph_data_size += size;