From 5ee964049a9f0808bc0729fad79be446a2a987cb Mon Sep 17 00:00:00 2001 From: dianqk Date: Sat, 10 Jan 2026 18:34:02 +0800 Subject: [PATCH] Add FileCheck to if_condition_int.rs --- ...t_opt_bool.SimplifyComparisonIntegral.diff | 2 +- ...opt_floats.SimplifyComparisonIntegral.diff | 2 +- ...comparison.SimplifyComparisonIntegral.diff | 18 ++--- ...t.opt_char.SimplifyComparisonIntegral.diff | 4 +- ...int.opt_i8.SimplifyComparisonIntegral.diff | 4 +- ...ltiple_ifs.SimplifyComparisonIntegral.diff | 8 +- ...t_negative.SimplifyComparisonIntegral.diff | 4 +- ...nt.opt_u32.SimplifyComparisonIntegral.diff | 4 +- tests/mir-opt/if_condition_int.rs | 76 ++++++++++++++++--- 9 files changed, 88 insertions(+), 34 deletions(-) diff --git a/tests/mir-opt/if_condition_int.dont_opt_bool.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.dont_opt_bool.SimplifyComparisonIntegral.diff index 2350faa05d3f..f2d527326592 100644 --- a/tests/mir-opt/if_condition_int.dont_opt_bool.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.dont_opt_bool.SimplifyComparisonIntegral.diff @@ -9,7 +9,7 @@ bb0: { StorageLive(_2); _2 = copy _1; - switchInt(move _2) -> [0: bb2, otherwise: bb1]; + switchInt(copy _1) -> [0: bb2, otherwise: bb1]; } bb1: { diff --git a/tests/mir-opt/if_condition_int.dont_opt_floats.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.dont_opt_floats.SimplifyComparisonIntegral.diff index 94570017730d..c72a58a145f4 100644 --- a/tests/mir-opt/if_condition_int.dont_opt_floats.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.dont_opt_floats.SimplifyComparisonIntegral.diff @@ -11,7 +11,7 @@ StorageLive(_2); StorageLive(_3); _3 = copy _1; - _2 = Eq(move _3, const -42f32); + _2 = Eq(copy _1, const -42f32); switchInt(move _2) -> [0: bb2, otherwise: bb1]; } diff --git a/tests/mir-opt/if_condition_int.dont_remove_comparison.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.dont_remove_comparison.SimplifyComparisonIntegral.diff index d19b4148405e..29d57722355b 100644 --- a/tests/mir-opt/if_condition_int.dont_remove_comparison.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.dont_remove_comparison.SimplifyComparisonIntegral.diff @@ -15,23 +15,20 @@ } bb0: { - StorageLive(_2); + nop; StorageLive(_3); _3 = copy _1; -- _2 = Eq(move _3, const 17_i8); -- StorageDead(_3); + _2 = Eq(copy _1, const 17_i8); + StorageDead(_3); - switchInt(copy _2) -> [0: bb2, otherwise: bb1]; -+ _2 = Eq(copy _3, const 17_i8); -+ nop; -+ switchInt(move _3) -> [17: bb1, otherwise: bb2]; ++ switchInt(move _1) -> [17: bb1, otherwise: bb2]; } bb1: { -+ StorageDead(_3); StorageLive(_6); StorageLive(_7); _7 = copy _2; - _6 = move _7 as i32 (IntToInt); + _6 = copy _2 as i32 (IntToInt); StorageDead(_7); _0 = Add(const 100_i32, move _6); StorageDead(_6); @@ -39,11 +36,10 @@ } bb2: { -+ StorageDead(_3); StorageLive(_4); StorageLive(_5); _5 = copy _2; - _4 = move _5 as i32 (IntToInt); + _4 = copy _2 as i32 (IntToInt); StorageDead(_5); _0 = Add(const 10_i32, move _4); StorageDead(_4); @@ -51,7 +47,7 @@ } bb3: { - StorageDead(_2); + nop; return; } } diff --git a/tests/mir-opt/if_condition_int.opt_char.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.opt_char.SimplifyComparisonIntegral.diff index e47b7e522598..37b41c2aa5ab 100644 --- a/tests/mir-opt/if_condition_int.opt_char.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.opt_char.SimplifyComparisonIntegral.diff @@ -11,10 +11,10 @@ StorageLive(_2); StorageLive(_3); _3 = copy _1; -- _2 = Eq(move _3, const 'x'); +- _2 = Eq(copy _1, const 'x'); - switchInt(move _2) -> [0: bb2, otherwise: bb1]; + nop; -+ switchInt(move _3) -> [120: bb1, otherwise: bb2]; ++ switchInt(move _1) -> [120: bb1, otherwise: bb2]; } bb1: { diff --git a/tests/mir-opt/if_condition_int.opt_i8.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.opt_i8.SimplifyComparisonIntegral.diff index b6cdc6af8bf4..c92a606090e4 100644 --- a/tests/mir-opt/if_condition_int.opt_i8.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.opt_i8.SimplifyComparisonIntegral.diff @@ -11,10 +11,10 @@ StorageLive(_2); StorageLive(_3); _3 = copy _1; -- _2 = Eq(move _3, const 42_i8); +- _2 = Eq(copy _1, const 42_i8); - switchInt(move _2) -> [0: bb2, otherwise: bb1]; + nop; -+ switchInt(move _3) -> [42: bb1, otherwise: bb2]; ++ switchInt(move _1) -> [42: bb1, otherwise: bb2]; } bb1: { diff --git a/tests/mir-opt/if_condition_int.opt_multiple_ifs.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.opt_multiple_ifs.SimplifyComparisonIntegral.diff index 31745ac732a2..a73670323b1f 100644 --- a/tests/mir-opt/if_condition_int.opt_multiple_ifs.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.opt_multiple_ifs.SimplifyComparisonIntegral.diff @@ -13,10 +13,10 @@ StorageLive(_2); StorageLive(_3); _3 = copy _1; -- _2 = Eq(move _3, const 42_u32); +- _2 = Eq(copy _1, const 42_u32); - switchInt(move _2) -> [0: bb2, otherwise: bb1]; + nop; -+ switchInt(move _3) -> [42: bb1, otherwise: bb2]; ++ switchInt(move _1) -> [42: bb1, otherwise: bb2]; } bb1: { @@ -30,10 +30,10 @@ StorageLive(_4); StorageLive(_5); _5 = copy _1; -- _4 = Ne(move _5, const 21_u32); +- _4 = Ne(copy _1, const 21_u32); - switchInt(move _4) -> [0: bb4, otherwise: bb3]; + nop; -+ switchInt(move _5) -> [21: bb4, otherwise: bb3]; ++ switchInt(move _1) -> [21: bb4, otherwise: bb3]; } bb3: { diff --git a/tests/mir-opt/if_condition_int.opt_negative.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.opt_negative.SimplifyComparisonIntegral.diff index d747985f6e13..36145ab02bea 100644 --- a/tests/mir-opt/if_condition_int.opt_negative.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.opt_negative.SimplifyComparisonIntegral.diff @@ -11,10 +11,10 @@ StorageLive(_2); StorageLive(_3); _3 = copy _1; -- _2 = Eq(move _3, const -42_i32); +- _2 = Eq(copy _1, const -42_i32); - switchInt(move _2) -> [0: bb2, otherwise: bb1]; + nop; -+ switchInt(move _3) -> [4294967254: bb1, otherwise: bb2]; ++ switchInt(move _1) -> [4294967254: bb1, otherwise: bb2]; } bb1: { diff --git a/tests/mir-opt/if_condition_int.opt_u32.SimplifyComparisonIntegral.diff b/tests/mir-opt/if_condition_int.opt_u32.SimplifyComparisonIntegral.diff index 1d6809a9438a..2cc4a8b76973 100644 --- a/tests/mir-opt/if_condition_int.opt_u32.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/if_condition_int.opt_u32.SimplifyComparisonIntegral.diff @@ -11,10 +11,10 @@ StorageLive(_2); StorageLive(_3); _3 = copy _1; -- _2 = Eq(move _3, const 42_u32); +- _2 = Eq(copy _1, const 42_u32); - switchInt(move _2) -> [0: bb2, otherwise: bb1]; + nop; -+ switchInt(move _3) -> [42: bb1, otherwise: bb2]; ++ switchInt(move _1) -> [42: bb1, otherwise: bb2]; } bb1: { diff --git a/tests/mir-opt/if_condition_int.rs b/tests/mir-opt/if_condition_int.rs index 4cc2c2b90210..24ef6b85529e 100644 --- a/tests/mir-opt/if_condition_int.rs +++ b/tests/mir-opt/if_condition_int.rs @@ -1,36 +1,76 @@ -// skip-filecheck //@ test-mir-pass: SimplifyComparisonIntegral -// EMIT_MIR if_condition_int.opt_u32.SimplifyComparisonIntegral.diff -// EMIT_MIR if_condition_int.opt_negative.SimplifyComparisonIntegral.diff -// EMIT_MIR if_condition_int.opt_char.SimplifyComparisonIntegral.diff -// EMIT_MIR if_condition_int.opt_i8.SimplifyComparisonIntegral.diff -// EMIT_MIR if_condition_int.dont_opt_bool.SimplifyComparisonIntegral.diff -// EMIT_MIR if_condition_int.opt_multiple_ifs.SimplifyComparisonIntegral.diff -// EMIT_MIR if_condition_int.dont_remove_comparison.SimplifyComparisonIntegral.diff -// EMIT_MIR if_condition_int.dont_opt_floats.SimplifyComparisonIntegral.diff +// GVN simplifies FileCheck. +//@ compile-flags: -Zmir-enable-passes=+GVN +// EMIT_MIR if_condition_int.opt_u32.SimplifyComparisonIntegral.diff fn opt_u32(x: u32) -> u32 { + // CHECK-LABEL: fn opt_u32( + // FIXME: This should be copy. + // CHECK: switchInt(move _1) -> [42: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; + // CHECK: [[BB1]]: + // CHECK: _0 = const 0_u32; + // CHECK: [[BB2]]: + // CHECK: _0 = const 1_u32; if x == 42 { 0 } else { 1 } } +// EMIT_MIR if_condition_int.dont_opt_bool.SimplifyComparisonIntegral.diff // don't opt: it is already optimal to switch on the bool fn dont_opt_bool(x: bool) -> u32 { + // CHECK-LABEL: fn dont_opt_bool( + // CHECK: switchInt(copy _1) -> [0: [[BB2:bb.*]], otherwise: [[BB1:bb.*]]]; + // CHECK: [[BB1]]: + // CHECK: _0 = const 0_u32; + // CHECK: [[BB2]]: + // CHECK: _0 = const 1_u32; if x { 0 } else { 1 } } +// EMIT_MIR if_condition_int.opt_char.SimplifyComparisonIntegral.diff fn opt_char(x: char) -> u32 { + // CHECK-LABEL: fn opt_char( + // CHECK: switchInt(move _1) -> [120: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; + // CHECK: [[BB1]]: + // CHECK: _0 = const 0_u32; + // CHECK: [[BB2]]: + // CHECK: _0 = const 1_u32; if x == 'x' { 0 } else { 1 } } +// EMIT_MIR if_condition_int.opt_i8.SimplifyComparisonIntegral.diff fn opt_i8(x: i8) -> u32 { + // CHECK-LABEL: fn opt_i8( + // CHECK: switchInt(move _1) -> [42: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; + // CHECK: [[BB1]]: + // CHECK: _0 = const 0_u32; + // CHECK: [[BB2]]: + // CHECK: _0 = const 1_u32; if x == 42 { 0 } else { 1 } } +// EMIT_MIR if_condition_int.opt_negative.SimplifyComparisonIntegral.diff fn opt_negative(x: i32) -> u32 { + // CHECK-LABEL: fn opt_negative( + // CHECK: switchInt(move _1) -> [4294967254: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; + // CHECK: [[BB1]]: + // CHECK: _0 = const 0_u32; + // CHECK: [[BB2]]: + // CHECK: _0 = const 1_u32; if x == -42 { 0 } else { 1 } } +// EMIT_MIR if_condition_int.opt_multiple_ifs.SimplifyComparisonIntegral.diff fn opt_multiple_ifs(x: u32) -> u32 { + // CHECK-LABEL: fn opt_multiple_ifs( + // CHECK: switchInt(move _1) -> [42: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; + // CHECK: [[BB1]]: + // CHECK: _0 = const 0_u32; + // CHECK: [[BB2]]: + // CHECK: switchInt(move _1) -> [21: [[BB4:bb.*]], otherwise: [[BB3:bb.*]]]; + // CHECK: [[BB3]]: + // CHECK: _0 = const 1_u32; + // CHECK: [[BB4]]: + // CHECK: _0 = const 2_u32; if x == 42 { 0 } else if x != 21 { @@ -40,8 +80,18 @@ fn opt_multiple_ifs(x: u32) -> u32 { } } +// EMIT_MIR if_condition_int.dont_remove_comparison.SimplifyComparisonIntegral.diff // test that we optimize, but do not remove the b statement, as that is used later on fn dont_remove_comparison(a: i8) -> i32 { + // CHECK-LABEL: fn dont_remove_comparison( + // CHECK: [[b:_.*]] = Eq(copy _1, const 17_i8); + // CHECK: switchInt(move _1) -> [17: [[BB1:bb.*]], otherwise: [[BB2:bb.*]]]; + // CHECK: [[BB1]]: + // CHECK: [[cast_1:_.*]] = copy [[b]] as i32 (IntToInt); + // CHECK: _0 = Add(const 100_i32, move [[cast_1]]); + // CHECK: [[BB2]]: + // CHECK: [[cast_2:_.*]] = copy [[b]] as i32 (IntToInt); + // CHECK: _0 = Add(const 10_i32, move [[cast_2]]); let b = a == 17; match b { false => 10 + b as i32, @@ -49,8 +99,16 @@ fn dont_remove_comparison(a: i8) -> i32 { } } +// EMIT_MIR if_condition_int.dont_opt_floats.SimplifyComparisonIntegral.diff // test that we do not optimize on floats fn dont_opt_floats(a: f32) -> i32 { + // CHECK-LABEL: fn dont_opt_floats( + // CHECK: [[cmp:_.*]] = Eq(copy _1, const -42f32); + // CHECK: switchInt(move [[cmp]]) -> [0: [[BB2:bb.*]], otherwise: [[BB1:bb.*]]]; + // CHECK: [[BB1]]: + // CHECK: _0 = const 0_i32; + // CHECK: [[BB2]]: + // CHECK: _0 = const 1_i32; if a == -42.0 { 0 } else { 1 } }