LCOV - code coverage report
Current view: top level - include/llvm/ADT - PointerUnion.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 21 21 100.0 %
Date: 2018-02-23 15:42:53 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- llvm/ADT/PointerUnion.h - Discriminated Union of 2 Ptrs --*- C++ -*-===//
       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             : // This file defines the PointerUnion class, which is a discriminated union of
      11             : // pointer types.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #ifndef LLVM_ADT_POINTERUNION_H
      16             : #define LLVM_ADT_POINTERUNION_H
      17             : 
      18             : #include "llvm/ADT/DenseMapInfo.h"
      19             : #include "llvm/ADT/PointerIntPair.h"
      20             : #include "llvm/Support/PointerLikeTypeTraits.h"
      21             : #include <cassert>
      22             : #include <cstddef>
      23             : #include <cstdint>
      24             : 
      25             : namespace llvm {
      26             : 
      27             : template <typename T> struct PointerUnionTypeSelectorReturn {
      28             :   using Return = T;
      29             : };
      30             : 
      31             : /// Get a type based on whether two types are the same or not.
      32             : ///
      33             : /// For:
      34             : ///
      35             : /// \code
      36             : ///   using Ret = typename PointerUnionTypeSelector<T1, T2, EQ, NE>::Return;
      37             : /// \endcode
      38             : ///
      39             : /// Ret will be EQ type if T1 is same as T2 or NE type otherwise.
      40             : template <typename T1, typename T2, typename RET_EQ, typename RET_NE>
      41             : struct PointerUnionTypeSelector {
      42             :   using Return = typename PointerUnionTypeSelectorReturn<RET_NE>::Return;
      43             : };
      44             : 
      45             : template <typename T, typename RET_EQ, typename RET_NE>
      46             : struct PointerUnionTypeSelector<T, T, RET_EQ, RET_NE> {
      47             :   using Return = typename PointerUnionTypeSelectorReturn<RET_EQ>::Return;
      48             : };
      49             : 
      50             : template <typename T1, typename T2, typename RET_EQ, typename RET_NE>
      51             : struct PointerUnionTypeSelectorReturn<
      52             :     PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE>> {
      53             :   using Return =
      54             :       typename PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE>::Return;
      55             : };
      56             : 
      57             : /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion
      58             : /// for the two template arguments.
      59             : template <typename PT1, typename PT2> class PointerUnionUIntTraits {
      60             : public:
      61             :   static inline void *getAsVoidPointer(void *P) { return P; }
      62             :   static inline void *getFromVoidPointer(void *P) { return P; }
      63             : 
      64             :   enum {
      65             :     PT1BitsAv = (int)(PointerLikeTypeTraits<PT1>::NumLowBitsAvailable),
      66             :     PT2BitsAv = (int)(PointerLikeTypeTraits<PT2>::NumLowBitsAvailable),
      67             :     NumLowBitsAvailable = PT1BitsAv < PT2BitsAv ? PT1BitsAv : PT2BitsAv
      68             :   };
      69             : };
      70             : 
      71             : /// A discriminated union of two pointer types, with the discriminator in the
      72             : /// low bit of the pointer.
      73             : ///
      74             : /// This implementation is extremely efficient in space due to leveraging the
      75             : /// low bits of the pointer, while exposing a natural and type-safe API.
      76             : ///
      77             : /// Common use patterns would be something like this:
      78             : ///    PointerUnion<int*, float*> P;
      79             : ///    P = (int*)0;
      80             : ///    printf("%d %d", P.is<int*>(), P.is<float*>());  // prints "1 0"
      81             : ///    X = P.get<int*>();     // ok.
      82             : ///    Y = P.get<float*>();   // runtime assertion failure.
      83             : ///    Z = P.get<double*>();  // compile time failure.
      84             : ///    P = (float*)0;
      85             : ///    Y = P.get<float*>();   // ok.
      86             : ///    X = P.get<int*>();     // runtime assertion failure.
      87             : template <typename PT1, typename PT2> class PointerUnion {
      88             : public:
      89             :   using ValTy =
      90             :       PointerIntPair<void *, 1, bool, PointerUnionUIntTraits<PT1, PT2>>;
      91             : 
      92             : private:
      93             :   ValTy Val;
      94             : 
      95             :   struct IsPT1 {
      96             :     static const int Num = 0;
      97             :   };
      98             :   struct IsPT2 {
      99             :     static const int Num = 1;
     100             :   };
     101             :   template <typename T> struct UNION_DOESNT_CONTAIN_TYPE {};
     102             : 
     103             : public:
     104             :   PointerUnion() = default;
     105             :   PointerUnion(PT1 V)
     106             :       : Val(const_cast<void *>(
     107             :             PointerLikeTypeTraits<PT1>::getAsVoidPointer(V))) {}
     108             :   PointerUnion(PT2 V)
     109             :       : Val(const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V)),
     110             :             1) {}
     111             : 
     112             :   /// Test if the pointer held in the union is null, regardless of
     113             :   /// which type it is.
     114             :   bool isNull() const {
     115             :     // Convert from the void* to one of the pointer types, to make sure that
     116             :     // we recursively strip off low bits if we have a nested PointerUnion.
     117     1180240 :     return !PointerLikeTypeTraits<PT1>::getFromVoidPointer(Val.getPointer());
     118             :   }
     119             : 
     120           7 :   explicit operator bool() const { return !isNull(); }
     121             : 
     122             :   /// Test if the Union currently holds the type matching T.
     123             :   template <typename T> int is() const {
     124             :     using Ty = typename ::llvm::PointerUnionTypeSelector<
     125             :         PT1, T, IsPT1,
     126             :         ::llvm::PointerUnionTypeSelector<PT2, T, IsPT2,
     127             :                                          UNION_DOESNT_CONTAIN_TYPE<T>>>::Return;
     128             :     int TyNo = Ty::Num;
     129    23808932 :     return static_cast<int>(Val.getInt()) == TyNo;
     130             :   }
     131             : 
     132             :   /// Returns the value of the specified pointer type.
     133             :   ///
     134             :   /// If the specified pointer type is incorrect, assert.
     135             :   template <typename T> T get() const {
     136             :     assert(is<T>() && "Invalid accessor called");
     137             :     return PointerLikeTypeTraits<T>::getFromVoidPointer(Val.getPointer());
     138             :   }
     139             : 
     140             :   /// Returns the current pointer if it is of the specified pointer type,
     141             :   /// otherwises returns null.
     142             :   template <typename T> T dyn_cast() const {
     143   456814537 :     if (is<T>())
     144             :       return get<T>();
     145             :     return T();
     146             :   }
     147             : 
     148             :   /// If the union is set to the first pointer type get an address pointing to
     149             :   /// it.
     150             :   PT1 const *getAddrOfPtr1() const {
     151             :     return const_cast<PointerUnion *>(this)->getAddrOfPtr1();
     152             :   }
     153             : 
     154             :   /// If the union is set to the first pointer type get an address pointing to
     155             :   /// it.
     156             :   PT1 *getAddrOfPtr1() {
     157             :     assert(is<PT1>() && "Val is not the first pointer");
     158             :     assert(
     159             :         get<PT1>() == Val.getPointer() &&
     160             :         "Can't get the address because PointerLikeTypeTraits changes the ptr");
     161             :     return const_cast<PT1 *>(
     162             :         reinterpret_cast<const PT1 *>(Val.getAddrOfPointer()));
     163             :   }
     164             : 
     165             :   /// Assignment from nullptr which just clears the union.
     166             :   const PointerUnion &operator=(std::nullptr_t) {
     167             :     Val.initWithPointer(nullptr);
     168             :     return *this;
     169             :   }
     170             : 
     171             :   /// Assignment operators - Allow assigning into this union from either
     172             :   /// pointer type, setting the discriminator to remember what it came from.
     173             :   const PointerUnion &operator=(const PT1 &RHS) {
     174             :     Val.initWithPointer(
     175             :         const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(RHS)));
     176             :     return *this;
     177             :   }
     178             :   const PointerUnion &operator=(const PT2 &RHS) {
     179             :     Val.setPointerAndInt(
     180             :         const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(RHS)),
     181             :         1);
     182             :     return *this;
     183             :   }
     184             : 
     185             :   void *getOpaqueValue() const { return Val.getOpaqueValue(); }
     186             :   static inline PointerUnion getFromOpaqueValue(void *VP) {
     187             :     PointerUnion V;
     188             :     V.Val = ValTy::getFromOpaqueValue(VP);
     189             :     return V;
     190             :   }
     191             : };
     192             : 
     193             : template <typename PT1, typename PT2>
     194             : bool operator==(PointerUnion<PT1, PT2> lhs, PointerUnion<PT1, PT2> rhs) {
     195           3 :   return lhs.getOpaqueValue() == rhs.getOpaqueValue();
     196             : }
     197             : 
     198             : template <typename PT1, typename PT2>
     199             : bool operator!=(PointerUnion<PT1, PT2> lhs, PointerUnion<PT1, PT2> rhs) {
     200           2 :   return lhs.getOpaqueValue() != rhs.getOpaqueValue();
     201             : }
     202             : 
     203             : template <typename PT1, typename PT2>
     204             : bool operator<(PointerUnion<PT1, PT2> lhs, PointerUnion<PT1, PT2> rhs) {
     205         743 :   return lhs.getOpaqueValue() < rhs.getOpaqueValue();
     206             : }
     207             : 
     208             : // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has
     209             : // # low bits available = min(PT1bits,PT2bits)-1.
     210             : template <typename PT1, typename PT2>
     211             : struct PointerLikeTypeTraits<PointerUnion<PT1, PT2>> {
     212             :   static inline void *getAsVoidPointer(const PointerUnion<PT1, PT2> &P) {
     213             :     return P.getOpaqueValue();
     214             :   }
     215             : 
     216             :   static inline PointerUnion<PT1, PT2> getFromVoidPointer(void *P) {
     217             :     return PointerUnion<PT1, PT2>::getFromOpaqueValue(P);
     218             :   }
     219             : 
     220             :   // The number of bits available are the min of the two pointer types.
     221             :   enum {
     222             :     NumLowBitsAvailable = PointerLikeTypeTraits<
     223             :         typename PointerUnion<PT1, PT2>::ValTy>::NumLowBitsAvailable
     224             :   };
     225             : };
     226             : 
     227             : /// A pointer union of three pointer types. See documentation for PointerUnion
     228             : /// for usage.
     229             : template <typename PT1, typename PT2, typename PT3> class PointerUnion3 {
     230             : public:
     231             :   using InnerUnion = PointerUnion<PT1, PT2>;
     232             :   using ValTy = PointerUnion<InnerUnion, PT3>;
     233             : 
     234             : private:
     235             :   ValTy Val;
     236             : 
     237             :   struct IsInnerUnion {
     238             :     ValTy Val;
     239             : 
     240             :     IsInnerUnion(ValTy val) : Val(val) {}
     241             : 
     242             :     template <typename T> int is() const {
     243     6974842 :       return Val.template is<InnerUnion>() &&
     244             :              Val.template get<InnerUnion>().template is<T>();
     245             :     }
     246             : 
     247             :     template <typename T> T get() const {
     248             :       return Val.template get<InnerUnion>().template get<T>();
     249             :     }
     250             :   };
     251             : 
     252             :   struct IsPT3 {
     253             :     ValTy Val;
     254             : 
     255             :     IsPT3(ValTy val) : Val(val) {}
     256             : 
     257             :     template <typename T> int is() const { return Val.template is<T>(); }
     258             :     template <typename T> T get() const { return Val.template get<T>(); }
     259             :   };
     260             : 
     261             : public:
     262             :   PointerUnion3() = default;
     263     2076390 :   PointerUnion3(PT1 V) { Val = InnerUnion(V); }
     264      203631 :   PointerUnion3(PT2 V) { Val = InnerUnion(V); }
     265         525 :   PointerUnion3(PT3 V) { Val = V; }
     266             : 
     267             :   /// Test if the pointer held in the union is null, regardless of
     268             :   /// which type it is.
     269             :   bool isNull() const { return Val.isNull(); }
     270             :   explicit operator bool() const { return !isNull(); }
     271             : 
     272             :   /// Test if the Union currently holds the type matching T.
     273             :   template <typename T> int is() const {
     274             :     // If T is PT1/PT2 choose IsInnerUnion otherwise choose IsPT3.
     275             :     using Ty = typename ::llvm::PointerUnionTypeSelector<
     276             :         PT1, T, IsInnerUnion,
     277             :         ::llvm::PointerUnionTypeSelector<PT2, T, IsInnerUnion, IsPT3>>::Return;
     278             :     return Ty(Val).template is<T>();
     279             :   }
     280             : 
     281             :   /// Returns the value of the specified pointer type.
     282             :   ///
     283             :   /// If the specified pointer type is incorrect, assert.
     284             :   template <typename T> T get() const {
     285             :     assert(is<T>() && "Invalid accessor called");
     286             :     // If T is PT1/PT2 choose IsInnerUnion otherwise choose IsPT3.
     287             :     using Ty = typename ::llvm::PointerUnionTypeSelector<
     288             :         PT1, T, IsInnerUnion,
     289             :         ::llvm::PointerUnionTypeSelector<PT2, T, IsInnerUnion, IsPT3>>::Return;
     290             :     return Ty(Val).template get<T>();
     291             :   }
     292             : 
     293             :   /// Returns the current pointer if it is of the specified pointer type,
     294             :   /// otherwises returns null.
     295             :   template <typename T> T dyn_cast() const {
     296     1367999 :     if (is<T>())
     297             :       return get<T>();
     298             :     return T();
     299             :   }
     300             : 
     301             :   /// Assignment from nullptr which just clears the union.
     302             :   const PointerUnion3 &operator=(std::nullptr_t) {
     303             :     Val = nullptr;
     304             :     return *this;
     305             :   }
     306             : 
     307             :   /// Assignment operators - Allow assigning into this union from either
     308             :   /// pointer type, setting the discriminator to remember what it came from.
     309             :   const PointerUnion3 &operator=(const PT1 &RHS) {
     310             :     Val = InnerUnion(RHS);
     311             :     return *this;
     312             :   }
     313             :   const PointerUnion3 &operator=(const PT2 &RHS) {
     314             :     Val = InnerUnion(RHS);
     315             :     return *this;
     316             :   }
     317             :   const PointerUnion3 &operator=(const PT3 &RHS) {
     318             :     Val = RHS;
     319             :     return *this;
     320             :   }
     321             : 
     322             :   void *getOpaqueValue() const { return Val.getOpaqueValue(); }
     323             :   static inline PointerUnion3 getFromOpaqueValue(void *VP) {
     324             :     PointerUnion3 V;
     325             :     V.Val = ValTy::getFromOpaqueValue(VP);
     326             :     return V;
     327             :   }
     328             : };
     329             : 
     330             : // Teach SmallPtrSet that PointerUnion3 is "basically a pointer", that has
     331             : // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
     332             : template <typename PT1, typename PT2, typename PT3>
     333             : struct PointerLikeTypeTraits<PointerUnion3<PT1, PT2, PT3>> {
     334             :   static inline void *getAsVoidPointer(const PointerUnion3<PT1, PT2, PT3> &P) {
     335             :     return P.getOpaqueValue();
     336             :   }
     337             : 
     338             :   static inline PointerUnion3<PT1, PT2, PT3> getFromVoidPointer(void *P) {
     339             :     return PointerUnion3<PT1, PT2, PT3>::getFromOpaqueValue(P);
     340             :   }
     341             : 
     342             :   // The number of bits available are the min of the two pointer types.
     343             :   enum {
     344             :     NumLowBitsAvailable = PointerLikeTypeTraits<
     345             :         typename PointerUnion3<PT1, PT2, PT3>::ValTy>::NumLowBitsAvailable
     346             :   };
     347             : };
     348             : 
     349             : /// A pointer union of four pointer types. See documentation for PointerUnion
     350             : /// for usage.
     351             : template <typename PT1, typename PT2, typename PT3, typename PT4>
     352             : class PointerUnion4 {
     353             : public:
     354             :   using InnerUnion1 = PointerUnion<PT1, PT2>;
     355             :   using InnerUnion2 = PointerUnion<PT3, PT4>;
     356             :   using ValTy = PointerUnion<InnerUnion1, InnerUnion2>;
     357             : 
     358             : private:
     359             :   ValTy Val;
     360             : 
     361             : public:
     362             :   PointerUnion4() = default;
     363     4409742 :   PointerUnion4(PT1 V) { Val = InnerUnion1(V); }
     364       24469 :   PointerUnion4(PT2 V) { Val = InnerUnion1(V); }
     365       60655 :   PointerUnion4(PT3 V) { Val = InnerUnion2(V); }
     366       22400 :   PointerUnion4(PT4 V) { Val = InnerUnion2(V); }
     367             : 
     368             :   /// Test if the pointer held in the union is null, regardless of
     369             :   /// which type it is.
     370             :   bool isNull() const { return Val.isNull(); }
     371             :   explicit operator bool() const { return !isNull(); }
     372             : 
     373             :   /// Test if the Union currently holds the type matching T.
     374             :   template <typename T> int is() const {
     375             :     // If T is PT1/PT2 choose InnerUnion1 otherwise choose InnerUnion2.
     376             :     using Ty = typename ::llvm::PointerUnionTypeSelector<
     377             :         PT1, T, InnerUnion1,
     378             :         ::llvm::PointerUnionTypeSelector<PT2, T, InnerUnion1,
     379             :                                          InnerUnion2>>::Return;
     380   162080468 :     return Val.template is<Ty>() && Val.template get<Ty>().template is<T>();
     381             :   }
     382             : 
     383             :   /// Returns the value of the specified pointer type.
     384             :   ///
     385             :   /// If the specified pointer type is incorrect, assert.
     386             :   template <typename T> T get() const {
     387             :     assert(is<T>() && "Invalid accessor called");
     388             :     // If T is PT1/PT2 choose InnerUnion1 otherwise choose InnerUnion2.
     389             :     using Ty = typename ::llvm::PointerUnionTypeSelector<
     390             :         PT1, T, InnerUnion1,
     391             :         ::llvm::PointerUnionTypeSelector<PT2, T, InnerUnion1,
     392             :                                          InnerUnion2>>::Return;
     393             :     return Val.template get<Ty>().template get<T>();
     394             :   }
     395             : 
     396             :   /// Returns the current pointer if it is of the specified pointer type,
     397             :   /// otherwises returns null.
     398             :   template <typename T> T dyn_cast() const {
     399             :     if (is<T>())
     400             :       return get<T>();
     401             :     return T();
     402             :   }
     403             : 
     404             :   /// Assignment from nullptr which just clears the union.
     405             :   const PointerUnion4 &operator=(std::nullptr_t) {
     406             :     Val = nullptr;
     407             :     return *this;
     408             :   }
     409             : 
     410             :   /// Assignment operators - Allow assigning into this union from either
     411             :   /// pointer type, setting the discriminator to remember what it came from.
     412             :   const PointerUnion4 &operator=(const PT1 &RHS) {
     413             :     Val = InnerUnion1(RHS);
     414             :     return *this;
     415             :   }
     416             :   const PointerUnion4 &operator=(const PT2 &RHS) {
     417             :     Val = InnerUnion1(RHS);
     418             :     return *this;
     419             :   }
     420             :   const PointerUnion4 &operator=(const PT3 &RHS) {
     421             :     Val = InnerUnion2(RHS);
     422             :     return *this;
     423             :   }
     424             :   const PointerUnion4 &operator=(const PT4 &RHS) {
     425             :     Val = InnerUnion2(RHS);
     426             :     return *this;
     427             :   }
     428             : 
     429             :   void *getOpaqueValue() const { return Val.getOpaqueValue(); }
     430             :   static inline PointerUnion4 getFromOpaqueValue(void *VP) {
     431             :     PointerUnion4 V;
     432             :     V.Val = ValTy::getFromOpaqueValue(VP);
     433             :     return V;
     434             :   }
     435             : };
     436             : 
     437             : // Teach SmallPtrSet that PointerUnion4 is "basically a pointer", that has
     438             : // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
     439             : template <typename PT1, typename PT2, typename PT3, typename PT4>
     440             : struct PointerLikeTypeTraits<PointerUnion4<PT1, PT2, PT3, PT4>> {
     441             :   static inline void *
     442             :   getAsVoidPointer(const PointerUnion4<PT1, PT2, PT3, PT4> &P) {
     443             :     return P.getOpaqueValue();
     444             :   }
     445             : 
     446             :   static inline PointerUnion4<PT1, PT2, PT3, PT4> getFromVoidPointer(void *P) {
     447             :     return PointerUnion4<PT1, PT2, PT3, PT4>::getFromOpaqueValue(P);
     448             :   }
     449             : 
     450             :   // The number of bits available are the min of the two pointer types.
     451             :   enum {
     452             :     NumLowBitsAvailable = PointerLikeTypeTraits<
     453             :         typename PointerUnion4<PT1, PT2, PT3, PT4>::ValTy>::NumLowBitsAvailable
     454             :   };
     455             : };
     456             : 
     457             : // Teach DenseMap how to use PointerUnions as keys.
     458             : template <typename T, typename U> struct DenseMapInfo<PointerUnion<T, U>> {
     459             :   using Pair = PointerUnion<T, U>;
     460             :   using FirstInfo = DenseMapInfo<T>;
     461             :   using SecondInfo = DenseMapInfo<U>;
     462             : 
     463             :   static inline Pair getEmptyKey() { return Pair(FirstInfo::getEmptyKey()); }
     464             : 
     465             :   static inline Pair getTombstoneKey() {
     466             :     return Pair(FirstInfo::getTombstoneKey());
     467             :   }
     468             : 
     469             :   static unsigned getHashValue(const Pair &PairVal) {
     470             :     intptr_t key = (intptr_t)PairVal.getOpaqueValue();
     471             :     return DenseMapInfo<intptr_t>::getHashValue(key);
     472             :   }
     473             : 
     474    11902271 :   static bool isEqual(const Pair &LHS, const Pair &RHS) {
     475    23416814 :     return LHS.template is<T>() == RHS.template is<T>() &&
     476      141029 :            (LHS.template is<T>() ? FirstInfo::isEqual(LHS.template get<T>(),
     477             :                                                       RHS.template get<T>())
     478             :                                  : SecondInfo::isEqual(LHS.template get<U>(),
     479    11902271 :                                                        RHS.template get<U>()));
     480             :   }
     481             : };
     482             : 
     483             : } // end namespace llvm
     484             : 
     485             : #endif // LLVM_ADT_POINTERUNION_H

Generated by: LCOV version 1.13