@@ -4466,13 +4466,15 @@ static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
4466
4466
} else
4467
4467
return nullptr ;
4468
4468
}
4469
- Constant *Res = ConstantFoldInstOperands (I, ConstOps, Q.DL , Q.TLI );
4469
+ Constant *Res = ConstantFoldInstOperands (I, ConstOps, Q.DL , Q.TLI ,
4470
+ /* AllowNonDeterministic=*/ false );
4470
4471
if (DropFlags && Res && I->hasPoisonGeneratingAnnotations ())
4471
4472
DropFlags->push_back (I);
4472
4473
return Res;
4473
4474
}
4474
4475
4475
- return ConstantFoldInstOperands (I, ConstOps, Q.DL , Q.TLI );
4476
+ return ConstantFoldInstOperands (I, ConstOps, Q.DL , Q.TLI ,
4477
+ /* AllowNonDeterministic=*/ false );
4476
4478
}
4477
4479
4478
4480
Value *llvm::simplifyWithOpReplaced (Value *V, Value *Op, Value *RepOp,
@@ -4616,11 +4618,11 @@ static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS,
4616
4618
}
4617
4619
4618
4620
// / 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) {
4624
4626
if (simplifyWithOpReplaced (FalseVal, CmpLHS, CmpRHS, Q.getWithoutUndef (),
4625
4627
/* AllowRefinement */ false ,
4626
4628
/* DropFlags */ nullptr , MaxRecurse) == TrueVal)
@@ -4721,11 +4723,11 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
4721
4723
// the arms of the select. See if substituting this value into the arm and
4722
4724
// simplifying the result yields the same value as the other arm.
4723
4725
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))
4726
4728
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))
4729
4731
return V;
4730
4732
4731
4733
Value *X;
@@ -4734,23 +4736,23 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
4734
4736
if (match (CmpLHS, m_Or (m_Value (X), m_Value (Y))) &&
4735
4737
match (CmpRHS, m_Zero ())) {
4736
4738
// (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))
4739
4741
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))
4742
4744
return V;
4743
4745
}
4744
4746
4745
4747
// select((X & Y) == -1 ? X : -1) --> -1 (commuted 2 ways)
4746
4748
if (match (CmpLHS, m_And (m_Value (X), m_Value (Y))) &&
4747
4749
match (CmpRHS, m_AllOnes ())) {
4748
4750
// (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))
4751
4753
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))
4754
4756
return V;
4755
4757
}
4756
4758
}
@@ -4761,27 +4763,46 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
4761
4763
// / Try to simplify a select instruction when its condition operand is a
4762
4764
// / floating-point comparison.
4763
4765
static Value *simplifySelectWithFCmp (Value *Cond, Value *T, Value *F,
4764
- const SimplifyQuery &Q) {
4766
+ const SimplifyQuery &Q,
4767
+ unsigned MaxRecurse) {
4765
4768
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)
4768
4797
return nullptr ;
4769
4798
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 ()) {
4778
4801
// (T == F) ? T : F --> F
4779
- // (F == T) ? T : F --> F
4780
4802
if (Pred == FCmpInst::FCMP_OEQ)
4781
4803
return F;
4782
4804
4783
4805
// (T != F) ? T : F --> T
4784
- // (F != T) ? T : F --> T
4785
4806
if (Pred == FCmpInst::FCMP_UNE)
4786
4807
return T;
4787
4808
}
@@ -4955,7 +4976,7 @@ static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
4955
4976
simplifySelectWithICmpCond (Cond, TrueVal, FalseVal, Q, MaxRecurse))
4956
4977
return V;
4957
4978
4958
- if (Value *V = simplifySelectWithFCmp (Cond, TrueVal, FalseVal, Q))
4979
+ if (Value *V = simplifySelectWithFCmp (Cond, TrueVal, FalseVal, Q, MaxRecurse ))
4959
4980
return V;
4960
4981
4961
4982
if (Value *V = foldSelectWithBinaryOp (Cond, TrueVal, FalseVal))
0 commit comments