From 3569d3c5ebfff8b9b20c47e7eb1ddf508e6e9bbf Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Fri, 8 Jan 2016 02:28:20 +0000 Subject: [PATCH] Remove static global GCNames from Function.cpp and move it to the Context This remove the need for locking when deleting a function. Differential Revision: http://reviews.llvm.org/D15988 From: Mehdi Amini git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@257139 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/Function.h | 11 +++-- include/llvm/IR/LLVMContext.h | 11 +++++ lib/IR/Core.cpp | 2 +- lib/IR/Function.cpp | 44 ++++--------------- lib/IR/LLVMContext.cpp | 16 +++++++ lib/IR/LLVMContextImpl.h | 7 +++ lib/Transforms/Scalar/PlaceSafepoints.cpp | 2 +- .../Scalar/RewriteStatepointsForGC.cpp | 2 +- 8 files changed, 53 insertions(+), 42 deletions(-) diff --git a/include/llvm/IR/Function.h b/include/llvm/IR/Function.h index 2a983930bf4..4f64caeade2 100644 --- a/include/llvm/IR/Function.h +++ b/include/llvm/IR/Function.h @@ -66,7 +66,8 @@ private: * bit 2 : HasPrologueData * bit 3 : HasPersonalityFn * bits 4-13 : CallingConvention - * bits 14-15 : [reserved] + * bits 14 : HasGC + * bits 15 : [reserved] */ /// Bits from GlobalObject::GlobalObjectSubclassData. @@ -220,9 +221,11 @@ public: /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm /// to use during code generation. - bool hasGC() const; - const char *getGC() const; - void setGC(const char *Str); + bool hasGC() const { + return getSubclassDataFromValue() & (1<<14); + } + const std::string &getGC() const; + void setGC(const std::string Str); void clearGC(); /// @brief adds the attribute to the list of attributes. diff --git a/include/llvm/IR/LLVMContext.h b/include/llvm/IR/LLVMContext.h index c546fc3d1ee..56aa3010d92 100644 --- a/include/llvm/IR/LLVMContext.h +++ b/include/llvm/IR/LLVMContext.h @@ -93,6 +93,17 @@ public: /// tag registered with an LLVMContext has an unique ID. uint32_t getOperandBundleTagID(StringRef Tag) const; + + /// Define the GC for a function + void setGC(const Function &Fn, std::string GCName); + + /// Return the GC for a function + const std::string &getGC(const Function &Fn); + + /// Remove the GC for a function + void deleteGC(const Function &Fn); + + typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context, unsigned LocCookie); diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp index 7f39c8085a6..591dafa22a4 100644 --- a/lib/IR/Core.cpp +++ b/lib/IR/Core.cpp @@ -1722,7 +1722,7 @@ void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) { const char *LLVMGetGC(LLVMValueRef Fn) { Function *F = unwrap(Fn); - return F->hasGC()? F->getGC() : nullptr; + return F->hasGC()? F->getGC().c_str() : nullptr; } void LLVMSetGC(LLVMValueRef Fn, const char *GC) { diff --git a/lib/IR/Function.cpp b/lib/IR/Function.cpp index cfb40b19c73..cfdfc40cd8a 100644 --- a/lib/IR/Function.cpp +++ b/lib/IR/Function.cpp @@ -366,47 +366,21 @@ void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { setAttributes(PAL); } -// Maintain the GC name for each function in an on-the-side table. This saves -// allocating an additional word in Function for programs which do not use GC -// (i.e., most programs) at the cost of increased overhead for clients which do -// use GC. -static DenseMap *GCNames; -static StringPool *GCNamePool; -static ManagedStatic > GCLock; - -bool Function::hasGC() const { - sys::SmartScopedReader Reader(*GCLock); - return GCNames && GCNames->count(this); -} - -const char *Function::getGC() const { +const std::string &Function::getGC() const { assert(hasGC() && "Function has no collector"); - sys::SmartScopedReader Reader(*GCLock); - return *(*GCNames)[this]; + return getContext().getGC(*this); } -void Function::setGC(const char *Str) { - sys::SmartScopedWriter Writer(*GCLock); - if (!GCNamePool) - GCNamePool = new StringPool(); - if (!GCNames) - GCNames = new DenseMap(); - (*GCNames)[this] = GCNamePool->intern(Str); +void Function::setGC(const std::string Str) { + setValueSubclassDataBit(14, !Str.empty()); + getContext().setGC(*this, std::move(Str)); } void Function::clearGC() { - sys::SmartScopedWriter Writer(*GCLock); - if (GCNames) { - GCNames->erase(this); - if (GCNames->empty()) { - delete GCNames; - GCNames = nullptr; - if (GCNamePool->empty()) { - delete GCNamePool; - GCNamePool = nullptr; - } - } - } + if (!hasGC()) + return; + getContext().deleteGC(*this); + setValueSubclassDataBit(14, false); } /// Copy all additional attributes (those not needed to create a Function) from diff --git a/lib/IR/LLVMContext.cpp b/lib/IR/LLVMContext.cpp index 8848bcb7147..48b53b0f532 100644 --- a/lib/IR/LLVMContext.cpp +++ b/lib/IR/LLVMContext.cpp @@ -304,3 +304,19 @@ void LLVMContext::getOperandBundleTags(SmallVectorImpl &Tags) const { uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const { return pImpl->getOperandBundleTagID(Tag); } + +void LLVMContext::setGC(const Function &Fn, std::string GCName) { + auto It = pImpl->GCNames.find(&Fn); + + if (It == pImpl->GCNames.end()) { + pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName))); + return; + } + It->second = std::move(GCName); +} +const std::string &LLVMContext::getGC(const Function &Fn) { + return pImpl->GCNames[&Fn]; +} +void LLVMContext::deleteGC(const Function &Fn) { + pImpl->GCNames.erase(&Fn); +} diff --git a/lib/IR/LLVMContextImpl.h b/lib/IR/LLVMContextImpl.h index a24114d0a0a..d42047d4e77 100644 --- a/lib/IR/LLVMContextImpl.h +++ b/lib/IR/LLVMContextImpl.h @@ -1027,6 +1027,13 @@ public: void getOperandBundleTags(SmallVectorImpl &Tags) const; uint32_t getOperandBundleTagID(StringRef Tag) const; + /// Maintain the GC name for each function. + /// + /// This saves allocating an additional word in Function for programs which + /// do not use GC (i.e., most programs) at the cost of increased overhead for + /// clients which do use GC. + DenseMap GCNames; + LLVMContextImpl(LLVMContext &C); ~LLVMContextImpl(); diff --git a/lib/Transforms/Scalar/PlaceSafepoints.cpp b/lib/Transforms/Scalar/PlaceSafepoints.cpp index 28c610c2486..b56b3559912 100644 --- a/lib/Transforms/Scalar/PlaceSafepoints.cpp +++ b/lib/Transforms/Scalar/PlaceSafepoints.cpp @@ -499,7 +499,7 @@ static bool isGCSafepointPoll(Function &F) { static bool shouldRewriteFunction(Function &F) { // TODO: This should check the GCStrategy if (F.hasGC()) { - const char *FunctionGCName = F.getGC(); + const auto &FunctionGCName = F.getGC(); const StringRef StatepointExampleName("statepoint-example"); const StringRef CoreCLRName("coreclr"); return (StatepointExampleName == FunctionGCName) || diff --git a/lib/Transforms/Scalar/RewriteStatepointsForGC.cpp b/lib/Transforms/Scalar/RewriteStatepointsForGC.cpp index b21d037bf7a..94768a255ca 100644 --- a/lib/Transforms/Scalar/RewriteStatepointsForGC.cpp +++ b/lib/Transforms/Scalar/RewriteStatepointsForGC.cpp @@ -2558,7 +2558,7 @@ void RewriteStatepointsForGC::stripNonValidAttributesFromBody(Function &F) { static bool shouldRewriteStatepointsIn(Function &F) { // TODO: This should check the GCStrategy if (F.hasGC()) { - const char *FunctionGCName = F.getGC(); + const auto &FunctionGCName = F.getGC(); const StringRef StatepointExampleName("statepoint-example"); const StringRef CoreCLRName("coreclr"); return (StatepointExampleName == FunctionGCName) || -- 2.34.1