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(void) {
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(void) {
36 return interprocedural_view;
38 INLINE void set_interprocedural_view(bool state) {
39 interprocedural_view = state;
42 static ident* frame_type_suffix = NULL;
43 void init_irgraph(void) {
44 frame_type_suffix = id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX));
47 #if USE_EXPLICIT_PHI_IN_STACK
48 /* really defined in ircons.c */
49 typedef struct Phi_in_stack Phi_in_stack;
50 Phi_in_stack *new_Phi_in_stack();
51 void free_Phi_in_stack(Phi_in_stack *s);
54 /* Allocates a list of nodes:
55 - The start block containing a start node and Proj nodes for it's four
56 results (X, M, P, Tuple).
57 - The end block containing an end node. This block is not matured after
58 new_ir_graph as predecessors need to be added to it.
59 - The current block, which is empty and also not matured.
60 Further it allocates several datastructures needed for graph construction
64 new_ir_graph (entity *ent, int n_loc)
70 res = (ir_graph *) malloc (sizeof (ir_graph));
73 current_ir_graph = res;
74 add_irp_irg(res); /* remember this graph global. */
77 * initialized for each graph. **/
78 #if PRECISE_EXC_CONTEXT
79 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
80 dereferenced in this graph plus one for
81 the store plus one for links to fragile
82 operations. n_loc is not the number of
83 parameters to the procedure! */
85 res->n_loc = n_loc + 1; /* number of local variables that are never
86 dereferenced in this graph plus one for
87 the store. This is not the number of parameters
91 res->visited = 0; /* visited flag, for the ir walker */
92 res->block_visited=0; /* visited flag, for the 'block'-walker */
94 #if USE_EXPLICIT_PHI_IN_STACK
95 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
98 res->kind = k_ir_graph;
99 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
100 obstack_init (res->obst);
101 res->value_table = new_identities (); /* value table for global value
102 numbering for optimizing use in
106 res->phase_state = phase_building;
107 res->pinned = pinned;
108 res->outs_state = no_outs;
109 res->dom_state = no_dom;
111 /** Type information for the procedure of the graph **/
113 set_entity_irg(ent, res);
116 contain "inner" methods as in Pascal. **/
117 res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
118 /* Remove type from type list. Must be treated differently than other types. */
119 remove_irp_type_from_list(res->frame_type);
121 /** Nodes needed in every graph **/
122 res->end_block = new_immBlock ();
123 res->end = new_End ();
125 res->start_block = new_immBlock ();
126 res->start = new_Start ();
127 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
128 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
130 /* Proj results of start node */
131 projX = new_Proj (res->start, mode_X, pns_initial_exec);
132 set_store (new_Proj (res->start, mode_M, pns_global_store));
133 res->frame = new_Proj (res->start, mode_P_mach, pns_frame_base);
134 res->globals = new_Proj (res->start, mode_P_mach, pns_globals);
135 res->args = new_Proj (res->start, mode_T, pns_args);
137 res->graph_nr = get_irp_new_node_nr();
141 add_in_edge(res->start_block, projX);
143 * The code generation needs it. leave it in now.
144 * Use of this edge is matter of discussion, unresolved. Also possible:
145 * add_in_edge(res->start_block, res->start_block), but invalid typed.
147 mature_block (res->current_block);
149 /** Make a block to start with **/
150 first_block = new_immBlock ();
151 add_in_edge (first_block, projX);
157 /* Make a rudimentary ir graph for the constant code.
158 Must look like a correct irg, spare everything else. */
159 ir_graph *new_const_code_irg() {
163 res = (ir_graph *) malloc (sizeof (ir_graph));
164 current_ir_graph = res;
165 res->n_loc = 1; /* Only the memory. */
166 res->visited = 0; /* visited flag, for the ir walker */
167 res->block_visited=0; /* visited flag, for the 'block'-walker */
168 #if USE_EXPLICIT_PHI_IN_STACK
169 res->Phi_in_stack = NULL;
171 res->kind = k_ir_graph;
172 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
173 obstack_init (res->obst);
174 res->phase_state = phase_building;
175 res->pinned = pinned;
176 res->value_table = new_identities (); /* value table for global value
177 numbering for optimizing use in
180 res->frame_type = NULL;
181 res->start_block = new_immBlock ();
182 res->end_block = new_immBlock ();
183 res->end = new_End ();
184 mature_block(get_cur_block());
185 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
186 res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
187 res->start = new_Start ();
189 /* Proj results of start node */
190 projX = new_Proj (res->start, mode_X, pns_initial_exec);
191 set_store (new_Proj (res->start, mode_M, pns_global_store));
192 add_in_edge(res->start_block, projX);
193 mature_block (res->current_block);
194 add_in_edge (new_immBlock (), projX);
195 mature_block(get_cur_block());
196 /* Set the visited flag high enough that the block will never be visited. */
197 set_irn_visited(get_cur_block(), -1);
198 set_Block_block_visited(get_cur_block(), -1);
199 set_Block_block_visited(res->start_block, -1);
203 /* Frees the passed irgraph.
204 Deallocates all nodes in this graph and the ir_graph structure.
205 Sets the field irgraph in the corresponding entity to NULL.
206 Does not remove the irgraph from the list in irprog (requires
207 inefficient search, call remove_irp_irg by hand).
208 Does not free types, entities or modes that are used only by this
209 graph, nor the entity standing for this graph. */
210 void free_ir_graph (ir_graph *irg) {
211 set_entity_irg(irg->ent, NULL);
213 #if USE_EXPLICIT_PHI_IN_STACK
214 free_Phi_in_stack(irg->Phi_in_stack);
219 /* access routines for all ir_graph attributes:
221 {attr type} get_irg_{attribute name} (ir_graph *irg);
222 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
225 is_ir_graph(void *thing) {
227 if (get_kind(thing) == k_ir_graph)
233 /* Outputs a unique number for this node */
236 get_irg_graph_nr(ir_graph *irg) {
239 return irg->graph_nr;
246 get_irg_start_block (ir_graph *irg)
248 return irg->start_block;
252 set_irg_start_block (ir_graph *irg, ir_node *node)
254 irg->start_block = node;
258 get_irg_start (ir_graph *irg)
264 set_irg_start(ir_graph *irg, ir_node *node)
270 get_irg_end_block (ir_graph *irg)
272 return irg->end_block;
276 set_irg_end_block (ir_graph *irg, ir_node *node)
278 irg->end_block = node;
282 get_irg_end (ir_graph *irg)
288 set_irg_end (ir_graph *irg, ir_node *node)
294 get_irg_cstore (ir_graph *irg)
300 set_irg_cstore (ir_graph *irg, ir_node *node)
306 get_irg_frame (ir_graph *irg)
312 set_irg_frame (ir_graph *irg, ir_node *node)
318 get_irg_globals (ir_graph *irg)
324 set_irg_globals (ir_graph *irg, ir_node *node)
330 get_irg_args (ir_graph *irg)
336 set_irg_args (ir_graph *irg, ir_node *node)
342 get_irg_bad (ir_graph *irg)
348 set_irg_bad (ir_graph *irg, ir_node *node)
354 get_irg_unknown (ir_graph *irg)
360 set_irg_unknown (ir_graph *irg, ir_node *node)
366 get_irg_current_block (ir_graph *irg)
368 return irg->current_block;
372 set_irg_current_block (ir_graph *irg, ir_node *node)
374 irg->current_block = node;
378 get_irg_ent (ir_graph *irg)
380 assert(irg && irg->ent);
385 set_irg_ent (ir_graph *irg, entity *ent)
391 get_irg_frame_type (ir_graph *irg)
393 assert(irg && irg->frame_type);
394 return irg->frame_type;
398 set_irg_frame_type (ir_graph *irg, type *ftp)
400 assert(is_class_type(ftp));
401 irg->frame_type = ftp;
405 /* To test for a frame type */
407 is_frame_type(type *ftp) {
409 if (is_class_type(ftp)) {
410 for (i = 0; i < get_irp_n_irgs(); i++) {
411 type *frame_tp = get_irg_frame_type(get_irp_irg(i));
412 if (ftp == frame_tp) return true;
419 get_irg_n_locs (ir_graph *irg)
421 #if PRECISE_EXC_CONTEXT
422 return irg->n_loc - 1 - 1;
424 return irg->n_loc - 1;
429 set_irg_n_loc (ir_graph *irg, int n_loc)
431 #if PRECISE_EXC_CONTEXT
432 irg->n_loc = n_loc + 1 + 1;
434 irg->n_loc = n_loc + 1;
440 /* Returns the obstack associated with the graph. */
441 struct obstack *get_irg_obstack(ir_graph *irg) {
446 * Returns true if the node n is allocated on the storage of graph irg.
448 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
450 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
452 struct _obstack_chunk *p;
455 * checks wheater the ir_node pointer i on the obstack.
456 * A more sophisticated chaeck would test the "whole" ir_node
458 for (p = irg->obst->chunk; p; p = p->prev) {
459 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
467 get_irg_phase_state (ir_graph *irg) {
468 return irg->phase_state;
472 set_irg_phase_low(ir_graph *irg) {
473 irg->phase_state = phase_low;
477 get_irg_pinned (ir_graph *irg) {
482 get_irg_outs_state(ir_graph *irg) {
483 return irg->outs_state;
487 set_irg_outs_inconsistent(ir_graph *irg) {
488 irg->outs_state = outs_inconsistent;
492 get_irg_dom_state(ir_graph *irg) {
493 return irg->dom_state;
497 set_irg_dom_inconsistent(ir_graph *irg) {
498 irg->dom_state = dom_inconsistent;
502 set_irg_pinned (ir_graph *irg, op_pinned p) {
507 set_irg_link (ir_graph *irg, void *thing) {
512 get_irg_link (ir_graph *irg) {
516 /* maximum visited flag content of all ir_graph visited fields. */
517 static int max_irg_visited = 0;
520 get_irg_visited (ir_graph *irg)
526 set_irg_visited (ir_graph *irg, unsigned long visited)
528 irg->visited = visited;
529 if (irg->visited > max_irg_visited) {
530 max_irg_visited = irg->visited;
535 inc_irg_visited (ir_graph *irg)
537 if (++irg->visited > max_irg_visited) {
538 max_irg_visited = irg->visited;
543 get_max_irg_visited(void)
547 for(i = 0; i < get_irp_n_irgs(); i++)
548 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
550 return max_irg_visited;
553 void set_max_irg_visited(int val) {
554 max_irg_visited = val;
558 inc_max_irg_visited(void)
562 for(i = 0; i < get_irp_n_irgs(); i++)
563 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
566 return max_irg_visited;
570 get_irg_block_visited (ir_graph *irg)
572 return irg->block_visited;
576 set_irg_block_visited (ir_graph *irg, unsigned long visited)
578 irg->block_visited = visited;
582 inc_irg_block_visited (ir_graph *irg)
584 ++irg->block_visited;