[PATCH] D52508: [InstCombine] Clean up after IndVarSimplify

Dave Green via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 25 10:32:01 PDT 2018


dmgreen created this revision.
dmgreen added reviewers: spatel, efriedma, craig.topper.
Herald added a subscriber: sanjoy.

This is an attempt to fix up the mess of code left over in:
https://godbolt.org/z/QSggQh

Where, when compiling this code (optionally having the loop do something):

  unsigned test(unsigned A)
  {
    do {
      A -= 32;
    } while(A >= 32);
    return A;
  }

We end up with this remainder code from IndVarSimplify calculating the exit value of A:

  define i32 @test(i32 %A) {
  entry:
    %l0 = icmp ult i32 %A, 31
    %l1 = select i1 %l0, i32 %A, i32 31
    %umax = xor i32 %l1, -1
    %l2 = add i32 %umax, %A
    %l3 = add i32 %l2, 32
    %l4 = and i32 %l3, -32
    %l5 = sub i32 %A, %l4
    ret i32 %l5
  }

This:

  (A - (-32 & (32 + A + ~min(A, 31))))

can be simplified to just (https://rise4fun.com/Alive/ARG).

  A & 31

This looks like an instcombine problem to me, so here I've put in two new folds. One takes us from:

  A + ~min(A, 31)      or   A + ~select(P, A, B)

to

  A < 31 ? -1 : A-32        select(P, -1, A+~B)

https://rise4fun.com/Alive/LvO. But can potentially break appart the min/max in the process. In this case we happen to fold back with the 32+ into another min/max, but that might not be the case in general.

The second part uses demand bits of

  -32 & umax(A, 31)

to prove that this is just (A & -32). See https://rise4fun.com/Alive/Pwo (the min we already get).

Neither of these feel particularly elegant (especially breaking min/max), so I'd thought I'd put these up and get some feedback on whether these seem sensible or if anyone can come up with better ways to do these. They shouldn't, apart from the breaking of minmax, make anything worse I believe. There are two things in here so I can split these out into separate commits, but this together shows the intent of them.


https://reviews.llvm.org/D52508

Files:
  lib/Transforms/InstCombine/InstCombineAddSub.cpp
  lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
  test/Transforms/IndVarSimplify/replace-loop-exit-folds.ll
  test/Transforms/InstCombine/add-select.ll
  test/Transforms/InstCombine/minmax-demandbits.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52508.166941.patch
Type: text/x-patch
Size: 10089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180925/5a0d770d/attachment.bin>


More information about the llvm-commits mailing list