[llvm] 6ee46ab - [Analysis] Share `findAffectedValues` between DomConditionCache and AssumptionCache; NFC

Noah Goldstein via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 4 14:53:47 PST 2024


Author: Noah Goldstein
Date: 2024-03-04T16:53:27-06:00
New Revision: 6ee46aba0695bd004e5b229b73dabe8fd5a70513

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

LOG: [Analysis] Share `findAffectedValues` between DomConditionCache and AssumptionCache; NFC

Added: 
    

Modified: 
    llvm/lib/Analysis/AssumptionCache.cpp
    llvm/lib/Analysis/ValueTracking.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/AssumptionCache.cpp b/llvm/lib/Analysis/AssumptionCache.cpp
index 1b7277df0e0cd0..9c86ef13f6df03 100644
--- a/llvm/lib/Analysis/AssumptionCache.cpp
+++ b/llvm/lib/Analysis/AssumptionCache.cpp
@@ -61,19 +61,13 @@ findAffectedValues(CallBase *CI, TargetTransformInfo *TTI,
   // Note: This code must be kept in-sync with the code in
   // computeKnownBitsFromAssume in ValueTracking.
 
-  auto AddAffected = [&Affected](Value *V, unsigned Idx =
-                                               AssumptionCache::ExprResultIdx) {
-    if (isa<Argument>(V) || isa<GlobalValue>(V)) {
+  auto InsertAffected = [&Affected](Value *V) {
+    Affected.push_back({V, AssumptionCache::ExprResultIdx});
+  };
+
+  auto AddAffectedVal = [&Affected](Value *V, unsigned Idx) {
+    if (isa<Argument>(V) || isa<GlobalValue>(V) || isa<Instruction>(V)) {
       Affected.push_back({V, Idx});
-    } else if (auto *I = dyn_cast<Instruction>(V)) {
-      Affected.push_back({I, Idx});
-
-      // Peek through unary operators to find the source of the condition.
-      Value *Op;
-      if (match(I, m_PtrToInt(m_Value(Op)))) {
-        if (isa<Instruction>(Op) || isa<Argument>(Op))
-          Affected.push_back({Op, Idx});
-      }
     }
   };
 
@@ -82,64 +76,23 @@ findAffectedValues(CallBase *CI, TargetTransformInfo *TTI,
     if (Bundle.getTagName() == "separate_storage") {
       assert(Bundle.Inputs.size() == 2 &&
              "separate_storage must have two args");
-      AddAffected(getUnderlyingObject(Bundle.Inputs[0]), Idx);
-      AddAffected(getUnderlyingObject(Bundle.Inputs[1]), Idx);
+      AddAffectedVal(getUnderlyingObject(Bundle.Inputs[0]), Idx);
+      AddAffectedVal(getUnderlyingObject(Bundle.Inputs[1]), Idx);
     } else if (Bundle.Inputs.size() > ABA_WasOn &&
                Bundle.getTagName() != IgnoreBundleTag)
-      AddAffected(Bundle.Inputs[ABA_WasOn], Idx);
+      AddAffectedVal(Bundle.Inputs[ABA_WasOn], Idx);
   }
 
-  Value *Cond = CI->getArgOperand(0), *A, *B;
-  AddAffected(Cond);
-  if (match(Cond, m_Not(m_Value(A))))
-    AddAffected(A);
-
-  CmpInst::Predicate Pred;
-  if (match(Cond, m_Cmp(Pred, m_Value(A), m_Value(B)))) {
-    AddAffected(A);
-    AddAffected(B);
-
-    if (Pred == ICmpInst::ICMP_EQ) {
-      if (match(B, m_ConstantInt())) {
-        Value *X;
-        // (X & C) or (X | C) or (X ^ C).
-        // (X << C) or (X >>_s C) or (X >>_u C).
-        if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
-            match(A, m_Shift(m_Value(X), m_ConstantInt())))
-          AddAffected(X);
-      }
-    } else if (Pred == ICmpInst::ICMP_NE) {
-      Value *X;
-      // Handle (X & pow2 != 0).
-      if (match(A, m_And(m_Value(X), m_Power2())) && match(B, m_Zero()))
-        AddAffected(X);
-    } else if (Pred == ICmpInst::ICMP_ULT) {
-      Value *X;
-      // Handle (A + C1) u< C2, which is the canonical form of A > C3 && A < C4,
-      // and recognized by LVI at least.
-      if (match(A, m_Add(m_Value(X), m_ConstantInt())) &&
-          match(B, m_ConstantInt()))
-        AddAffected(X);
-    } else if (CmpInst::isFPPredicate(Pred)) {
-      // fcmp fneg(x), y
-      // fcmp fabs(x), y
-      // fcmp fneg(fabs(x)), y
-      if (match(A, m_FNeg(m_Value(A))))
-        AddAffected(A);
-      if (match(A, m_FAbs(m_Value(A))))
-        AddAffected(A);
-    }
-  } else if (match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
-                                                            m_Value(B)))) {
-    AddAffected(A);
-  }
+  Value *Cond = CI->getArgOperand(0);
+  findValuesAffectedByCondition(Cond, /*IsAssume=*/true, InsertAffected);
 
   if (TTI) {
     const Value *Ptr;
     unsigned AS;
     std::tie(Ptr, AS) = TTI->getPredicatedAddrSpace(Cond);
     if (Ptr)
-      AddAffected(const_cast<Value *>(Ptr->stripInBoundsOffsets()));
+      AddAffectedVal(const_cast<Value *>(Ptr->stripInBoundsOffsets()),
+                     AssumptionCache::ExprResultIdx);
   }
 }
 

diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 4b230863b16722..dd78c165c4777f 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9124,7 +9124,6 @@ void llvm::findValuesAffectedByCondition(
     addValueAffectedByCondition(V, InsertAffected);
   };
 
-  assert(!IsAssume);
   SmallVector<Value *, 8> Worklist;
   SmallPtrSet<Value *, 8> Visited;
   Worklist.push_back(Cond);
@@ -9134,35 +9133,67 @@ void llvm::findValuesAffectedByCondition(
       continue;
 
     CmpInst::Predicate Pred;
-    Value *A, *B;
-    if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
+    Value *A, *B, *X;
+
+    if (IsAssume)
+      AddAffected(V);
+
+    if (IsAssume && match(V, m_Not(m_Value(X))))
+      AddAffected(X);
+    if (!IsAssume && match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
       Worklist.push_back(A);
       Worklist.push_back(B);
-    } else if (match(V, m_ICmp(Pred, m_Value(A), m_Constant()))) {
-      AddAffected(A);
+    } else if (match(V, m_Cmp(Pred, m_Value(A), m_Value(B))) &&
+               (IsAssume || isa<ICmpInst>(V))) {
+      if (IsAssume || match(B, m_Constant())) {
+        AddAffected(A);
+        if (IsAssume)
+          AddAffected(B);
+
+        if (IsAssume ? (Pred == ICmpInst::ICMP_EQ)
+                     : ICmpInst::isEquality(Pred)) {
+          if (match(B, m_ConstantInt())) {
+            // (X & C) or (X | C) or (X ^ C).
+            // (X << C) or (X >>_s C) or (X >>_u C).
+            if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
+                match(A, m_Shift(m_Value(X), m_ConstantInt())))
+              AddAffected(X);
+          }
+        } else {
+          if (Pred == ICmpInst::ICMP_NE)
+            if (match(A, m_And(m_Value(X), m_Power2())) && match(B, m_Zero()))
+              AddAffected(X);
+
+          if (!IsAssume || Pred == ICmpInst::ICMP_ULT) {
+            // Handle (A + C1) u< C2, which is the canonical form of
+            // A > C3 && A < C4.
+            if (match(A, m_Add(m_Value(X), m_ConstantInt())) &&
+                match(B, m_ConstantInt()))
+              AddAffected(X);
+          }
+          if (!IsAssume) {
+            // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
+            // by computeKnownFPClass().
+            if ((Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGT) &&
+                match(A, m_ElementWiseBitCast(m_Value(X))))
+              InsertAffected(X);
+          }
 
-      if (ICmpInst::isEquality(Pred)) {
-        Value *X;
-        // (X & C) or (X | C) or (X ^ C).
-        // (X << C) or (X >>_s C) or (X >>_u C).
-        if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
-            match(A, m_Shift(m_Value(X), m_ConstantInt())))
-          AddAffected(X);
-      } else {
-        Value *X;
-        // Handle (A + C1) u< C2, which is the canonical form of
-        // A > C3 && A < C4.
-        if (match(A, m_Add(m_Value(X), m_ConstantInt())))
-          AddAffected(X);
-        // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported by
-        // computeKnownFPClass().
-        if ((Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGT) &&
-            match(A, m_ElementWiseBitCast(m_Value(X))))
-          InsertAffected(X);
+          if (IsAssume && CmpInst::isFPPredicate(Pred)) {
+            // fcmp fneg(x), y
+            // fcmp fabs(x), y
+            // fcmp fneg(fabs(x)), y
+            if (match(A, m_FNeg(m_Value(A))))
+              AddAffected(A);
+            if (match(A, m_FAbs(m_Value(A))))
+              AddAffected(A);
+          }
+        }
       }
-    } else if (match(Cond, m_CombineOr(m_FCmp(Pred, m_Value(A), m_Constant()),
-                                       m_Intrinsic<Intrinsic::is_fpclass>(
-                                           m_Value(A), m_Constant())))) {
+    } else if ((!IsAssume &&
+                match(Cond, m_FCmp(Pred, m_Value(A), m_Constant()))) ||
+               match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
+                                                              m_Value(B)))) {
       // Handle patterns that computeKnownFPClass() support.
       AddAffected(A);
     }


        


More information about the llvm-commits mailing list