1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * Copyright (C) 2014-2021 Paul Cercueil <paul@crapouillou.net>
7 #include "lightrec-config.h"
8 #include "disassembler.h"
10 #include "memmanager.h"
11 #include "optimizer.h"
19 #define IF_OPT(opt, ptr) ((opt) ? (ptr) : NULL)
21 struct optimizer_list {
22 void (**optimizers)(struct opcode *);
23 unsigned int nb_optimizers;
26 static bool is_nop(union code op);
28 bool is_unconditional_jump(union code c)
32 return c.r.op == OP_SPECIAL_JR || c.r.op == OP_SPECIAL_JALR;
38 return c.i.rs == c.i.rt;
40 return (c.r.rt == OP_REGIMM_BGEZ ||
41 c.r.rt == OP_REGIMM_BGEZAL) && c.i.rs == 0;
47 bool is_syscall(union code c)
49 return (c.i.op == OP_SPECIAL && c.r.op == OP_SPECIAL_SYSCALL) ||
50 (c.i.op == OP_CP0 && (c.r.rs == OP_CP0_MTC0 ||
51 c.r.rs == OP_CP0_CTC0) &&
52 (c.r.rd == 12 || c.r.rd == 13));
55 static u64 opcode_read_mask(union code op)
60 case OP_SPECIAL_SYSCALL:
61 case OP_SPECIAL_BREAK:
80 return BIT(op.r.rs) | BIT(op.r.rt);
91 if (op.r.op == OP_CP2_BASIC) {
93 case OP_CP2_BASIC_MTC2:
94 case OP_CP2_BASIC_CTC2:
106 if (op.i.rs == op.i.rt)
119 return BIT(op.i.rs) | BIT(op.i.rt);
127 static u64 mult_div_write_mask(union code op)
131 if (!OPT_FLAG_MULT_DIV)
132 return BIT(REG_LO) | BIT(REG_HI);
135 flags = BIT(op.r.rd);
139 flags |= BIT(op.r.imm);
141 flags |= BIT(REG_HI);
146 u64 opcode_write_mask(union code op)
151 return mult_div_write_mask(op);
157 case OP_SPECIAL_SYSCALL:
158 case OP_SPECIAL_BREAK:
160 case OP_SPECIAL_MULT:
161 case OP_SPECIAL_MULTU:
163 case OP_SPECIAL_DIVU:
164 return mult_div_write_mask(op);
165 case OP_SPECIAL_MTHI:
167 case OP_SPECIAL_MTLO:
204 if (op.r.op == OP_CP2_BASIC) {
206 case OP_CP2_BASIC_MFC2:
207 case OP_CP2_BASIC_CFC2:
216 case OP_REGIMM_BLTZAL:
217 case OP_REGIMM_BGEZAL:
227 bool opcode_reads_register(union code op, u8 reg)
229 return opcode_read_mask(op) & BIT(reg);
232 bool opcode_writes_register(union code op, u8 reg)
234 return opcode_write_mask(op) & BIT(reg);
237 static int find_prev_writer(const struct opcode *list, unsigned int offset, u8 reg)
242 if (op_flag_sync(list[offset].flags))
245 for (i = offset; i > 0; i--) {
248 if (opcode_writes_register(c, reg)) {
249 if (i > 1 && has_delay_slot(list[i - 2].c))
255 if (op_flag_sync(list[i - 1].flags) ||
257 opcode_reads_register(c, reg))
264 static int find_next_reader(const struct opcode *list, unsigned int offset, u8 reg)
269 if (op_flag_sync(list[offset].flags))
272 for (i = offset; ; i++) {
275 if (opcode_reads_register(c, reg))
278 if (op_flag_sync(list[i].flags)
279 || (op_flag_no_ds(list[i].flags) && has_delay_slot(c))
280 || is_delay_slot(list, i)
281 || opcode_writes_register(c, reg))
288 static bool reg_is_dead(const struct opcode *list, unsigned int offset, u8 reg)
292 if (op_flag_sync(list[offset].flags) || is_delay_slot(list, offset))
295 for (i = offset + 1; ; i++) {
296 if (opcode_reads_register(list[i].c, reg))
299 if (opcode_writes_register(list[i].c, reg))
302 if (has_delay_slot(list[i].c)) {
303 if (op_flag_no_ds(list[i].flags) ||
304 opcode_reads_register(list[i + 1].c, reg))
307 return opcode_writes_register(list[i + 1].c, reg);
312 static bool reg_is_read(const struct opcode *list,
313 unsigned int a, unsigned int b, u8 reg)
315 /* Return true if reg is read in one of the opcodes of the interval
318 if (!is_nop(list[a].c) && opcode_reads_register(list[a].c, reg))
325 static bool reg_is_written(const struct opcode *list,
326 unsigned int a, unsigned int b, u8 reg)
328 /* Return true if reg is written in one of the opcodes of the interval
332 if (!is_nop(list[a].c) && opcode_writes_register(list[a].c, reg))
339 static bool reg_is_read_or_written(const struct opcode *list,
340 unsigned int a, unsigned int b, u8 reg)
342 return reg_is_read(list, a, b, reg) || reg_is_written(list, a, b, reg);
345 bool opcode_is_mfc(union code op)
359 if (op.r.op == OP_CP2_BASIC) {
361 case OP_CP2_BASIC_MFC2:
362 case OP_CP2_BASIC_CFC2:
377 bool opcode_is_load(union code op)
395 static bool opcode_is_store(union code op)
411 static u8 opcode_get_io_size(union code op)
427 bool opcode_is_io(union code op)
429 return opcode_is_load(op) || opcode_is_store(op);
433 static bool is_nop(union code op)
435 if (opcode_writes_register(op, 0)) {
438 return op.r.rs != OP_CP0_MFC0;
457 return op.r.rd == op.r.rt && op.r.rd == op.r.rs;
459 case OP_SPECIAL_ADDU:
460 return (op.r.rd == op.r.rt && op.r.rs == 0) ||
461 (op.r.rd == op.r.rs && op.r.rt == 0);
463 case OP_SPECIAL_SUBU:
464 return op.r.rd == op.r.rs && op.r.rt == 0;
466 if (op.r.rd == op.r.rt)
467 return op.r.rd == op.r.rs || op.r.rs == 0;
469 return (op.r.rd == op.r.rs) && op.r.rt == 0;
473 return op.r.rd == op.r.rt && op.r.imm == 0;
474 case OP_SPECIAL_MFHI:
475 case OP_SPECIAL_MFLO:
483 return op.i.rt == op.i.rs && op.i.imm == 0;
485 return (op.i.rs == 0 || op.i.imm == 1);
487 return (op.i.op == OP_REGIMM_BLTZ ||
488 op.i.op == OP_REGIMM_BLTZAL) &&
489 (op.i.rs == 0 || op.i.imm == 1);
491 return (op.i.rs == op.i.rt || op.i.imm == 1);
497 static void lightrec_optimize_sll_sra(struct opcode *list, unsigned int offset,
498 struct constprop_data *v)
500 struct opcode *ldop = NULL, *curr = &list[offset], *next;
501 struct opcode *to_change, *to_nop;
504 if (curr->r.imm != 24 && curr->r.imm != 16)
507 if (is_delay_slot(list, offset))
510 idx = find_next_reader(list, offset + 1, curr->r.rd);
516 if (next->i.op != OP_SPECIAL || next->r.op != OP_SPECIAL_SRA ||
517 next->r.imm != curr->r.imm || next->r.rt != curr->r.rd)
520 if (curr->r.rd != curr->r.rt && next->r.rd != next->r.rt) {
525 if (!reg_is_dead(list, idx, curr->r.rd) ||
526 reg_is_read_or_written(list, offset, idx, next->r.rd))
529 /* If rY is dead after the SRL, and rZ is not used after the SLL,
530 * we can change rY to rZ */
532 pr_debug("Detected SLL/SRA with middle temp register\n");
533 curr->r.rd = next->r.rd;
534 next->r.rt = curr->r.rd;
537 /* We got a SLL/SRA combo. If imm #16, that's a cast to s16.
538 * If imm #24 that's a cast to s8.
540 * First of all, make sure that the target register of the SLL is not
541 * read after the SRA. */
543 if (curr->r.rd == curr->r.rt) {
550 /* rX is used after the SRA - we cannot convert it. */
551 if (curr->r.rd != next->r.rd && !reg_is_dead(list, idx, curr->r.rd))
561 idx2 = find_prev_writer(list, offset, curr->r.rt);
563 /* Note that PSX games sometimes do casts after
564 * a LHU or LBU; in this case we can change the
565 * load opcode to a LH or LB, and the cast can
566 * be changed to a MOV or a simple NOP. */
570 if (next->r.rd != ldop->i.rt &&
571 !reg_is_dead(list, idx, ldop->i.rt))
573 else if (curr->r.imm == 16 && ldop->i.op == OP_LHU)
575 else if (curr->r.imm == 24 && ldop->i.op == OP_LBU)
581 if (next->r.rd == ldop->i.rt) {
582 to_change->opcode = 0;
583 } else if (reg_is_dead(list, idx, ldop->i.rt) &&
584 !reg_is_read_or_written(list, idx2 + 1, idx, next->r.rd)) {
585 /* The target register of the SRA is dead after the
586 * LBU/LHU; we can change the target register of the
587 * LBU/LHU to the one of the SRA. */
588 v[ldop->i.rt].known = 0;
589 v[ldop->i.rt].sign = 0;
590 ldop->i.rt = next->r.rd;
591 to_change->opcode = 0;
593 to_change->i.op = OP_META;
594 to_change->m.op = OP_META_MOV;
595 to_change->m.rd = next->r.rd;
596 to_change->m.rs = ldop->i.rt;
599 if (to_nop->r.imm == 24)
600 pr_debug("Convert LBU+SLL+SRA to LB\n");
602 pr_debug("Convert LHU+SLL+SRA to LH\n");
604 v[ldop->i.rt].known = 0;
605 v[ldop->i.rt].sign = 0xffffff80 << (24 - curr->r.imm);
610 pr_debug("Convert SLL/SRA #%u to EXT%c\n",
611 curr->r.imm, curr->r.imm == 24 ? 'C' : 'S');
613 to_change->m.rs = curr->r.rt;
614 to_change->m.op = to_nop->r.imm == 24 ? OP_META_EXTC : OP_META_EXTS;
615 to_change->i.op = OP_META;
622 lightrec_remove_useless_lui(struct block *block, unsigned int offset,
623 const struct constprop_data *v)
625 struct opcode *list = block->opcode_list,
626 *op = &block->opcode_list[offset];
629 if (!op_flag_sync(op->flags) && is_known(v, op->i.rt) &&
630 v[op->i.rt].value == op->i.imm << 16) {
631 pr_debug("Converting duplicated LUI to NOP\n");
636 if (op->i.imm != 0 || op->i.rt == 0 || offset == block->nb_ops - 1)
639 reader = find_next_reader(list, offset + 1, op->i.rt);
643 if (opcode_writes_register(list[reader].c, op->i.rt) ||
644 reg_is_dead(list, reader, op->i.rt)) {
645 pr_debug("Removing useless LUI 0x0\n");
647 if (list[reader].i.rs == op->i.rt)
648 list[reader].i.rs = 0;
649 if (list[reader].i.op == OP_SPECIAL &&
650 list[reader].i.rt == op->i.rt)
651 list[reader].i.rt = 0;
656 static void lightrec_lui_to_movi(struct block *block, unsigned int offset)
658 struct opcode *ori, *lui = &block->opcode_list[offset];
661 if (lui->i.op != OP_LUI)
664 next = find_next_reader(block->opcode_list, offset + 1, lui->i.rt);
666 ori = &block->opcode_list[next];
672 if (ori->i.rs == ori->i.rt && ori->i.imm) {
673 ori->flags |= LIGHTREC_MOVI;
674 lui->flags |= LIGHTREC_MOVI;
681 static void lightrec_modify_lui(struct block *block, unsigned int offset)
683 union code c, *lui = &block->opcode_list[offset].c;
684 bool stop = false, stop_next = false;
687 for (i = offset + 1; !stop && i < block->nb_ops; i++) {
688 c = block->opcode_list[i].c;
691 if ((opcode_is_store(c) && c.i.rt == lui->i.rt)
692 || (!opcode_is_load(c) && opcode_reads_register(c, lui->i.rt)))
695 if (opcode_writes_register(c, lui->i.rt)) {
696 if (c.i.op == OP_LWL || c.i.op == OP_LWR) {
697 /* LWL/LWR only partially write their target register;
698 * therefore the LUI should not write a different value. */
702 pr_debug("Convert LUI at offset 0x%x to kuseg\n",
704 lui->i.imm = kunseg(lui->i.imm << 16) >> 16;
708 if (has_delay_slot(c))
713 static int lightrec_transform_branches(struct lightrec_state *state,
720 for (i = 0; i < block->nb_ops; i++) {
721 op = &block->opcode_list[i];
725 /* Transform J opcode into BEQ $zero, $zero if possible. */
726 offset = (s32)((block->pc & 0xf0000000) >> 2 | op->j.imm)
727 - (s32)(block->pc >> 2) - (s32)i - 1;
729 if (offset == (s16)offset) {
730 pr_debug("Transform J into BEQ $zero, $zero\n");
746 static inline bool is_power_of_two(u32 value)
748 return popcount32(value) == 1;
751 static void lightrec_patch_known_zero(struct opcode *op,
752 const struct constprop_data *v)
758 case OP_SPECIAL_JALR:
759 case OP_SPECIAL_MTHI:
760 case OP_SPECIAL_MTLO:
761 if (is_known_zero(v, op->r.rs))
765 if (is_known_zero(v, op->r.rs))
771 if (is_known_zero(v, op->r.rt))
774 case OP_SPECIAL_SYSCALL:
775 case OP_SPECIAL_BREAK:
776 case OP_SPECIAL_MFHI:
777 case OP_SPECIAL_MFLO:
785 if (is_known_zero(v, op->r.rt))
793 if (op->r.op == OP_CP2_BASIC) {
795 case OP_CP2_BASIC_MTC2:
796 case OP_CP2_BASIC_CTC2:
797 if (is_known_zero(v, op->r.rt))
807 if (is_known_zero(v, op->i.rt))
823 if (is_known_zero(v, op->m.rs))
832 if (is_known_zero(v, op->i.rt))
845 if (is_known(v, op->i.rs)
846 && kunseg(v[op->i.rs].value) == 0)
854 static void lightrec_reset_syncs(struct block *block)
856 struct opcode *op, *list = block->opcode_list;
860 for (i = 0; i < block->nb_ops; i++)
861 list[i].flags &= ~LIGHTREC_SYNC;
863 for (i = 0; i < block->nb_ops; i++) {
866 if (has_delay_slot(op->c)) {
867 if (op_flag_local_branch(op->flags)) {
868 offset = i + 1 - op_flag_no_ds(op->flags) + (s16)op->i.imm;
869 list[offset].flags |= LIGHTREC_SYNC;
872 if (op_flag_emulate_branch(op->flags) && i + 2 < block->nb_ops)
873 list[i + 2].flags |= LIGHTREC_SYNC;
878 static void maybe_remove_load_delay(struct opcode *op)
880 if (op_flag_load_delay(op->flags) && opcode_is_load(op->c))
881 op->flags &= ~LIGHTREC_LOAD_DELAY;
884 static int lightrec_transform_ops(struct lightrec_state *state, struct block *block)
886 struct opcode *op, *list = block->opcode_list;
887 struct constprop_data v[32] = LIGHTREC_CONSTPROP_INITIALIZER;
893 for (i = 0; i < block->nb_ops; i++) {
896 lightrec_consts_propagate(block, i, v);
898 lightrec_patch_known_zero(op, v);
900 /* Transform all opcodes detected as useless to real NOPs
901 * (0x0: SLL r0, r0, #0) */
902 if (op->opcode != 0 && is_nop(op->c)) {
903 pr_debug("Converting useless opcode 0x%08x to NOP\n",
913 if (op->i.rs == op->i.rt ||
914 (is_known(v, op->i.rs) && is_known(v, op->i.rt) &&
915 v[op->i.rs].value == v[op->i.rt].value)) {
916 if (op->i.rs != op->i.rt)
917 pr_debug("Found always-taken BEQ\n");
921 } else if (v[op->i.rs].known & v[op->i.rt].known &
922 (v[op->i.rs].value ^ v[op->i.rt].value)) {
923 pr_debug("Found never-taken BEQ\n");
925 if (!op_flag_no_ds(op->flags))
926 maybe_remove_load_delay(&list[i + 1]);
928 local = op_flag_local_branch(op->flags);
933 lightrec_reset_syncs(block);
934 } else if (op->i.rs == 0) {
941 if (v[op->i.rs].known & v[op->i.rt].known &
942 (v[op->i.rs].value ^ v[op->i.rt].value)) {
943 pr_debug("Found always-taken BNE\n");
948 } else if (is_known(v, op->i.rs) && is_known(v, op->i.rt) &&
949 v[op->i.rs].value == v[op->i.rt].value) {
950 pr_debug("Found never-taken BNE\n");
952 if (!op_flag_no_ds(op->flags))
953 maybe_remove_load_delay(&list[i + 1]);
955 local = op_flag_local_branch(op->flags);
960 lightrec_reset_syncs(block);
961 } else if (op->i.rs == 0) {
968 if (v[op->i.rs].known & BIT(31) &&
969 v[op->i.rs].value & BIT(31)) {
970 pr_debug("Found always-taken BLEZ\n");
979 if (v[op->i.rs].known & BIT(31) &&
980 v[op->i.rs].value & BIT(31)) {
981 pr_debug("Found never-taken BGTZ\n");
983 if (!op_flag_no_ds(op->flags))
984 maybe_remove_load_delay(&list[i + 1]);
986 local = op_flag_local_branch(op->flags);
991 lightrec_reset_syncs(block);
996 if (i == 0 || !has_delay_slot(list[i - 1].c))
997 lightrec_modify_lui(block, i);
998 lightrec_remove_useless_lui(block, i, v);
999 if (i == 0 || !has_delay_slot(list[i - 1].c))
1000 lightrec_lui_to_movi(block, i);
1003 /* Transform ORI/ADDI/ADDIU with imm #0 or ORR/ADD/ADDU/SUB/SUBU
1004 * with register $zero to the MOV meta-opcode */
1008 if (op->i.imm == 0) {
1009 pr_debug("Convert ORI/ADDI/ADDIU #0 to MOV\n");
1010 op->m.rd = op->i.rt;
1011 op->m.op = OP_META_MOV;
1016 if (bits_are_known_zero(v, op->i.rs, ~op->i.imm)) {
1017 pr_debug("Found useless ANDI 0x%x\n", op->i.imm);
1019 if (op->i.rs == op->i.rt) {
1022 op->m.rd = op->i.rt;
1023 op->m.op = OP_META_MOV;
1030 if (i == 0 || !has_delay_slot(list[i - 1].c)) {
1031 idx = find_next_reader(list, i + 1, op->i.rt);
1032 if (idx > 0 && list[idx].i.op == (op->i.op ^ 0x4)
1033 && list[idx].i.rs == op->i.rs
1034 && list[idx].i.rt == op->i.rt
1035 && abs((s16)op->i.imm - (s16)list[idx].i.imm) == 3) {
1036 /* Replace a LWL/LWR combo with a META_LWU */
1037 if (op->i.op == OP_LWL)
1039 op->i.op = OP_META_LWU;
1040 list[idx].opcode = 0;
1041 pr_debug("Convert LWL/LWR to LWU\n");
1047 if (i == 0 || !has_delay_slot(list[i - 1].c)) {
1048 idx = find_next_reader(list, i + 1, op->i.rt);
1049 if (idx > 0 && list[idx].i.op == (op->i.op ^ 0x4)
1050 && list[idx].i.rs == op->i.rs
1051 && list[idx].i.rt == op->i.rt
1052 && abs((s16)op->i.imm - (s16)list[idx].i.imm) == 3) {
1053 /* Replace a SWL/SWR combo with a META_SWU */
1054 if (op->i.op == OP_SWL)
1056 op->i.op = OP_META_SWU;
1057 list[idx].opcode = 0;
1058 pr_debug("Convert SWL/SWR to SWU\n");
1064 case OP_REGIMM_BLTZ:
1065 case OP_REGIMM_BGEZ:
1066 if (!(v[op->r.rs].known & BIT(31)))
1069 if (!!(v[op->r.rs].value & BIT(31))
1070 ^ (op->r.rt == OP_REGIMM_BGEZ)) {
1071 pr_debug("Found always-taken BLTZ/BGEZ\n");
1076 pr_debug("Found never-taken BLTZ/BGEZ\n");
1078 if (!op_flag_no_ds(op->flags))
1079 maybe_remove_load_delay(&list[i + 1]);
1081 local = op_flag_local_branch(op->flags);
1086 lightrec_reset_syncs(block);
1089 case OP_REGIMM_BLTZAL:
1090 case OP_REGIMM_BGEZAL:
1091 /* TODO: Detect always-taken and replace with JAL */
1097 case OP_SPECIAL_SRAV:
1098 if ((v[op->r.rs].known & 0x1f) != 0x1f)
1101 pr_debug("Convert SRAV to SRA\n");
1102 op->r.imm = v[op->r.rs].value & 0x1f;
1103 op->r.op = OP_SPECIAL_SRA;
1106 case OP_SPECIAL_SRA:
1107 if (op->r.imm == 0) {
1108 pr_debug("Convert SRA #0 to MOV\n");
1109 op->m.rs = op->r.rt;
1110 op->m.op = OP_META_MOV;
1116 case OP_SPECIAL_SLLV:
1117 if ((v[op->r.rs].known & 0x1f) != 0x1f)
1120 pr_debug("Convert SLLV to SLL\n");
1121 op->r.imm = v[op->r.rs].value & 0x1f;
1122 op->r.op = OP_SPECIAL_SLL;
1125 case OP_SPECIAL_SLL:
1126 if (op->r.imm == 0) {
1127 pr_debug("Convert SLL #0 to MOV\n");
1128 op->m.rs = op->r.rt;
1129 op->m.op = OP_META_MOV;
1133 lightrec_optimize_sll_sra(block->opcode_list, i, v);
1136 case OP_SPECIAL_SRLV:
1137 if ((v[op->r.rs].known & 0x1f) != 0x1f)
1140 pr_debug("Convert SRLV to SRL\n");
1141 op->r.imm = v[op->r.rs].value & 0x1f;
1142 op->r.op = OP_SPECIAL_SRL;
1145 case OP_SPECIAL_SRL:
1146 if (op->r.imm == 0) {
1147 pr_debug("Convert SRL #0 to MOV\n");
1148 op->m.rs = op->r.rt;
1149 op->m.op = OP_META_MOV;
1154 case OP_SPECIAL_MULT:
1155 case OP_SPECIAL_MULTU:
1156 if (is_known(v, op->r.rs) &&
1157 is_power_of_two(v[op->r.rs].value)) {
1159 op->c.i.rs = op->c.i.rt;
1161 } else if (!is_known(v, op->r.rt) ||
1162 !is_power_of_two(v[op->r.rt].value)) {
1166 pr_debug("Multiply by power-of-two: %u\n",
1169 if (op->r.op == OP_SPECIAL_MULT)
1170 op->i.op = OP_META_MULT2;
1172 op->i.op = OP_META_MULTU2;
1174 op->r.op = ctz32(v[op->r.rt].value);
1176 case OP_SPECIAL_NOR:
1177 if (op->r.rs == 0 || op->r.rt == 0) {
1178 pr_debug("Convert NOR $zero to COM\n");
1180 op->m.op = OP_META_COM;
1182 op->m.rs = op->r.rt;
1186 case OP_SPECIAL_ADD:
1187 case OP_SPECIAL_ADDU:
1188 if (op->r.rs == 0) {
1189 pr_debug("Convert OR/ADD $zero to MOV\n");
1190 op->m.rs = op->r.rt;
1191 op->m.op = OP_META_MOV;
1195 case OP_SPECIAL_SUB:
1196 case OP_SPECIAL_SUBU:
1197 if (op->r.rt == 0) {
1198 pr_debug("Convert OR/ADD/SUB $zero to MOV\n");
1199 op->m.op = OP_META_MOV;
1215 static bool lightrec_can_switch_delay_slot(union code op, union code next_op)
1220 case OP_SPECIAL_JALR:
1221 if (opcode_reads_register(next_op, op.r.rd) ||
1222 opcode_writes_register(next_op, op.r.rd))
1226 if (opcode_writes_register(next_op, op.r.rs))
1236 if (opcode_reads_register(next_op, 31) ||
1237 opcode_writes_register(next_op, 31))
1243 if (op.i.rt && opcode_writes_register(next_op, op.i.rt))
1248 if (op.i.rs && opcode_writes_register(next_op, op.i.rs))
1253 case OP_REGIMM_BLTZAL:
1254 case OP_REGIMM_BGEZAL:
1255 if (opcode_reads_register(next_op, 31) ||
1256 opcode_writes_register(next_op, 31))
1259 case OP_REGIMM_BLTZ:
1260 case OP_REGIMM_BGEZ:
1261 if (op.i.rs && opcode_writes_register(next_op, op.i.rs))
1273 static int lightrec_switch_delay_slots(struct lightrec_state *state, struct block *block)
1275 struct opcode *list, *next = &block->opcode_list[0];
1277 union code op, next_op;
1280 for (i = 0; i < block->nb_ops - 1; i++) {
1282 next = &block->opcode_list[i + 1];
1286 if (!has_delay_slot(op) || op_flag_no_ds(list->flags) ||
1287 op_flag_emulate_branch(list->flags) ||
1288 op.opcode == 0 || next_op.opcode == 0)
1291 if (is_delay_slot(block->opcode_list, i))
1294 if (op_flag_sync(next->flags))
1297 if (op_flag_load_delay(next->flags) && opcode_is_load(next_op))
1300 if (!lightrec_can_switch_delay_slot(list->c, next_op))
1303 pr_debug("Swap branch and delay slot opcodes "
1304 "at offsets 0x%x / 0x%x\n",
1305 i << 2, (i + 1) << 2);
1307 flags = next->flags | (list->flags & LIGHTREC_SYNC);
1310 next->flags = (list->flags | LIGHTREC_NO_DS) & ~LIGHTREC_SYNC;
1311 list->flags = flags | LIGHTREC_NO_DS;
1317 static int lightrec_detect_impossible_branches(struct lightrec_state *state,
1318 struct block *block)
1320 struct opcode *op, *list = block->opcode_list, *next = &list[0];
1324 for (i = 0; i < block->nb_ops - 1; i++) {
1326 next = &list[i + 1];
1328 if (!has_delay_slot(op->c) ||
1329 (!has_delay_slot(next->c) &&
1330 !opcode_is_mfc(next->c) &&
1331 !(next->i.op == OP_CP0 && next->r.rs == OP_CP0_RFE)))
1334 if (op->c.opcode == next->c.opcode) {
1335 /* The delay slot is the exact same opcode as the branch
1336 * opcode: this is effectively a NOP */
1341 op->flags |= LIGHTREC_EMULATE_BRANCH;
1343 if (OPT_LOCAL_BRANCHES && i + 2 < block->nb_ops) {
1344 /* The interpreter will only emulate the branch, then
1345 * return to the compiled code. Add a SYNC after the
1346 * branch + delay slot in the case where the branch
1348 list[i + 2].flags |= LIGHTREC_SYNC;
1355 static bool is_local_branch(const struct block *block, unsigned int idx)
1357 const struct opcode *op = &block->opcode_list[idx];
1360 switch (op->c.i.op) {
1366 offset = idx + 1 + (s16)op->c.i.imm;
1367 if (offset >= 0 && offset < block->nb_ops)
1375 static int lightrec_handle_load_delays(struct lightrec_state *state,
1376 struct block *block)
1378 struct opcode *op, *list = block->opcode_list;
1382 for (i = 0; i < block->nb_ops; i++) {
1385 if (!opcode_is_load(op->c) || !op->c.i.rt || op->c.i.op == OP_LWC2)
1388 if (!is_delay_slot(list, i)) {
1389 /* Only handle load delays in delay slots.
1390 * PSX games never abused load delay slots otherwise. */
1394 if (is_local_branch(block, i - 1)) {
1395 imm = (s16)list[i - 1].c.i.imm;
1397 if (!opcode_reads_register(list[i + imm].c, op->c.i.rt)) {
1398 /* The target opcode of the branch is inside
1399 * the block, and it does not read the register
1400 * written to by the load opcode; we can ignore
1401 * the load delay. */
1406 op->flags |= LIGHTREC_LOAD_DELAY;
1412 static int lightrec_swap_load_delays(struct lightrec_state *state,
1413 struct block *block)
1417 bool in_ds = false, skip_next = false;
1420 if (block->nb_ops < 2)
1423 for (i = 0; i < block->nb_ops - 2; i++) {
1424 c = block->opcode_list[i].c;
1428 } else if (!in_ds && opcode_is_load(c) && c.i.op != OP_LWC2) {
1429 next = block->opcode_list[i + 1].c;
1431 switch (next.i.op) {
1442 if (opcode_reads_register(next, c.i.rt)
1443 && !opcode_writes_register(next, c.i.rs)) {
1444 pr_debug("Swapping opcodes at offset 0x%x to "
1445 "respect load delay\n", i << 2);
1447 op = block->opcode_list[i];
1448 block->opcode_list[i] = block->opcode_list[i + 1];
1449 block->opcode_list[i + 1] = op;
1454 in_ds = has_delay_slot(c);
1460 static int lightrec_local_branches(struct lightrec_state *state, struct block *block)
1462 const struct opcode *ds;
1463 struct opcode *list;
1467 for (i = 0; i < block->nb_ops; i++) {
1468 list = &block->opcode_list[i];
1470 if (should_emulate(list) || !is_local_branch(block, i))
1473 offset = i + 1 + (s16)list->c.i.imm;
1475 pr_debug("Found local branch to offset 0x%x\n", offset << 2);
1477 ds = get_delay_slot(block->opcode_list, i);
1478 if (op_flag_load_delay(ds->flags) && opcode_is_load(ds->c)) {
1479 pr_debug("Branch delay slot has a load delay - skip\n");
1483 if (should_emulate(&block->opcode_list[offset])) {
1484 pr_debug("Branch target must be emulated - skip\n");
1488 if (offset && has_delay_slot(block->opcode_list[offset - 1].c)) {
1489 pr_debug("Branch target is a delay slot - skip\n");
1493 list->flags |= LIGHTREC_LOCAL_BRANCH;
1496 lightrec_reset_syncs(block);
1501 bool has_delay_slot(union code op)
1507 case OP_SPECIAL_JALR:
1525 bool is_delay_slot(const struct opcode *list, unsigned int offset)
1528 && !op_flag_no_ds(list[offset - 1].flags)
1529 && has_delay_slot(list[offset - 1].c);
1532 bool should_emulate(const struct opcode *list)
1534 return op_flag_emulate_branch(list->flags) && has_delay_slot(list->c);
1537 static bool op_writes_rd(union code c)
1548 static void lightrec_add_reg_op(struct opcode *op, u8 reg, u32 reg_op)
1550 if (op_writes_rd(op->c) && reg == op->r.rd)
1551 op->flags |= LIGHTREC_REG_RD(reg_op);
1552 else if (op->i.rs == reg)
1553 op->flags |= LIGHTREC_REG_RS(reg_op);
1554 else if (op->i.rt == reg)
1555 op->flags |= LIGHTREC_REG_RT(reg_op);
1557 pr_debug("Cannot add unload/clean/discard flag: "
1558 "opcode does not touch register %s!\n",
1559 lightrec_reg_name(reg));
1562 static void lightrec_add_unload(struct opcode *op, u8 reg)
1564 lightrec_add_reg_op(op, reg, LIGHTREC_REG_UNLOAD);
1567 static void lightrec_add_discard(struct opcode *op, u8 reg)
1569 lightrec_add_reg_op(op, reg, LIGHTREC_REG_DISCARD);
1572 static void lightrec_add_clean(struct opcode *op, u8 reg)
1574 lightrec_add_reg_op(op, reg, LIGHTREC_REG_CLEAN);
1578 lightrec_early_unload_sync(struct opcode *list, s16 *last_r, s16 *last_w)
1583 for (reg = 0; reg < 34; reg++) {
1584 offset = s16_max(last_w[reg], last_r[reg]);
1587 lightrec_add_unload(&list[offset], reg);
1590 memset(last_r, 0xff, sizeof(*last_r) * 34);
1591 memset(last_w, 0xff, sizeof(*last_w) * 34);
1594 static int lightrec_early_unload(struct lightrec_state *state, struct block *block)
1598 s16 last_r[34], last_w[34], last_sync = 0, next_sync = 0;
1599 u64 mask_r, mask_w, dirty = 0, loaded = 0;
1600 u8 reg, load_delay_reg = 0;
1602 memset(last_r, 0xff, sizeof(last_r));
1603 memset(last_w, 0xff, sizeof(last_w));
1607 * - the register is dirty, and is read again after a branch opcode
1610 * - the register is dirty or loaded, and is not read again
1611 * - the register is dirty or loaded, and is written again after a branch opcode
1612 * - the next opcode has the SYNC flag set
1615 * - the register is dirty or loaded, and is written again
1618 for (i = 0; i < block->nb_ops; i++) {
1619 op = &block->opcode_list[i];
1621 if (OPT_HANDLE_LOAD_DELAYS && load_delay_reg) {
1622 /* Handle delayed register write from load opcodes in
1624 last_w[load_delay_reg] = i;
1628 if (op_flag_sync(op->flags) || should_emulate(op)) {
1629 /* The next opcode has the SYNC flag set, or is a branch
1630 * that should be emulated: unload all registers. */
1631 lightrec_early_unload_sync(block->opcode_list, last_r, last_w);
1636 if (next_sync == i) {
1638 pr_debug("Last sync: 0x%x\n", last_sync << 2);
1641 if (has_delay_slot(op->c)) {
1642 next_sync = i + 1 + !op_flag_no_ds(op->flags);
1643 pr_debug("Next sync: 0x%x\n", next_sync << 2);
1646 mask_r = opcode_read_mask(op->c);
1647 mask_w = opcode_write_mask(op->c);
1649 if (op_flag_load_delay(op->flags) && opcode_is_load(op->c)) {
1650 /* If we have a load opcode in a delay slot, its target
1651 * register is actually not written there but at a
1652 * later point, in the dispatcher. Prevent the algorithm
1653 * from discarding its previous value. */
1654 load_delay_reg = op->c.i.rt;
1655 mask_w &= ~BIT(op->c.i.rt);
1658 for (reg = 0; reg < 34; reg++) {
1659 if (mask_r & BIT(reg)) {
1660 if (dirty & BIT(reg) && last_w[reg] < last_sync) {
1661 /* The register is dirty, and is read
1662 * again after a branch: clean it */
1664 lightrec_add_clean(&block->opcode_list[last_w[reg]], reg);
1672 if (mask_w & BIT(reg)) {
1673 if ((dirty & BIT(reg) && last_w[reg] < last_sync) ||
1674 (loaded & BIT(reg) && last_r[reg] < last_sync)) {
1675 /* The register is dirty or loaded, and
1676 * is written again after a branch:
1679 offset = s16_max(last_w[reg], last_r[reg]);
1680 lightrec_add_unload(&block->opcode_list[offset], reg);
1682 loaded &= ~BIT(reg);
1683 } else if (!(mask_r & BIT(reg)) &&
1684 ((dirty & BIT(reg) && last_w[reg] > last_sync) ||
1685 (loaded & BIT(reg) && last_r[reg] > last_sync))) {
1686 /* The register is dirty or loaded, and
1687 * is written again: discard it */
1689 offset = s16_max(last_w[reg], last_r[reg]);
1690 lightrec_add_discard(&block->opcode_list[offset], reg);
1692 loaded &= ~BIT(reg);
1704 /* Unload all registers that are dirty or loaded at the end of block. */
1705 lightrec_early_unload_sync(block->opcode_list, last_r, last_w);
1710 static int lightrec_flag_io(struct lightrec_state *state, struct block *block)
1712 struct opcode *list;
1713 enum psx_map psx_map;
1714 struct constprop_data v[32] = LIGHTREC_CONSTPROP_INITIALIZER;
1716 u32 val, kunseg_val;
1719 for (i = 0; i < block->nb_ops; i++) {
1720 list = &block->opcode_list[i];
1722 lightrec_consts_propagate(block, i, v);
1724 switch (list->i.op) {
1728 /* Mark all store operations that target $sp or $gp
1729 * as not requiring code invalidation. This is based
1730 * on the heuristic that stores using one of these
1731 * registers as address will never hit a code page. */
1732 if (list->i.rs >= 28 && list->i.rs <= 29 &&
1733 !state->maps[PSX_MAP_KERNEL_USER_RAM].ops) {
1734 pr_debug("Flaging opcode 0x%08x as not requiring invalidation\n",
1736 list->flags |= LIGHTREC_NO_INVALIDATE;
1739 /* Detect writes whose destination address is inside the
1740 * current block, using constant propagation. When these
1741 * occur, we mark the blocks as not compilable. */
1742 if (is_known(v, list->i.rs) &&
1743 kunseg(v[list->i.rs].value) >= kunseg(block->pc) &&
1744 kunseg(v[list->i.rs].value) < (kunseg(block->pc) + block->nb_ops * 4)) {
1745 pr_debug("Self-modifying block detected\n");
1746 block_set_flags(block, BLOCK_NEVER_COMPILE);
1747 list->flags |= LIGHTREC_SMC;
1761 if (v[list->i.rs].known | v[list->i.rs].sign) {
1762 psx_map = lightrec_get_constprop_map(state, v,
1766 if (psx_map != PSX_MAP_UNKNOWN && !is_known(v, list->i.rs))
1767 pr_debug("Detected map thanks to bit-level const propagation!\n");
1769 list->flags &= ~LIGHTREC_IO_MASK;
1771 val = v[list->i.rs].value + (s16) list->i.imm;
1772 kunseg_val = kunseg(val);
1774 no_mask = (v[list->i.rs].known & ~v[list->i.rs].value
1775 & 0xe0000000) == 0xe0000000;
1778 case PSX_MAP_KERNEL_USER_RAM:
1780 list->flags |= LIGHTREC_NO_MASK;
1782 case PSX_MAP_MIRROR1:
1783 case PSX_MAP_MIRROR2:
1784 case PSX_MAP_MIRROR3:
1785 pr_debug("Flaging opcode %u as RAM access\n", i);
1786 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_RAM);
1787 if (no_mask && state->mirrors_mapped)
1788 list->flags |= LIGHTREC_NO_MASK;
1791 pr_debug("Flaging opcode %u as BIOS access\n", i);
1792 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_BIOS);
1794 list->flags |= LIGHTREC_NO_MASK;
1796 case PSX_MAP_SCRATCH_PAD:
1797 pr_debug("Flaging opcode %u as scratchpad access\n", i);
1798 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_SCRATCH);
1800 list->flags |= LIGHTREC_NO_MASK;
1802 /* Consider that we're never going to run code from
1803 * the scratchpad. */
1804 list->flags |= LIGHTREC_NO_INVALIDATE;
1806 case PSX_MAP_HW_REGISTERS:
1807 if (state->ops.hw_direct &&
1808 state->ops.hw_direct(kunseg_val,
1809 opcode_is_store(list->c),
1810 opcode_get_io_size(list->c))) {
1811 pr_debug("Flagging opcode %u as direct I/O access\n",
1813 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_DIRECT_HW);
1816 list->flags |= LIGHTREC_NO_MASK;
1818 pr_debug("Flagging opcode %u as I/O access\n",
1820 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
1828 if (!LIGHTREC_FLAGS_GET_IO_MODE(list->flags)
1829 && list->i.rs >= 28 && list->i.rs <= 29
1830 && !state->maps[PSX_MAP_KERNEL_USER_RAM].ops) {
1831 /* Assume that all I/O operations that target
1832 * $sp or $gp will always only target a mapped
1833 * memory (RAM, BIOS, scratchpad). */
1834 if (state->opt_flags & LIGHTREC_OPT_SP_GP_HIT_RAM)
1835 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_RAM);
1837 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_DIRECT);
1849 static u8 get_mfhi_mflo_reg(const struct block *block, u16 offset,
1850 const struct opcode *last,
1851 u32 mask, bool sync, bool mflo, bool another)
1853 const struct opcode *op, *next = &block->opcode_list[offset];
1855 u8 reg2, reg = mflo ? REG_LO : REG_HI;
1859 for (i = offset; i < block->nb_ops; i++) {
1861 next = &block->opcode_list[i + 1];
1864 /* If any other opcode writes or reads to the register
1865 * we'd use, then we cannot use it anymore. */
1866 mask |= opcode_read_mask(op->c);
1867 mask |= opcode_write_mask(op->c);
1869 if (op_flag_sync(op->flags))
1878 /* TODO: handle backwards branches too */
1879 if (!last && op_flag_local_branch(op->flags) &&
1880 (s16)op->c.i.imm >= 0) {
1881 branch_offset = i + 1 + (s16)op->c.i.imm
1882 - !!op_flag_no_ds(op->flags);
1884 reg = get_mfhi_mflo_reg(block, branch_offset, NULL,
1885 mask, sync, mflo, false);
1886 reg2 = get_mfhi_mflo_reg(block, offset + 1, next,
1887 mask, sync, mflo, false);
1888 if (reg > 0 && reg == reg2)
1894 return mflo ? REG_LO : REG_HI;
1896 case OP_META_MULTU2:
1900 case OP_SPECIAL_MULT:
1901 case OP_SPECIAL_MULTU:
1902 case OP_SPECIAL_DIV:
1903 case OP_SPECIAL_DIVU:
1905 case OP_SPECIAL_MTHI:
1909 case OP_SPECIAL_MTLO:
1917 if (!sync && !op_flag_no_ds(op->flags) &&
1918 (next->i.op == OP_SPECIAL) &&
1919 ((!mflo && next->r.op == OP_SPECIAL_MFHI) ||
1920 (mflo && next->r.op == OP_SPECIAL_MFLO)))
1924 case OP_SPECIAL_JALR:
1926 case OP_SPECIAL_MFHI:
1930 /* Must use REG_HI if there is another MFHI target*/
1931 reg2 = get_mfhi_mflo_reg(block, i + 1, next,
1932 0, sync, mflo, true);
1933 if (reg2 > 0 && reg2 != REG_HI)
1936 if (!sync && !(old_mask & BIT(op->r.rd)))
1942 case OP_SPECIAL_MFLO:
1946 /* Must use REG_LO if there is another MFLO target*/
1947 reg2 = get_mfhi_mflo_reg(block, i + 1, next,
1948 0, sync, mflo, true);
1949 if (reg2 > 0 && reg2 != REG_LO)
1952 if (!sync && !(old_mask & BIT(op->r.rd)))
1971 static void lightrec_replace_lo_hi(struct block *block, u16 offset,
1977 /* This function will remove the following MFLO/MFHI. It must be called
1978 * only if get_mfhi_mflo_reg() returned a non-zero value. */
1980 for (i = offset; i < last; i++) {
1981 struct opcode *op = &block->opcode_list[i];
1989 /* TODO: handle backwards branches too */
1990 if (op_flag_local_branch(op->flags) && (s16)op->c.i.imm >= 0) {
1991 branch_offset = i + 1 + (s16)op->c.i.imm
1992 - !!op_flag_no_ds(op->flags);
1994 lightrec_replace_lo_hi(block, branch_offset, last, lo);
1995 lightrec_replace_lo_hi(block, i + 1, branch_offset, lo);
2000 if (lo && op->r.op == OP_SPECIAL_MFLO) {
2001 pr_debug("Removing MFLO opcode at offset 0x%x\n",
2005 } else if (!lo && op->r.op == OP_SPECIAL_MFHI) {
2006 pr_debug("Removing MFHI opcode at offset 0x%x\n",
2019 static bool lightrec_always_skip_div_check(void)
2028 static int lightrec_flag_mults_divs(struct lightrec_state *state, struct block *block)
2030 struct opcode *list = NULL;
2031 struct constprop_data v[32] = LIGHTREC_CONSTPROP_INITIALIZER;
2035 for (i = 0; i < block->nb_ops - 1; i++) {
2036 list = &block->opcode_list[i];
2038 lightrec_consts_propagate(block, i, v);
2040 switch (list->i.op) {
2042 switch (list->r.op) {
2043 case OP_SPECIAL_DIV:
2044 case OP_SPECIAL_DIVU:
2045 /* If we are dividing by a non-zero constant, don't
2046 * emit the div-by-zero check. */
2047 if (lightrec_always_skip_div_check() ||
2048 (v[list->r.rt].known & v[list->r.rt].value)) {
2049 list->flags |= LIGHTREC_NO_DIV_CHECK;
2052 case OP_SPECIAL_MULT:
2053 case OP_SPECIAL_MULTU:
2060 case OP_META_MULTU2:
2066 /* Don't support opcodes in delay slots */
2067 if (is_delay_slot(block->opcode_list, i) ||
2068 op_flag_no_ds(list->flags)) {
2072 reg_lo = get_mfhi_mflo_reg(block, i + 1, NULL, 0, false, true, false);
2074 pr_debug("Mark MULT(U)/DIV(U) opcode at offset 0x%x as"
2075 " not writing LO\n", i << 2);
2076 list->flags |= LIGHTREC_NO_LO;
2079 reg_hi = get_mfhi_mflo_reg(block, i + 1, NULL, 0, false, false, false);
2081 pr_debug("Mark MULT(U)/DIV(U) opcode at offset 0x%x as"
2082 " not writing HI\n", i << 2);
2083 list->flags |= LIGHTREC_NO_HI;
2086 if (!reg_lo && !reg_hi) {
2087 pr_debug("Both LO/HI unused in this block, they will "
2088 "probably be used in parent block - removing "
2090 list->flags &= ~(LIGHTREC_NO_LO | LIGHTREC_NO_HI);
2093 if (reg_lo > 0 && reg_lo != REG_LO) {
2094 pr_debug("Found register %s to hold LO (rs = %u, rt = %u)\n",
2095 lightrec_reg_name(reg_lo), list->r.rs, list->r.rt);
2097 lightrec_replace_lo_hi(block, i + 1, block->nb_ops, true);
2098 list->r.rd = reg_lo;
2103 if (reg_hi > 0 && reg_hi != REG_HI) {
2104 pr_debug("Found register %s to hold HI (rs = %u, rt = %u)\n",
2105 lightrec_reg_name(reg_hi), list->r.rs, list->r.rt);
2107 lightrec_replace_lo_hi(block, i + 1, block->nb_ops, false);
2108 list->r.imm = reg_hi;
2117 static bool remove_div_sequence(struct block *block, unsigned int offset)
2120 unsigned int i, found = 0;
2123 * Scan for the zero-checking sequence that GCC automatically introduced
2124 * after most DIV/DIVU opcodes. This sequence checks the value of the
2125 * divisor, and if zero, executes a BREAK opcode, causing the BIOS
2126 * handler to crash the PS1.
2128 * For DIV opcodes, this sequence additionally checks that the signed
2129 * operation does not overflow.
2131 * With the assumption that the games never crashed the PS1, we can
2132 * therefore assume that the games never divided by zero or overflowed,
2133 * and these sequences can be removed.
2136 for (i = offset; i < block->nb_ops; i++) {
2137 op = &block->opcode_list[i];
2140 if (op->i.op == OP_SPECIAL &&
2141 (op->r.op == OP_SPECIAL_DIV || op->r.op == OP_SPECIAL_DIVU))
2144 if ((op->opcode & 0xfc1fffff) == 0x14000002) {
2145 /* BNE ???, zero, +8 */
2150 } else if (found == 1 && !op->opcode) {
2153 } else if (found == 2 && op->opcode == 0x0007000d) {
2156 } else if (found == 3 && op->opcode == 0x2401ffff) {
2159 } else if (found == 4 && (op->opcode & 0xfc1fffff) == 0x14010004) {
2160 /* BNE ???, at, +16 */
2162 } else if (found == 5 && op->opcode == 0x3c018000) {
2163 /* LUI at, 0x8000 */
2165 } else if (found == 6 && (op->opcode & 0x141fffff) == 0x14010002) {
2166 /* BNE ???, at, +16 */
2168 } else if (found == 7 && !op->opcode) {
2171 } else if (found == 8 && op->opcode == 0x0006000d) {
2184 pr_debug("Removing DIV%s sequence at offset 0x%x\n",
2185 found == 9 ? "" : "U", offset << 2);
2187 for (i = 0; i < found; i++)
2188 block->opcode_list[offset + i].opcode = 0;
2196 static int lightrec_remove_div_by_zero_check_sequence(struct lightrec_state *state,
2197 struct block *block)
2202 for (i = 0; i < block->nb_ops; i++) {
2203 op = &block->opcode_list[i];
2205 if (op->i.op == OP_SPECIAL &&
2206 (op->r.op == OP_SPECIAL_DIVU || op->r.op == OP_SPECIAL_DIV) &&
2207 remove_div_sequence(block, i + 1))
2208 op->flags |= LIGHTREC_NO_DIV_CHECK;
2214 static const u32 memset_code[] = {
2215 0x10a00006, // beqz a1, 2f
2216 0x24a2ffff, // addiu v0,a1,-1
2217 0x2403ffff, // li v1,-1
2218 0xac800000, // 1: sw zero,0(a0)
2219 0x2442ffff, // addiu v0,v0,-1
2220 0x1443fffd, // bne v0,v1, 1b
2221 0x24840004, // addiu a0,a0,4
2222 0x03e00008, // 2: jr ra
2226 static int lightrec_replace_memset(struct lightrec_state *state, struct block *block)
2231 for (i = 0; i < block->nb_ops; i++) {
2232 c = block->opcode_list[i].c;
2234 if (c.opcode != memset_code[i])
2237 if (i == ARRAY_SIZE(memset_code) - 1) {
2239 pr_debug("Block at PC 0x%x is a memset\n", block->pc);
2240 block_set_flags(block,
2241 BLOCK_IS_MEMSET | BLOCK_NEVER_COMPILE);
2243 /* Return non-zero to skip other optimizers. */
2251 static int lightrec_test_preload_pc(struct lightrec_state *state, struct block *block)
2257 for (i = 0; i < block->nb_ops; i++) {
2258 c = block->opcode_list[i].c;
2259 flags = block->opcode_list[i].flags;
2261 if (op_flag_sync(flags))
2267 block->flags |= BLOCK_PRELOAD_PC;
2272 case OP_REGIMM_BLTZAL:
2273 case OP_REGIMM_BGEZAL:
2274 block->flags |= BLOCK_PRELOAD_PC;
2284 if (!op_flag_local_branch(flags)) {
2285 block->flags |= BLOCK_PRELOAD_PC;
2291 case OP_SPECIAL_JALR:
2293 block->flags |= BLOCK_PRELOAD_PC;
2297 case OP_SPECIAL_SYSCALL:
2298 case OP_SPECIAL_BREAK:
2299 block->flags |= BLOCK_PRELOAD_PC;
2311 static int (*lightrec_optimizers[])(struct lightrec_state *state, struct block *) = {
2312 IF_OPT(OPT_REMOVE_DIV_BY_ZERO_SEQ, &lightrec_remove_div_by_zero_check_sequence),
2313 IF_OPT(OPT_REPLACE_MEMSET, &lightrec_replace_memset),
2314 IF_OPT(OPT_DETECT_IMPOSSIBLE_BRANCHES, &lightrec_detect_impossible_branches),
2315 IF_OPT(OPT_HANDLE_LOAD_DELAYS, &lightrec_handle_load_delays),
2316 IF_OPT(OPT_HANDLE_LOAD_DELAYS, &lightrec_swap_load_delays),
2317 IF_OPT(OPT_TRANSFORM_OPS, &lightrec_transform_branches),
2318 IF_OPT(OPT_LOCAL_BRANCHES, &lightrec_local_branches),
2319 IF_OPT(OPT_TRANSFORM_OPS, &lightrec_transform_ops),
2320 IF_OPT(OPT_SWITCH_DELAY_SLOTS, &lightrec_switch_delay_slots),
2321 IF_OPT(OPT_FLAG_IO, &lightrec_flag_io),
2322 IF_OPT(OPT_FLAG_MULT_DIV, &lightrec_flag_mults_divs),
2323 IF_OPT(OPT_EARLY_UNLOAD, &lightrec_early_unload),
2324 IF_OPT(OPT_PRELOAD_PC, &lightrec_test_preload_pc),
2327 int lightrec_optimize(struct lightrec_state *state, struct block *block)
2332 for (i = 0; i < ARRAY_SIZE(lightrec_optimizers); i++) {
2333 if (lightrec_optimizers[i]) {
2334 ret = (*lightrec_optimizers[i])(state, block);