Skip to content

Commit 1c12dcc

Browse files
[InstCombine] Extend sext/zext boolean additions to vectors
Reported-by: shao-hua-li Fixes: #68745.
1 parent c3356ac commit 1c12dcc

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,9 +2899,17 @@ Instruction *InstCombinerImpl::foldICmpSubConstant(ICmpInst &Cmp,
28992899
static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
29002900
Value *Op1, IRBuilderBase &Builder,
29012901
bool HasOneUse) {
2902+
auto FoldConstant = [&](bool Val) {
2903+
Constant *Res = Val ? Builder.getTrue() : Builder.getFalse();
2904+
if (Op0->getType()->isVectorTy())
2905+
Res = ConstantVector::getSplat(
2906+
cast<VectorType>(Op0->getType())->getElementCount(), Res);
2907+
return Res;
2908+
};
2909+
29022910
switch (Table.to_ulong()) {
29032911
case 0: // 0 0 0 0
2904-
return Builder.getFalse();
2912+
return FoldConstant(false);
29052913
case 1: // 0 0 0 1
29062914
return HasOneUse ? Builder.CreateNot(Builder.CreateOr(Op0, Op1)) : nullptr;
29072915
case 2: // 0 0 1 0
@@ -2931,7 +2939,7 @@ static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
29312939
case 14: // 1 1 1 0
29322940
return Builder.CreateOr(Op0, Op1);
29332941
case 15: // 1 1 1 1
2934-
return Builder.getTrue();
2942+
return FoldConstant(true);
29352943
default:
29362944
llvm_unreachable("Invalid Operation");
29372945
}

llvm/test/Transforms/InstCombine/icmp-add.ll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,19 @@ bb:
515515
ret i1 %t
516516
}
517517

518+
define <2 x i1> @cvt_icmp_2_sext_plus_zext_ne_vec(<2 x i1> %arg, <2 x i1> %arg1) {
519+
; CHECK-LABEL: @cvt_icmp_2_sext_plus_zext_ne_vec(
520+
; CHECK-NEXT: bb:
521+
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
522+
;
523+
bb:
524+
%i = sext <2 x i1> %arg to <2 x i32>
525+
%i2 = zext <2 x i1> %arg1 to <2 x i32>
526+
%i3 = add nsw <2 x i32> %i, %i2
527+
%i4 = icmp ne <2 x i32> %i3, <i32 2, i32 2>
528+
ret <2 x i1> %i4
529+
}
530+
518531
; test if zext i1 X + sext i1 Y converted to sext i1 X + zext i1 Y
519532
; and then processed
520533

@@ -531,6 +544,19 @@ bb:
531544
ret i1 %t
532545
}
533546

547+
define <2 x i1> @cvt_icmp_neg_2_zext_plus_sext_eq_vec(<2 x i1> %arg, <2 x i1> %arg1) {
548+
; CHECK-LABEL: @cvt_icmp_neg_2_zext_plus_sext_eq_vec(
549+
; CHECK-NEXT: bb:
550+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
551+
;
552+
bb:
553+
%i = zext <2 x i1> %arg to <2 x i32>
554+
%i2 = sext <2 x i1> %arg1 to <2 x i32>
555+
%i3 = add nsw <2 x i32> %i2, %i
556+
%i4 = icmp eq <2 x i32> %i3, <i32 2, i32 2>
557+
ret <2 x i1> %i4
558+
}
559+
534560
define i1 @cvt_icmp_neg_1_zext_plus_sext_eq(i1 %arg, i1 %arg1) {
535561
; CHECK-LABEL: @cvt_icmp_neg_1_zext_plus_sext_eq(
536562
; CHECK-NEXT: bb:

0 commit comments

Comments
 (0)