Skip to content

Commit a801dc4

Browse files
jservfennecJ
andcommitted
Add reverse redundant move patterns
This eliminates moves immediately overwritten by loads or constants: - {mov rd, rs; load rd, offset} → {load rd, offset} - {mov rd, rs; li rd, imm} → {li rd, imm} Co-authored-by: fennecJ <[email protected]>
1 parent 5509c88 commit a801dc4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/peephole.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,35 @@ bool redundant_move_elim(ph2_ir_t *ph2_ir)
317317
return true;
318318
}
319319

320+
/* Pattern 6: Move followed by load
321+
* {mov rd, rs; load rd, offset} → {load rd, offset}
322+
* The move is pointless if immediately overwritten by load
323+
*/
324+
if (ph2_ir->op == OP_assign &&
325+
(next->op == OP_load || next->op == OP_global_load) &&
326+
ph2_ir->dest == next->dest) {
327+
/* Replace move+load with just the load */
328+
ph2_ir->op = next->op;
329+
ph2_ir->src0 = next->src0;
330+
ph2_ir->src1 = next->src1;
331+
ph2_ir->next = next->next;
332+
return true;
333+
}
334+
335+
/* Pattern 7: Move followed by constant load
336+
* {mov rd, rs; li rd, imm} → {li rd, imm}
337+
* The move is pointless if immediately overwritten by constant
338+
*/
339+
if (ph2_ir->op == OP_assign && next->op == OP_load_constant &&
340+
ph2_ir->dest == next->dest) {
341+
/* Replace move+li with just the li */
342+
ph2_ir->op = OP_load_constant;
343+
ph2_ir->src0 = next->src0;
344+
ph2_ir->src1 = 0; /* Clear unused field */
345+
ph2_ir->next = next->next;
346+
return true;
347+
}
348+
320349
return false;
321350
}
322351

0 commit comments

Comments
 (0)