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/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"
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   User::value_op_iterator I = P.value_op_begin();
88   I += 3;
89   EXPECT_EQ(std::next(P.value_op_begin(), 3), I);
90   EXPECT_EQ(P.getOperand(3), *I);
91   I++;
92   EXPECT_EQ(P.getOperand(6), I[2]);
93   EXPECT_EQ(P.value_op_end(), (I - 2) + 8);
94 }
95 
96 } // end anonymous namespace
97