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;
26 INLINE ir_graph *get_current_ir_graph() {
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() {
36 return interprocedural_view;
38 INLINE void set_interprocedural_view(bool state) {
39 interprocedural_view = state;
42 #if USE_EXPLICIT_PHI_IN_STACK
43 /* really defined in ircons.c */
44 typedef struct Phi_in_stack Phi_in_stack;
45 Phi_in_stack *new_Phi_in_stack();
46 void free_Phi_in_stack(Phi_in_stack *s);
49 /* Allocates a list of nodes:
50 - The start block containing a start node and Proj nodes for it's four
51 results (X, M, P, Tuple).
52 - The end block containing an end node. This block is not matured after
53 new_ir_graph as predecessors need to be added to it.
54 - The current block, which is empty and also not matured.
55 Further it allocates several datastructures needed for graph construction
59 new_ir_graph (entity *ent, int n_loc)
66 res = (ir_graph *) malloc (sizeof (ir_graph));
67 current_ir_graph = res;
68 add_irp_irg(res); /* remember this graph global. */
70 /** Internal information for graph construction either held in the graph or
71 *** initialized for each graph. **/
72 #if PRECISE_EXC_CONTEXT
73 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
74 dereferenced in this graph plus one for
75 the store plus one for links to fragile
76 operations. n_loc is not the number of
77 parameters to the procedure! */
79 res->n_loc = n_loc + 1; /* number of local variables that are never
80 dereferenced in this graph plus one for
81 the store. This is not the number of parameters
85 res->visited = 0; /* visited flag, for the ir walker */
86 res->block_visited=0; /* visited flag, for the 'block'-walker */
88 #if USE_EXPLICIT_PHI_IN_STACK
89 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
92 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
93 obstack_init (res->obst);
94 res->value_table = new_identities (); /* value table for global value
95 numbering for optimizing use in
99 res->phase_state = phase_building;
100 res->pinned = pinned;
101 res->outs_state = no_outs;
102 res->dom_state = no_dom;
104 /** Type information for the procedure of the graph **/
106 set_entity_irg(ent, res);
108 /** A type that represents the stack frame. A class type so that it can
109 contain "inner" methods as in Pascal. **/
110 res->frame_type = new_type_class(mangle(get_entity_ident(ent),
111 id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX))));
112 /* Remove type from type list. Must be treated differently than other types. */
113 remove_irp_type_from_list(res->frame_type);
115 /** Nodes needed in every graph **/
116 res->end_block = new_immBlock ();
117 res->end = new_End ();
119 res->start_block = new_immBlock ();
120 res->start = new_Start ();
121 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
122 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
124 /* Proj results of start node */
125 projX = new_Proj (res->start, mode_X, pns_initial_exec);
126 set_store (new_Proj (res->start, mode_M, pns_global_store));
127 res->frame = new_Proj (res->start, mode_P, pns_frame_base);
128 res->globals = new_Proj (res->start, mode_P, pns_globals);
129 res->args = new_Proj (res->start, mode_T, pns_args);
131 add_in_edge(res->start_block, projX);
133 * The code generation needs it. leave it in now.
134 * Use of this edge is matter of discussion, unresolved. Also possible:
135 * add_in_edge(res->start_block, res->start_block), but invalid typed.
137 mature_block (res->current_block);
139 /** Make a block to start with **/
140 first_block = new_immBlock ();
141 add_in_edge (first_block, projX);
147 /* Make a rudimentary ir graph for the constant code.
148 Must look like a correct irg, spare everything else. */
149 ir_graph *new_const_code_irg() {
153 res = (ir_graph *) malloc (sizeof (ir_graph));
154 current_ir_graph = res;
155 res->n_loc = 1; /* Only the memory. */
156 res->visited = 0; /* visited flag, for the ir walker */
157 res->block_visited=0; /* visited flag, for the 'block'-walker */
158 #if USE_EXPLICIT_PHI_IN_STACK
159 res->Phi_in_stack = NULL;
161 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
162 obstack_init (res->obst);
163 res->pinned = pinned;
164 res->value_table = new_identities (); /* value table for global value
165 numbering for optimizing use in
168 res->frame_type = NULL;
169 res->start_block = new_immBlock ();
170 res->end_block = new_immBlock ();
171 res->end = new_End ();
172 mature_block(get_cur_block());
173 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
174 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
175 res->start = new_Start ();
176 /* Proj results of start node */
177 projX = new_Proj (res->start, mode_X, pns_initial_exec);
178 set_store (new_Proj (res->start, mode_M, pns_global_store));
179 add_in_edge(res->start_block, projX);
180 mature_block (res->current_block);
181 add_in_edge (new_immBlock (), projX);
182 mature_block(get_cur_block());
183 /* Set the visited flag high enough that the block will never be visited. */
184 set_irn_visited(get_cur_block(), -1);
185 set_Block_block_visited(get_cur_block(), -1);
186 set_Block_block_visited(res->start_block, -1);
190 /* Frees the passed irgraph.
191 Deallocates all nodes in this graph and the ir_graph structure.
192 Sets the field irgraph in the corresponding entity to NULL.
193 Does not remove the irgraph from the list in irprog (requires
194 inefficient search, call remove_irp_irg by hand).
195 Does not free types, entities or modes that are used only by this
196 graph, nor the entity standing for this graph. */
197 void free_ir_graph (ir_graph *irg) {
198 set_entity_irg(irg->ent, NULL);
200 #if USE_EXPLICIT_PHI_IN_STACK
201 free_Phi_in_stack(irg->Phi_in_stack);
206 /* access routines for all ir_graph attributes:
208 {attr type} get_irg_{attribute name} (ir_graph *irg);
209 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
212 get_irg_start_block (ir_graph *irg)
214 return irg->start_block;
218 set_irg_start_block (ir_graph *irg, ir_node *node)
220 irg->start_block = node;
224 get_irg_start (ir_graph *irg)
230 set_irg_start(ir_graph *irg, ir_node *node)
236 get_irg_end_block (ir_graph *irg)
238 return irg->end_block;
242 set_irg_end_block (ir_graph *irg, ir_node *node)
244 irg->end_block = node;
248 get_irg_end (ir_graph *irg)
254 set_irg_end (ir_graph *irg, ir_node *node)
260 get_irg_cstore (ir_graph *irg)
266 set_irg_cstore (ir_graph *irg, ir_node *node)
272 get_irg_frame (ir_graph *irg)
278 set_irg_frame (ir_graph *irg, ir_node *node)
284 get_irg_globals (ir_graph *irg)
290 set_irg_globals (ir_graph *irg, ir_node *node)
296 get_irg_args (ir_graph *irg)
302 set_irg_args (ir_graph *irg, ir_node *node)
308 get_irg_bad (ir_graph *irg)
314 set_irg_bad (ir_graph *irg, ir_node *node)
320 get_irg_unknown (ir_graph *irg)
326 set_irg_unknown (ir_graph *irg, ir_node *node)
332 get_irg_current_block (ir_graph *irg)
334 return irg->current_block;
338 set_irg_current_block (ir_graph *irg, ir_node *node)
340 irg->current_block = node;
344 get_irg_ent (ir_graph *irg)
346 assert(irg && irg->ent);
351 set_irg_ent (ir_graph *irg, entity *ent)
357 get_irg_frame_type (ir_graph *irg)
359 assert(irg && irg->frame_type);
360 return irg->frame_type;
364 set_irg_frame_type (ir_graph *irg, type *ftp)
366 irg->frame_type = ftp;
370 /* To test for a frame type */
372 is_frame_type(type *ftp) {
373 return ((is_class_type(ftp) || is_struct_type(ftp)) &&
374 id_is_suffix(id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX)),
375 get_type_ident(ftp)));
379 get_irg_n_locs (ir_graph *irg)
381 #if PRECISE_EXC_CONTEXT
382 return irg->n_loc - 1 - 1;
384 return irg->n_loc - 1;
389 set_irg_n_loc (ir_graph *irg, int n_loc)
391 #if PRECISE_EXC_CONTEXT
392 irg->n_loc = n_loc + 1 + 1;
394 irg->n_loc = n_loc + 1;
398 irg_phase_state get_irg_phase_state (ir_graph *irg) {
399 return irg->phase_state;
402 void set_irg_phase_low(ir_graph *irg) {
403 irg->phase_state = phase_low;
407 get_irg_pinned (ir_graph *irg) {
411 irg_outs_state get_irg_outs_state(ir_graph *irg) {
412 return irg->outs_state;
415 void set_irg_outs_inconsistent(ir_graph *irg) {
416 irg->outs_state = outs_inconsistent;
419 irg_dom_state get_irg_dom_state(ir_graph *irg) {
420 return irg->dom_state;
423 void set_irg_dom_inconsistent(ir_graph *irg) {
424 irg->dom_state = dom_inconsistent;
428 set_irg_pinned (ir_graph *irg, op_pinned p) {
433 set_irg_link (ir_graph *irg, void *thing) {
438 get_irg_link (ir_graph *irg) {
442 /* maximum visited flag content of all ir_graph visited fields. */
443 static int max_irg_visited = 0;
446 get_irg_visited (ir_graph *irg)
452 set_irg_visited (ir_graph *irg, unsigned long visited)
454 irg->visited = visited;
455 if (irg->visited > max_irg_visited) {
456 max_irg_visited = irg->visited;
461 inc_irg_visited (ir_graph *irg)
463 if (++irg->visited > max_irg_visited) {
464 max_irg_visited = irg->visited;
469 get_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)));
474 return max_irg_visited;
477 void set_max_irg_visited(int val) {
478 max_irg_visited = val;
482 inc_max_irg_visited(void)
485 // for(i = 0; i < get_irp_n_irgs(); i++)
486 //assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
488 return max_irg_visited;
492 get_irg_block_visited (ir_graph *irg)
494 return irg->block_visited;
498 set_irg_block_visited (ir_graph *irg, unsigned long visited)
500 irg->block_visited = visited;
504 inc_irg_block_visited (ir_graph *irg)
506 ++irg->block_visited;