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 #include "llvm/Assembly/Writer.h"
17 // PruneTypes - Given a type Ty, make sure that neither it, or one of its
18 // subtypes, occur in TypesToModify.
20 static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
21 set<const Type*> &ProcessedTypes) {
22 if (ProcessedTypes.count(Ty)) return; // Already been checked
23 ProcessedTypes.insert(Ty);
25 // If the element is in TypesToModify, remove it now...
26 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
27 TypesToModify.erase(ST); // This doesn't fail if the element isn't present
28 cerr << "Unable to swap type: " << ST << endl;
31 // Remove all types that this type contains as well... do not remove types
32 // that are referenced only through pointers, because we depend on the size of
33 // the pointer, not on what the structure points to.
35 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
37 if (!isa<PointerType>(*I))
38 PruneTypes(*I, TypesToModify, ProcessedTypes);
44 // doPassInitialization - This does all of the work of the pass
46 bool SwapStructContents::doPassInitialization(Module *M) {
47 // We need to know which types to modify, and which types we CAN'T modify
48 FindUsedTypes FUT/*(true)*/; // TODO: Do symbol tables as well
49 FindUnsafePointerTypes FUPT;
51 // Simutaneously find all of the types used, and all of the types that aren't
54 vector<Pass*> Analyses;
55 Analyses.push_back(&FUT);
56 Analyses.push_back(&FUPT);
57 Pass::runAllPasses(M, Analyses); // Do analyses
60 // Get the results out of the analyzers...
61 const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
62 const set<const Type *> &UsedTypes = FUT.getTypes();
65 // Combine the two sets, weeding out non structure types. Closures in C++
66 // sure would be nice.
67 set<const StructType*> TypesToModify;
68 for (set<const Type *>::const_iterator I = UsedTypes.begin(),
69 E = UsedTypes.end(); I != E; ++I)
70 if (const StructType *ST = dyn_cast<StructType>(*I))
71 TypesToModify.insert(ST);
74 // Go through the Unsafe types and remove all types from TypesToModify that we
75 // are not allowed to modify, because that would be unsafe.
77 set<const Type*> ProcessedTypes;
78 for (set<PointerType*>::const_iterator I = UnsafePTys.begin(),
79 E = UnsafePTys.end(); I != E; ++I) {
80 cerr << "Pruning type: " << *I << endl;
81 PruneTypes(*I, TypesToModify, ProcessedTypes);
85 // Build up a set of structure types that we are going to modify, and
86 // information describing how to modify them.
87 map<const StructType*, vector<int> > Transforms;
89 for (set<const StructType*>::iterator I = TypesToModify.begin(),
90 E = TypesToModify.end(); I != E; ++I) {
91 const StructType *ST = *I;
92 unsigned NumElements = ST->getElementTypes().size();
94 vector<int> &Transform = Transforms[ST]; // Fill in the map directly
95 Transform.reserve(NumElements);
97 // The transformation to do is: just simply swap the elements
98 for (unsigned i = 0; i < NumElements; ++i)
99 Transform.push_back(NumElements-i-1);
102 // Create the Worker to do our stuff for us...
103 StructMutator = new MutateStructTypes(Transforms);
106 return StructMutator->doPassInitialization(M);