Shift right is a noop if rhs is comptime known to be zero

This commit is contained in:
Jimmi Holst Christensen 2022-01-07 22:09:35 +01:00
parent a68e6cd3d7
commit d5093b6c13

View File

@ -7318,8 +7318,8 @@ fn zirShr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
const lhs = sema.resolveInst(extra.lhs);
const rhs = sema.resolveInst(extra.rhs);
if (try sema.resolveMaybeUndefVal(block, lhs_src, lhs)) |lhs_val| {
if (try sema.resolveMaybeUndefVal(block, rhs_src, rhs)) |rhs_val| {
if (try sema.resolveMaybeUndefVal(block, rhs_src, rhs)) |rhs_val| {
if (try sema.resolveMaybeUndefVal(block, lhs_src, lhs)) |lhs_val| {
const lhs_ty = sema.typeOf(lhs);
if (lhs_val.isUndef() or rhs_val.isUndef()) {
return sema.addConstUndef(lhs_ty);
@ -7331,6 +7331,12 @@ fn zirShr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
const val = try lhs_val.shr(rhs_val, sema.arena);
return sema.addConstant(lhs_ty, val);
}
// Even if lhs is not comptime known, we can still deduce certain things based
// on rhs.
// If rhs is 0, return lhs without doing any calculations.
else if (rhs_val.compareWithZero(.eq)) {
return lhs;
}
}
try sema.requireRuntimeBlock(block, src);