blob: 5c64aa3537db1b00eccbd84040b6e367053c7e87 [file] [log] [blame]
Chris Lattner04c85dc2002-01-21 07:52:35 +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
34 // getAnalysisUsageInfo - This function needs the results of the
35 // FindUsedTypes and FindUnsafePointerTypes analysis passes...
36 //
37 virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Required,
38 Pass::AnalysisSet &Destroyed,
39 Pass::AnalysisSet &Provided) {
40 Required.push_back(FindUsedTypes::ID);
41 Required.push_back(FindUnsafePointerTypes::ID);
42 MutateStructTypes::getAnalysisUsageInfo(Required, Destroyed, Provided);
43 }
44
45 private:
46 TransformsType getTransforms(Module *M, enum Transform);
47 };
48} // end anonymous namespace
49
50
Chris Lattner697954c2002-01-20 22:54:45 +000051
Chris Lattner7546c212001-11-10 07:28:25 +000052// PruneTypes - Given a type Ty, make sure that neither it, or one of its
53// subtypes, occur in TypesToModify.
54//
55static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
56 set<const Type*> &ProcessedTypes) {
57 if (ProcessedTypes.count(Ty)) return; // Already been checked
58 ProcessedTypes.insert(Ty);
59
60 // If the element is in TypesToModify, remove it now...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000061 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattner7546c212001-11-10 07:28:25 +000062 TypesToModify.erase(ST); // This doesn't fail if the element isn't present
Chris Lattner697954c2002-01-20 22:54:45 +000063 std::cerr << "Unable to swap type: " << ST << "\n";
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000064 }
Chris Lattner7546c212001-11-10 07:28:25 +000065
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000066 // Remove all types that this type contains as well... do not remove types
67 // that are referenced only through pointers, because we depend on the size of
68 // the pointer, not on what the structure points to.
Chris Lattner7546c212001-11-10 07:28:25 +000069 //
70 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000071 I != E; ++I) {
72 if (!isa<PointerType>(*I))
73 PruneTypes(*I, TypesToModify, ProcessedTypes);
74 }
Chris Lattner7546c212001-11-10 07:28:25 +000075}
76
Chris Lattner12739d92001-11-26 16:59:10 +000077static bool FirstLess(const pair<unsigned, unsigned> &LHS,
78 const pair<unsigned, unsigned> &RHS) {
79 return LHS.second < RHS.second;
80}
Chris Lattner7546c212001-11-10 07:28:25 +000081
Chris Lattner12739d92001-11-26 16:59:10 +000082static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
83 unsigned Field) {
84 for (unsigned i = 0; ; ++i)
85 if (Vec[i].first == Field) return i;
86}
87
88static inline void GetTransformation(const StructType *ST,
89 vector<int> &Transform,
Chris Lattnerf4de63f2002-01-21 07:31:50 +000090 enum SimpleStructMutation::Transform XForm) {
Chris Lattner12739d92001-11-26 16:59:10 +000091 unsigned NumElements = ST->getElementTypes().size();
92 Transform.reserve(NumElements);
93
94 switch (XForm) {
Chris Lattnerf4de63f2002-01-21 07:31:50 +000095 case SimpleStructMutation::SwapElements:
Chris Lattner12739d92001-11-26 16:59:10 +000096 // The transformation to do is: just simply swap the elements
97 for (unsigned i = 0; i < NumElements; ++i)
98 Transform.push_back(NumElements-i-1);
99 break;
100
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000101 case SimpleStructMutation::SortElements: {
Chris Lattner12739d92001-11-26 16:59:10 +0000102 vector<pair<unsigned, unsigned> > ElList;
103
104 // Build mapping from index to size
105 for (unsigned i = 0; i < NumElements; ++i)
Chris Lattner697954c2002-01-20 22:54:45 +0000106 ElList.push_back(
107 std::make_pair(i, TD.getTypeSize(ST->getElementTypes()[i])));
Chris Lattner12739d92001-11-26 16:59:10 +0000108
109 sort(ElList.begin(), ElList.end(), ptr_fun(FirstLess));
110
111 for (unsigned i = 0; i < NumElements; ++i)
112 Transform.push_back(getIndex(ElList, i));
113
114 break;
115 }
116 }
117}
Chris Lattner7546c212001-11-10 07:28:25 +0000118
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000119
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000120SimpleStructMutation::TransformsType
121 SimpleStructMutation::getTransforms(Module *M, enum Transform XForm) {
Chris Lattner7546c212001-11-10 07:28:25 +0000122 // We need to know which types to modify, and which types we CAN'T modify
Chris Lattner793c6b82002-01-31 00:45:11 +0000123 // TODO: Do symbol tables as well
Chris Lattner7546c212001-11-10 07:28:25 +0000124
125 // Get the results out of the analyzers...
Chris Lattner793c6b82002-01-31 00:45:11 +0000126 FindUsedTypes &FUT = getAnalysis<FindUsedTypes>();
127 const set<const Type *> &UsedTypes = FUT.getTypes();
128
129 FindUnsafePointerTypes &FUPT = getAnalysis<FindUnsafePointerTypes>();
130 const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
131
Chris Lattner7546c212001-11-10 07:28:25 +0000132
133
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000134 // Combine the two sets, weeding out non structure types. Closures in C++
135 // sure would be nice.
Chris Lattner7546c212001-11-10 07:28:25 +0000136 set<const StructType*> TypesToModify;
137 for (set<const Type *>::const_iterator I = UsedTypes.begin(),
138 E = UsedTypes.end(); I != E; ++I)
139 if (const StructType *ST = dyn_cast<StructType>(*I))
140 TypesToModify.insert(ST);
141
142
143 // Go through the Unsafe types and remove all types from TypesToModify that we
144 // are not allowed to modify, because that would be unsafe.
145 //
146 set<const Type*> ProcessedTypes;
147 for (set<PointerType*>::const_iterator I = UnsafePTys.begin(),
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000148 E = UnsafePTys.end(); I != E; ++I) {
Chris Lattner697954c2002-01-20 22:54:45 +0000149 //cerr << "Pruning type: " << *I << "\n";
Chris Lattner7546c212001-11-10 07:28:25 +0000150 PruneTypes(*I, TypesToModify, ProcessedTypes);
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000151 }
Chris Lattner7546c212001-11-10 07:28:25 +0000152
153
154 // Build up a set of structure types that we are going to modify, and
155 // information describing how to modify them.
Chris Lattner697954c2002-01-20 22:54:45 +0000156 std::map<const StructType*, vector<int> > Transforms;
Chris Lattner7546c212001-11-10 07:28:25 +0000157
158 for (set<const StructType*>::iterator I = TypesToModify.begin(),
159 E = TypesToModify.end(); I != E; ++I) {
160 const StructType *ST = *I;
Chris Lattner7546c212001-11-10 07:28:25 +0000161
162 vector<int> &Transform = Transforms[ST]; // Fill in the map directly
Chris Lattner12739d92001-11-26 16:59:10 +0000163 GetTransformation(ST, Transform, XForm);
Chris Lattner7546c212001-11-10 07:28:25 +0000164 }
165
Chris Lattner12739d92001-11-26 16:59:10 +0000166 return Transforms;
Chris Lattner7546c212001-11-10 07:28:25 +0000167}
168
Chris Lattner793c6b82002-01-31 00:45:11 +0000169
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000170Pass *createSwapElementsPass() {
171 return new SimpleStructMutation(SimpleStructMutation::SwapElements);
Chris Lattner793c6b82002-01-31 00:45:11 +0000172}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000173Pass *createSortElementsPass() {
174 return new SimpleStructMutation(SimpleStructMutation::SortElements);
175}
176