blob: 168f839736603e44476b843466476f18a673dccd [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 Lattnerd5b48ca2001-11-14 11:02:49 +000020#include "llvm/Assembly/Writer.h"
21
Chris Lattnerbd0ef772002-02-26 21:46:54 +000022namespace {
23 class SimpleStructMutation : public MutateStructTypes {
24 public:
25 enum Transform { SwapElements, SortElements } CurrentXForm;
26
27 SimpleStructMutation(enum Transform XForm) : CurrentXForm(XForm) {}
28
29 virtual bool run(Module *M) {
30 setTransforms(getTransforms(M, CurrentXForm));
31 bool Changed = MutateStructTypes::run(M);
32 clearTransforms();
33 return Changed;
34 }
35
36 // getAnalysisUsageInfo - This function needs the results of the
37 // FindUsedTypes and FindUnsafePointerTypes analysis passes...
38 //
39 virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Required,
40 Pass::AnalysisSet &Destroyed,
41 Pass::AnalysisSet &Provided) {
42 Required.push_back(FindUsedTypes::ID);
43 Required.push_back(FindUnsafePointerTypes::ID);
44 MutateStructTypes::getAnalysisUsageInfo(Required, Destroyed, Provided);
45 }
46
47 private:
48 TransformsType getTransforms(Module *M, enum Transform);
49 };
50} // end anonymous namespace
51
52
Chris Lattner697954c2002-01-20 22:54:45 +000053
Chris Lattner7546c212001-11-10 07:28:25 +000054// PruneTypes - Given a type Ty, make sure that neither it, or one of its
55// subtypes, occur in TypesToModify.
56//
57static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
58 set<const Type*> &ProcessedTypes) {
59 if (ProcessedTypes.count(Ty)) return; // Already been checked
60 ProcessedTypes.insert(Ty);
61
62 // If the element is in TypesToModify, remove it now...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000063 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattner7546c212001-11-10 07:28:25 +000064 TypesToModify.erase(ST); // This doesn't fail if the element isn't present
Chris Lattner697954c2002-01-20 22:54:45 +000065 std::cerr << "Unable to swap type: " << ST << "\n";
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000066 }
Chris Lattner7546c212001-11-10 07:28:25 +000067
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000068 // Remove all types that this type contains as well... do not remove types
69 // that are referenced only through pointers, because we depend on the size of
70 // the pointer, not on what the structure points to.
Chris Lattner7546c212001-11-10 07:28:25 +000071 //
72 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000073 I != E; ++I) {
74 if (!isa<PointerType>(*I))
75 PruneTypes(*I, TypesToModify, ProcessedTypes);
76 }
Chris Lattner7546c212001-11-10 07:28:25 +000077}
78
Chris Lattner12739d92001-11-26 16:59:10 +000079static bool FirstLess(const pair<unsigned, unsigned> &LHS,
80 const pair<unsigned, unsigned> &RHS) {
81 return LHS.second < RHS.second;
82}
Chris Lattner7546c212001-11-10 07:28:25 +000083
Chris Lattner12739d92001-11-26 16:59:10 +000084static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
85 unsigned Field) {
86 for (unsigned i = 0; ; ++i)
87 if (Vec[i].first == Field) return i;
88}
89
90static inline void GetTransformation(const StructType *ST,
91 vector<int> &Transform,
Chris Lattnerf4de63f2002-01-21 07:31:50 +000092 enum SimpleStructMutation::Transform XForm) {
Chris Lattner12739d92001-11-26 16:59:10 +000093 unsigned NumElements = ST->getElementTypes().size();
94 Transform.reserve(NumElements);
95
96 switch (XForm) {
Chris Lattnerf4de63f2002-01-21 07:31:50 +000097 case SimpleStructMutation::SwapElements:
Chris Lattner12739d92001-11-26 16:59:10 +000098 // The transformation to do is: just simply swap the elements
99 for (unsigned i = 0; i < NumElements; ++i)
100 Transform.push_back(NumElements-i-1);
101 break;
102
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000103 case SimpleStructMutation::SortElements: {
Chris Lattner12739d92001-11-26 16:59:10 +0000104 vector<pair<unsigned, unsigned> > ElList;
105
106 // Build mapping from index to size
107 for (unsigned i = 0; i < NumElements; ++i)
Chris Lattner697954c2002-01-20 22:54:45 +0000108 ElList.push_back(
109 std::make_pair(i, TD.getTypeSize(ST->getElementTypes()[i])));
Chris Lattner12739d92001-11-26 16:59:10 +0000110
111 sort(ElList.begin(), ElList.end(), ptr_fun(FirstLess));
112
113 for (unsigned i = 0; i < NumElements; ++i)
114 Transform.push_back(getIndex(ElList, i));
115
116 break;
117 }
118 }
119}
Chris Lattner7546c212001-11-10 07:28:25 +0000120
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000121
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000122SimpleStructMutation::TransformsType
123 SimpleStructMutation::getTransforms(Module *M, enum Transform XForm) {
Chris Lattner7546c212001-11-10 07:28:25 +0000124 // We need to know which types to modify, and which types we CAN'T modify
Chris Lattner793c6b82002-01-31 00:45:11 +0000125 // TODO: Do symbol tables as well
Chris Lattner7546c212001-11-10 07:28:25 +0000126
127 // Get the results out of the analyzers...
Chris Lattner793c6b82002-01-31 00:45:11 +0000128 FindUsedTypes &FUT = getAnalysis<FindUsedTypes>();
129 const set<const Type *> &UsedTypes = FUT.getTypes();
130
131 FindUnsafePointerTypes &FUPT = getAnalysis<FindUnsafePointerTypes>();
132 const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
133
Chris Lattner7546c212001-11-10 07:28:25 +0000134
135
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000136 // Combine the two sets, weeding out non structure types. Closures in C++
137 // sure would be nice.
Chris Lattner7546c212001-11-10 07:28:25 +0000138 set<const StructType*> TypesToModify;
139 for (set<const Type *>::const_iterator I = UsedTypes.begin(),
140 E = UsedTypes.end(); I != E; ++I)
141 if (const StructType *ST = dyn_cast<StructType>(*I))
142 TypesToModify.insert(ST);
143
144
145 // Go through the Unsafe types and remove all types from TypesToModify that we
146 // are not allowed to modify, because that would be unsafe.
147 //
148 set<const Type*> ProcessedTypes;
149 for (set<PointerType*>::const_iterator I = UnsafePTys.begin(),
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000150 E = UnsafePTys.end(); I != E; ++I) {
Chris Lattner697954c2002-01-20 22:54:45 +0000151 //cerr << "Pruning type: " << *I << "\n";
Chris Lattner7546c212001-11-10 07:28:25 +0000152 PruneTypes(*I, TypesToModify, ProcessedTypes);
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000153 }
Chris Lattner7546c212001-11-10 07:28:25 +0000154
155
156 // Build up a set of structure types that we are going to modify, and
157 // information describing how to modify them.
Chris Lattner697954c2002-01-20 22:54:45 +0000158 std::map<const StructType*, vector<int> > Transforms;
Chris Lattner7546c212001-11-10 07:28:25 +0000159
160 for (set<const StructType*>::iterator I = TypesToModify.begin(),
161 E = TypesToModify.end(); I != E; ++I) {
162 const StructType *ST = *I;
Chris Lattner7546c212001-11-10 07:28:25 +0000163
164 vector<int> &Transform = Transforms[ST]; // Fill in the map directly
Chris Lattner12739d92001-11-26 16:59:10 +0000165 GetTransformation(ST, Transform, XForm);
Chris Lattner7546c212001-11-10 07:28:25 +0000166 }
167
Chris Lattner12739d92001-11-26 16:59:10 +0000168 return Transforms;
Chris Lattner7546c212001-11-10 07:28:25 +0000169}
170
Chris Lattner793c6b82002-01-31 00:45:11 +0000171
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000172Pass *createSwapElementsPass() {
173 return new SimpleStructMutation(SimpleStructMutation::SwapElements);
Chris Lattner793c6b82002-01-31 00:45:11 +0000174}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000175Pass *createSortElementsPass() {
176 return new SimpleStructMutation(SimpleStructMutation::SortElements);
177}
178