[llvm] f61de3c - [NFC][PatternMatching] Promote `m_LogicalOp` matchers into `PatternMatch.h`

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 18 13:24:41 PST 2022


Author: Roman Lebedev
Date: 2022-12-19T00:24:28+03:00
New Revision: f61de3c1aa8fa4a5fdccbf213e5dca507073d431

URL: https://github.com/llvm/llvm-project/commit/f61de3c1aa8fa4a5fdccbf213e5dca507073d431
DIFF: https://github.com/llvm/llvm-project/commit/f61de3c1aa8fa4a5fdccbf213e5dca507073d431.diff

LOG: [NFC][PatternMatching] Promote `m_LogicalOp` matchers into `PatternMatch.h`

Added: 
    

Modified: 
    llvm/include/llvm/IR/PatternMatch.h
    llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index 9dc8cf90fd983..102716078cff0 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -2586,6 +2586,25 @@ m_c_LogicalOr(const LHS &L, const RHS &R) {
   return LogicalOp_match<LHS, RHS, Instruction::Or, true>(L, R);
 }
 
+/// Matches either L && R or L || R,
+/// either one being in the either binary or logical form.
+/// Note that the latter form is poison-blocking.
+template <typename LHS, typename RHS, bool Commutable = false>
+inline auto m_LogicalOp(const LHS &L, const RHS &R) {
+  return m_CombineOr(
+      LogicalOp_match<LHS, RHS, Instruction::And, Commutable>(L, R),
+      LogicalOp_match<LHS, RHS, Instruction::Or, Commutable>(L, R));
+}
+
+/// Matches either L && R or L || R where L and R are arbitrary values.
+inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
+
+/// Matches either L && R or L || R with LHS and RHS in either order.
+template <typename LHS, typename RHS>
+inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
+  return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
+}
+
 } // end namespace PatternMatch
 } // end namespace llvm
 

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 9fa5bb3826d97..f41909bc521a3 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2712,10 +2712,6 @@ static Instruction *foldNestedSelects(SelectInst &OuterSelVal,
   if (match(OuterSel.Cond, m_Not(m_Value(OuterSel.Cond))))
     std::swap(OuterSel.TrueVal, OuterSel.FalseVal);
 
-  auto m_c_LogicalOp = [](auto L, auto R) {
-    return m_CombineOr(m_c_LogicalAnd(L, R), m_c_LogicalOr(L, R));
-  };
-
   // The condition of the outermost select must be an `and`/`or`.
   if (!match(OuterSel.Cond, m_c_LogicalOp(m_Value(), m_Value())))
     return nullptr;
@@ -2741,7 +2737,7 @@ static Instruction *foldNestedSelects(SelectInst &OuterSelVal,
     std::swap(InnerSel.TrueVal, InnerSel.FalseVal);
 
   Value *AltCond = nullptr;
-  auto matchOuterCond = [OuterSel, m_c_LogicalOp, &AltCond](auto m_InnerCond) {
+  auto matchOuterCond = [OuterSel, &AltCond](auto m_InnerCond) {
     return match(OuterSel.Cond, m_c_LogicalOp(m_InnerCond, m_Value(AltCond)));
   };
 


        


More information about the llvm-commits mailing list