Actually privatize a IntegerTypes, and fix a few bugs exposed by this.
authorOwen Anderson <resistor@mac.com>
Thu, 13 Aug 2009 23:27:32 +0000 (23:27 +0000)
committerOwen Anderson <resistor@mac.com>
Thu, 13 Aug 2009 23:27:32 +0000 (23:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78955 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/DerivedTypes.h
include/llvm/Type.h
lib/Analysis/PointerTracking.cpp
lib/AsmParser/LLParser.cpp
lib/Bitcode/Reader/BitcodeReader.cpp
lib/CodeGen/ShadowStackGC.cpp
lib/Transforms/Utils/LowerInvoke.cpp
lib/VMCore/Core.cpp
lib/VMCore/LLVMContext.cpp
lib/VMCore/LLVMContextImpl.h
lib/VMCore/Type.cpp

index a8603acfbe2b1eb8d65450e8927bbcf974c50b54..fb51430b481e1db0beea27f83f77f8557308b88b 100644 (file)
@@ -37,7 +37,7 @@ class DerivedType : public Type {
   friend class Type;
 
 protected:
-  explicit DerivedType(TypeID id) : Type(id) {}
+  explicit DerivedType(LLVMContext &C, TypeID id) : Type(C, id) {}
 
   /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
   /// that the current type has transitioned from being abstract to being
@@ -83,8 +83,11 @@ public:
 /// Int64Ty.
 /// @brief Integer representation type
 class IntegerType : public DerivedType {
+  friend class LLVMContextImpl;
+  
 protected:
-  explicit IntegerType(unsigned NumBits) : DerivedType(IntegerTyID) {
+  explicit IntegerType(LLVMContext &C, unsigned NumBits) : 
+      DerivedType(C, IntegerTyID) {
     setSubclassData(NumBits);
   }
   friend class TypeMap<IntegerValType, IntegerType>;
@@ -208,7 +211,8 @@ public:
 /// and VectorType
 class CompositeType : public DerivedType {
 protected:
-  inline explicit CompositeType(TypeID id) : DerivedType(id) { }
+  inline explicit CompositeType(LLVMContext &C, TypeID id) :
+    DerivedType(C, id) { }
 public:
 
   /// getTypeAtIndex - Given an index value into the type, return the type of
@@ -236,7 +240,8 @@ class StructType : public CompositeType {
   friend class TypeMap<StructValType, StructType>;
   StructType(const StructType &);                   // Do not implement
   const StructType &operator=(const StructType &);  // Do not implement
-  StructType(const std::vector<const Type*> &Types, bool isPacked);
+  StructType(LLVMContext &C,
+             const std::vector<const Type*> &Types, bool isPacked);
 public:
   /// StructType::get - This static method is the primary way to create a
   /// StructType.
@@ -313,7 +318,7 @@ class SequentialType : public CompositeType {
   SequentialType* this_() { return this; }
 protected:
   SequentialType(TypeID TID, const Type *ElType)
-    : CompositeType(TID), ContainedType(ElType, this_()) {
+    : CompositeType(ElType->getContext(), TID), ContainedType(ElType, this_()) {
     ContainedTys = &ContainedType;
     NumContainedTys = 1;
   }
@@ -493,12 +498,12 @@ public:
 class OpaqueType : public DerivedType {
   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
-  OpaqueType();
+  OpaqueType(LLVMContext &C);
 public:
   /// OpaqueType::get - Static factory method for the OpaqueType class...
   ///
-  static OpaqueType *get() {
-    return new OpaqueType();           // All opaque types are distinct
+  static OpaqueType *get(LLVMContext &C) {
+    return new OpaqueType(C);           // All opaque types are distinct
   }
 
   // Implement support for type inquiry through isa, cast, and dyn_cast:
index 05a4e26c8bb4382db9cf3ed26ad6f9f5ac9d9f8e..b7516323c58a5abea9ed1b37f93e9f33770b28ff 100644 (file)
@@ -109,6 +109,7 @@ private:
 
   /// Context - This refers to the LLVMContext in which this type was uniqued.
   LLVMContext &Context;
+  friend class LLVMContextImpl;
 
   const Type *getForwardedTypeInternal() const;
 
@@ -117,8 +118,9 @@ private:
   void destroy() const; // const is a lie, this does "delete this"!
 
 protected:
-  explicit Type(TypeID id) : ID(id), Abstract(false), SubclassData(0),
-                             RefCount(0), Context(getGlobalContext()),
+  explicit Type(LLVMContext &C, TypeID id) :
+                             ID(id), Abstract(false), SubclassData(0),
+                             RefCount(0), Context(C),
                              ForwardType(0), NumContainedTys(0),
                              ContainedTys(0) {}
   virtual ~Type() {
index f2154f73889249bdf26a22f48631265f853712dd..d28e58e1013554228f66aaf6f60c9ca87a3dea9e 100644 (file)
@@ -104,12 +104,12 @@ const SCEV *PointerTracking::computeAllocationCount(Value *P,
       Constant *C = GV->getInitializer();
       if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
         Ty = ATy->getElementType();
-        return SE->getConstant(Type::getInt32Ty(Ty->getContext()),
+        return SE->getConstant(Type::getInt32Ty(P->getContext()),
                                ATy->getNumElements());
       }
     }
     Ty = GV->getType();
-    return SE->getConstant(Type::getInt32Ty(Ty->getContext()), 1);
+    return SE->getConstant(Type::getInt32Ty(P->getContext()), 1);
     //TODO: implement more tracking for globals
   }
 
@@ -118,13 +118,13 @@ const SCEV *PointerTracking::computeAllocationCount(Value *P,
     Function *F = dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts());
     const Loop *L = LI->getLoopFor(CI->getParent());
     if (F == callocFunc) {
-      Ty = Type::getInt8Ty(Ty->getContext());
+      Ty = Type::getInt8Ty(P->getContext());
       // calloc allocates arg0*arg1 bytes.
       return SE->getSCEVAtScope(SE->getMulExpr(SE->getSCEV(CS.getArgument(0)),
                                                SE->getSCEV(CS.getArgument(1))),
                                 L);
     } else if (F == reallocFunc) {
-      Ty = Type::getInt8Ty(Ty->getContext());
+      Ty = Type::getInt8Ty(P->getContext());
       // realloc allocates arg1 bytes.
       return SE->getSCEVAtScope(CS.getArgument(1), L);
     }
index c3fd31ed4ce067e6abe402024e3c9f0b14d21698..ec7bc650c45e5b5cdf7d21ff18f7cddec09e8104 100644 (file)
@@ -1150,7 +1150,7 @@ bool LLParser::ParseTypeRec(PATypeHolder &Result) {
     break;
   case lltok::kw_opaque:
     // TypeRec ::= 'opaque'
-    Result = OpaqueType::get();
+    Result = OpaqueType::get(Context);
     Lex.Lex();
     break;
   case lltok::lbrace:
@@ -1180,7 +1180,7 @@ bool LLParser::ParseTypeRec(PATypeHolder &Result) {
     if (const Type *T = M->getTypeByName(Lex.getStrVal())) {
       Result = T;
     } else {
-      Result = OpaqueType::get();
+      Result = OpaqueType::get(Context);
       ForwardRefTypes.insert(std::make_pair(Lex.getStrVal(),
                                             std::make_pair(Result,
                                                            Lex.getLoc())));
@@ -1199,7 +1199,7 @@ bool LLParser::ParseTypeRec(PATypeHolder &Result) {
       if (I != ForwardRefTypeIDs.end())
         Result = I->second.first;
       else {
-        Result = OpaqueType::get();
+        Result = OpaqueType::get(Context);
         ForwardRefTypeIDs.insert(std::make_pair(Lex.getUIntVal(),
                                                 std::make_pair(Result,
                                                                Lex.getLoc())));
@@ -1212,7 +1212,7 @@ bool LLParser::ParseTypeRec(PATypeHolder &Result) {
     Lex.Lex();
     unsigned Val;
     if (ParseUInt32(Val)) return true;
-    OpaqueType *OT = OpaqueType::get(); //Use temporary placeholder.
+    OpaqueType *OT = OpaqueType::get(Context); //Use temporary placeholder.
     UpRefs.push_back(UpRefRecord(Lex.getLoc(), Val, OT));
     Result = OT;
     break;
index 168f32951d56bba276ad31bc1fe509dd7ef48d89..a1e6099b9f0ec5ff0afc0ce09579788e030368fe 100644 (file)
@@ -358,7 +358,7 @@ const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) {
   // The type table allows forward references.  Push as many Opaque types as
   // needed to get up to ID.
   while (TypeList.size() <= ID)
-    TypeList.push_back(OpaqueType::get());
+    TypeList.push_back(OpaqueType::get(Context));
   return TypeList.back().get();
 }
 
@@ -597,7 +597,7 @@ bool BitcodeReader::ParseTypeTable() {
     
     if (NumRecords == TypeList.size()) {
       // If this is a new type slot, just append it.
-      TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get());
+      TypeList.push_back(ResultTy ? ResultTy : OpaqueType::get(Context));
       ++NumRecords;
     } else if (ResultTy == 0) {
       // Otherwise, this was forward referenced, so an opaque type was created,
index 5b4cc7fe2696a6b43ef8cf00ca9e82886bacd77e..18c49363fee69ad8d08d0a61315f0bb97069c410 100644 (file)
@@ -279,7 +279,7 @@ bool ShadowStackGC::initializeCustomLowering(Module &M) {
   //   FrameMap *Map;          // Pointer to constant FrameMap.
   //   void *Roots[];          // Stack roots (in-place array, so we pretend).
   // };
-  OpaqueType *RecursiveTy = OpaqueType::get();
+  OpaqueType *RecursiveTy = OpaqueType::get(M.getContext());
 
   EltTys.clear();
   EltTys.push_back(PointerType::getUnqual(RecursiveTy));
index b18a2307e1d452d04839ba964bb95c0f045e924b..4ecf6d7ba2604de466159f46f244059e8912c377 100644 (file)
@@ -127,7 +127,7 @@ bool LowerInvoke::doInitialization(Module &M) {
     { // The type is recursive, so use a type holder.
       std::vector<const Type*> Elements;
       Elements.push_back(JmpBufTy);
-      OpaqueType *OT = OpaqueType::get();
+      OpaqueType *OT = OpaqueType::get(M.getContext());
       Elements.push_back(PointerType::getUnqual(OT));
       PATypeHolder JBLType(StructType::get(M.getContext(), Elements));
       OT->refineAbstractTypeTo(JBLType.get());  // Complete the cycle.
index b2715d9d21a33d5a5ec8d9172d1f216065c655e5..e8f47fdf2e4f2eadb2709259c98ae39412232573 100644 (file)
@@ -293,7 +293,7 @@ LLVMTypeRef LLVMLabelType(void) {
 }
 
 LLVMTypeRef LLVMOpaqueType(void) {
-  return wrap(OpaqueType::get());
+  return wrap(OpaqueType::get(getGlobalContext()));
 }
 
 /*--.. Operations on type handles ..........................................--*/
index aa9dc3671a089b10ff15c43a8847949c197b4905..95ceeaee813f5aa2878aaf99862f89f6ed972d74 100644 (file)
@@ -27,7 +27,7 @@ LLVMContext& llvm::getGlobalContext() {
   return *GlobalContext;
 }
 
-LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
+LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { }
 LLVMContext::~LLVMContext() { delete pImpl; }
 
 GetElementPtrConstantExpr::GetElementPtrConstantExpr
index 30a7fc4af930d6473c3963716d1d21d48e9c78c6..9c48fbe73019f8499c3c5904a44c5176aeb069e0 100644 (file)
@@ -137,8 +137,60 @@ public:
   TypeMap<PointerValType, PointerType> PointerTypes;
   TypeMap<FunctionValType, FunctionType> FunctionTypes;
   TypeMap<StructValType, StructType> StructTypes;
+  TypeMap<IntegerValType, IntegerType> IntegerTypes;
   
-  LLVMContextImpl() : TheTrueVal(0), TheFalseVal(0) { }
+  const Type *VoidTy;
+  const Type *LabelTy;
+  const Type *FloatTy;
+  const Type *DoubleTy;
+  const Type *MetadataTy;
+  const Type *X86_FP80Ty;
+  const Type *FP128Ty;
+  const Type *PPC_FP128Ty;
+  
+  const IntegerType *Int1Ty;
+  const IntegerType *Int8Ty;
+  const IntegerType *Int16Ty;
+  const IntegerType *Int32Ty;
+  const IntegerType *Int64Ty;
+  
+  LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
+    VoidTy(new Type(C, Type::VoidTyID)),
+    LabelTy(new Type(C, Type::LabelTyID)),
+    FloatTy(new Type(C, Type::FloatTyID)),
+    DoubleTy(new Type(C, Type::DoubleTyID)),
+    MetadataTy(new Type(C, Type::MetadataTyID)),
+    X86_FP80Ty(new Type(C, Type::X86_FP80TyID)),
+    FP128Ty(new Type(C, Type::FP128TyID)),
+    PPC_FP128Ty(new Type(C, Type::PPC_FP128TyID)),
+    Int1Ty(new IntegerType(C, 1)),
+    Int8Ty(new IntegerType(C, 8)),
+    Int16Ty(new IntegerType(C, 16)),
+    Int32Ty(new IntegerType(C, 32)),
+    Int64Ty(new IntegerType(C, 64)) { }
+  
+  ~LLVMContextImpl() {
+    // In principle, we should delete the member types here.  However,
+    // this causes destruction order issues with the types in the TypeMaps.
+    // For now, just leak this, which is at least not a regression from the
+    // previous behavior, though still undesirable.
+#if 0
+    delete VoidTy;
+    delete LabelTy;
+    delete FloatTy;
+    delete DoubleTy;
+    delete MetadataTy;
+    delete X86_FP80Ty;
+    delete FP128Ty;
+    delete PPC_FP128Ty;
+    
+    delete Int1Ty;
+    delete Int8Ty;
+    delete Int16Ty;
+    delete Int32Ty;
+    delete Int64Ty;
+#endif
+  }
 };
 
 }
index 3072e838b500cb2b593bfeef8601451dbe0f83df..93433a7d364bcb61cd8059a2b04f07b279bd9e1c 100644 (file)
@@ -315,75 +315,56 @@ const Type *StructType::getTypeAtIndex(unsigned Idx) const {
 //                          Primitive 'Type' data
 //===----------------------------------------------------------------------===//
 
-namespace {
-  struct BuiltinIntegerType : public IntegerType {
-    explicit BuiltinIntegerType(unsigned W) : IntegerType(W) {}
-  };
-}
-
 const Type *Type::getVoidTy(LLVMContext &C) {
-  static const Type *VoidTy       = new Type(Type::VoidTyID);
-  return VoidTy;
+  return C.pImpl->VoidTy;
 }
 
 const Type *Type::getLabelTy(LLVMContext &C) {
-  static const Type *LabelTy      = new Type(Type::LabelTyID);
-  return LabelTy;
+  return C.pImpl->LabelTy;
 }
 
 const Type *Type::getFloatTy(LLVMContext &C) {
-  static const Type *FloatTy      = new Type(Type::FloatTyID);
-  return FloatTy;
+  return C.pImpl->FloatTy;
 }
 
 const Type *Type::getDoubleTy(LLVMContext &C) {
-  static const Type *DoubleTy      = new Type(Type::DoubleTyID);
-  return DoubleTy;
+  return C.pImpl->DoubleTy;
 }
 
 const Type *Type::getMetadataTy(LLVMContext &C) {
-  static const Type *MetadataTy   = new Type(Type::MetadataTyID);
-  return MetadataTy;
+  return C.pImpl->MetadataTy;
 }
 
 const Type *Type::getX86_FP80Ty(LLVMContext &C) {
-  static const Type *X86_FP80Ty   = new Type(Type::X86_FP80TyID);
-  return X86_FP80Ty;
+  return C.pImpl->X86_FP80Ty;
 }
 
 const Type *Type::getFP128Ty(LLVMContext &C) {
-  static const Type *FP128Ty      = new Type(Type::FP128TyID);
-  return FP128Ty;
+  return C.pImpl->FP128Ty;
 }
 
 const Type *Type::getPPC_FP128Ty(LLVMContext &C) {
-  static const Type *PPC_FP128Ty  = new Type(Type::PPC_FP128TyID);
-  return PPC_FP128Ty;
+  return C.pImpl->PPC_FP128Ty;
 }
 
 const IntegerType *Type::getInt1Ty(LLVMContext &C) {
-  static const IntegerType *Int1Ty  = new BuiltinIntegerType(1);
-  return Int1Ty;
+  return C.pImpl->Int1Ty;
 }
 
 const IntegerType *Type::getInt8Ty(LLVMContext &C) {
-  static const IntegerType *Int8Ty  = new BuiltinIntegerType(8);
-  return Int8Ty;
+  return C.pImpl->Int8Ty;
 }
 
 const IntegerType *Type::getInt16Ty(LLVMContext &C) {
-  static const IntegerType *Int16Ty = new BuiltinIntegerType(16);
-  return Int16Ty;
+  return C.pImpl->Int16Ty;
 }
 
 const IntegerType *Type::getInt32Ty(LLVMContext &C) {
-  static const IntegerType *Int32Ty = new BuiltinIntegerType(32);
-  return Int32Ty;
+  return C.pImpl->Int32Ty;
 }
 
 const IntegerType *Type::getInt64Ty(LLVMContext &C) {
-  static const IntegerType *Int64Ty = new BuiltinIntegerType(64);
-  return Int64Ty;
+  return C.pImpl->Int64Ty;
 }
 
 //===----------------------------------------------------------------------===//
@@ -430,7 +411,7 @@ bool FunctionType::isValidArgumentType(const Type *ArgTy) {
 FunctionType::FunctionType(const Type *Result,
                            const std::vector<const Type*> &Params,
                            bool IsVarArgs)
-  : DerivedType(FunctionTyID), isVarArgs(IsVarArgs) {
+  : DerivedType(Result->getContext(), FunctionTyID), isVarArgs(IsVarArgs) {
   ContainedTys = reinterpret_cast<PATypeHandle*>(this+1);
   NumContainedTys = Params.size() + 1; // + 1 for result type
   assert(isValidReturnType(Result) && "invalid return type for function");
@@ -450,8 +431,9 @@ FunctionType::FunctionType(const Type *Result,
   setAbstract(isAbstract);
 }
 
-StructType::StructType(const std::vector<const Type*> &Types, bool isPacked)
-  : CompositeType(StructTyID) {
+StructType::StructType(LLVMContext &C, 
+                       const std::vector<const Type*> &Types, bool isPacked)
+  : CompositeType(C, StructTyID) {
   ContainedTys = reinterpret_cast<PATypeHandle*>(this + 1);
   NumContainedTys = Types.size();
   setSubclassData(isPacked);
@@ -494,7 +476,7 @@ PointerType::PointerType(const Type *E, unsigned AddrSpace)
   setAbstract(E->isAbstract());
 }
 
-OpaqueType::OpaqueType() : DerivedType(OpaqueTyID) {
+OpaqueType::OpaqueType(LLVMContext &C) : DerivedType(C, OpaqueTyID) {
   setAbstract(true);
 #ifdef DEBUG_MERGE_TYPES
   DOUT << "Derived new type: " << *this << "\n";
@@ -521,7 +503,7 @@ void DerivedType::dropAllTypeUses() {
         llvm_acquire_global_lock();
         tmp = AlwaysOpaqueTy;
         if (!tmp) {
-          tmp = OpaqueType::get();
+          tmp = OpaqueType::get(getContext());
           PATypeHolder* tmp2 = new PATypeHolder(AlwaysOpaqueTy);
           sys::MemoryFence();
           AlwaysOpaqueTy = tmp;
@@ -531,7 +513,7 @@ void DerivedType::dropAllTypeUses() {
         llvm_release_global_lock();
       }
     } else {
-      AlwaysOpaqueTy = OpaqueType::get();
+      AlwaysOpaqueTy = OpaqueType::get(getContext());
       Holder = new PATypeHolder(AlwaysOpaqueTy);
     } 
         
@@ -755,9 +737,6 @@ static bool TypeHasCycleThroughItself(const Type *Ty) {
 //===----------------------------------------------------------------------===//
 // Function Type Factory and Value Class...
 //
-
-static ManagedStatic<TypeMap<IntegerValType, IntegerType> > IntegerTypes;
-
 const IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
   assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
   assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
@@ -772,6 +751,8 @@ const IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
     default: 
       break;
   }
+
+  LLVMContextImpl *pImpl = C.pImpl;
   
   IntegerValType IVT(NumBits);
   IntegerType *ITy = 0;
@@ -779,12 +760,12 @@ const IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
   // First, see if the type is already in the table, for which
   // a reader lock suffices.
   sys::SmartScopedLock<true> L(*TypeMapLock);
-  ITy = IntegerTypes->get(IVT);
+  ITy = pImpl->IntegerTypes.get(IVT);
     
   if (!ITy) {
     // Value not found.  Derive a new type!
-    ITy = new IntegerType(NumBits);
-    IntegerTypes->add(IVT, ITy);
+    ITy = new IntegerType(C, NumBits);
+    pImpl->IntegerTypes.add(IVT, ITy);
   }
 #ifdef DEBUG_MERGE_TYPES
   DOUT << "Derived new type: " << *ITy << "\n";
@@ -918,7 +899,7 @@ StructType *StructType::get(LLVMContext &Context,
     // Value not found.  Derive a new type!
     ST = (StructType*) operator new(sizeof(StructType) +
                                     sizeof(PATypeHandle) * ETypes.size());
-    new (ST) StructType(ETypes, isPacked);
+    new (ST) StructType(Context, ETypes, isPacked);
     pImpl->StructTypes.add(STV, ST);
   }
 #ifdef DEBUG_MERGE_TYPES