]> git.lizzy.rs Git - rust.git/blob - tests/codegen/iter-repeat-n-trivial-drop.rs
Rollup merge of #106446 - bzEq:fix-unwind-lsda, r=Amanieu
[rust.git] / tests / codegen / iter-repeat-n-trivial-drop.rs
1 // compile-flags: -O
2 // only-x86_64
3 // ignore-debug: the debug assertions get in the way
4
5 #![crate_type = "lib"]
6 #![feature(iter_repeat_n)]
7
8 #[derive(Clone)]
9 pub struct NotCopy(u16);
10
11 impl Drop for NotCopy {
12     fn drop(&mut self) {}
13 }
14
15 // For a type where `Drop::drop` doesn't do anything observable and a clone is the
16 // same as a move, make sure that the extra case for the last item disappears.
17
18 #[no_mangle]
19 // CHECK-LABEL: @iter_repeat_n_next
20 pub fn iter_repeat_n_next(it: &mut std::iter::RepeatN<NotCopy>) -> Option<NotCopy> {
21     // CHECK-NEXT: start:
22     // CHECK-NOT: br
23     // CHECK: %[[COUNT:.+]] = load i64
24     // CHECK-NEXT: %[[COUNT_ZERO:.+]] = icmp eq i64 %[[COUNT]], 0
25     // CHECK-NEXT: br i1 %[[COUNT_ZERO]], label %[[EMPTY:.+]], label %[[NOT_EMPTY:.+]]
26
27     // CHECK: [[NOT_EMPTY]]:
28     // CHECK-NEXT: %[[DEC:.+]] = add i64 %[[COUNT]], -1
29     // CHECK-NEXT: store i64 %[[DEC]]
30     // CHECK-NOT: br
31     // CHECK: %[[VAL:.+]] = load i16
32     // CHECK-NEXT: br label %[[EMPTY]]
33
34     // CHECK: [[EMPTY]]:
35     // CHECK-NOT: br
36     // CHECK: phi i16 [ undef, %start ], [ %[[VAL]], %[[NOT_EMPTY]] ]
37     // CHECK-NOT: br
38     // CHECK: ret
39
40     it.next()
41 }
42
43 // And as a result, using the iterator can optimize without special cases for
44 // the last iteration, like `memset`ing all the items in one call.
45
46 #[no_mangle]
47 // CHECK-LABEL: @vec_extend_via_iter_repeat_n
48 pub fn vec_extend_via_iter_repeat_n() -> Vec<u8> {
49     // CHECK: %[[ADDR:.+]] = tail call dereferenceable_or_null(1234) ptr @__rust_alloc(i64 1234, i64 1)
50     // CHECK: tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(1234) %[[ADDR]], i8 42, i64 1234,
51
52     let n = 1234_usize;
53     let mut v = Vec::with_capacity(n);
54     v.extend(std::iter::repeat_n(42_u8, n));
55     v
56 }