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" |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 12 | #include "llvm/Instruction.h" |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 13 | #include "llvm/Target/TargetData.h" |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 14 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 15 | #include "llvm/ConstPoolVals.h" |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 16 | #include <map> |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 17 | #include <set> |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 18 | |
| 19 | // TargetData Hack: Eventually we will have annotations given to us by the |
| 20 | // backend so that we know stuff about type size and alignments. For now |
| 21 | // though, just use this, because it happens to match the model that GCC uses. |
| 22 | // |
| 23 | // FIXME: This should use annotations |
| 24 | // |
| 25 | extern const TargetData TD; |
| 26 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 27 | static int getConstantValue(const ConstPoolInt *CPI) { |
| 28 | if (const ConstPoolSInt *CSI = dyn_cast<ConstPoolSInt>(CPI)) |
| 29 | return CSI->getValue(); |
| 30 | return cast<ConstPoolUInt>(CPI)->getValue(); |
| 31 | } |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 32 | |
| 33 | |
Chris Lattner | e34443d | 2001-11-06 08:34:29 +0000 | [diff] [blame] | 34 | // isFirstClassType - Return true if a value of the specified type can be held |
| 35 | // in a register. |
| 36 | // |
| 37 | static inline bool isFirstClassType(const Type *Ty) { |
| 38 | return Ty->isPrimitiveType() || Ty->isPointerType(); |
| 39 | } |
| 40 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 41 | // getPointedToComposite - If the argument is a pointer type, and the pointed to |
| 42 | // value is a composite type, return the composite type, else return null. |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 43 | // |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 44 | static inline const CompositeType *getPointedToComposite(const Type *Ty) { |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 45 | const PointerType *PT = dyn_cast<PointerType>(Ty); |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 46 | return PT ? dyn_cast<CompositeType>(PT->getValueType()) : 0; |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Chris Lattner | e34443d | 2001-11-06 08:34:29 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 50 | // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 51 | // with a value, then remove and delete the original instruction. |
| 52 | // |
| 53 | void ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 54 | BasicBlock::iterator &BI, Value *V); |
| 55 | |
| 56 | // ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 57 | // instruction specified by I. The original instruction is deleted and BI is |
| 58 | // updated to point to the new instruction. |
| 59 | // |
| 60 | void ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 61 | BasicBlock::iterator &BI, Instruction *I); |
| 62 | |
| 63 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 64 | // ConvertableToGEP - This function returns true if the specified value V is |
| 65 | // a valid index into a pointer of type Ty. If it is valid, Idx is filled in |
| 66 | // with the values that would be appropriate to make this a getelementptr |
| 67 | // instruction. The type returned is the root type that the GEP would point |
| 68 | // to if it were synthesized with this operands. |
| 69 | // |
| 70 | // If BI is nonnull, cast instructions are inserted as appropriate for the |
| 71 | // arguments of the getelementptr. |
| 72 | // |
| 73 | const Type *ConvertableToGEP(const Type *Ty, Value *V, vector<Value*> &Indices, |
| 74 | BasicBlock::iterator *BI = 0); |
| 75 | |
| 76 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 77 | // ------------- Expression Conversion --------------------- |
| 78 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 79 | typedef map<const Value*, const Type*> ValueTypeCache; |
| 80 | |
| 81 | struct ValueMapCache { |
| 82 | // Operands mapped - Contains an entry if the first value (the user) has had |
| 83 | // the second value (the operand) mapped already. |
| 84 | // |
Chris Lattner | e34443d | 2001-11-06 08:34:29 +0000 | [diff] [blame] | 85 | set<const User*> OperandsMapped; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 86 | |
| 87 | // Expression Map - Contains an entry from the old value to the new value of |
| 88 | // an expression that has been converted over. |
| 89 | // |
| 90 | map<const Value *, Value *> ExprMap; |
| 91 | typedef map<const Value *, Value *> ExprMapTy; |
| 92 | }; |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 93 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 94 | |
| 95 | bool ExpressionConvertableToType(Value *V, const Type *Ty, ValueTypeCache &Map); |
| 96 | Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC); |
| 97 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 98 | // ValueConvertableToType - Return true if it is possible |
| 99 | bool ValueConvertableToType(Value *V, const Type *Ty, |
| 100 | ValueTypeCache &ConvertedTypes); |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 102 | void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC); |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 103 | |
| 104 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 105 | //===----------------------------------------------------------------------===// |
| 106 | // ValueHandle Class - Smart pointer that occupies a slot on the users USE list |
| 107 | // that prevents it from being destroyed. This "looks" like an Instruction |
| 108 | // with Opcode UserOp1. |
| 109 | // |
| 110 | class ValueHandle : public Instruction { |
| 111 | ValueHandle(const ValueHandle &); // DO NOT IMPLEMENT |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 112 | ValueMapCache &Cache; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 113 | public: |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 114 | ValueHandle(ValueMapCache &VMC, Value *V); |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 115 | ~ValueHandle(); |
| 116 | |
| 117 | virtual Instruction *clone() const { abort(); return 0; } |
| 118 | |
| 119 | virtual const char *getOpcodeName() const { |
| 120 | return "ValueHandle"; |
| 121 | } |
| 122 | |
| 123 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 124 | static inline bool classof(const ValueHandle *) { return true; } |
| 125 | static inline bool classof(const Instruction *I) { |
| 126 | return (I->getOpcode() == Instruction::UserOp1); |
| 127 | } |
| 128 | static inline bool classof(const Value *V) { |
| 129 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 130 | } |
| 131 | }; |
| 132 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 133 | // getStructOffsetType - Return a vector of offsets that are to be used to index |
| 134 | // into the specified struct type to get as close as possible to index as we |
| 135 | // can. Note that it is possible that we cannot get exactly to Offset, in which |
| 136 | // case we update offset to be the offset we actually obtained. The resultant |
| 137 | // leaf type is returned. |
| 138 | // |
| 139 | // If StopEarly is set to true (the default), the first object with the |
| 140 | // specified type is returned, even if it is a struct type itself. In this |
| 141 | // case, this routine will not drill down to the leaf type. Set StopEarly to |
| 142 | // false if you want a leaf |
| 143 | // |
| 144 | const Type *getStructOffsetType(const Type *Ty, unsigned &Offset, |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 145 | vector<Value*> &Offsets, |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 146 | bool StopEarly = true); |
| 147 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 148 | #endif |