Programs that actually free memory were broken
[oota-llvm.git] / lib / Transforms / IPO / SimpleStructMutation.cpp
index 7e28af3df3afeb1a32b2cb6c3ef72856537922f9..5c64aa3537db1b00eccbd84040b6e367053c7e87 100644 (file)
@@ -17,7 +17,36 @@ 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
@@ -87,26 +116,19 @@ static inline void GetTransformation(const StructType *ST,
   }
 }
 
+
 SimpleStructMutation::TransformsType
   SimpleStructMutation::getTransforms(Module *M, enum Transform XForm) {
-
-  // FIXME: These should be calculated by the Pass framework!
-
   // We need to know which types to modify, and which types we CAN'T modify
-  FindUsedTypes          *FUT = new FindUsedTypes(/*true*/); // TODO: Do symbol tables as well
-  FindUnsafePointerTypes *FUPT = new FindUnsafePointerTypes();
-
-  // Simutaneously find all of the types used, and all of the types that aren't
-  // safe.
-  //
-  PassManager Analyses;
-  Analyses.add(FUT);
-  Analyses.add(FUPT);
-  Analyses.run(M);  // Do analyses
+  // TODO: Do symbol tables as well
 
   // Get the results out of the analyzers...
-  const set<PointerType*> &UnsafePTys = FUPT->getUnsafeTypes();
-  const set<const Type *> &UsedTypes  = FUT->getTypes();
+  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++
@@ -144,3 +166,11 @@ SimpleStructMutation::TransformsType
   return Transforms;
 }
 
+
+Pass *createSwapElementsPass() {
+  return new SimpleStructMutation(SimpleStructMutation::SwapElements);
+}
+Pass *createSortElementsPass() {
+  return new SimpleStructMutation(SimpleStructMutation::SortElements);
+}
+