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
, replaceUseOfWith
) {
123 const char *ModuleString
= "define void @f(i32 %x) {\n"
125 " %v0 = add i32 1, 1\n"
126 " %v1 = add i32 %x, 2\n"
130 std::unique_ptr
<Module
> M
= parseAssemblyString(ModuleString
, Err
, C
);
131 Function
*F
= M
->getFunction("f");
133 EXPECT_TRUE(F
->arg_begin() != F
->arg_end());
134 BasicBlock
& entryBB
= F
->front();
135 Instruction
& I0
= *(entryBB
.begin());
136 Instruction
& I1
= *(++(entryBB
.begin()));
138 Argument
&X
= *F
->arg_begin();
139 EXPECT_EQ("x", X
.getName());
140 EXPECT_NE(X
.user_begin() ,X
.user_end());
141 EXPECT_EQ(I0
.user_begin() ,I0
.user_end());
144 auto XUser
= find(X
.users(), &(I1
));
145 EXPECT_NE(XUser
, X
.user_end());
147 EXPECT_TRUE(XUser
->replaceUsesOfWith(&X
, &I0
));
148 EXPECT_EQ(X
.user_begin(), X
.user_end());
149 EXPECT_NE(I0
.user_begin(), I0
.user_end());
151 // All uses have already been replaced, nothing more to do.
152 EXPECT_FALSE(XUser
->replaceUsesOfWith(&X
, &I0
));
155 TEST(UserTest
, PersonalityUser
) {
157 Module
M("", Context
);
158 FunctionType
*RetVoidTy
= FunctionType::get(Type::getVoidTy(Context
), false);
159 Function
*PersonalityF
= Function::Create(
160 RetVoidTy
, GlobalValue::ExternalLinkage
, "PersonalityFn", &M
);
162 Function::Create(RetVoidTy
, GlobalValue::ExternalLinkage
, "TestFn", &M
);
164 // Set up the personality function
165 TestF
->setPersonalityFn(PersonalityF
);
166 auto PersonalityUsers
= PersonalityF
->user_begin();
168 // One user and that user is the Test function
169 EXPECT_EQ(*PersonalityUsers
, TestF
);
170 EXPECT_EQ(++PersonalityUsers
, PersonalityF
->user_end());
172 // Reset the personality function
173 TestF
->setPersonalityFn(nullptr);
175 // No users should remain
176 EXPECT_TRUE(TestF
->user_empty());
179 } // end anonymous namespace