Skip to content

Commit 7bbc049

Browse files
authored
[InstCombine] Consolidate another fold into select value equivalence (#117746)
We had a separate fold that handled just the trivial case where we're replacing exactly the argument of the select. Handle this in select value equivalence by relaxing the infinite loop protection to allow a replacement of a non-constant with a constant. This also fixes #113301, as the separate fold did not handle undef values correctly.
1 parent 979a035 commit 7bbc049

File tree

2 files changed

+2
-14
lines changed

2 files changed

+2
-14
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
13321332
// If we will be able to evaluate f(Y) to a constant, we can allow undef,
13331333
// otherwise Y cannot be undef as we might pick different values for undef
13341334
// in the cmp and in f(Y).
1335-
if (TrueVal == OldOp)
1335+
if (TrueVal == OldOp && (isa<Constant>(OldOp) || !isa<Constant>(NewOp)))
13361336
return nullptr;
13371337

13381338
if (Value *V = simplifyWithOpReplaced(TrueVal, OldOp, NewOp, SQ,
@@ -1966,17 +1966,6 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
19661966
ICmpInst::Predicate Pred = ICI->getPredicate();
19671967
Value *CmpLHS = ICI->getOperand(0);
19681968
Value *CmpRHS = ICI->getOperand(1);
1969-
if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS) && !isa<Constant>(CmpLHS)) {
1970-
if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) {
1971-
// Transform (X == C) ? X : Y -> (X == C) ? C : Y
1972-
replaceOperand(SI, 1, CmpRHS);
1973-
Changed = true;
1974-
} else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) {
1975-
// Transform (X != C) ? Y : X -> (X != C) ? Y : C
1976-
replaceOperand(SI, 2, CmpRHS);
1977-
Changed = true;
1978-
}
1979-
}
19801969

19811970
if (Instruction *NewSel = foldSelectICmpEq(SI, ICI, *this))
19821971
return NewSel;

llvm/test/Transforms/InstCombine/select-value-equivalence.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,11 @@ define <2 x i8> @select_vec_op_const_no_undef(<2 x i8> %x) {
322322
ret <2 x i8> %xr
323323
}
324324

325-
; FIXME: This is a miscompile.
326325
define <2 x i8> @select_vec_op_const_undef(<2 x i8> %x) {
327326
; CHECK-LABEL: define <2 x i8> @select_vec_op_const_undef(
328327
; CHECK-SAME: <2 x i8> [[X:%.*]]) {
329328
; CHECK-NEXT: [[XZ:%.*]] = icmp eq <2 x i8> [[X]], <i8 1, i8 undef>
330-
; CHECK-NEXT: [[XR:%.*]] = select <2 x i1> [[XZ]], <2 x i8> <i8 1, i8 undef>, <2 x i8> <i8 4, i8 3>
329+
; CHECK-NEXT: [[XR:%.*]] = select <2 x i1> [[XZ]], <2 x i8> [[X]], <2 x i8> <i8 4, i8 3>
331330
; CHECK-NEXT: ret <2 x i8> [[XR]]
332331
;
333332
%xz = icmp eq <2 x i8> %x, <i8 1, i8 undef>

0 commit comments

Comments
 (0)