1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Martin Trapp, Christian Schaefer
16 # include "irgraph_t.h"
23 ir_graph *current_ir_graph;
25 #if USE_EXPLICIT_PHI_IN_STACK
26 /* really defined in ircons.c */
27 typedef struct Phi_in_stack Phi_in_stack;
28 Phi_in_stack *new_Phi_in_stack();
29 void free_Phi_in_stack(Phi_in_stack *s);
32 /* Allocates a list of nodes:
33 - The start block containing a start node and Proj nodes for it's four
34 results (X, M, P, Tuple).
35 - The end block containing an end node. This block is not matured after
36 new_ir_graph as predecessors need to be added to it.
37 - The current block, which is empty and also not matured.
38 Further it allocates several datastructures needed for graph construction
42 new_ir_graph (entity *ent, int n_loc)
48 res = (ir_graph *) malloc (sizeof (ir_graph));
49 current_ir_graph = res;
50 add_irp_irg(res); /* remember this graph global. */
52 /** Internal information for graph construction either held in the graph or
53 *** initialized for each graph. **/
54 #if PRECISE_EXC_CONTEXT
55 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
56 dereferenced in this graph plus one for
57 the store plus one for links to fragile
58 operations. n_loc is not the number of
59 parameters to the procedure! */
61 res->n_loc = n_loc + 1; /* number of local variables that are never
62 dereferenced in this graph plus one for
63 the store. This is not the number of parameters
67 res->visited = 0; /* visited flag, for the ir walker */
68 res->block_visited=0; /* visited flag, for the 'block'-walker */
70 #if USE_EXPLICIT_PHI_IN_STACK
71 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
74 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
75 obstack_init (res->obst);
76 res->value_table = new_identities (); /* value table for global value
77 numbering for optimizing use in
81 res->phase_state = phase_building;
83 res->outs_state = no_outs;
84 res->dom_state = no_dom;
86 /** Type inforamtion for the procedure of the graph **/
88 set_entity_irg(ent, res);
90 /** A type that represents the stack frame. A class type so that it can
91 contain "inner" methods as in Pascal. **/
92 res->frame_type = new_type_class(mangle(get_entity_ident(ent),
93 id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX))));
95 /** Nodes needed in every graph **/
96 res->end_block = new_immBlock ();
97 res->end = new_End ();
99 res->start_block = new_immBlock ();
100 res->start = new_Start ();
101 res->bad = new_ir_node (res, res->start_block, op_Bad, mode_T, 0, NULL);
103 /* Proj results of start node */
104 projX = new_Proj (res->start, mode_X, pns_initial_exec);
105 set_store (new_Proj (res->start, mode_M, pns_global_store));
106 res->frame = new_Proj (res->start, mode_p, pns_frame_base);
107 res->globals = new_Proj (res->start, mode_p, pns_globals);
108 res->args = new_Proj (res->start, mode_T, pns_args);
110 add_in_edge(res->start_block, projX);
112 * The code generation needs it. leave it in now.
113 * Use of this edge is matter of discussion, unresolved. Also possible:
114 * add_in_edge(res->start_block, res->start_block), but invalid typed.
116 mature_block (res->current_block);
118 /** Make a block to start with **/
119 first_block = new_immBlock ();
120 add_in_edge (first_block, projX);
126 /* Make a rudimentary ir graph for the constant code.
127 Must look like a correct irg, spare everything else. */
128 ir_graph *new_const_code_irg() {
132 res = (ir_graph *) malloc (sizeof (ir_graph));
133 current_ir_graph = res;
134 res->n_loc = 1; /* Only the memory. */
135 res->visited = 0; /* visited flag, for the ir walker */
136 res->block_visited=0; /* visited flag, for the 'block'-walker */
137 #if USE_EXPLICIT_PHI_IN_STACK
138 res->Phi_in_stack = NULL;
140 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
141 obstack_init (res->obst);
142 res->pinned = pinned;
143 res->value_table = new_identities (); /* value table for global value
144 numbering for optimizing use in
147 res->frame_type = NULL;
148 res->start_block = new_immBlock ();
149 res->end_block = new_immBlock ();
150 res->end = new_End ();
151 mature_block(get_cur_block());
152 res->bad = new_ir_node (res, res->start_block, op_Bad, mode_T, 0, NULL);
153 res->start = new_Start ();
154 /* Proj results of start node */
155 projX = new_Proj (res->start, mode_X, pns_initial_exec);
156 set_store (new_Proj (res->start, mode_M, pns_global_store));
157 add_in_edge(res->start_block, projX);
158 mature_block (res->current_block);
159 add_in_edge (new_immBlock (), projX);
160 mature_block(get_cur_block());
161 /* Set the visited flag high enough that the block will never be visited. */
162 set_irn_visited(get_cur_block(), -1);
163 set_Block_block_visited(get_cur_block(), -1);
164 set_Block_block_visited(res->start_block, -1);
168 /* Frees the passed irgraph.
169 Deallocates all nodes in this graph and the ir_graph structure.
170 Sets the field irgraph in the corresponding entity to NULL.
171 Does not remove the irgraph from the list in irprog (requires
172 inefficient search, call remove_irp_irg by hand).
173 Does not free types, entities or modes that are used only by this
174 graph, nor the entity standing for this graph. */
175 void free_ir_graph (ir_graph *irg) {
176 set_entity_irg(irg->ent, NULL);
178 #if USE_EXPLICIT_PHI_IN_STACK
179 free_Phi_in_stack(irg->Phi_in_stack);
184 /* access routines for all ir_graph attributes:
186 {attr type} get_irg_{attribute name} (ir_graph *irg);
187 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
190 get_irg_start_block (ir_graph *irg)
192 return irg->start_block;
196 set_irg_start_block (ir_graph *irg, ir_node *node)
198 irg->start_block = node;
202 get_irg_start (ir_graph *irg)
208 set_irg_start(ir_graph *irg, ir_node *node)
214 get_irg_end_block (ir_graph *irg)
216 return irg->end_block;
220 set_irg_end_block (ir_graph *irg, ir_node *node)
222 irg->end_block = node;
226 get_irg_end (ir_graph *irg)
232 set_irg_end (ir_graph *irg, ir_node *node)
238 get_irg_cstore (ir_graph *irg)
244 set_irg_cstore (ir_graph *irg, ir_node *node)
250 get_irg_frame (ir_graph *irg)
256 set_irg_frame (ir_graph *irg, ir_node *node)
262 get_irg_globals (ir_graph *irg)
268 set_irg_globals (ir_graph *irg, ir_node *node)
274 get_irg_args (ir_graph *irg)
280 set_irg_args (ir_graph *irg, ir_node *node)
286 get_irg_bad (ir_graph *irg)
292 set_irg_bad (ir_graph *irg, ir_node *node)
298 get_irg_current_block (ir_graph *irg)
300 return irg->current_block;
304 set_irg_current_block (ir_graph *irg, ir_node *node)
306 irg->current_block = node;
310 get_irg_ent (ir_graph *irg)
312 assert(irg && irg->ent);
317 set_irg_ent (ir_graph *irg, entity *ent)
323 get_irg_frame_type (ir_graph *irg)
325 assert(irg && irg->frame_type);
326 return irg->frame_type;
330 set_irg_frame_type (ir_graph *irg, type *ftp)
332 irg->frame_type = ftp;
336 /* To test for a frame type */
338 is_frame_type(type *ftp) {
339 return ((is_class_type(ftp) || is_struct_type(ftp)) &&
340 id_is_suffix(id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX)),
341 get_type_ident(ftp)));
345 get_irg_n_loc (ir_graph *irg)
347 #if PRECISE_EXC_CONTEXT
348 return irg->n_loc - 1 - 1;
350 return irg->n_loc - 1;
355 set_irg_n_loc (ir_graph *irg, int n_loc)
357 #if PRECISE_EXC_CONTEXT
358 irg->n_loc = n_loc + 1 + 1;
360 irg->n_loc = n_loc + 1;
364 irg_phase_state get_irg_phase_state (ir_graph *irg) {
365 return irg->phase_state;
368 void set_irg_phase_low(ir_graph *irg) {
369 irg->phase_state = phase_low;
373 get_irg_pinned (ir_graph *irg) {
377 irg_outs_state get_irg_outs_state(ir_graph *irg) {
378 return irg->outs_state;
381 void set_irg_outs_inconsistent(ir_graph *irg) {
382 irg->outs_state = outs_inconsistent;
385 irg_dom_state get_irg_dom_state(ir_graph *irg) {
386 return irg->dom_state;
389 void set_irg_dom_inconsistent(ir_graph *irg) {
390 irg->dom_state = dom_inconsistent;
394 set_irg_pinned (ir_graph *irg, op_pinned p)
400 get_irg_visited (ir_graph *irg)
406 set_irg_visited (ir_graph *irg, unsigned long visited)
408 irg->visited = visited;
412 inc_irg_visited (ir_graph *irg)
414 irg->visited = irg->visited++;
418 get_irg_block_visited (ir_graph *irg)
420 return irg->block_visited;
424 set_irg_block_visited (ir_graph *irg, unsigned long visited)
426 irg->block_visited = visited;
430 inc_irg_block_visited (ir_graph *irg)
432 irg->block_visited = irg->block_visited++;