Skip to content

Commit 0eec4ba

Browse files
committed
test that the phi node got eliminated
1 parent 9a03f37 commit 0eec4ba

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

library/alloc/src/slice.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ pub(crate) mod hack {
160160
impl<T: Copy> ConvertVec for T {
161161
#[inline]
162162
fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> {
163+
if s.is_empty() {
164+
// The early return is not necessary for correctness, but it helps
165+
// LLVM by avoiding phi nodes flowing into memcpy.
166+
// See codegen/lib-optimizations/append-elements.rs
167+
return Vec::new_in(alloc);
168+
}
163169
let mut v = Vec::with_capacity_in(s.len(), alloc);
164170
// SAFETY:
165171
// allocated above with the capacity of `s`, and initialize to `s.len()` in

library/alloc/src/vec/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,11 +2520,9 @@ impl<T, A: Allocator> Vec<T, A> {
25202520
unsafe fn append_elements(&mut self, other: *const [T]) {
25212521
let count = unsafe { (*other).len() };
25222522
if count == 0 {
2523-
// The early return is not necessary for correctness, but in cases
2524-
// where LLVM sees all the way to the allocation site of `other`
2525-
// this can avoid a phi-node merging the two different pointers
2526-
// when zero-length allocations are special-cased.
2527-
// That in turn can enable more optimizations around the memcpy below.
2523+
// The early return is not necessary for correctness, but it helps
2524+
// LLVM by avoiding phi nodes flowing into memcpy.
2525+
// See codegen/lib-optimizations/append-elements.rs
25282526
return;
25292527
}
25302528
self.reserve(count);

0 commit comments

Comments
 (0)