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.
20 # include "irgraph_t.h"
21 # include "irprog_t.h"
27 # include "firmstat.h"
29 ir_graph *current_ir_graph;
30 INLINE ir_graph *get_current_ir_graph(void) {
31 return current_ir_graph;
33 INLINE void set_current_ir_graph(ir_graph *graph) {
34 current_ir_graph = graph;
38 bool interprocedural_view = false;
39 INLINE bool get_interprocedural_view(void) {
40 return interprocedural_view;
42 INLINE void set_interprocedural_view(bool state) {
43 interprocedural_view = state;
46 static ident* frame_type_suffix = NULL;
47 void init_irgraph(void) {
48 frame_type_suffix = id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX));
51 #if USE_EXPLICIT_PHI_IN_STACK
52 /* really defined in ircons.c */
53 typedef struct Phi_in_stack Phi_in_stack;
54 Phi_in_stack *new_Phi_in_stack();
55 void free_Phi_in_stack(Phi_in_stack *s);
58 /* Allocates a list of nodes:
59 - The start block containing a start node and Proj nodes for it's four
60 results (X, M, P, Tuple).
61 - The end block containing an end node. This block is not matured after
62 new_ir_graph as predecessors need to be added to it.
63 - The current block, which is empty and also not matured.
64 Further it allocates several datastructures needed for graph construction
68 new_ir_graph (entity *ent, int n_loc)
74 res = (ir_graph *) malloc (sizeof (ir_graph));
75 memset(res, 0, sizeof (ir_graph));
78 /* inform statistics here, as blocks will be already build on this graph */
79 stat_new_graph(res, ent);
81 current_ir_graph = res;
82 add_irp_irg(res); /* remember this graph global. */
84 /*-- initialized for each graph. --*/
85 #if PRECISE_EXC_CONTEXT
86 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
87 dereferenced in this graph plus one for
88 the store plus one for links to fragile
89 operations. n_loc is not the number of
90 parameters to the procedure! */
92 res->n_loc = n_loc + 1; /* number of local variables that are never
93 dereferenced in this graph plus one for
94 the store. This is not the number of parameters
98 res->visited = 0; /* visited flag, for the ir walker */
99 res->block_visited=0; /* visited flag, for the 'block'-walker */
101 #if USE_EXPLICIT_PHI_IN_STACK
102 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
105 res->kind = k_ir_graph;
106 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
107 obstack_init (res->obst);
108 res->value_table = new_identities (); /* value table for global value
109 numbering for optimizing use in
113 res->phase_state = phase_building;
114 res->pinned = pinned;
115 res->outs_state = no_outs;
116 res->dom_state = no_dom;
117 res->typeinfo_state = irg_typeinfo_none;
118 res->loopinfo_state = loopinfo_none;
120 /*-- Type information for the procedure of the graph --*/
122 set_entity_irg(ent, res);
124 /*-- contain "inner" methods as in Pascal. --*/
125 res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
126 /* Remove type from type list. Must be treated differently than other types. */
127 remove_irp_type_from_list(res->frame_type);
129 /*-- Nodes needed in every graph --*/
130 res->end_block = new_immBlock ();
131 res->end = new_End ();
132 res->end_reg = res->end;
133 res->end_except = res->end;
135 res->start_block = new_immBlock ();
136 res->start = new_Start ();
137 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
138 /* res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL); */
140 /* Proj results of start node */
141 projX = new_Proj (res->start, mode_X, pns_initial_exec);
142 set_store (new_Proj (res->start, mode_M, pns_global_store));
143 res->frame = new_Proj (res->start, mode_P_mach, pns_frame_base);
144 res->globals = new_Proj (res->start, mode_P_mach, pns_globals);
145 res->args = new_Proj (res->start, mode_T, pns_args);
147 res->graph_nr = get_irp_new_node_nr();
151 add_in_edge(res->start_block, projX);
153 * The code generation needs it. leave it in now.
154 * Use of this edge is matter of discussion, unresolved. Also possible:
155 * add_in_edge(res->start_block, res->start_block), but invalid typed.
157 mature_block (res->current_block);
159 /*-- Make a block to start with --*/
160 first_block = new_immBlock ();
161 add_in_edge (first_block, projX);
168 /* Make a rudimentary ir graph for the constant code.
169 Must look like a correct irg, spare everything else. */
170 ir_graph *new_const_code_irg(void) {
174 res = (ir_graph *) malloc (sizeof(*res));
175 memset(res, 0, sizeof(*res));
177 /* inform statistics here, as blocks will be already build on this graph */
178 stat_new_graph(res, NULL);
180 current_ir_graph = res;
181 res->n_loc = 1; /* Only the memory. */
182 res->visited = 0; /* visited flag, for the ir walker */
183 res->block_visited=0; /* visited flag, for the 'block'-walker */
184 #if USE_EXPLICIT_PHI_IN_STACK
185 res->Phi_in_stack = NULL;
187 res->kind = k_ir_graph;
188 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
189 obstack_init (res->obst);
190 res->phase_state = phase_building;
191 res->pinned = pinned;
192 res->value_table = new_identities (); /* value table for global value
193 numbering for optimizing use in
196 res->frame_type = NULL;
197 res->start_block = new_immBlock ();
198 res->end_block = new_immBlock ();
199 res->end = new_End ();
200 res->end_reg = res->end;
201 res->end_except = res->end;
202 mature_block(get_cur_block());
203 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
204 /* res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL); */
205 res->start = new_Start ();
207 /* Proj results of start node */
208 projX = new_Proj (res->start, mode_X, pns_initial_exec);
209 set_store (new_Proj (res->start, mode_M, pns_global_store));
210 add_in_edge(res->start_block, projX);
211 mature_block (res->current_block);
212 add_in_edge (new_immBlock (), projX);
213 mature_block(get_cur_block());
214 /* Set the visited flag high enough that the block will never be visited. */
215 set_irn_visited(get_cur_block(), -1);
216 set_Block_block_visited(get_cur_block(), -1);
217 set_Block_block_visited(res->start_block, -1);
218 set_irn_visited(res->start_block, -1);
219 set_irn_visited(res->bad, -1);
223 /* Defined in iropt.c */
224 void del_identities (pset *value_table);
226 /* Frees the passed irgraph.
227 Deallocates all nodes in this graph and the ir_graph structure.
228 Sets the field irgraph in the corresponding entity to NULL.
229 Does not remove the irgraph from the list in irprog (requires
230 inefficient search, call remove_irp_irg by hand).
231 Does not free types, entities or modes that are used only by this
232 graph, nor the entity standing for this graph. */
233 void free_ir_graph (ir_graph *irg) {
234 stat_free_graph(irg);
235 if (irg->ent) set_entity_irg(irg->ent, NULL); /* not set in const code irg */
237 if (irg->frame_type) free_type(irg->frame_type);
238 if (irg->value_table) del_identities(irg->value_table);
239 if (irg->outs_state != no_outs) free_outs(irg);
240 obstack_free(irg->obst,NULL);
242 #if USE_EXPLICIT_PHI_IN_STACK
243 free_Phi_in_stack(irg->Phi_in_stack);
249 /* access routines for all ir_graph attributes:
251 {attr type} get_irg_{attribute name} (ir_graph *irg);
252 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
255 is_ir_graph(void *thing) {
257 if (get_kind(thing) == k_ir_graph)
263 /* Outputs a unique number for this node */
266 get_irg_graph_nr(ir_graph *irg) {
269 return irg->graph_nr;
276 (get_irg_start_block)(ir_graph *irg)
278 return __get_irg_start_block(irg);
282 (set_irg_start_block)(ir_graph *irg, ir_node *node)
284 __set_irg_start_block(irg, node);
288 (get_irg_start)(ir_graph *irg)
290 return __get_irg_start(irg);
294 (set_irg_start)(ir_graph *irg, ir_node *node)
296 __set_irg_start(irg, node);
300 (get_irg_end_block)(ir_graph *irg)
302 return __get_irg_end_block(irg);
306 (set_irg_end_block)(ir_graph *irg, ir_node *node)
308 __set_irg_end_block(irg, node);
312 (get_irg_end)(ir_graph *irg)
314 return __get_irg_end(irg);
318 (set_irg_end)(ir_graph *irg, ir_node *node)
320 __set_irg_end(irg, node);
324 (get_irg_end_reg)(ir_graph *irg) {
325 return __get_irg_end_reg(irg);
328 void set_irg_end_reg (ir_graph *irg, ir_node *node) {
329 assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
334 (get_irg_end_except)(ir_graph *irg) {
335 return __get_irg_end_except(irg);
338 void set_irg_end_except (ir_graph *irg, ir_node *node) {
339 assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
340 irg->end_except = node;
344 (get_irg_cstore)(ir_graph *irg)
346 return __get_irg_cstore(irg);
350 (set_irg_cstore)(ir_graph *irg, ir_node *node)
352 __set_irg_cstore(irg, node);
356 (get_irg_frame)(ir_graph *irg)
358 return __get_irg_frame(irg);
362 (set_irg_frame)(ir_graph *irg, ir_node *node)
364 __set_irg_frame(irg, node);
368 (get_irg_globals)(ir_graph *irg)
370 return __get_irg_globals(irg);
374 (set_irg_globals)(ir_graph *irg, ir_node *node)
376 __set_irg_globals(irg, node);
380 (get_irg_args)(ir_graph *irg)
382 return __get_irg_args(irg);
386 (set_irg_args)(ir_graph *irg, ir_node *node)
388 __set_irg_args(irg, node);
392 (get_irg_bad)(ir_graph *irg)
394 return __get_irg_bad(irg);
398 (set_irg_bad)(ir_graph *irg, ir_node *node)
400 __set_irg_bad(irg, node);
403 /* GL removed: we need unknown with mode for analyses.
405 get_irg_unknown (ir_graph *irg)
411 set_irg_unknown (ir_graph *irg, ir_node *node)
418 (get_irg_current_block)(ir_graph *irg)
420 return __get_irg_current_block(irg);
424 (set_irg_current_block)(ir_graph *irg, ir_node *node)
426 __set_irg_current_block(irg, node);
430 (get_irg_ent)(ir_graph *irg)
432 return __get_irg_ent(irg);
436 (set_irg_ent)(ir_graph *irg, entity *ent)
438 __set_irg_ent(irg, ent);
442 (get_irg_frame_type)(ir_graph *irg)
444 return __get_irg_frame_type(irg);
448 (set_irg_frame_type)(ir_graph *irg, type *ftp)
450 __set_irg_frame_type(irg, ftp);
454 /* To test for a frame type */
456 is_frame_type(type *ftp) {
458 if (is_class_type(ftp)) {
459 for (i = 0; i < get_irp_n_irgs(); i++) {
460 type *frame_tp = get_irg_frame_type(get_irp_irg(i));
461 if (ftp == frame_tp) return true;
468 get_irg_n_locs (ir_graph *irg)
470 #if PRECISE_EXC_CONTEXT
471 return irg->n_loc - 1 - 1;
473 return irg->n_loc - 1;
478 set_irg_n_loc (ir_graph *irg, int n_loc)
480 #if PRECISE_EXC_CONTEXT
481 irg->n_loc = n_loc + 1 + 1;
483 irg->n_loc = n_loc + 1;
489 /* Returns the obstack associated with the graph. */
491 (get_irg_obstack)(ir_graph *irg) {
492 return __get_irg_obstack(irg);
496 * Returns true if the node n is allocated on the storage of graph irg.
498 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
500 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
502 struct _obstack_chunk *p;
505 * checks wheater the ir_node pointer i on the obstack.
506 * A more sophisticated check would test the "whole" ir_node
508 for (p = irg->obst->chunk; p; p = p->prev) {
509 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
517 get_irg_phase_state (ir_graph *irg) {
518 return irg->phase_state;
522 set_irg_phase_low(ir_graph *irg) {
523 irg->phase_state = phase_low;
527 get_irg_pinned (ir_graph *irg) {
532 get_irg_outs_state(ir_graph *irg) {
533 return irg->outs_state;
537 set_irg_outs_inconsistent(ir_graph *irg) {
538 irg->outs_state = outs_inconsistent;
542 get_irg_dom_state(ir_graph *irg) {
543 return irg->dom_state;
547 set_irg_dom_inconsistent(ir_graph *irg) {
548 irg->dom_state = dom_inconsistent;
552 get_irg_loopinfo_state(ir_graph *irg) {
553 return irg->loopinfo_state;
556 void set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
557 irg->loopinfo_state = s;
561 set_irg_loopinfo_inconsistent(ir_graph *irg) {
562 if (irg->loopinfo_state == loopinfo_ip_consistent)
563 irg->loopinfo_state = loopinfo_ip_inconsistent;
565 irg->loopinfo_state = loopinfo_inconsistent;
569 set_irg_pinned (ir_graph *irg, op_pinned p) {
574 irg_callee_info_state get_irg_callee_info_state(ir_graph *irg) {
575 return irg->callee_info_state;
578 void set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
579 irg->callee_info_state = s;
582 irg_inline_property get_irg_inline_property(ir_graph *irg) {
583 return irg->inline_property;
585 void set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
586 irg->inline_property = s;
591 (set_irg_link)(ir_graph *irg, void *thing) {
592 __set_irg_link(irg, thing);
596 (get_irg_link)(ir_graph *irg) {
597 return __get_irg_link(irg);
600 /* maximum visited flag content of all ir_graph visited fields. */
601 static int max_irg_visited = 0;
604 (get_irg_visited)(ir_graph *irg)
606 return __get_irg_visited(irg);
610 set_irg_visited (ir_graph *irg, unsigned long visited)
612 irg->visited = visited;
613 if (irg->visited > max_irg_visited) {
614 max_irg_visited = irg->visited;
619 inc_irg_visited (ir_graph *irg)
621 if (++irg->visited > max_irg_visited) {
622 max_irg_visited = irg->visited;
627 get_max_irg_visited(void)
631 for(i = 0; i < get_irp_n_irgs(); i++)
632 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
634 return max_irg_visited;
637 void set_max_irg_visited(int val) {
638 max_irg_visited = val;
642 inc_max_irg_visited(void)
646 for(i = 0; i < get_irp_n_irgs(); i++)
647 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
650 return max_irg_visited;
654 (get_irg_block_visited)(ir_graph *irg)
656 return __get_irg_block_visited(irg);
660 (set_irg_block_visited)(ir_graph *irg, unsigned long visited)
662 __set_irg_block_visited(irg, visited);
666 (inc_irg_block_visited)(ir_graph *irg)
668 __inc_irg_block_visited(irg);