aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Transforms
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2001-11-26 16:59:10 +0000
committerChris Lattner <sabre@nondot.org>2001-11-26 16:59:10 +0000
commit12739d9f871b89f8a0b0db4cd2b15c7584029781 (patch)
treea7f411dceb16819d2dfa4c7d314d28e1b25922af /lib/Transforms
parent8090e8c18e7093fc3706a5a5d688143f3effe513 (diff)
downloadexternal_llvm-12739d9f871b89f8a0b0db4cd2b15c7584029781.zip
external_llvm-12739d9f871b89f8a0b0db4cd2b15c7584029781.tar.gz
external_llvm-12739d9f871b89f8a0b0db4cd2b15c7584029781.tar.bz2
Support selectable structure transformations
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1342 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
-rw-r--r--lib/Transforms/IPO/SimpleStructMutation.cpp58
1 files changed, 45 insertions, 13 deletions
diff --git a/lib/Transforms/IPO/SimpleStructMutation.cpp b/lib/Transforms/IPO/SimpleStructMutation.cpp
index c8f8896..3807866 100644
--- a/lib/Transforms/IPO/SimpleStructMutation.cpp
+++ b/lib/Transforms/IPO/SimpleStructMutation.cpp
@@ -10,7 +10,8 @@
#include "llvm/Transforms/MutateStructTypes.h"
#include "llvm/Analysis/FindUsedTypes.h"
#include "llvm/Analysis/FindUnsafePointerTypes.h"
-#include "llvm/DerivedTypes.h"
+#include "TransformInternals.h"
+#include <algorithm>
#include "llvm/Assembly/Writer.h"
@@ -39,11 +40,51 @@ static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
}
}
+static bool FirstLess(const pair<unsigned, unsigned> &LHS,
+ const pair<unsigned, unsigned> &RHS) {
+ return LHS.second < RHS.second;
+}
+
+static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
+ unsigned Field) {
+ for (unsigned i = 0; ; ++i)
+ if (Vec[i].first == Field) return i;
+}
+
+static inline void GetTransformation(const StructType *ST,
+ vector<int> &Transform,
+ enum PrebuiltStructMutation::Transform XForm) {
+ unsigned NumElements = ST->getElementTypes().size();
+ Transform.reserve(NumElements);
+
+ switch (XForm) {
+ case PrebuiltStructMutation::SwapElements:
+ // The transformation to do is: just simply swap the elements
+ for (unsigned i = 0; i < NumElements; ++i)
+ Transform.push_back(NumElements-i-1);
+ break;
+
+ case PrebuiltStructMutation::SortElements: {
+ vector<pair<unsigned, unsigned> > ElList;
+
+ // Build mapping from index to size
+ for (unsigned i = 0; i < NumElements; ++i)
+ ElList.push_back(make_pair(i, TD.getTypeSize(ST->getElementTypes()[i])));
+ sort(ElList.begin(), ElList.end(), ptr_fun(FirstLess));
+
+ for (unsigned i = 0; i < NumElements; ++i)
+ Transform.push_back(getIndex(ElList, i));
+
+ break;
+ }
+ }
+}
// doPassInitialization - This does all of the work of the pass
//
-bool SwapStructContents::doPassInitialization(Module *M) {
+PrebuiltStructMutation::TransformsType
+ PrebuiltStructMutation::getTransforms(Module *M, enum Transform XForm) {
// We need to know which types to modify, and which types we CAN'T modify
FindUsedTypes FUT/*(true)*/; // TODO: Do symbol tables as well
FindUnsafePointerTypes FUPT;
@@ -89,20 +130,11 @@ bool SwapStructContents::doPassInitialization(Module *M) {
for (set<const StructType*>::iterator I = TypesToModify.begin(),
E = TypesToModify.end(); I != E; ++I) {
const StructType *ST = *I;
- unsigned NumElements = ST->getElementTypes().size();
vector<int> &Transform = Transforms[ST]; // Fill in the map directly
- Transform.reserve(NumElements);
-
- // The transformation to do is: just simply swap the elements
- for (unsigned i = 0; i < NumElements; ++i)
- Transform.push_back(NumElements-i-1);
+ GetTransformation(ST, Transform, XForm);
}
- // Create the Worker to do our stuff for us...
- StructMutator = new MutateStructTypes(Transforms);
-
- // Do initial work.
- return StructMutator->doPassInitialization(M);
+ return Transforms;
}