blob: 33e028950476a639f707c1e18c9f3920732d6e62 [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 {
21 class SimpleStructMutation : public MutateStructTypes {
22 public:
23 enum Transform { SwapElements, SortElements } CurrentXForm;
24
25 SimpleStructMutation(enum Transform XForm) : CurrentXForm(XForm) {}
26
27 virtual bool run(Module *M) {
28 setTransforms(getTransforms(M, CurrentXForm));
29 bool Changed = MutateStructTypes::run(M);
30 clearTransforms();
31 return Changed;
32 }
33
Chris Lattnerf57b8452002-04-27 06:56:12 +000034 // getAnalysisUsage - This function needs the results of the
Chris Lattnerbd0ef772002-02-26 21:46:54 +000035 // FindUsedTypes and FindUnsafePointerTypes analysis passes...
36 //
Chris Lattnerf57b8452002-04-27 06:56:12 +000037 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.addRequired(FindUsedTypes::ID);
39 AU.addRequired(FindUnsafePointerTypes::ID);
40 MutateStructTypes::getAnalysisUsage(AU);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000041 }
42
43 private:
44 TransformsType getTransforms(Module *M, enum Transform);
45 };
46} // end anonymous namespace
47
48
Chris Lattner697954c2002-01-20 22:54:45 +000049
Chris Lattner7546c212001-11-10 07:28:25 +000050// PruneTypes - Given a type Ty, make sure that neither it, or one of its
51// subtypes, occur in TypesToModify.
52//
53static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
54 set<const Type*> &ProcessedTypes) {
55 if (ProcessedTypes.count(Ty)) return; // Already been checked
56 ProcessedTypes.insert(Ty);
57
58 // If the element is in TypesToModify, remove it now...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000059 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattner7546c212001-11-10 07:28:25 +000060 TypesToModify.erase(ST); // This doesn't fail if the element isn't present
Chris Lattner697954c2002-01-20 22:54:45 +000061 std::cerr << "Unable to swap type: " << ST << "\n";
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000062 }
Chris Lattner7546c212001-11-10 07:28:25 +000063
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000064 // Remove all types that this type contains as well... do not remove types
65 // that are referenced only through pointers, because we depend on the size of
66 // the pointer, not on what the structure points to.
Chris Lattner7546c212001-11-10 07:28:25 +000067 //
68 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000069 I != E; ++I) {
70 if (!isa<PointerType>(*I))
71 PruneTypes(*I, TypesToModify, ProcessedTypes);
72 }
Chris Lattner7546c212001-11-10 07:28:25 +000073}
74
Chris Lattner12739d92001-11-26 16:59:10 +000075static bool FirstLess(const pair<unsigned, unsigned> &LHS,
76 const pair<unsigned, unsigned> &RHS) {
77 return LHS.second < RHS.second;
78}
Chris Lattner7546c212001-11-10 07:28:25 +000079
Chris Lattner12739d92001-11-26 16:59:10 +000080static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
81 unsigned Field) {
82 for (unsigned i = 0; ; ++i)
83 if (Vec[i].first == Field) return i;
84}
85
86static inline void GetTransformation(const StructType *ST,
87 vector<int> &Transform,
Chris Lattnerf4de63f2002-01-21 07:31:50 +000088 enum SimpleStructMutation::Transform XForm) {
Chris Lattner12739d92001-11-26 16:59:10 +000089 unsigned NumElements = ST->getElementTypes().size();
90 Transform.reserve(NumElements);
91
92 switch (XForm) {
Chris Lattnerf4de63f2002-01-21 07:31:50 +000093 case SimpleStructMutation::SwapElements:
Chris Lattner12739d92001-11-26 16:59:10 +000094 // The transformation to do is: just simply swap the elements
95 for (unsigned i = 0; i < NumElements; ++i)
96 Transform.push_back(NumElements-i-1);
97 break;
98
Chris Lattnerf4de63f2002-01-21 07:31:50 +000099 case SimpleStructMutation::SortElements: {
Chris Lattner12739d92001-11-26 16:59:10 +0000100 vector<pair<unsigned, unsigned> > ElList;
101
102 // Build mapping from index to size
103 for (unsigned i = 0; i < NumElements; ++i)
Chris Lattner697954c2002-01-20 22:54:45 +0000104 ElList.push_back(
105 std::make_pair(i, TD.getTypeSize(ST->getElementTypes()[i])));
Chris Lattner12739d92001-11-26 16:59:10 +0000106
107 sort(ElList.begin(), ElList.end(), ptr_fun(FirstLess));
108
109 for (unsigned i = 0; i < NumElements; ++i)
110 Transform.push_back(getIndex(ElList, i));
111
112 break;
113 }
114 }
115}
Chris Lattner7546c212001-11-10 07:28:25 +0000116
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000117
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000118SimpleStructMutation::TransformsType
119 SimpleStructMutation::getTransforms(Module *M, enum Transform XForm) {
Chris Lattner7546c212001-11-10 07:28:25 +0000120 // We need to know which types to modify, and which types we CAN'T modify
Chris Lattner793c6b82002-01-31 00:45:11 +0000121 // TODO: Do symbol tables as well
Chris Lattner7546c212001-11-10 07:28:25 +0000122
123 // Get the results out of the analyzers...
Chris Lattner793c6b82002-01-31 00:45:11 +0000124 FindUsedTypes &FUT = getAnalysis<FindUsedTypes>();
125 const set<const Type *> &UsedTypes = FUT.getTypes();
126
127 FindUnsafePointerTypes &FUPT = getAnalysis<FindUnsafePointerTypes>();
128 const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
129
Chris Lattner7546c212001-11-10 07:28:25 +0000130
131
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000132 // Combine the two sets, weeding out non structure types. Closures in C++
133 // sure would be nice.
Chris Lattner7546c212001-11-10 07:28:25 +0000134 set<const StructType*> TypesToModify;
135 for (set<const Type *>::const_iterator I = UsedTypes.begin(),
136 E = UsedTypes.end(); I != E; ++I)
137 if (const StructType *ST = dyn_cast<StructType>(*I))
138 TypesToModify.insert(ST);
139
140
141 // Go through the Unsafe types and remove all types from TypesToModify that we
142 // are not allowed to modify, because that would be unsafe.
143 //
144 set<const Type*> ProcessedTypes;
145 for (set<PointerType*>::const_iterator I = UnsafePTys.begin(),
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000146 E = UnsafePTys.end(); I != E; ++I) {
Chris Lattner697954c2002-01-20 22:54:45 +0000147 //cerr << "Pruning type: " << *I << "\n";
Chris Lattner7546c212001-11-10 07:28:25 +0000148 PruneTypes(*I, TypesToModify, ProcessedTypes);
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000149 }
Chris Lattner7546c212001-11-10 07:28:25 +0000150
151
152 // Build up a set of structure types that we are going to modify, and
153 // information describing how to modify them.
Chris Lattner697954c2002-01-20 22:54:45 +0000154 std::map<const StructType*, vector<int> > Transforms;
Chris Lattner7546c212001-11-10 07:28:25 +0000155
156 for (set<const StructType*>::iterator I = TypesToModify.begin(),
157 E = TypesToModify.end(); I != E; ++I) {
158 const StructType *ST = *I;
Chris Lattner7546c212001-11-10 07:28:25 +0000159
160 vector<int> &Transform = Transforms[ST]; // Fill in the map directly
Chris Lattner12739d92001-11-26 16:59:10 +0000161 GetTransformation(ST, Transform, XForm);
Chris Lattner7546c212001-11-10 07:28:25 +0000162 }
163
Chris Lattner12739d92001-11-26 16:59:10 +0000164 return Transforms;
Chris Lattner7546c212001-11-10 07:28:25 +0000165}
166
Chris Lattner793c6b82002-01-31 00:45:11 +0000167
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000168Pass *createSwapElementsPass() {
169 return new SimpleStructMutation(SimpleStructMutation::SwapElements);
Chris Lattner793c6b82002-01-31 00:45:11 +0000170}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000171Pass *createSortElementsPass() {
172 return new SimpleStructMutation(SimpleStructMutation::SortElements);
173}
174