First sketch of special section objects

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53320 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/TargetAsmInfo.cpp b/lib/Target/TargetAsmInfo.cpp
index 0c74d5a..3c58244 100644
--- a/lib/Target/TargetAsmInfo.cpp
+++ b/lib/Target/TargetAsmInfo.cpp
@@ -27,10 +27,17 @@
 
 TargetAsmInfo::TargetAsmInfo() :
   TextSection("\t.text"),
+  TextSection_(0),
   DataSection("\t.data"),
+  DataSection_(0),
   BSSSection("\t.bss"),
+  BSSSection_(0),
+  ReadOnlySection(0),
+  ReadOnlySection_(0),
   TLSDataSection("\t.section .tdata,\"awT\",@progbits"),
+  TLSDataSection_(0),
   TLSBSSSection("\t.section .tbss,\"awT\",@nobits"),
+  TLSBSSSection_(0),
   ZeroFillDirective(0),
   NonexecutableStackDirective(0),
   NeedsSet(false),
@@ -71,12 +78,15 @@
   JumpTableDataSection("\t.section .rodata"),
   JumpTableDirective(0),
   CStringSection(0),
+  CStringSection_(0),
   StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
   StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
   FourByteConstantSection(0),
+  FourByteConstantSection_(0),
   EightByteConstantSection(0),
+  EightByteConstantSection_(0),
   SixteenByteConstantSection(0),
-  ReadOnlySection(0),
+  SixteenByteConstantSection_(0),
   GlobalDirective("\t.globl\t"),
   SetDirective(0),
   LCOMMDirective(0),
@@ -112,6 +122,8 @@
   DwarfEHFrameSection(".eh_frame"),
   DwarfExceptionSection(".gcc_except_table"),
   AsmTransCBE(0) {
+  TextSection_ = getUnnamedSection(TextSection);
+  DataSection_ = getUnnamedSection(DataSection);
 }
 
 TargetAsmInfo::~TargetAsmInfo() {
@@ -257,51 +269,51 @@
 
 std::string
 TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
-  unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
-
-  std::string Name;
-
-  // FIXME: Should we use some hashing based on section name and just check
-  // flags?
-
+  const Section* S;
   // Select section name
   if (GV->hasSection()) {
     // Honour section already set, if any
-    Name = GV->getSection();
+    unsigned Flags = SectionFlagsForGlobal(GV,
+                                           GV->getSection().c_str());
+    S = getNamedSection(GV->getSection().c_str(), Flags);
   } else {
     // Use default section depending on the 'type' of global
-    Name = SelectSectionForGlobal(GV);
+    S = SelectSectionForGlobal(GV);
   }
 
+  std::string Name = S->Name;
+
   // If section is named we need to switch into it via special '.section'
   // directive and also append funky flags. Otherwise - section name is just
   // some magic assembler directive.
-  if (Flags & SectionFlags::Named)
-    Name = getSwitchToSectionDirective() + Name + PrintSectionFlags(Flags);
+  if (S->isNamed())
+    Name = getSwitchToSectionDirective() + Name + PrintSectionFlags(S->Flags);
 
   return Name;
 }
 
 // Lame default implementation. Calculate the section name for global.
-std::string
+const Section*
 TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
   SectionKind::Kind Kind = SectionKindForGlobal(GV);
 
-  if (GV->isWeakForLinker())
-    return UniqueSectionForGlobal(GV, Kind);
-  else {
+  if (GV->isWeakForLinker()) {
+    std::string Name = UniqueSectionForGlobal(GV, Kind);
+    unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
+    return getNamedSection(Name.c_str(), Flags);
+  } else {
     if (Kind == SectionKind::Text)
-      return getTextSection();
-    else if (Kind == SectionKind::BSS && getBSSSection())
-      return getBSSSection();
-    else if (getReadOnlySection() &&
+      return getTextSection_();
+    else if (Kind == SectionKind::BSS && getBSSSection_())
+      return getBSSSection_();
+    else if (getReadOnlySection_() &&
              (Kind == SectionKind::ROData ||
               Kind == SectionKind::RODataMergeConst ||
               Kind == SectionKind::RODataMergeStr))
-      return getReadOnlySection();
+      return getReadOnlySection_();
   }
 
-  return getDataSection();
+  return getDataSection_();
 }
 
 std::string
@@ -326,3 +338,29 @@
     assert(0 && "Unknown section kind");
   }
 }
