Fix handling of multiple unnamed globals with the same type
[oota-llvm.git] / lib / VMCore / Type.cpp
index fb7479a08ace20f3cf9775e3382904b1f3f8d7be..ba8830912cae910bdf3efbdf5bab21ba019d240d 100644 (file)
@@ -19,6 +19,7 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/SCCIterator.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/MathExtras.h"
 #include <algorithm>
 #include <iostream>
 using namespace llvm;
@@ -42,14 +43,13 @@ AbstractTypeUser::~AbstractTypeUser() {}
 static std::map<const Type*, std::string> ConcreteTypeDescriptions;
 static std::map<const Type*, std::string> AbstractTypeDescriptions;
 
-Type::Type( const std::string& name, TypeID id )
-  : RefCount(0), ForwardType(0) {
-  if (!name.empty())
-    ConcreteTypeDescriptions[this] = name;
-  ID = id;
-  Abstract = false;
+Type::Type(const char *Name, TypeID id)
+  : ID(id), Abstract(false),  RefCount(0), ForwardType(0) {
+  assert(Name && Name[0] && "Should use other ctor if no name!");
+  ConcreteTypeDescriptions[this] = Name;
 }
 
+
 const Type *Type::getPrimitiveType(TypeID IDNumber) {
   switch (IDNumber) {
   case VoidTyID  : return VoidTy;
@@ -138,34 +138,34 @@ const Type *Type::getSignedVersion() const {
 //
 unsigned Type::getPrimitiveSize() const {
   switch (getTypeID()) {
-  case Type::BoolTy:
-  case Type::SByteTy:
-  case Type::UByteTy: return 1;
-  case Type::UShortTy:
-  case Type::ShortTy: return 2;
-  case Type::FloatTy
-  case Type::IntTy
-  case Type::UIntTy: return 4;
-  case Type::LongTy:
-  case Type::ULongTy:
-  case Type::DoubleTy: return 8;
+  case Type::BoolTyID:
+  case Type::SByteTyID:
+  case Type::UByteTyID: return 1;
+  case Type::UShortTyID:
+  case Type::ShortTyID: return 2;
+  case Type::FloatTyID:
+  case Type::IntTyID:
+  case Type::UIntTyID: return 4;
+  case Type::LongTyID:
+  case Type::ULongTyID:
+  case Type::DoubleTyID: return 8;
   default: return 0;
   }
 }
 
 unsigned Type::getPrimitiveSizeInBits() const {
   switch (getTypeID()) {
-  case Type::BoolTy:  return 1;
-  case Type::SByteTy:
-  case Type::UByteTy: return 8;
-  case Type::UShortTy:
-  case Type::ShortTy: return 16;
-  case Type::FloatTy
-  case Type::IntTy
-  case Type::UIntTy: return 32;
-  case Type::LongTy:
-  case Type::ULongTy:
-  case Type::DoubleTy: return 64;
+  case Type::BoolTyID:  return 1;
+  case Type::SByteTyID:
+  case Type::UByteTyID: return 8;
+  case Type::UShortTyID:
+  case Type::ShortTyID: return 16;
+  case Type::FloatTyID:
+  case Type::IntTyID:
+  case Type::UIntTyID: return 32;
+  case Type::LongTyID:
+  case Type::ULongTyID:
+  case Type::DoubleTyID: return 64;
   default: return 0;
   }
 }
@@ -213,6 +213,14 @@ const Type *Type::getForwardedTypeInternal() const {
   return ForwardType;
 }
 
+void Type::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
+  abort();
+}
+void Type::typeBecameConcrete(const DerivedType *AbsTy) {
+  abort();
+}
+
+
 // getTypeDescription - This is a recursive function that walks a type hierarchy
 // calculating the description for a type.
 //
@@ -677,31 +685,56 @@ static bool TypeHasCycleThroughItself(const Type *Ty) {
 //                       Derived Type Factory Functions
 //===----------------------------------------------------------------------===//
 
-// TypeMap - Make sure that only one instance of a particular type may be
-// created on any given run of the compiler... note that this involves updating
-// our map if an abstract type gets refined somehow.
-//
 namespace llvm {
-template<class ValType, class TypeClass>
-class TypeMap {
-  std::map<ValType, PATypeHolder> Map;
-
+class TypeMapBase {
+protected:
   /// TypesByHash - Keep track of types by their structure hash value.  Note
   /// that we only keep track of types that have cycles through themselves in
   /// this map.
   ///
   std::multimap<unsigned, PATypeHolder> TypesByHash;
 
-  friend void Type::clearAllTypeMaps();
-
-private:
-  void clear(std::vector<Type *> &DerivedTypes) {
-    for (typename std::map<ValType, PATypeHolder>::iterator I = Map.begin(),
-         E = Map.end(); I != E; ++I)
-      DerivedTypes.push_back(I->second.get());
-    TypesByHash.clear();
-    Map.clear();
+public:
+  void RemoveFromTypesByHash(unsigned Hash, const Type *Ty) {
+    std::multimap<unsigned, PATypeHolder>::iterator I =
+    TypesByHash.lower_bound(Hash);
+    while (I->second != Ty) {
+      ++I;
+      assert(I != TypesByHash.end() && I->first == Hash);
+    }
+    TypesByHash.erase(I);
+  }
+  
+  /// TypeBecameConcrete - When Ty gets a notification that TheType just became
+  /// concrete, drop uses and make Ty non-abstract if we should.
+  void TypeBecameConcrete(DerivedType *Ty, const DerivedType *TheType) {
+    // If the element just became concrete, remove 'ty' from the abstract
+    // type user list for the type.  Do this for as many times as Ty uses
+    // OldType.
+    for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
+         I != E; ++I)
+      if (I->get() == TheType)
+        TheType->removeAbstractTypeUser(Ty);
+    
+    // If the type is currently thought to be abstract, rescan all of our
+    // subtypes to see if the type has just become concrete!  Note that this
+    // may send out notifications to AbstractTypeUsers that types become
+    // concrete.
+    if (Ty->isAbstract())
+      Ty->PromoteAbstractToConcrete();
   }
+};
+}
+
+
+// TypeMap - Make sure that only one instance of a particular type may be
+// created on any given run of the compiler... note that this involves updating
+// our map if an abstract type gets refined somehow.
+//
+namespace llvm {
+template<class ValType, class TypeClass>
+class TypeMap : public TypeMapBase {
+  std::map<ValType, PATypeHolder> Map;
 public:
   typedef typename std::map<ValType, PATypeHolder>::iterator iterator;
   ~TypeMap() { print("ON EXIT"); }
@@ -718,29 +751,29 @@ public:
     TypesByHash.insert(std::make_pair(ValType::hashTypeStructure(Ty), Ty));
     print("add");
   }
-
-  void RemoveFromTypesByHash(unsigned Hash, const Type *Ty) {
-    std::multimap<unsigned, PATypeHolder>::iterator I =
-      TypesByHash.lower_bound(Hash);
-    while (I->second != Ty) {
-      ++I;
-      assert(I != TypesByHash.end() && I->first == Hash);
-    }
-    TypesByHash.erase(I);
+  
+  void clear(std::vector<Type *> &DerivedTypes) {
+    for (typename std::map<ValType, PATypeHolder>::iterator I = Map.begin(),
+         E = Map.end(); I != E; ++I)
+      DerivedTypes.push_back(I->second.get());
+    TypesByHash.clear();
+    Map.clear();
   }
 
-  /// finishRefinement - This method is called after we have updated an existing
-  /// type with its new components.  We must now either merge the type away with
+ /// RefineAbstractType - This method is called after we have merged a type
+  /// with another one.  We must now either merge the type away with
   /// some other type or reinstall it in the map with it's new configuration.
-  /// The specified iterator tells us what the type USED to look like.
-  void finishRefinement(TypeClass *Ty, const DerivedType *OldType,
+  void RefineAbstractType(TypeClass *Ty, const DerivedType *OldType,
                         const Type *NewType) {
-    assert((Ty->isAbstract() || !OldType->isAbstract()) &&
-           "Refining a non-abstract type!");
 #ifdef DEBUG_MERGE_TYPES
-    std::cerr << "refineAbstractTy(" << (void*)OldType << "[" << *OldType
-              << "], " << (void*)NewType << " [" << *NewType << "])\n";
+    std::cerr << "RefineAbstractType(" << (void*)OldType << "[" << *OldType
+    << "], " << (void*)NewType << " [" << *NewType << "])\n";
 #endif
+    
+    // Otherwise, we are changing one subelement type into another.  Clearly the
+    // OldType must have been abstract, making us abstract.
+    assert(Ty->isAbstract() && "Refining a non-abstract type!");
+    assert(OldType != NewType);
 
     // Make a temporary type holder for the type so that it doesn't disappear on
     // us when we erase the entry from the map.
@@ -748,7 +781,8 @@ public:
 
     // The old record is now out-of-date, because one of the children has been
     // updated.  Remove the obsolete entry from the map.
-    Map.erase(ValType::get(Ty));
+    unsigned NumErased = Map.erase(ValType::get(Ty));
+    assert(NumErased && "Element not found!");
 
     // Remember the structural hash for the type before we start hacking on it,
     // in case we need it later.
@@ -756,25 +790,22 @@ public:
 
     // Find the type element we are refining... and change it now!
     for (unsigned i = 0, e = Ty->ContainedTys.size(); i != e; ++i)
-      if (Ty->ContainedTys[i] == OldType) {
-        Ty->ContainedTys[i].removeUserFromConcrete();
+      if (Ty->ContainedTys[i] == OldType)
         Ty->ContainedTys[i] = NewType;
-      }
-
-    unsigned TypeHash = ValType::hashTypeStructure(Ty);
-
+    unsigned NewTypeHash = ValType::hashTypeStructure(Ty);
+    
     // If there are no cycles going through this node, we can do a simple,
     // efficient lookup in the map, instead of an inefficient nasty linear
     // lookup.
-    if (!Ty->isAbstract() || !TypeHasCycleThroughItself(Ty)) {
+    if (!TypeHasCycleThroughItself(Ty)) {
       typename std::map<ValType, PATypeHolder>::iterator I;
       bool Inserted;
 
-      ValType V = ValType::get(Ty);
-      tie(I, Inserted) = Map.insert(std::make_pair(V, Ty));
+      tie(I, Inserted) = Map.insert(std::make_pair(ValType::get(Ty), Ty));
       if (!Inserted) {
+        assert(OldType != NewType);
         // Refined to a different type altogether?
-        RemoveFromTypesByHash(TypeHash, Ty);
+        RemoveFromTypesByHash(OldTypeHash, Ty);
 
         // We already have this type in the table.  Get rid of the newly refined
         // type.
@@ -782,7 +813,6 @@ public:
         Ty->refineAbstractTypeTo(NewTy);
         return;
       }
-
     } else {
       // Now we check to see if there is an existing entry in the table which is
       // structurally identical to the newly refined type.  If so, this type
@@ -792,44 +822,44 @@ public:
       tie(I, E) = TypesByHash.equal_range(OldTypeHash);
       Entry = E;
       for (; I != E; ++I) {
-        if (I->second != Ty) {
+        if (I->second == Ty) {
+          // Remember the position of the old type if we see it in our scan.
+          Entry = I;
+        } else {
           if (TypesEqual(Ty, I->second)) {
-            assert(Ty->isAbstract() && "Replacing a non-abstract type?");
             TypeClass *NewTy = cast<TypeClass>((Type*)I->second.get());
 
             if (Entry == E) {
-              // Find the location of Ty in the TypesByHash structure.
+              // Find the location of Ty in the TypesByHash structure if we
+              // haven't seen it already.
               while (I->second != Ty) {
                 ++I;
                 assert(I != E && "Structure doesn't contain type??");
               }
               Entry = I;
             }
-
             TypesByHash.erase(Entry);
             Ty->refineAbstractTypeTo(NewTy);
             return;
           }
-        } else {
-          // Remember the position of
-          Entry = I;
         }
       }
 
-
       // If there is no existing type of the same structure, we reinsert an
       // updated record into the map.
       Map.insert(std::make_pair(ValType::get(Ty), Ty));
     }
 
     // If the hash codes differ, update TypesByHash
-    if (TypeHash != OldTypeHash) {
+    if (NewTypeHash != OldTypeHash) {
       RemoveFromTypesByHash(OldTypeHash, Ty);
-      TypesByHash.insert(std::make_pair(TypeHash, Ty));
+      TypesByHash.insert(std::make_pair(NewTypeHash, Ty));
     }
-
+    
     // If the type is currently thought to be abstract, rescan all of our
-    // subtypes to see if the type has just become concrete!
+    // subtypes to see if the type has just become concrete!  Note that this
+    // may send out notifications to AbstractTypeUsers that types become
+    // concrete.
     if (Ty->isAbstract())
       Ty->PromoteAbstractToConcrete();
   }
@@ -1005,6 +1035,7 @@ static TypeMap<PackedValType, PackedType> PackedTypes;
 
 PackedType *PackedType::get(const Type *ElementType, unsigned NumElements) {
   assert(ElementType && "Can't get packed of null types!");
+  assert(isPowerOf2_32(NumElements) && "Vector length should be a power of 2!");
 
   PackedValType PVT(ElementType, NumElements);
   PackedType *PT = PackedTypes.get(PVT);
@@ -1110,6 +1141,10 @@ static TypeMap<PointerValType, PointerType> PointerTypes;
 
 PointerType *PointerType::get(const Type *ValueType) {
   assert(ValueType && "Can't get a pointer to <null> type!");
+  // FIXME: The sparc backend makes void pointers, which is horribly broken.
+  // "Fix" it, then reenable this assertion.
+  //assert(ValueType != Type::VoidTy &&
+  //       "Pointer to void is not valid, use sbyte* instead!");
   PointerValType PVT(ValueType);
 
   PointerType *PT = PointerTypes.get(PVT);
@@ -1124,7 +1159,6 @@ PointerType *PointerType::get(const Type *ValueType) {
   return PT;
 }
 
-
 //===----------------------------------------------------------------------===//
 //                     Derived Type Refinement Functions
 //===----------------------------------------------------------------------===//
@@ -1134,7 +1168,7 @@ PointerType *PointerType::get(const Type *ValueType) {
 // the PATypeHandle class.  When there are no users of the abstract type, it
 // is annihilated, because there is no way to get a reference to it ever again.
 //
-void DerivedType::removeAbstractTypeUser(AbstractTypeUser *U) const {
+void Type::removeAbstractTypeUser(AbstractTypeUser *U) const {
   // Search from back to front because we will notify users from back to
   // front.  Also, it is likely that there will be a stack like behavior to
   // users that register and unregister users.
@@ -1259,11 +1293,11 @@ void DerivedType::notifyUsesThatTypeBecameConcrete() {
 //
 void FunctionType::refineAbstractType(const DerivedType *OldType,
                                       const Type *NewType) {
-  FunctionTypes.finishRefinement(this, OldType, NewType);
+  FunctionTypes.RefineAbstractType(this, OldType, NewType);
 }
 
 void FunctionType::typeBecameConcrete(const DerivedType *AbsTy) {
-  refineAbstractType(AbsTy, AbsTy);
+  FunctionTypes.TypeBecameConcrete(this, AbsTy);
 }
 
 
@@ -1273,11 +1307,11 @@ void FunctionType::typeBecameConcrete(const DerivedType *AbsTy) {
 //
 void ArrayType::refineAbstractType(const DerivedType *OldType,
                                    const Type *NewType) {
-  ArrayTypes.finishRefinement(this, OldType, NewType);
+  ArrayTypes.RefineAbstractType(this, OldType, NewType);
 }
 
 void ArrayType::typeBecameConcrete(const DerivedType *AbsTy) {
-  refineAbstractType(AbsTy, AbsTy);
+  ArrayTypes.TypeBecameConcrete(this, AbsTy);
 }
 
 // refineAbstractType - Called when a contained type is found to be more
@@ -1286,11 +1320,11 @@ void ArrayType::typeBecameConcrete(const DerivedType *AbsTy) {
 //
 void PackedType::refineAbstractType(const DerivedType *OldType,
                                    const Type *NewType) {
-  PackedTypes.finishRefinement(this, OldType, NewType);
+  PackedTypes.RefineAbstractType(this, OldType, NewType);
 }
 
 void PackedType::typeBecameConcrete(const DerivedType *AbsTy) {
-  refineAbstractType(AbsTy, AbsTy);
+  PackedTypes.TypeBecameConcrete(this, AbsTy);
 }
 
 // refineAbstractType - Called when a contained type is found to be more
@@ -1299,11 +1333,11 @@ void PackedType::typeBecameConcrete(const DerivedType *AbsTy) {
 //
 void StructType::refineAbstractType(const DerivedType *OldType,
                                     const Type *NewType) {
-  StructTypes.finishRefinement(this, OldType, NewType);
+  StructTypes.RefineAbstractType(this, OldType, NewType);
 }
 
 void StructType::typeBecameConcrete(const DerivedType *AbsTy) {
-  refineAbstractType(AbsTy, AbsTy);
+  StructTypes.TypeBecameConcrete(this, AbsTy);
 }
 
 // refineAbstractType - Called when a contained type is found to be more
@@ -1312,11 +1346,11 @@ void StructType::typeBecameConcrete(const DerivedType *AbsTy) {
 //
 void PointerType::refineAbstractType(const DerivedType *OldType,
                                      const Type *NewType) {
-  PointerTypes.finishRefinement(this, OldType, NewType);
+  PointerTypes.RefineAbstractType(this, OldType, NewType);
 }
 
 void PointerType::typeBecameConcrete(const DerivedType *AbsTy) {
-  refineAbstractType(AbsTy, AbsTy);
+  PointerTypes.TypeBecameConcrete(this, AbsTy);
 }
 
 bool SequentialType::indexValid(const Value *V) const {