1 //===- SimpleStructMutation.cpp - Swap structure elements around -*- C++ -*--=//
3 // This pass does a simple transformation that swaps all of the elements of the
4 // struct types in the program around.
6 //===----------------------------------------------------------------------===//
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"
21 struct SimpleStructMutation : public MutateStructTypes {
22 enum Transform { SwapElements, SortElements } CurrentXForm;
24 SimpleStructMutation(enum Transform XForm) : CurrentXForm(XForm) {}
26 const char *getPassName() const { return "Simple Struct Mutation"; }
28 virtual bool run(Module *M) {
29 setTransforms(getTransforms(M, CurrentXForm));
30 bool Changed = MutateStructTypes::run(M);
35 // getAnalysisUsage - This function needs the results of the
36 // FindUsedTypes and FindUnsafePointerTypes analysis passes...
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.addRequired(FindUsedTypes::ID);
40 AU.addRequired(FindUnsafePointerTypes::ID);
41 MutateStructTypes::getAnalysisUsage(AU);
45 TransformsType getTransforms(Module *M, enum Transform);
47 } // end anonymous namespace
51 // PruneTypes - Given a type Ty, make sure that neither it, or one of its
52 // subtypes, occur in TypesToModify.
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);
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";
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.
69 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
71 if (!isa<PointerType>(*I))
72 PruneTypes(*I, TypesToModify, ProcessedTypes);
76 static bool FirstLess(const pair<unsigned, unsigned> &LHS,
77 const pair<unsigned, unsigned> &RHS) {
78 return LHS.second < RHS.second;
81 static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
83 for (unsigned i = 0; ; ++i)
84 if (Vec[i].first == Field) return i;
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);
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);
100 case SimpleStructMutation::SortElements: {
101 vector<pair<unsigned, unsigned> > ElList;
103 // Build mapping from index to size
104 for (unsigned i = 0; i < NumElements; ++i)
106 std::make_pair(i, TD.getTypeSize(ST->getElementTypes()[i])));
108 sort(ElList.begin(), ElList.end(), ptr_fun(FirstLess));
110 for (unsigned i = 0; i < NumElements; ++i)
111 Transform.push_back(getIndex(ElList, i));
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
124 // Get the results out of the analyzers...
125 FindUsedTypes &FUT = getAnalysis<FindUsedTypes>();
126 const set<const Type *> &UsedTypes = FUT.getTypes();
128 FindUnsafePointerTypes &FUPT = getAnalysis<FindUnsafePointerTypes>();
129 const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
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);
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.
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);
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;
157 for (set<const StructType*>::iterator I = TypesToModify.begin(),
158 E = TypesToModify.end(); I != E; ++I) {
159 const StructType *ST = *I;
161 vector<int> &Transform = Transforms[ST]; // Fill in the map directly
162 GetTransformation(ST, Transform, XForm);
169 Pass *createSwapElementsPass() {
170 return new SimpleStructMutation(SimpleStructMutation::SwapElements);
172 Pass *createSortElementsPass() {
173 return new SimpleStructMutation(SimpleStructMutation::SortElements);