diff --git a/include/llvm/Target/TargetAsmInfo.h b/include/llvm/Target/TargetAsmInfo.h index 6194e163cd5..8d2861f6a67 100644 --- a/include/llvm/Target/TargetAsmInfo.h +++ b/include/llvm/Target/TargetAsmInfo.h @@ -57,12 +57,12 @@ namespace llvm { EntitySize = 0xFF << 24 ///< Entity size for mergeable sections }; - static inline unsigned getEntitySize(unsigned flags) { - return (flags >> 24) & 0xFF; + static inline unsigned getEntitySize(unsigned Flags) { + return (Flags >> 24) & 0xFF; } - static inline unsigned setEntitySize(unsigned flags, unsigned size) { - return ((flags & ~EntitySize) | ((size & 0xFF) << 24)); + static inline unsigned setEntitySize(unsigned Flags, unsigned Size) { + return ((Flags & ~EntitySize) | ((Size & 0xFF) << 24)); } } diff --git a/lib/Target/X86/X86TargetAsmInfo.cpp b/lib/Target/X86/X86TargetAsmInfo.cpp index 5e328fb78a3..1217291a1d8 100644 --- a/lib/Target/X86/X86TargetAsmInfo.cpp +++ b/lib/Target/X86/X86TargetAsmInfo.cpp @@ -341,10 +341,11 @@ X86ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { // ELF targets usually have BSS sections return getBSSSection(); case SectionKind::ROData: - case SectionKind::RODataMergeStr: - case SectionKind::RODataMergeConst: - // FIXME: Temporary return getReadOnlySection(); + case SectionKind::RODataMergeStr: + return MergeableStringSection(GVar); + case SectionKind::RODataMergeConst: + return MergeableConstSection(GVar); case SectionKind::ThreadData: // ELF targets usually support TLS stuff return getTLSDataSection(); @@ -358,6 +359,66 @@ X86ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { assert(0 && "Unsupported global"); } +std::string +X86ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const { + unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str()); + unsigned Size = SectionFlags::getEntitySize(Flags); + + // FIXME: string here is temporary, until stuff will fully land in. + if (Size) + return ".rodata.cst" + utostr(Size); + else + return getReadOnlySection(); +} + +std::string +X86ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { + unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str()); + unsigned Size = SectionFlags::getEntitySize(Flags); + + if (Size) { + // We also need alignment here + const TargetData *TD = X86TM->getTargetData(); + unsigned Align = TD->getPreferredAlignment(GV); + if (Align < Size) + Align = Size; + + // FIXME: string here is temporary, until stuff will fully land in. + return ".rodata.str" + utostr(Size) + ',' + utostr(Align); + } else + return getReadOnlySection(); +} + +unsigned +X86ELFTargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV, + const char* name) const { + unsigned Flags = + TargetAsmInfo::SectionFlagsForGlobal(GV, + GV->getSection().c_str()); + + // If there was decision to put stuff into mergeable section - calculate + // entity size + if (Flags & SectionFlags::Mergeable) { + const TargetData *TD = X86TM->getTargetData(); + Constant *C = cast(GV)->getInitializer(); + const Type *Type; + + if (Flags & SectionFlags::Strings) { + const ConstantArray *CVA = cast(C); + Type = CVA->getType()->getElementType(); + } else + Type = C->getType(); + + unsigned Size = TD->getABITypeSize(Type); + if (Size > 32) + Size = 0; + Flags = SectionFlags::setEntitySize(Flags, Size); + } + + return Flags; +} + + std::string X86ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const { std::string Flags = ",\""; diff --git a/lib/Target/X86/X86TargetAsmInfo.h b/lib/Target/X86/X86TargetAsmInfo.h index 483b774d6d9..dd8d3d1afb1 100644 --- a/lib/Target/X86/X86TargetAsmInfo.h +++ b/lib/Target/X86/X86TargetAsmInfo.h @@ -20,6 +20,7 @@ namespace llvm { // Forward declaration. class X86TargetMachine; + class GlobalVariable; struct X86TargetAsmInfo : public TargetAsmInfo { explicit X86TargetAsmInfo(const X86TargetMachine &TM); @@ -47,7 +48,11 @@ namespace llvm { bool Global) const; virtual std::string SelectSectionForGlobal(const GlobalValue *GV) const; + virtual unsigned SectionFlagsForGlobal(const GlobalValue *GV, + const char* name) const; virtual std::string PrintSectionFlags(unsigned flags) const; + std::string MergeableConstSection(const GlobalVariable *GV) const; + std::string MergeableStringSection(const GlobalVariable *GV) const; }; struct X86COFFTargetAsmInfo : public X86TargetAsmInfo {