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