+
+const Section*
+TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags) const {
+  Section& S = Sections[Name];
+
+  // This is newly-created section, set it up properly.
+  if (S.Flags == SectionFlags::Invalid) {
+    S.Flags = Flags | SectionFlags::Named;
+    S.Name = Name;
+  }
+
+  return &S;
+}
+
+const Section*
+TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags) const {
+  Section& S = Sections[Directive];
+
+  // This is newly-created section, set it up properly.
+  if (S.Flags == SectionFlags::Invalid) {
+    S.Flags = Flags & ~SectionFlags::Named;
+    S.Name = Directive;
+  }
+
+  return &S;
+}
diff --git a/lib/Target/X86/X86TargetAsmInfo.cpp b/lib/Target/X86/X86TargetAsmInfo.cpp
index 43d60cb..bcee870 100644
--- a/lib/Target/X86/X86TargetAsmInfo.cpp
+++ b/lib/Target/X86/X86TargetAsmInfo.cpp
@@ -142,12 +142,31 @@
   ConstantPoolSection = "\t.const\n";
   JumpTableDataSection = "\t.const\n";
   CStringSection = "\t.cstring";
+  CStringSection_ = getUnnamedSection("\t.cstring",
+                               SectionFlags::Mergeable | SectionFlags::Strings);
   FourByteConstantSection = "\t.literal4\n";
-  EightByteConstantSection = "\t.literal8\n";
+  FourByteConstantSection_ = getUnnamedSection("\t.literal4\n",
+                                               SectionFlags::Mergeable);
+  EightByteConstantSection_ = getUnnamedSection("\t.literal8\n",
+                                                SectionFlags::Mergeable);
   // FIXME: Why don't always use this section?
-  if (is64Bit)
+  if (is64Bit) {
     SixteenByteConstantSection = "\t.literal16\n";
+    SixteenByteConstantSection_ = getUnnamedSection("\t.literal16\n",
+                                                    SectionFlags::Mergeable);
+  }
   ReadOnlySection = "\t.const\n";
+  ReadOnlySection_ = getUnnamedSection("\t.const\n", SectionFlags::None);
+  TextCoalSection =
+  getUnnamedSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
+                    SectionFlags::Code);
+  ConstDataCoalSection =
+    getUnnamedSection(".section __DATA,__const_coal,coalesced",
+                      SectionFlags::None);
+  ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
+  DataCoalSection = getUnnamedSection(".section __DATA,__datacoal_nt,coalesced",
+                                      SectionFlags::Writeable);
+
   LCOMMDirective = "\t.lcomm\t";
   SwitchToSectionDirective = "\t.section ";
   StringConstantPrefix = "\1LC";
@@ -221,7 +240,7 @@
     return DW_EH_PE_absptr;
 }
 
-std::string
+const Section*
 X86DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
   SectionKind::Kind Kind = SectionKindForGlobal(GV);
   bool isWeak = GV->isWeakForLinker();
@@ -229,29 +248,19 @@
   switch (Kind) {
    case SectionKind::Text:
     if (isWeak)
-      return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
+      return TextCoalSection;
     else
-      return getTextSection();
+      return getTextSection_();
    case SectionKind::Data:
    case SectionKind::ThreadData:
    case SectionKind::BSS:
    case SectionKind::ThreadBSS:
-    if (cast<GlobalVariable>(GV)->isConstant()) {
-      if (isWeak)
-        return ".section __DATA,__const_coal,coalesced";
-      else
-        return ".const_data";
-    } else {
-      if (isWeak)
-        return ".section __DATA,__datacoal_nt,coalesced";
-      else
-        return getDataSection();
-    }
-   case SectionKind::ROData:
-    if (isWeak)
-      return ".section __DATA,__const_coal,coalesced";
+    if (cast<GlobalVariable>(GV)->isConstant())
+      return (isWeak ? ConstDataCoalSection : ConstDataSection);
     else
-      return getReadOnlySection();
+      return (isWeak ? DataCoalSection : getDataSection_());
+   case SectionKind::ROData:
+    return (isWeak ? ConstDataCoalSection : getReadOnlySection_());
    case SectionKind::RODataMergeStr:
     return MergeableStringSection(cast<GlobalVariable>(GV));
    case SectionKind::RODataMergeConst:
@@ -263,34 +272,37 @@
   // FIXME: Do we have any extra special weird cases?
 }
 
