[llvm] e1d47d8 - [IR] Report whether replaceUsesOfWith() changed something (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed May 18 02:46:34 PDT 2022


Author: Nikita Popov
Date: 2022-05-18T11:46:28+02:00
New Revision: e1d47d86d84588d7e49dbb5172403d17c44467f7

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

LOG: [IR] Report whether replaceUsesOfWith() changed something (NFC)

With change reporting in transformation passes in mind.

Added: 
    

Modified: 
    llvm/include/llvm/IR/User.h
    llvm/lib/IR/User.cpp
    llvm/unittests/IR/UserTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/User.h b/llvm/include/llvm/IR/User.h
index 221bb5b2cb1cb..a9cf60151e5dc 100644
--- a/llvm/include/llvm/IR/User.h
+++ b/llvm/include/llvm/IR/User.h
@@ -304,8 +304,8 @@ class User : public Value {
   /// Replace uses of one Value with another.
   ///
   /// Replaces all references to the "From" definition with references to the
-  /// "To" definition.
-  void replaceUsesOfWith(Value *From, Value *To);
+  /// "To" definition. Returns whether any uses were replaced.
+  bool replaceUsesOfWith(Value *From, Value *To);
 
   // Methods for support type inquiry through isa, cast, and dyn_cast:
   static bool classof(const Value *V) {

diff  --git a/llvm/lib/IR/User.cpp b/llvm/lib/IR/User.cpp
index 68489075cd882..637af7aaa2453 100644
--- a/llvm/lib/IR/User.cpp
+++ b/llvm/lib/IR/User.cpp
@@ -18,8 +18,9 @@ class BasicBlock;
 //                                 User Class
 //===----------------------------------------------------------------------===//
 
-void User::replaceUsesOfWith(Value *From, Value *To) {
-  if (From == To) return;   // Duh what?
+bool User::replaceUsesOfWith(Value *From, Value *To) {
+  bool Changed = false;
+  if (From == To) return Changed;   // Duh what?
 
   assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
          "Cannot call User::replaceUsesOfWith on a constant!");
@@ -30,11 +31,16 @@ void User::replaceUsesOfWith(Value *From, Value *To) {
       // "To", adding "this" to the uses list of To, and
       // most importantly, removing "this" from the use list of "From".
       setOperand(i, To);
+      Changed = true;
     }
   if (auto DVI = dyn_cast_or_null<DbgVariableIntrinsic>(this)) {
-    if (is_contained(DVI->location_ops(), From))
+    if (is_contained(DVI->location_ops(), From)) {
       DVI->replaceVariableLocationOp(From, To);
+      Changed = true;
+    }
   }
+
+  return Changed;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/llvm/unittests/IR/UserTest.cpp b/llvm/unittests/IR/UserTest.cpp
index 8fd435ecbc2b6..46a8d1977c40e 100644
--- a/llvm/unittests/IR/UserTest.cpp
+++ b/llvm/unittests/IR/UserTest.cpp
@@ -144,9 +144,12 @@ TEST(UserTest, replaceUseOfWith) {
   auto XUser = find(X.users(), &(I1));
   EXPECT_NE(XUser, X.user_end());
  
-  XUser->replaceUsesOfWith(&X, &I0);
-  EXPECT_EQ(X.user_begin() ,X.user_end());
-  EXPECT_NE(I0.user_begin() ,I0.user_end());
+  EXPECT_TRUE(XUser->replaceUsesOfWith(&X, &I0));
+  EXPECT_EQ(X.user_begin(), X.user_end());
+  EXPECT_NE(I0.user_begin(), I0.user_end());
+
+  // All uses have already been replaced, nothing more to do.
+  EXPECT_FALSE(XUser->replaceUsesOfWith(&X, &I0));
 }
 
 TEST(UserTest, PersonalityUser) {


        


More information about the llvm-commits mailing list