Change over to use new style pass mechanism, now passes only expose small
[oota-llvm.git] / include / llvm / Transforms / MutateStructTypes.h
1 //===- llvm/Transforms/MutateStructTypes.h - Change struct defns -*- C++ -*--=//
2 //
3 // This pass is used to change structure accesses and type definitions in some
4 // way.  It can be used to arbitrarily permute structure fields, safely, without
5 // breaking code.  A transformation may only be done on a type if that type has
6 // been found to be "safe" by the 'FindUnsafePointerTypes' pass.  This pass will
7 // assert and die if you try to do an illegal transformation.
8 //
9 // This is an interprocedural pass that requires the entire program to do a
10 // transformation.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TRANSFORMS_MUTATESTRUCTTYPES_H
15 #define LLVM_TRANSFORMS_MUTATESTRUCTTYPES_H
16
17 #include "llvm/Pass.h"
18 #include "llvm/AbstractTypeUser.h"
19
20 class Value;
21 class Type;
22 class StructType;
23 class CompositeType;
24 class GlobalValue;
25
26 class MutateStructTypes : public Pass {
27   // TransformType - Representation of the destination type for a particular
28   // incoming structure.  The first member is the destination type that we are
29   // mapping to, and the second member is the destination slot # to put each
30   // incoming slot [or negative if the specified incoming slot should be
31   // removed].
32   //
33   typedef std::pair<const StructType*, std::vector<int> > TransformType;
34
35   // Transforms to do for each structure type...
36   std::map<const StructType*, TransformType> Transforms;
37
38   // Mapping of old type to new types...
39   std::map<const Type *, PATypeHolder<Type> > TypeMap;
40
41   // Mapping from global value of old type, to a global value of the new type...
42   std::map<const GlobalValue*, GlobalValue*> GlobalMap;
43
44   // Mapping from intra method value to intra method value
45   std::map<const Value*, Value*> LocalValueMap;
46
47 public:
48   // Ctor - Take a map that specifies what transformation to do for each field
49   // of the specified structure types.  There is one element of the vector for
50   // each field of the structure.  The value specified indicates which slot of
51   // the destination structure the field should end up in.  A negative value 
52   // indicates that the field should be deleted entirely.
53   //
54   typedef std::map<const StructType*, std::vector<int> > TransformsType;
55
56   MutateStructTypes(const TransformsType &Transforms) {
57     setTransforms(Transforms);
58   }
59
60   // run - do the transformation
61   virtual bool run(Module *M);
62
63   // getAnalysisUsageInfo - This function needs the results of the
64   // FindUsedTypes and FindUnsafePointerTypes analysis passes...
65   //
66   virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Required,
67                                     Pass::AnalysisSet &Destroyed,
68                                     Pass::AnalysisSet &Provided);
69
70 protected:
71
72   // Alternatively, it is valid to subclass this class and provide transforms
73   // this way.  See SimpleStructMutation for an example.
74   //
75   MutateStructTypes() {}
76   void setTransforms(const TransformsType &Transforms);
77   void clearTransforms();
78
79 private:
80
81   // processGlobals - This loops over global constants defined in the
82   // module, converting them to their new type.  Also this creates placeholder
83   // methods for methods than need to be copied because they have a new
84   // signature type.
85   //
86   void processGlobals(Module *M);
87
88   // transformMethod - This transforms the instructions of the method to use the
89   // new types.
90   //
91   void transformMethod(Method *M);
92
93   // removeDeadGlobals - This removes the old versions of methods that are no
94   // longer needed.
95   //
96   void removeDeadGlobals(Module *M);
97
98 private:
99   // ConvertType - Convert from the old type system to the new one...
100   const Type *ConvertType(const Type *Ty);
101
102   // ConvertValue - Convert from the old value in the old type system to the new
103   // type system.
104   //
105   Value *ConvertValue(const Value *V);
106
107   // AdjustIndices - Convert the indexes specifed by Idx to the new changed form
108   // using the specified OldTy as the base type being indexed into.
109   //
110   void AdjustIndices(const CompositeType *OldTy, std::vector<Value*> &Idx,
111                      unsigned idx = 0);
112 };
113
114 #endif