* Rename MethodPass class to FunctionPass
[oota-llvm.git] / lib / Transforms / IPO / SimpleStructMutation.cpp
1 //===- SimpleStructMutation.cpp - Swap structure elements around -*- C++ -*--=//
2 //
3 // This pass does a simple transformation that swaps all of the elements of the
4 // struct types in the program around.
5 //
6 //===----------------------------------------------------------------------===//
7
8
9 #include "llvm/Transforms/IPO/SimpleStructMutation.h"
10 #include "llvm/Transforms/IPO/MutateStructTypes.h"
11 #include "llvm/Analysis/FindUsedTypes.h"
12 #include "llvm/Analysis/FindUnsafePointerTypes.h"
13 #include "../TransformInternals.h"
14 #include <algorithm>
15 #include <iostream>
16 using std::vector;
17 using std::set;
18 using std::pair;
19
20 namespace {
21   class SimpleStructMutation : public MutateStructTypes {
22   public:
23     enum Transform { SwapElements, SortElements } CurrentXForm;
24     
25     SimpleStructMutation(enum Transform XForm) : CurrentXForm(XForm) {}
26     
27     virtual bool run(Module *M) {
28       setTransforms(getTransforms(M, CurrentXForm));
29       bool Changed = MutateStructTypes::run(M);
30       clearTransforms();
31       return Changed;
32     }
33     
34     // getAnalysisUsage - This function needs the results of the
35     // FindUsedTypes and FindUnsafePointerTypes analysis passes...
36     //
37     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38       AU.addRequired(FindUsedTypes::ID);
39       AU.addRequired(FindUnsafePointerTypes::ID);
40       MutateStructTypes::getAnalysisUsage(AU);
41     }
42     
43   private:
44     TransformsType getTransforms(Module *M, enum Transform);
45   };
46 }  // end anonymous namespace
47
48
49
50 // PruneTypes - Given a type Ty, make sure that neither it, or one of its
51 // subtypes, occur in TypesToModify.
52 //
53 static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
54                        set<const Type*> &ProcessedTypes) {
55   if (ProcessedTypes.count(Ty)) return;  // Already been checked
56   ProcessedTypes.insert(Ty);
57
58   // If the element is in TypesToModify, remove it now...
59   if (const StructType *ST = dyn_cast<StructType>(Ty)) {
60     TypesToModify.erase(ST);  // This doesn't fail if the element isn't present
61     std::cerr << "Unable to swap type: " << ST << "\n";
62   }
63
64   // Remove all types that this type contains as well... do not remove types
65   // that are referenced only through pointers, because we depend on the size of
66   // the pointer, not on what the structure points to.
67   //
68   for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
69        I != E; ++I) {
70     if (!isa<PointerType>(*I))
71       PruneTypes(*I, TypesToModify, ProcessedTypes);
72   }
73 }
74
75 static bool FirstLess(const pair<unsigned, unsigned> &LHS,
76                       const pair<unsigned, unsigned> &RHS) {
77   return LHS.second < RHS.second;
78 }
79
80 static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
81                          unsigned Field) {
82   for (unsigned i = 0; ; ++i)
83     if (Vec[i].first == Field) return i;
84 }
85
86 static inline void GetTransformation(const StructType *ST,
87                                      vector<int> &Transform,
88                                 enum SimpleStructMutation::Transform XForm) {
89   unsigned NumElements = ST->getElementTypes().size();
90   Transform.reserve(NumElements);
91
92   switch (XForm) {
93   case SimpleStructMutation::SwapElements:
94     // The transformation to do is: just simply swap the elements
95     for (unsigned i = 0; i < NumElements; ++i)
96       Transform.push_back(NumElements-i-1);
97     break;
98
99   case SimpleStructMutation::SortElements: {
100     vector<pair<unsigned, unsigned> > ElList;
101
102     // Build mapping from index to size
103     for (unsigned i = 0; i < NumElements; ++i)
104       ElList.push_back(
105               std::make_pair(i, TD.getTypeSize(ST->getElementTypes()[i])));
106
107     sort(ElList.begin(), ElList.end(), ptr_fun(FirstLess));
108
109     for (unsigned i = 0; i < NumElements; ++i)
110       Transform.push_back(getIndex(ElList, i));
111
112     break;
113   }
114   }
115 }
116
117
118 SimpleStructMutation::TransformsType
119   SimpleStructMutation::getTransforms(Module *M, enum Transform XForm) {
120   // We need to know which types to modify, and which types we CAN'T modify
121   // TODO: Do symbol tables as well
122
123   // Get the results out of the analyzers...
124   FindUsedTypes          &FUT = getAnalysis<FindUsedTypes>();
125   const set<const Type *> &UsedTypes  = FUT.getTypes();
126
127   FindUnsafePointerTypes &FUPT = getAnalysis<FindUnsafePointerTypes>();
128   const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
129
130
131
132   // Combine the two sets, weeding out non structure types.  Closures in C++
133   // sure would be nice.
134   set<const StructType*> TypesToModify;
135   for (set<const Type *>::const_iterator I = UsedTypes.begin(), 
136          E = UsedTypes.end(); I != E; ++I)
137     if (const StructType *ST = dyn_cast<StructType>(*I))
138       TypesToModify.insert(ST);
139
140
141   // Go through the Unsafe types and remove all types from TypesToModify that we
142   // are not allowed to modify, because that would be unsafe.
143   //
144   set<const Type*> ProcessedTypes;
145   for (set<PointerType*>::const_iterator I = UnsafePTys.begin(),
146          E = UnsafePTys.end(); I != E; ++I) {
147     //cerr << "Pruning type: " << *I << "\n";
148     PruneTypes(*I, TypesToModify, ProcessedTypes);
149   }
150
151
152   // Build up a set of structure types that we are going to modify, and
153   // information describing how to modify them.
154   std::map<const StructType*, vector<int> > Transforms;
155
156   for (set<const StructType*>::iterator I = TypesToModify.begin(),
157          E = TypesToModify.end(); I != E; ++I) {
158     const StructType *ST = *I;
159
160     vector<int> &Transform = Transforms[ST];  // Fill in the map directly
161     GetTransformation(ST, Transform, XForm);
162   }
163   
164   return Transforms;
165 }
166
167
168 Pass *createSwapElementsPass() {
169   return new SimpleStructMutation(SimpleStructMutation::SwapElements);
170 }
171 Pass *createSortElementsPass() {
172   return new SimpleStructMutation(SimpleStructMutation::SortElements);
173 }
174