[llvm] f422c5d - [InstCombine] fold select-of-zero-or-ones with negated op
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 28 09:09:20 PST 2022
Author: Sanjay Patel
Date: 2022-02-28T12:07:49-05:00
New Revision: f422c5d87142cc1dc65cf7fb15444c2afc810611
URL: https://github.com/llvm/llvm-project/commit/f422c5d87142cc1dc65cf7fb15444c2afc810611
DIFF: https://github.com/llvm/llvm-project/commit/f422c5d87142cc1dc65cf7fb15444c2afc810611.diff
LOG: [InstCombine] fold select-of-zero-or-ones with negated op
(X u< 2) ? -X : -1 --> sext (X != 0)
(X u> 1) ? -1 : -X --> sext (X != 0)
https://alive2.llvm.org/ce/z/U3y5Bb
https://alive2.llvm.org/ce/z/hgi-4p
This is part of solving:
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
llvm/test/Transforms/InstCombine/select.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index e9295f4c85fc2..a9745cf38a77e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -1512,6 +1512,30 @@ tryToReuseConstantFromSelectInComparison(SelectInst &Sel, ICmpInst &Cmp,
return &Sel;
}
+static Instruction *foldSelectZeroOrOnes(ICmpInst *Cmp, Value *TVal,
+ Value *FVal,
+ InstCombiner::BuilderTy &Builder) {
+ if (!Cmp->hasOneUse())
+ return nullptr;
+
+ const APInt *CmpC;
+ if (!match(Cmp->getOperand(1), m_APIntAllowUndef(CmpC)))
+ return nullptr;
+
+ // (X u< 2) ? -X : -1 --> sext (X != 0)
+ Value *X = Cmp->getOperand(0);
+ if (Cmp->getPredicate() == ICmpInst::ICMP_ULT && *CmpC == 2 &&
+ match(TVal, m_Neg(m_Specific(X))) && match(FVal, m_AllOnes()))
+ return new SExtInst(Builder.CreateIsNotNull(X), TVal->getType());
+
+ // (X u> 1) ? -1 : -X --> sext (X != 0)
+ if (Cmp->getPredicate() == ICmpInst::ICMP_UGT && *CmpC == 1 &&
+ match(FVal, m_Neg(m_Specific(X))) && match(TVal, m_AllOnes()))
+ return new SExtInst(Builder.CreateIsNotNull(X), TVal->getType());
+
+ return nullptr;
+}
+
/// Visit a SelectInst that has an ICmpInst as its first operand.
Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
ICmpInst *ICI) {
@@ -1608,6 +1632,9 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
if (Instruction *V = foldSelectCtlzToCttz(ICI, TrueVal, FalseVal, Builder))
return V;
+ if (Instruction *V = foldSelectZeroOrOnes(ICI, TrueVal, FalseVal, Builder))
+ return V;
+
if (Value *V = foldSelectICmpAndOr(ICI, TrueVal, FalseVal, Builder))
return replaceInstUsesWith(SI, V);
diff --git a/llvm/test/Transforms/InstCombine/select.ll b/llvm/test/Transforms/InstCombine/select.ll
index 48403bb461377..a5f716c79a36f 100644
--- a/llvm/test/Transforms/InstCombine/select.ll
+++ b/llvm/test/Transforms/InstCombine/select.ll
@@ -3006,9 +3006,8 @@ define <2 x i32> @mul_select_eq_undef_vector_not_merging_to_zero(<2 x i32> %x, <
define i8 @ne0_is_all_ones(i8 %x) {
; CHECK-LABEL: @ne0_is_all_ones(
-; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
-; CHECK-NEXT: [[UGT1:%.*]] = icmp ugt i8 [[X]], 1
-; CHECK-NEXT: [[R:%.*]] = select i1 [[UGT1]], i8 -1, i8 [[NEGX]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[X:%.*]], 0
+; CHECK-NEXT: [[R:%.*]] = sext i1 [[TMP1]] to i8
; CHECK-NEXT: ret i8 [[R]]
;
%negx = sub i8 0, %x
@@ -3021,8 +3020,8 @@ define i8 @ne0_is_all_ones_use1(i8 %x) {
; CHECK-LABEL: @ne0_is_all_ones_use1(
; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
; CHECK-NEXT: call void @use_i8(i8 [[NEGX]])
-; CHECK-NEXT: [[UGT1:%.*]] = icmp ugt i8 [[X]], 1
-; CHECK-NEXT: [[R:%.*]] = select i1 [[UGT1]], i8 -1, i8 [[NEGX]]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[X]], 0
+; CHECK-NEXT: [[R:%.*]] = sext i1 [[TMP1]] to i8
; CHECK-NEXT: ret i8 [[R]]
;
%negx = sub i8 0, %x
@@ -3032,6 +3031,8 @@ define i8 @ne0_is_all_ones_use1(i8 %x) {
ret i8 %r
}
+; negative test
+
define i8 @ne0_is_all_ones_use2(i8 %x) {
; CHECK-LABEL: @ne0_is_all_ones_use2(
; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
@@ -3047,6 +3048,8 @@ define i8 @ne0_is_all_ones_use2(i8 %x) {
ret i8 %r
}
+; negative test
+
define i8 @ne0_is_all_ones_wrong_pred(i8 %x) {
; CHECK-LABEL: @ne0_is_all_ones_wrong_pred(
; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
@@ -3060,6 +3063,8 @@ define i8 @ne0_is_all_ones_wrong_pred(i8 %x) {
ret i8 %r
}
+; negative test
+
define i8 @ne0_is_all_ones_wrong_cmp(i8 %x) {
; CHECK-LABEL: @ne0_is_all_ones_wrong_cmp(
; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
@@ -3073,6 +3078,8 @@ define i8 @ne0_is_all_ones_wrong_cmp(i8 %x) {
ret i8 %r
}
+; negative test
+
define i8 @ne0_is_all_ones_wrong_sel(i8 %x) {
; CHECK-LABEL: @ne0_is_all_ones_wrong_sel(
; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
@@ -3088,9 +3095,8 @@ define i8 @ne0_is_all_ones_wrong_sel(i8 %x) {
define <2 x i8> @ne0_is_all_ones_swap_vec(<2 x i8> %x) {
; CHECK-LABEL: @ne0_is_all_ones_swap_vec(
-; CHECK-NEXT: [[NEGX:%.*]] = sub <2 x i8> zeroinitializer, [[X:%.*]]
-; CHECK-NEXT: [[ULT2:%.*]] = icmp ult <2 x i8> [[X]], <i8 2, i8 2>
-; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[ULT2]], <2 x i8> [[NEGX]], <2 x i8> <i8 -1, i8 -1>
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ne <2 x i8> [[X:%.*]], zeroinitializer
+; CHECK-NEXT: [[R:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%negx = sub <2 x i8> zeroinitializer, %x
@@ -3101,9 +3107,8 @@ define <2 x i8> @ne0_is_all_ones_swap_vec(<2 x i8> %x) {
define <2 x i8> @ne0_is_all_ones_swap_vec_poison(<2 x i8> %x) {
; CHECK-LABEL: @ne0_is_all_ones_swap_vec_poison(
-; CHECK-NEXT: [[NEGX:%.*]] = sub <2 x i8> <i8 0, i8 poison>, [[X:%.*]]
-; CHECK-NEXT: [[ULT2:%.*]] = icmp ult <2 x i8> [[X]], <i8 2, i8 poison>
-; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[ULT2]], <2 x i8> [[NEGX]], <2 x i8> <i8 -1, i8 poison>
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ne <2 x i8> [[X:%.*]], zeroinitializer
+; CHECK-NEXT: [[R:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%negx = sub <2 x i8> <i8 0, i8 poison>, %x
More information about the llvm-commits
mailing list