From f6fc3e23b95ef0045d4453bd3fdddd765f29099b Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Fri, 21 Oct 2022 10:43:54 -0400 Subject: [PATCH] [InstCombine] refactor matching code for logical ands; NFCI Separating the matches makes it easier to enhance for commutative patterns. --- .../Transforms/InstCombine/InstCombineSelect.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index 756bece..0f56483 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -2847,16 +2847,18 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) { return replaceOperand(SI, 0, A); } - // (c && a) || (!c && b) --> sel c, a, b - // (!c && a) || (c && b) --> sel c, b, a - Value *C1, *C2; - if (match(CondVal, m_LogicalAnd(m_Value(C1), m_Value(A))) && - match(TrueVal, m_One()) && - match(FalseVal, m_LogicalAnd(m_Value(C2), m_Value(B)))) { - if (match(C2, m_Not(m_Specific(C1)))) // first case - return SelectInst::Create(C1, A, B); - else if (match(C1, m_Not(m_Specific(C2)))) // second case - return SelectInst::Create(C2, B, A); + if (match(TrueVal, m_One())) { + Value *C; + + // (C && A) || (!C && B) --> sel C, A, B + if (match(FalseVal, m_LogicalAnd(m_Not(m_Value(C)), m_Value(B))) && + match(CondVal, m_LogicalAnd(m_Specific(C), m_Value(A)))) + return SelectInst::Create(C, A, B); + + // (!C && A) || (C && B) --> sel C, B, A + if (match(CondVal, m_LogicalAnd(m_Not(m_Value(C)), m_Value(A))) && + match(FalseVal, m_LogicalAnd(m_Specific(C), m_Value(B)))) + return SelectInst::Create(C, B, A); } } -- 2.7.4