summaryrefslogtreecommitdiffstats
path: root/tests/mir-opt/gvn.rs
blob: a85e2ae368b41a0d6fcb65f2a08b4f9d0b752475 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// unit-test: GVN
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY

#![feature(raw_ref_op)]
#![feature(rustc_attrs)]
#![allow(unconditional_panic)]

struct S<T>(T);

fn subexpression_elimination(x: u64, y: u64, mut z: u64) {
    opaque(x + y);
    opaque(x * y);
    opaque(x - y);
    opaque(x / y);
    opaque(x % y);
    opaque(x & y);
    opaque(x | y);
    opaque(x ^ y);
    opaque(x << y);
    opaque(x >> y);
    opaque(x as u32);
    opaque(x as f32);
    opaque(S(x));
    opaque(S(x).0);

    // Those are duplicates to substitute somehow.
    opaque((x + y) + z);
    opaque((x * y) + z);
    opaque((x - y) + z);
    opaque((x / y) + z);
    opaque((x % y) + z);
    opaque((x & y) + z);
    opaque((x | y) + z);
    opaque((x ^ y) + z);
    opaque((x << y) + z);
    opaque((x >> y) + z);
    opaque(S(x));
    opaque(S(x).0);

    // We can substitute through an immutable reference too.
    let a = &z;
    opaque(*a + x);
    opaque(*a + x);

    // But not through a mutable reference or a pointer.
    let b = &mut z;
    opaque(*b + x);
    opaque(*b + x);
    unsafe {
        let c = &raw const z;
        opaque(*c + x);
        opaque(*c + x);
        let d = &raw mut z;
        opaque(*d + x);
        opaque(*d + x);
    }

    // We can substitute again, but not with the earlier computations.
    // Important: `e` is not `a`!
    let e = &z;
    opaque(*e + x);
    opaque(*e + x);

}

fn wrap_unwrap<T: Copy>(x: T) -> T {
    match Some(x) {
        Some(y) => y,
        None => panic!(),
    }
}

fn repeated_index<T: Copy, const N: usize>(x: T, idx: usize) {
    let a = [x; N];
    opaque(a[0]);
    opaque(a[idx]);
}

fn arithmetic(x: u64) {
    opaque(x + 0);
    opaque(x - 0);
    opaque(x * 0);
    opaque(x * 1);
    opaque(x / 0);
    opaque(x / 1);
    opaque(0 / x);
    opaque(1 / x);
    opaque(x % 0);
    opaque(x % 1);
    opaque(0 % x);
    opaque(1 % x);
    opaque(x & 0);
    opaque(x | 0);
    opaque(x ^ 0);
    opaque(x >> 0);
    opaque(x << 0);
}

#[rustc_inherit_overflow_checks]
fn arithmetic_checked(x: u64) {
    opaque(x + 0);
    opaque(x - 0);
    opaque(x * 0);
    opaque(x * 1);
    opaque(x / 0);
    opaque(x / 1);
    opaque(0 / x);
    opaque(1 / x);
    opaque(x % 0);
    opaque(x % 1);
    opaque(0 % x);
    opaque(1 % x);
    opaque(x & 0);
    opaque(x | 0);
    opaque(x ^ 0);
    opaque(x >> 0);
    opaque(x << 0);
}

fn arithmetic_float(x: f64) {
    opaque(x + 0.);
    opaque(x - 0.);
    opaque(x * 0.);
    opaque(x / 0.);
    opaque(0. / x);
    opaque(x % 0.);
    opaque(0. % x);
    // Those are not simplifiable to `true`/`false`, thanks to NaNs.
    opaque(x == x);
    opaque(x != x);
}

