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