Implement a more powerful, simpler, pass system. This pass system can figure
[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 <map>
19
20 class StructType;
21 class CompositeType;
22 class GlobalValue;
23
24 class MutateStructTypes : public Pass {
25   // TransformType - Representation of the destination type for a particular
26   // incoming structure.  The first member is the destination type that we are
27   // mapping to, and the second member is the destination slot # to put each
28   // incoming slot [or negative if the specified incoming slot should be
29   // removed].
30   //
31   typedef std::pair<const StructType*, std::vector<int> > TransformType;
32
33   // Transforms to do for each structure type...
34   std::map<const StructType*, TransformType> Transforms;
35
36   // Mapping of old type to new types...
37   std::map<const Type *, PATypeHolder<Type> > TypeMap;
38
39   // Mapping from global value of old type, to a global value of the new type...
40   std::map<const GlobalValue*, GlobalValue*> GlobalMap;
41
42   // Mapping from intra method value to intra method value
43   std::map<const Value*, Value*> LocalValueMap;
44
45 public:
46   // Ctor - Take a map that specifies what transformation to do for each field
47   // of the specified structure types.  There is one element of the vector for
48   // each field of the structure.  The value specified indicates which slot of
49   // the destination structure the field should end up in.  A negative value 
50   // indicates that the field should be deleted entirely.
51   //
52   typedef std::map<const StructType*, std::vector<int> > TransformsType;
53
54   MutateStructTypes(const TransformsType &Transforms) {
55     setTransforms(Transforms);
56   }
57
58   // run - do the transformation
59   virtual bool run(Module *M);
60
61 protected:
62
63   // Alternatively, it is valid to subclass this class and provide transforms
64   // this way.  See SimpleStructMutation for an example.
65   //
66   MutateStructTypes() {}
67   void setTransforms(const TransformsType &Transforms);
68   void clearTransforms();
69
70 private:
71
72   // processGlobals - This loops over global constants defined in the
73   // module, converting them to their new type.  Also this creates placeholder
74   // methods for methods than need to be copied because they have a new
75   // signature type.
76   //
77   void processGlobals(Module *M);
78
79   // transformMethod - This transforms the instructions of the method to use the
80   // new types.
81   //
82   void transformMethod(Method *M);
83
84   // removeDeadGlobals - This removes the old versions of methods that are no
85   // longer needed.
86   //
87   void removeDeadGlobals(Module *M);
88
89 private:
90   // ConvertType - Convert from the old type system to the new one...
91   const Type *ConvertType(const Type *Ty);
92
93   // ConvertValue - Convert from the old value in the old type system to the new
94   // type system.
95   //
96   Value *ConvertValue(const Value *V);
97
98   // AdjustIndices - Convert the indexes specifed by Idx to the new changed form
99   // using the specified OldTy as the base type being indexed into.
100   //
101   void AdjustIndices(const CompositeType *OldTy, std::vector<Value*> &Idx,
102                      unsigned idx = 0);
103 };
104
105 #endif