| Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- TransformInternals.h - Shared functions for Transforms --*- C++ -*-===// | 
| John Criswell | 29265fe | 2003-10-21 15:17:13 +0000 | [diff] [blame] | 2 | // | 
|  | 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. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 3084cb6 | 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 | 583e95e | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" | 
| Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" | 
| Chris Lattner | 3084cb6 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 22 | #include <map> | 
| Chris Lattner | 0ecba60 | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 23 | #include <set> | 
| Chris Lattner | 3084cb6 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 24 |  | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | namespace llvm { | 
|  | 26 |  | 
| Chris Lattner | ec3936a | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 27 | static inline int64_t getConstantValue(const ConstantInt *CPI) { | 
| Chris Lattner | 6077c31 | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 28 | return (int64_t)cast<ConstantInt>(CPI)->getRawValue(); | 
| Chris Lattner | c43c461 | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 29 | } | 
| Chris Lattner | 3084cb6 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 30 |  | 
|  | 31 |  | 
| Chris Lattner | c43c461 | 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 | 583e95e | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 34 | // | 
| Chris Lattner | c43c461 | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 35 | static inline const CompositeType *getPointedToComposite(const Type *Ty) { | 
| Chris Lattner | 583e95e | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 36 | const PointerType *PT = dyn_cast<PointerType>(Ty); | 
| Chris Lattner | 2413b16 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 37 | return PT ? dyn_cast<CompositeType>(PT->getElementType()) : 0; | 
| Chris Lattner | 583e95e | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 38 | } | 
|  | 39 |  | 
| Misha Brukman | e5838c4 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 40 | // ConvertibleToGEP - This function returns true if the specified value V is | 
| Chris Lattner | c43c461 | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 41 | // a valid index into a pointer of type Ty.  If it is valid, Idx is filled in | 
|  | 42 | // with the values that would be appropriate to make this a getelementptr | 
|  | 43 | // instruction.  The type returned is the root type that the GEP would point | 
|  | 44 | // to if it were synthesized with this operands. | 
|  | 45 | // | 
|  | 46 | // If BI is nonnull, cast instructions are inserted as appropriate for the | 
|  | 47 | // arguments of the getelementptr. | 
|  | 48 | // | 
| Misha Brukman | e5838c4 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 49 | const Type *ConvertibleToGEP(const Type *Ty, Value *V, | 
| Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 50 | std::vector<Value*> &Indices, | 
| Chris Lattner | c1f9206 | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 51 | const TargetData &TD, | 
| Chris Lattner | c43c461 | 2001-11-26 17:00:13 +0000 | [diff] [blame] | 52 | BasicBlock::iterator *BI = 0); | 
|  | 53 |  | 
|  | 54 |  | 
| Chris Lattner | 60ebba5 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 55 | //===----------------------------------------------------------------------===// | 
|  | 56 | //  ValueHandle Class - Smart pointer that occupies a slot on the users USE list | 
|  | 57 | //  that prevents it from being destroyed.  This "looks" like an Instruction | 
|  | 58 | //  with Opcode UserOp1. | 
|  | 59 | // | 
|  | 60 | class ValueMapCache; | 
|  | 61 | class ValueHandle : public Instruction { | 
| Chris Lattner | 84d3137 | 2005-01-29 00:37:36 +0000 | [diff] [blame] | 62 | Use Op; | 
| Chris Lattner | 60ebba5 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 63 | ValueMapCache &Cache; | 
|  | 64 | public: | 
|  | 65 | ValueHandle(ValueMapCache &VMC, Value *V); | 
|  | 66 | ValueHandle(const ValueHandle &); | 
|  | 67 | ~ValueHandle(); | 
|  | 68 |  | 
|  | 69 | virtual Instruction *clone() const { abort(); return 0; } | 
|  | 70 |  | 
|  | 71 | virtual const char *getOpcodeName() const { | 
|  | 72 | return "ValueHandle"; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | inline bool operator<(const ValueHandle &VH) const { | 
|  | 76 | return getOperand(0) < VH.getOperand(0); | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | // Methods for support type inquiry through isa, cast, and dyn_cast: | 
|  | 80 | static inline bool classof(const ValueHandle *) { return true; } | 
|  | 81 | static inline bool classof(const Instruction *I) { | 
|  | 82 | return (I->getOpcode() == Instruction::UserOp1); | 
|  | 83 | } | 
|  | 84 | static inline bool classof(const Value *V) { | 
|  | 85 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | 
|  | 86 | } | 
|  | 87 | }; | 
|  | 88 |  | 
|  | 89 |  | 
| Chris Lattner | 3084cb6 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 90 | // ------------- Expression Conversion --------------------- | 
|  | 91 |  | 
| Chris Lattner | 60ebba5 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 92 | typedef std::map<const Value*, const Type*> ValueTypeCache; | 
| Chris Lattner | 0ecba60 | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 93 |  | 
| Chris Lattner | 7dfc2d2 | 2004-10-27 16:14:51 +0000 | [diff] [blame] | 94 | class ValueMapCache { | 
|  | 95 | public: | 
| Chris Lattner | 0ecba60 | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 96 | // Operands mapped - Contains an entry if the first value (the user) has had | 
|  | 97 | // the second value (the operand) mapped already. | 
|  | 98 | // | 
| Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 99 | std::set<const User*> OperandsMapped; | 
| Chris Lattner | 0ecba60 | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 100 |  | 
|  | 101 | // Expression Map - Contains an entry from the old value to the new value of | 
|  | 102 | // an expression that has been converted over. | 
|  | 103 | // | 
| Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 104 | std::map<const Value *, Value *> ExprMap; | 
|  | 105 | typedef std::map<const Value *, Value *> ExprMapTy; | 
| Chris Lattner | 60ebba5 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 106 |  | 
|  | 107 | // Cast Map - Cast instructions can have their source and destination values | 
| Misha Brukman | be372b9 | 2003-08-21 22:14:26 +0000 | [diff] [blame] | 108 | // changed independently for each part.  Because of this, our old naive | 
| Chris Lattner | 60ebba5 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 109 | // implementation would create a TWO new cast instructions, which would cause | 
|  | 110 | // all kinds of problems.  Here we keep track of the newly allocated casts, so | 
|  | 111 | // that we only create one for a particular instruction. | 
|  | 112 | // | 
|  | 113 | std::set<ValueHandle> NewCasts; | 
| Chris Lattner | 0ecba60 | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 114 | }; | 
| Chris Lattner | 3084cb6 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 115 |  | 
| Chris Lattner | 583e95e | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 116 |  | 
| Misha Brukman | e5838c4 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 117 | bool ExpressionConvertibleToType(Value *V, const Type *Ty, ValueTypeCache &Map, | 
| Chris Lattner | c1f9206 | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 118 | const TargetData &TD); | 
|  | 119 | Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC, | 
|  | 120 | const TargetData &TD); | 
| Chris Lattner | 583e95e | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 121 |  | 
| Misha Brukman | e5838c4 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 122 | // ValueConvertibleToType - Return true if it is possible | 
|  | 123 | bool ValueConvertibleToType(Value *V, const Type *Ty, | 
| Chris Lattner | c1f9206 | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 124 | ValueTypeCache &ConvertedTypes, | 
|  | 125 | const TargetData &TD); | 
| Chris Lattner | 3084cb6 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 126 |  | 
| Chris Lattner | c1f9206 | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 127 | void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC, | 
|  | 128 | const TargetData &TD); | 
| Chris Lattner | 3084cb6 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 129 |  | 
|  | 130 |  | 
| Chris Lattner | 583e95e | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 131 | // getStructOffsetType - Return a vector of offsets that are to be used to index | 
|  | 132 | // into the specified struct type to get as close as possible to index as we | 
|  | 133 | // can.  Note that it is possible that we cannot get exactly to Offset, in which | 
|  | 134 | // case we update offset to be the offset we actually obtained.  The resultant | 
|  | 135 | // leaf type is returned. | 
|  | 136 | // | 
|  | 137 | // If StopEarly is set to true (the default), the first object with the | 
|  | 138 | // specified type is returned, even if it is a struct type itself.  In this | 
|  | 139 | // case, this routine will not drill down to the leaf type.  Set StopEarly to | 
|  | 140 | // false if you want a leaf | 
|  | 141 | // | 
|  | 142 | const Type *getStructOffsetType(const Type *Ty, unsigned &Offset, | 
| Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 143 | std::vector<Value*> &Offsets, | 
| Chris Lattner | c1f9206 | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 144 | const TargetData &TD, bool StopEarly = true); | 
| Chris Lattner | 583e95e | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 145 |  | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 146 | } // End llvm namespace | 
|  | 147 |  | 
| Chris Lattner | 3084cb6 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 148 | #endif |