Include new prototype
[oota-llvm.git] / include / llvm / Transforms / MutateStructTypes.h
1 //===-- llvm/Transforms/MutateStructTypes.h - Rearrange structs -*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass is used to change structure accesses and type definitions in some
11 // way.  It can be used to arbitrarily permute structure fields, safely, without
12 // breaking code.  A transformation may only be done on a type if that type has
13 // been found to be "safe" by the 'FindUnsafePointerTypes' pass.  This pass will
14 // assert and die if you try to do an illegal transformation.
15 //
16 // This is an interprocedural pass that requires the entire program to do a
17 // transformation.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_TRANSFORMS_MUTATESTRUCTTYPES_H
22 #define LLVM_TRANSFORMS_MUTATESTRUCTTYPES_H
23
24 #include "llvm/Pass.h"
25 #include "llvm/AbstractTypeUser.h"
26
27 class Value;
28 class Type;
29 class StructType;
30 class CompositeType;
31 class GlobalValue;
32
33 class MutateStructTypes : public Pass {
34   // TransformType - Representation of the destination type for a particular
35   // incoming structure.  The first member is the destination type that we are
36   // mapping to, and the second member is the destination slot # to put each
37   // incoming slot [or negative if the specified incoming slot should be
38   // removed].
39   //
40   typedef std::pair<const StructType*, std::vector<int> > TransformType;
41
42   // Transforms to do for each structure type...
43   std::map<const StructType*, TransformType> Transforms;
44
45   // Mapping of old type to new types...
46   std::map<const Type *, PATypeHolder> TypeMap;
47
48   // Mapping from global value of old type, to a global value of the new type...
49   std::map<const GlobalValue*, GlobalValue*> GlobalMap;
50
51   // Mapping from intra function value to intra function value
52   std::map<const Value*, Value*> LocalValueMap;
53
54 public:
55   // Ctor - Take a map that specifies what transformation to do for each field
56   // of the specified structure types.  There is one element of the vector for
57   // each field of the structure.  The value specified indicates which slot of
58   // the destination structure the field should end up in.  A negative value 
59   // indicates that the field should be deleted entirely.
60   //
61   typedef std::map<const StructType*, std::vector<int> > TransformsType;
62
63   MutateStructTypes(const TransformsType &Transforms) {
64     setTransforms(Transforms);
65   }
66
67   // run - do the transformation
68   virtual bool run(Module &M);
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   // functions for functions than need to be copied because they have a new
84   // signature type.
85   //
86   void processGlobals(Module &M);
87
88   // transformFunction - This transforms the instructions of the function to use
89   // the new types.
90   //
91   void transformFunction(Function *F);
92
93   // removeDeadGlobals - This removes the old versions of functions 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