[llvm] r238116 - AsmPrinter: Extract DwarfStringPoolEntry from DwarfStringPool, NFC

Duncan P. N. Exon Smith dexonsmith at apple.com
Sun May 24 09:33:33 PDT 2015


Author: dexonsmith
Date: Sun May 24 11:33:33 2015
New Revision: 238116

URL: http://llvm.org/viewvc/llvm-project?rev=238116&view=rev
Log:
AsmPrinter: Extract DwarfStringPoolEntry from DwarfStringPool, NFC

Extract out `DwarfStringPoolEntry` and `DwarfStringPoolRef` from
`DwarfStringPool` so that downstream users can start using
`DwarfStringPool::getEntry()` directly.  This will allow users to delay
the decision between emitting a symbol or an offset until later.

Added:
    llvm/trunk/include/llvm/CodeGen/DwarfStringPoolEntry.h
Modified:
    llvm/trunk/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp
    llvm/trunk/lib/CodeGen/AsmPrinter/DwarfStringPool.h

Added: llvm/trunk/include/llvm/CodeGen/DwarfStringPoolEntry.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/DwarfStringPoolEntry.h?rev=238116&view=auto
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/DwarfStringPoolEntry.h (added)
+++ llvm/trunk/include/llvm/CodeGen/DwarfStringPoolEntry.h Sun May 24 11:33:33 2015
@@ -0,0 +1,45 @@
+//===- llvm/CodeGen/DwarfStringPoolEntry.h - String pool entry --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_DWARFSTRINGPOOLENTRY_H
+#define LLVM_CODEGEN_DWARFSTRINGPOOLENTRY_H
+
+#include "llvm/ADT/StringMap.h"
+
+namespace llvm {
+
+class MCSymbol;
+
+/// Data for a string pool entry.
+struct DwarfStringPoolEntry {
+  MCSymbol *Symbol;
+  unsigned Offset;
+  unsigned Index;
+};
+
+/// String pool entry reference.
+struct DwarfStringPoolEntryRef {
+  const StringMapEntry<DwarfStringPoolEntry> *I = nullptr;
+
+public:
+  DwarfStringPoolEntryRef() = default;
+  explicit DwarfStringPoolEntryRef(
+      const StringMapEntry<DwarfStringPoolEntry> &I)
+      : I(&I) {}
+
+  explicit operator bool() const { return I; }
+  MCSymbol *getSymbol() const { return I->second.Symbol; }
+  unsigned getOffset() const { return I->second.Offset; }
+  unsigned getIndex() const { return I->second.Index; }
+  StringRef getString() const { return I->first(); }
+};
+
+} // end namespace llvm
+
+#endif

Modified: llvm/trunk/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp?rev=238116&r1=238115&r2=238116&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp Sun May 24 11:33:33 2015
@@ -12,10 +12,11 @@
 
 using namespace llvm;
 
-DwarfStringPool::EntryTy &DwarfStringPool::getEntry(AsmPrinter &Asm,
+DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
                                                     StringRef Str) {
-  auto &Entry = Pool[Str];
-  if (!Entry.Symbol) {
+  auto I = Pool.insert(std::make_pair(Str, EntryTy()));
+  if (I.second) {
+    auto &Entry = I.first->second;
     Entry.Index = Pool.size() - 1;
     Entry.Offset = NumBytes;
     Entry.Symbol = Asm.createTempSymbol(Prefix);
@@ -23,7 +24,7 @@ DwarfStringPool::EntryTy &DwarfStringPoo
     NumBytes += Str.size() + 1;
     assert(NumBytes > Entry.Offset && "Unexpected overflow");
   }
-  return Entry;
+  return EntryRef(*I.first);
 }
 
 void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,

Modified: llvm/trunk/lib/CodeGen/AsmPrinter/DwarfStringPool.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/DwarfStringPool.h?rev=238116&r1=238115&r2=238116&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/DwarfStringPool.h (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/DwarfStringPool.h Sun May 24 11:33:33 2015
@@ -12,6 +12,7 @@
 
 #include "llvm/ADT/StringMap.h"
 #include "llvm/CodeGen/AsmPrinter.h"
+#include "llvm/CodeGen/DwarfStringPoolEntry.h"
 #include "llvm/Support/Allocator.h"
 #include <utility>
 
@@ -25,16 +26,14 @@ class StringRef;
 // A String->Symbol mapping of strings used by indirect
 // references.
 class DwarfStringPool {
-  struct EntryTy {
-    MCSymbol *Symbol;
-    unsigned Offset;
-    unsigned Index;
-  };
+  typedef DwarfStringPoolEntry EntryTy;
   StringMap<EntryTy, BumpPtrAllocator &> Pool;
   StringRef Prefix;
   unsigned NumBytes = 0;
 
 public:
+  typedef DwarfStringPoolEntryRef EntryRef;
+
   DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, StringRef Prefix)
       : Pool(A), Prefix(Prefix) {}
 
@@ -44,24 +43,24 @@ public:
   /// \brief Returns an entry into the string pool with the given
   /// string text.
   MCSymbol *getSymbol(AsmPrinter &Asm, StringRef Str) {
-    return getEntry(Asm, Str).Symbol;
+    return getEntry(Asm, Str).getSymbol();
   }
 
   /// Get a byte offset into the string pool with the given text.
   unsigned getOffset(AsmPrinter &Asm, StringRef Str) {
-    return getEntry(Asm, Str).Offset;
+    return getEntry(Asm, Str).getOffset();
   }
 
   /// \brief Returns the index into the string pool with the given
   /// string text.
   unsigned getIndex(AsmPrinter &Asm, StringRef Str) {
-    return getEntry(Asm, Str).Index;
+    return getEntry(Asm, Str).getIndex();
   }
 
   bool empty() const { return Pool.empty(); }
 
-private:
-  EntryTy &getEntry(AsmPrinter &Asm, StringRef Str);
+  /// Get a reference to an entry in the string pool.
+  EntryRef getEntry(AsmPrinter &Asm, StringRef Str);
 };
 }
 #endif





More information about the llvm-commits mailing list