Get this building with gcc-4.4.
[oota-llvm.git] / include / llvm / ADT / PointerIntPair.h
1 //===- llvm/ADT/PointerIntPair.h - Pair for pointer and int -----*- 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 PointerIntPair class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ADT_POINTERINTPAIR_H
15 #define LLVM_ADT_POINTERINTPAIR_H
16
17 #include "llvm/Support/DataTypes.h"
18 #include <cassert>
19
20 namespace llvm {
21
22 template<typename T>
23 struct DenseMapInfo;
24
25 /// PointerIntPair - This class implements a pair of a pointer and small
26 /// integer.  It is designed to represent this in the space required by one
27 /// pointer by bitmangling the integer into the low part of the pointer.  This
28 /// can only be done for small integers: typically up to 3 bits, but it depends
29 /// on the alignment returned by the allocator in use.
30 ///
31 template <typename PointerTy, unsigned IntBits, typename IntType=unsigned>
32 class PointerIntPair {
33   intptr_t Value;
34 public:
35   PointerIntPair() : Value(0) {}
36   PointerIntPair(PointerTy Ptr, IntType Int) : Value(0) {
37     setPointer(Ptr);
38     setInt(Int);
39   }
40
41   PointerTy getPointer() const {
42     return reinterpret_cast<PointerTy>(Value & ~((1 << IntBits)-1));
43   }
44
45   IntType getInt() const {
46     return (IntType)(Value & ((1 << IntBits)-1));
47   }
48
49   void setPointer(PointerTy Ptr) {
50     intptr_t PtrVal = reinterpret_cast<intptr_t>(Ptr);
51     assert((PtrVal & ((1 << IntBits)-1)) == 0 &&
52            "Pointer is not sufficiently aligned");
53     Value = PtrVal | (intptr_t)getInt();
54   }
55
56   void setInt(IntType Int) {
57     intptr_t IntVal = Int;
58     assert(IntVal < (1 << IntBits) && "Integer too large for field");
59     Value = reinterpret_cast<intptr_t>(getPointer()) | IntVal;
60   }
61
62   void *getOpaqueValue() const { return reinterpret_cast<void*>(Value); }
63   void setFromOpaqueValue(void *Val) { Value = reinterpret_cast<intptr_t>(Val);}
64
65   bool operator==(const PointerIntPair &RHS) const {return Value == RHS.Value;}
66   bool operator!=(const PointerIntPair &RHS) const {return Value != RHS.Value;}
67   bool operator<(const PointerIntPair &RHS) const {return Value < RHS.Value;}
68   bool operator>(const PointerIntPair &RHS) const {return Value > RHS.Value;}
69   bool operator<=(const PointerIntPair &RHS) const {return Value <= RHS.Value;}
70   bool operator>=(const PointerIntPair &RHS) const {return Value >= RHS.Value;}
71 };
72
73 // Provide specialization of DenseMapInfo for PointerIntPair.
74 template<typename PointerTy, unsigned IntBits, typename IntType>
75 struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType> > {
76   typedef PointerIntPair<PointerTy, IntBits, IntType> Ty;
77   static Ty getEmptyKey() {
78     return Ty(reinterpret_cast<PointerTy>(-1 << IntBits),
79               IntType((1 << IntBits)-1));
80   }
81   static Ty getTombstoneKey() {
82     return Ty(reinterpret_cast<PointerTy>(-2 << IntBits), IntType(0));
83   }
84   static unsigned getHashValue(Ty V) {
85     uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
86     return unsigned(IV) ^ unsigned(IV >> 9);
87   }
88   static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
89   static bool isPod() { return true; }
90 };
91
92 } // end namespace llvm
93 #endif