[llvm-commits] [llvm] r54842 - in /llvm/trunk: include/llvm/Target/ELFTargetAsmInfo.h include/llvm/Target/TargetAsmInfo.h lib/Target/ELFTargetAsmInfo.cpp lib/Target/Sparc/SparcTargetAsmInfo.cpp lib/Target/Sparc/SparcTargetAsmInfo.h lib/Target/TargetAsmInfo.cpp lib/Target/X86/X86TargetAsmInfo.cpp lib/Target/X86/X86TargetAsmInfo.h

Anton Korobeynikov asl at math.spbu.ru
Sat Aug 16 05:57:10 PDT 2008


Author: asl
Date: Sat Aug 16 07:57:07 2008
New Revision: 54842

URL: http://llvm.org/viewvc/llvm-project?rev=54842&view=rev
Log:
Reduce heap trashing due to std::string construction / concatenation via caching of section flags string representations

Modified:
    llvm/trunk/include/llvm/Target/ELFTargetAsmInfo.h
    llvm/trunk/include/llvm/Target/TargetAsmInfo.h
    llvm/trunk/lib/Target/ELFTargetAsmInfo.cpp
    llvm/trunk/lib/Target/Sparc/SparcTargetAsmInfo.cpp
    llvm/trunk/lib/Target/Sparc/SparcTargetAsmInfo.h
    llvm/trunk/lib/Target/TargetAsmInfo.cpp
    llvm/trunk/lib/Target/X86/X86TargetAsmInfo.cpp
    llvm/trunk/lib/Target/X86/X86TargetAsmInfo.h

Modified: llvm/trunk/include/llvm/Target/ELFTargetAsmInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/ELFTargetAsmInfo.h?rev=54842&r1=54841&r2=54842&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Target/ELFTargetAsmInfo.h (original)
+++ llvm/trunk/include/llvm/Target/ELFTargetAsmInfo.h Sat Aug 16 07:57:07 2008
@@ -27,7 +27,7 @@
     explicit ELFTargetAsmInfo(const TargetMachine &TM);
 
     virtual const Section* SelectSectionForGlobal(const GlobalValue *GV) const;
-    virtual std::string PrintSectionFlags(unsigned flags) const;
+    virtual std::string printSectionFlags(unsigned flags) const;
     const Section* MergeableConstSection(const GlobalVariable *GV) const;
     inline const Section* MergeableConstSection(const Type *Ty) const;
     const Section* MergeableStringSection(const GlobalVariable *GV) const;

Modified: llvm/trunk/include/llvm/Target/TargetAsmInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetAsmInfo.h?rev=54842&r1=54841&r2=54842&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Target/TargetAsmInfo.h (original)
+++ llvm/trunk/include/llvm/Target/TargetAsmInfo.h Sat Aug 16 07:57:07 2008
@@ -16,6 +16,7 @@
 #ifndef LLVM_TARGET_ASM_INFO_H
 #define LLVM_TARGET_ASM_INFO_H
 
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/Support/DataTypes.h"
 #include <string>
@@ -83,6 +84,16 @@
     static inline unsigned setEntitySize(unsigned Flags, unsigned Size) {
       return ((Flags & ~EntitySize) | ((Size & 0xFF) << 24));
     }
+
+    struct KeyInfo {
+      static inline unsigned getEmptyKey() { return Invalid; }
+      static inline unsigned getTombstoneKey() { return Invalid - 1; }
+      static unsigned getHashValue(const unsigned &Key) { return Key; }
+      static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
+      static bool isPod() { return true; }
+    };
+
+    typedef DenseMap<unsigned, std::string, KeyInfo> FlagsStringsMapType;
   }
 
   class TargetMachine;
@@ -109,6 +120,7 @@
   class TargetAsmInfo {
   private:
     mutable StringMap<Section> Sections;
+    mutable SectionFlags::FlagsStringsMapType FlagsStrings;
   protected:
     //===------------------------------------------------------------------===//
     // Properties to be set by the target writer, used to configure asm printer.
@@ -551,7 +563,8 @@
     virtual std::string UniqueSectionForGlobal(const GlobalValue* GV,
                                                SectionKind::Kind kind) const;
 
-    virtual std::string PrintSectionFlags(unsigned flags) const { return ""; }
+    const std::string& getSectionFlags(unsigned Flags) const;
+    virtual std::string printSectionFlags(unsigned flags) const { return ""; }
 
     virtual const Section* SelectSectionForGlobal(const GlobalValue *GV) const;
 

Modified: llvm/trunk/lib/Target/ELFTargetAsmInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ELFTargetAsmInfo.cpp?rev=54842&r1=54841&r2=54842&view=diff

==============================================================================
--- llvm/trunk/lib/Target/ELFTargetAsmInfo.cpp (original)
+++ llvm/trunk/lib/Target/ELFTargetAsmInfo.cpp Sat Aug 16 07:57:07 2008
@@ -148,7 +148,7 @@
   return getReadOnlySection_();
 }
 
