Skip to content

Commit 651091a

Browse files
committed
[InstCombine] Consolidate another fold into select value equivalence
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 88cff86 commit 651091a

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
@@ -1348,7 +1348,7 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
13481348
// If we will be able to evaluate f(Y) to a constant, we can allow undef,
13491349
// otherwise Y cannot be undef as we might pick different values for undef
13501350
// in the cmp and in f(Y).
1351-
if (TrueVal == OldOp)
1351+
if (TrueVal == OldOp && (isa<Constant>(OldOp) || !isa<Constant>(NewOp)))
13521352
return nullptr;
13531353

13541354
if (Value *V = simplifyWithOpReplaced(TrueVal, OldOp, NewOp, SQ,
@@ -1925,17 +1925,6 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
19251925
ICmpInst::Predicate Pred = ICI->getPredicate();
19261926
Value *CmpLHS = ICI->getOperand(0);
19271927
Value *CmpRHS = ICI->getOperand(1);
1928-
if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS) && !isa<Constant>(CmpLHS)) {
1929-
if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) {
1930-
// Transform (X == C) ? X : Y -> (X == C) ? C : Y
1931-
replaceOperand(SI, 1, CmpRHS);
1932-
Changed = true;
1933-
} else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) {
1934-
// Transform (X != C) ? Y : X -> (X != C) ? Y : C
1935-
replaceOperand(SI, 2, CmpRHS);
1936-
Changed = true;
1937-
}
1938-
}
19391928

19401929
if (Instruction *NewSel = foldSelectICmpEq(SI, ICI, *this))
19411930
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)