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 | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 15 | #include "llvm/ConstantVals.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 | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 27 | static inline int getConstantValue(const ConstantInt *CPI) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 28 | if (const ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPI)) |
Chris Lattner | 4a94a70 | 2002-04-16 22:10:52 +0000 | [diff] [blame^] | 29 | return (int)CSI->getValue(); |
| 30 | return (int)cast<ConstantUInt>(CPI)->getValue(); |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 31 | } |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 32 | |
| 33 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 34 | // getPointedToComposite - If the argument is a pointer type, and the pointed to |
| 35 | // value is a composite type, return the composite type, else return null. |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 36 | // |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 37 | static inline const CompositeType *getPointedToComposite(const Type *Ty) { |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 38 | const PointerType *PT = dyn_cast<PointerType>(Ty); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 39 | return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0; |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Chris Lattner | e34443d | 2001-11-06 08:34:29 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 43 | // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 44 | // with a value, then remove and delete the original instruction. |
| 45 | // |
| 46 | void ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 47 | BasicBlock::iterator &BI, Value *V); |
| 48 | |
| 49 | // ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 50 | // instruction specified by I. The original instruction is deleted and BI is |
| 51 | // updated to point to the new instruction. |
| 52 | // |
| 53 | void ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 54 | BasicBlock::iterator &BI, Instruction *I); |
| 55 | |
Chris Lattner | c933344 | 2001-12-14 16:39:22 +0000 | [diff] [blame] | 56 | void ReplaceInstWithInst(Instruction *From, Instruction *To); |
| 57 | |
Chris Lattner | f78642b | 2002-03-21 06:15:53 +0000 | [diff] [blame] | 58 | // InsertInstBeforeInst - Insert 'NewInst' into the basic block that 'Existing' |
| 59 | // is already in, and put it right before 'Existing'. This instruction should |
| 60 | // only be used when there is no iterator to Existing already around. The |
| 61 | // returned iterator points to the new instruction. |
| 62 | // |
| 63 | BasicBlock::iterator InsertInstBeforeInst(Instruction *NewInst, |
| 64 | Instruction *Existing); |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 66 | // ConvertableToGEP - This function returns true if the specified value V is |
| 67 | // a valid index into a pointer of type Ty. If it is valid, Idx is filled in |
| 68 | // with the values that would be appropriate to make this a getelementptr |
| 69 | // instruction. The type returned is the root type that the GEP would point |
| 70 | // to if it were synthesized with this operands. |
| 71 | // |
| 72 | // If BI is nonnull, cast instructions are inserted as appropriate for the |
| 73 | // arguments of the getelementptr. |
| 74 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 75 | const Type *ConvertableToGEP(const Type *Ty, Value *V, |
| 76 | std::vector<Value*> &Indices, |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 77 | BasicBlock::iterator *BI = 0); |
| 78 | |
| 79 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 80 | // ------------- Expression Conversion --------------------- |
| 81 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +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 | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 95 | }; |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 96 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 97 | |
| 98 | bool ExpressionConvertableToType(Value *V, const Type *Ty, ValueTypeCache &Map); |
| 99 | Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC); |
| 100 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 101 | // ValueConvertableToType - Return true if it is possible |
| 102 | bool ValueConvertableToType(Value *V, const Type *Ty, |
| 103 | ValueTypeCache &ConvertedTypes); |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 105 | void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC); |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 106 | |
| 107 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 108 | //===----------------------------------------------------------------------===// |
| 109 | // ValueHandle Class - Smart pointer that occupies a slot on the users USE list |
| 110 | // that prevents it from being destroyed. This "looks" like an Instruction |
| 111 | // with Opcode UserOp1. |
| 112 | // |
| 113 | class ValueHandle : public Instruction { |
| 114 | ValueHandle(const ValueHandle &); // DO NOT IMPLEMENT |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 115 | ValueMapCache &Cache; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 116 | public: |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 117 | ValueHandle(ValueMapCache &VMC, Value *V); |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 118 | ~ValueHandle(); |
| 119 | |
| 120 | virtual Instruction *clone() const { abort(); return 0; } |
| 121 | |
| 122 | virtual const char *getOpcodeName() const { |
| 123 | return "ValueHandle"; |
| 124 | } |
| 125 | |
| 126 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 127 | static inline bool classof(const ValueHandle *) { return true; } |
| 128 | static inline bool classof(const Instruction *I) { |
| 129 | return (I->getOpcode() == Instruction::UserOp1); |
| 130 | } |
| 131 | static inline bool classof(const Value *V) { |
| 132 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 133 | } |
| 134 | }; |
| 135 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 136 | // getStructOffsetType - Return a vector of offsets that are to be used to index |
| 137 | // into the specified struct type to get as close as possible to index as we |
| 138 | // can. Note that it is possible that we cannot get exactly to Offset, in which |
| 139 | // case we update offset to be the offset we actually obtained. The resultant |
| 140 | // leaf type is returned. |
| 141 | // |
| 142 | // If StopEarly is set to true (the default), the first object with the |
| 143 | // specified type is returned, even if it is a struct type itself. In this |
| 144 | // case, this routine will not drill down to the leaf type. Set StopEarly to |
| 145 | // false if you want a leaf |
| 146 | // |
| 147 | const Type *getStructOffsetType(const Type *Ty, unsigned &Offset, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 148 | std::vector<Value*> &Offsets, |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 149 | bool StopEarly = true); |
| 150 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 151 | #endif |