Skip to content

Commit 156bda8

Browse files
authored
Avoid masking shift amounts (#1268)
Cranelift 0.87 now follows its own documentation regarding shift amounts, and implicitly masks them if the arch requires it. [0] [0]: bytecodealliance/wasmtime@0508932
1 parent b14c733 commit 156bda8

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

src/num.rs

+6-19
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,12 @@ pub(crate) fn codegen_int_binop<'tcx>(
150150
BinOp::BitXor => b.bxor(lhs, rhs),
151151
BinOp::BitAnd => b.band(lhs, rhs),
152152
BinOp::BitOr => b.bor(lhs, rhs),
153-
BinOp::Shl => {
154-
let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
155-
let actual_shift = fx.bcx.ins().band_imm(rhs, i64::from(lhs_ty.bits() - 1));
156-
fx.bcx.ins().ishl(lhs, actual_shift)
157-
}
153+
BinOp::Shl => b.ishl(lhs, rhs),
158154
BinOp::Shr => {
159-
let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
160-
let actual_shift = fx.bcx.ins().band_imm(rhs, i64::from(lhs_ty.bits() - 1));
161155
if signed {
162-
fx.bcx.ins().sshr(lhs, actual_shift)
156+
b.sshr(lhs, rhs)
163157
} else {
164-
fx.bcx.ins().ushr(lhs, actual_shift)
158+
b.ushr(lhs, rhs)
165159
}
166160
}
167161
// Compare binops handles by `codegen_binop`.
@@ -279,22 +273,15 @@ pub(crate) fn codegen_checked_int_binop<'tcx>(
279273
}
280274
}
281275
BinOp::Shl => {
282-
let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
283-
let masked_shift = fx.bcx.ins().band_imm(rhs, i64::from(lhs_ty.bits() - 1));
284-
let val = fx.bcx.ins().ishl(lhs, masked_shift);
276+
let val = fx.bcx.ins().ishl(lhs, rhs);
285277
let ty = fx.bcx.func.dfg.value_type(val);
286278
let max_shift = i64::from(ty.bits()) - 1;
287279
let has_overflow = fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift);
288280
(val, has_overflow)
289281
}
290282
BinOp::Shr => {
291-
let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
292-
let masked_shift = fx.bcx.ins().band_imm(rhs, i64::from(lhs_ty.bits() - 1));
293-
let val = if !signed {
294-
fx.bcx.ins().ushr(lhs, masked_shift)
295-
} else {
296-
fx.bcx.ins().sshr(lhs, masked_shift)
297-
};
283+
let val =
284+
if !signed { fx.bcx.ins().ushr(lhs, rhs) } else { fx.bcx.ins().sshr(lhs, rhs) };
298285
let ty = fx.bcx.func.dfg.value_type(val);
299286
let max_shift = i64::from(ty.bits()) - 1;
300287
let has_overflow = fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift);

0 commit comments

Comments
 (0)