1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Martin Trapp, Christian Schaefer
18 # include "irgraph_t.h"
19 # include "irprog_t.h"
25 ir_graph *current_ir_graph;
27 bool interprocedural_view = false;
29 #if USE_EXPLICIT_PHI_IN_STACK
30 /* really defined in ircons.c */
31 typedef struct Phi_in_stack Phi_in_stack;
32 Phi_in_stack *new_Phi_in_stack();
33 void free_Phi_in_stack(Phi_in_stack *s);
36 /* Allocates a list of nodes:
37 - The start block containing a start node and Proj nodes for it's four
38 results (X, M, P, Tuple).
39 - The end block containing an end node. This block is not matured after
40 new_ir_graph as predecessors need to be added to it.
41 - The current block, which is empty and also not matured.
42 Further it allocates several datastructures needed for graph construction
46 new_ir_graph (entity *ent, int n_loc)
53 res = (ir_graph *) malloc (sizeof (ir_graph));
54 current_ir_graph = res;
55 add_irp_irg(res); /* remember this graph global. */
57 /** Internal information for graph construction either held in the graph or
58 *** initialized for each graph. **/
59 #if PRECISE_EXC_CONTEXT
60 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
61 dereferenced in this graph plus one for
62 the store plus one for links to fragile
63 operations. n_loc is not the number of
64 parameters to the procedure! */
66 res->n_loc = n_loc + 1; /* number of local variables that are never
67 dereferenced in this graph plus one for
68 the store. This is not the number of parameters
72 res->visited = 0; /* visited flag, for the ir walker */
73 res->block_visited=0; /* visited flag, for the 'block'-walker */
75 #if USE_EXPLICIT_PHI_IN_STACK
76 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
79 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
80 obstack_init (res->obst);
81 res->value_table = new_identities (); /* value table for global value
82 numbering for optimizing use in
86 res->phase_state = phase_building;
88 res->outs_state = no_outs;
89 res->dom_state = no_dom;
91 /** Type information for the procedure of the graph **/
93 set_entity_irg(ent, res);
95 /** A type that represents the stack frame. A class type so that it can
96 contain "inner" methods as in Pascal. **/
97 res->frame_type = new_type_class(mangle(get_entity_ident(ent),
98 id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX))));
99 /* Remove type from type list. Must be treated differently than other types. */
100 remove_irp_type_from_list(res->frame_type);
102 /** Nodes needed in every graph **/
103 res->end_block = new_immBlock ();
104 res->end = new_End ();
106 res->start_block = new_immBlock ();
107 res->start = new_Start ();
108 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
109 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
111 /* Proj results of start node */
112 projX = new_Proj (res->start, mode_X, pns_initial_exec);
113 set_store (new_Proj (res->start, mode_M, pns_global_store));
114 res->frame = new_Proj (res->start, mode_p, pns_frame_base);
115 res->globals = new_Proj (res->start, mode_p, pns_globals);
116 res->args = new_Proj (res->start, mode_T, pns_args);
118 add_in_edge(res->start_block, projX);
120 * The code generation needs it. leave it in now.
121 * Use of this edge is matter of discussion, unresolved. Also possible:
122 * add_in_edge(res->start_block, res->start_block), but invalid typed.
124 mature_block (res->current_block);
126 /** Make a block to start with **/
127 first_block = new_immBlock ();
128 add_in_edge (first_block, projX);
134 /* Make a rudimentary ir graph for the constant code.
135 Must look like a correct irg, spare everything else. */
136 ir_graph *new_const_code_irg() {
140 res = (ir_graph *) malloc (sizeof (ir_graph));
141 current_ir_graph = res;
142 res->n_loc = 1; /* Only the memory. */
143 res->visited = 0; /* visited flag, for the ir walker */
144 res->block_visited=0; /* visited flag, for the 'block'-walker */
145 #if USE_EXPLICIT_PHI_IN_STACK
146 res->Phi_in_stack = NULL;
148 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
149 obstack_init (res->obst);
150 res->pinned = pinned;
151 res->value_table = new_identities (); /* value table for global value
152 numbering for optimizing use in
155 res->frame_type = NULL;
156 res->start_block = new_immBlock ();
157 res->end_block = new_immBlock ();
158 res->end = new_End ();
159 mature_block(get_cur_block());
160 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
161 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
162 res->start = new_Start ();
163 /* Proj results of start node */
164 projX = new_Proj (res->start, mode_X, pns_initial_exec);
165 set_store (new_Proj (res->start, mode_M, pns_global_store));
166 add_in_edge(res->start_block, projX);
167 mature_block (res->current_block);
168 add_in_edge (new_immBlock (), projX);
169 mature_block(get_cur_block());
170 /* Set the visited flag high enough that the block will never be visited. */
171 set_irn_visited(get_cur_block(), -1);
172 set_Block_block_visited(get_cur_block(), -1);
173 set_Block_block_visited(res->start_block, -1);
177 /* Frees the passed irgraph.
178 Deallocates all nodes in this graph and the ir_graph structure.
179 Sets the field irgraph in the corresponding entity to NULL.
180 Does not remove the irgraph from the list in irprog (requires
181 inefficient search, call remove_irp_irg by hand).
182 Does not free types, entities or modes that are used only by this
183 graph, nor the entity standing for this graph. */
184 void free_ir_graph (ir_graph *irg) {
185 set_entity_irg(irg->ent, NULL);
187 #if USE_EXPLICIT_PHI_IN_STACK
188 free_Phi_in_stack(irg->Phi_in_stack);
193 /* access routines for all ir_graph attributes:
195 {attr type} get_irg_{attribute name} (ir_graph *irg);
196 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
199 get_irg_start_block (ir_graph *irg)
201 return irg->start_block;
205 set_irg_start_block (ir_graph *irg, ir_node *node)
207 irg->start_block = node;
211 get_irg_start (ir_graph *irg)
217 set_irg_start(ir_graph *irg, ir_node *node)
223 get_irg_end_block (ir_graph *irg)
225 return irg->end_block;
229 set_irg_end_block (ir_graph *irg, ir_node *node)
231 irg->end_block = node;
235 get_irg_end (ir_graph *irg)
241 set_irg_end (ir_graph *irg, ir_node *node)
247 get_irg_cstore (ir_graph *irg)
253 set_irg_cstore (ir_graph *irg, ir_node *node)
259 get_irg_frame (ir_graph *irg)
265 set_irg_frame (ir_graph *irg, ir_node *node)
271 get_irg_globals (ir_graph *irg)
277 set_irg_globals (ir_graph *irg, ir_node *node)
283 get_irg_args (ir_graph *irg)
289 set_irg_args (ir_graph *irg, ir_node *node)
295 get_irg_bad (ir_graph *irg)
301 set_irg_bad (ir_graph *irg, ir_node *node)
307 get_irg_unknown (ir_graph *irg)
313 set_irg_unknown (ir_graph *irg, ir_node *node)
319 get_irg_current_block (ir_graph *irg)
321 return irg->current_block;
325 set_irg_current_block (ir_graph *irg, ir_node *node)
327 irg->current_block = node;
331 get_irg_ent (ir_graph *irg)
333 assert(irg && irg->ent);
338 set_irg_ent (ir_graph *irg, entity *ent)
344 get_irg_frame_type (ir_graph *irg)
346 assert(irg && irg->frame_type);
347 return irg->frame_type;
351 set_irg_frame_type (ir_graph *irg, type *ftp)
353 irg->frame_type = ftp;
357 /* To test for a frame type */
359 is_frame_type(type *ftp) {
360 return ((is_class_type(ftp) || is_struct_type(ftp)) &&
361 id_is_suffix(id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX)),
362 get_type_ident(ftp)));
366 get_irg_n_locs (ir_graph *irg)
368 #if PRECISE_EXC_CONTEXT
369 return irg->n_loc - 1 - 1;
371 return irg->n_loc - 1;
376 set_irg_n_loc (ir_graph *irg, int n_loc)
378 #if PRECISE_EXC_CONTEXT
379 irg->n_loc = n_loc + 1 + 1;
381 irg->n_loc = n_loc + 1;
385 irg_phase_state get_irg_phase_state (ir_graph *irg) {
386 return irg->phase_state;
389 void set_irg_phase_low(ir_graph *irg) {
390 irg->phase_state = phase_low;
394 get_irg_pinned (ir_graph *irg) {
398 irg_outs_state get_irg_outs_state(ir_graph *irg) {
399 return irg->outs_state;
402 void set_irg_outs_inconsistent(ir_graph *irg) {
403 irg->outs_state = outs_inconsistent;
406 irg_dom_state get_irg_dom_state(ir_graph *irg) {
407 return irg->dom_state;
410 void set_irg_dom_inconsistent(ir_graph *irg) {
411 irg->dom_state = dom_inconsistent;
415 set_irg_pinned (ir_graph *irg, op_pinned p) {
420 set_irg_link (ir_graph *irg, void *thing) {
425 get_irg_link (ir_graph *irg) {
429 /* maximum visited flag content of all ir_graph visited fields. */
430 static int max_irg_visited = 0;
433 get_irg_visited (ir_graph *irg)
439 set_irg_visited (ir_graph *irg, unsigned long visited)
441 irg->visited = visited;
442 if (irg->visited > max_irg_visited) {
443 max_irg_visited = irg->visited;
448 inc_irg_visited (ir_graph *irg)
450 if (++irg->visited > max_irg_visited) {
451 max_irg_visited = irg->visited;
456 get_max_irg_visited(void)
459 //for(i = 0; i < get_irp_n_irgs(); i++)
460 // assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
461 return max_irg_visited;
464 void set_max_irg_visited(int val) {
465 max_irg_visited = val;
469 inc_max_irg_visited(void)
472 // for(i = 0; i < get_irp_n_irgs(); i++)
473 //assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
475 return max_irg_visited;
479 get_irg_block_visited (ir_graph *irg)
481 return irg->block_visited;
485 set_irg_block_visited (ir_graph *irg, unsigned long visited)
487 irg->block_visited = visited;
491 inc_irg_block_visited (ir_graph *irg)
493 ++irg->block_visited;