[mips] Support -membedded-data and fix a related bug

-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

llvm-svn: 308758
diff --git a/llvm/lib/Target/Mips/MipsTargetObjectFile.cpp b/llvm/lib/Target/Mips/MipsTargetObjectFile.cpp
index 4d73c39..3bf32e8 100644
--- a/llvm/lib/Target/Mips/MipsTargetObjectFile.cpp
+++ b/llvm/lib/Target/Mips/MipsTargetObjectFile.cpp
@@ -36,6 +36,12 @@
                      "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(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 @@
                        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 @@
     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);
diff --git a/llvm/test/CodeGen/Mips/2008-07-15-SmallSection.ll b/llvm/test/CodeGen/Mips/2008-07-15-SmallSection.ll
index 79f4d79..2149542 100644
--- a/llvm/test/CodeGen/Mips/2008-07-15-SmallSection.ll
+++ b/llvm/test/CodeGen/Mips/2008-07-15-SmallSection.ll
@@ -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: