Programs that actually free memory were broken
[oota-llvm.git] / lib / Transforms / IPO / SimpleStructMutation.cpp
index d0b8bb2807ce641e77580c694b6bdd3474e3d325..5c64aa3537db1b00eccbd84040b6e367053c7e87 100644 (file)
@@ -1,4 +1,4 @@
-//===- SwapStructContents.cpp - Swap structure elements around ---*- C++ -*--=//
+//===- SimpleStructMutation.cpp - Swap structure elements around ---*- C++ -*--=//
 //
 // This pass does a simple transformation that swaps all of the elements of the
 // struct types in the program around.
@@ -6,18 +6,47 @@
 //===----------------------------------------------------------------------===//
 
 
-#include "llvm/Transforms/SwapStructContents.h"
-#include "llvm/Transforms/MutateStructTypes.h"
+#include "llvm/Transforms/IPO/SimpleStructMutation.h"
+#include "llvm/Transforms/IPO/MutateStructTypes.h"
 #include "llvm/Analysis/FindUsedTypes.h"
 #include "llvm/Analysis/FindUnsafePointerTypes.h"
-#include "TransformInternals.h"
+#include "../TransformInternals.h"
 #include <algorithm>
 #include <iostream>
 using std::vector;
 using std::set;
 using std::pair;
 
-#include "llvm/Assembly/Writer.h"
+namespace {
+  class SimpleStructMutation : public MutateStructTypes {
+  public:
+    enum Transform { SwapElements, SortElements } CurrentXForm;
+    
+    SimpleStructMutation(enum Transform XForm) : CurrentXForm(XForm) {}
+    
+    virtual bool run(Module *M) {
+      setTransforms(getTransforms(M, CurrentXForm));
+      bool Changed = MutateStructTypes::run(M);
+      clearTransforms();
+      return Changed;
+    }
+    
+    // getAnalysisUsageInfo - This function needs the results of the
+    // FindUsedTypes and FindUnsafePointerTypes analysis passes...
+    //
+    virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Required,
+                                      Pass::AnalysisSet &Destroyed,
+                                      Pass::AnalysisSet &Provided) {
+      Required.push_back(FindUsedTypes::ID);
+      Required.push_back(FindUnsafePointerTypes::ID);
+      MutateStructTypes::getAnalysisUsageInfo(Required, Destroyed, Provided);
+    }
+    
+  private:
+    TransformsType getTransforms(Module *M, enum Transform);
+  };
+}  // end anonymous namespace
+
 
 
 // PruneTypes - Given a type Ty, make sure that neither it, or one of its
@@ -58,18 +87,18 @@ static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
 
 static inline void GetTransformation(const StructType *ST,
                                      vector<int> &Transform,
-                                enum PrebuiltStructMutation::Transform XForm) {
+                                enum SimpleStructMutation::Transform XForm) {
   unsigned NumElements = ST->getElementTypes().size();
   Transform.reserve(NumElements);
 
   switch (XForm) {
-  case PrebuiltStructMutation::SwapElements:
+  case SimpleStructMutation::SwapElements:
     // The transformation to do is: just simply swap the elements
     for (unsigned i = 0; i < NumElements; ++i)
       Transform.push_back(NumElements-i-1);
     break;
 
-  case PrebuiltStructMutation::SortElements: {
+  case SimpleStructMutation::SortElements: {
     vector<pair<unsigned, unsigned> > ElList;
 
     // Build mapping from index to size
@@ -87,27 +116,20 @@ static inline void GetTransformation(const StructType *ST,
   }
 }
 
-// doPassInitialization - This does all of the work of the pass
-//
-PrebuiltStructMutation::TransformsType
-  PrebuiltStructMutation::getTransforms(Module *M, enum Transform XForm) {
-  // We need to know which types to modify, and which types we CAN'T modify
-  FindUsedTypes          FUT/*(true)*/; // TODO: Do symbol tables as well
-  FindUnsafePointerTypes FUPT;
-
-  // Simutaneously find all of the types used, and all of the types that aren't
-  // safe.
-  //
-  vector<Pass*> Analyses;
-  Analyses.push_back(&FUT);
-  Analyses.push_back(&FUPT);
-  Pass::runAllPasses(M, Analyses);  // Do analyses
 
+SimpleStructMutation::TransformsType
+  SimpleStructMutation::getTransforms(Module *M, enum Transform XForm) {
+  // We need to know which types to modify, and which types we CAN'T modify
+  // TODO: Do symbol tables as well
 
   // Get the results out of the analyzers...
-  const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
+  FindUsedTypes          &FUT = getAnalysis<FindUsedTypes>();
   const set<const Type *> &UsedTypes  = FUT.getTypes();
 
+  FindUnsafePointerTypes &FUPT = getAnalysis<FindUnsafePointerTypes>();
+  const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
+
+
 
   // Combine the two sets, weeding out non structure types.  Closures in C++
   // sure would be nice.
@@ -144,3 +166,11 @@ PrebuiltStructMutation::TransformsType
   return Transforms;
 }
 
+
+Pass *createSwapElementsPass() {
+  return new SimpleStructMutation(SimpleStructMutation::SwapElements);
+}
+Pass *createSortElementsPass() {
+  return new SimpleStructMutation(SimpleStructMutation::SortElements);
+}
+