[PATCH] D37419: Teach scalar evolution to handle inttoptr/ptrtoint

Xinliang David Li via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 3 16:49:20 PDT 2017


On Sun, Sep 3, 2017 at 4:23 PM, Sanjoy Das <sanjoy at playingwithpointers.com>
wrote:

> Hi,
>
> >>   %i0 = ptrtoint i8* %p0 to i64
> >>   %i1 = ptrtoint i8* %p1 to i64
> >>   %i2 = add i32 %i0, %i1
> >>   %ptr = inttoptr i64 %i2 to i8*
> >>
> >> `%ptr` can alias both `%p0` and `%p1`.  However, if you round-trip
> `%ptr`
> >> through SCEV and SCEVExpander, then nothing stops SCEVExpander from
> >> expanding the SCEV for `%ptr` to
> >>
> >>   %i0 = ptrtoint i8* %p0 to i64
> >>   %i1 = ptrtoint i8* %p1 to i64
> >>   %p0_ = inttoptr i64 %i0 to i8*
> >>   %ptr = getelementptr i8, i8* %p0_, i64 %p0
> >
> >  you mean
> >
> > %ptr = getelementptr i8, i8* %p0_, i64 %p1 ?
>
> I should have written `%ptr = getelementptr i8, i8* %p0, i64 %i1`, but
> yes, what you said gets at the core of the idea.
>
> >> and `%ptr` no longer aliases `%p1` (assume that we can prove `%p0`
> >> no-alias `%p1` in both the snippets).
> >
> > This looks like a convincing example. Do you consider this a bug in the
> SCEV
> > expander to be fixed?
>
> If we consider this to be a bug, then we can't fix it easily in SCEV
> expander alone.  IIUC with your change in both the cases above `%ptr`
> will be mapped into a `(add %p1 %p2)` SCEV expression, and so SCEV
> expander will have to conservatively assume that `(add %p1 %p2)` came
> from a `ptrtoint (add (inttoptr %p1) (inttoptr %p2))` (and have to
> expand accordingly), and this will regress alias analysis.


The original alias information is already conservative, isn't it? This does
not seem to regress it.  By the way, a natural place to 'canonicalize' IR
to get rid of the intptr/ptrint might actually be in SCEV itself. If that
is the case,  teaching SCEV to understand them (possibly in a special mode
as Hal mentioned) might be unavoidable.

David



> I think to
> properly handle this in general, we will need SCEV nodes for ptrtoint
> and inttoptr.
>
> -- Sanjoy
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170903/12c5d97d/attachment.html>


More information about the llvm-commits mailing list