Factor code out of TransformInternals into the Transform Utils library
[oota-llvm.git] / lib / Transforms / TransformInternals.h
1 //===-- TransformInternals.h - Shared functions for Transforms ---*- C++ -*--=//
2 //
3 //  This header file declares shared functions used by the different components
4 //  of the Transforms library.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef TRANSFORM_INTERNALS_H
9 #define TRANSFORM_INTERNALS_H
10
11 #include "llvm/BasicBlock.h"
12 #include "llvm/Instruction.h"
13 #include "llvm/Target/TargetData.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/Constants.h"
16 #include <map>
17 #include <set>
18
19 // TargetData Hack: Eventually we will have annotations given to us by the
20 // backend so that we know stuff about type size and alignments.  For now
21 // though, just use this, because it happens to match the model that GCC uses.
22 //
23 // FIXME: This should use annotations
24 //
25 extern const TargetData TD;
26
27 static inline int getConstantValue(const ConstantInt *CPI) {
28   if (const ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPI))
29     return (int)CSI->getValue();
30   return (int)cast<ConstantUInt>(CPI)->getValue();
31 }
32
33
34 // getPointedToComposite - If the argument is a pointer type, and the pointed to
35 // value is a composite type, return the composite type, else return null.
36 //
37 static inline const CompositeType *getPointedToComposite(const Type *Ty) {
38   const PointerType *PT = dyn_cast<PointerType>(Ty);
39   return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0;
40 }
41
42 // ConvertableToGEP - This function returns true if the specified value V is
43 // a valid index into a pointer of type Ty.  If it is valid, Idx is filled in
44 // with the values that would be appropriate to make this a getelementptr
45 // instruction.  The type returned is the root type that the GEP would point
46 // to if it were synthesized with this operands.
47 //
48 // If BI is nonnull, cast instructions are inserted as appropriate for the
49 // arguments of the getelementptr.
50 //
51 const Type *ConvertableToGEP(const Type *Ty, Value *V,
52                              std::vector<Value*> &Indices,
53                              BasicBlock::iterator *BI = 0);
54
55
56 // ------------- Expression Conversion ---------------------
57
58 typedef std::map<const Value*, const Type*>         ValueTypeCache;
59
60 struct ValueMapCache {
61   // Operands mapped - Contains an entry if the first value (the user) has had
62   // the second value (the operand) mapped already.
63   //
64   std::set<const User*> OperandsMapped;
65
66   // Expression Map - Contains an entry from the old value to the new value of
67   // an expression that has been converted over.
68   //
69   std::map<const Value *, Value *> ExprMap;
70   typedef std::map<const Value *, Value *> ExprMapTy;
71 };
72
73
74 bool ExpressionConvertableToType(Value *V, const Type *Ty, ValueTypeCache &Map);
75 Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC);
76
77 // ValueConvertableToType - Return true if it is possible
78 bool ValueConvertableToType(Value *V, const Type *Ty,
79                             ValueTypeCache &ConvertedTypes);
80
81 void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC);
82
83
84 //===----------------------------------------------------------------------===//
85 //  ValueHandle Class - Smart pointer that occupies a slot on the users USE list
86 //  that prevents it from being destroyed.  This "looks" like an Instruction
87 //  with Opcode UserOp1.
88 // 
89 class ValueHandle : public Instruction {
90   ValueHandle(const ValueHandle &); // DO NOT IMPLEMENT
91   ValueMapCache &Cache;
92 public:
93   ValueHandle(ValueMapCache &VMC, Value *V);
94   ~ValueHandle();
95
96   virtual Instruction *clone() const { abort(); return 0; }
97
98   virtual const char *getOpcodeName() const {
99     return "ValueHandle";
100   }
101
102   // Methods for support type inquiry through isa, cast, and dyn_cast:
103   static inline bool classof(const ValueHandle *) { return true; }
104   static inline bool classof(const Instruction *I) {
105     return (I->getOpcode() == Instruction::UserOp1);
106   }
107   static inline bool classof(const Value *V) {
108     return isa<Instruction>(V) && classof(cast<Instruction>(V));
109   }
110 };
111
112 // getStructOffsetType - Return a vector of offsets that are to be used to index
113 // into the specified struct type to get as close as possible to index as we
114 // can.  Note that it is possible that we cannot get exactly to Offset, in which
115 // case we update offset to be the offset we actually obtained.  The resultant
116 // leaf type is returned.
117 //
118 // If StopEarly is set to true (the default), the first object with the
119 // specified type is returned, even if it is a struct type itself.  In this
120 // case, this routine will not drill down to the leaf type.  Set StopEarly to
121 // false if you want a leaf
122 //
123 const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
124                                 std::vector<Value*> &Offsets,
125                                 bool StopEarly = true);
126
127 #endif