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