IR: Add Value::reverseUseList()

I'm going to use this to improve `verify-uselistorder`.  Part of PR5680.

llvm-svn: 214594
This commit is contained in:
Duncan P. N. Exon Smith 2014-08-01 23:28:49 +00:00
parent e52646cd80
commit 3441ffe98d
3 changed files with 65 additions and 0 deletions

View File

@ -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.
///

View File

@ -614,6 +614,25 @@ Value *Value::DoPHITranslation(const BasicBlock *CurBB,
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
//===----------------------------------------------------------------------===//

View File

@ -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