[llvm] 003903d - [InstCombine] Fold select (A & Shift == 0 | B & Shift == 0), 0, Shift to Shift & A & B (#212132)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 9 13:04:24 PDT 2026
Author: Shengxin Pei
Date: 2026-08-09T20:04:20Z
New Revision: 003903d92260a5847464f5cb36fffc8d98bf53b8
URL: https://github.com/llvm/llvm-project/commit/003903d92260a5847464f5cb36fffc8d98bf53b8
DIFF: https://github.com/llvm/llvm-project/commit/003903d92260a5847464f5cb36fffc8d98bf53b8.diff
LOG: [InstCombine] Fold select (A & Shift == 0 | B & Shift == 0), 0, Shift to Shift & A & B (#212132)
Implements the following folds:
1. `select ((A & Shift) == 0 || (B & Shift) == 0), 0, Shift`
$\rightarrow$ `Shift & A & B`
2. `select ((A & Shift) != 0 && (B & Shift) != 0), Shift, 0`
$\rightarrow$ `Shift & A & B`
Alive2:
[https://alive2.llvm.org/ce/z/KC0iRm](https://alive2.llvm.org/ce/z/KC0iRm)
Close #138650
Added:
llvm/test/Transforms/InstCombine/select-and-or-powerof2.ll
Modified:
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 39f0d11df4db9..558ad2ebccc37 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -4511,6 +4511,48 @@ static Instruction *foldSelectNegNot(SelectInst &SI,
return nullptr;
}
+/// Fold select (A & Shift == 0 | B & Shift == 0), 0, Shift -> Shift & A & B
+/// where Shift is known to be a power of two.
+static Instruction *foldSelectAndOrPowerOfTwo(SelectInst &SI,
+ InstCombiner::BuilderTy &Builder,
+ const SimplifyQuery &SQ) {
+ Value *Cond = SI.getCondition();
+
+ if (!Cond->hasOneUse())
+ return nullptr;
+
+ Value *TrueVal = SI.getTrueValue();
+ Value *FalseVal = SI.getFalseValue();
+
+ Value *A, *B, *Shift;
+
+ bool Case1 =
+ match(TrueVal, m_Zero()) && match(FalseVal, m_Value(Shift)) &&
+ match(Cond, m_Or(m_SpecificICmp(ICmpInst::ICMP_EQ,
+ m_c_And(m_Specific(Shift), m_Value(A)),
+ m_Zero()),
+ m_SpecificICmp(ICmpInst::ICMP_EQ,
+ m_c_And(m_Specific(Shift), m_Value(B)),
+ m_Zero())));
+
+ bool Case2 =
+ match(FalseVal, m_Zero()) && match(TrueVal, m_Value(Shift)) &&
+ match(Cond, m_And(m_SpecificICmp(ICmpInst::ICMP_NE,
+ m_c_And(m_Specific(Shift), m_Value(A)),
+ m_Zero()),
+ m_SpecificICmp(ICmpInst::ICMP_NE,
+ m_c_And(m_Specific(Shift), m_Value(B)),
+ m_Zero())));
+
+ if ((Case1 || Case2) && isKnownToBeAPowerOfTwo(Shift, /*OrZero=*/true,
+ SQ.getWithInstruction(&SI))) {
+ Value *And1 = Builder.CreateAnd(Shift, A);
+ return BinaryOperator::CreateAnd(And1, B);
+ }
+
+ return nullptr;
+}
+
// Return true if no use can observe the sign of zero of the select result,
// looking through phis, selects and the loop back edge to the select itself.
static bool isSelectZeroSignInsignificant(SelectInst &SI) {
@@ -4631,6 +4673,9 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
if (Instruction *I = foldSelectNegNot(SI, Builder))
return I;
+ if (Instruction *I = foldSelectAndOrPowerOfTwo(SI, Builder, SQ))
+ return I;
+
auto *SIFPOp = dyn_cast<FPMathOperator>(&SI);
if (auto *FCmp = dyn_cast<FCmpInst>(CondVal)) {
diff --git a/llvm/test/Transforms/InstCombine/select-and-or-powerof2.ll b/llvm/test/Transforms/InstCombine/select-and-or-powerof2.ll
new file mode 100644
index 0000000000000..3bc6a53d6bf11
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/select-and-or-powerof2.ll
@@ -0,0 +1,158 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i8 @select_or_eq_zero_powerof2(i8 %a, i8 %b, i8 %mask) {
+; CHECK-LABEL: define i8 @select_or_eq_zero_powerof2(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]], i8 [[MASK:%.*]]) {
+; CHECK-NEXT: [[SH:%.*]] = shl nuw i8 1, [[MASK]]
+; CHECK-NEXT: [[AND_1:%.*]] = and i8 [[SH]], [[A]]
+; CHECK-NEXT: [[ZERO_OR_SHIFT:%.*]] = and i8 [[AND_1]], [[B]]
+; CHECK-NEXT: ret i8 [[ZERO_OR_SHIFT]]
+;
+ %sh = shl nuw i8 1, %mask
+ %and_1 = and i8 %sh, %a
+ %lsb_1 = icmp eq i8 %and_1, 0
+ %and_2 = and i8 %sh, %b
+ %lsb_2 = icmp eq i8 %and_2, 0
+ %or = or i1 %lsb_1, %lsb_2
+ %zero_or_shift = select i1 %or, i8 0, i8 %sh
+ ret i8 %zero_or_shift
+}
+
+define i8 @select_and_ne_zero_powerof2(i8 %a, i8 %b, i8 %mask) {
+; CHECK-LABEL: define i8 @select_and_ne_zero_powerof2(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]], i8 [[MASK:%.*]]) {
+; CHECK-NEXT: [[SH:%.*]] = shl nuw i8 1, [[MASK]]
+; CHECK-NEXT: [[AND_1:%.*]] = and i8 [[SH]], [[A]]
+; CHECK-NEXT: [[ZERO_OR_SHIFT:%.*]] = and i8 [[AND_1]], [[B]]
+; CHECK-NEXT: ret i8 [[ZERO_OR_SHIFT]]
+;
+ %sh = shl nuw i8 1, %mask
+ %and_1 = and i8 %sh, %a
+ %lsb_1 = icmp ne i8 %and_1, 0
+ %and_2 = and i8 %sh, %b
+ %lsb_2 = icmp ne i8 %and_2, 0
+ %and_cond = and i1 %lsb_1, %lsb_2
+ %zero_or_shift = select i1 %and_cond, i8 %sh, i8 0
+ ret i8 %zero_or_shift
+}
+
+define i8 @select_or_eq_zero_powerof2_commuted(i8 %a, i8 %b, i8 %mask) {
+; CHECK-LABEL: define i8 @select_or_eq_zero_powerof2_commuted(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]], i8 [[MASK:%.*]]) {
+; CHECK-NEXT: [[SH:%.*]] = shl nuw i8 1, [[MASK]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[SH]], [[A]]
+; CHECK-NEXT: [[ZERO_OR_SHIFT:%.*]] = and i8 [[TMP1]], [[B]]
+; CHECK-NEXT: ret i8 [[ZERO_OR_SHIFT]]
+;
+ %sh = shl nuw i8 1, %mask
+ %and_1 = and i8 %a, %sh
+ %lsb_1 = icmp eq i8 %and_1, 0
+ %and_2 = and i8 %b, %sh
+ %lsb_2 = icmp eq i8 %and_2, 0
+ %or = or i1 %lsb_1, %lsb_2
+ %zero_or_shift = select i1 %or, i8 0, i8 %sh
+ ret i8 %zero_or_shift
+}
+
+define i8 @select_and_ne_zero_powerof2_commuted(i8 %a, i8 %b, i8 %mask) {
+; CHECK-LABEL: define i8 @select_and_ne_zero_powerof2_commuted(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]], i8 [[MASK:%.*]]) {
+; CHECK-NEXT: [[SH:%.*]] = shl nuw i8 1, [[MASK]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[SH]], [[A]]
+; CHECK-NEXT: [[ZERO_OR_SHIFT:%.*]] = and i8 [[TMP1]], [[B]]
+; CHECK-NEXT: ret i8 [[ZERO_OR_SHIFT]]
+;
+ %sh = shl nuw i8 1, %mask
+ %and_1 = and i8 %a, %sh
+ %lsb_1 = icmp ne i8 %and_1, 0
+ %and_2 = and i8 %b, %sh
+ %lsb_2 = icmp ne i8 %and_2, 0
+ %and_cond = and i1 %lsb_1, %lsb_2
+ %zero_or_shift = select i1 %and_cond, i8 %sh, i8 0
+ ret i8 %zero_or_shift
+}
+
+define <2 x i8> @select_or_eq_zero_powerof2_vec(<2 x i8> %a, <2 x i8> %b, <2 x i8> %mask) {
+; CHECK-LABEL: define <2 x i8> @select_or_eq_zero_powerof2_vec(
+; CHECK-SAME: <2 x i8> [[A:%.*]], <2 x i8> [[B:%.*]], <2 x i8> [[MASK:%.*]]) {
+; CHECK-NEXT: [[SH:%.*]] = shl nuw <2 x i8> splat (i8 1), [[MASK]]
+; CHECK-NEXT: [[AND_1:%.*]] = and <2 x i8> [[SH]], [[A]]
+; CHECK-NEXT: [[ZERO_OR_SHIFT:%.*]] = and <2 x i8> [[AND_1]], [[B]]
+; CHECK-NEXT: ret <2 x i8> [[ZERO_OR_SHIFT]]
+;
+ %sh = shl nuw <2 x i8> <i8 1, i8 1>, %mask
+ %and_1 = and <2 x i8> %sh, %a
+ %lsb_1 = icmp eq <2 x i8> %and_1, zeroinitializer
+ %and_2 = and <2 x i8> %sh, %b
+ %lsb_2 = icmp eq <2 x i8> %and_2, zeroinitializer
+ %or = or <2 x i1> %lsb_1, %lsb_2
+ %zero_or_shift = select <2 x i1> %or, <2 x i8> zeroinitializer, <2 x i8> %sh
+ ret <2 x i8> %zero_or_shift
+}
+
+define i8 @negative_not_power_of_two(i8 %a, i8 %b, i8 %sh) {
+; CHECK-LABEL: define i8 @negative_not_power_of_two(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]], i8 [[SH:%.*]]) {
+; CHECK-NEXT: [[AND_1:%.*]] = and i8 [[SH]], [[A]]
+; CHECK-NEXT: [[LSB_1:%.*]] = icmp eq i8 [[AND_1]], 0
+; CHECK-NEXT: [[AND_2:%.*]] = and i8 [[SH]], [[B]]
+; CHECK-NEXT: [[LSB_2:%.*]] = icmp eq i8 [[AND_2]], 0
+; CHECK-NEXT: [[OR:%.*]] = or i1 [[LSB_1]], [[LSB_2]]
+; CHECK-NEXT: [[ZERO_OR_SHIFT:%.*]] = select i1 [[OR]], i8 0, i8 [[SH]]
+; CHECK-NEXT: ret i8 [[ZERO_OR_SHIFT]]
+;
+ %and_1 = and i8 %sh, %a
+ %lsb_1 = icmp eq i8 %and_1, 0
+ %and_2 = and i8 %sh, %b
+ %lsb_2 = icmp eq i8 %and_2, 0
+ %or = or i1 %lsb_1, %lsb_2
+ %zero_or_shift = select i1 %or, i8 0, i8 %sh
+ ret i8 %zero_or_shift
+}
+
+define i8 @negative_mismatched_shift(i8 %a, i8 %b, i8 %mask, i8 %other_val) {
+; CHECK-LABEL: define i8 @negative_mismatched_shift(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]], i8 [[MASK:%.*]], i8 [[OTHER_VAL:%.*]]) {
+; CHECK-NEXT: [[SH:%.*]] = shl nuw i8 1, [[MASK]]
+; CHECK-NEXT: [[AND_1:%.*]] = and i8 [[SH]], [[A]]
+; CHECK-NEXT: [[LSB_1:%.*]] = icmp eq i8 [[AND_1]], 0
+; CHECK-NEXT: [[AND_2:%.*]] = and i8 [[SH]], [[B]]
+; CHECK-NEXT: [[LSB_2:%.*]] = icmp eq i8 [[AND_2]], 0
+; CHECK-NEXT: [[OR:%.*]] = or i1 [[LSB_1]], [[LSB_2]]
+; CHECK-NEXT: [[ZERO_OR_SHIFT:%.*]] = select i1 [[OR]], i8 0, i8 [[OTHER_VAL]]
+; CHECK-NEXT: ret i8 [[ZERO_OR_SHIFT]]
+;
+ %sh = shl nuw i8 1, %mask
+ %and_1 = and i8 %sh, %a
+ %lsb_1 = icmp eq i8 %and_1, 0
+ %and_2 = and i8 %sh, %b
+ %lsb_2 = icmp eq i8 %and_2, 0
+ %or = or i1 %lsb_1, %lsb_2
+ %zero_or_shift = select i1 %or, i8 0, i8 %other_val
+ ret i8 %zero_or_shift
+}
+
+define i8 @negative_multi_use_cond(i8 %a, i8 %b, i8 %mask, ptr %p) {
+; CHECK-LABEL: define i8 @negative_multi_use_cond(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]], i8 [[MASK:%.*]], ptr [[P:%.*]]) {
+; CHECK-NEXT: [[SH:%.*]] = shl nuw i8 1, [[MASK]]
+; CHECK-NEXT: [[AND_1:%.*]] = and i8 [[SH]], [[A]]
+; CHECK-NEXT: [[LSB_1:%.*]] = icmp eq i8 [[AND_1]], 0
+; CHECK-NEXT: [[AND_2:%.*]] = and i8 [[SH]], [[B]]
+; CHECK-NEXT: [[LSB_2:%.*]] = icmp eq i8 [[AND_2]], 0
+; CHECK-NEXT: [[OR:%.*]] = or i1 [[LSB_1]], [[LSB_2]]
+; CHECK-NEXT: store i1 [[OR]], ptr [[P]], align 1
+; CHECK-NEXT: [[ZERO_OR_SHIFT:%.*]] = select i1 [[OR]], i8 0, i8 [[SH]]
+; CHECK-NEXT: ret i8 [[ZERO_OR_SHIFT]]
+;
+ %sh = shl nuw i8 1, %mask
+ %and_1 = and i8 %sh, %a
+ %lsb_1 = icmp eq i8 %and_1, 0
+ %and_2 = and i8 %sh, %b
+ %lsb_2 = icmp eq i8 %and_2, 0
+ %or = or i1 %lsb_1, %lsb_2
+ store i1 %or, ptr %p
+ %zero_or_shift = select i1 %or, i8 0, i8 %sh
+ ret i8 %zero_or_shift
+}
More information about the llvm-commits
mailing list