blob: c0d9ef46bc29e88c408bc052924da221855a081e [file] [log] [blame]
Chris Lattnerf57b8452002-04-27 06:56:12 +00001//===- SimpleStructMutation.cpp - Swap structure elements around -*- C++ -*--=//
Chris Lattner7546c212001-11-10 07:28:25 +00002//
3// This pass does a simple transformation that swaps all of the elements of the
4// struct types in the program around.
5//
6//===----------------------------------------------------------------------===//
7
8
Chris Lattner04c85dc2002-01-21 07:52:35 +00009#include "llvm/Transforms/IPO/SimpleStructMutation.h"
10#include "llvm/Transforms/IPO/MutateStructTypes.h"
Chris Lattner7546c212001-11-10 07:28:25 +000011#include "llvm/Analysis/FindUsedTypes.h"
12#include "llvm/Analysis/FindUnsafePointerTypes.h"
Chris Lattner04c85dc2002-01-21 07:52:35 +000013#include "../TransformInternals.h"
Chris Lattner12739d92001-11-26 16:59:10 +000014#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000015#include <iostream>
16using std::vector;
17using std::set;
18using std::pair;
Chris Lattner7546c212001-11-10 07:28:25 +000019
Chris Lattnerbd0ef772002-02-26 21:46:54 +000020namespace {
Chris Lattner96c466b2002-04-29 14:57:45 +000021 struct SimpleStructMutation : public MutateStructTypes {
Chris Lattnerbd0ef772002-02-26 21:46:54 +000022 enum Transform { SwapElements, SortElements } CurrentXForm;
23
24 SimpleStructMutation(enum Transform XForm) : CurrentXForm(XForm) {}
25
Chris Lattner96c466b2002-04-29 14:57:45 +000026 const char *getPassName() const { return "Simple Struct Mutation"; }
27
Chris Lattnerbd0ef772002-02-26 21:46:54 +000028 virtual bool run(Module *M) {
29 setTransforms(getTransforms(M, CurrentXForm));
30 bool Changed = MutateStructTypes::run(M);
31 clearTransforms();
32 return Changed;
33 }
34
Chris Lattnerf57b8452002-04-27 06:56:12 +000035 // getAnalysisUsage - This function needs the results of the
Chris Lattnerbd0ef772002-02-26 21:46:54 +000036 // FindUsedTypes and FindUnsafePointerTypes analysis passes...
37 //
Chris Lattnerf57b8452002-04-27 06:56:12 +000038 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.addRequired(FindUsedTypes::ID);
40 AU.addRequired(FindUnsafePointerTypes::ID);
41 MutateStructTypes::getAnalysisUsage(AU);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000042 }
43
44 private:
45 TransformsType getTransforms(Module *M, enum Transform);
46 };
47} // end anonymous namespace
48
49
Chris Lattner697954c2002-01-20 22:54:45 +000050
Chris Lattner7546c212001-11-10 07:28:25 +000051// PruneTypes - Given a type Ty, make sure that neither it, or one of its
52// subtypes, occur in TypesToModify.
53//
54static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
55 set<const Type*> &ProcessedTypes) {
56 if (ProcessedTypes.count(Ty)) return; // Already been checked
57 ProcessedTypes.insert(Ty);
58
59 // If the element is in TypesToModify, remove it now...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000060 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattner7546c212001-11-10 07:28:25 +000061 TypesToModify.erase(ST); // This doesn't fail if the element isn't present
Chris Lattner697954c2002-01-20 22:54:45 +000062 std::cerr << "Unable to swap type: " << ST << "\n";
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000063 }
Chris Lattner7546c212001-11-10 07:28:25 +000064
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000065 // Remove all types that this type contains as well... do not remove types
66 // that are referenced only through pointers, because we depend on the size of
67 // the pointer, not on what the structure points to.
Chris Lattner7546c212001-11-10 07:28:25 +000068 //
69 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000070 I != E; ++I) {
71 if (!isa<PointerType>(*I))
72 PruneTypes(*I, TypesToModify, ProcessedTypes);
73 }
Chris Lattner7546c212001-11-10 07:28:25 +000074}
75
Chris Lattner12739d92001-11-26 16:59:10 +000076static bool FirstLess(const pair<unsigned, unsigned> &LHS,
77 const pair<unsigned, unsigned> &RHS) {
78 return LHS.second < RHS.second;
79}
Chris Lattner7546c212001-11-10 07:28:25 +000080
Chris Lattner12739d92001-11-26 16:59:10 +000081static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
82 unsigned Field) {
83 for (unsigned i = 0; ; ++i)
84 if (Vec[i].first == Field) return i;
85}
86
87static inline void GetTransformation(const StructType *ST,
88 vector<int> &Transform,
Chris Lattnerf4de63f2002-01-21 07:31:50 +000089 enum SimpleStructMutation::Transform XForm) {
Chris Lattner12739d92001-11-26 16:59:10 +000090 unsigned NumElements = ST->getElementTypes().size();
91 Transform.reserve(NumElements);
92
93 switch (XForm) {
Chris Lattnerf4de63f2002-01-21 07:31:50 +000094 case SimpleStructMutation::SwapElements:
Chris Lattner12739d92001-11-26 16:59:10 +000095 // The transformation to do is: just simply swap the elements
96 for (unsigned i = 0; i < NumElements; ++i)
97 Transform.push_back(NumElements-i-1);
98 break;
99
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000100 case SimpleStructMutation::SortElements: {
Chris Lattner12739d92001-11-26 16:59:10 +0000101 vector<pair<unsigned, unsigned> > ElList;
102
103 // Build mapping from index to size
104 for (unsigned i = 0; i < NumElements; ++i)
Chris Lattner697954c2002-01-20 22:54:45 +0000105 ElList.push_back(
106 std::make_pair(i, TD.getTypeSize(ST->getElementTypes()[i])));
Chris Lattner12739d92001-11-26 16:59:10 +0000107
108 sort(ElList.begin(), ElList.end(), ptr_fun(FirstLess));
109
110 for (unsigned i = 0; i < NumElements; ++i)
111 Transform.push_back(getIndex(ElList, i));
112
113 break;
114 }
115 }
116}
Chris Lattner7546c212001-11-10 07:28:25 +0000117
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000118
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000119SimpleStructMutation::TransformsType
120 SimpleStructMutation::getTransforms(Module *M, enum Transform XForm) {
Chris Lattner7546c212001-11-10 07:28:25 +0000121 // We need to know which types to modify, and which types we CAN'T modify
Chris Lattner793c6b82002-01-31 00:45:11 +0000122 // TODO: Do symbol tables as well
Chris Lattner7546c212001-11-10 07:28:25 +0000123
124 // Get the results out of the analyzers...
Chris Lattner793c6b82002-01-31 00:45:11 +0000125 FindUsedTypes &FUT = getAnalysis<FindUsedTypes>();
126 const set<const Type *> &UsedTypes = FUT.getTypes();
127
128 FindUnsafePointerTypes &FUPT = getAnalysis<FindUnsafePointerTypes>();
129 const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
130
Chris Lattner7546c212001-11-10 07:28:25 +0000131
132
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000133 // Combine the two sets, weeding out non structure types. Closures in C++
134 // sure would be nice.
Chris Lattner7546c212001-11-10 07:28:25 +0000135 set<const StructType*> TypesToModify;
136 for (set<const Type *>::const_iterator I = UsedTypes.begin(),
137 E = UsedTypes.end(); I != E; ++I)
138 if (const StructType *ST = dyn_cast<StructType>(*I))
139 TypesToModify.insert(ST);
140
141
142 // Go through the Unsafe types and remove all types from TypesToModify that we
143 // are not allowed to modify, because that would be unsafe.
144 //
145 set<const Type*> ProcessedTypes;
146 for (set<PointerType*>::const_iterator I = UnsafePTys.begin(),
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000147 E = UnsafePTys.end(); I != E; ++I) {
Chris Lattner697954c2002-01-20 22:54:45 +0000148 //cerr << "Pruning type: " << *I << "\n";
Chris Lattner7546c212001-11-10 07:28:25 +0000149 PruneTypes(*I, TypesToModify, ProcessedTypes);
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000150 }
Chris Lattner7546c212001-11-10 07:28:25 +0000151
152
153 // Build up a set of structure types that we are going to modify, and
154 // information describing how to modify them.
Chris Lattner697954c2002-01-20 22:54:45 +0000155 std::map<const StructType*, vector<int> > Transforms;
Chris Lattner7546c212001-11-10 07:28:25 +0000156
157 for (set<const StructType*>::iterator I = TypesToModify.begin(),
158 E = TypesToModify.end(); I != E; ++I) {
159 const StructType *ST = *I;
Chris Lattner7546c212001-11-10 07:28:25 +0000160
161 vector<int> &Transform = Transforms[ST]; // Fill in the map directly
Chris Lattner12739d92001-11-26 16:59:10 +0000162 GetTransformation(ST, Transform, XForm);
Chris Lattner7546c212001-11-10 07:28:25 +0000163 }
164
Chris Lattner12739d92001-11-26 16:59:10 +0000165 return Transforms;
Chris Lattner7546c212001-11-10 07:28:25 +0000166}
167
Chris Lattner793c6b82002-01-31 00:45:11 +0000168
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000169Pass *createSwapElementsPass() {
170 return new SimpleStructMutation(SimpleStructMutation::SwapElements);
Chris Lattner793c6b82002-01-31 00:45:11 +0000171}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000172Pass *createSortElementsPass() {
173 return new SimpleStructMutation(SimpleStructMutation::SortElements);
174}
175