1 //===- llvm/unittest/IR/UserTest.cpp - User 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/IR/User.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "gtest/gtest.h"
18 using namespace llvm;
19
20 namespace {
21
TEST(UserTest,ValueOpIteration)22 TEST(UserTest, ValueOpIteration) {
23 LLVMContext C;
24
25 const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
26 "entry:\n"
27 " switch i32 undef, label %s0\n"
28 " [ i32 1, label %s1\n"
29 " i32 2, label %s2\n"
30 " i32 3, label %s3\n"
31 " i32 4, label %s4\n"
32 " i32 5, label %s5\n"
33 " i32 6, label %s6\n"
34 " i32 7, label %s7\n"
35 " i32 8, label %s8\n"
36 " i32 9, label %s9 ]\n"
37 "\n"
38 "s0:\n"
39 " br label %exit\n"
40 "s1:\n"
41 " br label %exit\n"
42 "s2:\n"
43 " br label %exit\n"
44 "s3:\n"
45 " br label %exit\n"
46 "s4:\n"
47 " br label %exit\n"
48 "s5:\n"
49 " br label %exit\n"
50 "s6:\n"
51 " br label %exit\n"
52 "s7:\n"
53 " br label %exit\n"
54 "s8:\n"
55 " br label %exit\n"
56 "s9:\n"
57 " br label %exit\n"
58 "\n"
59 "exit:\n"
60 " %phi = phi i32 [ 0, %s0 ], [ 1, %s1 ],\n"
61 " [ 2, %s2 ], [ 3, %s3 ],\n"
62 " [ 4, %s4 ], [ 5, %s5 ],\n"
63 " [ 6, %s6 ], [ 7, %s7 ],\n"
64 " [ 8, %s8 ], [ 9, %s9 ]\n"
65 " ret void\n"
66 "}\n";
67 SMDiagnostic Err;
68 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
69
70 Function *F = M->getFunction("f");
71 BasicBlock &ExitBB = F->back();
72 PHINode &P = cast<PHINode>(ExitBB.front());
73 EXPECT_TRUE(P.value_op_begin() == P.value_op_begin());
74 EXPECT_FALSE(P.value_op_begin() == P.value_op_end());
75 EXPECT_TRUE(P.value_op_begin() != P.value_op_end());
76 EXPECT_FALSE(P.value_op_end() != P.value_op_end());
77 EXPECT_TRUE(P.value_op_begin() < P.value_op_end());
78 EXPECT_FALSE(P.value_op_begin() < P.value_op_begin());
79 EXPECT_TRUE(P.value_op_end() > P.value_op_begin());
80 EXPECT_FALSE(P.value_op_begin() > P.value_op_begin());
81 EXPECT_TRUE(P.value_op_begin() <= P.value_op_begin());
82 EXPECT_FALSE(P.value_op_end() <= P.value_op_begin());
83 EXPECT_TRUE(P.value_op_begin() >= P.value_op_begin());
84 EXPECT_FALSE(P.value_op_begin() >= P.value_op_end());
85 EXPECT_EQ(10, std::distance(P.value_op_begin(), P.value_op_end()));
86
87 // const value op iteration
88 const PHINode *IP = &P;
89 EXPECT_TRUE(IP->value_op_begin() == IP->value_op_begin());
90 EXPECT_FALSE(IP->value_op_begin() == IP->value_op_end());
91 EXPECT_TRUE(IP->value_op_begin() != IP->value_op_end());
92 EXPECT_FALSE(IP->value_op_end() != IP->value_op_end());
93 EXPECT_TRUE(IP->value_op_begin() < IP->value_op_end());
94 EXPECT_FALSE(IP->value_op_begin() < IP->value_op_begin());
95 EXPECT_TRUE(IP->value_op_end() > IP->value_op_begin());
96 EXPECT_FALSE(IP->value_op_begin() > IP->value_op_begin());
97 EXPECT_TRUE(IP->value_op_begin() <= IP->value_op_begin());
98 EXPECT_FALSE(IP->value_op_end() <= IP->value_op_begin());
99 EXPECT_TRUE(IP->value_op_begin() >= IP->value_op_begin());
100 EXPECT_FALSE(IP->value_op_begin() >= IP->value_op_end());
101 EXPECT_EQ(10, std::distance(IP->value_op_begin(), IP->value_op_end()));
102
103 User::value_op_iterator I = P.value_op_begin();
104 I += 3;
105 EXPECT_EQ(std::next(P.value_op_begin(), 3), I);
106 EXPECT_EQ(P.getOperand(3), *I);
107 I++;
108 EXPECT_EQ(P.getOperand(6), I[2]);
109 EXPECT_EQ(P.value_op_end(), (I - 2) + 8);
110
111 // const value op
112 User::const_value_op_iterator CI = IP->value_op_begin();
113 CI += 3;
114 EXPECT_EQ(std::next(IP->value_op_begin(), 3), CI);
115 EXPECT_EQ(IP->getOperand(3), *CI);
116 CI++;
117 EXPECT_EQ(IP->getOperand(6), CI[2]);
118 EXPECT_EQ(IP->value_op_end(), (CI - 2) + 8);
119 }
120
TEST(UserTest,PersonalityUser)121 TEST(UserTest, PersonalityUser) {
122 LLVMContext Context;
123 Module M("", Context);
124 FunctionType *RetVoidTy = FunctionType::get(Type::getVoidTy(Context), false);
125 Function *PersonalityF = Function::Create(
126 RetVoidTy, GlobalValue::ExternalLinkage, "PersonalityFn", &M);
127 Function *TestF =
128 Function::Create(RetVoidTy, GlobalValue::ExternalLinkage, "TestFn", &M);
129
130 // Set up the personality function
131 TestF->setPersonalityFn(PersonalityF);
132 auto PersonalityUsers = PersonalityF->user_begin();
133
134 // One user and that user is the Test function
135 EXPECT_EQ(*PersonalityUsers, TestF);
136 EXPECT_EQ(++PersonalityUsers, PersonalityF->user_end());
137
138 // Reset the personality function
139 TestF->setPersonalityFn(nullptr);
140
141 // No users should remain
142 EXPECT_TRUE(TestF->user_empty());
143 }
144
145 } // end anonymous namespace
146