PR14728: DebugInfo: TLS variables with -gsplit-dwarf

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@185398 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
index 339a5d2..f42a33e 100644
--- a/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
+++ b/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
@@ -201,15 +201,12 @@
 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
 ///
 void CompileUnit::addOpAddress(DIE *Die, const MCSymbol *Sym) {
-
   if (!DD->useSplitDwarf()) {
     addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
     addLabel(Die, 0, dwarf::DW_FORM_udata, Sym);
   } else {
-    unsigned idx = DU->getAddrPoolIndex(Sym);
-    DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
     addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
-    Die->addValue(0, dwarf::DW_FORM_GNU_addr_index, Value);
+    addUInt(Die, 0, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
   }
 }
 
@@ -1358,13 +1355,19 @@
       unsigned PointerSize = Asm->getDataLayout().getPointerSize();
       assert((PointerSize == 4 || PointerSize == 8) &&
              "Add support for other sizes if necessary");
+      const MCSymbolRefExpr *Ref =
+          Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
       // Based on GCC's support for TLS:
-      // 1) Start with a constNu of the appropriate pointer size
-      addUInt(Block, 0, dwarf::DW_FORM_data1,
-              PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
-      // 2) containing the (relocated) address of the TLS variable
-      addLabel(Block, 0, dwarf::DW_FORM_udata,
-               Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
+      if (!DD->useSplitDwarf()) {
+        // 1) Start with a constNu of the appropriate pointer size
+        addUInt(Block, 0, dwarf::DW_FORM_data1,
+                PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
+        // 2) containing the (relocated) address of the TLS variable
+        addLabel(Block, 0, dwarf::DW_FORM_udata, Ref);
+      } else {
+        addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
+        addUInt(Block, 0, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Ref));
+      }
       // 3) followed by a custom OP to tell the debugger about TLS (presumably)
       addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_lo_user);
     } else
diff --git a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index ead90c0..0731b90 100644
--- a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -248,7 +248,11 @@
 }
 
 unsigned DwarfUnits::getAddrPoolIndex(const MCSymbol *Sym) {
-  std::pair<DenseMap<const MCSymbol *, unsigned>::iterator, bool> P =
+  return getAddrPoolIndex(MCSymbolRefExpr::Create(Sym, Asm->OutContext));
+}
+
+unsigned DwarfUnits::getAddrPoolIndex(const MCSymbolRefExpr *Sym) {
+  std::pair<DenseMap<const MCSymbolRefExpr *, unsigned>::iterator, bool> P =
       AddressPool.insert(std::make_pair(Sym, NextAddrPoolNumber));
   if (P.second)
     ++NextAddrPoolNumber;
@@ -2357,10 +2361,11 @@
 
   // Get all of the address pool entries and put them in an array by their ID so
   // we can sort them.
-  SmallVector<std::pair<unsigned, const MCSymbol *>, 64> Entries;
+  SmallVector<std::pair<unsigned, const MCSymbolRefExpr *>, 64> Entries;
 
-  for (DenseMap<const MCSymbol *, unsigned>::iterator I = AddressPool.begin(),
-                                                      E = AddressPool.end();
+  for (DenseMap<const MCSymbolRefExpr *, unsigned>::iterator
+           I = AddressPool.begin(),
+           E = AddressPool.end();
        I != E; ++I)
     Entries.push_back(std::make_pair(I->second, I->first));
 
@@ -2368,8 +2373,8 @@
 
   for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
     // Emit a label for reference from debug information entries.
-    if (const MCSymbol *Sym = Entries[i].second)
-      Asm->EmitLabelReference(Sym, Asm->getDataLayout().getPointerSize());
+    if (const MCSymbolRefExpr *Sym = Entries[i].second)
+      Asm->OutStreamer.EmitValue(Sym, Asm->getDataLayout().getPointerSize());
     else
       Asm->OutStreamer.EmitIntValue(0, Asm->getDataLayout().getPointerSize());
   }
diff --git a/lib/CodeGen/AsmPrinter/DwarfDebug.h b/lib/CodeGen/AsmPrinter/DwarfDebug.h
index 00d48d7..893505c 100644
--- a/lib/CodeGen/AsmPrinter/DwarfDebug.h
+++ b/lib/CodeGen/AsmPrinter/DwarfDebug.h
@@ -197,7 +197,7 @@
 
 // A Symbol->unsigned mapping of addresses used by indirect
 // references.
-typedef DenseMap<const MCSymbol *, unsigned> AddrPool;
+typedef DenseMap<const MCSymbolRefExpr *, unsigned> AddrPool;
 
 /// \brief Collects and handles information specific to a particular
 /// collection of units.
@@ -270,6 +270,7 @@
 
   /// \brief Returns the index into the address pool with the given
   /// label/symbol.
+  unsigned getAddrPoolIndex(const MCSymbolRefExpr *);
   unsigned getAddrPoolIndex(const MCSymbol *);
 
   /// \brief Returns the address pool.