[llvm-commits] [llvm] r55734 - in /llvm/trunk: include/llvm/LinkAllPasses.h include/llvm/Transforms/IPO.h lib/Transforms/IPO/PartialSpecialization.cpp

Andrew Lenharth alenhar2 at cs.uiuc.edu
Wed Sep 3 14:00:28 PDT 2008


Author: alenhar2
Date: Wed Sep  3 16:00:28 2008
New Revision: 55734

URL: http://llvm.org/viewvc/llvm-project?rev=55734&view=rev
Log:
Initial version of a Partial Specialization IPO pass.  It triggers a couple hundred times on 176.gcc.  I don't know the performance impact yet, the heuristic is quite simple still.

Added:
    llvm/trunk/lib/Transforms/IPO/PartialSpecialization.cpp
Modified:
    llvm/trunk/include/llvm/LinkAllPasses.h
    llvm/trunk/include/llvm/Transforms/IPO.h

Modified: llvm/trunk/include/llvm/LinkAllPasses.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LinkAllPasses.h?rev=55734&r1=55733&r2=55734&view=diff

==============================================================================
--- llvm/trunk/include/llvm/LinkAllPasses.h (original)
+++ llvm/trunk/include/llvm/LinkAllPasses.h Wed Sep  3 16:00:28 2008
@@ -118,6 +118,7 @@
       (void) llvm::createPostDomTree();
       (void) llvm::createPostDomFrontier();
       (void) llvm::createInstructionNamerPass();
+      (void) llvm::createPartialSpecializationPass();
 
       (void)new llvm::IntervalPartition();
       (void)new llvm::FindUsedTypes();

Modified: llvm/trunk/include/llvm/Transforms/IPO.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO.h?rev=55734&r1=55733&r2=55734&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Transforms/IPO.h (original)
+++ llvm/trunk/include/llvm/Transforms/IPO.h Wed Sep  3 16:00:28 2008
@@ -182,6 +182,12 @@
 /// (prototypes) that are not used.
 ModulePass *createStripDeadPrototypesPass();
 
+//===----------------------------------------------------------------------===//
+/// createPartialSpecializationPass - This pass specializes functions for
+/// constant arguments.
+///
+ModulePass* createPartialSpecializationPass();
+
 } // End llvm namespace
 
 #endif

Added: llvm/trunk/lib/Transforms/IPO/PartialSpecialization.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PartialSpecialization.cpp?rev=55734&view=auto

==============================================================================
--- llvm/trunk/lib/Transforms/IPO/PartialSpecialization.cpp (added)
+++ llvm/trunk/lib/Transforms/IPO/PartialSpecialization.cpp Wed Sep  3 16:00:28 2008
@@ -0,0 +1,127 @@
+//===-- PartialSpecialization.cpp - Specialize for common constants--------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// 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.
+//
+//===----------------------------------------------------------------------===//
+
+#define DEBUG_TYPE "partialspecialization"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Constant.h"
+#include "llvm/Instructions.h"
+#include "llvm/Module.h"
+#include "llvm/Pass.h"
+#include "llvm/ADT/Statistic.h"
+#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
+static const int CallsMin = 5;
+//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 replaceUsersFor(Function&, int, Constant*, Function*);
+    int scanDistribution(Function&, int, std::map<Constant*, int>&);
+  public :
+    static char ID; // Pass identification, replacement for typeid
+    PartSpec() : ModulePass((intptr_t)&ID) {}
+    bool runOnModule(Module &M);
+  };
+}
+
+char PartSpec::ID = 0;
+static RegisterPass<PartSpec>
+X("partialspecialization", "Partial Specialization");
+
+bool PartSpec::runOnModule(Module &M) {
+  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;
+            }
+      }
+    }
+  }
+  return Changed;
+}
+
+/// scanForInterest - This function decides which arguments would be worth
+///                    specializing on.
+void PartSpec::scanForInterest(Function& F, std::vector<int>& 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) {
+      if (isa<CmpInst>(ui)) {
+        args.push_back(std::distance(F.arg_begin(), ii));
+        break;
+      }
+    }
+  }
+}
+
+/// replaceUsersFor - Replace direct calls to F with NF if the arg argnum is
+/// the constant val
+void PartSpec::replaceUsersFor(Function& F , int argnum, Constant* val, 
+                               Function* NF) {
+  ++numSpecialized;
+  for(Value::use_iterator ii = F.use_begin(), ee = F.use_end();
+      ii != ee; ++ii)
+    if (CallInst* CI = dyn_cast<CallInst>(ii))
+      if (CI->getOperand(0) == &F && CI->getOperand(argnum + 1) == val)
+        CI->setOperand(0, NF);
+}
+
+int PartSpec::scanDistribution(Function& F, int arg, 
+                               std::map<Constant*, int>& dist) {
+  bool hasInd = false;
+  int total = 0;
+  for(Value::use_iterator ii = F.use_begin(), ee = F.use_end();
+      ii != ee; ++ii)
+    if (CallInst* CI = dyn_cast<CallInst>(ii)) {
+      ++dist[dyn_cast<Constant>(CI->getOperand(arg + 1))];
+      ++total;
+    } else
+      hasInd = true;
+  if (hasInd) ++total;
+  return total;
+}
+
+ModulePass* llvm::createPartialSpecializationPass() { return new PartSpec(); }





More information about the llvm-commits mailing list