forked from OSchip/llvm-project
[ValueTracking] tighten up matchMinMax(); NFCI
This is similar to what the caller (matchSelectPattern()) does. In all cases where we succeed in matching a min/max pattern, the values in that pattern will be the values of the 'select', so hoist that and remove a bunch of duplicated code. llvm-svn: 292725
This commit is contained in:
parent
842a3b7000
commit
24c6f88e4c
|
@ -3918,6 +3918,10 @@ static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
|
|||
Value *CmpLHS, Value *CmpRHS,
|
||||
Value *TrueVal, Value *FalseVal,
|
||||
Value *&LHS, Value *&RHS) {
|
||||
// Assume success. If there's no match, callers should not use these anyway.
|
||||
LHS = TrueVal;
|
||||
RHS = FalseVal;
|
||||
|
||||
// Recognize variations of:
|
||||
// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
|
||||
const APInt *C1;
|
||||
|
@ -3926,35 +3930,23 @@ static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
|
|||
|
||||
// (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
|
||||
if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
|
||||
C1->slt(*C2) && Pred == CmpInst::ICMP_SLT) {
|
||||
LHS = TrueVal;
|
||||
RHS = FalseVal;
|
||||
C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
|
||||
return {SPF_SMAX, SPNB_NA, false};
|
||||
}
|
||||
|
||||
// (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
|
||||
if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
|
||||
C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT) {
|
||||
LHS = TrueVal;
|
||||
RHS = FalseVal;
|
||||
C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
|
||||
return {SPF_SMIN, SPNB_NA, false};
|
||||
}
|
||||
|
||||
// (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
|
||||
if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
|
||||
C1->ult(*C2) && Pred == CmpInst::ICMP_ULT) {
|
||||
LHS = TrueVal;
|
||||
RHS = FalseVal;
|
||||
C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
|
||||
return {SPF_UMAX, SPNB_NA, false};
|
||||
}
|
||||
|
||||
// (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
|
||||
if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
|
||||
C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT) {
|
||||
LHS = TrueVal;
|
||||
RHS = FalseVal;
|
||||
C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
|
||||
return {SPF_UMIN, SPNB_NA, false};
|
||||
}
|
||||
}
|
||||
|
||||
if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
|
||||
|
@ -3964,21 +3956,15 @@ static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
|
|||
// (X >s Y) ? 0 : Z ==> (Z >s 0) ? 0 : Z ==> SMIN(Z, 0)
|
||||
// (X <s Y) ? 0 : Z ==> (Z <s 0) ? 0 : Z ==> SMAX(Z, 0)
|
||||
if (match(TrueVal, m_Zero()) &&
|
||||
match(FalseVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS)))) {
|
||||
LHS = TrueVal;
|
||||
RHS = FalseVal;
|
||||
match(FalseVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS))))
|
||||
return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false};
|
||||
}
|
||||
|
||||
// Z = X -nsw Y
|
||||
// (X >s Y) ? Z : 0 ==> (Z >s 0) ? Z : 0 ==> SMAX(Z, 0)
|
||||
// (X <s Y) ? Z : 0 ==> (Z <s 0) ? Z : 0 ==> SMIN(Z, 0)
|
||||
if (match(FalseVal, m_Zero()) &&
|
||||
match(TrueVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS)))) {
|
||||
LHS = TrueVal;
|
||||
RHS = FalseVal;
|
||||
match(TrueVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS))))
|
||||
return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false};
|
||||
}
|
||||
|
||||
if (!match(CmpRHS, m_APInt(C1)))
|
||||
return {SPF_UNKNOWN, SPNB_NA, false};
|
||||
|
@ -3990,41 +3976,29 @@ static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
|
|||
// Is the sign bit set?
|
||||
// (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
|
||||
// (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
|
||||
if (Pred == CmpInst::ICMP_SLT && *C1 == 0 && C2->isMaxSignedValue()) {
|
||||
LHS = TrueVal;
|
||||
RHS = FalseVal;
|
||||
if (Pred == CmpInst::ICMP_SLT && *C1 == 0 && C2->isMaxSignedValue())
|
||||
return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
|
||||
}
|
||||
|
||||
// Is the sign bit clear?
|
||||
// (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
|
||||
// (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
|
||||
if (Pred == CmpInst::ICMP_SGT && C1->isAllOnesValue() &&
|
||||
C2->isMinSignedValue()) {
|
||||
LHS = TrueVal;
|
||||
RHS = FalseVal;
|
||||
C2->isMinSignedValue())
|
||||
return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
|
||||
}
|
||||
}
|
||||
|
||||
// Look through 'not' ops to find disguised signed min/max.
|
||||
// (X >s C) ? ~X : ~C ==> (~X <s ~C) ? ~X : ~C ==> SMIN(~X, ~C)
|
||||
// (X <s C) ? ~X : ~C ==> (~X >s ~C) ? ~X : ~C ==> SMAX(~X, ~C)
|
||||
if (match(TrueVal, m_Not(m_Specific(CmpLHS))) &&
|
||||
match(FalseVal, m_APInt(C2)) && ~(*C1) == *C2) {
|
||||
LHS = TrueVal;
|
||||
RHS = FalseVal;
|
||||
match(FalseVal, m_APInt(C2)) && ~(*C1) == *C2)
|
||||
return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false};
|
||||
}
|
||||
|
||||
// (X >s C) ? ~C : ~X ==> (~X <s ~C) ? ~C : ~X ==> SMAX(~C, ~X)
|
||||
// (X <s C) ? ~C : ~X ==> (~X >s ~C) ? ~C : ~X ==> SMIN(~C, ~X)
|
||||
if (match(FalseVal, m_Not(m_Specific(CmpLHS))) &&
|
||||
match(TrueVal, m_APInt(C2)) && ~(*C1) == *C2) {
|
||||
LHS = TrueVal;
|
||||
RHS = FalseVal;
|
||||
match(TrueVal, m_APInt(C2)) && ~(*C1) == *C2)
|
||||
return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false};
|
||||
}
|
||||
|
||||
return {SPF_UNKNOWN, SPNB_NA, false};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue