https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122727

--- Comment #6 from M Welinder <terra at gnome dot org> ---
I don't think sorting out the pointer aliases is going to do much here.  To see
that, let's side step it completely and consider the code you would have if you
somehow managed to optimize the temporaries away:

std::string
moo(std::string const &s, char c, std::string_view sv)
{
    std::string res;
    res.reserve(s.size() + 1 + sv.size());
    res += s;
    res += c;
    res += sv;
    return res;
}

https://godbolt.org/z/jac6xcWon


This computes the total size needed:
     lea     rbp, [r13+1+rcx]
If that sum is <=15, it then goes on to consider many cases for
r13 (aka s.size()): r13>=16 [pointless], r13==0, r13==1 [?], and r13 in 2-15.

The big fish here is the >=16 case.  We as humans know it cannot happen without
throwing length_error somewhere, probably in the last "+=".  But the length
addition is unsigned, so overflow there is not by itself undefined.  I don't
see a path to deducing that >=16 is impossible once the code has been
generated, but perhaps you do.

We must eliminate the >=16 case and several like it because that is required to
deduce that none of the += operations need to increase capacity.

That too-long verbiage was just to argue why I think the key here is to get the
front end to generate better code, even if that requires the front end to know
what std::string is and does.

Reply via email to