[llvm] r214594 - IR: Add Value::reverseUseList()
Duncan P. N. Exon Smith
dexonsmith at apple.com
Fri Aug 1 16:28:49 PDT 2014
Author: dexonsmith
Date: Fri Aug 1 18:28:49 2014
New Revision: 214594
URL: http://llvm.org/viewvc/llvm-project?rev=214594&view=rev
Log:
IR: Add Value::reverseUseList()
I'm going to use this to improve `verify-uselistorder`. Part of PR5680.
Modified:
llvm/trunk/include/llvm/IR/Value.h
llvm/trunk/lib/IR/Value.cpp
llvm/trunk/unittests/IR/UseTest.cpp
Modified: llvm/trunk/include/llvm/IR/Value.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Value.h?rev=214594&r1=214593&r2=214594&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Value.h (original)
+++ llvm/trunk/include/llvm/IR/Value.h Fri Aug 1 18:28:49 2014
@@ -462,6 +462,9 @@ public:
/// expected to compare two \a Use references.
template <class Compare> void sortUseList(Compare Cmp);
+ /// \brief Reverse the use-list.
+ void reverseUseList();
+
private:
/// \brief Merge two lists together.
///
Modified: llvm/trunk/lib/IR/Value.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Value.cpp?rev=214594&r1=214593&r2=214594&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Value.cpp (original)
+++ llvm/trunk/lib/IR/Value.cpp Fri Aug 1 18:28:49 2014
@@ -614,6 +614,25 @@ Value *Value::DoPHITranslation(const Bas
LLVMContext &Value::getContext() const { return VTy->getContext(); }
+void Value::reverseUseList() {
+ if (!UseList || !UseList->Next)
+ // No need to reverse 0 or 1 uses.
+ return;
+
+ Use *Head = UseList;
+ Use *Current = UseList->Next;
+ Head->Next = nullptr;
+ while (Current) {
+ Use *Next = Current->Next;
+ Current->Next = Head;
+ Head->setPrev(&Current->Next);
+ Head = Current;
+ Current = Next;
+ }
+ UseList = Head;
+ Head->setPrev(&UseList);
+}
+
//===----------------------------------------------------------------------===//
// ValueHandleBase Class
//===----------------------------------------------------------------------===//
Modified: llvm/trunk/unittests/IR/UseTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/UseTest.cpp?rev=214594&r1=214593&r2=214594&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/UseTest.cpp (original)
+++ llvm/trunk/unittests/IR/UseTest.cpp Fri Aug 1 18:28:49 2014
@@ -66,4 +66,47 @@ TEST(UseTest, sort) {
ASSERT_EQ(8u, I);
}
+TEST(UseTest, reverse) {
+ LLVMContext C;
+
+ const char *ModuleString = "define void @f(i32 %x) {\n"
+ "entry:\n"
+ " %v0 = add i32 %x, 0\n"
+ " %v2 = add i32 %x, 2\n"
+ " %v5 = add i32 %x, 5\n"
+ " %v1 = add i32 %x, 1\n"
+ " %v3 = add i32 %x, 3\n"
+ " %v7 = add i32 %x, 7\n"
+ " %v6 = add i32 %x, 6\n"
+ " %v4 = add i32 %x, 4\n"
+ " ret void\n"
+ "}\n";
+ SMDiagnostic Err;
+ char vnbuf[8];
+ Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C);
+ Function *F = M->getFunction("f");
+ ASSERT_TRUE(F);
+ ASSERT_TRUE(F->arg_begin() != F->arg_end());
+ Argument &X = *F->arg_begin();
+ ASSERT_EQ("x", X.getName());
+
+ X.sortUseList([](const Use &L, const Use &R) {
+ return L.getUser()->getName() < R.getUser()->getName();
+ });
+ unsigned I = 0;
+ for (User *U : X.users()) {
+ snprintf(vnbuf, sizeof(vnbuf), "v%u", I++);
+ EXPECT_EQ(vnbuf, U->getName());
+ }
+ ASSERT_EQ(8u, I);
+
+ X.reverseUseList();
+ I = 0;
+ for (User *U : X.users()) {
+ snprintf(vnbuf, sizeof(vnbuf), "v%u", (7 - I++));
+ EXPECT_EQ(vnbuf, U->getName());
+ }
+ ASSERT_EQ(8u, I);
+}
+
} // end anonymous namespace
More information about the llvm-commits
mailing list