Skip to content

Commit f658bd6

Browse files
committed
Avoid operating directly on i8 in get_discriminant
This helps avoid weird movzx instructions
1 parent 8a5ff54 commit f658bd6

File tree

1 file changed

+12
-15
lines changed
  • compiler/rustc_codegen_ssa/src/mir

1 file changed

+12
-15
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,16 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
270270
TagEncoding::Niche { untagged_variant, ref niche_variants, niche_start }
271271
if is_niche_after_nonniche(tag_scalar, niche_start, niche_variants, bx.cx()) =>
272272
{
273+
let (niche_llty, tag) = match tag_scalar.primitive() {
274+
// Operations on u8/u16 directly result in some additional movzxs, pretend the tag
275+
// is cast_to type (usize) instead.
276+
// FIXUP this is very x86 specific assumption
277+
Int(Integer::I8 | Integer::I16, _) => {
278+
(cast_to, bx.intcast(tag, cast_to, false))
279+
}
280+
_ => (niche_llty, tag),
281+
};
282+
273283
let is_untagged = bx.icmp(
274284
IntPredicate::IntULT,
275285
tag,
@@ -593,8 +603,7 @@ fn round_up_const_value_to_alignment<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
593603

594604
/// Are all niche variants encoded in tag greater than the actual untagged tag values (so that we
595605
/// can avoid the "relative discriminant" and we can simply `< niche_start` to ask whether it's
596-
/// untagged or not). Also check if last _variant_ value fits in the _tag_'s type.
597-
// Perhaps we can store this as flag in TagEncoding::Niche instead of recomputing?
606+
/// untagged or not).
598607
fn is_niche_after_nonniche(
599608
tag: Scalar,
600609
niche_start: u128,
@@ -606,17 +615,5 @@ fn is_niche_after_nonniche(
606615
return false;
607616
}
608617
let n_variants = niche_variants.end().as_u32() - niche_variants.start().as_u32();
609-
match tag.primitive() {
610-
Int(int, _) => {
611-
if niche_start.checked_add(n_variants as u128) != Some(tag_range.end) {
612-
return false;
613-
}
614-
match int {
615-
Integer::I8 if niche_variants.end().as_u32() > u8::MAX as u32 => false,
616-
Integer::I16 if niche_variants.end().as_u32() > u16::MAX as u32 => false,
617-
_ => true,
618-
}
619-
}
620-
_ => false,
621-
}
618+
niche_start.checked_add(n_variants as u128) == Some(tag_range.end)
622619
}

0 commit comments

Comments
 (0)