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