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 | 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 | |
| 26 | // losslessCastableTypes - Return true if the types are bitwise equivalent. |
| 27 | // This predicate returns true if it is possible to cast from one type to |
| 28 | // another without gaining or losing precision, or altering the bits in any way. |
| 29 | // |
| 30 | bool losslessCastableTypes(const Type *T1, const Type *T2); |
| 31 | |
| 32 | |
Chris Lattner | e34443d | 2001-11-06 08:34:29 +0000 | [diff] [blame] | 33 | // isFirstClassType - Return true if a value of the specified type can be held |
| 34 | // in a register. |
| 35 | // |
| 36 | static inline bool isFirstClassType(const Type *Ty) { |
| 37 | return Ty->isPrimitiveType() || Ty->isPointerType(); |
| 38 | } |
| 39 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame^] | 40 | // getPointedToStruct - If the argument is a pointer type, and the pointed to |
| 41 | // value is a struct type, return the struct type, else return null. |
| 42 | // |
| 43 | static inline const StructType *getPointedToStruct(const Type *Ty) { |
| 44 | const PointerType *PT = dyn_cast<PointerType>(Ty); |
| 45 | return PT ? dyn_cast<StructType>(PT->getValueType()) : 0; |
| 46 | } |
| 47 | |
Chris Lattner | e34443d | 2001-11-06 08:34:29 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 49 | // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 50 | // with a value, then remove and delete the original instruction. |
| 51 | // |
| 52 | void ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 53 | BasicBlock::iterator &BI, Value *V); |
| 54 | |
| 55 | // ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 56 | // instruction specified by I. The original instruction is deleted and BI is |
| 57 | // updated to point to the new instruction. |
| 58 | // |
| 59 | void ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 60 | BasicBlock::iterator &BI, Instruction *I); |
| 61 | |
| 62 | |
| 63 | // ------------- Expression Conversion --------------------- |
| 64 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 65 | typedef map<const Value*, const Type*> ValueTypeCache; |
| 66 | |
| 67 | struct ValueMapCache { |
| 68 | // Operands mapped - Contains an entry if the first value (the user) has had |
| 69 | // the second value (the operand) mapped already. |
| 70 | // |
Chris Lattner | e34443d | 2001-11-06 08:34:29 +0000 | [diff] [blame] | 71 | set<const User*> OperandsMapped; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 72 | |
| 73 | // Expression Map - Contains an entry from the old value to the new value of |
| 74 | // an expression that has been converted over. |
| 75 | // |
| 76 | map<const Value *, Value *> ExprMap; |
| 77 | typedef map<const Value *, Value *> ExprMapTy; |
| 78 | }; |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 79 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame^] | 80 | |
| 81 | bool ExpressionConvertableToType(Value *V, const Type *Ty, ValueTypeCache &Map); |
| 82 | Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC); |
| 83 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 84 | // RetValConvertableToType - Return true if it is possible |
| 85 | bool RetValConvertableToType(Value *V, const Type *Ty, |
| 86 | ValueTypeCache &ConvertedTypes); |
| 87 | |
| 88 | void ConvertUsersType(Value *V, Value *NewVal, ValueMapCache &VMC); |
| 89 | |
| 90 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 91 | //===----------------------------------------------------------------------===// |
| 92 | // ValueHandle Class - Smart pointer that occupies a slot on the users USE list |
| 93 | // that prevents it from being destroyed. This "looks" like an Instruction |
| 94 | // with Opcode UserOp1. |
| 95 | // |
| 96 | class ValueHandle : public Instruction { |
| 97 | ValueHandle(const ValueHandle &); // DO NOT IMPLEMENT |
| 98 | public: |
Chris Lattner | e34443d | 2001-11-06 08:34:29 +0000 | [diff] [blame] | 99 | ValueHandle(Value *V); |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 100 | ~ValueHandle(); |
| 101 | |
| 102 | virtual Instruction *clone() const { abort(); return 0; } |
| 103 | |
| 104 | virtual const char *getOpcodeName() const { |
| 105 | return "ValueHandle"; |
| 106 | } |
| 107 | |
| 108 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 109 | static inline bool classof(const ValueHandle *) { return true; } |
| 110 | static inline bool classof(const Instruction *I) { |
| 111 | return (I->getOpcode() == Instruction::UserOp1); |
| 112 | } |
| 113 | static inline bool classof(const Value *V) { |
| 114 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 115 | } |
| 116 | }; |
| 117 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame^] | 118 | // getStructOffsetType - Return a vector of offsets that are to be used to index |
| 119 | // into the specified struct type to get as close as possible to index as we |
| 120 | // can. Note that it is possible that we cannot get exactly to Offset, in which |
| 121 | // case we update offset to be the offset we actually obtained. The resultant |
| 122 | // leaf type is returned. |
| 123 | // |
| 124 | // If StopEarly is set to true (the default), the first object with the |
| 125 | // specified type is returned, even if it is a struct type itself. In this |
| 126 | // case, this routine will not drill down to the leaf type. Set StopEarly to |
| 127 | // false if you want a leaf |
| 128 | // |
| 129 | const Type *getStructOffsetType(const Type *Ty, unsigned &Offset, |
| 130 | vector<ConstPoolVal*> &Offsets, |
| 131 | bool StopEarly = true); |
| 132 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 133 | #endif |