8e256c8ab537700f3a80582796bafb37351dfa57
[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 (is_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK))
48                 clear_irg_state(irg, IR_GRAPH_STATE_NO_BAD_BLOCKS);
49         if (ir_entity_usage_not_computed == get_irg_entity_usage_state(irg))
50                 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_ENTITY_USAGE);
51         clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_BLOCKS); // FIXME seems to incorrect sometimes
52
53         /* assure that all requirements for the optimization are fulfilled */
54 #define PREPARE(st,func) if (st & (required ^ irg->state)) {func(irg); set_irg_state(irg,st);}
55         PREPARE(IR_GRAPH_STATE_ONE_RETURN,               normalize_one_return)
56         PREPARE(IR_GRAPH_STATE_NO_CRITICAL_EDGES,        remove_critical_cf_edges)
57         PREPARE(IR_GRAPH_STATE_NO_UNREACHABLE_BLOCKS,    remove_unreachable_blocks)
58         PREPARE(IR_GRAPH_STATE_NO_BAD_BLOCKS,            remove_bads)
59         PREPARE(IR_GRAPH_STATE_CONSISTENT_DOMINANCE,     assure_doms)
60         PREPARE(IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE, assure_postdoms)
61         PREPARE(IR_GRAPH_STATE_CONSISTENT_OUT_EDGES,     edges_assure)
62         PREPARE(IR_GRAPH_STATE_CONSISTENT_OUTS,          assure_irg_outs)
63         PREPARE(IR_GRAPH_STATE_CONSISTENT_LOOPINFO,      assure_cf_loop)
64         PREPARE(IR_GRAPH_STATE_CONSISTENT_ENTITY_USAGE,  assure_irg_entity_usage_computed)
65         PREPARE(IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS,    compute_extbb)
66
67         /* now all the requirements for the optimization are fulfilled */
68         if (dump_opts)
69                 dump_ir_graph(irg, opt->name);
70
71         new_irg_state = opt->optimization(irg);
72
73         if (dump_opts)
74                 dump_ir_graph(irg, opt->name);
75
76         /* unless the optimization returned that some state is retained,
77          * we disable the corresponding irg state.
78          * Since we currently duplicate information, sometimes another func must be called too.
79          */
80 #define INVALIDATE(state,func) if (!(state & new_irg_state)) {clear_irg_state(irg,state); func(irg);}
81         INVALIDATE(IR_GRAPH_STATE_NO_CRITICAL_EDGES,        nop)
82         INVALIDATE(IR_GRAPH_STATE_NO_UNREACHABLE_BLOCKS,    nop)
83         INVALIDATE(IR_GRAPH_STATE_NO_BAD_BLOCKS,            nop)
84         INVALIDATE(IR_GRAPH_STATE_ONE_RETURN,               nop)
85         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_DOMINANCE,     set_irg_doms_inconsistent)
86         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE, set_irg_postdoms_inconsistent)
87         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_OUTS,          set_irg_outs_inconsistent)
88         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_OUT_EDGES,     edges_deactivate)
89         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_LOOPINFO,      set_irg_loopinfo_inconsistent)
90         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_ENTITY_USAGE,  deactivate_entity_usage)
91         INVALIDATE(IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS,    set_irg_extblk_inconsistent)
92
93         if (!(new_irg_state & IR_GRAPH_STATE_BROKEN_FOR_VERIFIER)) {
94                 irg_verify(irg, VERIFY_ENFORCE_SSA);
95         }
96
97 }