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.
26 #include "irgraph_t.h"
38 #include "iredges_t.h"
41 #define INITIAL_IDX_IRN_MAP_SIZE 1024
44 * Indicates, whether additional data can be registered to graphs.
45 * If set to 1, this is not possible anymore.
47 static int forbid_new_data = 0;
50 * The amount of additional space for custom data to be allocated upon
51 * creating a new graph.
53 static size_t additional_graph_data_size = 0;
55 ir_graph *current_ir_graph;
56 ir_graph *get_current_ir_graph(void) {
57 return current_ir_graph;
59 void set_current_ir_graph(ir_graph *graph) {
60 current_ir_graph = graph;
64 int firm_interprocedural_view = 0;
66 int (get_interprocedural_view)(void) {
67 return _get_interprocedural_view();
70 void (set_interprocedural_view)(int state) {
71 firm_interprocedural_view = state;
73 /* set function vectors for faster access */
75 _get_irn_arity = _get_irn_inter_arity;
76 _get_irn_n = _get_irn_inter_n;
79 _get_irn_arity = _get_irn_intra_arity;
80 _get_irn_n = _get_irn_intra_n;
84 /** contains the suffix for frame type names */
85 static ident *frame_type_suffix = NULL;
87 /* initialize the IR graph module */
88 void firm_init_irgraph(void) {
89 frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
94 * Allocate a new IR graph.
95 * This function respects the registered graph data. The only reason for
96 * this function is, that there are two locations, where graphs are
97 * allocated (new_r_ir_graph, new_const_code_irg).
98 * @return Memory for a new graph.
100 static ir_graph *alloc_graph(void) {
101 size_t size = sizeof(ir_graph) + additional_graph_data_size;
102 char *ptr = xmalloc(size);
103 memset(ptr, 0, size);
105 return (ir_graph *) (ptr + additional_graph_data_size);
109 * Frees an allocated IR graph
111 static void free_graph(ir_graph *irg) {
112 char *ptr = (char *)irg;
113 free(ptr - additional_graph_data_size);
116 #if USE_EXPLICIT_PHI_IN_STACK
117 /* really defined in ircons.c */
118 typedef struct Phi_in_stack Phi_in_stack;
119 Phi_in_stack *new_Phi_in_stack();
120 void free_Phi_in_stack(Phi_in_stack *s);
123 /* Allocates a list of nodes:
124 - The start block containing a start node and Proj nodes for it's four
125 results (X, M, P, Tuple).
126 - The end block containing an end node. This block is not matured after
127 new_ir_graph as predecessors need to be added to it.
128 - The current block, which is empty and also not matured.
129 Further it allocates several datastructures needed for graph construction
133 new_r_ir_graph (entity *ent, int n_loc)
136 ir_node *first_block;
137 ir_node *end, *start, *start_block, *initial_mem, *projX;
140 res->kind = k_ir_graph;
142 edges_init_graph(res);
144 /* initialize the idx->node map. */
145 res->idx_irn_map = NEW_ARR_F(ir_node *, INITIAL_IDX_IRN_MAP_SIZE);
146 memset(res->idx_irn_map, 0, INITIAL_IDX_IRN_MAP_SIZE * sizeof(res->idx_irn_map[0]));
148 /* inform statistics here, as blocks will be already build on this graph */
149 hook_new_graph(res, ent);
151 current_ir_graph = res;
153 /*-- initialized for each graph. --*/
154 if (get_opt_precise_exc_context()) {
155 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
156 dereferenced in this graph plus one for
157 the store plus one for links to fragile
158 operations. n_loc is not the number of
159 parameters to the procedure! */
162 res->n_loc = n_loc + 1; /* number of local variables that are never
163 dereferenced in this graph plus one for
164 the store. This is not the number of parameters
168 /* descriptions will be allocated on demand */
169 res->loc_descriptions = NULL;
171 res->visited = 0; /* visited flag, for the ir walker */
172 res->block_visited = 0; /* visited flag, for the 'block'-walker */
174 #if USE_EXPLICIT_PHI_IN_STACK
175 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
178 res->kind = k_ir_graph;
179 res->obst = xmalloc (sizeof(*res->obst));
180 obstack_init(res->obst);
181 res->extbb_obst = NULL;
183 res->last_node_idx = 0;
185 res->value_table = new_identities (); /* value table for global value
186 numbering for optimizing use in
190 res->inline_property = irg_inline_any;
191 res->additional_properties = mtp_property_inherited; /* inherited from type */
193 res->phase_state = phase_building;
194 res->irg_pinned_state = op_pin_state_pinned;
195 res->outs_state = outs_none;
196 res->dom_state = dom_none;
197 res->pdom_state = dom_none;
198 res->typeinfo_state = ir_typeinfo_none;
199 set_irp_typeinfo_inconsistent(); /* there is a new graph with typeinfo_none. */
200 res->callee_info_state = irg_callee_info_none;
201 res->loopinfo_state = loopinfo_none;
202 res->execfreq_state = exec_freq_none;
203 res->class_cast_state = ir_class_casts_transitive;
204 res->extblk_state = ir_extblk_info_none;
205 res->fp_model = fp_model_precise;
207 /*-- Type information for the procedure of the graph --*/
209 set_entity_irg(ent, res);
211 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
212 res->frame_type = new_type_frame(mangle(get_entity_ident(ent), frame_type_suffix));
214 /*-- Nodes needed in every graph --*/
215 set_irg_end_block (res, new_immBlock());
217 set_irg_end (res, end);
218 set_irg_end_reg (res, end);
219 set_irg_end_except(res, end);
221 start_block = new_immBlock();
222 set_irg_start_block(res, start_block);
223 set_irg_bad (res, new_ir_node(NULL, res, start_block, op_Bad, mode_T, 0, NULL));
224 set_irg_no_mem (res, new_ir_node(NULL, res, start_block, op_NoMem, mode_M, 0, NULL));
226 set_irg_start (res, start);
228 /* Proj results of start node */
229 projX = new_Proj(start, mode_X, pn_Start_X_initial_exec);
230 set_irg_frame (res, new_Proj(start, mode_P_data, pn_Start_P_frame_base));
231 set_irg_globals (res, new_Proj(start, mode_P_data, pn_Start_P_globals));
232 set_irg_tls (res, new_Proj(start, mode_P_data, pn_Start_P_tls));
233 set_irg_args (res, new_Proj(start, mode_T, pn_Start_T_args));
234 set_irg_value_param_base(res, new_Proj(start, mode_P_data, pn_Start_P_value_arg_base));
235 initial_mem = new_Proj(start, mode_M, pn_Start_M);
236 set_irg_initial_mem(res, initial_mem);
238 add_immBlock_pred(start_block, projX);
239 set_store(initial_mem);
242 res->graph_nr = get_irp_new_node_nr();
244 res->proj_args = NULL;
247 * The code generation needs it. leave it in now.
248 * Use of this edge is matter of discussion, unresolved. Also possible:
249 * add_immBlock_pred(res->start_block, res->start_block), but invalid typed.
251 mature_immBlock(res->current_block);
253 /*-- Make a block to start with --*/
254 first_block = new_immBlock();
255 add_immBlock_pred(first_block, projX);
257 res->method_execution_frequency = -1;
258 res->estimated_node_count = 0;
265 new_ir_graph(entity *ent, int n_loc)
267 ir_graph *res = new_r_ir_graph(ent, n_loc);
268 add_irp_irg(res); /* remember this graph global. */
272 /* Make a rudimentary IR graph for the constant code.
273 Must look like a correct irg, spare everything else. */
274 ir_graph *new_const_code_irg(void) {
276 ir_node *end, *start_block, *start, *projX;
280 /* initialize the idx->node map. */
281 res->idx_irn_map = NEW_ARR_F(ir_node *, INITIAL_IDX_IRN_MAP_SIZE);
282 memset(res->idx_irn_map, 0, INITIAL_IDX_IRN_MAP_SIZE * sizeof(res->idx_irn_map[0]));
284 /* inform statistics here, as blocks will be already build on this graph */
285 hook_new_graph(res, NULL);
287 current_ir_graph = res;
288 res->n_loc = 1; /* Only the memory. */
289 res->visited = 0; /* visited flag, for the ir walker */
290 res->block_visited = 0; /* visited flag, for the 'block'-walker */
291 #if USE_EXPLICIT_PHI_IN_STACK
292 res->Phi_in_stack = NULL;
294 res->kind = k_ir_graph;
295 res->obst = xmalloc (sizeof(*res->obst));
296 obstack_init (res->obst);
297 res->extbb_obst = NULL;
299 res->last_node_idx = 0;
301 res->phase_state = phase_building;
302 res->irg_pinned_state = op_pin_state_pinned;
303 res->extblk_state = ir_extblk_info_none;
304 res->fp_model = fp_model_precise;
306 res->value_table = new_identities (); /* value table for global value
307 numbering for optimizing use in
310 res->frame_type = NULL;
312 /* -- The end block -- */
313 set_irg_end_block (res, new_immBlock());
315 set_irg_end (res, end);
316 set_irg_end_reg (res, end);
317 set_irg_end_except(res, end);
318 mature_immBlock(get_cur_block()); /* mature the end block */
320 /* -- The start block -- */
321 start_block = new_immBlock();
322 set_irg_start_block(res, start_block);
323 set_irg_bad (res, new_ir_node (NULL, res, start_block, op_Bad, mode_T, 0, NULL));
324 set_irg_no_mem (res, new_ir_node (NULL, res, start_block, op_NoMem, mode_M, 0, NULL));
326 set_irg_start (res, start);
328 /* Proj results of start node */
329 set_irg_initial_mem(res, new_Proj(start, mode_M, pn_Start_M));
330 projX = new_Proj(start, mode_X, pn_Start_X_initial_exec);
331 add_immBlock_pred(start_block, projX);
332 mature_immBlock (start_block); /* mature the start block */
334 add_immBlock_pred(new_immBlock(), projX);
335 mature_immBlock (get_cur_block()); /* mature the 'body' block for expressions */
337 /* Set the visited flag high enough that the blocks will never be visited. */
338 set_irn_visited(get_cur_block(), -1);
339 set_Block_block_visited(get_cur_block(), -1);
340 set_Block_block_visited(start_block, -1);
341 set_irn_visited(start_block, -1);
342 set_irn_visited(get_irg_bad(res), -1);
343 set_irn_visited(get_irg_no_mem(res), -1);
345 res->phase_state = phase_high;
350 /* Defined in iropt.c */
351 void del_identities (pset *value_table);
353 /* Frees the passed irgraph.
354 Deallocates all nodes in this graph and the ir_graph structure.
355 Sets the field irgraph in the corresponding entity to NULL.
356 Does not remove the irgraph from the list in irprog (requires
357 inefficient search, call remove_irp_irg by hand).
358 Does not free types, entities or modes that are used only by this
359 graph, nor the entity standing for this graph. */
360 void free_ir_graph (ir_graph *irg) {
361 assert(is_ir_graph(irg));
363 hook_free_graph(irg);
364 if (irg->outs_state != outs_none) free_irg_outs(irg);
365 if (irg->frame_type) free_type(irg->frame_type);
366 if (irg->value_table) del_identities(irg->value_table);
368 peculiarity pec = get_entity_peculiarity (irg->ent);
369 set_entity_peculiarity (irg->ent, peculiarity_description);
370 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
371 set_entity_peculiarity (irg->ent, pec);
374 free_End(get_irg_end(irg));
375 obstack_free(irg->obst,NULL);
377 #if USE_EXPLICIT_PHI_IN_STACK
378 free_Phi_in_stack(irg->Phi_in_stack);
380 if (irg->loc_descriptions)
381 free(irg->loc_descriptions);
386 /* access routines for all ir_graph attributes:
388 {attr type} get_irg_{attribute name} (ir_graph *irg);
389 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
392 (is_ir_graph)(const void *thing) {
393 return _is_ir_graph(thing);
396 /* Outputs a unique number for this node */
397 long get_irg_graph_nr(ir_graph *irg) {
400 return irg->graph_nr;
402 return (long)PTR_TO_INT(irg);
407 (get_irg_start_block)(const ir_graph *irg) {
408 return _get_irg_start_block(irg);
412 (set_irg_start_block)(ir_graph *irg, ir_node *node) {
413 _set_irg_start_block(irg, node);
417 (get_irg_start)(const ir_graph *irg) {
418 return _get_irg_start(irg);
422 (set_irg_start)(ir_graph *irg, ir_node *node) {
423 _set_irg_start(irg, node);
427 (get_irg_end_block)(const ir_graph *irg) {
428 return _get_irg_end_block(irg);
432 (set_irg_end_block)(ir_graph *irg, ir_node *node) {
433 _set_irg_end_block(irg, node);
437 (get_irg_end)(const ir_graph *irg) {
438 return _get_irg_end(irg);
442 (set_irg_end)(ir_graph *irg, ir_node *node) {
443 _set_irg_end(irg, node);
447 (get_irg_end_reg)(const ir_graph *irg) {
448 return _get_irg_end_reg(irg);
451 void set_irg_end_reg (ir_graph *irg, ir_node *node) {
452 assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
453 irg->anchors[anchor_end_reg] = node;
457 (get_irg_end_except)(const ir_graph *irg) {
458 return _get_irg_end_except(irg);
461 void set_irg_end_except (ir_graph *irg, ir_node *node) {
462 assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
463 irg->anchors[anchor_end_except] = node;
467 (get_irg_frame)(const ir_graph *irg) {
468 return _get_irg_frame(irg);
472 (set_irg_frame)(ir_graph *irg, ir_node *node) {
473 _set_irg_frame(irg, node);
477 (get_irg_globals)(const ir_graph *irg) {
478 return _get_irg_globals(irg);
482 (set_irg_globals)(ir_graph *irg, ir_node *node) {
483 _set_irg_globals(irg, node);
487 (get_irg_tls)(const ir_graph *irg) {
488 return _get_irg_tls(irg);
492 (set_irg_tls)(ir_graph *irg, ir_node *node) {
493 _set_irg_tls(irg, node);
497 (get_irg_initial_mem)(const ir_graph *irg) {
498 return _get_irg_initial_mem(irg);
502 (set_irg_initial_mem)(ir_graph *irg, ir_node *node) {
503 _set_irg_initial_mem(irg, node);
507 (get_irg_args)(const ir_graph *irg) {
508 return _get_irg_args(irg);
512 (set_irg_args)(ir_graph *irg, ir_node *node) {
513 _set_irg_args(irg, node);
517 (get_irg_value_param_base)(const ir_graph *irg) {
518 return _get_irg_value_param_base(irg);
522 (set_irg_value_param_base)(ir_graph *irg, ir_node *node) {
523 _set_irg_value_param_base(irg, node);
527 (get_irg_proj_args) (const ir_graph *irg) {
528 return _get_irg_proj_args (irg);
532 (set_irg_proj_args) (ir_graph *irg, ir_node **nodes) {
533 _set_irg_proj_args (irg, nodes);
537 (get_irg_bad)(const ir_graph *irg) {
538 return _get_irg_bad(irg);
542 (set_irg_bad)(ir_graph *irg, ir_node *node) {
543 _set_irg_bad(irg, node);
547 (get_irg_no_mem)(const ir_graph *irg) {
548 return _get_irg_no_mem(irg);
552 (set_irg_no_mem)(ir_graph *irg, ir_node *node) {
553 _set_irg_no_mem(irg, node);
557 (get_irg_current_block)(const ir_graph *irg) {
558 return _get_irg_current_block(irg);
562 (set_irg_current_block)(ir_graph *irg, ir_node *node) {
563 _set_irg_current_block(irg, node);
567 (get_irg_entity)(const ir_graph *irg) {
568 return _get_irg_entity(irg);
572 (set_irg_entity)(ir_graph *irg, entity *ent) {
573 _set_irg_entity(irg, ent);
577 (get_irg_frame_type)(ir_graph *irg) {
578 return _get_irg_frame_type(irg);
582 (set_irg_frame_type)(ir_graph *irg, ir_type *ftp) {
583 _set_irg_frame_type(irg, ftp);
587 get_irg_n_locs (ir_graph *irg)
589 if (get_opt_precise_exc_context())
590 return irg->n_loc - 1 - 1;
592 return irg->n_loc - 1;
596 set_irg_n_loc (ir_graph *irg, int n_loc)
598 if (get_opt_precise_exc_context())
599 irg->n_loc = n_loc + 1 + 1;
601 irg->n_loc = n_loc + 1;
606 /* Returns the obstack associated with the graph. */
608 (get_irg_obstack)(const ir_graph *irg) {
609 return _get_irg_obstack(irg);
613 * Returns true if the node n is allocated on the storage of graph irg.
615 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
617 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
619 struct _obstack_chunk *p;
622 * checks weather the ir_node pointer is on the obstack.
623 * A more sophisticated check would test the "whole" ir_node
625 for (p = irg->obst->chunk; p; p = p->prev) {
626 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
634 (get_irg_phase_state)(const ir_graph *irg) {
635 return _get_irg_phase_state(irg);
639 (set_irg_phase_state)(ir_graph *irg, irg_phase_state state) {
640 _set_irg_phase_state(irg, state);
644 (get_irg_pinned)(const ir_graph *irg) {
645 return _get_irg_pinned(irg);
649 (get_irg_outs_state)(const ir_graph *irg) {
650 return _get_irg_outs_state(irg);
654 (set_irg_outs_inconsistent)(ir_graph *irg) {
655 _set_irg_outs_inconsistent(irg);
659 (get_irg_extblk_state)(const ir_graph *irg) {
660 return _get_irg_extblk_state(irg);
664 (set_irg_extblk_inconsistent)(ir_graph *irg) {
665 _set_irg_extblk_inconsistent(irg);
669 (get_irg_dom_state)(const ir_graph *irg) {
670 return _get_irg_dom_state(irg);
674 (get_irg_postdom_state)(const ir_graph *irg) {
675 return _get_irg_postdom_state(irg);
679 (set_irg_doms_inconsistent)(ir_graph *irg) {
680 _set_irg_doms_inconsistent(irg);
684 (get_irg_loopinfo_state)(const ir_graph *irg) {
685 return _get_irg_loopinfo_state(irg);
689 (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s) {
690 _set_irg_loopinfo_state(irg, s);
694 (set_irg_loopinfo_inconsistent)(ir_graph *irg) {
695 _set_irg_loopinfo_inconsistent(irg);
698 void set_irp_loopinfo_inconsistent(void) {
699 int i, n_irgs = get_irp_n_irgs();
700 for (i = 0; i < n_irgs; ++i) {
701 set_irg_loopinfo_inconsistent(get_irp_irg(i));
708 (set_irg_pinned)(ir_graph *irg, op_pin_state p) {
709 _set_irg_pinned(irg, p);
712 irg_callee_info_state
713 (get_irg_callee_info_state)(const ir_graph *irg) {
714 return _get_irg_callee_info_state(irg);
718 (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s) {
719 _set_irg_callee_info_state(irg, s);
723 (get_irg_inline_property)(const ir_graph *irg) {
724 return _get_irg_inline_property(irg);
728 (set_irg_inline_property)(ir_graph *irg, irg_inline_property s) {
729 _set_irg_inline_property(irg, s);
733 (get_irg_additional_properties)(const ir_graph *irg) {
734 return _get_irg_additional_properties(irg);
738 (set_irg_additional_properties)(ir_graph *irg, unsigned property_mask) {
739 _set_irg_additional_properties(irg, property_mask);
743 (set_irg_additional_property)(ir_graph *irg, mtp_additional_property flag) {
744 _set_irg_additional_property(irg, flag);
748 (set_irg_link)(ir_graph *irg, void *thing) {
749 _set_irg_link(irg, thing);
753 (get_irg_link)(const ir_graph *irg) {
754 return _get_irg_link(irg);
757 /** maximum visited flag content of all ir_graph visited fields. */
758 static unsigned long max_irg_visited = 0;
761 (get_irg_visited)(const ir_graph *irg) {
762 return _get_irg_visited(irg);
766 set_irg_visited (ir_graph *irg, unsigned long visited)
768 irg->visited = visited;
769 if (irg->visited > max_irg_visited) {
770 max_irg_visited = irg->visited;
775 inc_irg_visited (ir_graph *irg)
777 if (++irg->visited > max_irg_visited) {
778 max_irg_visited = irg->visited;
783 get_max_irg_visited(void)
787 for(i = 0; i < get_irp_n_irgs(); i++)
788 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
790 return max_irg_visited;
793 void set_max_irg_visited(int val) {
794 max_irg_visited = val;
798 inc_max_irg_visited(void)
802 for(i = 0; i < get_irp_n_irgs(); i++)
803 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
806 return max_irg_visited;
810 (get_irg_block_visited)(const ir_graph *irg) {
811 return _get_irg_block_visited(irg);
815 (set_irg_block_visited)(ir_graph *irg, unsigned long visited) {
816 _set_irg_block_visited(irg, visited);
820 (inc_irg_block_visited)(ir_graph *irg) {
821 _inc_irg_block_visited(irg);
824 /* Return the floating point model of this graph. */
825 unsigned (get_irg_fp_model)(const ir_graph *irg) {
826 return _get_irg_fp_model(irg);
829 /* Sets the floating point model for this graph. */
830 void set_irg_fp_model(ir_graph *irg, unsigned model) {
831 irg->fp_model = model;
835 * walker Start->End: places Proj nodes into the same block
836 * as it's predecessors
841 static void normalize_proj_walker(ir_node *n, void *env){
843 ir_node *pred = get_Proj_pred(n);
844 ir_node *block = get_nodes_block(pred);
846 set_nodes_block(n, block);
850 /* move Proj nodes into the same block as its predecessors */
851 void normalize_proj_nodes(ir_graph *irg) {
852 irg_walk_graph(irg, NULL, normalize_proj_walker, NULL);
853 set_irg_outs_inconsistent(irg);
856 /* set a description for local value n */
857 void set_irg_loc_description(ir_graph *irg, int n, void *description) {
858 assert(0 <= n && n < irg->n_loc);
860 if (! irg->loc_descriptions)
861 irg->loc_descriptions = xcalloc(sizeof(*irg->loc_descriptions), irg->n_loc);
863 irg->loc_descriptions[n] = description;
866 /* get the description for local value n */
867 void *get_irg_loc_description(ir_graph *irg, int n) {
868 assert(0 <= n && n < irg->n_loc);
869 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
872 /* Returns a estimated node count of the irg. */
873 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg) {
874 return _get_irg_estimated_node_cnt(irg);
877 /* Returns the last irn index for this graph. */
878 unsigned get_irg_last_idx(const ir_graph *irg) {
879 return irg->last_node_idx;
882 /* register additional space in an IR graph */
883 size_t register_additional_graph_data(size_t size)
885 assert(!forbid_new_data && "Too late to register additional node data");
890 return additional_graph_data_size += size;