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"
37 #include "iredges_t.h"
41 * Indicates, whether additional data can be registered to graphs.
42 * If set to 1, this is not possible anymore.
44 static int forbid_new_data = 0;
47 * The amount of additional space for custom data to be allocated upon
48 * creating a new graph.
50 static size_t additional_graph_data_size = 0;
52 ir_graph *current_ir_graph;
53 ir_graph *get_current_ir_graph(void) {
54 return current_ir_graph;
56 void set_current_ir_graph(ir_graph *graph) {
57 current_ir_graph = graph;
61 int firm_interprocedural_view = 0;
63 int (get_interprocedural_view)(void) {
64 return _get_interprocedural_view();
67 void (set_interprocedural_view)(int state) {
68 firm_interprocedural_view = state;
70 /* set function vectors for faster access */
72 _get_irn_arity = _get_irn_inter_arity;
73 _get_irn_n = _get_irn_inter_n;
76 _get_irn_arity = _get_irn_intra_arity;
77 _get_irn_n = _get_irn_intra_n;
81 /** contains the suffix for frame type names */
82 static ident *frame_type_suffix = NULL;
84 /* initialize the IR graph module */
85 void firm_init_irgraph(void) {
86 frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
91 * Allocate a new IR graph.
92 * This function respects the registered graph data. The only reason for
93 * this function is, that there are two locations, where graphs are
94 * allocated (new_r_ir_graph, new_const_code_irg).
95 * @return Memory for a new graph.
97 static ir_graph *alloc_graph(void) {
98 size_t size = sizeof(ir_graph) + additional_graph_data_size;
99 char *ptr = xmalloc(size);
100 memset(ptr, 0, size);
102 return (ir_graph *) (ptr + additional_graph_data_size);
106 * Frees an allocated IR graph
108 static void free_graph(ir_graph *irg) {
109 char *ptr = (char *)irg;
110 free(ptr - additional_graph_data_size);
113 #if USE_EXPLICIT_PHI_IN_STACK
114 /* really defined in ircons.c */
115 typedef struct Phi_in_stack Phi_in_stack;
116 Phi_in_stack *new_Phi_in_stack();
117 void free_Phi_in_stack(Phi_in_stack *s);
120 /* Allocates a list of nodes:
121 - The start block containing a start node and Proj nodes for it's four
122 results (X, M, P, Tuple).
123 - The end block containing an end node. This block is not matured after
124 new_ir_graph as predecessors need to be added to it.
125 - The current block, which is empty and also not matured.
126 Further it allocates several datastructures needed for graph construction
130 new_r_ir_graph (entity *ent, int n_loc)
133 ir_node *first_block;
134 ir_node *end, *start, *start_block, *initial_mem, *projX;
137 res->kind = k_ir_graph;
139 edges_init_graph(res);
141 /* inform statistics here, as blocks will be already build on this graph */
142 hook_new_graph(res, ent);
144 current_ir_graph = res;
146 /*-- initialized for each graph. --*/
147 if (get_opt_precise_exc_context()) {
148 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
149 dereferenced in this graph plus one for
150 the store plus one for links to fragile
151 operations. n_loc is not the number of
152 parameters to the procedure! */
155 res->n_loc = n_loc + 1; /* number of local variables that are never
156 dereferenced in this graph plus one for
157 the store. This is not the number of parameters
161 /* descriptions will be allocated on demand */
162 res->loc_descriptions = NULL;
164 res->visited = 0; /* visited flag, for the ir walker */
165 res->block_visited = 0; /* visited flag, for the 'block'-walker */
167 #if USE_EXPLICIT_PHI_IN_STACK
168 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
171 res->kind = k_ir_graph;
172 res->obst = xmalloc (sizeof(*res->obst));
173 obstack_init(res->obst);
174 res->extbb_obst = NULL;
176 res->value_table = new_identities (); /* value table for global value
177 numbering for optimizing use in
181 res->inline_property = irg_inline_any;
182 res->additional_properties = mtp_property_inherited; /* inherited from type */
184 res->phase_state = phase_building;
185 res->irg_pinned_state = op_pin_state_pinned;
186 res->outs_state = outs_none;
187 res->dom_state = dom_none;
188 res->pdom_state = dom_none;
189 res->typeinfo_state = ir_typeinfo_none;
190 set_irp_typeinfo_inconsistent(); /* there is a new graph with typeinfo_none. */
191 res->callee_info_state = irg_callee_info_none;
192 res->loopinfo_state = loopinfo_none;
193 res->execfreq_state = exec_freq_none;
194 res->class_cast_state = ir_class_casts_transitive;
195 res->extblk_state = ir_extblk_info_none;
197 /*-- Type information for the procedure of the graph --*/
199 set_entity_irg(ent, res);
201 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
202 res->frame_type = new_type_frame(mangle(get_entity_ident(ent), frame_type_suffix));
204 /*-- Nodes needed in every graph --*/
205 set_irg_end_block (res, new_immBlock());
207 set_irg_end (res, end);
208 set_irg_end_reg (res, end);
209 set_irg_end_except(res, end);
211 start_block = new_immBlock();
212 set_irg_start_block(res, start_block);
213 set_irg_bad (res, new_ir_node(NULL, res, start_block, op_Bad, mode_T, 0, NULL));
214 set_irg_no_mem (res, new_ir_node(NULL, res, start_block, op_NoMem, mode_M, 0, NULL));
216 set_irg_start (res, start);
218 /* Proj results of start node */
219 projX = new_Proj(start, mode_X, pn_Start_X_initial_exec);
220 set_irg_frame (res, new_Proj(start, mode_P_data, pn_Start_P_frame_base));
221 set_irg_globals (res, new_Proj(start, mode_P_data, pn_Start_P_globals));
222 set_irg_args (res, new_Proj(start, mode_T, pn_Start_T_args));
223 initial_mem = new_Proj(start, mode_M, pn_Start_M);
224 set_irg_initial_mem(res, initial_mem);
226 add_immBlock_pred(start_block, projX);
227 set_store(initial_mem);
230 res->graph_nr = get_irp_new_node_nr();
232 res->proj_args = NULL;
235 * The code generation needs it. leave it in now.
236 * Use of this edge is matter of discussion, unresolved. Also possible:
237 * add_immBlock_pred(res->start_block, res->start_block), but invalid typed.
239 mature_immBlock (res->current_block);
241 /*-- Make a block to start with --*/
242 first_block = new_immBlock();
243 add_immBlock_pred (first_block, projX);
245 res->method_execution_frequency = -1;
246 res->estimated_node_count = 0;
253 new_ir_graph (entity *ent, int n_loc)
255 ir_graph *res = new_r_ir_graph (ent, n_loc);
256 add_irp_irg(res); /* remember this graph global. */
260 /* Make a rudimentary ir graph for the constant code.
261 Must look like a correct irg, spare everything else. */
262 ir_graph *new_const_code_irg(void) {
264 ir_node *end, *start_block, *start, *projX;
268 /* inform statistics here, as blocks will be already build on this graph */
269 hook_new_graph(res, NULL);
271 current_ir_graph = res;
272 res->n_loc = 1; /* Only the memory. */
273 res->visited = 0; /* visited flag, for the ir walker */
274 res->block_visited = 0; /* visited flag, for the 'block'-walker */
275 #if USE_EXPLICIT_PHI_IN_STACK
276 res->Phi_in_stack = NULL;
278 res->kind = k_ir_graph;
279 res->obst = xmalloc (sizeof(*res->obst));
280 obstack_init (res->obst);
281 res->extbb_obst = NULL;
283 res->phase_state = phase_building;
284 res->irg_pinned_state = op_pin_state_pinned;
285 res->extblk_state = ir_extblk_info_none;
287 res->value_table = new_identities (); /* value table for global value
288 numbering for optimizing use in
291 res->frame_type = NULL;
293 /* -- The end block -- */
294 set_irg_end_block (res, new_immBlock());
296 set_irg_end (res, end);
297 set_irg_end_reg (res, end);
298 set_irg_end_except(res, end);
299 mature_immBlock(get_cur_block()); /* mature the end block */
301 /* -- The start block -- */
302 start_block = new_immBlock();
303 set_irg_start_block(res, start_block);
304 set_irg_bad (res, new_ir_node (NULL, res, start_block, op_Bad, mode_T, 0, NULL));
305 set_irg_no_mem (res, new_ir_node (NULL, res, start_block, op_NoMem, mode_M, 0, NULL));
307 set_irg_start (res, start);
309 /* Proj results of start node */
310 set_irg_initial_mem(res, new_Proj(start, mode_M, pn_Start_M));
311 projX = new_Proj(start, mode_X, pn_Start_X_initial_exec);
312 add_immBlock_pred(start_block, projX);
313 mature_immBlock (start_block); /* mature the start block */
315 add_immBlock_pred(new_immBlock(), projX);
316 mature_immBlock (get_cur_block()); /* mature the 'body' block for expressions */
318 /* Set the visited flag high enough that the blocks will never be visited. */
319 set_irn_visited(get_cur_block(), -1);
320 set_Block_block_visited(get_cur_block(), -1);
321 set_Block_block_visited(start_block, -1);
322 set_irn_visited(start_block, -1);
323 set_irn_visited(get_irg_bad(res), -1);
324 set_irn_visited(get_irg_no_mem(res), -1);
326 res->phase_state = phase_high;
331 /* Defined in iropt.c */
332 void del_identities (pset *value_table);
334 /* Frees the passed irgraph.
335 Deallocates all nodes in this graph and the ir_graph structure.
336 Sets the field irgraph in the corresponding entity to NULL.
337 Does not remove the irgraph from the list in irprog (requires
338 inefficient search, call remove_irp_irg by hand).
339 Does not free types, entities or modes that are used only by this
340 graph, nor the entity standing for this graph. */
341 void free_ir_graph (ir_graph *irg) {
342 assert(is_ir_graph(irg));
344 hook_free_graph(irg);
345 if (irg->outs_state != outs_none) free_irg_outs(irg);
346 if (irg->frame_type) free_type(irg->frame_type);
347 if (irg->value_table) del_identities(irg->value_table);
349 peculiarity pec = get_entity_peculiarity (irg->ent);
350 set_entity_peculiarity (irg->ent, peculiarity_description);
351 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
352 set_entity_peculiarity (irg->ent, pec);
355 free_End(get_irg_end(irg));
356 obstack_free(irg->obst,NULL);
358 #if USE_EXPLICIT_PHI_IN_STACK
359 free_Phi_in_stack(irg->Phi_in_stack);
361 if (irg->loc_descriptions)
362 free(irg->loc_descriptions);
367 /* access routines for all ir_graph attributes:
369 {attr type} get_irg_{attribute name} (ir_graph *irg);
370 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
373 (is_ir_graph)(const void *thing) {
374 return _is_ir_graph(thing);
377 /* Outputs a unique number for this node */
378 long get_irg_graph_nr(ir_graph *irg) {
381 return irg->graph_nr;
383 return (long)PTR_TO_INT(irg);
388 (get_irg_start_block)(const ir_graph *irg) {
389 return _get_irg_start_block(irg);
393 (set_irg_start_block)(ir_graph *irg, ir_node *node) {
394 _set_irg_start_block(irg, node);
398 (get_irg_start)(const ir_graph *irg) {
399 return _get_irg_start(irg);
403 (set_irg_start)(ir_graph *irg, ir_node *node) {
404 _set_irg_start(irg, node);
408 (get_irg_end_block)(const ir_graph *irg) {
409 return _get_irg_end_block(irg);
413 (set_irg_end_block)(ir_graph *irg, ir_node *node) {
414 _set_irg_end_block(irg, node);
418 (get_irg_end)(const ir_graph *irg) {
419 return _get_irg_end(irg);
423 (set_irg_end)(ir_graph *irg, ir_node *node) {
424 _set_irg_end(irg, node);
428 (get_irg_end_reg)(const ir_graph *irg) {
429 return _get_irg_end_reg(irg);
432 void set_irg_end_reg (ir_graph *irg, ir_node *node) {
433 assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
434 irg->anchors[anchor_end_reg] = node;
438 (get_irg_end_except)(const ir_graph *irg) {
439 return _get_irg_end_except(irg);
442 void set_irg_end_except (ir_graph *irg, ir_node *node) {
443 assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
444 irg->anchors[anchor_end_except] = node;
448 (get_irg_cstore)(const ir_graph *irg) {
449 return _get_irg_cstore(irg);
453 (set_irg_cstore)(ir_graph *irg, ir_node *node) {
454 _set_irg_cstore(irg, node);
458 (get_irg_frame)(const ir_graph *irg) {
459 return _get_irg_frame(irg);
463 (set_irg_frame)(ir_graph *irg, ir_node *node) {
464 _set_irg_frame(irg, node);
468 (get_irg_globals)(const ir_graph *irg) {
469 return _get_irg_globals(irg);
473 (set_irg_globals)(ir_graph *irg, ir_node *node) {
474 _set_irg_globals(irg, node);
478 (get_irg_initial_mem)(const ir_graph *irg) {
479 return _get_irg_initial_mem(irg);
483 (set_irg_initial_mem)(ir_graph *irg, ir_node *node) {
484 _set_irg_initial_mem(irg, node);
488 (get_irg_args)(const ir_graph *irg) {
489 return _get_irg_args(irg);
493 (set_irg_args)(ir_graph *irg, ir_node *node) {
494 _set_irg_args(irg, node);
498 (get_irg_proj_args) (const ir_graph *irg) {
499 return _get_irg_proj_args (irg);
503 (set_irg_proj_args) (ir_graph *irg, ir_node **nodes) {
504 _set_irg_proj_args (irg, nodes);
508 (get_irg_bad)(const ir_graph *irg) {
509 return _get_irg_bad(irg);
513 (set_irg_bad)(ir_graph *irg, ir_node *node) {
514 _set_irg_bad(irg, node);
518 (get_irg_no_mem)(const ir_graph *irg) {
519 return _get_irg_no_mem(irg);
523 (set_irg_no_mem)(ir_graph *irg, ir_node *node) {
524 _set_irg_no_mem(irg, node);
528 (get_irg_current_block)(const ir_graph *irg) {
529 return _get_irg_current_block(irg);
533 (set_irg_current_block)(ir_graph *irg, ir_node *node) {
534 _set_irg_current_block(irg, node);
538 (get_irg_entity)(const ir_graph *irg) {
539 return _get_irg_entity(irg);
543 (set_irg_entity)(ir_graph *irg, entity *ent) {
544 _set_irg_entity(irg, ent);
548 (get_irg_frame_type)(ir_graph *irg) {
549 return _get_irg_frame_type(irg);
553 (set_irg_frame_type)(ir_graph *irg, ir_type *ftp) {
554 _set_irg_frame_type(irg, ftp);
558 get_irg_n_locs (ir_graph *irg)
560 if (get_opt_precise_exc_context())
561 return irg->n_loc - 1 - 1;
563 return irg->n_loc - 1;
567 set_irg_n_loc (ir_graph *irg, int n_loc)
569 if (get_opt_precise_exc_context())
570 irg->n_loc = n_loc + 1 + 1;
572 irg->n_loc = n_loc + 1;
577 /* Returns the obstack associated with the graph. */
579 (get_irg_obstack)(const ir_graph *irg) {
580 return _get_irg_obstack(irg);
584 * Returns true if the node n is allocated on the storage of graph irg.
586 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
588 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
590 struct _obstack_chunk *p;
593 * checks weather the ir_node pointer is on the obstack.
594 * A more sophisticated check would test the "whole" ir_node
596 for (p = irg->obst->chunk; p; p = p->prev) {
597 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
605 (get_irg_phase_state)(const ir_graph *irg) {
606 return _get_irg_phase_state(irg);
610 (set_irg_phase_state)(ir_graph *irg, irg_phase_state state) {
611 _set_irg_phase_state(irg, state);
615 (get_irg_pinned)(const ir_graph *irg) {
616 return _get_irg_pinned(irg);
620 (get_irg_outs_state)(const ir_graph *irg) {
621 return _get_irg_outs_state(irg);
625 (set_irg_outs_inconsistent)(ir_graph *irg) {
626 _set_irg_outs_inconsistent(irg);
630 (get_irg_extblk_state)(const ir_graph *irg) {
631 return _get_irg_extblk_state(irg);
635 (set_irg_extblk_inconsistent)(ir_graph *irg) {
636 _set_irg_extblk_inconsistent(irg);
640 (get_irg_dom_state)(const ir_graph *irg) {
641 return _get_irg_dom_state(irg);
645 (get_irg_postdom_state)(const ir_graph *irg) {
646 return _get_irg_postdom_state(irg);
650 (set_irg_doms_inconsistent)(ir_graph *irg) {
651 _set_irg_doms_inconsistent(irg);
655 (get_irg_loopinfo_state)(const ir_graph *irg) {
656 return _get_irg_loopinfo_state(irg);
660 (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s) {
661 _set_irg_loopinfo_state(irg, s);
665 (set_irg_loopinfo_inconsistent)(ir_graph *irg) {
666 _set_irg_loopinfo_inconsistent(irg);
669 void set_irp_loopinfo_inconsistent(void) {
670 int i, n_irgs = get_irp_n_irgs();
671 for (i = 0; i < n_irgs; ++i) {
672 set_irg_loopinfo_inconsistent(get_irp_irg(i));
679 (set_irg_pinned)(ir_graph *irg, op_pin_state p) {
680 _set_irg_pinned(irg, p);
683 irg_callee_info_state
684 (get_irg_callee_info_state)(const ir_graph *irg) {
685 return _get_irg_callee_info_state(irg);
689 (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s) {
690 _set_irg_callee_info_state(irg, s);
694 (get_irg_inline_property)(const ir_graph *irg) {
695 return _get_irg_inline_property(irg);
699 (set_irg_inline_property)(ir_graph *irg, irg_inline_property s) {
700 _set_irg_inline_property(irg, s);
704 (get_irg_additional_properties)(const ir_graph *irg) {
705 return _get_irg_additional_properties(irg);
709 (set_irg_additional_properties)(ir_graph *irg, unsigned property_mask) {
710 _set_irg_additional_properties(irg, property_mask);
714 (set_irg_additional_property)(ir_graph *irg, mtp_additional_property flag) {
715 _set_irg_additional_property(irg, flag);
719 (set_irg_link)(ir_graph *irg, void *thing) {
720 _set_irg_link(irg, thing);
724 (get_irg_link)(const ir_graph *irg) {
725 return _get_irg_link(irg);
728 /** maximum visited flag content of all ir_graph visited fields. */
729 static unsigned long max_irg_visited = 0;
732 (get_irg_visited)(const ir_graph *irg) {
733 return _get_irg_visited(irg);
737 set_irg_visited (ir_graph *irg, unsigned long visited)
739 irg->visited = visited;
740 if (irg->visited > max_irg_visited) {
741 max_irg_visited = irg->visited;
746 inc_irg_visited (ir_graph *irg)
748 if (++irg->visited > max_irg_visited) {
749 max_irg_visited = irg->visited;
754 get_max_irg_visited(void)
758 for(i = 0; i < get_irp_n_irgs(); i++)
759 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
761 return max_irg_visited;
764 void set_max_irg_visited(int val) {
765 max_irg_visited = val;
769 inc_max_irg_visited(void)
773 for(i = 0; i < get_irp_n_irgs(); i++)
774 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
777 return max_irg_visited;
781 (get_irg_block_visited)(const ir_graph *irg) {
782 return _get_irg_block_visited(irg);
786 (set_irg_block_visited)(ir_graph *irg, unsigned long visited) {
787 _set_irg_block_visited(irg, visited);
791 (inc_irg_block_visited)(ir_graph *irg) {
792 _inc_irg_block_visited(irg);
797 * walker Start->End: places Proj nodes into the same block
798 * as it's predecessors
803 static void normalize_proj_walker(ir_node *n, void *env)
806 ir_node *pred = get_Proj_pred(n);
807 ir_node *block = get_nodes_block(pred);
809 set_nodes_block(n, block);
813 /* move Proj nodes into the same block as its predecessors */
814 void normalize_proj_nodes(ir_graph *irg)
816 irg_walk_graph(irg, NULL, normalize_proj_walker, NULL);
817 set_irg_outs_inconsistent(irg);
820 /* set a description for local value n */
821 void set_irg_loc_description(ir_graph *irg, int n, void *description)
823 assert(0 <= n && n < irg->n_loc);
825 if (! irg->loc_descriptions)
826 irg->loc_descriptions = xcalloc(sizeof(*irg->loc_descriptions), irg->n_loc);
828 irg->loc_descriptions[n] = description;
831 /* get the description for local value n */
832 void *get_irg_loc_description(ir_graph *irg, int n)
834 assert(0 <= n && n < irg->n_loc);
835 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
838 /* Returns a estimated node count of the irg. */
839 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg) {
840 return _get_irg_estimated_node_cnt(irg);
843 /* register additional space in an IR graph */
844 size_t register_additional_graph_data(size_t size)
846 assert(!forbid_new_data && "Too late to register additional node data");
851 return additional_graph_data_size += size;