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));
71 current_ir_graph = res;
72 add_irp_irg(res); /* remember this graph global. */
75 * initialized for each graph. **/
76 #if PRECISE_EXC_CONTEXT
77 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
78 dereferenced in this graph plus one for
79 the store plus one for links to fragile
80 operations. n_loc is not the number of
81 parameters to the procedure! */
83 res->n_loc = n_loc + 1; /* number of local variables that are never
84 dereferenced in this graph plus one for
85 the store. This is not the number of parameters
89 res->visited = 0; /* visited flag, for the ir walker */
90 res->block_visited=0; /* visited flag, for the 'block'-walker */
92 #if USE_EXPLICIT_PHI_IN_STACK
93 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
96 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
97 obstack_init (res->obst);
98 res->value_table = new_identities (); /* value table for global value
99 numbering for optimizing use in
103 res->phase_state = phase_building;
104 res->pinned = pinned;
105 res->outs_state = no_outs;
106 res->dom_state = no_dom;
108 /** Type information for the procedure of the graph **/
110 set_entity_irg(ent, res);
113 contain "inner" methods as in Pascal. **/
114 res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
115 /* Remove type from type list. Must be treated differently than other types. */
116 remove_irp_type_from_list(res->frame_type);
118 /** Nodes needed in every graph **/
119 res->end_block = new_immBlock ();
120 res->end = new_End ();
122 res->start_block = new_immBlock ();
123 res->start = new_Start ();
124 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
125 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
127 /* Proj results of start node */
128 projX = new_Proj (res->start, mode_X, pns_initial_exec);
129 set_store (new_Proj (res->start, mode_M, pns_global_store));
130 res->frame = new_Proj (res->start, mode_P, pns_frame_base);
131 res->globals = new_Proj (res->start, mode_P, pns_globals);
132 res->args = new_Proj (res->start, mode_T, pns_args);
134 res->graph_nr = get_irp_new_node_nr();
138 add_in_edge(res->start_block, projX);
140 * The code generation needs it. leave it in now.
141 * Use of this edge is matter of discussion, unresolved. Also possible:
142 * add_in_edge(res->start_block, res->start_block), but invalid typed.
144 mature_block (res->current_block);
146 /** Make a block to start with **/
147 first_block = new_immBlock ();
148 add_in_edge (first_block, projX);
154 /* Make a rudimentary ir graph for the constant code.
155 Must look like a correct irg, spare everything else. */
156 ir_graph *new_const_code_irg() {
160 res = (ir_graph *) malloc (sizeof (ir_graph));
161 current_ir_graph = res;
162 res->n_loc = 1; /* Only the memory. */
163 res->visited = 0; /* visited flag, for the ir walker */
164 res->block_visited=0; /* visited flag, for the 'block'-walker */
165 #if USE_EXPLICIT_PHI_IN_STACK
166 res->Phi_in_stack = NULL;
168 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
169 obstack_init (res->obst);
170 res->phase_state = phase_building;
171 res->pinned = pinned;
172 res->value_table = new_identities (); /* value table for global value
173 numbering for optimizing use in
176 res->frame_type = NULL;
177 res->start_block = new_immBlock ();
178 res->end_block = new_immBlock ();
179 res->end = new_End ();
180 mature_block(get_cur_block());
181 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
182 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
183 res->start = new_Start ();
184 /* Proj results of start node */
185 projX = new_Proj (res->start, mode_X, pns_initial_exec);
186 set_store (new_Proj (res->start, mode_M, pns_global_store));
187 add_in_edge(res->start_block, projX);
188 mature_block (res->current_block);
189 add_in_edge (new_immBlock (), projX);
190 mature_block(get_cur_block());
191 /* Set the visited flag high enough that the block will never be visited. */
192 set_irn_visited(get_cur_block(), -1);
193 set_Block_block_visited(get_cur_block(), -1);
194 set_Block_block_visited(res->start_block, -1);
198 /* Frees the passed irgraph.
199 Deallocates all nodes in this graph and the ir_graph structure.
200 Sets the field irgraph in the corresponding entity to NULL.
201 Does not remove the irgraph from the list in irprog (requires
202 inefficient search, call remove_irp_irg by hand).
203 Does not free types, entities or modes that are used only by this
204 graph, nor the entity standing for this graph. */
205 void free_ir_graph (ir_graph *irg) {
206 set_entity_irg(irg->ent, NULL);
208 #if USE_EXPLICIT_PHI_IN_STACK
209 free_Phi_in_stack(irg->Phi_in_stack);
214 /* access routines for all ir_graph attributes:
216 {attr type} get_irg_{attribute name} (ir_graph *irg);
217 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
219 /* Outputs a unique number for this node */
222 get_irg_graph_nr(ir_graph *irg) {
225 return irg->graph_nr;
232 get_irg_start_block (ir_graph *irg)
234 return irg->start_block;
238 set_irg_start_block (ir_graph *irg, ir_node *node)
240 irg->start_block = node;
244 get_irg_start (ir_graph *irg)
250 set_irg_start(ir_graph *irg, ir_node *node)
256 get_irg_end_block (ir_graph *irg)
258 return irg->end_block;
262 set_irg_end_block (ir_graph *irg, ir_node *node)
264 irg->end_block = node;
268 get_irg_end (ir_graph *irg)
274 set_irg_end (ir_graph *irg, ir_node *node)
280 get_irg_cstore (ir_graph *irg)
286 set_irg_cstore (ir_graph *irg, ir_node *node)
292 get_irg_frame (ir_graph *irg)
298 set_irg_frame (ir_graph *irg, ir_node *node)
304 get_irg_globals (ir_graph *irg)
310 set_irg_globals (ir_graph *irg, ir_node *node)
316 get_irg_args (ir_graph *irg)
322 set_irg_args (ir_graph *irg, ir_node *node)
328 get_irg_bad (ir_graph *irg)
334 set_irg_bad (ir_graph *irg, ir_node *node)
340 get_irg_unknown (ir_graph *irg)
346 set_irg_unknown (ir_graph *irg, ir_node *node)
352 get_irg_current_block (ir_graph *irg)
354 return irg->current_block;
358 set_irg_current_block (ir_graph *irg, ir_node *node)
360 irg->current_block = node;
364 get_irg_ent (ir_graph *irg)
366 assert(irg && irg->ent);
371 set_irg_ent (ir_graph *irg, entity *ent)
377 get_irg_frame_type (ir_graph *irg)
379 assert(irg && irg->frame_type);
380 return irg->frame_type;
384 set_irg_frame_type (ir_graph *irg, type *ftp)
386 irg->frame_type = ftp;
390 /* To test for a frame type */
392 is_frame_type(type *ftp) {
393 return ((is_class_type(ftp) || is_struct_type(ftp)) &&
394 id_is_suffix(id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX)),
395 get_type_ident(ftp)));
399 get_irg_n_locs (ir_graph *irg)
401 #if PRECISE_EXC_CONTEXT
402 return irg->n_loc - 1 - 1;
404 return irg->n_loc - 1;
409 set_irg_n_loc (ir_graph *irg, int n_loc)
411 #if PRECISE_EXC_CONTEXT
412 irg->n_loc = n_loc + 1 + 1;
414 irg->n_loc = n_loc + 1;
420 /* Returns the obstack associated with the graph. */
421 struct obstack *get_irg_obstack(ir_graph *irg) {
426 * Returns true if the node n is allocated on the storage of graph irg.
428 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
430 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
432 struct _obstack_chunk *p;
435 * checks wheater the ir_node pointer i on the obstack.
436 * A more sophisticated chaeck would test the "whole" ir_node
438 for (p = irg->obst->chunk; p; p = p->prev) {
439 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
447 get_irg_phase_state (ir_graph *irg) {
448 return irg->phase_state;
452 set_irg_phase_low(ir_graph *irg) {
453 irg->phase_state = phase_low;
457 get_irg_pinned (ir_graph *irg) {
462 get_irg_outs_state(ir_graph *irg) {
463 return irg->outs_state;
467 set_irg_outs_inconsistent(ir_graph *irg) {
468 irg->outs_state = outs_inconsistent;
472 get_irg_dom_state(ir_graph *irg) {
473 return irg->dom_state;
477 set_irg_dom_inconsistent(ir_graph *irg) {
478 irg->dom_state = dom_inconsistent;
482 set_irg_pinned (ir_graph *irg, op_pinned p) {
487 set_irg_link (ir_graph *irg, void *thing) {
492 get_irg_link (ir_graph *irg) {
496 /* maximum visited flag content of all ir_graph visited fields. */
497 static int max_irg_visited = 0;
500 get_irg_visited (ir_graph *irg)
506 set_irg_visited (ir_graph *irg, unsigned long visited)
508 irg->visited = visited;
509 if (irg->visited > max_irg_visited) {
510 max_irg_visited = irg->visited;
515 inc_irg_visited (ir_graph *irg)
517 if (++irg->visited > max_irg_visited) {
518 max_irg_visited = irg->visited;
523 get_max_irg_visited(void)
526 //for(i = 0; i < get_irp_n_irgs(); i++)
527 // assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
528 return max_irg_visited;
531 void set_max_irg_visited(int val) {
532 max_irg_visited = val;
536 inc_max_irg_visited(void)
539 // for(i = 0; i < get_irp_n_irgs(); i++)
540 //assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
542 return max_irg_visited;
546 get_irg_block_visited (ir_graph *irg)
548 return irg->block_visited;
552 set_irg_block_visited (ir_graph *irg, unsigned long visited)
554 irg->block_visited = visited;
558 inc_irg_block_visited (ir_graph *irg)
560 ++irg->block_visited;