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)
65 res = (ir_graph *) malloc (sizeof (ir_graph));
66 current_ir_graph = res;
67 add_irp_irg(res); /* remember this graph global. */
70 * initialized for each graph. **/
71 #if PRECISE_EXC_CONTEXT
72 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
73 dereferenced in this graph plus one for
74 the store plus one for links to fragile
75 operations. n_loc is not the number of
76 parameters to the procedure! */
78 res->n_loc = n_loc + 1; /* number of local variables that are never
79 dereferenced in this graph plus one for
80 the store. This is not the number of parameters
84 res->visited = 0; /* visited flag, for the ir walker */
85 res->block_visited=0; /* visited flag, for the 'block'-walker */
87 #if USE_EXPLICIT_PHI_IN_STACK
88 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
91 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
92 obstack_init (res->obst);
93 res->value_table = new_identities (); /* value table for global value
94 numbering for optimizing use in
98 res->phase_state = phase_building;
100 res->outs_state = no_outs;
101 res->dom_state = no_dom;
103 /** Type information for the procedure of the graph **/
105 set_entity_irg(ent, res);
108 contain "inner" methods as in Pascal. **/
109 res->frame_type = new_type_class(mangle(get_entity_ident(ent),
110 id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX))));
111 /* Remove type from type list. Must be treated differently than other types. */
112 remove_irp_type_from_list(res->frame_type);
114 /** Nodes needed in every graph **/
115 res->end_block = new_immBlock ();
116 res->end = new_End ();
118 res->start_block = new_immBlock ();
119 res->start = new_Start ();
120 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
121 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
123 /* Proj results of start node */
124 projX = new_Proj (res->start, mode_X, pns_initial_exec);
125 set_store (new_Proj (res->start, mode_M, pns_global_store));
126 res->frame = new_Proj (res->start, mode_P, pns_frame_base);
127 res->globals = new_Proj (res->start, mode_P, pns_globals);
128 res->args = new_Proj (res->start, mode_T, pns_args);
130 res->graph_nr = get_irp_new_node_nr();
134 add_in_edge(res->start_block, projX);
136 * The code generation needs it. leave it in now.
137 * Use of this edge is matter of discussion, unresolved. Also possible:
138 * add_in_edge(res->start_block, res->start_block), but invalid typed.
140 mature_block (res->current_block);
142 /** Make a block to start with **/
143 first_block = new_immBlock ();
144 add_in_edge (first_block, projX);
150 /* Make a rudimentary ir graph for the constant code.
151 Must look like a correct irg, spare everything else. */
152 ir_graph *new_const_code_irg() {
156 res = (ir_graph *) malloc (sizeof (ir_graph));
157 current_ir_graph = res;
158 res->n_loc = 1; /* Only the memory. */
159 res->visited = 0; /* visited flag, for the ir walker */
160 res->block_visited=0; /* visited flag, for the 'block'-walker */
161 #if USE_EXPLICIT_PHI_IN_STACK
162 res->Phi_in_stack = NULL;
164 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
165 obstack_init (res->obst);
166 res->phase_state = phase_building;
167 res->pinned = pinned;
168 res->value_table = new_identities (); /* value table for global value
169 numbering for optimizing use in
172 res->frame_type = NULL;
173 res->start_block = new_immBlock ();
174 res->end_block = new_immBlock ();
175 res->end = new_End ();
176 mature_block(get_cur_block());
177 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
178 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
179 res->start = new_Start ();
180 /* Proj results of start node */
181 projX = new_Proj (res->start, mode_X, pns_initial_exec);
182 set_store (new_Proj (res->start, mode_M, pns_global_store));
183 add_in_edge(res->start_block, projX);
184 mature_block (res->current_block);
185 add_in_edge (new_immBlock (), projX);
186 mature_block(get_cur_block());
187 /* Set the visited flag high enough that the block will never be visited. */
188 set_irn_visited(get_cur_block(), -1);
189 set_Block_block_visited(get_cur_block(), -1);
190 set_Block_block_visited(res->start_block, -1);
194 /* Frees the passed irgraph.
195 Deallocates all nodes in this graph and the ir_graph structure.
196 Sets the field irgraph in the corresponding entity to NULL.
197 Does not remove the irgraph from the list in irprog (requires
198 inefficient search, call remove_irp_irg by hand).
199 Does not free types, entities or modes that are used only by this
200 graph, nor the entity standing for this graph. */
201 void free_ir_graph (ir_graph *irg) {
202 set_entity_irg(irg->ent, NULL);
204 #if USE_EXPLICIT_PHI_IN_STACK
205 free_Phi_in_stack(irg->Phi_in_stack);
210 /* access routines for all ir_graph attributes:
212 {attr type} get_irg_{attribute name} (ir_graph *irg);
213 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
215 /* Outputs a unique number for this node */
218 get_irg_graph_nr(ir_graph *irg) {
221 return irg->graph_nr;
228 get_irg_start_block (ir_graph *irg)
230 return irg->start_block;
234 set_irg_start_block (ir_graph *irg, ir_node *node)
236 irg->start_block = node;
240 get_irg_start (ir_graph *irg)
246 set_irg_start(ir_graph *irg, ir_node *node)
252 get_irg_end_block (ir_graph *irg)
254 return irg->end_block;
258 set_irg_end_block (ir_graph *irg, ir_node *node)
260 irg->end_block = node;
264 get_irg_end (ir_graph *irg)
270 set_irg_end (ir_graph *irg, ir_node *node)
276 get_irg_cstore (ir_graph *irg)
282 set_irg_cstore (ir_graph *irg, ir_node *node)
288 get_irg_frame (ir_graph *irg)
294 set_irg_frame (ir_graph *irg, ir_node *node)
300 get_irg_globals (ir_graph *irg)
306 set_irg_globals (ir_graph *irg, ir_node *node)
312 get_irg_args (ir_graph *irg)
318 set_irg_args (ir_graph *irg, ir_node *node)
324 get_irg_bad (ir_graph *irg)
330 set_irg_bad (ir_graph *irg, ir_node *node)
336 get_irg_unknown (ir_graph *irg)
342 set_irg_unknown (ir_graph *irg, ir_node *node)
348 get_irg_current_block (ir_graph *irg)
350 return irg->current_block;
354 set_irg_current_block (ir_graph *irg, ir_node *node)
356 irg->current_block = node;
360 get_irg_ent (ir_graph *irg)
362 assert(irg && irg->ent);
367 set_irg_ent (ir_graph *irg, entity *ent)
373 get_irg_frame_type (ir_graph *irg)
375 assert(irg && irg->frame_type);
376 return irg->frame_type;
380 set_irg_frame_type (ir_graph *irg, type *ftp)
382 irg->frame_type = ftp;
386 /* To test for a frame type */
388 is_frame_type(type *ftp) {
389 return ((is_class_type(ftp) || is_struct_type(ftp)) &&
390 id_is_suffix(id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX)),
391 get_type_ident(ftp)));
395 get_irg_n_locs (ir_graph *irg)
397 #if PRECISE_EXC_CONTEXT
398 return irg->n_loc - 1 - 1;
400 return irg->n_loc - 1;
405 set_irg_n_loc (ir_graph *irg, int n_loc)
407 #if PRECISE_EXC_CONTEXT
408 irg->n_loc = n_loc + 1 + 1;
410 irg->n_loc = n_loc + 1;
415 get_irg_phase_state (ir_graph *irg) {
416 return irg->phase_state;
420 set_irg_phase_low(ir_graph *irg) {
421 irg->phase_state = phase_low;
425 get_irg_pinned (ir_graph *irg) {
430 get_irg_outs_state(ir_graph *irg) {
431 return irg->outs_state;
435 set_irg_outs_inconsistent(ir_graph *irg) {
436 irg->outs_state = outs_inconsistent;
440 get_irg_dom_state(ir_graph *irg) {
441 return irg->dom_state;
445 set_irg_dom_inconsistent(ir_graph *irg) {
446 irg->dom_state = dom_inconsistent;
450 set_irg_pinned (ir_graph *irg, op_pinned p) {
455 set_irg_link (ir_graph *irg, void *thing) {
460 get_irg_link (ir_graph *irg) {
464 /* maximum visited flag content of all ir_graph visited fields. */
465 static int max_irg_visited = 0;
468 get_irg_visited (ir_graph *irg)
474 set_irg_visited (ir_graph *irg, unsigned long visited)
476 irg->visited = visited;
477 if (irg->visited > max_irg_visited) {
478 max_irg_visited = irg->visited;
483 inc_irg_visited (ir_graph *irg)
485 if (++irg->visited > max_irg_visited) {
486 max_irg_visited = irg->visited;
491 get_max_irg_visited(void)
494 //for(i = 0; i < get_irp_n_irgs(); i++)
495 // assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
496 return max_irg_visited;
499 void set_max_irg_visited(int val) {
500 max_irg_visited = val;
504 inc_max_irg_visited(void)
507 // for(i = 0; i < get_irp_n_irgs(); i++)
508 //assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
510 return max_irg_visited;
514 get_irg_block_visited (ir_graph *irg)
516 return irg->block_visited;
520 set_irg_block_visited (ir_graph *irg, unsigned long visited)
522 irg->block_visited = visited;
526 inc_irg_block_visited (ir_graph *irg)
528 ++irg->block_visited;