[llvm-commits] [llvm] r52868 - in /llvm/trunk: include/llvm/Target/TargetAsmInfo.h lib/Target/TargetAsmInfo.cpp

Anton Korobeynikov asl at math.spbu.ru
Sat Jun 28 06:45:57 PDT 2008


Author: asl
Date: Sat Jun 28 08:45:57 2008
New Revision: 52868

URL: http://llvm.org/viewvc/llvm-project?rev=52868&view=rev
Log:
Start refactoring of asmprinters: provide a TAI hook, which will select a 'section kind' for a global.

Modified:
    llvm/trunk/include/llvm/Target/TargetAsmInfo.h
    llvm/trunk/lib/Target/TargetAsmInfo.cpp

Modified: llvm/trunk/include/llvm/Target/TargetAsmInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetAsmInfo.h?rev=52868&r1=52867&r2=52868&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Target/TargetAsmInfo.h (original)
+++ llvm/trunk/include/llvm/Target/TargetAsmInfo.h Sat Jun 28 08:45:57 2008
@@ -29,8 +29,22 @@
     };
   }
 
+  namespace SectionKind {
+    enum Kind {
+      Text,             ///< Text section
+      Data,             ///< Data section
+      BSS,              ///< BSS section
+      ROData,           ///< Readonly data section
+      RODataMergeStr,   ///< Readonly data section (mergeable strings)
+      RODataMergeConst, ///< Readonly data section (mergeable constants)
+      ThreadData,       ///< Initialized TLS data objects
+      ThreadBSS         ///< Uninitialized TLS data objects
+    };
+  }
+
   class TargetMachine;
   class CallInst;
+  class GlobalValue;
 
   /// TargetAsmInfo - This class is intended to be used as a base class for asm
   /// properties and features specific to the target.
@@ -427,7 +441,11 @@
     /// if the symbol can be relocated.
     virtual unsigned PreferredEHDataFormat(DwarfEncoding::Target Reason,
                                            bool Global) const;
-    
+
+    /// SectionKindForGlobal - This hook allows the target to select proper
+    /// section kind used for global emission.
+    SectionKind::Kind SectionKindForGlobal(const GlobalValue *GV) const;
+
     // Accessors.
     //
     const char *getTextSection() const {
@@ -642,7 +660,7 @@
     }
     const char *getDwarfSectionOffsetDirective() const {
       return DwarfSectionOffsetDirective;
-    }    
+    }
     const char *getDwarfAbbrevSection() const {
       return DwarfAbbrevSection;
     }

Modified: llvm/trunk/lib/Target/TargetAsmInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/TargetAsmInfo.cpp?rev=52868&r1=52867&r2=52868&view=diff

==============================================================================
--- llvm/trunk/lib/Target/TargetAsmInfo.cpp (original)
+++ llvm/trunk/lib/Target/TargetAsmInfo.cpp Sat Jun 28 08:45:57 2008
@@ -12,7 +12,13 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Constants.h"
+#include "llvm/GlobalVariable.h"
+#include "llvm/Function.h"
+#include "llvm/Module.h"
+#include "llvm/Type.h"
 #include "llvm/Target/TargetAsmInfo.h"
+#include "llvm/Target/TargetOptions.h"
 #include "llvm/Support/Dwarf.h"
 #include <cctype>
 #include <cstring>
@@ -142,3 +148,46 @@
   return dwarf::DW_EH_PE_absptr;
 }
 
+static bool isSuitableForBSS(const GlobalVariable *GV) {
+  if (!GV->hasInitializer())
+    return true;
+
+  // Leave constant zeros in readonly constant sections, so they can be shared
+  Constant *C = GV->getInitializer();
+  return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
+}
+
+SectionKind::Kind
+TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
+  // Early exit - functions should be always in text sections.
+  if (isa<Function>(GV))
+    return SectionKind::Text;
+
+  const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
+  bool isThreadLocal = GVar->isThreadLocal();
+  assert(GVar && "Invalid global value for section selection");
+
+  SectionKind::Kind kind;
+  if (isSuitableForBSS(GVar)) {
+    // Variable can be easily put to BSS section.
+    return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS);
+  } else if (GVar->isConstant() && !isThreadLocal) {
+    // Now we know, that varible has initializer and it is constant. We need to
+    // check its initializer to decide, which section to output it into. Also
+    // note, there is no thread-local r/o section.
+    Constant *C = GVar->getInitializer();
+    if (C->ContainsRelocations())
+      kind = SectionKind::ROData;
+    else {
+      const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
+      // Check, if initializer is a null-terminated string
+      if (CVA && CVA->isCString())
+        kind = SectionKind::RODataMergeStr;
+      else
+        kind = SectionKind::RODataMergeConst;
+    }
+  }
+
+  // Variable is not constant or thread-local - emit to generic data section.
+  return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data);
+}





More information about the llvm-commits mailing list