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