Skip to content

Commit e42bea9

Browse files
committed
Auto merge of #108282 - cjgillot:mir-checked-sh, r=tmiasko
Implement checked Shl/Shr at MIR building. This does not require any special handling by codegen backends, as the overflow behaviour is entirely determined by the rhs (shift amount). This allows MIR ConstProp to remove the overflow check for constant shifts. ~There is an existing different behaviour between cg_llvm and cg_clif (cc `@bjorn3).` I took cg_llvm's one as reference: overflow if `rhs < 0 || rhs > number_of_bits_in_lhs_ty`.~ EDIT: `cg_llvm` and `cg_clif` implement the overflow check differently. This PR uses `cg_llvm`'s implementation based on a `BitAnd` instead of `cg_clif`'s one based on an unsigned comparison.
2 parents 7b3bd56 + 760275d commit e42bea9

File tree

1 file changed

+0
-23
lines changed

1 file changed

+0
-23
lines changed

src/num.rs

-23
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,6 @@ pub(crate) fn codegen_checked_int_binop<'tcx>(
170170
in_lhs: CValue<'tcx>,
171171
in_rhs: CValue<'tcx>,
172172
) -> CValue<'tcx> {
173-
if bin_op != BinOp::Shl && bin_op != BinOp::Shr {
174-
assert_eq!(
175-
in_lhs.layout().ty,
176-
in_rhs.layout().ty,
177-
"checked int binop requires lhs and rhs of same type"
178-
);
179-
}
180-
181173
let lhs = in_lhs.load_scalar(fx);
182174
let rhs = in_rhs.load_scalar(fx);
183175

@@ -271,21 +263,6 @@ pub(crate) fn codegen_checked_int_binop<'tcx>(
271263
_ => unreachable!("invalid non-integer type {}", ty),
272264
}
273265
}
274-
BinOp::Shl => {
275-
let val = fx.bcx.ins().ishl(lhs, rhs);
276-
let ty = fx.bcx.func.dfg.value_type(val);
277-
let max_shift = i64::from(ty.bits()) - 1;
278-
let has_overflow = fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift);
279-
(val, has_overflow)
280-
}
281-
BinOp::Shr => {
282-
let val =
283-
if !signed { fx.bcx.ins().ushr(lhs, rhs) } else { fx.bcx.ins().sshr(lhs, rhs) };
284-
let ty = fx.bcx.func.dfg.value_type(val);
285-
let max_shift = i64::from(ty.bits()) - 1;
286-
let has_overflow = fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift);
287-
(val, has_overflow)
288-
}
289266
_ => bug!("binop {:?} on checked int/uint lhs: {:?} rhs: {:?}", bin_op, in_lhs, in_rhs),
290267
};
291268

0 commit comments

Comments
 (0)