[llvm-branch-commits] [llvm-branch] r101510 - in /llvm/branches/Apple/Morbo: include/llvm/Target/TargetMachine.h lib/CodeGen/TargetLoweringObjectFileImpl.cpp lib/Target/TargetMachine.cpp test/CodeGen/X86/global-sections.ll

Daniel Dunbar daniel at zuster.org
Fri Apr 16 11:33:42 PDT 2010


Author: ddunbar
Date: Fri Apr 16 13:33:42 2010
New Revision: 101510

URL: http://llvm.org/viewvc/llvm-project?rev=101510&view=rev
Log:
add llvm codegen support for -ffunction-sections and -fdata-sections, patch by Sylvere Teissier!

Modified:
    llvm/branches/Apple/Morbo/include/llvm/Target/TargetMachine.h
    llvm/branches/Apple/Morbo/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
    llvm/branches/Apple/Morbo/lib/Target/TargetMachine.cpp
    llvm/branches/Apple/Morbo/test/CodeGen/X86/global-sections.ll

Modified: llvm/branches/Apple/Morbo/include/llvm/Target/TargetMachine.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Morbo/include/llvm/Target/TargetMachine.h?rev=101510&r1=101509&r2=101510&view=diff
==============================================================================
--- llvm/branches/Apple/Morbo/include/llvm/Target/TargetMachine.h (original)
+++ llvm/branches/Apple/Morbo/include/llvm/Target/TargetMachine.h Fri Apr 16 13:33:42 2010
@@ -171,6 +171,21 @@
   /// is false.
   static void setAsmVerbosityDefault(bool);
 
+  /// getDataSections - Return true if data objects should be emitted into their
+  /// own section, corresponds to -fdata-sections.
+  static bool getDataSections();
+
+  /// getFunctionSections - Return true if functions should be emitted into
+  /// their own section, corresponding to -ffunction-sections.
+  static bool getFunctionSections();
+
+  /// setDataSections - Set if the data are emit into separate sections.
+  static void setDataSections(bool);
+
+  /// setFunctionSections - Set if the functions are emit into separate
+  /// sections.
+  static void setFunctionSections(bool);
+
   /// CodeGenFileType - These enums are meant to be passed into
   /// addPassesToEmitFile to indicate what type of file to emit, and returned by
   /// it to indicate what type of file could actually be made.

Modified: llvm/branches/Apple/Morbo/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Morbo/lib/CodeGen/TargetLoweringObjectFileImpl.cpp?rev=101510&r1=101509&r2=101510&view=diff
==============================================================================
--- llvm/branches/Apple/Morbo/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (original)
+++ llvm/branches/Apple/Morbo/lib/CodeGen/TargetLoweringObjectFileImpl.cpp Fri Apr 16 13:33:42 2010
@@ -300,14 +300,48 @@
   return ".gnu.linkonce.d.rel.ro.";
 }
 
