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