@@ -1781,6 +1781,46 @@ static Value *foldSelectInstWithICmpConst(SelectInst &SI, ICmpInst *ICI,
1781
1781
return nullptr ;
1782
1782
}
1783
1783
1784
+ // / `A == MIN_INT ? B != MIN_INT : A < B` --> `A < B`
1785
+ // / `A == MAX_INT ? B != MAX_INT : A > B` --> `A > B`
1786
+ static Instruction *foldSelectWithExtremeEqCond (Value *CmpLHS, Value *CmpRHS,
1787
+ Value *TrueVal,
1788
+ Value *FalseVal) {
1789
+ Type *Ty = CmpLHS->getType ();
1790
+
1791
+ if (Ty->isPtrOrPtrVectorTy ())
1792
+ return nullptr ;
1793
+
1794
+ CmpPredicate Pred;
1795
+ Value *B;
1796
+
1797
+ if (!match (FalseVal, m_c_ICmp (Pred, m_Specific (CmpLHS), m_Value (B))))
1798
+ return nullptr ;
1799
+
1800
+ Value *TValRHS;
1801
+ if (!match (TrueVal, m_SpecificICmp (ICmpInst::ICMP_NE, m_Specific (B),
1802
+ m_Value (TValRHS))))
1803
+ return nullptr ;
1804
+
1805
+ APInt C;
1806
+ unsigned BitWidth = Ty->getScalarSizeInBits ();
1807
+
1808
+ if (ICmpInst::isLT (Pred)) {
1809
+ C = CmpInst::isSigned (Pred) ? APInt::getSignedMinValue (BitWidth)
1810
+ : APInt::getMinValue (BitWidth);
1811
+ } else if (ICmpInst::isGT (Pred)) {
1812
+ C = CmpInst::isSigned (Pred) ? APInt::getSignedMaxValue (BitWidth)
1813
+ : APInt::getMaxValue (BitWidth);
1814
+ } else {
1815
+ return nullptr ;
1816
+ }
1817
+
1818
+ if (!match (CmpRHS, m_SpecificInt (C)) || !match (TValRHS, m_SpecificInt (C)))
1819
+ return nullptr ;
1820
+
1821
+ return new ICmpInst (Pred, CmpLHS, B);
1822
+ }
1823
+
1784
1824
static Instruction *foldSelectICmpEq (SelectInst &SI, ICmpInst *ICI,
1785
1825
InstCombinerImpl &IC) {
1786
1826
ICmpInst::Predicate Pred = ICI->getPredicate ();
@@ -1795,6 +1835,10 @@ static Instruction *foldSelectICmpEq(SelectInst &SI, ICmpInst *ICI,
1795
1835
if (Pred == ICmpInst::ICMP_NE)
1796
1836
std::swap (TrueVal, FalseVal);
1797
1837
1838
+ if (Instruction *Res =
1839
+ foldSelectWithExtremeEqCond (CmpLHS, CmpRHS, TrueVal, FalseVal))
1840
+ return Res;
1841
+
1798
1842
// Transform (X == C) ? X : Y -> (X == C) ? C : Y
1799
1843
// specific handling for Bitwise operation.
1800
1844
// x&y -> (x|y) ^ (x^y) or (x|y) & ~(x^y)
0 commit comments