Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- TransformInternals.h - Shared functions for Transforms --*- C++ -*-===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 856ba76 | 2003-10-21 15:17:13 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 856ba76 | 2003-10-21 15:17:13 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 9 | // |
| 10 | // This header file declares shared functions used by the different components |
| 11 | // of the Transforms library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef TRANSFORM_INTERNALS_H |
| 16 | #define TRANSFORM_INTERNALS_H |
| 17 | |
| 18 | #include "llvm/BasicBlock.h" |
| 19 | #include "llvm/Target/TargetData.h" |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 22 | #include <map> |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 23 | #include <set> |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 24 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | namespace llvm { |
| 26 | |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 27 | static inline int64_t getConstantValue(const ConstantInt *CPI) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 28 | return (int64_t)cast<ConstantInt>(CPI)->getZExtValue(); |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 29 | } |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 30 | |
| 31 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 32 | // getPointedToComposite - If the argument is a pointer type, and the pointed to |
| 33 | // value is a composite type, return the composite type, else return null. |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 34 | // |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 35 | static inline const CompositeType *getPointedToComposite(const Type *Ty) { |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 36 | const PointerType *PT = dyn_cast<PointerType>(Ty); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 37 | return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0; |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Chris Lattner | 491b29f | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 40 | |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | // ValueHandle Class - Smart pointer that occupies a slot on the users USE list |
| 43 | // that prevents it from being destroyed. This "looks" like an Instruction |
| 44 | // with Opcode UserOp1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 45 | // |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 46 | class ValueMapCache; |
| 47 | class ValueHandle : public Instruction { |
Chris Lattner | 5598aa0 | 2005-01-29 00:37:36 +0000 | [diff] [blame] | 48 | Use Op; |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 49 | ValueMapCache &Cache; |
| 50 | public: |
| 51 | ValueHandle(ValueMapCache &VMC, Value *V); |
| 52 | ValueHandle(const ValueHandle &); |
| 53 | ~ValueHandle(); |
| 54 | |
| 55 | virtual Instruction *clone() const { abort(); return 0; } |
| 56 | |
| 57 | virtual const char *getOpcodeName() const { |
| 58 | return "ValueHandle"; |
| 59 | } |
| 60 | |
| 61 | inline bool operator<(const ValueHandle &VH) const { |
| 62 | return getOperand(0) < VH.getOperand(0); |
| 63 | } |
| 64 | |
| 65 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 66 | static inline bool classof(const ValueHandle *) { return true; } |
| 67 | static inline bool classof(const Instruction *I) { |
| 68 | return (I->getOpcode() == Instruction::UserOp1); |
| 69 | } |
| 70 | static inline bool classof(const Value *V) { |
| 71 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 76 | // ------------- Expression Conversion --------------------- |
| 77 | |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 78 | typedef std::map<const Value*, const Type*> ValueTypeCache; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 1fca5ff | 2004-10-27 16:14:51 +0000 | [diff] [blame] | 80 | class ValueMapCache { |
| 81 | public: |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 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 | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 85 | std::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 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 90 | std::map<const Value *, Value *> ExprMap; |
| 91 | typedef std::map<const Value *, Value *> ExprMapTy; |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 92 | |
| 93 | // Cast Map - Cast instructions can have their source and destination values |
Misha Brukman | ef6a6a6 | 2003-08-21 22:14:26 +0000 | [diff] [blame] | 94 | // changed independently for each part. Because of this, our old naive |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 95 | // implementation would create a TWO new cast instructions, which would cause |
| 96 | // all kinds of problems. Here we keep track of the newly allocated casts, so |
| 97 | // that we only create one for a particular instruction. |
| 98 | // |
| 99 | std::set<ValueHandle> NewCasts; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 100 | }; |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 101 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 102 | |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 103 | bool ExpressionConvertibleToType(Value *V, const Type *Ty, ValueTypeCache &Map, |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 104 | const TargetData &TD); |
| 105 | Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC, |
| 106 | const TargetData &TD); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 107 | |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 108 | // ValueConvertibleToType - Return true if it is possible |
| 109 | bool ValueConvertibleToType(Value *V, const Type *Ty, |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 110 | ValueTypeCache &ConvertedTypes, |
| 111 | const TargetData &TD); |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 113 | void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC, |
| 114 | const TargetData &TD); |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 115 | |
| 116 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 117 | // getStructOffsetType - Return a vector of offsets that are to be used to index |
| 118 | // into the specified struct type to get as close as possible to index as we |
| 119 | // can. Note that it is possible that we cannot get exactly to Offset, in which |
| 120 | // case we update offset to be the offset we actually obtained. The resultant |
| 121 | // leaf type is returned. |
| 122 | // |
| 123 | // If StopEarly is set to true (the default), the first object with the |
| 124 | // specified type is returned, even if it is a struct type itself. In this |
| 125 | // case, this routine will not drill down to the leaf type. Set StopEarly to |
| 126 | // false if you want a leaf |
| 127 | // |
| 128 | const Type *getStructOffsetType(const Type *Ty, unsigned &Offset, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 129 | std::vector<Value*> &Offsets, |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 130 | const TargetData &TD, bool StopEarly = true); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 131 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 132 | } // End llvm namespace |
| 133 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 134 | #endif |