diff --git a/include/llvm/CodeGen/AsmPrinter.h b/include/llvm/CodeGen/AsmPrinter.h index 68ca2d0e32a..223841e1d20 100644 --- a/include/llvm/CodeGen/AsmPrinter.h +++ b/include/llvm/CodeGen/AsmPrinter.h @@ -95,11 +95,6 @@ namespace llvm { /// void SwitchToDataSection(const char *NewSection, const GlobalValue *GV); - /// 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 getPreferredAlignmentLog(const GlobalVariable *GV) const; - /// getGlobalLinkName - Returns the asm/link name of of the specified /// global variable. Should be overridden by each target asm printer to /// generate the appropriate value. diff --git a/include/llvm/Target/TargetData.h b/include/llvm/Target/TargetData.h index e4ea5c1cc4c..b8cdae5f7f3 100644 --- a/include/llvm/Target/TargetData.h +++ b/include/llvm/Target/TargetData.h @@ -31,6 +31,7 @@ class Value; class Type; class StructType; class StructLayout; +class GlobalVariable; class TargetData : public ImmutablePass { bool LittleEndian; // Defaults to false @@ -142,6 +143,11 @@ public: /// removed, this method must be called whenever a StructType is removed to /// avoid a dangling pointer in this cache. void InvalidateStructLayoutInfo(const StructType *Ty) 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). + virtual unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const; }; /// StructLayout - used to lazily calculate structure layout information for a diff --git a/lib/CodeGen/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter.cpp index 09978344a78..9a740692b9b 100644 --- a/lib/CodeGen/AsmPrinter.cpp +++ b/lib/CodeGen/AsmPrinter.cpp @@ -326,29 +326,6 @@ void AsmPrinter::EmitXXStructorList(Constant *List) { } } -/// 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 AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const { - const Type *ElemType = GV->getType()->getElementType(); - unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(ElemType); - if (GV->getAlignment() > (1U << Alignment)) - Alignment = Log2_32(GV->getAlignment()); - - if (GV->hasInitializer()) { - // Always round up alignment of global doubles to 8 bytes. - if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3) - Alignment = 3; - if (Alignment < 4) { - // If the global is not external, see if it is large. If so, give it a - // larger alignment. - if (TM.getTargetData()->getTypeSize(ElemType) > 128) - Alignment = 4; // 16-byte alignment. - } - } - return Alignment; -} - /// getGlobalLinkName - Returns the asm/link name of of the specified /// global variable. Should be overridden by each target asm printer to /// generate the appropriate value. diff --git a/lib/Target/Alpha/AlphaAsmPrinter.cpp b/lib/Target/Alpha/AlphaAsmPrinter.cpp index 3448f3c4739..f94f5537bc5 100644 --- a/lib/Target/Alpha/AlphaAsmPrinter.cpp +++ b/lib/Target/Alpha/AlphaAsmPrinter.cpp @@ -232,7 +232,7 @@ bool AlphaAsmPrinter::doFinalization(Module &M) { Constant *C = I->getInitializer(); unsigned Size = TD->getTypeSize(C->getType()); // unsigned Align = TD->getTypeAlignmentShift(C->getType()); - unsigned Align = getPreferredAlignmentLog(I); + unsigned Align = TD->getPreferredAlignmentLog(I); if (C->isNullValue() && (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || diff --git a/lib/Target/PowerPC/PPCAsmPrinter.cpp b/lib/Target/PowerPC/PPCAsmPrinter.cpp index bba9d75558f..a7086975f05 100644 --- a/lib/Target/PowerPC/PPCAsmPrinter.cpp +++ b/lib/Target/PowerPC/PPCAsmPrinter.cpp @@ -513,7 +513,7 @@ bool DarwinAsmPrinter::doFinalization(Module &M) { std::string name = Mang->getValueName(I); Constant *C = I->getInitializer(); unsigned Size = TD->getTypeSize(C->getType()); - unsigned Align = getPreferredAlignmentLog(I); + unsigned Align = TD->getPreferredAlignmentLog(I); if (C->isNullValue() && /* FIXME: Verify correct */ (I->hasInternalLinkage() || I->hasWeakLinkage() || diff --git a/lib/Target/TargetData.cpp b/lib/Target/TargetData.cpp index 834177420f6..03a232c6714 100644 --- a/lib/Target/TargetData.cpp +++ b/lib/Target/TargetData.cpp @@ -354,3 +354,26 @@ uint64_t TargetData::getIndexedOffset(const Type *ptrTy, 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 { + const Type *ElemType = GV->getType()->getElementType(); + unsigned Alignment = getTypeAlignmentShift(ElemType); + if (GV->getAlignment() > (1U << Alignment)) + Alignment = Log2_32(GV->getAlignment()); + + if (GV->hasInitializer()) { + // Always round up alignment of global doubles to 8 bytes. + if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3) + Alignment = 3; + if (Alignment < 4) { + // If the global is not external, see if it is large. If so, give it a + // larger alignment. + if (getTypeSize(ElemType) > 128) + Alignment = 4; // 16-byte alignment. + } + } + return Alignment; +} + diff --git a/lib/Target/X86/X86AsmPrinter.cpp b/lib/Target/X86/X86AsmPrinter.cpp index 2438546f3db..5e13a35f831 100644 --- a/lib/Target/X86/X86AsmPrinter.cpp +++ b/lib/Target/X86/X86AsmPrinter.cpp @@ -139,7 +139,7 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) { std::string name = Mang->getValueName(I); Constant *C = I->getInitializer(); unsigned Size = TD->getTypeSize(C->getType()); - unsigned Align = getPreferredAlignmentLog(I); + unsigned Align = TD->getPreferredAlignmentLog(I); if (C->isNullValue() && /* FIXME: Verify correct */ (I->hasInternalLinkage() || I->hasWeakLinkage() || diff --git a/lib/Target/X86/X86IntelAsmPrinter.cpp b/lib/Target/X86/X86IntelAsmPrinter.cpp index 7a420d894e8..11caea9dbef 100755 --- a/lib/Target/X86/X86IntelAsmPrinter.cpp +++ b/lib/Target/X86/X86IntelAsmPrinter.cpp @@ -387,7 +387,7 @@ bool X86IntelAsmPrinter::doFinalization(Module &M) { std::string name = Mang->getValueName(I); Constant *C = I->getInitializer(); unsigned Size = TD->getTypeSize(C->getType()); - unsigned Align = getPreferredAlignmentLog(I); + unsigned Align = TD->getPreferredAlignmentLog(I); bool bCustomSegment = false; switch (I->getLinkage()) {