Skip to content

Commit 58eac70

Browse files
committed
Simplify match based on the cast result of IntToInt.
1 parent 9e27377 commit 58eac70

File tree

3 files changed

+125
-60
lines changed

3 files changed

+125
-60
lines changed

compiler/rustc_mir_transform/src/match_branches.rs

+73-60
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_middle::mir::patch::MirPatch;
33
use rustc_middle::mir::*;
44
use rustc_middle::ty::{ParamEnv, ScalarInt, Ty, TyCtxt};
55
use rustc_target::abi::Size;
6+
use std::cmp::Ordering;
67
use std::iter;
78

89
use super::simplify::simplify_cfg;
@@ -263,33 +264,59 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToIf {
263264
}
264265
}
265266

267+
/// Check if the cast constant using `IntToInt` is equal to the target constant.
268+
fn can_cast(
269+
from_val: impl Into<u128>,
270+
from_size: Size,
271+
target_scalar: ScalarInt,
272+
from_is_signed: bool,
273+
) -> bool {
274+
let from_val = from_val.into();
275+
let from_scalar = ScalarInt::try_from_uint(from_val, from_size).unwrap();
276+
let to_size = target_scalar.size();
277+
let cast_scalar = match to_size.cmp(&from_size) {
278+
// Truncate to the target size.
279+
Ordering::Less => ScalarInt::truncate_from_uint(from_val, to_size).0,
280+
Ordering::Equal => from_scalar,
281+
Ordering::Greater => {
282+
if from_is_signed {
283+
// Extend to the target size using sext.
284+
ScalarInt::try_from_int(from_scalar.to_int(from_size), to_size).unwrap()
285+
} else {
286+
// Extend to the target size using zext.
287+
ScalarInt::try_from_uint(from_scalar.to_uint(from_size), to_size).unwrap()
288+
}
289+
}
290+
};
291+
cast_scalar == target_scalar
292+
}
293+
266294
#[derive(Default)]
267295
struct SimplifyToExp {
268-
transfrom_types: Vec<TransfromType>,
296+
transfrom_kinds: Vec<TransfromKind>,
269297
}
270298

