Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1 | //===-- Constants.cpp - Implement Constant nodes --------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 10 | // This file implements the Constant* classes... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | 5a945e3 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 15 | #include "ConstantFolding.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalValue.h" |
Misha Brukman | 63b38bd | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | #include "llvm/SymbolTable.h" |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Jim Laskey | b74c666 | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Reid Spencer | cf394bf | 2004-07-04 11:51:24 +0000 | [diff] [blame] | 24 | #include <iostream> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 27 | ConstantBool *ConstantBool::True = new ConstantBool(true); |
| 28 | ConstantBool *ConstantBool::False = new ConstantBool(false); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 32 | // Constant Class |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 35 | void Constant::destroyConstantImpl() { |
| 36 | // When a Constant is destroyed, there may be lingering |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 37 | // references to the constant by other constants in the constant pool. These |
Misha Brukman | be372b9 | 2003-08-21 22:14:26 +0000 | [diff] [blame] | 38 | // constants are implicitly dependent on the module that is being deleted, |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 39 | // but they don't know that. Because we only find out when the CPV is |
| 40 | // deleted, we must now notify all of our users (that should only be |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 41 | // Constants) that they are, in fact, invalid now and should be deleted. |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 42 | // |
| 43 | while (!use_empty()) { |
| 44 | Value *V = use_back(); |
| 45 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | d9f4ac66 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 46 | if (!isa<Constant>(V)) |
| 47 | std::cerr << "While deleting: " << *this |
| 48 | << "\n\nUse still stuck around after Def is destroyed: " |
| 49 | << *V << "\n\n"; |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 50 | #endif |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 51 | assert(isa<Constant>(V) && "References remain to Constant being destroyed"); |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 52 | Constant *CV = cast<Constant>(V); |
| 53 | CV->destroyConstant(); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 54 | |
| 55 | // The constant should remove itself from our use list... |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 56 | assert((use_empty() || use_back() != V) && "Constant not removed!"); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Value has no outstanding references it is safe to delete it now... |
| 60 | delete this; |
Chris Lattner | 3856934 | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 61 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 62 | |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 63 | // Static constructor to create a '0' constant of arbitrary type... |
| 64 | Constant *Constant::getNullValue(const Type *Ty) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 65 | switch (Ty->getTypeID()) { |
Chris Lattner | 3e88ef9 | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 66 | case Type::BoolTyID: { |
| 67 | static Constant *NullBool = ConstantBool::get(false); |
| 68 | return NullBool; |
| 69 | } |
| 70 | case Type::SByteTyID: { |
| 71 | static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0); |
| 72 | return NullSByte; |
| 73 | } |
| 74 | case Type::UByteTyID: { |
| 75 | static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0); |
| 76 | return NullUByte; |
| 77 | } |
| 78 | case Type::ShortTyID: { |
| 79 | static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0); |
| 80 | return NullShort; |
| 81 | } |
| 82 | case Type::UShortTyID: { |
| 83 | static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0); |
| 84 | return NullUShort; |
| 85 | } |
| 86 | case Type::IntTyID: { |
| 87 | static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0); |
| 88 | return NullInt; |
| 89 | } |
| 90 | case Type::UIntTyID: { |
| 91 | static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0); |
| 92 | return NullUInt; |
| 93 | } |
| 94 | case Type::LongTyID: { |
| 95 | static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0); |
| 96 | return NullLong; |
| 97 | } |
| 98 | case Type::ULongTyID: { |
| 99 | static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0); |
| 100 | return NullULong; |
| 101 | } |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 3e88ef9 | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 103 | case Type::FloatTyID: { |
| 104 | static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0); |
| 105 | return NullFloat; |
| 106 | } |
| 107 | case Type::DoubleTyID: { |
| 108 | static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0); |
| 109 | return NullDouble; |
| 110 | } |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 111 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 112 | case Type::PointerTyID: |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 113 | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
Chris Lattner | 3e88ef9 | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 115 | case Type::StructTyID: |
| 116 | case Type::ArrayTyID: |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 117 | case Type::PackedTyID: |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 118 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 119 | default: |
Reid Spencer | cf394bf | 2004-07-04 11:51:24 +0000 | [diff] [blame] | 120 | // Function, Label, or Opaque type? |
| 121 | assert(!"Cannot create a null constant of that type!"); |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Static constructor to create the maximum constant of an integral type... |
| 127 | ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 128 | switch (Ty->getTypeID()) { |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 129 | case Type::BoolTyID: return ConstantBool::True; |
| 130 | case Type::SByteTyID: |
| 131 | case Type::ShortTyID: |
| 132 | case Type::IntTyID: |
| 133 | case Type::LongTyID: { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 134 | // Calculate 011111111111111... |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 135 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 136 | int64_t Val = INT64_MAX; // All ones |
| 137 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 138 | return ConstantSInt::get(Ty, Val); |
| 139 | } |
| 140 | |
| 141 | case Type::UByteTyID: |
| 142 | case Type::UShortTyID: |
| 143 | case Type::UIntTyID: |
| 144 | case Type::ULongTyID: return getAllOnesValue(Ty); |
| 145 | |
Chris Lattner | 31408f7 | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 146 | default: return 0; |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | // Static constructor to create the minimum constant for an integral type... |
| 151 | ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 152 | switch (Ty->getTypeID()) { |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 153 | case Type::BoolTyID: return ConstantBool::False; |
| 154 | case Type::SByteTyID: |
| 155 | case Type::ShortTyID: |
| 156 | case Type::IntTyID: |
| 157 | case Type::LongTyID: { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 158 | // Calculate 1111111111000000000000 |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 159 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 160 | int64_t Val = -1; // All ones |
| 161 | Val <<= TypeBits-1; // Shift over to the right spot |
| 162 | return ConstantSInt::get(Ty, Val); |
| 163 | } |
| 164 | |
| 165 | case Type::UByteTyID: |
| 166 | case Type::UShortTyID: |
| 167 | case Type::UIntTyID: |
| 168 | case Type::ULongTyID: return ConstantUInt::get(Ty, 0); |
| 169 | |
Chris Lattner | 31408f7 | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 170 | default: return 0; |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | // Static constructor to create an integral constant with all bits set |
| 175 | ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 176 | switch (Ty->getTypeID()) { |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 177 | case Type::BoolTyID: return ConstantBool::True; |
| 178 | case Type::SByteTyID: |
| 179 | case Type::ShortTyID: |
| 180 | case Type::IntTyID: |
| 181 | case Type::LongTyID: return ConstantSInt::get(Ty, -1); |
| 182 | |
| 183 | case Type::UByteTyID: |
| 184 | case Type::UShortTyID: |
| 185 | case Type::UIntTyID: |
| 186 | case Type::ULongTyID: { |
| 187 | // Calculate ~0 of the right type... |
| 188 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 189 | uint64_t Val = ~0ULL; // All ones |
| 190 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 191 | return ConstantUInt::get(Ty, Val); |
| 192 | } |
Chris Lattner | 31408f7 | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 193 | default: return 0; |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Chris Lattner | 83e5d39 | 2003-03-10 22:39:02 +0000 | [diff] [blame] | 197 | bool ConstantUInt::isAllOnesValue() const { |
| 198 | unsigned TypeBits = getType()->getPrimitiveSize()*8; |
| 199 | uint64_t Val = ~0ULL; // All ones |
| 200 | Val >>= 64-TypeBits; // Shift out inappropriate bits |
| 201 | return getValue() == Val; |
| 202 | } |
| 203 | |
Chris Lattner | b1585a9 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 205 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 206 | // ConstantXXX Classes |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 207 | //===----------------------------------------------------------------------===// |
| 208 | |
| 209 | //===----------------------------------------------------------------------===// |
| 210 | // Normal Constructors |
| 211 | |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 212 | ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V) |
| 213 | : Constant(Ty, VT, 0, 0) { |
Chris Lattner | 265eb64 | 2004-06-21 12:12:12 +0000 | [diff] [blame] | 214 | Val.Unsigned = V; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 215 | } |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 216 | |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 217 | ConstantBool::ConstantBool(bool V) |
| 218 | : ConstantIntegral(Type::BoolTy, ConstantBoolVal, V) { |
Chris Lattner | 265eb64 | 2004-06-21 12:12:12 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 221 | ConstantInt::ConstantInt(const Type *Ty, ValueTy VT, uint64_t V) |
| 222 | : ConstantIntegral(Ty, VT, V) { |
Chris Lattner | 7309d66 | 2001-07-21 19:16:08 +0000 | [diff] [blame] | 223 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 224 | |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 225 | ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) |
| 226 | : ConstantInt(Ty, ConstantSIntVal, V) { |
Chris Lattner | 236c129 | 2002-09-11 01:21:04 +0000 | [diff] [blame] | 227 | assert(Ty->isInteger() && Ty->isSigned() && |
Reid Spencer | f064bb2 | 2005-03-09 15:19:41 +0000 | [diff] [blame] | 228 | "Illegal type for signed integer constant!"); |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 229 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 232 | ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) |
| 233 | : ConstantInt(Ty, ConstantUIntVal, V) { |
Chris Lattner | 236c129 | 2002-09-11 01:21:04 +0000 | [diff] [blame] | 234 | assert(Ty->isInteger() && Ty->isUnsigned() && |
| 235 | "Illegal type for unsigned integer constant!"); |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 236 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 239 | ConstantFP::ConstantFP(const Type *Ty, double V) |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 240 | : Constant(Ty, ConstantFPVal, 0, 0) { |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 241 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 242 | Val = V; |
| 243 | } |
| 244 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 245 | ConstantArray::ConstantArray(const ArrayType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 246 | const std::vector<Constant*> &V) |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 247 | : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) { |
Alkis Evlogimenos | 0507ffe | 2004-09-15 02:32:15 +0000 | [diff] [blame] | 248 | assert(V.size() == T->getNumElements() && |
| 249 | "Invalid initializer vector for constant array"); |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 250 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 251 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 252 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 253 | Constant *C = *I; |
| 254 | assert((C->getType() == T->getElementType() || |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 255 | (T->isAbstract() && |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 256 | C->getType()->getTypeID() == T->getElementType()->getTypeID())) && |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 257 | "Initializer for array element doesn't match array element type!"); |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 258 | OL->init(C, this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 262 | ConstantArray::~ConstantArray() { |
| 263 | delete [] OperandList; |
| 264 | } |
| 265 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 266 | ConstantStruct::ConstantStruct(const StructType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 267 | const std::vector<Constant*> &V) |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 268 | : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) { |
Chris Lattner | ac6db75 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 269 | assert(V.size() == T->getNumElements() && |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 270 | "Invalid initializer vector for constant structure"); |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 271 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 272 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 273 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 274 | Constant *C = *I; |
| 275 | assert((C->getType() == T->getElementType(I-V.begin()) || |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 276 | ((T->getElementType(I-V.begin())->isAbstract() || |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 277 | C->getType()->isAbstract()) && |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 278 | T->getElementType(I-V.begin())->getTypeID() == |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 279 | C->getType()->getTypeID())) && |
Chris Lattner | 93c8f14 | 2003-06-02 17:42:47 +0000 | [diff] [blame] | 280 | "Initializer for struct element doesn't match struct element type!"); |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 281 | OL->init(C, this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 285 | ConstantStruct::~ConstantStruct() { |
| 286 | delete [] OperandList; |
| 287 | } |
| 288 | |
| 289 | |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 290 | ConstantPacked::ConstantPacked(const PackedType *T, |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 291 | const std::vector<Constant*> &V) |
Chris Lattner | e7e139e | 2005-09-27 06:09:08 +0000 | [diff] [blame] | 292 | : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) { |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 293 | Use *OL = OperandList; |
Chris Lattner | 0144fad | 2005-10-03 21:56:24 +0000 | [diff] [blame] | 294 | for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); |
| 295 | I != E; ++I, ++OL) { |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 296 | Constant *C = *I; |
| 297 | assert((C->getType() == T->getElementType() || |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 298 | (T->isAbstract() && |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 299 | C->getType()->getTypeID() == T->getElementType()->getTypeID())) && |
Alkis Evlogimenos | cb031d9 | 2004-09-10 04:16:59 +0000 | [diff] [blame] | 300 | "Initializer for packed element doesn't match packed element type!"); |
Chris Lattner | 20a2445 | 2005-10-07 05:23:36 +0000 | [diff] [blame] | 301 | OL->init(C, this); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 305 | ConstantPacked::~ConstantPacked() { |
| 306 | delete [] OperandList; |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 309 | /// UnaryConstantExpr - This class is private to Constants.cpp, and is used |
| 310 | /// behind the scenes to implement unary constant exprs. |
| 311 | class UnaryConstantExpr : public ConstantExpr { |
| 312 | Use Op; |
| 313 | public: |
| 314 | UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty) |
| 315 | : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {} |
| 316 | }; |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 22ced56 | 2003-06-22 20:48:30 +0000 | [diff] [blame] | 318 | static bool isSetCC(unsigned Opcode) { |
| 319 | return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE || |
| 320 | Opcode == Instruction::SetLT || Opcode == Instruction::SetGT || |
| 321 | Opcode == Instruction::SetLE || Opcode == Instruction::SetGE; |
| 322 | } |
| 323 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 324 | /// BinaryConstantExpr - This class is private to Constants.cpp, and is used |
| 325 | /// behind the scenes to implement binary constant exprs. |
| 326 | class BinaryConstantExpr : public ConstantExpr { |
| 327 | Use Ops[2]; |
| 328 | public: |
| 329 | BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2) |
| 330 | : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(), |
| 331 | Opcode, Ops, 2) { |
| 332 | Ops[0].init(C1, this); |
| 333 | Ops[1].init(C2, this); |
| 334 | } |
| 335 | }; |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 336 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 337 | /// SelectConstantExpr - This class is private to Constants.cpp, and is used |
| 338 | /// behind the scenes to implement select constant exprs. |
| 339 | class SelectConstantExpr : public ConstantExpr { |
| 340 | Use Ops[3]; |
| 341 | public: |
| 342 | SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
| 343 | : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) { |
| 344 | Ops[0].init(C1, this); |
| 345 | Ops[1].init(C2, this); |
| 346 | Ops[2].init(C3, this); |
| 347 | } |
| 348 | }; |
| 349 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 350 | /// ExtractElementConstantExpr - This class is private to |
| 351 | /// Constants.cpp, and is used behind the scenes to implement |
| 352 | /// extractelement constant exprs. |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 353 | class ExtractElementConstantExpr : public ConstantExpr { |
| 354 | Use Ops[2]; |
| 355 | public: |
| 356 | ExtractElementConstantExpr(Constant *C1, Constant *C2) |
| 357 | : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(), |
| 358 | Instruction::ExtractElement, Ops, 2) { |
| 359 | Ops[0].init(C1, this); |
| 360 | Ops[1].init(C2, this); |
| 361 | } |
| 362 | }; |
| 363 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 364 | /// InsertElementConstantExpr - This class is private to |
| 365 | /// Constants.cpp, and is used behind the scenes to implement |
| 366 | /// insertelement constant exprs. |
| 367 | class InsertElementConstantExpr : public ConstantExpr { |
| 368 | Use Ops[3]; |
| 369 | public: |
| 370 | InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
| 371 | : ConstantExpr(C1->getType(), Instruction::InsertElement, |
| 372 | Ops, 3) { |
| 373 | Ops[0].init(C1, this); |
| 374 | Ops[1].init(C2, this); |
| 375 | Ops[2].init(C3, this); |
| 376 | } |
| 377 | }; |
| 378 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 379 | /// ShuffleVectorConstantExpr - This class is private to |
| 380 | /// Constants.cpp, and is used behind the scenes to implement |
| 381 | /// shufflevector constant exprs. |
| 382 | class ShuffleVectorConstantExpr : public ConstantExpr { |
| 383 | Use Ops[3]; |
| 384 | public: |
| 385 | ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3) |
| 386 | : ConstantExpr(C1->getType(), Instruction::ShuffleVector, |
| 387 | Ops, 3) { |
| 388 | Ops[0].init(C1, this); |
| 389 | Ops[1].init(C2, this); |
| 390 | Ops[2].init(C3, this); |
| 391 | } |
| 392 | }; |
| 393 | |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 394 | /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is |
| 395 | /// used behind the scenes to implement getelementpr constant exprs. |
| 396 | struct GetElementPtrConstantExpr : public ConstantExpr { |
| 397 | GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, |
| 398 | const Type *DestTy) |
| 399 | : ConstantExpr(DestTy, Instruction::GetElementPtr, |
| 400 | new Use[IdxList.size()+1], IdxList.size()+1) { |
| 401 | OperandList[0].init(C, this); |
| 402 | for (unsigned i = 0, E = IdxList.size(); i != E; ++i) |
| 403 | OperandList[i+1].init(IdxList[i], this); |
| 404 | } |
| 405 | ~GetElementPtrConstantExpr() { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 406 | delete [] OperandList; |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 407 | } |
| 408 | }; |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 410 | /// ConstantExpr::get* - Return some common constants without having to |
| 411 | /// specify the full Instruction::OPCODE identifier. |
| 412 | /// |
| 413 | Constant *ConstantExpr::getNeg(Constant *C) { |
Chris Lattner | 3cdc27c | 2004-03-29 19:51:24 +0000 | [diff] [blame] | 414 | if (!C->getType()->isFloatingPoint()) |
| 415 | return get(Instruction::Sub, getNullValue(C->getType()), C); |
| 416 | else |
| 417 | return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C); |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 418 | } |
| 419 | Constant *ConstantExpr::getNot(Constant *C) { |
| 420 | assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!"); |
| 421 | return get(Instruction::Xor, C, |
| 422 | ConstantIntegral::getAllOnesValue(C->getType())); |
| 423 | } |
| 424 | Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) { |
| 425 | return get(Instruction::Add, C1, C2); |
| 426 | } |
| 427 | Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) { |
| 428 | return get(Instruction::Sub, C1, C2); |
| 429 | } |
| 430 | Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) { |
| 431 | return get(Instruction::Mul, C1, C2); |
| 432 | } |
| 433 | Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) { |
| 434 | return get(Instruction::Div, C1, C2); |
| 435 | } |
| 436 | Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) { |
| 437 | return get(Instruction::Rem, C1, C2); |
| 438 | } |
| 439 | Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) { |
| 440 | return get(Instruction::And, C1, C2); |
| 441 | } |
| 442 | Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) { |
| 443 | return get(Instruction::Or, C1, C2); |
| 444 | } |
| 445 | Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { |
| 446 | return get(Instruction::Xor, C1, C2); |
| 447 | } |
| 448 | Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) { |
| 449 | return get(Instruction::SetEQ, C1, C2); |
| 450 | } |
| 451 | Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) { |
| 452 | return get(Instruction::SetNE, C1, C2); |
| 453 | } |
| 454 | Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) { |
| 455 | return get(Instruction::SetLT, C1, C2); |
| 456 | } |
| 457 | Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) { |
| 458 | return get(Instruction::SetGT, C1, C2); |
| 459 | } |
| 460 | Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) { |
| 461 | return get(Instruction::SetLE, C1, C2); |
| 462 | } |
| 463 | Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) { |
| 464 | return get(Instruction::SetGE, C1, C2); |
| 465 | } |
| 466 | Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) { |
| 467 | return get(Instruction::Shl, C1, C2); |
| 468 | } |
| 469 | Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) { |
| 470 | return get(Instruction::Shr, C1, C2); |
| 471 | } |
| 472 | |
Chris Lattner | db8bdba | 2004-05-25 05:32:43 +0000 | [diff] [blame] | 473 | Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) { |
| 474 | if (C1->getType()->isUnsigned()) return getShr(C1, C2); |
| 475 | return getCast(getShr(getCast(C1, |
| 476 | C1->getType()->getUnsignedVersion()), C2), C1->getType()); |
| 477 | } |
Chris Lattner | 817175f | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 478 | |
Chris Lattner | db8bdba | 2004-05-25 05:32:43 +0000 | [diff] [blame] | 479 | Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) { |
| 480 | if (C1->getType()->isSigned()) return getShr(C1, C2); |
| 481 | return getCast(getShr(getCast(C1, |
| 482 | C1->getType()->getSignedVersion()), C2), C1->getType()); |
| 483 | } |
Chris Lattner | 60e0dd7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 484 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 485 | |
| 486 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 487 | // isValueValidForType implementations |
| 488 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 489 | bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 490 | switch (Ty->getTypeID()) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 491 | default: |
| 492 | return false; // These can't be represented as integers!!! |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 493 | // Signed types... |
| 494 | case Type::SByteTyID: |
| 495 | return (Val <= INT8_MAX && Val >= INT8_MIN); |
| 496 | case Type::ShortTyID: |
| 497 | return (Val <= INT16_MAX && Val >= INT16_MIN); |
| 498 | case Type::IntTyID: |
Chris Lattner | 7424851 | 2004-06-08 23:21:39 +0000 | [diff] [blame] | 499 | return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 500 | case Type::LongTyID: |
| 501 | return true; // This is the largest type... |
| 502 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 505 | bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 506 | switch (Ty->getTypeID()) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 507 | default: |
| 508 | return false; // These can't be represented as integers!!! |
| 509 | |
| 510 | // Unsigned types... |
| 511 | case Type::UByteTyID: |
| 512 | return (Val <= UINT8_MAX); |
| 513 | case Type::UShortTyID: |
| 514 | return (Val <= UINT16_MAX); |
| 515 | case Type::UIntTyID: |
| 516 | return (Val <= UINT32_MAX); |
| 517 | case Type::ULongTyID: |
| 518 | return true; // This is the largest type... |
| 519 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 522 | bool ConstantFP::isValueValidForType(const Type *Ty, double Val) { |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 523 | switch (Ty->getTypeID()) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 524 | default: |
| 525 | return false; // These can't be represented as floating point! |
| 526 | |
Reid Spencer | b95f8ab | 2004-12-07 07:38:08 +0000 | [diff] [blame] | 527 | // TODO: Figure out how to test if a double can be cast to a float! |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 528 | case Type::FloatTyID: |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 529 | case Type::DoubleTyID: |
| 530 | return true; // This is the largest type... |
| 531 | } |
| 532 | }; |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 533 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 534 | //===----------------------------------------------------------------------===// |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 535 | // Factory Function Implementation |
| 536 | |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 537 | // ConstantCreator - A class that is used to create constants by |
| 538 | // ValueMap*. This class should be partially specialized if there is |
| 539 | // something strange that needs to be done to interface to the ctor for the |
| 540 | // constant. |
| 541 | // |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 542 | namespace llvm { |
| 543 | template<class ConstantClass, class TypeClass, class ValType> |
| 544 | struct ConstantCreator { |
| 545 | static ConstantClass *create(const TypeClass *Ty, const ValType &V) { |
| 546 | return new ConstantClass(Ty, V); |
| 547 | } |
| 548 | }; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 549 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 550 | template<class ConstantClass, class TypeClass> |
| 551 | struct ConvertConstantType { |
| 552 | static void convert(ConstantClass *OldC, const TypeClass *NewTy) { |
| 553 | assert(0 && "This type cannot be converted!\n"); |
| 554 | abort(); |
| 555 | } |
| 556 | }; |
| 557 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 559 | namespace { |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 560 | template<class ValType, class TypeClass, class ConstantClass, |
| 561 | bool HasLargeKey = false /*true for arrays and structs*/ > |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 562 | class ValueMap : public AbstractTypeUser { |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 563 | public: |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 564 | typedef std::pair<const TypeClass*, ValType> MapKey; |
| 565 | typedef std::map<MapKey, ConstantClass *> MapTy; |
| 566 | typedef typename MapTy::iterator MapIterator; |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 567 | private: |
Chris Lattner | 5bbf60a5 | 2005-10-04 16:52:46 +0000 | [diff] [blame] | 568 | /// Map - This is the main map from the element descriptor to the Constants. |
| 569 | /// This is the primary way we avoid creating two of the same shape |
| 570 | /// constant. |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 571 | MapTy Map; |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 572 | |
| 573 | /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping |
| 574 | /// from the constants to their element in Map. This is important for |
| 575 | /// removal of constants from the array, which would otherwise have to scan |
| 576 | /// through the map with very large keys. |
| 577 | std::map<ConstantClass*, MapIterator> InverseMap; |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 578 | |
| 579 | typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy; |
| 580 | AbstractTypeMapTy AbstractTypeMap; |
Chris Lattner | 99a669b | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 581 | |
| 582 | friend void Constant::clearAllValueMaps(); |
| 583 | private: |
| 584 | void clear(std::vector<Constant *> &Constants) { |
| 585 | for(MapIterator I = Map.begin(); I != Map.end(); ++I) |
| 586 | Constants.push_back(I->second); |
| 587 | Map.clear(); |
| 588 | AbstractTypeMap.clear(); |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 589 | InverseMap.clear(); |
Chris Lattner | 99a669b | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 592 | public: |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 593 | MapIterator map_end() { return Map.end(); } |
| 594 | |
| 595 | /// InsertOrGetItem - Return an iterator for the specified element. |
| 596 | /// If the element exists in the map, the returned iterator points to the |
| 597 | /// entry and Exists=true. If not, the iterator points to the newly |
| 598 | /// inserted entry and returns Exists=false. Newly inserted entries have |
| 599 | /// I->second == 0, and should be filled in. |
| 600 | MapIterator InsertOrGetItem(std::pair<MapKey, ConstantClass *> &InsertVal, |
| 601 | bool &Exists) { |
| 602 | std::pair<MapIterator, bool> IP = Map.insert(InsertVal); |
| 603 | Exists = !IP.second; |
| 604 | return IP.first; |
| 605 | } |
Chris Lattner | 5bbf60a5 | 2005-10-04 16:52:46 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 607 | private: |
| 608 | MapIterator FindExistingElement(ConstantClass *CP) { |
| 609 | if (HasLargeKey) { |
| 610 | typename std::map<ConstantClass*, MapIterator>::iterator |
| 611 | IMI = InverseMap.find(CP); |
| 612 | assert(IMI != InverseMap.end() && IMI->second != Map.end() && |
| 613 | IMI->second->second == CP && |
| 614 | "InverseMap corrupt!"); |
| 615 | return IMI->second; |
| 616 | } |
| 617 | |
| 618 | MapIterator I = |
| 619 | Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP))); |
Chris Lattner | 5bbf60a5 | 2005-10-04 16:52:46 +0000 | [diff] [blame] | 620 | if (I == Map.end() || I->second != CP) { |
| 621 | // FIXME: This should not use a linear scan. If this gets to be a |
| 622 | // performance problem, someone should look at this. |
| 623 | for (I = Map.begin(); I != Map.end() && I->second != CP; ++I) |
| 624 | /* empty */; |
| 625 | } |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 626 | return I; |
| 627 | } |
| 628 | public: |
| 629 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 630 | /// getOrCreate - Return the specified constant from the map, creating it if |
| 631 | /// necessary. |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 632 | ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) { |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 633 | MapKey Lookup(Ty, V); |
| 634 | MapIterator I = Map.lower_bound(Lookup); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 635 | if (I != Map.end() && I->first == Lookup) |
| 636 | return I->second; // Is it in the map? |
| 637 | |
| 638 | // If no preexisting value, create one now... |
| 639 | ConstantClass *Result = |
| 640 | ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V); |
| 641 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 642 | /// FIXME: why does this assert fail when loading 176.gcc? |
| 643 | //assert(Result->getType() == Ty && "Type specified is not correct!"); |
| 644 | I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result)); |
| 645 | |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 646 | if (HasLargeKey) // Remember the reverse mapping if needed. |
| 647 | InverseMap.insert(std::make_pair(Result, I)); |
| 648 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 649 | // If the type of the constant is abstract, make sure that an entry exists |
| 650 | // for it in the AbstractTypeMap. |
| 651 | if (Ty->isAbstract()) { |
| 652 | typename AbstractTypeMapTy::iterator TI = |
| 653 | AbstractTypeMap.lower_bound(Ty); |
| 654 | |
| 655 | if (TI == AbstractTypeMap.end() || TI->first != Ty) { |
| 656 | // Add ourselves to the ATU list of the type. |
| 657 | cast<DerivedType>(Ty)->addAbstractTypeUser(this); |
| 658 | |
| 659 | AbstractTypeMap.insert(TI, std::make_pair(Ty, I)); |
| 660 | } |
| 661 | } |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 662 | return Result; |
| 663 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 664 | |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 665 | void remove(ConstantClass *CP) { |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 666 | MapIterator I = FindExistingElement(CP); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 667 | assert(I != Map.end() && "Constant not found in constant table!"); |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 668 | assert(I->second == CP && "Didn't find correct element?"); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 669 | |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 670 | if (HasLargeKey) // Remember the reverse mapping if needed. |
| 671 | InverseMap.erase(CP); |
| 672 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 673 | // Now that we found the entry, make sure this isn't the entry that |
| 674 | // the AbstractTypeMap points to. |
| 675 | const TypeClass *Ty = I->first.first; |
| 676 | if (Ty->isAbstract()) { |
| 677 | assert(AbstractTypeMap.count(Ty) && |
| 678 | "Abstract type not in AbstractTypeMap?"); |
| 679 | MapIterator &ATMEntryIt = AbstractTypeMap[Ty]; |
| 680 | if (ATMEntryIt == I) { |
| 681 | // Yes, we are removing the representative entry for this type. |
| 682 | // See if there are any other entries of the same type. |
| 683 | MapIterator TmpIt = ATMEntryIt; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 684 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 685 | // First check the entry before this one... |
| 686 | if (TmpIt != Map.begin()) { |
| 687 | --TmpIt; |
| 688 | if (TmpIt->first.first != Ty) // Not the same type, move back... |
| 689 | ++TmpIt; |
| 690 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 691 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 692 | // If we didn't find the same type, try to move forward... |
| 693 | if (TmpIt == ATMEntryIt) { |
| 694 | ++TmpIt; |
| 695 | if (TmpIt == Map.end() || TmpIt->first.first != Ty) |
| 696 | --TmpIt; // No entry afterwards with the same type |
| 697 | } |
| 698 | |
| 699 | // If there is another entry in the map of the same abstract type, |
| 700 | // update the AbstractTypeMap entry now. |
| 701 | if (TmpIt != ATMEntryIt) { |
| 702 | ATMEntryIt = TmpIt; |
| 703 | } else { |
| 704 | // Otherwise, we are removing the last instance of this type |
| 705 | // from the table. Remove from the ATM, and from user list. |
| 706 | cast<DerivedType>(Ty)->removeAbstractTypeUser(this); |
| 707 | AbstractTypeMap.erase(Ty); |
| 708 | } |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 709 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 710 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 711 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 712 | Map.erase(I); |
| 713 | } |
| 714 | |
Chris Lattner | 3b793c6 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 715 | |
| 716 | /// MoveConstantToNewSlot - If we are about to change C to be the element |
| 717 | /// specified by I, update our internal data structures to reflect this |
| 718 | /// fact. |
| 719 | void MoveConstantToNewSlot(ConstantClass *C, MapIterator I) { |
| 720 | // First, remove the old location of the specified constant in the map. |
| 721 | MapIterator OldI = FindExistingElement(C); |
| 722 | assert(OldI != Map.end() && "Constant not found in constant table!"); |
| 723 | assert(OldI->second == C && "Didn't find correct element?"); |
| 724 | |
| 725 | // If this constant is the representative element for its abstract type, |
| 726 | // update the AbstractTypeMap so that the representative element is I. |
| 727 | if (C->getType()->isAbstract()) { |
| 728 | typename AbstractTypeMapTy::iterator ATI = |
| 729 | AbstractTypeMap.find(C->getType()); |
| 730 | assert(ATI != AbstractTypeMap.end() && |
| 731 | "Abstract type not in AbstractTypeMap?"); |
| 732 | if (ATI->second == OldI) |
| 733 | ATI->second = I; |
| 734 | } |
| 735 | |
| 736 | // Remove the old entry from the map. |
| 737 | Map.erase(OldI); |
| 738 | |
| 739 | // Update the inverse map so that we know that this constant is now |
| 740 | // located at descriptor I. |
| 741 | if (HasLargeKey) { |
| 742 | assert(I->second == C && "Bad inversemap entry!"); |
| 743 | InverseMap[C] = I; |
| 744 | } |
| 745 | } |
| 746 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 747 | void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 748 | typename AbstractTypeMapTy::iterator I = |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 749 | AbstractTypeMap.find(cast<TypeClass>(OldTy)); |
| 750 | |
| 751 | assert(I != AbstractTypeMap.end() && |
| 752 | "Abstract type not in AbstractTypeMap?"); |
| 753 | |
| 754 | // Convert a constant at a time until the last one is gone. The last one |
| 755 | // leaving will remove() itself, causing the AbstractTypeMapEntry to be |
| 756 | // eliminated eventually. |
| 757 | do { |
| 758 | ConvertConstantType<ConstantClass, |
| 759 | TypeClass>::convert(I->second->second, |
| 760 | cast<TypeClass>(NewTy)); |
| 761 | |
| 762 | I = AbstractTypeMap.find(cast<TypeClass>(OldTy)); |
| 763 | } while (I != AbstractTypeMap.end()); |
| 764 | } |
| 765 | |
| 766 | // If the type became concrete without being refined to any other existing |
| 767 | // type, we just remove ourselves from the ATU list. |
| 768 | void typeBecameConcrete(const DerivedType *AbsTy) { |
| 769 | AbsTy->removeAbstractTypeUser(this); |
| 770 | } |
| 771 | |
| 772 | void dump() const { |
| 773 | std::cerr << "Constant.cpp: ValueMap\n"; |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 774 | } |
| 775 | }; |
| 776 | } |
| 777 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 778 | //---- ConstantUInt::get() and ConstantSInt::get() implementations... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 779 | // |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 780 | static ValueMap< int64_t, Type, ConstantSInt> SIntConstants; |
| 781 | static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants; |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 782 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 783 | ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) { |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 784 | return SIntConstants.getOrCreate(Ty, V); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 787 | ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) { |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 788 | return UIntConstants.getOrCreate(Ty, V); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 791 | ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) { |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 792 | assert(V <= 127 && "Can only be used with very small positive constants!"); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 793 | if (Ty->isSigned()) return ConstantSInt::get(Ty, V); |
| 794 | return ConstantUInt::get(Ty, V); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 797 | //---- ConstantFP::get() implementation... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 798 | // |
Chris Lattner | ac80ea4 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 799 | namespace llvm { |
| 800 | template<> |
| 801 | struct ConstantCreator<ConstantFP, Type, uint64_t> { |
| 802 | static ConstantFP *create(const Type *Ty, uint64_t V) { |
| 803 | assert(Ty == Type::DoubleTy); |
Jim Laskey | b74c666 | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 804 | return new ConstantFP(Ty, BitsToDouble(V)); |
Chris Lattner | ac80ea4 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 805 | } |
| 806 | }; |
| 807 | template<> |
| 808 | struct ConstantCreator<ConstantFP, Type, uint32_t> { |
| 809 | static ConstantFP *create(const Type *Ty, uint32_t V) { |
| 810 | assert(Ty == Type::FloatTy); |
Jim Laskey | b74c666 | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 811 | return new ConstantFP(Ty, BitsToFloat(V)); |
Chris Lattner | ac80ea4 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 812 | } |
| 813 | }; |
| 814 | } |
| 815 | |
| 816 | static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants; |
| 817 | static ValueMap<uint32_t, Type, ConstantFP> FloatConstants; |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 818 | |
Jim Laskey | 8ad8f71 | 2005-08-17 20:06:22 +0000 | [diff] [blame] | 819 | bool ConstantFP::isNullValue() const { |
| 820 | return DoubleToBits(Val) == 0; |
| 821 | } |
| 822 | |
| 823 | bool ConstantFP::isExactlyValue(double V) const { |
| 824 | return DoubleToBits(V) == DoubleToBits(Val); |
| 825 | } |
| 826 | |
| 827 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 828 | ConstantFP *ConstantFP::get(const Type *Ty, double V) { |
Chris Lattner | 241ed4c | 2004-01-23 00:55:21 +0000 | [diff] [blame] | 829 | if (Ty == Type::FloatTy) { |
| 830 | // Force the value through memory to normalize it. |
Jim Laskey | b74c666 | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 831 | return FloatConstants.getOrCreate(Ty, FloatToBits(V)); |
Chris Lattner | ac80ea4 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 832 | } else { |
| 833 | assert(Ty == Type::DoubleTy); |
Jim Laskey | b74c666 | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 834 | return DoubleConstants.getOrCreate(Ty, DoubleToBits(V)); |
Chris Lattner | 241ed4c | 2004-01-23 00:55:21 +0000 | [diff] [blame] | 835 | } |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 838 | //---- ConstantAggregateZero::get() implementation... |
| 839 | // |
| 840 | namespace llvm { |
| 841 | // ConstantAggregateZero does not take extra "value" argument... |
| 842 | template<class ValType> |
| 843 | struct ConstantCreator<ConstantAggregateZero, Type, ValType> { |
| 844 | static ConstantAggregateZero *create(const Type *Ty, const ValType &V){ |
| 845 | return new ConstantAggregateZero(Ty); |
| 846 | } |
| 847 | }; |
| 848 | |
| 849 | template<> |
| 850 | struct ConvertConstantType<ConstantAggregateZero, Type> { |
| 851 | static void convert(ConstantAggregateZero *OldC, const Type *NewTy) { |
| 852 | // Make everyone now use a constant of the new type... |
| 853 | Constant *New = ConstantAggregateZero::get(NewTy); |
| 854 | assert(New != OldC && "Didn't replace constant??"); |
| 855 | OldC->uncheckedReplaceAllUsesWith(New); |
| 856 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 857 | } |
| 858 | }; |
| 859 | } |
| 860 | |
| 861 | static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants; |
| 862 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 863 | static char getValType(ConstantAggregateZero *CPZ) { return 0; } |
| 864 | |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 865 | Constant *ConstantAggregateZero::get(const Type *Ty) { |
| 866 | return AggZeroConstants.getOrCreate(Ty, 0); |
| 867 | } |
| 868 | |
| 869 | // destroyConstant - Remove the constant from the constant table... |
| 870 | // |
| 871 | void ConstantAggregateZero::destroyConstant() { |
| 872 | AggZeroConstants.remove(this); |
| 873 | destroyConstantImpl(); |
| 874 | } |
| 875 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 876 | //---- ConstantArray::get() implementation... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 877 | // |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 878 | namespace llvm { |
| 879 | template<> |
| 880 | struct ConvertConstantType<ConstantArray, ArrayType> { |
| 881 | static void convert(ConstantArray *OldC, const ArrayType *NewTy) { |
| 882 | // Make everyone now use a constant of the new type... |
| 883 | std::vector<Constant*> C; |
| 884 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 885 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
| 886 | Constant *New = ConstantArray::get(NewTy, C); |
| 887 | assert(New != OldC && "Didn't replace constant??"); |
| 888 | OldC->uncheckedReplaceAllUsesWith(New); |
| 889 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 890 | } |
| 891 | }; |
| 892 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 893 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 894 | static std::vector<Constant*> getValType(ConstantArray *CA) { |
| 895 | std::vector<Constant*> Elements; |
| 896 | Elements.reserve(CA->getNumOperands()); |
| 897 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
| 898 | Elements.push_back(cast<Constant>(CA->getOperand(i))); |
| 899 | return Elements; |
| 900 | } |
| 901 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 902 | typedef ValueMap<std::vector<Constant*>, ArrayType, |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 903 | ConstantArray, true /*largekey*/> ArrayConstantsTy; |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 904 | static ArrayConstantsTy ArrayConstants; |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 905 | |
Chris Lattner | 015e821 | 2004-02-15 04:14:47 +0000 | [diff] [blame] | 906 | Constant *ConstantArray::get(const ArrayType *Ty, |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 907 | const std::vector<Constant*> &V) { |
| 908 | // If this is an all-zero array, return a ConstantAggregateZero object |
| 909 | if (!V.empty()) { |
| 910 | Constant *C = V[0]; |
| 911 | if (!C->isNullValue()) |
| 912 | return ArrayConstants.getOrCreate(Ty, V); |
| 913 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 914 | if (V[i] != C) |
| 915 | return ArrayConstants.getOrCreate(Ty, V); |
| 916 | } |
| 917 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 920 | // destroyConstant - Remove the constant from the constant table... |
| 921 | // |
| 922 | void ConstantArray::destroyConstant() { |
| 923 | ArrayConstants.remove(this); |
| 924 | destroyConstantImpl(); |
| 925 | } |
| 926 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 927 | // ConstantArray::get(const string&) - Return an array that is initialized to |
Chris Lattner | 8f80fe0 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 928 | // contain the specified string. A null terminator is added to the specified |
| 929 | // string so that it may be used in a natural way... |
| 930 | // |
Chris Lattner | 015e821 | 2004-02-15 04:14:47 +0000 | [diff] [blame] | 931 | Constant *ConstantArray::get(const std::string &Str) { |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 932 | std::vector<Constant*> ElementVals; |
Chris Lattner | 8f80fe0 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 933 | |
| 934 | for (unsigned i = 0; i < Str.length(); ++i) |
Chris Lattner | bec9b0b | 2001-12-14 16:41:18 +0000 | [diff] [blame] | 935 | ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i])); |
Chris Lattner | 8f80fe0 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 936 | |
| 937 | // Add a null terminator to the string... |
Chris Lattner | bec9b0b | 2001-12-14 16:41:18 +0000 | [diff] [blame] | 938 | ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0)); |
Chris Lattner | 8f80fe0 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 939 | |
Chris Lattner | bec9b0b | 2001-12-14 16:41:18 +0000 | [diff] [blame] | 940 | ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 941 | return ConstantArray::get(ATy, ElementVals); |
Vikram S. Adve | 3441043 | 2001-10-14 23:17:20 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 944 | /// isString - This method returns true if the array is an array of sbyte or |
| 945 | /// ubyte, and if the elements of the array are all ConstantInt's. |
| 946 | bool ConstantArray::isString() const { |
| 947 | // Check the element type for sbyte or ubyte... |
Chris Lattner | e8701f6 | 2004-01-14 17:51:53 +0000 | [diff] [blame] | 948 | if (getType()->getElementType() != Type::UByteTy && |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 949 | getType()->getElementType() != Type::SByteTy) |
| 950 | return false; |
| 951 | // Check the elements to make sure they are all integers, not constant |
| 952 | // expressions. |
| 953 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 954 | if (!isa<ConstantInt>(getOperand(i))) |
| 955 | return false; |
| 956 | return true; |
| 957 | } |
| 958 | |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 959 | // getAsString - If the sub-element type of this array is either sbyte or ubyte, |
| 960 | // then this method converts the array to an std::string and returns it. |
| 961 | // Otherwise, it asserts out. |
| 962 | // |
| 963 | std::string ConstantArray::getAsString() const { |
Chris Lattner | e8dfcca | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 964 | assert(isString() && "Not a string!"); |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 965 | std::string Result; |
Chris Lattner | 6077c31 | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 966 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 967 | Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue(); |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 968 | return Result; |
| 969 | } |
| 970 | |
| 971 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 972 | //---- ConstantStruct::get() implementation... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 973 | // |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 974 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 975 | namespace llvm { |
| 976 | template<> |
| 977 | struct ConvertConstantType<ConstantStruct, StructType> { |
| 978 | static void convert(ConstantStruct *OldC, const StructType *NewTy) { |
| 979 | // Make everyone now use a constant of the new type... |
| 980 | std::vector<Constant*> C; |
| 981 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 982 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
| 983 | Constant *New = ConstantStruct::get(NewTy, C); |
| 984 | assert(New != OldC && "Didn't replace constant??"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 985 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 986 | OldC->uncheckedReplaceAllUsesWith(New); |
| 987 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 988 | } |
| 989 | }; |
| 990 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 991 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 992 | typedef ValueMap<std::vector<Constant*>, StructType, |
Chris Lattner | 935aa92 | 2005-10-04 17:48:46 +0000 | [diff] [blame] | 993 | ConstantStruct, true /*largekey*/> StructConstantsTy; |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 994 | static StructConstantsTy StructConstants; |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 995 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 996 | static std::vector<Constant*> getValType(ConstantStruct *CS) { |
| 997 | std::vector<Constant*> Elements; |
| 998 | Elements.reserve(CS->getNumOperands()); |
| 999 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) |
| 1000 | Elements.push_back(cast<Constant>(CS->getOperand(i))); |
| 1001 | return Elements; |
| 1002 | } |
| 1003 | |
Chris Lattner | 015e821 | 2004-02-15 04:14:47 +0000 | [diff] [blame] | 1004 | Constant *ConstantStruct::get(const StructType *Ty, |
| 1005 | const std::vector<Constant*> &V) { |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1006 | // Create a ConstantAggregateZero value if all elements are zeros... |
| 1007 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 1008 | if (!V[i]->isNullValue()) |
| 1009 | return StructConstants.getOrCreate(Ty, V); |
| 1010 | |
| 1011 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1012 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | d6108ca | 2004-07-12 20:35:11 +0000 | [diff] [blame] | 1014 | Constant *ConstantStruct::get(const std::vector<Constant*> &V) { |
| 1015 | std::vector<const Type*> StructEls; |
| 1016 | StructEls.reserve(V.size()); |
| 1017 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 1018 | StructEls.push_back(V[i]->getType()); |
| 1019 | return get(StructType::get(StructEls), V); |
| 1020 | } |
| 1021 | |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1022 | // destroyConstant - Remove the constant from the constant table... |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1023 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1024 | void ConstantStruct::destroyConstant() { |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1025 | StructConstants.remove(this); |
| 1026 | destroyConstantImpl(); |
| 1027 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1028 | |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1029 | //---- ConstantPacked::get() implementation... |
| 1030 | // |
| 1031 | namespace llvm { |
| 1032 | template<> |
| 1033 | struct ConvertConstantType<ConstantPacked, PackedType> { |
| 1034 | static void convert(ConstantPacked *OldC, const PackedType *NewTy) { |
| 1035 | // Make everyone now use a constant of the new type... |
| 1036 | std::vector<Constant*> C; |
| 1037 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 1038 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
| 1039 | Constant *New = ConstantPacked::get(NewTy, C); |
| 1040 | assert(New != OldC && "Didn't replace constant??"); |
| 1041 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1042 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1043 | } |
| 1044 | }; |
| 1045 | } |
| 1046 | |
| 1047 | static std::vector<Constant*> getValType(ConstantPacked *CP) { |
| 1048 | std::vector<Constant*> Elements; |
| 1049 | Elements.reserve(CP->getNumOperands()); |
| 1050 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 1051 | Elements.push_back(CP->getOperand(i)); |
| 1052 | return Elements; |
| 1053 | } |
| 1054 | |
| 1055 | static ValueMap<std::vector<Constant*>, PackedType, |
| 1056 | ConstantPacked> PackedConstants; |
| 1057 | |
| 1058 | Constant *ConstantPacked::get(const PackedType *Ty, |
| 1059 | const std::vector<Constant*> &V) { |
| 1060 | // If this is an all-zero packed, return a ConstantAggregateZero object |
| 1061 | if (!V.empty()) { |
| 1062 | Constant *C = V[0]; |
| 1063 | if (!C->isNullValue()) |
| 1064 | return PackedConstants.getOrCreate(Ty, V); |
| 1065 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 1066 | if (V[i] != C) |
| 1067 | return PackedConstants.getOrCreate(Ty, V); |
| 1068 | } |
| 1069 | return ConstantAggregateZero::get(Ty); |
| 1070 | } |
| 1071 | |
| 1072 | Constant *ConstantPacked::get(const std::vector<Constant*> &V) { |
| 1073 | assert(!V.empty() && "Cannot infer type if V is empty"); |
| 1074 | return get(PackedType::get(V.front()->getType(),V.size()), V); |
| 1075 | } |
| 1076 | |
| 1077 | // destroyConstant - Remove the constant from the constant table... |
| 1078 | // |
| 1079 | void ConstantPacked::destroyConstant() { |
| 1080 | PackedConstants.remove(this); |
| 1081 | destroyConstantImpl(); |
| 1082 | } |
| 1083 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1084 | //---- ConstantPointerNull::get() implementation... |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1085 | // |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1086 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1087 | namespace llvm { |
| 1088 | // ConstantPointerNull does not take extra "value" argument... |
| 1089 | template<class ValType> |
| 1090 | struct ConstantCreator<ConstantPointerNull, PointerType, ValType> { |
| 1091 | static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){ |
| 1092 | return new ConstantPointerNull(Ty); |
| 1093 | } |
| 1094 | }; |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1095 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1096 | template<> |
| 1097 | struct ConvertConstantType<ConstantPointerNull, PointerType> { |
| 1098 | static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) { |
| 1099 | // Make everyone now use a constant of the new type... |
| 1100 | Constant *New = ConstantPointerNull::get(NewTy); |
| 1101 | assert(New != OldC && "Didn't replace constant??"); |
| 1102 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1103 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1104 | } |
| 1105 | }; |
| 1106 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1107 | |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1108 | static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants; |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1109 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1110 | static char getValType(ConstantPointerNull *) { |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1115 | ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) { |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1116 | return NullPtrConstants.getOrCreate(Ty, 0); |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1119 | // destroyConstant - Remove the constant from the constant table... |
| 1120 | // |
| 1121 | void ConstantPointerNull::destroyConstant() { |
| 1122 | NullPtrConstants.remove(this); |
| 1123 | destroyConstantImpl(); |
| 1124 | } |
| 1125 | |
| 1126 | |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1127 | //---- UndefValue::get() implementation... |
| 1128 | // |
| 1129 | |
| 1130 | namespace llvm { |
| 1131 | // UndefValue does not take extra "value" argument... |
| 1132 | template<class ValType> |
| 1133 | struct ConstantCreator<UndefValue, Type, ValType> { |
| 1134 | static UndefValue *create(const Type *Ty, const ValType &V) { |
| 1135 | return new UndefValue(Ty); |
| 1136 | } |
| 1137 | }; |
| 1138 | |
| 1139 | template<> |
| 1140 | struct ConvertConstantType<UndefValue, Type> { |
| 1141 | static void convert(UndefValue *OldC, const Type *NewTy) { |
| 1142 | // Make everyone now use a constant of the new type. |
| 1143 | Constant *New = UndefValue::get(NewTy); |
| 1144 | assert(New != OldC && "Didn't replace constant??"); |
| 1145 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1146 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1147 | } |
| 1148 | }; |
| 1149 | } |
| 1150 | |
| 1151 | static ValueMap<char, Type, UndefValue> UndefValueConstants; |
| 1152 | |
| 1153 | static char getValType(UndefValue *) { |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | UndefValue *UndefValue::get(const Type *Ty) { |
| 1159 | return UndefValueConstants.getOrCreate(Ty, 0); |
| 1160 | } |
| 1161 | |
| 1162 | // destroyConstant - Remove the constant from the constant table. |
| 1163 | // |
| 1164 | void UndefValue::destroyConstant() { |
| 1165 | UndefValueConstants.remove(this); |
| 1166 | destroyConstantImpl(); |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1172 | //---- ConstantExpr::get() implementations... |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1173 | // |
Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1174 | typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType; |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1175 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1176 | namespace llvm { |
| 1177 | template<> |
| 1178 | struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { |
| 1179 | static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) { |
| 1180 | if (V.first == Instruction::Cast) |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 1181 | return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1182 | if ((V.first >= Instruction::BinaryOpsBegin && |
| 1183 | V.first < Instruction::BinaryOpsEnd) || |
| 1184 | V.first == Instruction::Shl || V.first == Instruction::Shr) |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 1185 | return new BinaryConstantExpr(V.first, V.second[0], V.second[1]); |
Chris Lattner | b7897ce | 2004-03-30 22:51:03 +0000 | [diff] [blame] | 1186 | if (V.first == Instruction::Select) |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 1187 | return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1188 | if (V.first == Instruction::ExtractElement) |
| 1189 | return new ExtractElementConstantExpr(V.second[0], V.second[1]); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1190 | if (V.first == Instruction::InsertElement) |
| 1191 | return new InsertElementConstantExpr(V.second[0], V.second[1], |
| 1192 | V.second[2]); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1193 | if (V.first == Instruction::ShuffleVector) |
| 1194 | return new ShuffleVectorConstantExpr(V.second[0], V.second[1], |
| 1195 | V.second[2]); |
| 1196 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1197 | assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1198 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1199 | std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end()); |
Chris Lattner | d0df99c | 2005-01-29 00:34:39 +0000 | [diff] [blame] | 1200 | return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1201 | } |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1202 | }; |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1203 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1204 | template<> |
| 1205 | struct ConvertConstantType<ConstantExpr, Type> { |
| 1206 | static void convert(ConstantExpr *OldC, const Type *NewTy) { |
| 1207 | Constant *New; |
| 1208 | switch (OldC->getOpcode()) { |
| 1209 | case Instruction::Cast: |
| 1210 | New = ConstantExpr::getCast(OldC->getOperand(0), NewTy); |
| 1211 | break; |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1212 | case Instruction::Select: |
| 1213 | New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0), |
| 1214 | OldC->getOperand(1), |
| 1215 | OldC->getOperand(2)); |
| 1216 | break; |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1217 | case Instruction::Shl: |
| 1218 | case Instruction::Shr: |
| 1219 | New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(), |
| 1220 | OldC->getOperand(0), OldC->getOperand(1)); |
| 1221 | break; |
| 1222 | default: |
| 1223 | assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin && |
| 1224 | OldC->getOpcode() < Instruction::BinaryOpsEnd); |
| 1225 | New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0), |
| 1226 | OldC->getOperand(1)); |
| 1227 | break; |
| 1228 | case Instruction::GetElementPtr: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1229 | // Make everyone now use a constant of the new type... |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1230 | std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end()); |
| 1231 | New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx); |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1232 | break; |
| 1233 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1234 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1235 | assert(New != OldC && "Didn't replace constant??"); |
| 1236 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1237 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1238 | } |
| 1239 | }; |
| 1240 | } // end namespace llvm |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1241 | |
| 1242 | |
Chris Lattner | 3e650af | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1243 | static ExprMapKeyType getValType(ConstantExpr *CE) { |
| 1244 | std::vector<Constant*> Operands; |
| 1245 | Operands.reserve(CE->getNumOperands()); |
| 1246 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) |
| 1247 | Operands.push_back(cast<Constant>(CE->getOperand(i))); |
| 1248 | return ExprMapKeyType(CE->getOpcode(), Operands); |
| 1249 | } |
| 1250 | |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1251 | static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants; |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1252 | |
Chris Lattner | 46b3d30 | 2003-04-16 22:40:51 +0000 | [diff] [blame] | 1253 | Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) { |
Chris Lattner | 815ae2b | 2003-10-07 22:19:19 +0000 | [diff] [blame] | 1254 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
| 1255 | |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1256 | if (Constant *FC = ConstantFoldCastInstruction(C, Ty)) |
| 1257 | return FC; // Fold a few common cases... |
| 1258 | |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1259 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1260 | std::vector<Constant*> argVec(1, C); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1261 | ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec); |
| 1262 | return ExprConstants.getOrCreate(Ty, Key); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1263 | } |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1264 | |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1265 | Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) { |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1266 | assert(C->getType()->isIntegral() && Ty->isIntegral() && |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1267 | C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() && |
| 1268 | "This is an illegal sign extension!"); |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1269 | if (C->getType() != Type::BoolTy) { |
| 1270 | C = ConstantExpr::getCast(C, C->getType()->getSignedVersion()); |
| 1271 | return ConstantExpr::getCast(C, Ty); |
| 1272 | } else { |
| 1273 | if (C == ConstantBool::True) |
| 1274 | return ConstantIntegral::getAllOnesValue(Ty); |
| 1275 | else |
| 1276 | return ConstantIntegral::getNullValue(Ty); |
| 1277 | } |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) { |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1281 | assert(C->getType()->isIntegral() && Ty->isIntegral() && |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1282 | C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() && |
| 1283 | "This is an illegal zero extension!"); |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1284 | if (C->getType() != Type::BoolTy) |
| 1285 | C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion()); |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1286 | return ConstantExpr::getCast(C, Ty); |
| 1287 | } |
| 1288 | |
Alkis Evlogimenos | da5de05 | 2004-10-24 01:41:10 +0000 | [diff] [blame] | 1289 | Constant *ConstantExpr::getSizeOf(const Type *Ty) { |
Chris Lattner | acc4e54 | 2004-12-13 19:48:51 +0000 | [diff] [blame] | 1290 | // sizeof is implemented as: (ulong) gep (Ty*)null, 1 |
Alkis Evlogimenos | da5de05 | 2004-10-24 01:41:10 +0000 | [diff] [blame] | 1291 | return getCast( |
Chris Lattner | acc4e54 | 2004-12-13 19:48:51 +0000 | [diff] [blame] | 1292 | getGetElementPtr(getNullValue(PointerType::get(Ty)), |
| 1293 | std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))), |
| 1294 | Type::ULongTy); |
Alkis Evlogimenos | da5de05 | 2004-10-24 01:41:10 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Alkis Evlogimenos | 9160d5f | 2005-03-19 11:40:31 +0000 | [diff] [blame] | 1297 | Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) { |
| 1298 | // pointer from array is implemented as: getelementptr arr ptr, 0, 0 |
| 1299 | static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0)); |
| 1300 | |
| 1301 | return ConstantExpr::getGetElementPtr(C, Indices); |
| 1302 | } |
| 1303 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1304 | Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode, |
| 1305 | Constant *C1, Constant *C2) { |
Chris Lattner | 5645e8a | 2004-01-12 19:04:55 +0000 | [diff] [blame] | 1306 | if (Opcode == Instruction::Shl || Opcode == Instruction::Shr) |
| 1307 | return getShiftTy(ReqTy, Opcode, C1, C2); |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1308 | // Check the operands for consistency first |
| 1309 | assert((Opcode >= Instruction::BinaryOpsBegin && |
| 1310 | Opcode < Instruction::BinaryOpsEnd) && |
| 1311 | "Invalid opcode in binary constant expression"); |
| 1312 | assert(C1->getType() == C2->getType() && |
| 1313 | "Operand types in binary constant expression should match"); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1314 | |
Chris Lattner | 29ca2c6 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1315 | if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) && |
| 1316 | ReqTy == Type::BoolTy)) |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1317 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
| 1318 | return FC; // Fold a few common cases... |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1319 | |
Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1320 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1321 | ExprMapKeyType Key = std::make_pair(Opcode, argVec); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1322 | return ExprConstants.getOrCreate(ReqTy, Key); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Chris Lattner | 29ca2c6 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1325 | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) { |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1326 | #ifndef NDEBUG |
| 1327 | switch (Opcode) { |
| 1328 | case Instruction::Add: case Instruction::Sub: |
| 1329 | case Instruction::Mul: case Instruction::Div: |
| 1330 | case Instruction::Rem: |
| 1331 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Chris Lattner | c421a26 | 2006-01-04 01:01:04 +0000 | [diff] [blame] | 1332 | assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() || |
| 1333 | isa<PackedType>(C1->getType())) && |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1334 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1335 | break; |
| 1336 | case Instruction::And: |
| 1337 | case Instruction::Or: |
| 1338 | case Instruction::Xor: |
| 1339 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Chris Lattner | c421a26 | 2006-01-04 01:01:04 +0000 | [diff] [blame] | 1340 | assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1341 | "Tried to create a logical operation on a non-integral type!"); |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1342 | break; |
| 1343 | case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE: |
| 1344 | case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE: |
| 1345 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
| 1346 | break; |
| 1347 | case Instruction::Shl: |
| 1348 | case Instruction::Shr: |
| 1349 | assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!"); |
Chris Lattner | c421a26 | 2006-01-04 01:01:04 +0000 | [diff] [blame] | 1350 | assert((C1->getType()->isInteger() || isa<PackedType>(C1->getType())) && |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1351 | "Tried to create a shift operation on a non-integer type!"); |
| 1352 | break; |
| 1353 | default: |
| 1354 | break; |
| 1355 | } |
| 1356 | #endif |
| 1357 | |
Chris Lattner | 29ca2c6 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1358 | if (Instruction::isRelational(Opcode)) |
| 1359 | return getTy(Type::BoolTy, Opcode, C1, C2); |
| 1360 | else |
| 1361 | return getTy(C1->getType(), Opcode, C1, C2); |
| 1362 | } |
| 1363 | |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1364 | Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C, |
| 1365 | Constant *V1, Constant *V2) { |
| 1366 | assert(C->getType() == Type::BoolTy && "Select condition must be bool!"); |
| 1367 | assert(V1->getType() == V2->getType() && "Select value types must match!"); |
| 1368 | assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!"); |
| 1369 | |
| 1370 | if (ReqTy == V1->getType()) |
| 1371 | if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2)) |
| 1372 | return SC; // Fold common cases |
| 1373 | |
| 1374 | std::vector<Constant*> argVec(3, C); |
| 1375 | argVec[1] = V1; |
| 1376 | argVec[2] = V2; |
| 1377 | ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec); |
| 1378 | return ExprConstants.getOrCreate(ReqTy, Key); |
| 1379 | } |
| 1380 | |
Chris Lattner | 9eb2b52 | 2004-01-12 19:12:58 +0000 | [diff] [blame] | 1381 | /// getShiftTy - Return a shift left or shift right constant expr |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1382 | Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode, |
| 1383 | Constant *C1, Constant *C2) { |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1384 | // Check the operands for consistency first |
| 1385 | assert((Opcode == Instruction::Shl || |
| 1386 | Opcode == Instruction::Shr) && |
| 1387 | "Invalid opcode in binary constant expression"); |
| 1388 | assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy && |
| 1389 | "Invalid operand types for Shift constant expr!"); |
| 1390 | |
Chris Lattner | 0bba771 | 2004-01-12 20:40:42 +0000 | [diff] [blame] | 1391 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1392 | return FC; // Fold a few common cases... |
| 1393 | |
| 1394 | // Look up the constant in the table first to ensure uniqueness |
| 1395 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1396 | ExprMapKeyType Key = std::make_pair(Opcode, argVec); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1397 | return ExprConstants.getOrCreate(ReqTy, Key); |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1401 | Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1402 | const std::vector<Value*> &IdxList) { |
| 1403 | assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) && |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1404 | "GEP indices invalid!"); |
| 1405 | |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1406 | if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList)) |
| 1407 | return FC; // Fold a few common cases... |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1408 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1409 | assert(isa<PointerType>(C->getType()) && |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1410 | "Non-pointer type for constant GetElementPtr expression"); |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1411 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1412 | std::vector<Constant*> ArgVec; |
| 1413 | ArgVec.reserve(IdxList.size()+1); |
| 1414 | ArgVec.push_back(C); |
| 1415 | for (unsigned i = 0, e = IdxList.size(); i != e; ++i) |
| 1416 | ArgVec.push_back(cast<Constant>(IdxList[i])); |
| 1417 | const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1418 | return ExprConstants.getOrCreate(ReqTy, Key); |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1421 | Constant *ConstantExpr::getGetElementPtr(Constant *C, |
| 1422 | const std::vector<Constant*> &IdxList){ |
| 1423 | // Get the result type of the getelementptr! |
| 1424 | std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end()); |
| 1425 | |
| 1426 | const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList, |
| 1427 | true); |
| 1428 | assert(Ty && "GEP indices invalid!"); |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1429 | return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList); |
| 1430 | } |
| 1431 | |
| 1432 | Constant *ConstantExpr::getGetElementPtr(Constant *C, |
| 1433 | const std::vector<Value*> &IdxList) { |
| 1434 | // Get the result type of the getelementptr! |
| 1435 | const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList, |
| 1436 | true); |
| 1437 | assert(Ty && "GEP indices invalid!"); |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1438 | return getGetElementPtrTy(PointerType::get(Ty), C, IdxList); |
| 1439 | } |
| 1440 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1441 | Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val, |
| 1442 | Constant *Idx) { |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 1443 | if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx)) |
| 1444 | return FC; // Fold a few common cases... |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1445 | // Look up the constant in the table first to ensure uniqueness |
| 1446 | std::vector<Constant*> ArgVec(1, Val); |
| 1447 | ArgVec.push_back(Idx); |
| 1448 | const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec); |
| 1449 | return ExprConstants.getOrCreate(ReqTy, Key); |
| 1450 | } |
| 1451 | |
| 1452 | Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { |
| 1453 | assert(isa<PackedType>(Val->getType()) && |
| 1454 | "Tried to create extractelement operation on non-packed type!"); |
| 1455 | assert(Idx->getType() == Type::UIntTy && |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1456 | "Extractelement index must be uint type!"); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1457 | return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(), |
| 1458 | Val, Idx); |
| 1459 | } |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1460 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1461 | Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val, |
| 1462 | Constant *Elt, Constant *Idx) { |
| 1463 | if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx)) |
| 1464 | return FC; // Fold a few common cases... |
| 1465 | // Look up the constant in the table first to ensure uniqueness |
| 1466 | std::vector<Constant*> ArgVec(1, Val); |
| 1467 | ArgVec.push_back(Elt); |
| 1468 | ArgVec.push_back(Idx); |
| 1469 | const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec); |
| 1470 | return ExprConstants.getOrCreate(ReqTy, Key); |
| 1471 | } |
| 1472 | |
| 1473 | Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, |
| 1474 | Constant *Idx) { |
| 1475 | assert(isa<PackedType>(Val->getType()) && |
| 1476 | "Tried to create insertelement operation on non-packed type!"); |
| 1477 | assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType() |
| 1478 | && "Insertelement types must match!"); |
| 1479 | assert(Idx->getType() == Type::UIntTy && |
| 1480 | "Insertelement index must be uint type!"); |
| 1481 | return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(), |
| 1482 | Val, Elt, Idx); |
| 1483 | } |
| 1484 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1485 | Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1, |
| 1486 | Constant *V2, Constant *Mask) { |
| 1487 | if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask)) |
| 1488 | return FC; // Fold a few common cases... |
| 1489 | // Look up the constant in the table first to ensure uniqueness |
| 1490 | std::vector<Constant*> ArgVec(1, V1); |
| 1491 | ArgVec.push_back(V2); |
| 1492 | ArgVec.push_back(Mask); |
| 1493 | const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec); |
| 1494 | return ExprConstants.getOrCreate(ReqTy, Key); |
| 1495 | } |
| 1496 | |
| 1497 | Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, |
| 1498 | Constant *Mask) { |
| 1499 | assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && |
| 1500 | "Invalid shuffle vector constant expr operands!"); |
| 1501 | return getShuffleVectorTy(V1->getType(), V1, V2, Mask); |
| 1502 | } |
| 1503 | |
| 1504 | |
Vikram S. Adve | 4c48533 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1505 | // destroyConstant - Remove the constant from the constant table... |
| 1506 | // |
| 1507 | void ConstantExpr::destroyConstant() { |
| 1508 | ExprConstants.remove(this); |
| 1509 | destroyConstantImpl(); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Chris Lattner | 3cd8c56 | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 1512 | const char *ConstantExpr::getOpcodeName() const { |
| 1513 | return Instruction::getOpcodeName(getOpcode()); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1514 | } |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 1515 | |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1516 | //===----------------------------------------------------------------------===// |
| 1517 | // replaceUsesOfWithOnConstant implementations |
| 1518 | |
| 1519 | void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1520 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1521 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1522 | Constant *ToC = cast<Constant>(To); |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1523 | |
| 1524 | unsigned OperandToUpdate = U-OperandList; |
| 1525 | assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); |
| 1526 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1527 | std::pair<ArrayConstantsTy::MapKey, ConstantArray*> Lookup; |
| 1528 | Lookup.first.first = getType(); |
| 1529 | Lookup.second = this; |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1530 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1531 | std::vector<Constant*> &Values = Lookup.first.second; |
| 1532 | Values.reserve(getNumOperands()); // Build replacement array. |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1533 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1534 | // Fill values with the modified operands of the constant array. Also, |
| 1535 | // compute whether this turns into an all-zeros array. |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1536 | bool isAllZeros = false; |
| 1537 | if (!ToC->isNullValue()) { |
| 1538 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) |
| 1539 | Values.push_back(cast<Constant>(O->get())); |
| 1540 | } else { |
| 1541 | isAllZeros = true; |
| 1542 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 1543 | Constant *Val = cast<Constant>(O->get()); |
| 1544 | Values.push_back(Val); |
| 1545 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 1546 | } |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1547 | } |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1548 | Values[OperandToUpdate] = ToC; |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1549 | |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1550 | Constant *Replacement = 0; |
| 1551 | if (isAllZeros) { |
| 1552 | Replacement = ConstantAggregateZero::get(getType()); |
| 1553 | } else { |
| 1554 | // Check to see if we have this array type already. |
| 1555 | bool Exists; |
| 1556 | ArrayConstantsTy::MapIterator I = |
| 1557 | ArrayConstants.InsertOrGetItem(Lookup, Exists); |
| 1558 | |
| 1559 | if (Exists) { |
| 1560 | Replacement = I->second; |
| 1561 | } else { |
| 1562 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 1563 | // creating a new constant array, inserting it, replaceallusesof'ing the |
| 1564 | // old with the new, then deleting the old... just update the current one |
| 1565 | // in place! |
Chris Lattner | 3b793c6 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 1566 | ArrayConstants.MoveConstantToNewSlot(this, I); |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1567 | |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1568 | // Update to the new value. |
| 1569 | setOperand(OperandToUpdate, ToC); |
Chris Lattner | b64419a | 2005-10-03 22:51:37 +0000 | [diff] [blame] | 1570 | return; |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | // Otherwise, I do need to replace this with an existing value. |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1575 | assert(Replacement != this && "I didn't contain From!"); |
| 1576 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1577 | // Everyone using this now uses the replacement. |
| 1578 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1579 | |
| 1580 | // Delete the old constant! |
| 1581 | destroyConstant(); |
| 1582 | } |
| 1583 | |
| 1584 | void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1585 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1586 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1587 | Constant *ToC = cast<Constant>(To); |
| 1588 | |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1589 | unsigned OperandToUpdate = U-OperandList; |
| 1590 | assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); |
| 1591 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1592 | std::pair<StructConstantsTy::MapKey, ConstantStruct*> Lookup; |
| 1593 | Lookup.first.first = getType(); |
| 1594 | Lookup.second = this; |
| 1595 | std::vector<Constant*> &Values = Lookup.first.second; |
| 1596 | Values.reserve(getNumOperands()); // Build replacement struct. |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1597 | |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1598 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1599 | // Fill values with the modified operands of the constant struct. Also, |
| 1600 | // compute whether this turns into an all-zeros struct. |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1601 | bool isAllZeros = false; |
| 1602 | if (!ToC->isNullValue()) { |
| 1603 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) |
| 1604 | Values.push_back(cast<Constant>(O->get())); |
| 1605 | } else { |
| 1606 | isAllZeros = true; |
| 1607 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 1608 | Constant *Val = cast<Constant>(O->get()); |
| 1609 | Values.push_back(Val); |
| 1610 | if (isAllZeros) isAllZeros = Val->isNullValue(); |
| 1611 | } |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1612 | } |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1613 | Values[OperandToUpdate] = ToC; |
| 1614 | |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1615 | Constant *Replacement = 0; |
| 1616 | if (isAllZeros) { |
| 1617 | Replacement = ConstantAggregateZero::get(getType()); |
| 1618 | } else { |
| 1619 | // Check to see if we have this array type already. |
| 1620 | bool Exists; |
| 1621 | StructConstantsTy::MapIterator I = |
Chris Lattner | 3b793c6 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 1622 | StructConstants.InsertOrGetItem(Lookup, Exists); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1623 | |
| 1624 | if (Exists) { |
| 1625 | Replacement = I->second; |
| 1626 | } else { |
| 1627 | // Okay, the new shape doesn't exist in the system yet. Instead of |
| 1628 | // creating a new constant struct, inserting it, replaceallusesof'ing the |
| 1629 | // old with the new, then deleting the old... just update the current one |
| 1630 | // in place! |
Chris Lattner | 3b793c6 | 2005-10-04 21:35:50 +0000 | [diff] [blame] | 1631 | StructConstants.MoveConstantToNewSlot(this, I); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1632 | |
Chris Lattner | dff5911 | 2005-10-04 18:47:09 +0000 | [diff] [blame] | 1633 | // Update to the new value. |
| 1634 | setOperand(OperandToUpdate, ToC); |
Chris Lattner | 8760ec7 | 2005-10-04 01:17:50 +0000 | [diff] [blame] | 1635 | return; |
| 1636 | } |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1639 | assert(Replacement != this && "I didn't contain From!"); |
| 1640 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1641 | // Everyone using this now uses the replacement. |
| 1642 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1643 | |
| 1644 | // Delete the old constant! |
| 1645 | destroyConstant(); |
| 1646 | } |
| 1647 | |
| 1648 | void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1649 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1650 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 1651 | |
| 1652 | std::vector<Constant*> Values; |
| 1653 | Values.reserve(getNumOperands()); // Build replacement array... |
| 1654 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1655 | Constant *Val = getOperand(i); |
| 1656 | if (Val == From) Val = cast<Constant>(To); |
| 1657 | Values.push_back(Val); |
| 1658 | } |
| 1659 | |
| 1660 | Constant *Replacement = ConstantPacked::get(getType(), Values); |
| 1661 | assert(Replacement != this && "I didn't contain From!"); |
| 1662 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1663 | // Everyone using this now uses the replacement. |
| 1664 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1665 | |
| 1666 | // Delete the old constant! |
| 1667 | destroyConstant(); |
| 1668 | } |
| 1669 | |
| 1670 | void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1671 | Use *U) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1672 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); |
| 1673 | Constant *To = cast<Constant>(ToV); |
| 1674 | |
| 1675 | Constant *Replacement = 0; |
| 1676 | if (getOpcode() == Instruction::GetElementPtr) { |
| 1677 | std::vector<Constant*> Indices; |
| 1678 | Constant *Pointer = getOperand(0); |
| 1679 | Indices.reserve(getNumOperands()-1); |
| 1680 | if (Pointer == From) Pointer = To; |
| 1681 | |
| 1682 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1683 | Constant *Val = getOperand(i); |
| 1684 | if (Val == From) Val = To; |
| 1685 | Indices.push_back(Val); |
| 1686 | } |
| 1687 | Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices); |
| 1688 | } else if (getOpcode() == Instruction::Cast) { |
| 1689 | assert(getOperand(0) == From && "Cast only has one use!"); |
| 1690 | Replacement = ConstantExpr::getCast(To, getType()); |
| 1691 | } else if (getOpcode() == Instruction::Select) { |
| 1692 | Constant *C1 = getOperand(0); |
| 1693 | Constant *C2 = getOperand(1); |
| 1694 | Constant *C3 = getOperand(2); |
| 1695 | if (C1 == From) C1 = To; |
| 1696 | if (C2 == From) C2 = To; |
| 1697 | if (C3 == From) C3 = To; |
| 1698 | Replacement = ConstantExpr::getSelect(C1, C2, C3); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1699 | } else if (getOpcode() == Instruction::ExtractElement) { |
| 1700 | Constant *C1 = getOperand(0); |
| 1701 | Constant *C2 = getOperand(1); |
| 1702 | if (C1 == From) C1 = To; |
| 1703 | if (C2 == From) C2 = To; |
| 1704 | Replacement = ConstantExpr::getExtractElement(C1, C2); |
Chris Lattner | a93b4b5 | 2006-04-08 05:09:48 +0000 | [diff] [blame^] | 1705 | } else if (getOpcode() == Instruction::InsertElement) { |
| 1706 | Constant *C1 = getOperand(0); |
| 1707 | Constant *C2 = getOperand(1); |
| 1708 | Constant *C3 = getOperand(1); |
| 1709 | if (C1 == From) C1 = To; |
| 1710 | if (C2 == From) C2 = To; |
| 1711 | if (C3 == From) C3 = To; |
| 1712 | Replacement = ConstantExpr::getInsertElement(C1, C2, C3); |
| 1713 | } else if (getOpcode() == Instruction::ShuffleVector) { |
| 1714 | Constant *C1 = getOperand(0); |
| 1715 | Constant *C2 = getOperand(1); |
| 1716 | Constant *C3 = getOperand(2); |
| 1717 | if (C1 == From) C1 = To; |
| 1718 | if (C2 == From) C2 = To; |
| 1719 | if (C3 == From) C3 = To; |
| 1720 | Replacement = ConstantExpr::getShuffleVector(C1, C2, C3); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1721 | } else if (getNumOperands() == 2) { |
| 1722 | Constant *C1 = getOperand(0); |
| 1723 | Constant *C2 = getOperand(1); |
| 1724 | if (C1 == From) C1 = To; |
| 1725 | if (C2 == From) C2 = To; |
| 1726 | Replacement = ConstantExpr::get(getOpcode(), C1, C2); |
| 1727 | } else { |
| 1728 | assert(0 && "Unknown ConstantExpr type!"); |
| 1729 | return; |
| 1730 | } |
| 1731 | |
| 1732 | assert(Replacement != this && "I didn't contain From!"); |
| 1733 | |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 1734 | // Everyone using this now uses the replacement. |
| 1735 | uncheckedReplaceAllUsesWith(Replacement); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 1736 | |
| 1737 | // Delete the old constant! |
| 1738 | destroyConstant(); |
| 1739 | } |
| 1740 | |
| 1741 | |
| 1742 | |
Chris Lattner | 99a669b | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 1743 | /// clearAllValueMaps - This method frees all internal memory used by the |
| 1744 | /// constant subsystem, which can be used in environments where this memory |
| 1745 | /// is otherwise reported as a leak. |
| 1746 | void Constant::clearAllValueMaps() { |
| 1747 | std::vector<Constant *> Constants; |
| 1748 | |
| 1749 | DoubleConstants.clear(Constants); |
| 1750 | FloatConstants.clear(Constants); |
| 1751 | SIntConstants.clear(Constants); |
| 1752 | UIntConstants.clear(Constants); |
| 1753 | AggZeroConstants.clear(Constants); |
| 1754 | ArrayConstants.clear(Constants); |
| 1755 | StructConstants.clear(Constants); |
| 1756 | PackedConstants.clear(Constants); |
| 1757 | NullPtrConstants.clear(Constants); |
| 1758 | UndefValueConstants.clear(Constants); |
| 1759 | ExprConstants.clear(Constants); |
| 1760 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1761 | for (std::vector<Constant *>::iterator I = Constants.begin(), |
Chris Lattner | 99a669b | 2004-11-19 16:39:44 +0000 | [diff] [blame] | 1762 | E = Constants.end(); I != E; ++I) |
| 1763 | (*I)->dropAllReferences(); |
| 1764 | for (std::vector<Constant *>::iterator I = Constants.begin(), |
| 1765 | E = Constants.end(); I != E; ++I) |
| 1766 | (*I)->destroyConstantImpl(); |
| 1767 | Constants.clear(); |
| 1768 | } |
Jim Laskey | 2698f0d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 1769 | |
| 1770 | /// getStringValue - Turn an LLVM constant pointer that eventually points to a |
| 1771 | /// global into a string value. Return an empty string if we can't do it. |
Evan Cheng | 38280c0 | 2006-03-10 23:52:03 +0000 | [diff] [blame] | 1772 | /// Parameter Chop determines if the result is chopped at the first null |
| 1773 | /// terminator. |
Jim Laskey | 2698f0d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 1774 | /// |
Evan Cheng | 38280c0 | 2006-03-10 23:52:03 +0000 | [diff] [blame] | 1775 | std::string Constant::getStringValue(bool Chop, unsigned Offset) { |
Jim Laskey | 2698f0d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 1776 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) { |
| 1777 | if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) { |
| 1778 | ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); |
| 1779 | if (Init->isString()) { |
| 1780 | std::string Result = Init->getAsString(); |
| 1781 | if (Offset < Result.size()) { |
| 1782 | // If we are pointing INTO The string, erase the beginning... |
| 1783 | Result.erase(Result.begin(), Result.begin()+Offset); |
| 1784 | |
| 1785 | // Take off the null terminator, and any string fragments after it. |
Evan Cheng | 38280c0 | 2006-03-10 23:52:03 +0000 | [diff] [blame] | 1786 | if (Chop) { |
| 1787 | std::string::size_type NullPos = Result.find_first_of((char)0); |
| 1788 | if (NullPos != std::string::npos) |
| 1789 | Result.erase(Result.begin()+NullPos, Result.end()); |
| 1790 | } |
Jim Laskey | 2698f0d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 1791 | return Result; |
| 1792 | } |
| 1793 | } |
| 1794 | } |
| 1795 | } else if (Constant *C = dyn_cast<Constant>(this)) { |
| 1796 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Evan Cheng | 2c5e530 | 2006-03-11 00:13:10 +0000 | [diff] [blame] | 1797 | return GV->getStringValue(Chop, Offset); |
Jim Laskey | 2698f0d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 1798 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 1799 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 1800 | // Turn a gep into the specified offset. |
| 1801 | if (CE->getNumOperands() == 3 && |
| 1802 | cast<Constant>(CE->getOperand(1))->isNullValue() && |
| 1803 | isa<ConstantInt>(CE->getOperand(2))) { |
| 1804 | Offset += cast<ConstantInt>(CE->getOperand(2))->getRawValue(); |
Evan Cheng | 2c5e530 | 2006-03-11 00:13:10 +0000 | [diff] [blame] | 1805 | return CE->getOperand(0)->getStringValue(Chop, Offset); |
Jim Laskey | 2698f0d | 2006-03-08 18:11:07 +0000 | [diff] [blame] | 1806 | } |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | return ""; |
| 1811 | } |
| 1812 | |