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 /* initialize the IR graph module */
83 void init_irgraph(void) {
84 frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
89 * Allocate a new IR graph.
90 * This function respects the registered graph data. The only reason for
91 * this function is, that there are two locations, where graphs are
92 * allocated (new_r_ir_graph, new_const_code_irg).
93 * @return Memory for a new graph.
95 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);
104 * Frees an allocated IR graph
106 static void free_graph(ir_graph *irg) {
107 char *ptr = (char *)irg;
108 free(ptr - additional_graph_data_size);
111 #if USE_EXPLICIT_PHI_IN_STACK
112 /* really defined in ircons.c */
113 typedef struct Phi_in_stack Phi_in_stack;
114 Phi_in_stack *new_Phi_in_stack();
115 void free_Phi_in_stack(Phi_in_stack *s);
118 /* Allocates a list of nodes:
119 - The start block containing a start node and Proj nodes for it's four
120 results (X, M, P, Tuple).
121 - The end block containing an end node. This block is not matured after
122 new_ir_graph as predecessors need to be added to it.
123 - The current block, which is empty and also not matured.
124 Further it allocates several datastructures needed for graph construction
128 new_r_ir_graph (entity *ent, int n_loc)
131 ir_node *first_block;
135 res->kind = k_ir_graph;
137 edges_init_graph(res);
139 /* inform statistics here, as blocks will be already build on this graph */
140 hook_new_graph(res, ent);
142 current_ir_graph = res;
144 /*-- initialized for each graph. --*/
145 if (get_opt_precise_exc_context()) {
146 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
147 dereferenced in this graph plus one for
148 the store plus one for links to fragile
149 operations. n_loc is not the number of
150 parameters to the procedure! */
153 res->n_loc = n_loc + 1; /* number of local variables that are never
154 dereferenced in this graph plus one for
155 the store. This is not the number of parameters
159 /* descriptions will be allocated on demand */
160 res->loc_descriptions = NULL;
162 res->visited = 0; /* visited flag, for the ir walker */
163 res->block_visited = 0; /* visited flag, for the 'block'-walker */
165 #if USE_EXPLICIT_PHI_IN_STACK
166 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
169 res->kind = k_ir_graph;
170 res->obst = xmalloc (sizeof(*res->obst));
171 obstack_init (res->obst);
172 res->value_table = new_identities (); /* value table for global value
173 numbering for optimizing use in
177 res->phase_state = phase_building;
178 res->op_pin_state_pinned = op_pin_state_pinned;
179 res->outs_state = outs_none;
180 res->dom_state = dom_none;
181 res->typeinfo_state = ir_typeinfo_none;
182 set_irp_typeinfo_inconsistent(); /* there is a new graph with typeinfo_none. */
183 res->loopinfo_state = loopinfo_none;
184 res->class_cast_state = ir_class_casts_transitive;
186 /*-- Type information for the procedure of the graph --*/
188 set_entity_irg(ent, res);
190 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
191 res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
193 /* Remove type from type list. Must be treated differently than other types. */
194 remove_irp_type_from_list(res->frame_type);
196 /*-- Nodes needed in every graph --*/
197 res->end_block = new_immBlock();
198 res->end = new_End();
199 res->end_reg = res->end;
200 res->end_except = res->end;
202 res->start_block = new_immBlock();
203 res->start = new_Start();
204 res->bad = new_ir_node(NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
205 res->no_mem = new_ir_node(NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
207 /* Proj results of start node */
208 projX = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
209 res->frame = new_Proj (res->start, mode_P_mach, pn_Start_P_frame_base);
210 res->globals = new_Proj (res->start, mode_P_mach, pn_Start_P_globals);
211 res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
212 res->args = new_Proj (res->start, mode_T, pn_Start_T_args);
214 res->graph_nr = get_irp_new_node_nr();
216 res->proj_args = NULL;
218 set_store(res->initial_mem);
220 add_immBlock_pred(res->start_block, projX);
222 * The code generation needs it. leave it in now.
223 * Use of this edge is matter of discussion, unresolved. Also possible:
224 * add_immBlock_pred(res->start_block, res->start_block), but invalid typed.
226 mature_immBlock (res->current_block);
228 /*-- Make a block to start with --*/
229 first_block = new_immBlock();
230 add_immBlock_pred (first_block, projX);
232 res->method_execution_frequency = -1;
239 new_ir_graph (entity *ent, int n_loc)
241 ir_graph *res = new_r_ir_graph (ent, n_loc);
242 add_irp_irg(res); /* remember this graph global. */
246 /* Make a rudimentary ir graph for the constant code.
247 Must look like a correct irg, spare everything else. */
248 ir_graph *new_const_code_irg(void) {
254 /* inform statistics here, as blocks will be already build on this graph */
255 hook_new_graph(res, NULL);
257 current_ir_graph = res;
258 res->n_loc = 1; /* Only the memory. */
259 res->visited = 0; /* visited flag, for the ir walker */
260 res->block_visited=0; /* visited flag, for the 'block'-walker */
261 #if USE_EXPLICIT_PHI_IN_STACK
262 res->Phi_in_stack = NULL;
264 res->kind = k_ir_graph;
265 res->obst = xmalloc (sizeof(*res->obst));
266 obstack_init (res->obst);
267 res->phase_state = phase_building;
268 res->op_pin_state_pinned = op_pin_state_pinned;
269 res->value_table = new_identities (); /* value table for global value
270 numbering for optimizing use in
273 res->frame_type = NULL;
275 /* -- The end block -- */
276 res->end_block = new_immBlock ();
277 res->end = new_End ();
278 res->end_reg = res->end;
279 res->end_except = res->end;
280 mature_immBlock(get_cur_block()); /* mature the end block */
282 /* -- The start block -- */
283 res->start_block = new_immBlock ();
284 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
285 res->no_mem = new_ir_node (NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
286 res->start = new_Start ();
287 /* Proj results of start node */
288 res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
289 projX = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
290 add_immBlock_pred (res->start_block, projX);
291 mature_immBlock (res->start_block); /* mature the start block */
293 add_immBlock_pred (new_immBlock (), projX);
294 mature_immBlock (get_cur_block()); /* mature the 'body' block for expressions */
296 /* Set the visited flag high enough that the blocks will never be visited. */
297 set_irn_visited(get_cur_block(), -1);
298 set_Block_block_visited(get_cur_block(), -1);
299 set_Block_block_visited(res->start_block, -1);
300 set_irn_visited(res->start_block, -1);
301 set_irn_visited(res->bad, -1);
302 set_irn_visited(res->no_mem, -1);
304 res->phase_state = phase_high;
309 /* Defined in iropt.c */
310 void del_identities (pset *value_table);
312 /* Frees the passed irgraph.
313 Deallocates all nodes in this graph and the ir_graph structure.
314 Sets the field irgraph in the corresponding entity to NULL.
315 Does not remove the irgraph from the list in irprog (requires
316 inefficient search, call remove_irp_irg by hand).
317 Does not free types, entities or modes that are used only by this
318 graph, nor the entity standing for this graph. */
319 void free_ir_graph (ir_graph *irg) {
320 assert(is_ir_graph(irg));
322 hook_free_graph(irg);
323 if (irg->outs_state != outs_none) free_outs(irg);
324 if (irg->frame_type) free_type(irg->frame_type);
325 if (irg->value_table) del_identities(irg->value_table);
327 peculiarity pec = get_entity_peculiarity (irg->ent);
328 set_entity_peculiarity (irg->ent, peculiarity_description);
329 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
330 set_entity_peculiarity (irg->ent, pec);
334 obstack_free(irg->obst,NULL);
336 #if USE_EXPLICIT_PHI_IN_STACK
337 free_Phi_in_stack(irg->Phi_in_stack);
339 if (irg->loc_descriptions)
340 free(irg->loc_descriptions);
345 /* access routines for all ir_graph attributes:
347 {attr type} get_irg_{attribute name} (ir_graph *irg);
348 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
351 (is_ir_graph)(const void *thing) {
352 return _is_ir_graph(thing);
355 /* Outputs a unique number for this node */
358 get_irg_graph_nr(ir_graph *irg) {
361 return irg->graph_nr;
368 (get_irg_start_block)(const ir_graph *irg) {
369 return _get_irg_start_block(irg);
373 (set_irg_start_block)(ir_graph *irg, ir_node *node) {
374 _set_irg_start_block(irg, node);
378 (get_irg_start)(const ir_graph *irg) {
379 return _get_irg_start(irg);
383 (set_irg_start)(ir_graph *irg, ir_node *node) {
384 _set_irg_start(irg, node);
388 (get_irg_end_block)(const ir_graph *irg) {
389 return _get_irg_end_block(irg);
393 (set_irg_end_block)(ir_graph *irg, ir_node *node) {
394 _set_irg_end_block(irg, node);
398 (get_irg_end)(const ir_graph *irg) {
399 return _get_irg_end(irg);
403 (set_irg_end)(ir_graph *irg, ir_node *node) {
404 _set_irg_end(irg, node);
408 (get_irg_end_reg)(const ir_graph *irg) {
409 return _get_irg_end_reg(irg);
412 void set_irg_end_reg (ir_graph *irg, ir_node *node) {
413 assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
418 (get_irg_end_except)(const ir_graph *irg) {
419 return _get_irg_end_except(irg);
422 void set_irg_end_except (ir_graph *irg, ir_node *node) {
423 assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
424 irg->end_except = node;
428 (get_irg_cstore)(const ir_graph *irg) {
429 return _get_irg_cstore(irg);
433 (set_irg_cstore)(ir_graph *irg, ir_node *node) {
434 _set_irg_cstore(irg, node);
438 (get_irg_frame)(const ir_graph *irg) {
439 return _get_irg_frame(irg);
443 (set_irg_frame)(ir_graph *irg, ir_node *node) {
444 _set_irg_frame(irg, node);
448 (get_irg_globals)(const ir_graph *irg) {
449 return _get_irg_globals(irg);
453 (set_irg_globals)(ir_graph *irg, ir_node *node) {
454 _set_irg_globals(irg, node);
458 (get_irg_initial_mem)(const ir_graph *irg) {
459 return _get_irg_initial_mem(irg);
463 (set_irg_initial_mem)(ir_graph *irg, ir_node *node) {
464 _set_irg_initial_mem(irg, node);
468 (get_irg_args)(const ir_graph *irg) {
469 return _get_irg_args(irg);
473 (set_irg_args)(ir_graph *irg, ir_node *node) {
474 _set_irg_args(irg, node);
478 (get_irg_proj_args) (const ir_graph *irg) {
479 return _get_irg_proj_args (irg);
483 (set_irg_proj_args) (ir_graph *irg, ir_node **nodes) {
484 _set_irg_proj_args (irg, nodes);
488 (get_irg_bad)(const ir_graph *irg) {
489 return _get_irg_bad(irg);
493 (set_irg_bad)(ir_graph *irg, ir_node *node) {
494 _set_irg_bad(irg, node);
498 (get_irg_no_mem)(const ir_graph *irg) {
499 return _get_irg_no_mem(irg);
503 (set_irg_no_mem)(ir_graph *irg, ir_node *node) {
504 _set_irg_no_mem(irg, node);
508 (get_irg_current_block)(const ir_graph *irg) {
509 return _get_irg_current_block(irg);
513 (set_irg_current_block)(ir_graph *irg, ir_node *node) {
514 _set_irg_current_block(irg, node);
518 (get_irg_entity)(const ir_graph *irg) {
519 return _get_irg_entity(irg);
523 (set_irg_entity)(ir_graph *irg, entity *ent) {
524 _set_irg_entity(irg, ent);
528 (get_irg_frame_type)(const ir_graph *irg) {
529 return _get_irg_frame_type(irg);
533 (set_irg_frame_type)(ir_graph *irg, type *ftp) {
534 _set_irg_frame_type(irg, ftp);
538 /* To test for a frame type */
540 is_frame_type(const type *ftp) {
542 if (is_Class_type(ftp)) {
543 for (i = 0; i < get_irp_n_irgs(); i++) {
544 const type *frame_tp = get_irg_frame_type(get_irp_irg(i));
545 if (ftp == frame_tp) return true;
552 get_irg_n_locs (ir_graph *irg)
554 if (get_opt_precise_exc_context())
555 return irg->n_loc - 1 - 1;
557 return irg->n_loc - 1;
561 set_irg_n_loc (ir_graph *irg, int n_loc)
563 if (get_opt_precise_exc_context())
564 irg->n_loc = n_loc + 1 + 1;
566 irg->n_loc = n_loc + 1;
571 /* Returns the obstack associated with the graph. */
573 (get_irg_obstack)(const ir_graph *irg) {
574 return _get_irg_obstack(irg);
578 * Returns true if the node n is allocated on the storage of graph irg.
580 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
582 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
584 struct _obstack_chunk *p;
587 * checks weather the ir_node pointer is on the obstack.
588 * A more sophisticated check would test the "whole" ir_node
590 for (p = irg->obst->chunk; p; p = p->prev) {
591 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
599 (get_irg_phase_state)(const ir_graph *irg) {
600 return _get_irg_phase_state(irg);
604 (set_irg_phase_low)(ir_graph *irg) {
605 _set_irg_phase_low(irg);
609 (get_irg_pinned)(const ir_graph *irg) {
610 return _get_irg_pinned(irg);
614 (get_irg_outs_state)(const ir_graph *irg) {
615 return _get_irg_outs_state(irg);
619 (set_irg_outs_inconsistent)(ir_graph *irg) {
620 _set_irg_outs_inconsistent(irg);
624 (get_irg_dom_state)(const ir_graph *irg) {
625 return _get_irg_dom_state(irg);
629 (set_irg_dom_inconsistent)(ir_graph *irg) {
630 _set_irg_dom_inconsistent(irg);
634 (get_irg_loopinfo_state)(const ir_graph *irg) {
635 return _get_irg_loopinfo_state(irg);
639 (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s) {
640 _set_irg_loopinfo_state(irg, s);
644 (set_irg_loopinfo_inconsistent)(ir_graph *irg) {
645 _set_irg_loopinfo_inconsistent(irg);
649 (set_irg_pinned)(ir_graph *irg, op_pin_state p) {
650 _set_irg_pinned(irg, p);
653 irg_callee_info_state
654 (get_irg_callee_info_state)(const ir_graph *irg) {
655 return _get_irg_callee_info_state(irg);
659 (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s) {
660 _set_irg_callee_info_state(irg, s);
664 (get_irg_inline_property)(const ir_graph *irg) {
665 return _get_irg_inline_property(irg);
669 (set_irg_inline_property)(ir_graph *irg, irg_inline_property s) {
670 _set_irg_inline_property(irg, s);
674 (set_irg_link)(ir_graph *irg, void *thing) {
675 _set_irg_link(irg, thing);
679 (get_irg_link)(const ir_graph *irg) {
680 return _get_irg_link(irg);
683 /** maximum visited flag content of all ir_graph visited fields. */
684 static unsigned long max_irg_visited = 0;
687 (get_irg_visited)(const ir_graph *irg) {
688 return _get_irg_visited(irg);
692 set_irg_visited (ir_graph *irg, unsigned long visited)
694 irg->visited = visited;
695 if (irg->visited > max_irg_visited) {
696 max_irg_visited = irg->visited;
701 inc_irg_visited (ir_graph *irg)
703 if (++irg->visited > max_irg_visited) {
704 max_irg_visited = irg->visited;
709 get_max_irg_visited(void)
713 for(i = 0; i < get_irp_n_irgs(); i++)
714 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
716 return max_irg_visited;
719 void set_max_irg_visited(int val) {
720 max_irg_visited = val;
724 inc_max_irg_visited(void)
728 for(i = 0; i < get_irp_n_irgs(); i++)
729 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
732 return max_irg_visited;
736 (get_irg_block_visited)(const ir_graph *irg) {
737 return _get_irg_block_visited(irg);
741 (set_irg_block_visited)(ir_graph *irg, unsigned long visited) {
742 _set_irg_block_visited(irg, visited);
746 (inc_irg_block_visited)(ir_graph *irg) {
747 _inc_irg_block_visited(irg);
752 * walker Start->End: places Proj nodes into the same block
753 * as it's predecessors
758 static void normalize_proj_walker(ir_node *n, void *env)
761 ir_node *pred = get_Proj_pred(n);
762 ir_node *block = get_nodes_block(pred);
764 set_nodes_block(n, block);
768 /* move Proj nodes into the same block as its predecessors */
769 void normalize_proj_nodes(ir_graph *irg)
771 irg_walk_graph(irg, NULL, normalize_proj_walker, NULL);
772 set_irg_outs_inconsistent(irg);
775 /* set a description for local value n */
776 void set_irg_loc_description(ir_graph *irg, int n, void *description)
778 assert(0 <= n && n < irg->n_loc);
780 if (! irg->loc_descriptions)
781 irg->loc_descriptions = xcalloc(sizeof(*irg->loc_descriptions), irg->n_loc);
783 irg->loc_descriptions[n] = description;
786 /* get the description for local value n */
787 void *get_irg_loc_description(ir_graph *irg, int n)
789 assert(0 <= n && n < irg->n_loc);
790 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
793 /* register additional space in an IR graph */
794 size_t register_additional_graph_data(size_t size)
796 assert(!forbid_new_data && "Too late to register additional node data");
801 return additional_graph_data_size += size;