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"
38 * Indicates, whether additional data can be registered to graphs.
39 * If set to 1, this is not possible anymore.
41 static int forbid_new_data = 0;
44 * The amount of additional space for custom data to be allocated upon
45 * creating a new graph.
47 static size_t additional_graph_data_size = 0;
49 ir_graph *current_ir_graph;
50 ir_graph *get_current_ir_graph(void) {
51 return current_ir_graph;
53 void set_current_ir_graph(ir_graph *graph) {
54 current_ir_graph = graph;
58 int __interprocedural_view = false;
60 int (get_interprocedural_view)(void) {
61 return _get_interprocedural_view();
64 void (set_interprocedural_view)(int state) {
65 __interprocedural_view = state;
67 /* set function vectors for faster access */
69 _get_irn_arity = _get_irn_inter_arity;
70 _get_irn_n = _get_irn_inter_n;
73 _get_irn_arity = _get_irn_intra_arity;
74 _get_irn_n = _get_irn_intra_n;
78 /** contains the suffix for frame type names */
79 static ident *frame_type_suffix = NULL;
81 /* initialize the IR graph module */
82 void init_irgraph(void) {
83 frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
88 * Allocate a new IR graph.
89 * This function respects the registered graph data. The only reason for
90 * this function is, that there are two locations, where graphs are
91 * allocated (new_r_ir_graph, new_const_code_irg).
92 * @return Memory for a new graph.
94 static ir_graph *alloc_graph(void)
96 size_t size = sizeof(ir_graph) + additional_graph_data_size;
97 char *ptr = xmalloc(size);
100 return (ir_graph *) (ptr + additional_graph_data_size);
103 #if USE_EXPLICIT_PHI_IN_STACK
104 /* really defined in ircons.c */
105 typedef struct Phi_in_stack Phi_in_stack;
106 Phi_in_stack *new_Phi_in_stack();
107 void free_Phi_in_stack(Phi_in_stack *s);
110 /* Allocates a list of nodes:
111 - The start block containing a start node and Proj nodes for it's four
112 results (X, M, P, Tuple).
113 - The end block containing an end node. This block is not matured after
114 new_ir_graph as predecessors need to be added to it.
115 - The current block, which is empty and also not matured.
116 Further it allocates several datastructures needed for graph construction
120 new_r_ir_graph (entity *ent, int n_loc)
123 ir_node *first_block;
127 res->kind = k_ir_graph;
129 /* inform statistics here, as blocks will be already build on this graph */
130 hook_new_graph(res, ent);
132 current_ir_graph = res;
134 /*-- initialized for each graph. --*/
135 if (get_opt_precise_exc_context()) {
136 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
137 dereferenced in this graph plus one for
138 the store plus one for links to fragile
139 operations. n_loc is not the number of
140 parameters to the procedure! */
143 res->n_loc = n_loc + 1; /* number of local variables that are never
144 dereferenced in this graph plus one for
145 the store. This is not the number of parameters
149 /* descriptions will be allocated on demand */
150 res->loc_descriptions = NULL;
152 res->visited = 0; /* visited flag, for the ir walker */
153 res->block_visited = 0; /* visited flag, for the 'block'-walker */
155 #if USE_EXPLICIT_PHI_IN_STACK
156 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
159 res->kind = k_ir_graph;
160 res->obst = xmalloc (sizeof(*res->obst));
161 obstack_init (res->obst);
162 res->value_table = new_identities (); /* value table for global value
163 numbering for optimizing use in
167 res->phase_state = phase_building;
168 res->op_pin_state_pinned = op_pin_state_pinned;
169 res->outs_state = outs_none;
170 res->dom_state = dom_none;
171 res->typeinfo_state = irg_typeinfo_none;
172 res->loopinfo_state = loopinfo_none;
174 /*-- Type information for the procedure of the graph --*/
176 set_entity_irg(ent, res);
178 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
179 res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
181 /* Remove type from type list. Must be treated differently than other types. */
182 remove_irp_type_from_list(res->frame_type);
184 /*-- Nodes needed in every graph --*/
185 res->end_block = new_immBlock();
186 res->end = new_End();
187 res->end_reg = res->end;
188 res->end_except = res->end;
190 res->start_block = new_immBlock();
191 res->start = new_Start();
192 res->bad = new_ir_node(NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
193 res->no_mem = new_ir_node(NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
195 /* Proj results of start node */
196 projX = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
197 res->frame = new_Proj (res->start, mode_P_mach, pn_Start_P_frame_base);
198 res->globals = new_Proj (res->start, mode_P_mach, pn_Start_P_globals);
199 res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
200 res->args = new_Proj (res->start, mode_T, pn_Start_T_args);
202 res->graph_nr = get_irp_new_node_nr();
204 res->proj_args = NULL;
206 set_store(res->initial_mem);
208 add_immBlock_pred(res->start_block, projX);
210 * The code generation needs it. leave it in now.
211 * Use of this edge is matter of discussion, unresolved. Also possible:
212 * add_immBlock_pred(res->start_block, res->start_block), but invalid typed.
214 mature_immBlock (res->current_block);
216 /*-- Make a block to start with --*/
217 first_block = new_immBlock();
218 add_immBlock_pred (first_block, projX);
220 res->method_execution_frequency = -1;
227 new_ir_graph (entity *ent, int n_loc)
229 ir_graph *res = new_r_ir_graph (ent, n_loc);
230 add_irp_irg(res); /* remember this graph global. */
234 /* Make a rudimentary ir graph for the constant code.
235 Must look like a correct irg, spare everything else. */
236 ir_graph *new_const_code_irg(void) {
242 /* inform statistics here, as blocks will be already build on this graph */
243 hook_new_graph(res, NULL);
245 current_ir_graph = res;
246 res->n_loc = 1; /* Only the memory. */
247 res->visited = 0; /* visited flag, for the ir walker */
248 res->block_visited=0; /* visited flag, for the 'block'-walker */
249 #if USE_EXPLICIT_PHI_IN_STACK
250 res->Phi_in_stack = NULL;
252 res->kind = k_ir_graph;
253 res->obst = xmalloc (sizeof(*res->obst));
254 obstack_init (res->obst);
255 res->phase_state = phase_building;
256 res->op_pin_state_pinned = op_pin_state_pinned;
257 res->value_table = new_identities (); /* value table for global value
258 numbering for optimizing use in
261 res->frame_type = NULL;
263 /* -- The end block -- */
264 res->end_block = new_immBlock ();
265 res->end = new_End ();
266 res->end_reg = res->end;
267 res->end_except = res->end;
268 mature_immBlock(get_cur_block()); /* mature the end block */
270 /* -- The start block -- */
271 res->start_block = new_immBlock ();
272 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
273 res->no_mem = new_ir_node (NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
274 res->start = new_Start ();
275 /* Proj results of start node */
276 res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
277 projX = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
278 add_immBlock_pred (res->start_block, projX);
279 mature_immBlock (res->start_block); /* mature the start block */
281 add_immBlock_pred (new_immBlock (), projX);
282 mature_immBlock (get_cur_block()); /* mature the 'body' block for expressions */
284 /* Set the visited flag high enough that the blocks will never be visited. */
285 set_irn_visited(get_cur_block(), -1);
286 set_Block_block_visited(get_cur_block(), -1);
287 set_Block_block_visited(res->start_block, -1);
288 set_irn_visited(res->start_block, -1);
289 set_irn_visited(res->bad, -1);
290 set_irn_visited(res->no_mem, -1);
292 res->phase_state = phase_high;
296 /* Defined in iropt.c */
297 void del_identities (pset *value_table);
299 /* Frees the passed irgraph.
300 Deallocates all nodes in this graph and the ir_graph structure.
301 Sets the field irgraph in the corresponding entity to NULL.
302 Does not remove the irgraph from the list in irprog (requires
303 inefficient search, call remove_irp_irg by hand).
304 Does not free types, entities or modes that are used only by this
305 graph, nor the entity standing for this graph. */
306 void free_ir_graph (ir_graph *irg) {
308 hook_free_graph(irg);
309 if (irg->outs_state != outs_none) free_outs(irg);
310 if (irg->frame_type) free_type(irg->frame_type);
311 if (irg->value_table) del_identities(irg->value_table);
313 peculiarity pec = get_entity_peculiarity (irg->ent);
314 set_entity_peculiarity (irg->ent, peculiarity_description);
315 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
316 set_entity_peculiarity (irg->ent, pec);
320 obstack_free(irg->obst,NULL);
322 #if USE_EXPLICIT_PHI_IN_STACK
323 free_Phi_in_stack(irg->Phi_in_stack);
325 if (irg->loc_descriptions)
326 free(irg->loc_descriptions);
331 /* access routines for all ir_graph attributes:
333 {attr type} get_irg_{attribute name} (ir_graph *irg);
334 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
337 (is_ir_graph)(const void *thing) {
338 return _is_ir_graph(thing);
341 /* Outputs a unique number for this node */
344 get_irg_graph_nr(ir_graph *irg) {
347 return irg->graph_nr;
354 (get_irg_start_block)(const ir_graph *irg) {
355 return _get_irg_start_block(irg);
359 (set_irg_start_block)(ir_graph *irg, ir_node *node) {
360 _set_irg_start_block(irg, node);
364 (get_irg_start)(const ir_graph *irg) {
365 return _get_irg_start(irg);
369 (set_irg_start)(ir_graph *irg, ir_node *node) {
370 _set_irg_start(irg, node);
374 (get_irg_end_block)(const ir_graph *irg) {
375 return _get_irg_end_block(irg);
379 (set_irg_end_block)(ir_graph *irg, ir_node *node) {
380 _set_irg_end_block(irg, node);
384 (get_irg_end)(const ir_graph *irg) {
385 return _get_irg_end(irg);
389 (set_irg_end)(ir_graph *irg, ir_node *node) {
390 _set_irg_end(irg, node);
394 (get_irg_end_reg)(const ir_graph *irg) {
395 return _get_irg_end_reg(irg);
398 void set_irg_end_reg (ir_graph *irg, ir_node *node) {
399 assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
404 (get_irg_end_except)(const ir_graph *irg) {
405 return _get_irg_end_except(irg);
408 void set_irg_end_except (ir_graph *irg, ir_node *node) {
409 assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
410 irg->end_except = node;
414 (get_irg_cstore)(const ir_graph *irg) {
415 return _get_irg_cstore(irg);
419 (set_irg_cstore)(ir_graph *irg, ir_node *node) {
420 _set_irg_cstore(irg, node);
424 (get_irg_frame)(const ir_graph *irg) {
425 return _get_irg_frame(irg);
429 (set_irg_frame)(ir_graph *irg, ir_node *node) {
430 _set_irg_frame(irg, node);
434 (get_irg_globals)(const ir_graph *irg) {
435 return _get_irg_globals(irg);
439 (set_irg_globals)(ir_graph *irg, ir_node *node) {
440 _set_irg_globals(irg, node);
444 (get_irg_initial_mem)(const ir_graph *irg) {
445 return _get_irg_initial_mem(irg);
449 (set_irg_initial_mem)(ir_graph *irg, ir_node *node) {
450 _set_irg_initial_mem(irg, node);
454 (get_irg_args)(const ir_graph *irg) {
455 return _get_irg_args(irg);
459 (set_irg_args)(ir_graph *irg, ir_node *node) {
460 _set_irg_args(irg, node);
464 (get_irg_proj_args) (const ir_graph *irg) {
465 return _get_irg_proj_args (irg);
469 (set_irg_proj_args) (ir_graph *irg, ir_node **nodes) {
470 _set_irg_proj_args (irg, nodes);
474 (get_irg_bad)(const ir_graph *irg) {
475 return _get_irg_bad(irg);
479 (set_irg_bad)(ir_graph *irg, ir_node *node) {
480 _set_irg_bad(irg, node);
484 (get_irg_no_mem)(const ir_graph *irg) {
485 return _get_irg_no_mem(irg);
489 (set_irg_no_mem)(ir_graph *irg, ir_node *node) {
490 _set_irg_no_mem(irg, node);
494 (get_irg_current_block)(const ir_graph *irg) {
495 return _get_irg_current_block(irg);
499 (set_irg_current_block)(ir_graph *irg, ir_node *node) {
500 _set_irg_current_block(irg, node);
504 (get_irg_entity)(const ir_graph *irg) {
505 return _get_irg_entity(irg);
509 (set_irg_entity)(ir_graph *irg, entity *ent) {
510 _set_irg_entity(irg, ent);
514 (get_irg_frame_type)(const ir_graph *irg) {
515 return _get_irg_frame_type(irg);
519 (set_irg_frame_type)(ir_graph *irg, type *ftp) {
520 _set_irg_frame_type(irg, ftp);
524 /* To test for a frame type */
526 is_frame_type(const type *ftp) {
528 if (is_Class_type(ftp)) {
529 for (i = 0; i < get_irp_n_irgs(); i++) {
530 const type *frame_tp = get_irg_frame_type(get_irp_irg(i));
531 if (ftp == frame_tp) return true;
538 get_irg_n_locs (ir_graph *irg)
540 if (get_opt_precise_exc_context())
541 return irg->n_loc - 1 - 1;
543 return irg->n_loc - 1;
547 set_irg_n_loc (ir_graph *irg, int n_loc)
549 if (get_opt_precise_exc_context())
550 irg->n_loc = n_loc + 1 + 1;
552 irg->n_loc = n_loc + 1;
557 /* Returns the obstack associated with the graph. */
559 (get_irg_obstack)(const ir_graph *irg) {
560 return _get_irg_obstack(irg);
564 * Returns true if the node n is allocated on the storage of graph irg.
566 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
568 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
570 struct _obstack_chunk *p;
573 * checks weather the ir_node pointer is on the obstack.
574 * A more sophisticated check would test the "whole" ir_node
576 for (p = irg->obst->chunk; p; p = p->prev) {
577 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
585 (get_irg_phase_state)(const ir_graph *irg) {
586 return _get_irg_phase_state(irg);
590 (set_irg_phase_low)(ir_graph *irg) {
591 _set_irg_phase_low(irg);
595 (get_irg_pinned)(const ir_graph *irg) {
596 return _get_irg_pinned(irg);
600 (get_irg_outs_state)(const ir_graph *irg) {
601 return _get_irg_outs_state(irg);
605 (set_irg_outs_inconsistent)(ir_graph *irg) {
606 _set_irg_outs_inconsistent(irg);
610 (get_irg_dom_state)(const ir_graph *irg) {
611 return _get_irg_dom_state(irg);
615 (set_irg_dom_inconsistent)(ir_graph *irg) {
616 _set_irg_dom_inconsistent(irg);
620 (get_irg_loopinfo_state)(const ir_graph *irg) {
621 return _get_irg_loopinfo_state(irg);
625 (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s) {
626 _set_irg_loopinfo_state(irg, s);
630 set_irg_loopinfo_inconsistent(ir_graph *irg) {
631 if (irg->loopinfo_state == loopinfo_ip_consistent)
632 irg->loopinfo_state = loopinfo_ip_inconsistent;
634 else if (irg->loopinfo_state == loopinfo_consistent)
635 irg->loopinfo_state = loopinfo_inconsistent;
637 else if (irg->loopinfo_state == loopinfo_cf_ip_consistent)
638 irg->loopinfo_state = loopinfo_cf_ip_inconsistent;
640 else if (irg->loopinfo_state == loopinfo_cf_consistent)
641 irg->loopinfo_state = loopinfo_cf_inconsistent;
645 (set_irg_pinned)(ir_graph *irg, op_pin_state p) {
646 _set_irg_pinned(irg, p);
649 irg_callee_info_state
650 (get_irg_callee_info_state)(const ir_graph *irg) {
651 return _get_irg_callee_info_state(irg);
655 (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s) {
656 _set_irg_callee_info_state(irg, s);
660 (get_irg_inline_property)(const ir_graph *irg) {
661 return _get_irg_inline_property(irg);
665 (set_irg_inline_property)(ir_graph *irg, irg_inline_property s) {
666 _set_irg_inline_property(irg, s);
670 (set_irg_link)(ir_graph *irg, void *thing) {
671 _set_irg_link(irg, thing);
675 (get_irg_link)(const ir_graph *irg) {
676 return _get_irg_link(irg);
679 /** maximum visited flag content of all ir_graph visited fields. */
680 static unsigned long max_irg_visited = 0;
683 (get_irg_visited)(const ir_graph *irg) {
684 return _get_irg_visited(irg);
688 set_irg_visited (ir_graph *irg, unsigned long visited)
690 irg->visited = visited;
691 if (irg->visited > max_irg_visited) {
692 max_irg_visited = irg->visited;
697 inc_irg_visited (ir_graph *irg)
699 if (++irg->visited > max_irg_visited) {
700 max_irg_visited = irg->visited;
705 get_max_irg_visited(void)
709 for(i = 0; i < get_irp_n_irgs(); i++)
710 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
712 return max_irg_visited;
715 void set_max_irg_visited(int val) {
716 max_irg_visited = val;
720 inc_max_irg_visited(void)
724 for(i = 0; i < get_irp_n_irgs(); i++)
725 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
728 return max_irg_visited;
732 (get_irg_block_visited)(const ir_graph *irg) {
733 return _get_irg_block_visited(irg);
737 (set_irg_block_visited)(ir_graph *irg, unsigned long visited) {
738 _set_irg_block_visited(irg, visited);
742 (inc_irg_block_visited)(ir_graph *irg) {
743 _inc_irg_block_visited(irg);
748 * walker Start->End: places Proj nodes into the same block
749 * as it's predecessors
754 static void normalize_proj_walker(ir_node *n, void *env)
757 ir_node *pred = get_Proj_pred(n);
758 ir_node *block = get_nodes_block(pred);
760 set_nodes_block(n, block);
764 /* move Proj nodes into the same block as its predecessors */
765 void normalize_proj_nodes(ir_graph *irg)
767 irg_walk_graph(irg, NULL, normalize_proj_walker, NULL);
768 set_irg_outs_inconsistent(irg);
771 /* set a description for local value n */
772 void set_irg_loc_description(ir_graph *irg, int n, void *description)
774 assert(0 <= n && n < irg->n_loc);
776 if (! irg->loc_descriptions)
777 irg->loc_descriptions = xcalloc(sizeof(*irg->loc_descriptions), irg->n_loc);
779 irg->loc_descriptions[n] = description;
782 /* get the description for local value n */
783 void *get_irg_loc_description(ir_graph *irg, int n)
785 assert(0 <= n && n < irg->n_loc);
786 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
789 /* register additional space in an IR graph */
790 size_t register_additional_graph_data(size_t size)
792 assert(!forbid_new_data && "Too late to register additional node data");
797 return additional_graph_data_size += size;