<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/66953>66953</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Clang does not optimize out _LIBCPP_ASSERT_NON_OVERLAPPING_RANGES from libc++
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang,
            libc++
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          davidben
      </td>
    </tr>
</table>

<pre>
    libc++ contains assertions of the following form:
https://github.com/llvm/llvm-project/blob/933ad5c897ee366759a54869b35b2d7285a92137/libcxx/include/__string/char_traits.h#L239-L246

These are enabled with `_LIBCPP_ENABLE_SAFE_MODE` and are, among other places, used when constructing `std::string`s from string literals:
https://github.com/llvm/llvm-project/blob/933ad5c897ee366759a54869b35b2d7285a92137/libcxx/include/string#L940
https://github.com/llvm/llvm-project/blob/933ad5c897ee366759a54869b35b2d7285a92137/libcxx/include/string#L2168

Looking at the disassembly of some code in Chromium, this does not seem to get optimized out, even when all we're doing is constructing a `std::string` from a string literal. The compiler should know that string literals cannot alias with much of anything.

I put together this minimal program in godbolt and it seems, even with `-O3`, Clang (and GCC) do not optimize these checks out. That suggests Clang (and GCC) just don't know how to optimize this pattern? Separately, I'm not sure if the check is correct. See the comment about the "fixed" versions.
https://godbolt.org/z/oef93T9xb
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMlMtu47gShp-G3hRiyNTF1kILx7GCAD5OEAdna1BkWWSHIg2ylEs__YBWp5NpZDOrmY0EUlSx6vv_KhGj6R1iw8prVt7MxEjah0aJF6M6dLPOq_fGmk4yfs34NUjvSBgXQcSIgYx3EfwJSCOcvLX-1bgeTj4MLF-z7IZla010jmnFW8bb3pAeu7n0A-OttS8fr6tz8D9QEuNtZ33HeFvnuVClXNVLxLyqlmUtymJV1V1edlwt-aoUNV_kyxTAdPLtjfHWO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