1 //===- llvm/unittest/IR/UserTest.cpp - User 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/IR/User.h"
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/Support/SourceMgr.h"
16 #include "gtest/gtest.h"
21 TEST(UserTest
, ValueOpIteration
) {
24 const char *ModuleString
= "define void @f(i32 %x, i32 %y) {\n"
26 " switch i32 undef, label %s0\n"
27 " [ i32 1, label %s1\n"
35 " i32 9, label %s9 ]\n"
59 " %phi = phi i32 [ 0, %s0 ], [ 1, %s1 ],\n"
60 " [ 2, %s2 ], [ 3, %s3 ],\n"
61 " [ 4, %s4 ], [ 5, %s5 ],\n"
62 " [ 6, %s6 ], [ 7, %s7 ],\n"
63 " [ 8, %s8 ], [ 9, %s9 ]\n"
67 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
69 Function
*F
= M
->getFunction("f");
70 BasicBlock
&ExitBB
= F
->back();
71 PHINode
&P
= cast
<PHINode
>(ExitBB
.front());
72 EXPECT_TRUE(P
.value_op_begin() == P
.value_op_begin());
73 EXPECT_FALSE(P
.value_op_begin() == P
.value_op_end());
74 EXPECT_TRUE(P
.value_op_begin() != P
.value_op_end());
75 EXPECT_FALSE(P
.value_op_end() != P
.value_op_end());
76 EXPECT_TRUE(P
.value_op_begin() < P
.value_op_end());
77 EXPECT_FALSE(P
.value_op_begin() < P
.value_op_begin());
78 EXPECT_TRUE(P
.value_op_end() > P
.value_op_begin());
79 EXPECT_FALSE(P
.value_op_begin() > P
.value_op_begin());
80 EXPECT_TRUE(P
.value_op_begin() <= P
.value_op_begin());
81 EXPECT_FALSE(P
.value_op_end() <= P
.value_op_begin());
82 EXPECT_TRUE(P
.value_op_begin() >= P
.value_op_begin());
83 EXPECT_FALSE(P
.value_op_begin() >= P
.value_op_end());
84 EXPECT_EQ(10, std::distance(P
.value_op_begin(), P
.value_op_end()));
86 // const value op iteration
87 const PHINode
*IP
= &P
;
88 EXPECT_TRUE(IP
->value_op_begin() == IP
->value_op_begin());
89 EXPECT_FALSE(IP
->value_op_begin() == IP
->value_op_end());
90 EXPECT_TRUE(IP
->value_op_begin() != IP
->value_op_end());
91 EXPECT_FALSE(IP
->value_op_end() != IP
->value_op_end());
92 EXPECT_TRUE(IP
->value_op_begin() < IP
->value_op_end());
93 EXPECT_FALSE(IP
->value_op_begin() < IP
->value_op_begin());
94 EXPECT_TRUE(IP
->value_op_end() > IP
->value_op_begin());
95 EXPECT_FALSE(IP
->value_op_begin() > IP
->value_op_begin());
96 EXPECT_TRUE(IP
->value_op_begin() <= IP
->value_op_begin());
97 EXPECT_FALSE(IP
->value_op_end() <= IP
->value_op_begin());
98 EXPECT_TRUE(IP
->value_op_begin() >= IP
->value_op_begin());
99 EXPECT_FALSE(IP
->value_op_begin() >= IP
->value_op_end());
100 EXPECT_EQ(10, std::distance(IP
->value_op_begin(), IP
->value_op_end()));
102 User::value_op_iterator I
= P
.value_op_begin();
104 EXPECT_EQ(std::next(P
.value_op_begin(), 3), I
);
105 EXPECT_EQ(P
.getOperand(3), *I
);
107 EXPECT_EQ(P
.getOperand(6), I
[2]);
108 EXPECT_EQ(P
.value_op_end(), (I
- 2) + 8);
111 User::const_value_op_iterator CI
= IP
->value_op_begin();
113 EXPECT_EQ(std::next(IP
->value_op_begin(), 3), CI
);
114 EXPECT_EQ(IP
->getOperand(3), *CI
);
116 EXPECT_EQ(IP
->getOperand(6), CI
[2]);
117 EXPECT_EQ(IP
->value_op_end(), (CI
- 2) + 8);
120 TEST(UserTest
, PersonalityUser
) {
122 Module
M("", Context
);
123 FunctionType
*RetVoidTy
= FunctionType::get(Type::getVoidTy(Context
), false);
124 Function
*PersonalityF
= Function::Create(
125 RetVoidTy
, GlobalValue::ExternalLinkage
, "PersonalityFn", &M
);
127 Function::Create(RetVoidTy
, GlobalValue::ExternalLinkage
, "TestFn", &M
);
129 // Set up the personality function
130 TestF
->setPersonalityFn(PersonalityF
);
131 auto PersonalityUsers
= PersonalityF
->user_begin();
133 // One user and that user is the Test function
134 EXPECT_EQ(*PersonalityUsers
, TestF
);
135 EXPECT_EQ(++PersonalityUsers
, PersonalityF
->user_end());
137 // Reset the personality function
138 TestF
->setPersonalityFn(nullptr);
140 // No users should remain
141 EXPECT_TRUE(TestF
->user_empty());
144 } // end anonymous namespace