Add FileCheck to if_condition_int.rs

This commit is contained in:
dianqk 2026-01-10 18:34:02 +08:00
parent f57eac1bf9
commit 5ee964049a
No known key found for this signature in database
9 changed files with 88 additions and 34 deletions

View file

@ -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: {

View file

@ -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];
}

View file

@ -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;
}
}

View file

@ -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: {

View file

@ -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: {

View file

@ -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: {

View file

@ -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: {

View file

@ -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: {

View file

@ -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 }
}