Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 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/Target/TargetData.h" |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 13 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 15 | #include <map> |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 16 | #include <set> |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 26 | static inline int64_t getConstantValue(const ConstantInt *CPI) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 27 | if (const ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPI)) |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 28 | return CSI->getValue(); |
| 29 | return (int64_t)cast<ConstantUInt>(CPI)->getValue(); |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 30 | } |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 31 | |
| 32 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 33 | // getPointedToComposite - If the argument is a pointer type, and the pointed to |
| 34 | // value is a composite type, return the composite type, else return null. |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 35 | // |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 36 | static inline const CompositeType *getPointedToComposite(const Type *Ty) { |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 37 | const PointerType *PT = dyn_cast<PointerType>(Ty); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 38 | return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0; |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 41 | // ConvertableToGEP - This function returns true if the specified value V is |
| 42 | // a valid index into a pointer of type Ty. If it is valid, Idx is filled in |
| 43 | // with the values that would be appropriate to make this a getelementptr |
| 44 | // instruction. The type returned is the root type that the GEP would point |
| 45 | // to if it were synthesized with this operands. |
| 46 | // |
| 47 | // If BI is nonnull, cast instructions are inserted as appropriate for the |
| 48 | // arguments of the getelementptr. |
| 49 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 50 | const Type *ConvertableToGEP(const Type *Ty, Value *V, |
| 51 | std::vector<Value*> &Indices, |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 52 | BasicBlock::iterator *BI = 0); |
| 53 | |
| 54 | |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 89 | // ------------- Expression Conversion --------------------- |
| 90 | |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 91 | typedef std::map<const Value*, const Type*> ValueTypeCache; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 92 | |
| 93 | struct ValueMapCache { |
| 94 | // Operands mapped - Contains an entry if the first value (the user) has had |
| 95 | // the second value (the operand) mapped already. |
| 96 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 97 | std::set<const User*> OperandsMapped; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 98 | |
| 99 | // Expression Map - Contains an entry from the old value to the new value of |
| 100 | // an expression that has been converted over. |
| 101 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 102 | std::map<const Value *, Value *> ExprMap; |
| 103 | typedef std::map<const Value *, Value *> ExprMapTy; |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 104 | |
| 105 | // Cast Map - Cast instructions can have their source and destination values |
| 106 | // changed independantly for each part. Because of this, our old naive |
| 107 | // implementation would create a TWO new cast instructions, which would cause |
| 108 | // all kinds of problems. Here we keep track of the newly allocated casts, so |
| 109 | // that we only create one for a particular instruction. |
| 110 | // |
| 111 | std::set<ValueHandle> NewCasts; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 112 | }; |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 113 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 114 | |
| 115 | bool ExpressionConvertableToType(Value *V, const Type *Ty, ValueTypeCache &Map); |
| 116 | Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC); |
| 117 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 118 | // ValueConvertableToType - Return true if it is possible |
| 119 | bool ValueConvertableToType(Value *V, const Type *Ty, |
| 120 | ValueTypeCache &ConvertedTypes); |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 122 | void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC); |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 123 | |
| 124 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 125 | // getStructOffsetType - Return a vector of offsets that are to be used to index |
| 126 | // into the specified struct type to get as close as possible to index as we |
| 127 | // can. Note that it is possible that we cannot get exactly to Offset, in which |
| 128 | // case we update offset to be the offset we actually obtained. The resultant |
| 129 | // leaf type is returned. |
| 130 | // |
| 131 | // If StopEarly is set to true (the default), the first object with the |
| 132 | // specified type is returned, even if it is a struct type itself. In this |
| 133 | // case, this routine will not drill down to the leaf type. Set StopEarly to |
| 134 | // false if you want a leaf |
| 135 | // |
| 136 | const Type *getStructOffsetType(const Type *Ty, unsigned &Offset, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 137 | std::vector<Value*> &Offsets, |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 138 | bool StopEarly = true); |
| 139 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 140 | #endif |