LLVMContext: Store APInt/APFloat directly into the ConstantInt/FP DenseMaps.

Required some APInt massaging to get proper empty/tombstone values. Apart
from making the code a bit simpler this also reduces the bucket size of
the ConstantInt map from 32 to 24 bytes.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223478 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Benjamin Kramer 2014-12-05 17:03:01 +00:00
parent 3ad762170b
commit 472e9cc4bc
3 changed files with 26 additions and 57 deletions

View File

@ -91,6 +91,8 @@ class APInt {
APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t)) APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
}; };
friend struct DenseMapAPIntKeyInfo;
/// \brief Fast internal constructor /// \brief Fast internal constructor
/// ///
/// This constructor is used only internally for speed of construction of /// This constructor is used only internally for speed of construction of
@ -277,7 +279,6 @@ public:
/// Simply makes *this a copy of that. /// Simply makes *this a copy of that.
/// @brief Copy Constructor. /// @brief Copy Constructor.
APInt(const APInt &that) : BitWidth(that.BitWidth), VAL(0) { APInt(const APInt &that) : BitWidth(that.BitWidth), VAL(0) {
assert(BitWidth && "bitwidth too small");
if (isSingleWord()) if (isSingleWord())
VAL = that.VAL; VAL = that.VAL;
else else

View File

@ -555,18 +555,17 @@ Constant *ConstantInt::getFalse(Type *Ty) {
} }
// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap // Get a ConstantInt from an APInt.
// 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(LLVMContext &Context, const APInt &V) { 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 // get an existing value or the insertion position
LLVMContextImpl *pImpl = Context.pImpl; LLVMContextImpl *pImpl = Context.pImpl;
ConstantInt *&Slot = pImpl->IntConstants[DenseMapAPIntKeyInfo::KeyTy(V, ITy)]; ConstantInt *&Slot = pImpl->IntConstants[V];
if (!Slot) Slot = new ConstantInt(ITy, 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; return Slot;
} }
@ -689,7 +688,7 @@ Constant *ConstantFP::getZeroValueForNegation(Type *Ty) {
ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
LLVMContextImpl* pImpl = Context.pImpl; LLVMContextImpl* pImpl = Context.pImpl;
ConstantFP *&Slot = pImpl->FPConstants[DenseMapAPFloatKeyInfo::KeyTy(V)]; ConstantFP *&Slot = pImpl->FPConstants[V];
if (!Slot) { if (!Slot) {
Type *Ty; Type *Ty;

View File

@ -46,55 +46,26 @@ class Type;
class Value; class Value;
struct DenseMapAPIntKeyInfo { struct DenseMapAPIntKeyInfo {
struct KeyTy { static inline APInt getEmptyKey() { return APInt(nullptr, 0); }
APInt val; static inline APInt getTombstoneKey() {
Type* type; return APInt(reinterpret_cast<uint64_t *>(sizeof(uint64_t)), 0);
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 { static unsigned getHashValue(const APInt &Key) {
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) {
return static_cast<unsigned>(hash_value(Key)); return static_cast<unsigned>(hash_value(Key));
} }
static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { static bool isEqual(const APInt &LHS, const APInt &RHS) {
return LHS == RHS; return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
} }
}; };
struct DenseMapAPFloatKeyInfo { struct DenseMapAPFloatKeyInfo {
struct KeyTy { static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); }
APFloat val; static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); }
KeyTy(const APFloat& V) : val(V){} static unsigned getHashValue(const APFloat &Key) {
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) {
return static_cast<unsigned>(hash_value(Key)); return static_cast<unsigned>(hash_value(Key));
} }
static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
return LHS == RHS; return LHS.bitwiseIsEqual(RHS);
} }
}; };
@ -277,12 +248,10 @@ public:
LLVMContext::YieldCallbackTy YieldCallback; LLVMContext::YieldCallbackTy YieldCallback;
void *YieldOpaqueHandle; void *YieldOpaqueHandle;
typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt *, typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
DenseMapAPIntKeyInfo> IntMapTy;
IntMapTy IntConstants; IntMapTy IntConstants;
typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
DenseMapAPFloatKeyInfo> FPMapTy;
FPMapTy FPConstants; FPMapTy FPConstants;
FoldingSet<AttributeImpl> AttrsSet; FoldingSet<AttributeImpl> AttrsSet;