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"
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
40 typedef std::pair<const StructType*, std::vector<int> > TransformType;
42 // Transforms to do for each structure type...
43 std::map<const StructType*, TransformType> Transforms;
45 // Mapping of old type to new types...
46 std::map<const Type *, PATypeHolder> TypeMap;
48 // Mapping from global value of old type, to a global value of the new type...
49 std::map<const GlobalValue*, GlobalValue*> GlobalMap;
51 // Mapping from intra function value to intra function value
52 std::map<const Value*, Value*> LocalValueMap;
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.
61 typedef std::map<const StructType*, std::vector<int> > TransformsType;
63 MutateStructTypes(const TransformsType &Transforms) {
64 setTransforms(Transforms);
67 // run - do the transformation
68 virtual bool run(Module &M);
72 // Alternatively, it is valid to subclass this class and provide transforms
73 // this way. See SimpleStructMutation for an example.
75 MutateStructTypes() {}
76 void setTransforms(const TransformsType &Transforms);
77 void clearTransforms();
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
86 void processGlobals(Module &M);
88 // transformFunction - This transforms the instructions of the function to use
91 void transformFunction(Function *F);
93 // removeDeadGlobals - This removes the old versions of functions that are no
96 void removeDeadGlobals(Module &M);
99 // ConvertType - Convert from the old type system to the new one...
100 const Type *ConvertType(const Type *Ty);
102 // ConvertValue - Convert from the old value in the old type system to the new
105 Value *ConvertValue(const Value *V);
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.
110 void AdjustIndices(const CompositeType *OldTy, std::vector<Value*> &Idx,