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;
167 res->irg_pinned_state = op_pin_state_pinned;
168 res->typeinfo_state = ir_typeinfo_none;
169 set_irp_typeinfo_inconsistent(); /* there is a new graph with typeinfo_none. */
170 res->callee_info_state = irg_callee_info_none;
171 res->class_cast_state = ir_class_casts_transitive;
172 res->fp_model = fp_model_precise;
173 res->mem_disambig_opt = aa_opt_inherited;
175 /*-- Type information for the procedure of the graph --*/
177 set_entity_irg(ent, res);
179 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
180 res->frame_type = new_type_frame();
182 /* the Anchor node must be created first */
183 res->anchor = new_r_Anchor(res);
185 /*-- Nodes needed in every graph --*/
186 set_irg_end_block(res, new_r_immBlock(res));
187 set_irg_end(res, new_r_End(res, 0, NULL));
189 start_block = new_r_Block_noopt(res, 0, NULL);
190 set_irg_start_block(res, start_block);
191 set_irg_no_mem (res, new_r_NoMem(res));
192 start = new_r_Start(res);
193 set_irg_start (res, start);
195 /* Proj results of start node */
196 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
197 set_irg_initial_exec (res, projX);
198 set_irg_frame (res, new_r_Proj(start, mode_P_data, pn_Start_P_frame_base));
199 set_irg_args (res, new_r_Proj(start, mode_T, pn_Start_T_args));
200 initial_mem = new_r_Proj(start, mode_M, pn_Start_M);
201 set_irg_initial_mem(res, initial_mem);
203 res->index = get_irp_new_irg_idx();
205 res->graph_nr = get_irp_new_node_nr();
208 set_r_cur_block(res, start_block);
209 set_r_store(res, initial_mem);
211 /*-- Make a block to start with --*/
212 first_block = new_r_Block(res, 1, &projX);
213 set_r_cur_block(res, first_block);
215 res->method_execution_frequency = -1.0;
216 res->estimated_node_count = 0;
221 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
223 ir_graph *res = new_r_ir_graph(ent, n_loc);
224 add_irp_irg(res); /* remember this graph global. */
228 ir_graph *new_const_code_irg(void)
230 ir_graph *res = alloc_graph();
236 ir_node *start_block;
239 /* inform statistics here, as blocks will be already build on this graph */
240 hook_new_graph(res, NULL);
242 res->n_loc = 1; /* Only the memory. */
243 res->visited = 0; /* visited flag, for the ir walker */
244 res->block_visited = 0; /* visited flag, for the 'block'-walker */
245 res->obst = XMALLOC(struct obstack);
246 obstack_init(res->obst);
248 res->last_node_idx = 0;
250 res->phase_state = phase_building;
251 res->irg_pinned_state = op_pin_state_pinned;
252 res->fp_model = fp_model_precise;
254 /* value table for global value numbering for optimizing use in iropt.c */
257 res->frame_type = NULL;
259 /* the Anchor node must be created first */
260 res->anchor = new_r_Anchor(res);
262 /* -- The end block -- */
263 end_block = new_r_Block_noopt(res, 0, NULL);
264 set_irg_end_block(res, end_block);
265 end = new_r_End(res, 0, NULL);
266 set_irg_end(res, end);
268 /* -- The start block -- */
269 start_block = new_r_Block_noopt(res, 0, NULL);
270 set_irg_start_block(res, start_block);
271 no_mem = new_r_NoMem(res);
272 set_irg_no_mem(res, no_mem);
273 start = new_r_Start(res);
274 set_irg_start(res, start);
276 /* Proj results of start node */
277 set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
278 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
280 body_block = new_r_Block(res, 1, &projX);
282 set_r_cur_block(res, body_block);
284 /* Set the visited flag high enough that the blocks will never be visited. */
285 set_irn_visited(body_block, -1);
286 set_Block_block_visited(body_block, -1);
287 set_Block_block_visited(start_block, -1);
288 set_irn_visited(start_block, -1);
294 * Pre-Walker: Copies blocks and nodes from the original method graph
295 * to the copied graph.
297 * @param n A node from the original method graph.
298 * @param env The copied graph.
300 static void copy_all_nodes(ir_node *node, void *env)
302 ir_graph *irg = (ir_graph*)env;
303 ir_node *new_node = irn_copy_into_irg(node, irg);
305 set_irn_link(node, new_node);
307 /* fix access to entities on the stack frame */
308 if (is_Sel(new_node)) {
309 ir_entity *ent = get_Sel_entity(new_node);
310 ir_type *tp = get_entity_owner(ent);
312 if (is_frame_type(tp)) {
313 /* replace by the copied entity */
314 ent = (ir_entity*)get_entity_link(ent);
316 assert(is_entity(ent));
317 assert(get_entity_owner(ent) == get_irg_frame_type(irg));
318 set_Sel_entity(new_node, ent);
324 * Post-walker: Set the predecessors of the copied nodes.
325 * The copied nodes are set as link of their original nodes. The links of
326 * "irn" predecessors are the predecessors of copied node.
328 static void rewire(ir_node *irn, void *env)
331 irn_rewire_inputs(irn);
334 static ir_node *get_new_node(const ir_node *old_node)
336 return (ir_node*) get_irn_link(old_node);
339 ir_graph *create_irg_copy(ir_graph *irg)
346 res->visited = 0; /* visited flag, for the ir walker */
347 res->block_visited = 0; /* visited flag, for the 'block'-walker */
348 res->obst = XMALLOC(struct obstack);
349 obstack_init(res->obst);
351 res->last_node_idx = 0;
353 res->phase_state = irg->phase_state;
354 res->irg_pinned_state = irg->irg_pinned_state;
355 res->fp_model = irg->fp_model;
359 /* clone the frame type here for safety */
360 irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
361 res->frame_type = clone_frame_type(irg->frame_type);
363 res->phase_state = irg->phase_state;
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 /* Copy the node count estimation. Would be strange if this
386 is different from the original one. */
387 res->estimated_node_count = irg->estimated_node_count;
389 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
390 irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
395 void free_ir_graph(ir_graph *irg)
397 assert(is_ir_graph(irg));
399 edges_deactivate(irg);
401 hook_free_graph(irg);
405 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
408 free_End(get_irg_end(irg));
409 obstack_free(irg->obst, NULL);
411 if (irg->loc_descriptions)
412 free(irg->loc_descriptions);
417 int (is_ir_graph)(const void *thing)
419 return is_ir_graph_(thing);
423 long get_irg_graph_nr(const ir_graph *irg)
425 return irg->graph_nr;
428 long get_irg_graph_nr(const ir_graph *irg)
430 return PTR_TO_INT(irg);
434 size_t get_irg_idx(const ir_graph *irg)
439 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
441 return get_idx_irn_(irg, idx);
444 ir_node *(get_irg_start_block)(const ir_graph *irg)
446 return get_irg_start_block_(irg);
449 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
451 set_irg_start_block_(irg, node);
454 ir_node *(get_irg_start)(const ir_graph *irg)
456 return get_irg_start_(irg);
459 void (set_irg_start)(ir_graph *irg, ir_node *node)
461 set_irg_start_(irg, node);
464 ir_node *(get_irg_end_block)(const ir_graph *irg)
466 return get_irg_end_block_(irg);
469 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
471 set_irg_end_block_(irg, node);
474 ir_node *(get_irg_end)(const ir_graph *irg)
476 return get_irg_end_(irg);
479 void (set_irg_end)(ir_graph *irg, ir_node *node)
481 set_irg_end_(irg, node);
484 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
486 return get_irg_initial_exec_(irg);
489 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
491 set_irg_initial_exec_(irg, node);
494 ir_node *(get_irg_frame)(const ir_graph *irg)
496 return get_irg_frame_(irg);
499 void (set_irg_frame)(ir_graph *irg, ir_node *node)
501 set_irg_frame_(irg, node);
504 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
506 return get_irg_initial_mem_(irg);
509 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
511 set_irg_initial_mem_(irg, node);
514 ir_node *(get_irg_args)(const ir_graph *irg)
516 return get_irg_args_(irg);
519 void (set_irg_args)(ir_graph *irg, ir_node *node)
521 set_irg_args_(irg, node);
524 ir_node *(get_irg_no_mem)(const ir_graph *irg)
526 return get_irg_no_mem_(irg);
529 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
531 set_irg_no_mem_(irg, node);
534 ir_entity *(get_irg_entity)(const ir_graph *irg)
536 return get_irg_entity_(irg);
539 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
541 set_irg_entity_(irg, ent);
544 ir_type *(get_irg_frame_type)(ir_graph *irg)
546 return get_irg_frame_type_(irg);
549 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
551 set_irg_frame_type_(irg, ftp);
554 int get_irg_n_locs(ir_graph *irg)
556 return irg->n_loc - 1;
559 struct obstack *(get_irg_obstack)(const ir_graph *irg)
561 return get_irg_obstack_(irg);
564 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
566 struct _obstack_chunk *p;
569 * checks weather the ir_node pointer is on the obstack.
570 * A more sophisticated check would test the "whole" ir_node
572 for (p = irg->obst->chunk; p; p = p->prev) {
573 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
580 irg_phase_state (get_irg_phase_state)(const ir_graph *irg)
582 return get_irg_phase_state_(irg);
585 void (set_irg_phase_state)(ir_graph *irg, irg_phase_state state)
587 set_irg_phase_state_(irg, state);
590 op_pin_state (get_irg_pinned)(const ir_graph *irg)
592 return get_irg_pinned_(irg);
595 void (set_irg_pinned)(ir_graph *irg, op_pin_state p)
597 set_irg_pinned_(irg, p);
600 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
602 return get_irg_callee_info_state_(irg);
605 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
607 set_irg_callee_info_state_(irg, s);
610 void (set_irg_link)(ir_graph *irg, void *thing)
612 set_irg_link_(irg, thing);
615 void *(get_irg_link)(const ir_graph *irg)
617 return get_irg_link_(irg);
620 ir_visited_t (get_irg_visited)(const ir_graph *irg)
622 return get_irg_visited_(irg);
625 /** maximum visited flag content of all ir_graph visited fields. */
626 static ir_visited_t max_irg_visited = 0;
628 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
630 irg->visited = visited;
631 if (irg->visited > max_irg_visited) {
632 max_irg_visited = irg->visited;
636 void inc_irg_visited(ir_graph *irg)
639 if (irg->visited > max_irg_visited) {
640 max_irg_visited = irg->visited;
644 ir_visited_t get_max_irg_visited(void)
646 return max_irg_visited;
649 void set_max_irg_visited(int val)
651 max_irg_visited = val;
654 ir_visited_t inc_max_irg_visited(void)
658 for (i = 0; i < get_irp_n_irgs(); i++)
659 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
661 return ++max_irg_visited;
664 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
666 return get_irg_block_visited_(irg);
669 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
671 set_irg_block_visited_(irg, visited);
674 void (inc_irg_block_visited)(ir_graph *irg)
676 inc_irg_block_visited_(irg);
679 unsigned (get_irg_fp_model)(const ir_graph *irg)
681 return get_irg_fp_model_(irg);
684 void set_irg_fp_model(ir_graph *irg, unsigned model)
686 irg->fp_model = model;
689 void set_irg_loc_description(ir_graph *irg, int n, void *description)
691 assert(0 <= n && n < irg->n_loc);
693 if (! irg->loc_descriptions)
694 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
696 irg->loc_descriptions[n] = description;
699 void *get_irg_loc_description(ir_graph *irg, int n)
701 assert(0 <= n && n < irg->n_loc);
702 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
706 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
708 assert((irg->reserved_resources & resources) == 0);
709 irg->reserved_resources |= resources;
712 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
714 assert((irg->reserved_resources & resources) == resources);
715 irg->reserved_resources &= ~resources;
718 ir_resources_t ir_resources_reserved(const ir_graph *irg)
720 return irg->reserved_resources;
724 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg)
726 return get_irg_estimated_node_cnt_(irg);
729 unsigned get_irg_last_idx(const ir_graph *irg)
731 return irg->last_node_idx;
734 size_t register_additional_graph_data(size_t size)
736 assert(!forbid_new_data && "Too late to register additional node data");
741 return additional_graph_data_size += size;
744 void add_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
746 irg->constraints |= constraints;
749 void clear_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
751 irg->constraints &= ~constraints;
754 int (irg_is_constrained)(const ir_graph *irg, ir_graph_constraints_t constraints)
756 return irg_is_constrained_(irg, constraints);
759 void (add_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
761 add_irg_properties_(irg, props);
764 void (clear_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
766 clear_irg_properties_(irg, props);
769 int (irg_has_properties)(const ir_graph *irg, ir_graph_properties_t props)
771 return irg_has_properties_(irg, props);
774 typedef void (*assure_property_func)(ir_graph *irg);
776 void assure_irg_properties(ir_graph *irg, ir_graph_properties_t props)
779 ir_graph_properties_t property;
780 assure_property_func func;
781 } property_functions[] = {
782 { IR_GRAPH_PROPERTY_ONE_RETURN, normalize_one_return },
783 { IR_GRAPH_PROPERTY_MANY_RETURNS, normalize_n_returns },
784 { IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES, remove_critical_cf_edges },
785 { IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE, remove_unreachable_code },
786 { IR_GRAPH_PROPERTY_NO_BADS, remove_bads },
787 { IR_GRAPH_PROPERTY_NO_TUPLES, remove_tuples },
788 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE, assure_doms },
789 { IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE, assure_postdoms },
790 { IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES, assure_edges },
791 { IR_GRAPH_PROPERTY_CONSISTENT_OUTS, assure_irg_outs },
792 { IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO, assure_loopinfo },
793 { IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE, assure_irg_entity_usage_computed },
794 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS, ir_compute_dominance_frontiers },
797 for (i = 0; i < ARRAY_SIZE(property_functions); ++i) {
798 ir_graph_properties_t missing = props & ~irg->properties;
799 if (missing & property_functions[i].property)
800 property_functions[i].func(irg);
802 assert((props & ~irg->properties) == IR_GRAPH_PROPERTIES_NONE);
805 void confirm_irg_properties(ir_graph *irg, ir_graph_properties_t props)
807 clear_irg_properties(irg, ~props);
808 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES))
809 edges_deactivate(irg);
810 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUTS)
811 && (irg->properties & IR_GRAPH_PROPERTY_CONSISTENT_OUTS))
813 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS))
814 ir_free_dominance_frontiers(irg);