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"
22 # include "irnode_t.h"
24 # include "irflag_t.h"
29 # include "firmstat.h"
31 ir_graph *current_ir_graph;
32 INLINE ir_graph *get_current_ir_graph(void) {
33 return current_ir_graph;
35 INLINE void set_current_ir_graph(ir_graph *graph) {
36 current_ir_graph = graph;
40 int __interprocedural_view = false;
42 int (get_interprocedural_view)(void) {
43 return __get_interprocedural_view();
46 void (set_interprocedural_view)(int state) {
47 __interprocedural_view = state;
49 /* set function vectors for faster access */
51 __get_irn_arity = __get_irn_inter_arity;
52 __get_irn_n = __get_irn_inter_n;
55 __get_irn_arity = __get_irn_intra_arity;
56 __get_irn_n = __get_irn_intra_n;
60 static ident* frame_type_suffix = NULL;
61 void init_irgraph(void) {
62 frame_type_suffix = new_id_from_chars(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX));
65 #if USE_EXPLICIT_PHI_IN_STACK
66 /* really defined in ircons.c */
67 typedef struct Phi_in_stack Phi_in_stack;
68 Phi_in_stack *new_Phi_in_stack();
69 void free_Phi_in_stack(Phi_in_stack *s);
72 /* Allocates a list of nodes:
73 - The start block containing a start node and Proj nodes for it's four
74 results (X, M, P, Tuple).
75 - The end block containing an end node. This block is not matured after
76 new_ir_graph as predecessors need to be added to it.
77 - The current block, which is empty and also not matured.
78 Further it allocates several datastructures needed for graph construction
82 new_r_ir_graph (entity *ent, int n_loc)
88 res = (ir_graph *) malloc (sizeof (ir_graph));
89 memset(res, 0, sizeof (ir_graph));
90 res->kind = k_ir_graph;
92 /* inform statistics here, as blocks will be already build on this graph */
93 stat_new_graph(res, ent);
95 current_ir_graph = res;
97 /*-- initialized for each graph. --*/
98 if (get_opt_precise_exc_context()) {
99 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
100 dereferenced in this graph plus one for
101 the store plus one for links to fragile
102 operations. n_loc is not the number of
103 parameters to the procedure! */
106 res->n_loc = n_loc + 1; /* number of local variables that are never
107 dereferenced in this graph plus one for
108 the store. This is not the number of parameters
112 res->visited = 0; /* visited flag, for the ir walker */
113 res->block_visited = 0; /* visited flag, for the 'block'-walker */
115 #if USE_EXPLICIT_PHI_IN_STACK
116 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
119 res->kind = k_ir_graph;
120 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
121 obstack_init (res->obst);
122 res->value_table = new_identities (); /* value table for global value
123 numbering for optimizing use in
127 res->phase_state = phase_building;
128 res->op_pin_state_pinned = op_pin_state_pinned;
129 res->outs_state = outs_none;
130 res->dom_state = dom_none;
131 res->typeinfo_state = irg_typeinfo_none;
132 res->loopinfo_state = loopinfo_none;
134 /*-- Type information for the procedure of the graph --*/
136 set_entity_irg(ent, res);
138 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
139 res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
141 /* Remove type from type list. Must be treated differently than other types. */
142 remove_irp_type_from_list(res->frame_type);
144 /*-- Nodes needed in every graph --*/
145 res->end_block = new_immBlock();
146 res->end = new_End();
147 res->end_reg = res->end;
148 res->end_except = res->end;
150 res->start_block = new_immBlock();
151 res->start = new_Start();
152 res->bad = new_ir_node(NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
153 res->no_mem = new_ir_node(NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
155 /* Proj results of start node */
156 projX = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
157 res->frame = new_Proj (res->start, mode_P_mach, pn_Start_P_frame_base);
158 res->globals = new_Proj (res->start, mode_P_mach, pn_Start_P_globals);
159 res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
160 res->args = new_Proj (res->start, mode_T, pn_Start_T_args);
162 res->graph_nr = get_irp_new_node_nr();
165 set_store(res->initial_mem);
167 add_immBlock_pred(res->start_block, projX);
169 * The code generation needs it. leave it in now.
170 * Use of this edge is matter of discussion, unresolved. Also possible:
171 * add_immBlock_pred(res->start_block, res->start_block), but invalid typed.
173 mature_immBlock (res->current_block);
175 /*-- Make a block to start with --*/
176 first_block = new_immBlock();
177 add_immBlock_pred (first_block, projX);
184 new_ir_graph (entity *ent, int n_loc)
186 ir_graph *res = new_r_ir_graph (ent, n_loc);
187 add_irp_irg(res); /* remember this graph global. */
191 /* Make a rudimentary ir graph for the constant code.
192 Must look like a correct irg, spare everything else. */
193 ir_graph *new_const_code_irg(void) {
197 res = (ir_graph *) malloc (sizeof(*res));
198 memset(res, 0, sizeof(*res));
200 /* inform statistics here, as blocks will be already build on this graph */
201 stat_new_graph(res, NULL);
203 current_ir_graph = res;
204 res->n_loc = 1; /* Only the memory. */
205 res->visited = 0; /* visited flag, for the ir walker */
206 res->block_visited=0; /* visited flag, for the 'block'-walker */
207 #if USE_EXPLICIT_PHI_IN_STACK
208 res->Phi_in_stack = NULL;
210 res->kind = k_ir_graph;
211 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
212 obstack_init (res->obst);
213 res->phase_state = phase_building;
214 res->op_pin_state_pinned = op_pin_state_pinned;
215 res->value_table = new_identities (); /* value table for global value
216 numbering for optimizing use in
219 res->frame_type = NULL;
220 res->start_block = new_immBlock ();
221 res->end_block = new_immBlock ();
222 res->end = new_End ();
223 res->end_reg = res->end;
224 res->end_except = res->end;
225 mature_immBlock(get_cur_block()); /* mature the end block */
226 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
227 res->no_mem = new_ir_node (NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
228 res->start = new_Start ();
229 res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
231 /* Proj results of start node */
232 projX = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
233 add_immBlock_pred (res->start_block, projX);
234 mature_immBlock (res->start_block); /* mature the start block */
236 add_immBlock_pred (new_immBlock (), projX);
237 mature_immBlock (get_cur_block()); /* mature the 'body' block for expressions */
238 /* Set the visited flag high enough that the block will never be visited. */
239 set_irn_visited(get_cur_block(), -1);
240 set_Block_block_visited(get_cur_block(), -1);
241 set_Block_block_visited(res->start_block, -1);
242 set_irn_visited(res->start_block, -1);
243 set_irn_visited(res->bad, -1);
244 set_irn_visited(res->no_mem, -1);
246 res->phase_state = phase_high;
250 /* Defined in iropt.c */
251 void del_identities (pset *value_table);
253 /* Frees the passed irgraph.
254 Deallocates all nodes in this graph and the ir_graph structure.
255 Sets the field irgraph in the corresponding entity to NULL.
256 Does not remove the irgraph from the list in irprog (requires
257 inefficient search, call remove_irp_irg by hand).
258 Does not free types, entities or modes that are used only by this
259 graph, nor the entity standing for this graph. */
260 void free_ir_graph (ir_graph *irg) {
262 stat_free_graph(irg);
263 if (irg->outs_state != outs_none) free_outs(irg);
264 if (irg->frame_type) free_type(irg->frame_type);
265 if (irg->value_table) del_identities(irg->value_table);
267 peculiarity pec = get_entity_peculiarity (irg->ent);
268 set_entity_peculiarity (irg->ent, peculiarity_description);
269 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
270 set_entity_peculiarity (irg->ent, pec);
274 obstack_free(irg->obst,NULL);
276 #if USE_EXPLICIT_PHI_IN_STACK
277 free_Phi_in_stack(irg->Phi_in_stack);
283 /* access routines for all ir_graph attributes:
285 {attr type} get_irg_{attribute name} (ir_graph *irg);
286 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
289 (is_ir_graph)(const void *thing) {
290 return __is_ir_graph(thing);
293 /* Outputs a unique number for this node */
296 get_irg_graph_nr(ir_graph *irg) {
299 return irg->graph_nr;
306 (get_irg_start_block)(const ir_graph *irg) {
307 return __get_irg_start_block(irg);
311 (set_irg_start_block)(ir_graph *irg, ir_node *node) {
312 __set_irg_start_block(irg, node);
316 (get_irg_start)(const ir_graph *irg) {
317 return __get_irg_start(irg);
321 (set_irg_start)(ir_graph *irg, ir_node *node) {
322 __set_irg_start(irg, node);
326 (get_irg_end_block)(const ir_graph *irg) {
327 return __get_irg_end_block(irg);
331 (set_irg_end_block)(ir_graph *irg, ir_node *node) {
332 __set_irg_end_block(irg, node);
336 (get_irg_end)(const ir_graph *irg) {
337 return __get_irg_end(irg);
341 (set_irg_end)(ir_graph *irg, ir_node *node) {
342 __set_irg_end(irg, node);
346 (get_irg_end_reg)(const ir_graph *irg) {
347 return __get_irg_end_reg(irg);
350 void set_irg_end_reg (ir_graph *irg, ir_node *node) {
351 assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
356 (get_irg_end_except)(const ir_graph *irg) {
357 return __get_irg_end_except(irg);
360 void set_irg_end_except (ir_graph *irg, ir_node *node) {
361 assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
362 irg->end_except = node;
366 (get_irg_cstore)(const ir_graph *irg) {
367 return __get_irg_cstore(irg);
371 (set_irg_cstore)(ir_graph *irg, ir_node *node) {
372 __set_irg_cstore(irg, node);
376 (get_irg_frame)(const ir_graph *irg) {
377 return __get_irg_frame(irg);
381 (set_irg_frame)(ir_graph *irg, ir_node *node) {
382 __set_irg_frame(irg, node);
386 (get_irg_globals)(const ir_graph *irg) {
387 return __get_irg_globals(irg);
391 (set_irg_globals)(ir_graph *irg, ir_node *node) {
392 __set_irg_globals(irg, node);
396 (get_irg_initial_mem)(const ir_graph *irg) {
397 return __get_irg_initial_mem(irg);
401 (set_irg_initial_mem)(ir_graph *irg, ir_node *node) {
402 __set_irg_initial_mem(irg, node);
406 (get_irg_args)(const ir_graph *irg) {
407 return __get_irg_args(irg);
411 (set_irg_args)(ir_graph *irg, ir_node *node) {
412 __set_irg_args(irg, node);
416 (get_irg_bad)(const ir_graph *irg) {
417 return __get_irg_bad(irg);
421 (set_irg_bad)(ir_graph *irg, ir_node *node) {
422 __set_irg_bad(irg, node);
426 (get_irg_no_mem)(const ir_graph *irg) {
427 return __get_irg_no_mem(irg);
431 (set_irg_no_mem)(ir_graph *irg, ir_node *node) {
432 __set_irg_no_mem(irg, node);
436 (get_irg_current_block)(const ir_graph *irg) {
437 return __get_irg_current_block(irg);
441 (set_irg_current_block)(ir_graph *irg, ir_node *node) {
442 __set_irg_current_block(irg, node);
446 (get_irg_entity)(const ir_graph *irg) {
447 return __get_irg_entity(irg);
451 (set_irg_entity)(ir_graph *irg, entity *ent) {
452 __set_irg_entity(irg, ent);
456 (get_irg_frame_type)(const ir_graph *irg) {
457 return __get_irg_frame_type(irg);
461 (set_irg_frame_type)(ir_graph *irg, type *ftp) {
462 __set_irg_frame_type(irg, ftp);
466 /* To test for a frame type */
468 is_frame_type(const type *ftp) {
470 if (is_class_type(ftp)) {
471 for (i = 0; i < get_irp_n_irgs(); i++) {
472 const type *frame_tp = get_irg_frame_type(get_irp_irg(i));
473 if (ftp == frame_tp) return true;
480 get_irg_n_locs (ir_graph *irg)
482 if (get_opt_precise_exc_context())
483 return irg->n_loc - 1 - 1;
485 return irg->n_loc - 1;
489 set_irg_n_loc (ir_graph *irg, int n_loc)
491 if (get_opt_precise_exc_context())
492 irg->n_loc = n_loc + 1 + 1;
494 irg->n_loc = n_loc + 1;
499 /* Returns the obstack associated with the graph. */
501 (get_irg_obstack)(const ir_graph *irg) {
502 return __get_irg_obstack(irg);
506 * Returns true if the node n is allocated on the storage of graph irg.
508 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
510 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
512 struct _obstack_chunk *p;
515 * checks wheater the ir_node pointer i on the obstack.
516 * A more sophisticated check would test the "whole" ir_node
518 for (p = irg->obst->chunk; p; p = p->prev) {
519 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
527 (get_irg_phase_state)(const ir_graph *irg) {
528 return __get_irg_phase_state(irg);
532 (set_irg_phase_low)(ir_graph *irg) {
533 __set_irg_phase_low(irg);
537 (get_irg_pinned)(const ir_graph *irg) {
538 return __get_irg_pinned(irg);
542 (get_irg_outs_state)(const ir_graph *irg) {
543 return __get_irg_outs_state(irg);
547 (set_irg_outs_inconsistent)(ir_graph *irg) {
548 __set_irg_outs_inconsistent(irg);
552 (get_irg_dom_state)(const ir_graph *irg) {
553 return __get_irg_dom_state(irg);
557 (set_irg_dom_inconsistent)(ir_graph *irg) {
558 __set_irg_dom_inconsistent(irg);
562 (get_irg_loopinfo_state)(const ir_graph *irg) {
563 return __get_irg_loopinfo_state(irg);
567 (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s) {
568 __set_irg_loopinfo_state(irg, s);
572 set_irg_loopinfo_inconsistent(ir_graph *irg) {
573 if (irg->loopinfo_state == loopinfo_ip_consistent)
574 irg->loopinfo_state = loopinfo_ip_inconsistent;
576 else if (irg->loopinfo_state == loopinfo_consistent)
577 irg->loopinfo_state = loopinfo_inconsistent;
579 else if (irg->loopinfo_state == loopinfo_cf_ip_consistent)
580 irg->loopinfo_state = loopinfo_cf_ip_inconsistent;
582 else if (irg->loopinfo_state == loopinfo_cf_consistent)
583 irg->loopinfo_state = loopinfo_cf_inconsistent;
587 (set_irg_pinned)(ir_graph *irg, op_pin_state p) {
588 __set_irg_pinned(irg, p);
591 irg_callee_info_state
592 (get_irg_callee_info_state)(const ir_graph *irg) {
593 return __get_irg_callee_info_state(irg);
597 (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s) {
598 __set_irg_callee_info_state(irg, s);
602 (get_irg_inline_property)(const ir_graph *irg) {
603 return __get_irg_inline_property(irg);
607 (set_irg_inline_property)(ir_graph *irg, irg_inline_property s) {
608 __set_irg_inline_property(irg, s);
612 (set_irg_link)(ir_graph *irg, void *thing) {
613 __set_irg_link(irg, thing);
617 (get_irg_link)(const ir_graph *irg) {
618 return __get_irg_link(irg);
621 /* maximum visited flag content of all ir_graph visited fields. */
622 static int max_irg_visited = 0;
625 (get_irg_visited)(const ir_graph *irg) {
626 return __get_irg_visited(irg);
630 set_irg_visited (ir_graph *irg, unsigned long visited)
632 irg->visited = visited;
633 if (irg->visited > max_irg_visited) {
634 max_irg_visited = irg->visited;
639 inc_irg_visited (ir_graph *irg)
641 if (++irg->visited > max_irg_visited) {
642 max_irg_visited = irg->visited;
647 get_max_irg_visited(void)
651 for(i = 0; i < get_irp_n_irgs(); i++)
652 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
654 return max_irg_visited;
657 void set_max_irg_visited(int val) {
658 max_irg_visited = val;
662 inc_max_irg_visited(void)
666 for(i = 0; i < get_irp_n_irgs(); i++)
667 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
670 return max_irg_visited;
674 (get_irg_block_visited)(const ir_graph *irg) {
675 return __get_irg_block_visited(irg);
679 (set_irg_block_visited)(ir_graph *irg, unsigned long visited) {
680 __set_irg_block_visited(irg, visited);
684 (inc_irg_block_visited)(ir_graph *irg) {
685 __inc_irg_block_visited(irg);