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"
28 ir_graph *current_ir_graph;
29 INLINE ir_graph *get_current_ir_graph(void) {
30 return current_ir_graph;
32 INLINE void set_current_ir_graph(ir_graph *graph) {
33 current_ir_graph = graph;
37 bool interprocedural_view = false;
38 INLINE bool get_interprocedural_view(void) {
39 return interprocedural_view;
41 INLINE void set_interprocedural_view(bool state) {
42 interprocedural_view = state;
45 static ident* frame_type_suffix = NULL;
46 void init_irgraph(void) {
47 frame_type_suffix = id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX));
50 #if USE_EXPLICIT_PHI_IN_STACK
51 /* really defined in ircons.c */
52 typedef struct Phi_in_stack Phi_in_stack;
53 Phi_in_stack *new_Phi_in_stack();
54 void free_Phi_in_stack(Phi_in_stack *s);
57 /* Allocates a list of nodes:
58 - The start block containing a start node and Proj nodes for it's four
59 results (X, M, P, Tuple).
60 - The end block containing an end node. This block is not matured after
61 new_ir_graph as predecessors need to be added to it.
62 - The current block, which is empty and also not matured.
63 Further it allocates several datastructures needed for graph construction
67 new_ir_graph (entity *ent, int n_loc)
73 res = (ir_graph *) malloc (sizeof (ir_graph));
76 current_ir_graph = res;
77 add_irp_irg(res); /* remember this graph global. */
80 * initialized for each graph. **/
81 #if PRECISE_EXC_CONTEXT
82 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
83 dereferenced in this graph plus one for
84 the store plus one for links to fragile
85 operations. n_loc is not the number of
86 parameters to the procedure! */
88 res->n_loc = n_loc + 1; /* number of local variables that are never
89 dereferenced in this graph plus one for
90 the store. This is not the number of parameters
94 res->visited = 0; /* visited flag, for the ir walker */
95 res->block_visited=0; /* visited flag, for the 'block'-walker */
97 #if USE_EXPLICIT_PHI_IN_STACK
98 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
101 res->kind = k_ir_graph;
102 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
103 obstack_init (res->obst);
104 res->value_table = new_identities (); /* value table for global value
105 numbering for optimizing use in
109 res->phase_state = phase_building;
110 res->pinned = pinned;
111 res->outs_state = no_outs;
112 res->dom_state = no_dom;
113 res->typeinfo_state = irg_typeinfo_none;
115 /** Type information for the procedure of the graph **/
117 set_entity_irg(ent, res);
120 contain "inner" methods as in Pascal. **/
121 res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
122 /* Remove type from type list. Must be treated differently than other types. */
123 remove_irp_type_from_list(res->frame_type);
125 /** Nodes needed in every graph **/
126 res->end_block = new_immBlock ();
127 res->end = new_End ();
129 res->start_block = new_immBlock ();
130 res->start = new_Start ();
131 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
132 //res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
134 /* Proj results of start node */
135 projX = new_Proj (res->start, mode_X, pns_initial_exec);
136 set_store (new_Proj (res->start, mode_M, pns_global_store));
137 res->frame = new_Proj (res->start, mode_P_mach, pns_frame_base);
138 res->globals = new_Proj (res->start, mode_P_mach, pns_globals);
139 res->args = new_Proj (res->start, mode_T, pns_args);
141 res->graph_nr = get_irp_new_node_nr();
145 add_in_edge(res->start_block, projX);
147 * The code generation needs it. leave it in now.
148 * Use of this edge is matter of discussion, unresolved. Also possible:
149 * add_in_edge(res->start_block, res->start_block), but invalid typed.
151 mature_block (res->current_block);
153 /** Make a block to start with **/
154 first_block = new_immBlock ();
155 add_in_edge (first_block, projX);
161 /* Make a rudimentary ir graph for the constant code.
162 Must look like a correct irg, spare everything else. */
163 ir_graph *new_const_code_irg(void) {
167 res = (ir_graph *) malloc (sizeof(*res));
168 memset(res, 0, sizeof(*res));
170 current_ir_graph = res;
171 res->n_loc = 1; /* Only the memory. */
172 res->visited = 0; /* visited flag, for the ir walker */
173 res->block_visited=0; /* visited flag, for the 'block'-walker */
174 #if USE_EXPLICIT_PHI_IN_STACK
175 res->Phi_in_stack = NULL;
177 res->kind = k_ir_graph;
178 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
179 obstack_init (res->obst);
180 res->phase_state = phase_building;
181 res->pinned = pinned;
182 res->value_table = new_identities (); /* value table for global value
183 numbering for optimizing use in
186 res->frame_type = NULL;
187 res->start_block = new_immBlock ();
188 res->end_block = new_immBlock ();
189 res->end = new_End ();
190 mature_block(get_cur_block());
191 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
192 //res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
193 res->start = new_Start ();
195 /* Proj results of start node */
196 projX = new_Proj (res->start, mode_X, pns_initial_exec);
197 set_store (new_Proj (res->start, mode_M, pns_global_store));
198 add_in_edge(res->start_block, projX);
199 mature_block (res->current_block);
200 add_in_edge (new_immBlock (), projX);
201 mature_block(get_cur_block());
202 /* Set the visited flag high enough that the block will never be visited. */
203 set_irn_visited(get_cur_block(), -1);
204 set_Block_block_visited(get_cur_block(), -1);
205 set_Block_block_visited(res->start_block, -1);
206 set_irn_visited(res->start_block, -1);
207 set_irn_visited(res->bad, -1);
211 /* Defined in iropt.c */
212 void del_identities (pset *value_table);
214 /* Frees the passed irgraph.
215 Deallocates all nodes in this graph and the ir_graph structure.
216 Sets the field irgraph in the corresponding entity to NULL.
217 Does not remove the irgraph from the list in irprog (requires
218 inefficient search, call remove_irp_irg by hand).
219 Does not free types, entities or modes that are used only by this
220 graph, nor the entity standing for this graph. */
221 void free_ir_graph (ir_graph *irg) {
222 if (irg->ent) set_entity_irg(irg->ent, NULL); /* not set in const code irg */
224 if (irg->frame_type) free_type(irg->frame_type);
225 if (irg->value_table) del_identities(irg->value_table);
226 if (irg->outs_state != no_outs) free_outs(irg);
228 #if USE_EXPLICIT_PHI_IN_STACK
229 free_Phi_in_stack(irg->Phi_in_stack);
235 /* access routines for all ir_graph attributes:
237 {attr type} get_irg_{attribute name} (ir_graph *irg);
238 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
241 is_ir_graph(void *thing) {
243 if (get_kind(thing) == k_ir_graph)
249 /* Outputs a unique number for this node */
252 get_irg_graph_nr(ir_graph *irg) {
255 return irg->graph_nr;
262 get_irg_start_block (ir_graph *irg)
264 return irg->start_block;
268 set_irg_start_block (ir_graph *irg, ir_node *node)
270 irg->start_block = node;
274 get_irg_start (ir_graph *irg)
280 set_irg_start(ir_graph *irg, ir_node *node)
286 get_irg_end_block (ir_graph *irg)
288 return irg->end_block;
292 set_irg_end_block (ir_graph *irg, ir_node *node)
294 irg->end_block = node;
298 get_irg_end (ir_graph *irg)
304 set_irg_end (ir_graph *irg, ir_node *node)
310 get_irg_cstore (ir_graph *irg)
316 set_irg_cstore (ir_graph *irg, ir_node *node)
322 get_irg_frame (ir_graph *irg)
328 set_irg_frame (ir_graph *irg, ir_node *node)
334 get_irg_globals (ir_graph *irg)
340 set_irg_globals (ir_graph *irg, ir_node *node)
346 get_irg_args (ir_graph *irg)
352 set_irg_args (ir_graph *irg, ir_node *node)
358 get_irg_bad (ir_graph *irg)
364 set_irg_bad (ir_graph *irg, ir_node *node)
369 /* GL removed: we need unknown with mode for analyses.
371 get_irg_unknown (ir_graph *irg)
377 set_irg_unknown (ir_graph *irg, ir_node *node)
384 get_irg_current_block (ir_graph *irg)
386 return irg->current_block;
390 set_irg_current_block (ir_graph *irg, ir_node *node)
392 irg->current_block = node;
396 get_irg_ent (ir_graph *irg)
398 assert(irg && irg->ent);
403 set_irg_ent (ir_graph *irg, entity *ent)
409 get_irg_frame_type (ir_graph *irg)
411 assert(irg && irg->frame_type);
412 return irg->frame_type;
416 set_irg_frame_type (ir_graph *irg, type *ftp)
418 assert(is_class_type(ftp));
419 irg->frame_type = ftp;
423 /* To test for a frame type */
425 is_frame_type(type *ftp) {
427 if (is_class_type(ftp)) {
428 for (i = 0; i < get_irp_n_irgs(); i++) {
429 type *frame_tp = get_irg_frame_type(get_irp_irg(i));
430 if (ftp == frame_tp) return true;
437 get_irg_n_locs (ir_graph *irg)
439 #if PRECISE_EXC_CONTEXT
440 return irg->n_loc - 1 - 1;
442 return irg->n_loc - 1;
447 set_irg_n_loc (ir_graph *irg, int n_loc)
449 #if PRECISE_EXC_CONTEXT
450 irg->n_loc = n_loc + 1 + 1;
452 irg->n_loc = n_loc + 1;
458 /* Returns the obstack associated with the graph. */
459 struct obstack *get_irg_obstack(ir_graph *irg) {
464 * Returns true if the node n is allocated on the storage of graph irg.
466 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
468 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
470 struct _obstack_chunk *p;
473 * checks wheater the ir_node pointer i on the obstack.
474 * A more sophisticated check would test the "whole" ir_node
476 for (p = irg->obst->chunk; p; p = p->prev) {
477 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
485 get_irg_phase_state (ir_graph *irg) {
486 return irg->phase_state;
490 set_irg_phase_low(ir_graph *irg) {
491 irg->phase_state = phase_low;
495 get_irg_pinned (ir_graph *irg) {
500 get_irg_outs_state(ir_graph *irg) {
501 return irg->outs_state;
505 set_irg_outs_inconsistent(ir_graph *irg) {
506 irg->outs_state = outs_inconsistent;
510 get_irg_dom_state(ir_graph *irg) {
511 return irg->dom_state;
515 set_irg_dom_inconsistent(ir_graph *irg) {
516 irg->dom_state = dom_inconsistent;
520 get_irg_loopinfo_state(ir_graph *irg) {
521 assert(0 && "not implemented");
526 set_irg_loopinfo_inconsistent(ir_graph *irg) {
527 assert(0 && "not implemented");
531 set_irg_pinned (ir_graph *irg, op_pinned p) {
536 set_irg_link (ir_graph *irg, void *thing) {
541 get_irg_link (ir_graph *irg) {
545 /* maximum visited flag content of all ir_graph visited fields. */
546 static int max_irg_visited = 0;
549 get_irg_visited (ir_graph *irg)
555 set_irg_visited (ir_graph *irg, unsigned long visited)
557 irg->visited = visited;
558 if (irg->visited > max_irg_visited) {
559 max_irg_visited = irg->visited;
564 inc_irg_visited (ir_graph *irg)
566 if (++irg->visited > max_irg_visited) {
567 max_irg_visited = irg->visited;
572 get_max_irg_visited(void)
576 for(i = 0; i < get_irp_n_irgs(); i++)
577 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
579 return max_irg_visited;
582 void set_max_irg_visited(int val) {
583 max_irg_visited = val;
587 inc_max_irg_visited(void)
591 for(i = 0; i < get_irp_n_irgs(); i++)
592 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
595 return max_irg_visited;
599 get_irg_block_visited (ir_graph *irg)
601 return irg->block_visited;
605 set_irg_block_visited (ir_graph *irg, unsigned long visited)
607 irg->block_visited = visited;
611 inc_irg_block_visited (ir_graph *irg)
613 ++irg->block_visited;