[llvm] [InstCombine] Improve handling of `not` and free inversion. (PR #66787)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 18 02:12:28 PST 2023


================
@@ -233,58 +233,114 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
                                                 PatternMatch::m_Value()));
   }
 
-  /// Return true if the specified value is free to invert (apply ~ to).
-  /// This happens in cases where the ~ can be eliminated.  If WillInvertAllUses
-  /// is true, work under the assumption that the caller intends to remove all
-  /// uses of V and only keep uses of ~V.
-  ///
-  /// See also: canFreelyInvertAllUsersOf()
-  static bool isFreeToInvert(Value *V, bool WillInvertAllUses,
-                             unsigned Depth = 0) {
+  /// Return nonnull value if V is free to invert (with condition) regarding
+  /// WillInvertAllUses.
+  /// If Builder is nonnull, it will return a simplified ~V
+  /// If Builder is null, it will return an arbitrary nonnull value (not
+  /// dereferenceable).
+  static Value *getFreelyInverted(Value *V, bool WillInvertAllUses,
+                                  BuilderTy *Builder, unsigned Depth = 0) {
     using namespace llvm::PatternMatch;
+    static Value *const NonNull = reinterpret_cast<Value *>(uintptr_t(1));
     // ~(~(X)) -> X.
-    if (match(V, m_Not(m_Value())))
-      return true;
+    Value *A, *B;
+    if (match(V, m_Not(m_Value(A))))
+      return A;
 
+    Constant *C;
     // Constants can be considered to be not'ed values.
-    if (match(V, m_AnyIntegralConstant()))
-      return true;
+    if (match(V, m_ImmConstant(C)))
+      return ConstantExpr::getNot(C);
 
     if (Depth++ >= MaxAnalysisRecursionDepth)
-      return false;
+      return nullptr;
 
     // The rest of the cases require that we invert all uses so don't bother
     // doing the analysis if we know we can't use the result.
     if (!WillInvertAllUses)
-      return false;
+      return nullptr;
 
     // Compares can be inverted if all of their uses are being modified to use
     // the ~V.
-    if (isa<CmpInst>(V))
-      return true;
+    if (auto *I = dyn_cast<CmpInst>(V)) {
+      if (Builder != nullptr)
+        return Builder->CreateCmp(I->getInversePredicate(), I->getOperand(0),
+                                  I->getOperand(1));
+      return NonNull;
+    }
 
-    Value *A, *B;
-    // If `V` is of the form `A + B` then `-1 - V` can be folded into
-    // `~B - A` or `~A - B` if we are willing to invert all of the uses.
-    if (match(V, m_Add(m_Value(A), m_Value(B))))
-      return isFreeToInvert(A, A->hasOneUse(), Depth) ||
-             isFreeToInvert(B, B->hasOneUse(), Depth);
+    // 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 (match(V, m_Add(m_Value(A), m_Value(B)))) {
+      if (auto *BV = getFreelyInverted(B, B->hasOneUse(), Builder, Depth))
+        return Builder ? Builder->CreateSub(BV, A) : NonNull;
+      if (auto *AV = getFreelyInverted(A, A->hasOneUse(), Builder, Depth))
+        return Builder ? Builder->CreateSub(AV, B) : NonNull;
+      return nullptr;
+    }
 
-    // If `V` is of the form `A - B` then `-1 - V` can be folded into
-    // `~A + B` if we are willing to invert all of the uses.
-    if (match(V, m_Sub(m_Value(A), m_Value())))
-      return isFreeToInvert(A, A->hasOneUse(), Depth);
+    // If `V` is of the form `A ^ ~B` then `~(A ^ ~B)` can be folded
+    // into `A ^ B` if we are willing to invert all of the uses.
+    if (match(V, m_Xor(m_Value(A), m_Value(B)))) {
+      if (auto *BV = getFreelyInverted(B, B->hasOneUse(), Builder, Depth))
+        return Builder ? Builder->CreateXor(A, BV) : NonNull;
+      if (auto *AV = getFreelyInverted(A, A->hasOneUse(), Builder, Depth))
+        return Builder ? Builder->CreateXor(AV, B) : NonNull;
+      return nullptr;
+    }
 
+    // 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(m_Value(A), m_Value(B)))) {
+      if (auto *AV = getFreelyInverted(A, A->hasOneUse(), Builder, Depth))
+        return Builder ? Builder->CreateAdd(AV, B) : NonNull;
+      return nullptr;
+    }
+
+    // If `V` is of the form `(~A) s>> B` then `~((~A) s>> B)` can be folded
+    // into `A s>> B` if we are willing to invert all of the uses.
+    if (match(V, m_AShr(m_Value(A), m_Value(B)))) {
+      if (auto *AV = getFreelyInverted(A, A->hasOneUse(), Builder, Depth))
+        return Builder ? Builder->CreateAShr(AV, B) : NonNull;
+      return nullptr;
+    }
+
+    Value *Cond;
     // LogicOps are special in that we canonicalize them at the cost of an
     // instruction.
-    bool IsSelect = match(V, m_Select(m_Value(), m_Value(A), m_Value(B))) &&
+    bool IsSelect = match(V, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&
                     !shouldAvoidAbsorbingNotIntoSelect(*cast<SelectInst>(V));
     // Selects/min/max with invertible operands are freely invertible
-    if (IsSelect || match(V, m_MaxOrMin(m_Value(A), m_Value(B))))
-      return isFreeToInvert(A, A->hasOneUse(), Depth) &&
-             isFreeToInvert(B, B->hasOneUse(), Depth);
+    if (IsSelect || match(V, m_MaxOrMin(m_Value(A), m_Value(B)))) {
+      if (!getFreelyInverted(A, A->hasOneUse(), /*Builder*/ nullptr, Depth))
----------------
nikic wrote:

It might be better to check B first, so that we generate instructions for A and then B. Makes not semantic difference, but I think it will give a more natural order and avoid some diffs in your last commit.

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


More information about the llvm-commits mailing list