Non-functionality change just to make it more clear what is going on
[oota-llvm.git] / lib / Transforms / IPO / SimpleStructMutation.cpp
index 908b5b1030a3c31cfe373ae1cf3d36a8d4166bfd..4e21dd69a0d4838376bc42ee511a4e456a9b91b5 100644 (file)
@@ -5,54 +5,63 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Transforms/IPO/SimpleStructMutation.h"
+#include "llvm/Transforms/IPO.h"
 #include "llvm/Transforms/IPO/MutateStructTypes.h"
 #include "llvm/Analysis/FindUsedTypes.h"
 #include "llvm/Analysis/FindUnsafePointerTypes.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/DerivedTypes.h"
 #include <algorithm>
-#include <iostream>
 using std::vector;
 using std::set;
 using std::pair;
 
-// FIXME: TargetData Hack: Eventually we will have annotations given to us by
-// the backend so that we know stuff about type size and alignments.  For now
-// though, just use this, because it happens to match the model that GCC and the
-// Sparc backend use.
-//
-const TargetData TD("SimpleStructMutation Should be GCC though!");
-
 namespace {
   struct SimpleStructMutation : public MutateStructTypes {
-    enum Transform { SwapElements, SortElements } CurrentXForm;
-    
-    SimpleStructMutation(enum Transform XForm) : CurrentXForm(XForm) {}
-    
-    const char *getPassName() const { return "Simple Struct Mutation"; }
-    
-    virtual bool run(Module *M) {
-      setTransforms(getTransforms(M, CurrentXForm));
-      bool Changed = MutateStructTypes::run(M);
-      clearTransforms();
-      return Changed;
-    }
+    enum Transform { SwapElements, SortElements };
     
+    virtual bool run(Module &M)  = 0;
+
     // getAnalysisUsage - This function needs the results of the
     // FindUsedTypes and FindUnsafePointerTypes analysis passes...
     //
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-      AU.addRequired(FindUsedTypes::ID);
-      AU.addRequired(FindUnsafePointerTypes::ID);
+      AU.addRequired<TargetData>();
+      AU.addRequired<FindUsedTypes>();
+      AU.addRequired<FindUnsafePointerTypes>();
       MutateStructTypes::getAnalysisUsage(AU);
     }
     
-  private:
-    TransformsType getTransforms(Module *M, enum Transform);
+  protected:
+    TransformsType getTransforms(Module &M, enum Transform);
+  };
+
+  struct SwapStructElements : public SimpleStructMutation {
+    virtual bool run(Module &M) {
+      setTransforms(getTransforms(M, SwapElements));
+      bool Changed = MutateStructTypes::run(M);
+      clearTransforms();
+      return Changed;
+    }
+  };
+
+  struct SortStructElements : public SimpleStructMutation {
+    virtual bool run(Module &M) {
+      setTransforms(getTransforms(M, SortElements));
+      bool Changed = MutateStructTypes::run(M);
+      clearTransforms();
+      return Changed;
+    }
   };
+
+  RegisterOpt<SwapStructElements> X("swapstructs",
+                                    "Swap structure types around");
+  RegisterOpt<SortStructElements> Y("sortstructs",
+                                    "Sort structure elements by size");
 }  // end anonymous namespace
 
+Pass *createSwapElementsPass() { return new SwapStructElements(); }
+Pass *createSortElementsPass() { return new SortStructElements(); }
 
 
 // PruneTypes - Given a type Ty, make sure that neither it, or one of its
@@ -91,7 +100,7 @@ static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
     if (Vec[i].first == Field) return i;
 }
 
-static inline void GetTransformation(const StructType *ST,
+static inline void GetTransformation(const TargetData &TD, const StructType *ST,
                                      vector<int> &Transform,
                                    enum SimpleStructMutation::Transform XForm) {
   unsigned NumElements = ST->getElementTypes().size();
@@ -124,7 +133,7 @@ static inline void GetTransformation(const StructType *ST,
 
 
 SimpleStructMutation::TransformsType
-  SimpleStructMutation::getTransforms(Module *M, enum Transform XForm) {
+  SimpleStructMutation::getTransforms(Module &, enum Transform XForm) {
   // We need to know which types to modify, and which types we CAN'T modify
   // TODO: Do symbol tables as well
 
@@ -160,23 +169,15 @@ SimpleStructMutation::TransformsType
   // Build up a set of structure types that we are going to modify, and
   // information describing how to modify them.
   std::map<const StructType*, vector<int> > Transforms;
+  TargetData &TD = getAnalysis<TargetData>();
 
   for (set<const StructType*>::iterator I = TypesToModify.begin(),
          E = TypesToModify.end(); I != E; ++I) {
     const StructType *ST = *I;
 
     vector<int> &Transform = Transforms[ST];  // Fill in the map directly
-    GetTransformation(ST, Transform, XForm);
+    GetTransformation(TD, ST, Transform, XForm);
   }
   
   return Transforms;
 }
-
-
-Pass *createSwapElementsPass() {
-  return new SimpleStructMutation(SimpleStructMutation::SwapElements);
-}
-Pass *createSortElementsPass() {
-  return new SimpleStructMutation(SimpleStructMutation::SortElements);
-}
-