]> git.lizzy.rs Git - rust.git/blob - src/test/codegen/issue-45222.rs
Auto merge of #63521 - newpavlov:redox_builder, r=pietroalbini
[rust.git] / src / test / codegen / issue-45222.rs
1 // compile-flags: -O
2 // ignore-debug: the debug assertions get in the way
3
4 #![crate_type = "lib"]
5
6 // verify that LLVM recognizes a loop involving 0..=n and will const-fold it.
7
8 // Example from original issue #45222
9
10 fn foo2(n: u64) -> u64 {
11     let mut count = 0;
12     for _ in 0..n {
13         for j in (0..=n).rev() {
14             count += j;
15         }
16     }
17     count
18 }
19
20 // CHECK-LABEL: @check_foo2
21 #[no_mangle]
22 pub fn check_foo2() -> u64 {
23     // CHECK: ret i64 500005000000000
24     foo2(100000)
25 }
26
27 // Simplified example of #45222
28
29 fn triangle_inc(n: u64) -> u64 {
30     let mut count = 0;
31     for j in 0 ..= n {
32         count += j;
33     }
34     count
35 }
36
37 // CHECK-LABEL: @check_triangle_inc
38 #[no_mangle]
39 pub fn check_triangle_inc() -> u64 {
40     // CHECK: ret i64 5000050000
41     triangle_inc(100000)
42 }
43
44 // Demo in #48012
45
46 fn foo3r(n: u64) -> u64 {
47     let mut count = 0;
48     (0..n).for_each(|_| {
49         (0 ..= n).rev().for_each(|j| {
50             count += j;
51         })
52     });
53     count
54 }
55
56 // CHECK-LABEL: @check_foo3r
57 #[no_mangle]
58 pub fn check_foo3r() -> u64 {
59     // CHECK: ret i64 500050000000
60     foo3r(10000)
61 }