Cache const/non-const variants of Atomic and ReferenceTypes.

More reduction of dynamic memory allocation.
This commit is contained in:
Matt Pharr
2012-05-04 13:14:44 -07:00
parent c0019bd8e5
commit bff02017da
2 changed files with 27 additions and 4 deletions

View File

@@ -185,6 +185,7 @@ const AtomicType *AtomicType::Void =
AtomicType::AtomicType(BasicType bt, Variability v, bool ic) AtomicType::AtomicType(BasicType bt, Variability v, bool ic)
: Type(ATOMIC_TYPE), basicType(bt), variability(v), isConst(ic) { : Type(ATOMIC_TYPE), basicType(bt), variability(v), isConst(ic) {
asOtherConstType = NULL;
} }
@@ -257,7 +258,11 @@ AtomicType::GetAsConstType() const {
if (basicType == TYPE_VOID || isConst == true) if (basicType == TYPE_VOID || isConst == true)
return this; return this;
return new AtomicType(basicType, variability, true); if (asOtherConstType == NULL) {
asOtherConstType = new AtomicType(basicType, variability, true);
asOtherConstType->asOtherConstType = this;
}
return asOtherConstType;
} }
@@ -266,7 +271,11 @@ AtomicType::GetAsNonConstType() const {
if (basicType == TYPE_VOID || isConst == false) if (basicType == TYPE_VOID || isConst == false)
return this; return this;
return new AtomicType(basicType, variability, false); if (asOtherConstType == NULL) {
asOtherConstType = new AtomicType(basicType, variability, false);
asOtherConstType->asOtherConstType = this;
}
return asOtherConstType;
} }
@@ -2310,6 +2319,7 @@ UndefinedStructType::GetDIType(llvm::DIDescriptor scope) const {
ReferenceType::ReferenceType(const Type *t) ReferenceType::ReferenceType(const Type *t)
: Type(REFERENCE_TYPE), targetType(t) { : Type(REFERENCE_TYPE), targetType(t) {
asOtherConstType = NULL;
} }
@@ -2450,7 +2460,12 @@ ReferenceType::GetAsConstType() const {
} }
if (IsConstType()) if (IsConstType())
return this; return this;
return new ReferenceType(targetType->GetAsConstType());
if (asOtherConstType == NULL) {
asOtherConstType = new ReferenceType(targetType->GetAsConstType());
asOtherConstType->asOtherConstType = this;
}
return asOtherConstType;
} }
@@ -2462,7 +2477,12 @@ ReferenceType::GetAsNonConstType() const {
} }
if (!IsConstType()) if (!IsConstType())
return this; return this;
return new ReferenceType(targetType->GetAsNonConstType());
if (asOtherConstType == NULL) {
asOtherConstType = new ReferenceType(targetType->GetAsNonConstType());
asOtherConstType->asOtherConstType = this;
}
return asOtherConstType;
} }

3
type.h
View File

@@ -332,6 +332,8 @@ private:
const Variability variability; const Variability variability;
const bool isConst; const bool isConst;
AtomicType(BasicType basicType, Variability v, bool isConst); AtomicType(BasicType basicType, Variability v, bool isConst);
mutable const AtomicType *asOtherConstType;
}; };
@@ -802,6 +804,7 @@ public:
private: private:
const Type * const targetType; const Type * const targetType;
mutable const ReferenceType *asOtherConstType;
}; };