Use unique_ptr in LLVMContextImpl's constant maps.

Reviewers: timshen

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D25419

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283767 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Justin Lebar 2016-10-10 16:26:13 +00:00
parent a5afae3a31
commit a2e3c5a457
3 changed files with 34 additions and 29 deletions

View File

@ -545,14 +545,14 @@ Constant *ConstantInt::getFalse(Type *Ty) {
ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) { ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
// 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[V]; std::unique_ptr<ConstantInt> &Slot = pImpl->IntConstants[V];
if (!Slot) { if (!Slot) {
// Get the corresponding integer type for the bit width of the value. // Get the corresponding integer type for the bit width of the value.
IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
Slot = new ConstantInt(ITy, V); Slot.reset(new ConstantInt(ITy, V));
} }
assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth())); assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth()));
return Slot; return Slot.get();
} }
Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) { Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
@ -685,7 +685,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[V]; std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];
if (!Slot) { if (!Slot) {
Type *Ty; Type *Ty;
@ -704,10 +704,10 @@ ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
"Unknown FP format"); "Unknown FP format");
Ty = Type::getPPC_FP128Ty(Context); Ty = Type::getPPC_FP128Ty(Context);
} }
Slot = new ConstantFP(Ty, V); Slot.reset(new ConstantFP(Ty, V));
} }
return Slot; return Slot.get();
} }
Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) { Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
@ -1259,12 +1259,13 @@ bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) { ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) && assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
"Cannot create an aggregate zero of non-aggregate type!"); "Cannot create an aggregate zero of non-aggregate type!");
ConstantAggregateZero *&Entry = Ty->getContext().pImpl->CAZConstants[Ty];
if (!Entry)
Entry = new ConstantAggregateZero(Ty);
return Entry; std::unique_ptr<ConstantAggregateZero> &Entry =
Ty->getContext().pImpl->CAZConstants[Ty];
if (!Entry)
Entry.reset(new ConstantAggregateZero(Ty));
return Entry.get();
} }
/// Remove the constant from the constant table. /// Remove the constant from the constant table.
@ -1325,11 +1326,12 @@ const APInt &Constant::getUniqueInteger() const {
// //
ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) { ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
ConstantPointerNull *&Entry = Ty->getContext().pImpl->CPNConstants[Ty]; std::unique_ptr<ConstantPointerNull> &Entry =
Ty->getContext().pImpl->CPNConstants[Ty];
if (!Entry) if (!Entry)
Entry = new ConstantPointerNull(Ty); Entry.reset(new ConstantPointerNull(Ty));
return Entry; return Entry.get();
} }
/// Remove the constant from the constant table. /// Remove the constant from the constant table.
@ -1338,11 +1340,11 @@ void ConstantPointerNull::destroyConstantImpl() {
} }
UndefValue *UndefValue::get(Type *Ty) { UndefValue *UndefValue::get(Type *Ty) {
UndefValue *&Entry = Ty->getContext().pImpl->UVConstants[Ty]; std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
if (!Entry) if (!Entry)
Entry = new UndefValue(Ty); Entry.reset(new UndefValue(Ty));
return Entry; return Entry.get();
} }
/// Remove the constant from the constant table. /// Remove the constant from the constant table.

View File

@ -94,12 +94,13 @@ LLVMContextImpl::~LLVMContextImpl() {
ArrayConstants.freeConstants(); ArrayConstants.freeConstants();
StructConstants.freeConstants(); StructConstants.freeConstants();
VectorConstants.freeConstants(); VectorConstants.freeConstants();
DeleteContainerSeconds(CAZConstants);
DeleteContainerSeconds(CPNConstants);
DeleteContainerSeconds(UVConstants);
InlineAsms.freeConstants(); InlineAsms.freeConstants();
DeleteContainerSeconds(IntConstants);
DeleteContainerSeconds(FPConstants); CAZConstants.clear();
CPNConstants.clear();
UVConstants.clear();
IntConstants.clear();
FPConstants.clear();
for (auto &CDSConstant : CDSConstants) for (auto &CDSConstant : CDSConstants)
delete CDSConstant.second; delete CDSConstant.second;

View File

@ -1049,10 +1049,12 @@ public:
LLVMContext::YieldCallbackTy YieldCallback; LLVMContext::YieldCallbackTy YieldCallback;
void *YieldOpaqueHandle; void *YieldOpaqueHandle;
typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy; typedef DenseMap<APInt, std::unique_ptr<ConstantInt>, DenseMapAPIntKeyInfo>
IntMapTy;
IntMapTy IntConstants; IntMapTy IntConstants;
typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy; typedef DenseMap<APFloat, std::unique_ptr<ConstantFP>, DenseMapAPFloatKeyInfo>
FPMapTy;
FPMapTy FPConstants; FPMapTy FPConstants;
FoldingSet<AttributeImpl> AttrsSet; FoldingSet<AttributeImpl> AttrsSet;
@ -1078,7 +1080,7 @@ public:
// them on context teardown. // them on context teardown.
std::vector<MDNode *> DistinctMDNodes; std::vector<MDNode *> DistinctMDNodes;
DenseMap<Type*, ConstantAggregateZero*> CAZConstants; DenseMap<Type *, std::unique_ptr<ConstantAggregateZero>> CAZConstants;
typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy; typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
ArrayConstantsTy ArrayConstants; ArrayConstantsTy ArrayConstants;
@ -1088,11 +1090,11 @@ public:
typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy; typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
VectorConstantsTy VectorConstants; VectorConstantsTy VectorConstants;
DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
DenseMap<Type*, UndefValue*> UVConstants; DenseMap<PointerType *, std::unique_ptr<ConstantPointerNull>> CPNConstants;
DenseMap<Type *, std::unique_ptr<UndefValue>> UVConstants;
StringMap<ConstantDataSequential*> CDSConstants; StringMap<ConstantDataSequential*> CDSConstants;
DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *> DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>