[PATCH] D84135: [IR] add comments and a unit test for User::replaceUsesOfWith

Chuanqi Xu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 19 23:07:16 PDT 2020


ChuanqiXu created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

The origin comment in User::replaceUsesOfWith writes:

  if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
        // The side effects of this setOperand call include linking to
        // "To", adding "this" to the uses list of To, and
        // most importantly, removing "this" from the use list of "From".
        setOperand(i, To);// fix it now
      }

The line `fix it now` looks like we need a patch here to achieve the effects described above. However, the effects had been implemented. So I think it is more clear to delete this comment.


https://reviews.llvm.org/D84135

Files:
  llvm/lib/IR/User.cpp
  llvm/unittests/IR/UserTest.cpp


Index: llvm/unittests/IR/UserTest.cpp
===================================================================
--- llvm/unittests/IR/UserTest.cpp
+++ llvm/unittests/IR/UserTest.cpp
@@ -117,6 +117,38 @@
   EXPECT_EQ(IP->value_op_end(), (CI - 2) + 8);
 }
 
+TEST(UserTest, replaceUseOfWith) {
+  LLVMContext C;
+
+  const char *ModuleString = "define void @f(i32 %x) {\n"
+                             "entry:\n"
+                             "  %v0 = add i32 1, 1\n"
+                             "  %v1 = add i32 %x, 2\n"
+                             "  ret void\n"
+                             "}\n";
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
+  Function *F = M->getFunction("f");
+  EXPECT_TRUE(F);
+  EXPECT_TRUE(F->arg_begin() != F->arg_end());
+  BasicBlock& entryBB = F->front();
+  Instruction& I0 = *(entryBB.begin());
+  Instruction& I1 = *(++(entryBB.begin()));
+
+  Argument &X = *F->arg_begin();
+  EXPECT_EQ("x", X.getName());
+  EXPECT_NE(X.user_begin() ,X.user_end());
+  EXPECT_EQ(I0.user_begin() ,I0.user_end());
+
+
+  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());
+}
+
 TEST(UserTest, PersonalityUser) {
   LLVMContext Context;
   Module M("", Context);
Index: llvm/lib/IR/User.cpp
===================================================================
--- llvm/lib/IR/User.cpp
+++ llvm/lib/IR/User.cpp
@@ -29,7 +29,7 @@
       // The side effects of this setOperand call include linking to
       // "To", adding "this" to the uses list of To, and
       // most importantly, removing "this" from the use list of "From".
-      setOperand(i, To); // Fix it now...
+      setOperand(i, To);
     }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84135.279126.patch
Type: text/x-patch
Size: 1827 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200720/a2934d58/attachment.bin>


More information about the llvm-commits mailing list