[llvm-commits] [gcc-plugin] r82546 - in /gcc-plugin/trunk: llvm-backend.cpp llvm-convert.cpp llvm-internal.h

Duncan Sands baldrick at free.fr
Tue Sep 22 07:46:32 PDT 2009


Author: baldrick
Date: Tue Sep 22 09:46:29 2009
New Revision: 82546

URL: http://llvm.org/viewvc/llvm-project?rev=82546&view=rev
Log:
Collect statistics on the amount of gcc statements and
basic blocks converted.  Run the garbage collector if
it seems like it would free up a lot of memory.

Modified:
    gcc-plugin/trunk/llvm-backend.cpp
    gcc-plugin/trunk/llvm-convert.cpp
    gcc-plugin/trunk/llvm-internal.h

Modified: gcc-plugin/trunk/llvm-backend.cpp
URL: http://llvm.org/viewvc/llvm-project/gcc-plugin/trunk/llvm-backend.cpp?rev=82546&r1=82545&r2=82546&view=diff

==============================================================================
--- gcc-plugin/trunk/llvm-backend.cpp (original)
+++ gcc-plugin/trunk/llvm-backend.cpp Tue Sep 22 09:46:29 2009
@@ -20,6 +20,7 @@
 02111-1307, USA.  */
 
 // LLVM headers
+#define DEBUG_TYPE "plugin"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/LLVMContext.h"
@@ -41,6 +42,7 @@
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Transforms/IPO.h"
+#include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -92,6 +94,12 @@
 #include "llvm-cache.h"
 }
 
+// TODO: Space aliens beamed these numbers into my head.  Replace with something
+// more down-to-earth.
+#define ESTIMATED_MEMORY_PER_BASIC_BLOCK	1500
+#define ESTIMATED_MEMORY_PER_GIMPLE_STATEMENT	128
+#define MIN_BYTES_WORTH_GARBAGE_COLLECTING	(1024*1024)
+
 // Non-zero if bytecode from PCH is successfully read.
 int flag_llvm_pch_read;
 
@@ -136,6 +144,49 @@
 static void createPerModuleOptimizationPasses();
 //TODOstatic void destroyOptimizationPasses();
 
+
+//===----------------------------------------------------------------------===//
+//                                Statistics
+//===----------------------------------------------------------------------===//
+
+STATISTIC(NumBasicBlocks, "Number of basic blocks converted");
+STATISTIC(NumStatements,  "Number of gimple statements converted");
+
+/// NoteBasicBlock - Called once for each GCC basic block converted.
+void NoteBasicBlock(basic_block bb) {
+  ++NumBasicBlocks;
+}
+
+/// NoteStatement - Called once for each GCC gimple statement converted.
+void NoteStatement(gimple stmt) {
+  ++NumStatements;
+}
+
+static size_t LastNumBasicBlocks;
+static size_t LastNumStatements;
+
+/// EstimatedCollectableGCCMemory - Return an estimate of the amount of memory
+/// we think the GCC garbage collector would free if we ran it.
+static size_t EstimatedCollectableGCCMemory() {
+  return
+    (NumBasicBlocks - LastNumBasicBlocks) * ESTIMATED_MEMORY_PER_BASIC_BLOCK +
+    (NumStatements - LastNumStatements) * ESTIMATED_MEMORY_PER_GIMPLE_STATEMENT;
+}
+
+/// isWorthGarbageCollecting - Returns whether running the GCC garbage collector
+/// would free up enough memory to make it worthwhile.
+static bool isWorthGarbageCollecting() {
+  return EstimatedCollectableGCCMemory() > MIN_BYTES_WORTH_GARBAGE_COLLECTING;
+}
+
+/// ResetGarbageCollectionStatistics - The memory estimated by the previous
+/// statistics will be garbage collected.  Reset the statistics.
+static void ResetGarbageCollectionStatistics() {
+  LastNumBasicBlocks = NumBasicBlocks;
+  LastNumStatements = NumStatements;
+}
+
+
 //===----------------------------------------------------------------------===//
 //                   Matching LLVM Values with GCC DECL trees
 //===----------------------------------------------------------------------===//
@@ -337,6 +388,8 @@
 
   if (time_report || !quiet_flag  || flag_detailed_statistics)
     Args.push_back("--time-passes");
+  if (!quiet_flag  || flag_detailed_statistics)
+    Args.push_back("--stats");
   if (fast_math_flags_set_p())
     Args.push_back("--enable-unsafe-fp-math");
   if (!flag_omit_frame_pointer)
