Use getPreferredAlignmentLog or getPreferredAlignment
authorDuncan Sands <baldrick@free.fr>
Tue, 29 Jan 2008 06:23:44 +0000 (06:23 +0000)
committerDuncan Sands <baldrick@free.fr>
Tue, 29 Jan 2008 06:23:44 +0000 (06:23 +0000)
to get the alignment of global variables, rather than
using hand-made versions.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46495 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Target/TargetData.h
lib/CodeGen/ELFWriter.cpp
lib/CodeGen/MachOWriter.cpp
lib/ExecutionEngine/JIT/JIT.cpp
lib/Target/IA64/IA64AsmPrinter.cpp
lib/Target/Sparc/SparcAsmPrinter.cpp
lib/Target/TargetData.cpp

index 0bcd4019b751ec267620f3d80dbe122fa8d8e846..2d9dc076e2ffe90f03a4574abf9755fa66a3a953 100644 (file)
@@ -200,7 +200,7 @@ public:
 
 
   /// getPrefTypeAlignment - Return the preferred stack/global alignment for
-  /// the specified type.
+  /// the specified type.  This is always at least as good as the ABI alignment.
   unsigned char getPrefTypeAlignment(const Type *Ty) const;
 
   /// getPreferredTypeAlignmentShift - Return the preferred alignment for the
@@ -230,6 +230,11 @@ public:
   /// avoid a dangling pointer in this cache.
   void InvalidateStructLayoutInfo(const StructType *Ty) const;
 
+  /// getPreferredAlignment - Return the preferred alignment of the specified
+  /// global.  This includes an explicitly requested alignment (if the global
+  /// has one).
+  unsigned getPreferredAlignment(const GlobalVariable *GV) const;
+
   /// getPreferredAlignmentLog - Return the preferred alignment of the
   /// specified global, returned in log form.  This includes an explicitly
   /// requested alignment (if the global has one).
index 3074e31edde69eb7776090fa9e5ecd6259fc1971..908abe5f1bba032e318bf3bcb2ffb7e2691c2f56 100644 (file)
@@ -257,7 +257,7 @@ void ELFWriter::EmitGlobal(GlobalVariable *GV) {
   }
 
   const Type *GVType = (const Type*)GV->getType();
-  unsigned Align = TM.getTargetData()->getPrefTypeAlignment(GVType);
+  unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
   unsigned Size  = TM.getTargetData()->getABITypeSize(GVType);
 
   // If this global has a zero initializer, it is part of the .bss or common
index 56bf70a62ae44ab99e3184f1219e07fef1f470d8..71e7430635a78f4bd77ccf3b8c34885f11318ddc 100644 (file)
@@ -334,10 +334,8 @@ MachOWriter::~MachOWriter() {
 void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) {
   const Type *Ty = GV->getType()->getElementType();
   unsigned Size = TM.getTargetData()->getABITypeSize(Ty);
-  unsigned Align = GV->getAlignment();
-  if (Align == 0)
-    Align = TM.getTargetData()->getPrefTypeAlignment(Ty);
-  
+  unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
+
   // Reserve space in the .bss section for this symbol while maintaining the
   // desired section alignment, which must be at least as much as required by
   // this symbol.
index 8b7b972f6c14ce9e9d5475384804701d0efeac69..70ee114b7750380cc1b15e79f38d681fe45c8d5b 100644 (file)
@@ -355,7 +355,7 @@ void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
     // compilation.
     const Type *GlobalType = GV->getType()->getElementType();
     size_t S = getTargetData()->getABITypeSize(GlobalType);
-    size_t A = getTargetData()->getPrefTypeAlignment(GlobalType);
+    size_t A = getTargetData()->getPreferredAlignment(GV);
     if (A <= 8) {
       Ptr = malloc(S);
     } else {
index a018c49209c912bf634cd3dd3ed43ecb10f144f5..d948557d4834b6e673cd195cdbaf7e7124ab6ae9 100644 (file)
@@ -271,8 +271,8 @@ bool IA64AsmPrinter::doFinalization(Module &M) {
       std::string name = Mang->getValueName(I);
       Constant *C = I->getInitializer();
       unsigned Size = TD->getABITypeSize(C->getType());
-      unsigned Align = TD->getPreferredTypeAlignmentShift(C->getType());
-      
+      unsigned Align = TD->getPreferredAlignmentLog(I);
+
       if (C->isNullValue() &&
           (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
            I->hasWeakLinkage() /* FIXME: Verify correct */)) {
index 22df0963b7e2748419409328b660ce757de17939..265428365451436ca4dfa086a92d7fdcc18fcab3 100644 (file)
@@ -229,7 +229,7 @@ bool SparcAsmPrinter::doFinalization(Module &M) {
       std::string name = Mang->getValueName(I);
       Constant *C = I->getInitializer();
       unsigned Size = TD->getABITypeSize(C->getType());
-      unsigned Align = TD->getPrefTypeAlignment(C->getType());
+      unsigned Align = TD->getPreferredAlignment(I);
 
       if (C->isNullValue() &&
           (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
index 6d566693e8507be9fa499acb8391d19ff3b035c9..64332a0df56e50f874f207dfa35b048d0868c6f7 100644 (file)
@@ -101,6 +101,7 @@ unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
 TargetAlignElem
 TargetAlignElem::get(AlignTypeEnum align_type, unsigned char abi_align,
                      unsigned char pref_align, uint32_t bit_width) {
+  assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
   TargetAlignElem retval;
   retval.AlignType = align_type;
   retval.ABIAlign = abi_align;
@@ -242,6 +243,7 @@ TargetData::TargetData(const Module *M)
 void
 TargetData::setAlignment(AlignTypeEnum align_type, unsigned char abi_align,
                          unsigned char pref_align, uint32_t bit_width) {
+  assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
   for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
     if (Alignments[i].AlignType == align_type &&
         Alignments[i].TypeBitWidth == bit_width) {
@@ -576,22 +578,29 @@ uint64_t TargetData::getIndexedOffset(const Type *ptrTy, Value* const* Indices,
   return Result;
 }
 
-/// getPreferredAlignmentLog - Return the preferred alignment of the
-/// specified global, returned in log form.  This includes an explicitly
-/// requested alignment (if the global has one).
-unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const {
+/// getPreferredAlignment - Return the preferred alignment of the specified
+/// global.  This includes an explicitly requested alignment (if the global
+/// has one).
+unsigned TargetData::getPreferredAlignment(const GlobalVariable *GV) const {
   const Type *ElemType = GV->getType()->getElementType();
-  unsigned Alignment = getPreferredTypeAlignmentShift(ElemType);
-  if (GV->getAlignment() > (1U << Alignment))
-    Alignment = Log2_32(GV->getAlignment());
-  
+  unsigned Alignment = getPrefTypeAlignment(ElemType);
+  if (GV->getAlignment() > Alignment)
+    Alignment = GV->getAlignment();
+
   if (GV->hasInitializer()) {
-    if (Alignment < 4) {
+    if (Alignment < 16) {
       // If the global is not external, see if it is large.  If so, give it a
       // larger alignment.
       if (getTypeSizeInBits(ElemType) > 128)
-        Alignment = 4;    // 16-byte alignment.
+        Alignment = 16;    // 16-byte alignment.
     }
   }
   return Alignment;
 }
+
+/// getPreferredAlignmentLog - Return the preferred alignment of the
+/// specified global, returned in log form.  This includes an explicitly
+/// requested alignment (if the global has one).
+unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const {
+  return Log2_32(getPreferredAlignment(GV));
+}