automatically generated pn_ numbers from the spec file
[libfirm] / ir / ir / irgraph.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Entry point to the representation of procedure code.
23  * @author   Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  * @version  $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29 #include <stddef.h>
30
31 #include "xmalloc.h"
32 #include "ircons_t.h"
33 #include "irgraph_t.h"
34 #include "irprog_t.h"
35 #include "irgraph_t.h"
36 #include "irnode_t.h"
37 #include "iropt_t.h"
38 #include "irflag_t.h"
39 #include "array.h"
40 #include "irgmod.h"
41 #include "irouts.h"
42 #include "irhooks.h"
43 #include "irtools.h"
44 #include "irgwalk.h"
45 #include "irbackedge_t.h"
46 #include "iredges_t.h"
47 #include "type_t.h"
48 #include "irmemory.h"
49 #include "irphase.h"
50
51 #define INITIAL_IDX_IRN_MAP_SIZE 1024
52
53 /**
54  * Indicates, whether additional data can be registered to graphs.
55  * If set to 1, this is not possible anymore.
56  */
57 static int forbid_new_data = 0;
58
59 /**
60  * The amount of additional space for custom data to be allocated upon
61  * creating a new graph.
62  */
63 static size_t additional_graph_data_size = 0;
64
65 ir_graph *current_ir_graph;
66 ir_graph *get_current_ir_graph(void)
67 {
68         return current_ir_graph;
69 }
70
71 void set_current_ir_graph(ir_graph *graph)
72 {
73         current_ir_graph = graph;
74 }
75
76 /** contains the suffix for frame type names */
77 static ident *frame_type_suffix = NULL;
78
79 /* initialize the IR graph module */
80 void firm_init_irgraph(void)
81 {
82         frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
83         forbid_new_data   = 1;
84 }
85
86 /**
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.
92  */
93 static ir_graph *alloc_graph(void)
94 {
95         ir_graph *res;
96         size_t   size = sizeof(ir_graph) + additional_graph_data_size;
97         char     *ptr = XMALLOCNZ(char, size);
98
99         res = (ir_graph *)(ptr + additional_graph_data_size);
100         res->kind = k_ir_graph;
101
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]));
105
106         return res;
107 }
108
109 /**
110  * Frees an allocated IR graph
111  */
112 static void free_graph(ir_graph *irg)
113 {
114         char *ptr = (char *)irg;
115         int  i;
116
117         for (i = 0; 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);
121 }
122
123 /**
124  * Set the number of locals for a given graph.
125  *
126  * @param irg    the graph
127  * @param n_loc  number of locals
128  */
129 void irg_set_nloc(ir_graph *res, int n_loc)
130 {
131         assert(res->phase_state == phase_building);
132
133         if (get_opt_precise_exc_context()) {
134                 res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
135                                                dereferenced in this graph plus one for
136                                                the store plus one for links to fragile
137                                                operations.  n_loc is not the number of
138                                                parameters to the procedure!  */
139         } else {
140                 res->n_loc = n_loc + 1;     /* number of local variables that are never
141                                                dereferenced in this graph plus one for
142                                                the store. This is not the number of parameters
143                                                to the procedure!  */
144         }
145         if (res->loc_descriptions) {
146                 xfree(res->loc_descriptions);
147                 res->loc_descriptions = NULL;
148         }
149 }
150
151 /* Allocates a list of nodes:
152     - The start block containing a start node and Proj nodes for it's four
153       results (X, M, P, Tuple).
154     - The end block containing an end node. This block is not matured after
155       new_ir_graph as predecessors need to be added to it.
156     - The current block, which is empty and also not matured.
157    Further it allocates several datastructures needed for graph construction
158    and optimization.
159 */
160 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc)
161 {
162         ir_graph *res;
163         ir_node  *first_block;
164         ir_node  *end, *start, *start_block, *initial_mem, *projX, *bad;
165
166         res = alloc_graph();
167
168         /* inform statistics here, as blocks will be already build on this graph */
169         hook_new_graph(res, ent);
170
171         current_ir_graph = res;
172
173         /*-- initialized for each graph. --*/
174         res->kind = k_ir_graph;
175         res->obst = XMALLOC(struct obstack);
176         obstack_init(res->obst);
177
178         res->phase_state = phase_building;
179         irg_set_nloc(res, n_loc);
180
181         /* descriptions will be allocated on demand */
182         res->loc_descriptions = NULL;
183
184         res->visited       = 0; /* visited flag, for the ir walker */
185         res->block_visited = 0; /* visited flag, for the 'block'-walker */
186
187         res->extbb_obst = NULL;
188
189         res->last_node_idx = 0;
190
191         res->value_table = new_identities(); /* value table for global value
192                                                 numbering for optimizing use in iropt.c */
193         res->outs = NULL;
194
195         res->inline_property       = irg_inline_any;
196         res->additional_properties = mtp_property_inherited;  /* inherited from type */
197
198         res->irg_pinned_state    = op_pin_state_pinned;
199         res->outs_state          = outs_none;
200         res->dom_state           = dom_none;
201         res->pdom_state          = dom_none;
202         res->typeinfo_state      = ir_typeinfo_none;
203         set_irp_typeinfo_inconsistent();           /* there is a new graph with typeinfo_none. */
204         res->callee_info_state   = irg_callee_info_none;
205         res->loopinfo_state      = loopinfo_none;
206         res->class_cast_state    = ir_class_casts_transitive;
207         res->extblk_state        = ir_extblk_info_none;
208         res->execfreq_state      = exec_freq_none;
209         res->fp_model            = fp_model_precise;
210         res->entity_usage_state  = ir_entity_usage_not_computed;
211         res->mem_disambig_opt    = aa_opt_inherited;
212
213         /*-- Type information for the procedure of the graph --*/
214         res->ent = ent;
215         set_entity_irg(ent, res);
216
217         /*--  a class type so that it can contain "inner" methods as in Pascal. --*/
218         res->frame_type = new_type_frame();
219
220         /* the Anchor node must be created first */
221         res->anchor = new_Anchor(res);
222
223         /*-- Nodes needed in every graph --*/
224         set_irg_end_block (res, new_immBlock());
225         set_cur_block(get_irg_end_block(res));
226         end               = new_End();
227         set_irg_end       (res, end);
228
229         start_block = new_immBlock();
230         set_cur_block(start_block);
231         set_irg_start_block(res, start_block);
232         bad = new_ir_node(NULL, res, start_block, op_Bad, mode_T, 0, NULL);
233         bad->attr.irg.irg = res;
234         set_irg_bad        (res, bad);
235         set_irg_no_mem     (res, new_ir_node(NULL, res, start_block, op_NoMem, mode_M, 0, NULL));
236         start = new_Start();
237         set_irg_start      (res, start);
238
239         /* Proj results of start node */
240         projX                   = new_Proj(start, mode_X, pn_Start_X_initial_exec);
241         set_irg_initial_exec    (res, projX);
242         set_irg_frame           (res, new_Proj(start, mode_P_data, pn_Start_P_frame_base));
243         set_irg_tls             (res, new_Proj(start, mode_P_data, pn_Start_P_tls));
244         set_irg_args            (res, new_Proj(start, mode_T,      pn_Start_T_args));
245         initial_mem             = new_Proj(start, mode_M, pn_Start_M);
246         set_irg_initial_mem(res, initial_mem);
247
248         set_store(initial_mem);
249
250         res->index       = get_irp_new_irg_idx();
251 #ifdef DEBUG_libfirm
252         res->graph_nr    = get_irp_new_node_nr();
253 #endif
254
255         mature_immBlock(res->current_block);
256
257         /*-- Make a block to start with --*/
258         first_block = new_immBlock();
259         set_cur_block(first_block);
260         add_immBlock_pred(first_block, projX);
261
262         res->method_execution_frequency = -1.0;
263         res->estimated_node_count       = 0;
264
265         return res;
266 }
267
268 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
269 {
270         ir_graph *res = new_r_ir_graph(ent, n_loc);
271         add_irp_irg(res);          /* remember this graph global. */
272         return res;
273 }
274
275 /* Make a rudimentary IR graph for the constant code.
276    Must look like a correct irg, spare everything else. */
277 ir_graph *new_const_code_irg(void)
278 {
279         ir_graph *res = alloc_graph();
280         ir_node  *bad;
281         ir_node  *body_block;
282         ir_node  *end;
283         ir_node  *end_block;
284         ir_node  *no_mem;
285         ir_node  *projX;
286         ir_node  *start_block;
287         ir_node  *start;
288
289         /* inform statistics here, as blocks will be already build on this graph */
290         hook_new_graph(res, NULL);
291
292         current_ir_graph   = res;
293         res->n_loc         = 1; /* Only the memory. */
294         res->visited       = 0; /* visited flag, for the ir walker */
295         res->block_visited = 0; /* visited flag, for the 'block'-walker */
296         res->obst          = XMALLOC(struct obstack);
297         obstack_init(res->obst);
298         res->extbb_obst = NULL;
299
300         res->last_node_idx = 0;
301
302         res->phase_state      = phase_building;
303         res->irg_pinned_state = op_pin_state_pinned;
304         res->extblk_state     = ir_extblk_info_none;
305         res->fp_model         = fp_model_precise;
306
307         /* value table for global value numbering for optimizing use in iropt.c */
308         res->value_table = new_identities();
309         res->ent         = NULL;
310         res->frame_type  = NULL;
311
312         /* the Anchor node must be created first */
313         res->anchor = new_Anchor(res);
314
315         /* -- The end block -- */
316         end_block = new_immBlock();
317         set_irg_end_block(res, end_block);
318         set_cur_block(end_block);
319         end = new_End();
320         set_irg_end       (res, end);
321         mature_immBlock(end_block);
322
323         /* -- The start block -- */
324         start_block        = new_immBlock();
325         set_cur_block(start_block);
326         set_irg_start_block(res, start_block);
327         bad = new_ir_node(NULL, res, start_block, op_Bad, mode_T, 0, NULL);
328         bad->attr.irg.irg = res;
329         set_irg_bad(res, bad);
330         no_mem = new_ir_node(NULL, res, start_block, op_NoMem, mode_M, 0, NULL);
331         set_irg_no_mem(res, no_mem);
332         start = new_Start();
333         set_irg_start(res, start);
334
335         /* Proj results of start node */
336         set_irg_initial_mem(res, new_Proj(start, mode_M, pn_Start_M));
337         projX = new_Proj(start, mode_X, pn_Start_X_initial_exec);
338         mature_immBlock(start_block);
339
340         body_block = new_immBlock();
341         add_immBlock_pred(body_block, projX);
342         mature_immBlock(body_block); /* mature the 'body' block for expressions */
343         set_cur_block(body_block);
344
345         /* Set the visited flag high enough that the blocks will never be visited. */
346         set_irn_visited(body_block, -1);
347         set_Block_block_visited(body_block, -1);
348         set_Block_block_visited(start_block, -1);
349         set_irn_visited(start_block, -1);
350         set_irn_visited(bad, -1);
351         set_irn_visited(no_mem, -1);
352
353         res->phase_state = phase_high;
354
355         return res;
356 }
357
358 /**
359  * Pre-Walker: Copies blocks and nodes from the original method graph
360  * to the copied graph.
361  *
362  * @param n    A node from the original method graph.
363  * @param env  The copied graph.
364  */
365 static void copy_all_nodes(ir_node *node, void *env)
366 {
367         ir_graph *irg      = env;
368         ir_node  *new_node = irn_copy_into_irg(node, irg);
369
370         set_irn_link(node, new_node);
371
372         /* fix access to entities on the stack frame */
373         if (is_Sel(new_node)) {
374                 ir_entity *ent = get_Sel_entity(new_node);
375                 ir_type   *tp  = get_entity_owner(ent);
376
377                 if (is_frame_type(tp)) {
378                         /* replace by the copied entity */
379                         ent = get_entity_link(ent);
380
381                         assert(is_entity(ent));
382                         assert(get_entity_owner(ent) == get_irg_frame_type(irg));
383                         set_Sel_entity(new_node, ent);
384                 }
385         }
386 }
387
388 /**
389  * Post-walker: Set the predecessors of the copied nodes.
390  * The copied nodes are set as link of their original nodes. The links of
391  * "irn" predecessors are the predecessors of copied node.
392  */
393 static void rewire(ir_node *irn, void *env)
394 {
395         (void) env;
396         irn_rewire_inputs(irn);
397 }
398
399 static ir_node *get_new_node(const ir_node *old_node)
400 {
401         return (ir_node*) get_irn_link(old_node);
402 }
403
404 /*
405  * Create a new graph that is a copy of a given one.
406  */
407 ir_graph *create_irg_copy(ir_graph *irg)
408 {
409         ir_graph *res;
410
411         res = alloc_graph();
412
413         res->n_loc = 0;
414         res->visited = 0;       /* visited flag, for the ir walker */
415         res->block_visited = 0; /* visited flag, for the 'block'-walker */
416         res->obst       = XMALLOC(struct obstack);
417         obstack_init(res->obst);
418         res->extbb_obst = NULL;
419
420         res->last_node_idx = 0;
421
422         res->phase_state      = irg->phase_state;
423         res->irg_pinned_state = irg->irg_pinned_state;
424         res->extblk_state     = ir_extblk_info_none;
425         res->fp_model         = irg->fp_model;
426
427         res->value_table = new_identities();
428
429         /* clone the frame type here for safety */
430         irp_reserve_resources(irp, IR_RESOURCE_ENTITY_LINK);
431         res->frame_type  = clone_frame_type(irg->frame_type);
432
433         res->phase_state = irg->phase_state;
434
435         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
436
437         /* copy all nodes from the graph irg to the new graph res */
438         irg_walk_anchors(irg, copy_all_nodes, rewire, res);
439
440         /* copy the Anchor node */
441         res->anchor = get_new_node(irg->anchor);
442
443         /* -- The end block -- */
444         set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
445         set_irg_end       (res, get_new_node(get_irg_end(irg)));
446
447         /* -- The start block -- */
448         set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
449         set_irg_bad        (res, get_new_node(get_irg_bad(irg)));
450         set_irg_no_mem     (res, get_new_node(get_irg_no_mem(irg)));
451         set_irg_start      (res, get_new_node(get_irg_start(irg)));
452
453         /* Proj results of start node */
454         set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
455
456         /* Copy the node count estimation. Would be strange if this
457            is different from the original one. */
458         res->estimated_node_count = irg->estimated_node_count;
459
460         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
461         irp_free_resources(irp, IR_RESOURCE_ENTITY_LINK);
462
463         return res;
464 }
465
466 /* Frees the passed irgraph.
467    Deallocates all nodes in this graph and the ir_graph structure.
468    Sets the field irgraph in the corresponding entity to NULL.
469    Does not remove the irgraph from the list in irprog (requires
470    inefficient search, call remove_irp_irg by hand).
471    Does not free types, entities or modes that are used only by this
472    graph, nor the entity standing for this graph. */
473 void free_ir_graph(ir_graph *irg)
474 {
475         assert(is_ir_graph(irg));
476
477         edges_deactivate(irg);
478
479         hook_free_graph(irg);
480         if (irg->outs_state != outs_none)
481                 free_irg_outs(irg);
482         if (irg->frame_type)
483                 free_type(irg->frame_type);
484         if (irg->value_table)
485                 del_identities(irg->value_table);
486         if (irg->ent) {
487                 set_entity_irg(irg->ent, NULL);  /* not set in const code irg */
488         }
489
490         free_End(get_irg_end(irg));
491         obstack_free(irg->obst,NULL);
492         free(irg->obst);
493         if (irg->loc_descriptions)
494                 free(irg->loc_descriptions);
495         irg->kind = k_BAD;
496         free_graph(irg);
497 }
498
499 /* access routines for all ir_graph attributes:
500    templates:
501    {attr type} get_irg_{attribute name} (ir_graph *irg);
502    void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
503
504 int (is_ir_graph)(const void *thing)
505 {
506         return _is_ir_graph(thing);
507 }
508
509 #ifdef DEBUG_libfirm
510 /* Outputs a unique number for this node */
511 long get_irg_graph_nr(const ir_graph *irg)
512 {
513         return irg->graph_nr;
514 }
515 #else
516 long get_irg_graph_nr(const ir_graph *irg)
517 {
518         return PTR_TO_INT(irg);
519 }
520 #endif
521
522 int get_irg_idx(const ir_graph *irg)
523 {
524         return irg->index;
525 }
526
527 ir_node *(get_idx_irn)(ir_graph *irg, unsigned idx)
528 {
529         return _get_idx_irn(irg, idx);
530 }
531
532 ir_node *(get_irg_start_block)(const ir_graph *irg)
533 {
534         return _get_irg_start_block(irg);
535 }
536
537 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
538 {
539         _set_irg_start_block(irg, node);
540 }
541
542 ir_node *(get_irg_start)(const ir_graph *irg)
543 {
544         return _get_irg_start(irg);
545 }
546
547 void (set_irg_start)(ir_graph *irg, ir_node *node)
548 {
549         _set_irg_start(irg, node);
550 }
551
552 ir_node *(get_irg_end_block)(const ir_graph *irg)
553 {
554         return _get_irg_end_block(irg);
555 }
556
557 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
558 {
559   _set_irg_end_block(irg, node);
560 }
561
562 ir_node *(get_irg_end)(const ir_graph *irg)
563 {
564         return _get_irg_end(irg);
565 }
566
567 void (set_irg_end)(ir_graph *irg, ir_node *node)
568 {
569         _set_irg_end(irg, node);
570 }
571
572 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
573 {
574         return _get_irg_initial_exec(irg);
575 }
576
577 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
578 {
579         _set_irg_initial_exec(irg, node);
580 }
581
582 ir_node *(get_irg_frame)(const ir_graph *irg)
583 {
584         return _get_irg_frame(irg);
585 }
586
587 void (set_irg_frame)(ir_graph *irg, ir_node *node)
588 {
589         _set_irg_frame(irg, node);
590 }
591
592 ir_node *(get_irg_tls)(const ir_graph *irg)
593 {
594         return _get_irg_tls(irg);
595 }
596
597 void (set_irg_tls)(ir_graph *irg, ir_node *node)
598 {
599         _set_irg_tls(irg, node);
600 }
601
602 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
603 {
604         return _get_irg_initial_mem(irg);
605 }
606
607 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
608 {
609         _set_irg_initial_mem(irg, node);
610 }
611
612 ir_node *(get_irg_args)(const ir_graph *irg)
613 {
614         return _get_irg_args(irg);
615 }
616
617 void (set_irg_args)(ir_graph *irg, ir_node *node)
618 {
619         _set_irg_args(irg, node);
620 }
621
622 ir_node *(get_irg_bad)(const ir_graph *irg)
623 {
624         return _get_irg_bad(irg);
625 }
626
627 void (set_irg_bad)(ir_graph *irg, ir_node *node)
628 {
629         _set_irg_bad(irg, node);
630 }
631
632 ir_node *(get_irg_no_mem)(const ir_graph *irg)
633 {
634         return _get_irg_no_mem(irg);
635 }
636
637 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
638 {
639         _set_irg_no_mem(irg, node);
640 }
641
642 ir_node *(get_irg_current_block)(const ir_graph *irg)
643 {
644         return _get_irg_current_block(irg);
645 }
646
647 void (set_irg_current_block)(ir_graph *irg, ir_node *node)
648 {
649         _set_irg_current_block(irg, node);
650 }
651
652 ir_entity *(get_irg_entity)(const ir_graph *irg)
653 {
654         return _get_irg_entity(irg);
655 }
656
657 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
658 {
659         _set_irg_entity(irg, ent);
660 }
661
662 ir_type *(get_irg_frame_type)(ir_graph *irg)
663 {
664         return _get_irg_frame_type(irg);
665 }
666
667 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
668 {
669         _set_irg_frame_type(irg, ftp);
670 }
671
672 /* Returns the value parameter type of an IR graph. */
673 ir_type *get_irg_value_param_type(ir_graph *irg)
674 {
675         ir_entity *ent = get_irg_entity(irg);
676         ir_type   *mtp = get_entity_type(ent);
677         return get_method_value_param_type(mtp);
678 }
679
680 int get_irg_n_locs(ir_graph *irg)
681 {
682         if (get_opt_precise_exc_context())
683                 return irg->n_loc - 1 - 1;
684         else
685                 return irg->n_loc - 1;
686 }
687
688 /* Returns the obstack associated with the graph. */
689 struct obstack *
690 (get_irg_obstack)(const ir_graph *irg) {
691         return _get_irg_obstack(irg);
692 }
693
694 /*
695  * Returns true if the node n is allocated on the storage of graph irg.
696  *
697  * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
698  */
699 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
700 {
701         struct _obstack_chunk *p;
702
703         /*
704          * checks weather the ir_node pointer is on the obstack.
705          * A more sophisticated check would test the "whole" ir_node
706          */
707         for (p = irg->obst->chunk; p; p = p->prev) {
708                 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
709                         return 1;
710         }
711
712         return 0;
713 }
714
715 irg_phase_state (get_irg_phase_state)(const ir_graph *irg)
716 {
717         return _get_irg_phase_state(irg);
718 }
719
720 void (set_irg_phase_state)(ir_graph *irg, irg_phase_state state)
721 {
722         _set_irg_phase_state(irg, state);
723 }
724
725 op_pin_state (get_irg_pinned)(const ir_graph *irg)
726 {
727         return _get_irg_pinned(irg);
728 }
729
730 irg_outs_state (get_irg_outs_state)(const ir_graph *irg)
731 {
732         return _get_irg_outs_state(irg);
733 }
734
735 void (set_irg_outs_inconsistent)(ir_graph *irg)
736 {
737         _set_irg_outs_inconsistent(irg);
738 }
739
740 irg_extblk_state (get_irg_extblk_state)(const ir_graph *irg)
741 {
742         return _get_irg_extblk_state(irg);
743 }
744
745 void (set_irg_extblk_inconsistent)(ir_graph *irg)
746 {
747         _set_irg_extblk_inconsistent(irg);
748 }
749
750 irg_dom_state (get_irg_dom_state)(const ir_graph *irg)
751 {
752         return _get_irg_dom_state(irg);
753 }
754
755 irg_dom_state (get_irg_postdom_state)(const ir_graph *irg)
756 {
757         return _get_irg_postdom_state(irg);
758 }
759
760 void (set_irg_doms_inconsistent)(ir_graph *irg)
761 {
762         _set_irg_doms_inconsistent(irg);
763 }
764
765 irg_loopinfo_state (get_irg_loopinfo_state)(const ir_graph *irg)
766 {
767         return _get_irg_loopinfo_state(irg);
768 }
769
770 void (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s)
771 {
772         _set_irg_loopinfo_state(irg, s);
773 }
774
775 void (set_irg_loopinfo_inconsistent)(ir_graph *irg)
776 {
777         _set_irg_loopinfo_inconsistent(irg);
778 }
779
780 void set_irp_loopinfo_inconsistent(void)
781 {
782         int i;
783         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
784                 set_irg_loopinfo_inconsistent(get_irp_irg(i));
785         }
786 }
787
788
789
790 void (set_irg_pinned)(ir_graph *irg, op_pin_state p)
791 {
792         _set_irg_pinned(irg, p);
793 }
794
795 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
796 {
797         return _get_irg_callee_info_state(irg);
798 }
799
800 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
801 {
802         _set_irg_callee_info_state(irg, s);
803 }
804
805 irg_inline_property (get_irg_inline_property)(const ir_graph *irg)
806 {
807         return _get_irg_inline_property(irg);
808 }
809
810 void (set_irg_inline_property)(ir_graph *irg, irg_inline_property s)
811 {
812         _set_irg_inline_property(irg, s);
813 }
814
815 unsigned (get_irg_additional_properties)(const ir_graph *irg)
816 {
817         return _get_irg_additional_properties(irg);
818 }
819
820 void (set_irg_additional_properties)(ir_graph *irg, unsigned property_mask)
821 {
822         _set_irg_additional_properties(irg, property_mask);
823 }
824
825 void (set_irg_additional_property)(ir_graph *irg, mtp_additional_property flag)
826 {
827         _set_irg_additional_property(irg, flag);
828 }
829
830 void (set_irg_link)(ir_graph *irg, void *thing)
831 {
832         _set_irg_link(irg, thing);
833 }
834
835 void *(get_irg_link)(const ir_graph *irg)
836 {
837         return _get_irg_link(irg);
838 }
839
840 ir_visited_t (get_irg_visited)(const ir_graph *irg)
841 {
842         return _get_irg_visited(irg);
843 }
844
845 /** maximum visited flag content of all ir_graph visited fields. */
846 static ir_visited_t max_irg_visited = 0;
847
848 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
849 {
850         irg->visited = visited;
851         if (irg->visited > max_irg_visited) {
852                 max_irg_visited = irg->visited;
853         }
854 }
855
856 void inc_irg_visited(ir_graph *irg)
857 {
858         ++irg->visited;
859         if (irg->visited > max_irg_visited) {
860                 max_irg_visited = irg->visited;
861         }
862 }
863
864 ir_visited_t get_max_irg_visited(void)
865 {
866         return max_irg_visited;
867 }
868
869 void set_max_irg_visited(int val)
870 {
871         max_irg_visited = val;
872 }
873
874 ir_visited_t inc_max_irg_visited(void)
875 {
876 #ifndef NDEBUG
877         int i;
878         for (i = 0; i < get_irp_n_irgs(); i++)
879                 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
880 #endif
881         return ++max_irg_visited;
882 }
883
884 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
885 {
886         return _get_irg_block_visited(irg);
887 }
888
889 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
890 {
891         _set_irg_block_visited(irg, visited);
892 }
893
894 void (inc_irg_block_visited)(ir_graph *irg)
895 {
896   _inc_irg_block_visited(irg);
897 }
898
899 /* Return the floating point model of this graph. */
900 unsigned (get_irg_fp_model)(const ir_graph *irg)
901 {
902         return _get_irg_fp_model(irg);
903 }
904
905 /* Sets the floating point model for this graph. */
906 void set_irg_fp_model(ir_graph *irg, unsigned model)
907 {
908         irg->fp_model = model;
909 }
910
911 /**
912  * walker Start->End: places Proj nodes into the same block
913  * as it's predecessors
914  *
915  * @param n    the node
916  * @param env  ignored
917  */
918 static void normalize_proj_walker(ir_node *n, void *env)
919 {
920         (void) env;
921         if (is_Proj(n)) {
922                 ir_node *pred  = get_Proj_pred(n);
923                 ir_node *block = get_nodes_block(pred);
924
925                 set_nodes_block(n, block);
926         }
927 }
928
929 /* move Proj nodes into the same block as its predecessors */
930 void normalize_proj_nodes(ir_graph *irg)
931 {
932         irg_walk_graph(irg, NULL, normalize_proj_walker, NULL);
933         set_irg_outs_inconsistent(irg);
934 }
935
936 /* set a description for local value n */
937 void set_irg_loc_description(ir_graph *irg, int n, void *description)
938 {
939         assert(0 <= n && n < irg->n_loc);
940
941         if (! irg->loc_descriptions)
942                 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
943
944         irg->loc_descriptions[n] = description;
945 }
946
947 /* get the description for local value n */
948 void *get_irg_loc_description(ir_graph *irg, int n)
949 {
950         assert(0 <= n && n < irg->n_loc);
951         return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
952 }
953
954 void irg_register_phase(ir_graph *irg, ir_phase_id id, ir_phase *phase)
955 {
956         assert(id <= PHASE_LAST);
957         assert(irg->phases[id] == NULL);
958         irg->phases[id] = phase;
959 }
960
961 void irg_invalidate_phases(ir_graph *irg)
962 {
963         int p;
964         for (p = 0; p <= PHASE_LAST; ++p) {
965                 ir_phase *phase = irg->phases[p];
966                 if (phase == NULL)
967                         continue;
968
969                 phase_free(phase);
970                 irg->phases[p] = NULL;
971         }
972 }
973
974 #ifndef NDEBUG
975 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
976 {
977         assert((resources & ~IR_RESOURCE_LOCAL_MASK) == 0);
978         assert((irg->reserved_resources & resources) == 0);
979         irg->reserved_resources |= resources;
980 }
981
982 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
983 {
984         assert((irg->reserved_resources & resources) == resources);
985         irg->reserved_resources &= ~resources;
986 }
987
988 ir_resources_t ir_resources_reserved(const ir_graph *irg)
989 {
990         return irg->reserved_resources;
991 }
992 #endif /* NDEBUG */
993
994 /* Returns a estimated node count of the irg. */
995 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg)
996 {
997         return _get_irg_estimated_node_cnt(irg);
998 }
999
1000 /* Returns the last irn index for this graph. */
1001 unsigned get_irg_last_idx(const ir_graph *irg)
1002 {
1003         return irg->last_node_idx;
1004 }
1005
1006 /* register additional space in an IR graph */
1007 size_t register_additional_graph_data(size_t size)
1008 {
1009         assert(!forbid_new_data && "Too late to register additional node data");
1010
1011         if (forbid_new_data)
1012                 return 0;
1013
1014         return additional_graph_data_size += size;
1015 }
1016
1017 void (set_irg_state)(ir_graph *irg, ir_graph_state_t state)
1018 {
1019         _set_irg_state(irg, state);
1020 }
1021
1022 void (clear_irg_state)(ir_graph *irg, ir_graph_state_t state)
1023 {
1024         _clear_irg_state(irg, state);
1025 }
1026
1027 int (is_irg_state)(const ir_graph *irg, ir_graph_state_t state)
1028 {
1029         return _is_irg_state(irg, state);
1030 }