-std::string
+const Section*
 X86DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
-  unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
-  unsigned Size = SectionFlags::getEntitySize(Flags);
+  const TargetData *TD = X86TM->getTargetData();
+  Constant *C = cast<GlobalVariable>(GV)->getInitializer();
+  const Type *Type = cast<ConstantArray>(C)->getType()->getElementType();
 
+  unsigned Size = TD->getABITypeSize(Type);
   if (Size) {
     const TargetData *TD = X86TM->getTargetData();
     unsigned Align = TD->getPreferredAlignment(GV);
     if (Align <= 32)
-      return getCStringSection();
+      return getCStringSection_();
   }
 
-  return getReadOnlySection();
+  return getReadOnlySection_();
 }
 
-std::string
+const Section*
 X86DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
-  unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
-  unsigned Size = SectionFlags::getEntitySize(Flags);
+  const TargetData *TD = X86TM->getTargetData();
+  Constant *C = cast<GlobalVariable>(GV)->getInitializer();
 
+  unsigned Size = TD->getABITypeSize(C->getType());
   if (Size == 4)
-    return FourByteConstantSection;
+    return FourByteConstantSection_;
   else if (Size == 8)
-    return EightByteConstantSection;
+    return EightByteConstantSection_;
   else if (Size == 16 && X86TM->getSubtarget<X86Subtarget>().is64Bit())
-    return SixteenByteConstantSection;
+    return SixteenByteConstantSection_;
 
-  return getReadOnlySection();
+  return getReadOnlySection_();
 }
 
 std::string
@@ -300,44 +312,20 @@
   return "";
 }
 
-unsigned
-X86DarwinTargetAsmInfo::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<GlobalVariable>(GV)->getInitializer();
-    const Type *Type;
-
-    if (Flags & SectionFlags::Strings) {
-      const ConstantArray *CVA = cast<ConstantArray>(C);
-      Type = CVA->getType()->getElementType();
-    } else
-      Type = C->getType();
-
-    unsigned Size = TD->getABITypeSize(Type);
-    if (Size > 16 ||
-        !(Flags & SectionFlags::Strings ||
-          (Size == 4 || Size == 8 || Size == 16))) {
-      // Not suitable for mergeable
-      Size = 0;
-      Flags &= ~SectionFlags::Mergeable;
-    }
-    Flags = SectionFlags::setEntitySize(Flags, Size);
-  }
-
-  return Flags;
-}
-
 X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM):
   X86TargetAsmInfo(TM) {
   bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit();
 
+  TextSection_ = getUnnamedSection("\t.text", SectionFlags::Code);
+  DataSection_ = getUnnamedSection("\t.data", SectionFlags::Writeable);
+  BSSSection_  = getUnnamedSection("\t.bss",
+                                   SectionFlags::Writeable | SectionFlags::BSS);
+  ReadOnlySection_ = getUnnamedSection("\t.rodata", SectionFlags::None);
+  TLSDataSection_ = getNamedSection("\t.tdata",
+                                    SectionFlags::Writeable | SectionFlags::TLS);
+  TLSBSSSection_ = getNamedSection("\t.tbss",
+                SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
+
   ReadOnlySection = ".rodata";
   FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
   EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
@@ -417,9 +405,9 @@
   }
 }
 
-std::string
+const Section*
 X86ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
-  SectionKind::Kind kind = SectionKindForGlobal(GV);
+  SectionKind::Kind Kind = SectionKindForGlobal(GV);
 
   if (const Function *F = dyn_cast<Function>(GV)) {
     switch (F->getLinkage()) {
@@ -427,32 +415,36 @@
      case Function::InternalLinkage:
      case Function::DLLExportLinkage:
      case Function::ExternalLinkage:
-      return getTextSection();
+      return getTextSection_();
      case Function::WeakLinkage:
      case Function::LinkOnceLinkage:
-      return UniqueSectionForGlobal(F, kind);
+      std::string Name = UniqueSectionForGlobal(GV, Kind);
+      unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
+      return getNamedSection(Name.c_str(), Flags);
     }
   } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
