-//===- SwapStructContents.cpp - Swap structure elements around ---*- C++ -*--=//
+//===- SimpleStructMutation.cpp - Swap structure elements around -*- C++ -*--=//
//
// This pass does a simple transformation that swaps all of the elements of the
// struct types in the program around.
//
//===----------------------------------------------------------------------===//
-
-#include "llvm/Transforms/SwapStructContents.h"
-#include "llvm/Transforms/MutateStructTypes.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/IPO/MutateStructTypes.h"
#include "llvm/Analysis/FindUsedTypes.h"
#include "llvm/Analysis/FindUnsafePointerTypes.h"
-#include "TransformInternals.h"
+#include "llvm/Target/TargetData.h"
+#include "llvm/DerivedTypes.h"
#include <algorithm>
-#include <iostream>
using std::vector;
using std::set;
using std::pair;
-#include "llvm/Assembly/Writer.h"
+namespace {
+ struct SimpleStructMutation : public MutateStructTypes {
+ enum Transform { SwapElements, SortElements };
+
+ virtual bool run(Module &M) = 0;
+
+ // getAnalysisUsage - This function needs the results of the
+ // FindUsedTypes and FindUnsafePointerTypes analysis passes...
+ //
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+ AU.addRequired<TargetData>();
+ AU.addRequired<FindUsedTypes>();
+ AU.addRequired<FindUnsafePointerTypes>();
+ MutateStructTypes::getAnalysisUsage(AU);
+ }
+
+ protected:
+ TransformsType getTransforms(Module &M, enum Transform);
+ };
+
+ struct SwapStructElements : public SimpleStructMutation {
+ virtual bool run(Module &M) {
+ setTransforms(getTransforms(M, SwapElements));
+ bool Changed = MutateStructTypes::run(M);
+ clearTransforms();
+ return Changed;
+ }
+ };
+
+ struct SortStructElements : public SimpleStructMutation {
+ virtual bool run(Module &M) {
+ setTransforms(getTransforms(M, SortElements));
+ bool Changed = MutateStructTypes::run(M);
+ clearTransforms();
+ return Changed;
+ }
+ };
+
+ RegisterOpt<SwapStructElements> X("swapstructs",
+ "Swap structure types around");
+ RegisterOpt<SortStructElements> Y("sortstructs",
+ "Sort structure elements by size");
+} // end anonymous namespace
+
+Pass *createSwapElementsPass() { return new SwapStructElements(); }
+Pass *createSortElementsPass() { return new SortStructElements(); }
// PruneTypes - Given a type Ty, make sure that neither it, or one of its
if (Vec[i].first == Field) return i;
}
-static inline void GetTransformation(const StructType *ST,
+static inline void GetTransformation(const TargetData &TD, const StructType *ST,
vector<int> &Transform,
- enum SimpleStructMutation::Transform XForm) {
+ enum SimpleStructMutation::Transform XForm) {
unsigned NumElements = ST->getElementTypes().size();
Transform.reserve(NumElements);
}
}
-SimpleStructMutation::TransformsType
- SimpleStructMutation::getTransforms(Module *M, enum Transform XForm) {
-
- // FIXME: These should be calculated by the Pass framework!
+SimpleStructMutation::TransformsType
+ SimpleStructMutation::getTransforms(Module &, enum Transform XForm) {
// We need to know which types to modify, and which types we CAN'T modify
- FindUsedTypes *FUT = new FindUsedTypes(/*true*/); // TODO: Do symbol tables as well
- FindUnsafePointerTypes *FUPT = new FindUnsafePointerTypes();
-
- // Simutaneously find all of the types used, and all of the types that aren't
- // safe.
- //
- PassManager Analyses;
- Analyses.add(FUT);
- Analyses.add(FUPT);
- Analyses.run(M); // Do analyses
+ // TODO: Do symbol tables as well
// Get the results out of the analyzers...
- const set<PointerType*> &UnsafePTys = FUPT->getUnsafeTypes();
- const set<const Type *> &UsedTypes = FUT->getTypes();
+ FindUsedTypes &FUT = getAnalysis<FindUsedTypes>();
+ const set<const Type *> &UsedTypes = FUT.getTypes();
+
+ FindUnsafePointerTypes &FUPT = getAnalysis<FindUnsafePointerTypes>();
+ const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
+
// Combine the two sets, weeding out non structure types. Closures in C++
// Build up a set of structure types that we are going to modify, and
// information describing how to modify them.
std::map<const StructType*, vector<int> > Transforms;
+ TargetData &TD = getAnalysis<TargetData>();
for (set<const StructType*>::iterator I = TypesToModify.begin(),
E = TypesToModify.end(); I != E; ++I) {
const StructType *ST = *I;
vector<int> &Transform = Transforms[ST]; // Fill in the map directly
- GetTransformation(ST, Transform, XForm);
+ GetTransformation(TD, ST, Transform, XForm);
}
return Transforms;
}
-