Open Quuxplusone opened 5 years ago
Attached integers.ll
(1433 bytes, text/plain): Example LLVM IR
"GCC-{8.*,9.*}" should read "Clang-8" of course. Damn Copy/Paste ghosts.
https://rise4fun.com/Alive/PbSX describes the proposed transform:
Name: PR43238_sext
Pre: C < 2147483648 && C >= 0
%x = shl i32 1, %s
%xx = sext i32 %x to i64
%a = udiv i64 C, %xx
=>
%ss = sext i32 %s to i64
%a = lshr i64 C, %ss
Precondition is C < TYPE_MAX-1, this works:
Name: PR43238_sext
Pre: C < 18446744073709551615 && C >= 0
%x = shl i32 1, %s
%xx = sext i32 %x to i64
%a = udiv i64 C, %xx
=>
%ss = sext i32 %s to i64
%a = lshr i64 C, %ss
Optimization: PR43238_sext
Done: 1
Optimization is correct!
Also zext:
Name: PR43238_zext
Pre: C < 4294967295 && C >= 0
%x = shl i8 1, %s
%xx = zext i8 %x to i32
%a = udiv i32 C, %xx
=>
%ss = zext i8 %s to i32
%a = lshr i32 C, %ss
Optimization: PR43238_zext
Done: 1
Optimization is correct!
(In reply to David Bolvansky from comment #3)
> Precondition is C < TYPE_MAX-1
That's wrong; try, for example, 4294967296.
Alive is somehow getting confused by your "Pre" line. Try, for example:
Name: Impossible
Pre: C < 18446744073709551615 && C >= 0
%x = C
=>
%x = C+1
Ah, sorry!
Nuno Lopes, can you take a look at this Alive case?
Well, 18446744073709551615 is negative in i64, so 'C < 0 && C >= 0' means the
precondition is always false.
I've now added a check to Alive to print a warning if the precondition is
always false.
integers.ll
(1433 bytes, text/plain)