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" |
| 14 | #include <map> |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 15 | #include <set> |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 16 | |
| 17 | // TargetData Hack: Eventually we will have annotations given to us by the |
| 18 | // backend so that we know stuff about type size and alignments. For now |
| 19 | // though, just use this, because it happens to match the model that GCC uses. |
| 20 | // |
| 21 | // FIXME: This should use annotations |
| 22 | // |
| 23 | extern const TargetData TD; |
| 24 | |
| 25 | // losslessCastableTypes - Return true if the types are bitwise equivalent. |
| 26 | // This predicate returns true if it is possible to cast from one type to |
| 27 | // another without gaining or losing precision, or altering the bits in any way. |
| 28 | // |
| 29 | bool losslessCastableTypes(const Type *T1, const Type *T2); |
| 30 | |
| 31 | |
Chris Lattner | e34443d | 2001-11-06 08:34:29 +0000 | [diff] [blame^] | 32 | // isFirstClassType - Return true if a value of the specified type can be held |
| 33 | // in a register. |
| 34 | // |
| 35 | static inline bool isFirstClassType(const Type *Ty) { |
| 36 | return Ty->isPrimitiveType() || Ty->isPointerType(); |
| 37 | } |
| 38 | |
| 39 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 40 | // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 41 | // with a value, then remove and delete the original instruction. |
| 42 | // |
| 43 | void ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 44 | BasicBlock::iterator &BI, Value *V); |
| 45 | |
| 46 | // ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 47 | // instruction specified by I. The original instruction is deleted and BI is |
| 48 | // updated to point to the new instruction. |
| 49 | // |
| 50 | void ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 51 | BasicBlock::iterator &BI, Instruction *I); |
| 52 | |
| 53 | |
| 54 | // ------------- Expression Conversion --------------------- |
| 55 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 56 | typedef map<const Value*, const Type*> ValueTypeCache; |
| 57 | |
| 58 | struct ValueMapCache { |
| 59 | // Operands mapped - Contains an entry if the first value (the user) has had |
| 60 | // the second value (the operand) mapped already. |
| 61 | // |
Chris Lattner | e34443d | 2001-11-06 08:34:29 +0000 | [diff] [blame^] | 62 | set<const User*> OperandsMapped; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 63 | |
| 64 | // Expression Map - Contains an entry from the old value to the new value of |
| 65 | // an expression that has been converted over. |
| 66 | // |
| 67 | map<const Value *, Value *> ExprMap; |
| 68 | typedef map<const Value *, Value *> ExprMapTy; |
| 69 | }; |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 70 | |
| 71 | // RetValConvertableToType - Return true if it is possible |
| 72 | bool RetValConvertableToType(Value *V, const Type *Ty, |
| 73 | ValueTypeCache &ConvertedTypes); |
| 74 | |
| 75 | void ConvertUsersType(Value *V, Value *NewVal, ValueMapCache &VMC); |
| 76 | |
| 77 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 78 | //===----------------------------------------------------------------------===// |
| 79 | // ValueHandle Class - Smart pointer that occupies a slot on the users USE list |
| 80 | // that prevents it from being destroyed. This "looks" like an Instruction |
| 81 | // with Opcode UserOp1. |
| 82 | // |
| 83 | class ValueHandle : public Instruction { |
| 84 | ValueHandle(const ValueHandle &); // DO NOT IMPLEMENT |
| 85 | public: |
Chris Lattner | e34443d | 2001-11-06 08:34:29 +0000 | [diff] [blame^] | 86 | ValueHandle(Value *V); |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 87 | ~ValueHandle(); |
| 88 | |
| 89 | virtual Instruction *clone() const { abort(); return 0; } |
| 90 | |
| 91 | virtual const char *getOpcodeName() const { |
| 92 | return "ValueHandle"; |
| 93 | } |
| 94 | |
| 95 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 96 | static inline bool classof(const ValueHandle *) { return true; } |
| 97 | static inline bool classof(const Instruction *I) { |
| 98 | return (I->getOpcode() == Instruction::UserOp1); |
| 99 | } |
| 100 | static inline bool classof(const Value *V) { |
| 101 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 102 | } |
| 103 | }; |
| 104 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 105 | #endif |