Skip to content

Commit b7096f7

Browse files
committed
Merging r348462:
------------------------------------------------------------------------ r348462 | lebedevri | 2018-12-06 00:14:24 -0800 (Thu, 06 Dec 2018) | 13 lines [InstCombine] foldICmpWithLowBitMaskedVal(): don't miscompile -1 vector elts I was finally able to quantify what i thought was missing in the fix, it was vector constants. If we have a scalar (and %x, -1), it will be instsimplified before we reach this code, but if it is a vector, we may still have a -1 element. Thus, we want to avoid the fold if *at least one* element is -1. Or in other words, ignoring the undef elements, no sign bits should be set. Thus, m_NonNegative(). A follow-up for rL348181 https://bugs.llvm.org/show_bug.cgi?id=39861 ------------------------------------------------------------------------ llvm-svn: 348538
1 parent 9e856fa commit b7096f7

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,6 +2926,8 @@ static Value *foldICmpWithLowBitMaskedVal(ICmpInst &I,
29262926
return nullptr; // Ignore the other case.
29272927
if (!match(M, m_Constant())) // Can not do this fold with non-constant.
29282928
return nullptr;
2929+
if (!match(M, m_NonNegative())) // Must not have any -1 vector elements.
2930+
return nullptr;
29292931
DstPred = ICmpInst::Predicate::ICMP_SLE;
29302932
break;
29312933
case ICmpInst::Predicate::ICMP_SLT:
@@ -2934,6 +2936,8 @@ static Value *foldICmpWithLowBitMaskedVal(ICmpInst &I,
29342936
return nullptr; // Ignore the other case.
29352937
if (!match(M, m_Constant())) // Can not do this fold with non-constant.
29362938
return nullptr;
2939+
if (!match(M, m_NonNegative())) // Must not have any -1 vector elements.
2940+
return nullptr;
29372941
DstPred = ICmpInst::Predicate::ICMP_SGT;
29382942
break;
29392943
case ICmpInst::Predicate::ICMP_SLE:

llvm/test/Transforms/InstCombine/canonicalize-constant-low-bit-mask-and-icmp-sge-to-icmp-sle.ll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ define i1 @nv(i8 %x, i8 %y) {
205205

206206
define <2 x i1> @n3_vec(<2 x i8> %x) {
207207
; CHECK-LABEL: @n3_vec(
208-
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i8> [[X:%.*]], <i8 4, i8 0>
209-
; CHECK-NEXT: ret <2 x i1> [[TMP1]]
208+
; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X:%.*]], <i8 3, i8 -1>
209+
; CHECK-NEXT: [[RET:%.*]] = icmp sge <2 x i8> [[TMP0]], [[X]]
210+
; CHECK-NEXT: ret <2 x i1> [[RET]]
210211
;
211212
%tmp0 = and <2 x i8> %x, <i8 3, i8 -1>
212213
%ret = icmp sge <2 x i8> %tmp0, %x
@@ -215,8 +216,9 @@ define <2 x i1> @n3_vec(<2 x i8> %x) {
215216

216217
define <3 x i1> @n4_vec(<3 x i8> %x) {
217218
; CHECK-LABEL: @n4_vec(
218-
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <3 x i8> [[X:%.*]], <i8 4, i8 undef, i8 0>
219-
; CHECK-NEXT: ret <3 x i1> [[TMP1]]
219+
; CHECK-NEXT: [[TMP0:%.*]] = and <3 x i8> [[X:%.*]], <i8 3, i8 undef, i8 -1>
220+
; CHECK-NEXT: [[RET:%.*]] = icmp sge <3 x i8> [[TMP0]], [[X]]
221+
; CHECK-NEXT: ret <3 x i1> [[RET]]
220222
;
221223
%tmp0 = and <3 x i8> %x, <i8 3, i8 undef, i8 -1>
222224
%ret = icmp sge <3 x i8> %tmp0, %x

llvm/test/Transforms/InstCombine/canonicalize-constant-low-bit-mask-and-icmp-slt-to-icmp-sgt.ll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ define i1 @nv(i8 %x, i8 %y) {
205205

206206
define <2 x i1> @n3(<2 x i8> %x) {
207207
; CHECK-LABEL: @n3(
208-
; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i8> [[X:%.*]], <i8 3, i8 -1>
209-
; CHECK-NEXT: ret <2 x i1> [[TMP1]]
208+
; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X:%.*]], <i8 3, i8 -1>
209+
; CHECK-NEXT: [[RET:%.*]] = icmp slt <2 x i8> [[TMP0]], [[X]]
210+
; CHECK-NEXT: ret <2 x i1> [[RET]]
210211
;
211212
%tmp0 = and <2 x i8> %x, <i8 3, i8 -1>
212213
%ret = icmp slt <2 x i8> %tmp0, %x
@@ -215,8 +216,9 @@ define <2 x i1> @n3(<2 x i8> %x) {
215216

216217
define <3 x i1> @n4(<3 x i8> %x) {
217218
; CHECK-LABEL: @n4(
218-
; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <3 x i8> [[X:%.*]], <i8 3, i8 undef, i8 -1>
219-
; CHECK-NEXT: ret <3 x i1> [[TMP1]]
219+
; CHECK-NEXT: [[TMP0:%.*]] = and <3 x i8> [[X:%.*]], <i8 3, i8 undef, i8 -1>
220+
; CHECK-NEXT: [[RET:%.*]] = icmp slt <3 x i8> [[TMP0]], [[X]]
221+
; CHECK-NEXT: ret <3 x i1> [[RET]]
220222
;
221223
%tmp0 = and <3 x i8> %x, <i8 3, i8 undef, i8 -1>
222224
%ret = icmp slt <3 x i8> %tmp0, %x

0 commit comments

Comments
 (0)