[llvm] [SLP]Consider (f)sub, being operand of llvm.(f)abs/icmp eq/ne 0, commutative. (PR #86196)

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 22 08:27:32 PDT 2024


================
@@ -305,7 +305,32 @@ static bool isCommutative(Instruction *I) {
   if (auto *Cmp = dyn_cast<CmpInst>(I))
     return Cmp->isCommutative();
   if (auto *BO = dyn_cast<BinaryOperator>(I))
-    return BO->isCommutative();
+    return BO->isCommutative() ||
+           (BO->getOpcode() == Instruction::Sub &&
+            !BO->hasNUsesOrMore(UsesLimit) &&
+            all_of(BO->uses(),
+                   [](const Use &U) {
+                     // Commutative, if icmp eq/ne sub, 0
+                     ICmpInst::Predicate Pred;
+                     if (match(U.getUser(),
+                               m_ICmp(Pred, m_Specific(U.get()), m_Zero())) &&
+                         (Pred == ICmpInst::ICMP_EQ ||
+                          Pred == ICmpInst::ICMP_NE))
+                       return true;
+                     // Commutative, if abs(sub, flag).
+                     if (U.getOperandNo() != 0)
+                       return false;
+                     const auto *IC = dyn_cast<IntrinsicInst>(U.getUser());
+                     return IC && IC->getIntrinsicID() == Intrinsic::abs;
+                   })) ||
+           (BO->getOpcode() == Instruction::FSub &&
+            !BO->hasNUsesOrMore(UsesLimit) &&
+            all_of(BO->uses(), [](const Use &U) {
+              if (U.getOperandNo() != 0)
+                return false;
+              const auto *IC = dyn_cast<IntrinsicInst>(U.getUser());
+              return IC && IC->getIntrinsicID() == Intrinsic::fabs;
+            }));
----------------
alexey-bataev wrote:

> Yet more code that really shouldn't be inside SLP - can we find somewhere more general to put it please?
 Actually, it should be in SLP. It limits the graph and thus affects the cost. It does not handle all possible cases, though (e.g., operands with same opcodes can be non-schedulable, we cannot detect it here, maybe later), so having something in the instcombine is still good.

https://github.com/llvm/llvm-project/pull/86196


More information about the llvm-commits mailing list