trverify: cleanup, check irg.entity == entity.irg
[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 #include "irnode.h"
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 nop(ir_graph *irg) {
19         (void)irg;
20 }
21
22 void perform_irg_optimization(ir_graph *irg, optdesc_t *opt)
23 {
24         ir_graph_state_t new_irg_state;
25         ir_graph_state_t required = opt->requirements;
26         const bool dump = get_irp_optimization_dumps();
27
28         /* It does not make sense to require both: */
29         assert (!((required & IR_GRAPH_STATE_ONE_RETURN) && (required & IR_GRAPH_STATE_MANY_RETURNS)));
30
31         /* assure that all requirements for the optimization are fulfilled */
32 #define PREPARE(st,func) if (st & (required ^ irg->state)) {func(irg); set_irg_state(irg,st);}
33         PREPARE(IR_GRAPH_STATE_ONE_RETURN,               normalize_one_return)
34         PREPARE(IR_GRAPH_STATE_MANY_RETURNS,             normalize_n_returns)
35         PREPARE(IR_GRAPH_STATE_NO_CRITICAL_EDGES,        remove_critical_cf_edges)
36         PREPARE(IR_GRAPH_STATE_NO_UNREACHABLE_CODE,      remove_unreachable_code)
37         PREPARE(IR_GRAPH_STATE_NO_BADS,                  remove_bads)
38         PREPARE(IR_GRAPH_STATE_CONSISTENT_DOMINANCE,     assure_doms)
39         PREPARE(IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE, assure_postdoms)
40         PREPARE(IR_GRAPH_STATE_CONSISTENT_OUT_EDGES,     edges_assure)
41         PREPARE(IR_GRAPH_STATE_CONSISTENT_OUTS,          assure_irg_outs)
42         PREPARE(IR_GRAPH_STATE_CONSISTENT_LOOPINFO,      assure_loopinfo)
43         PREPARE(IR_GRAPH_STATE_CONSISTENT_ENTITY_USAGE,  assure_irg_entity_usage_computed)
44         PREPARE(IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS,    compute_extbb)
45
46         /* now all the requirements for the optimization are fulfilled */
47         if (dump)
48                 dump_ir_graph(irg, opt->name);
49
50         new_irg_state = opt->optimization(irg);
51
52         if (dump)
53                 dump_ir_graph(irg, opt->name);
54
55         /* unless the optimization returned that some state is retained,
56          * we disable the corresponding irg state.
57          * Since we currently duplicate information, sometimes another func must be called too.
58          */
59 #define INVALIDATE(state,func) if (!(state & new_irg_state)) {clear_irg_state(irg,state); func(irg);}
60         INVALIDATE(IR_GRAPH_STATE_NO_CRITICAL_EDGES,        nop)
61         INVALIDATE(IR_GRAPH_STATE_NO_UNREACHABLE_CODE,      nop)
62         INVALIDATE(IR_GRAPH_STATE_NO_BADS,                  nop)
63         INVALIDATE(IR_GRAPH_STATE_ONE_RETURN,               nop)
64         INVALIDATE(IR_GRAPH_STATE_MANY_RETURNS,             nop)
65         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_DOMINANCE,     nop)
66         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE, nop)
67         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_OUTS,          nop)
68         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_OUT_EDGES,     edges_deactivate)
69         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_LOOPINFO,      nop)
70         INVALIDATE(IR_GRAPH_STATE_CONSISTENT_ENTITY_USAGE,  nop)
71         INVALIDATE(IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS,    nop)
72
73         remove_End_Bads_and_doublets(get_irg_end(irg));
74
75         irg_verify(irg, VERIFY_ENFORCE_SSA);
76 }