blob: 908b5b1030a3c31cfe373ae1cf3d36a8d4166bfd [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
Chris Lattner04c85dc2002-01-21 07:52:35 +00008#include "llvm/Transforms/IPO/SimpleStructMutation.h"
9#include "llvm/Transforms/IPO/MutateStructTypes.h"
Chris Lattner7546c212001-11-10 07:28:25 +000010#include "llvm/Analysis/FindUsedTypes.h"
11#include "llvm/Analysis/FindUnsafePointerTypes.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000012#include "llvm/Target/TargetData.h"
13#include "llvm/DerivedTypes.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 Lattner497c60c2002-05-07 18:12:18 +000020// FIXME: TargetData Hack: Eventually we will have annotations given to us by
21// the backend so that we know stuff about type size and alignments. For now
22// though, just use this, because it happens to match the model that GCC and the
23// Sparc backend use.
24//
25const TargetData TD("SimpleStructMutation Should be GCC though!");
26
Chris Lattnerbd0ef772002-02-26 21:46:54 +000027namespace {
Chris Lattner96c466b2002-04-29 14:57:45 +000028 struct SimpleStructMutation : public MutateStructTypes {
Chris Lattnerbd0ef772002-02-26 21:46:54 +000029 enum Transform { SwapElements, SortElements } CurrentXForm;
30
31 SimpleStructMutation(enum Transform XForm) : CurrentXForm(XForm) {}
32
Chris Lattner96c466b2002-04-29 14:57:45 +000033 const char *getPassName() const { return "Simple Struct Mutation"; }
34
Chris Lattnerbd0ef772002-02-26 21:46:54 +000035 virtual bool run(Module *M) {
36 setTransforms(getTransforms(M, CurrentXForm));
37 bool Changed = MutateStructTypes::run(M);
38 clearTransforms();
39 return Changed;
40 }
41
Chris Lattnerf57b8452002-04-27 06:56:12 +000042 // getAnalysisUsage - This function needs the results of the
Chris Lattnerbd0ef772002-02-26 21:46:54 +000043 // FindUsedTypes and FindUnsafePointerTypes analysis passes...
44 //
Chris Lattnerf57b8452002-04-27 06:56:12 +000045 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.addRequired(FindUsedTypes::ID);
47 AU.addRequired(FindUnsafePointerTypes::ID);
48 MutateStructTypes::getAnalysisUsage(AU);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000049 }
50
51 private:
52 TransformsType getTransforms(Module *M, enum Transform);
53 };
54} // end anonymous namespace
55
56
Chris Lattner697954c2002-01-20 22:54:45 +000057
Chris Lattner7546c212001-11-10 07:28:25 +000058// PruneTypes - Given a type Ty, make sure that neither it, or one of its
59// subtypes, occur in TypesToModify.
60//
61static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
62 set<const Type*> &ProcessedTypes) {
63 if (ProcessedTypes.count(Ty)) return; // Already been checked
64 ProcessedTypes.insert(Ty);
65
66 // If the element is in TypesToModify, remove it now...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000067 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattner7546c212001-11-10 07:28:25 +000068 TypesToModify.erase(ST); // This doesn't fail if the element isn't present
Chris Lattner697954c2002-01-20 22:54:45 +000069 std::cerr << "Unable to swap type: " << ST << "\n";
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000070 }
Chris Lattner7546c212001-11-10 07:28:25 +000071
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000072 // Remove all types that this type contains as well... do not remove types
73 // that are referenced only through pointers, because we depend on the size of
74 // the pointer, not on what the structure points to.
Chris Lattner7546c212001-11-10 07:28:25 +000075 //
76 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000077 I != E; ++I) {
78 if (!isa<PointerType>(*I))
79 PruneTypes(*I, TypesToModify, ProcessedTypes);
80 }
Chris Lattner7546c212001-11-10 07:28:25 +000081}
82
Chris Lattner12739d92001-11-26 16:59:10 +000083static bool FirstLess(const pair<unsigned, unsigned> &LHS,
84 const pair<unsigned, unsigned> &RHS) {
85 return LHS.second < RHS.second;
86}
Chris Lattner7546c212001-11-10 07:28:25 +000087
Chris Lattner12739d92001-11-26 16:59:10 +000088static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
89 unsigned Field) {
90 for (unsigned i = 0; ; ++i)
91 if (Vec[i].first == Field) return i;
92}
93
94static inline void GetTransformation(const StructType *ST,
95 vector<int> &Transform,
Chris Lattner497c60c2002-05-07 18:12:18 +000096 enum SimpleStructMutation::Transform XForm) {
Chris Lattner12739d92001-11-26 16:59:10 +000097 unsigned NumElements = ST->getElementTypes().size();
98 Transform.reserve(NumElements);
99
100 switch (XForm) {
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000101 case SimpleStructMutation::SwapElements:
Chris Lattner12739d92001-11-26 16:59:10 +0000102 // The transformation to do is: just simply swap the elements
103 for (unsigned i = 0; i < NumElements; ++i)
104 Transform.push_back(NumElements-i-1);
105 break;
106
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000107 case SimpleStructMutation::SortElements: {
Chris Lattner12739d92001-11-26 16:59:10 +0000108 vector<pair<unsigned, unsigned> > ElList;
109
110 // Build mapping from index to size
111 for (unsigned i = 0; i < NumElements; ++i)
Chris Lattner697954c2002-01-20 22:54:45 +0000112 ElList.push_back(
113 std::make_pair(i, TD.getTypeSize(ST->getElementTypes()[i])));
Chris Lattner12739d92001-11-26 16:59:10 +0000114
115 sort(ElList.begin(), ElList.end(), ptr_fun(FirstLess));
116
117 for (unsigned i = 0; i < NumElements; ++i)
118 Transform.push_back(getIndex(ElList, i));
119
120 break;
121 }
122 }
123}
Chris Lattner7546c212001-11-10 07:28:25 +0000124
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000125
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000126SimpleStructMutation::TransformsType
127 SimpleStructMutation::getTransforms(Module *M, enum Transform XForm) {
Chris Lattner7546c212001-11-10 07:28:25 +0000128 // We need to know which types to modify, and which types we CAN'T modify
Chris Lattner793c6b82002-01-31 00:45:11 +0000129 // TODO: Do symbol tables as well
Chris Lattner7546c212001-11-10 07:28:25 +0000130
131 // Get the results out of the analyzers...
Chris Lattner793c6b82002-01-31 00:45:11 +0000132 FindUsedTypes &FUT = getAnalysis<FindUsedTypes>();
133 const set<const Type *> &UsedTypes = FUT.getTypes();
134
135 FindUnsafePointerTypes &FUPT = getAnalysis<FindUnsafePointerTypes>();
136 const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
137
Chris Lattner7546c212001-11-10 07:28:25 +0000138
139
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000140 // Combine the two sets, weeding out non structure types. Closures in C++
141 // sure would be nice.
Chris Lattner7546c212001-11-10 07:28:25 +0000142 set<const StructType*> TypesToModify;
143 for (set<const Type *>::const_iterator I = UsedTypes.begin(),
144 E = UsedTypes.end(); I != E; ++I)
145 if (const StructType *ST = dyn_cast<StructType>(*I))
146 TypesToModify.insert(ST);
147
148
149 // Go through the Unsafe types and remove all types from TypesToModify that we
150 // are not allowed to modify, because that would be unsafe.
151 //
152 set<const Type*> ProcessedTypes;
153 for (set<PointerType*>::const_iterator I = UnsafePTys.begin(),
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000154 E = UnsafePTys.end(); I != E; ++I) {
Chris Lattner697954c2002-01-20 22:54:45 +0000155 //cerr << "Pruning type: " << *I << "\n";
Chris Lattner7546c212001-11-10 07:28:25 +0000156 PruneTypes(*I, TypesToModify, ProcessedTypes);
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000157 }
Chris Lattner7546c212001-11-10 07:28:25 +0000158
159
160 // Build up a set of structure types that we are going to modify, and
161 // information describing how to modify them.
Chris Lattner697954c2002-01-20 22:54:45 +0000162 std::map<const StructType*, vector<int> > Transforms;
Chris Lattner7546c212001-11-10 07:28:25 +0000163
164 for (set<const StructType*>::iterator I = TypesToModify.begin(),
165 E = TypesToModify.end(); I != E; ++I) {
166 const StructType *ST = *I;
Chris Lattner7546c212001-11-10 07:28:25 +0000167
168 vector<int> &Transform = Transforms[ST]; // Fill in the map directly
Chris Lattner12739d92001-11-26 16:59:10 +0000169 GetTransformation(ST, Transform, XForm);
Chris Lattner7546c212001-11-10 07:28:25 +0000170 }
171
Chris Lattner12739d92001-11-26 16:59:10 +0000172 return Transforms;
Chris Lattner7546c212001-11-10 07:28:25 +0000173}
174
Chris Lattner793c6b82002-01-31 00:45:11 +0000175
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000176Pass *createSwapElementsPass() {
177 return new SimpleStructMutation(SimpleStructMutation::SwapElements);
Chris Lattner793c6b82002-01-31 00:45:11 +0000178}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000179Pass *createSortElementsPass() {
180 return new SimpleStructMutation(SimpleStructMutation::SortElements);
181}
182