Skip to content
/ rust Public
forked from rust-lang/rust

Commit d9fa6d2

Browse files
committed
test that Range loops optimize away
but RangeInclusive loops do not
1 parent 8d6b380 commit d9fa6d2

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// This test ensures that Range iterators are optimizable, to
2+
// the point that some loops can be entirely optimized out.
3+
4+
//@ compile-flags: -Copt-level=3
5+
6+
#![crate_type = "lib"]
7+
8+
use std::ops::{Range, RangeInclusive};
9+
10+
// CHECK-LABEL: @range_noop_loop(
11+
#[no_mangle]
12+
pub unsafe fn range_noop_loop() {
13+
// CHECK-NEXT: start:
14+
// CHECK-NEXT: ret void
15+
16+
// This loop should be optimized out entirely.
17+
for _ in 0_u8..100 {
18+
()
19+
}
20+
}
21+
22+
// CHECK-LABEL: @range_count(
23+
#[no_mangle]
24+
pub unsafe fn range_count(s: u8, e: u8) -> usize {
25+
// CHECK-NOT: br {{.*}}
26+
// CHECK: ret i64
27+
28+
// This loop should be optimized to arithmetic.
29+
let mut count = 0;
30+
for _ in s..e {
31+
count += 1;
32+
}
33+
count
34+
}
35+
36+
// RangeInclusive currently cannot optimize the same way.
37+
38+
// CHECK-LABEL: @rangeinclusive_noop_loop(
39+
#[no_mangle]
40+
pub unsafe fn rangeinclusive_noop_loop() {
41+
// CHECK: br {{.*}}
42+
// CHECK: ret void
43+
44+
for _ in 0_u8..=100 {
45+
()
46+
}
47+
}
48+
49+
// CHECK-LABEL: @rangeinclusive_count(
50+
#[no_mangle]
51+
pub unsafe fn rangeinclusive_count(s: u8, e: u8) -> usize {
52+
// CHECK: br {{.*}}
53+
// CHECK: ret i64
54+
55+
let mut count = 0;
56+
for _ in s..=e {
57+
count += 1;
58+
}
59+
count
60+
}

0 commit comments

Comments
 (0)