1 //===- llvm/unittest/IR/UseTest.cpp - Use unit tests ----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/AsmParser/Parser.h" 11 #include "llvm/IR/Function.h" 12 #include "llvm/IR/LLVMContext.h" 13 #include "llvm/IR/Module.h" 14 #include "llvm/IR/User.h" 15 #include "llvm/IR/Instructions.h" 16 #include "llvm/Support/SourceMgr.h" 17 #include "gtest/gtest.h" 18 using namespace llvm; 19 20 namespace { 21 22 TEST(UseTest, sort) { 23 LLVMContext C; 24 25 const char *ModuleString = "define void @f(i32 %x) {\n" 26 "entry:\n" 27 " %v0 = add i32 %x, 0\n" 28 " %v2 = add i32 %x, 2\n" 29 " %v5 = add i32 %x, 5\n" 30 " %v1 = add i32 %x, 1\n" 31 " %v3 = add i32 %x, 3\n" 32 " %v7 = add i32 %x, 7\n" 33 " %v6 = add i32 %x, 6\n" 34 " %v4 = add i32 %x, 4\n" 35 " ret void\n" 36 "}\n"; 37 SMDiagnostic Err; 38 Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C); 39 Function *F = M->getFunction("f"); 40 ASSERT_TRUE(F); 41 ASSERT_TRUE(F->arg_begin() != F->arg_end()); 42 Argument &X = *F->arg_begin(); 43 ASSERT_EQ("x", X.getName()); 44 45 X.sortUseList([](const Use &L, const Use &R) { 46 return L.getUser()->getName() < R.getUser()->getName(); 47 }); 48 unsigned I = 0; 49 for (User *U : X.users()) 50 EXPECT_EQ("v" + std::to_string(I++), U->getName()); 51 ASSERT_EQ(8u, I); 52 53 X.sortUseList([](const Use &L, const Use &R) { 54 return L.getUser()->getName() > R.getUser()->getName(); 55 }); 56 I = 0; 57 for (User *U : X.users()) 58 EXPECT_EQ("v" + std::to_string((7 - I++)), U->getName()); 59 ASSERT_EQ(8u, I); 60 } 61 62 } // end anonymous namespace 63