2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Entry point to the representation of procedure code.
23 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
32 #include "irgraph_t.h"
34 #include "irgraph_t.h"
45 #include "irbackedge_t.h"
46 #include "iredges_t.h"
50 #define INITIAL_IDX_IRN_MAP_SIZE 1024
53 * Indicates, whether additional data can be registered to graphs.
54 * If set to 1, this is not possible anymore.
56 static int forbid_new_data = 0;
59 * The amount of additional space for custom data to be allocated upon
60 * creating a new graph.
62 static size_t additional_graph_data_size = 0;
64 ir_graph *current_ir_graph;
65 ir_graph *get_current_ir_graph(void)
67 return current_ir_graph;
70 void set_current_ir_graph(ir_graph *graph)
72 current_ir_graph = graph;
75 /** contains the suffix for frame type names */
76 static ident *frame_type_suffix = NULL;
78 void firm_init_irgraph(void)
80 frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
85 * Allocate a new IR graph.
86 * This function respects the registered graph data. The only reason for
87 * this function is, that there are two locations, where graphs are
88 * allocated (new_r_ir_graph, new_const_code_irg).
89 * @return Memory for a new graph.
91 static ir_graph *alloc_graph(void)
94 size_t size = sizeof(ir_graph) + additional_graph_data_size;
95 char *ptr = XMALLOCNZ(char, size);
97 res = (ir_graph *)(ptr + additional_graph_data_size);
98 res->kind = k_ir_graph;
100 /* initialize the idx->node map. */
101 res->idx_irn_map = NEW_ARR_F(ir_node *, INITIAL_IDX_IRN_MAP_SIZE);
102 memset(res->idx_irn_map, 0, INITIAL_IDX_IRN_MAP_SIZE * sizeof(res->idx_irn_map[0]));
108 * Frees an allocated IR graph
110 static void free_graph(ir_graph *irg)
112 char *ptr = (char *)irg;
115 for (i = EDGE_KIND_FIRST; i < EDGE_KIND_LAST; ++i)
116 edges_deactivate_kind(irg, i);
117 DEL_ARR_F(irg->idx_irn_map);
118 free(ptr - additional_graph_data_size);
121 void irg_set_nloc(ir_graph *res, int n_loc)
123 assert(res->phase_state == phase_building);
125 res->n_loc = n_loc + 1; /* number of local variables that are never
126 dereferenced in this graph plus one for
127 the store. This is not the number of
128 parameters to the procedure! */
130 if (res->loc_descriptions) {
131 xfree(res->loc_descriptions);
132 res->loc_descriptions = NULL;
136 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc)
139 ir_node *first_block;
140 ir_node *start, *start_block, *initial_mem, *projX;
144 /* inform statistics here, as blocks will be already build on this graph */
145 hook_new_graph(res, ent);
147 /*-- initialized for each graph. --*/
148 res->kind = k_ir_graph;
149 res->obst = XMALLOC(struct obstack);
150 obstack_init(res->obst);
152 res->phase_state = phase_building;
153 irg_set_nloc(res, n_loc);
155 /* descriptions will be allocated on demand */
156 res->loc_descriptions = NULL;
158 res->visited = 0; /* visited flag, for the ir walker */
159 res->block_visited = 0; /* visited flag, for the 'block'-walker */
161 res->extbb_obst = NULL;
163 res->last_node_idx = 0;
168 res->inline_property = irg_inline_any;
169 res->additional_properties = mtp_property_inherited; /* inherited from type */
171 res->irg_pinned_state = op_pin_state_pinned;
172 res->typeinfo_state = ir_typeinfo_none;
173 set_irp_typeinfo_inconsistent(); /* there is a new graph with typeinfo_none. */
174 res->callee_info_state = irg_callee_info_none;
175 res->class_cast_state = ir_class_casts_transitive;
176 res->fp_model = fp_model_precise;
177 res->mem_disambig_opt = aa_opt_inherited;
179 /*-- Type information for the procedure of the graph --*/
181 set_entity_irg(ent, res);
183 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
184 res->frame_type = new_type_frame();
186 /* the Anchor node must be created first */
187 res->anchor = new_r_Anchor(res);
189 /*-- Nodes needed in every graph --*/
190 set_irg_end_block (res, new_r_immBlock(res));
191 set_irg_end(res, new_r_End(res, 0, NULL));
193 start_block = new_r_Block_noopt(res, 0, NULL);
194 set_irg_start_block(res, start_block);
195 set_irg_no_mem (res, new_r_NoMem(res));
196 start = new_r_Start(res);
197 set_irg_start (res, start);
199 /* Proj results of start node */
200 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
201 set_irg_initial_exec (res, projX);
202 set_irg_frame (res, new_r_Proj(start, mode_P_data, pn_Start_P_frame_base));
203 set_irg_args (res, new_r_Proj(start, mode_T, pn_Start_T_args));
204 initial_mem = new_r_Proj(start, mode_M, pn_Start_M);
205 set_irg_initial_mem(res, initial_mem);
207 res->index = get_irp_new_irg_idx();
209 res->graph_nr = get_irp_new_node_nr();
212 set_r_cur_block(res, start_block);
213 set_r_store(res, initial_mem);
215 /*-- Make a block to start with --*/
216 first_block = new_r_immBlock(res);
217 set_r_cur_block(res, first_block);
218 add_immBlock_pred(first_block, projX);
220 res->method_execution_frequency = -1.0;
221 res->estimated_node_count = 0;
226 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
228 ir_graph *res = new_r_ir_graph(ent, n_loc);
229 add_irp_irg(res); /* remember this graph global. */
233 ir_graph *new_const_code_irg(void)
235 ir_graph *res = alloc_graph();
241 ir_node *start_block;
244 /* inform statistics here, as blocks will be already build on this graph */
245 hook_new_graph(res, NULL);
247 res->n_loc = 1; /* Only the memory. */
248 res->visited = 0; /* visited flag, for the ir walker */
249 res->block_visited = 0; /* visited flag, for the 'block'-walker */
250 res->obst = XMALLOC(struct obstack);
251 obstack_init(res->obst);
252 res->extbb_obst = NULL;
254 res->last_node_idx = 0;
256 res->phase_state = phase_building;
257 res->irg_pinned_state = op_pin_state_pinned;
258 res->fp_model = fp_model_precise;
260 /* value table for global value numbering for optimizing use in iropt.c */
263 res->frame_type = NULL;
265 /* the Anchor node must be created first */
266 res->anchor = new_r_Anchor(res);
268 /* -- The end block -- */
269 end_block = new_r_Block_noopt(res, 0, NULL);
270 set_irg_end_block(res, end_block);
271 end = new_r_End(res, 0, NULL);
272 set_irg_end(res, end);
274 /* -- The start block -- */
275 start_block = new_r_Block_noopt(res, 0, NULL);
276 set_irg_start_block(res, start_block);
277 no_mem = new_r_NoMem(res);
278 set_irg_no_mem(res, no_mem);
279 start = new_r_Start(res);
280 set_irg_start(res, start);
282 /* Proj results of start node */
283 set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
284 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
286 body_block = new_r_Block(res, 1, &projX);
288 set_r_cur_block(res, body_block);
290 /* Set the visited flag high enough that the blocks will never be visited. */
291 set_irn_visited(body_block, -1);
292 set_Block_block_visited(body_block, -1);
293 set_Block_block_visited(start_block, -1);
294 set_irn_visited(start_block, -1);
300 * Pre-Walker: Copies blocks and nodes from the original method graph
301 * to the copied graph.
303 * @param n A node from the original method graph.
304 * @param env The copied graph.
306 static void copy_all_nodes(ir_node *node, void *env)
308 ir_graph *irg = (ir_graph*)env;
309 ir_node *new_node = irn_copy_into_irg(node, irg);
311 set_irn_link(node, new_node);
313 /* fix access to entities on the stack frame */
314 if (is_Sel(new_node)) {
315 ir_entity *ent = get_Sel_entity(new_node);
316 ir_type *tp = get_entity_owner(ent);
318 if (is_frame_type(tp)) {
319 /* replace by the copied entity */
320 ent = (ir_entity*)get_entity_link(ent);
322 assert(is_entity(ent));
323 assert(get_entity_owner(ent) == get_irg_frame_type(irg));
324 set_Sel_entity(new_node, ent);
330 * Post-walker: Set the predecessors of the copied nodes.
331 * The copied nodes are set as link of their original nodes. The links of
332 * "irn" predecessors are the predecessors of copied node.
334 static void rewire(ir_node *irn, void *env)
337 irn_rewire_inputs(irn);
340 static ir_node *get_new_node(const ir_node *old_node)
342 return (ir_node*) get_irn_link(old_node);
345 ir_graph *create_irg_copy(ir_graph *irg)
352 res->visited = 0; /* visited flag, for the ir walker */
353 res->block_visited = 0; /* visited flag, for the 'block'-walker */
354 res->obst = XMALLOC(struct obstack);
355 obstack_init(res->obst);
356 res->extbb_obst = NULL;
358 res->last_node_idx = 0;
360 res->phase_state = irg->phase_state;
361 res->irg_pinned_state = irg->irg_pinned_state;
362 res->fp_model = irg->fp_model;
366 /* clone the frame type here for safety */
367 irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
368 res->frame_type = clone_frame_type(irg->frame_type);
370 res->phase_state = irg->phase_state;
372 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
374 /* copy all nodes from the graph irg to the new graph res */
375 irg_walk_anchors(irg, copy_all_nodes, rewire, res);
377 /* copy the Anchor node */
378 res->anchor = get_new_node(irg->anchor);
380 /* -- The end block -- */
381 set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
382 set_irg_end (res, get_new_node(get_irg_end(irg)));
384 /* -- The start block -- */
385 set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
386 set_irg_no_mem (res, get_new_node(get_irg_no_mem(irg)));
387 set_irg_start (res, get_new_node(get_irg_start(irg)));
389 /* Proj results of start node */
390 set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
392 /* Copy the node count estimation. Would be strange if this
393 is different from the original one. */
394 res->estimated_node_count = irg->estimated_node_count;
396 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
397 irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
402 void free_ir_graph(ir_graph *irg)
404 assert(is_ir_graph(irg));
406 edges_deactivate(irg);
408 hook_free_graph(irg);
412 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
415 free_End(get_irg_end(irg));
416 obstack_free(irg->obst, NULL);
418 if (irg->loc_descriptions)
419 free(irg->loc_descriptions);
424 int (is_ir_graph)(const void *thing)
426 return is_ir_graph_(thing);
430 long get_irg_graph_nr(const ir_graph *irg)
432 return irg->graph_nr;
435 long get_irg_graph_nr(const ir_graph *irg)
437 return PTR_TO_INT(irg);
441 size_t get_irg_idx(const ir_graph *irg)
446 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
448 return get_idx_irn_(irg, idx);
451 ir_node *(get_irg_start_block)(const ir_graph *irg)
453 return get_irg_start_block_(irg);
456 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
458 set_irg_start_block_(irg, node);
461 ir_node *(get_irg_start)(const ir_graph *irg)
463 return get_irg_start_(irg);
466 void (set_irg_start)(ir_graph *irg, ir_node *node)
468 set_irg_start_(irg, node);
471 ir_node *(get_irg_end_block)(const ir_graph *irg)
473 return get_irg_end_block_(irg);
476 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
478 set_irg_end_block_(irg, node);
481 ir_node *(get_irg_end)(const ir_graph *irg)
483 return get_irg_end_(irg);
486 void (set_irg_end)(ir_graph *irg, ir_node *node)
488 set_irg_end_(irg, node);
491 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
493 return get_irg_initial_exec_(irg);
496 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
498 set_irg_initial_exec_(irg, node);
501 ir_node *(get_irg_frame)(const ir_graph *irg)
503 return get_irg_frame_(irg);
506 void (set_irg_frame)(ir_graph *irg, ir_node *node)
508 set_irg_frame_(irg, node);
511 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
513 return get_irg_initial_mem_(irg);
516 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
518 set_irg_initial_mem_(irg, node);
521 ir_node *(get_irg_args)(const ir_graph *irg)
523 return get_irg_args_(irg);
526 void (set_irg_args)(ir_graph *irg, ir_node *node)
528 set_irg_args_(irg, node);
531 ir_node *(get_irg_no_mem)(const ir_graph *irg)
533 return get_irg_no_mem_(irg);
536 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
538 set_irg_no_mem_(irg, node);
541 ir_entity *(get_irg_entity)(const ir_graph *irg)
543 return get_irg_entity_(irg);
546 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
548 set_irg_entity_(irg, ent);
551 ir_type *(get_irg_frame_type)(ir_graph *irg)
553 return get_irg_frame_type_(irg);
556 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
558 set_irg_frame_type_(irg, ftp);
561 int get_irg_n_locs(ir_graph *irg)
563 return irg->n_loc - 1;
566 struct obstack *(get_irg_obstack)(const ir_graph *irg)
568 return get_irg_obstack_(irg);
571 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
573 struct _obstack_chunk *p;
576 * checks weather the ir_node pointer is on the obstack.
577 * A more sophisticated check would test the "whole" ir_node
579 for (p = irg->obst->chunk; p; p = p->prev) {
580 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
587 irg_phase_state (get_irg_phase_state)(const ir_graph *irg)
589 return get_irg_phase_state_(irg);
592 void (set_irg_phase_state)(ir_graph *irg, irg_phase_state state)
594 set_irg_phase_state_(irg, state);
597 op_pin_state (get_irg_pinned)(const ir_graph *irg)
599 return get_irg_pinned_(irg);
602 void (set_irg_pinned)(ir_graph *irg, op_pin_state p)
604 set_irg_pinned_(irg, p);
607 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
609 return get_irg_callee_info_state_(irg);
612 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
614 set_irg_callee_info_state_(irg, s);
617 irg_inline_property (get_irg_inline_property)(const ir_graph *irg)
619 return get_irg_inline_property_(irg);
622 void (set_irg_inline_property)(ir_graph *irg, irg_inline_property s)
624 set_irg_inline_property_(irg, s);
627 mtp_additional_properties (get_irg_additional_properties)(const ir_graph *irg)
629 return get_irg_additional_properties_(irg);
632 void (set_irg_additional_properties)(ir_graph *irg, mtp_additional_properties property_mask)
634 set_irg_additional_properties_(irg, property_mask);
637 void (add_irg_additional_properties)(ir_graph *irg, mtp_additional_properties flag)
639 add_irg_additional_properties_(irg, flag);
642 void (set_irg_link)(ir_graph *irg, void *thing)
644 set_irg_link_(irg, thing);
647 void *(get_irg_link)(const ir_graph *irg)
649 return get_irg_link_(irg);
652 ir_visited_t (get_irg_visited)(const ir_graph *irg)
654 return get_irg_visited_(irg);
657 /** maximum visited flag content of all ir_graph visited fields. */
658 static ir_visited_t max_irg_visited = 0;
660 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
662 irg->visited = visited;
663 if (irg->visited > max_irg_visited) {
664 max_irg_visited = irg->visited;
668 void inc_irg_visited(ir_graph *irg)
671 if (irg->visited > max_irg_visited) {
672 max_irg_visited = irg->visited;
676 ir_visited_t get_max_irg_visited(void)
678 return max_irg_visited;
681 void set_max_irg_visited(int val)
683 max_irg_visited = val;
686 ir_visited_t inc_max_irg_visited(void)
690 for (i = 0; i < get_irp_n_irgs(); i++)
691 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
693 return ++max_irg_visited;
696 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
698 return get_irg_block_visited_(irg);
701 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
703 set_irg_block_visited_(irg, visited);
706 void (inc_irg_block_visited)(ir_graph *irg)
708 inc_irg_block_visited_(irg);
711 unsigned (get_irg_fp_model)(const ir_graph *irg)
713 return get_irg_fp_model_(irg);
716 void set_irg_fp_model(ir_graph *irg, unsigned model)
718 irg->fp_model = model;
721 void set_irg_loc_description(ir_graph *irg, int n, void *description)
723 assert(0 <= n && n < irg->n_loc);
725 if (! irg->loc_descriptions)
726 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
728 irg->loc_descriptions[n] = description;
731 void *get_irg_loc_description(ir_graph *irg, int n)
733 assert(0 <= n && n < irg->n_loc);
734 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
738 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
740 assert((irg->reserved_resources & resources) == 0);
741 irg->reserved_resources |= resources;
744 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
746 assert((irg->reserved_resources & resources) == resources);
747 irg->reserved_resources &= ~resources;
750 ir_resources_t ir_resources_reserved(const ir_graph *irg)
752 return irg->reserved_resources;
756 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg)
758 return get_irg_estimated_node_cnt_(irg);
761 unsigned get_irg_last_idx(const ir_graph *irg)
763 return irg->last_node_idx;
766 size_t register_additional_graph_data(size_t size)
768 assert(!forbid_new_data && "Too late to register additional node data");
773 return additional_graph_data_size += size;
776 void (set_irg_state)(ir_graph *irg, ir_graph_state_t state)
778 set_irg_state_(irg, state);
781 void (clear_irg_state)(ir_graph *irg, ir_graph_state_t state)
783 clear_irg_state_(irg, state);
786 int (is_irg_state)(const ir_graph *irg, ir_graph_state_t state)
788 return is_irg_state_(irg, state);