-std::string ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
+std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
   std::string Flags = ",\"";
 
   if (!(flags & SectionFlags::Debug))

Modified: llvm/trunk/lib/Target/Sparc/SparcTargetAsmInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/Sparc/SparcTargetAsmInfo.cpp?rev=54842&r1=54841&r2=54842&view=diff

==============================================================================
--- llvm/trunk/lib/Target/Sparc/SparcTargetAsmInfo.cpp (original)
+++ llvm/trunk/lib/Target/Sparc/SparcTargetAsmInfo.cpp Sat Aug 16 07:57:07 2008
@@ -27,9 +27,9 @@
   CStringSection=".rodata.str";
 }
 
-std::string SparcELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
+std::string SparcELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
   if (flags & SectionFlags::Mergeable)
-    return ELFTargetAsmInfo::PrintSectionFlags(flags);
+    return ELFTargetAsmInfo::printSectionFlags(flags);
 
   std::string Flags;
   if (!(flags & SectionFlags::Debug))

Modified: llvm/trunk/lib/Target/Sparc/SparcTargetAsmInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/Sparc/SparcTargetAsmInfo.h?rev=54842&r1=54841&r2=54842&view=diff

==============================================================================
--- llvm/trunk/lib/Target/Sparc/SparcTargetAsmInfo.h (original)
+++ llvm/trunk/lib/Target/Sparc/SparcTargetAsmInfo.h Sat Aug 16 07:57:07 2008
@@ -25,7 +25,7 @@
   struct SparcELFTargetAsmInfo : public ELFTargetAsmInfo {
     explicit SparcELFTargetAsmInfo(const TargetMachine &TM);
 
-    std::string PrintSectionFlags(unsigned flags) const;
+    std::string printSectionFlags(unsigned flags) const;
   };
 
 } // namespace llvm

Modified: llvm/trunk/lib/Target/TargetAsmInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/TargetAsmInfo.cpp?rev=54842&r1=54841&r2=54842&view=diff

==============================================================================
--- llvm/trunk/lib/Target/TargetAsmInfo.cpp (original)
+++ llvm/trunk/lib/Target/TargetAsmInfo.cpp Sat Aug 16 07:57:07 2008
@@ -291,7 +291,7 @@
   // If section is named we need to switch into it via special '.section'
   // directive and also append funky flags. Otherwise - section name is just
   // some magic assembler directive.
-  return getSwitchToSectionDirective() + S->Name + PrintSectionFlags(S->Flags);
+  return getSwitchToSectionDirective() + S->Name + getSectionFlags(S->Flags);
 }
 
 // Lame default implementation. Calculate the section name for global.
@@ -376,3 +376,16 @@
 
   return &S;
 }
+
+const std::string&
+TargetAsmInfo::getSectionFlags(unsigned Flags) const {
+  SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags);
+
+  // We didn't print these flags yet, print and save them to map. This reduces
+  // amount of heap trashing due to std::string construction / concatenation.
+  if (I == FlagsStrings.end())
+    I = FlagsStrings.insert(std::make_pair(Flags,
+                                           printSectionFlags(Flags))).first;
+
+  return I->second;
+}

Modified: llvm/trunk/lib/Target/X86/X86TargetAsmInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86TargetAsmInfo.cpp?rev=54842&r1=54841&r2=54842&view=diff

==============================================================================
--- llvm/trunk/lib/Target/X86/X86TargetAsmInfo.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86TargetAsmInfo.cpp Sat Aug 16 07:57:07 2008
@@ -404,7 +404,7 @@
   }
 }
 
-std::string X86COFFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
+std::string X86COFFTargetAsmInfo::printSectionFlags(unsigned flags) const {
   std::string Flags = ",\"";
 
   if (flags & SectionFlags::Code)

Modified: llvm/trunk/lib/Target/X86/X86TargetAsmInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86TargetAsmInfo.h?rev=54842&r1=54841&r2=54842&view=diff

==============================================================================
--- llvm/trunk/lib/Target/X86/X86TargetAsmInfo.h (original)
+++ llvm/trunk/lib/Target/X86/X86TargetAsmInfo.h Sat Aug 16 07:57:07 2008
@@ -52,7 +52,7 @@
                                            bool Global) const;
     virtual std::string UniqueSectionForGlobal(const GlobalValue* GV,
                                                SectionKind::Kind kind) const;
-    virtual std::string PrintSectionFlags(unsigned flags) const;
+    virtual std::string printSectionFlags(unsigned flags) const;
   protected:
     const X86TargetMachine *X86TM;
   };





More information about the llvm-commits mailing list