Skip to content

Commit eac7bcd

Browse files
committed
Move eval_bits optimization upstream
1 parent 0a6d794 commit eac7bcd

File tree

2 files changed

+16
-36
lines changed

2 files changed

+16
-36
lines changed

compiler/rustc_middle/src/mir/consts.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,16 @@ impl<'tcx> Const<'tcx> {
288288
tcx: TyCtxt<'tcx>,
289289
param_env: ty::ParamEnv<'tcx>,
290290
) -> Option<ScalarInt> {
291-
self.try_eval_scalar(tcx, param_env)?.try_to_int().ok()
291+
match self {
292+
// If the constant is already evaluated, we shortcut here.
293+
Const::Ty(c) if let ty::ConstKind::Value(valtree) = c.kind() => {
294+
valtree.try_to_scalar_int()
295+
},
296+
// This is a more general form of the previous case.
297+
_ => {
298+
self.try_eval_scalar(tcx, param_env)?.try_to_int().ok()
299+
},
300+
}
292301
}
293302

294303
#[inline]

compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

+6-35
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use rustc_middle::ty::layout::IntegerExt;
6161
use rustc_middle::ty::{self, Ty, TyCtxt, VariantDef};
6262
use rustc_session::lint;
6363
use rustc_span::{Span, DUMMY_SP};
64-
use rustc_target::abi::{FieldIdx, Integer, Primitive, Size, VariantIdx, FIRST_VARIANT};
64+
use rustc_target::abi::{FieldIdx, Integer, VariantIdx, FIRST_VARIANT};
6565

6666
use self::Constructor::*;
6767
use self::SliceKind::*;
@@ -86,35 +86,6 @@ fn expand_or_pat<'p, 'tcx>(pat: &'p Pat<'tcx>) -> Vec<&'p Pat<'tcx>> {
8686
pats
8787
}
8888

89-
/// Evaluate an int constant, with a faster branch for a common case.
90-
#[inline]
91-
fn fast_try_eval_bits<'tcx>(
92-
tcx: TyCtxt<'tcx>,
93-
param_env: ty::ParamEnv<'tcx>,
94-
value: &mir::Const<'tcx>,
95-
) -> Option<u128> {
96-
let int = match value {
97-
// If the constant is already evaluated, we shortcut here.
98-
mir::Const::Ty(c) if let ty::ConstKind::Value(valtree) = c.kind() => {
99-
valtree.unwrap_leaf()
100-
},
101-
// This is a more general form of the previous case.
102-
_ => {
103-
value.try_eval_scalar_int(tcx, param_env)?
104-
},
105-
};
106-
let size = match value.ty().kind() {
107-
ty::Bool => Size::from_bytes(1),
108-
ty::Char => Size::from_bytes(4),
109-
ty::Int(ity) => Integer::from_int_ty(&tcx, *ity).size(),
110-
ty::Uint(uty) => Integer::from_uint_ty(&tcx, *uty).size(),
111-
ty::Float(ty::FloatTy::F32) => Primitive::F32.size(&tcx),
112-
ty::Float(ty::FloatTy::F64) => Primitive::F64.size(&tcx),
113-
_ => return None,
114-
};
115-
int.to_bits(size).ok()
116-
}
117-
11889
/// An inclusive interval, used for precise integer exhaustiveness checking.
11990
/// `IntRange`s always store a contiguous range. This means that values are
12091
/// encoded such that `0` encodes the minimum value for the integer,
@@ -1346,14 +1317,14 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
13461317
PatKind::Constant { value } => {
13471318
match pat.ty.kind() {
13481319
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) => {
1349-
ctor = match fast_try_eval_bits(cx.tcx, cx.param_env, value) {
1320+
ctor = match value.try_eval_bits(cx.tcx, cx.param_env) {
13501321
Some(bits) => IntRange(IntRange::from_bits(cx.tcx, pat.ty, bits)),
13511322
None => Opaque,
13521323
};
13531324
fields = Fields::empty();
13541325
}
13551326
ty::Float(ty::FloatTy::F32) => {
1356-
ctor = match fast_try_eval_bits(cx.tcx, cx.param_env, value) {
1327+
ctor = match value.try_eval_bits(cx.tcx, cx.param_env) {
13571328
Some(bits) => {
13581329
use rustc_apfloat::Float;
13591330
let value = rustc_apfloat::ieee::Single::from_bits(bits);
@@ -1364,7 +1335,7 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
13641335
fields = Fields::empty();
13651336
}
13661337
ty::Float(ty::FloatTy::F64) => {
1367-
ctor = match fast_try_eval_bits(cx.tcx, cx.param_env, value) {
1338+
ctor = match value.try_eval_bits(cx.tcx, cx.param_env) {
13681339
Some(bits) => {
13691340
use rustc_apfloat::Float;
13701341
let value = rustc_apfloat::ieee::Double::from_bits(bits);
@@ -1399,8 +1370,8 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
13991370
PatKind::Range(box PatRange { lo, hi, end }) => {
14001371
use rustc_apfloat::Float;
14011372
let ty = lo.ty();
1402-
let lo = fast_try_eval_bits(cx.tcx, cx.param_env, lo).unwrap();
1403-
let hi = fast_try_eval_bits(cx.tcx, cx.param_env, hi).unwrap();
1373+
let lo = lo.try_eval_bits(cx.tcx, cx.param_env).unwrap();
1374+
let hi = hi.try_eval_bits(cx.tcx, cx.param_env).unwrap();
14041375
ctor = match ty.kind() {
14051376
ty::Char | ty::Int(_) | ty::Uint(_) => {
14061377
IntRange(IntRange::from_range(cx.tcx, lo, hi, ty, *end))

0 commit comments

Comments
 (0)