@@ -1704,6 +1757,13 @@
 static unsigned int emit_function (void) {
   LazilyInitializeModule();
 
+  // The previously converted function is now garbage collectable.  If it seems
+  // worthwhile, run the garbage collector after converting this function (the
+  // current function will not itself be collected though).
+  ggc_force_collect = isWorthGarbageCollecting();
+  if (ggc_force_collect)
+    ResetGarbageCollectionStatistics();
+
 //TODO Don't want to use sorry at this stage...
 //TODO  if (cfun->nonlocal_goto_save_area)
 //TODO    sorry("%Jnon-local gotos not supported by LLVM", fndecl);
@@ -1787,6 +1847,15 @@
 
   LazilyInitializeModule();
 
+  // If it seems worthwhile, garbage collect any functions we converted before
+  // running the optimizers or generating code.
+  if (isWorthGarbageCollecting()) {
+    ResetGarbageCollectionStatistics();
+    ggc_force_collect = 1;
+    ggc_collect();
+    ggc_force_collect = 0;
+  }
+
 //TODO  timevar_push(TV_LLVM_PERFILE);
   LLVMContext &Context = getGlobalContext();
 

Modified: gcc-plugin/trunk/llvm-convert.cpp
URL: http://llvm.org/viewvc/llvm-project/gcc-plugin/trunk/llvm-convert.cpp?rev=82546&r1=82545&r2=82546&view=diff

==============================================================================
--- gcc-plugin/trunk/llvm-convert.cpp (original)
+++ gcc-plugin/trunk/llvm-convert.cpp Tue Sep 22 09:46:29 2009
@@ -910,6 +910,8 @@
 }
 
 void TreeToLLVM::EmitBasicBlock(basic_block bb) {
+  NoteBasicBlock(bb);
+
   // Avoid outputting a pointless branch at the end of the entry block.
   if (bb != ENTRY_BLOCK_PTR)
     EmitBlock(getBasicBlock(bb));
@@ -943,23 +945,24 @@
   // Render statements.
   for (gimple_stmt_iterator gsi = gsi_start_bb(bb); !gsi_end_p(gsi);
        gsi_next(&gsi)) {
-    gimple gimple_stmt = gsi_stmt(gsi);
+    gimple stmt = gsi_stmt(gsi);
+    NoteStatement(stmt);
 
-    switch (gimple_code(gimple_stmt)) {
+    switch (gimple_code(stmt)) {
     case GIMPLE_ASM:
-      RenderGIMPLE_ASM(gimple_stmt);
+      RenderGIMPLE_ASM(stmt);
       break;
 
     case GIMPLE_ASSIGN:
-      RenderGIMPLE_ASSIGN(gimple_stmt);
+      RenderGIMPLE_ASSIGN(stmt);
       break;
 
     case GIMPLE_CALL:
-       RenderGIMPLE_CALL(gimple_stmt);
+       RenderGIMPLE_CALL(stmt);
        break;
 
     case GIMPLE_COND:
-      RenderGIMPLE_COND(gimple_stmt);
+      RenderGIMPLE_COND(stmt);
       break;
 
     case GIMPLE_DEBUG:
@@ -967,7 +970,7 @@
       break;
 
     case GIMPLE_GOTO:
-      RenderGIMPLE_GOTO(gimple_stmt);
+      RenderGIMPLE_GOTO(stmt);
       break;
 
     case GIMPLE_LABEL:
@@ -976,19 +979,19 @@
       break;
 
     case GIMPLE_RESX:
-      RenderGIMPLE_RESX(gimple_stmt);
+      RenderGIMPLE_RESX(stmt);
       break;
 
     case GIMPLE_RETURN:
-      RenderGIMPLE_RETURN(gimple_stmt);
+      RenderGIMPLE_RETURN(stmt);
       break;
 
     case GIMPLE_SWITCH:
-      RenderGIMPLE_SWITCH(gimple_stmt);
+      RenderGIMPLE_SWITCH(stmt);
       break;
 
     default:
-      dump(gimple_stmt);
+      dump(stmt);
       llvm_unreachable("Unhandled GIMPLE statement during LLVM emission!");
     }
   }

Modified: gcc-plugin/trunk/llvm-internal.h
URL: http://llvm.org/viewvc/llvm-project/gcc-plugin/trunk/llvm-internal.h?rev=82546&r1=82545&r2=82546&view=diff

==============================================================================
--- gcc-plugin/trunk/llvm-internal.h (original)
+++ gcc-plugin/trunk/llvm-internal.h Tue Sep 22 09:46:29 2009
@@ -97,6 +97,14 @@
 /// annotate attribute to a vector to be emitted later.
 extern void AddAnnotateAttrsToGlobal(GlobalValue *GV, union tree_node* decl);
 
+// Statistics.
+
+/// NoteBasicBlock - Called once for each GCC basic block converted.
+extern void NoteBasicBlock(basic_block bb);
+
+/// NoteStatement - Called once for each GCC gimple statement converted.
+extern void NoteStatement(gimple stmt);
+
 // Mapping between GCC declarations and LLVM values.
 
 /// DECL_LLVM - Holds the LLVM expression for the value of a variable or





More information about the llvm-commits mailing list