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_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;
96 add_irp_irg(res); /* remember this graph global. */
98 /*-- initialized for each graph. --*/
99 if (get_opt_precise_exc_context()) {
100 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
101 dereferenced in this graph plus one for
102 the store plus one for links to fragile
103 operations. n_loc is not the number of
104 parameters to the procedure! */
107 res->n_loc = n_loc + 1; /* number of local variables that are never
108 dereferenced in this graph plus one for
109 the store. This is not the number of parameters
113 res->visited = 0; /* visited flag, for the ir walker */
114 res->block_visited = 0; /* visited flag, for the 'block'-walker */
116 #if USE_EXPLICIT_PHI_IN_STACK
117 res->Phi_in_stack = new_Phi_in_stack(); /* A stack needed for automatic Phi
120 res->kind = k_ir_graph;
121 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
122 obstack_init (res->obst);
123 res->value_table = new_identities (); /* value table for global value
124 numbering for optimizing use in
128 res->phase_state = phase_building;
129 res->op_pin_state_pinned = op_pin_state_pinned;
130 res->outs_state = outs_none;
131 res->dom_state = dom_none;
132 res->typeinfo_state = irg_typeinfo_none;
133 res->loopinfo_state = loopinfo_none;
135 /*-- Type information for the procedure of the graph --*/
137 set_entity_irg(ent, res);
139 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
140 res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
142 /* Remove type from type list. Must be treated differently than other types. */
143 remove_irp_type_from_list(res->frame_type);
145 /*-- Nodes needed in every graph --*/
146 res->end_block = new_immBlock();
147 res->end = new_End();
148 res->end_reg = res->end;
149 res->end_except = res->end;
151 res->start_block = new_immBlock();
152 res->start = new_Start();
153 res->bad = new_ir_node(NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
154 res->no_mem = new_ir_node(NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
156 /* Proj results of start node */
157 projX = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
158 res->frame = new_Proj (res->start, mode_P_mach, pn_Start_P_frame_base);
159 res->globals = new_Proj (res->start, mode_P_mach, pn_Start_P_globals);
160 res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
161 res->args = new_Proj (res->start, mode_T, pn_Start_T_args);
163 res->graph_nr = get_irp_new_node_nr();
166 set_store(res->initial_mem);
168 add_immBlock_pred(res->start_block, projX);
170 * The code generation needs it. leave it in now.
171 * Use of this edge is matter of discussion, unresolved. Also possible:
172 * add_immBlock_pred(res->start_block, res->start_block), but invalid typed.
174 mature_immBlock (res->current_block);
176 /*-- Make a block to start with --*/
177 first_block = new_immBlock();
178 add_immBlock_pred (first_block, projX);
184 /* Make a rudimentary ir graph for the constant code.
185 Must look like a correct irg, spare everything else. */
186 ir_graph *new_const_code_irg(void) {
190 res = (ir_graph *) malloc (sizeof(*res));
191 memset(res, 0, sizeof(*res));
193 /* inform statistics here, as blocks will be already build on this graph */
194 stat_new_graph(res, NULL);
196 current_ir_graph = res;
197 res->n_loc = 1; /* Only the memory. */
198 res->visited = 0; /* visited flag, for the ir walker */
199 res->block_visited=0; /* visited flag, for the 'block'-walker */
200 #if USE_EXPLICIT_PHI_IN_STACK
201 res->Phi_in_stack = NULL;
203 res->kind = k_ir_graph;
204 res->obst = (struct obstack *) xmalloc (sizeof (struct obstack));
205 obstack_init (res->obst);
206 res->phase_state = phase_building;
207 res->op_pin_state_pinned = op_pin_state_pinned;
208 res->value_table = new_identities (); /* value table for global value
209 numbering for optimizing use in
212 res->frame_type = NULL;
213 res->start_block = new_immBlock ();
214 res->end_block = new_immBlock ();
215 res->end = new_End ();
216 res->end_reg = res->end;
217 res->end_except = res->end;
218 mature_immBlock(get_cur_block()); /* mature the end block */
219 res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
220 res->no_mem = new_ir_node (NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
221 res->start = new_Start ();
222 res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
224 /* Proj results of start node */
225 projX = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
226 add_immBlock_pred (res->start_block, projX);
227 mature_immBlock (res->start_block); /* mature the start block */
229 add_immBlock_pred (new_immBlock (), projX);
230 mature_immBlock (get_cur_block()); /* mature the 'body' block for expressions */
231 /* Set the visited flag high enough that the block will never be visited. */
232 set_irn_visited(get_cur_block(), -1);
233 set_Block_block_visited(get_cur_block(), -1);
234 set_Block_block_visited(res->start_block, -1);
235 set_irn_visited(res->start_block, -1);
236 set_irn_visited(res->bad, -1);
237 set_irn_visited(res->no_mem, -1);
239 res->phase_state = phase_high;
243 /* Defined in iropt.c */
244 void del_identities (pset *value_table);
246 /* Frees the passed irgraph.
247 Deallocates all nodes in this graph and the ir_graph structure.
248 Sets the field irgraph in the corresponding entity to NULL.
249 Does not remove the irgraph from the list in irprog (requires
250 inefficient search, call remove_irp_irg by hand).
251 Does not free types, entities or modes that are used only by this
252 graph, nor the entity standing for this graph. */
253 void free_ir_graph (ir_graph *irg) {
255 stat_free_graph(irg);
256 if (irg->outs_state != outs_none) free_outs(irg);
257 if (irg->frame_type) free_type(irg->frame_type);
258 if (irg->value_table) del_identities(irg->value_table);
260 peculiarity pec = get_entity_peculiarity (irg->ent);
261 set_entity_peculiarity (irg->ent, peculiarity_description);
262 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
263 set_entity_peculiarity (irg->ent, pec);
267 obstack_free(irg->obst,NULL);
269 #if USE_EXPLICIT_PHI_IN_STACK
270 free_Phi_in_stack(irg->Phi_in_stack);
276 /* access routines for all ir_graph attributes:
278 {attr type} get_irg_{attribute name} (ir_graph *irg);
279 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
282 (is_ir_graph)(const void *thing) {
283 return __is_ir_graph(thing);
286 /* Outputs a unique number for this node */
289 get_irg_graph_nr(ir_graph *irg) {
292 return irg->graph_nr;
299 (get_irg_start_block)(const ir_graph *irg) {
300 return __get_irg_start_block(irg);
304 (set_irg_start_block)(ir_graph *irg, ir_node *node) {
305 __set_irg_start_block(irg, node);
309 (get_irg_start)(const ir_graph *irg) {
310 return __get_irg_start(irg);
314 (set_irg_start)(ir_graph *irg, ir_node *node) {
315 __set_irg_start(irg, node);
319 (get_irg_end_block)(const ir_graph *irg) {
320 return __get_irg_end_block(irg);
324 (set_irg_end_block)(ir_graph *irg, ir_node *node) {
325 __set_irg_end_block(irg, node);
329 (get_irg_end)(const ir_graph *irg) {
330 return __get_irg_end(irg);
334 (set_irg_end)(ir_graph *irg, ir_node *node) {
335 __set_irg_end(irg, node);
339 (get_irg_end_reg)(const ir_graph *irg) {
340 return __get_irg_end_reg(irg);
343 void set_irg_end_reg (ir_graph *irg, ir_node *node) {
344 assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
349 (get_irg_end_except)(const ir_graph *irg) {
350 return __get_irg_end_except(irg);
353 void set_irg_end_except (ir_graph *irg, ir_node *node) {
354 assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
355 irg->end_except = node;
359 (get_irg_cstore)(const ir_graph *irg) {
360 return __get_irg_cstore(irg);
364 (set_irg_cstore)(ir_graph *irg, ir_node *node) {
365 __set_irg_cstore(irg, node);
369 (get_irg_frame)(const ir_graph *irg) {
370 return __get_irg_frame(irg);
374 (set_irg_frame)(ir_graph *irg, ir_node *node) {
375 __set_irg_frame(irg, node);
379 (get_irg_globals)(const ir_graph *irg) {
380 return __get_irg_globals(irg);
384 (set_irg_globals)(ir_graph *irg, ir_node *node) {
385 __set_irg_globals(irg, node);
389 (get_irg_initial_mem)(const ir_graph *irg) {
390 return __get_irg_initial_mem(irg);
394 (set_irg_initial_mem)(ir_graph *irg, ir_node *node) {
395 __set_irg_initial_mem(irg, node);
399 (get_irg_args)(const ir_graph *irg) {
400 return __get_irg_args(irg);
404 (set_irg_args)(ir_graph *irg, ir_node *node) {
405 __set_irg_args(irg, node);
409 (get_irg_bad)(const ir_graph *irg) {
410 return __get_irg_bad(irg);
414 (set_irg_bad)(ir_graph *irg, ir_node *node) {
415 __set_irg_bad(irg, node);
419 (get_irg_no_mem)(const ir_graph *irg) {
420 return __get_irg_no_mem(irg);
424 (set_irg_no_mem)(ir_graph *irg, ir_node *node) {
425 __set_irg_no_mem(irg, node);
429 (get_irg_current_block)(const ir_graph *irg) {
430 return __get_irg_current_block(irg);
434 (set_irg_current_block)(ir_graph *irg, ir_node *node) {
435 __set_irg_current_block(irg, node);
439 (get_irg_entity)(const ir_graph *irg) {
440 return __get_irg_entity(irg);
444 (set_irg_entity)(ir_graph *irg, entity *ent) {
445 __set_irg_entity(irg, ent);
449 (get_irg_frame_type)(const ir_graph *irg) {
450 return __get_irg_frame_type(irg);
454 (set_irg_frame_type)(ir_graph *irg, type *ftp) {
455 __set_irg_frame_type(irg, ftp);
459 /* To test for a frame type */
461 is_frame_type(const type *ftp) {
463 if (is_class_type(ftp)) {
464 for (i = 0; i < get_irp_n_irgs(); i++) {
465 const type *frame_tp = get_irg_frame_type(get_irp_irg(i));
466 if (ftp == frame_tp) return true;
473 get_irg_n_locs (ir_graph *irg)
475 if (get_opt_precise_exc_context())
476 return irg->n_loc - 1 - 1;
478 return irg->n_loc - 1;
482 set_irg_n_loc (ir_graph *irg, int n_loc)
484 if (get_opt_precise_exc_context())
485 irg->n_loc = n_loc + 1 + 1;
487 irg->n_loc = n_loc + 1;
492 /* Returns the obstack associated with the graph. */
494 (get_irg_obstack)(const ir_graph *irg) {
495 return __get_irg_obstack(irg);
499 * Returns true if the node n is allocated on the storage of graph irg.
501 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
503 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
505 struct _obstack_chunk *p;
508 * checks wheater the ir_node pointer i on the obstack.
509 * A more sophisticated check would test the "whole" ir_node
511 for (p = irg->obst->chunk; p; p = p->prev) {
512 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
520 (get_irg_phase_state)(const ir_graph *irg) {
521 return __get_irg_phase_state(irg);
525 (set_irg_phase_low)(ir_graph *irg) {
526 __set_irg_phase_low(irg);
530 (get_irg_pinned)(const ir_graph *irg) {
531 return __get_irg_pinned(irg);
535 (get_irg_outs_state)(const ir_graph *irg) {
536 return __get_irg_outs_state(irg);
540 (set_irg_outs_inconsistent)(ir_graph *irg) {
541 __set_irg_outs_inconsistent(irg);
545 (get_irg_dom_state)(const ir_graph *irg) {
546 return __get_irg_dom_state(irg);
550 (set_irg_dom_inconsistent)(ir_graph *irg) {
551 __set_irg_dom_inconsistent(irg);
555 (get_irg_loopinfo_state)(const ir_graph *irg) {
556 return __get_irg_loopinfo_state(irg);
560 (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s) {
561 __set_irg_loopinfo_state(irg, s);
565 set_irg_loopinfo_inconsistent(ir_graph *irg) {
566 if (irg->loopinfo_state == loopinfo_ip_consistent)
567 irg->loopinfo_state = loopinfo_ip_inconsistent;
569 else if (irg->loopinfo_state == loopinfo_consistent)
570 irg->loopinfo_state = loopinfo_inconsistent;
572 else if (irg->loopinfo_state == loopinfo_cf_ip_consistent)
573 irg->loopinfo_state = loopinfo_cf_ip_inconsistent;
575 else if (irg->loopinfo_state == loopinfo_cf_consistent)
576 irg->loopinfo_state = loopinfo_cf_inconsistent;
580 (set_irg_pinned)(ir_graph *irg, op_pin_state p) {
581 __set_irg_pinned(irg, p);
584 irg_callee_info_state
585 (get_irg_callee_info_state)(const ir_graph *irg) {
586 return __get_irg_callee_info_state(irg);
590 (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s) {
591 __set_irg_callee_info_state(irg, s);
595 (get_irg_inline_property)(const ir_graph *irg) {
596 return __get_irg_inline_property(irg);
600 (set_irg_inline_property)(ir_graph *irg, irg_inline_property s) {
601 __set_irg_inline_property(irg, s);
605 (set_irg_link)(ir_graph *irg, void *thing) {
606 __set_irg_link(irg, thing);
610 (get_irg_link)(const ir_graph *irg) {
611 return __get_irg_link(irg);
614 /* maximum visited flag content of all ir_graph visited fields. */
615 static int max_irg_visited = 0;
618 (get_irg_visited)(const ir_graph *irg) {
619 return __get_irg_visited(irg);
623 set_irg_visited (ir_graph *irg, unsigned long visited)
625 irg->visited = visited;
626 if (irg->visited > max_irg_visited) {
627 max_irg_visited = irg->visited;
632 inc_irg_visited (ir_graph *irg)
634 if (++irg->visited > max_irg_visited) {
635 max_irg_visited = irg->visited;
640 get_max_irg_visited(void)
644 for(i = 0; i < get_irp_n_irgs(); i++)
645 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
647 return max_irg_visited;
650 void set_max_irg_visited(int val) {
651 max_irg_visited = val;
655 inc_max_irg_visited(void)
659 for(i = 0; i < get_irp_n_irgs(); i++)
660 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
663 return max_irg_visited;
667 (get_irg_block_visited)(const ir_graph *irg) {
668 return __get_irg_block_visited(irg);
672 (set_irg_block_visited)(ir_graph *irg, unsigned long visited) {
673 __set_irg_block_visited(irg, visited);
677 (inc_irg_block_visited)(ir_graph *irg) {
678 __inc_irg_block_visited(irg);
681 /* is irg a pseudo graph for analysis? */
682 int is_pseudo_ir_graph(ir_graph *irg)
687 assert(irg && "nothing here");
688 assert(is_ir_graph(irg) && "no ir_graph given");
690 ent = get_irg_entity(irg);
691 if(visibility_external_allocated == get_entity_visibility(ent)
692 && peculiarity_existent == get_entity_peculiarity(ent)) {