Skip to content

Commit

Permalink
[IR] Adding const_value_op_iterator for IR/User.h
Browse files Browse the repository at this point in the history
const value op iterator is missing from User.h class.

Differential Revision: https://reviews.llvm.org/D28464



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@291443 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Mohammed Agabaria committed Jan 9, 2017
1 parent 7f03185 commit a2d2e19
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
20 changes: 20 additions & 0 deletions include/llvm/IR/User.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,26 @@ class User : public Value {
return make_range(value_op_begin(), value_op_end());
}

struct const_value_op_iterator
: iterator_adaptor_base<const_value_op_iterator, const_op_iterator,
std::random_access_iterator_tag, const Value *,
ptrdiff_t, const Value *, const Value *> {
explicit const_value_op_iterator(const Use *U = nullptr) :
iterator_adaptor_base(U) {}
const Value *operator*() const { return *I; }
const Value *operator->() const { return operator*(); }
};

const_value_op_iterator value_op_begin() const {
return const_value_op_iterator(op_begin());
}
const_value_op_iterator value_op_end() const {
return const_value_op_iterator(op_end());
}
iterator_range<const_value_op_iterator> operand_values() const {
return make_range(value_op_begin(), value_op_end());
}

/// \brief Drop all references to operands.
///
/// This function is in charge of "letting go" of all objects that this User
Expand Down
25 changes: 25 additions & 0 deletions unittests/IR/UserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,38 @@ TEST(UserTest, ValueOpIteration) {
EXPECT_FALSE(P.value_op_begin() >= P.value_op_end());
EXPECT_EQ(10, std::distance(P.value_op_begin(), P.value_op_end()));

// const value op iteration
const PHINode *IP = &P;
EXPECT_TRUE(IP->value_op_begin() == IP->value_op_begin());
EXPECT_FALSE(IP->value_op_begin() == IP->value_op_end());
EXPECT_TRUE(IP->value_op_begin() != IP->value_op_end());
EXPECT_FALSE(IP->value_op_end() != IP->value_op_end());
EXPECT_TRUE(IP->value_op_begin() < IP->value_op_end());
EXPECT_FALSE(IP->value_op_begin() < IP->value_op_begin());
EXPECT_TRUE(IP->value_op_end() > IP->value_op_begin());
EXPECT_FALSE(IP->value_op_begin() > IP->value_op_begin());
EXPECT_TRUE(IP->value_op_begin() <= IP->value_op_begin());
EXPECT_FALSE(IP->value_op_end() <= IP->value_op_begin());
EXPECT_TRUE(IP->value_op_begin() >= IP->value_op_begin());
EXPECT_FALSE(IP->value_op_begin() >= IP->value_op_end());
EXPECT_EQ(10, std::distance(IP->value_op_begin(), IP->value_op_end()));

User::value_op_iterator I = P.value_op_begin();
I += 3;
EXPECT_EQ(std::next(P.value_op_begin(), 3), I);
EXPECT_EQ(P.getOperand(3), *I);
I++;
EXPECT_EQ(P.getOperand(6), I[2]);
EXPECT_EQ(P.value_op_end(), (I - 2) + 8);

// const value op
User::const_value_op_iterator CI = IP->value_op_begin();
CI += 3;
EXPECT_EQ(std::next(IP->value_op_begin(), 3), CI);
EXPECT_EQ(IP->getOperand(3), *CI);
CI++;
EXPECT_EQ(IP->getOperand(6), CI[2]);
EXPECT_EQ(IP->value_op_end(), (CI - 2) + 8);
}

TEST(UserTest, PersonalityUser) {
Expand Down

0 comments on commit a2d2e19

Please sign in to comment.