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"
20 #include "llvm/Assembly/Writer.h"
23 // PruneTypes - Given a type Ty, make sure that neither it, or one of its
24 // subtypes, occur in TypesToModify.
26 static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
27 set<const Type*> &ProcessedTypes) {
28 if (ProcessedTypes.count(Ty)) return; // Already been checked
29 ProcessedTypes.insert(Ty);
31 // If the element is in TypesToModify, remove it now...
32 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
33 TypesToModify.erase(ST); // This doesn't fail if the element isn't present
34 std::cerr << "Unable to swap type: " << ST << "\n";
37 // Remove all types that this type contains as well... do not remove types
38 // that are referenced only through pointers, because we depend on the size of
39 // the pointer, not on what the structure points to.
41 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
43 if (!isa<PointerType>(*I))
44 PruneTypes(*I, TypesToModify, ProcessedTypes);
48 static bool FirstLess(const pair<unsigned, unsigned> &LHS,
49 const pair<unsigned, unsigned> &RHS) {
50 return LHS.second < RHS.second;
53 static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
55 for (unsigned i = 0; ; ++i)
56 if (Vec[i].first == Field) return i;
59 static inline void GetTransformation(const StructType *ST,
60 vector<int> &Transform,
61 enum SimpleStructMutation::Transform XForm) {
62 unsigned NumElements = ST->getElementTypes().size();
63 Transform.reserve(NumElements);
66 case SimpleStructMutation::SwapElements:
67 // The transformation to do is: just simply swap the elements
68 for (unsigned i = 0; i < NumElements; ++i)
69 Transform.push_back(NumElements-i-1);
72 case SimpleStructMutation::SortElements: {
73 vector<pair<unsigned, unsigned> > ElList;
75 // Build mapping from index to size
76 for (unsigned i = 0; i < NumElements; ++i)
78 std::make_pair(i, TD.getTypeSize(ST->getElementTypes()[i])));
80 sort(ElList.begin(), ElList.end(), ptr_fun(FirstLess));
82 for (unsigned i = 0; i < NumElements; ++i)
83 Transform.push_back(getIndex(ElList, i));
90 SimpleStructMutation::TransformsType
91 SimpleStructMutation::getTransforms(Module *M, enum Transform XForm) {
92 // We need to know which types to modify, and which types we CAN'T modify
93 // TODO: Do symbol tables as well
95 // Get the results out of the analyzers...
96 FindUsedTypes &FUT = getAnalysis<FindUsedTypes>();
97 const set<const Type *> &UsedTypes = FUT.getTypes();
99 FindUnsafePointerTypes &FUPT = getAnalysis<FindUnsafePointerTypes>();
100 const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
104 // Combine the two sets, weeding out non structure types. Closures in C++
105 // sure would be nice.
106 set<const StructType*> TypesToModify;
107 for (set<const Type *>::const_iterator I = UsedTypes.begin(),
108 E = UsedTypes.end(); I != E; ++I)
109 if (const StructType *ST = dyn_cast<StructType>(*I))
110 TypesToModify.insert(ST);
113 // Go through the Unsafe types and remove all types from TypesToModify that we
114 // are not allowed to modify, because that would be unsafe.
116 set<const Type*> ProcessedTypes;
117 for (set<PointerType*>::const_iterator I = UnsafePTys.begin(),
118 E = UnsafePTys.end(); I != E; ++I) {
119 //cerr << "Pruning type: " << *I << "\n";
120 PruneTypes(*I, TypesToModify, ProcessedTypes);
124 // Build up a set of structure types that we are going to modify, and
125 // information describing how to modify them.
126 std::map<const StructType*, vector<int> > Transforms;
128 for (set<const StructType*>::iterator I = TypesToModify.begin(),
129 E = TypesToModify.end(); I != E; ++I) {
130 const StructType *ST = *I;
132 vector<int> &Transform = Transforms[ST]; // Fill in the map directly
133 GetTransformation(ST, Transform, XForm);
140 // getAnalysisUsageInfo - This function needs the results of the
141 // FindUsedTypes and FindUnsafePointerTypes analysis passes...
143 void SimpleStructMutation::getAnalysisUsageInfo(Pass::AnalysisSet &Required,
144 Pass::AnalysisSet &Destroyed,
145 Pass::AnalysisSet &Provided){
146 Required.push_back(FindUsedTypes::ID);
147 Required.push_back(FindUnsafePointerTypes::ID);
148 MutateStructTypes::getAnalysisUsageInfo(Required, Destroyed, Provided);