From: Chris Lattner Date: Mon, 26 Nov 2001 16:59:10 +0000 (+0000) Subject: Support selectable structure transformations X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=12739d9f871b89f8a0b0db4cd2b15c7584029781;p=oota-llvm.git Support selectable structure transformations git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1342 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/IPO/SimpleStructMutation.cpp b/lib/Transforms/IPO/SimpleStructMutation.cpp index c8f8896766f..3807866214c 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 #include "llvm/Assembly/Writer.h" @@ -39,11 +40,51 @@ static void PruneTypes(const Type *Ty, set &TypesToModify, } } +static bool FirstLess(const pair &LHS, + const pair &RHS) { + return LHS.second < RHS.second; +} + +static unsigned getIndex(const vector > &Vec, + unsigned Field) { + for (unsigned i = 0; ; ++i) + if (Vec[i].first == Field) return i; +} + +static inline void GetTransformation(const StructType *ST, + vector &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 > 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::iterator I = TypesToModify.begin(), E = TypesToModify.end(); I != E; ++I) { const StructType *ST = *I; - unsigned NumElements = ST->getElementTypes().size(); vector &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; }