From: Jeffrey Yasskin Date: Sun, 21 Mar 2010 20:37:19 +0000 (+0000) Subject: Memoize InlineAsms into the LLVMContext and delete them on shutdown. X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=bf48a9b6db111fc14a8faef1adefbce5d807aaef;p=oota-llvm.git Memoize InlineAsms into the LLVMContext and delete them on shutdown. Fixes PR803. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@99143 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/InlineAsm.h b/include/llvm/InlineAsm.h index 4490ce5a546..2ac0fcab796 100644 --- a/include/llvm/InlineAsm.h +++ b/include/llvm/InlineAsm.h @@ -24,8 +24,17 @@ namespace llvm { class PointerType; class FunctionType; class Module; +struct InlineAsmKeyType; +template +class ConstantUniqueMap; +template +struct ConstantCreator; class InlineAsm : public Value { + friend struct ConstantCreator; + friend class ConstantUniqueMap; + InlineAsm(const InlineAsm &); // do not implement void operator=(const InlineAsm&); // do not implement @@ -33,10 +42,14 @@ class InlineAsm : public Value { bool HasSideEffects; bool IsAlignStack; - InlineAsm(const FunctionType *Ty, StringRef AsmString, - StringRef Constraints, bool hasSideEffects, - bool isAlignStack = false); + InlineAsm(const PointerType *Ty, const std::string &AsmString, + const std::string &Constraints, bool hasSideEffects, + bool isAlignStack); virtual ~InlineAsm(); + + /// When the ConstantUniqueMap merges two types and makes two InlineAsms + /// identical, it destroys one of them with this method. + void destroyConstant(); public: /// InlineAsm::get - Return the specified uniqued inline asm string. diff --git a/lib/VMCore/ConstantsContext.h b/lib/VMCore/ConstantsContext.h index c798ba2664c..560e6c4974f 100644 --- a/lib/VMCore/ConstantsContext.h +++ b/lib/VMCore/ConstantsContext.h @@ -15,6 +15,7 @@ #ifndef LLVM_CONSTANTSCONTEXT_H #define LLVM_CONSTANTSCONTEXT_H +#include "llvm/InlineAsm.h" #include "llvm/Instructions.h" #include "llvm/Operator.h" #include "llvm/Support/Debug.h" @@ -327,6 +328,39 @@ struct ExprMapKeyType { } }; +struct InlineAsmKeyType { + InlineAsmKeyType(StringRef AsmString, + StringRef Constraints, bool hasSideEffects, + bool isAlignStack) + : asm_string(AsmString), constraints(Constraints), + has_side_effects(hasSideEffects), is_align_stack(isAlignStack) {} + std::string asm_string; + std::string constraints; + bool has_side_effects; + bool is_align_stack; + bool operator==(const InlineAsmKeyType& that) const { + return this->asm_string == that.asm_string && + this->constraints == that.constraints && + this->has_side_effects == that.has_side_effects && + this->is_align_stack == that.is_align_stack; + } + bool operator<(const InlineAsmKeyType& that) const { + if (this->asm_string != that.asm_string) + return this->asm_string < that.asm_string; + if (this->constraints != that.constraints) + return this->constraints < that.constraints; + if (this->has_side_effects != that.has_side_effects) + return this->has_side_effects < that.has_side_effects; + if (this->is_align_stack != that.is_align_stack) + return this->is_align_stack < that.is_align_stack; + return false; + } + + bool operator!=(const InlineAsmKeyType& that) const { + return !(*this == that); + } +}; + // The number of operands for each ConstantCreator::create method is // determined by the ConstantTraits template. // ConstantCreator - A class that is used to create constants by @@ -517,6 +551,23 @@ struct ConstantKeyData { } }; +template<> +struct ConstantCreator { + static InlineAsm *create(const PointerType *Ty, const InlineAsmKeyType &Key) { + return new InlineAsm(Ty, Key.asm_string, Key.constraints, + Key.has_side_effects, Key.is_align_stack); + } +}; + +template<> +struct ConstantKeyData { + typedef InlineAsmKeyType ValType; + static ValType getValType(InlineAsm *Asm) { + return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(), + Asm->hasSideEffects(), Asm->isAlignStack()); + } +}; + template class ConstantUniqueMap : public AbstractTypeUser { diff --git a/lib/VMCore/InlineAsm.cpp b/lib/VMCore/InlineAsm.cpp index 6355834880b..0d2eca9c3de 100644 --- a/lib/VMCore/InlineAsm.cpp +++ b/lib/VMCore/InlineAsm.cpp @@ -12,6 +12,8 @@ //===----------------------------------------------------------------------===// #include "llvm/InlineAsm.h" +#include "ConstantsContext.h" +#include "LLVMContextImpl.h" #include "llvm/DerivedTypes.h" #include #include @@ -23,28 +25,29 @@ InlineAsm::~InlineAsm() { } -// NOTE: when memoizing the function type, we have to be careful to handle the -// case when the type gets refined. - InlineAsm *InlineAsm::get(const FunctionType *Ty, StringRef AsmString, StringRef Constraints, bool hasSideEffects, bool isAlignStack) { - // FIXME: memoize! - return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects, - isAlignStack); + InlineAsmKeyType Key(AsmString, Constraints, hasSideEffects, isAlignStack); + LLVMContextImpl *pImpl = Ty->getContext().pImpl; + return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(Ty), Key); } -InlineAsm::InlineAsm(const FunctionType *Ty, StringRef asmString, - StringRef constraints, bool hasSideEffects, +InlineAsm::InlineAsm(const PointerType *Ty, const std::string &asmString, + const std::string &constraints, bool hasSideEffects, bool isAlignStack) - : Value(PointerType::getUnqual(Ty), - Value::InlineAsmVal), + : Value(Ty, Value::InlineAsmVal), AsmString(asmString), Constraints(constraints), HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack) { // Do various checks on the constraint string and type. - assert(Verify(Ty, constraints) && "Function type not legal for constraints!"); + assert(Verify(getFunctionType(), constraints) && + "Function type not legal for constraints!"); +} + +void InlineAsm::destroyConstant() { + delete this; } const FunctionType *InlineAsm::getFunctionType() const { diff --git a/lib/VMCore/LLVMContextImpl.h b/lib/VMCore/LLVMContextImpl.h index 9978f40ed0d..8d5a924ca26 100644 --- a/lib/VMCore/LLVMContextImpl.h +++ b/lib/VMCore/LLVMContextImpl.h @@ -135,6 +135,8 @@ public: DenseMap , BlockAddress*> BlockAddresses; ConstantUniqueMap ExprConstants; + + ConstantUniqueMap InlineAsms; ConstantInt *TheTrueVal; ConstantInt *TheFalseVal; @@ -224,6 +226,7 @@ public: AggZeroConstants.freeConstants(); NullPtrConstants.freeConstants(); UndefValueConstants.freeConstants(); + InlineAsms.freeConstants(); for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end(); I != E; ++I) { if (I->second->use_empty())