[llvm-commits] [poolalloc] r129005 - in /poolalloc/trunk: include/poolalloc/PoolAllocate.h lib/PoolAllocate/PASimple.cpp lib/PoolAllocate/PoolAllocate.cpp lib/PoolAllocate/TransformFunctionBody.cpp

Matthew Wala mttjwl at gmail.com
Wed Apr 6 09:06:59 PDT 2011


Author: wala1
Date: Wed Apr  6 11:06:59 2011
New Revision: 129005

URL: http://llvm.org/viewvc/llvm-project?rev=129005&view=rev
Log:
Added CStdLib function argument count information to PoolAllocate.
This consolidates previously redundant pool allocation code.


Modified:
    poolalloc/trunk/include/poolalloc/PoolAllocate.h
    poolalloc/trunk/lib/PoolAllocate/PASimple.cpp
    poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp
    poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp

Modified: poolalloc/trunk/include/poolalloc/PoolAllocate.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/poolalloc/PoolAllocate.h?rev=129005&r1=129004&r2=129005&view=diff
==============================================================================
--- poolalloc/trunk/include/poolalloc/PoolAllocate.h (original)
+++ poolalloc/trunk/include/poolalloc/PoolAllocate.h Wed Apr  6 11:06:59 2011
@@ -27,6 +27,7 @@
 #include "llvm/ADT/VectorExtras.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/Support/CommandLine.h"
 
 #include "dsa/DataStructure.h"
@@ -208,6 +209,10 @@
   
   // Map a cloned function to its original function
   std::map<const Function*, Function*> CloneToOrigMap;
+
+  // Map a CStdLib function name to its pool argument count.
+  StringMap<unsigned> CStdLibPoolArgs;
+
 public:
 
   Constant *PoolInit, *PoolDestroy, *PoolAlloc, *PoolRealloc, *PoolMemAlign, *PoolThreadWrapper;
@@ -242,6 +247,8 @@
 		  SAFECodeEnabled = SAFECode |  PA::PA_SAFECODE;
 		  lie_preserve_passes = SAFECodeEnabled ? LIE_PRESERVE_ALL : LIE_PRESERVE_DSA;
 		  dsa_pass_to_use = SAFECodeEnabled ? PASS_EQTD : PASS_BUEQ;
+
+      InitializeCStdLibPoolArgs();
       }
 
   /*TODO: finish removing the SAFECode flag*/
@@ -264,6 +271,8 @@
   			  dsa_pass_to_use = SAFECodeEnabled ? PASS_EQTD : PASS_BUEQ;
   		  else
   			  dsa_pass_to_use = dsa_pass_to_use_;
+
+        InitializeCStdLibPoolArgs();
         }
 
   virtual bool runOnModule(Module &M);
@@ -417,6 +426,9 @@
       return I->second;
   }
 
+  // Get the initial pool argument count for a CStdLib function.
+  unsigned getCStdLibPoolArguments(StringRef funcname);
+
 protected:
   
   /// AddPoolPrototypes - Add prototypes for the pool functions to the
@@ -427,6 +439,9 @@
 
  private:
 
+  /// Initialiaze the pool argument counts.
+  void InitializeCStdLibPoolArgs();
+
   /// MicroOptimizePoolCalls - Apply any microoptimizations to calls to pool
   /// allocation function calls that we can.
   void MicroOptimizePoolCalls();

Modified: poolalloc/trunk/lib/PoolAllocate/PASimple.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/PASimple.cpp?rev=129005&r1=129004&r2=129005&view=diff
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/PASimple.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/PASimple.cpp Wed Apr  6 11:06:59 2011
@@ -103,21 +103,6 @@
     return 1;
   }
   
-  // CStdLib functions
-
-  else if ( ( funcname == "pool_strlen"  ) ||
-            ( funcname == "pool_strchr"  ) ||
-            ( funcname == "pool_strrchr" ) ) {
-    return 1;
-  }
-  else if ( ( funcname == "pool_strcpy"  ) ||
-            ( funcname == "pool_strncat" ) ||
-            ( funcname == "pool_strcat"  ) ||
-            ( funcname == "pool_strstr"  ) ||
-            ( funcname == "pool_strpbrk" ) ) {
-    return 2;
-  }
-
   return 0;
 }
 
@@ -466,12 +451,13 @@
         // pool.
         //
         if (CF) {
-          if (unsigned count = initialPoolArguments (CF->getName())) {
+          unsigned count;
+          if ((count = initialPoolArguments(CF->getName())) || \
+                (count = getCStdLibPoolArguments(CF->getName()))) {
             Type * VoidPtrTy = PointerType::getUnqual(Int8Type);
             Value * Pool = castTo (TheGlobalPool, VoidPtrTy, "pool", ii);
-            for (unsigned index = 1; index <= count; index++ ) {
+            for (unsigned index = 1; index <= count; index++ )
               CI->setOperand (index, Pool);
-            }
           }
         }
       }

