[llvm-commits] CVS: llvm/lib/Transforms/IPO/ExtractFunction.cpp

Misha Brukman brukman at cs.uiuc.edu
Thu Apr 22 17:52:06 PDT 2004


Changes in directory llvm/lib/Transforms/IPO:

ExtractFunction.cpp updated: 1.7 -> 1.8

---
Log message:

Add a flag to choose between isolating a function or deleting the function from
the Module. The default behavior keeps functionality as before: the chosen
function is the one that remains.


---
Diffs of the changes:  (+29 -6)

Index: llvm/lib/Transforms/IPO/ExtractFunction.cpp
diff -u llvm/lib/Transforms/IPO/ExtractFunction.cpp:1.7 llvm/lib/Transforms/IPO/ExtractFunction.cpp:1.8
--- llvm/lib/Transforms/IPO/ExtractFunction.cpp:1.7	Fri Nov 21 15:54:22 2003
+++ llvm/lib/Transforms/IPO/ExtractFunction.cpp	Thu Apr 22 17:52:22 2004
@@ -6,17 +6,27 @@
 // the University of Illinois Open Source License. See LICENSE.TXT for details.
 // 
 //===----------------------------------------------------------------------===//
+//
+// This pass extracts
+//
+//===----------------------------------------------------------------------===//
 
-#include "llvm/Transforms/IPO.h"
-#include "llvm/Pass.h"
 #include "llvm/Module.h"
+#include "llvm/Pass.h"
+#include "llvm/Transforms/IPO.h"
 using namespace llvm;
 
 namespace {
   class FunctionExtractorPass : public Pass {
     Function *Named;
+    bool isolateFunc;
   public:
-    FunctionExtractorPass(Function *F = 0) : Named(F) {}
+    /// FunctionExtractorPass - ctor for the pass. If isolateFn is true, then
+    /// the named function is the only thing left in the Module (default
+    /// behavior), otherwise the function is the thing deleted.
+    ///
+    FunctionExtractorPass(Function *F = 0, bool isolateFn = true) 
+      : Named(F), isolateFunc(isolateFn) {}
 
     bool run(Module &M) {
       if (Named == 0) {
@@ -24,6 +34,20 @@
         if (Named == 0) return false;  // No function to extract
       }
 
+      if (isolateFunc)
+        return isolateFunction(M);
+      else 
+        return deleteFunction();
+    }
+
+    bool deleteFunction() {
+      Named->setLinkage(GlobalValue::ExternalLinkage);
+      Named->deleteBody();
+      assert(Named->isExternal() && "This didn't make the function external!");
+      return true;
+    }
+
+    bool isolateFunction(Module &M) {
       // Make sure our result is globally accessible...
       Named->setLinkage(GlobalValue::ExternalLinkage);
 
@@ -37,7 +61,6 @@
       // All of the functions may be used by global variables or the named
       // function.  Loop through them and create a new, external functions that
       // can be "used", instead of ones with bodies.
-      //
       std::vector<Function*> NewFunctions;
       
       Function *Last = &M.back();  // Figure out where the last real fn is...
@@ -89,6 +112,6 @@
   RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
 }
 
-Pass *llvm::createFunctionExtractionPass(Function *F) {
-  return new FunctionExtractorPass(F);
+Pass *llvm::createFunctionExtractionPass(Function *F, bool isolateFn) {
+  return new FunctionExtractorPass(F, isolateFn);
 }





More information about the llvm-commits mailing list