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