Removed IR_GRAPH_STATE_BAD_BLOCK symbol
[libfirm] / ir / opt / opt_manage.c
1 #include "config.h"
2
3 #include <assert.h>
4 #include <stdbool.h>
5
6 #include "irgraph_t.h"
7
8 #include "iroptimize.h"
9 #include "irgopt.h"
10 #include "irdom.h"
11 #include "iredges.h"
12 #include "irouts.h"
13 #include "irverify.h"
14 #include "irdump.h"
15 #include "opt_manage.h"
16
17 // TODO some API to enable dumping
18 static const bool dump_opts = false;
19
20 static void deactivate_entity_usage(ir_graph *irg)
21 {
22         set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
23 }
24
25 static void nop(ir_graph *irg) {
26         (void)irg;
27 }
28
29 void perform_irg_optimization(ir_graph *irg, optdesc_t *opt)
30 {
31         ir_graph_state_t new_irg_state;
32         ir_graph_state_t required = opt->requirements;
33         const bool dump = get_irp_optimization_dumps();
34
35         /* no bad block requires no unreachable code */
36         if (required & IR_GRAPH_STATE_NO_BAD_BLOCKS)
37                 required |= IR_GRAPH_STATE_NO_UNREACHABLE_BLOCKS;
38
39         /** Some workarounds because information is currently duplicated */
40         // FIXME should not be necessary!
41         if (dom_inconsistent == get_irg_dom_state(irg))
42                 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
43         if (outs_inconsistent == get_irg_outs_state(irg))
44                 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS);
45         if (loopinfo_inconsistent == get_irg_loopinfo_state(irg))
46                 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO);
47         if (ir_entity_usage_not_computed == get_irg_entity_usage_state(irg))
48                 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_ENTITY_USAGE);
49
50         /* assure that all requirements for the optimization are fulfilled */
51 #define PREPARE(st,func) if (st & (required ^ irg->state)) {func(irg); set_irg_state(irg,st);}
52         PREPARE(IR_GRAPH_STATE_ONE_RETURN,               normalize_one_return)
53         PREPARE(IR_GRAPH_STATE_NO_CRITICAL_EDGES,        remove_critical_cf_edges)
54         PREPARE(IR_GRAPH_STATE_NO_UNREACHABLE_BLOCKS,    remove_unreachable_blocks)
55         PREPARE(IR_GRAPH_STATE_NO_BAD_BLOCKS,            remove_bads)
56         PREPARE(IR_GRAPH_STATE_CONSISTENT_DOMINANCE,     assure_doms)
57         PREPARE(IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE, assure_postdoms)
58         PREPARE(IR_GRAPH_STATE_CONSISTENT_OUT_EDGES,     edges_assure)
59         PREPARE(IR_GRAPH_STATE_CONSISTENT_OUTS,          assure_irg_outs)
60         PREPARE(IR_GRAPH_STATE_CONSISTENT_LOOPINFO,      assure_cf_loop)
61         PREPARE(IR_GRAPH_STATE_CONSISTENT_ENTITY_USAGE,  assure_irg_entity_usage_computed)
62         PREPARE(IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS,    compute_extbb)
63
64         /* now all the requirements for the optimization are fulfilled */
65         if (dump_opts)
66                 dump_ir_graph(irg, opt->name);
67
68         new_irg_state = opt->optimization(irg);
69
70         if (dump_opts)
71                 dump_ir_graph(irg, opt->name);
72
73         /* unless the optimization returned that some state is retained,
74          * we disable the corresponding irg state.
75          * Since we currently duplicate information, sometimes another func must be called too.
76          */
77 #define INVALIDATE(state,func) if (!(state & new_irg_state)) {clear_irg_state(irg,state); func(irg);}
78         INVALIDATE(IR_GRAPH_STATE_NO_CRITICAL_EDGES,        nop)
79         INVALIDATE(IR_GRAPH_STATE_NO_UNREACHABLE_BLOCKS,    nop)
80         INVALIDATE(IR_GRAPH_STATE_NO_BAD_BLOCKS,            nop)
81         INVALIDATE(IR_GRAPH_STATE_ONE_RETURN,               nop)
82         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_DOMINANCE,     set_irg_doms_inconsistent)
83         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE, set_irg_postdoms_inconsistent)
84         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_OUTS,          set_irg_outs_inconsistent)
85         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_OUT_EDGES,     edges_deactivate)
86         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_LOOPINFO,      set_irg_loopinfo_inconsistent)
87         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_ENTITY_USAGE,  deactivate_entity_usage)
88         INVALIDATE(IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS,    set_irg_extblk_inconsistent)
89
90         if (!(new_irg_state & IR_GRAPH_STATE_BROKEN_FOR_VERIFIER)) {
91                 irg_verify(irg, VERIFY_ENFORCE_SSA);
92         }
93
94 }