Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 1 | //===----------------- LLVMContextImpl.h - Implementation ------*- C++ -*--===// |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Owen Anderson | 36f62e5 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file declares LLVMContextImpl, the opaque implementation |
| 11 | // of LLVMContext. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 14 | |
| 15 | #ifndef LLVM_LLVMCONTEXT_IMPL_H |
| 16 | #define LLVM_LLVMCONTEXT_IMPL_H |
| 17 | |
Owen Anderson | 2ad5217 | 2009-07-21 02:47:59 +0000 | [diff] [blame^] | 18 | #include "llvm/LLVMContext.h" |
| 19 | #include "llvm/DerivedTypes.h" |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 20 | #include "llvm/System/RWMutex.h" |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/APFloat.h" |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/APInt.h" |
| 23 | #include "llvm/ADT/DenseMap.h" |
Owen Anderson | 4118dde | 2009-07-16 23:44:30 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/FoldingSet.h" |
Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringMap.h" |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 26 | |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 27 | namespace llvm { |
| 28 | |
| 29 | class ConstantInt; |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 30 | class ConstantFP; |
Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 31 | class MDString; |
Owen Anderson | 4118dde | 2009-07-16 23:44:30 +0000 | [diff] [blame] | 32 | class MDNode; |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 33 | class LLVMContext; |
| 34 | class Type; |
Owen Anderson | 4118dde | 2009-07-16 23:44:30 +0000 | [diff] [blame] | 35 | class Value; |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 36 | |
| 37 | struct DenseMapAPIntKeyInfo { |
| 38 | struct KeyTy { |
| 39 | APInt val; |
| 40 | const Type* type; |
| 41 | KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {} |
| 42 | KeyTy(const KeyTy& that) : val(that.val), type(that.type) {} |
| 43 | bool operator==(const KeyTy& that) const { |
| 44 | return type == that.type && this->val == that.val; |
| 45 | } |
| 46 | bool operator!=(const KeyTy& that) const { |
| 47 | return !this->operator==(that); |
| 48 | } |
| 49 | }; |
| 50 | static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } |
| 51 | static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } |
| 52 | static unsigned getHashValue(const KeyTy &Key) { |
| 53 | return DenseMapInfo<void*>::getHashValue(Key.type) ^ |
| 54 | Key.val.getHashValue(); |
| 55 | } |
| 56 | static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { |
| 57 | return LHS == RHS; |
| 58 | } |
| 59 | static bool isPod() { return false; } |
| 60 | }; |
| 61 | |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 62 | struct DenseMapAPFloatKeyInfo { |
| 63 | struct KeyTy { |
| 64 | APFloat val; |
| 65 | KeyTy(const APFloat& V) : val(V){} |
| 66 | KeyTy(const KeyTy& that) : val(that.val) {} |
| 67 | bool operator==(const KeyTy& that) const { |
| 68 | return this->val.bitwiseIsEqual(that.val); |
| 69 | } |
| 70 | bool operator!=(const KeyTy& that) const { |
| 71 | return !this->operator==(that); |
| 72 | } |
| 73 | }; |
| 74 | static inline KeyTy getEmptyKey() { |
| 75 | return KeyTy(APFloat(APFloat::Bogus,1)); |
| 76 | } |
| 77 | static inline KeyTy getTombstoneKey() { |
| 78 | return KeyTy(APFloat(APFloat::Bogus,2)); |
| 79 | } |
| 80 | static unsigned getHashValue(const KeyTy &Key) { |
| 81 | return Key.val.getHashValue(); |
| 82 | } |
| 83 | static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { |
| 84 | return LHS == RHS; |
| 85 | } |
| 86 | static bool isPod() { return false; } |
| 87 | }; |
| 88 | |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 89 | class LLVMContextImpl { |
| 90 | sys::SmartRWMutex<true> ConstantsLock; |
| 91 | |
| 92 | typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, |
| 93 | DenseMapAPIntKeyInfo> IntMapTy; |
| 94 | IntMapTy IntConstants; |
| 95 | |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 96 | typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, |
| 97 | DenseMapAPFloatKeyInfo> FPMapTy; |
| 98 | FPMapTy FPConstants; |
| 99 | |
Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 100 | StringMap<MDString*> MDStringCache; |
| 101 | |
Owen Anderson | 4118dde | 2009-07-16 23:44:30 +0000 | [diff] [blame] | 102 | FoldingSet<MDNode> MDNodeSet; |
| 103 | |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 104 | LLVMContext &Context; |
Owen Anderson | 2ad5217 | 2009-07-21 02:47:59 +0000 | [diff] [blame^] | 105 | ConstantInt *TheTrueVal; |
| 106 | ConstantInt *TheFalseVal; |
| 107 | |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 108 | LLVMContextImpl(); |
| 109 | LLVMContextImpl(const LLVMContextImpl&); |
| 110 | public: |
Owen Anderson | 2ad5217 | 2009-07-21 02:47:59 +0000 | [diff] [blame^] | 111 | LLVMContextImpl(LLVMContext &C) : Context(C), TheTrueVal(0), TheFalseVal(0) {} |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 112 | |
| 113 | /// Return a ConstantInt with the specified value and an implied Type. The |
| 114 | /// type is the integer type that corresponds to the bit width of the value. |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 115 | ConstantInt *getConstantInt(const APInt &V); |
| 116 | |
| 117 | ConstantFP *getConstantFP(const APFloat &V); |
Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 118 | |
| 119 | MDString *getMDString(const char *StrBegin, const char *StrEnd); |
| 120 | |
Owen Anderson | 4118dde | 2009-07-16 23:44:30 +0000 | [diff] [blame] | 121 | MDNode *getMDNode(Value*const* Vals, unsigned NumVals); |
Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 122 | |
Owen Anderson | 2ad5217 | 2009-07-21 02:47:59 +0000 | [diff] [blame^] | 123 | ConstantInt *getConstantIntTrue() { |
| 124 | if (TheTrueVal) |
| 125 | return TheTrueVal; |
| 126 | else |
| 127 | return (TheTrueVal = Context.getConstantInt(IntegerType::get(1), 1)); |
| 128 | } |
| 129 | |
| 130 | ConstantInt *getConstantIntFalse() { |
| 131 | if (TheFalseVal) |
| 132 | return TheFalseVal; |
| 133 | else |
| 134 | return (TheFalseVal = Context.getConstantInt(IntegerType::get(1), 0)); |
| 135 | } |
| 136 | |
Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 137 | void erase(MDString *M); |
Owen Anderson | 4118dde | 2009-07-16 23:44:30 +0000 | [diff] [blame] | 138 | void erase(MDNode *M); |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | } |
| 142 | |
Owen Anderson | 36f62e5 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 143 | #endif |