1 //===- llvm/unittest/IR/UseTest.cpp - Use unit tests ----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/AsmParser/Parser.h"
10 #include "llvm/IR/Function.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/IR/User.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "gtest/gtest.h"
25 const char *ModuleString
= "define void @f(i32 %x) {\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"
39 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
40 Function
*F
= M
->getFunction("f");
42 ASSERT_TRUE(F
->arg_begin() != F
->arg_end());
43 Argument
&X
= *F
->arg_begin();
44 ASSERT_EQ("x", X
.getName());
46 X
.sortUseList([](const Use
&L
, const Use
&R
) {
47 return L
.getUser()->getName() < R
.getUser()->getName();
50 for (User
*U
: X
.users()) {
51 format("v%u", I
++).snprint(vnbuf
, sizeof(vnbuf
));
52 EXPECT_EQ(vnbuf
, U
->getName());
56 X
.sortUseList([](const Use
&L
, const Use
&R
) {
57 return L
.getUser()->getName() > R
.getUser()->getName();
60 for (User
*U
: X
.users()) {
61 format("v%u", (7 - I
++)).snprint(vnbuf
, sizeof(vnbuf
));
62 EXPECT_EQ(vnbuf
, U
->getName());
67 TEST(UseTest
, reverse
) {
70 const char *ModuleString
= "define void @f(i32 %x) {\n"
72 " %v0 = add i32 %x, 0\n"
73 " %v2 = add i32 %x, 2\n"
74 " %v5 = add i32 %x, 5\n"
75 " %v1 = add i32 %x, 1\n"
76 " %v3 = add i32 %x, 3\n"
77 " %v7 = add i32 %x, 7\n"
78 " %v6 = add i32 %x, 6\n"
79 " %v4 = add i32 %x, 4\n"
84 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
85 Function
*F
= M
->getFunction("f");
87 ASSERT_TRUE(F
->arg_begin() != F
->arg_end());
88 Argument
&X
= *F
->arg_begin();
89 ASSERT_EQ("x", X
.getName());
91 X
.sortUseList([](const Use
&L
, const Use
&R
) {
92 return L
.getUser()->getName() < R
.getUser()->getName();
95 for (User
*U
: X
.users()) {
96 format("v%u", I
++).snprint(vnbuf
, sizeof(vnbuf
));
97 EXPECT_EQ(vnbuf
, U
->getName());
103 for (User
*U
: X
.users()) {
104 format("v%u", (7 - I
++)).snprint(vnbuf
, sizeof(vnbuf
));
105 EXPECT_EQ(vnbuf
, U
->getName());
110 } // end anonymous namespace