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
55 * Indicates, whether additional data can be registered to graphs.
56 * If set to 1, this is not possible anymore.
58 static int forbid_new_data = 0;
61 * The amount of additional space for custom data to be allocated upon
62 * creating a new graph.
64 static size_t additional_graph_data_size = 0;
66 ir_graph *current_ir_graph;
67 ir_graph *get_current_ir_graph(void)
69 return current_ir_graph;
72 void set_current_ir_graph(ir_graph *graph)
74 current_ir_graph = graph;
77 /** contains the suffix for frame type names */
78 static ident *frame_type_suffix = NULL;
80 void firm_init_irgraph(void)
82 frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
87 * Allocate a new IR graph.
88 * This function respects the registered graph data. The only reason for
89 * this function is, that there are two locations, where graphs are
90 * allocated (new_r_ir_graph, new_const_code_irg).
91 * @return Memory for a new graph.
93 static ir_graph *alloc_graph(void)
96 size_t size = sizeof(ir_graph) + additional_graph_data_size;
97 char *ptr = XMALLOCNZ(char, size);
99 res = (ir_graph *)(ptr + additional_graph_data_size);
100 res->kind = k_ir_graph;
102 /* initialize the idx->node map. */
103 res->idx_irn_map = NEW_ARR_F(ir_node *, INITIAL_IDX_IRN_MAP_SIZE);
104 memset(res->idx_irn_map, 0, INITIAL_IDX_IRN_MAP_SIZE * sizeof(res->idx_irn_map[0]));
110 * Frees an allocated IR graph
112 static void free_graph(ir_graph *irg)
114 char *ptr = (char *)irg;
117 for (i = EDGE_KIND_FIRST; i < EDGE_KIND_LAST; ++i)
118 edges_deactivate_kind(irg, i);
119 DEL_ARR_F(irg->idx_irn_map);
120 free(ptr - additional_graph_data_size);
123 void irg_set_nloc(ir_graph *res, int n_loc)
125 assert(res->phase_state == phase_building);
127 res->n_loc = n_loc + 1; /* number of local variables that are never
128 dereferenced in this graph plus one for
129 the store. This is not the number of
130 parameters to the procedure! */
132 if (res->loc_descriptions) {
133 xfree(res->loc_descriptions);
134 res->loc_descriptions = NULL;
138 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc)
141 ir_node *first_block;
142 ir_node *start, *start_block, *initial_mem, *projX;
146 /* inform statistics here, as blocks will be already build on this graph */
147 hook_new_graph(res, ent);
149 /*-- initialized for each graph. --*/
150 res->kind = k_ir_graph;
151 res->obst = XMALLOC(struct obstack);
152 obstack_init(res->obst);
154 res->phase_state = phase_building;
155 irg_set_nloc(res, n_loc);
157 /* descriptions will be allocated on demand */
158 res->loc_descriptions = NULL;
160 res->visited = 0; /* visited flag, for the ir walker */
161 res->block_visited = 0; /* visited flag, for the 'block'-walker */
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_Block(res, 1, &projX);
217 set_r_cur_block(res, first_block);
219 res->method_execution_frequency = -1.0;
220 res->estimated_node_count = 0;
225 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
227 ir_graph *res = new_r_ir_graph(ent, n_loc);
228 add_irp_irg(res); /* remember this graph global. */
232 ir_graph *new_const_code_irg(void)
234 ir_graph *res = alloc_graph();
240 ir_node *start_block;
243 /* inform statistics here, as blocks will be already build on this graph */
244 hook_new_graph(res, NULL);
246 res->n_loc = 1; /* Only the memory. */
247 res->visited = 0; /* visited flag, for the ir walker */
248 res->block_visited = 0; /* visited flag, for the 'block'-walker */
249 res->obst = XMALLOC(struct obstack);
250 obstack_init(res->obst);
252 res->last_node_idx = 0;
254 res->phase_state = phase_building;
255 res->irg_pinned_state = op_pin_state_pinned;
256 res->fp_model = fp_model_precise;
258 /* value table for global value numbering for optimizing use in iropt.c */
261 res->frame_type = NULL;
263 /* the Anchor node must be created first */
264 res->anchor = new_r_Anchor(res);
266 /* -- The end block -- */
267 end_block = new_r_Block_noopt(res, 0, NULL);
268 set_irg_end_block(res, end_block);
269 end = new_r_End(res, 0, NULL);
270 set_irg_end(res, end);
272 /* -- The start block -- */
273 start_block = new_r_Block_noopt(res, 0, NULL);
274 set_irg_start_block(res, start_block);
275 no_mem = new_r_NoMem(res);
276 set_irg_no_mem(res, no_mem);
277 start = new_r_Start(res);
278 set_irg_start(res, start);
280 /* Proj results of start node */
281 set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
282 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
284 body_block = new_r_Block(res, 1, &projX);
286 set_r_cur_block(res, body_block);
288 /* Set the visited flag high enough that the blocks will never be visited. */
289 set_irn_visited(body_block, -1);
290 set_Block_block_visited(body_block, -1);
291 set_Block_block_visited(start_block, -1);
292 set_irn_visited(start_block, -1);
298 * Pre-Walker: Copies blocks and nodes from the original method graph
299 * to the copied graph.
301 * @param n A node from the original method graph.
302 * @param env The copied graph.
304 static void copy_all_nodes(ir_node *node, void *env)
306 ir_graph *irg = (ir_graph*)env;
307 ir_node *new_node = irn_copy_into_irg(node, irg);
309 set_irn_link(node, new_node);
311 /* fix access to entities on the stack frame */
312 if (is_Sel(new_node)) {
313 ir_entity *ent = get_Sel_entity(new_node);
314 ir_type *tp = get_entity_owner(ent);
316 if (is_frame_type(tp)) {
317 /* replace by the copied entity */
318 ent = (ir_entity*)get_entity_link(ent);
320 assert(is_entity(ent));
321 assert(get_entity_owner(ent) == get_irg_frame_type(irg));
322 set_Sel_entity(new_node, ent);
328 * Post-walker: Set the predecessors of the copied nodes.
329 * The copied nodes are set as link of their original nodes. The links of
330 * "irn" predecessors are the predecessors of copied node.
332 static void rewire(ir_node *irn, void *env)
335 irn_rewire_inputs(irn);
338 static ir_node *get_new_node(const ir_node *old_node)
340 return (ir_node*) get_irn_link(old_node);
343 ir_graph *create_irg_copy(ir_graph *irg)
350 res->visited = 0; /* visited flag, for the ir walker */
351 res->block_visited = 0; /* visited flag, for the 'block'-walker */
352 res->obst = XMALLOC(struct obstack);
353 obstack_init(res->obst);
355 res->last_node_idx = 0;
357 res->phase_state = irg->phase_state;
358 res->irg_pinned_state = irg->irg_pinned_state;
359 res->fp_model = irg->fp_model;
363 /* clone the frame type here for safety */
364 irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
365 res->frame_type = clone_frame_type(irg->frame_type);
367 res->phase_state = irg->phase_state;
369 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
371 /* copy all nodes from the graph irg to the new graph res */
372 irg_walk_anchors(irg, copy_all_nodes, rewire, res);
374 /* copy the Anchor node */
375 res->anchor = get_new_node(irg->anchor);
377 /* -- The end block -- */
378 set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
379 set_irg_end (res, get_new_node(get_irg_end(irg)));
381 /* -- The start block -- */
382 set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
383 set_irg_no_mem (res, get_new_node(get_irg_no_mem(irg)));
384 set_irg_start (res, get_new_node(get_irg_start(irg)));
386 /* Proj results of start node */
387 set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
389 /* Copy the node count estimation. Would be strange if this
390 is different from the original one. */
391 res->estimated_node_count = irg->estimated_node_count;
393 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
394 irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
399 void free_ir_graph(ir_graph *irg)
401 assert(is_ir_graph(irg));
403 edges_deactivate(irg);
405 hook_free_graph(irg);
409 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
412 free_End(get_irg_end(irg));
413 obstack_free(irg->obst, NULL);
415 if (irg->loc_descriptions)
416 free(irg->loc_descriptions);
421 int (is_ir_graph)(const void *thing)
423 return is_ir_graph_(thing);
427 long get_irg_graph_nr(const ir_graph *irg)
429 return irg->graph_nr;
432 long get_irg_graph_nr(const ir_graph *irg)
434 return PTR_TO_INT(irg);
438 size_t get_irg_idx(const ir_graph *irg)
443 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
445 return get_idx_irn_(irg, idx);
448 ir_node *(get_irg_start_block)(const ir_graph *irg)
450 return get_irg_start_block_(irg);
453 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
455 set_irg_start_block_(irg, node);
458 ir_node *(get_irg_start)(const ir_graph *irg)
460 return get_irg_start_(irg);
463 void (set_irg_start)(ir_graph *irg, ir_node *node)
465 set_irg_start_(irg, node);
468 ir_node *(get_irg_end_block)(const ir_graph *irg)
470 return get_irg_end_block_(irg);
473 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
475 set_irg_end_block_(irg, node);
478 ir_node *(get_irg_end)(const ir_graph *irg)
480 return get_irg_end_(irg);
483 void (set_irg_end)(ir_graph *irg, ir_node *node)
485 set_irg_end_(irg, node);
488 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
490 return get_irg_initial_exec_(irg);
493 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
495 set_irg_initial_exec_(irg, node);
498 ir_node *(get_irg_frame)(const ir_graph *irg)
500 return get_irg_frame_(irg);
503 void (set_irg_frame)(ir_graph *irg, ir_node *node)
505 set_irg_frame_(irg, node);
508 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
510 return get_irg_initial_mem_(irg);
513 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
515 set_irg_initial_mem_(irg, node);
518 ir_node *(get_irg_args)(const ir_graph *irg)
520 return get_irg_args_(irg);
523 void (set_irg_args)(ir_graph *irg, ir_node *node)
525 set_irg_args_(irg, node);
528 ir_node *(get_irg_no_mem)(const ir_graph *irg)
530 return get_irg_no_mem_(irg);
533 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
535 set_irg_no_mem_(irg, node);
538 ir_entity *(get_irg_entity)(const ir_graph *irg)
540 return get_irg_entity_(irg);
543 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
545 set_irg_entity_(irg, ent);
548 ir_type *(get_irg_frame_type)(ir_graph *irg)
550 return get_irg_frame_type_(irg);
553 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
555 set_irg_frame_type_(irg, ftp);
558 int get_irg_n_locs(ir_graph *irg)
560 return irg->n_loc - 1;
563 struct obstack *(get_irg_obstack)(const ir_graph *irg)
565 return get_irg_obstack_(irg);
568 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
570 struct _obstack_chunk *p;
573 * checks weather the ir_node pointer is on the obstack.
574 * A more sophisticated check would test the "whole" ir_node
576 for (p = irg->obst->chunk; p; p = p->prev) {
577 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
584 irg_phase_state (get_irg_phase_state)(const ir_graph *irg)
586 return get_irg_phase_state_(irg);
589 void (set_irg_phase_state)(ir_graph *irg, irg_phase_state state)
591 set_irg_phase_state_(irg, state);
594 op_pin_state (get_irg_pinned)(const ir_graph *irg)
596 return get_irg_pinned_(irg);
599 void (set_irg_pinned)(ir_graph *irg, op_pin_state p)
601 set_irg_pinned_(irg, p);
604 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
606 return get_irg_callee_info_state_(irg);
609 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
611 set_irg_callee_info_state_(irg, s);
614 irg_inline_property (get_irg_inline_property)(const ir_graph *irg)
616 return get_irg_inline_property_(irg);
619 void (set_irg_inline_property)(ir_graph *irg, irg_inline_property s)
621 set_irg_inline_property_(irg, s);
624 mtp_additional_properties (get_irg_additional_properties)(const ir_graph *irg)
626 return get_irg_additional_properties_(irg);
629 void (set_irg_additional_properties)(ir_graph *irg, mtp_additional_properties property_mask)
631 set_irg_additional_properties_(irg, property_mask);
634 void (add_irg_additional_properties)(ir_graph *irg, mtp_additional_properties flag)
636 add_irg_additional_properties_(irg, flag);
639 void (set_irg_link)(ir_graph *irg, void *thing)
641 set_irg_link_(irg, thing);
644 void *(get_irg_link)(const ir_graph *irg)
646 return get_irg_link_(irg);
649 ir_visited_t (get_irg_visited)(const ir_graph *irg)
651 return get_irg_visited_(irg);
654 /** maximum visited flag content of all ir_graph visited fields. */
655 static ir_visited_t max_irg_visited = 0;
657 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
659 irg->visited = visited;
660 if (irg->visited > max_irg_visited) {
661 max_irg_visited = irg->visited;
665 void inc_irg_visited(ir_graph *irg)
668 if (irg->visited > max_irg_visited) {
669 max_irg_visited = irg->visited;
673 ir_visited_t get_max_irg_visited(void)
675 return max_irg_visited;
678 void set_max_irg_visited(int val)
680 max_irg_visited = val;
683 ir_visited_t inc_max_irg_visited(void)
687 for (i = 0; i < get_irp_n_irgs(); i++)
688 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
690 return ++max_irg_visited;
693 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
695 return get_irg_block_visited_(irg);
698 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
700 set_irg_block_visited_(irg, visited);
703 void (inc_irg_block_visited)(ir_graph *irg)
705 inc_irg_block_visited_(irg);
708 unsigned (get_irg_fp_model)(const ir_graph *irg)
710 return get_irg_fp_model_(irg);
713 void set_irg_fp_model(ir_graph *irg, unsigned model)
715 irg->fp_model = model;
718 void set_irg_loc_description(ir_graph *irg, int n, void *description)
720 assert(0 <= n && n < irg->n_loc);
722 if (! irg->loc_descriptions)
723 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
725 irg->loc_descriptions[n] = description;
728 void *get_irg_loc_description(ir_graph *irg, int n)
730 assert(0 <= n && n < irg->n_loc);
731 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
735 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
737 assert((irg->reserved_resources & resources) == 0);
738 irg->reserved_resources |= resources;
741 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
743 assert((irg->reserved_resources & resources) == resources);
744 irg->reserved_resources &= ~resources;
747 ir_resources_t ir_resources_reserved(const ir_graph *irg)
749 return irg->reserved_resources;
753 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg)
755 return get_irg_estimated_node_cnt_(irg);
758 unsigned get_irg_last_idx(const ir_graph *irg)
760 return irg->last_node_idx;
763 size_t register_additional_graph_data(size_t size)
765 assert(!forbid_new_data && "Too late to register additional node data");
770 return additional_graph_data_size += size;
773 void add_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
775 irg->constraints |= constraints;
778 void clear_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
780 irg->constraints &= ~constraints;
783 int (irg_is_constrained)(const ir_graph *irg, ir_graph_constraints_t constraints)
785 return irg_is_constrained_(irg, constraints);
788 void (add_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
790 add_irg_properties_(irg, props);
793 void (clear_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
795 clear_irg_properties_(irg, props);
798 int (irg_has_properties)(const ir_graph *irg, ir_graph_properties_t props)
800 return irg_has_properties_(irg, props);
803 typedef void (*assure_property_func)(ir_graph *irg);
805 void assure_irg_properties(ir_graph *irg, ir_graph_properties_t props)
808 ir_graph_properties_t property;
809 assure_property_func func;
810 } property_functions[] = {
811 { IR_GRAPH_PROPERTY_ONE_RETURN, normalize_one_return },
812 { IR_GRAPH_PROPERTY_MANY_RETURNS, normalize_n_returns },
813 { IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES, remove_critical_cf_edges },
814 { IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE, remove_unreachable_code },
815 { IR_GRAPH_PROPERTY_NO_BADS, remove_bads },
816 { IR_GRAPH_PROPERTY_NO_TUPLES, remove_tuples },
817 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE, assure_doms },
818 { IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE, assure_postdoms },
819 { IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES, assure_edges },
820 { IR_GRAPH_PROPERTY_CONSISTENT_OUTS, assure_irg_outs },
821 { IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO, assure_loopinfo },
822 { IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE, assure_irg_entity_usage_computed },
823 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS, ir_compute_dominance_frontiers },
826 for (i = 0; i < ARRAY_SIZE(property_functions); ++i) {
827 ir_graph_properties_t missing = props & ~irg->properties;
828 if (missing & property_functions[i].property)
829 property_functions[i].func(irg);
831 assert((props & ~irg->properties) == IR_GRAPH_PROPERTIES_NONE);
834 void confirm_irg_properties(ir_graph *irg, ir_graph_properties_t props)
836 clear_irg_properties(irg, ~props);
837 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES))
838 edges_deactivate(irg);
839 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUTS)
840 && (irg->properties & IR_GRAPH_PROPERTY_CONSISTENT_OUTS))
842 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS))
843 ir_free_dominance_frontiers(irg);