blob: 8583e3e850d7a2ac827abb9bd2e75641759293b9 [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 Lattner697954c2002-01-20 22:54:45 +000022
Chris Lattner7546c212001-11-10 07:28:25 +000023// PruneTypes - Given a type Ty, make sure that neither it, or one of its
24// subtypes, occur in TypesToModify.
25//
26static void PruneTypes(const Type *Ty, set<const StructType*> &TypesToModify,
27 set<const Type*> &ProcessedTypes) {
28 if (ProcessedTypes.count(Ty)) return; // Already been checked
29 ProcessedTypes.insert(Ty);
30
31 // If the element is in TypesToModify, remove it now...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000032 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattner7546c212001-11-10 07:28:25 +000033 TypesToModify.erase(ST); // This doesn't fail if the element isn't present
Chris Lattner697954c2002-01-20 22:54:45 +000034 std::cerr << "Unable to swap type: " << ST << "\n";
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000035 }
Chris Lattner7546c212001-11-10 07:28:25 +000036
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000037 // Remove all types that this type contains as well... do not remove types
38 // that are referenced only through pointers, because we depend on the size of
39 // the pointer, not on what the structure points to.
Chris Lattner7546c212001-11-10 07:28:25 +000040 //
41 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000042 I != E; ++I) {
43 if (!isa<PointerType>(*I))
44 PruneTypes(*I, TypesToModify, ProcessedTypes);
45 }
Chris Lattner7546c212001-11-10 07:28:25 +000046}
47
Chris Lattner12739d92001-11-26 16:59:10 +000048static bool FirstLess(const pair<unsigned, unsigned> &LHS,
49 const pair<unsigned, unsigned> &RHS) {
50 return LHS.second < RHS.second;
51}
Chris Lattner7546c212001-11-10 07:28:25 +000052
Chris Lattner12739d92001-11-26 16:59:10 +000053static unsigned getIndex(const vector<pair<unsigned, unsigned> > &Vec,
54 unsigned Field) {
55 for (unsigned i = 0; ; ++i)
56 if (Vec[i].first == Field) return i;
57}
58
59static inline void GetTransformation(const StructType *ST,
60 vector<int> &Transform,
Chris Lattnerf4de63f2002-01-21 07:31:50 +000061 enum SimpleStructMutation::Transform XForm) {
Chris Lattner12739d92001-11-26 16:59:10 +000062 unsigned NumElements = ST->getElementTypes().size();
63 Transform.reserve(NumElements);
64
65 switch (XForm) {
Chris Lattnerf4de63f2002-01-21 07:31:50 +000066 case SimpleStructMutation::SwapElements:
Chris Lattner12739d92001-11-26 16:59:10 +000067 // The transformation to do is: just simply swap the elements
68 for (unsigned i = 0; i < NumElements; ++i)
69 Transform.push_back(NumElements-i-1);
70 break;
71
Chris Lattnerf4de63f2002-01-21 07:31:50 +000072 case SimpleStructMutation::SortElements: {
Chris Lattner12739d92001-11-26 16:59:10 +000073 vector<pair<unsigned, unsigned> > ElList;
74
75 // Build mapping from index to size
76 for (unsigned i = 0; i < NumElements; ++i)
Chris Lattner697954c2002-01-20 22:54:45 +000077 ElList.push_back(
78 std::make_pair(i, TD.getTypeSize(ST->getElementTypes()[i])));
Chris Lattner12739d92001-11-26 16:59:10 +000079
80 sort(ElList.begin(), ElList.end(), ptr_fun(FirstLess));
81
82 for (unsigned i = 0; i < NumElements; ++i)
83 Transform.push_back(getIndex(ElList, i));
84
85 break;
86 }
87 }
88}
Chris Lattner7546c212001-11-10 07:28:25 +000089
Chris Lattnerf4de63f2002-01-21 07:31:50 +000090SimpleStructMutation::TransformsType
91 SimpleStructMutation::getTransforms(Module *M, enum Transform XForm) {
Chris Lattner7546c212001-11-10 07:28:25 +000092 // We need to know which types to modify, and which types we CAN'T modify
Chris Lattner793c6b82002-01-31 00:45:11 +000093 // TODO: Do symbol tables as well
Chris Lattner7546c212001-11-10 07:28:25 +000094
95 // Get the results out of the analyzers...
Chris Lattner793c6b82002-01-31 00:45:11 +000096 FindUsedTypes &FUT = getAnalysis<FindUsedTypes>();
97 const set<const Type *> &UsedTypes = FUT.getTypes();
98
99 FindUnsafePointerTypes &FUPT = getAnalysis<FindUnsafePointerTypes>();
100 const set<PointerType*> &UnsafePTys = FUPT.getUnsafeTypes();
101
Chris Lattner7546c212001-11-10 07:28:25 +0000102
103
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000104 // Combine the two sets, weeding out non structure types. Closures in C++
105 // sure would be nice.
Chris Lattner7546c212001-11-10 07:28:25 +0000106 set<const StructType*> TypesToModify;
107 for (set<const Type *>::const_iterator I = UsedTypes.begin(),
108 E = UsedTypes.end(); I != E; ++I)
109 if (const StructType *ST = dyn_cast<StructType>(*I))
110 TypesToModify.insert(ST);
111
112
113 // Go through the Unsafe types and remove all types from TypesToModify that we
114 // are not allowed to modify, because that would be unsafe.
115 //
116 set<const Type*> ProcessedTypes;
117 for (set<PointerType*>::const_iterator I = UnsafePTys.begin(),
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000118 E = UnsafePTys.end(); I != E; ++I) {
Chris Lattner697954c2002-01-20 22:54:45 +0000119 //cerr << "Pruning type: " << *I << "\n";
Chris Lattner7546c212001-11-10 07:28:25 +0000120 PruneTypes(*I, TypesToModify, ProcessedTypes);
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000121 }
Chris Lattner7546c212001-11-10 07:28:25 +0000122
123
124 // Build up a set of structure types that we are going to modify, and
125 // information describing how to modify them.
Chris Lattner697954c2002-01-20 22:54:45 +0000126 std::map<const StructType*, vector<int> > Transforms;
Chris Lattner7546c212001-11-10 07:28:25 +0000127
128 for (set<const StructType*>::iterator I = TypesToModify.begin(),
129 E = TypesToModify.end(); I != E; ++I) {
130 const StructType *ST = *I;
Chris Lattner7546c212001-11-10 07:28:25 +0000131
132 vector<int> &Transform = Transforms[ST]; // Fill in the map directly
Chris Lattner12739d92001-11-26 16:59:10 +0000133 GetTransformation(ST, Transform, XForm);
Chris Lattner7546c212001-11-10 07:28:25 +0000134 }
135
Chris Lattner12739d92001-11-26 16:59:10 +0000136 return Transforms;
Chris Lattner7546c212001-11-10 07:28:25 +0000137}
138
Chris Lattner793c6b82002-01-31 00:45:11 +0000139
140// getAnalysisUsageInfo - This function needs the results of the
141// FindUsedTypes and FindUnsafePointerTypes analysis passes...
142//
143void SimpleStructMutation::getAnalysisUsageInfo(Pass::AnalysisSet &Required,
144 Pass::AnalysisSet &Destroyed,
145 Pass::AnalysisSet &Provided){
146 Required.push_back(FindUsedTypes::ID);
147 Required.push_back(FindUnsafePointerTypes::ID);
148 MutateStructTypes::getAnalysisUsageInfo(Required, Destroyed, Provided);
149}