https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87502
--- Comment #12 from Jan Hubicka <hubicka at ucw dot cz> --- > (In reply to Jakub Jelinek from comment #10) > > __builtin_memcpy (&D.35539.D.25336._M_local_buf, "abc", 3); > > MEM[(char_type &)&D.35539 + 11] = 0; > > change to > > __builtin_memcpy (&D.35539.D.25336._M_local_buf, "abc", 4); > > seems like something that the strlen pass could do. > > That is very similar to PR 92245 and PR 93558 and PR 91019. I agree that this would be useful. However std::string has one extra invariant that the end pointer always points to 0. This lets us to optimize this even when source is not known constant string, but another std::string. This is probably something we can not easily expose to back-end. Technically we may do that using __builtin_unreachable but then we would need to pattern match it somewehre in the middle-end. It seems that updating std::string to use 0 termination in common scenarios is not too hard to do, so perhaps we could do both (extend strlen pass and optimize std::string implementation).