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_FZ(ir_node*, INITIAL_IDX_IRN_MAP_SIZE);
90 obstack_init(&res->obst);
92 /* value table for global value numbering for optimizing use in iropt.c */
99 * Frees an allocated IR graph
101 static void free_graph(ir_graph *irg)
103 for (ir_edge_kind_t i = EDGE_KIND_FIRST; i < EDGE_KIND_LAST; ++i)
104 edges_deactivate_kind(irg, i);
105 DEL_ARR_F(irg->idx_irn_map);
109 void irg_set_nloc(ir_graph *res, int n_loc)
111 assert(irg_is_constrained(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION));
113 res->n_loc = n_loc + 1; /* number of local variables that are never
114 dereferenced in this graph plus one for
115 the store. This is not the number of
116 parameters to the procedure! */
118 if (res->loc_descriptions) {
119 xfree(res->loc_descriptions);
120 res->loc_descriptions = NULL;
124 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc)
127 ir_node *first_block;
128 ir_node *start, *start_block, *initial_mem, *projX;
132 /* inform statistics here, as blocks will be already build on this graph */
133 hook_new_graph(res, ent);
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 res->irg_pinned_state = op_pin_state_pinned;
140 res->typeinfo_state = ir_typeinfo_none;
141 set_irp_typeinfo_inconsistent(); /* there is a new graph with typeinfo_none. */
142 res->callee_info_state = irg_callee_info_none;
143 res->class_cast_state = ir_class_casts_transitive;
144 res->fp_model = fp_model_precise;
145 res->mem_disambig_opt = aa_opt_inherited;
147 /*-- Type information for the procedure of the graph --*/
149 set_entity_irg(ent, res);
151 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
152 res->frame_type = new_type_frame();
154 /* the Anchor node must be created first */
155 res->anchor = new_r_Anchor(res);
157 /*-- Nodes needed in every graph --*/
158 set_irg_end_block(res, new_r_immBlock(res));
159 set_irg_end(res, new_r_End(res, 0, NULL));
161 start_block = new_r_Block_noopt(res, 0, NULL);
162 set_irg_start_block(res, start_block);
163 set_irg_no_mem (res, new_r_NoMem(res));
164 start = new_r_Start(res);
165 set_irg_start (res, start);
167 /* Proj results of start node */
168 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
169 set_irg_initial_exec (res, projX);
170 set_irg_frame (res, new_r_Proj(start, mode_P_data, pn_Start_P_frame_base));
171 set_irg_args (res, new_r_Proj(start, mode_T, pn_Start_T_args));
172 initial_mem = new_r_Proj(start, mode_M, pn_Start_M);
173 set_irg_initial_mem(res, initial_mem);
175 res->index = get_irp_new_irg_idx();
177 res->graph_nr = get_irp_new_node_nr();
180 set_r_cur_block(res, start_block);
181 set_r_store(res, initial_mem);
183 /*-- Make a block to start with --*/
184 first_block = new_r_Block(res, 1, &projX);
185 set_r_cur_block(res, first_block);
187 res->method_execution_frequency = -1.0;
192 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
194 ir_graph *res = new_r_ir_graph(ent, n_loc);
195 add_irp_irg(res); /* remember this graph global. */
199 ir_graph *new_const_code_irg(void)
201 ir_graph *res = alloc_graph();
207 ir_node *start_block;
210 /* inform statistics here, as blocks will be already build on this graph */
211 hook_new_graph(res, NULL);
213 res->n_loc = 1; /* Only the memory. */
214 res->irg_pinned_state = op_pin_state_pinned;
215 res->fp_model = fp_model_precise;
217 add_irg_constraints(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION);
219 /* the Anchor node must be created first */
220 res->anchor = new_r_Anchor(res);
222 /* -- The end block -- */
223 end_block = new_r_Block_noopt(res, 0, NULL);
224 set_irg_end_block(res, end_block);
225 end = new_r_End(res, 0, NULL);
226 set_irg_end(res, end);
228 /* -- The start block -- */
229 start_block = new_r_Block_noopt(res, 0, NULL);
230 set_irg_start_block(res, start_block);
231 no_mem = new_r_NoMem(res);
232 set_irg_no_mem(res, no_mem);
233 start = new_r_Start(res);
234 set_irg_start(res, start);
236 /* Proj results of start node */
237 set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
238 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
240 body_block = new_r_Block(res, 1, &projX);
242 set_r_cur_block(res, body_block);
244 /* Set the visited flag high enough that the blocks will never be visited. */
245 set_irn_visited(body_block, -1);
246 set_Block_block_visited(body_block, -1);
247 set_Block_block_visited(start_block, -1);
248 set_irn_visited(start_block, -1);
254 * Pre-Walker: Copies blocks and nodes from the original method graph
255 * to the copied graph.
257 * @param n A node from the original method graph.
258 * @param env The copied graph.
260 static void copy_all_nodes(ir_node *node, void *env)
262 ir_graph *irg = (ir_graph*)env;
263 ir_node *new_node = irn_copy_into_irg(node, irg);
265 set_irn_link(node, new_node);
267 /* fix access to entities on the stack frame */
268 if (is_Sel(new_node)) {
269 ir_entity *ent = get_Sel_entity(new_node);
270 ir_type *tp = get_entity_owner(ent);
272 if (is_frame_type(tp)) {
273 /* replace by the copied entity */
274 ent = (ir_entity*)get_entity_link(ent);
276 assert(is_entity(ent));
277 assert(get_entity_owner(ent) == get_irg_frame_type(irg));
278 set_Sel_entity(new_node, ent);
284 * Post-walker: Set the predecessors of the copied nodes.
285 * The copied nodes are set as link of their original nodes. The links of
286 * "irn" predecessors are the predecessors of copied node.
288 static void rewire(ir_node *irn, void *env)
291 irn_rewire_inputs(irn);
294 static ir_node *get_new_node(const ir_node *old_node)
296 return (ir_node*) get_irn_link(old_node);
299 ir_graph *create_irg_copy(ir_graph *irg)
305 res->irg_pinned_state = irg->irg_pinned_state;
306 res->fp_model = irg->fp_model;
308 /* clone the frame type here for safety */
309 irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
310 res->frame_type = clone_frame_type(irg->frame_type);
312 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
314 /* copy all nodes from the graph irg to the new graph res */
315 irg_walk_anchors(irg, copy_all_nodes, rewire, res);
317 /* copy the Anchor node */
318 res->anchor = get_new_node(irg->anchor);
320 /* -- The end block -- */
321 set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
322 set_irg_end (res, get_new_node(get_irg_end(irg)));
324 /* -- The start block -- */
325 set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
326 set_irg_no_mem (res, get_new_node(get_irg_no_mem(irg)));
327 set_irg_start (res, get_new_node(get_irg_start(irg)));
329 /* Proj results of start node */
330 set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
332 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
333 irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
338 void free_ir_graph(ir_graph *irg)
340 assert(is_ir_graph(irg));
343 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
345 hook_free_graph(irg);
349 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
352 free_End(get_irg_end(irg));
353 obstack_free(&irg->obst, NULL);
354 if (irg->loc_descriptions)
355 free(irg->loc_descriptions);
360 int (is_ir_graph)(const void *thing)
362 return is_ir_graph_(thing);
366 long get_irg_graph_nr(const ir_graph *irg)
368 return irg->graph_nr;
371 long get_irg_graph_nr(const ir_graph *irg)
373 return PTR_TO_INT(irg);
377 size_t get_irg_idx(const ir_graph *irg)
382 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
384 return get_idx_irn_(irg, idx);
387 ir_node *(get_irg_start_block)(const ir_graph *irg)
389 return get_irg_start_block_(irg);
392 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
394 set_irg_start_block_(irg, node);
397 ir_node *(get_irg_start)(const ir_graph *irg)
399 return get_irg_start_(irg);
402 void (set_irg_start)(ir_graph *irg, ir_node *node)
404 set_irg_start_(irg, node);
407 ir_node *(get_irg_end_block)(const ir_graph *irg)
409 return get_irg_end_block_(irg);
412 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
414 set_irg_end_block_(irg, node);
417 ir_node *(get_irg_end)(const ir_graph *irg)
419 return get_irg_end_(irg);
422 void (set_irg_end)(ir_graph *irg, ir_node *node)
424 set_irg_end_(irg, node);
427 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
429 return get_irg_initial_exec_(irg);
432 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
434 set_irg_initial_exec_(irg, node);
437 ir_node *(get_irg_frame)(const ir_graph *irg)
439 return get_irg_frame_(irg);
442 void (set_irg_frame)(ir_graph *irg, ir_node *node)
444 set_irg_frame_(irg, node);
447 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
449 return get_irg_initial_mem_(irg);
452 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
454 set_irg_initial_mem_(irg, node);
457 ir_node *(get_irg_args)(const ir_graph *irg)
459 return get_irg_args_(irg);
462 void (set_irg_args)(ir_graph *irg, ir_node *node)
464 set_irg_args_(irg, node);
467 ir_node *(get_irg_no_mem)(const ir_graph *irg)
469 return get_irg_no_mem_(irg);
472 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
474 set_irg_no_mem_(irg, node);
477 ir_entity *(get_irg_entity)(const ir_graph *irg)
479 return get_irg_entity_(irg);
482 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
484 set_irg_entity_(irg, ent);
487 ir_type *(get_irg_frame_type)(ir_graph *irg)
489 return get_irg_frame_type_(irg);
492 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
494 set_irg_frame_type_(irg, ftp);
497 int get_irg_n_locs(ir_graph *irg)
499 return irg->n_loc - 1;
502 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
504 /* Check whether the ir_node pointer is on the obstack.
505 * A more sophisticated check would test the "whole" ir_node. */
506 for (struct _obstack_chunk const *p = irg->obst.chunk; p; p = p->prev) {
507 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
514 op_pin_state (get_irg_pinned)(const ir_graph *irg)
516 return get_irg_pinned_(irg);
519 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
521 return get_irg_callee_info_state_(irg);
524 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
526 set_irg_callee_info_state_(irg, s);
529 void (set_irg_link)(ir_graph *irg, void *thing)
531 set_irg_link_(irg, thing);
534 void *(get_irg_link)(const ir_graph *irg)
536 return get_irg_link_(irg);
539 ir_visited_t (get_irg_visited)(const ir_graph *irg)
541 return get_irg_visited_(irg);
544 /** maximum visited flag content of all ir_graph visited fields. */
545 static ir_visited_t max_irg_visited = 0;
547 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
549 irg->visited = visited;
550 if (irg->visited > max_irg_visited) {
551 max_irg_visited = irg->visited;
555 void inc_irg_visited(ir_graph *irg)
558 if (irg->visited > max_irg_visited) {
559 max_irg_visited = irg->visited;
563 ir_visited_t get_max_irg_visited(void)
565 return max_irg_visited;
568 void set_max_irg_visited(int val)
570 max_irg_visited = val;
573 ir_visited_t inc_max_irg_visited(void)
577 for (i = 0; i < get_irp_n_irgs(); i++)
578 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
580 return ++max_irg_visited;
583 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
585 return get_irg_block_visited_(irg);
588 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
590 set_irg_block_visited_(irg, visited);
593 void (inc_irg_block_visited)(ir_graph *irg)
595 inc_irg_block_visited_(irg);
598 unsigned (get_irg_fp_model)(const ir_graph *irg)
600 return get_irg_fp_model_(irg);
603 void set_irg_fp_model(ir_graph *irg, unsigned model)
605 irg->fp_model = model;
608 void set_irg_loc_description(ir_graph *irg, int n, void *description)
610 assert(0 <= n && n < irg->n_loc);
612 if (! irg->loc_descriptions)
613 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
615 irg->loc_descriptions[n] = description;
618 void *get_irg_loc_description(ir_graph *irg, int n)
620 assert(0 <= n && n < irg->n_loc);
621 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
625 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
627 assert((irg->reserved_resources & resources) == 0);
628 irg->reserved_resources |= resources;
631 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
633 assert((irg->reserved_resources & resources) == resources);
634 irg->reserved_resources &= ~resources;
637 ir_resources_t ir_resources_reserved(const ir_graph *irg)
639 return irg->reserved_resources;
643 unsigned get_irg_last_idx(const ir_graph *irg)
645 return irg->last_node_idx;
648 void add_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
650 irg->constraints |= constraints;
653 void clear_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
655 irg->constraints &= ~constraints;
658 int (irg_is_constrained)(const ir_graph *irg, ir_graph_constraints_t constraints)
660 return irg_is_constrained_(irg, constraints);
663 void (add_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
665 add_irg_properties_(irg, props);
668 void (clear_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
670 clear_irg_properties_(irg, props);
673 int (irg_has_properties)(const ir_graph *irg, ir_graph_properties_t props)
675 return irg_has_properties_(irg, props);
678 typedef void (*assure_property_func)(ir_graph *irg);
680 void assure_irg_properties(ir_graph *irg, ir_graph_properties_t props)
683 ir_graph_properties_t property;
684 assure_property_func func;
685 } property_functions[] = {
686 { IR_GRAPH_PROPERTY_ONE_RETURN, normalize_one_return },
687 { IR_GRAPH_PROPERTY_MANY_RETURNS, normalize_n_returns },
688 { IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES, remove_critical_cf_edges },
689 { IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE, remove_unreachable_code },
690 { IR_GRAPH_PROPERTY_NO_BADS, remove_bads },
691 { IR_GRAPH_PROPERTY_NO_TUPLES, remove_tuples },
692 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE, assure_doms },
693 { IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE, assure_postdoms },
694 { IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES, assure_edges },
695 { IR_GRAPH_PROPERTY_CONSISTENT_OUTS, assure_irg_outs },
696 { IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO, assure_loopinfo },
697 { IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE, assure_irg_entity_usage_computed },
698 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS, ir_compute_dominance_frontiers },
701 for (i = 0; i < ARRAY_SIZE(property_functions); ++i) {
702 ir_graph_properties_t missing = props & ~irg->properties;
703 if (missing & property_functions[i].property)
704 property_functions[i].func(irg);
706 assert((props & ~irg->properties) == IR_GRAPH_PROPERTIES_NONE);
709 void confirm_irg_properties(ir_graph *irg, ir_graph_properties_t props)
711 clear_irg_properties(irg, ~props);
712 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES))
713 edges_deactivate(irg);
714 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUTS)
715 && (irg->properties & IR_GRAPH_PROPERTY_CONSISTENT_OUTS))
717 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS))
718 ir_free_dominance_frontiers(irg);