[llvm-exegesis][NFC] Return many CodeTemplates instead of one.
[llvm-complete.git] / unittests / IR / UseTest.cpp
blob51ac2f48689dba46a351d8454b25930c581fcf29
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 //===----------------------------------------------------------------------===//
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/Support/Format.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "gtest/gtest.h"
19 using namespace llvm;
21 namespace {
23 TEST(UseTest, sort) {
24 LLVMContext C;
26 const char *ModuleString = "define void @f(i32 %x) {\n"
27 "entry:\n"
28 " %v0 = add i32 %x, 0\n"
29 " %v2 = add i32 %x, 2\n"
30 " %v5 = add i32 %x, 5\n"
31 " %v1 = add i32 %x, 1\n"
32 " %v3 = add i32 %x, 3\n"
33 " %v7 = add i32 %x, 7\n"
34 " %v6 = add i32 %x, 6\n"
35 " %v4 = add i32 %x, 4\n"
36 " ret void\n"
37 "}\n";
38 SMDiagnostic Err;
39 char vnbuf[8];
40 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
41 Function *F = M->getFunction("f");
42 ASSERT_TRUE(F);
43 ASSERT_TRUE(F->arg_begin() != F->arg_end());
44 Argument &X = *F->arg_begin();
45 ASSERT_EQ("x", X.getName());
47 X.sortUseList([](const Use &L, const Use &R) {
48 return L.getUser()->getName() < R.getUser()->getName();
49 });
50 unsigned I = 0;
51 for (User *U : X.users()) {
52 format("v%u", I++).snprint(vnbuf, sizeof(vnbuf));
53 EXPECT_EQ(vnbuf, U->getName());
55 ASSERT_EQ(8u, I);
57 X.sortUseList([](const Use &L, const Use &R) {
58 return L.getUser()->getName() > R.getUser()->getName();
59 });
60 I = 0;
61 for (User *U : X.users()) {
62 format("v%u", (7 - I++)).snprint(vnbuf, sizeof(vnbuf));
63 EXPECT_EQ(vnbuf, U->getName());
65 ASSERT_EQ(8u, I);
68 TEST(UseTest, reverse) {
69 LLVMContext C;
71 const char *ModuleString = "define void @f(i32 %x) {\n"
72 "entry:\n"
73 " %v0 = add i32 %x, 0\n"
74 " %v2 = add i32 %x, 2\n"
75 " %v5 = add i32 %x, 5\n"
76 " %v1 = add i32 %x, 1\n"
77 " %v3 = add i32 %x, 3\n"
78 " %v7 = add i32 %x, 7\n"
79 " %v6 = add i32 %x, 6\n"
80 " %v4 = add i32 %x, 4\n"
81 " ret void\n"
82 "}\n";
83 SMDiagnostic Err;
84 char vnbuf[8];
85 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
86 Function *F = M->getFunction("f");
87 ASSERT_TRUE(F);
88 ASSERT_TRUE(F->arg_begin() != F->arg_end());
89 Argument &X = *F->arg_begin();
90 ASSERT_EQ("x", X.getName());
92 X.sortUseList([](const Use &L, const Use &R) {
93 return L.getUser()->getName() < R.getUser()->getName();
94 });
95 unsigned I = 0;
96 for (User *U : X.users()) {
97 format("v%u", I++).snprint(vnbuf, sizeof(vnbuf));
98 EXPECT_EQ(vnbuf, U->getName());
100 ASSERT_EQ(8u, I);
102 X.reverseUseList();
103 I = 0;
104 for (User *U : X.users()) {
105 format("v%u", (7 - I++)).snprint(vnbuf, sizeof(vnbuf));
106 EXPECT_EQ(vnbuf, U->getName());
108 ASSERT_EQ(8u, I);
111 } // end anonymous namespace