@@ -1262,27 +1262,23 @@ bool InstCombinerImpl::replaceInInstruction(Value *V, Value *Old, Value *New,
12621262// /
12631263// / We can't replace %sel with %add unless we strip away the flags.
12641264// / TODO: Wrapping flags could be preserved in some cases with better analysis.
1265- Instruction *InstCombinerImpl::foldSelectValueEquivalence (SelectInst &Sel,
1266- ICmpInst &Cmp ) {
1267- if (!Cmp. isEquality ())
1265+ Instruction *InstCombinerImpl::foldSelectValueEquivalence (
1266+ SelectInst &Sel, ICmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS ) {
1267+ if (!ICmpInst:: isEquality (Pred ))
12681268 return nullptr ;
12691269
12701270 // Canonicalize the pattern to ICMP_EQ by swapping the select operands.
12711271 Value *TrueVal = Sel.getTrueValue (), *FalseVal = Sel.getFalseValue ();
12721272 bool Swapped = false ;
1273- if (Cmp. getPredicate () == ICmpInst::ICMP_NE) {
1273+ if (Pred == ICmpInst::ICMP_NE) {
12741274 std::swap (TrueVal, FalseVal);
12751275 Swapped = true ;
12761276 }
12771277
12781278 // In X == Y ? f(X) : Z, try to evaluate f(Y) and replace the operand.
12791279 // Make sure Y cannot be undef though, as we might pick different values for
1280- // undef in the icmp and in f(Y). Additionally, take care to avoid replacing
1281- // X == Y ? X : Z with X == Y ? Y : Z, as that would lead to an infinite
1282- // replacement cycle.
1283- Value *CmpLHS = Cmp.getOperand (0 ), *CmpRHS = Cmp.getOperand (1 );
1284- if (TrueVal != CmpLHS &&
1285- isGuaranteedNotToBeUndefOrPoison (CmpRHS, SQ.AC , &Sel, &DT)) {
1280+ // undef in the icmp and in f(Y).
1281+ if (isGuaranteedNotToBeUndefOrPoison (CmpRHS, SQ.AC , &Sel, &DT)) {
12861282 if (Value *V = simplifyWithOpReplaced (TrueVal, CmpLHS, CmpRHS, SQ,
12871283 /* AllowRefinement */ true ))
12881284 // Require either the replacement or the simplification result to be a
@@ -1299,7 +1295,7 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
12991295 // profitability is not clear for other cases.
13001296 // FIXME: Support vectors.
13011297 if (match (CmpRHS, m_ImmConstant ()) && !match (CmpLHS, m_ImmConstant ()) &&
1302- !Cmp. getType ()->isVectorTy ())
1298+ !CmpLHS-> getType ()->isVectorTy ())
13031299 if (replaceInInstruction (TrueVal, CmpLHS, CmpRHS))
13041300 return &Sel;
13051301 }
@@ -1680,7 +1676,8 @@ static Value *foldSelectInstWithICmpConst(SelectInst &SI, ICmpInst *ICI,
16801676// / Visit a SelectInst that has an ICmpInst as its first operand.
16811677Instruction *InstCombinerImpl::foldSelectInstWithICmp (SelectInst &SI,
16821678 ICmpInst *ICI) {
1683- if (Instruction *NewSel = foldSelectValueEquivalence (SI, *ICI))
1679+ if (Instruction *NewSel = foldSelectValueEquivalence (
1680+ SI, ICI->getPredicate (), ICI->getOperand (0 ), ICI->getOperand (1 )))
16841681 return NewSel;
16851682
16861683 if (Value *V =
@@ -3376,21 +3373,15 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
33763373 if (Instruction *I = canonicalizeScalarSelectOfVecs (SI, *this ))
33773374 return I;
33783375
3379- // If the type of select is not an integer type or if the condition and
3380- // the selection type are not both scalar nor both vector types, there is no
3381- // point in attempting to match these patterns.
33823376 Type *CondType = CondVal->getType ();
3383- if (!isa<Constant>(CondVal) && SelType->isIntOrIntVectorTy () &&
3384- CondType->isVectorTy () == SelType->isVectorTy ()) {
3385- if (Value *S = simplifyWithOpReplaced (TrueVal, CondVal,
3386- ConstantInt::getTrue (CondType), SQ,
3387- /* AllowRefinement */ true ))
3388- return replaceOperand (SI, 1 , S);
3377+ if (!isa<Constant>(CondVal)) {
3378+ if (Instruction *I = foldSelectValueEquivalence (
3379+ SI, ICmpInst::ICMP_EQ, CondVal, ConstantInt::getTrue (CondType)))
3380+ return I;
33893381
3390- if (Value *S = simplifyWithOpReplaced (FalseVal, CondVal,
3391- ConstantInt::getFalse (CondType), SQ,
3392- /* AllowRefinement */ true ))
3393- return replaceOperand (SI, 2 , S);
3382+ if (Instruction *I = foldSelectValueEquivalence (
3383+ SI, ICmpInst::ICMP_NE, CondVal, ConstantInt::getFalse (CondType)))
3384+ return I;
33943385 }
33953386
33963387 if (Instruction *R = foldSelectOfBools (SI))
0 commit comments