Skip to content

Commit b96149d

Browse files
committed
[InstCombine] Simplify select if it combinated and/or/xor (#71792)
`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
1 parent 07479dd commit b96149d

File tree

2 files changed

+156
-102
lines changed

2 files changed

+156
-102
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,110 @@ 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+
Instruction &ISI = cast<Instruction>(SI);
1715+
const unsigned AndOps = Instruction::And, OrOps = Instruction::Or,
1716+
XorOps = Instruction::Xor, NoOps = 0;
1717+
enum NotMask { None = 0, NotInner, NotRHS };
1718+
1719+
auto matchFalseVal = [&](unsigned OuterOpc, unsigned InnerOpc,
1720+
unsigned NotMask) {
1721+
auto matchInner = m_c_BinOp(InnerOpc, m_Specific(X), m_Specific(Y));
1722+
if (OuterOpc == NoOps)
1723+
return match(CmpRHS, m_Zero()) && match(FalseVal, matchInner);
1724+
1725+
if (NotMask == NotInner) {
1726+
return match(FalseVal,
1727+
m_c_BinOp(OuterOpc, m_Not(matchInner), m_Specific(CmpRHS)));
1728+
} else if (NotMask == NotRHS) {
1729+
return match(FalseVal,
1730+
m_c_BinOp(OuterOpc, matchInner, m_Not(m_Specific(CmpRHS))));
1731+
} else {
1732+
return match(FalseVal,
1733+
m_c_BinOp(OuterOpc, matchInner, m_Specific(CmpRHS)));
1734+
}
1735+
};
1736+
1737+
// (X&Y)==C ? X|Y : X^Y -> (X^Y)|C : X^Y or (X^Y)^ C : X^Y
1738+
// (X&Y)==C ? X^Y : X|Y -> (X|Y)^C : X|Y or (X|Y)&~C : X|Y
1739+
if (match(CmpLHS, m_And(m_Value(X), m_Value(Y)))) {
1740+
if (match(TrueVal, m_c_Or(m_Specific(X), m_Specific(Y)))) {
1741+
// (X&Y)==C ? X|Y : (X^Y)|C -> (X^Y)|C : (X^Y)|C -> (X^Y)|C
1742+
// (X&Y)==C ? X|Y : (X^Y)^C -> (X^Y)^C : (X^Y)^C -> (X^Y)^C
1743+
if (matchFalseVal(OrOps, XorOps, None) ||
1744+
matchFalseVal(XorOps, XorOps, None))
1745+
return IC.replaceInstUsesWith(ISI, FalseVal);
1746+
} else if (match(TrueVal, m_c_Xor(m_Specific(X), m_Specific(Y)))) {
1747+
// (X&Y)==C ? X^Y : (X|Y)^ C -> (X|Y)^ C : (X|Y)^ C -> (X|Y)^ C
1748+
// (X&Y)==C ? X^Y : (X|Y)&~C -> (X|Y)&~C : (X|Y)&~C -> (X|Y)&~C
1749+
if (matchFalseVal(XorOps, OrOps, None) ||
1750+
matchFalseVal(AndOps, OrOps, NotRHS))
1751+
return IC.replaceInstUsesWith(ISI, FalseVal);
1752+
}
1753+
}
1754+
1755+
// (X|Y)==C ? X&Y : X^Y -> (X^Y)^C : X^Y or ~(X^Y)&C : X^Y
1756+
// (X|Y)==C ? X^Y : X&Y -> (X&Y)^C : X&Y or ~(X&Y)&C : X&Y
1757+
if (match(CmpLHS, m_Or(m_Value(X), m_Value(Y)))) {
1758+
if (match(TrueVal, m_c_And(m_Specific(X), m_Specific(Y)))) {
1759+
// (X|Y)==C ? X&Y: (X^Y)^C -> (X^Y)^C: (X^Y)^C -> (X^Y)^C
1760+
// (X|Y)==C ? X&Y:~(X^Y)&C ->~(X^Y)&C:~(X^Y)&C -> ~(X^Y)&C
1761+
if (matchFalseVal(XorOps, XorOps, None) ||
1762+
matchFalseVal(AndOps, XorOps, NotInner))
1763+
return IC.replaceInstUsesWith(ISI, FalseVal);
1764+
} else if (match(TrueVal, m_c_Xor(m_Specific(X), m_Specific(Y)))) {
1765+
// (X|Y)==C ? X^Y : (X&Y)^C -> (X&Y)^C : (X&Y)^C -> (X&Y)^C
1766+
// (X|Y)==C ? X^Y :~(X&Y)&C -> ~(X&Y)&C :~(X&Y)&C -> ~(X&Y)&C
1767+
if (matchFalseVal(XorOps, AndOps, None) ||
1768+
matchFalseVal(AndOps, AndOps, NotInner))
1769+
return IC.replaceInstUsesWith(ISI, FalseVal);
1770+
}
1771+
}
1772+
1773+
// (X^Y)==C ? X&Y : X|Y -> (X|Y)^C : X|Y or (X|Y)&~C : X|Y
1774+
// (X^Y)==C ? X|Y : X&Y -> (X&Y)|C : X&Y or (X&Y)^ C : X&Y
1775+
if (match(CmpLHS, m_Xor(m_Value(X), m_Value(Y)))) {
1776+
if ((match(TrueVal, m_c_And(m_Specific(X), m_Specific(Y))))) {
1777+
// (X^Y)==C ? X&Y : (X|Y)^C -> (X|Y)^C
1778+
// (X^Y)==C ? X&Y : (X|Y)&~C -> (X|Y)&~C
1779+
if (matchFalseVal(XorOps, OrOps, None) ||
1780+
matchFalseVal(AndOps, OrOps, NotRHS))
1781+
return IC.replaceInstUsesWith(ISI, FalseVal);
1782+
} else if (match(TrueVal, m_c_Or(m_Specific(X), m_Specific(Y)))) {
1783+
// (X^Y)==C ? (X|Y) : (X&Y)|C -> (X&Y)|C
1784+
// (X^Y)==C ? (X|Y) : (X&Y)^C -> (X&Y)^C
1785+
if (matchFalseVal(OrOps, AndOps, None) ||
1786+
matchFalseVal(XorOps, AndOps, None))
1787+
return IC.replaceInstUsesWith(ISI, FalseVal);
1788+
}
1789+
}
1790+
1791+
return nullptr;
1792+
}
1793+
16901794
/// Visit a SelectInst that has an ICmpInst as its first operand.
16911795
Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
16921796
ICmpInst *ICI) {
@@ -1729,6 +1833,9 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
17291833
}
17301834
}
17311835

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

0 commit comments

Comments
 (0)