[llvm] 539a51a - [InstCombine] narrow select to match trunc condition operand size (#219794)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 30 10:05:02 PDT 2026


Author: Andreas Jonson
Date: 2026-08-30T19:04:56+02:00
New Revision: 539a51af8f5bb5955db36c6ba606522fe1974de4

URL: https://github.com/llvm/llvm-project/commit/539a51af8f5bb5955db36c6ba606522fe1974de4
DIFF: https://github.com/llvm/llvm-project/commit/539a51af8f5bb5955db36c6ba606522fe1974de4.diff

LOG: [InstCombine] narrow select to match trunc condition operand size (#219794)

changed to matches the cmp handling and avoids some regressions from
https://github.com/llvm/llvm-project/pull/184182

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/test/Transforms/InstCombine/select-bitext.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index f785ea182a389..81d949fd1fb28 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2718,9 +2718,9 @@ Instruction *InstCombinerImpl::foldSelectExtConst(SelectInst &Sel) {
   Value *X = ExtInst->getOperand(0);
   Type *SmallType = X->getType();
   Value *Cond = Sel.getCondition();
-  auto *Cmp = dyn_cast<CmpInst>(Cond);
   if (!SmallType->isIntOrIntVectorTy(1) &&
-      (!Cmp || Cmp->getOperand(0)->getType() != SmallType))
+      (!isa<CmpInst, TruncInst>(Cond) ||
+       cast<Instruction>(Cond)->getOperand(0)->getType() != SmallType))
     return nullptr;
 
   // If the constant is the same after truncation to the smaller type and

diff  --git a/llvm/test/Transforms/InstCombine/select-bitext.ll b/llvm/test/Transforms/InstCombine/select-bitext.ll
index 553d5ae155153..ea02ea69222e2 100644
--- a/llvm/test/Transforms/InstCombine/select-bitext.ll
+++ b/llvm/test/Transforms/InstCombine/select-bitext.ll
@@ -646,5 +646,31 @@ define <2 x i32> @zext_false_val_must_be_zero_vec(<2 x i1> %x) {
   ret <2 x i32> %sel
 }
 
+define i32 @sel_trunc_cond_zext_const(i8 %a, i8 %x) {
+; CHECK-LABEL: @sel_trunc_cond_zext_const(
+; CHECK-NEXT:    [[COND:%.*]] = trunc i8 [[X:%.*]] to i1
+; CHECK-NEXT:    [[A:%.*]] = select i1 [[COND]], i8 [[A1:%.*]], i8 127
+; CHECK-NEXT:    [[A_EXT:%.*]] = zext i8 [[A]] to i32
+; CHECK-NEXT:    ret i32 [[A_EXT]]
+;
+  %cond = trunc i8 %x to i1
+  %a.ext = zext i8 %a to i32
+  %r = select i1 %cond, i32 %a.ext, i32 127
+  ret i32 %r
+}
+
+define i32 @sel_trunc_cond_sext_const(i8 %a, i8 %x) {
+; CHECK-LABEL: @sel_trunc_cond_sext_const(
+; CHECK-NEXT:    [[COND:%.*]] = trunc i8 [[X:%.*]] to i1
+; CHECK-NEXT:    [[A:%.*]] = select i1 [[COND]], i8 [[A1:%.*]], i8 -4
+; CHECK-NEXT:    [[A_EXT:%.*]] = sext i8 [[A]] to i32
+; CHECK-NEXT:    ret i32 [[A_EXT]]
+;
+  %cond = trunc i8 %x to i1
+  %a.ext = sext i8 %a to i32
+  %r = select i1 %cond, i32 %a.ext, i32 -4
+  ret i32 %r
+}
+
 !0 = !{!"branch_weights", i32 3, i32 5}
 


        


More information about the llvm-commits mailing list