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"
49 #include "iroptimize.h"
52 #define INITIAL_IDX_IRN_MAP_SIZE 1024
53 /** Suffix that is added to every frame type. */
54 #define FRAME_TP_SUFFIX "frame_tp"
57 * Indicates, whether additional data can be registered to graphs.
58 * If set to 1, this is not possible anymore.
60 static int forbid_new_data = 0;
63 * The amount of additional space for custom data to be allocated upon
64 * creating a new graph.
66 static size_t additional_graph_data_size = 0;
68 ir_graph *current_ir_graph;
69 ir_graph *get_current_ir_graph(void)
71 return current_ir_graph;
74 void set_current_ir_graph(ir_graph *graph)
76 current_ir_graph = graph;
79 /** contains the suffix for frame type names */
80 static ident *frame_type_suffix = NULL;
82 void firm_init_irgraph(void)
84 frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
89 * Allocate a new IR graph.
90 * This function respects the registered graph data. The only reason for
91 * this function is, that there are two locations, where graphs are
92 * allocated (new_r_ir_graph, new_const_code_irg).
93 * @return Memory for a new graph.
95 static ir_graph *alloc_graph(void)
98 size_t size = sizeof(ir_graph) + additional_graph_data_size;
99 char *ptr = XMALLOCNZ(char, size);
101 res = (ir_graph *)(ptr + additional_graph_data_size);
102 res->kind = k_ir_graph;
104 /* initialize the idx->node map. */
105 res->idx_irn_map = NEW_ARR_F(ir_node *, INITIAL_IDX_IRN_MAP_SIZE);
106 memset(res->idx_irn_map, 0, INITIAL_IDX_IRN_MAP_SIZE * sizeof(res->idx_irn_map[0]));
112 * Frees an allocated IR graph
114 static void free_graph(ir_graph *irg)
116 char *ptr = (char *)irg;
119 for (i = EDGE_KIND_FIRST; i < EDGE_KIND_LAST; ++i)
120 edges_deactivate_kind(irg, i);
121 DEL_ARR_F(irg->idx_irn_map);
122 free(ptr - additional_graph_data_size);
125 void irg_set_nloc(ir_graph *res, int n_loc)
127 assert(irg_is_constrained(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION));
129 res->n_loc = n_loc + 1; /* number of local variables that are never
130 dereferenced in this graph plus one for
131 the store. This is not the number of
132 parameters to the procedure! */
134 if (res->loc_descriptions) {
135 xfree(res->loc_descriptions);
136 res->loc_descriptions = NULL;
140 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc)
143 ir_node *first_block;
144 ir_node *start, *start_block, *initial_mem, *projX;
148 /* inform statistics here, as blocks will be already build on this graph */
149 hook_new_graph(res, ent);
151 /*-- initialized for each graph. --*/
152 res->kind = k_ir_graph;
153 res->obst = XMALLOC(struct obstack);
154 obstack_init(res->obst);
156 /* graphs are in construction mode by default */
157 add_irg_constraints(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION);
158 irg_set_nloc(res, n_loc);
160 /* descriptions will be allocated on demand */
161 res->loc_descriptions = NULL;
163 res->visited = 0; /* visited flag, for the ir walker */
164 res->block_visited = 0; /* visited flag, for the 'block'-walker */
166 res->last_node_idx = 0;
170 res->irg_pinned_state = op_pin_state_pinned;
171 res->typeinfo_state = ir_typeinfo_none;
172 set_irp_typeinfo_inconsistent(); /* there is a new graph with typeinfo_none. */
173 res->callee_info_state = irg_callee_info_none;
174 res->class_cast_state = ir_class_casts_transitive;
175 res->fp_model = fp_model_precise;
176 res->mem_disambig_opt = aa_opt_inherited;
178 /*-- Type information for the procedure of the graph --*/
180 set_entity_irg(ent, res);
182 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
183 res->frame_type = new_type_frame();
185 /* the Anchor node must be created first */
186 res->anchor = new_r_Anchor(res);
188 /*-- Nodes needed in every graph --*/
189 set_irg_end_block(res, new_r_immBlock(res));
190 set_irg_end(res, new_r_End(res, 0, NULL));
192 start_block = new_r_Block_noopt(res, 0, NULL);
193 set_irg_start_block(res, start_block);
194 set_irg_no_mem (res, new_r_NoMem(res));
195 start = new_r_Start(res);
196 set_irg_start (res, start);
198 /* Proj results of start node */
199 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
200 set_irg_initial_exec (res, projX);
201 set_irg_frame (res, new_r_Proj(start, mode_P_data, pn_Start_P_frame_base));
202 set_irg_args (res, new_r_Proj(start, mode_T, pn_Start_T_args));
203 initial_mem = new_r_Proj(start, mode_M, pn_Start_M);
204 set_irg_initial_mem(res, initial_mem);
206 res->index = get_irp_new_irg_idx();
208 res->graph_nr = get_irp_new_node_nr();
211 set_r_cur_block(res, start_block);
212 set_r_store(res, initial_mem);
214 /*-- Make a block to start with --*/
215 first_block = new_r_Block(res, 1, &projX);
216 set_r_cur_block(res, first_block);
218 res->method_execution_frequency = -1.0;
223 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
225 ir_graph *res = new_r_ir_graph(ent, n_loc);
226 add_irp_irg(res); /* remember this graph global. */
230 ir_graph *new_const_code_irg(void)
232 ir_graph *res = alloc_graph();
238 ir_node *start_block;
241 /* inform statistics here, as blocks will be already build on this graph */
242 hook_new_graph(res, NULL);
244 res->n_loc = 1; /* Only the memory. */
245 res->visited = 0; /* visited flag, for the ir walker */
246 res->block_visited = 0; /* visited flag, for the 'block'-walker */
247 res->obst = XMALLOC(struct obstack);
248 obstack_init(res->obst);
250 res->last_node_idx = 0;
252 res->irg_pinned_state = op_pin_state_pinned;
253 res->fp_model = fp_model_precise;
255 /* value table for global value numbering for optimizing use in iropt.c */
258 res->frame_type = NULL;
260 add_irg_constraints(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION);
262 /* the Anchor node must be created first */
263 res->anchor = new_r_Anchor(res);
265 /* -- The end block -- */
266 end_block = new_r_Block_noopt(res, 0, NULL);
267 set_irg_end_block(res, end_block);
268 end = new_r_End(res, 0, NULL);
269 set_irg_end(res, end);
271 /* -- The start block -- */
272 start_block = new_r_Block_noopt(res, 0, NULL);
273 set_irg_start_block(res, start_block);
274 no_mem = new_r_NoMem(res);
275 set_irg_no_mem(res, no_mem);
276 start = new_r_Start(res);
277 set_irg_start(res, start);
279 /* Proj results of start node */
280 set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
281 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
283 body_block = new_r_Block(res, 1, &projX);
285 set_r_cur_block(res, body_block);
287 /* Set the visited flag high enough that the blocks will never be visited. */
288 set_irn_visited(body_block, -1);
289 set_Block_block_visited(body_block, -1);
290 set_Block_block_visited(start_block, -1);
291 set_irn_visited(start_block, -1);
297 * Pre-Walker: Copies blocks and nodes from the original method graph
298 * to the copied graph.
300 * @param n A node from the original method graph.
301 * @param env The copied graph.
303 static void copy_all_nodes(ir_node *node, void *env)
305 ir_graph *irg = (ir_graph*)env;
306 ir_node *new_node = irn_copy_into_irg(node, irg);
308 set_irn_link(node, new_node);
310 /* fix access to entities on the stack frame */
311 if (is_Sel(new_node)) {
312 ir_entity *ent = get_Sel_entity(new_node);
313 ir_type *tp = get_entity_owner(ent);
315 if (is_frame_type(tp)) {
316 /* replace by the copied entity */
317 ent = (ir_entity*)get_entity_link(ent);
319 assert(is_entity(ent));
320 assert(get_entity_owner(ent) == get_irg_frame_type(irg));
321 set_Sel_entity(new_node, ent);
327 * Post-walker: Set the predecessors of the copied nodes.
328 * The copied nodes are set as link of their original nodes. The links of
329 * "irn" predecessors are the predecessors of copied node.
331 static void rewire(ir_node *irn, void *env)
334 irn_rewire_inputs(irn);
337 static ir_node *get_new_node(const ir_node *old_node)
339 return (ir_node*) get_irn_link(old_node);
342 ir_graph *create_irg_copy(ir_graph *irg)
349 res->visited = 0; /* visited flag, for the ir walker */
350 res->block_visited = 0; /* visited flag, for the 'block'-walker */
351 res->obst = XMALLOC(struct obstack);
352 obstack_init(res->obst);
354 res->last_node_idx = 0;
356 res->irg_pinned_state = irg->irg_pinned_state;
357 res->fp_model = irg->fp_model;
361 /* clone the frame type here for safety */
362 irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
363 res->frame_type = clone_frame_type(irg->frame_type);
365 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
367 /* copy all nodes from the graph irg to the new graph res */
368 irg_walk_anchors(irg, copy_all_nodes, rewire, res);
370 /* copy the Anchor node */
371 res->anchor = get_new_node(irg->anchor);
373 /* -- The end block -- */
374 set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
375 set_irg_end (res, get_new_node(get_irg_end(irg)));
377 /* -- The start block -- */
378 set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
379 set_irg_no_mem (res, get_new_node(get_irg_no_mem(irg)));
380 set_irg_start (res, get_new_node(get_irg_start(irg)));
382 /* Proj results of start node */
383 set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
385 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
386 irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
391 void free_ir_graph(ir_graph *irg)
393 assert(is_ir_graph(irg));
396 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
398 hook_free_graph(irg);
402 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
405 free_End(get_irg_end(irg));
406 obstack_free(irg->obst, NULL);
408 if (irg->loc_descriptions)
409 free(irg->loc_descriptions);
414 int (is_ir_graph)(const void *thing)
416 return is_ir_graph_(thing);
420 long get_irg_graph_nr(const ir_graph *irg)
422 return irg->graph_nr;
425 long get_irg_graph_nr(const ir_graph *irg)
427 return PTR_TO_INT(irg);
431 size_t get_irg_idx(const ir_graph *irg)
436 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
438 return get_idx_irn_(irg, idx);
441 ir_node *(get_irg_start_block)(const ir_graph *irg)
443 return get_irg_start_block_(irg);
446 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
448 set_irg_start_block_(irg, node);
451 ir_node *(get_irg_start)(const ir_graph *irg)
453 return get_irg_start_(irg);
456 void (set_irg_start)(ir_graph *irg, ir_node *node)
458 set_irg_start_(irg, node);
461 ir_node *(get_irg_end_block)(const ir_graph *irg)
463 return get_irg_end_block_(irg);
466 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
468 set_irg_end_block_(irg, node);
471 ir_node *(get_irg_end)(const ir_graph *irg)
473 return get_irg_end_(irg);
476 void (set_irg_end)(ir_graph *irg, ir_node *node)
478 set_irg_end_(irg, node);
481 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
483 return get_irg_initial_exec_(irg);
486 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
488 set_irg_initial_exec_(irg, node);
491 ir_node *(get_irg_frame)(const ir_graph *irg)
493 return get_irg_frame_(irg);
496 void (set_irg_frame)(ir_graph *irg, ir_node *node)
498 set_irg_frame_(irg, node);
501 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
503 return get_irg_initial_mem_(irg);
506 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
508 set_irg_initial_mem_(irg, node);
511 ir_node *(get_irg_args)(const ir_graph *irg)
513 return get_irg_args_(irg);
516 void (set_irg_args)(ir_graph *irg, ir_node *node)
518 set_irg_args_(irg, node);
521 ir_node *(get_irg_no_mem)(const ir_graph *irg)
523 return get_irg_no_mem_(irg);
526 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
528 set_irg_no_mem_(irg, node);
531 ir_entity *(get_irg_entity)(const ir_graph *irg)
533 return get_irg_entity_(irg);
536 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
538 set_irg_entity_(irg, ent);
541 ir_type *(get_irg_frame_type)(ir_graph *irg)
543 return get_irg_frame_type_(irg);
546 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
548 set_irg_frame_type_(irg, ftp);
551 int get_irg_n_locs(ir_graph *irg)
553 return irg->n_loc - 1;
556 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
558 struct _obstack_chunk *p;
561 * checks weather the ir_node pointer is on the obstack.
562 * A more sophisticated check would test the "whole" ir_node
564 for (p = irg->obst->chunk; p; p = p->prev) {
565 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
572 op_pin_state (get_irg_pinned)(const ir_graph *irg)
574 return get_irg_pinned_(irg);
577 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
579 return get_irg_callee_info_state_(irg);
582 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
584 set_irg_callee_info_state_(irg, s);
587 void (set_irg_link)(ir_graph *irg, void *thing)
589 set_irg_link_(irg, thing);
592 void *(get_irg_link)(const ir_graph *irg)
594 return get_irg_link_(irg);
597 ir_visited_t (get_irg_visited)(const ir_graph *irg)
599 return get_irg_visited_(irg);
602 /** maximum visited flag content of all ir_graph visited fields. */
603 static ir_visited_t max_irg_visited = 0;
605 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
607 irg->visited = visited;
608 if (irg->visited > max_irg_visited) {
609 max_irg_visited = irg->visited;
613 void inc_irg_visited(ir_graph *irg)
616 if (irg->visited > max_irg_visited) {
617 max_irg_visited = irg->visited;
621 ir_visited_t get_max_irg_visited(void)
623 return max_irg_visited;
626 void set_max_irg_visited(int val)
628 max_irg_visited = val;
631 ir_visited_t inc_max_irg_visited(void)
635 for (i = 0; i < get_irp_n_irgs(); i++)
636 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
638 return ++max_irg_visited;
641 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
643 return get_irg_block_visited_(irg);
646 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
648 set_irg_block_visited_(irg, visited);
651 void (inc_irg_block_visited)(ir_graph *irg)
653 inc_irg_block_visited_(irg);
656 unsigned (get_irg_fp_model)(const ir_graph *irg)
658 return get_irg_fp_model_(irg);
661 void set_irg_fp_model(ir_graph *irg, unsigned model)
663 irg->fp_model = model;
666 void set_irg_loc_description(ir_graph *irg, int n, void *description)
668 assert(0 <= n && n < irg->n_loc);
670 if (! irg->loc_descriptions)
671 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
673 irg->loc_descriptions[n] = description;
676 void *get_irg_loc_description(ir_graph *irg, int n)
678 assert(0 <= n && n < irg->n_loc);
679 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
683 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
685 assert((irg->reserved_resources & resources) == 0);
686 irg->reserved_resources |= resources;
689 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
691 assert((irg->reserved_resources & resources) == resources);
692 irg->reserved_resources &= ~resources;
695 ir_resources_t ir_resources_reserved(const ir_graph *irg)
697 return irg->reserved_resources;
701 unsigned get_irg_last_idx(const ir_graph *irg)
703 return irg->last_node_idx;
706 size_t register_additional_graph_data(size_t size)
708 assert(!forbid_new_data && "Too late to register additional node data");
713 return additional_graph_data_size += size;
716 void add_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
718 irg->constraints |= constraints;
721 void clear_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
723 irg->constraints &= ~constraints;
726 int (irg_is_constrained)(const ir_graph *irg, ir_graph_constraints_t constraints)
728 return irg_is_constrained_(irg, constraints);
731 void (add_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
733 add_irg_properties_(irg, props);
736 void (clear_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
738 clear_irg_properties_(irg, props);
741 int (irg_has_properties)(const ir_graph *irg, ir_graph_properties_t props)
743 return irg_has_properties_(irg, props);
746 typedef void (*assure_property_func)(ir_graph *irg);
748 void assure_irg_properties(ir_graph *irg, ir_graph_properties_t props)
751 ir_graph_properties_t property;
752 assure_property_func func;
753 } property_functions[] = {
754 { IR_GRAPH_PROPERTY_ONE_RETURN, normalize_one_return },
755 { IR_GRAPH_PROPERTY_MANY_RETURNS, normalize_n_returns },
756 { IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES, remove_critical_cf_edges },
757 { IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE, remove_unreachable_code },
758 { IR_GRAPH_PROPERTY_NO_BADS, remove_bads },
759 { IR_GRAPH_PROPERTY_NO_TUPLES, remove_tuples },
760 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE, assure_doms },
761 { IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE, assure_postdoms },
762 { IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES, assure_edges },
763 { IR_GRAPH_PROPERTY_CONSISTENT_OUTS, assure_irg_outs },
764 { IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO, assure_loopinfo },
765 { IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE, assure_irg_entity_usage_computed },
766 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS, ir_compute_dominance_frontiers },
769 for (i = 0; i < ARRAY_SIZE(property_functions); ++i) {
770 ir_graph_properties_t missing = props & ~irg->properties;
771 if (missing & property_functions[i].property)
772 property_functions[i].func(irg);
774 assert((props & ~irg->properties) == IR_GRAPH_PROPERTIES_NONE);
777 void confirm_irg_properties(ir_graph *irg, ir_graph_properties_t props)
779 clear_irg_properties(irg, ~props);
780 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES))
781 edges_deactivate(irg);
782 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUTS)
783 && (irg->properties & IR_GRAPH_PROPERTY_CONSISTENT_OUTS))
785 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS))
786 ir_free_dominance_frontiers(irg);