Skip to content

Commit 4ef22fc

Browse files
authored
[InstCombine] Simplify select if it combinated and/or/xor (#73362)
`and/or/xor` operations can each be changed to sum of logical operations including operators other than themselves. `x&y -> (x|y) ^ (x^y)` `x|y -> (x&y) | (x^y)` `x^y -> (x|y) ^ (x&y)` if left of condition of `SelectInst` is `and/or/xor` logical operation and right is equal to `0, -1`, or a `constant`, and if `TrueVal` consist of `and/or/xor` logical operation then we can optimize this case. This patch implements this combination. Proof: https://alive2.llvm.org/ce/z/WW8iRR Fixes #71792.
1 parent de3e05e commit 4ef22fc

File tree

2 files changed

+900
-0
lines changed

2 files changed

+900
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,109 @@ static Value *foldSelectInstWithICmpConst(SelectInst &SI, ICmpInst *ICI,
16871687
return nullptr;
16881688
}
16891689

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+
16901793
/// Visit a SelectInst that has an ICmpInst as its first operand.
16911794
Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
16921795
ICmpInst *ICI) {
@@ -1729,6 +1832,9 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
17291832
}
17301833
}
17311834

1835+
if (Instruction *NewSel = foldSelectICmpEq(SI, ICI, *this))
1836+
return NewSel;
1837+
17321838
// Canonicalize a signbit condition to use zero constant by swapping:
17331839
// (CmpLHS > -1) ? TV : FV --> (CmpLHS < 0) ? FV : TV
17341840
// To avoid conflicts (infinite loops) with other canonicalizations, this is

0 commit comments

Comments
 (0)