[llvm-commits] [llvm] r63142 - in /llvm/trunk/lib/Target: DarwinTargetAsmInfo.cpp ELFTargetAsmInfo.cpp TargetAsmInfo.cpp

Anton Korobeynikov asl at math.spbu.ru
Tue Jan 27 14:29:25 PST 2009


Author: asl
Date: Tue Jan 27 16:29:24 2009
New Revision: 63142

URL: http://llvm.org/viewvc/llvm-project?rev=63142&view=rev
Log:
Treat [1 x i8] zeroinitializer as a C string, placing such stuff into
mergeable string section. I don't see any bad impact of such decision
(rather then placing it into mergeable const section, as it was before),
but at least Darwin linker won't complain anymore.

The problem in LLVM is that we don't have special type for string constants
(like gcc does). Even more, we have two separate types: ConstatArray for non-null
strings and ConstantAggregateZero for null stuff.... It's a bit weird :)

Modified:
    llvm/trunk/lib/Target/DarwinTargetAsmInfo.cpp
    llvm/trunk/lib/Target/ELFTargetAsmInfo.cpp
    llvm/trunk/lib/Target/TargetAsmInfo.cpp

Modified: llvm/trunk/lib/Target/DarwinTargetAsmInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/DarwinTargetAsmInfo.cpp?rev=63142&r1=63141&r2=63142&view=diff

==============================================================================
--- llvm/trunk/lib/Target/DarwinTargetAsmInfo.cpp (original)
+++ llvm/trunk/lib/Target/DarwinTargetAsmInfo.cpp Tue Jan 27 16:29:24 2009
@@ -115,9 +115,9 @@
 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
   const TargetData *TD = TM.getTargetData();
   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
-  const Type *Type = cast<ConstantArray>(C)->getType()->getElementType();
+  const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
 
-  unsigned Size = TD->getTypePaddedSize(Type);
+  unsigned Size = TD->getTypePaddedSize(Ty);
   if (Size) {
     unsigned Align = TD->getPreferredAlignment(GV);
     if (Align <= 32)

Modified: llvm/trunk/lib/Target/ELFTargetAsmInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ELFTargetAsmInfo.cpp?rev=63142&r1=63141&r2=63142&view=diff

==============================================================================
--- llvm/trunk/lib/Target/ELFTargetAsmInfo.cpp (original)
+++ llvm/trunk/lib/Target/ELFTargetAsmInfo.cpp Tue Jan 27 16:29:24 2009
@@ -126,8 +126,7 @@
 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
   const TargetData *TD = TM.getTargetData();
   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
-  const ConstantArray *CVA = cast<ConstantArray>(C);
-  const Type *Ty = CVA->getType()->getElementType();
+  const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
 
   unsigned Size = TD->getTypePaddedSize(Ty);
   if (Size <= 16) {

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

==============================================================================
--- llvm/trunk/lib/Target/TargetAsmInfo.cpp (original)
+++ llvm/trunk/lib/Target/TargetAsmInfo.cpp Tue Jan 27 16:29:24 2009
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Constants.h"
+#include "llvm/DerivedTypes.h"
 #include "llvm/GlobalVariable.h"
 #include "llvm/Function.h"
 #include "llvm/Module.h"
@@ -170,6 +171,25 @@
   return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
 }
 
+static bool isConstantString(const Constant *C) {
+  // First check: is we have constant array of i8 terminated with zero
+  const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
+  // Check, if initializer is a null-terminated string
+  if (CVA && CVA->isCString())
+    return true;
+
+  // Another possibility: [1 x i8] zeroinitializer
+  if (isa<ConstantAggregateZero>(C)) {
+    if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType())) {
+      return (Ty->getElementType() == Type::Int8Ty &&
+              Ty->getNumElements() == 1);
+    }
+  }
+
+  return false;
+}
+
+
 SectionKind::Kind
 TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
   // Early exit - functions should be always in text sections.
@@ -191,9 +211,8 @@
     if (C->ContainsRelocations())
       return SectionKind::ROData;
     else {
-      const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
       // Check, if initializer is a null-terminated string
-      if (CVA && CVA->isCString())
+      if (isConstantString(C))
         return SectionKind::RODataMergeStr;
       else
         return SectionKind::RODataMergeConst;





More information about the llvm-commits mailing list