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