Simplify a lot of code by using a R/W mutex that becomes a no-op when multithreading is disabled.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73714 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Owen Anderson 2009-06-18 19:10:19 +00:00
parent 0e4d606684
commit ee6aefcaa3

View File

@ -37,7 +37,8 @@ using namespace llvm;
// Constant Class
//===----------------------------------------------------------------------===//
ManagedStatic<sys::RWMutex> ConstantsLock;
// Becomes a no-op when multithreading is disabled.
ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
void Constant::destroyConstantImpl() {
// When a Constant is destroyed, there may be lingering
@ -300,27 +301,20 @@ ConstantInt *ConstantInt::get(const APInt& V) {
// get an existing value or the insertion position
DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
if (llvm_is_multithreaded()) {
ConstantsLock->reader_acquire();
ConstantInt *&Slot = (*IntConstants)[Key];
ConstantsLock->reader_release();
if (!Slot) {
sys::ScopedWriter Writer(&*ConstantsLock);
ConstantInt *&Slot = (*IntConstants)[Key];
sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
ConstantInt *&NewSlot = (*IntConstants)[Key];
if (!Slot) {
Slot = new ConstantInt(ITy, V);
}
NewSlot = new ConstantInt(ITy, V);
}
return Slot;
return NewSlot;
} 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);
}
}
@ -414,15 +408,14 @@ static ManagedStatic<FPMapTy> FPConstants;
ConstantFP *ConstantFP::get(const APFloat &V) {
DenseMapAPFloatKeyInfo::KeyTy Key(V);
if (llvm_is_multithreaded()) {
ConstantsLock->reader_acquire();
ConstantFP *&Slot = (*FPConstants)[Key];
ConstantsLock->reader_release();
if (!Slot) {
sys::ScopedWriter Writer(&*ConstantsLock);
Slot = (*FPConstants)[Key];
if (!Slot) {
sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
ConstantFP *&NewSlot = (*FPConstants)[Key];
if (!NewSlot) {
const Type *Ty;
if (&V.getSemantics() == &APFloat::IEEEsingle)
Ty = Type::FloatTy;
@ -437,33 +430,13 @@ ConstantFP *ConstantFP::get(const APFloat &V) {
"Unknown FP format");
Ty = Type::PPC_FP128Ty;
}
Slot = new ConstantFP(Ty, V);
NewSlot = new ConstantFP(Ty, V);
}
return NewSlot;
}
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);
}
}
/// get() - This returns a constant fp for the specified value in the
@ -1255,7 +1228,6 @@ public:
/// necessary.
ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
MapKey Lookup(Ty, V);
if (llvm_is_multithreaded()) {
ConstantClass* Result = 0;
ConstantsLock->reader_acquire();
@ -1266,7 +1238,7 @@ public:
ConstantsLock->reader_release();
if (!Result) {
sys::ScopedWriter Writer(&*ConstantsLock);
sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
I = Map.find(Lookup);
// Is it in the map?
if (I != Map.end())
@ -1278,19 +1250,10 @@ public:
}
return Result;
} else {
typename MapTy::iterator I = Map.find(Lookup);
// Is it in the map?
if (I != Map.end())
return static_cast<ConstantClass *>(I->second);
// If no preexisting value, create one now...
return Create(Ty, V, I);
}
}
void remove(ConstantClass *CP) {
if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
ConstantsLock->writer_acquire();
typename MapTy::iterator I = FindExistingElement(CP);
assert(I != Map.end() && "Constant not found in constant table!");
assert(I->second == CP && "Didn't find correct element?");
@ -1339,7 +1302,7 @@ public:
Map.erase(I);
if (llvm_is_multithreaded()) ConstantsLock->writer_release();
ConstantsLock->writer_release();
}
@ -1377,7 +1340,7 @@ public:
}
void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
if (llvm_is_multithreaded()) ConstantsLock->writer_acquire();
ConstantsLock->writer_acquire();
typename AbstractTypeMapTy::iterator I =
AbstractTypeMap.find(cast<Type>(OldTy));
@ -1396,16 +1359,13 @@ public:
I = AbstractTypeMap.find(cast<Type>(OldTy));
} while (I != AbstractTypeMap.end());
if (llvm_is_multithreaded()) ConstantsLock->writer_release();
ConstantsLock->writer_release();
}
// If the type became concrete without being refined to any other existing
// type, we just remove ourselves from the ATU list.
void typeBecameConcrete(const DerivedType *AbsTy) {
if (llvm_is_multithreaded()) {
sys::ScopedWriter Writer(&*ConstantsLock);
AbsTy->removeAbstractTypeUser(this);
} else
sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
AbsTy->removeAbstractTypeUser(this);
}
@ -1708,11 +1668,7 @@ Constant *ConstantVector::get(const std::vector<Constant*> &V) {
// destroyConstant - Remove the constant from the constant table...
//
void ConstantVector::destroyConstant() {
if (llvm_is_multithreaded()) {
sys::ScopedWriter Write(&*ConstantsLock);
VectorConstants->remove(this);
} else
sys::SmartScopedWriter<true> Write(&*ConstantsLock);
VectorConstants->remove(this);
destroyConstantImpl();
}
@ -1784,10 +1740,7 @@ ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
// destroyConstant - Remove the constant from the constant table...
//
void ConstantPointerNull::destroyConstant() {
if (llvm_is_multithreaded()) {
sys::ScopedWriter Writer(&*ConstantsLock);
NullPtrConstants->remove(this);
} else
sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
NullPtrConstants->remove(this);
destroyConstantImpl();
}
@ -1847,8 +1800,7 @@ MDString::MDString(const char *begin, const char *end)
static ManagedStatic<StringMap<MDString*> > MDStringCache;
MDString *MDString::get(const char *StrBegin, const char *StrEnd) {
if (llvm_is_multithreaded()) {
sys::ScopedWriter Writer(&*ConstantsLock);
sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
StrBegin, StrEnd);
MDString *&S = Entry.getValue();
@ -1856,24 +1808,11 @@ MDString *MDString::get(const char *StrBegin, const char *StrEnd) {
Entry.getKeyData() + Entry.getKeyLength());
return S;
} else {
StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
StrBegin, StrEnd);
MDString *&S = Entry.getValue();
if (!S) S = new MDString(Entry.getKeyData(),
Entry.getKeyData() + Entry.getKeyLength());
return S;
}
}
void MDString::destroyConstant() {
if (llvm_is_multithreaded()) {
sys::ScopedWriter Writer(&*ConstantsLock);
sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd));
} else
MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd));
destroyConstantImpl();
}
@ -1898,40 +1837,25 @@ MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) {
for (unsigned i = 0; i != NumVals; ++i)
ID.AddPointer(Vals[i]);
if (llvm_is_multithreaded()) {
ConstantsLock->reader_acquire();
void *InsertPoint;
MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
ConstantsLock->reader_release();
if (!N) {
sys::ScopedWriter Writer(&*ConstantsLock);
sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
if (!N) {
// InsertPoint will have been set by the FindNodeOrInsertPos call.
MDNode *N = new(0) MDNode(Vals, NumVals);
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()) {
sys::ScopedWriter Writer(&*ConstantsLock);
MDNodeSet->RemoveNode(this);
} else
sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
MDNodeSet->RemoveNode(this);
destroyConstantImpl();
@ -2797,12 +2721,8 @@ Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
// destroyConstant - Remove the constant from the constant table...
//
void ConstantExpr::destroyConstant() {
if (llvm_is_multithreaded()) {
sys::ScopedWriter Writer(&*ConstantsLock);
sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
ExprConstants->remove(this);
} else
ExprConstants->remove(this);
destroyConstantImpl();
}
@ -2866,7 +2786,7 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Replacement = ConstantAggregateZero::get(getType());
} else {
// Check to see if we have this array type already.
sys::ScopedWriter Writer(&*ConstantsLock);
sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
bool Exists;
ArrayConstantsTy::MapTy::iterator I =
ArrayConstants->InsertOrGetItem(Lookup, Exists);
@ -2942,7 +2862,7 @@ void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Replacement = ConstantAggregateZero::get(getType());
} else {
// Check to see if we have this array type already.
sys::ScopedWriter Writer(&*ConstantsLock);
sys::SmartScopedWriter<true> Writer(&*ConstantsLock);
bool Exists;
StructConstantsTy::MapTy::iterator I =
StructConstants->InsertOrGetItem(Lookup, Exists);