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

    <tr>
        <th>Summary</th>
        <td>
            Redundant pointer comparisons after arithmetic not deleted
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    I noticed this as I was exploring the compiler output for some of our (Chromium's) checked iterators. To be honest, I'm not sure this check makes sense, since we should just assume the container is self-consistent. But, nonetheless, I think Clang could have deleted this check, so I'm filing this as a missed optimization.

Reproducer: https://godbolt.org/z/EzoWqGEEK

This code ends up performing a few variations on checking that `data_ <= data_ + size_`, where `size_` is a `size_t`. Of course, the real reason this is always valid is that `[data_, data_ + size_)` describes a span, but the compiler doesn't a priori know that. However, I believe it can infer this because:

1. `size_` is unsigned, so we're not adding a negative number to `data_`
2. Pointer arithmetic is required to stay in bounds, and is not defined to cast to `ptrdiff_t` first: https://eel.is/c++draft/expr.add#4.2
3. Therefore, the addition cannot have overflowed and `data_ <= data_ + size_` is always true in all defined cases
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVM1u4zgTfBr60ogg0z-KDz7ESfx9wR52MQiwx0FLbFk9oUgNm7QnefoFKSebzR72IoM0m1VdVWwU4ZMj2qvNQW0eFpji4MPe4JlNS27RevO6fwLnI3dkIA4sgAJPcEEB-jVZH9idIA4EnR8nthTApzilCL0PIH4k8D34FEDp2_sh-JHTqHQjSu-gG6h7IQMcKWD0QSp49tASDN6RRKXv4UnpZsz4ICnQTKCUwYgvJCDkhPJBYdcRXAhk8Mka-JEkAoqkka70XER2FIBzle1vOu-EJZKLFRxSQXPeURzIkkgBz3juBe4tuhN05d4BzwSGLMV3PQqdQsFf6fZsZ1VmtRBGFiEDfoo88htG9q5S9YOq7-bvN5qCN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