-    if (GVar->isWeakForLinker())
-      return UniqueSectionForGlobal(GVar, kind);
-    else {
-      switch (kind) {
+    if (GVar->isWeakForLinker()) {
+      std::string Name = UniqueSectionForGlobal(GVar, Kind);
+      unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
+      return getNamedSection(Name.c_str(), Flags);
+    } else {
+      switch (Kind) {
        case SectionKind::Data:
-        return getDataSection();
+        return getDataSection_();
        case SectionKind::BSS:
         // ELF targets usually have BSS sections
-        return getBSSSection();
+        return getBSSSection_();
        case SectionKind::ROData:
-        return getReadOnlySection();
+        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();
+        return getTLSDataSection_();
        case SectionKind::ThreadBSS:
-        return getTLSBSSSection();
+        return getTLSBSSSection_();
        default:
         assert(0 && "Unsuported section kind for global");
       }
@@ -461,87 +453,52 @@
     assert(0 && "Unsupported global");
 }
 
-std::string
+const Section*
 X86ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
-  unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
-  unsigned Size = SectionFlags::getEntitySize(Flags);
+  const TargetData *TD = X86TM->getTargetData();
+  Constant *C = cast<GlobalVariable>(GV)->getInitializer();
+  const Type *Type = C->getType();
 
   // FIXME: string here is temporary, until stuff will fully land in.
   // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
   // currently directly used by asmprinter.
-  if (Size == 4 || Size == 8 || Size == 16)
-    return ".rodata.cst" + utostr(Size);
+  unsigned Size = TD->getABITypeSize(Type);
+  if (Size == 4 || Size == 8 || Size == 16) {
+    std::string Name =  ".rodata.cst" + utostr(Size);
 
-  return getReadOnlySection();
+    return getNamedSection(Name.c_str(),
+                           SectionFlags::setEntitySize(SectionFlags::Mergeable,
+                                                       Size));
+  }
+
+  return getReadOnlySection_();
 }
 
-std::string
+const Section*
 X86ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
-  unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
-  unsigned Size = SectionFlags::getEntitySize(Flags);
+  const TargetData *TD = X86TM->getTargetData();
+  Constant *C = cast<GlobalVariable>(GV)->getInitializer();
+  const ConstantArray *CVA = cast<ConstantArray>(C);
+  const Type *Type = CVA->getType()->getElementType();
 
-  if (Size) {
+  unsigned Size = TD->getABITypeSize(Type);
+  if (Size <= 16) {
     // We also need alignment here
     const TargetData *TD = X86TM->getTargetData();
     unsigned Align = TD->getPreferredAlignment(GV);
     if (Align < Size)
       Align = Size;
 
-    return getCStringSection() + utostr(Size) + '.' + utostr(Align);
+    std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
+    unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
+                                                 SectionFlags::Strings,
+                                                 Size);
+    return getNamedSection(Name.c_str(), Flags);
   }
 
-  return getReadOnlySection();
+  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<GlobalVariable>(GV)->getInitializer();
-    const Type *Type;
-
-    if (Flags & SectionFlags::Strings) {
-      const ConstantArray *CVA = cast<ConstantArray>(C);
-      Type = CVA->getType()->getElementType();
-    } else
-      Type = C->getType();
-
-    unsigned Size = TD->getABITypeSize(Type);
-    // FIXME: size check here ugly, and will hopefully have gone, when we will
-    // have sane interface for attaching flags to sections.
-    if (Size > 16 ||
-        !(Flags & SectionFlags::Strings ||
-          (Size == 4 || Size == 8 || Size == 16))) {
-      // Not suitable for mergeable
-      Size = 0;
-      Flags &= ~SectionFlags::Mergeable;
-    }
-    Flags = SectionFlags::setEntitySize(Flags, Size);
-  }
-
-  // FIXME: This is hacky and will be removed when switching from std::string
-  // sections into 'general' ones
-
-  // Mark section as named, when needed (so, we we will need .section directive
-  // to switch into it).
-  unsigned TypeFlags = Flags & SectionFlags::TypeFlags;
-  if (!TypeFlags /* Read-only section */ ||
-      (TypeFlags & (SectionFlags::Mergeable |
-                    SectionFlags::TLS |
-                    SectionFlags::Linkonce)))
-    Flags |= SectionFlags::Named;
-
-  return Flags;
-}
-
-
 std::string X86ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
   std::string Flags = ",\"";
 
