1 // SPDX-License-Identifier: MIT
3 // optimize.c - main optimization loop
5 // Copyright (C) 2004 Linus Torvalds
6 // Copyright (C) 2004 Christopher Li
10 #include "flowgraph.h"
11 #include "linearize.h"
20 static void clear_symbol_pseudos(struct entrypoint
*ep
)
24 FOR_EACH_PTR(ep
->accesses
, pseudo
) {
25 pseudo
->sym
->pseudo
= NULL
;
26 } END_FOR_EACH_PTR(pseudo
);
30 static void clean_up_insns(struct entrypoint
*ep
)
32 struct basic_block
*bb
;
34 FOR_EACH_PTR(ep
->bbs
, bb
) {
35 struct instruction
*insn
;
36 FOR_EACH_PTR(bb
->insns
, insn
) {
37 repeat_phase
|= simplify_instruction(insn
);
40 assert(insn
->bb
== bb
);
42 } END_FOR_EACH_PTR(insn
);
43 } END_FOR_EACH_PTR(bb
);
46 void optimize(struct entrypoint
*ep
)
48 if (fdump_ir
& PASS_LINEARIZE
)
52 * Do trivial flow simplification - branches to
53 * branches, kill dead basicblocks etc
55 kill_unreachable_bbs(ep
);
61 * Turn symbols into pseudos
63 if (fpasses
& PASS_MEM2REG
)
66 if (fdump_ir
& PASS_MEM2REG
)
69 if (!(fpasses
& PASS_OPTIM
))
73 * Remove trivial instructions, and try to CSE
81 if (repeat_phase
& REPEAT_CFG_CLEANUP
)
82 kill_unreachable_bbs(ep
);
86 if (repeat_phase
& REPEAT_SYMBOL_CLEANUP
)
88 } while (repeat_phase
);
89 pack_basic_blocks(ep
);
90 if (repeat_phase
& REPEAT_CFG_CLEANUP
)
91 kill_unreachable_bbs(ep
);
92 } while (repeat_phase
);
97 clear_symbol_pseudos(ep
);
99 /* And track pseudo register usage */
100 track_pseudo_liveness(ep
);
103 * Some flow optimizations can only effectively
104 * be done when we've done liveness analysis. But
105 * if they trigger, we need to start all over
108 if (simplify_flow(ep
)) {
110 if (repeat_phase
& REPEAT_CFG_CLEANUP
)
111 kill_unreachable_bbs(ep
);
115 /* Finally, add deathnotes to pseudos now that we have them */
117 track_pseudo_death(ep
);