From: Benjamin Kramer Date: Sat, 6 Dec 2014 13:12:56 +0000 (+0000) Subject: Reapply "LLVMContext: Store APInt/APFloat directly into the ConstantInt/FP DenseMaps." X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=51df2c2a106440b4b9fb7309da922ef0ba244a15;hp=278bbacd274e5aa9e4a65641071c99b37583b25b;p=oota-llvm.git Reapply "LLVMContext: Store APInt/APFloat directly into the ConstantInt/FP DenseMaps." This reapplies r223478 with a fix for 32 bit targets. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223586 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h index f4e7e3c6356..025397d9ce4 100644 --- a/include/llvm/ADT/APInt.h +++ b/include/llvm/ADT/APInt.h @@ -91,6 +91,8 @@ class APInt { APINT_WORD_SIZE = static_cast(sizeof(uint64_t)) }; + friend struct DenseMapAPIntKeyInfo; + /// \brief Fast internal constructor /// /// This constructor is used only internally for speed of construction of @@ -277,7 +279,6 @@ public: /// Simply makes *this a copy of that. /// @brief Copy Constructor. APInt(const APInt &that) : BitWidth(that.BitWidth), VAL(0) { - assert(BitWidth && "bitwidth too small"); if (isSingleWord()) VAL = that.VAL; else diff --git a/lib/IR/Constants.cpp b/lib/IR/Constants.cpp index 96d9510313e..1d2602aef13 100644 --- a/lib/IR/Constants.cpp +++ b/lib/IR/Constants.cpp @@ -554,19 +554,17 @@ Constant *ConstantInt::getFalse(Type *Ty) { ConstantInt::getFalse(Ty->getContext())); } -// 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. +// Get a ConstantInt from an APInt. ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) { - // Get the corresponding integer type for the bit width of the value. - IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); // get an existing value or the insertion position LLVMContextImpl *pImpl = Context.pImpl; - ConstantInt *&Slot = pImpl->IntConstants[DenseMapAPIntKeyInfo::KeyTy(V, ITy)]; - if (!Slot) + ConstantInt *&Slot = pImpl->IntConstants[V]; + if (!Slot) { + // Get the corresponding integer type for the bit width of the value. + IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); Slot = new ConstantInt(ITy, V); + } + assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth())); return Slot; } @@ -689,7 +687,7 @@ Constant *ConstantFP::getZeroValueForNegation(Type *Ty) { ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { LLVMContextImpl* pImpl = Context.pImpl; - ConstantFP *&Slot = pImpl->FPConstants[DenseMapAPFloatKeyInfo::KeyTy(V)]; + ConstantFP *&Slot = pImpl->FPConstants[V]; if (!Slot) { Type *Ty; diff --git a/lib/IR/LLVMContextImpl.h b/lib/IR/LLVMContextImpl.h index 7b5f14deb5c..31f5fe83d68 100644 --- a/lib/IR/LLVMContextImpl.h +++ b/lib/IR/LLVMContextImpl.h @@ -46,48 +46,33 @@ class Type; class Value; struct DenseMapAPIntKeyInfo { - struct KeyTy { - APInt val; - Type *type; - KeyTy(const APInt &V, Type *Ty) : val(V), type(Ty) {} - bool operator==(const KeyTy &that) const { - return type == that.type && this->val == that.val; - } - bool operator!=(const KeyTy &that) const { return !this->operator==(that); } - friend hash_code hash_value(const KeyTy &Key) { - return hash_combine(Key.type, Key.val); - } - }; - static inline KeyTy getEmptyKey() { return KeyTy(APInt(1, 0), nullptr); } - static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1, 1), nullptr); } - static unsigned getHashValue(const KeyTy &Key) { + static inline APInt getEmptyKey() { + APInt V(nullptr, 0); + V.VAL = 0; + return V; + } + static inline APInt getTombstoneKey() { + APInt V(nullptr, 0); + V.VAL = 1; + return V; + } + static unsigned getHashValue(const APInt &Key) { return static_cast(hash_value(Key)); } - static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { return LHS == RHS; } + static bool isEqual(const APInt &LHS, const APInt &RHS) { + return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS; + } }; struct DenseMapAPFloatKeyInfo { - struct KeyTy { - APFloat val; - KeyTy(const APFloat &V) : val(V) {} - bool operator==(const KeyTy &that) const { - return this->val.bitwiseIsEqual(that.val); - } - bool operator!=(const KeyTy &that) const { return !this->operator==(that); } - friend hash_code hash_value(const KeyTy &Key) { - return hash_combine(Key.val); - } - }; - static inline KeyTy getEmptyKey() { - return KeyTy(APFloat(APFloat::Bogus, 1)); - } - static inline KeyTy getTombstoneKey() { - return KeyTy(APFloat(APFloat::Bogus, 2)); - } - static unsigned getHashValue(const KeyTy &Key) { + static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); } + static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); } + static unsigned getHashValue(const APFloat &Key) { return static_cast(hash_value(Key)); } - static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { return LHS == RHS; } + static bool isEqual(const APFloat &LHS, const APFloat &RHS) { + return LHS.bitwiseIsEqual(RHS); + } }; struct AnonStructTypeKeyInfo { @@ -269,12 +254,10 @@ public: LLVMContext::YieldCallbackTy YieldCallback; void *YieldOpaqueHandle; - typedef DenseMap IntMapTy; + typedef DenseMap IntMapTy; IntMapTy IntConstants; - typedef DenseMap FPMapTy; + typedef DenseMap FPMapTy; FPMapTy FPConstants; FoldingSet AttrsSet;