1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
4 * Authors: Martin Trapp, Christian Schaefer
18 # include "irgraph_t.h"
19 # include "irprog_t.h"
25 ir_graph *current_ir_graph;
26 INLINE ir_graph *get_current_ir_graph(void) {
27 return current_ir_graph;
29 INLINE void set_current_ir_graph(ir_graph *graph) {
30 current_ir_graph = graph;
34 bool interprocedural_view = false;
35 INLINE bool get_interprocedural_view(void) {
36 return interprocedural_view;
38 INLINE void set_interprocedural_view(bool state) {
39 interprocedural_view = state;
42 static ident* frame_type_suffix = NULL;
43 void init_irgraph(void) {
44 frame_type_suffix = id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX));
47 #if USE_EXPLICIT_PHI_IN_STACK
48 /* really defined in ircons.c */
49 typedef struct Phi_in_stack Phi_in_stack;
50 Phi_in_stack *new_Phi_in_stack();
51 void free_Phi_in_stack(Phi_in_stack *s);
54 /* Allocates a list of nodes:
55 - The start block containing a start node and Proj nodes for it's four
56 results (X, M, P, Tuple).
57 - The end block containing an end node. This block is not matured after
58 new_ir_graph as predecessors need to be added to it.
59 - The current block, which is empty and also not matured.
60 Further it allocates several datastructures needed for graph construction
64 new_ir_graph (entity *ent, int n_loc)
70 res = (ir_graph *) malloc (sizeof (ir_graph));
73 current_ir_graph = res;
74 add_irp_irg(res); /* remember this graph global. */
77 * initialized for each graph. **/
78 #if PRECISE_EXC_CONTEXT
79 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
80 dereferenced in this graph plus one for
81 the store plus one for links to fragile
82 operations. n_loc is not the number of
83 parameters to the procedure! */
85 res->n_loc = n_loc + 1; /* number of local variables that are never
86 dereferenced in this graph plus one for
87 the store. This is not the number of parameters
91 res->visited = 0; /* visited flag, for the ir walker */
92 res->block_visited=0; /* visited flag, for the 'block'-walker */
94 #if USE_EXPLICIT_PHI_IN_STACK
95 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
98 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
99 obstack_init (res->obst);
100 res->value_table = new_identities (); /* value table for global value
101 numbering for optimizing use in
105 res->phase_state = phase_building;
106 res->pinned = pinned;
107 res->outs_state = no_outs;
108 res->dom_state = no_dom;
110 /** Type information for the procedure of the graph **/
112 set_entity_irg(ent, res);
115 contain "inner" methods as in Pascal. **/
116 res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
117 /* Remove type from type list. Must be treated differently than other types. */
118 remove_irp_type_from_list(res->frame_type);
120 /** Nodes needed in every graph **/
121 res->end_block = new_immBlock ();
122 res->end = new_End ();
124 res->start_block = new_immBlock ();
125 res->start = new_Start ();
126 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
127 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
129 /* Proj results of start node */
130 projX = new_Proj (res->start, mode_X, pns_initial_exec);
131 set_store (new_Proj (res->start, mode_M, pns_global_store));
132 res->frame = new_Proj (res->start, mode_P, pns_frame_base);
133 res->globals = new_Proj (res->start, mode_P, pns_globals);
134 res->args = new_Proj (res->start, mode_T, pns_args);
136 res->graph_nr = get_irp_new_node_nr();
140 add_in_edge(res->start_block, projX);
142 * The code generation needs it. leave it in now.
143 * Use of this edge is matter of discussion, unresolved. Also possible:
144 * add_in_edge(res->start_block, res->start_block), but invalid typed.
146 mature_block (res->current_block);
148 /** Make a block to start with **/
149 first_block = new_immBlock ();
150 add_in_edge (first_block, projX);
156 /* Make a rudimentary ir graph for the constant code.
157 Must look like a correct irg, spare everything else. */
158 ir_graph *new_const_code_irg() {
162 res = (ir_graph *) malloc (sizeof (ir_graph));
163 current_ir_graph = res;
164 res->n_loc = 1; /* Only the memory. */
165 res->visited = 0; /* visited flag, for the ir walker */
166 res->block_visited=0; /* visited flag, for the 'block'-walker */
167 #if USE_EXPLICIT_PHI_IN_STACK
168 res->Phi_in_stack = NULL;
170 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
171 obstack_init (res->obst);
172 res->phase_state = phase_building;
173 res->pinned = pinned;
174 res->value_table = new_identities (); /* value table for global value
175 numbering for optimizing use in
178 res->frame_type = NULL;
179 res->start_block = new_immBlock ();
180 res->end_block = new_immBlock ();
181 res->end = new_End ();
182 mature_block(get_cur_block());
183 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
184 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
185 res->start = new_Start ();
186 /* Proj results of start node */
187 projX = new_Proj (res->start, mode_X, pns_initial_exec);
188 set_store (new_Proj (res->start, mode_M, pns_global_store));
189 add_in_edge(res->start_block, projX);
190 mature_block (res->current_block);
191 add_in_edge (new_immBlock (), projX);
192 mature_block(get_cur_block());
193 /* Set the visited flag high enough that the block will never be visited. */
194 set_irn_visited(get_cur_block(), -1);
195 set_Block_block_visited(get_cur_block(), -1);
196 set_Block_block_visited(res->start_block, -1);
200 /* Frees the passed irgraph.
201 Deallocates all nodes in this graph and the ir_graph structure.
202 Sets the field irgraph in the corresponding entity to NULL.
203 Does not remove the irgraph from the list in irprog (requires
204 inefficient search, call remove_irp_irg by hand).
205 Does not free types, entities or modes that are used only by this
206 graph, nor the entity standing for this graph. */
207 void free_ir_graph (ir_graph *irg) {
208 set_entity_irg(irg->ent, NULL);
210 #if USE_EXPLICIT_PHI_IN_STACK
211 free_Phi_in_stack(irg->Phi_in_stack);
216 /* access routines for all ir_graph attributes:
218 {attr type} get_irg_{attribute name} (ir_graph *irg);
219 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
221 /* Outputs a unique number for this node */
224 get_irg_graph_nr(ir_graph *irg) {
227 return irg->graph_nr;
234 get_irg_start_block (ir_graph *irg)
236 return irg->start_block;
240 set_irg_start_block (ir_graph *irg, ir_node *node)
242 irg->start_block = node;
246 get_irg_start (ir_graph *irg)
252 set_irg_start(ir_graph *irg, ir_node *node)
258 get_irg_end_block (ir_graph *irg)
260 return irg->end_block;
264 set_irg_end_block (ir_graph *irg, ir_node *node)
266 irg->end_block = node;
270 get_irg_end (ir_graph *irg)
276 set_irg_end (ir_graph *irg, ir_node *node)
282 get_irg_cstore (ir_graph *irg)
288 set_irg_cstore (ir_graph *irg, ir_node *node)
294 get_irg_frame (ir_graph *irg)
300 set_irg_frame (ir_graph *irg, ir_node *node)
306 get_irg_globals (ir_graph *irg)
312 set_irg_globals (ir_graph *irg, ir_node *node)
318 get_irg_args (ir_graph *irg)
324 set_irg_args (ir_graph *irg, ir_node *node)
330 get_irg_bad (ir_graph *irg)
336 set_irg_bad (ir_graph *irg, ir_node *node)
342 get_irg_unknown (ir_graph *irg)
348 set_irg_unknown (ir_graph *irg, ir_node *node)
354 get_irg_current_block (ir_graph *irg)
356 return irg->current_block;
360 set_irg_current_block (ir_graph *irg, ir_node *node)
362 irg->current_block = node;
366 get_irg_ent (ir_graph *irg)
368 assert(irg && irg->ent);
373 set_irg_ent (ir_graph *irg, entity *ent)
379 get_irg_frame_type (ir_graph *irg)
381 assert(irg && irg->frame_type);
382 return irg->frame_type;
386 set_irg_frame_type (ir_graph *irg, type *ftp)
388 irg->frame_type = ftp;
392 /* To test for a frame type */
394 is_frame_type(type *ftp) {
395 return ((is_class_type(ftp) || is_struct_type(ftp)) &&
396 id_is_suffix(id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX)),
397 get_type_ident(ftp)));
401 get_irg_n_locs (ir_graph *irg)
403 #if PRECISE_EXC_CONTEXT
404 return irg->n_loc - 1 - 1;
406 return irg->n_loc - 1;
411 set_irg_n_loc (ir_graph *irg, int n_loc)
413 #if PRECISE_EXC_CONTEXT
414 irg->n_loc = n_loc + 1 + 1;
416 irg->n_loc = n_loc + 1;
422 /* Returns the obstack associated with the graph. */
423 struct obstack *get_irg_obstack(ir_graph *irg) {
428 * Returns true if the node n is allocated on the storage of graph irg.
430 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
432 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
434 struct _obstack_chunk *p;
437 * checks wheater the ir_node pointer i on the obstack.
438 * A more sophisticated chaeck would test the "whole" ir_node
440 for (p = irg->obst->chunk; p; p = p->prev) {
441 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
449 get_irg_phase_state (ir_graph *irg) {
450 return irg->phase_state;
454 set_irg_phase_low(ir_graph *irg) {
455 irg->phase_state = phase_low;
459 get_irg_pinned (ir_graph *irg) {
464 get_irg_outs_state(ir_graph *irg) {
465 return irg->outs_state;
469 set_irg_outs_inconsistent(ir_graph *irg) {
470 irg->outs_state = outs_inconsistent;
474 get_irg_dom_state(ir_graph *irg) {
475 return irg->dom_state;
479 set_irg_dom_inconsistent(ir_graph *irg) {
480 irg->dom_state = dom_inconsistent;
484 set_irg_pinned (ir_graph *irg, op_pinned p) {
489 set_irg_link (ir_graph *irg, void *thing) {
494 get_irg_link (ir_graph *irg) {
498 /* maximum visited flag content of all ir_graph visited fields. */
499 static int max_irg_visited = 0;
502 get_irg_visited (ir_graph *irg)
508 set_irg_visited (ir_graph *irg, unsigned long visited)
510 irg->visited = visited;
511 if (irg->visited > max_irg_visited) {
512 max_irg_visited = irg->visited;
517 inc_irg_visited (ir_graph *irg)
519 if (++irg->visited > max_irg_visited) {
520 max_irg_visited = irg->visited;
525 get_max_irg_visited(void)
529 for(i = 0; i < get_irp_n_irgs(); i++)
530 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
532 return max_irg_visited;
535 void set_max_irg_visited(int val) {
536 max_irg_visited = val;
540 inc_max_irg_visited(void)
544 for(i = 0; i < get_irp_n_irgs(); i++)
545 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
548 return max_irg_visited;
552 get_irg_block_visited (ir_graph *irg)
554 return irg->block_visited;
558 set_irg_block_visited (ir_graph *irg, unsigned long visited)
560 irg->block_visited = visited;
564 inc_irg_block_visited (ir_graph *irg)
566 ++irg->block_visited;