Skip to content

Commit d5bcfb3

Browse files
committed
Simplify codegen for niche-encoded variant tests
1 parent 13b1e40 commit d5bcfb3

File tree

4 files changed

+110
-63
lines changed

4 files changed

+110
-63
lines changed

compiler/rustc_abi/src/lib.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use std::fmt;
4343
#[cfg(feature = "nightly")]
4444
use std::iter::Step;
4545
use std::num::{NonZeroUsize, ParseIntError};
46-
use std::ops::{Add, AddAssign, Deref, Mul, RangeInclusive, Sub};
46+
use std::ops::{Add, AddAssign, Deref, Mul, RangeFull, RangeInclusive, Sub};
4747
use std::str::FromStr;
4848

4949
use bitflags::bitflags;
@@ -1391,12 +1391,45 @@ impl WrappingRange {
13911391
}
13921392

13931393
/// Returns `true` if `size` completely fills the range.
1394+
///
1395+
/// Note that this is *not* the same as `self == WrappingRange::full(size)`.
1396+
/// Niche calculations can produce full ranges which are not the canonical one;
1397+
/// for example `Option<NonZero<u16>>` gets `valid_range: (..=0) | (1..)`.
13941398
#[inline]
13951399
fn is_full_for(&self, size: Size) -> bool {
13961400
let max_value = size.unsigned_int_max();
13971401
debug_assert!(self.start <= max_value && self.end <= max_value);
13981402
self.start == (self.end.wrapping_add(1) & max_value)
13991403
}
1404+
1405+
/// Checks whether this range is considered non-wrapping when the values are
1406+
/// interpreted as *unsigned* numbers of width `size`.
1407+
///
1408+
/// Returns `Ok(true)` if there's no wrap-around, `Ok(false)` if there is,
1409+
/// and `Err(..)` if the range is full so it depends how you think about it.
1410+
#[inline]
1411+
pub fn no_unsigned_wraparound(&self, size: Size) -> Result<bool, RangeFull> {
1412+
if self.is_full_for(size) { Err(..) } else { Ok(self.start <= self.end) }
1413+
}
1414+
1415+
/// Checks whether this range is considered non-wrapping when the values are
1416+
/// interpreted as *signed* numbers of width `size`.
1417+
///
1418+
/// This is heavily dependent on the `size`, as `100..=200` does wrap when
1419+
/// interpreted as `i8`, but doesn't when interpreted as `i16`.
1420+
///
1421+
/// Returns `Ok(true)` if there's no wrap-around, `Ok(false)` if there is,
1422+
/// and `Err(..)` if the range is full so it depends how you think about it.
1423+
#[inline]
1424+
pub fn no_signed_wraparound(&self, size: Size) -> Result<bool, RangeFull> {
1425+
if self.is_full_for(size) {
1426+
Err(..)
1427+
} else {
1428+
let start: i128 = size.sign_extend(self.start);
1429+
let end: i128 = size.sign_extend(self.end);
1430+
Ok(start <= end)
1431+
}
1432+
}
14001433
}
14011434

