summaryrefslogtreecommitdiffstats
path: root/tests/mir-opt/if_condition_int.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
commit218caa410aa38c29984be31a5229b9fa717560ee (patch)
treec54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/mir-opt/if_condition_int.rs
parentReleasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz
rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/mir-opt/if_condition_int.rs')
-rw-r--r--tests/mir-opt/if_condition_int.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/mir-opt/if_condition_int.rs b/tests/mir-opt/if_condition_int.rs
new file mode 100644
index 000000000..398311e6b
--- /dev/null
+++ b/tests/mir-opt/if_condition_int.rs
@@ -0,0 +1,65 @@
+// unit-test: 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
+
+fn opt_u32(x: u32) -> u32 {
+ if x == 42 { 0 } else { 1 }
+}
+
+// don't opt: it is already optimal to switch on the bool
+fn dont_opt_bool(x: bool) -> u32 {
+ if x { 0 } else { 1 }
+}
+
+fn opt_char(x: char) -> u32 {
+ if x == 'x' { 0 } else { 1 }
+}
+
+fn opt_i8(x: i8) -> u32 {
+ if x == 42 { 0 } else { 1 }
+}
+
+fn opt_negative(x: i32) -> u32 {
+ if x == -42 { 0 } else { 1 }
+}
+
+fn opt_multiple_ifs(x: u32) -> u32 {
+ if x == 42 {
+ 0
+ } else if x != 21 {
+ 1
+ } else {
+ 2
+ }
+}
+
+// test that we optimize, but do not remove the b statement, as that is used later on
+fn dont_remove_comparison(a: i8) -> i32 {
+ let b = a == 17;
+ match b {
+ false => 10 + b as i32,
+ true => 100 + b as i32,
+ }
+}
+
+// test that we do not optimize on floats
+fn dont_opt_floats(a: f32) -> i32 {
+ if a == -42.0 { 0 } else { 1 }
+}
+
+fn main() {
+ opt_u32(0);
+ opt_char('0');
+ opt_i8(22);
+ dont_opt_bool(false);
+ opt_negative(0);
+ opt_multiple_ifs(0);
+ dont_remove_comparison(11);
+ dont_opt_floats(1.0);
+}