1 //===-- llvm/Transforms/MutateStructTypes.h - Rearrange structs -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
16 // This is an interprocedural pass that requires the entire program to do a
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_TRANSFORMS_MUTATESTRUCTTYPES_H
22 #define LLVM_TRANSFORMS_MUTATESTRUCTTYPES_H
24 #include "llvm/Pass.h"
25 #include "llvm/AbstractTypeUser.h"
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
42 typedef std::pair<const StructType*, std::vector<int> > TransformType;
44 // Transforms to do for each structure type...
45 std::map<const StructType*, TransformType> Transforms;
47 // Mapping of old type to new types...
48 std::map<const Type *, PATypeHolder> TypeMap;
50 // Mapping from global value of old type, to a global value of the new type...
51 std::map<const GlobalValue*, GlobalValue*> GlobalMap;
53 // Mapping from intra function value to intra function value
54 std::map<const Value*, Value*> LocalValueMap;
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.
63 typedef std::map<const StructType*, std::vector<int> > TransformsType;
65 MutateStructTypes(const TransformsType &Transforms) {
66 setTransforms(Transforms);
69 // run - do the transformation
70 virtual bool run(Module &M);
74 // Alternatively, it is valid to subclass this class and provide transforms
75 // this way. See SimpleStructMutation for an example.
77 MutateStructTypes() {}
78 void setTransforms(const TransformsType &Transforms);
79 void clearTransforms();
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
88 void processGlobals(Module &M);
90 // transformFunction - This transforms the instructions of the function to use
93 void transformFunction(Function *F);
95 // removeDeadGlobals - This removes the old versions of functions that are no
98 void removeDeadGlobals(Module &M);
101 // ConvertType - Convert from the old type system to the new one...
102 const Type *ConvertType(const Type *Ty);
104 // ConvertValue - Convert from the old value in the old type system to the new
107 Value *ConvertValue(const Value *V);
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.
112 void AdjustIndices(const CompositeType *OldTy, std::vector<Value*> &Idx,
116 } // End llvm namespace