1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Martin Trapp, Christian Schaefer
18 # include "irgraph_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)
52 res = (ir_graph *) malloc (sizeof (ir_graph));
53 current_ir_graph = res;
54 add_irp_irg(res); /* remember this graph global. */
56 /** Internal information for graph construction either held in the graph or
57 *** initialized for each graph. **/
58 #if PRECISE_EXC_CONTEXT
59 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
60 dereferenced in this graph plus one for
61 the store plus one for links to fragile
62 operations. n_loc is not the number of
63 parameters to the procedure! */
65 res->n_loc = n_loc + 1; /* number of local variables that are never
66 dereferenced in this graph plus one for
67 the store. This is not the number of parameters
71 res->visited = 0; /* visited flag, for the ir walker */
72 res->block_visited=0; /* visited flag, for the 'block'-walker */
74 #if USE_EXPLICIT_PHI_IN_STACK
75 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
78 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
79 obstack_init (res->obst);
80 res->value_table = new_identities (); /* value table for global value
81 numbering for optimizing use in
85 res->phase_state = phase_building;
87 res->outs_state = no_outs;
88 res->dom_state = no_dom;
90 /** Type inforamtion for the procedure of the graph **/
92 set_entity_irg(ent, res);
94 /** A type that represents the stack frame. A class type so that it can
95 contain "inner" methods as in Pascal. **/
96 res->frame_type = new_type_class(mangle(get_entity_ident(ent),
97 id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX))));
99 /** Nodes needed in every graph **/
100 res->end_block = new_immBlock ();
101 res->end = new_End ();
103 res->start_block = new_immBlock ();
104 res->start = new_Start ();
105 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
106 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
108 /* Proj results of start node */
109 projX = new_Proj (res->start, mode_X, pns_initial_exec);
110 set_store (new_Proj (res->start, mode_M, pns_global_store));
111 res->frame = new_Proj (res->start, mode_p, pns_frame_base);
112 res->globals = new_Proj (res->start, mode_p, pns_globals);
113 res->args = new_Proj (res->start, mode_T, pns_args);
115 add_in_edge(res->start_block, projX);
117 * The code generation needs it. leave it in now.
118 * Use of this edge is matter of discussion, unresolved. Also possible:
119 * add_in_edge(res->start_block, res->start_block), but invalid typed.
121 mature_block (res->current_block);
123 /** Make a block to start with **/
124 first_block = new_immBlock ();
125 add_in_edge (first_block, projX);
131 /* Make a rudimentary ir graph for the constant code.
132 Must look like a correct irg, spare everything else. */
133 ir_graph *new_const_code_irg() {
137 res = (ir_graph *) malloc (sizeof (ir_graph));
138 current_ir_graph = res;
139 res->n_loc = 1; /* Only the memory. */
140 res->visited = 0; /* visited flag, for the ir walker */
141 res->block_visited=0; /* visited flag, for the 'block'-walker */
142 #if USE_EXPLICIT_PHI_IN_STACK
143 res->Phi_in_stack = NULL;
145 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
146 obstack_init (res->obst);
147 res->pinned = pinned;
148 res->value_table = new_identities (); /* value table for global value
149 numbering for optimizing use in
152 res->frame_type = NULL;
153 res->start_block = new_immBlock ();
154 res->end_block = new_immBlock ();
155 res->end = new_End ();
156 mature_block(get_cur_block());
157 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
158 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
159 res->start = new_Start ();
160 /* Proj results of start node */
161 projX = new_Proj (res->start, mode_X, pns_initial_exec);
162 set_store (new_Proj (res->start, mode_M, pns_global_store));
163 add_in_edge(res->start_block, projX);
164 mature_block (res->current_block);
165 add_in_edge (new_immBlock (), projX);
166 mature_block(get_cur_block());
167 /* Set the visited flag high enough that the block will never be visited. */
168 set_irn_visited(get_cur_block(), -1);
169 set_Block_block_visited(get_cur_block(), -1);
170 set_Block_block_visited(res->start_block, -1);
174 /* Frees the passed irgraph.
175 Deallocates all nodes in this graph and the ir_graph structure.
176 Sets the field irgraph in the corresponding entity to NULL.
177 Does not remove the irgraph from the list in irprog (requires
178 inefficient search, call remove_irp_irg by hand).
179 Does not free types, entities or modes that are used only by this
180 graph, nor the entity standing for this graph. */
181 void free_ir_graph (ir_graph *irg) {
182 set_entity_irg(irg->ent, NULL);
184 #if USE_EXPLICIT_PHI_IN_STACK
185 free_Phi_in_stack(irg->Phi_in_stack);
190 /* access routines for all ir_graph attributes:
192 {attr type} get_irg_{attribute name} (ir_graph *irg);
193 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
196 get_irg_start_block (ir_graph *irg)
198 return irg->start_block;
202 set_irg_start_block (ir_graph *irg, ir_node *node)
204 irg->start_block = node;
208 get_irg_start (ir_graph *irg)
214 set_irg_start(ir_graph *irg, ir_node *node)
220 get_irg_end_block (ir_graph *irg)
222 return irg->end_block;
226 set_irg_end_block (ir_graph *irg, ir_node *node)
228 irg->end_block = node;
232 get_irg_end (ir_graph *irg)
238 set_irg_end (ir_graph *irg, ir_node *node)
244 get_irg_cstore (ir_graph *irg)
250 set_irg_cstore (ir_graph *irg, ir_node *node)
256 get_irg_frame (ir_graph *irg)
262 set_irg_frame (ir_graph *irg, ir_node *node)
268 get_irg_globals (ir_graph *irg)
274 set_irg_globals (ir_graph *irg, ir_node *node)
280 get_irg_args (ir_graph *irg)
286 set_irg_args (ir_graph *irg, ir_node *node)
292 get_irg_bad (ir_graph *irg)
298 set_irg_bad (ir_graph *irg, ir_node *node)
304 get_irg_unknown (ir_graph *irg)
310 set_irg_unknown (ir_graph *irg, ir_node *node)
316 get_irg_current_block (ir_graph *irg)
318 return irg->current_block;
322 set_irg_current_block (ir_graph *irg, ir_node *node)
324 irg->current_block = node;
328 get_irg_ent (ir_graph *irg)
330 assert(irg && irg->ent);
335 set_irg_ent (ir_graph *irg, entity *ent)
341 get_irg_frame_type (ir_graph *irg)
343 assert(irg && irg->frame_type);
344 return irg->frame_type;
348 set_irg_frame_type (ir_graph *irg, type *ftp)
350 irg->frame_type = ftp;
354 /* To test for a frame type */
356 is_frame_type(type *ftp) {
357 return ((is_class_type(ftp) || is_struct_type(ftp)) &&
358 id_is_suffix(id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX)),
359 get_type_ident(ftp)));
363 get_irg_n_loc (ir_graph *irg)
365 #if PRECISE_EXC_CONTEXT
366 return irg->n_loc - 1 - 1;
368 return irg->n_loc - 1;
373 set_irg_n_loc (ir_graph *irg, int n_loc)
375 #if PRECISE_EXC_CONTEXT
376 irg->n_loc = n_loc + 1 + 1;
378 irg->n_loc = n_loc + 1;
382 irg_phase_state get_irg_phase_state (ir_graph *irg) {
383 return irg->phase_state;
386 void set_irg_phase_low(ir_graph *irg) {
387 irg->phase_state = phase_low;
391 get_irg_pinned (ir_graph *irg) {
395 irg_outs_state get_irg_outs_state(ir_graph *irg) {
396 return irg->outs_state;
399 void set_irg_outs_inconsistent(ir_graph *irg) {
400 irg->outs_state = outs_inconsistent;
403 irg_dom_state get_irg_dom_state(ir_graph *irg) {
404 return irg->dom_state;
407 void set_irg_dom_inconsistent(ir_graph *irg) {
408 irg->dom_state = dom_inconsistent;
412 set_irg_pinned (ir_graph *irg, op_pinned p)
417 /* maximum of all ir_graphs */
418 static int max_irg_visited = 0;
422 get_irg_visited (ir_graph *irg)
428 set_irg_visited (ir_graph *irg, unsigned long visited)
430 irg->visited = visited;
434 inc_irg_visited (ir_graph *irg)
436 if (++irg->visited > max_irg_visited) {
437 max_irg_visited = irg->visited;
442 get_max_irg_visited(void)
444 return max_irg_visited;
448 get_irg_block_visited (ir_graph *irg)
450 return irg->block_visited;
454 set_irg_block_visited (ir_graph *irg, unsigned long visited)
456 irg->block_visited = visited;
460 inc_irg_block_visited (ir_graph *irg)
462 ++irg->block_visited;