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);
318 * Pre-Walker: Copies blocks and nodes from the original method graph
319 * to the copied graph.
321 * @param n A node from the original method graph.
322 * @param env The copied graph.
324 static void copy_all_nodes(ir_node *node, void *env)
326 ir_graph *irg = (ir_graph*)env;
327 ir_node *new_node = irn_copy_into_irg(node, irg);
329 set_irn_link(node, new_node);
331 /* fix access to entities on the stack frame */
332 if (is_Sel(new_node)) {
333 ir_entity *ent = get_Sel_entity(new_node);
334 ir_type *tp = get_entity_owner(ent);
336 if (is_frame_type(tp)) {
337 /* replace by the copied entity */
338 ent = (ir_entity*)get_entity_link(ent);
340 assert(is_entity(ent));
341 assert(get_entity_owner(ent) == get_irg_frame_type(irg));
342 set_Sel_entity(new_node, ent);
348 * Post-walker: Set the predecessors of the copied nodes.
349 * The copied nodes are set as link of their original nodes. The links of
350 * "irn" predecessors are the predecessors of copied node.
352 static void rewire(ir_node *irn, void *env)
355 irn_rewire_inputs(irn);
358 static ir_node *get_new_node(const ir_node *old_node)
360 return (ir_node*) get_irn_link(old_node);
364 * Create a new graph that is a copy of a given one.
366 ir_graph *create_irg_copy(ir_graph *irg)
373 res->visited = 0; /* visited flag, for the ir walker */
374 res->block_visited = 0; /* visited flag, for the 'block'-walker */
375 res->obst = XMALLOC(struct obstack);
376 obstack_init(res->obst);
377 res->extbb_obst = NULL;
379 res->last_node_idx = 0;
381 res->phase_state = irg->phase_state;
382 res->irg_pinned_state = irg->irg_pinned_state;
383 res->fp_model = irg->fp_model;
387 /* clone the frame type here for safety */
388 irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
389 res->frame_type = clone_frame_type(irg->frame_type);
391 res->phase_state = irg->phase_state;
393 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
395 /* copy all nodes from the graph irg to the new graph res */
396 irg_walk_anchors(irg, copy_all_nodes, rewire, res);
398 /* copy the Anchor node */
399 res->anchor = get_new_node(irg->anchor);
401 /* -- The end block -- */
402 set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
403 set_irg_end (res, get_new_node(get_irg_end(irg)));
405 /* -- The start block -- */
406 set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
407 set_irg_no_mem (res, get_new_node(get_irg_no_mem(irg)));
408 set_irg_start (res, get_new_node(get_irg_start(irg)));
410 /* Proj results of start node */
411 set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
413 /* Copy the node count estimation. Would be strange if this
414 is different from the original one. */
415 res->estimated_node_count = irg->estimated_node_count;
417 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
418 irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
423 /* Frees the passed irgraph.
424 Deallocates all nodes in this graph and the ir_graph structure.
425 Sets the field irgraph in the corresponding entity to NULL.
426 Does not remove the irgraph from the list in irprog (requires
427 inefficient search, call remove_irp_irg by hand).
428 Does not free types, entities or modes that are used only by this
429 graph, nor the entity standing for this graph. */
430 void free_ir_graph(ir_graph *irg)
432 assert(is_ir_graph(irg));
434 edges_deactivate(irg);
436 hook_free_graph(irg);
440 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
443 free_End(get_irg_end(irg));
444 obstack_free(irg->obst, NULL);
446 if (irg->loc_descriptions)
447 free(irg->loc_descriptions);
452 /* access routines for all ir_graph attributes:
454 {attr type} get_irg_{attribute name} (ir_graph *irg);
455 void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
457 int (is_ir_graph)(const void *thing)
459 return is_ir_graph_(thing);
463 /* Outputs a unique number for this node */
464 long get_irg_graph_nr(const ir_graph *irg)
466 return irg->graph_nr;
469 long get_irg_graph_nr(const ir_graph *irg)
471 return PTR_TO_INT(irg);
475 size_t get_irg_idx(const ir_graph *irg)
480 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
482 return get_idx_irn_(irg, idx);
485 ir_node *(get_irg_start_block)(const ir_graph *irg)
487 return get_irg_start_block_(irg);
490 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
492 set_irg_start_block_(irg, node);
495 ir_node *(get_irg_start)(const ir_graph *irg)
497 return get_irg_start_(irg);
500 void (set_irg_start)(ir_graph *irg, ir_node *node)
502 set_irg_start_(irg, node);
505 ir_node *(get_irg_end_block)(const ir_graph *irg)
507 return get_irg_end_block_(irg);
510 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
512 set_irg_end_block_(irg, node);
515 ir_node *(get_irg_end)(const ir_graph *irg)
517 return get_irg_end_(irg);
520 void (set_irg_end)(ir_graph *irg, ir_node *node)
522 set_irg_end_(irg, node);
525 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
527 return get_irg_initial_exec_(irg);
530 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
532 set_irg_initial_exec_(irg, node);
535 ir_node *(get_irg_frame)(const ir_graph *irg)
537 return get_irg_frame_(irg);
540 void (set_irg_frame)(ir_graph *irg, ir_node *node)
542 set_irg_frame_(irg, node);
545 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
547 return get_irg_initial_mem_(irg);
550 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
552 set_irg_initial_mem_(irg, node);
555 ir_node *(get_irg_args)(const ir_graph *irg)
557 return get_irg_args_(irg);
560 void (set_irg_args)(ir_graph *irg, ir_node *node)
562 set_irg_args_(irg, node);
565 ir_node *(get_irg_no_mem)(const ir_graph *irg)
567 return get_irg_no_mem_(irg);
570 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
572 set_irg_no_mem_(irg, node);
575 ir_entity *(get_irg_entity)(const ir_graph *irg)
577 return get_irg_entity_(irg);
580 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
582 set_irg_entity_(irg, ent);
585 ir_type *(get_irg_frame_type)(ir_graph *irg)
587 return get_irg_frame_type_(irg);
590 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
592 set_irg_frame_type_(irg, ftp);
595 int get_irg_n_locs(ir_graph *irg)
597 return irg->n_loc - 1;
600 /* Returns the obstack associated with the graph. */
601 struct obstack *(get_irg_obstack)(const ir_graph *irg)
603 return get_irg_obstack_(irg);
607 * Returns true if the node n is allocated on the storage of graph irg.
609 * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
611 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
613 struct _obstack_chunk *p;
616 * checks weather the ir_node pointer is on the obstack.
617 * A more sophisticated check would test the "whole" ir_node
619 for (p = irg->obst->chunk; p; p = p->prev) {
620 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
627 irg_phase_state (get_irg_phase_state)(const ir_graph *irg)
629 return get_irg_phase_state_(irg);
632 void (set_irg_phase_state)(ir_graph *irg, irg_phase_state state)
634 set_irg_phase_state_(irg, state);
637 op_pin_state (get_irg_pinned)(const ir_graph *irg)
639 return get_irg_pinned_(irg);
642 void (set_irg_pinned)(ir_graph *irg, op_pin_state p)
644 set_irg_pinned_(irg, p);
647 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
649 return get_irg_callee_info_state_(irg);
652 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
654 set_irg_callee_info_state_(irg, s);
657 irg_inline_property (get_irg_inline_property)(const ir_graph *irg)
659 return get_irg_inline_property_(irg);
662 void (set_irg_inline_property)(ir_graph *irg, irg_inline_property s)
664 set_irg_inline_property_(irg, s);
667 mtp_additional_properties (get_irg_additional_properties)(const ir_graph *irg)
669 return get_irg_additional_properties_(irg);
672 void (set_irg_additional_properties)(ir_graph *irg, mtp_additional_properties property_mask)
674 set_irg_additional_properties_(irg, property_mask);
677 void (add_irg_additional_properties)(ir_graph *irg, mtp_additional_properties flag)
679 add_irg_additional_properties_(irg, flag);
682 void (set_irg_link)(ir_graph *irg, void *thing)
684 set_irg_link_(irg, thing);
687 void *(get_irg_link)(const ir_graph *irg)
689 return get_irg_link_(irg);
692 ir_visited_t (get_irg_visited)(const ir_graph *irg)
694 return get_irg_visited_(irg);
697 /** maximum visited flag content of all ir_graph visited fields. */
698 static ir_visited_t max_irg_visited = 0;
700 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
702 irg->visited = visited;
703 if (irg->visited > max_irg_visited) {
704 max_irg_visited = irg->visited;
708 void inc_irg_visited(ir_graph *irg)
711 if (irg->visited > max_irg_visited) {
712 max_irg_visited = irg->visited;
716 ir_visited_t get_max_irg_visited(void)
718 return max_irg_visited;
721 void set_max_irg_visited(int val)
723 max_irg_visited = val;
726 ir_visited_t inc_max_irg_visited(void)
730 for (i = 0; i < get_irp_n_irgs(); i++)
731 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
733 return ++max_irg_visited;
736 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
738 return get_irg_block_visited_(irg);
741 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
743 set_irg_block_visited_(irg, visited);
746 void (inc_irg_block_visited)(ir_graph *irg)
748 inc_irg_block_visited_(irg);
751 /* Return the floating point model of this graph. */
752 unsigned (get_irg_fp_model)(const ir_graph *irg)
754 return get_irg_fp_model_(irg);
757 /* Sets the floating point model for this graph. */
758 void set_irg_fp_model(ir_graph *irg, unsigned model)
760 irg->fp_model = model;
763 /* set a description for local value n */
764 void set_irg_loc_description(ir_graph *irg, int n, void *description)
766 assert(0 <= n && n < irg->n_loc);
768 if (! irg->loc_descriptions)
769 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
771 irg->loc_descriptions[n] = description;
774 /* get the description for local value n */
775 void *get_irg_loc_description(ir_graph *irg, int n)
777 assert(0 <= n && n < irg->n_loc);
778 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
782 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
784 assert((irg->reserved_resources & resources) == 0);
785 irg->reserved_resources |= resources;
788 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
790 assert((irg->reserved_resources & resources) == resources);
791 irg->reserved_resources &= ~resources;
794 ir_resources_t ir_resources_reserved(const ir_graph *irg)
796 return irg->reserved_resources;
800 /* Returns a estimated node count of the irg. */
801 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg)
803 return get_irg_estimated_node_cnt_(irg);
806 /* Returns the last irn index for this graph. */
807 unsigned get_irg_last_idx(const ir_graph *irg)
809 return irg->last_node_idx;
812 /* register additional space in an IR graph */
813 size_t register_additional_graph_data(size_t size)
815 assert(!forbid_new_data && "Too late to register additional node data");
820 return additional_graph_data_size += size;
823 void (set_irg_state)(ir_graph *irg, ir_graph_state_t state)
825 set_irg_state_(irg, state);
828 void (clear_irg_state)(ir_graph *irg, ir_graph_state_t state)
830 clear_irg_state_(irg, state);
833 int (is_irg_state)(const ir_graph *irg, ir_graph_state_t state)
835 return is_irg_state_(irg, state);