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)
117 return BIT(op.i.rs) | BIT(op.i.rt);
125 static u64 mult_div_write_mask(union code op)
129 if (!OPT_FLAG_MULT_DIV)
130 return BIT(REG_LO) | BIT(REG_HI);
133 flags = BIT(op.r.rd);
137 flags |= BIT(op.r.imm);
139 flags |= BIT(REG_HI);
144 u64 opcode_write_mask(union code op)
149 return mult_div_write_mask(op);
155 case OP_SPECIAL_SYSCALL:
156 case OP_SPECIAL_BREAK:
158 case OP_SPECIAL_MULT:
159 case OP_SPECIAL_MULTU:
161 case OP_SPECIAL_DIVU:
162 return mult_div_write_mask(op);
163 case OP_SPECIAL_MTHI:
165 case OP_SPECIAL_MTLO:
201 if (op.r.op == OP_CP2_BASIC) {
203 case OP_CP2_BASIC_MFC2:
204 case OP_CP2_BASIC_CFC2:
213 case OP_REGIMM_BLTZAL:
214 case OP_REGIMM_BGEZAL:
224 bool opcode_reads_register(union code op, u8 reg)
226 return opcode_read_mask(op) & BIT(reg);
229 bool opcode_writes_register(union code op, u8 reg)
231 return opcode_write_mask(op) & BIT(reg);
234 static int find_prev_writer(const struct opcode *list, unsigned int offset, u8 reg)
239 if (op_flag_sync(list[offset].flags))
242 for (i = offset; i > 0; i--) {
245 if (opcode_writes_register(c, reg)) {
246 if (i > 1 && has_delay_slot(list[i - 2].c))
252 if (op_flag_sync(list[i - 1].flags) ||
254 opcode_reads_register(c, reg))
261 static int find_next_reader(const struct opcode *list, unsigned int offset, u8 reg)
266 if (op_flag_sync(list[offset].flags))
269 for (i = offset; ; i++) {
272 if (opcode_reads_register(c, reg))
275 if (op_flag_sync(list[i].flags)
276 || (op_flag_no_ds(list[i].flags) && has_delay_slot(c))
277 || is_delay_slot(list, i)
278 || opcode_writes_register(c, reg))
285 static bool reg_is_dead(const struct opcode *list, unsigned int offset, u8 reg)
289 if (op_flag_sync(list[offset].flags) || is_delay_slot(list, offset))
292 for (i = offset + 1; ; i++) {
293 if (opcode_reads_register(list[i].c, reg))
296 if (opcode_writes_register(list[i].c, reg))
299 if (has_delay_slot(list[i].c)) {
300 if (op_flag_no_ds(list[i].flags) ||
301 opcode_reads_register(list[i + 1].c, reg))
304 return opcode_writes_register(list[i + 1].c, reg);
309 static bool reg_is_read(const struct opcode *list,
310 unsigned int a, unsigned int b, u8 reg)
312 /* Return true if reg is read in one of the opcodes of the interval
315 if (!is_nop(list[a].c) && opcode_reads_register(list[a].c, reg))
322 static bool reg_is_written(const struct opcode *list,
323 unsigned int a, unsigned int b, u8 reg)
325 /* Return true if reg is written in one of the opcodes of the interval
329 if (!is_nop(list[a].c) && opcode_writes_register(list[a].c, reg))
336 static bool reg_is_read_or_written(const struct opcode *list,
337 unsigned int a, unsigned int b, u8 reg)
339 return reg_is_read(list, a, b, reg) || reg_is_written(list, a, b, reg);
342 bool opcode_is_mfc(union code op)
356 if (op.r.op == OP_CP2_BASIC) {
358 case OP_CP2_BASIC_MFC2:
359 case OP_CP2_BASIC_CFC2:
374 bool opcode_is_load(union code op)
391 static bool opcode_is_store(union code op)
406 static u8 opcode_get_io_size(union code op)
422 bool opcode_is_io(union code op)
424 return opcode_is_load(op) || opcode_is_store(op);
428 static bool is_nop(union code op)
430 if (opcode_writes_register(op, 0)) {
433 return op.r.rs != OP_CP0_MFC0;
451 return op.r.rd == op.r.rt && op.r.rd == op.r.rs;
453 case OP_SPECIAL_ADDU:
454 return (op.r.rd == op.r.rt && op.r.rs == 0) ||
455 (op.r.rd == op.r.rs && op.r.rt == 0);
457 case OP_SPECIAL_SUBU:
458 return op.r.rd == op.r.rs && op.r.rt == 0;
460 if (op.r.rd == op.r.rt)
461 return op.r.rd == op.r.rs || op.r.rs == 0;
463 return (op.r.rd == op.r.rs) && op.r.rt == 0;
467 return op.r.rd == op.r.rt && op.r.imm == 0;
468 case OP_SPECIAL_MFHI:
469 case OP_SPECIAL_MFLO:
477 return op.i.rt == op.i.rs && op.i.imm == 0;
479 return (op.i.rs == 0 || op.i.imm == 1);
481 return (op.i.op == OP_REGIMM_BLTZ ||
482 op.i.op == OP_REGIMM_BLTZAL) &&
483 (op.i.rs == 0 || op.i.imm == 1);
485 return (op.i.rs == op.i.rt || op.i.imm == 1);
491 static void lightrec_optimize_sll_sra(struct opcode *list, unsigned int offset,
492 struct constprop_data *v)
494 struct opcode *ldop = NULL, *curr = &list[offset], *next;
495 struct opcode *to_change, *to_nop;
498 if (curr->r.imm != 24 && curr->r.imm != 16)
501 if (is_delay_slot(list, offset))
504 idx = find_next_reader(list, offset + 1, curr->r.rd);
510 if (next->i.op != OP_SPECIAL || next->r.op != OP_SPECIAL_SRA ||
511 next->r.imm != curr->r.imm || next->r.rt != curr->r.rd)
514 if (curr->r.rd != curr->r.rt && next->r.rd != next->r.rt) {
519 if (!reg_is_dead(list, idx, curr->r.rd) ||
520 reg_is_read_or_written(list, offset, idx, next->r.rd))
523 /* If rY is dead after the SRL, and rZ is not used after the SLL,
524 * we can change rY to rZ */
526 pr_debug("Detected SLL/SRA with middle temp register\n");
527 curr->r.rd = next->r.rd;
528 next->r.rt = curr->r.rd;
531 /* We got a SLL/SRA combo. If imm #16, that's a cast to s16.
532 * If imm #24 that's a cast to s8.
534 * First of all, make sure that the target register of the SLL is not
535 * read after the SRA. */
537 if (curr->r.rd == curr->r.rt) {
544 /* rX is used after the SRA - we cannot convert it. */
545 if (curr->r.rd != next->r.rd && !reg_is_dead(list, idx, curr->r.rd))
555 idx2 = find_prev_writer(list, offset, curr->r.rt);
557 /* Note that PSX games sometimes do casts after
558 * a LHU or LBU; in this case we can change the
559 * load opcode to a LH or LB, and the cast can
560 * be changed to a MOV or a simple NOP. */
564 if (next->r.rd != ldop->i.rt &&
565 !reg_is_dead(list, idx, ldop->i.rt))
567 else if (curr->r.imm == 16 && ldop->i.op == OP_LHU)
569 else if (curr->r.imm == 24 && ldop->i.op == OP_LBU)
575 if (next->r.rd == ldop->i.rt) {
576 to_change->opcode = 0;
577 } else if (reg_is_dead(list, idx, ldop->i.rt) &&
578 !reg_is_read_or_written(list, idx2 + 1, idx, next->r.rd)) {
579 /* The target register of the SRA is dead after the
580 * LBU/LHU; we can change the target register of the
581 * LBU/LHU to the one of the SRA. */
582 v[ldop->i.rt].known = 0;
583 v[ldop->i.rt].sign = 0;
584 ldop->i.rt = next->r.rd;
585 to_change->opcode = 0;
587 to_change->i.op = OP_META;
588 to_change->m.op = OP_META_MOV;
589 to_change->m.rd = next->r.rd;
590 to_change->m.rs = ldop->i.rt;
593 if (to_nop->r.imm == 24)
594 pr_debug("Convert LBU+SLL+SRA to LB\n");
596 pr_debug("Convert LHU+SLL+SRA to LH\n");
598 v[ldop->i.rt].known = 0;
599 v[ldop->i.rt].sign = 0xffffff80 << (24 - curr->r.imm);
604 pr_debug("Convert SLL/SRA #%u to EXT%c\n",
605 curr->r.imm, curr->r.imm == 24 ? 'C' : 'S');
607 to_change->m.rs = curr->r.rt;
608 to_change->m.op = to_nop->r.imm == 24 ? OP_META_EXTC : OP_META_EXTS;
609 to_change->i.op = OP_META;
616 lightrec_remove_useless_lui(struct block *block, unsigned int offset,
617 const struct constprop_data *v)
619 struct opcode *list = block->opcode_list,
620 *op = &block->opcode_list[offset];
623 if (!op_flag_sync(op->flags) && is_known(v, op->i.rt) &&
624 v[op->i.rt].value == op->i.imm << 16) {
625 pr_debug("Converting duplicated LUI to NOP\n");
630 if (op->i.imm != 0 || op->i.rt == 0 || offset == block->nb_ops - 1)
633 reader = find_next_reader(list, offset + 1, op->i.rt);
637 if (opcode_writes_register(list[reader].c, op->i.rt) ||
638 reg_is_dead(list, reader, op->i.rt)) {
639 pr_debug("Removing useless LUI 0x0\n");
641 if (list[reader].i.rs == op->i.rt)
642 list[reader].i.rs = 0;
643 if (list[reader].i.op == OP_SPECIAL &&
644 list[reader].i.rt == op->i.rt)
645 list[reader].i.rt = 0;
650 static void lightrec_lui_to_movi(struct block *block, unsigned int offset)
652 struct opcode *ori, *lui = &block->opcode_list[offset];
655 if (lui->i.op != OP_LUI)
658 next = find_next_reader(block->opcode_list, offset + 1, lui->i.rt);
660 ori = &block->opcode_list[next];
666 if (ori->i.rs == ori->i.rt && ori->i.imm) {
667 ori->flags |= LIGHTREC_MOVI;
668 lui->flags |= LIGHTREC_MOVI;
675 static void lightrec_modify_lui(struct block *block, unsigned int offset)
677 union code c, *lui = &block->opcode_list[offset].c;
678 bool stop = false, stop_next = false;
681 for (i = offset + 1; !stop && i < block->nb_ops; i++) {
682 c = block->opcode_list[i].c;
685 if ((opcode_is_store(c) && c.i.rt == lui->i.rt)
686 || (!opcode_is_load(c) && opcode_reads_register(c, lui->i.rt)))
689 if (opcode_writes_register(c, lui->i.rt)) {
690 if (c.i.op == OP_LWL || c.i.op == OP_LWR) {
691 /* LWL/LWR only partially write their target register;
692 * therefore the LUI should not write a different value. */
696 pr_debug("Convert LUI at offset 0x%x to kuseg\n",
698 lui->i.imm = kunseg(lui->i.imm << 16) >> 16;
702 if (has_delay_slot(c))
707 static int lightrec_transform_branches(struct lightrec_state *state,
714 for (i = 0; i < block->nb_ops; i++) {
715 op = &block->opcode_list[i];
719 /* Transform J opcode into BEQ $zero, $zero if possible. */
720 offset = (s32)((block->pc & 0xf0000000) >> 2 | op->j.imm)
721 - (s32)(block->pc >> 2) - (s32)i - 1;
723 if (offset == (s16)offset) {
724 pr_debug("Transform J into BEQ $zero, $zero\n");
740 static inline bool is_power_of_two(u32 value)
742 return popcount32(value) == 1;
745 static void lightrec_patch_known_zero(struct opcode *op,
746 const struct constprop_data *v)
752 case OP_SPECIAL_JALR:
753 case OP_SPECIAL_MTHI:
754 case OP_SPECIAL_MTLO:
755 if (is_known_zero(v, op->r.rs))
759 if (is_known_zero(v, op->r.rs))
765 if (is_known_zero(v, op->r.rt))
768 case OP_SPECIAL_SYSCALL:
769 case OP_SPECIAL_BREAK:
770 case OP_SPECIAL_MFHI:
771 case OP_SPECIAL_MFLO:
779 if (is_known_zero(v, op->r.rt))
787 if (op->r.op == OP_CP2_BASIC) {
789 case OP_CP2_BASIC_MTC2:
790 case OP_CP2_BASIC_CTC2:
791 if (is_known_zero(v, op->r.rt))
801 if (is_known_zero(v, op->i.rt))
817 if (is_known_zero(v, op->m.rs))
825 if (is_known_zero(v, op->i.rt))
837 if (is_known(v, op->i.rs)
838 && kunseg(v[op->i.rs].value) == 0)
846 static void lightrec_reset_syncs(struct block *block)
848 struct opcode *op, *list = block->opcode_list;
852 for (i = 0; i < block->nb_ops; i++)
853 list[i].flags &= ~LIGHTREC_SYNC;
855 for (i = 0; i < block->nb_ops; i++) {
858 if (has_delay_slot(op->c)) {
859 if (op_flag_local_branch(op->flags)) {
860 offset = i + 1 - op_flag_no_ds(op->flags) + (s16)op->i.imm;
861 list[offset].flags |= LIGHTREC_SYNC;
864 if (op_flag_emulate_branch(op->flags) && i + 2 < block->nb_ops)
865 list[i + 2].flags |= LIGHTREC_SYNC;
870 static void maybe_remove_load_delay(struct opcode *op)
872 if (op_flag_load_delay(op->flags) && opcode_is_load(op->c))
873 op->flags &= ~LIGHTREC_LOAD_DELAY;
876 static int lightrec_transform_ops(struct lightrec_state *state, struct block *block)
878 struct opcode *op, *list = block->opcode_list;
879 struct constprop_data v[32] = LIGHTREC_CONSTPROP_INITIALIZER;
884 for (i = 0; i < block->nb_ops; i++) {
887 lightrec_consts_propagate(block, i, v);
889 lightrec_patch_known_zero(op, v);
891 /* Transform all opcodes detected as useless to real NOPs
892 * (0x0: SLL r0, r0, #0) */
893 if (op->opcode != 0 && is_nop(op->c)) {
894 pr_debug("Converting useless opcode 0x%08x to NOP\n",
904 if (op->i.rs == op->i.rt ||
905 (is_known(v, op->i.rs) && is_known(v, op->i.rt) &&
906 v[op->i.rs].value == v[op->i.rt].value)) {
907 if (op->i.rs != op->i.rt)
908 pr_debug("Found always-taken BEQ\n");
912 } else if (v[op->i.rs].known & v[op->i.rt].known &
913 (v[op->i.rs].value ^ v[op->i.rt].value)) {
914 pr_debug("Found never-taken BEQ\n");
916 if (!op_flag_no_ds(op->flags))
917 maybe_remove_load_delay(&list[i + 1]);
919 local = op_flag_local_branch(op->flags);
924 lightrec_reset_syncs(block);
925 } else if (op->i.rs == 0) {
932 if (v[op->i.rs].known & v[op->i.rt].known &
933 (v[op->i.rs].value ^ v[op->i.rt].value)) {
934 pr_debug("Found always-taken BNE\n");
939 } else if (is_known(v, op->i.rs) && is_known(v, op->i.rt) &&
940 v[op->i.rs].value == v[op->i.rt].value) {
941 pr_debug("Found never-taken BNE\n");
943 if (!op_flag_no_ds(op->flags))
944 maybe_remove_load_delay(&list[i + 1]);
946 local = op_flag_local_branch(op->flags);
951 lightrec_reset_syncs(block);
952 } else if (op->i.rs == 0) {
959 if (v[op->i.rs].known & BIT(31) &&
960 v[op->i.rs].value & BIT(31)) {
961 pr_debug("Found always-taken BLEZ\n");
970 if (v[op->i.rs].known & BIT(31) &&
971 v[op->i.rs].value & BIT(31)) {
972 pr_debug("Found never-taken BGTZ\n");
974 if (!op_flag_no_ds(op->flags))
975 maybe_remove_load_delay(&list[i + 1]);
977 local = op_flag_local_branch(op->flags);
982 lightrec_reset_syncs(block);
987 if (i == 0 || !has_delay_slot(list[i - 1].c))
988 lightrec_modify_lui(block, i);
989 lightrec_remove_useless_lui(block, i, v);
990 if (i == 0 || !has_delay_slot(list[i - 1].c))
991 lightrec_lui_to_movi(block, i);
994 /* Transform ORI/ADDI/ADDIU with imm #0 or ORR/ADD/ADDU/SUB/SUBU
995 * with register $zero to the MOV meta-opcode */
999 if (op->i.imm == 0) {
1000 pr_debug("Convert ORI/ADDI/ADDIU #0 to MOV\n");
1001 op->m.rd = op->i.rt;
1002 op->m.op = OP_META_MOV;
1007 if (bits_are_known_zero(v, op->i.rs, ~op->i.imm)) {
1008 pr_debug("Found useless ANDI 0x%x\n", op->i.imm);
1010 if (op->i.rs == op->i.rt) {
1013 op->m.rd = op->i.rt;
1014 op->m.op = OP_META_MOV;
1021 case OP_REGIMM_BLTZ:
1022 case OP_REGIMM_BGEZ:
1023 if (!(v[op->r.rs].known & BIT(31)))
1026 if (!!(v[op->r.rs].value & BIT(31))
1027 ^ (op->r.rt == OP_REGIMM_BGEZ)) {
1028 pr_debug("Found always-taken BLTZ/BGEZ\n");
1033 pr_debug("Found never-taken BLTZ/BGEZ\n");
1035 if (!op_flag_no_ds(op->flags))
1036 maybe_remove_load_delay(&list[i + 1]);
1038 local = op_flag_local_branch(op->flags);
1043 lightrec_reset_syncs(block);
1046 case OP_REGIMM_BLTZAL:
1047 case OP_REGIMM_BGEZAL:
1048 /* TODO: Detect always-taken and replace with JAL */
1054 case OP_SPECIAL_SRAV:
1055 if ((v[op->r.rs].known & 0x1f) != 0x1f)
1058 pr_debug("Convert SRAV to SRA\n");
1059 op->r.imm = v[op->r.rs].value & 0x1f;
1060 op->r.op = OP_SPECIAL_SRA;
1063 case OP_SPECIAL_SRA:
1064 if (op->r.imm == 0) {
1065 pr_debug("Convert SRA #0 to MOV\n");
1066 op->m.rs = op->r.rt;
1067 op->m.op = OP_META_MOV;
1073 case OP_SPECIAL_SLLV:
1074 if ((v[op->r.rs].known & 0x1f) != 0x1f)
1077 pr_debug("Convert SLLV to SLL\n");
1078 op->r.imm = v[op->r.rs].value & 0x1f;
1079 op->r.op = OP_SPECIAL_SLL;
1082 case OP_SPECIAL_SLL:
1083 if (op->r.imm == 0) {
1084 pr_debug("Convert SLL #0 to MOV\n");
1085 op->m.rs = op->r.rt;
1086 op->m.op = OP_META_MOV;
1090 lightrec_optimize_sll_sra(block->opcode_list, i, v);
1093 case OP_SPECIAL_SRLV:
1094 if ((v[op->r.rs].known & 0x1f) != 0x1f)
1097 pr_debug("Convert SRLV to SRL\n");
1098 op->r.imm = v[op->r.rs].value & 0x1f;
1099 op->r.op = OP_SPECIAL_SRL;
1102 case OP_SPECIAL_SRL:
1103 if (op->r.imm == 0) {
1104 pr_debug("Convert SRL #0 to MOV\n");
1105 op->m.rs = op->r.rt;
1106 op->m.op = OP_META_MOV;
1111 case OP_SPECIAL_MULT:
1112 case OP_SPECIAL_MULTU:
1113 if (is_known(v, op->r.rs) &&
1114 is_power_of_two(v[op->r.rs].value)) {
1116 op->c.i.rs = op->c.i.rt;
1118 } else if (!is_known(v, op->r.rt) ||
1119 !is_power_of_two(v[op->r.rt].value)) {
1123 pr_debug("Multiply by power-of-two: %u\n",
1126 if (op->r.op == OP_SPECIAL_MULT)
1127 op->i.op = OP_META_MULT2;
1129 op->i.op = OP_META_MULTU2;
1131 op->r.op = ctz32(v[op->r.rt].value);
1133 case OP_SPECIAL_NOR:
1134 if (op->r.rs == 0 || op->r.rt == 0) {
1135 pr_debug("Convert NOR $zero to COM\n");
1137 op->m.op = OP_META_COM;
1139 op->m.rs = op->r.rt;
1143 case OP_SPECIAL_ADD:
1144 case OP_SPECIAL_ADDU:
1145 if (op->r.rs == 0) {
1146 pr_debug("Convert OR/ADD $zero to MOV\n");
1147 op->m.rs = op->r.rt;
1148 op->m.op = OP_META_MOV;
1152 case OP_SPECIAL_SUB:
1153 case OP_SPECIAL_SUBU:
1154 if (op->r.rt == 0) {
1155 pr_debug("Convert OR/ADD/SUB $zero to MOV\n");
1156 op->m.op = OP_META_MOV;
1172 static bool lightrec_can_switch_delay_slot(union code op, union code next_op)
1177 case OP_SPECIAL_JALR:
1178 if (opcode_reads_register(next_op, op.r.rd) ||
1179 opcode_writes_register(next_op, op.r.rd))
1183 if (opcode_writes_register(next_op, op.r.rs))
1193 if (opcode_reads_register(next_op, 31) ||
1194 opcode_writes_register(next_op, 31))
1200 if (op.i.rt && opcode_writes_register(next_op, op.i.rt))
1205 if (op.i.rs && opcode_writes_register(next_op, op.i.rs))
1210 case OP_REGIMM_BLTZAL:
1211 case OP_REGIMM_BGEZAL:
1212 if (opcode_reads_register(next_op, 31) ||
1213 opcode_writes_register(next_op, 31))
1216 case OP_REGIMM_BLTZ:
1217 case OP_REGIMM_BGEZ:
1218 if (op.i.rs && opcode_writes_register(next_op, op.i.rs))
1230 static int lightrec_switch_delay_slots(struct lightrec_state *state, struct block *block)
1232 struct opcode *list, *next = &block->opcode_list[0];
1234 union code op, next_op;
1237 for (i = 0; i < block->nb_ops - 1; i++) {
1239 next = &block->opcode_list[i + 1];
1243 if (!has_delay_slot(op) || op_flag_no_ds(list->flags) ||
1244 op_flag_emulate_branch(list->flags) ||
1245 op.opcode == 0 || next_op.opcode == 0)
1248 if (is_delay_slot(block->opcode_list, i))
1251 if (op_flag_sync(next->flags))
1254 if (op_flag_load_delay(next->flags) && opcode_is_load(next_op))
1257 if (!lightrec_can_switch_delay_slot(list->c, next_op))
1260 pr_debug("Swap branch and delay slot opcodes "
1261 "at offsets 0x%x / 0x%x\n",
1262 i << 2, (i + 1) << 2);
1264 flags = next->flags | (list->flags & LIGHTREC_SYNC);
1267 next->flags = (list->flags | LIGHTREC_NO_DS) & ~LIGHTREC_SYNC;
1268 list->flags = flags | LIGHTREC_NO_DS;
1274 static int lightrec_detect_impossible_branches(struct lightrec_state *state,
1275 struct block *block)
1277 struct opcode *op, *list = block->opcode_list, *next = &list[0];
1281 for (i = 0; i < block->nb_ops - 1; i++) {
1283 next = &list[i + 1];
1285 if (!has_delay_slot(op->c) ||
1286 (!has_delay_slot(next->c) &&
1287 !opcode_is_mfc(next->c) &&
1288 !(next->i.op == OP_CP0 && next->r.rs == OP_CP0_RFE)))
1291 if (op->c.opcode == next->c.opcode) {
1292 /* The delay slot is the exact same opcode as the branch
1293 * opcode: this is effectively a NOP */
1298 op->flags |= LIGHTREC_EMULATE_BRANCH;
1300 if (OPT_LOCAL_BRANCHES && i + 2 < block->nb_ops) {
1301 /* The interpreter will only emulate the branch, then
1302 * return to the compiled code. Add a SYNC after the
1303 * branch + delay slot in the case where the branch
1305 list[i + 2].flags |= LIGHTREC_SYNC;
1312 static bool is_local_branch(const struct block *block, unsigned int idx)
1314 const struct opcode *op = &block->opcode_list[idx];
1317 switch (op->c.i.op) {
1323 offset = idx + 1 + (s16)op->c.i.imm;
1324 if (offset >= 0 && offset < block->nb_ops)
1332 static int lightrec_handle_load_delays(struct lightrec_state *state,
1333 struct block *block)
1335 struct opcode *op, *list = block->opcode_list;
1339 for (i = 0; i < block->nb_ops; i++) {
1342 if (!opcode_is_load(op->c) || !op->c.i.rt || op->c.i.op == OP_LWC2)
1345 if (!is_delay_slot(list, i)) {
1346 /* Only handle load delays in delay slots.
1347 * PSX games never abused load delay slots otherwise. */
1351 if (is_local_branch(block, i - 1)) {
1352 imm = (s16)list[i - 1].c.i.imm;
1354 if (!opcode_reads_register(list[i + imm].c, op->c.i.rt)) {
1355 /* The target opcode of the branch is inside
1356 * the block, and it does not read the register
1357 * written to by the load opcode; we can ignore
1358 * the load delay. */
1363 op->flags |= LIGHTREC_LOAD_DELAY;
1369 static int lightrec_swap_load_delays(struct lightrec_state *state,
1370 struct block *block)
1374 bool in_ds = false, skip_next = false;
1377 if (block->nb_ops < 2)
1380 for (i = 0; i < block->nb_ops - 2; i++) {
1381 c = block->opcode_list[i].c;
1385 } else if (!in_ds && opcode_is_load(c) && c.i.op != OP_LWC2) {
1386 next = block->opcode_list[i + 1].c;
1388 switch (next.i.op) {
1399 if (opcode_reads_register(next, c.i.rt)
1400 && !opcode_writes_register(next, c.i.rs)) {
1401 pr_debug("Swapping opcodes at offset 0x%x to "
1402 "respect load delay\n", i << 2);
1404 op = block->opcode_list[i];
1405 block->opcode_list[i] = block->opcode_list[i + 1];
1406 block->opcode_list[i + 1] = op;
1411 in_ds = has_delay_slot(c);
1417 static int lightrec_local_branches(struct lightrec_state *state, struct block *block)
1419 const struct opcode *ds;
1420 struct opcode *list;
1424 for (i = 0; i < block->nb_ops; i++) {
1425 list = &block->opcode_list[i];
1427 if (should_emulate(list) || !is_local_branch(block, i))
1430 offset = i + 1 + (s16)list->c.i.imm;
1432 pr_debug("Found local branch to offset 0x%x\n", offset << 2);
1434 ds = get_delay_slot(block->opcode_list, i);
1435 if (op_flag_load_delay(ds->flags) && opcode_is_load(ds->c)) {
1436 pr_debug("Branch delay slot has a load delay - skip\n");
1440 if (should_emulate(&block->opcode_list[offset])) {
1441 pr_debug("Branch target must be emulated - skip\n");
1445 if (offset && has_delay_slot(block->opcode_list[offset - 1].c)) {
1446 pr_debug("Branch target is a delay slot - skip\n");
1450 list->flags |= LIGHTREC_LOCAL_BRANCH;
1453 lightrec_reset_syncs(block);
1458 bool has_delay_slot(union code op)
1464 case OP_SPECIAL_JALR:
1482 bool is_delay_slot(const struct opcode *list, unsigned int offset)
1485 && !op_flag_no_ds(list[offset - 1].flags)
1486 && has_delay_slot(list[offset - 1].c);
1489 bool should_emulate(const struct opcode *list)
1491 return op_flag_emulate_branch(list->flags) && has_delay_slot(list->c);
1494 static bool op_writes_rd(union code c)
1505 static void lightrec_add_reg_op(struct opcode *op, u8 reg, u32 reg_op)
1507 if (op_writes_rd(op->c) && reg == op->r.rd)
1508 op->flags |= LIGHTREC_REG_RD(reg_op);
1509 else if (op->i.rs == reg)
1510 op->flags |= LIGHTREC_REG_RS(reg_op);
1511 else if (op->i.rt == reg)
1512 op->flags |= LIGHTREC_REG_RT(reg_op);
1514 pr_debug("Cannot add unload/clean/discard flag: "
1515 "opcode does not touch register %s!\n",
1516 lightrec_reg_name(reg));
1519 static void lightrec_add_unload(struct opcode *op, u8 reg)
1521 lightrec_add_reg_op(op, reg, LIGHTREC_REG_UNLOAD);
1524 static void lightrec_add_discard(struct opcode *op, u8 reg)
1526 lightrec_add_reg_op(op, reg, LIGHTREC_REG_DISCARD);
1529 static void lightrec_add_clean(struct opcode *op, u8 reg)
1531 lightrec_add_reg_op(op, reg, LIGHTREC_REG_CLEAN);
1535 lightrec_early_unload_sync(struct opcode *list, s16 *last_r, s16 *last_w)
1540 for (reg = 0; reg < 34; reg++) {
1541 offset = s16_max(last_w[reg], last_r[reg]);
1544 lightrec_add_unload(&list[offset], reg);
1547 memset(last_r, 0xff, sizeof(*last_r) * 34);
1548 memset(last_w, 0xff, sizeof(*last_w) * 34);
1551 static int lightrec_early_unload(struct lightrec_state *state, struct block *block)
1555 s16 last_r[34], last_w[34], last_sync = 0, next_sync = 0;
1556 u64 mask_r, mask_w, dirty = 0, loaded = 0;
1557 u8 reg, load_delay_reg = 0;
1559 memset(last_r, 0xff, sizeof(last_r));
1560 memset(last_w, 0xff, sizeof(last_w));
1564 * - the register is dirty, and is read again after a branch opcode
1567 * - the register is dirty or loaded, and is not read again
1568 * - the register is dirty or loaded, and is written again after a branch opcode
1569 * - the next opcode has the SYNC flag set
1572 * - the register is dirty or loaded, and is written again
1575 for (i = 0; i < block->nb_ops; i++) {
1576 op = &block->opcode_list[i];
1578 if (OPT_HANDLE_LOAD_DELAYS && load_delay_reg) {
1579 /* Handle delayed register write from load opcodes in
1581 last_w[load_delay_reg] = i;
1585 if (op_flag_sync(op->flags) || should_emulate(op)) {
1586 /* The next opcode has the SYNC flag set, or is a branch
1587 * that should be emulated: unload all registers. */
1588 lightrec_early_unload_sync(block->opcode_list, last_r, last_w);
1593 if (next_sync == i) {
1595 pr_debug("Last sync: 0x%x\n", last_sync << 2);
1598 if (has_delay_slot(op->c)) {
1599 next_sync = i + 1 + !op_flag_no_ds(op->flags);
1600 pr_debug("Next sync: 0x%x\n", next_sync << 2);
1603 mask_r = opcode_read_mask(op->c);
1604 mask_w = opcode_write_mask(op->c);
1606 if (op_flag_load_delay(op->flags) && opcode_is_load(op->c)) {
1607 /* If we have a load opcode in a delay slot, its target
1608 * register is actually not written there but at a
1609 * later point, in the dispatcher. Prevent the algorithm
1610 * from discarding its previous value. */
1611 load_delay_reg = op->c.i.rt;
1612 mask_w &= ~BIT(op->c.i.rt);
1615 for (reg = 0; reg < 34; reg++) {
1616 if (mask_r & BIT(reg)) {
1617 if (dirty & BIT(reg) && last_w[reg] < last_sync) {
1618 /* The register is dirty, and is read
1619 * again after a branch: clean it */
1621 lightrec_add_clean(&block->opcode_list[last_w[reg]], reg);
1629 if (mask_w & BIT(reg)) {
1630 if ((dirty & BIT(reg) && last_w[reg] < last_sync) ||
1631 (loaded & BIT(reg) && last_r[reg] < last_sync)) {
1632 /* The register is dirty or loaded, and
1633 * is written again after a branch:
1636 offset = s16_max(last_w[reg], last_r[reg]);
1637 lightrec_add_unload(&block->opcode_list[offset], reg);
1639 loaded &= ~BIT(reg);
1640 } else if (!(mask_r & BIT(reg)) &&
1641 ((dirty & BIT(reg) && last_w[reg] > last_sync) ||
1642 (loaded & BIT(reg) && last_r[reg] > last_sync))) {
1643 /* The register is dirty or loaded, and
1644 * is written again: discard it */
1646 offset = s16_max(last_w[reg], last_r[reg]);
1647 lightrec_add_discard(&block->opcode_list[offset], reg);
1649 loaded &= ~BIT(reg);
1661 /* Unload all registers that are dirty or loaded at the end of block. */
1662 lightrec_early_unload_sync(block->opcode_list, last_r, last_w);
1667 static int lightrec_flag_io(struct lightrec_state *state, struct block *block)
1669 struct opcode *list;
1670 enum psx_map psx_map;
1671 struct constprop_data v[32] = LIGHTREC_CONSTPROP_INITIALIZER;
1673 u32 val, kunseg_val;
1676 for (i = 0; i < block->nb_ops; i++) {
1677 list = &block->opcode_list[i];
1679 lightrec_consts_propagate(block, i, v);
1681 switch (list->i.op) {
1685 /* Mark all store operations that target $sp or $gp
1686 * as not requiring code invalidation. This is based
1687 * on the heuristic that stores using one of these
1688 * registers as address will never hit a code page. */
1689 if (list->i.rs >= 28 && list->i.rs <= 29 &&
1690 !state->maps[PSX_MAP_KERNEL_USER_RAM].ops) {
1691 pr_debug("Flaging opcode 0x%08x as not requiring invalidation\n",
1693 list->flags |= LIGHTREC_NO_INVALIDATE;
1696 /* Detect writes whose destination address is inside the
1697 * current block, using constant propagation. When these
1698 * occur, we mark the blocks as not compilable. */
1699 if (is_known(v, list->i.rs) &&
1700 kunseg(v[list->i.rs].value) >= kunseg(block->pc) &&
1701 kunseg(v[list->i.rs].value) < (kunseg(block->pc) + block->nb_ops * 4)) {
1702 pr_debug("Self-modifying block detected\n");
1703 block_set_flags(block, BLOCK_NEVER_COMPILE);
1704 list->flags |= LIGHTREC_SMC;
1718 if (v[list->i.rs].known | v[list->i.rs].sign) {
1719 psx_map = lightrec_get_constprop_map(state, v,
1723 if (psx_map != PSX_MAP_UNKNOWN && !is_known(v, list->i.rs))
1724 pr_debug("Detected map thanks to bit-level const propagation!\n");
1726 list->flags &= ~LIGHTREC_IO_MASK;
1728 val = v[list->i.rs].value + (s16) list->i.imm;
1729 kunseg_val = kunseg(val);
1731 no_mask = (v[list->i.rs].known & ~v[list->i.rs].value
1732 & 0xe0000000) == 0xe0000000;
1735 case PSX_MAP_KERNEL_USER_RAM:
1737 list->flags |= LIGHTREC_NO_MASK;
1739 case PSX_MAP_MIRROR1:
1740 case PSX_MAP_MIRROR2:
1741 case PSX_MAP_MIRROR3:
1742 pr_debug("Flaging opcode %u as RAM access\n", i);
1743 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_RAM);
1744 if (no_mask && state->mirrors_mapped)
1745 list->flags |= LIGHTREC_NO_MASK;
1748 pr_debug("Flaging opcode %u as BIOS access\n", i);
1749 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_BIOS);
1751 list->flags |= LIGHTREC_NO_MASK;
1753 case PSX_MAP_SCRATCH_PAD:
1754 pr_debug("Flaging opcode %u as scratchpad access\n", i);
1755 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_SCRATCH);
1757 list->flags |= LIGHTREC_NO_MASK;
1759 /* Consider that we're never going to run code from
1760 * the scratchpad. */
1761 list->flags |= LIGHTREC_NO_INVALIDATE;
1763 case PSX_MAP_HW_REGISTERS:
1764 if (state->ops.hw_direct &&
1765 state->ops.hw_direct(kunseg_val,
1766 opcode_is_store(list->c),
1767 opcode_get_io_size(list->c))) {
1768 pr_debug("Flagging opcode %u as direct I/O access\n",
1770 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_DIRECT_HW);
1773 list->flags |= LIGHTREC_NO_MASK;
1775 pr_debug("Flagging opcode %u as I/O access\n",
1777 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
1785 if (!LIGHTREC_FLAGS_GET_IO_MODE(list->flags)
1786 && list->i.rs >= 28 && list->i.rs <= 29
1787 && !state->maps[PSX_MAP_KERNEL_USER_RAM].ops) {
1788 /* Assume that all I/O operations that target
1789 * $sp or $gp will always only target a mapped
1790 * memory (RAM, BIOS, scratchpad). */
1791 if (state->opt_flags & LIGHTREC_OPT_SP_GP_HIT_RAM)
1792 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_RAM);
1794 list->flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_DIRECT);
1806 static u8 get_mfhi_mflo_reg(const struct block *block, u16 offset,
1807 const struct opcode *last,
1808 u32 mask, bool sync, bool mflo, bool another)
1810 const struct opcode *op, *next = &block->opcode_list[offset];
1812 u8 reg2, reg = mflo ? REG_LO : REG_HI;
1816 for (i = offset; i < block->nb_ops; i++) {
1818 next = &block->opcode_list[i + 1];
1821 /* If any other opcode writes or reads to the register
1822 * we'd use, then we cannot use it anymore. */
1823 mask |= opcode_read_mask(op->c);
1824 mask |= opcode_write_mask(op->c);
1826 if (op_flag_sync(op->flags))
1835 /* TODO: handle backwards branches too */
1836 if (!last && op_flag_local_branch(op->flags) &&
1837 (s16)op->c.i.imm >= 0) {
1838 branch_offset = i + 1 + (s16)op->c.i.imm
1839 - !!op_flag_no_ds(op->flags);
1841 reg = get_mfhi_mflo_reg(block, branch_offset, NULL,
1842 mask, sync, mflo, false);
1843 reg2 = get_mfhi_mflo_reg(block, offset + 1, next,
1844 mask, sync, mflo, false);
1845 if (reg > 0 && reg == reg2)
1851 return mflo ? REG_LO : REG_HI;
1853 case OP_META_MULTU2:
1857 case OP_SPECIAL_MULT:
1858 case OP_SPECIAL_MULTU:
1859 case OP_SPECIAL_DIV:
1860 case OP_SPECIAL_DIVU:
1862 case OP_SPECIAL_MTHI:
1866 case OP_SPECIAL_MTLO:
1874 if (!sync && !op_flag_no_ds(op->flags) &&
1875 (next->i.op == OP_SPECIAL) &&
1876 ((!mflo && next->r.op == OP_SPECIAL_MFHI) ||
1877 (mflo && next->r.op == OP_SPECIAL_MFLO)))
1881 case OP_SPECIAL_JALR:
1883 case OP_SPECIAL_MFHI:
1887 /* Must use REG_HI if there is another MFHI target*/
1888 reg2 = get_mfhi_mflo_reg(block, i + 1, next,
1889 0, sync, mflo, true);
1890 if (reg2 > 0 && reg2 != REG_HI)
1893 if (!sync && !(old_mask & BIT(op->r.rd)))
1899 case OP_SPECIAL_MFLO:
1903 /* Must use REG_LO if there is another MFLO target*/
1904 reg2 = get_mfhi_mflo_reg(block, i + 1, next,
1905 0, sync, mflo, true);
1906 if (reg2 > 0 && reg2 != REG_LO)
1909 if (!sync && !(old_mask & BIT(op->r.rd)))
1928 static void lightrec_replace_lo_hi(struct block *block, u16 offset,
1934 /* This function will remove the following MFLO/MFHI. It must be called
1935 * only if get_mfhi_mflo_reg() returned a non-zero value. */
1937 for (i = offset; i < last; i++) {
1938 struct opcode *op = &block->opcode_list[i];
1946 /* TODO: handle backwards branches too */
1947 if (op_flag_local_branch(op->flags) && (s16)op->c.i.imm >= 0) {
1948 branch_offset = i + 1 + (s16)op->c.i.imm
1949 - !!op_flag_no_ds(op->flags);
1951 lightrec_replace_lo_hi(block, branch_offset, last, lo);
1952 lightrec_replace_lo_hi(block, i + 1, branch_offset, lo);
1957 if (lo && op->r.op == OP_SPECIAL_MFLO) {
1958 pr_debug("Removing MFLO opcode at offset 0x%x\n",
1962 } else if (!lo && op->r.op == OP_SPECIAL_MFHI) {
1963 pr_debug("Removing MFHI opcode at offset 0x%x\n",
1976 static bool lightrec_always_skip_div_check(void)
1985 static int lightrec_flag_mults_divs(struct lightrec_state *state, struct block *block)
1987 struct opcode *list = NULL;
1988 struct constprop_data v[32] = LIGHTREC_CONSTPROP_INITIALIZER;
1992 for (i = 0; i < block->nb_ops - 1; i++) {
1993 list = &block->opcode_list[i];
1995 lightrec_consts_propagate(block, i, v);
1997 switch (list->i.op) {
1999 switch (list->r.op) {
2000 case OP_SPECIAL_DIV:
2001 case OP_SPECIAL_DIVU:
2002 /* If we are dividing by a non-zero constant, don't
2003 * emit the div-by-zero check. */
2004 if (lightrec_always_skip_div_check() ||
2005 (v[list->r.rt].known & v[list->r.rt].value)) {
2006 list->flags |= LIGHTREC_NO_DIV_CHECK;
2009 case OP_SPECIAL_MULT:
2010 case OP_SPECIAL_MULTU:
2017 case OP_META_MULTU2:
2023 /* Don't support opcodes in delay slots */
2024 if (is_delay_slot(block->opcode_list, i) ||
2025 op_flag_no_ds(list->flags)) {
2029 reg_lo = get_mfhi_mflo_reg(block, i + 1, NULL, 0, false, true, false);
2031 pr_debug("Mark MULT(U)/DIV(U) opcode at offset 0x%x as"
2032 " not writing LO\n", i << 2);
2033 list->flags |= LIGHTREC_NO_LO;
2036 reg_hi = get_mfhi_mflo_reg(block, i + 1, NULL, 0, false, false, false);
2038 pr_debug("Mark MULT(U)/DIV(U) opcode at offset 0x%x as"
2039 " not writing HI\n", i << 2);
2040 list->flags |= LIGHTREC_NO_HI;
2043 if (!reg_lo && !reg_hi) {
2044 pr_debug("Both LO/HI unused in this block, they will "
2045 "probably be used in parent block - removing "
2047 list->flags &= ~(LIGHTREC_NO_LO | LIGHTREC_NO_HI);
2050 if (reg_lo > 0 && reg_lo != REG_LO) {
2051 pr_debug("Found register %s to hold LO (rs = %u, rt = %u)\n",
2052 lightrec_reg_name(reg_lo), list->r.rs, list->r.rt);
2054 lightrec_replace_lo_hi(block, i + 1, block->nb_ops, true);
2055 list->r.rd = reg_lo;
2060 if (reg_hi > 0 && reg_hi != REG_HI) {
2061 pr_debug("Found register %s to hold HI (rs = %u, rt = %u)\n",
2062 lightrec_reg_name(reg_hi), list->r.rs, list->r.rt);
2064 lightrec_replace_lo_hi(block, i + 1, block->nb_ops, false);
2065 list->r.imm = reg_hi;
2074 static bool remove_div_sequence(struct block *block, unsigned int offset)
2077 unsigned int i, found = 0;
2080 * Scan for the zero-checking sequence that GCC automatically introduced
2081 * after most DIV/DIVU opcodes. This sequence checks the value of the
2082 * divisor, and if zero, executes a BREAK opcode, causing the BIOS
2083 * handler to crash the PS1.
2085 * For DIV opcodes, this sequence additionally checks that the signed
2086 * operation does not overflow.
2088 * With the assumption that the games never crashed the PS1, we can
2089 * therefore assume that the games never divided by zero or overflowed,
2090 * and these sequences can be removed.
2093 for (i = offset; i < block->nb_ops; i++) {
2094 op = &block->opcode_list[i];
2097 if (op->i.op == OP_SPECIAL &&
2098 (op->r.op == OP_SPECIAL_DIV || op->r.op == OP_SPECIAL_DIVU))
2101 if ((op->opcode & 0xfc1fffff) == 0x14000002) {
2102 /* BNE ???, zero, +8 */
2107 } else if (found == 1 && !op->opcode) {
2110 } else if (found == 2 && op->opcode == 0x0007000d) {
2113 } else if (found == 3 && op->opcode == 0x2401ffff) {
2116 } else if (found == 4 && (op->opcode & 0xfc1fffff) == 0x14010004) {
2117 /* BNE ???, at, +16 */
2119 } else if (found == 5 && op->opcode == 0x3c018000) {
2120 /* LUI at, 0x8000 */
2122 } else if (found == 6 && (op->opcode & 0x141fffff) == 0x14010002) {
2123 /* BNE ???, at, +16 */
2125 } else if (found == 7 && !op->opcode) {
2128 } else if (found == 8 && op->opcode == 0x0006000d) {
2141 pr_debug("Removing DIV%s sequence at offset 0x%x\n",
2142 found == 9 ? "" : "U", offset << 2);
2144 for (i = 0; i < found; i++)
2145 block->opcode_list[offset + i].opcode = 0;
2153 static int lightrec_remove_div_by_zero_check_sequence(struct lightrec_state *state,
2154 struct block *block)
2159 for (i = 0; i < block->nb_ops; i++) {
2160 op = &block->opcode_list[i];
2162 if (op->i.op == OP_SPECIAL &&
2163 (op->r.op == OP_SPECIAL_DIVU || op->r.op == OP_SPECIAL_DIV) &&
2164 remove_div_sequence(block, i + 1))
2165 op->flags |= LIGHTREC_NO_DIV_CHECK;
2171 static const u32 memset_code[] = {
2172 0x10a00006, // beqz a1, 2f
2173 0x24a2ffff, // addiu v0,a1,-1
2174 0x2403ffff, // li v1,-1
2175 0xac800000, // 1: sw zero,0(a0)
2176 0x2442ffff, // addiu v0,v0,-1
2177 0x1443fffd, // bne v0,v1, 1b
2178 0x24840004, // addiu a0,a0,4
2179 0x03e00008, // 2: jr ra
2183 static int lightrec_replace_memset(struct lightrec_state *state, struct block *block)
2188 for (i = 0; i < block->nb_ops; i++) {
2189 c = block->opcode_list[i].c;
2191 if (c.opcode != memset_code[i])
2194 if (i == ARRAY_SIZE(memset_code) - 1) {
2196 pr_debug("Block at PC 0x%x is a memset\n", block->pc);
2197 block_set_flags(block,
2198 BLOCK_IS_MEMSET | BLOCK_NEVER_COMPILE);
2200 /* Return non-zero to skip other optimizers. */
2208 static int lightrec_test_preload_pc(struct lightrec_state *state, struct block *block)
2214 for (i = 0; i < block->nb_ops; i++) {
2215 c = block->opcode_list[i].c;
2216 flags = block->opcode_list[i].flags;
2218 if (op_flag_sync(flags))
2224 block->flags |= BLOCK_PRELOAD_PC;
2229 case OP_REGIMM_BLTZAL:
2230 case OP_REGIMM_BGEZAL:
2231 block->flags |= BLOCK_PRELOAD_PC;
2241 if (!op_flag_local_branch(flags)) {
2242 block->flags |= BLOCK_PRELOAD_PC;
2248 case OP_SPECIAL_JALR:
2250 block->flags |= BLOCK_PRELOAD_PC;
2254 case OP_SPECIAL_SYSCALL:
2255 case OP_SPECIAL_BREAK:
2256 block->flags |= BLOCK_PRELOAD_PC;
2268 static int (*lightrec_optimizers[])(struct lightrec_state *state, struct block *) = {
2269 IF_OPT(OPT_REMOVE_DIV_BY_ZERO_SEQ, &lightrec_remove_div_by_zero_check_sequence),
2270 IF_OPT(OPT_REPLACE_MEMSET, &lightrec_replace_memset),
2271 IF_OPT(OPT_DETECT_IMPOSSIBLE_BRANCHES, &lightrec_detect_impossible_branches),
2272 IF_OPT(OPT_HANDLE_LOAD_DELAYS, &lightrec_handle_load_delays),
2273 IF_OPT(OPT_HANDLE_LOAD_DELAYS, &lightrec_swap_load_delays),
2274 IF_OPT(OPT_TRANSFORM_OPS, &lightrec_transform_branches),
2275 IF_OPT(OPT_LOCAL_BRANCHES, &lightrec_local_branches),
2276 IF_OPT(OPT_TRANSFORM_OPS, &lightrec_transform_ops),
2277 IF_OPT(OPT_SWITCH_DELAY_SLOTS, &lightrec_switch_delay_slots),
2278 IF_OPT(OPT_FLAG_IO, &lightrec_flag_io),
2279 IF_OPT(OPT_FLAG_MULT_DIV, &lightrec_flag_mults_divs),
2280 IF_OPT(OPT_EARLY_UNLOAD, &lightrec_early_unload),
2281 IF_OPT(OPT_PRELOAD_PC, &lightrec_test_preload_pc),
2284 int lightrec_optimize(struct lightrec_state *state, struct block *block)
2289 for (i = 0; i < ARRAY_SIZE(lightrec_optimizers); i++) {
2290 if (lightrec_optimizers[i]) {
2291 ret = (*lightrec_optimizers[i])(state, block);