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);
227 obstack_free(irg->obst,NULL);
229 #if USE_EXPLICIT_PHI_IN_STACK
230 free_Phi_in_stack(irg->Phi_in_stack);
236 /* access routines for all ir_graph attributes:
238 {attr type} get_irg_{attribute name} (ir_graph *irg);
239 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
242 is_ir_graph(void *thing) {
244 if (get_kind(thing) == k_ir_graph)
250 /* Outputs a unique number for this node */
253 get_irg_graph_nr(ir_graph *irg) {
256 return irg->graph_nr;
263 get_irg_start_block (ir_graph *irg)
265 return irg->start_block;
269 set_irg_start_block (ir_graph *irg, ir_node *node)
271 irg->start_block = node;
275 get_irg_start (ir_graph *irg)
281 set_irg_start(ir_graph *irg, ir_node *node)
287 get_irg_end_block (ir_graph *irg)
289 return irg->end_block;
293 set_irg_end_block (ir_graph *irg, ir_node *node)
295 irg->end_block = node;
299 get_irg_end (ir_graph *irg)
305 set_irg_end (ir_graph *irg, ir_node *node)
311 get_irg_cstore (ir_graph *irg)
317 set_irg_cstore (ir_graph *irg, ir_node *node)
323 get_irg_frame (ir_graph *irg)
329 set_irg_frame (ir_graph *irg, ir_node *node)
335 get_irg_globals (ir_graph *irg)
341 set_irg_globals (ir_graph *irg, ir_node *node)
347 get_irg_args (ir_graph *irg)
353 set_irg_args (ir_graph *irg, ir_node *node)
359 get_irg_bad (ir_graph *irg)
365 set_irg_bad (ir_graph *irg, ir_node *node)
370 /* GL removed: we need unknown with mode for analyses.
372 get_irg_unknown (ir_graph *irg)
378 set_irg_unknown (ir_graph *irg, ir_node *node)
385 get_irg_current_block (ir_graph *irg)
387 return irg->current_block;
391 set_irg_current_block (ir_graph *irg, ir_node *node)
393 irg->current_block = node;
397 get_irg_ent (ir_graph *irg)
399 assert(irg && irg->ent);
404 set_irg_ent (ir_graph *irg, entity *ent)
410 get_irg_frame_type (ir_graph *irg)
412 assert(irg && irg->frame_type);
413 return irg->frame_type;
417 set_irg_frame_type (ir_graph *irg, type *ftp)
419 assert(is_class_type(ftp));
420 irg->frame_type = ftp;
424 /* To test for a frame type */
426 is_frame_type(type *ftp) {
428 if (is_class_type(ftp)) {
429 for (i = 0; i < get_irp_n_irgs(); i++) {
430 type *frame_tp = get_irg_frame_type(get_irp_irg(i));
431 if (ftp == frame_tp) return true;
438 get_irg_n_locs (ir_graph *irg)
440 #if PRECISE_EXC_CONTEXT
441 return irg->n_loc - 1 - 1;
443 return irg->n_loc - 1;
448 set_irg_n_loc (ir_graph *irg, int n_loc)
450 #if PRECISE_EXC_CONTEXT
451 irg->n_loc = n_loc + 1 + 1;
453 irg->n_loc = n_loc + 1;
459 /* Returns the obstack associated with the graph. */
460 struct obstack *get_irg_obstack(ir_graph *irg) {
465 * Returns true if the node n is allocated on the storage of graph irg.
467 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
469 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
471 struct _obstack_chunk *p;
474 * checks wheater the ir_node pointer i on the obstack.
475 * A more sophisticated check would test the "whole" ir_node
477 for (p = irg->obst->chunk; p; p = p->prev) {
478 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
486 get_irg_phase_state (ir_graph *irg) {
487 return irg->phase_state;
491 set_irg_phase_low(ir_graph *irg) {
492 irg->phase_state = phase_low;
496 get_irg_pinned (ir_graph *irg) {
501 get_irg_outs_state(ir_graph *irg) {
502 return irg->outs_state;
506 set_irg_outs_inconsistent(ir_graph *irg) {
507 irg->outs_state = outs_inconsistent;
511 get_irg_dom_state(ir_graph *irg) {
512 return irg->dom_state;
516 set_irg_dom_inconsistent(ir_graph *irg) {
517 irg->dom_state = dom_inconsistent;
521 get_irg_loopinfo_state(ir_graph *irg) {
522 assert(0 && "not implemented");
527 set_irg_loopinfo_inconsistent(ir_graph *irg) {
528 assert(0 && "not implemented");
532 set_irg_pinned (ir_graph *irg, op_pinned p) {
537 set_irg_link (ir_graph *irg, void *thing) {
542 get_irg_link (ir_graph *irg) {
546 /* maximum visited flag content of all ir_graph visited fields. */
547 static int max_irg_visited = 0;
550 get_irg_visited (ir_graph *irg)
556 set_irg_visited (ir_graph *irg, unsigned long visited)
558 irg->visited = visited;
559 if (irg->visited > max_irg_visited) {
560 max_irg_visited = irg->visited;
565 inc_irg_visited (ir_graph *irg)
567 if (++irg->visited > max_irg_visited) {
568 max_irg_visited = irg->visited;
573 get_max_irg_visited(void)
577 for(i = 0; i < get_irp_n_irgs(); i++)
578 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
580 return max_irg_visited;
583 void set_max_irg_visited(int val) {
584 max_irg_visited = val;
588 inc_max_irg_visited(void)
592 for(i = 0; i < get_irp_n_irgs(); i++)
593 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
596 return max_irg_visited;
600 get_irg_block_visited (ir_graph *irg)
602 return irg->block_visited;
606 set_irg_block_visited (ir_graph *irg, unsigned long visited)
608 irg->block_visited = visited;
612 inc_irg_block_visited (ir_graph *irg)
614 ++irg->block_visited;