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 ();
188 /* Proj results of start node */
189 projX = new_Proj (res->start, mode_X, pns_initial_exec);
190 set_store (new_Proj (res->start, mode_M, pns_global_store));
191 add_in_edge(res->start_block, projX);
192 mature_block (res->current_block);
193 add_in_edge (new_immBlock (), projX);
194 mature_block(get_cur_block());
195 /* Set the visited flag high enough that the block will never be visited. */
196 set_irn_visited(get_cur_block(), -1);
197 set_Block_block_visited(get_cur_block(), -1);
198 set_Block_block_visited(res->start_block, -1);
202 /* Frees the passed irgraph.
203 Deallocates all nodes in this graph and the ir_graph structure.
204 Sets the field irgraph in the corresponding entity to NULL.
205 Does not remove the irgraph from the list in irprog (requires
206 inefficient search, call remove_irp_irg by hand).
207 Does not free types, entities or modes that are used only by this
208 graph, nor the entity standing for this graph. */
209 void free_ir_graph (ir_graph *irg) {
210 set_entity_irg(irg->ent, NULL);
212 #if USE_EXPLICIT_PHI_IN_STACK
213 free_Phi_in_stack(irg->Phi_in_stack);
218 /* access routines for all ir_graph attributes:
220 {attr type} get_irg_{attribute name} (ir_graph *irg);
221 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
224 is_ir_graph(void *thing) {
226 if (get_kind(thing) == k_ir_graph)
232 /* Outputs a unique number for this node */
235 get_irg_graph_nr(ir_graph *irg) {
238 return irg->graph_nr;
245 get_irg_start_block (ir_graph *irg)
247 return irg->start_block;
251 set_irg_start_block (ir_graph *irg, ir_node *node)
253 irg->start_block = node;
257 get_irg_start (ir_graph *irg)
263 set_irg_start(ir_graph *irg, ir_node *node)
269 get_irg_end_block (ir_graph *irg)
271 return irg->end_block;
275 set_irg_end_block (ir_graph *irg, ir_node *node)
277 irg->end_block = node;
281 get_irg_end (ir_graph *irg)
287 set_irg_end (ir_graph *irg, ir_node *node)
293 get_irg_cstore (ir_graph *irg)
299 set_irg_cstore (ir_graph *irg, ir_node *node)
305 get_irg_frame (ir_graph *irg)
311 set_irg_frame (ir_graph *irg, ir_node *node)
317 get_irg_globals (ir_graph *irg)
323 set_irg_globals (ir_graph *irg, ir_node *node)
329 get_irg_args (ir_graph *irg)
335 set_irg_args (ir_graph *irg, ir_node *node)
341 get_irg_bad (ir_graph *irg)
347 set_irg_bad (ir_graph *irg, ir_node *node)
353 get_irg_unknown (ir_graph *irg)
359 set_irg_unknown (ir_graph *irg, ir_node *node)
365 get_irg_current_block (ir_graph *irg)
367 return irg->current_block;
371 set_irg_current_block (ir_graph *irg, ir_node *node)
373 irg->current_block = node;
377 get_irg_ent (ir_graph *irg)
379 assert(irg && irg->ent);
384 set_irg_ent (ir_graph *irg, entity *ent)
390 get_irg_frame_type (ir_graph *irg)
392 assert(irg && irg->frame_type);
393 return irg->frame_type;
397 set_irg_frame_type (ir_graph *irg, type *ftp)
399 irg->frame_type = ftp;
403 /* To test for a frame type */
405 is_frame_type(type *ftp) {
406 return ((is_class_type(ftp) || is_struct_type(ftp)) &&
407 id_is_suffix(id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX)),
408 get_type_ident(ftp)));
412 get_irg_n_locs (ir_graph *irg)
414 #if PRECISE_EXC_CONTEXT
415 return irg->n_loc - 1 - 1;
417 return irg->n_loc - 1;
422 set_irg_n_loc (ir_graph *irg, int n_loc)
424 #if PRECISE_EXC_CONTEXT
425 irg->n_loc = n_loc + 1 + 1;
427 irg->n_loc = n_loc + 1;
433 /* Returns the obstack associated with the graph. */
434 struct obstack *get_irg_obstack(ir_graph *irg) {
439 * Returns true if the node n is allocated on the storage of graph irg.
441 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
443 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
445 struct _obstack_chunk *p;
448 * checks wheater the ir_node pointer i on the obstack.
449 * A more sophisticated chaeck would test the "whole" ir_node
451 for (p = irg->obst->chunk; p; p = p->prev) {
452 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
460 get_irg_phase_state (ir_graph *irg) {
461 return irg->phase_state;
465 set_irg_phase_low(ir_graph *irg) {
466 irg->phase_state = phase_low;
470 get_irg_pinned (ir_graph *irg) {
475 get_irg_outs_state(ir_graph *irg) {
476 return irg->outs_state;
480 set_irg_outs_inconsistent(ir_graph *irg) {
481 irg->outs_state = outs_inconsistent;
485 get_irg_dom_state(ir_graph *irg) {
486 return irg->dom_state;
490 set_irg_dom_inconsistent(ir_graph *irg) {
491 irg->dom_state = dom_inconsistent;
495 set_irg_pinned (ir_graph *irg, op_pinned p) {
500 set_irg_link (ir_graph *irg, void *thing) {
505 get_irg_link (ir_graph *irg) {
509 /* maximum visited flag content of all ir_graph visited fields. */
510 static int max_irg_visited = 0;
513 get_irg_visited (ir_graph *irg)
519 set_irg_visited (ir_graph *irg, unsigned long visited)
521 irg->visited = visited;
522 if (irg->visited > max_irg_visited) {
523 max_irg_visited = irg->visited;
528 inc_irg_visited (ir_graph *irg)
530 if (++irg->visited > max_irg_visited) {
531 max_irg_visited = irg->visited;
536 get_max_irg_visited(void)
540 for(i = 0; i < get_irp_n_irgs(); i++)
541 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
543 return max_irg_visited;
546 void set_max_irg_visited(int val) {
547 max_irg_visited = val;
551 inc_max_irg_visited(void)
555 for(i = 0; i < get_irp_n_irgs(); i++)
556 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
559 return max_irg_visited;
563 get_irg_block_visited (ir_graph *irg)
565 return irg->block_visited;
569 set_irg_block_visited (ir_graph *irg, unsigned long visited)
571 irg->block_visited = visited;
575 inc_irg_block_visited (ir_graph *irg)
577 ++irg->block_visited;