mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-11-01 15:11:24 +00:00
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
This commit is contained in:
parent
a30ccb064b
commit
c95ce87c23
@ -129,11 +129,12 @@ public:
|
||||
|
||||
/// Convenience iterator for directly iterating over the Values in the
|
||||
/// OperandList
|
||||
class value_op_iterator : public std::iterator<std::forward_iterator_tag,
|
||||
Value*> {
|
||||
class value_op_iterator
|
||||
: public std::iterator<std::random_access_iterator_tag, Value *,
|
||||
ptrdiff_t, Value *, Value *> {
|
||||
op_iterator OI;
|
||||
public:
|
||||
explicit value_op_iterator(Use *U) : OI(U) {}
|
||||
explicit value_op_iterator(Use *U = nullptr) : OI(U) {}
|
||||
|
||||
bool operator==(const value_op_iterator &x) const {
|
||||
return OI == x.OI;
|
||||
@ -142,21 +143,47 @@ public:
|
||||
return !operator==(x);
|
||||
}
|
||||
|
||||
/// Iterator traversal: forward iteration only
|
||||
value_op_iterator &operator++() { // Preincrement
|
||||
++OI;
|
||||
value_op_iterator &operator+=(ptrdiff_t n) {
|
||||
OI += n;
|
||||
return *this;
|
||||
}
|
||||
value_op_iterator operator++(int) { // Postincrement
|
||||
value_op_iterator tmp = *this; ++*this; return tmp;
|
||||
}
|
||||
|
||||
/// Retrieve a pointer to the current Value.
|
||||
Value *operator*() const {
|
||||
return *OI;
|
||||
value_op_iterator &operator-=(ptrdiff_t n) {
|
||||
OI -= n;
|
||||
return *this;
|
||||
}
|
||||
value_op_iterator operator+(ptrdiff_t n) const {
|
||||
return value_op_iterator(OI + n);
|
||||
}
|
||||
friend value_op_iterator operator+(ptrdiff_t n,
|
||||
const value_op_iterator &i) {
|
||||
return i + n;
|
||||
}
|
||||
value_op_iterator operator-(ptrdiff_t n) const {
|
||||
return value_op_iterator(OI - n);
|
||||
}
|
||||
ptrdiff_t operator-(const value_op_iterator &RHS) const {
|
||||
return OI - RHS.OI;
|
||||
}
|
||||
bool operator<(const value_op_iterator &RHS) const { return OI < RHS.OI; }
|
||||
bool operator>(const value_op_iterator &RHS) const { return OI > RHS.OI; }
|
||||
bool operator<=(const value_op_iterator &RHS) const { return OI <= RHS.OI; }
|
||||
bool operator>=(const value_op_iterator &RHS) const { return OI >= RHS.OI; }
|
||||
value_op_iterator &operator++() { return *this += 1; }
|
||||
value_op_iterator &operator--() { return *this -= 1; }
|
||||
value_op_iterator operator++(int) {
|
||||
value_op_iterator tmp = *this;
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
value_op_iterator operator--(int) {
|
||||
value_op_iterator tmp = *this;
|
||||
--*this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
Value *operator*() const { return *OI; }
|
||||
Value *operator->() const { return operator*(); }
|
||||
Value *operator[](ptrdiff_t n) const { return *(*this + n); }
|
||||
};
|
||||
|
||||
inline value_op_iterator value_op_begin() {
|
||||
|
@ -21,6 +21,7 @@ set(IRSources
|
||||
PatternMatch.cpp
|
||||
TypeBuilderTest.cpp
|
||||
TypesTest.cpp
|
||||
UserTest.cpp
|
||||
ValueHandleTest.cpp
|
||||
ValueMapTest.cpp
|
||||
ValueTest.cpp
|
||||
|
96
unittests/IR/UserTest.cpp
Normal file
96
unittests/IR/UserTest.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
//===- llvm/unittest/IR/UserTest.cpp - User unit tests --------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/AsmParser/Parser.h"
|
||||
#include "llvm/IR/Function.h"
|
||||
#include "llvm/IR/LLVMContext.h"
|
||||
#include "llvm/IR/Module.h"
|
||||
#include "llvm/IR/User.h"
|
||||
#include "llvm/IR/Instructions.h"
|
||||
#include "llvm/Support/SourceMgr.h"
|
||||
#include "gtest/gtest.h"
|
||||
using namespace llvm;
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(UserTest, ValueOpIteration) {
|
||||
LLVMContext C;
|
||||
|
||||
const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
|
||||
"entry:\n"
|
||||
" switch i32 undef, label %s0\n"
|
||||
" [ i32 1, label %s1\n"
|
||||
" i32 2, label %s2\n"
|
||||
" i32 3, label %s3\n"
|
||||
" i32 4, label %s4\n"
|
||||
" i32 5, label %s5\n"
|
||||
" i32 6, label %s6\n"
|
||||
" i32 7, label %s7\n"
|
||||
" i32 8, label %s8\n"
|
||||
" i32 9, label %s9 ]\n"
|
||||
"\n"
|
||||
"s0:\n"
|
||||
" br label %exit\n"
|
||||
"s1:\n"
|
||||
" br label %exit\n"
|
||||
"s2:\n"
|
||||
" br label %exit\n"
|
||||
"s3:\n"
|
||||
" br label %exit\n"
|
||||
"s4:\n"
|
||||
" br label %exit\n"
|
||||
"s5:\n"
|
||||
" br label %exit\n"
|
||||
"s6:\n"
|
||||
" br label %exit\n"
|
||||
"s7:\n"
|
||||
" br label %exit\n"
|
||||
"s8:\n"
|
||||
" br label %exit\n"
|
||||
"s9:\n"
|
||||
" br label %exit\n"
|
||||
"\n"
|
||||
"exit:\n"
|
||||
" %phi = phi i32 [ 0, %s0 ], [ 1, %s1 ],\n"
|
||||
" [ 2, %s2 ], [ 3, %s3 ],\n"
|
||||
" [ 4, %s4 ], [ 5, %s5 ],\n"
|
||||
" [ 6, %s6 ], [ 7, %s7 ],\n"
|
||||
" [ 8, %s8 ], [ 9, %s9 ]\n"
|
||||
" ret void\n"
|
||||
"}\n";
|
||||
SMDiagnostic Err;
|
||||
Module *M = ParseAssemblyString(ModuleString, NULL, Err, C);
|
||||
|
||||
Function *F = M->getFunction("f");
|
||||
BasicBlock &ExitBB = F->back();
|
||||
PHINode &P = cast<PHINode>(ExitBB.front());
|
||||
EXPECT_TRUE(P.value_op_begin() == P.value_op_begin());
|
||||
EXPECT_FALSE(P.value_op_begin() == P.value_op_end());
|
||||
EXPECT_TRUE(P.value_op_begin() != P.value_op_end());
|
||||
EXPECT_FALSE(P.value_op_end() != P.value_op_end());
|
||||
EXPECT_TRUE(P.value_op_begin() < P.value_op_end());
|
||||
EXPECT_FALSE(P.value_op_begin() < P.value_op_begin());
|
||||
EXPECT_TRUE(P.value_op_end() > P.value_op_begin());
|
||||
EXPECT_FALSE(P.value_op_begin() > P.value_op_begin());
|
||||
EXPECT_TRUE(P.value_op_begin() <= P.value_op_begin());
|
||||
EXPECT_FALSE(P.value_op_end() <= P.value_op_begin());
|
||||
EXPECT_TRUE(P.value_op_begin() >= P.value_op_begin());
|
||||
EXPECT_FALSE(P.value_op_begin() >= P.value_op_end());
|
||||
EXPECT_EQ(10, std::distance(P.value_op_begin(), P.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);
|
||||
}
|
||||
|
||||
} // end anonymous namespace
|
Loading…
Reference in New Issue
Block a user