It's not legal to output a GV in a coalesced section if it's used in an ARM PIC relative constantpool.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54519 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/ARM/ARMAsmPrinter.cpp b/lib/Target/ARM/ARMAsmPrinter.cpp
index 3b30f9c..420ccd0 100644
--- a/lib/Target/ARM/ARMAsmPrinter.cpp
+++ b/lib/Target/ARM/ARMAsmPrinter.cpp
@@ -29,6 +29,7 @@
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/Compiler.h"
@@ -65,14 +66,18 @@
     typedef std::map<const Value *, unsigned> ValueMapTy;
     ValueMapTy NumberForBB;
 
-    /// Keeps the set of GlobalValues that require non-lazy-pointers for
-    /// indirect access.
+    /// GVNonLazyPtrs - Keeps the set of GlobalValues that require
+    /// non-lazy-pointers for indirect access.
     std::set<std::string> GVNonLazyPtrs;
 
-    /// Keeps the set of external function GlobalAddresses that the asm
-    /// printer should generate stubs for.
+    /// FnStubs - Keeps the set of external function GlobalAddresses that the
+    /// asm printer should generate stubs for.
     std::set<std::string> FnStubs;
 
+    /// PCRelGVs - Keeps the set of GlobalValues used in pc relative
+    /// constantpool.
+    SmallPtrSet<const GlobalValue*, 8> PCRelGVs;
+
     /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
     bool InCPMode;
     
@@ -124,10 +129,12 @@
     /// specified function body into.
     virtual std::string getSectionForFunction(const Function &F) const;
 
+    /// EmitMachineConstantPoolValue - Print a machine constantpool value to
+    /// the .s file.
     virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
       printDataDirective(MCPV->getType());
 
-      ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MCPV;
+      ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
       GlobalValue *GV = ACPV->getGV();
       std::string Name = GV ? Mang->getValueName(GV) : TAI->getGlobalPrefix();
       if (!GV)
@@ -680,9 +687,15 @@
     const MachineConstantPoolEntry &MCPE =  // Chasing pointers is fun?
       MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
     
