1 // SPDX-License-Identifier: MIT
3 // Copyright (C) 2004 Linus Torvalds
4 // Copyright (C) 2004 Christopher Li
7 // Optimization main loop
8 // ----------------------
12 #include "flowgraph.h"
13 #include "linearize.h"
23 static void clear_symbol_pseudos(struct entrypoint
*ep
)
27 FOR_EACH_PTR(ep
->accesses
, pseudo
) {
28 pseudo
->sym
->pseudo
= NULL
;
29 } END_FOR_EACH_PTR(pseudo
);
33 static void clean_up_insns(struct entrypoint
*ep
)
35 struct basic_block
*bb
;
37 FOR_EACH_PTR(ep
->bbs
, bb
) {
38 struct instruction
*insn
;
39 FOR_EACH_PTR(bb
->insns
, insn
) {
42 repeat_phase
|= simplify_instruction(insn
);
45 assert(insn
->bb
== bb
);
47 } END_FOR_EACH_PTR(insn
);
48 } END_FOR_EACH_PTR(bb
);
51 static void cleanup_cfg(struct entrypoint
*ep
)
53 kill_unreachable_bbs(ep
);
58 // optimization main loop
59 void optimize(struct entrypoint
*ep
)
61 if (fdump_ir
& PASS_LINEARIZE
)
65 * Do trivial flow simplification - branches to
66 * branches, kill dead basicblocks etc
68 kill_unreachable_bbs(ep
);
72 if (simplify_cfg_early(ep
))
73 kill_unreachable_bbs(ep
);
79 * Turn symbols into pseudos
81 if (fpasses
& PASS_MEM2REG
)
84 if (fdump_ir
& PASS_MEM2REG
)
87 if (!(fpasses
& PASS_OPTIM
))
91 * Remove trivial instructions, and try to CSE
99 if (repeat_phase
& REPEAT_CFG_CLEANUP
)
100 kill_unreachable_bbs(ep
);
104 } while (repeat_phase
);
105 pack_basic_blocks(ep
);
106 if (repeat_phase
& REPEAT_CFG_CLEANUP
)
108 } while (repeat_phase
);
113 clear_symbol_pseudos(ep
);
115 /* And track pseudo register usage */
116 track_pseudo_liveness(ep
);
119 * Some flow optimizations can only effectively
120 * be done when we've done liveness analysis. But
121 * if they trigger, we need to start all over
124 if (simplify_flow(ep
)) {
126 if (repeat_phase
& REPEAT_CFG_CLEANUP
)
131 /* Finally, add deathnotes to pseudos now that we have them */
133 track_pseudo_death(ep
);