[llvm-dev] RFC: Killing undef and spreading poison

Sanjoy Das via llvm-dev llvm-dev at lists.llvm.org
Tue Nov 8 15:32:10 PST 2016

Hi Nuno, Chandler,

Nuno Lopes via llvm-dev wrote:
 > This program stores 8 bits, and leaves the remaining 24 bits
 > uninitialized. It then loads 16 bits, half initialized to %v, half
 > uninitialized. SROA transforms the above function to:
 > define i16 @g(i8 %in) {
 >    %v = add nsw i8 127, %in
 >    %1 = zext i8 %v to i16
 >    %2 = shl i16 %1, 8
 >    %3 = and i16 undef, 255
 >    %4 = or i16 %3, %2
 >    ret i16 %4
 > }

This program above returns i16 poison only if "shl i16 poison, 8" is a
full value poison.  Whether that is the case today or not is anybody's
guess (as Eli says, we should not rely too much on semantics that are
known to be broken), so the important question is whether "shl i16
poison, 8" _can_ be defined to be poison only on the higher 8 bits in
a world with bitwise poison.  If we could make that happen, we'd also
get some other fringe benefits, like ComputeKnownBits would be able to
fearlessly return "the low 8 bits of `shl i16 %val, 8` are 0" as
opposed to "the low 8 bits of `shl i16 %val, 8` are 0 or poison".

One problem with shl always generating non-poison zeroes in the lower
bits is that it then is not equivalent to multiplication.  This breaks
optimizations like:

   %t = ...
   %q = shl i32 %t, 6


   %t = ...
   %q = mul i32 %t, 64

and (probably more meaningful)

   %t = ...
   %q0 = shl i32 %t, 6
   %q1 = mul i32 %q0, 3


   %t = ...
   ;; %q0 = shl i32 %t, 6
   %q1 = mul i32 %q0, (3 * 64)

and (probably even worse) it also breaks reassociation:

   %t = ...
   %q0 = shl i32 %t, %P
   %q1 = mul i32 %q0, %Q

(No other uses of %q0, but there are uses of %q1)


   %t = ...
   %q0 = mul i32 %t, %Q
   %q1 = shl i32 %q1, %P

I'm especially worried about this last re-association example since
SCEV does things like this internally when canonicalizing expressions,
and I'm not convinced that we can change it to tiptoe around these
problematic cases.

-- Sanjoy

More information about the llvm-dev mailing list