fix(mir): 模2^n AND优化——修复 x<0 且 x%2^n==0 时的零值错误

-4%2 应得 0,但 AND+符号修正路径算出 -2。
增加 cmp masked,#0; csel dst,wzr,temp,EQ 零值保护。
平台无 -O 时该路径被触发,导致 48_assign_complex_expr WA。
lzk
lzkk 3 days ago
parent 2cfc9e2fc8
commit 035f83b209

@ -606,11 +606,21 @@ namespace mir
Operand::VReg(masked, VRegClass::Int),
Operand::VReg(val_reg, VRegClass::Int)});
}
int after_sign = function.CreateVReg(VRegClass::Int);
block.Append(Opcode::Csel,
{Operand::VReg(dst, VRegClass::Int),
{Operand::VReg(after_sign, VRegClass::Int),
Operand::VReg(neg_fixup, VRegClass::Int),
Operand::VReg(masked, VRegClass::Int),
Operand::Imm(static_cast<int>(CondCode::LT))});
// 修正:若 masked==0 则结果必须为 0-4 % 2 = 0不是 -2
block.Append(Opcode::CmpImm,
{Operand::VReg(masked, VRegClass::Int),
Operand::Imm(0)});
block.Append(Opcode::Csel,
{Operand::VReg(dst, VRegClass::Int),
Operand::Reg(PhysReg::WZR),
Operand::VReg(after_sign, VRegClass::Int),
Operand::Imm(static_cast<int>(CondCode::EQ))});
value_vregs[value] = dst;
return dst;
}
@ -660,11 +670,21 @@ namespace mir
Operand::VReg(masked, VRegClass::Int),
Operand::VReg(val_reg, VRegClass::Int)});
}
int after_sign2 = function.CreateVReg(VRegClass::Int);
block.Append(Opcode::Csel,
{Operand::VReg(dst, VRegClass::Int),
{Operand::VReg(after_sign2, VRegClass::Int),
Operand::VReg(neg_fixup, VRegClass::Int),
Operand::VReg(masked, VRegClass::Int),
Operand::Imm(static_cast<int>(CondCode::LT))});
// 修正:若 masked==0 则结果必须为 0
block.Append(Opcode::CmpImm,
{Operand::VReg(masked, VRegClass::Int),
Operand::Imm(0)});
block.Append(Opcode::Csel,
{Operand::VReg(dst, VRegClass::Int),
Operand::Reg(PhysReg::WZR),
Operand::VReg(after_sign2, VRegClass::Int),
Operand::Imm(static_cast<int>(CondCode::EQ))});
value_vregs[value] = dst;
return dst;
}

Loading…
Cancel
Save