LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 39861 - Incorrect fold of 'x & (-1 >> y) s>= x'
Summary: Incorrect fold of 'x & (-1 >> y) s>= x'
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Scalar Optimizations (show other bugs)
Version: trunk
Hardware: All All
: P normal
Assignee: Roman Lebedev
URL:
Keywords: miscompilation
Depends on:
Blocks:
 
Reported: 2018-12-02 14:47 PST by Nuno Lopes
Modified: 2019-06-08 22:07 PDT (History)
6 users (show)

See Also:
Fixed By Commit(s): 348181, 348461, 348462


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nuno Lopes 2018-12-02 14:47:39 PST
This is a bug in test/Transforms/InstCombine/canonicalize-constant-low-bit-mask-and-icmp-sge-to-icmp-sle.ll

Output of Alive2:
declare i1 @pv(i8 %x, i8 %y) {
  %tmp0 = lshr i8 255, %y
  %tmp1 = and i8 %tmp0, %x
  %ret = icmp sge i8 %tmp1, %x
  ret i1 %ret
}
=>
declare i1 @pv(i8 %x, i8 %y) {
  %tmp0 = lshr i8 255, %y
  %1 = icmp sge i8 %tmp0, %x
  ret i1 %1
}
Transformation doesn't verify!
ERROR: Value mismatch

Example:
i8 %x = 0x7e (126)
i8 %y = 0x0 (0)

Source:
i8 %tmp0 = 0xff (255, -1)
i8 %tmp1 = 0x7e (126)
i1 %ret = 0x1 (1, -1)

Target:
i8 %tmp0 = 0xff (255, -1)
i1 %1 = 0x0 (0)
Source value: 0x1 (1, -1)
Target value: 0x0 (0)
Comment 1 Roman Lebedev 2018-12-02 22:21:55 PST
Hm, lookiug..
Comment 2 Roman Lebedev 2018-12-02 22:41:55 PST
(In reply to Roman Lebedev from comment #1)
> Hm, lookiug..

Okay, so that is because in
  %tmp0 = lshr i8 255, %y
%y is 0. Ouch. That was not caught in the initial check, which was
rL337109 https://rise4fun.com/Alive/I3O because 
  Pre: isPowerOf2(C1+1)
where C1 was -1, so C1+1 would be 0, which is not PowerOf2 so it wasn't checked

Can you tell if any other canonicalize-constant-low-bit-mask-and-icmp-* are bad?
Comment 3 Nuno Lopes 2018-12-03 02:14:00 PST
The only other test 'canonicalize-*' test I see failing is this:

canonicalize-constant-low-bit-mask-and-icmp-slt-to-icmp-sgt.ll

declare i1 @pv(i8 %x, i8 %y) {
  %tmp0 = lshr i8 255, %y
  %tmp1 = and i8 %tmp0, %x
  %ret = icmp slt i8 %tmp1, %x
  ret i1 %ret
}
=>
declare i1 @pv(i8 %x, i8 %y) {
  %tmp0 = lshr i8 255, %y
  %1 = icmp slt i8 %tmp0, %x
  ret i1 %1
}
Transformation doesn't verify!
ERROR: Value mismatch

Example:
i8 %x = 0x7e (126)
i8 %y = 0x0 (0)

Source:
i8 %tmp0 = 0xff (255, -1)
i8 %tmp1 = 0x7e (126)
i1 %ret = 0x0 (0)

Target:
i8 %tmp0 = 0xff (255, -1)
i1 %1 = 0x1 (1, -1)
Source value: 0x0 (0)
Target value: 0x1 (1, -1)
Comment 4 Roman Lebedev 2018-12-03 02:18:04 PST
Got alive2 to work, can confirm that this test file is failing.

(In reply to Nuno Lopes from comment #3)
> The only other test 'canonicalize-*' test I see failing is this:
> 
> canonicalize-constant-low-bit-mask-and-icmp-slt-to-icmp-sgt.ll

Ok, thank you, i will take care of that.
Comment 5 Roman Lebedev 2018-12-03 12:08:33 PST
Thanks for reporting!