@@ -1687,6 +1687,109 @@ static Value *foldSelectInstWithICmpConst(SelectInst &SI, ICmpInst *ICI,
1687
1687
return nullptr ;
1688
1688
}
1689
1689
1690
+ static Instruction *foldSelectICmpEq (SelectInst &SI, ICmpInst *ICI,
1691
+ InstCombinerImpl &IC) {
1692
+ ICmpInst::Predicate Pred = ICI->getPredicate ();
1693
+ if (!ICmpInst::isEquality (Pred))
1694
+ return nullptr ;
1695
+
1696
+ Value *TrueVal = SI.getTrueValue ();
1697
+ Value *FalseVal = SI.getFalseValue ();
1698
+ Value *CmpLHS = ICI->getOperand (0 );
1699
+ Value *CmpRHS = ICI->getOperand (1 );
1700
+
1701
+ if (Pred == ICmpInst::ICMP_NE)
1702
+ std::swap (TrueVal, FalseVal);
1703
+
1704
+ // Transform (X == C) ? X : Y -> (X == C) ? C : Y
1705
+ // specific handling for Bitwise operation.
1706
+ // x&y -> (x|y) ^ (x^y) or (x|y) & ~(x^y)
1707
+ // x|y -> (x&y) | (x^y) or (x&y) ^ (x^y)
1708
+ // x^y -> (x|y) ^ (x&y) or (x|y) & ~(x&y)
1709
+ Value *X, *Y;
1710
+ if (!match (CmpLHS, m_BitwiseLogic (m_Value (X), m_Value (Y))) ||
1711
+ !match (TrueVal, m_c_BitwiseLogic (m_Specific (X), m_Specific (Y))))
1712
+ return nullptr ;
1713
+
1714
+ const unsigned AndOps = Instruction::And, OrOps = Instruction::Or,
1715
+ XorOps = Instruction::Xor, NoOps = 0 ;
1716
+ enum NotMask { None = 0 , NotInner, NotRHS };
1717
+
1718
+ auto matchFalseVal = [&](unsigned OuterOpc, unsigned InnerOpc,
1719
+ unsigned NotMask) {
1720
+ auto matchInner = m_c_BinOp (InnerOpc, m_Specific (X), m_Specific (Y));
1721
+ if (OuterOpc == NoOps)
1722
+ return match (CmpRHS, m_Zero ()) && match (FalseVal, matchInner);
1723
+
1724
+ if (NotMask == NotInner) {
1725
+ return match (FalseVal,
1726
+ m_c_BinOp (OuterOpc, m_Not (matchInner), m_Specific (CmpRHS)));
1727
+ } else if (NotMask == NotRHS) {
1728
+ return match (FalseVal,
1729
+ m_c_BinOp (OuterOpc, matchInner, m_Not (m_Specific (CmpRHS))));
1730
+ } else {
1731
+ return match (FalseVal,
1732
+ m_c_BinOp (OuterOpc, matchInner, m_Specific (CmpRHS)));
1733
+ }
1734
+ };
1735
+
1736
+ // (X&Y)==C ? X|Y : X^Y -> (X^Y)|C : X^Y or (X^Y)^ C : X^Y
1737
+ // (X&Y)==C ? X^Y : X|Y -> (X|Y)^C : X|Y or (X|Y)&~C : X|Y
1738
+ if (match (CmpLHS, m_And (m_Value (X), m_Value (Y)))) {
1739
+ if (match (TrueVal, m_c_Or (m_Specific (X), m_Specific (Y)))) {
1740
+ // (X&Y)==C ? X|Y : (X^Y)|C -> (X^Y)|C : (X^Y)|C -> (X^Y)|C
1741
+ // (X&Y)==C ? X|Y : (X^Y)^C -> (X^Y)^C : (X^Y)^C -> (X^Y)^C
1742
+ if (matchFalseVal (OrOps, XorOps, None) ||
1743
+ matchFalseVal (XorOps, XorOps, None))
1744
+ return IC.replaceInstUsesWith (SI, FalseVal);
1745
+ } else if (match (TrueVal, m_c_Xor (m_Specific (X), m_Specific (Y)))) {
1746
+ // (X&Y)==C ? X^Y : (X|Y)^ C -> (X|Y)^ C : (X|Y)^ C -> (X|Y)^ C
1747
+ // (X&Y)==C ? X^Y : (X|Y)&~C -> (X|Y)&~C : (X|Y)&~C -> (X|Y)&~C
1748
+ if (matchFalseVal (XorOps, OrOps, None) ||
1749
+ matchFalseVal (AndOps, OrOps, NotRHS))
1750
+ return IC.replaceInstUsesWith (SI, FalseVal);
1751
+ }
1752
+ }
1753
+
1754
+ // (X|Y)==C ? X&Y : X^Y -> (X^Y)^C : X^Y or ~(X^Y)&C : X^Y
1755
+ // (X|Y)==C ? X^Y : X&Y -> (X&Y)^C : X&Y or ~(X&Y)&C : X&Y
1756
+ if (match (CmpLHS, m_Or (m_Value (X), m_Value (Y)))) {
1757
+ if (match (TrueVal, m_c_And (m_Specific (X), m_Specific (Y)))) {
1758
+ // (X|Y)==C ? X&Y: (X^Y)^C -> (X^Y)^C: (X^Y)^C -> (X^Y)^C
1759
+ // (X|Y)==C ? X&Y:~(X^Y)&C ->~(X^Y)&C:~(X^Y)&C -> ~(X^Y)&C
1760
+ if (matchFalseVal (XorOps, XorOps, None) ||
1761
+ matchFalseVal (AndOps, XorOps, NotInner))
1762
+ return IC.replaceInstUsesWith (SI, FalseVal);
1763
+ } else if (match (TrueVal, m_c_Xor (m_Specific (X), m_Specific (Y)))) {
1764
+ // (X|Y)==C ? X^Y : (X&Y)^C -> (X&Y)^C : (X&Y)^C -> (X&Y)^C
1765
+ // (X|Y)==C ? X^Y :~(X&Y)&C -> ~(X&Y)&C :~(X&Y)&C -> ~(X&Y)&C
1766
+ if (matchFalseVal (XorOps, AndOps, None) ||
1767
+ matchFalseVal (AndOps, AndOps, NotInner))
1768
+ return IC.replaceInstUsesWith (SI, FalseVal);
1769
+ }
1770
+ }
1771
+
1772
+ // (X^Y)==C ? X&Y : X|Y -> (X|Y)^C : X|Y or (X|Y)&~C : X|Y
1773
+ // (X^Y)==C ? X|Y : X&Y -> (X&Y)|C : X&Y or (X&Y)^ C : X&Y
1774
+ if (match (CmpLHS, m_Xor (m_Value (X), m_Value (Y)))) {
1775
+ if ((match (TrueVal, m_c_And (m_Specific (X), m_Specific (Y))))) {
1776
+ // (X^Y)==C ? X&Y : (X|Y)^C -> (X|Y)^C
1777
+ // (X^Y)==C ? X&Y : (X|Y)&~C -> (X|Y)&~C
1778
+ if (matchFalseVal (XorOps, OrOps, None) ||
1779
+ matchFalseVal (AndOps, OrOps, NotRHS))
1780
+ return IC.replaceInstUsesWith (SI, FalseVal);
1781
+ } else if (match (TrueVal, m_c_Or (m_Specific (X), m_Specific (Y)))) {
1782
+ // (X^Y)==C ? (X|Y) : (X&Y)|C -> (X&Y)|C
1783
+ // (X^Y)==C ? (X|Y) : (X&Y)^C -> (X&Y)^C
1784
+ if (matchFalseVal (OrOps, AndOps, None) ||
1785
+ matchFalseVal (XorOps, AndOps, None))
1786
+ return IC.replaceInstUsesWith (SI, FalseVal);
1787
+ }
1788
+ }
1789
+
1790
+ return nullptr ;
1791
+ }
1792
+
1690
1793
// / Visit a SelectInst that has an ICmpInst as its first operand.
1691
1794
Instruction *InstCombinerImpl::foldSelectInstWithICmp (SelectInst &SI,
1692
1795
ICmpInst *ICI) {
@@ -1729,6 +1832,9 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
1729
1832
}
1730
1833
}
1731
1834
1835
+ if (Instruction *NewSel = foldSelectICmpEq (SI, ICI, *this ))
1836
+ return NewSel;
1837
+
1732
1838
// Canonicalize a signbit condition to use zero constant by swapping:
1733
1839
// (CmpLHS > -1) ? TV : FV --> (CmpLHS < 0) ? FV : TV
1734
1840
// To avoid conflicts (infinite loops) with other canonicalizations, this is
0 commit comments