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"
56 ir_graph *current_ir_graph;
57 ir_graph *get_current_ir_graph(void)
59 return current_ir_graph;
62 void set_current_ir_graph(ir_graph *graph)
64 current_ir_graph = graph;
67 /** contains the suffix for frame type names */
68 static ident *frame_type_suffix = NULL;
70 void firm_init_irgraph(void)
72 frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
76 * Allocate a new IR graph.
77 * This function respects the registered graph data. The only reason for
78 * this function is, that there are two locations, where graphs are
79 * allocated (new_r_ir_graph, new_const_code_irg).
80 * @return Memory for a new graph.
82 static ir_graph *alloc_graph(void)
84 ir_graph *const res = XMALLOCZ(ir_graph);
85 res->kind = k_ir_graph;
87 /* initialize the idx->node map. */
88 res->idx_irn_map = NEW_ARR_F(ir_node *, INITIAL_IDX_IRN_MAP_SIZE);
89 memset(res->idx_irn_map, 0, INITIAL_IDX_IRN_MAP_SIZE * sizeof(res->idx_irn_map[0]));
95 * Frees an allocated IR graph
97 static void free_graph(ir_graph *irg)
99 for (ir_edge_kind_t i = EDGE_KIND_FIRST; i < EDGE_KIND_LAST; ++i)
100 edges_deactivate_kind(irg, i);
101 DEL_ARR_F(irg->idx_irn_map);
105 void irg_set_nloc(ir_graph *res, int n_loc)
107 assert(irg_is_constrained(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION));
109 res->n_loc = n_loc + 1; /* number of local variables that are never
110 dereferenced in this graph plus one for
111 the store. This is not the number of
112 parameters to the procedure! */
114 if (res->loc_descriptions) {
115 xfree(res->loc_descriptions);
116 res->loc_descriptions = NULL;
120 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc)
123 ir_node *first_block;
124 ir_node *start, *start_block, *initial_mem, *projX;
128 /* inform statistics here, as blocks will be already build on this graph */
129 hook_new_graph(res, ent);
131 /*-- initialized for each graph. --*/
132 res->kind = k_ir_graph;
133 obstack_init(&res->obst);
135 /* graphs are in construction mode by default */
136 add_irg_constraints(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION);
137 irg_set_nloc(res, n_loc);
139 /* descriptions will be allocated on demand */
140 res->loc_descriptions = NULL;
142 res->visited = 0; /* visited flag, for the ir walker */
143 res->block_visited = 0; /* visited flag, for the 'block'-walker */
145 res->last_node_idx = 0;
149 res->irg_pinned_state = op_pin_state_pinned;
150 res->typeinfo_state = ir_typeinfo_none;
151 set_irp_typeinfo_inconsistent(); /* there is a new graph with typeinfo_none. */
152 res->callee_info_state = irg_callee_info_none;
153 res->class_cast_state = ir_class_casts_transitive;
154 res->fp_model = fp_model_precise;
155 res->mem_disambig_opt = aa_opt_inherited;
157 /*-- Type information for the procedure of the graph --*/
159 set_entity_irg(ent, res);
161 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
162 res->frame_type = new_type_frame();
164 /* the Anchor node must be created first */
165 res->anchor = new_r_Anchor(res);
167 /*-- Nodes needed in every graph --*/
168 set_irg_end_block(res, new_r_immBlock(res));
169 set_irg_end(res, new_r_End(res, 0, NULL));
171 start_block = new_r_Block_noopt(res, 0, NULL);
172 set_irg_start_block(res, start_block);
173 set_irg_no_mem (res, new_r_NoMem(res));
174 start = new_r_Start(res);
175 set_irg_start (res, start);
177 /* Proj results of start node */
178 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
179 set_irg_initial_exec (res, projX);
180 set_irg_frame (res, new_r_Proj(start, mode_P_data, pn_Start_P_frame_base));
181 set_irg_args (res, new_r_Proj(start, mode_T, pn_Start_T_args));
182 initial_mem = new_r_Proj(start, mode_M, pn_Start_M);
183 set_irg_initial_mem(res, initial_mem);
185 res->index = get_irp_new_irg_idx();
187 res->graph_nr = get_irp_new_node_nr();
190 set_r_cur_block(res, start_block);
191 set_r_store(res, initial_mem);
193 /*-- Make a block to start with --*/
194 first_block = new_r_Block(res, 1, &projX);
195 set_r_cur_block(res, first_block);
197 res->method_execution_frequency = -1.0;
202 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
204 ir_graph *res = new_r_ir_graph(ent, n_loc);
205 add_irp_irg(res); /* remember this graph global. */
209 ir_graph *new_const_code_irg(void)
211 ir_graph *res = alloc_graph();
217 ir_node *start_block;
220 /* inform statistics here, as blocks will be already build on this graph */
221 hook_new_graph(res, NULL);
223 res->n_loc = 1; /* Only the memory. */
224 res->visited = 0; /* visited flag, for the ir walker */
225 res->block_visited = 0; /* visited flag, for the 'block'-walker */
226 obstack_init(&res->obst);
228 res->last_node_idx = 0;
230 res->irg_pinned_state = op_pin_state_pinned;
231 res->fp_model = fp_model_precise;
233 /* value table for global value numbering for optimizing use in iropt.c */
236 res->frame_type = NULL;
238 add_irg_constraints(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION);
240 /* the Anchor node must be created first */
241 res->anchor = new_r_Anchor(res);
243 /* -- The end block -- */
244 end_block = new_r_Block_noopt(res, 0, NULL);
245 set_irg_end_block(res, end_block);
246 end = new_r_End(res, 0, NULL);
247 set_irg_end(res, end);
249 /* -- The start block -- */
250 start_block = new_r_Block_noopt(res, 0, NULL);
251 set_irg_start_block(res, start_block);
252 no_mem = new_r_NoMem(res);
253 set_irg_no_mem(res, no_mem);
254 start = new_r_Start(res);
255 set_irg_start(res, start);
257 /* Proj results of start node */
258 set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
259 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
261 body_block = new_r_Block(res, 1, &projX);
263 set_r_cur_block(res, body_block);
265 /* Set the visited flag high enough that the blocks will never be visited. */
266 set_irn_visited(body_block, -1);
267 set_Block_block_visited(body_block, -1);
268 set_Block_block_visited(start_block, -1);
269 set_irn_visited(start_block, -1);
275 * Pre-Walker: Copies blocks and nodes from the original method graph
276 * to the copied graph.
278 * @param n A node from the original method graph.
279 * @param env The copied graph.
281 static void copy_all_nodes(ir_node *node, void *env)
283 ir_graph *irg = (ir_graph*)env;
284 ir_node *new_node = irn_copy_into_irg(node, irg);
286 set_irn_link(node, new_node);
288 /* fix access to entities on the stack frame */
289 if (is_Sel(new_node)) {
290 ir_entity *ent = get_Sel_entity(new_node);
291 ir_type *tp = get_entity_owner(ent);
293 if (is_frame_type(tp)) {
294 /* replace by the copied entity */
295 ent = (ir_entity*)get_entity_link(ent);
297 assert(is_entity(ent));
298 assert(get_entity_owner(ent) == get_irg_frame_type(irg));
299 set_Sel_entity(new_node, ent);
305 * Post-walker: Set the predecessors of the copied nodes.
306 * The copied nodes are set as link of their original nodes. The links of
307 * "irn" predecessors are the predecessors of copied node.
309 static void rewire(ir_node *irn, void *env)
312 irn_rewire_inputs(irn);
315 static ir_node *get_new_node(const ir_node *old_node)
317 return (ir_node*) get_irn_link(old_node);
320 ir_graph *create_irg_copy(ir_graph *irg)
327 res->visited = 0; /* visited flag, for the ir walker */
328 res->block_visited = 0; /* visited flag, for the 'block'-walker */
329 obstack_init(&res->obst);
331 res->last_node_idx = 0;
333 res->irg_pinned_state = irg->irg_pinned_state;
334 res->fp_model = irg->fp_model;
338 /* clone the frame type here for safety */
339 irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
340 res->frame_type = clone_frame_type(irg->frame_type);
342 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
344 /* copy all nodes from the graph irg to the new graph res */
345 irg_walk_anchors(irg, copy_all_nodes, rewire, res);
347 /* copy the Anchor node */
348 res->anchor = get_new_node(irg->anchor);
350 /* -- The end block -- */
351 set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
352 set_irg_end (res, get_new_node(get_irg_end(irg)));
354 /* -- The start block -- */
355 set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
356 set_irg_no_mem (res, get_new_node(get_irg_no_mem(irg)));
357 set_irg_start (res, get_new_node(get_irg_start(irg)));
359 /* Proj results of start node */
360 set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
362 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
363 irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
368 void free_ir_graph(ir_graph *irg)
370 assert(is_ir_graph(irg));
373 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
375 hook_free_graph(irg);
379 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
382 free_End(get_irg_end(irg));
383 obstack_free(&irg->obst, NULL);
384 if (irg->loc_descriptions)
385 free(irg->loc_descriptions);
390 int (is_ir_graph)(const void *thing)
392 return is_ir_graph_(thing);
396 long get_irg_graph_nr(const ir_graph *irg)
398 return irg->graph_nr;
401 long get_irg_graph_nr(const ir_graph *irg)
403 return PTR_TO_INT(irg);
407 size_t get_irg_idx(const ir_graph *irg)
412 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
414 return get_idx_irn_(irg, idx);
417 ir_node *(get_irg_start_block)(const ir_graph *irg)
419 return get_irg_start_block_(irg);
422 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
424 set_irg_start_block_(irg, node);
427 ir_node *(get_irg_start)(const ir_graph *irg)
429 return get_irg_start_(irg);
432 void (set_irg_start)(ir_graph *irg, ir_node *node)
434 set_irg_start_(irg, node);
437 ir_node *(get_irg_end_block)(const ir_graph *irg)
439 return get_irg_end_block_(irg);
442 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
444 set_irg_end_block_(irg, node);
447 ir_node *(get_irg_end)(const ir_graph *irg)
449 return get_irg_end_(irg);
452 void (set_irg_end)(ir_graph *irg, ir_node *node)
454 set_irg_end_(irg, node);
457 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
459 return get_irg_initial_exec_(irg);
462 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
464 set_irg_initial_exec_(irg, node);
467 ir_node *(get_irg_frame)(const ir_graph *irg)
469 return get_irg_frame_(irg);
472 void (set_irg_frame)(ir_graph *irg, ir_node *node)
474 set_irg_frame_(irg, node);
477 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
479 return get_irg_initial_mem_(irg);
482 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
484 set_irg_initial_mem_(irg, node);
487 ir_node *(get_irg_args)(const ir_graph *irg)
489 return get_irg_args_(irg);
492 void (set_irg_args)(ir_graph *irg, ir_node *node)
494 set_irg_args_(irg, node);
497 ir_node *(get_irg_no_mem)(const ir_graph *irg)
499 return get_irg_no_mem_(irg);
502 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
504 set_irg_no_mem_(irg, node);
507 ir_entity *(get_irg_entity)(const ir_graph *irg)
509 return get_irg_entity_(irg);
512 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
514 set_irg_entity_(irg, ent);
517 ir_type *(get_irg_frame_type)(ir_graph *irg)
519 return get_irg_frame_type_(irg);
522 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
524 set_irg_frame_type_(irg, ftp);
527 int get_irg_n_locs(ir_graph *irg)
529 return irg->n_loc - 1;
532 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
534 /* Check whether the ir_node pointer is on the obstack.
535 * A more sophisticated check would test the "whole" ir_node. */
536 for (struct _obstack_chunk const *p = irg->obst.chunk; p; p = p->prev) {
537 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
544 op_pin_state (get_irg_pinned)(const ir_graph *irg)
546 return get_irg_pinned_(irg);
549 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
551 return get_irg_callee_info_state_(irg);
554 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
556 set_irg_callee_info_state_(irg, s);
559 void (set_irg_link)(ir_graph *irg, void *thing)
561 set_irg_link_(irg, thing);
564 void *(get_irg_link)(const ir_graph *irg)
566 return get_irg_link_(irg);
569 ir_visited_t (get_irg_visited)(const ir_graph *irg)
571 return get_irg_visited_(irg);
574 /** maximum visited flag content of all ir_graph visited fields. */
575 static ir_visited_t max_irg_visited = 0;
577 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
579 irg->visited = visited;
580 if (irg->visited > max_irg_visited) {
581 max_irg_visited = irg->visited;
585 void inc_irg_visited(ir_graph *irg)
588 if (irg->visited > max_irg_visited) {
589 max_irg_visited = irg->visited;
593 ir_visited_t get_max_irg_visited(void)
595 return max_irg_visited;
598 void set_max_irg_visited(int val)
600 max_irg_visited = val;
603 ir_visited_t inc_max_irg_visited(void)
607 for (i = 0; i < get_irp_n_irgs(); i++)
608 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
610 return ++max_irg_visited;
613 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
615 return get_irg_block_visited_(irg);
618 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
620 set_irg_block_visited_(irg, visited);
623 void (inc_irg_block_visited)(ir_graph *irg)
625 inc_irg_block_visited_(irg);
628 unsigned (get_irg_fp_model)(const ir_graph *irg)
630 return get_irg_fp_model_(irg);
633 void set_irg_fp_model(ir_graph *irg, unsigned model)
635 irg->fp_model = model;
638 void set_irg_loc_description(ir_graph *irg, int n, void *description)
640 assert(0 <= n && n < irg->n_loc);
642 if (! irg->loc_descriptions)
643 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
645 irg->loc_descriptions[n] = description;
648 void *get_irg_loc_description(ir_graph *irg, int n)
650 assert(0 <= n && n < irg->n_loc);
651 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
655 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
657 assert((irg->reserved_resources & resources) == 0);
658 irg->reserved_resources |= resources;
661 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
663 assert((irg->reserved_resources & resources) == resources);
664 irg->reserved_resources &= ~resources;
667 ir_resources_t ir_resources_reserved(const ir_graph *irg)
669 return irg->reserved_resources;
673 unsigned get_irg_last_idx(const ir_graph *irg)
675 return irg->last_node_idx;
678 void add_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
680 irg->constraints |= constraints;
683 void clear_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
685 irg->constraints &= ~constraints;
688 int (irg_is_constrained)(const ir_graph *irg, ir_graph_constraints_t constraints)
690 return irg_is_constrained_(irg, constraints);
693 void (add_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
695 add_irg_properties_(irg, props);
698 void (clear_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
700 clear_irg_properties_(irg, props);
703 int (irg_has_properties)(const ir_graph *irg, ir_graph_properties_t props)
705 return irg_has_properties_(irg, props);
708 typedef void (*assure_property_func)(ir_graph *irg);
710 void assure_irg_properties(ir_graph *irg, ir_graph_properties_t props)
713 ir_graph_properties_t property;
714 assure_property_func func;
715 } property_functions[] = {
716 { IR_GRAPH_PROPERTY_ONE_RETURN, normalize_one_return },
717 { IR_GRAPH_PROPERTY_MANY_RETURNS, normalize_n_returns },
718 { IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES, remove_critical_cf_edges },
719 { IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE, remove_unreachable_code },
720 { IR_GRAPH_PROPERTY_NO_BADS, remove_bads },
721 { IR_GRAPH_PROPERTY_NO_TUPLES, remove_tuples },
722 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE, assure_doms },
723 { IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE, assure_postdoms },
724 { IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES, assure_edges },
725 { IR_GRAPH_PROPERTY_CONSISTENT_OUTS, assure_irg_outs },
726 { IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO, assure_loopinfo },
727 { IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE, assure_irg_entity_usage_computed },
728 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS, ir_compute_dominance_frontiers },
731 for (i = 0; i < ARRAY_SIZE(property_functions); ++i) {
732 ir_graph_properties_t missing = props & ~irg->properties;
733 if (missing & property_functions[i].property)
734 property_functions[i].func(irg);
736 assert((props & ~irg->properties) == IR_GRAPH_PROPERTIES_NONE);
739 void confirm_irg_properties(ir_graph *irg, ir_graph_properties_t props)
741 clear_irg_properties(irg, ~props);
742 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES))
743 edges_deactivate(irg);
744 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUTS)
745 && (irg->properties & IR_GRAPH_PROPERTY_CONSISTENT_OUTS))
747 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS))
748 ir_free_dominance_frontiers(irg);