2009-03-29 06:06:02 +00:00
|
|
|
//===- llvm/ADT/PointerUnion.h - Discriminated Union of 2 Ptrs --*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the PointerUnion class, which is a discriminated union of
|
|
|
|
// pointer types.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_POINTERUNION_H
|
|
|
|
#define LLVM_ADT_POINTERUNION_H
|
|
|
|
|
2014-04-15 07:08:40 +00:00
|
|
|
#include "llvm/ADT/DenseMapInfo.h"
|
2009-03-29 06:06:02 +00:00
|
|
|
#include "llvm/ADT/PointerIntPair.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-03-29 06:06:02 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2011-03-07 01:30:20 +00:00
|
|
|
template <typename T>
|
|
|
|
struct PointerUnionTypeSelectorReturn {
|
|
|
|
typedef T Return;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Get a type based on whether two types are the same or not. For:
|
|
|
|
/// @code
|
|
|
|
/// typedef typename PointerUnionTypeSelector<T1, T2, EQ, NE>::Return Ret;
|
|
|
|
/// @endcode
|
|
|
|
/// Ret will be EQ type if T1 is same as T2 or NE type otherwise.
|
|
|
|
template <typename T1, typename T2, typename RET_EQ, typename RET_NE>
|
|
|
|
struct PointerUnionTypeSelector {
|
|
|
|
typedef typename PointerUnionTypeSelectorReturn<RET_NE>::Return Return;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename RET_EQ, typename RET_NE>
|
|
|
|
struct PointerUnionTypeSelector<T, T, RET_EQ, RET_NE> {
|
|
|
|
typedef typename PointerUnionTypeSelectorReturn<RET_EQ>::Return Return;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T1, typename T2, typename RET_EQ, typename RET_NE>
|
|
|
|
struct PointerUnionTypeSelectorReturn<
|
|
|
|
PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE> > {
|
|
|
|
typedef typename PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE>::Return
|
|
|
|
Return;
|
|
|
|
};
|
|
|
|
|
2009-03-29 06:06:02 +00:00
|
|
|
/// Provide PointerLikeTypeTraits for void* that is used by PointerUnion
|
|
|
|
/// for the two template arguments.
|
|
|
|
template <typename PT1, typename PT2>
|
|
|
|
class PointerUnionUIntTraits {
|
|
|
|
public:
|
|
|
|
static inline void *getAsVoidPointer(void *P) { return P; }
|
|
|
|
static inline void *getFromVoidPointer(void *P) { return P; }
|
|
|
|
enum {
|
2012-07-13 21:06:54 +00:00
|
|
|
PT1BitsAv = (int)(PointerLikeTypeTraits<PT1>::NumLowBitsAvailable),
|
|
|
|
PT2BitsAv = (int)(PointerLikeTypeTraits<PT2>::NumLowBitsAvailable),
|
2009-03-29 06:06:02 +00:00
|
|
|
NumLowBitsAvailable = PT1BitsAv < PT2BitsAv ? PT1BitsAv : PT2BitsAv
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/// PointerUnion - This implements a discriminated union of two pointer types,
|
|
|
|
/// and keeps the discriminator bit-mangled into the low bits of the pointer.
|
|
|
|
/// This allows the implementation to be extremely efficient in space, but
|
|
|
|
/// permits a very natural and type-safe API.
|
|
|
|
///
|
|
|
|
/// Common use patterns would be something like this:
|
|
|
|
/// PointerUnion<int*, float*> P;
|
|
|
|
/// P = (int*)0;
|
|
|
|
/// printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0"
|
|
|
|
/// X = P.get<int*>(); // ok.
|
|
|
|
/// Y = P.get<float*>(); // runtime assertion failure.
|
2013-08-21 21:30:23 +00:00
|
|
|
/// Z = P.get<double*>(); // compile time failure.
|
2009-03-29 06:06:02 +00:00
|
|
|
/// P = (float*)0;
|
|
|
|
/// Y = P.get<float*>(); // ok.
|
|
|
|
/// X = P.get<int*>(); // runtime assertion failure.
|
|
|
|
template <typename PT1, typename PT2>
|
|
|
|
class PointerUnion {
|
|
|
|
public:
|
|
|
|
typedef PointerIntPair<void*, 1, bool,
|
|
|
|
PointerUnionUIntTraits<PT1,PT2> > ValTy;
|
|
|
|
private:
|
|
|
|
ValTy Val;
|
2011-03-07 05:35:01 +00:00
|
|
|
|
|
|
|
struct IsPT1 {
|
|
|
|
static const int Num = 0;
|
|
|
|
};
|
|
|
|
struct IsPT2 {
|
|
|
|
static const int Num = 1;
|
|
|
|
};
|
|
|
|
template <typename T>
|
|
|
|
struct UNION_DOESNT_CONTAIN_TYPE { };
|
|
|
|
|
2009-03-29 06:06:02 +00:00
|
|
|
public:
|
|
|
|
PointerUnion() {}
|
|
|
|
|
Eliminate redundant bitwise operations when using a llvm/ADT/PointerUnion.
For comparison, with this code sample:
PointerUnion<int *, char *> Data;
PointerUnion<int *, char *> foo1() {
Data = new int;
return new int;
}
PointerUnion<int *, char *> foo2() {
Data = new char;
return new char;
}
Before this patch we would get:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%.masked.i = and i64 %2, -3
%5 = or i64 %4, %.masked.i
store i64 %5, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%6 = tail call noalias i8* @_Znwm(i64 4)
%7 = ptrtoint i8* %6 to i64
%8 = and i64 %7, -3
ret i64 %8
}
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%5 = or i64 %2, %4
%6 = or i64 %5, 2
store i64 %6, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%7 = tail call noalias i8* @_Znwm(i64 1)
%8 = ptrtoint i8* %7 to i64
%9 = or i64 %8, 2
ret i64 %9
}
After the patch:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
store i64 %2, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%3 = tail call noalias i8* @_Znwm(i64 4)
%4 = ptrtoint i8* %3 to i64
ret i64 %4
}
declare noalias i8* @_Znwm(i64)
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = or i64 %2, 2
store i64 %3, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = tail call noalias i8* @_Znwm(i64 1)
%5 = ptrtoint i8* %4 to i64
%6 = or i64 %5, 2
ret i64 %6
}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169147 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-03 19:59:23 +00:00
|
|
|
PointerUnion(PT1 V) : Val(
|
|
|
|
const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(V))) {
|
2009-03-29 06:06:02 +00:00
|
|
|
}
|
Eliminate redundant bitwise operations when using a llvm/ADT/PointerUnion.
For comparison, with this code sample:
PointerUnion<int *, char *> Data;
PointerUnion<int *, char *> foo1() {
Data = new int;
return new int;
}
PointerUnion<int *, char *> foo2() {
Data = new char;
return new char;
}
Before this patch we would get:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%.masked.i = and i64 %2, -3
%5 = or i64 %4, %.masked.i
store i64 %5, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%6 = tail call noalias i8* @_Znwm(i64 4)
%7 = ptrtoint i8* %6 to i64
%8 = and i64 %7, -3
ret i64 %8
}
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%5 = or i64 %2, %4
%6 = or i64 %5, 2
store i64 %6, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%7 = tail call noalias i8* @_Znwm(i64 1)
%8 = ptrtoint i8* %7 to i64
%9 = or i64 %8, 2
ret i64 %9
}
After the patch:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
store i64 %2, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%3 = tail call noalias i8* @_Znwm(i64 4)
%4 = ptrtoint i8* %3 to i64
ret i64 %4
}
declare noalias i8* @_Znwm(i64)
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = or i64 %2, 2
store i64 %3, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = tail call noalias i8* @_Znwm(i64 1)
%5 = ptrtoint i8* %4 to i64
%6 = or i64 %5, 2
ret i64 %6
}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169147 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-03 19:59:23 +00:00
|
|
|
PointerUnion(PT2 V) : Val(
|
|
|
|
const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V)), 1) {
|
2009-03-29 06:06:02 +00:00
|
|
|
}
|
|
|
|
|
2009-07-29 18:27:22 +00:00
|
|
|
/// isNull - Return true if the pointer held in the union is null,
|
2009-03-29 07:03:30 +00:00
|
|
|
/// regardless of which type it is.
|
2011-08-12 04:31:38 +00:00
|
|
|
bool isNull() const {
|
|
|
|
// Convert from the void* to one of the pointer types, to make sure that
|
|
|
|
// we recursively strip off low bits if we have a nested PointerUnion.
|
|
|
|
return !PointerLikeTypeTraits<PT1>::getFromVoidPointer(Val.getPointer());
|
|
|
|
}
|
2013-05-15 07:36:59 +00:00
|
|
|
LLVM_EXPLICIT operator bool() const { return !isNull(); }
|
2009-03-30 20:29:27 +00:00
|
|
|
|
2009-03-29 07:03:30 +00:00
|
|
|
/// is<T>() return true if the Union currently holds the type matching T.
|
2009-03-29 06:06:02 +00:00
|
|
|
template<typename T>
|
|
|
|
int is() const {
|
2011-03-07 05:35:01 +00:00
|
|
|
typedef typename
|
|
|
|
::llvm::PointerUnionTypeSelector<PT1, T, IsPT1,
|
|
|
|
::llvm::PointerUnionTypeSelector<PT2, T, IsPT2,
|
|
|
|
UNION_DOESNT_CONTAIN_TYPE<T> > >::Return Ty;
|
|
|
|
int TyNo = Ty::Num;
|
2009-06-29 17:12:06 +00:00
|
|
|
return static_cast<int>(Val.getInt()) == TyNo;
|
2009-03-29 06:06:02 +00:00
|
|
|
}
|
2009-03-29 07:03:30 +00:00
|
|
|
|
|
|
|
/// get<T>() - Return the value of the specified pointer type. If the
|
|
|
|
/// specified pointer type is incorrect, assert.
|
2009-03-29 06:06:02 +00:00
|
|
|
template<typename T>
|
|
|
|
T get() const {
|
|
|
|
assert(is<T>() && "Invalid accessor called");
|
2009-03-30 20:29:27 +00:00
|
|
|
return PointerLikeTypeTraits<T>::getFromVoidPointer(Val.getPointer());
|
2009-03-29 06:06:02 +00:00
|
|
|
}
|
|
|
|
|
2009-03-29 07:03:30 +00:00
|
|
|
/// dyn_cast<T>() - If the current value is of the specified pointer type,
|
|
|
|
/// return it, otherwise return null.
|
|
|
|
template<typename T>
|
|
|
|
T dyn_cast() const {
|
2009-03-30 20:29:27 +00:00
|
|
|
if (is<T>()) return get<T>();
|
2009-03-29 07:03:30 +00:00
|
|
|
return T();
|
|
|
|
}
|
2011-02-19 03:55:58 +00:00
|
|
|
|
2012-03-06 07:14:54 +00:00
|
|
|
/// \brief If the union is set to the first pointer type get an address
|
|
|
|
/// pointing to it.
|
|
|
|
PT1 const *getAddrOfPtr1() const {
|
|
|
|
return const_cast<PointerUnion *>(this)->getAddrOfPtr1();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief If the union is set to the first pointer type get an address
|
|
|
|
/// pointing to it.
|
|
|
|
PT1 *getAddrOfPtr1() {
|
2011-02-19 03:55:58 +00:00
|
|
|
assert(is<PT1>() && "Val is not the first pointer");
|
|
|
|
assert(get<PT1>() == Val.getPointer() &&
|
|
|
|
"Can't get the address because PointerLikeTypeTraits changes the ptr");
|
2012-03-06 07:14:54 +00:00
|
|
|
return (PT1 *)Val.getAddrOfPointer();
|
2011-02-19 03:55:58 +00:00
|
|
|
}
|
2014-04-29 00:14:27 +00:00
|
|
|
|
|
|
|
/// \brief Assignment from nullptr which just clears the union.
|
|
|
|
const PointerUnion &operator=(std::nullptr_t) {
|
|
|
|
Val.initWithPointer(nullptr);
|
|
|
|
return *this;
|
|
|
|
}
|
2009-03-29 07:03:30 +00:00
|
|
|
|
|
|
|
/// Assignment operators - Allow assigning into this union from either
|
|
|
|
/// pointer type, setting the discriminator to remember what it came from.
|
2009-03-29 06:06:02 +00:00
|
|
|
const PointerUnion &operator=(const PT1 &RHS) {
|
Eliminate redundant bitwise operations when using a llvm/ADT/PointerUnion.
For comparison, with this code sample:
PointerUnion<int *, char *> Data;
PointerUnion<int *, char *> foo1() {
Data = new int;
return new int;
}
PointerUnion<int *, char *> foo2() {
Data = new char;
return new char;
}
Before this patch we would get:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%.masked.i = and i64 %2, -3
%5 = or i64 %4, %.masked.i
store i64 %5, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%6 = tail call noalias i8* @_Znwm(i64 4)
%7 = ptrtoint i8* %6 to i64
%8 = and i64 %7, -3
ret i64 %8
}
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%5 = or i64 %2, %4
%6 = or i64 %5, 2
store i64 %6, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%7 = tail call noalias i8* @_Znwm(i64 1)
%8 = ptrtoint i8* %7 to i64
%9 = or i64 %8, 2
ret i64 %9
}
After the patch:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
store i64 %2, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%3 = tail call noalias i8* @_Znwm(i64 4)
%4 = ptrtoint i8* %3 to i64
ret i64 %4
}
declare noalias i8* @_Znwm(i64)
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = or i64 %2, 2
store i64 %3, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = tail call noalias i8* @_Znwm(i64 1)
%5 = ptrtoint i8* %4 to i64
%6 = or i64 %5, 2
ret i64 %6
}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169147 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-03 19:59:23 +00:00
|
|
|
Val.initWithPointer(
|
2009-03-31 23:19:54 +00:00
|
|
|
const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(RHS)));
|
2009-03-29 06:06:02 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const PointerUnion &operator=(const PT2 &RHS) {
|
Eliminate redundant bitwise operations when using a llvm/ADT/PointerUnion.
For comparison, with this code sample:
PointerUnion<int *, char *> Data;
PointerUnion<int *, char *> foo1() {
Data = new int;
return new int;
}
PointerUnion<int *, char *> foo2() {
Data = new char;
return new char;
}
Before this patch we would get:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%.masked.i = and i64 %2, -3
%5 = or i64 %4, %.masked.i
store i64 %5, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%6 = tail call noalias i8* @_Znwm(i64 4)
%7 = ptrtoint i8* %6 to i64
%8 = and i64 %7, -3
ret i64 %8
}
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%5 = or i64 %2, %4
%6 = or i64 %5, 2
store i64 %6, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%7 = tail call noalias i8* @_Znwm(i64 1)
%8 = ptrtoint i8* %7 to i64
%9 = or i64 %8, 2
ret i64 %9
}
After the patch:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
store i64 %2, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%3 = tail call noalias i8* @_Znwm(i64 4)
%4 = ptrtoint i8* %3 to i64
ret i64 %4
}
declare noalias i8* @_Znwm(i64)
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = or i64 %2, 2
store i64 %3, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = tail call noalias i8* @_Znwm(i64 1)
%5 = ptrtoint i8* %4 to i64
%6 = or i64 %5, 2
ret i64 %6
}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169147 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-03 19:59:23 +00:00
|
|
|
Val.setPointerAndInt(
|
|
|
|
const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(RHS)),
|
|
|
|
1);
|
2009-03-29 06:06:02 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *getOpaqueValue() const { return Val.getOpaqueValue(); }
|
2010-03-30 16:20:03 +00:00
|
|
|
static inline PointerUnion getFromOpaqueValue(void *VP) {
|
2009-03-29 06:06:02 +00:00
|
|
|
PointerUnion V;
|
|
|
|
V.Val = ValTy::getFromOpaqueValue(VP);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
};
|
2013-05-15 07:36:59 +00:00
|
|
|
|
|
|
|
template<typename PT1, typename PT2>
|
2013-08-16 22:09:02 +00:00
|
|
|
static bool operator==(PointerUnion<PT1, PT2> lhs,
|
|
|
|
PointerUnion<PT1, PT2> rhs) {
|
2013-05-15 07:36:59 +00:00
|
|
|
return lhs.getOpaqueValue() == rhs.getOpaqueValue();
|
|
|
|
}
|
2013-08-16 22:09:02 +00:00
|
|
|
|
|
|
|
template<typename PT1, typename PT2>
|
|
|
|
static bool operator!=(PointerUnion<PT1, PT2> lhs,
|
|
|
|
PointerUnion<PT1, PT2> rhs) {
|
2013-08-16 22:29:44 +00:00
|
|
|
return lhs.getOpaqueValue() != rhs.getOpaqueValue();
|
2013-08-16 22:09:02 +00:00
|
|
|
}
|
|
|
|
|
2009-03-30 20:29:27 +00:00
|
|
|
// Teach SmallPtrSet that PointerUnion is "basically a pointer", that has
|
2009-03-29 06:06:02 +00:00
|
|
|
// # low bits available = min(PT1bits,PT2bits)-1.
|
|
|
|
template<typename PT1, typename PT2>
|
|
|
|
class PointerLikeTypeTraits<PointerUnion<PT1, PT2> > {
|
|
|
|
public:
|
|
|
|
static inline void *
|
|
|
|
getAsVoidPointer(const PointerUnion<PT1, PT2> &P) {
|
|
|
|
return P.getOpaqueValue();
|
|
|
|
}
|
|
|
|
static inline PointerUnion<PT1, PT2>
|
|
|
|
getFromVoidPointer(void *P) {
|
|
|
|
return PointerUnion<PT1, PT2>::getFromOpaqueValue(P);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The number of bits available are the min of the two pointer types.
|
|
|
|
enum {
|
|
|
|
NumLowBitsAvailable =
|
2009-03-30 20:29:27 +00:00
|
|
|
PointerLikeTypeTraits<typename PointerUnion<PT1,PT2>::ValTy>
|
|
|
|
::NumLowBitsAvailable
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// PointerUnion3 - This is a pointer union of three pointer types. See
|
|
|
|
/// documentation for PointerUnion for usage.
|
|
|
|
template <typename PT1, typename PT2, typename PT3>
|
|
|
|
class PointerUnion3 {
|
|
|
|
public:
|
|
|
|
typedef PointerUnion<PT1, PT2> InnerUnion;
|
|
|
|
typedef PointerUnion<InnerUnion, PT3> ValTy;
|
|
|
|
private:
|
|
|
|
ValTy Val;
|
2011-03-07 01:30:20 +00:00
|
|
|
|
|
|
|
struct IsInnerUnion {
|
|
|
|
ValTy Val;
|
|
|
|
IsInnerUnion(ValTy val) : Val(val) { }
|
|
|
|
template<typename T>
|
|
|
|
int is() const {
|
|
|
|
return Val.template is<InnerUnion>() &&
|
|
|
|
Val.template get<InnerUnion>().template is<T>();
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
T get() const {
|
|
|
|
return Val.template get<InnerUnion>().template get<T>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IsPT3 {
|
|
|
|
ValTy Val;
|
|
|
|
IsPT3(ValTy val) : Val(val) { }
|
|
|
|
template<typename T>
|
|
|
|
int is() const {
|
|
|
|
return Val.template is<T>();
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
T get() const {
|
|
|
|
return Val.template get<T>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-03-30 20:29:27 +00:00
|
|
|
public:
|
|
|
|
PointerUnion3() {}
|
|
|
|
|
|
|
|
PointerUnion3(PT1 V) {
|
|
|
|
Val = InnerUnion(V);
|
|
|
|
}
|
|
|
|
PointerUnion3(PT2 V) {
|
|
|
|
Val = InnerUnion(V);
|
|
|
|
}
|
|
|
|
PointerUnion3(PT3 V) {
|
|
|
|
Val = V;
|
|
|
|
}
|
|
|
|
|
2009-07-29 18:27:22 +00:00
|
|
|
/// isNull - Return true if the pointer held in the union is null,
|
2009-03-30 20:29:27 +00:00
|
|
|
/// regardless of which type it is.
|
|
|
|
bool isNull() const { return Val.isNull(); }
|
2013-05-15 07:36:59 +00:00
|
|
|
LLVM_EXPLICIT operator bool() const { return !isNull(); }
|
2009-03-30 20:29:27 +00:00
|
|
|
|
|
|
|
/// is<T>() return true if the Union currently holds the type matching T.
|
|
|
|
template<typename T>
|
|
|
|
int is() const {
|
2011-03-07 01:30:20 +00:00
|
|
|
// If T is PT1/PT2 choose IsInnerUnion otherwise choose IsPT3.
|
|
|
|
typedef typename
|
|
|
|
::llvm::PointerUnionTypeSelector<PT1, T, IsInnerUnion,
|
|
|
|
::llvm::PointerUnionTypeSelector<PT2, T, IsInnerUnion, IsPT3 >
|
|
|
|
>::Return Ty;
|
2012-03-11 02:22:41 +00:00
|
|
|
return Ty(Val).template is<T>();
|
2009-03-30 20:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// get<T>() - Return the value of the specified pointer type. If the
|
|
|
|
/// specified pointer type is incorrect, assert.
|
|
|
|
template<typename T>
|
|
|
|
T get() const {
|
|
|
|
assert(is<T>() && "Invalid accessor called");
|
2011-03-07 01:30:20 +00:00
|
|
|
// If T is PT1/PT2 choose IsInnerUnion otherwise choose IsPT3.
|
|
|
|
typedef typename
|
|
|
|
::llvm::PointerUnionTypeSelector<PT1, T, IsInnerUnion,
|
|
|
|
::llvm::PointerUnionTypeSelector<PT2, T, IsInnerUnion, IsPT3 >
|
|
|
|
>::Return Ty;
|
2012-03-11 02:22:41 +00:00
|
|
|
return Ty(Val).template get<T>();
|
2009-03-30 20:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// dyn_cast<T>() - If the current value is of the specified pointer type,
|
|
|
|
/// return it, otherwise return null.
|
|
|
|
template<typename T>
|
|
|
|
T dyn_cast() const {
|
|
|
|
if (is<T>()) return get<T>();
|
|
|
|
return T();
|
|
|
|
}
|
2014-04-29 00:14:27 +00:00
|
|
|
|
|
|
|
/// \brief Assignment from nullptr which just clears the union.
|
|
|
|
const PointerUnion3 &operator=(std::nullptr_t) {
|
|
|
|
Val = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
2009-03-30 20:29:27 +00:00
|
|
|
|
|
|
|
/// Assignment operators - Allow assigning into this union from either
|
|
|
|
/// pointer type, setting the discriminator to remember what it came from.
|
|
|
|
const PointerUnion3 &operator=(const PT1 &RHS) {
|
|
|
|
Val = InnerUnion(RHS);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const PointerUnion3 &operator=(const PT2 &RHS) {
|
|
|
|
Val = InnerUnion(RHS);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const PointerUnion3 &operator=(const PT3 &RHS) {
|
|
|
|
Val = RHS;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *getOpaqueValue() const { return Val.getOpaqueValue(); }
|
2010-03-30 16:20:03 +00:00
|
|
|
static inline PointerUnion3 getFromOpaqueValue(void *VP) {
|
2009-03-30 20:29:27 +00:00
|
|
|
PointerUnion3 V;
|
|
|
|
V.Val = ValTy::getFromOpaqueValue(VP);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Teach SmallPtrSet that PointerUnion3 is "basically a pointer", that has
|
|
|
|
// # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
|
|
|
|
template<typename PT1, typename PT2, typename PT3>
|
|
|
|
class PointerLikeTypeTraits<PointerUnion3<PT1, PT2, PT3> > {
|
|
|
|
public:
|
|
|
|
static inline void *
|
|
|
|
getAsVoidPointer(const PointerUnion3<PT1, PT2, PT3> &P) {
|
|
|
|
return P.getOpaqueValue();
|
|
|
|
}
|
|
|
|
static inline PointerUnion3<PT1, PT2, PT3>
|
|
|
|
getFromVoidPointer(void *P) {
|
|
|
|
return PointerUnion3<PT1, PT2, PT3>::getFromOpaqueValue(P);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The number of bits available are the min of the two pointer types.
|
|
|
|
enum {
|
|
|
|
NumLowBitsAvailable =
|
|
|
|
PointerLikeTypeTraits<typename PointerUnion3<PT1, PT2, PT3>::ValTy>
|
|
|
|
::NumLowBitsAvailable
|
2009-03-29 06:06:02 +00:00
|
|
|
};
|
|
|
|
};
|
2009-07-29 18:19:47 +00:00
|
|
|
|
|
|
|
/// PointerUnion4 - This is a pointer union of four pointer types. See
|
|
|
|
/// documentation for PointerUnion for usage.
|
|
|
|
template <typename PT1, typename PT2, typename PT3, typename PT4>
|
|
|
|
class PointerUnion4 {
|
|
|
|
public:
|
|
|
|
typedef PointerUnion<PT1, PT2> InnerUnion1;
|
|
|
|
typedef PointerUnion<PT3, PT4> InnerUnion2;
|
|
|
|
typedef PointerUnion<InnerUnion1, InnerUnion2> ValTy;
|
|
|
|
private:
|
|
|
|
ValTy Val;
|
|
|
|
public:
|
|
|
|
PointerUnion4() {}
|
|
|
|
|
|
|
|
PointerUnion4(PT1 V) {
|
|
|
|
Val = InnerUnion1(V);
|
|
|
|
}
|
|
|
|
PointerUnion4(PT2 V) {
|
|
|
|
Val = InnerUnion1(V);
|
|
|
|
}
|
|
|
|
PointerUnion4(PT3 V) {
|
|
|
|
Val = InnerUnion2(V);
|
|
|
|
}
|
|
|
|
PointerUnion4(PT4 V) {
|
|
|
|
Val = InnerUnion2(V);
|
|
|
|
}
|
|
|
|
|
2009-07-29 18:27:22 +00:00
|
|
|
/// isNull - Return true if the pointer held in the union is null,
|
2009-07-29 18:19:47 +00:00
|
|
|
/// regardless of which type it is.
|
|
|
|
bool isNull() const { return Val.isNull(); }
|
2013-05-15 07:36:59 +00:00
|
|
|
LLVM_EXPLICIT operator bool() const { return !isNull(); }
|
2009-07-29 18:19:47 +00:00
|
|
|
|
|
|
|
/// is<T>() return true if the Union currently holds the type matching T.
|
|
|
|
template<typename T>
|
|
|
|
int is() const {
|
2011-03-07 01:30:20 +00:00
|
|
|
// If T is PT1/PT2 choose InnerUnion1 otherwise choose InnerUnion2.
|
|
|
|
typedef typename
|
|
|
|
::llvm::PointerUnionTypeSelector<PT1, T, InnerUnion1,
|
|
|
|
::llvm::PointerUnionTypeSelector<PT2, T, InnerUnion1, InnerUnion2 >
|
|
|
|
>::Return Ty;
|
|
|
|
return Val.template is<Ty>() &&
|
|
|
|
Val.template get<Ty>().template is<T>();
|
2009-07-29 18:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// get<T>() - Return the value of the specified pointer type. If the
|
|
|
|
/// specified pointer type is incorrect, assert.
|
|
|
|
template<typename T>
|
|
|
|
T get() const {
|
|
|
|
assert(is<T>() && "Invalid accessor called");
|
2011-03-07 01:30:20 +00:00
|
|
|
// If T is PT1/PT2 choose InnerUnion1 otherwise choose InnerUnion2.
|
|
|
|
typedef typename
|
|
|
|
::llvm::PointerUnionTypeSelector<PT1, T, InnerUnion1,
|
|
|
|
::llvm::PointerUnionTypeSelector<PT2, T, InnerUnion1, InnerUnion2 >
|
|
|
|
>::Return Ty;
|
|
|
|
return Val.template get<Ty>().template get<T>();
|
2009-07-29 18:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// dyn_cast<T>() - If the current value is of the specified pointer type,
|
|
|
|
/// return it, otherwise return null.
|
|
|
|
template<typename T>
|
|
|
|
T dyn_cast() const {
|
|
|
|
if (is<T>()) return get<T>();
|
|
|
|
return T();
|
|
|
|
}
|
2014-04-29 00:14:27 +00:00
|
|
|
|
|
|
|
/// \brief Assignment from nullptr which just clears the union.
|
|
|
|
const PointerUnion4 &operator=(std::nullptr_t) {
|
|
|
|
Val = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
2009-07-29 18:19:47 +00:00
|
|
|
|
|
|
|
/// Assignment operators - Allow assigning into this union from either
|
|
|
|
/// pointer type, setting the discriminator to remember what it came from.
|
|
|
|
const PointerUnion4 &operator=(const PT1 &RHS) {
|
|
|
|
Val = InnerUnion1(RHS);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const PointerUnion4 &operator=(const PT2 &RHS) {
|
|
|
|
Val = InnerUnion1(RHS);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const PointerUnion4 &operator=(const PT3 &RHS) {
|
|
|
|
Val = InnerUnion2(RHS);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const PointerUnion4 &operator=(const PT4 &RHS) {
|
|
|
|
Val = InnerUnion2(RHS);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *getOpaqueValue() const { return Val.getOpaqueValue(); }
|
2010-03-30 16:20:03 +00:00
|
|
|
static inline PointerUnion4 getFromOpaqueValue(void *VP) {
|
2009-07-29 18:19:47 +00:00
|
|
|
PointerUnion4 V;
|
|
|
|
V.Val = ValTy::getFromOpaqueValue(VP);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Teach SmallPtrSet that PointerUnion4 is "basically a pointer", that has
|
|
|
|
// # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
|
|
|
|
template<typename PT1, typename PT2, typename PT3, typename PT4>
|
|
|
|
class PointerLikeTypeTraits<PointerUnion4<PT1, PT2, PT3, PT4> > {
|
|
|
|
public:
|
|
|
|
static inline void *
|
|
|
|
getAsVoidPointer(const PointerUnion4<PT1, PT2, PT3, PT4> &P) {
|
|
|
|
return P.getOpaqueValue();
|
|
|
|
}
|
|
|
|
static inline PointerUnion4<PT1, PT2, PT3, PT4>
|
|
|
|
getFromVoidPointer(void *P) {
|
|
|
|
return PointerUnion4<PT1, PT2, PT3, PT4>::getFromOpaqueValue(P);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The number of bits available are the min of the two pointer types.
|
|
|
|
enum {
|
|
|
|
NumLowBitsAvailable =
|
|
|
|
PointerLikeTypeTraits<typename PointerUnion4<PT1, PT2, PT3, PT4>::ValTy>
|
|
|
|
::NumLowBitsAvailable
|
|
|
|
};
|
|
|
|
};
|
2014-04-15 07:08:40 +00:00
|
|
|
|
|
|
|
// Teach DenseMap how to use PointerUnions as keys.
|
|
|
|
template<typename T, typename U>
|
|
|
|
struct DenseMapInfo<PointerUnion<T, U> > {
|
|
|
|
typedef PointerUnion<T, U> Pair;
|
|
|
|
typedef DenseMapInfo<T> FirstInfo;
|
|
|
|
typedef DenseMapInfo<U> SecondInfo;
|
|
|
|
|
|
|
|
static inline Pair getEmptyKey() {
|
|
|
|
return Pair(FirstInfo::getEmptyKey());
|
|
|
|
}
|
|
|
|
static inline Pair getTombstoneKey() {
|
|
|
|
return Pair(FirstInfo::getTombstoneKey());
|
|
|
|
}
|
|
|
|
static unsigned getHashValue(const Pair &PairVal) {
|
|
|
|
intptr_t key = (intptr_t)PairVal.getOpaqueValue();
|
|
|
|
return DenseMapInfo<intptr_t>::getHashValue(key);
|
|
|
|
}
|
|
|
|
static bool isEqual(const Pair &LHS, const Pair &RHS) {
|
|
|
|
return LHS.template is<T>() == RHS.template is<T>() &&
|
|
|
|
(LHS.template is<T>() ?
|
|
|
|
FirstInfo::isEqual(LHS.template get<T>(),
|
|
|
|
RHS.template get<T>()) :
|
|
|
|
SecondInfo::isEqual(LHS.template get<U>(),
|
|
|
|
RHS.template get<U>()));
|
|
|
|
}
|
|
|
|
};
|
2009-03-29 06:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|