llvm.org GIT mirror llvm / c95ce87
Make the User::value_op_iterator a random access iterator. I had written this code ages ago and lost track of it. Seems worth doing though -- this thing can get called from places that would benefit from knowing that std::distance is O(1). Also add a very fledgeling unittest for Users and make sure various aspects of this seem to work reasonably. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206453 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 6 years ago
3 changed file(s) with 139 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
128128
129129 /// Convenience iterator for directly iterating over the Values in the
130130 /// OperandList
131 class value_op_iterator : public std::iterator
132 Value*> {
131 class value_op_iterator
132 : public std::iterator
133 ptrdiff_t, Value *, Value *> {
133134 op_iterator OI;
134135 public:
135 explicit value_op_iterator(Use *U) : OI(U) {}
136 explicit value_op_iterator(Use *U = nullptr) : OI(U) {}
136137
137138 bool operator==(const value_op_iterator &x) const {
138139 return OI == x.OI;
141142 return !operator==(x);
142143 }
143144
144 /// Iterator traversal: forward iteration only
145 value_op_iterator &operator++() { // Preincrement
146 ++OI;
145 value_op_iterator &operator+=(ptrdiff_t n) {
146 OI += n;
147147 return *this;
148148 }
149 value_op_iterator operator++(int) { // Postincrement
150 value_op_iterator tmp = *this; ++*this; return tmp;
151 }
152
153 /// Retrieve a pointer to the current Value.
154 Value *operator*() const {
155 return *OI;
156 }
157
149 value_op_iterator &operator-=(ptrdiff_t n) {
150 OI -= n;
151 return *this;
152 }
153 value_op_iterator operator+(ptrdiff_t n) const {
154 return value_op_iterator(OI + n);
155 }
156 friend value_op_iterator operator+(ptrdiff_t n,
157 const value_op_iterator &i) {
158 return i + n;
159 }
160 value_op_iterator operator-(ptrdiff_t n) const {
161 return value_op_iterator(OI - n);
162 }
163 ptrdiff_t operator-(const value_op_iterator &RHS) const {
164 return OI - RHS.OI;
165 }
166 bool operator<(const value_op_iterator &RHS) const { return OI < RHS.OI; }
167 bool operator>(const value_op_iterator &RHS) const { return OI > RHS.OI; }
168 bool operator<=(const value_op_iterator &RHS) const { return OI <= RHS.OI; }
169 bool operator>=(const value_op_iterator &RHS) const { return OI >= RHS.OI; }
170 value_op_iterator &operator++() { return *this += 1; }
171 value_op_iterator &operator--() { return *this -= 1; }
172 value_op_iterator operator++(int) {
173 value_op_iterator tmp = *this;
174 ++*this;
175 return tmp;
176 }
177 value_op_iterator operator--(int) {
178 value_op_iterator tmp = *this;
179 --*this;
180 return tmp;
181 }
182
183 Value *operator*() const { return *OI; }
158184 Value *operator->() const { return operator*(); }
185 Value *operator[](ptrdiff_t n) const { return *(*this + n); }
159186 };
160187
161188 inline value_op_iterator value_op_begin() {
2020 PatternMatch.cpp
2121 TypeBuilderTest.cpp
2222 TypesTest.cpp
23 UserTest.cpp
2324 ValueHandleTest.cpp
2425 ValueMapTest.cpp
2526 ValueTest.cpp
0 //===- llvm/unittest/IR/UserTest.cpp - User unit tests --------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/AsmParser/Parser.h"
10 #include "llvm/IR/Function.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/IR/User.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "gtest/gtest.h"
17 using namespace llvm;
18
19 namespace {
20
21 TEST(UserTest, ValueOpIteration) {
22 LLVMContext C;
23
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 Module *M = ParseAssemblyString(ModuleString, NULL, Err, C);
68
69 Function *F = M->getFunction("f");
70 BasicBlock &ExitBB = F->back();
71 PHINode &P = cast(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()));
85
86 User::value_op_iterator I = P.value_op_begin();
87 I += 3;
88 EXPECT_EQ(std::next(P.value_op_begin(), 3), I);
89 EXPECT_EQ(P.getOperand(3), *I);
90 I++;
91 EXPECT_EQ(P.getOperand(6), I[2]);
92 EXPECT_EQ(P.value_op_end(), (I - 2) + 8);
93 }
94
95 } // end anonymous namespace