[llvm-commits] PATCH: Simplify comparisons of constant-related pointers, similar to simplifying their difference
benny.kra at googlemail.com
Sun Mar 25 04:39:31 PDT 2012
On 25.03.2012, at 13:28, Chandler Carruth wrote:
> On Mar 25, 2012 4:14 AM, "Benjamin Kramer" <benny.kra at googlemail.com> wrote:
> > On 25.03.2012, at 12:51, Chandler Carruth wrote:
> > > I recently taught instsimplify to compute the constant difference of two pointers which are constant-derived from a common base. This uses the same logic to handle comparisons. These show up all over the place due to C++ pointers-as-iterators comparing against the end iterator.
> > >
> > > I'll add a test case before committing, but it's nothing surprising. Just wanted to double check the actual code before moving forward.
> > Compares are a bit more complicated than subs ;)
> > - The resulting predicate should be signed, "&foo[-1] u> &foo" --> "-1 s> 0" --> false
> Good catch!
> > - It should ignore signed pointer compares. We can't reason about them due to GEP's overflow rules. "&foo <s &foo" can't be folded to "true" because "foo" could be the maximum signed value for the pointer type.
> > - It's only safe with inbounds GEPs, so we don't wrap around at the end of address space.
> Yep. It's actually a bug with the existing logic I suspect; I'll fix the strip routine to only operate on inbounds geps first and add a test.
> However, given inbounds geps, why can't we handle signed comparisons?
An inbounds GEP can't have unsigned overflow when adding the base pointer, but it can have signed overflow.
There is nothing preventing the base pointer from being e.g. LONG_MAX, and adding something to that will have signed overflow and change the outcome of the compare.
See also http://llvm.org/docs/GetElementPtr.html#overflow
I haven't seen a signed pointer compare in non-fabricated IR yet, so it's more of a theoretical issue.
> > - Ben
> > > <simplify-ptr-cmp.diff>_______________________________________________
> > > llvm-commits mailing list
> > > llvm-commits at cs.uiuc.edu
> > > http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
> > _______________________________________________
> > llvm-commits mailing list
> > llvm-commits at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
More information about the llvm-commits