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
82 if (repeat_phase
& REPEAT_CFG_CLEANUP
)
83 kill_unreachable_bbs(ep
);
87 if (repeat_phase
& REPEAT_SYMBOL_CLEANUP
)
90 } while (repeat_phase
);
91 pack_basic_blocks(ep
);
93 if (repeat_phase
& REPEAT_CFG_CLEANUP
)
94 kill_unreachable_bbs(ep
);
96 } while (repeat_phase
);
102 clear_symbol_pseudos(ep
);
104 /* And track pseudo register usage */
105 track_pseudo_liveness(ep
);
108 * Some flow optimizations can only effectively
109 * be done when we've done liveness analysis. But
110 * if they trigger, we need to start all over
113 if (simplify_flow(ep
)) {
116 if (repeat_phase
& REPEAT_CFG_CLEANUP
)
117 kill_unreachable_bbs(ep
);
122 /* Finally, add deathnotes to pseudos now that we have them */
124 track_pseudo_death(ep
);