[llvm-commits] [llvm] r55766 - /llvm/trunk/lib/Transforms/IPO/PartialSpecialization.cpp

Andrew Lenharth alenhar2 at cs.uiuc.edu
Thu Sep 4 07:34:23 PDT 2008


Author: alenhar2
Date: Thu Sep  4 09:34:22 2008
New Revision: 55766

URL: http://llvm.org/viewvc/llvm-project?rev=55766&view=rev
Log:
cleanup as per Duncan's review

Modified:
    llvm/trunk/lib/Transforms/IPO/PartialSpecialization.cpp

Modified: llvm/trunk/lib/Transforms/IPO/PartialSpecialization.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PartialSpecialization.cpp?rev=55766&r1=55765&r2=55766&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/IPO/PartialSpecialization.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/PartialSpecialization.cpp Thu Sep  4 09:34:22 2008
@@ -10,7 +10,11 @@
 // This pass finds function arguments that are often a common constant and 
 // specializes a version of the called function for that constant.
 //
-// The initial heuristic favors constant arguments that used in control flow.
+// This pass simply does the cloning for functions it specializes.  It depends
+// on IPSCCP and DAE to clean up the results.
+//
+// The initial heuristic favors constant arguments that are used in control 
+// flow.
 //
 //===----------------------------------------------------------------------===//
 
@@ -24,19 +28,19 @@
 #include "llvm/Transforms/Utils/Cloning.h"
 #include "llvm/Support/Compiler.h"
 #include <map>
-#include <vector>
 using namespace llvm;
 
 STATISTIC(numSpecialized, "Number of specialized functions created");
 
-//Call must be used at least occasionally
+// Call must be used at least occasionally
 static const int CallsMin = 5;
-//Must have 10% of calls having the same constant to specialize on
+
+// Must have 10% of calls having the same constant to specialize on
 static const double ConstValPercent = .1;
 
 namespace {
   class VISIBILITY_HIDDEN PartSpec : public ModulePass {
-    void scanForInterest(Function&, std::vector<int>&);
+    void scanForInterest(Function&, SmallVector<int, 6>&);
     void replaceUsersFor(Function&, int, Constant*, Function*);
     int scanDistribution(Function&, int, std::map<Constant*, int>&);
   public :
@@ -54,41 +58,43 @@
   bool Changed = false;
   for (Module::iterator I = M.begin(); I != M.end(); ++I) {
     Function &F = *I;
-    if (!F.isDeclaration()) {
-      std::vector<int> interestingArgs;
-      scanForInterest(F, interestingArgs);
-      //Find the first interesting Argument that we can specialize on
-      //If there are multiple intersting Arguments, then those will be found
-      //when processing the cloned function.
-      bool breakOuter = false;
-      for (unsigned int x = 0; !breakOuter && x < interestingArgs.size(); ++x) {
-        std::map<Constant*, int> distribution;
-        int total = scanDistribution(F, interestingArgs[x], distribution);
-        if (total > CallsMin) 
-          for (std::map<Constant*, int>::iterator ii = distribution.begin(),
-                 ee = distribution.end(); ii != ee; ++ii)
-            if ( total > ii->second  && ii->first &&
-                 ii->second > total * ConstValPercent ) {
-              Function* NF = CloneFunction(&F);
-              NF->setLinkage(GlobalValue::InternalLinkage);
-              M.getFunctionList().push_back(NF);
-              replaceUsersFor(F, interestingArgs[x], ii->first, NF);
-              breakOuter = true;
-              Changed = true;
-            }
-      }
+    if (F.isDeclaration()) continue;
+    SmallVector<int, 6> interestingArgs;
+    scanForInterest(F, interestingArgs);
+
+    // Find the first interesting Argument that we can specialize on
+    // If there are multiple interesting Arguments, then those will be found
+    // when processing the cloned function.
+    bool breakOuter = false;
+    for (unsigned int x = 0; !breakOuter && x < interestingArgs.size(); ++x) {
+      std::map<Constant*, int> distribution;
+      int total = scanDistribution(F, interestingArgs[x], distribution);
+      if (total > CallsMin) 
+        for (std::map<Constant*, int>::iterator ii = distribution.begin(),
+               ee = distribution.end(); ii != ee; ++ii)
+          if (total > ii->second && ii->first &&
+               ii->second > total * ConstValPercent) {
+            Function* NF = CloneFunction(&F);
+            NF->setLinkage(GlobalValue::InternalLinkage);
+            M.getFunctionList().push_back(NF);
+            replaceUsersFor(F, interestingArgs[x], ii->first, NF);
+            breakOuter = true;
+            Changed = true;
+          }
     }
   }
   return Changed;
 }
 
 /// scanForInterest - This function decides which arguments would be worth
-///                    specializing on.
-void PartSpec::scanForInterest(Function& F, std::vector<int>& args) {
+/// specializing on.
+void PartSpec::scanForInterest(Function& F, SmallVector<int, 6>& args) {
   for(Function::arg_iterator ii = F.arg_begin(), ee = F.arg_end();
       ii != ee; ++ii) {
     for(Value::use_iterator ui = ii->use_begin(), ue = ii->use_end();
         ui != ue; ++ui) {
+      // As an initial proxy for control flow, specialize on arguments
+      // that are used in comparisons.
       if (isa<CmpInst>(ui)) {
         args.push_back(std::distance(F.arg_begin(), ii));
         break;
@@ -111,7 +117,7 @@
 
 int PartSpec::scanDistribution(Function& F, int arg, 
                                std::map<Constant*, int>& dist) {
-  bool hasInd = false;
+  bool hasIndirect = false;
   int total = 0;
   for(Value::use_iterator ii = F.use_begin(), ee = F.use_end();
       ii != ee; ++ii)
@@ -119,8 +125,11 @@
       ++dist[dyn_cast<Constant>(CI->getOperand(arg + 1))];
       ++total;
     } else
-      hasInd = true;
-  if (hasInd) ++total;
+      hasIndirect = true;
+
+  // Preserve the original address taken function even if all other uses
+  // will be specialized.
+  if (hasIndirect) ++total;
   return total;
 }
 





More information about the llvm-commits mailing list