Fix preselection/lowerswitches bug
[oota-llvm.git] / lib / Target / TargetData.cpp
index 158f57e3accf2941e27022c9c5d7ada39d0eb432..a377fd0d7f945b3a830d41af41fc9078ce6549f7 100644 (file)
@@ -1,4 +1,11 @@
 //===-- TargetData.cpp - Data size & alignment routines --------------------==//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This file defines target properties related to datatype size/offset/alignment
 // information.  It uses lazy annotations to cache information about how 
@@ -15,7 +22,7 @@
 #include "llvm/DerivedTypes.h"
 #include "llvm/Constants.h"
 
-// Handle the Pass registration stuff neccesary to use TargetData's.
+// Handle the Pass registration stuff necessary to use TargetData's.
 namespace {
   // Register the default SparcV9 implementation...
   RegisterPass<TargetData> X("targetdata", "Target Data Layout");
@@ -45,7 +52,7 @@ StructLayout::StructLayout(const StructType *ST, const TargetData &TD)
     getTypeInfo(Ty, &TD, TySize, A);
     TyAlign = A;
 
-    // Add padding if neccesary to make the data element aligned properly...
+    // Add padding if necessary to make the data element aligned properly...
     if (StructSize % TyAlign != 0)
       StructSize = (StructSize/TyAlign + 1) * TyAlign;   // Add padding...
 
@@ -56,25 +63,23 @@ StructLayout::StructLayout(const StructType *ST, const TargetData &TD)
     StructSize += TySize;                 // Consume space for this data item
   }
 
+  // Empty structures have alignment of 1 byte.
+  if (StructAlignment == 0) StructAlignment = 1;
+
   // Add padding to the end of the struct so that it could be put in an array
   // and all array elements would be aligned correctly.
   if (StructSize % StructAlignment != 0)
     StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
-
-  if (StructSize == 0) {
-    StructSize = 1;           // Empty struct is 1 byte
-    StructAlignment = 1;
-  }
 }
 
 Annotation *TargetData::TypeAnFactory(AnnotationID AID, const Annotable *T,
                                      void *D) {
   const TargetData &TD = *(const TargetData*)D;
   assert(AID == TD.AID && "Target data annotation ID mismatch!");
-  const Type *Ty = cast<const Type>((const Value *)T);
+  const Type *Ty = cast<Type>((const Value *)T);
   assert(isa<StructType>(Ty) && 
         "Can only create StructLayout annotation on structs!");
-  return new StructLayout((const StructType *)Ty, TD);
+  return new StructLayout(cast<StructType>(Ty), TD);
 }
 
 //===----------------------------------------------------------------------===//
@@ -114,8 +119,8 @@ TargetData::TargetData(const std::string &ToolName, const Module *M)
   : AID(AnnotationManager::getID("TargetData::" + ToolName)) {
   AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this);
 
-  LittleEndian     = M->isLittleEndian();
-  PointerSize      = M->has32BitPointers() ? 4 : 8;
+  LittleEndian     = M->getEndianness() != Module::BigEndian;
+  PointerSize      = M->getPointerSize() != Module::Pointer64 ? 4 : 8;
   PointerAlignment = PointerSize;
   DoubleAlignment  = PointerSize;
   FloatAlignment   = 4;
@@ -195,12 +200,10 @@ uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
       Ty = cast<SequentialType>(Ty)->getElementType();
 
       // Get the array index and the size of each array element.
-      // Both must be known constants, or the index shd be 0; else this fails.
       int64_t arrayIdx = cast<ConstantSInt>(Idx[CurIDX])->getValue();
-      Result += arrayIdx == 0? 0
-                : (uint64_t) (arrayIdx * (int64_t) getTypeSize(Ty)); 
-
-    } else if (const StructType *STy = dyn_cast<const StructType>(Ty)) {
+      Result += arrayIdx * (int64_t)getTypeSize(Ty);
+    } else {
+      const StructType *STy = cast<StructType>(Ty);
       assert(Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx");
       unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
 
@@ -213,9 +216,6 @@ uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
 
       // Update Ty to refer to current element
       Ty = STy->getElementTypes()[FieldNo];
-    } else {
-      assert(0 && "Indexing type that is not struct or array?");
-      return 0;                         // Load directly through ptr
     }
   }