Programs that actually free memory were broken
[oota-llvm.git] / lib / Transforms / IPO / SimpleStructMutation.cpp
index 8ec7f4b7a8019c6f6446bf735b7aec1f7dba1003..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,11 +6,48 @@
 //===----------------------------------------------------------------------===//
 
 
-#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 "llvm/DerivedTypes.h"
+#include "../TransformInternals.h"
+#include <algorithm>
+#include <iostream>
+using std::vector;
+using std::set;
+using std::pair;
+
+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
 // subtypes, occur in TypesToModify.
@@ -21,41 +58,81 @@ static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
   ProcessedTypes.insert(Ty);
 
   // If the element is in TypesToModify, remove it now...
-  if (const StructType *ST = dyn_cast<StructType>(Ty))
+  if (const StructType *ST = dyn_cast<StructType>(Ty)) {
     TypesToModify.erase(ST);  // This doesn't fail if the element isn't present
+    std::cerr << "Unable to swap type: " << ST << "\n";
+  }
 
-  // Remove all types that this type contains as well...
+  // Remove all types that this type contains as well... do not remove types
+  // that are referenced only through pointers, because we depend on the size of
+  // the pointer, not on what the structure points to.
   //
   for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
-       I != E; ++I)
-    PruneTypes(*I, TypesToModify, ProcessedTypes);
+       I != E; ++I) {
+    if (!isa<PointerType>(*I))
+      PruneTypes(*I, TypesToModify, ProcessedTypes);
+  }
 }
 
+static bool FirstLess(const pair<unsigned, unsigned> &LHS,
+                      const pair<unsigned, unsigned> &RHS) {
+  return LHS.second < RHS.second;
+}
 
+static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
+                         unsigned Field) {
+  for (unsigned i = 0; ; ++i)
+    if (Vec[i].first == Field) return i;
+}
 
-// doPassInitialization - This does all of the work of the pass
-//
-bool SwapStructContents::doPassInitialization(Module *M) {
-  // 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;
+static inline void GetTransformation(const StructType *ST,
+                                     vector<int> &Transform,
+                                enum SimpleStructMutation::Transform XForm) {
+  unsigned NumElements = ST->getElementTypes().size();
+  Transform.reserve(NumElements);
 
-  // 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
+  switch (XForm) {
+  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 SimpleStructMutation::SortElements: {
+    vector<pair<unsigned, unsigned> > ElList;
+
+    // Build mapping from index to size
+    for (unsigned i = 0; i < NumElements; ++i)
+      ElList.push_back(
+              std::make_pair(i, TD.getTypeSize(ST->getElementTypes()[i])));
+
+    sort(ElList.begin(), ElList.end(), ptr_fun(FirstLess));
 
+    for (unsigned i = 0; i < NumElements; ++i)
+      Transform.push_back(getIndex(ElList, i));
+
+    break;
+  }
+  }
+}
+
+
+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 should
-  // would be nice.
+
+  // Combine the two sets, weeding out non structure types.  Closures in C++
+  // sure would be nice.
   set<const StructType*> TypesToModify;
   for (set<const Type *>::const_iterator I = UsedTypes.begin(), 
          E = UsedTypes.end(); I != E; ++I)
@@ -68,31 +145,32 @@ bool SwapStructContents::doPassInitialization(Module *M) {
   //
   set<const Type*> ProcessedTypes;
   for (set<PointerType*>::const_iterator I = UnsafePTys.begin(),
-         E = UnsafePTys.end(); I != E; ++I)
+         E = UnsafePTys.end(); I != E; ++I) {
+    //cerr << "Pruning type: " << *I << "\n";
     PruneTypes(*I, TypesToModify, ProcessedTypes);
+  }
 
 
   // Build up a set of structure types that we are going to modify, and
   // information describing how to modify them.
-  map<const StructType*, vector<int> > Transforms;
+  std::map<const StructType*, vector<int> > Transforms;
 
   for (set<const StructType*>::iterator I = TypesToModify.begin(),
          E = TypesToModify.end(); I != E; ++I) {
     const StructType *ST = *I;
-    unsigned NumElements = ST->getElementTypes().size();
 
     vector<int> &Transform = Transforms[ST];  // Fill in the map directly
-    Transform.reserve(NumElements);
-
-    // The transformation to do is: just simply swap the elements
-    for (unsigned i = 0; i < NumElements; ++i)
-      Transform.push_back(NumElements-i-1);
+    GetTransformation(ST, Transform, XForm);
   }
   
-  // Create the Worker to do our stuff for us...
-  StructMutator = new MutateStructTypes(Transforms);
-  
-  // Do initial work.
-  return StructMutator->doPassInitialization(M);
+  return Transforms;
+}
+
+
+Pass *createSwapElementsPass() {
+  return new SimpleStructMutation(SimpleStructMutation::SwapElements);
+}
+Pass *createSortElementsPass() {
+  return new SimpleStructMutation(SimpleStructMutation::SortElements);
 }