3 * File name: ir/ir/irgraph.c
4 * Purpose: Entry point to the representation of procedure code.
5 * Author: Martin Trapp, Christian Schaefer
6 * Modified by: Goetz Lindenmaier
9 * Copyright: (c) 1998-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
20 # include "irgraph_t.h"
21 # include "irprog_t.h"
27 # include "firmstat.h"
29 ir_graph *current_ir_graph;
30 INLINE ir_graph *get_current_ir_graph(void) {
31 return current_ir_graph;
33 INLINE void set_current_ir_graph(ir_graph *graph) {
34 current_ir_graph = graph;
38 bool interprocedural_view = false;
39 INLINE bool get_interprocedural_view(void) {
40 return interprocedural_view;
42 INLINE void set_interprocedural_view(bool state) {
43 interprocedural_view = state;
46 static ident* frame_type_suffix = NULL;
47 void init_irgraph(void) {
48 frame_type_suffix = id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX));
51 #if USE_EXPLICIT_PHI_IN_STACK
52 /* really defined in ircons.c */
53 typedef struct Phi_in_stack Phi_in_stack;
54 Phi_in_stack *new_Phi_in_stack();
55 void free_Phi_in_stack(Phi_in_stack *s);
58 /* Allocates a list of nodes:
59 - The start block containing a start node and Proj nodes for it's four
60 results (X, M, P, Tuple).
61 - The end block containing an end node. This block is not matured after
62 new_ir_graph as predecessors need to be added to it.
63 - The current block, which is empty and also not matured.
64 Further it allocates several datastructures needed for graph construction
68 new_ir_graph (entity *ent, int n_loc)
74 res = (ir_graph *) malloc (sizeof (ir_graph));
75 memset(res, 0, sizeof (ir_graph));
78 /* inform statistics here, as blocks will be already build on this graph */
81 current_ir_graph = res;
82 add_irp_irg(res); /* remember this graph global. */
84 /*-- initialized for each graph. --*/
85 #if PRECISE_EXC_CONTEXT
86 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
87 dereferenced in this graph plus one for
88 the store plus one for links to fragile
89 operations. n_loc is not the number of
90 parameters to the procedure! */
92 res->n_loc = n_loc + 1; /* number of local variables that are never
93 dereferenced in this graph plus one for
94 the store. This is not the number of parameters
98 res->visited = 0; /* visited flag, for the ir walker */
99 res->block_visited=0; /* visited flag, for the 'block'-walker */
101 #if USE_EXPLICIT_PHI_IN_STACK
102 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
105 res->kind = k_ir_graph;
106 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
107 obstack_init (res->obst);
108 res->value_table = new_identities (); /* value table for global value
109 numbering for optimizing use in
113 res->phase_state = phase_building;
114 res->pinned = pinned;
115 res->outs_state = no_outs;
116 res->dom_state = no_dom;
117 res->typeinfo_state = irg_typeinfo_none;
118 res->loopinfo_state = loopinfo_none;
120 /*-- Type information for the procedure of the graph --*/
122 set_entity_irg(ent, res);
124 /*-- contain "inner" methods as in Pascal. --*/
125 res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
126 /* Remove type from type list. Must be treated differently than other types. */
127 remove_irp_type_from_list(res->frame_type);
129 /*-- Nodes needed in every graph --*/
130 res->end_block = new_immBlock ();
131 res->end = new_End ();
132 res->end_reg = res->end;
133 res->end_except = res->end;
135 res->start_block = new_immBlock ();
136 res->start = new_Start ();
137 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
138 /* res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL); */
140 /* Proj results of start node */
141 projX = new_Proj (res->start, mode_X, pns_initial_exec);
142 set_store (new_Proj (res->start, mode_M, pns_global_store));
143 res->frame = new_Proj (res->start, mode_P_mach, pns_frame_base);
144 res->globals = new_Proj (res->start, mode_P_mach, pns_globals);
145 res->args = new_Proj (res->start, mode_T, pns_args);
147 res->graph_nr = get_irp_new_node_nr();
151 add_in_edge(res->start_block, projX);
153 * The code generation needs it. leave it in now.
154 * Use of this edge is matter of discussion, unresolved. Also possible:
155 * add_in_edge(res->start_block, res->start_block), but invalid typed.
157 mature_block (res->current_block);
159 /*-- Make a block to start with --*/
160 first_block = new_immBlock ();
161 add_in_edge (first_block, projX);
168 /* Make a rudimentary ir graph for the constant code.
169 Must look like a correct irg, spare everything else. */
170 ir_graph *new_const_code_irg(void) {
174 res = (ir_graph *) malloc (sizeof(*res));
175 memset(res, 0, sizeof(*res));
177 current_ir_graph = res;
178 res->n_loc = 1; /* Only the memory. */
179 res->visited = 0; /* visited flag, for the ir walker */
180 res->block_visited=0; /* visited flag, for the 'block'-walker */
181 #if USE_EXPLICIT_PHI_IN_STACK
182 res->Phi_in_stack = NULL;
184 res->kind = k_ir_graph;
185 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
186 obstack_init (res->obst);
187 res->phase_state = phase_building;
188 res->pinned = pinned;
189 res->value_table = new_identities (); /* value table for global value
190 numbering for optimizing use in
193 res->frame_type = NULL;
194 res->start_block = new_immBlock ();
195 res->end_block = new_immBlock ();
196 res->end = new_End ();
197 res->end_reg = res->end;
198 res->end_except = res->end;
199 mature_block(get_cur_block());
200 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
201 /* res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL); */
202 res->start = new_Start ();
204 /* Proj results of start node */
205 projX = new_Proj (res->start, mode_X, pns_initial_exec);
206 set_store (new_Proj (res->start, mode_M, pns_global_store));
207 add_in_edge(res->start_block, projX);
208 mature_block (res->current_block);
209 add_in_edge (new_immBlock (), projX);
210 mature_block(get_cur_block());
211 /* Set the visited flag high enough that the block will never be visited. */
212 set_irn_visited(get_cur_block(), -1);
213 set_Block_block_visited(get_cur_block(), -1);
214 set_Block_block_visited(res->start_block, -1);
215 set_irn_visited(res->start_block, -1);
216 set_irn_visited(res->bad, -1);
220 /* Defined in iropt.c */
221 void del_identities (pset *value_table);
223 /* Frees the passed irgraph.
224 Deallocates all nodes in this graph and the ir_graph structure.
225 Sets the field irgraph in the corresponding entity to NULL.
226 Does not remove the irgraph from the list in irprog (requires
227 inefficient search, call remove_irp_irg by hand).
228 Does not free types, entities or modes that are used only by this
229 graph, nor the entity standing for this graph. */
230 void free_ir_graph (ir_graph *irg) {
231 if (irg->ent) set_entity_irg(irg->ent, NULL); /* not set in const code irg */
233 if (irg->frame_type) free_type(irg->frame_type);
234 if (irg->value_table) del_identities(irg->value_table);
235 if (irg->outs_state != no_outs) free_outs(irg);
236 obstack_free(irg->obst,NULL);
238 #if USE_EXPLICIT_PHI_IN_STACK
239 free_Phi_in_stack(irg->Phi_in_stack);
245 /* access routines for all ir_graph attributes:
247 {attr type} get_irg_{attribute name} (ir_graph *irg);
248 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
251 is_ir_graph(void *thing) {
253 if (get_kind(thing) == k_ir_graph)
259 /* Outputs a unique number for this node */
262 get_irg_graph_nr(ir_graph *irg) {
265 return irg->graph_nr;
272 get_irg_start_block (ir_graph *irg)
274 return irg->start_block;
278 set_irg_start_block (ir_graph *irg, ir_node *node)
280 irg->start_block = node;
284 get_irg_start (ir_graph *irg)
290 set_irg_start(ir_graph *irg, ir_node *node)
296 get_irg_end_block (ir_graph *irg)
298 return irg->end_block;
302 set_irg_end_block (ir_graph *irg, ir_node *node)
304 irg->end_block = node;
308 get_irg_end (ir_graph *irg)
314 set_irg_end (ir_graph *irg, ir_node *node)
320 get_irg_end_reg (ir_graph *irg) {
323 void set_irg_end_reg (ir_graph *irg, ir_node *node) {
324 assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
328 ir_node *get_irg_end_except (ir_graph *irg) {
329 return irg->end_except;
332 void set_irg_end_except (ir_graph *irg, ir_node *node) {
333 assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
334 irg->end_except = node;
338 get_irg_cstore (ir_graph *irg)
344 set_irg_cstore (ir_graph *irg, ir_node *node)
350 get_irg_frame (ir_graph *irg)
356 set_irg_frame (ir_graph *irg, ir_node *node)
362 get_irg_globals (ir_graph *irg)
368 set_irg_globals (ir_graph *irg, ir_node *node)
374 get_irg_args (ir_graph *irg)
380 set_irg_args (ir_graph *irg, ir_node *node)
386 get_irg_bad (ir_graph *irg)
392 set_irg_bad (ir_graph *irg, ir_node *node)
397 /* GL removed: we need unknown with mode for analyses.
399 get_irg_unknown (ir_graph *irg)
405 set_irg_unknown (ir_graph *irg, ir_node *node)
412 get_irg_current_block (ir_graph *irg)
414 return irg->current_block;
418 set_irg_current_block (ir_graph *irg, ir_node *node)
420 irg->current_block = node;
424 get_irg_ent (ir_graph *irg)
426 assert(irg && irg->ent);
431 set_irg_ent (ir_graph *irg, entity *ent)
437 get_irg_frame_type (ir_graph *irg)
439 assert(irg && irg->frame_type);
440 return irg->frame_type;
444 set_irg_frame_type (ir_graph *irg, type *ftp)
446 assert(is_class_type(ftp));
447 irg->frame_type = ftp;
451 /* To test for a frame type */
453 is_frame_type(type *ftp) {
455 if (is_class_type(ftp)) {
456 for (i = 0; i < get_irp_n_irgs(); i++) {
457 type *frame_tp = get_irg_frame_type(get_irp_irg(i));
458 if (ftp == frame_tp) return true;
465 get_irg_n_locs (ir_graph *irg)
467 #if PRECISE_EXC_CONTEXT
468 return irg->n_loc - 1 - 1;
470 return irg->n_loc - 1;
475 set_irg_n_loc (ir_graph *irg, int n_loc)
477 #if PRECISE_EXC_CONTEXT
478 irg->n_loc = n_loc + 1 + 1;
480 irg->n_loc = n_loc + 1;
486 /* Returns the obstack associated with the graph. */
487 struct obstack *get_irg_obstack(ir_graph *irg) {
492 * Returns true if the node n is allocated on the storage of graph irg.
494 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
496 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
498 struct _obstack_chunk *p;
501 * checks wheater the ir_node pointer i on the obstack.
502 * A more sophisticated check would test the "whole" ir_node
504 for (p = irg->obst->chunk; p; p = p->prev) {
505 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
513 get_irg_phase_state (ir_graph *irg) {
514 return irg->phase_state;
518 set_irg_phase_low(ir_graph *irg) {
519 irg->phase_state = phase_low;
523 get_irg_pinned (ir_graph *irg) {
528 get_irg_outs_state(ir_graph *irg) {
529 return irg->outs_state;
533 set_irg_outs_inconsistent(ir_graph *irg) {
534 irg->outs_state = outs_inconsistent;
538 get_irg_dom_state(ir_graph *irg) {
539 return irg->dom_state;
543 set_irg_dom_inconsistent(ir_graph *irg) {
544 irg->dom_state = dom_inconsistent;
548 get_irg_loopinfo_state(ir_graph *irg) {
549 return irg->loopinfo_state;
552 void set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
553 irg->loopinfo_state = s;
557 set_irg_loopinfo_inconsistent(ir_graph *irg) {
558 if (irg->loopinfo_state == loopinfo_ip_consistent)
559 irg->loopinfo_state = loopinfo_ip_inconsistent;
561 irg->loopinfo_state = loopinfo_inconsistent;
565 set_irg_pinned (ir_graph *irg, op_pinned p) {
570 irg_callee_info_state get_irg_callee_info_state(ir_graph *irg) {
571 return irg->callee_info_state;
574 void set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
575 irg->callee_info_state = s;
578 irg_inline_property get_irg_inline_property(ir_graph *irg) {
579 return irg->inline_property;
581 void set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
582 irg->inline_property = s;
587 set_irg_link (ir_graph *irg, void *thing) {
592 get_irg_link (ir_graph *irg) {
596 /* maximum visited flag content of all ir_graph visited fields. */
597 static int max_irg_visited = 0;
600 get_irg_visited (ir_graph *irg)
606 set_irg_visited (ir_graph *irg, unsigned long visited)
608 irg->visited = visited;
609 if (irg->visited > max_irg_visited) {
610 max_irg_visited = irg->visited;
615 inc_irg_visited (ir_graph *irg)
617 if (++irg->visited > max_irg_visited) {
618 max_irg_visited = irg->visited;
623 get_max_irg_visited(void)
627 for(i = 0; i < get_irp_n_irgs(); i++)
628 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
630 return max_irg_visited;
633 void set_max_irg_visited(int val) {
634 max_irg_visited = val;
638 inc_max_irg_visited(void)
642 for(i = 0; i < get_irp_n_irgs(); i++)
643 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
646 return max_irg_visited;
650 get_irg_block_visited (ir_graph *irg)
652 return irg->block_visited;
656 set_irg_block_visited (ir_graph *irg, unsigned long visited)
658 irg->block_visited = visited;
662 inc_irg_block_visited (ir_graph *irg)
664 ++irg->block_visited;