OSDN Git Service

[mips] Support -membedded-data and fix a related bug
authorSimon Dardis <simon.dardis@imgtec.com>
Fri, 21 Jul 2017 17:19:00 +0000 (17:19 +0000)
committerSimon Dardis <simon.dardis@imgtec.com>
Fri, 21 Jul 2017 17:19:00 +0000 (17:19 +0000)
-membedded-data changes the location of constant data from the .sdata to
the .rodata section. Previously it was (incorrectly) always located in the
.rodata section.

Reviewers: atanasyan

Differential Revision: https://reviews.llvm.org/D35686

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@308758 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/Mips/MipsTargetObjectFile.cpp
test/CodeGen/Mips/2008-07-15-SmallSection.ll

index 4d73c39..3bf32e8 100644 (file)
@@ -36,6 +36,12 @@ ExternSData("mextern-sdata", cl::Hidden,
                      "current object."),
             cl::init(true));
 
+static cl::opt<bool>
+EmbeddedData("membedded-data", cl::Hidden,
+             cl::desc("MIPS: Try to allocate variables in the following"
+                      " sections if possible: .rodata, .sdata, .data ."),
+             cl::init(false));
+
 void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
   InitializeELF(TM.Options.UseInitArray);
@@ -77,8 +83,9 @@ bool MipsTargetObjectFile::IsGlobalInSmallSection(
 bool MipsTargetObjectFile::
 IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
                        SectionKind Kind) const {
-  return (IsGlobalInSmallSectionImpl(GO, TM) &&
-          (Kind.isData() || Kind.isBSS() || Kind.isCommon()));
+  return IsGlobalInSmallSectionImpl(GO, TM) &&
+         (Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
+          Kind.isReadOnly());
 }
 
 /// Return true if this global address should be placed into small data/bss
@@ -108,6 +115,10 @@ IsGlobalInSmallSectionImpl(const GlobalObject *GO,
                        GVA->hasCommonLinkage()))
     return false;
 
+  // Enforce -membedded-data.
+  if (EmbeddedData && GVA->isConstant())
+    return false;
+
   Type *Ty = GVA->getValueType();
   return IsInSmallSection(
       GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
@@ -123,6 +134,8 @@ MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
     return SmallBSSSection;
   if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
     return SmallDataSection;
+  if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
+    return SmallDataSection;
 
   // Otherwise, we work the same as ELF.
   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
index 79f4d79..2149542 100644 (file)
@@ -1,16 +1,32 @@
-; RUN: llc < %s -march=mips -mcpu=mips32 -mips-ssection-threshold=8 -relocation-model=static -mattr=+noabicalls -mgpopt | FileCheck %s
+; RUN: llc < %s -march=mips -mcpu=mips32 -mips-ssection-threshold=8 \
+; RUN:     -relocation-model=static -mattr=+noabicalls -mgpopt \
+; RUN:   | FileCheck %s --check-prefixes=BASIC,COMMON
+; RUN: llc < %s -march=mips -mcpu=mips32 -mips-ssection-threshold=8 \
+; RUN:     -relocation-model=static -mattr=+noabicalls -mgpopt -membedded-data \
+; RUN:   | FileCheck %s --check-prefixes=EMBDATA,COMMON
+
+; Test the layout of objects when compiling for static, noabicalls environment.
 
 %struct.anon = type { i32, i32 }
 
+; BASIC: .type  s0,@object
+; BASIC-NEXT: .section .sdata,"aw",@progbits
+
+; EMDATA: .type  s0,@object
+; EMDATA-NEXT: .section .rodata,"a",@progbits
+
 @s0 = constant [8 x i8] c"AAAAAAA\00", align 4
 
-; CHECK: .type  foo,@object
-; CHECK-NEXT: .section .sdata,"aw",@progbits
+; BASIC: .type  foo,@object
+; BASIC-NOT:  .section
+
+; EMBDATA: .type  foo,@object
+; EMBDATA-NEXT:  .section .sdata,"aw",@progbits
 @foo = global %struct.anon { i32 2, i32 3 }
 
-; CHECK:  .type bar,@object
-; CHECK-NEXT:  .section  .sbss,"aw",@nobits
-@bar = global %struct.anon zeroinitializer 
+; COMMON:  .type bar,@object
+; COMMON-NEXT:  .section  .sbss,"aw",@nobits
+@bar = global %struct.anon zeroinitializer
 
 define i8* @A0() nounwind {
 entry: