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 | |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 18 | static inline int64_t getConstantValue(const ConstantInt *CPI) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 19 | if (const ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPI)) |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 20 | return CSI->getValue(); |
| 21 | return (int64_t)cast<ConstantUInt>(CPI)->getValue(); |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 22 | } |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 23 | |
| 24 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 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. |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 27 | // |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 28 | static inline const CompositeType *getPointedToComposite(const Type *Ty) { |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 29 | const PointerType *PT = dyn_cast<PointerType>(Ty); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 30 | return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0; |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 33 | // ConvertableToGEP - 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. |
| 38 | // |
| 39 | // If BI is nonnull, cast instructions are inserted as appropriate for the |
| 40 | // arguments of the getelementptr. |
| 41 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 42 | const Type *ConvertableToGEP(const Type *Ty, Value *V, |
| 43 | std::vector<Value*> &Indices, |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 44 | const TargetData &TD, |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 45 | BasicBlock::iterator *BI = 0); |
| 46 | |
| 47 | |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 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. |
| 52 | // |
| 53 | class ValueMapCache; |
| 54 | class ValueHandle : public Instruction { |
| 55 | ValueMapCache &Cache; |
| 56 | public: |
| 57 | ValueHandle(ValueMapCache &VMC, Value *V); |
| 58 | ValueHandle(const ValueHandle &); |
| 59 | ~ValueHandle(); |
| 60 | |
| 61 | virtual Instruction *clone() const { abort(); return 0; } |
| 62 | |
| 63 | virtual const char *getOpcodeName() const { |
| 64 | return "ValueHandle"; |
| 65 | } |
| 66 | |
| 67 | inline bool operator<(const ValueHandle &VH) const { |
| 68 | return getOperand(0) < VH.getOperand(0); |
| 69 | } |
| 70 | |
| 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); |
| 75 | } |
| 76 | static inline bool classof(const Value *V) { |
| 77 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 82 | // ------------- Expression Conversion --------------------- |
| 83 | |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 84 | typedef std::map<const Value*, const Type*> ValueTypeCache; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 85 | |
| 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. |
| 89 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 90 | std::set<const User*> OperandsMapped; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 91 | |
| 92 | // Expression Map - Contains an entry from the old value to the new value of |
| 93 | // an expression that has been converted over. |
| 94 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 95 | std::map<const Value *, Value *> ExprMap; |
| 96 | typedef std::map<const Value *, Value *> ExprMapTy; |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 97 | |
| 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. |
| 103 | // |
| 104 | std::set<ValueHandle> NewCasts; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 105 | }; |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 106 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 108 | bool ExpressionConvertableToType(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); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 113 | // ValueConvertableToType - Return true if it is possible |
| 114 | bool ValueConvertableToType(Value *V, const Type *Ty, |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 115 | ValueTypeCache &ConvertedTypes, |
| 116 | const TargetData &TD); |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 118 | void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC, |
| 119 | const TargetData &TD); |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 120 | |
| 121 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 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. |
| 127 | // |
| 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 |
| 132 | // |
| 133 | const Type *getStructOffsetType(const Type *Ty, unsigned &Offset, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 134 | std::vector<Value*> &Offsets, |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 135 | const TargetData &TD, bool StopEarly = true); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 137 | #endif |