* Change ExpressionConvertableToType to more closely match map behavior of
[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 <map>
16 #include <set>
17
18 // TargetData Hack: Eventually we will have annotations given to us by the
19 // backend so that we know stuff about type size and alignments.  For now
20 // though, just use this, because it happens to match the model that GCC uses.
21 //
22 // FIXME: This should use annotations
23 //
24 extern const TargetData TD;
25
26 // losslessCastableTypes - Return true if the types are bitwise equivalent.
27 // This predicate returns true if it is possible to cast from one type to
28 // another without gaining or losing precision, or altering the bits in any way.
29 //
30 bool losslessCastableTypes(const Type *T1, const Type *T2);
31
32
33 // isFirstClassType - Return true if a value of the specified type can be held
34 // in a register.
35 //
36 static inline bool isFirstClassType(const Type *Ty) {
37   return Ty->isPrimitiveType() || Ty->isPointerType();
38 }
39
40 // getPointedToStruct - If the argument is a pointer type, and the pointed to
41 // value is a struct type, return the struct type, else return null.
42 //
43 static inline const StructType *getPointedToStruct(const Type *Ty) {
44   const PointerType *PT = dyn_cast<PointerType>(Ty);
45   return PT ? dyn_cast<StructType>(PT->getValueType()) : 0;
46 }
47
48
49 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
50 // with a value, then remove and delete the original instruction.
51 //
52 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
53                           BasicBlock::iterator &BI, Value *V);
54
55 // ReplaceInstWithInst - Replace the instruction specified by BI with the
56 // instruction specified by I.  The original instruction is deleted and BI is
57 // updated to point to the new instruction.
58 //
59 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
60                          BasicBlock::iterator &BI, Instruction *I);
61
62
63 // ------------- Expression Conversion ---------------------
64
65 typedef map<const Value*, const Type*>         ValueTypeCache;
66
67 struct ValueMapCache {
68   // Operands mapped - Contains an entry if the first value (the user) has had
69   // the second value (the operand) mapped already.
70   //
71   set<const User*> OperandsMapped;
72
73   // Expression Map - Contains an entry from the old value to the new value of
74   // an expression that has been converted over.
75   //
76   map<const Value *, Value *> ExprMap;
77   typedef map<const Value *, Value *> ExprMapTy;
78 };
79
80
81 bool ExpressionConvertableToType(Value *V, const Type *Ty, ValueTypeCache &Map);
82 Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC);
83
84 // RetValConvertableToType - Return true if it is possible
85 bool RetValConvertableToType(Value *V, const Type *Ty,
86                              ValueTypeCache &ConvertedTypes);
87
88 void ConvertUsersType(Value *V, Value *NewVal, ValueMapCache &VMC);
89
90
91 //===----------------------------------------------------------------------===//
92 //  ValueHandle Class - Smart pointer that occupies a slot on the users USE list
93 //  that prevents it from being destroyed.  This "looks" like an Instruction
94 //  with Opcode UserOp1.
95 // 
96 class ValueHandle : public Instruction {
97   ValueHandle(const ValueHandle &); // DO NOT IMPLEMENT
98   ValueMapCache &Cache;
99 public:
100   ValueHandle(ValueMapCache &VMC, Value *V);
101   ~ValueHandle();
102
103   virtual Instruction *clone() const { abort(); return 0; }
104
105   virtual const char *getOpcodeName() const {
106     return "ValueHandle";
107   }
108
109   // Methods for support type inquiry through isa, cast, and dyn_cast:
110   static inline bool classof(const ValueHandle *) { return true; }
111   static inline bool classof(const Instruction *I) {
112     return (I->getOpcode() == Instruction::UserOp1);
113   }
114   static inline bool classof(const Value *V) {
115     return isa<Instruction>(V) && classof(cast<Instruction>(V));
116   }
117 };
118
119 // getStructOffsetType - Return a vector of offsets that are to be used to index
120 // into the specified struct type to get as close as possible to index as we
121 // can.  Note that it is possible that we cannot get exactly to Offset, in which
122 // case we update offset to be the offset we actually obtained.  The resultant
123 // leaf type is returned.
124 //
125 // If StopEarly is set to true (the default), the first object with the
126 // specified type is returned, even if it is a struct type itself.  In this
127 // case, this routine will not drill down to the leaf type.  Set StopEarly to
128 // false if you want a leaf
129 //
130 const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
131                                 vector<ConstPoolVal*> &Offsets,
132                                 bool StopEarly = true);
133
134 #endif