+/// getSectionPrefixForGlobal - Return the section prefix name used by options
+/// FunctionsSections and DataSections.
+static const char *getSectionPrefixForGlobal(SectionKind Kind) {
+  if (Kind.isText())                 return ".text.";
+  if (Kind.isReadOnly())             return ".rodata.";
+
+  if (Kind.isThreadData())           return ".tdata.";
+  if (Kind.isThreadBSS())            return ".tbss.";
+
+  if (Kind.isDataNoRel())            return ".data.";
+  if (Kind.isDataRelLocal())         return ".data.rel.local.";
+  if (Kind.isDataRel())              return ".data.rel.";
+  if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
+
+  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
+  return ".data.rel.ro.";
+}
+
+
 const MCSection *TargetLoweringObjectFileELF::
 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
                        Mangler *Mang, const TargetMachine &TM) const {
+  // If we have -ffunction-section or -fdata-section then we should emit the
+  // global value to a uniqued section specifically for it.
+  bool EmitUniquedSection;
+  if (Kind.isText())
+    EmitUniquedSection = TM.getFunctionSections();
+  else 
+    EmitUniquedSection = TM.getDataSections();
 
   // If this global is linkonce/weak and the target handles this by emitting it
   // into a 'uniqued' section name, create and return the section now.
-  if (GV->isWeakForLinker() && !Kind.isCommon() && !Kind.isBSS()) {
-    const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
+  if ((GV->isWeakForLinker() || EmitUniquedSection) &&
+      !Kind.isCommon() && !Kind.isBSS()) {
+    const char *Prefix;
+    if (GV->isWeakForLinker())
+      Prefix = getSectionPrefixForUniqueGlobal(Kind);
+    else {
+      assert(EmitUniquedSection);
+      Prefix = getSectionPrefixForGlobal(Kind);
+    }
+
     SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
     MCSymbol *Sym = Mang->getSymbol(GV);
     Name.append(Sym->getName().begin(), Sym->getName().end());

Modified: llvm/branches/Apple/Morbo/lib/Target/TargetMachine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Morbo/lib/Target/TargetMachine.cpp?rev=101510&r1=101509&r2=101510&view=diff
==============================================================================
--- llvm/branches/Apple/Morbo/lib/Target/TargetMachine.cpp (original)
+++ llvm/branches/Apple/Morbo/lib/Target/TargetMachine.cpp Fri Apr 16 13:33:42 2010
@@ -197,7 +197,14 @@
   cl::desc("Use strong PHI elimination."),
   cl::location(StrongPHIElim),
   cl::init(false));
-
+static cl::opt<bool>
+DataSections("fdata-sections",
+  cl::desc("Emit data into separate sections"),
+  cl::init(false));
+static cl::opt<bool>
+FunctionSections("ffunction-sections",
+  cl::desc("Emit functions into separate sections"),
+  cl::init(false));
 //---------------------------------------------------------------------------
 // TargetMachine Class
 //
@@ -244,6 +251,22 @@
   AsmVerbosityDefault = V;
 }
 
+bool TargetMachine::getFunctionSections() {
+  return FunctionSections;
+}
+
+bool TargetMachine::getDataSections() {
+  return DataSections;
+}
+
+void TargetMachine::setFunctionSections(bool V) {
+  FunctionSections = V;
+}
+
+void TargetMachine::setDataSections(bool V) {
+  DataSections = V;
+}
+
 namespace llvm {
   /// LessPreciseFPMAD - This flag return true when -enable-fp-mad option
   /// is specified on the command line.  When this flag is off(default), the

Modified: llvm/branches/Apple/Morbo/test/CodeGen/X86/global-sections.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Morbo/test/CodeGen/X86/global-sections.ll?rev=101510&r1=101509&r2=101510&view=diff
==============================================================================
--- llvm/branches/Apple/Morbo/test/CodeGen/X86/global-sections.ll (original)
+++ llvm/branches/Apple/Morbo/test/CodeGen/X86/global-sections.ll Fri Apr 16 13:33:42 2010
@@ -1,5 +1,6 @@
 ; RUN: llc < %s -mtriple=i386-unknown-linux-gnu | FileCheck %s -check-prefix=LINUX
 ; RUN: llc < %s -mtriple=i386-apple-darwin9.7 | FileCheck %s -check-prefix=DARWIN
+; RUN: llc < %s -mtriple=i386-unknown-linux-gnu -fdata-sections | FileCheck %s -check-prefix=LINUX-SECTIONS
 
 
 ; int G1;
@@ -32,6 +33,12 @@
 ; DARWIN: _G3:
 ; DARWIN:     .long _G1
 
+; LINUX:   .section        .rodata,"a", at progbits
+; LINUX:   .globl  G3
+
+; LINUX-SECTIONS: .section        .rodata.G3,"a", at progbits
+; LINUX-SECTIONS: .globl  G3
+
 
 ; _Complex long long const G4 = 34;
 @G4 = constant {i64,i64} { i64 34, i64 0 }
@@ -97,6 +104,9 @@
 ; LINUX: G7:
 ; LINUX:	.asciz	"abcdefghi"
 
+; LINUX-SECTIONS: .section        .rodata.G7,"aMS", at progbits,1
+; LINUX-SECTIONS:	.globl G7
+
 
 @G8 = constant [4 x i16] [ i16 1, i16 2, i16 3, i16 0 ]
 





More information about the llvm-branch-commits mailing list