Skip to content

Commit 194497a

Browse files
committed
Remove attempt to optimize codegen for discriminants.
1 parent 65c334e commit 194497a

File tree

1 file changed

+0
-92
lines changed
  • compiler/rustc_codegen_ssa/src/mir

1 file changed

+0
-92
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
211211
) -> V {
212212
let dl = &bx.tcx().data_layout;
213213
let cast_to_layout = bx.cx().layout_of(cast_to);
214-
let cast_to_size = cast_to_layout.layout.size();
215214
let cast_to = bx.cx().immediate_backend_type(cast_to_layout);
216215
if self.layout.abi.is_uninhabited() {
217216
return bx.cx().const_poison(cast_to);
@@ -261,21 +260,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
261260
_ => (tag_imm, bx.cx().immediate_backend_type(tag_op.layout)),
262261
};
263262

264-
let tag_size = tag_scalar.size(bx.cx());
265-
let max_unsigned = tag_size.unsigned_int_max();
266-
let max_signed = tag_size.signed_int_max() as u128;
267-
let min_signed = max_signed + 1;
268263
let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32();
269-
let niche_end = niche_start.wrapping_add(relative_max as u128) & max_unsigned;
270-
let range = tag_scalar.valid_range(bx.cx());
271-
272-
let sle = |lhs: u128, rhs: u128| -> bool {
273-
// Signed and unsigned comparisons give the same results,
274-
// except that in signed comparisons an integer with the
275-
// sign bit set is less than one with the sign bit clear.
276-
// Toggle the sign bit to do a signed comparison.
277-
(lhs ^ min_signed) <= (rhs ^ min_signed)
278-
};
279264

280265
// We have a subrange `niche_start..=niche_end` inside `range`.
281266
// If the value of the tag is inside this subrange, it's a
@@ -291,49 +276,6 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
291276
// untagged_variant
292277
// }
293278
// However, we will likely be able to emit simpler code.
294-
295-
// Find the least and greatest values in `range`, considered
296-
// both as signed and unsigned.
297-
let (low_unsigned, high_unsigned) = if range.start <= range.end {
298-
(range.start, range.end)
299-
} else {
300-
(0, max_unsigned)
301-
};
302-
let (low_signed, high_signed) = if sle(range.start, range.end) {
303-
(range.start, range.end)
304-
} else {
305-
(min_signed, max_signed)
306-
};
307-
308-
let niches_ule = niche_start <= niche_end;
309-
let niches_sle = sle(niche_start, niche_end);
310-
let cast_smaller = cast_to_size <= tag_size;
311-
312-
// In the algorithm above, we can change
313-
// cast(relative_tag) + niche_variants.start()
314-
// into
315-
// cast(tag + (niche_variants.start() - niche_start))
316-
// if either the casted type is no larger than the original
317-
// type, or if the niche values are contiguous (in either the
318-
// signed or unsigned sense).
319-
let can_incr = cast_smaller || niches_ule || niches_sle;
320-
321-
let data_for_boundary_niche = || -> Option<(IntPredicate, u128)> {
322-
if !can_incr {
323-
None
324-
} else if niche_start == low_unsigned {
325-
Some((IntPredicate::IntULE, niche_end))
326-
} else if niche_end == high_unsigned {
327-
Some((IntPredicate::IntUGE, niche_start))
328-
} else if niche_start == low_signed {
329-
Some((IntPredicate::IntSLE, niche_end))
330-
} else if niche_end == high_signed {
331-
Some((IntPredicate::IntSGE, niche_start))
332-
} else {
333-
None
334-
}
335-
};
336-
337279
let (is_niche, tagged_discr, delta) = if relative_max == 0 {
338280
// Best case scenario: only one tagged variant. This will
339281
// likely become just a comparison and a jump.
@@ -349,40 +291,6 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
349291
let tagged_discr =
350292
bx.cx().const_uint(cast_to, niche_variants.start().as_u32() as u64);
351293
(is_niche, tagged_discr, 0)
352-
} else if let Some((predicate, constant)) = data_for_boundary_niche() {
353-
// The niche values are either the lowest or the highest in
354-
// `range`. We can avoid the first subtraction in the
355-
// algorithm.
356-
// The algorithm is now this:
357-
// is_niche = tag <= niche_end
358-
// discr = if is_niche {
359-
// cast(tag + (niche_variants.start() - niche_start))
360-
// } else {
361-
// untagged_variant
362-
// }
363-
// (the first line may instead be tag >= niche_start,
364-
// and may be a signed or unsigned comparison)
365-
// The arithmetic must be done before the cast, so we can
366-
// have the correct wrapping behavior. See issue #104519 for
367-
// the consequences of getting this wrong.
368-
let is_niche =
369-
bx.icmp(predicate, tag, bx.cx().const_uint_big(tag_llty, constant));
370-
let delta = (niche_variants.start().as_u32() as u128).wrapping_sub(niche_start);
371-
let incr_tag = if delta == 0 {
372-
tag
373-
} else {
374-
bx.add(tag, bx.cx().const_uint_big(tag_llty, delta))
375-
};
376-
377-
let cast_tag = if cast_smaller {
378-
bx.intcast(incr_tag, cast_to, false)
379-
} else if niches_ule {
380-
bx.zext(incr_tag, cast_to)
381-
} else {
382-
bx.sext(incr_tag, cast_to)
383-
};
384-
385-
(is_niche, cast_tag, 0)
386294
} else {
387295
// The special cases don't apply, so we'll have to go with
388296
// the general algorithm.

0 commit comments

Comments
 (0)