[PATCH] D31488: [SimplifyIndvar] Replace the sdiv used by IV if we can prove both of its operands are non-negative

Hongbin Zheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 29 23:33:51 PDT 2017


etherzhhb added a comment.

Hi Tobias

In https://reviews.llvm.org/D31488#713840, @grosser wrote:

> Hi Hongbin,
>
> I am not sure what you are trying to achieve here. This kernel is already optimized by -instcombine to


This is based on the discussion in https://groups.google.com/d/topic/llvm-dev/HKK9ffG-dIU/discussion

> 
> 
>   define void @test(i32* %a) {
>   entry:
>     br label %for.body
>   
>   for.body:                                         ; preds = %for.body, %entry
>     %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
>     %div = shl nsw i32 %i.01, 5
>     %idxprom = sext i32 %div to i64
>     %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
>     store i32 %i.01, i32* %arrayidx, align 4
>     %inc = add nsw i32 %i.01, 1
>     %cmp = icmp slt i32 %i.01, 63
>     br i1 %cmp, label %for.body, label %for.end
>   
>   for.end:                                          ; preds = %for.body
>     ret void
>   }
> 
> 
> So the sdiv is replaced by a lsh, taking into account the information that sdiv is working on unsigned inputs only.
> 
> If you divide by a non-power-of-two, instcombine does not change the code and leaves the sdiv in. However, if you would like to move to an sdiv even in this case this should probably be added to instcombine.

Yes, see below.

> I would also be interested to learn why an 'udiv' is a better canonical form than an 'sdiv'. Is it faster to execute (non-power-of-two) udivs on FPGAs than sdivs?

I am focusing on the induction variable and its users here. I am trying to get an AddRec from arrayidx.
However, there is no sdiv in SCEV, and SCEV will give up when it see a sdiv. From this perspective udiv is better then sdiv. I think I should explicitly explain this in the summary

Let me also look at instcombine, the original motivation to put it here is mainly because SimplifyIndVar use SCEV, which provide better range knowledge about induction variables.


Repository:
  rL LLVM

https://reviews.llvm.org/D31488





More information about the llvm-commits mailing list