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