cleanup irouts
[libfirm] / ir / ir / irgraph.c
1 /*
2  * Copyright (C) 1995-2011 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  */
25 #include "config.h"
26
27 #include <string.h>
28 #include <stddef.h>
29
30 #include "xmalloc.h"
31 #include "ircons_t.h"
32 #include "irgraph_t.h"
33 #include "irprog_t.h"
34 #include "irgraph_t.h"
35 #include "irnode_t.h"
36 #include "iropt_t.h"
37 #include "irflag_t.h"
38 #include "array.h"
39 #include "irgmod.h"
40 #include "irouts.h"
41 #include "irhooks.h"
42 #include "irtools.h"
43 #include "util.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 "iroptimize.h"
50 #include "irgopt.h"
51
52 #define INITIAL_IDX_IRN_MAP_SIZE 1024
53
54 /**
55  * Indicates, whether additional data can be registered to graphs.
56  * If set to 1, this is not possible anymore.
57  */
58 static int forbid_new_data = 0;
59
60 /**
61  * The amount of additional space for custom data to be allocated upon
62  * creating a new graph.
63  */
64 static size_t additional_graph_data_size = 0;
65
66 ir_graph *current_ir_graph;
67 ir_graph *get_current_ir_graph(void)
68 {
69         return current_ir_graph;
70 }
71
72 void set_current_ir_graph(ir_graph *graph)
73 {
74         current_ir_graph = graph;
75 }
76
77 /** contains the suffix for frame type names */
78 static ident *frame_type_suffix = NULL;
79
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         ir_edge_kind_t  i;
116
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);
121 }
122
123 void irg_set_nloc(ir_graph *res, int n_loc)
124 {
125         assert(res->phase_state == phase_building);
126
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!  */
131
132         if (res->loc_descriptions) {
133                 xfree(res->loc_descriptions);
134                 res->loc_descriptions = NULL;
135         }
136 }
137
138 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc)
139 {
140         ir_graph *res;
141         ir_node  *first_block;
142         ir_node  *start, *start_block, *initial_mem, *projX;
143
144         res = alloc_graph();
145
146         /* inform statistics here, as blocks will be already build on this graph */
147         hook_new_graph(res, ent);
148
149         /*-- initialized for each graph. --*/
150         res->kind = k_ir_graph;
151         res->obst = XMALLOC(struct obstack);
152         obstack_init(res->obst);
153
154         res->phase_state = phase_building;
155         irg_set_nloc(res, n_loc);
156
157         /* descriptions will be allocated on demand */
158         res->loc_descriptions = NULL;
159
160         res->visited       = 0; /* visited flag, for the ir walker */
161         res->block_visited = 0; /* visited flag, for the 'block'-walker */
162
163         res->last_node_idx = 0;
164
165         new_identities(res);
166
167         res->inline_property       = irg_inline_any;
168         res->additional_properties = mtp_property_inherited;  /* inherited from type */
169
170         res->irg_pinned_state    = op_pin_state_pinned;
171         res->typeinfo_state      = ir_typeinfo_none;
172         set_irp_typeinfo_inconsistent();           /* there is a new graph with typeinfo_none. */
173         res->callee_info_state   = irg_callee_info_none;
174         res->class_cast_state    = ir_class_casts_transitive;
175         res->fp_model            = fp_model_precise;
176         res->mem_disambig_opt    = aa_opt_inherited;
177
178         /*-- Type information for the procedure of the graph --*/
179         res->ent = ent;
180         set_entity_irg(ent, res);
181
182         /*--  a class type so that it can contain "inner" methods as in Pascal. --*/
183         res->frame_type = new_type_frame();
184
185         /* the Anchor node must be created first */
186         res->anchor = new_r_Anchor(res);
187
188         /*-- Nodes needed in every graph --*/
189         set_irg_end_block(res, new_r_immBlock(res));
190         set_irg_end(res, new_r_End(res, 0, NULL));
191
192         start_block = new_r_Block_noopt(res, 0, NULL);
193         set_irg_start_block(res, start_block);
194         set_irg_no_mem     (res, new_r_NoMem(res));
195         start = new_r_Start(res);
196         set_irg_start      (res, start);
197
198         /* Proj results of start node */
199         projX                   = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
200         set_irg_initial_exec    (res, projX);
201         set_irg_frame           (res, new_r_Proj(start, mode_P_data, pn_Start_P_frame_base));
202         set_irg_args            (res, new_r_Proj(start, mode_T,      pn_Start_T_args));
203         initial_mem             = new_r_Proj(start, mode_M, pn_Start_M);
204         set_irg_initial_mem(res, initial_mem);
205
206         res->index       = get_irp_new_irg_idx();
207 #ifdef DEBUG_libfirm
208         res->graph_nr    = get_irp_new_node_nr();
209 #endif
210
211         set_r_cur_block(res, start_block);
212         set_r_store(res, initial_mem);
213
214         /*-- Make a block to start with --*/
215         first_block = new_r_Block(res, 1, &projX);
216         set_r_cur_block(res, first_block);
217
218         res->method_execution_frequency = -1.0;
219         res->estimated_node_count       = 0;
220
221         return res;
222 }
223
224 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
225 {
226         ir_graph *res = new_r_ir_graph(ent, n_loc);
227         add_irp_irg(res);          /* remember this graph global. */
228         return res;
229 }
230
231 ir_graph *new_const_code_irg(void)
232 {
233         ir_graph *res = alloc_graph();
234         ir_node  *body_block;
235         ir_node  *end;
236         ir_node  *end_block;
237         ir_node  *no_mem;
238         ir_node  *projX;
239         ir_node  *start_block;
240         ir_node  *start;
241
242         /* inform statistics here, as blocks will be already build on this graph */
243         hook_new_graph(res, NULL);
244
245         res->n_loc         = 1; /* Only the memory. */
246         res->visited       = 0; /* visited flag, for the ir walker */
247         res->block_visited = 0; /* visited flag, for the 'block'-walker */
248         res->obst          = XMALLOC(struct obstack);
249         obstack_init(res->obst);
250
251         res->last_node_idx = 0;
252
253         res->phase_state      = phase_building;
254         res->irg_pinned_state = op_pin_state_pinned;
255         res->fp_model         = fp_model_precise;
256
257         /* value table for global value numbering for optimizing use in iropt.c */
258         new_identities(res);
259         res->ent         = NULL;
260         res->frame_type  = NULL;
261
262         /* the Anchor node must be created first */
263         res->anchor = new_r_Anchor(res);
264
265         /* -- The end block -- */
266         end_block = new_r_Block_noopt(res, 0, NULL);
267         set_irg_end_block(res, end_block);
268         end = new_r_End(res, 0, NULL);
269         set_irg_end(res, end);
270
271         /* -- The start block -- */
272         start_block = new_r_Block_noopt(res, 0, NULL);
273         set_irg_start_block(res, start_block);
274         no_mem = new_r_NoMem(res);
275         set_irg_no_mem(res, no_mem);
276         start = new_r_Start(res);
277         set_irg_start(res, start);
278
279         /* Proj results of start node */
280         set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
281         projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
282
283         body_block = new_r_Block(res, 1, &projX);
284
285         set_r_cur_block(res, body_block);
286
287         /* Set the visited flag high enough that the blocks will never be visited. */
288         set_irn_visited(body_block, -1);
289         set_Block_block_visited(body_block, -1);
290         set_Block_block_visited(start_block, -1);
291         set_irn_visited(start_block, -1);
292
293         return res;
294 }
295
296 /**
297  * Pre-Walker: Copies blocks and nodes from the original method graph
298  * to the copied graph.
299  *
300  * @param n    A node from the original method graph.
301  * @param env  The copied graph.
302  */
303 static void copy_all_nodes(ir_node *node, void *env)
304 {
305         ir_graph *irg      = (ir_graph*)env;
306         ir_node  *new_node = irn_copy_into_irg(node, irg);
307
308         set_irn_link(node, new_node);
309
310         /* fix access to entities on the stack frame */
311         if (is_Sel(new_node)) {
312                 ir_entity *ent = get_Sel_entity(new_node);
313                 ir_type   *tp  = get_entity_owner(ent);
314
315                 if (is_frame_type(tp)) {
316                         /* replace by the copied entity */
317                         ent = (ir_entity*)get_entity_link(ent);
318
319                         assert(is_entity(ent));
320                         assert(get_entity_owner(ent) == get_irg_frame_type(irg));
321                         set_Sel_entity(new_node, ent);
322                 }
323         }
324 }
325
326 /**
327  * Post-walker: Set the predecessors of the copied nodes.
328  * The copied nodes are set as link of their original nodes. The links of
329  * "irn" predecessors are the predecessors of copied node.
330  */
331 static void rewire(ir_node *irn, void *env)
332 {
333         (void) env;
334         irn_rewire_inputs(irn);
335 }
336
337 static ir_node *get_new_node(const ir_node *old_node)
338 {
339         return (ir_node*) get_irn_link(old_node);
340 }
341
342 ir_graph *create_irg_copy(ir_graph *irg)
343 {
344         ir_graph *res;
345
346         res = alloc_graph();
347
348         res->n_loc = 0;
349         res->visited = 0;       /* visited flag, for the ir walker */
350         res->block_visited = 0; /* visited flag, for the 'block'-walker */
351         res->obst       = XMALLOC(struct obstack);
352         obstack_init(res->obst);
353
354         res->last_node_idx = 0;
355
356         res->phase_state      = irg->phase_state;
357         res->irg_pinned_state = irg->irg_pinned_state;
358         res->fp_model         = irg->fp_model;
359
360         new_identities(res);
361
362         /* clone the frame type here for safety */
363         irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
364         res->frame_type  = clone_frame_type(irg->frame_type);
365
366         res->phase_state = irg->phase_state;
367
368         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
369
370         /* copy all nodes from the graph irg to the new graph res */
371         irg_walk_anchors(irg, copy_all_nodes, rewire, res);
372
373         /* copy the Anchor node */
374         res->anchor = get_new_node(irg->anchor);
375
376         /* -- The end block -- */
377         set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
378         set_irg_end       (res, get_new_node(get_irg_end(irg)));
379
380         /* -- The start block -- */
381         set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
382         set_irg_no_mem     (res, get_new_node(get_irg_no_mem(irg)));
383         set_irg_start      (res, get_new_node(get_irg_start(irg)));
384
385         /* Proj results of start node */
386         set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
387
388         /* Copy the node count estimation. Would be strange if this
389            is different from the original one. */
390         res->estimated_node_count = irg->estimated_node_count;
391
392         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
393         irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
394
395         return res;
396 }
397
398 void free_ir_graph(ir_graph *irg)
399 {
400         assert(is_ir_graph(irg));
401
402         edges_deactivate(irg);
403
404         hook_free_graph(irg);
405         free_irg_outs(irg);
406         del_identities(irg);
407         if (irg->ent) {
408                 set_entity_irg(irg->ent, NULL);  /* not set in const code irg */
409         }
410
411         free_End(get_irg_end(irg));
412         obstack_free(irg->obst, NULL);
413         free(irg->obst);
414         if (irg->loc_descriptions)
415                 free(irg->loc_descriptions);
416         irg->kind = k_BAD;
417         free_graph(irg);
418 }
419
420 int (is_ir_graph)(const void *thing)
421 {
422         return is_ir_graph_(thing);
423 }
424
425 #ifdef DEBUG_libfirm
426 long get_irg_graph_nr(const ir_graph *irg)
427 {
428         return irg->graph_nr;
429 }
430 #else
431 long get_irg_graph_nr(const ir_graph *irg)
432 {
433         return PTR_TO_INT(irg);
434 }
435 #endif
436
437 size_t get_irg_idx(const ir_graph *irg)
438 {
439         return irg->index;
440 }
441
442 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
443 {
444         return get_idx_irn_(irg, idx);
445 }
446
447 ir_node *(get_irg_start_block)(const ir_graph *irg)
448 {
449         return get_irg_start_block_(irg);
450 }
451
452 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
453 {
454         set_irg_start_block_(irg, node);
455 }
456
457 ir_node *(get_irg_start)(const ir_graph *irg)
458 {
459         return get_irg_start_(irg);
460 }
461
462 void (set_irg_start)(ir_graph *irg, ir_node *node)
463 {
464         set_irg_start_(irg, node);
465 }
466
467 ir_node *(get_irg_end_block)(const ir_graph *irg)
468 {
469         return get_irg_end_block_(irg);
470 }
471
472 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
473 {
474         set_irg_end_block_(irg, node);
475 }
476
477 ir_node *(get_irg_end)(const ir_graph *irg)
478 {
479         return get_irg_end_(irg);
480 }
481
482 void (set_irg_end)(ir_graph *irg, ir_node *node)
483 {
484         set_irg_end_(irg, node);
485 }
486
487 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
488 {
489         return get_irg_initial_exec_(irg);
490 }
491
492 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
493 {
494         set_irg_initial_exec_(irg, node);
495 }
496
497 ir_node *(get_irg_frame)(const ir_graph *irg)
498 {
499         return get_irg_frame_(irg);
500 }
501
502 void (set_irg_frame)(ir_graph *irg, ir_node *node)
503 {
504         set_irg_frame_(irg, node);
505 }
506
507 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
508 {
509         return get_irg_initial_mem_(irg);
510 }
511
512 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
513 {
514         set_irg_initial_mem_(irg, node);
515 }
516
517 ir_node *(get_irg_args)(const ir_graph *irg)
518 {
519         return get_irg_args_(irg);
520 }
521
522 void (set_irg_args)(ir_graph *irg, ir_node *node)
523 {
524         set_irg_args_(irg, node);
525 }
526
527 ir_node *(get_irg_no_mem)(const ir_graph *irg)
528 {
529         return get_irg_no_mem_(irg);
530 }
531
532 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
533 {
534         set_irg_no_mem_(irg, node);
535 }
536
537 ir_entity *(get_irg_entity)(const ir_graph *irg)
538 {
539         return get_irg_entity_(irg);
540 }
541
542 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
543 {
544         set_irg_entity_(irg, ent);
545 }
546
547 ir_type *(get_irg_frame_type)(ir_graph *irg)
548 {
549         return get_irg_frame_type_(irg);
550 }
551
552 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
553 {
554         set_irg_frame_type_(irg, ftp);
555 }
556
557 int get_irg_n_locs(ir_graph *irg)
558 {
559         return irg->n_loc - 1;
560 }
561
562 struct obstack *(get_irg_obstack)(const ir_graph *irg)
563 {
564         return get_irg_obstack_(irg);
565 }
566
567 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
568 {
569         struct _obstack_chunk *p;
570
571         /*
572          * checks weather the ir_node pointer is on the obstack.
573          * A more sophisticated check would test the "whole" ir_node
574          */
575         for (p = irg->obst->chunk; p; p = p->prev) {
576                 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
577                         return 1;
578         }
579
580         return 0;
581 }
582
583 irg_phase_state (get_irg_phase_state)(const ir_graph *irg)
584 {
585         return get_irg_phase_state_(irg);
586 }
587
588 void (set_irg_phase_state)(ir_graph *irg, irg_phase_state state)
589 {
590         set_irg_phase_state_(irg, state);
591 }
592
593 op_pin_state (get_irg_pinned)(const ir_graph *irg)
594 {
595         return get_irg_pinned_(irg);
596 }
597
598 void (set_irg_pinned)(ir_graph *irg, op_pin_state p)
599 {
600         set_irg_pinned_(irg, p);
601 }
602
603 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
604 {
605         return get_irg_callee_info_state_(irg);
606 }
607
608 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
609 {
610         set_irg_callee_info_state_(irg, s);
611 }
612
613 irg_inline_property (get_irg_inline_property)(const ir_graph *irg)
614 {
615         return get_irg_inline_property_(irg);
616 }
617
618 void (set_irg_inline_property)(ir_graph *irg, irg_inline_property s)
619 {
620         set_irg_inline_property_(irg, s);
621 }
622
623 mtp_additional_properties (get_irg_additional_properties)(const ir_graph *irg)
624 {
625         return get_irg_additional_properties_(irg);
626 }
627
628 void (set_irg_additional_properties)(ir_graph *irg, mtp_additional_properties property_mask)
629 {
630         set_irg_additional_properties_(irg, property_mask);
631 }
632
633 void (add_irg_additional_properties)(ir_graph *irg, mtp_additional_properties flag)
634 {
635         add_irg_additional_properties_(irg, flag);
636 }
637
638 void (set_irg_link)(ir_graph *irg, void *thing)
639 {
640         set_irg_link_(irg, thing);
641 }
642
643 void *(get_irg_link)(const ir_graph *irg)
644 {
645         return get_irg_link_(irg);
646 }
647
648 ir_visited_t (get_irg_visited)(const ir_graph *irg)
649 {
650         return get_irg_visited_(irg);
651 }
652
653 /** maximum visited flag content of all ir_graph visited fields. */
654 static ir_visited_t max_irg_visited = 0;
655
656 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
657 {
658         irg->visited = visited;
659         if (irg->visited > max_irg_visited) {
660                 max_irg_visited = irg->visited;
661         }
662 }
663
664 void inc_irg_visited(ir_graph *irg)
665 {
666         ++irg->visited;
667         if (irg->visited > max_irg_visited) {
668                 max_irg_visited = irg->visited;
669         }
670 }
671
672 ir_visited_t get_max_irg_visited(void)
673 {
674         return max_irg_visited;
675 }
676
677 void set_max_irg_visited(int val)
678 {
679         max_irg_visited = val;
680 }
681
682 ir_visited_t inc_max_irg_visited(void)
683 {
684 #ifndef NDEBUG
685         size_t i;
686         for (i = 0; i < get_irp_n_irgs(); i++)
687                 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
688 #endif
689         return ++max_irg_visited;
690 }
691
692 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
693 {
694         return get_irg_block_visited_(irg);
695 }
696
697 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
698 {
699         set_irg_block_visited_(irg, visited);
700 }
701
702 void (inc_irg_block_visited)(ir_graph *irg)
703 {
704   inc_irg_block_visited_(irg);
705 }
706
707 unsigned (get_irg_fp_model)(const ir_graph *irg)
708 {
709         return get_irg_fp_model_(irg);
710 }
711
712 void set_irg_fp_model(ir_graph *irg, unsigned model)
713 {
714         irg->fp_model = model;
715 }
716
717 void set_irg_loc_description(ir_graph *irg, int n, void *description)
718 {
719         assert(0 <= n && n < irg->n_loc);
720
721         if (! irg->loc_descriptions)
722                 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
723
724         irg->loc_descriptions[n] = description;
725 }
726
727 void *get_irg_loc_description(ir_graph *irg, int n)
728 {
729         assert(0 <= n && n < irg->n_loc);
730         return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
731 }
732
733 #ifndef NDEBUG
734 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
735 {
736         assert((irg->reserved_resources & resources) == 0);
737         irg->reserved_resources |= resources;
738 }
739
740 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
741 {
742         assert((irg->reserved_resources & resources) == resources);
743         irg->reserved_resources &= ~resources;
744 }
745
746 ir_resources_t ir_resources_reserved(const ir_graph *irg)
747 {
748         return irg->reserved_resources;
749 }
750 #endif
751
752 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg)
753 {
754         return get_irg_estimated_node_cnt_(irg);
755 }
756
757 unsigned get_irg_last_idx(const ir_graph *irg)
758 {
759         return irg->last_node_idx;
760 }
761
762 size_t register_additional_graph_data(size_t size)
763 {
764         assert(!forbid_new_data && "Too late to register additional node data");
765
766         if (forbid_new_data)
767                 return 0;
768
769         return additional_graph_data_size += size;
770 }
771
772 void add_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
773 {
774         irg->constraints |= constraints;
775 }
776
777 void clear_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
778 {
779         irg->constraints &= ~constraints;
780 }
781
782 int (irg_is_constrained)(const ir_graph *irg, ir_graph_constraints_t constraints)
783 {
784         return irg_is_constrained_(irg, constraints);
785 }
786
787 void (add_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
788 {
789         add_irg_properties_(irg, props);
790 }
791
792 void (clear_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
793 {
794         clear_irg_properties_(irg, props);
795 }
796
797 int (irg_has_properties)(const ir_graph *irg, ir_graph_properties_t props)
798 {
799         return irg_has_properties_(irg, props);
800 }
801
802 typedef void (*assure_property_func)(ir_graph *irg);
803
804 void assure_irg_properties(ir_graph *irg, ir_graph_properties_t props)
805 {
806         static struct {
807                 ir_graph_properties_t property;
808                 assure_property_func  func;
809         } property_functions[] = {
810                 { IR_GRAPH_PROPERTY_ONE_RETURN,               normalize_one_return },
811                 { IR_GRAPH_PROPERTY_MANY_RETURNS,             normalize_n_returns },
812                 { IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES,        remove_critical_cf_edges },
813                 { IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE,      remove_unreachable_code },
814                 { IR_GRAPH_PROPERTY_NO_BADS,                  remove_bads },
815                 { IR_GRAPH_PROPERTY_NO_TUPLES,                remove_tuples },
816                 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE,     assure_doms },
817                 { IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE, assure_postdoms },
818                 { IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES,     assure_edges },
819                 { IR_GRAPH_PROPERTY_CONSISTENT_OUTS,          assure_irg_outs },
820                 { IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO,      assure_loopinfo },
821                 { IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE,  assure_irg_entity_usage_computed },
822                 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS, ir_compute_dominance_frontiers },
823         };
824         size_t i;
825         for (i = 0; i < ARRAY_SIZE(property_functions); ++i) {
826                 ir_graph_properties_t missing = props & ~irg->properties;
827                 if (missing & property_functions[i].property)
828                         property_functions[i].func(irg);
829         }
830         assert((props & ~irg->properties) == IR_GRAPH_PROPERTIES_NONE);
831 }
832
833 void confirm_irg_properties(ir_graph *irg, ir_graph_properties_t props)
834 {
835         clear_irg_properties(irg, ~props);
836         if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES))
837                 edges_deactivate(irg);
838         if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUTS)
839             && (irg->properties & IR_GRAPH_PROPERTY_CONSISTENT_OUTS))
840             free_irg_outs(irg);
841         if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS))
842                 ir_free_dominance_frontiers(irg);
843 }