Added LLVM copyright header.
[oota-llvm.git] / lib / Transforms / TransformInternals.h
1 //===-- TransformInternals.h - Shared functions for Transforms --*- 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 header file declares shared functions used by the different components
11 //  of the Transforms library.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef TRANSFORM_INTERNALS_H
16 #define TRANSFORM_INTERNALS_H
17
18 #include "llvm/BasicBlock.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Constants.h"
22 #include <map>
23 #include <set>
24
25 static inline int64_t getConstantValue(const ConstantInt *CPI) {
26   return (int64_t)cast<ConstantInt>(CPI)->getRawValue();
27 }
28
29
30 // getPointedToComposite - If the argument is a pointer type, and the pointed to
31 // value is a composite type, return the composite type, else return null.
32 //
33 static inline const CompositeType *getPointedToComposite(const Type *Ty) {
34   const PointerType *PT = dyn_cast<PointerType>(Ty);
35   return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0;
36 }
37
38 // ConvertibleToGEP - This function returns true if the specified value V is
39 // a valid index into a pointer of type Ty.  If it is valid, Idx is filled in
40 // with the values that would be appropriate to make this a getelementptr
41 // instruction.  The type returned is the root type that the GEP would point
42 // to if it were synthesized with this operands.
43 //
44 // If BI is nonnull, cast instructions are inserted as appropriate for the
45 // arguments of the getelementptr.
46 //
47 const Type *ConvertibleToGEP(const Type *Ty, Value *V,
48                              std::vector<Value*> &Indices,
49                              const TargetData &TD,
50                              BasicBlock::iterator *BI = 0);
51
52
53 //===----------------------------------------------------------------------===//
54 //  ValueHandle Class - Smart pointer that occupies a slot on the users USE list
55 //  that prevents it from being destroyed.  This "looks" like an Instruction
56 //  with Opcode UserOp1.
57 // 
58 class ValueMapCache;
59 class ValueHandle : public Instruction {
60   ValueMapCache &Cache;
61 public:
62   ValueHandle(ValueMapCache &VMC, Value *V);
63   ValueHandle(const ValueHandle &);
64   ~ValueHandle();
65
66   virtual Instruction *clone() const { abort(); return 0; }
67
68   virtual const char *getOpcodeName() const {
69     return "ValueHandle";
70   }
71
72   inline bool operator<(const ValueHandle &VH) const {
73     return getOperand(0) < VH.getOperand(0);
74   }
75
76   // Methods for support type inquiry through isa, cast, and dyn_cast:
77   static inline bool classof(const ValueHandle *) { return true; }
78   static inline bool classof(const Instruction *I) {
79     return (I->getOpcode() == Instruction::UserOp1);
80   }
81   static inline bool classof(const Value *V) {
82     return isa<Instruction>(V) && classof(cast<Instruction>(V));
83   }
84 };
85
86
87 // ------------- Expression Conversion ---------------------
88
89 typedef std::map<const Value*, const Type*> ValueTypeCache;
90
91 struct ValueMapCache {
92   // Operands mapped - Contains an entry if the first value (the user) has had
93   // the second value (the operand) mapped already.
94   //
95   std::set<const User*> OperandsMapped;
96
97   // Expression Map - Contains an entry from the old value to the new value of
98   // an expression that has been converted over.
99   //
100   std::map<const Value *, Value *> ExprMap;
101   typedef std::map<const Value *, Value *> ExprMapTy;
102
103   // Cast Map - Cast instructions can have their source and destination values
104   // changed independently for each part.  Because of this, our old naive
105   // implementation would create a TWO new cast instructions, which would cause
106   // all kinds of problems.  Here we keep track of the newly allocated casts, so
107   // that we only create one for a particular instruction.
108   //
109   std::set<ValueHandle> NewCasts;
110 };
111
112
113 bool ExpressionConvertibleToType(Value *V, const Type *Ty, ValueTypeCache &Map,
114                                  const TargetData &TD);
115 Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC,
116                                const TargetData &TD);
117
118 // ValueConvertibleToType - Return true if it is possible
119 bool ValueConvertibleToType(Value *V, const Type *Ty,
120                             ValueTypeCache &ConvertedTypes,
121                             const TargetData &TD);
122
123 void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC,
124                            const TargetData &TD);
125
126
127 // getStructOffsetType - Return a vector of offsets that are to be used to index
128 // into the specified struct type to get as close as possible to index as we
129 // can.  Note that it is possible that we cannot get exactly to Offset, in which
130 // case we update offset to be the offset we actually obtained.  The resultant
131 // leaf type is returned.
132 //
133 // If StopEarly is set to true (the default), the first object with the
134 // specified type is returned, even if it is a struct type itself.  In this
135 // case, this routine will not drill down to the leaf type.  Set StopEarly to
136 // false if you want a leaf
137 //
138 const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
139                                 std::vector<Value*> &Offsets,
140                                 const TargetData &TD, bool StopEarly = true);
141
142 #endif