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"
50 #define INITIAL_IDX_IRN_MAP_SIZE 1024
53 * Indicates, whether additional data can be registered to graphs.
54 * If set to 1, this is not possible anymore.
56 static int forbid_new_data = 0;
59 * The amount of additional space for custom data to be allocated upon
60 * creating a new graph.
62 static size_t additional_graph_data_size = 0;
64 ir_graph *current_ir_graph;
65 ir_graph *get_current_ir_graph(void)
67 return current_ir_graph;
70 void set_current_ir_graph(ir_graph *graph)
72 current_ir_graph = graph;
75 /** contains the suffix for frame type names */
76 static ident *frame_type_suffix = NULL;
78 /* initialize the IR graph module */
79 void firm_init_irgraph(void)
81 frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
86 * Allocate a new IR graph.
87 * This function respects the registered graph data. The only reason for
88 * this function is, that there are two locations, where graphs are
89 * allocated (new_r_ir_graph, new_const_code_irg).
90 * @return Memory for a new graph.
92 static ir_graph *alloc_graph(void)
95 size_t size = sizeof(ir_graph) + additional_graph_data_size;
96 char *ptr = XMALLOCNZ(char, size);
98 res = (ir_graph *)(ptr + additional_graph_data_size);
99 res->kind = k_ir_graph;
101 /* initialize the idx->node map. */
102 res->idx_irn_map = NEW_ARR_F(ir_node *, INITIAL_IDX_IRN_MAP_SIZE);
103 memset(res->idx_irn_map, 0, INITIAL_IDX_IRN_MAP_SIZE * sizeof(res->idx_irn_map[0]));
109 * Frees an allocated IR graph
111 static void free_graph(ir_graph *irg)
113 char *ptr = (char *)irg;
116 for (i = EDGE_KIND_FIRST; i < EDGE_KIND_LAST; ++i)
117 edges_deactivate_kind(irg, i);
118 DEL_ARR_F(irg->idx_irn_map);
119 free(ptr - additional_graph_data_size);
123 * Set the number of locals for a given graph.
125 * @param irg the graph
126 * @param n_loc number of locals
128 void irg_set_nloc(ir_graph *res, int n_loc)
130 assert(res->phase_state == phase_building);
132 res->n_loc = n_loc + 1; /* number of local variables that are never
133 dereferenced in this graph plus one for
134 the store. This is not the number of
135 parameters to the procedure! */
137 if (res->loc_descriptions) {
138 xfree(res->loc_descriptions);
139 res->loc_descriptions = NULL;
143 /* Allocates a list of nodes:
144 - The start block containing a start node and Proj nodes for its four
145 results (X, M, P, Tuple).
146 - The end block containing an end node. This block is not matured after
147 new_ir_graph as predecessors need to be added to it.
148 - The current block, which is empty and also not matured.
149 Further it allocates several datastructures needed for graph construction
152 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc)
155 ir_node *first_block;
156 ir_node *start, *start_block, *initial_mem, *projX;
160 /* inform statistics here, as blocks will be already build on this graph */
161 hook_new_graph(res, ent);
163 /*-- initialized for each graph. --*/
164 res->kind = k_ir_graph;
165 res->obst = XMALLOC(struct obstack);
166 obstack_init(res->obst);
168 res->phase_state = phase_building;
169 irg_set_nloc(res, n_loc);
171 /* descriptions will be allocated on demand */
172 res->loc_descriptions = NULL;
174 res->visited = 0; /* visited flag, for the ir walker */
175 res->block_visited = 0; /* visited flag, for the 'block'-walker */
177 res->extbb_obst = NULL;
179 res->last_node_idx = 0;
184 res->inline_property = irg_inline_any;
185 res->additional_properties = mtp_property_inherited; /* inherited from type */
187 res->irg_pinned_state = op_pin_state_pinned;
188 res->typeinfo_state = ir_typeinfo_none;
189 set_irp_typeinfo_inconsistent(); /* there is a new graph with typeinfo_none. */
190 res->callee_info_state = irg_callee_info_none;
191 res->class_cast_state = ir_class_casts_transitive;
192 res->fp_model = fp_model_precise;
193 res->mem_disambig_opt = aa_opt_inherited;
195 /*-- Type information for the procedure of the graph --*/
197 set_entity_irg(ent, res);
199 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
200 res->frame_type = new_type_frame();
202 /* the Anchor node must be created first */
203 res->anchor = new_r_Anchor(res);
205 /*-- Nodes needed in every graph --*/
206 set_irg_end_block (res, new_r_immBlock(res));
207 set_irg_end(res, new_r_End(res, 0, NULL));
209 start_block = new_r_Block_noopt(res, 0, NULL);
210 set_irg_start_block(res, start_block);
211 set_irg_no_mem (res, new_r_NoMem(res));
212 start = new_r_Start(res);
213 set_irg_start (res, start);
215 /* Proj results of start node */
216 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
217 set_irg_initial_exec (res, projX);
218 set_irg_frame (res, new_r_Proj(start, mode_P_data, pn_Start_P_frame_base));
219 set_irg_args (res, new_r_Proj(start, mode_T, pn_Start_T_args));
220 initial_mem = new_r_Proj(start, mode_M, pn_Start_M);
221 set_irg_initial_mem(res, initial_mem);
223 res->index = get_irp_new_irg_idx();
225 res->graph_nr = get_irp_new_node_nr();
228 set_r_cur_block(res, start_block);
229 set_r_store(res, initial_mem);
231 /*-- Make a block to start with --*/
232 first_block = new_r_immBlock(res);
233 set_r_cur_block(res, first_block);
234 add_immBlock_pred(first_block, projX);
236 res->method_execution_frequency = -1.0;
237 res->estimated_node_count = 0;
242 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
244 ir_graph *res = new_r_ir_graph(ent, n_loc);
245 add_irp_irg(res); /* remember this graph global. */
249 /* Make a rudimentary IR graph for the constant code.
250 Must look like a correct irg, spare everything else. */
251 ir_graph *new_const_code_irg(void)
253 ir_graph *res = alloc_graph();
259 ir_node *start_block;
262 /* inform statistics here, as blocks will be already build on this graph */
263 hook_new_graph(res, NULL);
265 res->n_loc = 1; /* Only the memory. */
266 res->visited = 0; /* visited flag, for the ir walker */
267 res->block_visited = 0; /* visited flag, for the 'block'-walker */
268 res->obst = XMALLOC(struct obstack);
269 obstack_init(res->obst);
270 res->extbb_obst = NULL;
272 res->last_node_idx = 0;
274 res->phase_state = phase_building;
275 res->irg_pinned_state = op_pin_state_pinned;
276 res->fp_model = fp_model_precise;
278 /* value table for global value numbering for optimizing use in iropt.c */
281 res->frame_type = NULL;
283 /* the Anchor node must be created first */
284 res->anchor = new_r_Anchor(res);
286 /* -- The end block -- */
287 end_block = new_r_Block_noopt(res, 0, NULL);
288 set_irg_end_block(res, end_block);
289 end = new_r_End(res, 0, NULL);
290 set_irg_end(res, end);
292 /* -- The start block -- */
293 start_block = new_r_Block_noopt(res, 0, NULL);
294 set_irg_start_block(res, start_block);
295 no_mem = new_r_NoMem(res);
296 set_irg_no_mem(res, no_mem);
297 start = new_r_Start(res);
298 set_irg_start(res, start);
300 /* Proj results of start node */
301 set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
302 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
304 body_block = new_r_Block(res, 1, &projX);
306 set_r_cur_block(res, body_block);
308 /* Set the visited flag high enough that the blocks will never be visited. */
309 set_irn_visited(body_block, -1);
310 set_Block_block_visited(body_block, -1);
311 set_Block_block_visited(start_block, -1);
312 set_irn_visited(start_block, -1);
313 set_irn_visited(no_mem, -1);
319 * Pre-Walker: Copies blocks and nodes from the original method graph
320 * to the copied graph.
322 * @param n A node from the original method graph.
323 * @param env The copied graph.
325 static void copy_all_nodes(ir_node *node, void *env)
327 ir_graph *irg = (ir_graph*)env;
328 ir_node *new_node = irn_copy_into_irg(node, irg);
330 set_irn_link(node, new_node);
332 /* fix access to entities on the stack frame */
333 if (is_Sel(new_node)) {
334 ir_entity *ent = get_Sel_entity(new_node);
335 ir_type *tp = get_entity_owner(ent);
337 if (is_frame_type(tp)) {
338 /* replace by the copied entity */
339 ent = (ir_entity*)get_entity_link(ent);
341 assert(is_entity(ent));
342 assert(get_entity_owner(ent) == get_irg_frame_type(irg));
343 set_Sel_entity(new_node, ent);
349 * Post-walker: Set the predecessors of the copied nodes.
350 * The copied nodes are set as link of their original nodes. The links of
351 * "irn" predecessors are the predecessors of copied node.
353 static void rewire(ir_node *irn, void *env)
356 irn_rewire_inputs(irn);
359 static ir_node *get_new_node(const ir_node *old_node)
361 return (ir_node*) get_irn_link(old_node);
365 * Create a new graph that is a copy of a given one.
367 ir_graph *create_irg_copy(ir_graph *irg)
374 res->visited = 0; /* visited flag, for the ir walker */
375 res->block_visited = 0; /* visited flag, for the 'block'-walker */
376 res->obst = XMALLOC(struct obstack);
377 obstack_init(res->obst);
378 res->extbb_obst = NULL;
380 res->last_node_idx = 0;
382 res->phase_state = irg->phase_state;
383 res->irg_pinned_state = irg->irg_pinned_state;
384 res->fp_model = irg->fp_model;
388 /* clone the frame type here for safety */
389 irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
390 res->frame_type = clone_frame_type(irg->frame_type);
392 res->phase_state = irg->phase_state;
394 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
396 /* copy all nodes from the graph irg to the new graph res */
397 irg_walk_anchors(irg, copy_all_nodes, rewire, res);
399 /* copy the Anchor node */
400 res->anchor = get_new_node(irg->anchor);
402 /* -- The end block -- */
403 set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
404 set_irg_end (res, get_new_node(get_irg_end(irg)));
406 /* -- The start block -- */
407 set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
408 set_irg_no_mem (res, get_new_node(get_irg_no_mem(irg)));
409 set_irg_start (res, get_new_node(get_irg_start(irg)));
411 /* Proj results of start node */
412 set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
414 /* Copy the node count estimation. Would be strange if this
415 is different from the original one. */
416 res->estimated_node_count = irg->estimated_node_count;
418 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
419 irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
424 /* Frees the passed irgraph.
425 Deallocates all nodes in this graph and the ir_graph structure.
426 Sets the field irgraph in the corresponding entity to NULL.
427 Does not remove the irgraph from the list in irprog (requires
428 inefficient search, call remove_irp_irg by hand).
429 Does not free types, entities or modes that are used only by this
430 graph, nor the entity standing for this graph. */
431 void free_ir_graph(ir_graph *irg)
433 assert(is_ir_graph(irg));
435 edges_deactivate(irg);
437 hook_free_graph(irg);
440 free_type(irg->frame_type);
443 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
446 free_End(get_irg_end(irg));
447 obstack_free(irg->obst,NULL);
449 if (irg->loc_descriptions)
450 free(irg->loc_descriptions);
455 /* access routines for all ir_graph attributes:
457 {attr type} get_irg_{attribute name} (ir_graph *irg);
458 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
460 int (is_ir_graph)(const void *thing)
462 return is_ir_graph_(thing);
466 /* Outputs a unique number for this node */
467 long get_irg_graph_nr(const ir_graph *irg)
469 return irg->graph_nr;
472 long get_irg_graph_nr(const ir_graph *irg)
474 return PTR_TO_INT(irg);
478 size_t get_irg_idx(const ir_graph *irg)
483 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
485 return get_idx_irn_(irg, idx);
488 ir_node *(get_irg_start_block)(const ir_graph *irg)
490 return get_irg_start_block_(irg);
493 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
495 set_irg_start_block_(irg, node);
498 ir_node *(get_irg_start)(const ir_graph *irg)
500 return get_irg_start_(irg);
503 void (set_irg_start)(ir_graph *irg, ir_node *node)
505 set_irg_start_(irg, node);
508 ir_node *(get_irg_end_block)(const ir_graph *irg)
510 return get_irg_end_block_(irg);
513 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
515 set_irg_end_block_(irg, node);
518 ir_node *(get_irg_end)(const ir_graph *irg)
520 return get_irg_end_(irg);
523 void (set_irg_end)(ir_graph *irg, ir_node *node)
525 set_irg_end_(irg, node);
528 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
530 return get_irg_initial_exec_(irg);
533 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
535 set_irg_initial_exec_(irg, node);
538 ir_node *(get_irg_frame)(const ir_graph *irg)
540 return get_irg_frame_(irg);
543 void (set_irg_frame)(ir_graph *irg, ir_node *node)
545 set_irg_frame_(irg, node);
548 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
550 return get_irg_initial_mem_(irg);
553 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
555 set_irg_initial_mem_(irg, node);
558 ir_node *(get_irg_args)(const ir_graph *irg)
560 return get_irg_args_(irg);
563 void (set_irg_args)(ir_graph *irg, ir_node *node)
565 set_irg_args_(irg, node);
568 ir_node *(get_irg_no_mem)(const ir_graph *irg)
570 return get_irg_no_mem_(irg);
573 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
575 set_irg_no_mem_(irg, node);
578 ir_entity *(get_irg_entity)(const ir_graph *irg)
580 return get_irg_entity_(irg);
583 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
585 set_irg_entity_(irg, ent);
588 ir_type *(get_irg_frame_type)(ir_graph *irg)
590 return get_irg_frame_type_(irg);
593 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
595 set_irg_frame_type_(irg, ftp);
598 int get_irg_n_locs(ir_graph *irg)
600 return irg->n_loc - 1;
603 /* Returns the obstack associated with the graph. */
604 struct obstack *(get_irg_obstack)(const ir_graph *irg)
606 return get_irg_obstack_(irg);
610 * Returns true if the node n is allocated on the storage of graph irg.
612 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
614 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
616 struct _obstack_chunk *p;
619 * checks weather the ir_node pointer is on the obstack.
620 * A more sophisticated check would test the "whole" ir_node
622 for (p = irg->obst->chunk; p; p = p->prev) {
623 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
630 irg_phase_state (get_irg_phase_state)(const ir_graph *irg)
632 return get_irg_phase_state_(irg);
635 void (set_irg_phase_state)(ir_graph *irg, irg_phase_state state)
637 set_irg_phase_state_(irg, state);
640 op_pin_state (get_irg_pinned)(const ir_graph *irg)
642 return get_irg_pinned_(irg);
645 void (set_irg_pinned)(ir_graph *irg, op_pin_state p)
647 set_irg_pinned_(irg, p);
650 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
652 return get_irg_callee_info_state_(irg);
655 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
657 set_irg_callee_info_state_(irg, s);
660 irg_inline_property (get_irg_inline_property)(const ir_graph *irg)
662 return get_irg_inline_property_(irg);
665 void (set_irg_inline_property)(ir_graph *irg, irg_inline_property s)
667 set_irg_inline_property_(irg, s);
670 mtp_additional_properties (get_irg_additional_properties)(const ir_graph *irg)
672 return get_irg_additional_properties_(irg);
675 void (set_irg_additional_properties)(ir_graph *irg, mtp_additional_properties property_mask)
677 set_irg_additional_properties_(irg, property_mask);
680 void (add_irg_additional_properties)(ir_graph *irg, mtp_additional_properties flag)
682 add_irg_additional_properties_(irg, flag);
685 void (set_irg_link)(ir_graph *irg, void *thing)
687 set_irg_link_(irg, thing);
690 void *(get_irg_link)(const ir_graph *irg)
692 return get_irg_link_(irg);
695 ir_visited_t (get_irg_visited)(const ir_graph *irg)
697 return get_irg_visited_(irg);
700 /** maximum visited flag content of all ir_graph visited fields. */
701 static ir_visited_t max_irg_visited = 0;
703 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
705 irg->visited = visited;
706 if (irg->visited > max_irg_visited) {
707 max_irg_visited = irg->visited;
711 void inc_irg_visited(ir_graph *irg)
714 if (irg->visited > max_irg_visited) {
715 max_irg_visited = irg->visited;
719 ir_visited_t get_max_irg_visited(void)
721 return max_irg_visited;
724 void set_max_irg_visited(int val)
726 max_irg_visited = val;
729 ir_visited_t inc_max_irg_visited(void)
733 for (i = 0; i < get_irp_n_irgs(); i++)
734 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
736 return ++max_irg_visited;
739 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
741 return get_irg_block_visited_(irg);
744 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
746 set_irg_block_visited_(irg, visited);
749 void (inc_irg_block_visited)(ir_graph *irg)
751 inc_irg_block_visited_(irg);
754 /* Return the floating point model of this graph. */
755 unsigned (get_irg_fp_model)(const ir_graph *irg)
757 return get_irg_fp_model_(irg);
760 /* Sets the floating point model for this graph. */
761 void set_irg_fp_model(ir_graph *irg, unsigned model)
763 irg->fp_model = model;
766 /* set a description for local value n */
767 void set_irg_loc_description(ir_graph *irg, int n, void *description)
769 assert(0 <= n && n < irg->n_loc);
771 if (! irg->loc_descriptions)
772 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
774 irg->loc_descriptions[n] = description;
777 /* get the description for local value n */
778 void *get_irg_loc_description(ir_graph *irg, int n)
780 assert(0 <= n && n < irg->n_loc);
781 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
785 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
787 assert((irg->reserved_resources & resources) == 0);
788 irg->reserved_resources |= resources;
791 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
793 assert((irg->reserved_resources & resources) == resources);
794 irg->reserved_resources &= ~resources;
797 ir_resources_t ir_resources_reserved(const ir_graph *irg)
799 return irg->reserved_resources;
803 /* Returns a estimated node count of the irg. */
804 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg)
806 return get_irg_estimated_node_cnt_(irg);
809 /* Returns the last irn index for this graph. */
810 unsigned get_irg_last_idx(const ir_graph *irg)
812 return irg->last_node_idx;
815 /* register additional space in an IR graph */
816 size_t register_additional_graph_data(size_t size)
818 assert(!forbid_new_data && "Too late to register additional node data");
823 return additional_graph_data_size += size;
826 void (set_irg_state)(ir_graph *irg, ir_graph_state_t state)
828 set_irg_state_(irg, state);
831 void (clear_irg_state)(ir_graph *irg, ir_graph_state_t state)
833 clear_irg_state_(irg, state);
836 int (is_irg_state)(const ir_graph *irg, ir_graph_state_t state)
838 return is_irg_state_(irg, state);