[PATCH] peephole optimization in switch table lookup: reuse the guarding table comparison if possible

Erik Eckstein eeckstein at apple.com
Thu Nov 20 06:49:49 PST 2014


> From what I understand, this is doing something different

The approach is different, but the result is the same: v1, v2, ... are constants and op is the compare (r == 0 in our example). Together with jump threading the compare will be eliminated.
I actually tried it and it works. The only limitation is that FoldOpIntoPhi requires that the phi has no other uses than the compare (but this could be changed).

Now I thought it would be a good idea to just do FoldOpIntoPhi instead of my patch. But it's not that easy because of phase ordering: it must be done before switch table generation but only if it does not prevent switch table generation.
Anyway I found that doing it separately is easier and it has the advantage that I only do it if the cmp can be optimized away afterwards (FoldOpIntoPhi would also trigger if the cmp is against a case value).

I attached another version of my patch. The main difference is that I made it more general by doing "constant propagation" on the compare. 

Please let me know your comments

Thanks,
Erik

-------------- next part --------------
A non-text attachment was scrubbed...
Name: llvm-switch-cmp-reuse2.patch
Type: application/octet-stream
Size: 9218 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141120/282ddebd/attachment.obj>
-------------- next part --------------

> On 20 Nov 2014, at 01:38, Hans Wennborg <hans at chromium.org> wrote:
> 
> On Wed, Nov 19, 2014 at 1:21 AM, Erik Eckstein <eeckstein at apple.com> wrote:
>> I did some investigation and found that we already have such an
>> optimization.
>> It is InstCombiner::FoldOpIntoPhi(Instruction &I) where I is the "r == 0"
>> cmp instruction.
> 
> From what I understand, this is doing something different: it's
> basically transforming
> 
> x = phi(v1, v2, ...)
> y = op(x)
> 
> into
> 
> y = phi(op(v1), op(v2), ...)
> 
> Which I don't think helps in your code example (at least not if r has
> more uses than the "r==0" expression).
> 
> - Hans



More information about the llvm-commits mailing list