271299
#[derive(Clone, Copy)]
272-
enum CompareType<'tcx, 'a> {
300+
enum ExpectedTransformKind<'tcx, 'a> {
273301
/// Identical statements.
274302
Same(&'a StatementKind<'tcx>),
275303
/// Assignment statements have the same value.
276-
Eq(&'a Place<'tcx>, Ty<'tcx>, ScalarInt),
304+
SameByEq { place: &'a Place<'tcx>, ty: Ty<'tcx>, scalar: ScalarInt },
277305
/// Enum variant comparison type.
278-
Discr { place: &'a Place<'tcx>, ty: Ty<'tcx>, is_signed: bool },
306+
Cast { place: &'a Place<'tcx>, ty: Ty<'tcx> },
279307
}
280308

281-
enum TransfromType {
309+
enum TransfromKind {
282310
Same,
283-
Eq,
284-
Discr,
311+
Cast,
285312
}
286313

287-
impl From<CompareType<'_, '_>> for TransfromType {
288-
fn from(compare_type: CompareType<'_, '_>) -> Self {
314+
impl From<ExpectedTransformKind<'_, '_>> for TransfromKind {
315+
fn from(compare_type: ExpectedTransformKind<'_, '_>) -> Self {
289316
match compare_type {
290-
CompareType::Same(_) => TransfromType::Same,
291-
CompareType::Eq(_, _, _) => TransfromType::Eq,
292-
CompareType::Discr { .. } => TransfromType::Discr,
317+
ExpectedTransformKind::Same(_) => TransfromKind::Same,
318+
ExpectedTransformKind::SameByEq { .. } => TransfromKind::Same,
319+
ExpectedTransformKind::Cast { .. } => TransfromKind::Cast,
293320
}
294321
}
295322
}
@@ -353,7 +380,7 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
353380
return None;
354381
}
355382
let mut target_iter = targets.iter();
356-
let (first_val, first_target) = target_iter.next().unwrap();
383+
let (first_case_val, first_target) = target_iter.next().unwrap();
357384
let first_terminator_kind = &bbs[first_target].terminator().kind;
358385
// Check that destinations are identical, and if not, then don't optimize this block
359386
if !targets
@@ -365,22 +392,18 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
365392

366393
let discr_size = tcx.layout_of(param_env.and(discr_ty)).unwrap().size;
367394
let first_stmts = &bbs[first_target].statements;
368-
let (second_val, second_target) = target_iter.next().unwrap();
395+
let (second_case_val, second_target) = target_iter.next().unwrap();
369396
let second_stmts = &bbs[second_target].statements;
370397
if first_stmts.len() != second_stmts.len() {
371398
return None;
372399
}
373400

374-
fn int_equal(l: ScalarInt, r: impl Into<u128>, size: Size) -> bool {
375-
l.to_bits_unchecked() == ScalarInt::try_from_uint(r, size).unwrap().to_bits_unchecked()
376-
}
377-
378401
// We first compare the two branches, and then the other branches need to fulfill the same conditions.
379-
let mut compare_types = Vec::new();
402+
let mut expected_transform_kinds = Vec::new();
380403
for (f, s) in iter::zip(first_stmts, second_stmts) {
381404
let compare_type = match (&f.kind, &s.kind) {
382405
// If two statements are exactly the same, we can optimize.
383-
(f_s, s_s) if f_s == s_s => CompareType::Same(f_s),
406+
(f_s, s_s) if f_s == s_s => ExpectedTransformKind::Same(f_s),
384407

385408
// If two statements are assignments with the match values to the same place, we can optimize.
386409
(
@@ -394,22 +417,23 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
394417
f_c.const_.try_eval_scalar_int(tcx, param_env),
395418
s_c.const_.try_eval_scalar_int(tcx, param_env),
396419
) {
397-
(Some(f), Some(s)) if f == s => CompareType::Eq(lhs_f, f_c.const_.ty(), f),
398-
// Enum variants can also be simplified to an assignment statement if their values are equal.
399-
// We need to consider both unsigned and signed scenarios here.
420+
(Some(f), Some(s)) if f == s => ExpectedTransformKind::SameByEq {
421+
place: lhs_f,
422+
ty: f_c.const_.ty(),
423+
scalar: f,
424+
},
425+
// Enum variants can also be simplified to an assignment statement,
426+
// if we can use `IntToInt` cast to get an equal value.
400427
(Some(f), Some(s))
401-
if ((f_c.const_.ty().is_signed() || discr_ty.is_signed())
402-
&& int_equal(f, first_val, discr_size)
403-
&& int_equal(s, second_val, discr_size))
404-
|| (Some(f) == ScalarInt::try_from_uint(first_val, f.size())
405-
&& Some(s)
406-
== ScalarInt::try_from_uint(second_val, s.size())) =>
428+
if (can_cast(first_case_val, discr_size, f, discr_ty.is_signed())
429+
&& can_cast(
430+
second_case_val,
431+
discr_size,
432+
s,
433+
discr_ty.is_signed(),
434+
)) =>
407435
{
408-
CompareType::Discr {
409-
place: lhs_f,
410-
ty: f_c.const_.ty(),
411-
is_signed: f_c.const_.ty().is_signed() || discr_ty.is_signed(),
412-
}
436+
ExpectedTransformKind::Cast { place: lhs_f, ty: f_c.const_.ty() }
413437
}
414438
_ => {
415439
return None;
@@ -420,47 +444,36 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
420444
// Otherwise we cannot optimize. Try another block.
421445
_ => return None,
422446
};
423-
compare_types.push(compare_type);
447+
expected_transform_kinds.push(compare_type);
424448
}
425449

426450
// All remaining BBs need to fulfill the same pattern as the two BBs from the previous step.
427451
for (other_val, other_target) in target_iter {
428452
let other_stmts = &bbs[other_target].statements;
429-
if compare_types.len() != other_stmts.len() {
453+
if expected_transform_kinds.len() != other_stmts.len() {
430454
return None;
431455
}
432-
for (f, s) in iter::zip(&compare_types, other_stmts) {
456+
for (f, s) in iter::zip(&expected_transform_kinds, other_stmts) {
433457
match (*f, &s.kind) {
434-
(CompareType::Same(f_s), s_s) if f_s == s_s => {}
458+
(ExpectedTransformKind::Same(f_s), s_s) if f_s == s_s => {}
435459
(
436-
CompareType::Eq(lhs_f, f_ty, val),
460+
ExpectedTransformKind::SameByEq { place: lhs_f, ty: f_ty, scalar },
437461
StatementKind::Assign(box (lhs_s, Rvalue::Use(Operand::Constant(s_c)))),
438462
) if lhs_f == lhs_s
439463
&& s_c.const_.ty() == f_ty
440-
&& s_c.const_.try_eval_scalar_int(tcx, param_env) == Some(val) => {}
464+
&& s_c.const_.try_eval_scalar_int(tcx, param_env) == Some(scalar) => {}
441465
(
442-
CompareType::Discr { place: lhs_f, ty: f_ty, is_signed },
466+
ExpectedTransformKind::Cast { place: lhs_f, ty: f_ty },
443467
StatementKind::Assign(box (lhs_s, Rvalue::Use(Operand::Constant(s_c)))),
444-
) if lhs_f == lhs_s && s_c.const_.ty() == f_ty => {
445-
let Some(f) = s_c.const_.try_eval_scalar_int(tcx, param_env) else {
446-
return None;
447-
};
448-
if is_signed
449-
&& s_c.const_.ty().is_signed()
450-
&& int_equal(f, other_val, discr_size)
451-
{
452-
continue;
453-
}
454-
if Some(f) == ScalarInt::try_from_uint(other_val, f.size()) {
455-
continue;
456-
}
457-
return None;
458-
}
468+
) if let Some(f) = s_c.const_.try_eval_scalar_int(tcx, param_env)
469+
&& lhs_f == lhs_s
470+
&& s_c.const_.ty() == f_ty
471+
&& can_cast(other_val, discr_size, f, discr_ty.is_signed()) => {}
459472
_ => return None,
460473
}
461474
}
462475
}
463-
self.transfrom_types = compare_types.into_iter().map(|c| c.into()).collect();
476+
self.transfrom_kinds = expected_transform_kinds.into_iter().map(|c| c.into()).collect();
464477
Some(())
465478
}
466479

@@ -478,13 +491,13 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
478491
let (_, first) = targets.iter().next().unwrap();
479492
let first = &bbs[first];
480493

481-
for (t, s) in iter::zip(&self.transfrom_types, &first.statements) {
494+
for (t, s) in iter::zip(&self.transfrom_kinds, &first.statements) {
482495
match (t, &s.kind) {
483-
(TransfromType::Same, _) | (TransfromType::Eq, _) => {
496+
(TransfromKind::Same, _) => {
484497
patch.add_statement(parent_end, s.kind.clone());
485498
}
486499
(
487-
TransfromType::Discr,
500+
TransfromKind::Cast,
488501
StatementKind::Assign(box (lhs, Rvalue::Use(Operand::Constant(f_c)))),
489502
) => {
490503
let operand = Operand::Copy(Place::from(discr_local));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
- // MIR for `match_i8_i16_failed_2` before MatchBranchSimplification
2+
+ // MIR for `match_i8_i16_failed_2` after MatchBranchSimplification
3+
4+
fn match_i8_i16_failed_2(_1: EnumAi8) -> i16 {
5+
debug i => _1;
6+
let mut _0: i16;
7+
let mut _2: i8;
8+
9+
bb0: {
10+
_2 = discriminant(_1);
11+
switchInt(move _2) -> [255: bb3, 2: bb4, 253: bb2, otherwise: bb1];
12+
}
13+
14+
bb1: {
15+
unreachable;
16+
}
17+
18+
bb2: {
19+
_0 = const -3_i16;
20+
goto -> bb5;
21+
}
22+
23+
bb3: {
24+
_0 = const 255_i16;
25+
goto -> bb5;
26+
}
27+
28+
bb4: {
29+
_0 = const 2_i16;
30+
goto -> bb5;
31+
}
32+
33+
bb5: {
34+
return;
35+
}
36+
}
37+

tests/mir-opt/matches_reduce_branches.rs

+15
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,20 @@ enum EnumAi16 {
226226
C = -3,
227227
}
228228

229+
// We cannot transform it, even though `-1i8` and `255i16` are the same in terms of bits,
230+
// what we actually require is that they are considered equal in a signed comparison,
231+
// after sign-extending to the larger type.
232+
// EMIT_MIR matches_reduce_branches.match_i8_i16_failed_2.MatchBranchSimplification.diff
233+
fn match_i8_i16_failed_2(i: EnumAi8) -> i16 {
234+
// CHECK-LABEL: fn match_i8_i16_failed_2(
235+
// CHECK: switchInt
236+
match i {
237+
EnumAi8::A => 255,
238+
EnumAi8::B => 2,
239+
EnumAi8::C => -3,
240+
}
241+
}
242+
229243
// EMIT_MIR matches_reduce_branches.match_i16_i8.MatchBranchSimplification.diff
230244
fn match_i16_i8(i: EnumAi16) -> i8 {
231245
// CHECK-LABEL: fn match_i16_i8(
@@ -270,6 +284,7 @@ fn main() {
270284
let _ = match_u8_u16_2(EnumBu8::A);
271285
let _ = match_i8_i16(EnumAi8::A);
272286
let _ = match_i8_i16_failed(EnumAi8::A);
287+
let _ = match_i8_i16_failed_2(EnumAi8::A);
273288
let _ = match_i8_i16(EnumAi8::A);
274289
let _ = match_i16_i8(EnumAi16::A);
275290
let _ = match_i128_u128(EnumAi128::A);

0 commit comments

Comments
 (0)