Skip to content

Commit

Permalink
Auto merge of rust-lang#65089 - nnethercote:optimize-integral-pattern…
Browse files Browse the repository at this point in the history
…-matching, r=Mark-Simulacrum

Optimize integral pattern matching

Various improvements to integral pattern matching. Together they reduce instruction counts for `unicode_normalization-check-clean` by about 16%.

r? @Mark-Simulacrum
  • Loading branch information
bors committed Oct 6, 2019
2 parents 0358617 + 2a3a544 commit 9203ee7
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 165 deletions.
11 changes: 8 additions & 3 deletions src/librustc/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,19 @@ impl<'tcx, Tag> Scalar<Tag> {
}
}

#[inline(always)]
pub fn check_raw(data: u128, size: u8, target_size: Size) {
assert_eq!(target_size.bytes(), size as u64);
assert_ne!(size, 0, "you should never look at the bits of a ZST");
Scalar::check_data(data, size);
}

/// Do not call this method! Use either `assert_bits` or `force_bits`.
#[inline]
pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
match self {
Scalar::Raw { data, size } => {
assert_eq!(target_size.bytes(), size as u64);
assert_ne!(size, 0, "you should never look at the bits of a ZST");
Scalar::check_data(data, size);
Self::check_raw(data, size, target_size);
Ok(data)
}
Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
Expand Down
18 changes: 2 additions & 16 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_macros::HashStable;
use crate::ty::subst::{InternalSubsts, Subst, SubstsRef, GenericArg, GenericArgKind};
use crate::ty::{self, AdtDef, Discr, DefIdTree, TypeFlags, Ty, TyCtxt, TypeFoldable};
use crate::ty::{List, TyS, ParamEnvAnd, ParamEnv};
use crate::ty::layout::{Size, Integer, IntegerExt, VariantIdx};
use crate::ty::layout::VariantIdx;
use crate::util::captures::Captures;
use crate::mir::interpret::{Scalar, GlobalId};

Expand All @@ -24,7 +24,6 @@ use std::marker::PhantomData;
use std::ops::Range;
use rustc_target::spec::abi;
use syntax::ast::{self, Ident};
use syntax::attr::{SignedInt, UnsignedInt};
use syntax::symbol::{kw, InternedString};

use self::InferTy::*;
Expand Down Expand Up @@ -2300,20 +2299,7 @@ impl<'tcx> Const<'tcx> {
ty: Ty<'tcx>,
) -> Option<u128> {
assert_eq!(self.ty, ty);
// This is purely an optimization -- layout_of is a pretty expensive operation,
// but if we can determine the size without calling it, we don't need all that complexity
// (hashing, caching, etc.). As such, try to skip it.
let size = match ty.kind {
ty::Bool => Size::from_bytes(1),
ty::Char => Size::from_bytes(4),
ty::Int(ity) => {
Integer::from_attr(&tcx, SignedInt(ity)).size()
}
ty::Uint(uty) => {
Integer::from_attr(&tcx, UnsignedInt(uty)).size()
}
_ => tcx.layout_of(param_env.with_reveal_all().and(ty)).ok()?.size,
};
let size = tcx.layout_of(param_env.with_reveal_all().and(ty)).ok()?.size;
// if `ty` does not depend on generic parameters, use an empty param_env
self.eval(tcx, param_env).val.try_to_bits(size)
}
Expand Down
Loading

0 comments on commit 9203ee7

Please sign in to comment.