14021435
impl fmt::Debug for WrappingRange {

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
486486
// value and the variant index match, since that's all `Niche` can encode.
487487

488488
let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32();
489+
let niche_start_const = bx.cx().const_uint_big(tag_llty, niche_start);
489490

490491
// We have a subrange `niche_start..=niche_end` inside `range`.
491492
// If the value of the tag is inside this subrange, it's a
@@ -511,35 +512,44 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
511512
// } else {
512513
// untagged_variant
513514
// }
514-
let niche_start = bx.cx().const_uint_big(tag_llty, niche_start);
515-
let is_niche = bx.icmp(IntPredicate::IntEQ, tag, niche_start);
515+
let is_niche = bx.icmp(IntPredicate::IntEQ, tag, niche_start_const);
516516
let tagged_discr =
517517
bx.cx().const_uint(cast_to, niche_variants.start().as_u32() as u64);
518518
(is_niche, tagged_discr, 0)
519519
} else {
520520
// The special cases don't apply, so we'll have to go with
521521
// the general algorithm.
522-
let relative_discr = bx.sub(tag, bx.cx().const_uint_big(tag_llty, niche_start));
522+
523+
let tag_range = tag_scalar.valid_range(&dl);
524+
let tag_size = tag_scalar.size(&dl);
525+
let niche_end = u128::from(relative_max).wrapping_add(niche_start);
526+
let niche_end = tag_size.truncate(niche_end);
527+
528+
let relative_discr = bx.sub(tag, niche_start_const);
523529
let cast_tag = bx.intcast(relative_discr, cast_to, false);
524-
let is_niche = bx.icmp(
525-
IntPredicate::IntULE,
526-
relative_discr,
527-
bx.cx().const_uint(tag_llty, relative_max as u64),
528-
);
529-
530-
// Thanks to parameter attributes and load metadata, LLVM already knows
531-
// the general valid range of the tag. It's possible, though, for there
532-
// to be an impossible value *in the middle*, which those ranges don't
533-
// communicate, so it's worth an `assume` to let the optimizer know.
534-
if niche_variants.contains(&untagged_variant)
535-
&& bx.cx().sess().opts.optimize != OptLevel::No
536-
{
537-
let impossible =
538-
u64::from(untagged_variant.as_u32() - niche_variants.start().as_u32());
539-
let impossible = bx.cx().const_uint(tag_llty, impossible);
540-
let ne = bx.icmp(IntPredicate::IntNE, relative_discr, impossible);
541-
bx.assume(ne);
542-
}
530+
let is_niche = if tag_range.no_unsigned_wraparound(tag_size) == Ok(true) {
531+
if niche_start == tag_range.start {
532+
let niche_end_const = bx.cx().const_uint_big(tag_llty, niche_end);
533+
bx.icmp(IntPredicate::IntULE, tag, niche_end_const)
534+
} else {
535+
assert_eq!(niche_end, tag_range.end);
536+
bx.icmp(IntPredicate::IntUGE, tag, niche_start_const)
537+
}
538+
} else if tag_range.no_signed_wraparound(tag_size) == Ok(true) {
539+
if niche_start == tag_range.start {
540+
let niche_end_const = bx.cx().const_uint_big(tag_llty, niche_end);
541+
bx.icmp(IntPredicate::IntSLE, tag, niche_end_const)
542+
} else {
543+
assert_eq!(niche_end, tag_range.end);
544+
bx.icmp(IntPredicate::IntSGE, tag, niche_start_const)
545+
}
546+
} else {
547+
bx.icmp(
548+
IntPredicate::IntULE,
549+
relative_discr,
550+
bx.cx().const_uint(tag_llty, relative_max as u64),
551+
)
552+
};
543553

544554
(is_niche, cast_tag, niche_variants.start().as_u32() as u128)
545555
};
@@ -550,11 +560,24 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
550560
bx.add(tagged_discr, bx.cx().const_uint_big(cast_to, delta))
551561
};
552562

553-
let discr = bx.select(
554-
is_niche,
555-
tagged_discr,
556-
bx.cx().const_uint(cast_to, untagged_variant.as_u32() as u64),
557-
);
563+
let untagged_variant_const =
564+
bx.cx().const_uint(cast_to, u64::from(untagged_variant.as_u32()));
565+
566+
// Thanks to parameter attributes and load metadata, LLVM already knows
567+
// the general valid range of the tag. It's possible, though, for there
568+
// to be an impossible value *in the middle*, which those ranges don't
569+
// communicate, so it's worth an `assume` to let the optimizer know.
570+
// Most importantly, this means when optimizing a variant test like
571+
// `SELECT(is_niche, complex, CONST) == CONST` it's ok to simplify that
572+
// to `!is_niche` because the `complex` part can't possibly match.
573+
if niche_variants.contains(&untagged_variant)
574+
&& bx.cx().sess().opts.optimize != OptLevel::No
575+
{
576+
let ne = bx.icmp(IntPredicate::IntNE, tagged_discr, untagged_variant_const);
577+
bx.assume(ne);
578+
}
579+
580+
let discr = bx.select(is_niche, tagged_discr, untagged_variant_const);
558581

