]> git.lizzy.rs Git - rust.git/blob - src/test/codegen/issue-45222.rs
Rollup merge of #62666 - estebank:preempt-ice, r=eddyb
[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 //------------------------------------------------------------------------------
9 // Example from original issue #45222
10
11 fn foo2(n: u64) -> u64 {
12     let mut count = 0;
13     for _ in 0..n {
14         for j in (0..=n).rev() {
15             count += j;
16         }
17     }
18     count
19 }
20
21 // CHECK-LABEL: @check_foo2
22 #[no_mangle]
23 pub fn check_foo2() -> u64 {
24     // CHECK: ret i64 500005000000000
25     foo2(100000)
26 }
27
28 //------------------------------------------------------------------------------
29 // Simplified example of #45222
30
31 fn triangle_inc(n: u64) -> u64 {
32     let mut count = 0;
33     for j in 0 ..= n {
34         count += j;
35     }
36     count
37 }
38
39 // CHECK-LABEL: @check_triangle_inc
40 #[no_mangle]
41 pub fn check_triangle_inc() -> u64 {
42     // CHECK: ret i64 5000050000
43     triangle_inc(100000)
44 }
45
46 //------------------------------------------------------------------------------
47 // Demo in #48012
48
49 fn foo3r(n: u64) -> u64 {
50     let mut count = 0;
51     (0..n).for_each(|_| {
52         (0 ..= n).rev().for_each(|j| {
53             count += j;
54         })
55     });
56     count
57 }
58
59 // CHECK-LABEL: @check_foo3r
60 #[no_mangle]
61 pub fn check_foo3r() -> u64 {
62     // CHECK: ret i64 500050000000
63     foo3r(10000)
64 }