fn cast() {
    let i = 1_i64;
    let u = 1_u64;
    let f = 1_f64;
    opaque(i as u8);
    opaque(i as u16);
    opaque(i as u32);
    opaque(i as u64);
    opaque(i as i8);
    opaque(i as i16);
    opaque(i as i32);
    opaque(i as i64);
    opaque(i as f32);
    opaque(i as f64);
    opaque(u as u8);
    opaque(u as u16);
    opaque(u as u32);
    opaque(u as u64);
    opaque(u as i8);
    opaque(u as i16);
    opaque(u as i32);
    opaque(u as i64);
    opaque(u as f32);
    opaque(u as f64);
    opaque(f as u8);
    opaque(f as u16);
    opaque(f as u32);
    opaque(f as u64);
    opaque(f as i8);
    opaque(f as i16);
    opaque(f as i32);
    opaque(f as i64);
    opaque(f as f32);
    opaque(f as f64);
}

fn multiple_branches(t: bool, x: u8, y: u8) {
    if t {
        opaque(x + y); // a
        opaque(x + y); // should reuse a
    } else {
        opaque(x + y); // b
        opaque(x + y); // shoud reuse b
    }
    opaque(x + y); // c
    if t {
        opaque(x + y); // should reuse c
    } else {
        opaque(x + y); // should reuse c
    }
}

fn references(mut x: impl Sized) {
    opaque(&x);
    opaque(&x); // should not reuse a
    opaque(&mut x);
    opaque(&mut x); // should not reuse a
    opaque(&raw const x);
    opaque(&raw const x); // should not reuse a
    opaque(&raw mut x);
    opaque(&raw mut x); // should not reuse a
}

fn dereferences(t: &mut u32, u: &impl Copy, s: &S<u32>) {
    opaque(*t);
    opaque(*t); // this cannot reuse a, as x is &mut.
    let z = &raw const *t;
    unsafe { opaque(*z) };
    unsafe { opaque(*z) }; // this cannot reuse a, as x is *const.
    let z = &raw mut *t;
    unsafe { opaque(*z) };
    unsafe { opaque(*z) }; // this cannot reuse a, as x is *mut.
    let z = &*t;
    opaque(*z);
    opaque(*z); // this can reuse, as `z` is immutable ref, Freeze and Copy.
    opaque(&*z); // but not for a reborrow.
    opaque(*u);
    opaque(*u); // this cannot reuse, as `z` is not Freeze.
    opaque(s.0);
    opaque(s.0); // *s is not Copy, by (*s).0 is, so we can reuse.
}

fn slices() {
    let s = "my favourite slice"; // This is a `Const::Slice` in MIR.
    opaque(s);
    let t = s; // This should be the same pointer, so cannot be a `Const::Slice`.
    opaque(t);
    assert_eq!(s.as_ptr(), t.as_ptr());
    let u = unsafe { std::mem::transmute::<&str, &[u8]>(s) };
    opaque(u);
    assert_eq!(s.as_ptr(), u.as_ptr());
}

fn main() {
    subexpression_elimination(2, 4, 5);
    wrap_unwrap(5);
    repeated_index::<u32, 7>(5, 3);
    arithmetic(5);
    arithmetic_checked(5);
    arithmetic_float(5.);
    cast();
    multiple_branches(true, 5, 9);
    references(5);
    dereferences(&mut 5, &6, &S(7));
    slices();
}

#[inline(never)]
fn opaque(_: impl Sized) {}

// EMIT_MIR gvn.subexpression_elimination.GVN.diff
// EMIT_MIR gvn.wrap_unwrap.GVN.diff
// EMIT_MIR gvn.repeated_index.GVN.diff
// EMIT_MIR gvn.arithmetic.GVN.diff
// EMIT_MIR gvn.arithmetic_checked.GVN.diff
// EMIT_MIR gvn.arithmetic_float.GVN.diff
// EMIT_MIR gvn.cast.GVN.diff
// EMIT_MIR gvn.multiple_branches.GVN.diff
// EMIT_MIR gvn.references.GVN.diff
// EMIT_MIR gvn.dereferences.GVN.diff
// EMIT_MIR gvn.slices.GVN.diff