559582
// In principle we could insert assumes on the possible range of `discr`, but
560583
// currently in LLVM this isn't worth it because the original `tag` will

tests/codegen/enum/enum-discriminant-eq.rs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ pub fn mid_bool_eq_discr(a: Mid<bool>, b: Mid<bool>) -> bool {
8989
// CHECK-LABEL: @mid_bool_eq_discr(
9090

9191
// CHECK: %[[A_REL_DISCR:.+]] = add nsw i8 %a, -2
92-
// CHECK: %[[A_IS_NICHE:.+]] = icmp ult i8 %[[A_REL_DISCR]], 3
92+
// CHECK: %[[A_IS_NICHE:.+]] = icmp samesign ugt i8 %a, 1
9393
// CHECK: %[[A_NOT_HOLE:.+]] = icmp ne i8 %[[A_REL_DISCR]], 1
9494
// CHECK: tail call void @llvm.assume(i1 %[[A_NOT_HOLE]])
9595
// CHECK: %[[A_DISCR:.+]] = select i1 %[[A_IS_NICHE]], i8 %[[A_REL_DISCR]], i8 1
9696

9797
// CHECK: %[[B_REL_DISCR:.+]] = add nsw i8 %b, -2
98-
// CHECK: %[[B_IS_NICHE:.+]] = icmp ult i8 %[[B_REL_DISCR]], 3
98+
// CHECK: %[[B_IS_NICHE:.+]] = icmp samesign ugt i8 %b, 1
9999
// CHECK: %[[B_NOT_HOLE:.+]] = icmp ne i8 %[[B_REL_DISCR]], 1
100100
// CHECK: tail call void @llvm.assume(i1 %[[B_NOT_HOLE]])
101101
// CHECK: %[[B_DISCR:.+]] = select i1 %[[B_IS_NICHE]], i8 %[[B_REL_DISCR]], i8 1
@@ -109,13 +109,13 @@ pub fn mid_ord_eq_discr(a: Mid<Ordering>, b: Mid<Ordering>) -> bool {
109109
// CHECK-LABEL: @mid_ord_eq_discr(
110110

111111
// CHECK: %[[A_REL_DISCR:.+]] = add nsw i8 %a, -2
112-
// CHECK: %[[A_IS_NICHE:.+]] = icmp ult i8 %[[A_REL_DISCR]], 3
112+
// CHECK: %[[A_IS_NICHE:.+]] = icmp sgt i8 %a, 1
113113
// CHECK: %[[A_NOT_HOLE:.+]] = icmp ne i8 %[[A_REL_DISCR]], 1
114114
// CHECK: tail call void @llvm.assume(i1 %[[A_NOT_HOLE]])
115115
// CHECK: %[[A_DISCR:.+]] = select i1 %[[A_IS_NICHE]], i8 %[[A_REL_DISCR]], i8 1
116116

117117
// CHECK: %[[B_REL_DISCR:.+]] = add nsw i8 %b, -2
118-
// CHECK: %[[B_IS_NICHE:.+]] = icmp ult i8 %[[B_REL_DISCR]], 3
118+
// CHECK: %[[B_IS_NICHE:.+]] = icmp sgt i8 %b, 1
119119
// CHECK: %[[B_NOT_HOLE:.+]] = icmp ne i8 %[[B_REL_DISCR]], 1
120120
// CHECK: tail call void @llvm.assume(i1 %[[B_NOT_HOLE]])
121121
// CHECK: %[[B_DISCR:.+]] = select i1 %[[B_IS_NICHE]], i8 %[[B_REL_DISCR]], i8 1
@@ -138,13 +138,13 @@ pub fn mid_ac_eq_discr(a: Mid<AC>, b: Mid<AC>) -> bool {
138138
// CHECK-LABEL: @mid_ac_eq_discr(
139139

140140
// CHECK: %[[A_REL_DISCR:.+]] = xor i8 %a, -128
141-
// CHECK: %[[A_IS_NICHE:.+]] = icmp ult i8 %[[A_REL_DISCR]], 3
141+
// CHECK: %[[A_IS_NICHE:.+]] = icmp slt i8 %a, 0
142142
// CHECK: %[[A_NOT_HOLE:.+]] = icmp ne i8 %a, -127
143143
// CHECK: tail call void @llvm.assume(i1 %[[A_NOT_HOLE]])
144144
// CHECK: %[[A_DISCR:.+]] = select i1 %[[A_IS_NICHE]], i8 %[[A_REL_DISCR]], i8 1
145145

146146
// CHECK: %[[B_REL_DISCR:.+]] = xor i8 %b, -128
147-
// CHECK: %[[B_IS_NICHE:.+]] = icmp ult i8 %[[B_REL_DISCR]], 3
147+
// CHECK: %[[B_IS_NICHE:.+]] = icmp slt i8 %b, 0
148148
// CHECK: %[[B_NOT_HOLE:.+]] = icmp ne i8 %b, -127
149149
// CHECK: tail call void @llvm.assume(i1 %[[B_NOT_HOLE]])
150150
// CHECK: %[[B_DISCR:.+]] = select i1 %[[B_IS_NICHE]], i8 %[[B_REL_DISCR]], i8 1
@@ -160,17 +160,17 @@ pub fn mid_ac_eq_discr(a: Mid<AC>, b: Mid<AC>) -> bool {
160160
pub fn mid_giant_eq_discr(a: Mid<Giant>, b: Mid<Giant>) -> bool {
161161
// CHECK-LABEL: @mid_giant_eq_discr(
162162

163-
// CHECK: %[[A_REL_DISCR_WIDE:.+]] = add nsw i128 %a, -5
164-
// CHECK: %[[A_REL_DISCR:.+]] = trunc nsw i128 %[[A_REL_DISCR_WIDE]] to i64
165-
// CHECK: %[[A_IS_NICHE:.+]] = icmp ult i128 %[[A_REL_DISCR_WIDE]], 3
166-
// CHECK: %[[A_NOT_HOLE:.+]] = icmp ne i128 %[[A_REL_DISCR_WIDE]], 1
163+
// CHECK: %[[A_TRUNC:.+]] = trunc nuw nsw i128 %a to i64
164+
// CHECK: %[[A_REL_DISCR:.+]] = add nsw i64 %[[A_TRUNC]], -5
165+
// CHECK: %[[A_IS_NICHE:.+]] = icmp samesign ugt i128 %a, 4
166+
// CHECK: %[[A_NOT_HOLE:.+]] = icmp ne i64 %[[A_REL_DISCR]], 1
167167
// CHECK: tail call void @llvm.assume(i1 %[[A_NOT_HOLE]])
168168
// CHECK: %[[A_DISCR:.+]] = select i1 %[[A_IS_NICHE]], i64 %[[A_REL_DISCR]], i64 1
169169

170-
// CHECK: %[[B_REL_DISCR_WIDE:.+]] = add nsw i128 %b, -5
171-
// CHECK: %[[B_REL_DISCR:.+]] = trunc nsw i128 %[[B_REL_DISCR_WIDE]] to i64
172-
// CHECK: %[[B_IS_NICHE:.+]] = icmp ult i128 %[[B_REL_DISCR_WIDE]], 3
173-
// CHECK: %[[B_NOT_HOLE:.+]] = icmp ne i128 %[[B_REL_DISCR_WIDE]], 1
170+
// CHECK: %[[B_TRUNC:.+]] = trunc nuw nsw i128 %b to i64
171+
// CHECK: %[[B_REL_DISCR:.+]] = add nsw i64 %[[B_TRUNC]], -5
172+
// CHECK: %[[B_IS_NICHE:.+]] = icmp samesign ugt i128 %b, 4
173+
// CHECK: %[[B_NOT_HOLE:.+]] = icmp ne i64 %[[B_REL_DISCR]], 1
174174
// CHECK: tail call void @llvm.assume(i1 %[[B_NOT_HOLE]])
175175
// CHECK: %[[B_DISCR:.+]] = select i1 %[[B_IS_NICHE]], i64 %[[B_REL_DISCR]], i64 1
176176

@@ -181,23 +181,19 @@ pub fn mid_giant_eq_discr(a: Mid<Giant>, b: Mid<Giant>) -> bool {
181181

182182
// In niche-encoded enums, testing for the untagged variant should optimize to a
183183
// straight-forward comparison looking for the natural range of the payload value.
184-
// FIXME: A bunch don't, though.
185184

186185
#[unsafe(no_mangle)]
187186
pub fn mid_bool_is_thing(a: Mid<bool>) -> bool {
188187
// CHECK-LABEL: @mid_bool_is_thing(
189-
190-
// CHECK: %[[REL_DISCR:.+]] = add nsw i8 %a, -2
191-
// CHECK: %[[R:.+]] = icmp ugt i8 %[[REL_DISCR]], 2
188+
// CHECK: %[[R:.+]] = icmp samesign ult i8 %a, 2
192189
// CHECK: ret i1 %[[R]]
193190
discriminant_value(&a) == 1
194191
}
195192

196193
#[unsafe(no_mangle)]
197194
pub fn mid_ord_is_thing(a: Mid<Ordering>) -> bool {
198195
// CHECK-LABEL: @mid_ord_is_thing(
199-
// CHECK: %[[REL_DISCR:.+]] = add nsw i8 %a, -2
200-
// CHECK: %[[R:.+]] = icmp ugt i8 %[[REL_DISCR]], 2
196+
// CHECK: %[[R:.+]] = icmp slt i8 %a, 2
201197
// CHECK: ret i1 %[[R]]
202198
discriminant_value(&a) == 1
203199
}
@@ -221,11 +217,7 @@ pub fn mid_ac_is_thing(a: Mid<AC>) -> bool {
221217
#[unsafe(no_mangle)]
222218
pub fn mid_giant_is_thing(a: Mid<Giant>) -> bool {
223219
// CHECK-LABEL: @mid_giant_is_thing(
224-
// CHECK: %[[REL_DISCR_WIDE:.+]] = add nsw i128 %a, -5
225-
// CHECK: %[[REL_DISCR:.+]] = trunc nsw i128 %[[REL_DISCR_WIDE]] to i64
226-
// CHECK: %[[NOT_NICHE:.+]] = icmp ugt i128 %[[REL_DISCR_WIDE]], 2
227-
// CHECK: %[[IS_MID_VARIANT:.+]] = icmp eq i64 %[[REL_DISCR]], 1
228-
// CHECK: %[[R:.+]] = select i1 %[[NOT_NICHE]], i1 true, i1 %[[IS_MID_VARIANT]]
220+
// CHECK: %[[R:.+]] = icmp samesign ult i128 %a, 5
229221
// CHECK: ret i1 %[[R]]
230222
discriminant_value(&a) == 1
231223
}

tests/codegen/enum/enum-match.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub enum Enum1 {
4141
// CHECK-NEXT: start:
4242
// CHECK-NEXT: %[[REL_VAR:.+]] = add{{( nsw)?}} i8 %0, -2
4343
// CHECK-NEXT: %[[REL_VAR_WIDE:.+]] = zext i8 %[[REL_VAR]] to i64
44-
// CHECK-NEXT: %[[IS_NICHE:.+]] = icmp ult i8 %[[REL_VAR]], 2
44+
// CHECK-NEXT: %[[IS_NICHE:.+]] = icmp{{( samesign)?}} ugt i8 %0, 1
4545
// CHECK-NEXT: %[[NICHE_DISCR:.+]] = add nuw nsw i64 %[[REL_VAR_WIDE]], 1
4646
// CHECK-NEXT: %[[DISCR:.+]] = select i1 %[[IS_NICHE]], i64 %[[NICHE_DISCR]], i64 0
4747
// CHECK-NEXT: switch i64 %[[DISCR]]
@@ -148,10 +148,10 @@ pub enum MiddleNiche {
148148
// CHECK-LABEL: define{{( dso_local)?}} noundef{{( range\(i8 -?[0-9]+, -?[0-9]+\))?}} i8 @match4(i8{{.+}}%0)
149149
// CHECK-NEXT: start:
150150
// CHECK-NEXT: %[[REL_VAR:.+]] = add{{( nsw)?}} i8 %0, -2
151-
// CHECK-NEXT: %[[IS_NICHE:.+]] = icmp ult i8 %[[REL_VAR]], 5
152151
// CHECK-NEXT: %[[NOT_IMPOSSIBLE:.+]] = icmp ne i8 %[[REL_VAR]], 2
153152
// CHECK-NEXT: call void @llvm.assume(i1 %[[NOT_IMPOSSIBLE]])
154-
// CHECK-NEXT: %[[DISCR:.+]] = select i1 %[[IS_NICHE]], i8 %[[REL_VAR]], i8 2
153+
// CHECK-NEXT: %[[NOT_NICHE:.+]] = icmp{{( samesign)?}} ult i8 %0, 2
154+
// CHECK-NEXT: %[[DISCR:.+]] = select i1 %[[NOT_NICHE]], i8 2, i8 %[[REL_VAR]]
155155
// CHECK-NEXT: switch i8 %[[DISCR]]
156156
#[no_mangle]
157157
pub fn match4(e: MiddleNiche) -> u8 {
@@ -167,11 +167,10 @@ pub fn match4(e: MiddleNiche) -> u8 {
167167

168168
// CHECK-LABEL: define{{.+}}i1 @match4_is_c(i8{{.+}}%e)
169169
// CHECK-NEXT: start
170-
// CHECK-NEXT: %[[REL_VAR:.+]] = add{{( nsw)?}} i8 %e, -2
171-
// CHECK-NEXT: %[[NOT_NICHE:.+]] = icmp ugt i8 %[[REL_VAR]], 4
172-
// CHECK-NEXT: %[[NOT_IMPOSSIBLE:.+]] = icmp ne i8 %[[REL_VAR]], 2
170+
// CHECK-NEXT: %[[NOT_IMPOSSIBLE:.+]] = icmp ne i8 %e, 4
173171
// CHECK-NEXT: call void @llvm.assume(i1 %[[NOT_IMPOSSIBLE]])
174-
// CHECK-NEXT: ret i1 %[[NOT_NICHE]]
172+
// CHECK-NEXT: %[[IS_C:.+]] = icmp{{( samesign)?}} ult i8 %e, 2
173+
// CHECK-NEXT: ret i1 %[[IS_C]]
175174
#[no_mangle]
176175
pub fn match4_is_c(e: MiddleNiche) -> bool {
177176
// Before #139098, this couldn't optimize out the `select` because it looked
@@ -453,10 +452,10 @@ pub enum HugeVariantIndex {
453452
// CHECK-NEXT: start:
454453
// CHECK-NEXT: %[[REL_VAR:.+]] = add{{( nsw)?}} i8 %0, -2
455454
// CHECK-NEXT: %[[REL_VAR_WIDE:.+]] = zext i8 %[[REL_VAR]] to i64
456-
// CHECK-NEXT: %[[IS_NICHE:.+]] = icmp ult i8 %[[REL_VAR]], 3
457-
// CHECK-NEXT: %[[NOT_IMPOSSIBLE:.+]] = icmp ne i8 %[[REL_VAR]], 1
458-
// CHECK-NEXT: call void @llvm.assume(i1 %[[NOT_IMPOSSIBLE]])
455+
// CHECK-NEXT: %[[IS_NICHE:.+]] = icmp{{( samesign)?}} ugt i8 %0, 1
459456
// CHECK-NEXT: %[[NICHE_DISCR:.+]] = add nuw nsw i64 %[[REL_VAR_WIDE]], 257
457+
// CHECK-NEXT: %[[NOT_IMPOSSIBLE:.+]] = icmp ne i64 %[[NICHE_DISCR]], 258
458+
// CHECK-NEXT: call void @llvm.assume(i1 %[[NOT_IMPOSSIBLE]])
460459
// CHECK-NEXT: %[[DISCR:.+]] = select i1 %[[IS_NICHE]], i64 %[[NICHE_DISCR]], i64 258
461460
// CHECK-NEXT: switch i64 %[[DISCR]],
462461
// CHECK-NEXT: i64 257,

0 commit comments

Comments
 (0)