Skip to content

Commit b1e9d2e

Browse files
committed
Revert "Avoid masking shift amounts (#1268)"
This reverts commit 156bda8. This breaks the mir_overflow_off rustc test: https://github.com/bjorn3/rustc_codegen_cranelift/runs/7971362755?check_suite_focus=true#step:7:2904
1 parent 48c45c4 commit b1e9d2e

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/num.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,18 @@ 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 => b.ishl(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+
}
154158
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));
155161
if signed {
156-
b.sshr(lhs, rhs)
162+
fx.bcx.ins().sshr(lhs, actual_shift)
157163
} else {
158-
b.ushr(lhs, rhs)
164+
fx.bcx.ins().ushr(lhs, actual_shift)
159165
}
160166
}
161167
// Compare binops handles by `codegen_binop`.
@@ -273,15 +279,22 @@ pub(crate) fn codegen_checked_int_binop<'tcx>(
273279
}
274280
}
275281
BinOp::Shl => {
276-
let val = fx.bcx.ins().ishl(lhs, rhs);
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);
277285
let ty = fx.bcx.func.dfg.value_type(val);
278286
let max_shift = i64::from(ty.bits()) - 1;
279287
let has_overflow = fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift);
280288
(val, has_overflow)
281289
}
282290
BinOp::Shr => {
283-
let val =
284-
if !signed { fx.bcx.ins().ushr(lhs, rhs) } else { fx.bcx.ins().sshr(lhs, rhs) };
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+
};
285298
let ty = fx.bcx.func.dfg.value_type(val);
286299
let max_shift = i64::from(ty.bits()) - 1;
287300
let has_overflow = fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift);

0 commit comments

Comments
 (0)