@@ -663,25 +620,6 @@
   }
 }
 
-unsigned
-X86COFFTargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
-                                            const char* name) const {
-  unsigned Flags =
-    TargetAsmInfo::SectionFlagsForGlobal(GV,
-                                         GV->getSection().c_str());
-
-  // Mark section as named, when needed (so, we we will need .section directive
-  // to switch into it).
-  unsigned TypeFlags = Flags & SectionFlags::TypeFlags;
-  if (!TypeFlags /* Read-only section */ ||
-      (TypeFlags & (SectionFlags::Mergeable |
-                    SectionFlags::TLS |
-                    SectionFlags::Linkonce)))
-    Flags |= SectionFlags::Named;
-
-  return Flags;
-}
-
 std::string X86COFFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
   std::string Flags = ",\"";
 
diff --git a/lib/Target/X86/X86TargetAsmInfo.h b/lib/Target/X86/X86TargetAsmInfo.h
index bf68bf1..2fc2d65 100644
--- a/lib/Target/X86/X86TargetAsmInfo.h
+++ b/lib/Target/X86/X86TargetAsmInfo.h
@@ -34,16 +34,19 @@
   };
 
   struct X86DarwinTargetAsmInfo : public X86TargetAsmInfo {
+    const Section* TextCoalSection;
+    const Section* ConstDataCoalSection;
+    const Section* ConstDataSection;
+    const Section* DataCoalSection;
+
     explicit X86DarwinTargetAsmInfo(const X86TargetMachine &TM);
     virtual unsigned PreferredEHDataFormat(DwarfEncoding::Target Reason,
                                            bool Global) const;
-    virtual std::string SelectSectionForGlobal(const GlobalValue *GV) const;
-    virtual unsigned SectionFlagsForGlobal(const GlobalValue *GV,
-                                           const char* name) const;
+    virtual const Section* SelectSectionForGlobal(const GlobalValue *GV) const;
     virtual std::string UniqueSectionForGlobal(const GlobalValue* GV,
                                                SectionKind::Kind kind) const;
-    std::string MergeableConstSection(const GlobalVariable *GV) const;
-    std::string MergeableStringSection(const GlobalVariable *GV) const;
+    const Section* MergeableConstSection(const GlobalVariable *GV) const;
+    const Section* MergeableStringSection(const GlobalVariable *GV) const;
   };
 
   struct X86ELFTargetAsmInfo : public X86TargetAsmInfo {
@@ -51,20 +54,16 @@
     virtual unsigned PreferredEHDataFormat(DwarfEncoding::Target Reason,
                                            bool Global) const;
 
-    virtual std::string SelectSectionForGlobal(const GlobalValue *GV) const;
-    virtual unsigned SectionFlagsForGlobal(const GlobalValue *GV,
-                                           const char* name) const;
+    virtual const Section* SelectSectionForGlobal(const GlobalValue *GV) const;
     virtual std::string PrintSectionFlags(unsigned flags) const;
-    std::string MergeableConstSection(const GlobalVariable *GV) const;
-    std::string MergeableStringSection(const GlobalVariable *GV) const;
+    const Section* MergeableConstSection(const GlobalVariable *GV) const;
+    const Section* MergeableStringSection(const GlobalVariable *GV) const ;
   };
 
   struct X86COFFTargetAsmInfo : public X86TargetAsmInfo {
     explicit X86COFFTargetAsmInfo(const X86TargetMachine &TM);
     virtual unsigned PreferredEHDataFormat(DwarfEncoding::Target Reason,
                                            bool Global) const;
-    virtual unsigned SectionFlagsForGlobal(const GlobalValue *GV,
-                                           const char* name) const;
     virtual std::string UniqueSectionForGlobal(const GlobalValue* GV,
                                                SectionKind::Kind kind) const;
     virtual std::string PrintSectionFlags(unsigned flags) const;