[llvm-bugs] [Bug 49778] New: [InstCombine] Miscompile of (x & (~(-1 << x))) << x

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Mar 30 14:32:20 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=49778

            Bug ID: 49778
           Summary: [InstCombine] Miscompile of (x & (~(-1 << x))) << x
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: meheff at google.com
                CC: llvm-bugs at lists.llvm.org

This expression is miscompiled in the case where x is zext of a single bit
value. Here's before and after from a single instcombine transform extracted
using debug_counter and fed to Alive:

----------------------------------------
define i32 @src(i1 %x2) {
%0:
  %x13 = zext i1 %x2 to i32
  %_7 = shl i32 4294967295, %x13
  %mask = xor i32 %_7, 4294967295
  %_8 = and i32 %mask, %x13
  %_9 = shl i32 %_8, %x13
  ret i32 %_9
}
=>
define i32 @tgt(i1 %x2) {
%0:
  %x13 = zext i1 %x2 to i32
  %1 = shl i32 %x13, %x13
  %_9 = and i32 %1, 0
  ret i32 %_9
}
Transformation doesn't verify!
ERROR: Value mismatch

Example:
i1 %x2 = #x1 (1)

Source:
i32 %x13 = #x00000001 (1)
i32 %_7 = #xfffffffe (4294967294, -2)
i32 %mask = #x00000001 (1)
i32 %_8 = #x00000001 (1)
i32 %_9 = #x00000002 (2)

Target:
i32 %x13 = #x00000001 (1)
i32 %1 = #x00000002 (2)
i32 %_9 = #x00000000 (0)
Source value: #x00000002 (2)
Target value: #x00000000 (0)


I suspect the problematic transform is this one:

https://github.com/llvm/llvm-project/blob/c06a8f9caa51c7ea71dac716e0a35f5e343e4546/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp#L168

Looks like maybe(?) NewMask is incorrectly being computed as zero:

https://github.com/llvm/llvm-project/blob/c06a8f9caa51c7ea71dac716e0a35f5e343e4546/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp#L317

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210330/3e67fb8b/attachment-0001.html>


More information about the llvm-bugs mailing list