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

    <tr>
        <th>Summary</th>
        <td>
            Loop invariant is not deduced in C++-iterator-style loop over pointers
        </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>
    In the following loop, the compiler should be able to optimize out the assert.

```
struct SpanAsPointers {
 int *begin, *end;
};

int sum_span_as_pointers(SpanAsPointers s) {
    int *begin = s.begin;
    int *end = s.end;
 __builtin_assume(begin <= end);

    int sum = 0;
    int *iter = begin;
    while (iter != end) {
        assert(iter < end);
 sum += *iter;
        iter++;
    }
    return sum;
}
```

See this link for a runnable example, and a longer discussion on why the invariant is true: https://godbolt.org/z/ad5P4d5M5

Also in that link is something interesting: Clang _does_ figure out the invariant when we use integers instead of pointers! It just doesn't apply the same analysis to pointers for some reason.

This is the missing compiler piece needed to solve #101370.

(CC @ldionne @var-const @danakj)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxsVNFu4joQ_RrzMgIlDjTwkAdKhbTSvdJKe9-REw-Ju44decZwu1-_ssmW0i1CThyPzznjOWNFZHqH2IjNs9i8LFTkwYdGq4vRLbpF6_Vb880BDwhnb62_GteD9X4S8pC_dn6cjMUANPhoNbQIqrUI7MFPbEbzC8FHzrGKCAOvRPEiiv08PhXzP0-JQ-wYfkzK7em7N44xEIj6-bYMxjEIuW-xNy4pEHKPTotqXhf1y_09j2kDxfFEk3InRadpxhRy-4mEhNx9YAJ4IANRvQCtbsTV30Ho9BzyUQ-cTm00lk3ipjiikNs_cIcUnoLl7pPmP7AUx4xZfEVoGENe_VvSdTAWQcjtLUaWd6bHBNPvVpP34OrwWdNNhnxOIDPvA1vWlD7K5xz1YSmV430SkGNwCe2hXF-54Db-QAQeDIE17iecfQAFITqX7YX_q3GymDygnAYF1rseA2hDXSQy3oF3cB3esvGMu6hglGMwBBwiimoPA_NEotoLeRTy2HvdessrH3ohj7-EPCq9-b7Wm383H0XtLXkwqSEU34QZAvIj8pA6I3sJiY3rE8XBKtfDSXukE5xNH8O9Ge6argM6uCJEwgzQJzsaR4xKgz_D3bMlfGN4jcSQIJ2QNYOaJnvLktSIoJyyb5TS9O8b8-ElkRBQkXcPHfhfOuIUPyCMhiil8d7Uk8EOwSFq1AmRvL0ka1VlUVZ18djKcns4gFgXVhvvHKbXiwrLzjviNNHKqZ-vQu4Wuqn0rtqpBTZlLeV6XW9KuRga1LJQ3bkrnir5hJszFpszll3drpUqd-1uYRpZyHVRV2VZV9tKrradasuqXG9KfKpr1Yl1gaMydmXtZUzFXBiiiE3WKxdWtWgp33ZSOrxCXhVSpssvNGnTso09pSwMMd1h2LDF5h_vp0c3Oc-gUccOdfLF4dYGy9QRin1YEr9ZzDcm-Es60bkmixhs88mChofYrjo_CnlMxPNjOQX_ih0LecxyScjjnM-lkb8DAAD__-Exx3c">