TargetData.cpp::getTypeInfo() was returning alignment of element type as the
[oota-llvm.git] / lib / Target / TargetData.cpp
index 75e76fe18605989cebcc9e4bd40a3c4ba6b4a918..63c5b6178a3f8803e52e8dddb376babc2bb4a029 100644 (file)
@@ -131,6 +131,9 @@ TargetData::TargetData(const std::string &ToolName, const Module *M) {
   BoolAlignment    = 1;
 }
 
+/// Layouts - The lazy cache of structure layout information maintained by
+/// TargetData.
+///
 static std::map<std::pair<const TargetData*,const StructType*>,
                 StructLayout> *Layouts = 0;
 
@@ -165,6 +168,21 @@ const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
   }
 }
 
+/// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
+/// objects.  If a TargetData object is alive when types are being refined and
+/// removed, this method must be called whenever a StructType is removed to
+/// avoid a dangling pointer in this cache.
+void TargetData::InvalidateStructLayoutInfo(const StructType *Ty) const {
+  if (!Layouts) return;  // No cache.
+
+  std::map<std::pair<const TargetData*,const StructType*>,
+           StructLayout>::iterator I = Layouts->find(std::make_pair(this, Ty));
+  if (I != Layouts->end())
+    Layouts->erase(I);
+}
+
+
+
 static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
                                uint64_t &Size, unsigned char &Alignment) {
   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
@@ -197,6 +215,9 @@ static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
     getTypeInfo(PTy->getElementType(), TD, Size, Alignment);
     unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
     Size = AlignedSize*PTy->getNumElements();
+    // FIXME: The alignments of specific packed types are target dependent.
+    // For now, just set it to be equal to Size.
+    Alignment = Size;
     return;
   }
   case Type::StructTyID: {