2002-04-27 06:56:12 +00:00
|
|
|
//===- SimpleStructMutation.cpp - Swap structure elements around -*- C++ -*--=//
|
2001-11-10 07:28:25 +00:00
|
|
|
//
|
|
|
|
// This pass does a simple transformation that swaps all of the elements of the
|
|
|
|
// struct types in the program around.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-07-24 17:12:05 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2002-01-21 07:52:35 +00:00
|
|
|
#include "llvm/Transforms/IPO/MutateStructTypes.h"
|
2001-11-10 07:28:25 +00:00
|
|
|
#include "llvm/Analysis/FindUsedTypes.h"
|
|
|
|
#include "llvm/Analysis/FindUnsafePointerTypes.h"
|
2002-05-07 18:12:18 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-11-26 16:59:10 +00:00
|
|
|
#include <algorithm>
|
2002-01-20 22:54:45 +00:00
|
|
|
#include <iostream>
|
|
|
|
using std::vector;
|
|
|
|
using std::set;
|
|
|
|
using std::pair;
|
2001-11-10 07:28:25 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
namespace {
|
2002-04-29 14:57:45 +00:00
|
|
|
struct SimpleStructMutation : public MutateStructTypes {
|
2002-07-23 18:03:11 +00:00
|
|
|
enum Transform { SwapElements, SortElements };
|
|
|
|
const TargetData &TD;
|
|
|
|
SimpleStructMutation(const TargetData &td) : TD(td) {}
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2002-07-23 18:03:11 +00:00
|
|
|
virtual bool run(Module &M) = 0;
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
// getAnalysisUsage - This function needs the results of the
|
2002-02-26 21:46:54 +00:00
|
|
|
// FindUsedTypes and FindUnsafePointerTypes analysis passes...
|
|
|
|
//
|
2002-04-27 06:56:12 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-08-08 19:01:30 +00:00
|
|
|
AU.addRequired<FindUsedTypes>();
|
|
|
|
AU.addRequired<FindUnsafePointerTypes>();
|
2002-04-27 06:56:12 +00:00
|
|
|
MutateStructTypes::getAnalysisUsage(AU);
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
2002-07-23 18:03:11 +00:00
|
|
|
protected:
|
2002-06-25 16:13:21 +00:00
|
|
|
TransformsType getTransforms(Module &M, enum Transform);
|
2002-02-26 21:46:54 +00:00
|
|
|
};
|
2002-07-23 18:03:11 +00:00
|
|
|
|
|
|
|
struct SwapStructElements : public SimpleStructMutation {
|
|
|
|
SwapStructElements(const TargetData &TD) : SimpleStructMutation(TD) {}
|
|
|
|
|
|
|
|
virtual bool run(Module &M) {
|
|
|
|
setTransforms(getTransforms(M, SwapElements));
|
|
|
|
bool Changed = MutateStructTypes::run(M);
|
|
|
|
clearTransforms();
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SortStructElements : public SimpleStructMutation {
|
|
|
|
SortStructElements(const TargetData &TD) : SimpleStructMutation(TD) {}
|
|
|
|
|
|
|
|
virtual bool run(Module &M) {
|
|
|
|
setTransforms(getTransforms(M, SortElements));
|
|
|
|
bool Changed = MutateStructTypes::run(M);
|
|
|
|
clearTransforms();
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
};
|
2002-02-26 21:46:54 +00:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
|
2001-11-10 07:28:25 +00:00
|
|
|
// PruneTypes - Given a type Ty, make sure that neither it, or one of its
|
|
|
|
// subtypes, occur in TypesToModify.
|
|
|
|
//
|
|
|
|
static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
|
|
|
|
set<const Type*> &ProcessedTypes) {
|
|
|
|
if (ProcessedTypes.count(Ty)) return; // Already been checked
|
|
|
|
ProcessedTypes.insert(Ty);
|
|
|
|
|
|
|
|
// If the element is in TypesToModify, remove it now...
|
2001-11-14 11:02:49 +00:00
|
|
|
if (const StructType *ST = dyn_cast<StructType>(Ty)) {
|
2001-11-10 07:28:25 +00:00
|
|
|
TypesToModify.erase(ST); // This doesn't fail if the element isn't present
|
2002-01-20 22:54:45 +00:00
|
|
|
std::cerr << "Unable to swap type: " << ST << "\n";
|
2001-11-14 11:02:49 +00:00
|
|
|
}
|
2001-11-10 07:28:25 +00:00
|
|
|
|
2001-11-14 11:02:49 +00:00
|
|
|
// Remove all types that this type contains as well... do not remove types
|
|
|
|
// that are referenced only through pointers, because we depend on the size of
|
|
|
|
// the pointer, not on what the structure points to.
|
2001-11-10 07:28:25 +00:00
|
|
|
//
|
|
|
|
for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
|
2001-11-14 11:02:49 +00:00
|
|
|
I != E; ++I) {
|
|
|
|
if (!isa<PointerType>(*I))
|
|
|
|
PruneTypes(*I, TypesToModify, ProcessedTypes);
|
|
|
|
}
|
2001-11-10 07:28:25 +00:00
|
|
|
}
|
|
|
|
|
2001-11-26 16:59:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-07-23 18:03:11 +00:00
|
|
|
static inline void GetTransformation(const TargetData &TD, const StructType *ST,
|
2001-11-26 16:59:10 +00:00
|
|
|
vector<int> &Transform,
|
2002-05-07 18:12:18 +00:00
|
|
|
enum SimpleStructMutation::Transform XForm) {
|
2001-11-26 16:59:10 +00:00
|
|
|
unsigned NumElements = ST->getElementTypes().size();
|
|
|
|
Transform.reserve(NumElements);
|
|
|
|
|
|
|
|
switch (XForm) {
|
2002-01-21 07:31:50 +00:00
|
|
|
case SimpleStructMutation::SwapElements:
|
2001-11-26 16:59:10 +00:00
|
|
|
// The transformation to do is: just simply swap the elements
|
|
|
|
for (unsigned i = 0; i < NumElements; ++i)
|
|
|
|
Transform.push_back(NumElements-i-1);
|
|
|
|
break;
|
|
|
|
|
2002-01-21 07:31:50 +00:00
|
|
|
case SimpleStructMutation::SortElements: {
|
2001-11-26 16:59:10 +00:00
|
|
|
vector<pair<unsigned, unsigned> > ElList;
|
|
|
|
|
|
|
|
// Build mapping from index to size
|
|
|
|
for (unsigned i = 0; i < NumElements; ++i)
|
2002-01-20 22:54:45 +00:00
|
|
|
ElList.push_back(
|
|
|
|
std::make_pair(i, TD.getTypeSize(ST->getElementTypes()[i])));
|
2001-11-10 07:28:25 +00:00
|
|
|
|
2001-11-26 16:59:10 +00:00
|
|
|
sort(ElList.begin(), ElList.end(), ptr_fun(FirstLess));
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NumElements; ++i)
|
|
|
|
Transform.push_back(getIndex(ElList, i));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-10 07:28:25 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2002-01-21 07:31:50 +00:00
|
|
|
SimpleStructMutation::TransformsType
|
2002-06-25 16:13:21 +00:00
|
|
|
SimpleStructMutation::getTransforms(Module &, enum Transform XForm) {
|
2001-11-10 07:28:25 +00:00
|
|
|
// We need to know which types to modify, and which types we CAN'T modify
|
2002-01-31 00:45:11 +00:00
|
|
|
// TODO: Do symbol tables as well
|
2001-11-10 07:28:25 +00:00
|
|
|
|
|
|
|
// Get the results out of the analyzers...
|
2002-01-31 00:45:11 +00:00
|
|
|
FindUsedTypes &FUT = getAnalysis<FindUsedTypes>();
|
|
|
|
const set<const Type *> &UsedTypes = FUT.getTypes();
|
|
|
|
|
|
|
|
FindUnsafePointerTypes &FUPT = getAnalysis<FindUnsafePointerTypes>();
|
|
|
|
const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
|
|
|
|
|
2001-11-10 07:28:25 +00:00
|
|
|
|
|
|
|
|
2001-11-14 11:02:49 +00:00
|
|
|
// Combine the two sets, weeding out non structure types. Closures in C++
|
|
|
|
// sure would be nice.
|
2001-11-10 07:28:25 +00:00
|
|
|
set<const StructType*> TypesToModify;
|
|
|
|
for (set<const Type *>::const_iterator I = UsedTypes.begin(),
|
|
|
|
E = UsedTypes.end(); I != E; ++I)
|
|
|
|
if (const StructType *ST = dyn_cast<StructType>(*I))
|
|
|
|
TypesToModify.insert(ST);
|
|
|
|
|
|
|
|
|
|
|
|
// Go through the Unsafe types and remove all types from TypesToModify that we
|
|
|
|
// are not allowed to modify, because that would be unsafe.
|
|
|
|
//
|
|
|
|
set<const Type*> ProcessedTypes;
|
|
|
|
for (set<PointerType*>::const_iterator I = UnsafePTys.begin(),
|
2001-11-14 11:02:49 +00:00
|
|
|
E = UnsafePTys.end(); I != E; ++I) {
|
2002-01-20 22:54:45 +00:00
|
|
|
//cerr << "Pruning type: " << *I << "\n";
|
2001-11-10 07:28:25 +00:00
|
|
|
PruneTypes(*I, TypesToModify, ProcessedTypes);
|
2001-11-14 11:02:49 +00:00
|
|
|
}
|
2001-11-10 07:28:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Build up a set of structure types that we are going to modify, and
|
|
|
|
// information describing how to modify them.
|
2002-01-20 22:54:45 +00:00
|
|
|
std::map<const StructType*, vector<int> > Transforms;
|
2001-11-10 07:28:25 +00:00
|
|
|
|
|
|
|
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
|
2002-07-23 18:03:11 +00:00
|
|
|
GetTransformation(TD, ST, Transform, XForm);
|
2001-11-10 07:28:25 +00:00
|
|
|
}
|
|
|
|
|
2001-11-26 16:59:10 +00:00
|
|
|
return Transforms;
|
2001-11-10 07:28:25 +00:00
|
|
|
}
|
|
|
|
|
2002-01-31 00:45:11 +00:00
|
|
|
|
2002-07-23 18:03:11 +00:00
|
|
|
Pass *createSwapElementsPass(const TargetData &TD) {
|
|
|
|
return new SwapStructElements(TD);
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
2002-07-23 18:03:11 +00:00
|
|
|
Pass *createSortElementsPass(const TargetData &TD) {
|
|
|
|
return new SortStructElements(TD);
|
2002-01-31 00:45:11 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2002-07-23 18:03:11 +00:00
|
|
|
namespace {
|
2002-07-26 21:12:44 +00:00
|
|
|
RegisterOpt<SwapStructElements> X("swapstructs",
|
|
|
|
"Swap structure types around",
|
|
|
|
createSwapElementsPass);
|
|
|
|
RegisterOpt<SortStructElements> Y("sortstructs",
|
|
|
|
"Sort structure elements by size",
|
|
|
|
createSortElementsPass);
|
2002-07-23 18:03:11 +00:00
|
|
|
}
|