preopt: use replaced arg after having replaced BinaryImm

when replacing BinaryImm, we use the prior arg, but later use the arg
that was replaced when writing an alias if we can determine the new op
is actually equivalent to a simple copy
This commit is contained in:
iximeow
2019-07-31 19:06:31 -07:00
committed by Benjamin Bouvier
parent 057d4f6e3c
commit 6e57e3f8f3
2 changed files with 17 additions and 2 deletions

View File

@@ -585,6 +585,7 @@ fn simplify(pos: &mut FuncCursor, inst: Inst) {
InstructionData::BinaryImm { opcode, arg, imm } => { InstructionData::BinaryImm { opcode, arg, imm } => {
let ty = pos.func.dfg.ctrl_typevar(inst); let ty = pos.func.dfg.ctrl_typevar(inst);
let mut arg = arg;
let mut imm = imm; let mut imm = imm;
match opcode { match opcode {
Opcode::IaddImm Opcode::IaddImm
@@ -613,12 +614,13 @@ fn simplify(pos: &mut FuncCursor, inst: Inst) {
_ => panic!("can't happen"), _ => panic!("can't happen"),
}; };
let new_imm = immediates::Imm64::from(new_imm); let new_imm = immediates::Imm64::from(new_imm);
let arg = *prev_arg; let new_arg = *prev_arg;
pos.func pos.func
.dfg .dfg
.replace(inst) .replace(inst)
.BinaryImm(opcode, ty, new_imm, arg); .BinaryImm(opcode, ty, new_imm, new_arg);
imm = new_imm; imm = new_imm;
arg = new_arg;
} }
} }
} }

View File

@@ -278,3 +278,16 @@ ebb0:
; nextln: v2 = sextend.i64 v3 ; nextln: v2 = sextend.i64 v3
; nextln: return v2 ; nextln: return v2
; nextln: } ; nextln: }
function %add_imm_fold(i32) -> i32 {
ebb0(v0: i32):
v1 = iadd_imm v0, 42
v2 = iadd_imm v1, -42
return v2
}
; sameln: function %add_imm_fold(i32)
; nextln: ebb0(v0: i32):
; nextln: v2 -> v0
; nextln: v1 = iadd_imm v0, 42
; nextln: nop
; nextln: return v2