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"
26 # include "irprog_t.h"
27 # include "irnode_t.h"
29 # include "irflag_t.h"
36 # include "iredges_t.h"
39 * Indicates, whether additional data can be registered to graphs.
40 * If set to 1, this is not possible anymore.
42 static int forbid_new_data = 0;
45 * The amount of additional space for custom data to be allocated upon
46 * creating a new graph.
48 static size_t additional_graph_data_size = 0;
50 ir_graph *current_ir_graph;
51 ir_graph *get_current_ir_graph(void) {
52 return current_ir_graph;
54 void set_current_ir_graph(ir_graph *graph) {
55 current_ir_graph = graph;
59 int __interprocedural_view = false;
61 int (get_interprocedural_view)(void) {
62 return _get_interprocedural_view();
65 void (set_interprocedural_view)(int state) {
66 __interprocedural_view = state;
68 /* set function vectors for faster access */
70 _get_irn_arity = _get_irn_inter_arity;
71 _get_irn_n = _get_irn_inter_n;
74 _get_irn_arity = _get_irn_intra_arity;
75 _get_irn_n = _get_irn_intra_n;
79 /** contains the suffix for frame type names */
80 static ident *frame_type_suffix = NULL;
82 /** The default firm calling convention mask. */
83 static unsigned firm_default_cc_mask = 0;
85 /* initialize the IR graph module */
86 void firm_init_irgraph(unsigned default_cc_mask) {
87 frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
89 firm_default_cc_mask = default_cc_mask;
92 /* Gets the default calling convention for new constructed graphs. */
93 unsigned get_firm_default_calling_convention(void) {
94 return firm_default_cc_mask;
97 /* Sets the default calling convention for new constructed graphs. */
98 void set_firm_default_calling_convention(unsigned cc_mask) {
99 firm_default_cc_mask = cc_mask;
103 * Allocate a new IR graph.
104 * This function respects the registered graph data. The only reason for
105 * this function is, that there are two locations, where graphs are
106 * allocated (new_r_ir_graph, new_const_code_irg).
107 * @return Memory for a new graph.
109 static ir_graph *alloc_graph(void) {
110 size_t size = sizeof(ir_graph) + additional_graph_data_size;
111 char *ptr = xmalloc(size);
112 memset(ptr, 0, size);
114 return (ir_graph *) (ptr + additional_graph_data_size);
118 * Frees an allocated IR graph
120 static void free_graph(ir_graph *irg) {
121 char *ptr = (char *)irg;
122 free(ptr - additional_graph_data_size);
125 #if USE_EXPLICIT_PHI_IN_STACK
126 /* really defined in ircons.c */
127 typedef struct Phi_in_stack Phi_in_stack;
128 Phi_in_stack *new_Phi_in_stack();
129 void free_Phi_in_stack(Phi_in_stack *s);
132 /* Allocates a list of nodes:
133 - The start block containing a start node and Proj nodes for it's four
134 results (X, M, P, Tuple).
135 - The end block containing an end node. This block is not matured after
136 new_ir_graph as predecessors need to be added to it.
137 - The current block, which is empty and also not matured.
138 Further it allocates several datastructures needed for graph construction
142 new_r_ir_graph (entity *ent, int n_loc)
145 ir_node *first_block;
149 res->kind = k_ir_graph;
151 edges_init_graph(res);
153 /* inform statistics here, as blocks will be already build on this graph */
154 hook_new_graph(res, ent);
156 current_ir_graph = res;
158 /*-- initialized for each graph. --*/
159 if (get_opt_precise_exc_context()) {
160 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
161 dereferenced in this graph plus one for
162 the store plus one for links to fragile
163 operations. n_loc is not the number of
164 parameters to the procedure! */
167 res->n_loc = n_loc + 1; /* number of local variables that are never
168 dereferenced in this graph plus one for
169 the store. This is not the number of parameters
173 /* descriptions will be allocated on demand */
174 res->loc_descriptions = NULL;
176 res->visited = 0; /* visited flag, for the ir walker */
177 res->block_visited = 0; /* visited flag, for the 'block'-walker */
179 #if USE_EXPLICIT_PHI_IN_STACK
180 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
183 res->kind = k_ir_graph;
184 res->obst = xmalloc (sizeof(*res->obst));
185 obstack_init(res->obst);
186 res->extbb_obst = NULL;
188 res->value_table = new_identities (); /* value table for global value
189 numbering for optimizing use in
193 res->inline_property = irg_inline_any;
194 res->additional_properties = 0;
196 res->phase_state = phase_building;
197 res->op_pin_state_pinned = op_pin_state_pinned;
198 res->outs_state = outs_none;
199 res->dom_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->execfreq_state = exec_freq_none;
205 res->class_cast_state = ir_class_casts_transitive;
206 res->extblk_state = ir_extblk_info_none;
207 res->calling_conv = get_firm_default_calling_convention();
209 /*-- Type information for the procedure of the graph --*/
211 set_entity_irg(ent, res);
213 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
214 res->frame_type = new_type_frame(mangle(get_entity_ident(ent), frame_type_suffix));
216 /*-- Nodes needed in every graph --*/
217 res->end_block = new_immBlock();
218 res->end = new_End();
219 res->end_reg = res->end;
220 res->end_except = res->end;
222 res->start_block = new_immBlock();
223 res->start = new_Start();
224 res->bad = new_ir_node(NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
225 res->no_mem = new_ir_node(NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
227 /* Proj results of start node */
228 projX = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
229 res->frame = new_Proj (res->start, mode_P_data, pn_Start_P_frame_base);
230 res->globals = new_Proj (res->start, mode_P_data, pn_Start_P_globals);
231 res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
232 res->args = new_Proj (res->start, mode_T, pn_Start_T_args);
234 res->graph_nr = get_irp_new_node_nr();
236 res->proj_args = NULL;
238 set_store(res->initial_mem);
240 add_immBlock_pred(res->start_block, projX);
242 * The code generation needs it. leave it in now.
243 * Use of this edge is matter of discussion, unresolved. Also possible:
244 * add_immBlock_pred(res->start_block, res->start_block), but invalid typed.
246 mature_immBlock (res->current_block);
248 /*-- Make a block to start with --*/
249 first_block = new_immBlock();
250 add_immBlock_pred (first_block, projX);
252 res->method_execution_frequency = -1;
259 new_ir_graph (entity *ent, int n_loc)
261 ir_graph *res = new_r_ir_graph (ent, n_loc);
262 add_irp_irg(res); /* remember this graph global. */
266 /* Make a rudimentary ir graph for the constant code.
267 Must look like a correct irg, spare everything else. */
268 ir_graph *new_const_code_irg(void) {
274 /* inform statistics here, as blocks will be already build on this graph */
275 hook_new_graph(res, NULL);
277 current_ir_graph = res;
278 res->n_loc = 1; /* Only the memory. */
279 res->visited = 0; /* visited flag, for the ir walker */
280 res->block_visited=0; /* visited flag, for the 'block'-walker */
281 #if USE_EXPLICIT_PHI_IN_STACK
282 res->Phi_in_stack = NULL;
284 res->kind = k_ir_graph;
285 res->obst = xmalloc (sizeof(*res->obst));
286 obstack_init (res->obst);
287 res->extbb_obst = NULL;
289 res->phase_state = phase_building;
290 res->op_pin_state_pinned = op_pin_state_pinned;
291 res->extblk_state = ir_extblk_info_none;
293 res->value_table = new_identities (); /* value table for global value
294 numbering for optimizing use in
297 res->frame_type = NULL;
299 /* -- The end block -- */
300 res->end_block = new_immBlock ();
301 res->end = new_End ();
302 res->end_reg = res->end;
303 res->end_except = res->end;
304 mature_immBlock(get_cur_block()); /* mature the end block */
306 /* -- The start block -- */
307 res->start_block = new_immBlock ();
308 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
309 res->no_mem = new_ir_node (NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
310 res->start = new_Start ();
311 /* Proj results of start node */
312 res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
313 projX = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
314 add_immBlock_pred (res->start_block, projX);
315 mature_immBlock (res->start_block); /* mature the start block */
317 add_immBlock_pred (new_immBlock (), projX);
318 mature_immBlock (get_cur_block()); /* mature the 'body' block for expressions */
320 /* Set the visited flag high enough that the blocks will never be visited. */
321 set_irn_visited(get_cur_block(), -1);
322 set_Block_block_visited(get_cur_block(), -1);
323 set_Block_block_visited(res->start_block, -1);
324 set_irn_visited(res->start_block, -1);
325 set_irn_visited(res->bad, -1);
326 set_irn_visited(res->no_mem, -1);
328 res->phase_state = phase_high;
333 /* Defined in iropt.c */
334 void del_identities (pset *value_table);
336 /* Frees the passed irgraph.
337 Deallocates all nodes in this graph and the ir_graph structure.
338 Sets the field irgraph in the corresponding entity to NULL.
339 Does not remove the irgraph from the list in irprog (requires
340 inefficient search, call remove_irp_irg by hand).
341 Does not free types, entities or modes that are used only by this
342 graph, nor the entity standing for this graph. */
343 void free_ir_graph (ir_graph *irg) {
344 assert(is_ir_graph(irg));
346 hook_free_graph(irg);
347 if (irg->outs_state != outs_none) free_irg_outs(irg);
348 if (irg->frame_type) free_type(irg->frame_type);
349 if (irg->value_table) del_identities(irg->value_table);
351 peculiarity pec = get_entity_peculiarity (irg->ent);
352 set_entity_peculiarity (irg->ent, peculiarity_description);
353 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
354 set_entity_peculiarity (irg->ent, pec);
358 obstack_free(irg->obst,NULL);
360 #if USE_EXPLICIT_PHI_IN_STACK
361 free_Phi_in_stack(irg->Phi_in_stack);
363 if (irg->loc_descriptions)
364 free(irg->loc_descriptions);
369 /* access routines for all ir_graph attributes:
371 {attr type} get_irg_{attribute name} (ir_graph *irg);
372 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
375 (is_ir_graph)(const void *thing) {
376 return _is_ir_graph(thing);
379 /* Outputs a unique number for this node */
382 get_irg_graph_nr(ir_graph *irg) {
385 return irg->graph_nr;
392 (get_irg_start_block)(const ir_graph *irg) {
393 return _get_irg_start_block(irg);
397 (set_irg_start_block)(ir_graph *irg, ir_node *node) {
398 _set_irg_start_block(irg, node);
402 (get_irg_start)(const ir_graph *irg) {
403 return _get_irg_start(irg);
407 (set_irg_start)(ir_graph *irg, ir_node *node) {
408 _set_irg_start(irg, node);
412 (get_irg_end_block)(const ir_graph *irg) {
413 return _get_irg_end_block(irg);
417 (set_irg_end_block)(ir_graph *irg, ir_node *node) {
418 _set_irg_end_block(irg, node);
422 (get_irg_end)(const ir_graph *irg) {
423 return _get_irg_end(irg);
427 (set_irg_end)(ir_graph *irg, ir_node *node) {
428 _set_irg_end(irg, node);
432 (get_irg_end_reg)(const ir_graph *irg) {
433 return _get_irg_end_reg(irg);
436 void set_irg_end_reg (ir_graph *irg, ir_node *node) {
437 assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
442 (get_irg_end_except)(const ir_graph *irg) {
443 return _get_irg_end_except(irg);
446 void set_irg_end_except (ir_graph *irg, ir_node *node) {
447 assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
448 irg->end_except = node;
452 (get_irg_cstore)(const ir_graph *irg) {
453 return _get_irg_cstore(irg);
457 (set_irg_cstore)(ir_graph *irg, ir_node *node) {
458 _set_irg_cstore(irg, node);
462 (get_irg_frame)(const ir_graph *irg) {
463 return _get_irg_frame(irg);
467 (set_irg_frame)(ir_graph *irg, ir_node *node) {
468 _set_irg_frame(irg, node);
472 (get_irg_globals)(const ir_graph *irg) {
473 return _get_irg_globals(irg);
477 (set_irg_globals)(ir_graph *irg, ir_node *node) {
478 _set_irg_globals(irg, node);
482 (get_irg_initial_mem)(const ir_graph *irg) {
483 return _get_irg_initial_mem(irg);
487 (set_irg_initial_mem)(ir_graph *irg, ir_node *node) {
488 _set_irg_initial_mem(irg, node);
492 (get_irg_args)(const ir_graph *irg) {
493 return _get_irg_args(irg);
497 (set_irg_args)(ir_graph *irg, ir_node *node) {
498 _set_irg_args(irg, node);
502 (get_irg_proj_args) (const ir_graph *irg) {
503 return _get_irg_proj_args (irg);
507 (set_irg_proj_args) (ir_graph *irg, ir_node **nodes) {
508 _set_irg_proj_args (irg, nodes);
512 (get_irg_bad)(const ir_graph *irg) {
513 return _get_irg_bad(irg);
517 (set_irg_bad)(ir_graph *irg, ir_node *node) {
518 _set_irg_bad(irg, node);
522 (get_irg_no_mem)(const ir_graph *irg) {
523 return _get_irg_no_mem(irg);
527 (set_irg_no_mem)(ir_graph *irg, ir_node *node) {
528 _set_irg_no_mem(irg, node);
532 (get_irg_current_block)(const ir_graph *irg) {
533 return _get_irg_current_block(irg);
537 (set_irg_current_block)(ir_graph *irg, ir_node *node) {
538 _set_irg_current_block(irg, node);
542 (get_irg_entity)(const ir_graph *irg) {
543 return _get_irg_entity(irg);
547 (set_irg_entity)(ir_graph *irg, entity *ent) {
548 _set_irg_entity(irg, ent);
552 (get_irg_frame_type)(ir_graph *irg) {
553 return _get_irg_frame_type(irg);
557 (set_irg_frame_type)(ir_graph *irg, type *ftp) {
558 _set_irg_frame_type(irg, ftp);
562 get_irg_n_locs (ir_graph *irg)
564 if (get_opt_precise_exc_context())
565 return irg->n_loc - 1 - 1;
567 return irg->n_loc - 1;
571 set_irg_n_loc (ir_graph *irg, int n_loc)
573 if (get_opt_precise_exc_context())
574 irg->n_loc = n_loc + 1 + 1;
576 irg->n_loc = n_loc + 1;
581 /* Returns the obstack associated with the graph. */
583 (get_irg_obstack)(const ir_graph *irg) {
584 return _get_irg_obstack(irg);
588 * Returns true if the node n is allocated on the storage of graph irg.
590 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
592 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
594 struct _obstack_chunk *p;
597 * checks weather the ir_node pointer is on the obstack.
598 * A more sophisticated check would test the "whole" ir_node
600 for (p = irg->obst->chunk; p; p = p->prev) {
601 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
609 (get_irg_phase_state)(const ir_graph *irg) {
610 return _get_irg_phase_state(irg);
614 (set_irg_phase_low)(ir_graph *irg) {
615 _set_irg_phase_low(irg);
619 (get_irg_pinned)(const ir_graph *irg) {
620 return _get_irg_pinned(irg);
624 (get_irg_outs_state)(const ir_graph *irg) {
625 return _get_irg_outs_state(irg);
629 (set_irg_outs_inconsistent)(ir_graph *irg) {
630 _set_irg_outs_inconsistent(irg);
634 (get_irg_dom_state)(const ir_graph *irg) {
635 return _get_irg_dom_state(irg);
639 (set_irg_dom_inconsistent)(ir_graph *irg) {
640 _set_irg_dom_inconsistent(irg);
644 (get_irg_loopinfo_state)(const ir_graph *irg) {
645 return _get_irg_loopinfo_state(irg);
649 (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s) {
650 _set_irg_loopinfo_state(irg, s);
654 (set_irg_loopinfo_inconsistent)(ir_graph *irg) {
655 _set_irg_loopinfo_inconsistent(irg);
658 void set_irp_loopinfo_inconsistent(void) {
659 int i, n_irgs = get_irp_n_irgs();
660 for (i = 0; i < n_irgs; ++i) {
661 set_irg_loopinfo_inconsistent(get_irp_irg(i));
668 (set_irg_pinned)(ir_graph *irg, op_pin_state p) {
669 _set_irg_pinned(irg, p);
672 irg_callee_info_state
673 (get_irg_callee_info_state)(const ir_graph *irg) {
674 return _get_irg_callee_info_state(irg);
678 (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s) {
679 _set_irg_callee_info_state(irg, s);
683 (get_irg_inline_property)(const ir_graph *irg) {
684 return _get_irg_inline_property(irg);
688 (set_irg_inline_property)(ir_graph *irg, irg_inline_property s) {
689 _set_irg_inline_property(irg, s);
693 (get_irg_additional_properties)(const ir_graph *irg) {
694 return _get_irg_additional_properties(irg);
698 (set_irg_additional_properties)(ir_graph *irg, unsigned property_mask) {
699 _set_irg_additional_properties(irg, property_mask);
703 (set_irg_additional_property)(ir_graph *irg, irg_additional_property flag) {
704 _set_irg_additional_property(irg, flag);
707 /* Returns the calling convention of a graph. */
709 (get_irg_calling_convention)(const ir_graph *irg) {
710 return _get_irg_calling_convention(irg);
713 /* Sets the calling convention of a graph. */
715 (set_irg_calling_convention)(ir_graph *irg, unsigned cc_mask) {
716 _set_irg_calling_convention(irg, cc_mask);
720 (set_irg_link)(ir_graph *irg, void *thing) {
721 _set_irg_link(irg, thing);
725 (get_irg_link)(const ir_graph *irg) {
726 return _get_irg_link(irg);
729 /** maximum visited flag content of all ir_graph visited fields. */
730 static unsigned long max_irg_visited = 0;
733 (get_irg_visited)(const ir_graph *irg) {
734 return _get_irg_visited(irg);
738 set_irg_visited (ir_graph *irg, unsigned long visited)
740 irg->visited = visited;
741 if (irg->visited > max_irg_visited) {
742 max_irg_visited = irg->visited;
747 inc_irg_visited (ir_graph *irg)
749 if (++irg->visited > max_irg_visited) {
750 max_irg_visited = irg->visited;
755 get_max_irg_visited(void)
759 for(i = 0; i < get_irp_n_irgs(); i++)
760 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
762 return max_irg_visited;
765 void set_max_irg_visited(int val) {
766 max_irg_visited = val;
770 inc_max_irg_visited(void)
774 for(i = 0; i < get_irp_n_irgs(); i++)
775 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
778 return max_irg_visited;
782 (get_irg_block_visited)(const ir_graph *irg) {
783 return _get_irg_block_visited(irg);
787 (set_irg_block_visited)(ir_graph *irg, unsigned long visited) {
788 _set_irg_block_visited(irg, visited);
792 (inc_irg_block_visited)(ir_graph *irg) {
793 _inc_irg_block_visited(irg);
798 * walker Start->End: places Proj nodes into the same block
799 * as it's predecessors
804 static void normalize_proj_walker(ir_node *n, void *env)
807 ir_node *pred = get_Proj_pred(n);
808 ir_node *block = get_nodes_block(pred);
810 set_nodes_block(n, block);
814 /* move Proj nodes into the same block as its predecessors */
815 void normalize_proj_nodes(ir_graph *irg)
817 irg_walk_graph(irg, NULL, normalize_proj_walker, NULL);
818 set_irg_outs_inconsistent(irg);
821 /* set a description for local value n */
822 void set_irg_loc_description(ir_graph *irg, int n, void *description)
824 assert(0 <= n && n < irg->n_loc);
826 if (! irg->loc_descriptions)
827 irg->loc_descriptions = xcalloc(sizeof(*irg->loc_descriptions), irg->n_loc);
829 irg->loc_descriptions[n] = description;
832 /* get the description for local value n */
833 void *get_irg_loc_description(ir_graph *irg, int n)
835 assert(0 <= n && n < irg->n_loc);
836 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
839 /* register additional space in an IR graph */
840 size_t register_additional_graph_data(size_t size)
842 assert(!forbid_new_data && "Too late to register additional node data");
847 return additional_graph_data_size += size;