[llvm] 114f780 - [InstCombine] Fold select of intrinsic into intrinsic of select (#178002)

via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 3 08:52:30 PST 2026


Author: Gauravsingh Sisodia
Date: 2026-02-03T17:44:34+01:00
New Revision: 114f7808554b6959156bc205b5bfa1827a621823

URL: https://github.com/llvm/llvm-project/commit/114f7808554b6959156bc205b5bfa1827a621823
DIFF: https://github.com/llvm/llvm-project/commit/114f7808554b6959156bc205b5bfa1827a621823.diff

LOG: [InstCombine] Fold select of intrinsic into intrinsic of select (#178002)

Fix https://github.com/llvm/llvm-project/issues/140917

Transform:
`select cond, intrinsic(x, ...), intrinsic(y, ...)`
into:
`intrinsic(select cond, x, y, ...)`
for `ctpop/ctlz/cttz/abs`.

alive2 proof: https://alive2.llvm.org/ce/z/Zp55Dy

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineInternal.h
    llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/test/Transforms/InstCombine/intrinsic-select.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index 8c8b300bb2002..8a478bf344536 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -805,6 +805,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
   Instruction *foldSelectExtConst(SelectInst &Sel);
   Instruction *foldSelectEqualityTest(SelectInst &SI);
   Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
+  Instruction *foldSelectIntrinsic(SelectInst &SI);
   Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *);
   Instruction *foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
                             Value *A, Value *B, Instruction &Outer,

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 62ace0474ed7c..1a8be9436f4ff 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -493,6 +493,51 @@ Instruction *InstCombinerImpl::foldSelectOpOp(SelectInst &SI, Instruction *TI,
   return nullptr;
 }
 
+/// This transforms patterns of the form:
+///   select cond, intrinsic(x, ...), intrinsic(y, ...)
+/// into:
+///   intrinsic(select cond, x, y, ...)
+Instruction *InstCombinerImpl::foldSelectIntrinsic(SelectInst &SI) {
+  auto *LHSIntrinsic = dyn_cast<IntrinsicInst>(SI.getTrueValue());
+  if (!LHSIntrinsic)
+    return nullptr;
+  auto *RHSIntrinsic = dyn_cast<IntrinsicInst>(SI.getFalseValue());
+  if (!RHSIntrinsic ||
+      LHSIntrinsic->getIntrinsicID() != RHSIntrinsic->getIntrinsicID() ||
+      !LHSIntrinsic->hasOneUse() || !RHSIntrinsic->hasOneUse())
+    return nullptr;
+
+  const Intrinsic::ID IID = LHSIntrinsic->getIntrinsicID();
+  switch (IID) {
+  case Intrinsic::abs:
+  case Intrinsic::cttz:
+  case Intrinsic::ctlz: {
+    auto *TZ = cast<ConstantInt>(LHSIntrinsic->getArgOperand(1));
+    auto *FZ = cast<ConstantInt>(RHSIntrinsic->getArgOperand(1));
+
+    Value *TV = LHSIntrinsic->getArgOperand(0);
+    Value *FV = RHSIntrinsic->getArgOperand(0);
+
+    Value *NewSel = Builder.CreateSelect(SI.getCondition(), TV, FV, "", &SI);
+    Value *NewPoisonFlag = Builder.CreateAnd(TZ, FZ);
+    Value *NewCall = Builder.CreateBinaryIntrinsic(IID, NewSel, NewPoisonFlag);
+
+    return replaceInstUsesWith(SI, NewCall);
+  }
+  case Intrinsic::ctpop: {
+    Value *TV = LHSIntrinsic->getArgOperand(0);
+    Value *FV = RHSIntrinsic->getArgOperand(0);
+
+    Value *NewSel = Builder.CreateSelect(SI.getCondition(), TV, FV, "", &SI);
+    Value *NewCall = Builder.CreateUnaryIntrinsic(IID, NewSel);
+
+    return replaceInstUsesWith(SI, NewCall);
+  }
+  default:
+    return nullptr;
+  }
+}
+
 static bool isSelect01(const APInt &C1I, const APInt &C2I) {
   if (!C1I.isZero() && !C2I.isZero()) // One side must be zero.
     return false;
@@ -4485,6 +4530,9 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
     if (Instruction *IV = foldSelectOpOp(SI, TI, FI))
       return IV;
 
+  if (Instruction *I = foldSelectIntrinsic(SI))
+    return I;
+
   if (Instruction *I = foldSelectExtConst(SI))
     return I;
 

diff  --git a/llvm/test/Transforms/InstCombine/intrinsic-select.ll b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
index c8f5a216ed534..aa4168918d939 100644
--- a/llvm/test/Transforms/InstCombine/intrinsic-select.ll
+++ b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
@@ -532,3 +532,156 @@ define <2 x i8> @test_select_rotate_right_non_splat(i1 %0) {
   %ret = call <2 x i8> @llvm.fshr.v2i8(<2 x i8> %s, <2 x i8> %s, <2 x i8> <i8 7, i8 7>)
   ret <2 x i8> %ret
 }
+
+define i32 @select_of_ctpop(i1 %cond, i32 %x, i32 %y) {
+; CHECK-LABEL: @select_of_ctpop(
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[COND:%.*]], i32 [[X:%.*]], i32 [[Y:%.*]]
+; CHECK-NEXT:    [[SEL:%.*]] = call range(i32 0, 33) i32 @llvm.ctpop.i32(i32 [[TMP1]])
+; CHECK-NEXT:    ret i32 [[SEL]]
+;
+  %ctpop1 = call i32 @llvm.ctpop.i32(i32 %x)
+  %ctpop2 = call i32 @llvm.ctpop.i32(i32 %y)
+  %sel = select i1 %cond, i32 %ctpop1, i32 %ctpop2
+  ret i32 %sel
+}
+
+; Negative test: TV is not ctpop, should not transform
+define i32 @select_of_ctpop_negative_tv(i1 %cond, i32 %x, i32 %y) {
+; CHECK-LABEL: @select_of_ctpop_negative_tv(
+; CHECK-NEXT:    [[CTPOP2:%.*]] = call range(i32 0, 33) i32 @llvm.ctpop.i32(i32 [[Y:%.*]])
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i32 [[X:%.*]], i32 [[CTPOP2]]
+; CHECK-NEXT:    ret i32 [[SEL]]
+;
+  %ctpop1 = add i32 %x, 0
+  %ctpop2 = call i32 @llvm.ctpop.i32(i32 %y)
+  %sel = select i1 %cond, i32 %ctpop1, i32 %ctpop2
+  ret i32 %sel
+}
+
+; Negative test: FV is not ctpop, should not transform
+define i32 @select_of_ctpop_negative_fv(i1 %cond, i32 %x, i32 %y) {
+; CHECK-LABEL: @select_of_ctpop_negative_fv(
+; CHECK-NEXT:    [[CTPOP1:%.*]] = call range(i32 0, 33) i32 @llvm.ctpop.i32(i32 [[Y:%.*]])
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i32 [[CTPOP1]], i32 [[X:%.*]]
+; CHECK-NEXT:    ret i32 [[SEL]]
+;
+  %ctpop1 = call i32 @llvm.ctpop.i32(i32 %y)
+  %ctpop2 = add i32 %x, 0
+  %sel = select i1 %cond, i32 %ctpop1, i32 %ctpop2
+  ret i32 %sel
+}
+
+; Negative test: ctpop1 has multiple uses, don't transform
+define i32 @select_of_ctpop_negative_multi_use(i1 %cond, i32 %x, i32 %y) {
+; CHECK-LABEL: @select_of_ctpop_negative_multi_use(
+; CHECK-NEXT:    [[CTPOP1:%.*]] = call range(i32 0, 33) i32 @llvm.ctpop.i32(i32 [[X:%.*]])
+; CHECK-NEXT:    call void @use(i32 [[CTPOP1]])
+; CHECK-NEXT:    [[CTPOP2:%.*]] = call range(i32 0, 33) i32 @llvm.ctpop.i32(i32 [[Y:%.*]])
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i32 [[CTPOP1]], i32 [[CTPOP2]]
+; CHECK-NEXT:    ret i32 [[SEL]]
+;
+  %ctpop1 = call i32 @llvm.ctpop.i32(i32 %x)
+  call void @use(i32 %ctpop1)
+  %ctpop2 = call i32 @llvm.ctpop.i32(i32 %y)
+  %sel = select i1 %cond, i32 %ctpop1, i32 %ctpop2
+  ret i32 %sel
+}
+
+define <4 x i32> @select_of_ctpop_vec(<4 x i1> %cond, <4 x i32> %x, <4 x i32> %y) {
+; CHECK-LABEL: @select_of_ctpop_vec(
+; CHECK-NEXT:    [[TMP1:%.*]] = select <4 x i1> [[COND:%.*]], <4 x i32> [[X:%.*]], <4 x i32> [[Y:%.*]]
+; CHECK-NEXT:    [[SEL:%.*]] = call range(i32 0, 33) <4 x i32> @llvm.ctpop.v4i32(<4 x i32> [[TMP1]])
+; CHECK-NEXT:    ret <4 x i32> [[SEL]]
+;
+  %ctpop1 = call <4 x i32> @llvm.ctpop.v4i32(<4 x i32> %x)
+  %ctpop2 = call <4 x i32> @llvm.ctpop.v4i32(<4 x i32> %y)
+  %sel = select <4 x i1> %cond, <4 x i32> %ctpop1, <4 x i32> %ctpop2
+  ret <4 x i32> %sel
+}
+
+define i32 @select_of_ctlz(i1 %cond, i32 %x, i32 %y) {
+; CHECK-LABEL: @select_of_ctlz(
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[COND:%.*]], i32 [[X:%.*]], i32 [[Y:%.*]]
+; CHECK-NEXT:    [[SEL:%.*]] = call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 [[TMP1]], i1 false)
+; CHECK-NEXT:    ret i32 [[SEL]]
+;
+  %ctlz1 = call i32 @llvm.ctlz.i32(i32 %x, i1 false)
+  %ctlz2 = call i32 @llvm.ctlz.i32(i32 %y, i1 false)
+  %sel = select i1 %cond, i32 %ctlz1, i32 %ctlz2
+  ret i32 %sel
+}
+
+define i32 @select_of_ctlz_zero_poison_false(i1 %cond, i32 %x, i32 %y) {
+; CHECK-LABEL: @select_of_ctlz_zero_poison_false(
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[COND:%.*]], i32 [[X:%.*]], i32 [[Y:%.*]]
+; CHECK-NEXT:    [[SEL:%.*]] = call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 [[TMP1]], i1 false)
+; CHECK-NEXT:    ret i32 [[SEL]]
+;
+  %ctlz1 = call i32 @llvm.ctlz.i32(i32 %x, i1 false)
+  %ctlz2 = call i32 @llvm.ctlz.i32(i32 %y, i1 true)
+  %sel = select i1 %cond, i32 %ctlz1, i32 %ctlz2
+  ret i32 %sel
+}
+
+define i32 @select_of_ctlz_zero_poison_true(i1 %cond, i32 %x, i32 %y) {
+; CHECK-LABEL: @select_of_ctlz_zero_poison_true(
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[COND:%.*]], i32 [[X:%.*]], i32 [[Y:%.*]]
+; CHECK-NEXT:    [[SEL:%.*]] = call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 [[TMP1]], i1 true)
+; CHECK-NEXT:    ret i32 [[SEL]]
+;
+  %ctlz1 = call i32 @llvm.ctlz.i32(i32 %x, i1 true)
+  %ctlz2 = call i32 @llvm.ctlz.i32(i32 %y, i1 true)
+  %sel = select i1 %cond, i32 %ctlz1, i32 %ctlz2
+  ret i32 %sel
+}
+
+; Negative test: ctlz1 has multiple uses, don't transform
+define i32 @select_of_ctlz_negative_multi_use(i1 %cond, i32 %x, i32 %y) {
+; CHECK-LABEL: @select_of_ctlz_negative_multi_use(
+; CHECK-NEXT:    [[CTLZ1:%.*]] = call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false)
+; CHECK-NEXT:    call void @use(i32 [[CTLZ1]])
+; CHECK-NEXT:    [[CTLZ2:%.*]] = call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 [[Y:%.*]], i1 false)
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i32 [[CTLZ1]], i32 [[CTLZ2]]
+; CHECK-NEXT:    ret i32 [[SEL]]
+;
+  %ctlz1 = call i32 @llvm.ctlz.i32(i32 %x, i1 false)
+  call void @use(i32 %ctlz1)
+  %ctlz2 = call i32 @llvm.ctlz.i32(i32 %y, i1 false)
+  %sel = select i1 %cond, i32 %ctlz1, i32 %ctlz2
+  ret i32 %sel
+}
+
+define <4 x i32> @select_of_ctlz_vec(<4 x i1> %cond, <4 x i32> %x, <4 x i32> %y) {
+; CHECK-LABEL: @select_of_ctlz_vec(
+; CHECK-NEXT:    [[CTLZ2:%.*]] = call range(i32 0, 33) <4 x i32> @llvm.ctlz.v4i32(<4 x i32> [[Y:%.*]], i1 false)
+; CHECK-NEXT:    ret <4 x i32> [[CTLZ2]]
+;
+  %ctlz1 = call <4 x i32> @llvm.ctlz.v4i32(<4 x i32> %x, i1 false)
+  %ctlz2 = call <4 x i32> @llvm.ctlz.v4i32(<4 x i32> %y, i1 false)
+  %sel = select <4 x i1> %cond, <4 x i32> %ctlz2, <4 x i32> %ctlz2
+  ret <4 x i32> %sel
+}
+
+define i32 @select_of_cttz(i1 %cond, i32 %x, i32 %y) {
+; CHECK-LABEL: @select_of_cttz(
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[COND:%.*]], i32 [[X:%.*]], i32 [[Y:%.*]]
+; CHECK-NEXT:    [[SEL:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[TMP1]], i1 false)
+; CHECK-NEXT:    ret i32 [[SEL]]
+;
+  %cttz1 = call i32 @llvm.cttz.i32(i32 %x, i1 false)
+  %cttz2 = call i32 @llvm.cttz.i32(i32 %y, i1 false)
+  %sel = select i1 %cond, i32 %cttz1, i32 %cttz2
+  ret i32 %sel
+}
+
+define i32 @select_of_abs(i1 %cond, i32 %x, i32 %y) {
+; CHECK-LABEL: @select_of_abs(
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[COND:%.*]], i32 [[X:%.*]], i32 [[Y:%.*]]
+; CHECK-NEXT:    [[SEL:%.*]] = call i32 @llvm.abs.i32(i32 [[TMP1]], i1 false)
+; CHECK-NEXT:    ret i32 [[SEL]]
+;
+  %abs1 = call i32 @llvm.abs.i32(i32 %x, i1 false)
+  %abs2 = call i32 @llvm.abs.i32(i32 %y, i1 false)
+  %sel = select i1 %cond, i32 %abs1, i32 %abs2
+  ret i32 %sel
+}


        


More information about the llvm-commits mailing list