@@ -4466,13 +4466,15 @@ static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
44664466 } else
44674467 return nullptr ;
44684468 }
4469- Constant *Res = ConstantFoldInstOperands (I, ConstOps, Q.DL , Q.TLI );
4469+ Constant *Res = ConstantFoldInstOperands (I, ConstOps, Q.DL , Q.TLI ,
4470+ /* AllowNonDeterministic=*/ false );
44704471 if (DropFlags && Res && I->hasPoisonGeneratingAnnotations ())
44714472 DropFlags->push_back (I);
44724473 return Res;
44734474 }
44744475
4475- return ConstantFoldInstOperands (I, ConstOps, Q.DL , Q.TLI );
4476+ return ConstantFoldInstOperands (I, ConstOps, Q.DL , Q.TLI ,
4477+ /* AllowNonDeterministic=*/ false );
44764478}
44774479
44784480Value *llvm::simplifyWithOpReplaced (Value *V, Value *Op, Value *RepOp,
@@ -4616,11 +4618,11 @@ static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS,
46164618}
46174619
46184620// / Try to simplify a select instruction when its condition operand is an
4619- // / integer equality comparison.
4620- static Value *simplifySelectWithICmpEq (Value *CmpLHS, Value *CmpRHS,
4621- Value *TrueVal, Value *FalseVal,
4622- const SimplifyQuery &Q,
4623- unsigned MaxRecurse) {
4621+ // / integer equality or floating-point equivalence comparison.
4622+ static Value *simplifySelectWithEquivalence (Value *CmpLHS, Value *CmpRHS,
4623+ Value *TrueVal, Value *FalseVal,
4624+ const SimplifyQuery &Q,
4625+ unsigned MaxRecurse) {
46244626 if (simplifyWithOpReplaced (FalseVal, CmpLHS, CmpRHS, Q.getWithoutUndef (),
46254627 /* AllowRefinement */ false ,
46264628 /* DropFlags */ nullptr , MaxRecurse) == TrueVal)
@@ -4721,11 +4723,11 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
47214723 // the arms of the select. See if substituting this value into the arm and
47224724 // simplifying the result yields the same value as the other arm.
47234725 if (Pred == ICmpInst::ICMP_EQ) {
4724- if (Value *V = simplifySelectWithICmpEq (CmpLHS, CmpRHS, TrueVal, FalseVal ,
4725- Q, MaxRecurse))
4726+ if (Value *V = simplifySelectWithEquivalence (CmpLHS, CmpRHS, TrueVal,
4727+ FalseVal, Q, MaxRecurse))
47264728 return V;
4727- if (Value *V = simplifySelectWithICmpEq (CmpRHS, CmpLHS, TrueVal, FalseVal ,
4728- Q, MaxRecurse))
4729+ if (Value *V = simplifySelectWithEquivalence (CmpRHS, CmpLHS, TrueVal,
4730+ FalseVal, Q, MaxRecurse))
47294731 return V;
47304732
47314733 Value *X;
@@ -4734,23 +4736,23 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
47344736 if (match (CmpLHS, m_Or (m_Value (X), m_Value (Y))) &&
47354737 match (CmpRHS, m_Zero ())) {
47364738 // (X | Y) == 0 implies X == 0 and Y == 0.
4737- if (Value *V = simplifySelectWithICmpEq (X, CmpRHS, TrueVal, FalseVal, Q ,
4738- MaxRecurse))
4739+ if (Value *V = simplifySelectWithEquivalence (X, CmpRHS, TrueVal, FalseVal,
4740+ Q, MaxRecurse))
47394741 return V;
4740- if (Value *V = simplifySelectWithICmpEq (Y, CmpRHS, TrueVal, FalseVal, Q ,
4741- MaxRecurse))
4742+ if (Value *V = simplifySelectWithEquivalence (Y, CmpRHS, TrueVal, FalseVal,
4743+ Q, MaxRecurse))
47424744 return V;
47434745 }
47444746
47454747 // select((X & Y) == -1 ? X : -1) --> -1 (commuted 2 ways)
47464748 if (match (CmpLHS, m_And (m_Value (X), m_Value (Y))) &&
47474749 match (CmpRHS, m_AllOnes ())) {
47484750 // (X & Y) == -1 implies X == -1 and Y == -1.
4749- if (Value *V = simplifySelectWithICmpEq (X, CmpRHS, TrueVal, FalseVal, Q ,
4750- MaxRecurse))
4751+ if (Value *V = simplifySelectWithEquivalence (X, CmpRHS, TrueVal, FalseVal,
4752+ Q, MaxRecurse))
47514753 return V;
4752- if (Value *V = simplifySelectWithICmpEq (Y, CmpRHS, TrueVal, FalseVal, Q ,
4753- MaxRecurse))
4754+ if (Value *V = simplifySelectWithEquivalence (Y, CmpRHS, TrueVal, FalseVal,
4755+ Q, MaxRecurse))
47544756 return V;
47554757 }
47564758 }
@@ -4761,27 +4763,46 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
47614763// / Try to simplify a select instruction when its condition operand is a
47624764// / floating-point comparison.
47634765static Value *simplifySelectWithFCmp (Value *Cond, Value *T, Value *F,
4764- const SimplifyQuery &Q) {
4766+ const SimplifyQuery &Q,
4767+ unsigned MaxRecurse) {
47654768 FCmpInst::Predicate Pred;
4766- if (!match (Cond, m_FCmp (Pred, m_Specific (T), m_Specific (F))) &&
4767- !match (Cond, m_FCmp (Pred, m_Specific (F), m_Specific (T))))
4769+ Value *CmpLHS, *CmpRHS;
4770+ if (!match (Cond, m_FCmp (Pred, m_Value (CmpLHS), m_Value (CmpRHS))))
4771+ return nullptr ;
4772+ FCmpInst *I = cast<FCmpInst>(Cond);
4773+
4774+ bool IsEquiv = I->isEquivalence ();
4775+ if (I->isEquivalence (/* Invert=*/ true )) {
4776+ std::swap (T, F);
4777+ Pred = FCmpInst::getInversePredicate (Pred);
4778+ IsEquiv = true ;
4779+ }
4780+
4781+ // This transforms is safe if at least one operand is known to not be zero.
4782+ // Otherwise, the select can change the sign of a zero operand.
4783+ if (IsEquiv) {
4784+ if (Value *V =
4785+ simplifySelectWithEquivalence (CmpLHS, CmpRHS, T, F, Q, MaxRecurse))
4786+ return V;
4787+ if (Value *V =
4788+ simplifySelectWithEquivalence (CmpRHS, CmpLHS, T, F, Q, MaxRecurse))
4789+ return V;
4790+ }
4791+
4792+ // Canonicalize CmpLHS to be T, and CmpRHS to be F, if they're swapped.
4793+ if (CmpLHS == F && CmpRHS == T)
4794+ std::swap (CmpLHS, CmpRHS);
4795+
4796+ if (CmpLHS != T || CmpRHS != F)
47684797 return nullptr ;
47694798
4770- // This transform is safe if we do not have (do not care about) -0.0 or if
4771- // at least one operand is known to not be -0.0. Otherwise, the select can
4772- // change the sign of a zero operand.
4773- bool HasNoSignedZeros =
4774- Q.CxtI && isa<FPMathOperator>(Q.CxtI ) && Q.CxtI ->hasNoSignedZeros ();
4775- const APFloat *C;
4776- if (HasNoSignedZeros || (match (T, m_APFloat (C)) && C->isNonZero ()) ||
4777- (match (F, m_APFloat (C)) && C->isNonZero ())) {
4799+ // This transform is also safe if we do not have (do not care about) -0.0.
4800+ if (Q.CxtI && isa<FPMathOperator>(Q.CxtI ) && Q.CxtI ->hasNoSignedZeros ()) {
47784801 // (T == F) ? T : F --> F
4779- // (F == T) ? T : F --> F
47804802 if (Pred == FCmpInst::FCMP_OEQ)
47814803 return F;
47824804
47834805 // (T != F) ? T : F --> T
4784- // (F != T) ? T : F --> T
47854806 if (Pred == FCmpInst::FCMP_UNE)
47864807 return T;
47874808 }
@@ -4955,7 +4976,7 @@ static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
49554976 simplifySelectWithICmpCond (Cond, TrueVal, FalseVal, Q, MaxRecurse))
49564977 return V;
49574978
4958- if (Value *V = simplifySelectWithFCmp (Cond, TrueVal, FalseVal, Q))
4979+ if (Value *V = simplifySelectWithFCmp (Cond, TrueVal, FalseVal, Q, MaxRecurse ))
49594980 return V;
49604981
49614982 if (Value *V = foldSelectWithBinaryOp (Cond, TrueVal, FalseVal))
0 commit comments