summaryrefslogtreecommitdiffstats
path: root/tests/mir-opt/if_condition_int.rs
blob: a3dd74d9a37969f4622d0f08153d33f088f907f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// skip-filecheck
// 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);
}