Skip to content

Commit 6cd35ac

Browse files
committed
Simplify cast_shift_expr_rhs.
It's only ever used with shift operators.
1 parent 8c02f4d commit 6cd35ac

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

compiler/rustc_codegen_ssa/src/base.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -337,31 +337,26 @@ pub fn coerce_unsized_into<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
337337

338338
pub fn cast_shift_expr_rhs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
339339
bx: &mut Bx,
340-
op: hir::BinOpKind,
341340
lhs: Bx::Value,
342341
rhs: Bx::Value,
343342
) -> Bx::Value {
344343
// Shifts may have any size int on the rhs
345-
if op.is_shift() {
346-
let mut rhs_llty = bx.cx().val_ty(rhs);
347-
let mut lhs_llty = bx.cx().val_ty(lhs);
348-
if bx.cx().type_kind(rhs_llty) == TypeKind::Vector {
349-
rhs_llty = bx.cx().element_type(rhs_llty)
350-
}
351-
if bx.cx().type_kind(lhs_llty) == TypeKind::Vector {
352-
lhs_llty = bx.cx().element_type(lhs_llty)
353-
}
354-
let rhs_sz = bx.cx().int_width(rhs_llty);
355-
let lhs_sz = bx.cx().int_width(lhs_llty);
356-
if lhs_sz < rhs_sz {
357-
bx.trunc(rhs, lhs_llty)
358-
} else if lhs_sz > rhs_sz {
359-
// FIXME (#1877: If in the future shifting by negative
360-
// values is no longer undefined then this is wrong.
361-
bx.zext(rhs, lhs_llty)
362-
} else {
363-
rhs
364-
}
344+
let mut rhs_llty = bx.cx().val_ty(rhs);
345+
let mut lhs_llty = bx.cx().val_ty(lhs);
346+
if bx.cx().type_kind(rhs_llty) == TypeKind::Vector {
347+
rhs_llty = bx.cx().element_type(rhs_llty)
348+
}
349+
if bx.cx().type_kind(lhs_llty) == TypeKind::Vector {
350+
lhs_llty = bx.cx().element_type(lhs_llty)
351+
}
352+
let rhs_sz = bx.cx().int_width(rhs_llty);
353+
let lhs_sz = bx.cx().int_width(lhs_llty);
354+
if lhs_sz < rhs_sz {
355+
bx.trunc(rhs, lhs_llty)
356+
} else if lhs_sz > rhs_sz {
357+
// FIXME (#1877: If in the future shifting by negative
358+
// values is no longer undefined then this is wrong.
359+
bx.zext(rhs, lhs_llty)
365360
} else {
366361
rhs
367362
}

compiler/rustc_codegen_ssa/src/common.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![allow(non_camel_case_types)]
22

33
use rustc_errors::struct_span_err;
4-
use rustc_hir as hir;
54
use rustc_hir::LangItem;
65
use rustc_middle::mir::interpret::ConstValue;
76
use rustc_middle::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
@@ -140,7 +139,7 @@ pub fn build_unchecked_lshift<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
140139
lhs: Bx::Value,
141140
rhs: Bx::Value,
142141
) -> Bx::Value {
143-
let rhs = base::cast_shift_expr_rhs(bx, hir::BinOpKind::Shl, lhs, rhs);
142+
let rhs = base::cast_shift_expr_rhs(bx, lhs, rhs);
144143
// #1877, #10183: Ensure that input is always valid
145144
let rhs = shift_mask_rhs(bx, rhs);
146145
bx.shl(lhs, rhs)
@@ -152,7 +151,7 @@ pub fn build_unchecked_rshift<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
152151
lhs: Bx::Value,
153152
rhs: Bx::Value,
154153
) -> Bx::Value {
155-
let rhs = base::cast_shift_expr_rhs(bx, hir::BinOpKind::Shr, lhs, rhs);
154+
let rhs = base::cast_shift_expr_rhs(bx, lhs, rhs);
156155
// #1877, #10183: Ensure that input is always valid
157156
let rhs = shift_mask_rhs(bx, rhs);
158157
let is_signed = lhs_t.is_signed();

0 commit comments

Comments
 (0)