From b37a4b9991a0a669594b53caa0eb75f489211d69 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Sun, 23 Jun 2024 00:16:58 +0800 Subject: [InstCombine] Improve coverage of `foldSelectValueEquivalence` for non-constants If f(Y) simplifies to Y, replace with Y. This requires Y to be non-undef. Closes #94719 --- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | 2 +- llvm/test/Transforms/InstCombine/select-cmp-eq-op-fold.ll | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index fbac209..27563d4 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -1310,7 +1310,7 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel, // If NewOp is a constant and OldOp is not replace iff NewOp doesn't // contain and undef elements. - if (match(NewOp, m_ImmConstant())) { + if (match(NewOp, m_ImmConstant()) || NewOp == V) { if (isGuaranteedNotToBeUndef(NewOp, SQ.AC, &Sel, &DT)) return replaceOperand(Sel, Swapped ? 2 : 1, V); return nullptr; diff --git a/llvm/test/Transforms/InstCombine/select-cmp-eq-op-fold.ll b/llvm/test/Transforms/InstCombine/select-cmp-eq-op-fold.ll index ec82b19..2f2c2d3 100644 --- a/llvm/test/Transforms/InstCombine/select-cmp-eq-op-fold.ll +++ b/llvm/test/Transforms/InstCombine/select-cmp-eq-op-fold.ll @@ -6,8 +6,7 @@ declare void @use.i8(i8) define i8 @replace_with_y_noundef(i8 %x, i8 noundef %y, i8 %z) { ; CHECK-LABEL: @replace_with_y_noundef( ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], [[Y]] -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[AND]], i8 [[Z:%.*]] +; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[Y]], i8 [[Z:%.*]] ; CHECK-NEXT: ret i8 [[SEL]] ; %cmp = icmp eq i8 %x, %y @@ -20,8 +19,7 @@ define i8 @replace_with_x_noundef(i8 noundef %x, i8 %y, i8 %z) { ; CHECK-LABEL: @replace_with_x_noundef( ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: call void @use.i1(i1 [[CMP]]) -; CHECK-NEXT: [[AND:%.*]] = or i8 [[X]], [[Y]] -; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[Z:%.*]], i8 [[AND]] +; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[Z:%.*]], i8 [[X]] ; CHECK-NEXT: ret i8 [[SEL]] ; %cmp = icmp ne i8 %x, %y -- cgit v1.1