[llvm] r266902 - IR: Avoid mallocs in constructor of ModuleSlotTracker

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 20 12:05:59 PDT 2016


Author: dexonsmith
Date: Wed Apr 20 14:05:59 2016
New Revision: 266902

URL: http://llvm.org/viewvc/llvm-project?rev=266902&view=rev
Log:
IR: Avoid mallocs in constructor of ModuleSlotTracker

A ModuleSlotTracker can be created without actually being used (e.g.,
r266889 added one to the Verifier).  Create the SlotTracker within it
lazily on the first call to ModuleSlotTracker::getMachine.

Modified:
    llvm/trunk/include/llvm/IR/ModuleSlotTracker.h
    llvm/trunk/lib/IR/AsmWriter.cpp

Modified: llvm/trunk/include/llvm/IR/ModuleSlotTracker.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/ModuleSlotTracker.h?rev=266902&r1=266901&r2=266902&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/ModuleSlotTracker.h (original)
+++ llvm/trunk/include/llvm/IR/ModuleSlotTracker.h Wed Apr 20 14:05:59 2016
@@ -30,6 +30,8 @@ class Value;
 class ModuleSlotTracker {
   /// Storage for a slot tracker.
   std::unique_ptr<SlotTracker> MachineStorage;
+  bool ShouldCreateStorage = false;
+  bool ShouldInitializeAllMetadata = false;
 
   const Module *M = nullptr;
   const Function *F = nullptr;
@@ -53,7 +55,9 @@ public:
   /// Destructor to clean up storage.
   ~ModuleSlotTracker();
 
-  SlotTracker *getMachine() const { return Machine; }
+  /// Lazily creates a slot tracker.
+  SlotTracker *getMachine();
+
   const Module *getModule() const { return M; }
   const Function *getCurrentFunction() const { return F; }
 

Modified: llvm/trunk/lib/IR/AsmWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/AsmWriter.cpp?rev=266902&r1=266901&r2=266902&view=diff
==============================================================================
--- llvm/trunk/lib/IR/AsmWriter.cpp (original)
+++ llvm/trunk/lib/IR/AsmWriter.cpp Wed Apr 20 14:05:59 2016
@@ -697,14 +697,25 @@ ModuleSlotTracker::ModuleSlotTracker(Slo
 
 ModuleSlotTracker::ModuleSlotTracker(const Module *M,
                                      bool ShouldInitializeAllMetadata)
-    : MachineStorage(M ? new SlotTracker(M, ShouldInitializeAllMetadata)
-                       : nullptr),
-      M(M), Machine(MachineStorage.get()) {}
+    : ShouldCreateStorage(M),
+      ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), M(M) {}
 
 ModuleSlotTracker::~ModuleSlotTracker() {}
 
+SlotTracker *ModuleSlotTracker::getMachine() {
+  if (!ShouldCreateStorage)
+    return Machine;
+
+  ShouldCreateStorage = false;
+  MachineStorage =
+      llvm::make_unique<SlotTracker>(M, ShouldInitializeAllMetadata);
+  Machine = MachineStorage.get();
+  return Machine;
+}
+
 void ModuleSlotTracker::incorporateFunction(const Function &F) {
-  if (!Machine)
+  // Using getMachine() may lazily create the slot tracker.
+  if (!getMachine())
     return;
 
   // Nothing to do if this is the right function already.




More information about the llvm-commits mailing list