[llvm] 5d7d2f0 - [InstCombine] improve efficiency of isFreeToInvert

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 23 12:03:34 PDT 2021


Author: Sanjay Patel
Date: 2021-08-23T14:56:14-04:00
New Revision: 5d7d2f0d2e7b137e68337b0b472130b61d9f305e

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

LOG: [InstCombine] improve efficiency of isFreeToInvert

This is NFC-intended when viewed from outside the pass.
I was trying to make sure that we don't infinite loop
in subtract combines and noticed that we handle the
non-canonical forms of add/sub here, but it should
not be necessary. Coding it this way seems slightly
clearer than mixing all 4 patterns as before.

Added: 
    

Modified: 
    llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
index 3f13df7b76b4b..f763612a95a65 100644
--- a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
+++ b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
@@ -246,12 +246,13 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
 
     // If `V` is of the form `A + Constant` then `-1 - V` can be folded into
     // `(-1 - Constant) - A` if we are willing to invert all of the uses.
-    if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
-      if (BO->getOpcode() == Instruction::Add ||
-          BO->getOpcode() == Instruction::Sub)
-        if (match(BO, PatternMatch::m_c_BinOp(PatternMatch::m_Value(),
-                                              PatternMatch::m_ImmConstant())))
-          return WillInvertAllUses;
+    if (match(V, m_Add(PatternMatch::m_Value(), PatternMatch::m_ImmConstant())))
+      return WillInvertAllUses;
+
+    // If `V` is of the form `Constant - A` then `-1 - V` can be folded into
+    // `A + (-1 - Constant)` if we are willing to invert all of the uses.
+    if (match(V, m_Sub(PatternMatch::m_ImmConstant(), PatternMatch::m_Value())))
+      return WillInvertAllUses;
 
     // Selects with invertible operands are freely invertible
     if (match(V,


        


More information about the llvm-commits mailing list