@@ -103,7 +103,6 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
103
103
}
104
104
} ;
105
105
106
- let cast_to_size = dest_layout. layout . size ( ) ;
107
106
let cast_to = fx. clif_type ( dest_layout. ty ) . unwrap ( ) ;
108
107
109
108
// Read the tag/niche-encoded discriminant from memory.
@@ -122,21 +121,7 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
122
121
dest. write_cvalue ( fx, res) ;
123
122
}
124
123
TagEncoding :: Niche { untagged_variant, ref niche_variants, niche_start } => {
125
- let tag_size = tag_scalar. size ( fx) ;
126
- let max_unsigned = tag_size. unsigned_int_max ( ) ;
127
- let max_signed = tag_size. signed_int_max ( ) as u128 ;
128
- let min_signed = max_signed + 1 ;
129
124
let relative_max = niche_variants. end ( ) . as_u32 ( ) - niche_variants. start ( ) . as_u32 ( ) ;
130
- let niche_end = niche_start. wrapping_add ( relative_max as u128 ) & max_unsigned;
131
- let range = tag_scalar. valid_range ( fx) ;
132
-
133
- let sle = |lhs : u128 , rhs : u128 | -> bool {
134
- // Signed and unsigned comparisons give the same results,
135
- // except that in signed comparisons an integer with the
136
- // sign bit set is less than one with the sign bit clear.
137
- // Toggle the sign bit to do a signed comparison.
138
- ( lhs ^ min_signed) <= ( rhs ^ min_signed)
139
- } ;
140
125
141
126
// We have a subrange `niche_start..=niche_end` inside `range`.
142
127
// If the value of the tag is inside this subrange, it's a
@@ -153,45 +138,6 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
153
138
// }
154
139
// However, we will likely be able to emit simpler code.
155
140
156
- // Find the least and greatest values in `range`, considered
157
- // both as signed and unsigned.
158
- let ( low_unsigned, high_unsigned) =
159
- if range. start <= range. end { ( range. start , range. end ) } else { ( 0 , max_unsigned) } ;
160
- let ( low_signed, high_signed) = if sle ( range. start , range. end ) {
161
- ( range. start , range. end )
162
- } else {
163
- ( min_signed, max_signed)
164
- } ;
165
-
166
- let niches_ule = niche_start <= niche_end;
167
- let niches_sle = sle ( niche_start, niche_end) ;
168
- let cast_smaller = cast_to_size <= tag_size;
169
-
170
- // In the algorithm above, we can change
171
- // cast(relative_tag) + niche_variants.start()
172
- // into
173
- // cast(tag + (niche_variants.start() - niche_start))
174
- // if either the casted type is no larger than the original
175
- // type, or if the niche values are contiguous (in either the
176
- // signed or unsigned sense).
177
- let can_incr = cast_smaller || niches_ule || niches_sle;
178
-
179
- let data_for_boundary_niche = || -> Option < ( IntCC , u128 ) > {
180
- if !can_incr {
181
- None
182
- } else if niche_start == low_unsigned {
183
- Some ( ( IntCC :: UnsignedLessThanOrEqual , niche_end) )
184
- } else if niche_end == high_unsigned {
185
- Some ( ( IntCC :: UnsignedGreaterThanOrEqual , niche_start) )
186
- } else if niche_start == low_signed {
187
- Some ( ( IntCC :: SignedLessThanOrEqual , niche_end) )
188
- } else if niche_end == high_signed {
189
- Some ( ( IntCC :: SignedGreaterThanOrEqual , niche_start) )
190
- } else {
191
- None
192
- }
193
- } ;
194
-
195
141
let ( is_niche, tagged_discr, delta) = if relative_max == 0 {
196
142
// Best case scenario: only one tagged variant. This will
197
143
// likely become just a comparison and a jump.
@@ -206,41 +152,6 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
206
152
let tagged_discr =
207
153
fx. bcx . ins ( ) . iconst ( cast_to, niche_variants. start ( ) . as_u32 ( ) as i64 ) ;
208
154
( is_niche, tagged_discr, 0 )
209
- } else if let Some ( ( predicate, constant) ) = data_for_boundary_niche ( ) {
210
- // The niche values are either the lowest or the highest in
211
- // `range`. We can avoid the first subtraction in the
212
- // algorithm.
213
- // The algorithm is now this:
214
- // is_niche = tag <= niche_end
215
- // discr = if is_niche {
216
- // cast(tag + (niche_variants.start() - niche_start))
217
- // } else {
218
- // untagged_variant
219
- // }
220
- // (the first line may instead be tag >= niche_start,
221
- // and may be a signed or unsigned comparison)
222
- // The arithmetic must be done before the cast, so we can
223
- // have the correct wrapping behavior. See issue #104519 for
224
- // the consequences of getting this wrong.
225
- let is_niche = codegen_icmp_imm ( fx, predicate, tag, constant as i128 ) ;
226
- let delta = ( niche_variants. start ( ) . as_u32 ( ) as u128 ) . wrapping_sub ( niche_start) ;
227
- let incr_tag = if delta == 0 {
228
- tag
229
- } else {
230
- let delta = match fx. bcx . func . dfg . value_type ( tag) {
231
- types:: I128 => {
232
- let lsb = fx. bcx . ins ( ) . iconst ( types:: I64 , delta as u64 as i64 ) ;
233
- let msb = fx. bcx . ins ( ) . iconst ( types:: I64 , ( delta >> 64 ) as u64 as i64 ) ;
234
- fx. bcx . ins ( ) . iconcat ( lsb, msb)
235
- }
236
- ty => fx. bcx . ins ( ) . iconst ( ty, delta as i64 ) ,
237
- } ;
238
- fx. bcx . ins ( ) . iadd ( tag, delta)
239
- } ;
240
-
241
- let cast_tag = clif_intcast ( fx, incr_tag, cast_to, !niches_ule) ;
242
-
243
- ( is_niche, cast_tag, 0 )
244
155
} else {
245
156
// The special cases don't apply, so we'll have to go with
246
157
// the general algorithm.
0 commit comments