Modified: poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp?rev=129005&r1=129004&r2=129005&view=diff
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp Wed Apr  6 11:06:59 2011
@@ -1563,3 +1563,31 @@
   }
 }
 
+// Builds the StringMap that holds information about the pool
+// argument counts of C standard library functions.
+void PoolAllocate::InitializeCStdLibPoolArgs()
+{
+  CStdLibPoolArgs.GetOrCreateValue("pool_strcpy",   2);
+  CStdLibPoolArgs.GetOrCreateValue("pool_strlen",   1);
+  CStdLibPoolArgs.GetOrCreateValue("pool_strchr",   1);
+  CStdLibPoolArgs.GetOrCreateValue("pool_strrchr",  1);
+  CStdLibPoolArgs.GetOrCreateValue("pool_strcat",   2);
+  CStdLibPoolArgs.GetOrCreateValue("pool_strncat",  2);
+  CStdLibPoolArgs.GetOrCreateValue("pool_strstr",   2);
+  CStdLibPoolArgs.GetOrCreateValue("pool_strpbrk",  2);
+  //CStdLibPoolArgs.GetOrCreateValue("pool_strtok",   2);
+  //CStdLibPoolArgs.GetOrCreateValue("pool_strtok_r", 2);
+  //CStdLibPoolArgs.GetOrCreateValue("pool_strspn",   2);
+  //CStdLibPoolArgs.GetOrCreateValue("pool_strcspn",  2);
+}
+
+// Return the number of initial pool arguments for the specified CStdLib
+// function, or 0 if it is not found in the table.
+unsigned PoolAllocate::getCStdLibPoolArguments(StringRef funcname)
+{
+  StringMap<unsigned>::const_iterator argc = CStdLibPoolArgs.find(funcname);
+  if (argc != CStdLibPoolArgs.end())
+    return argc->getValue();
+  else
+    return 0;
+}

Modified: poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp?rev=129005&r1=129004&r2=129005&view=diff
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp Wed Apr  6 11:06:59 2011
@@ -56,7 +56,6 @@
                   std::multimap<AllocaInst*, CallInst*> &poolFrees)
       : PAInfo(P), G(g), FI(fi), 
         PoolUses(poolUses), PoolFrees(poolFrees) {
-      initializeCStdLibPoolArgcs();
     }
 
     template <typename InstType, typename SetType>
@@ -92,23 +91,6 @@
     Instruction *TransformAllocationInstr(Instruction *I, Value *Size);
     Instruction *InsertPoolFreeInstr(Value *V, Instruction *Where);
 
-    // Used for looking up CStdLib function names and their initial pool
-    // argument counts
-    StringMap<unsigned> CStdLibPoolArgcs;
-
-    // Initialize the map from CStdLib function name to initial pool
-    // argument counts.
-    void initializeCStdLibPoolArgcs() {
-      CStdLibPoolArgcs.GetOrCreateValue("pool_strcpy",  2);
-      CStdLibPoolArgcs.GetOrCreateValue("pool_strlen",  1);
-      CStdLibPoolArgcs.GetOrCreateValue("pool_strchr",  1);
-      CStdLibPoolArgcs.GetOrCreateValue("pool_strrchr", 1);
-      CStdLibPoolArgcs.GetOrCreateValue("pool_strcat",  2);
-      CStdLibPoolArgcs.GetOrCreateValue("pool_strncat", 2);
-      CStdLibPoolArgcs.GetOrCreateValue("pool_strstr",  2);
-      CStdLibPoolArgcs.GetOrCreateValue("pool_strpbrk", 2);
-    }
-
     //
     // Method: UpdateNewToOldValueMap()
     //
@@ -818,8 +800,7 @@
   const Function *CF = CS.getCalledFunction();
   Instruction *TheCall = CS.getInstruction();
   bool thread_creation_point = false;
-
-  StringMap<unsigned>::const_iterator pool_argc = CStdLibPoolArgcs.end();
+  unsigned argc;
 
   //
   // Get the value that is called at this call site.  Strip away any pointer
@@ -892,8 +873,8 @@
                (CF->getName() == "sc.pool_unregister") ||
                (CF->getName() == "sc.get_actual_val")) {
       visitRuntimeCheck (CS);
-    } else if ((pool_argc = CStdLibPoolArgcs.find(CF->getName())) != CStdLibPoolArgcs.end()) {
-      visitCStdLibCheck(CS, pool_argc->getValue());
+    } else if ((argc = PAInfo.getCStdLibPoolArguments(CF->getName())) > 0) {
+      visitCStdLibCheck(CS, argc);
     } else if (CF->getName() == "pthread_create") {
       thread_creation_point = true;
 





More information about the llvm-commits mailing list