-    if (MCPE.isMachineConstantPoolEntry())
+    if (MCPE.isMachineConstantPoolEntry()) {
       EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
-    else {
+      ARMConstantPoolValue *ACPV =
+        static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
+      if (ACPV->getPCAdjustment() != 0) {
+        const GlobalValue *GV = ACPV->getGV();
+        PCRelGVs.insert(GV);
+      }
+    } else {
       EmitGlobalConstant(MCPE.Val.ConstVal);
       // remember to emit the weak reference
       if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
@@ -850,7 +863,8 @@
     return;
   }
 
-  std::string SectionName = TAI->SectionForGlobal(GVar);
+  bool NoCoalesc = PCRelGVs.count(GVar);
+  std::string SectionName = TAI->SectionForGlobal(GVar, NoCoalesc);
   std::string name = Mang->getValueName(GVar);
   Constant *C = GVar->getInitializer();
   const Type *Type = C->getType();
@@ -887,7 +901,7 @@
       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
 
       if (TAI->getLCOMMDirective() != NULL) {
-        if (GVar->hasInternalLinkage()) {
+        if (NoCoalesc || GVar->hasInternalLinkage()) {
           O << TAI->getLCOMMDirective() << name << "," << Size;
           if (Subtarget->isTargetDarwin())
             O << "," << Align;
diff --git a/lib/Target/DarwinTargetAsmInfo.cpp b/lib/Target/DarwinTargetAsmInfo.cpp
index 749cb71..978f5cf 100644
--- a/lib/Target/DarwinTargetAsmInfo.cpp
+++ b/lib/Target/DarwinTargetAsmInfo.cpp
@@ -51,14 +51,15 @@
 }
 
 const Section*
-DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
+DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
+                                            bool NoCoalesce) const {
   SectionKind::Kind Kind = SectionKindForGlobal(GV);
-  bool isWeak = GV->isWeakForLinker();
+  bool CanCoalesce = !NoCoalesce && GV->isWeakForLinker();
   bool isNonStatic = (DTM->getRelocationModel() != Reloc::Static);
 
   switch (Kind) {
    case SectionKind::Text:
-    if (isWeak)
+    if (CanCoalesce)
       return TextCoalSection;
     else
       return getTextSection_();
@@ -67,18 +68,18 @@
    case SectionKind::BSS:
    case SectionKind::ThreadBSS:
     if (cast<GlobalVariable>(GV)->isConstant())
-      return (isWeak ? ConstDataCoalSection : ConstDataSection);
+      return (CanCoalesce ? ConstDataCoalSection : ConstDataSection);
     else
-      return (isWeak ? DataCoalSection : getDataSection_());
+      return (CanCoalesce ? DataCoalSection : getDataSection_());
    case SectionKind::ROData:
-    return (isWeak ? ConstDataCoalSection :
+    return (CanCoalesce ? ConstDataCoalSection :
             (isNonStatic ? ConstDataSection : getReadOnlySection_()));
    case SectionKind::RODataMergeStr:
-    return (isWeak ?
+    return (CanCoalesce ?
             ConstDataCoalSection :
             MergeableStringSection(cast<GlobalVariable>(GV)));
    case SectionKind::RODataMergeConst:
-    return (isWeak ?
+    return (CanCoalesce ?
             ConstDataCoalSection:
             MergeableConstSection(cast<GlobalVariable>(GV)));
    default:
diff --git a/lib/Target/ELFTargetAsmInfo.cpp b/lib/Target/ELFTargetAsmInfo.cpp
index 82ac847..523527c 100644
--- a/lib/Target/ELFTargetAsmInfo.cpp
+++ b/lib/Target/ELFTargetAsmInfo.cpp
@@ -40,7 +40,8 @@
 }
 
 const Section*
-ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
+ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
+                                         bool NoCoalesce) const {
   SectionKind::Kind Kind = SectionKindForGlobal(GV);
 
   if (const Function *F = dyn_cast<Function>(GV)) {
diff --git a/lib/Target/Mips/MipsTargetAsmInfo.cpp b/lib/Target/Mips/MipsTargetAsmInfo.cpp
index 04edd0d..eff69ab 100644
--- a/lib/Target/Mips/MipsTargetAsmInfo.cpp
+++ b/lib/Target/Mips/MipsTargetAsmInfo.cpp
@@ -82,7 +82,7 @@
 }
 
 const Section* MipsTargetAsmInfo::
-SelectSectionForGlobal(const GlobalValue *GV) const {
+SelectSectionForGlobal(const GlobalValue *GV, bool NoCoalesce) const {
   SectionKind::Kind K = SectionKindForGlobal(GV);
   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
 
diff --git a/lib/Target/Mips/MipsTargetAsmInfo.h b/lib/Target/Mips/MipsTargetAsmInfo.h
index 2b5a739..806cda8 100644
--- a/lib/Target/Mips/MipsTargetAsmInfo.h
+++ b/lib/Target/Mips/MipsTargetAsmInfo.h
@@ -40,7 +40,8 @@
     SectionFlagsForGlobal(const GlobalValue *GV = NULL,
                           const char* name = NULL) const;
 
-    virtual const Section* SelectSectionForGlobal(const GlobalValue *GV) const;
+    virtual const Section* SelectSectionForGlobal(const GlobalValue *GV,
+                                                  bool NoCoalesce) const;
 
     private:
       const MipsSubtarget *Subtarget;
diff --git a/lib/Target/TargetAsmInfo.cpp b/lib/Target/TargetAsmInfo.cpp
index 9bc0b40..cf43112 100644
--- a/lib/Target/TargetAsmInfo.cpp
+++ b/lib/Target/TargetAsmInfo.cpp
@@ -272,7 +272,7 @@
 }
 
 std::string
-TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
+TargetAsmInfo::SectionForGlobal(const GlobalValue *GV, bool NoCoalesce) const {
   const Section* S;
   // Select section name
   if (GV->hasSection()) {
@@ -282,7 +282,7 @@
     S = getNamedSection(GV->getSection().c_str(), Flags);
   } else {
     // Use default section depending on the 'type' of global
-    S = SelectSectionForGlobal(GV);
+    S = SelectSectionForGlobal(GV, NoCoalesce);
   }
 
   if (!S->isNamed())
@@ -295,8 +295,8 @@
 }
 
 // Lame default implementation. Calculate the section name for global.
-const Section*
-TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
+const Section* TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
+                                                     bool NoCoalesce) const {
   SectionKind::Kind Kind = SectionKindForGlobal(GV);
 
   if (GV->isWeakForLinker()) {