[PATCH] D157977: [InstCombine] OptimizePointerDifference when a gep has phi ptr

Biplob Mishra via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 11 03:45:04 PDT 2023


bipmis added a comment.

In D157977#4641397 <https://reviews.llvm.org/D157977#4641397>, @goldstein.w.n wrote:

> In D157977#4636857 <https://reviews.llvm.org/D157977#4636857>, @bipmis wrote:
>
>> Rebase patch after pre-committing tests. Address review comments.
>> Alive 2 links
>> https://alive2.llvm.org/ce/z/wZJfzE
>> https://alive2.llvm.org/ce/z/izA8ij
>
> These can/should be simplified a great deal.
> For example if I understand correctly youre trying to transform the following pattern:
>
>   define i1 @src(ptr %p, i64 %A, i64 %B, i64 %C, i1 %c) {
>   entry:
>     br i1 %c, label %true, label %false
>   
>   true:
>     %p_gepA = getelementptr i8, ptr %p, i64 %A
>     br label %false
>   false:
>     %p_phi = phi ptr [ %p_gepA, %true ], [ %p, %entry ]
>     %phi_gepC = getelementptr i8, ptr %p_phi, i64 %C
>   
>     %gepC_int = ptrtoint ptr %phi_gepC to i64
>     %p_gepB = getelementptr i8, ptr %p, i64 %B
>     %gepB_int = ptrtoint ptr %p_gepB to i64
>     %sub = sub i64 %gepC_int, %gepB_int
>     %r = icmp eq i64 %sub, 0
>     ret i1 %r
>   }
>
> Also in general this patch appears to be doing a lot more than the summary indicates.
> Please update to summary to be more precise.
>
> AFAICT Its really:
>
>   (phi, (sub (phi (phi GEPA), GEPB), GEPB), ...)
>   or
>   (phi, (ashr/lshr (sub (phi (phi GEPA), GEPB), GEPB), ...), ...)
>
> Is that correct?

Not exactly. This transform will benefit but what I am trying to address is the test case which has a loop in the given pattern. If the sub which is the result of the loop results in an icmp, or(icmp) and based on the pattern it can be deduced that we can are better off with just the difference of index of the 2 pointers. In our example it then
converts the sub to a sub(gep_index1, gep_index2). 
It is also noted that other transforms in presence of TBAA can further reduce this reduction by eliminating the entire loop.

As per my understanding it is a
phi(sub(prttoint(A), ptrint(B)), ...)
where A -> GEP(PHI(gep A, B)) and B is a ptr/GEP


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157977/new/

https://reviews.llvm.org/D157977



More information about the llvm-commits mailing list