Recommit "rL366894: [yaml2obj] - Allow custom fields for the SHT_UNDEF sections."
[llvm-complete.git] / unittests / IR / UserTest.cpp
blobe495735aec6532b282e8614f01f6524c6d9e3d4d
1 //===- llvm/unittest/IR/UserTest.cpp - User unit tests --------------------===//
2 //
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
6 //
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"
17 using namespace llvm;
19 namespace {
21 TEST(UserTest, ValueOpIteration) {
22 LLVMContext C;
24 const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
25 "entry:\n"
26 " switch i32 undef, label %s0\n"
27 " [ i32 1, label %s1\n"
28 " i32 2, label %s2\n"
29 " i32 3, label %s3\n"
30 " i32 4, label %s4\n"
31 " i32 5, label %s5\n"
32 " i32 6, label %s6\n"
33 " i32 7, label %s7\n"
34 " i32 8, label %s8\n"
35 " i32 9, label %s9 ]\n"
36 "\n"
37 "s0:\n"
38 " br label %exit\n"
39 "s1:\n"
40 " br label %exit\n"
41 "s2:\n"
42 " br label %exit\n"
43 "s3:\n"
44 " br label %exit\n"
45 "s4:\n"
46 " br label %exit\n"
47 "s5:\n"
48 " br label %exit\n"
49 "s6:\n"
50 " br label %exit\n"
51 "s7:\n"
52 " br label %exit\n"
53 "s8:\n"
54 " br label %exit\n"
55 "s9:\n"
56 " br label %exit\n"
57 "\n"
58 "exit:\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"
64 " ret void\n"
65 "}\n";
66 SMDiagnostic Err;
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();
103 I += 3;
104 EXPECT_EQ(std::next(P.value_op_begin(), 3), I);
105 EXPECT_EQ(P.getOperand(3), *I);
106 I++;
107 EXPECT_EQ(P.getOperand(6), I[2]);
108 EXPECT_EQ(P.value_op_end(), (I - 2) + 8);
110 // const value op
111 User::const_value_op_iterator CI = IP->value_op_begin();
112 CI += 3;
113 EXPECT_EQ(std::next(IP->value_op_begin(), 3), CI);
114 EXPECT_EQ(IP->getOperand(3), *CI);
115 CI++;
116 EXPECT_EQ(IP->getOperand(6), CI[2]);
117 EXPECT_EQ(IP->value_op_end(), (CI - 2) + 8);
120 TEST(UserTest, PersonalityUser) {
121 LLVMContext Context;
122 Module M("", Context);
123 FunctionType *RetVoidTy = FunctionType::get(Type::getVoidTy(Context), false);
124 Function *PersonalityF = Function::Create(
125 RetVoidTy, GlobalValue::ExternalLinkage, "PersonalityFn", &M);
126 Function *TestF =
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