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