1 //===- SwapStructContents.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/SwapStructContents.h"
10 #include "llvm/Transforms/MutateStructTypes.h"
11 #include "llvm/Analysis/FindUsedTypes.h"
12 #include "llvm/Analysis/FindUnsafePointerTypes.h"
13 #include "llvm/DerivedTypes.h"
15 // PruneTypes - Given a type Ty, make sure that neither it, or one of its
16 // subtypes, occur in TypesToModify.
18 static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
19 set<const Type*> &ProcessedTypes) {
20 if (ProcessedTypes.count(Ty)) return; // Already been checked
21 ProcessedTypes.insert(Ty);
23 // If the element is in TypesToModify, remove it now...
24 if (const StructType *ST = dyn_cast<StructType>(Ty))
25 TypesToModify.erase(ST); // This doesn't fail if the element isn't present
27 // Remove all types that this type contains as well...
29 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
31 PruneTypes(*I, TypesToModify, ProcessedTypes);
36 // doPassInitialization - This does all of the work of the pass
38 bool SwapStructContents::doPassInitialization(Module *M) {
39 // We need to know which types to modify, and which types we CAN'T modify
40 FindUsedTypes FUT/*(true)*/; // TODO: Do symbol tables as well
41 FindUnsafePointerTypes FUPT;
43 // Simutaneously find all of the types used, and all of the types that aren't
46 vector<Pass*> Analyses;
47 Analyses.push_back(&FUT);
48 Analyses.push_back(&FUPT);
49 Pass::runAllPasses(M, Analyses); // Do analyses
52 // Get the results out of the analyzers...
53 const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
54 const set<const Type *> &UsedTypes = FUT.getTypes();
57 // Combine the two sets, weeding out non structure types. Closures should
59 set<const StructType*> TypesToModify;
60 for (set<const Type *>::const_iterator I = UsedTypes.begin(),
61 E = UsedTypes.end(); I != E; ++I)
62 if (const StructType *ST = dyn_cast<StructType>(*I))
63 TypesToModify.insert(ST);
66 // Go through the Unsafe types and remove all types from TypesToModify that we
67 // are not allowed to modify, because that would be unsafe.
69 set<const Type*> ProcessedTypes;
70 for (set<PointerType*>::const_iterator I = UnsafePTys.begin(),
71 E = UnsafePTys.end(); I != E; ++I)
72 PruneTypes(*I, TypesToModify, ProcessedTypes);
75 // Build up a set of structure types that we are going to modify, and
76 // information describing how to modify them.
77 map<const StructType*, vector<int> > Transforms;
79 for (set<const StructType*>::iterator I = TypesToModify.begin(),
80 E = TypesToModify.end(); I != E; ++I) {
81 const StructType *ST = *I;
82 unsigned NumElements = ST->getElementTypes().size();
84 vector<int> &Transform = Transforms[ST]; // Fill in the map directly
85 Transform.reserve(NumElements);
87 // The transformation to do is: just simply swap the elements
88 for (unsigned i = 0; i < NumElements; ++i)
89 Transform.push_back(NumElements-i-1);
92 // Create the Worker to do our stuff for us...
93 StructMutator = new MutateStructTypes(Transforms);
96 return StructMutator->doPassInitialization(M);