aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Goldstein <goldstein.w.n@gmail.com>2024-06-23 00:16:58 +0800
committerNoah Goldstein <goldstein.w.n@gmail.com>2024-06-23 11:15:47 +0800
commitb37a4b9991a0a669594b53caa0eb75f489211d69 (patch)
tree4ff77fd7f939a15411116e4f02ad6a0b511346cb
parent61c4d7b9231d22f8cff7a6a6b3b52d401cba9f6f (diff)
downloadllvm-b37a4b9991a0a669594b53caa0eb75f489211d69.zip
llvm-b37a4b9991a0a669594b53caa0eb75f489211d69.tar.gz
llvm-b37a4b9991a0a669594b53caa0eb75f489211d69.tar.bz2
[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
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp2
-rw-r--r--llvm/test/Transforms/InstCombine/select-cmp-eq-op-fold.ll6
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