[llvm] ValueTracking: Do not look at users of constants for ephemeral values (PR #134618)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 8 09:13:54 PDT 2025


https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/134618

>From fb23a37eafb2933822bef1a4bd53cc89d6287a1a Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Mon, 7 Apr 2025 18:44:40 +0700
Subject: [PATCH 1/2] ValueTracking: Do not look at users of constants for
 ephemeral values

These can only be instructions
---
 llvm/lib/Analysis/ValueTracking.cpp | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 3b0249f91d6d7..111515e685d61 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -472,8 +472,13 @@ static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
                      !cast<Instruction>(V)->mayHaveSideEffects() &&
                      !cast<Instruction>(V)->isTerminator())) {
        EphValues.insert(V);
-       if (const User *U = dyn_cast<User>(V))
-         append_range(WorkSet, U->operands());
+
+       if (const User *U = dyn_cast<User>(V)) {
+         for (const Use &U : U->operands()) {
+           if (!isa<Constant>(U))
+             WorkSet.push_back(U.get());
+         }
+       }
       }
     }
   }

>From b4bde69945daac1a6d06463b839761f3bfb3a8b3 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Tue, 8 Apr 2025 23:01:52 +0700
Subject: [PATCH 2/2] Change type of set and list

---
 llvm/lib/Analysis/ValueTracking.cpp | 34 ++++++++++++++---------------
 1 file changed, 16 insertions(+), 18 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 111515e685d61..d9c55330f8664 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -446,9 +446,9 @@ void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
 }
 
 static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
-  SmallVector<const Value *, 16> WorkSet(1, I);
-  SmallPtrSet<const Value *, 32> Visited;
-  SmallPtrSet<const Value *, 16> EphValues;
+  SmallVector<const Instruction *, 16> WorkSet(1, I);
+  SmallPtrSet<const Instruction *, 32> Visited;
+  SmallPtrSet<const Instruction *, 16> EphValues;
 
   // The instruction defining an assumption's condition itself is always
   // considered ephemeral to that assumption (even if it has other
@@ -457,28 +457,26 @@ static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
     return true;
 
   while (!WorkSet.empty()) {
-    const Value *V = WorkSet.pop_back_val();
+    const Instruction *V = WorkSet.pop_back_val();
     if (!Visited.insert(V).second)
       continue;
 
     // If all uses of this value are ephemeral, then so is this value.
-    if (llvm::all_of(V->users(), [&](const User *U) {
-                                   return EphValues.count(U);
-                                 })) {
+    if (all_of(V->users(), [&](const User *U) {
+          return EphValues.count(cast<Instruction>(U));
+        })) {
       if (V == E)
         return true;
 
-      if (V == I || (isa<Instruction>(V) &&
-                     !cast<Instruction>(V)->mayHaveSideEffects() &&
-                     !cast<Instruction>(V)->isTerminator())) {
-       EphValues.insert(V);
-
-       if (const User *U = dyn_cast<User>(V)) {
-         for (const Use &U : U->operands()) {
-           if (!isa<Constant>(U))
-             WorkSet.push_back(U.get());
-         }
-       }
+      if (V == I || (!V->mayHaveSideEffects() && !V->isTerminator())) {
+        EphValues.insert(V);
+
+        if (const User *U = dyn_cast<User>(V)) {
+          for (const Use &U : U->operands()) {
+            if (const auto *I = dyn_cast<Instruction>(U.get()))
+              WorkSet.push_back(I);
+          }
+        }
       }
     }
   }



More information about the llvm-commits mailing list