Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Transform (1 << (x NUW+ 1)) to (2 << x) #41601

Open Quuxplusone opened 5 years ago

Quuxplusone commented 5 years ago
Bugzilla Link PR42631
Status NEW
Importance P enhancement
Reported by David Bolvansky (david.bolvansky@gmail.com)
Reported on 2019-07-16 03:30:08 -0700
Last modified on 2019-07-16 14:48:45 -0700
Version trunk
Hardware PC Linux
CC craig.topper@gmail.com, lebedev.ri@gmail.com, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, richard-llvm@metafoo.co.uk, spatel+llvm@rotateright.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
%a = add nuw i32 %x, 1
%r = shl i32 1, %a
  =>
%r = shl i32 2, %x

Done: 1
Optimization is correct!

int v1(unsigned x) {

    return (2 << x);
}

int v2(unsigned x) {

    return (1 << (x+1));
}

v1:                                     # @v1
        mov     ecx, edi
        mov     eax, 2
        shl     eax, cl
        ret
v2:                                     # @v2
        lea     ecx, [rdi + 1]
        mov     eax, 1
        shl     eax, cl
        ret

Worth to do? Opinions?
Quuxplusone commented 5 years ago

1 << (x+1) is defined when x == -1, 2 << x is not.

Quuxplusone commented 5 years ago

It should be unsigned ‘x’ (as shown in alife proof). Sorry for misleading title (thanks Roman for title fix).

Quuxplusone commented 5 years ago

Ah, I see. The C source code example you give is not the same as the IR -- in the C source code, 1 << (x+1) should not have a 'nuw' on the add. Yeah, the transformation seems correct on that IR (but it's not correct on the C code).