1 //===- llvm/unittest/IR/UserTest.cpp - User unit tests --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/SourceMgr.h"
17 #include "gtest/gtest.h"
22 TEST(UserTest, ValueOpIteration) {
25 const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
27 " switch i32 undef, label %s0\n"
28 " [ i32 1, label %s1\n"
36 " i32 9, label %s9 ]\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"
68 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
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()));
87 User::value_op_iterator I = P.value_op_begin();
89 EXPECT_EQ(std::next(P.value_op_begin(), 3), I);
90 EXPECT_EQ(P.getOperand(3), *I);
92 EXPECT_EQ(P.getOperand(6), I[2]);
93 EXPECT_EQ(P.value_op_end(), (I - 2) + 8);
96 TEST(UserTest, PersonalityUser) {
97 Module M("", getGlobalContext());
98 FunctionType *RetVoidTy =
99 FunctionType::get(Type::getVoidTy(getGlobalContext()), false);
100 Function *PersonalityF = Function::Create(
101 RetVoidTy, GlobalValue::ExternalLinkage, "PersonalityFn", &M);
103 Function::Create(RetVoidTy, GlobalValue::ExternalLinkage, "TestFn", &M);
105 // Set up the personality function
106 TestF->setPersonalityFn(PersonalityF);
107 auto PersonalityUsers = PersonalityF->user_begin();
109 // One user and that user is the Test function
110 EXPECT_EQ(*PersonalityUsers, TestF);
111 EXPECT_EQ(++PersonalityUsers, PersonalityF->user_end());
113 // Reset the personality function
114 TestF->setPersonalityFn(nullptr);
116 // No users should remain
117 EXPECT_TRUE(TestF->user_empty());
120 } // end anonymous namespace