Skip to content

[InstCombine] Consolidate another fold into select value equivalence #117746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,7 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
// If we will be able to evaluate f(Y) to a constant, we can allow undef,
// otherwise Y cannot be undef as we might pick different values for undef
// in the cmp and in f(Y).
if (TrueVal == OldOp)
if (TrueVal == OldOp && (isa<Constant>(OldOp) || !isa<Constant>(NewOp)))
return nullptr;

if (Value *V = simplifyWithOpReplaced(TrueVal, OldOp, NewOp, SQ,
Expand Down Expand Up @@ -1925,17 +1925,6 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
ICmpInst::Predicate Pred = ICI->getPredicate();
Value *CmpLHS = ICI->getOperand(0);
Value *CmpRHS = ICI->getOperand(1);
if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS) && !isa<Constant>(CmpLHS)) {
if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) {
// Transform (X == C) ? X : Y -> (X == C) ? C : Y
replaceOperand(SI, 1, CmpRHS);
Changed = true;
} else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) {
// Transform (X != C) ? Y : X -> (X != C) ? Y : C
replaceOperand(SI, 2, CmpRHS);
Changed = true;
}
}

if (Instruction *NewSel = foldSelectICmpEq(SI, ICI, *this))
return NewSel;
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/InstCombine/select-value-equivalence.ll
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,11 @@ define <2 x i8> @select_vec_op_const_no_undef(<2 x i8> %x) {
ret <2 x i8> %xr
}

; FIXME: This is a miscompile.
define <2 x i8> @select_vec_op_const_undef(<2 x i8> %x) {
; CHECK-LABEL: define <2 x i8> @select_vec_op_const_undef(
; CHECK-SAME: <2 x i8> [[X:%.*]]) {
; CHECK-NEXT: [[XZ:%.*]] = icmp eq <2 x i8> [[X]], <i8 1, i8 undef>
; CHECK-NEXT: [[XR:%.*]] = select <2 x i1> [[XZ]], <2 x i8> <i8 1, i8 undef>, <2 x i8> <i8 4, i8 3>
; CHECK-NEXT: [[XR:%.*]] = select <2 x i1> [[XZ]], <2 x i8> [[X]], <2 x i8> <i8 4, i8 3>
; CHECK-NEXT: ret <2 x i8> [[XR]]
;
%xz = icmp eq <2 x i8> %x, <i8 1, i8 undef>
Expand Down