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 ir_graph *current_ir_graph;
28 INLINE ir_graph *get_current_ir_graph(void) {
29 return current_ir_graph;
31 INLINE void set_current_ir_graph(ir_graph *graph) {
32 current_ir_graph = graph;
36 bool interprocedural_view = false;
37 INLINE bool get_interprocedural_view(void) {
38 return interprocedural_view;
40 INLINE void set_interprocedural_view(bool state) {
41 interprocedural_view = state;
44 static ident* frame_type_suffix = NULL;
45 void init_irgraph(void) {
46 frame_type_suffix = id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX));
49 #if USE_EXPLICIT_PHI_IN_STACK
50 /* really defined in ircons.c */
51 typedef struct Phi_in_stack Phi_in_stack;
52 Phi_in_stack *new_Phi_in_stack();
53 void free_Phi_in_stack(Phi_in_stack *s);
56 /* Allocates a list of nodes:
57 - The start block containing a start node and Proj nodes for it's four
58 results (X, M, P, Tuple).
59 - The end block containing an end node. This block is not matured after
60 new_ir_graph as predecessors need to be added to it.
61 - The current block, which is empty and also not matured.
62 Further it allocates several datastructures needed for graph construction
66 new_ir_graph (entity *ent, int n_loc)
72 res = (ir_graph *) malloc (sizeof (ir_graph));
75 current_ir_graph = res;
76 add_irp_irg(res); /* remember this graph global. */
79 * initialized for each graph. **/
80 #if PRECISE_EXC_CONTEXT
81 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
82 dereferenced in this graph plus one for
83 the store plus one for links to fragile
84 operations. n_loc is not the number of
85 parameters to the procedure! */
87 res->n_loc = n_loc + 1; /* number of local variables that are never
88 dereferenced in this graph plus one for
89 the store. This is not the number of parameters
93 res->visited = 0; /* visited flag, for the ir walker */
94 res->block_visited=0; /* visited flag, for the 'block'-walker */
96 #if USE_EXPLICIT_PHI_IN_STACK
97 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
100 res->kind = k_ir_graph;
101 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
102 obstack_init (res->obst);
103 res->value_table = new_identities (); /* value table for global value
104 numbering for optimizing use in
108 res->phase_state = phase_building;
109 res->pinned = pinned;
110 res->outs_state = no_outs;
111 res->dom_state = no_dom;
112 res->typeinfo_state = irg_typeinfo_none;
114 /** Type information for the procedure of the graph **/
116 set_entity_irg(ent, res);
119 contain "inner" methods as in Pascal. **/
120 res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
121 /* Remove type from type list. Must be treated differently than other types. */
122 remove_irp_type_from_list(res->frame_type);
124 /** Nodes needed in every graph **/
125 res->end_block = new_immBlock ();
126 res->end = new_End ();
128 res->start_block = new_immBlock ();
129 res->start = new_Start ();
130 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
131 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
133 /* Proj results of start node */
134 projX = new_Proj (res->start, mode_X, pns_initial_exec);
135 set_store (new_Proj (res->start, mode_M, pns_global_store));
136 res->frame = new_Proj (res->start, mode_P_mach, pns_frame_base);
137 res->globals = new_Proj (res->start, mode_P_mach, pns_globals);
138 res->args = new_Proj (res->start, mode_T, pns_args);
140 res->graph_nr = get_irp_new_node_nr();
144 add_in_edge(res->start_block, projX);
146 * The code generation needs it. leave it in now.
147 * Use of this edge is matter of discussion, unresolved. Also possible:
148 * add_in_edge(res->start_block, res->start_block), but invalid typed.
150 mature_block (res->current_block);
152 /** Make a block to start with **/
153 first_block = new_immBlock ();
154 add_in_edge (first_block, projX);
160 /* Make a rudimentary ir graph for the constant code.
161 Must look like a correct irg, spare everything else. */
162 ir_graph *new_const_code_irg(void) {
166 res = (ir_graph *) malloc (sizeof(*res));
167 memset(res, 0, sizeof(*res));
169 current_ir_graph = res;
170 res->n_loc = 1; /* Only the memory. */
171 res->visited = 0; /* visited flag, for the ir walker */
172 res->block_visited=0; /* visited flag, for the 'block'-walker */
173 #if USE_EXPLICIT_PHI_IN_STACK
174 res->Phi_in_stack = NULL;
176 res->kind = k_ir_graph;
177 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
178 obstack_init (res->obst);
179 res->phase_state = phase_building;
180 res->pinned = pinned;
181 res->value_table = new_identities (); /* value table for global value
182 numbering for optimizing use in
185 res->frame_type = NULL;
186 res->start_block = new_immBlock ();
187 res->end_block = new_immBlock ();
188 res->end = new_End ();
189 mature_block(get_cur_block());
190 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
191 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
192 res->start = new_Start ();
194 /* Proj results of start node */
195 projX = new_Proj (res->start, mode_X, pns_initial_exec);
196 set_store (new_Proj (res->start, mode_M, pns_global_store));
197 add_in_edge(res->start_block, projX);
198 mature_block (res->current_block);
199 add_in_edge (new_immBlock (), projX);
200 mature_block(get_cur_block());
201 /* Set the visited flag high enough that the block will never be visited. */
202 set_irn_visited(get_cur_block(), -1);
203 set_Block_block_visited(get_cur_block(), -1);
204 set_Block_block_visited(res->start_block, -1);
205 set_irn_visited(res->start_block, -1);
206 set_irn_visited(res->bad, -1);
210 /* Frees the passed irgraph.
211 Deallocates all nodes in this graph and the ir_graph structure.
212 Sets the field irgraph in the corresponding entity to NULL.
213 Does not remove the irgraph from the list in irprog (requires
214 inefficient search, call remove_irp_irg by hand).
215 Does not free types, entities or modes that are used only by this
216 graph, nor the entity standing for this graph. */
217 void free_ir_graph (ir_graph *irg) {
218 set_entity_irg(irg->ent, NULL);
221 #if USE_EXPLICIT_PHI_IN_STACK
222 free_Phi_in_stack(irg->Phi_in_stack);
227 /* access routines for all ir_graph attributes:
229 {attr type} get_irg_{attribute name} (ir_graph *irg);
230 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
233 is_ir_graph(void *thing) {
235 if (get_kind(thing) == k_ir_graph)
241 /* Outputs a unique number for this node */
244 get_irg_graph_nr(ir_graph *irg) {
247 return irg->graph_nr;
254 get_irg_start_block (ir_graph *irg)
256 return irg->start_block;
260 set_irg_start_block (ir_graph *irg, ir_node *node)
262 irg->start_block = node;
266 get_irg_start (ir_graph *irg)
272 set_irg_start(ir_graph *irg, ir_node *node)
278 get_irg_end_block (ir_graph *irg)
280 return irg->end_block;
284 set_irg_end_block (ir_graph *irg, ir_node *node)
286 irg->end_block = node;
290 get_irg_end (ir_graph *irg)
296 set_irg_end (ir_graph *irg, ir_node *node)
302 get_irg_cstore (ir_graph *irg)
308 set_irg_cstore (ir_graph *irg, ir_node *node)
314 get_irg_frame (ir_graph *irg)
320 set_irg_frame (ir_graph *irg, ir_node *node)
326 get_irg_globals (ir_graph *irg)
332 set_irg_globals (ir_graph *irg, ir_node *node)
338 get_irg_args (ir_graph *irg)
344 set_irg_args (ir_graph *irg, ir_node *node)
350 get_irg_bad (ir_graph *irg)
356 set_irg_bad (ir_graph *irg, ir_node *node)
362 get_irg_unknown (ir_graph *irg)
368 set_irg_unknown (ir_graph *irg, ir_node *node)
374 get_irg_current_block (ir_graph *irg)
376 return irg->current_block;
380 set_irg_current_block (ir_graph *irg, ir_node *node)
382 irg->current_block = node;
386 get_irg_ent (ir_graph *irg)
388 assert(irg && irg->ent);
393 set_irg_ent (ir_graph *irg, entity *ent)
399 get_irg_frame_type (ir_graph *irg)
401 assert(irg && irg->frame_type);
402 return irg->frame_type;
406 set_irg_frame_type (ir_graph *irg, type *ftp)
408 assert(is_class_type(ftp));
409 irg->frame_type = ftp;
413 /* To test for a frame type */
415 is_frame_type(type *ftp) {
417 if (is_class_type(ftp)) {
418 for (i = 0; i < get_irp_n_irgs(); i++) {
419 type *frame_tp = get_irg_frame_type(get_irp_irg(i));
420 if (ftp == frame_tp) return true;
427 get_irg_n_locs (ir_graph *irg)
429 #if PRECISE_EXC_CONTEXT
430 return irg->n_loc - 1 - 1;
432 return irg->n_loc - 1;
437 set_irg_n_loc (ir_graph *irg, int n_loc)
439 #if PRECISE_EXC_CONTEXT
440 irg->n_loc = n_loc + 1 + 1;
442 irg->n_loc = n_loc + 1;
448 /* Returns the obstack associated with the graph. */
449 struct obstack *get_irg_obstack(ir_graph *irg) {
454 * Returns true if the node n is allocated on the storage of graph irg.
456 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
458 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
460 struct _obstack_chunk *p;
463 * checks wheater the ir_node pointer i on the obstack.
464 * A more sophisticated chaeck would test the "whole" ir_node
466 for (p = irg->obst->chunk; p; p = p->prev) {
467 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
475 get_irg_phase_state (ir_graph *irg) {
476 return irg->phase_state;
480 set_irg_phase_low(ir_graph *irg) {
481 irg->phase_state = phase_low;
485 get_irg_pinned (ir_graph *irg) {
490 get_irg_outs_state(ir_graph *irg) {
491 return irg->outs_state;
495 set_irg_outs_inconsistent(ir_graph *irg) {
496 irg->outs_state = outs_inconsistent;
500 get_irg_dom_state(ir_graph *irg) {
501 return irg->dom_state;
505 set_irg_dom_inconsistent(ir_graph *irg) {
506 irg->dom_state = dom_inconsistent;
510 get_irg_loopinfo_state(ir_graph *irg) {
511 assert(0 && "not implemented");
516 set_irg_loopinfo_inconsistent(ir_graph *irg) {
517 assert(0 && "not implemented");
521 set_irg_pinned (ir_graph *irg, op_pinned p) {
526 set_irg_link (ir_graph *irg, void *thing) {
531 get_irg_link (ir_graph *irg) {
535 /* maximum visited flag content of all ir_graph visited fields. */
536 static int max_irg_visited = 0;
539 get_irg_visited (ir_graph *irg)
545 set_irg_visited (ir_graph *irg, unsigned long visited)
547 irg->visited = visited;
548 if (irg->visited > max_irg_visited) {
549 max_irg_visited = irg->visited;
554 inc_irg_visited (ir_graph *irg)
556 if (++irg->visited > max_irg_visited) {
557 max_irg_visited = irg->visited;
562 get_max_irg_visited(void)
566 for(i = 0; i < get_irp_n_irgs(); i++)
567 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
569 return max_irg_visited;
572 void set_max_irg_visited(int val) {
573 max_irg_visited = val;
577 inc_max_irg_visited(void)
581 for(i = 0; i < get_irp_n_irgs(); i++)
582 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
585 return max_irg_visited;
589 get_irg_block_visited (ir_graph *irg)
591 return irg->block_visited;
595 set_irg_block_visited (ir_graph *irg, unsigned long visited)
597 irg->block_visited = visited;
601 inc_irg_block_visited (ir_graph *irg)
603 ++irg->block_visited;