Issue 95987
Summary Failure to remove `alloca`
Labels missed-optimization
Assignees
Reporter Kmeakin
    `str::starts_with(char)` and `str::ends_with(char)` converts the 4-byte character codepoint into a 1-4 byte buffer on the stack, then compares against the start/end of the `str`'s bytes contents. LLVM is able to remove the stack allocation when the `char` is a single byte, but if it is 2 or more, it is not able:

https://godbolt.org/z/zjhanPx7j
```rust
#[no_mangle]
pub fn starts_with_char1(s: &str) -> bool {
    s.starts_with('a')
}

#[no_mangle]
pub fn starts_with_str1(s: &str) -> bool {
    s.starts_with("a")
}

#[no_mangle]
pub fn starts_with_char2(s: &str) -> bool {
    s.starts_with('£')
}

#[no_mangle]
pub fn starts_with_str2(s: &str) -> bool {
    s.ends_with("£")
}

#[no_mangle]
pub fn ends_with_char1(s: &str) -> bool {
    s.ends_with('a')
}

#[no_mangle]
pub fn ends_with_str1(s: &str) -> bool {
    s.ends_with("a")
}

#[no_mangle]
pub fn ends_with_char2(s: &str) -> bool {
    s.ends_with('£')
}

#[no_mangle]
pub fn ends_with_str2(s: &str) -> bool {
    s.ends_with("£")
}
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to