Skip to content

Commit b15bb6d

Browse files
committed
Add some constant propagation tests
1 parent efbea54 commit b15bb6d

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

src/test/mir-opt/const-prop-1.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
fn main() {
11+
let x = 10;
12+
let y = 20;
13+
if x == 12 {
14+
[42u8][y];
15+
} else {
16+
return;
17+
}
18+
}
19+
20+
// END RUST SOURCE
21+
// START rustc.node4.CsPropagate.before.mir
22+
// bb0: {
23+
// var0 = const 10i32;
24+
// var1 = const 20usize;
25+
// tmp1 = var0;
26+
// tmp0 = Eq(tmp1, const 12i32);
27+
// if(tmp0) -> [true: bb1, false: bb2];
28+
// }
29+
//
30+
// bb1: {
31+
// tmp4 = [const 42u8];
32+
// tmp5 = var1;
33+
// tmp6 = Len(tmp4);
34+
// tmp7 = Lt(tmp5, tmp6);
35+
// assert(tmp7, "index out of bounds: the len is {} but the index is {}", tmp6, tmp5) -> bb3;
36+
// }
37+
//
38+
// bb2: {
39+
// return = ();
40+
// goto -> bb4;
41+
// }
42+
//
43+
// bb3: {
44+
// tmp3 = tmp4[tmp5];
45+
// tmp2 = tmp3;
46+
// return = ();
47+
// goto -> bb4;
48+
// }
49+
// END rustc.node4.CsPropagate.before.mir
50+
// START rustc.node4.DeadCode.after.mir
51+
// bb0: {
52+
// goto -> bb1;
53+
// }
54+
//
55+
// bb1: {
56+
// return = ();
57+
// return;
58+
// }
59+
// END rustc.node4.DeadCode.after.mir

src/test/mir-opt/const-prop-2.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
fn main() {
11+
let cfg = cfg!(never_ever_set);
12+
let x = 42 + if cfg { 42 } else { 0 };
13+
::std::process::exit(x - 42);
14+
}
15+
16+
// END RUST SOURCE
17+
// START rustc.node4.CsPropagate.before.mir
18+
// bb0: {
19+
// var0 = const false;
20+
// tmp1 = var0;
21+
// if(tmp1) -> [true: bb1, false: bb2];
22+
// }
23+
//
24+
// bb1: {
25+
// tmp0 = const 42i32;
26+
// goto -> bb3;
27+
// }
28+
//
29+
// bb2: {
30+
// tmp0 = const 0i32;
31+
// goto -> bb3;
32+
// }
33+
//
34+
// bb3: {
35+
// var1 = Add(const 42i32, tmp0);
36+
// tmp4 = var1;
37+
// tmp3 = Sub(tmp4, const 42i32);
38+
// std::process::exit(tmp3);
39+
// }
40+
// END rustc.node4.CsPropagate.before.mir
41+
// START rustc.node4.DeadCode.after.mir
42+
// bb1: {
43+
// std::process::exit(const 0i32);
44+
// }
45+
// END rustc.node4.DeadCode.after.mir

src/test/mir-opt/const-prop-loop-1.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
fn hdrty() -> usize { 6 }
11+
12+
fn inc(idx: u16) -> usize { 1 }
13+
14+
fn main() {
15+
let hdrty = hdrty();
16+
let max = match hdrty {
17+
0 => 6,
18+
1 => 2,
19+
_ => 0,
20+
};
21+
let mut i = 0;
22+
while i < max {
23+
let next = inc(i as u16);
24+
i = next;
25+
}
26+
}
27+
28+
// END RUST SOURCE
29+
// START rustc.node17.CsPropagate.before.mir
30+
// bb6: {
31+
// tmp1 = var2;
32+
// tmp2 = var1;
33+
// tmp0 = Lt(tmp1, tmp2);
34+
// if(tmp0) -> [true: bb8, false: bb7];
35+
// }
36+
//
37+
// bb7: {
38+
// return = ();
39+
// return;
40+
// }
41+
//
42+
// bb8: {
43+
// tmp5 = var2;
44+
// tmp4 = tmp5 as u16 (Misc);
45+
// var3 = inc(tmp4) -> bb9;
46+
// }
47+
//
48+
// bb9: {
49+
// tmp6 = var3;
50+
// var2 = tmp6;
51+
// tmp3 = ();
52+
// goto -> bb6;
53+
// }
54+
// END rustc.node17.CsPropagate.before.mir
55+
// START rustc.node17.CsPropagate.after.mir
56+
// bb6: {
57+
// tmp1 = var2;
58+
// tmp2 = var1;
59+
// tmp0 = Lt(tmp1, tmp2);
60+
// if(tmp0) -> [true: bb8, false: bb7];
61+
// }
62+
//
63+
// bb7: {
64+
// return = ();
65+
// return;
66+
// }
67+
//
68+
// bb8: {
69+
// tmp5 = var2;
70+
// tmp4 = tmp5 as u16 (Misc);
71+
// var3 = inc(tmp4) -> bb9;
72+
// }
73+
//
74+
// bb9: {
75+
// tmp6 = var3;
76+
// var2 = tmp6;
77+
// tmp3 = ();
78+
// goto -> bb6;
79+
// }
80+
// END rustc.node17.CsPropagate.after.mir

0 commit comments

Comments
 (0)