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() {
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. */
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);
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->phase_state = phase_building;
164 res->pinned = pinned;
165 res->value_table = new_identities (); /* value table for global value
166 numbering for optimizing use in
169 res->frame_type = NULL;
170 res->start_block = new_immBlock ();
171 res->end_block = new_immBlock ();
172 res->end = new_End ();
173 mature_block(get_cur_block());
174 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
175 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
176 res->start = new_Start ();
177 /* Proj results of start node */
178 projX = new_Proj (res->start, mode_X, pns_initial_exec);
179 set_store (new_Proj (res->start, mode_M, pns_global_store));
180 add_in_edge(res->start_block, projX);
181 mature_block (res->current_block);
182 add_in_edge (new_immBlock (), projX);
183 mature_block(get_cur_block());
184 /* Set the visited flag high enough that the block will never be visited. */
185 set_irn_visited(get_cur_block(), -1);
186 set_Block_block_visited(get_cur_block(), -1);
187 set_Block_block_visited(res->start_block, -1);
191 /* Frees the passed irgraph.
192 Deallocates all nodes in this graph and the ir_graph structure.
193 Sets the field irgraph in the corresponding entity to NULL.
194 Does not remove the irgraph from the list in irprog (requires
195 inefficient search, call remove_irp_irg by hand).
196 Does not free types, entities or modes that are used only by this
197 graph, nor the entity standing for this graph. */
198 void free_ir_graph (ir_graph *irg) {
199 set_entity_irg(irg->ent, NULL);
201 #if USE_EXPLICIT_PHI_IN_STACK
202 free_Phi_in_stack(irg->Phi_in_stack);
207 /* access routines for all ir_graph attributes:
209 {attr type} get_irg_{attribute name} (ir_graph *irg);
210 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
213 get_irg_start_block (ir_graph *irg)
215 return irg->start_block;
219 set_irg_start_block (ir_graph *irg, ir_node *node)
221 irg->start_block = node;
225 get_irg_start (ir_graph *irg)
231 set_irg_start(ir_graph *irg, ir_node *node)
237 get_irg_end_block (ir_graph *irg)
239 return irg->end_block;
243 set_irg_end_block (ir_graph *irg, ir_node *node)
245 irg->end_block = node;
249 get_irg_end (ir_graph *irg)
255 set_irg_end (ir_graph *irg, ir_node *node)
261 get_irg_cstore (ir_graph *irg)
267 set_irg_cstore (ir_graph *irg, ir_node *node)
273 get_irg_frame (ir_graph *irg)
279 set_irg_frame (ir_graph *irg, ir_node *node)
285 get_irg_globals (ir_graph *irg)
291 set_irg_globals (ir_graph *irg, ir_node *node)
297 get_irg_args (ir_graph *irg)
303 set_irg_args (ir_graph *irg, ir_node *node)
309 get_irg_bad (ir_graph *irg)
315 set_irg_bad (ir_graph *irg, ir_node *node)
321 get_irg_unknown (ir_graph *irg)
327 set_irg_unknown (ir_graph *irg, ir_node *node)
333 get_irg_current_block (ir_graph *irg)
335 return irg->current_block;
339 set_irg_current_block (ir_graph *irg, ir_node *node)
341 irg->current_block = node;
345 get_irg_ent (ir_graph *irg)
347 assert(irg && irg->ent);
352 set_irg_ent (ir_graph *irg, entity *ent)
358 get_irg_frame_type (ir_graph *irg)
360 assert(irg && irg->frame_type);
361 return irg->frame_type;
365 set_irg_frame_type (ir_graph *irg, type *ftp)
367 irg->frame_type = ftp;
371 /* To test for a frame type */
373 is_frame_type(type *ftp) {
374 return ((is_class_type(ftp) || is_struct_type(ftp)) &&
375 id_is_suffix(id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX)),
376 get_type_ident(ftp)));
380 get_irg_n_locs (ir_graph *irg)
382 #if PRECISE_EXC_CONTEXT
383 return irg->n_loc - 1 - 1;
385 return irg->n_loc - 1;
390 set_irg_n_loc (ir_graph *irg, int n_loc)
392 #if PRECISE_EXC_CONTEXT
393 irg->n_loc = n_loc + 1 + 1;
395 irg->n_loc = n_loc + 1;
400 get_irg_phase_state (ir_graph *irg) {
401 return irg->phase_state;
405 set_irg_phase_low(ir_graph *irg) {
406 irg->phase_state = phase_low;
410 get_irg_pinned (ir_graph *irg) {
415 get_irg_outs_state(ir_graph *irg) {
416 return irg->outs_state;
420 set_irg_outs_inconsistent(ir_graph *irg) {
421 irg->outs_state = outs_inconsistent;
425 get_irg_dom_state(ir_graph *irg) {
426 return irg->dom_state;
430 set_irg_dom_inconsistent(ir_graph *irg) {
431 irg->dom_state = dom_inconsistent;
435 set_irg_pinned (ir_graph *irg, op_pinned p) {
440 set_irg_link (ir_graph *irg, void *thing) {
445 get_irg_link (ir_graph *irg) {
449 /* maximum visited flag content of all ir_graph visited fields. */
450 static int max_irg_visited = 0;
453 get_irg_visited (ir_graph *irg)
459 set_irg_visited (ir_graph *irg, unsigned long visited)
461 irg->visited = visited;
462 if (irg->visited > max_irg_visited) {
463 max_irg_visited = irg->visited;
468 inc_irg_visited (ir_graph *irg)
470 if (++irg->visited > max_irg_visited) {
471 max_irg_visited = irg->visited;
476 get_max_irg_visited(void)
479 //for(i = 0; i < get_irp_n_irgs(); i++)
480 // assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
481 return max_irg_visited;
484 void set_max_irg_visited(int val) {
485 max_irg_visited = val;
489 inc_max_irg_visited(void)
492 // for(i = 0; i < get_irp_n_irgs(); i++)
493 //assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
495 return max_irg_visited;
499 get_irg_block_visited (ir_graph *irg)
501 return irg->block_visited;
505 set_irg_block_visited (ir_graph *irg, unsigned long visited)
507 irg->block_visited = visited;
511 inc_irg_block_visited (ir_graph *irg)
513 ++irg->block_visited;