diff options
Diffstat (limited to 'lib/VMCore')
-rw-r--r-- | lib/VMCore/Constants.cpp | 61 | ||||
-rw-r--r-- | lib/VMCore/LLVMContext.cpp | 4 | ||||
-rw-r--r-- | lib/VMCore/LLVMContextImpl.cpp | 48 | ||||
-rw-r--r-- | lib/VMCore/LLVMContextImpl.h | 52 |
4 files changed, 100 insertions, 65 deletions
diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 49dd6a06d8..680aed5902 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -190,67 +190,6 @@ ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) { return WhichOne ? TheTrueVal : TheFalseVal; } - -namespace { - struct DenseMapAPIntKeyInfo { - struct KeyTy { - APInt val; - const Type* type; - KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {} - KeyTy(const KeyTy& that) : val(that.val), type(that.type) {} - bool operator==(const KeyTy& that) const { - return type == that.type && this->val == that.val; - } - bool operator!=(const KeyTy& that) const { - return !this->operator==(that); - } - }; - static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } - static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } - static unsigned getHashValue(const KeyTy &Key) { - return DenseMapInfo<void*>::getHashValue(Key.type) ^ - Key.val.getHashValue(); - } - static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { - return LHS == RHS; - } - static bool isPod() { return false; } - }; -} - - -typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, - DenseMapAPIntKeyInfo> IntMapTy; -static ManagedStatic<IntMapTy> IntConstants; - -// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap -// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the -// operator== and operator!= to ensure that the DenseMap doesn't attempt to -// compare APInt's of different widths, which would violate an APInt class -// invariant which generates an assertion. -ConstantInt *ConstantInt::get(const APInt& V) { - // Get the corresponding integer type for the bit width of the value. - const IntegerType *ITy = IntegerType::get(V.getBitWidth()); - // get an existing value or the insertion position - DenseMapAPIntKeyInfo::KeyTy Key(V, ITy); - - ConstantsLock->reader_acquire(); - ConstantInt *&Slot = (*IntConstants)[Key]; - ConstantsLock->reader_release(); - - if (!Slot) { - sys::SmartScopedWriter<true> Writer(*ConstantsLock); - ConstantInt *&NewSlot = (*IntConstants)[Key]; - if (!Slot) { - NewSlot = new ConstantInt(ITy, V); - } - - return NewSlot; - } else { - return Slot; - } -} - //===----------------------------------------------------------------------===// // ConstantFP //===----------------------------------------------------------------------===// diff --git a/lib/VMCore/LLVMContext.cpp b/lib/VMCore/LLVMContext.cpp index 73c93a2d6e..0372f31e67 100644 --- a/lib/VMCore/LLVMContext.cpp +++ b/lib/VMCore/LLVMContext.cpp @@ -29,7 +29,7 @@ LLVMContext& llvm::getGlobalContext() { return *GlobalContext; } -LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { } +LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { } LLVMContext::~LLVMContext() { delete pImpl; } // Constant accessors @@ -117,7 +117,7 @@ Constant *LLVMContext::getConstantIntSigned(const Type *Ty, int64_t V) { } ConstantInt* LLVMContext::getConstantInt(const APInt& V) { - return ConstantInt::get(V); + return pImpl->getConstantInt(V); } Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) { diff --git a/lib/VMCore/LLVMContextImpl.cpp b/lib/VMCore/LLVMContextImpl.cpp new file mode 100644 index 0000000000..a92c19fe04 --- /dev/null +++ b/lib/VMCore/LLVMContextImpl.cpp @@ -0,0 +1,48 @@ +//===--------------- LLVMContextImpl.cpp - Implementation ------*- C++ -*--===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements LLVMContextImpl, the opaque implementation +// of LLVMContext. +// +//===----------------------------------------------------------------------===// + +#include "LLVMContextImpl.h" +#include "llvm/Constants.h" +#include "llvm/DerivedTypes.h" +#include "llvm/LLVMContext.h" +using namespace llvm; + +// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap +// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the +// operator== and operator!= to ensure that the DenseMap doesn't attempt to +// compare APInt's of different widths, which would violate an APInt class +// invariant which generates an assertion. +ConstantInt *LLVMContextImpl::getConstantInt(const APInt& V) { + // Get the corresponding integer type for the bit width of the value. + const IntegerType *ITy = Context.getIntegerType(V.getBitWidth()); + // get an existing value or the insertion position + DenseMapAPIntKeyInfo::KeyTy Key(V, ITy); + + ConstantsLock.reader_acquire(); + ConstantInt *&Slot = IntConstants[Key]; + ConstantsLock.reader_release(); + + if (!Slot) { + sys::SmartScopedWriter<true> Writer(ConstantsLock); + ConstantInt *&NewSlot = IntConstants[Key]; + if (!Slot) { + NewSlot = new ConstantInt(ITy, V); + } + + return NewSlot; + } else { + return Slot; + } +} + diff --git a/lib/VMCore/LLVMContextImpl.h b/lib/VMCore/LLVMContextImpl.h index 4e089fb661..fbf29fdfc4 100644 --- a/lib/VMCore/LLVMContextImpl.h +++ b/lib/VMCore/LLVMContextImpl.h @@ -1,4 +1,4 @@ -//===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===// +//===----------------- LLVMContextImpl.h - Implementation ------*- C++ -*--===// // // The LLVM Compiler Infrastructure // @@ -15,9 +15,57 @@ #ifndef LLVM_LLVMCONTEXT_IMPL_H #define LLVM_LLVMCONTEXT_IMPL_H +#include "llvm/System/RWMutex.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/DenseMap.h" + namespace llvm { -class LLVMContextImpl { +class ConstantInt; +class LLVMContext; +class Type; + +struct DenseMapAPIntKeyInfo { + struct KeyTy { + APInt val; + const Type* type; + KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {} + KeyTy(const KeyTy& that) : val(that.val), type(that.type) {} + bool operator==(const KeyTy& that) const { + return type == that.type && this->val == that.val; + } + bool operator!=(const KeyTy& that) const { + return !this->operator==(that); + } + }; + static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } + static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } + static unsigned getHashValue(const KeyTy &Key) { + return DenseMapInfo<void*>::getHashValue(Key.type) ^ + Key.val.getHashValue(); + } + static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { + return LHS == RHS; + } + static bool isPod() { return false; } +}; + +class LLVMContextImpl { + sys::SmartRWMutex<true> ConstantsLock; + + typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, + DenseMapAPIntKeyInfo> IntMapTy; + IntMapTy IntConstants; + + LLVMContext &Context; + LLVMContextImpl(); + LLVMContextImpl(const LLVMContextImpl&); +public: + LLVMContextImpl(LLVMContext &C) : Context(C) { } + + /// Return a ConstantInt with the specified value and an implied Type. The + /// type is the integer type that corresponds to the bit width of the value. + ConstantInt* getConstantInt(const APInt &V); }; } |