[llvm] [InstCombine] Simplify select if it combinated `and/or/xor` (PR #73362)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 26 09:39:29 PST 2023
================
@@ -4425,6 +4425,48 @@ Value *llvm::simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
RecursionLimit);
}
+/// Try simplifying the selection command when condition, trueValue, and
+/// falseValue are combinations of special bitwise operators.
+static Value *simplifySelectBitTestSpec(Value *TrueVal, Value *FalseVal,
+ Value *X, Value *Y,
+ bool TrueWhenUnset) {
+ // (X & Y) == 0 ? X | Y : X ^ Y --> X ^ Y
+ // (X & Y) == 0 ? X ^ Y : X | Y --> X | Y
+ // (X & Y) == 1 ? X | Y : X ^ Y --> X | Y
+ // (X & Y) == 1 ? X ^ Y : X | Y --> X ^ Y
+ if (match(TrueVal, m_Or(m_Specific(X), m_Specific(Y))) &&
+ match(FalseVal, m_Xor(m_Specific(X), m_Specific(Y)))) {
+ return TrueWhenUnset ? TrueVal : FalseVal;
+ } else if (match(FalseVal, m_Or(m_Specific(X), m_Specific(Y))) &&
+ match(TrueVal, m_Xor(m_Specific(X), m_Specific(Y)))) {
+ return TrueWhenUnset ? TrueVal : FalseVal;
+ }
+ // (X | Y) == 0 ? X & Y : X ^ Y --> X ^ Y
+ // (X | Y) == 0 ? X ^ Y : X & Y --> X & Y
+ // (X | Y) == 1 ? X & Y : X ^ Y --> X & Y
+ // (X | Y) == 1 ? X ^ Y : X & Y --> X ^ Y
+ if (match(TrueVal, m_And(m_Specific(X), m_Specific(Y))) &&
----------------
goldsteinn wrote:
Think you have an issue. You can end up matching `X & Y == 0 ? X & Y : X ^ Y` -> `X ^ Y` which isn't valid. You need to either pass the matching compare binop to `simplifySelectBitTestSpec` or just do the cmp match in this function.
https://github.com/llvm/llvm-project/pull/73362
More information about the llvm-commits
mailing list