[llvm-commits] [llvm] r73624 - /llvm/trunk/lib/VMCore/Constants.cpp
Owen Anderson
resistor at mac.com
Wed Jun 17 11:40:33 PDT 2009
Author: resistor
Date: Wed Jun 17 13:40:29 2009
New Revision: 73624
URL: http://llvm.org/viewvc/llvm-project?rev=73624&view=rev
Log:
Type safety for Constants.cpp! Some of this is temporary, as I'm planning to push some of the R/W locking into FoldingSet.
Modified:
llvm/trunk/lib/VMCore/Constants.cpp
Modified: llvm/trunk/lib/VMCore/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Constants.cpp?rev=73624&r1=73623&r2=73624&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Constants.cpp (original)
+++ llvm/trunk/lib/VMCore/Constants.cpp Wed Jun 17 13:40:29 2009
@@ -25,6 +25,8 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MathExtras.h"
+#include "llvm/Support/Threading.h"
+#include "llvm/System/RWMutex.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include <algorithm>
@@ -35,6 +37,8 @@
// Constant Class
//===----------------------------------------------------------------------===//
+ManagedStatic<sys::RWMutex> ConstantsLock;
+
void Constant::destroyConstantImpl() {
// When a Constant is destroyed, there may be lingering
// references to the constant by other constants in the constant pool. These
@@ -295,12 +299,30 @@
const IntegerType *ITy = IntegerType::get(V.getBitWidth());
// get an existing value or the insertion position
DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
- ConstantInt *&Slot = (*IntConstants)[Key];
- // if it exists, return it.
- if (Slot)
+
+ if (llvm_is_multithreaded()) {
+ ConstantsLock->reader_acquire();
+ ConstantInt *&Slot = (*IntConstants)[Key];
+ ConstantsLock->reader_release();
+
+ if (!Slot) {
+ ConstantsLock->writer_acquire();
+ ConstantInt *&Slot = (*IntConstants)[Key];
+ if (!Slot) {
+ Slot = new ConstantInt(ITy, V);
+ }
+ ConstantsLock->writer_release();
+ }
+
return Slot;
- // otherwise create a new one, insert it, and return it.
- return Slot = new ConstantInt(ITy, V);
+ } else {
+ ConstantInt *&Slot = (*IntConstants)[Key];
+ // if it exists, return it.
+ if (Slot)
+ return Slot;
+ // otherwise create a new one, insert it, and return it.
+ return Slot = new ConstantInt(ITy, V);
+ }
}
Constant *ConstantInt::get(const Type *Ty, const APInt &V) {
@@ -392,24 +414,58 @@
ConstantFP *ConstantFP::get(const APFloat &V) {
DenseMapAPFloatKeyInfo::KeyTy Key(V);
- ConstantFP *&Slot = (*FPConstants)[Key];
- if (Slot) return Slot;
- const Type *Ty;
- if (&V.getSemantics() == &APFloat::IEEEsingle)
- Ty = Type::FloatTy;
- else if (&V.getSemantics() == &APFloat::IEEEdouble)
- Ty = Type::DoubleTy;
- else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
- Ty = Type::X86_FP80Ty;
- else if (&V.getSemantics() == &APFloat::IEEEquad)
- Ty = Type::FP128Ty;
- else {
- assert(&V.getSemantics() == &APFloat::PPCDoubleDouble&&"Unknown FP format");
- Ty = Type::PPC_FP128Ty;
+ if (llvm_is_multithreaded()) {
+ ConstantsLock->reader_acquire();
+ ConstantFP *&Slot = (*FPConstants)[Key];
+ ConstantsLock->reader_release();
+
+ if (!Slot) {
+ ConstantsLock->writer_acquire();
+ Slot = (*FPConstants)[Key];
+ if (!Slot) {
+ const Type *Ty;
+ if (&V.getSemantics() == &APFloat::IEEEsingle)
+ Ty = Type::FloatTy;
+ else if (&V.getSemantics() == &APFloat::IEEEdouble)
+ Ty = Type::DoubleTy;
+ else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
+ Ty = Type::X86_FP80Ty;
+ else if (&V.getSemantics() == &APFloat::IEEEquad)
+ Ty = Type::FP128Ty;
+ else {
+ assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
+ "Unknown FP format");
+ Ty = Type::PPC_FP128Ty;
+ }
+
+ Slot = new ConstantFP(Ty, V);
+ }
+ ConstantsLock->writer_release();
+ }
+
+ return Slot;
+ } else {
+ ConstantFP *&Slot = (*FPConstants)[Key];
+ if (Slot) return Slot;
+
+ const Type *Ty;
+ if (&V.getSemantics() == &APFloat::IEEEsingle)
+ Ty = Type::FloatTy;
+ else if (&V.getSemantics() == &APFloat::IEEEdouble)
+ Ty = Type::DoubleTy;
+ else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
+ Ty = Type::X86_FP80Ty;
+ else if (&V.getSemantics() == &APFloat::IEEEquad)
+ Ty = Type::FP128Ty;
+ else {
+ assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
+ "Unknown FP format");
+ Ty = Type::PPC_FP128Ty;
+ }
+
+ return Slot = new ConstantFP(Ty, V);
}
-
- return Slot = new ConstantFP(Ty, V);
}
/// get() - This returns a constant fp for the specified value in the
@@ -1346,13 +1402,19 @@
ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
"Cannot create an aggregate zero of non-aggregate type!");
- return AggZeroConstants->getOrCreate(Ty, 0);
+ ConstantAggregateZero* result = 0;
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ result = AggZeroConstants->getOrCreate(Ty, 0);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
/// destroyConstant - Remove the constant from the constant table...
///
void ConstantAggregateZero::destroyConstant() {
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
AggZeroConstants->remove(this);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
destroyConstantImpl();
}
@@ -1389,21 +1451,30 @@
Constant *ConstantArray::get(const ArrayType *Ty,
const std::vector<Constant*> &V) {
// If this is an all-zero array, return a ConstantAggregateZero object
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
if (!V.empty()) {
Constant *C = V[0];
- if (!C->isNullValue())
+ if (!C->isNullValue()) {
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
return ArrayConstants->getOrCreate(Ty, V);
+ }
for (unsigned i = 1, e = V.size(); i != e; ++i)
- if (V[i] != C)
+ if (V[i] != C) {
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
return ArrayConstants->getOrCreate(Ty, V);
+ }
}
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+
return ConstantAggregateZero::get(Ty);
}
/// destroyConstant - Remove the constant from the constant table...
///
void ConstantArray::destroyConstant() {
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
ArrayConstants->remove(this);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
destroyConstantImpl();
}
@@ -1512,9 +1583,14 @@
Constant *ConstantStruct::get(const StructType *Ty,
const std::vector<Constant*> &V) {
// Create a ConstantAggregateZero value if all elements are zeros...
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
for (unsigned i = 0, e = V.size(); i != e; ++i)
- if (!V[i]->isNullValue())
- return StructConstants->getOrCreate(Ty, V);
+ if (!V[i]->isNullValue()) {
+ Constant* result = StructConstants->getOrCreate(Ty, V);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
+ }
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
return ConstantAggregateZero::get(Ty);
}
@@ -1530,7 +1606,9 @@
// destroyConstant - Remove the constant from the constant table...
//
void ConstantStruct::destroyConstant() {
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
StructConstants->remove(this);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
destroyConstantImpl();
}
@@ -1584,7 +1662,10 @@
return ConstantAggregateZero::get(Ty);
if (isUndef)
return UndefValue::get(Ty);
- return VectorConstants->getOrCreate(Ty, V);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ Constant* result = VectorConstants->getOrCreate(Ty, V);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
Constant *ConstantVector::get(const std::vector<Constant*> &V) {
@@ -1595,7 +1676,9 @@
// destroyConstant - Remove the constant from the constant table...
//
void ConstantVector::destroyConstant() {
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
VectorConstants->remove(this);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
destroyConstantImpl();
}
@@ -1659,13 +1742,18 @@
ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
- return NullPtrConstants->getOrCreate(Ty, 0);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ ConstantPointerNull* result = NullPtrConstants->getOrCreate(Ty, 0);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
// destroyConstant - Remove the constant from the constant table...
//
void ConstantPointerNull::destroyConstant() {
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
NullPtrConstants->remove(this);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
destroyConstantImpl();
}
@@ -1702,13 +1790,18 @@
UndefValue *UndefValue::get(const Type *Ty) {
- return UndefValueConstants->getOrCreate(Ty, 0);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ UndefValue* result = UndefValueConstants->getOrCreate(Ty, 0);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
// destroyConstant - Remove the constant from the constant table.
//
void UndefValue::destroyConstant() {
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
UndefValueConstants->remove(this);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
destroyConstantImpl();
}
@@ -1722,16 +1815,21 @@
static ManagedStatic<StringMap<MDString*> > MDStringCache;
MDString *MDString::get(const char *StrBegin, const char *StrEnd) {
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(StrBegin,
StrEnd);
MDString *&S = Entry.getValue();
if (!S) S = new MDString(Entry.getKeyData(),
Entry.getKeyData() + Entry.getKeyLength());
+
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
return S;
}
void MDString::destroyConstant() {
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd));
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
destroyConstantImpl();
}
@@ -1756,18 +1854,40 @@
for (unsigned i = 0; i != NumVals; ++i)
ID.AddPointer(Vals[i]);
- void *InsertPoint;
- if (MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint))
+ if (llvm_is_multithreaded()) {
+ ConstantsLock->reader_acquire();
+ void *InsertPoint;
+ MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
+ ConstantsLock->reader_release();
+
+ if (!N) {
+ ConstantsLock->writer_acquire();
+ N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
+ if (!N) {
+ // InsertPoint will have been set by the FindNodeOrInsertPos call.
+ MDNode *N = new(0) MDNode(Vals, NumVals);
+ MDNodeSet->InsertNode(N, InsertPoint);
+ }
+ ConstantsLock->writer_release();
+ }
+
return N;
-
- // InsertPoint will have been set by the FindNodeOrInsertPos call.
- MDNode *N = new(0) MDNode(Vals, NumVals);
- MDNodeSet->InsertNode(N, InsertPoint);
- return N;
+ } else {
+ void *InsertPoint;
+ if (MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint))
+ return N;
+
+ // InsertPoint will have been set by the FindNodeOrInsertPos call.
+ MDNode *N = new(0) MDNode(Vals, NumVals);
+ MDNodeSet->InsertNode(N, InsertPoint);
+ return N;
+ }
}
void MDNode::destroyConstant() {
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
MDNodeSet->RemoveNode(this);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
destroyConstantImpl();
}
@@ -1934,7 +2054,11 @@
// Look up the constant in the table first to ensure uniqueness
std::vector<Constant*> argVec(1, C);
ExprMapKeyType Key(opc, argVec);
- return ExprConstants->getOrCreate(Ty, Key);
+
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ Constant* result = ExprConstants->getOrCreate(Ty, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
@@ -2194,7 +2318,10 @@
std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
ExprMapKeyType Key(Opcode, argVec);
- return ExprConstants->getOrCreate(ReqTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ Constant* result = ExprConstants->getOrCreate(ReqTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
Constant *ConstantExpr::getCompareTy(unsigned short predicate,
@@ -2305,7 +2432,10 @@
argVec[1] = V1;
argVec[2] = V2;
ExprMapKeyType Key(Instruction::Select, argVec);
- return ExprConstants->getOrCreate(ReqTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ Constant* result = ExprConstants->getOrCreate(ReqTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
@@ -2328,7 +2458,10 @@
for (unsigned i = 0; i != NumIdx; ++i)
ArgVec.push_back(cast<Constant>(Idxs[i]));
const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
- return ExprConstants->getOrCreate(ReqTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ Constant *result = ExprConstants->getOrCreate(ReqTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
@@ -2362,7 +2495,10 @@
ArgVec.push_back(RHS);
// Get the key type with both the opcode and predicate
const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
- return ExprConstants->getOrCreate(Type::Int1Ty, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ Constant* result = ExprConstants->getOrCreate(Type::Int1Ty, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
Constant *
@@ -2379,7 +2515,10 @@
ArgVec.push_back(RHS);
// Get the key type with both the opcode and predicate
const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
- return ExprConstants->getOrCreate(Type::Int1Ty, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ Constant* result = ExprConstants->getOrCreate(Type::Int1Ty, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
Constant *
@@ -2424,7 +2563,10 @@
ArgVec.push_back(RHS);
// Get the key type with both the opcode and predicate
const ExprMapKeyType Key(Instruction::VICmp, ArgVec, pred);
- return ExprConstants->getOrCreate(LHS->getType(), Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ Constant* result = ExprConstants->getOrCreate(LHS->getType(), Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
Constant *
@@ -2471,7 +2613,10 @@
ArgVec.push_back(RHS);
// Get the key type with both the opcode and predicate
const ExprMapKeyType Key(Instruction::VFCmp, ArgVec, pred);
- return ExprConstants->getOrCreate(ResultTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ Constant* result = ExprConstants->getOrCreate(ResultTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
@@ -2482,7 +2627,10 @@
std::vector<Constant*> ArgVec(1, Val);
ArgVec.push_back(Idx);
const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
- return ExprConstants->getOrCreate(ReqTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ Constant* result = ExprConstants->getOrCreate(ReqTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
@@ -2503,7 +2651,10 @@
ArgVec.push_back(Elt);
ArgVec.push_back(Idx);
const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
- return ExprConstants->getOrCreate(ReqTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ Constant* result = ExprConstants->getOrCreate(ReqTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
@@ -2526,7 +2677,10 @@
ArgVec.push_back(V2);
ArgVec.push_back(Mask);
const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
- return ExprConstants->getOrCreate(ReqTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
+ Constant* result = ExprConstants->getOrCreate(ReqTy, Key);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
+ return result;
}
Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
@@ -2609,7 +2763,9 @@
// destroyConstant - Remove the constant from the constant table...
//
void ConstantExpr::destroyConstant() {
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
ExprConstants->remove(this);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
destroyConstantImpl();
}
@@ -2673,6 +2829,7 @@
Replacement = ConstantAggregateZero::get(getType());
} else {
// Check to see if we have this array type already.
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
bool Exists;
ArrayConstantsTy::MapTy::iterator I =
ArrayConstants->InsertOrGetItem(Lookup, Exists);
@@ -2698,8 +2855,10 @@
if (getOperand(i) == From)
setOperand(i, ToC);
}
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
return;
}
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
}
// Otherwise, I do need to replace this with an existing value.
@@ -2748,6 +2907,7 @@
Replacement = ConstantAggregateZero::get(getType());
} else {
// Check to see if we have this array type already.
+ if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
bool Exists;
StructConstantsTy::MapTy::iterator I =
StructConstants->InsertOrGetItem(Lookup, Exists);
@@ -2763,8 +2923,10 @@
// Update to the new value.
setOperand(OperandToUpdate, ToC);
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
return;
}
+ if (llvm_is_multithreaded()) ConstantsLock->writer_release();
}
assert(Replacement != this && "I didn't contain From!");
More information about the llvm-commits
mailing list