Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1 | //===-- Constants.cpp - Implement Constant nodes --------------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 10 | // This file implements the Constant* classes... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | cbfd406 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 15 | #include "ConstantFolding.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Reid Spencer | 1c9c8e6 | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalValue.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | #include "llvm/SymbolTable.h" |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 21 | #include "Support/StringExtras.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Reid Spencer | 57f6efc | 2004-07-04 11:51:24 +0000 | [diff] [blame] | 23 | #include <iostream> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 26 | ConstantBool *ConstantBool::True = new ConstantBool(true); |
| 27 | ConstantBool *ConstantBool::False = new ConstantBool(false); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 31 | // Constant Class |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | // Specialize setName to take care of symbol table majik |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 35 | void Constant::setName(const std::string &Name, SymbolTable *ST) { |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 36 | assert(ST && "Type::setName - Must provide symbol table argument!"); |
| 37 | |
| 38 | if (Name.size()) ST->insert(Name, this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 41 | void Constant::destroyConstantImpl() { |
| 42 | // When a Constant is destroyed, there may be lingering |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 43 | // references to the constant by other constants in the constant pool. These |
Misha Brukman | ef6a6a6 | 2003-08-21 22:14:26 +0000 | [diff] [blame] | 44 | // constants are implicitly dependent on the module that is being deleted, |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 45 | // but they don't know that. Because we only find out when the CPV is |
| 46 | // deleted, we must now notify all of our users (that should only be |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 47 | // Constants) that they are, in fact, invalid now and should be deleted. |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 48 | // |
| 49 | while (!use_empty()) { |
| 50 | Value *V = use_back(); |
| 51 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | 6183b92 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 52 | if (!isa<Constant>(V)) |
| 53 | std::cerr << "While deleting: " << *this |
| 54 | << "\n\nUse still stuck around after Def is destroyed: " |
| 55 | << *V << "\n\n"; |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 56 | #endif |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 57 | assert(isa<Constant>(V) && "References remain to Constant being destroyed"); |
Reid Spencer | 1c9c8e6 | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 58 | Constant *CV = cast<Constant>(V); |
| 59 | CV->destroyConstant(); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 60 | |
| 61 | // The constant should remove itself from our use list... |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 62 | assert((use_empty() || use_back() != V) && "Constant not removed!"); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Value has no outstanding references it is safe to delete it now... |
| 66 | delete this; |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 67 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 69 | // Static constructor to create a '0' constant of arbitrary type... |
| 70 | Constant *Constant::getNullValue(const Type *Ty) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 71 | switch (Ty->getTypeID()) { |
Chris Lattner | 70b06fc | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 72 | case Type::BoolTyID: { |
| 73 | static Constant *NullBool = ConstantBool::get(false); |
| 74 | return NullBool; |
| 75 | } |
| 76 | case Type::SByteTyID: { |
| 77 | static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0); |
| 78 | return NullSByte; |
| 79 | } |
| 80 | case Type::UByteTyID: { |
| 81 | static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0); |
| 82 | return NullUByte; |
| 83 | } |
| 84 | case Type::ShortTyID: { |
| 85 | static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0); |
| 86 | return NullShort; |
| 87 | } |
| 88 | case Type::UShortTyID: { |
| 89 | static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0); |
| 90 | return NullUShort; |
| 91 | } |
| 92 | case Type::IntTyID: { |
| 93 | static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0); |
| 94 | return NullInt; |
| 95 | } |
| 96 | case Type::UIntTyID: { |
| 97 | static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0); |
| 98 | return NullUInt; |
| 99 | } |
| 100 | case Type::LongTyID: { |
| 101 | static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0); |
| 102 | return NullLong; |
| 103 | } |
| 104 | case Type::ULongTyID: { |
| 105 | static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0); |
| 106 | return NullULong; |
| 107 | } |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 70b06fc | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 109 | case Type::FloatTyID: { |
| 110 | static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0); |
| 111 | return NullFloat; |
| 112 | } |
| 113 | case Type::DoubleTyID: { |
| 114 | static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0); |
| 115 | return NullDouble; |
| 116 | } |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 117 | |
| 118 | case Type::PointerTyID: |
| 119 | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
Chris Lattner | 70b06fc | 2003-10-03 19:34:51 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 121 | case Type::StructTyID: |
| 122 | case Type::ArrayTyID: |
| 123 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 124 | default: |
Reid Spencer | 57f6efc | 2004-07-04 11:51:24 +0000 | [diff] [blame] | 125 | // Function, Label, or Opaque type? |
| 126 | assert(!"Cannot create a null constant of that type!"); |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Static constructor to create the maximum constant of an integral type... |
| 132 | ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 133 | switch (Ty->getTypeID()) { |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 134 | case Type::BoolTyID: return ConstantBool::True; |
| 135 | case Type::SByteTyID: |
| 136 | case Type::ShortTyID: |
| 137 | case Type::IntTyID: |
| 138 | case Type::LongTyID: { |
| 139 | // Calculate 011111111111111... |
| 140 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 141 | int64_t Val = INT64_MAX; // All ones |
| 142 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 143 | return ConstantSInt::get(Ty, Val); |
| 144 | } |
| 145 | |
| 146 | case Type::UByteTyID: |
| 147 | case Type::UShortTyID: |
| 148 | case Type::UIntTyID: |
| 149 | case Type::ULongTyID: return getAllOnesValue(Ty); |
| 150 | |
Chris Lattner | 227b86c | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 151 | default: return 0; |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | // Static constructor to create the minimum constant for an integral type... |
| 156 | ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 157 | switch (Ty->getTypeID()) { |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 158 | case Type::BoolTyID: return ConstantBool::False; |
| 159 | case Type::SByteTyID: |
| 160 | case Type::ShortTyID: |
| 161 | case Type::IntTyID: |
| 162 | case Type::LongTyID: { |
| 163 | // Calculate 1111111111000000000000 |
| 164 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 165 | int64_t Val = -1; // All ones |
| 166 | Val <<= TypeBits-1; // Shift over to the right spot |
| 167 | return ConstantSInt::get(Ty, Val); |
| 168 | } |
| 169 | |
| 170 | case Type::UByteTyID: |
| 171 | case Type::UShortTyID: |
| 172 | case Type::UIntTyID: |
| 173 | case Type::ULongTyID: return ConstantUInt::get(Ty, 0); |
| 174 | |
Chris Lattner | 227b86c | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 175 | default: return 0; |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | // Static constructor to create an integral constant with all bits set |
| 180 | ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 181 | switch (Ty->getTypeID()) { |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 182 | case Type::BoolTyID: return ConstantBool::True; |
| 183 | case Type::SByteTyID: |
| 184 | case Type::ShortTyID: |
| 185 | case Type::IntTyID: |
| 186 | case Type::LongTyID: return ConstantSInt::get(Ty, -1); |
| 187 | |
| 188 | case Type::UByteTyID: |
| 189 | case Type::UShortTyID: |
| 190 | case Type::UIntTyID: |
| 191 | case Type::ULongTyID: { |
| 192 | // Calculate ~0 of the right type... |
| 193 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 194 | uint64_t Val = ~0ULL; // All ones |
| 195 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 196 | return ConstantUInt::get(Ty, Val); |
| 197 | } |
Chris Lattner | 227b86c | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 198 | default: return 0; |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Chris Lattner | 1680312 | 2003-03-10 22:39:02 +0000 | [diff] [blame] | 202 | bool ConstantUInt::isAllOnesValue() const { |
| 203 | unsigned TypeBits = getType()->getPrimitiveSize()*8; |
| 204 | uint64_t Val = ~0ULL; // All ones |
| 205 | Val >>= 64-TypeBits; // Shift out inappropriate bits |
| 206 | return getValue() == Val; |
| 207 | } |
| 208 | |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 210 | //===----------------------------------------------------------------------===// |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 211 | // ConstantXXX Classes |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 212 | //===----------------------------------------------------------------------===// |
| 213 | |
| 214 | //===----------------------------------------------------------------------===// |
| 215 | // Normal Constructors |
| 216 | |
Chris Lattner | c2dfb8b | 2004-06-21 12:12:12 +0000 | [diff] [blame] | 217 | ConstantIntegral::ConstantIntegral(const Type *Ty, uint64_t V) |
| 218 | : Constant(Ty) { |
| 219 | Val.Unsigned = V; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 221 | |
Chris Lattner | c2dfb8b | 2004-06-21 12:12:12 +0000 | [diff] [blame] | 222 | ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy, V) { |
| 223 | } |
| 224 | |
| 225 | ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty, V) { |
Chris Lattner | d7fa0fd | 2001-07-21 19:16:08 +0000 | [diff] [blame] | 226 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 227 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 228 | ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) { |
Chris Lattner | 562219d | 2002-09-11 01:21:04 +0000 | [diff] [blame] | 229 | assert(Ty->isInteger() && Ty->isSigned() && |
| 230 | "Illegal type for unsigned integer constant!"); |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 231 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 234 | ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) { |
Chris Lattner | 562219d | 2002-09-11 01:21:04 +0000 | [diff] [blame] | 235 | assert(Ty->isInteger() && Ty->isUnsigned() && |
| 236 | "Illegal type for unsigned integer constant!"); |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 237 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 240 | ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) { |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 241 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 242 | Val = V; |
| 243 | } |
| 244 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 245 | ConstantArray::ConstantArray(const ArrayType *T, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 246 | const std::vector<Constant*> &V) : Constant(T) { |
Chris Lattner | d43c7d2 | 2002-10-08 23:33:52 +0000 | [diff] [blame] | 247 | Operands.reserve(V.size()); |
| 248 | for (unsigned i = 0, e = V.size(); i != e; ++i) { |
Chris Lattner | 8164d65 | 2003-08-18 16:54:48 +0000 | [diff] [blame] | 249 | assert(V[i]->getType() == T->getElementType() || |
| 250 | (T->isAbstract() && |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 251 | V[i]->getType()->getTypeID() == T->getElementType()->getTypeID())); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 252 | Operands.push_back(Use(V[i], this)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 256 | ConstantStruct::ConstantStruct(const StructType *T, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 257 | const std::vector<Constant*> &V) : Constant(T) { |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 258 | assert(V.size() == T->getNumElements() && |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 259 | "Invalid initializer vector for constant structure"); |
Chris Lattner | d43c7d2 | 2002-10-08 23:33:52 +0000 | [diff] [blame] | 260 | Operands.reserve(V.size()); |
| 261 | for (unsigned i = 0, e = V.size(); i != e; ++i) { |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 262 | assert((V[i]->getType() == T->getElementType(i) || |
| 263 | ((T->getElementType(i)->isAbstract() || |
| 264 | V[i]->getType()->isAbstract()) && |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 265 | T->getElementType(i)->getTypeID() == V[i]->getType()->getTypeID())) && |
Chris Lattner | b843889 | 2003-06-02 17:42:47 +0000 | [diff] [blame] | 266 | "Initializer for struct element doesn't match struct element type!"); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 267 | Operands.push_back(Use(V[i], this)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 271 | ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty) |
Chris Lattner | ab4c19e | 2004-07-19 00:59:10 +0000 | [diff] [blame] | 272 | : Constant(Ty, ConstantExprVal), iType(Opcode) { |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 273 | Operands.reserve(1); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 274 | Operands.push_back(Use(C, this)); |
| 275 | } |
| 276 | |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 277 | // Select instruction creation ctor |
| 278 | ConstantExpr::ConstantExpr(Constant *C, Constant *V1, Constant *V2) |
Chris Lattner | ab4c19e | 2004-07-19 00:59:10 +0000 | [diff] [blame] | 279 | : Constant(V1->getType(), ConstantExprVal), iType(Instruction::Select) { |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 280 | Operands.reserve(3); |
| 281 | Operands.push_back(Use(C, this)); |
| 282 | Operands.push_back(Use(V1, this)); |
| 283 | Operands.push_back(Use(V2, this)); |
| 284 | } |
| 285 | |
| 286 | |
Chris Lattner | 037d258 | 2003-06-22 20:48:30 +0000 | [diff] [blame] | 287 | static bool isSetCC(unsigned Opcode) { |
| 288 | return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE || |
| 289 | Opcode == Instruction::SetLT || Opcode == Instruction::SetGT || |
| 290 | Opcode == Instruction::SetLE || Opcode == Instruction::SetGE; |
| 291 | } |
| 292 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 293 | ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2) |
Chris Lattner | ab4c19e | 2004-07-19 00:59:10 +0000 | [diff] [blame] | 294 | : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType(), ConstantExprVal), |
| 295 | iType(Opcode) { |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 296 | Operands.reserve(2); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 297 | Operands.push_back(Use(C1, this)); |
| 298 | Operands.push_back(Use(C2, this)); |
| 299 | } |
| 300 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 301 | ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, |
| 302 | const Type *DestTy) |
Chris Lattner | ab4c19e | 2004-07-19 00:59:10 +0000 | [diff] [blame] | 303 | : Constant(DestTy, ConstantExprVal), iType(Instruction::GetElementPtr) { |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 304 | Operands.reserve(1+IdxList.size()); |
| 305 | Operands.push_back(Use(C, this)); |
| 306 | for (unsigned i = 0, E = IdxList.size(); i != E; ++i) |
| 307 | Operands.push_back(Use(IdxList[i], this)); |
| 308 | } |
| 309 | |
Chris Lattner | 4dcb540 | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 310 | /// ConstantExpr::get* - Return some common constants without having to |
| 311 | /// specify the full Instruction::OPCODE identifier. |
| 312 | /// |
| 313 | Constant *ConstantExpr::getNeg(Constant *C) { |
Chris Lattner | d25e3ec | 2004-03-29 19:51:24 +0000 | [diff] [blame] | 314 | if (!C->getType()->isFloatingPoint()) |
| 315 | return get(Instruction::Sub, getNullValue(C->getType()), C); |
| 316 | else |
| 317 | return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C); |
Chris Lattner | 4dcb540 | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 318 | } |
| 319 | Constant *ConstantExpr::getNot(Constant *C) { |
| 320 | assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!"); |
| 321 | return get(Instruction::Xor, C, |
| 322 | ConstantIntegral::getAllOnesValue(C->getType())); |
| 323 | } |
| 324 | Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) { |
| 325 | return get(Instruction::Add, C1, C2); |
| 326 | } |
| 327 | Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) { |
| 328 | return get(Instruction::Sub, C1, C2); |
| 329 | } |
| 330 | Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) { |
| 331 | return get(Instruction::Mul, C1, C2); |
| 332 | } |
| 333 | Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) { |
| 334 | return get(Instruction::Div, C1, C2); |
| 335 | } |
| 336 | Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) { |
| 337 | return get(Instruction::Rem, C1, C2); |
| 338 | } |
| 339 | Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) { |
| 340 | return get(Instruction::And, C1, C2); |
| 341 | } |
| 342 | Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) { |
| 343 | return get(Instruction::Or, C1, C2); |
| 344 | } |
| 345 | Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { |
| 346 | return get(Instruction::Xor, C1, C2); |
| 347 | } |
| 348 | Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) { |
| 349 | return get(Instruction::SetEQ, C1, C2); |
| 350 | } |
| 351 | Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) { |
| 352 | return get(Instruction::SetNE, C1, C2); |
| 353 | } |
| 354 | Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) { |
| 355 | return get(Instruction::SetLT, C1, C2); |
| 356 | } |
| 357 | Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) { |
| 358 | return get(Instruction::SetGT, C1, C2); |
| 359 | } |
| 360 | Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) { |
| 361 | return get(Instruction::SetLE, C1, C2); |
| 362 | } |
| 363 | Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) { |
| 364 | return get(Instruction::SetGE, C1, C2); |
| 365 | } |
| 366 | Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) { |
| 367 | return get(Instruction::Shl, C1, C2); |
| 368 | } |
| 369 | Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) { |
| 370 | return get(Instruction::Shr, C1, C2); |
| 371 | } |
| 372 | |
Chris Lattner | c971025 | 2004-05-25 05:32:43 +0000 | [diff] [blame] | 373 | Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) { |
| 374 | if (C1->getType()->isUnsigned()) return getShr(C1, C2); |
| 375 | return getCast(getShr(getCast(C1, |
| 376 | C1->getType()->getUnsignedVersion()), C2), C1->getType()); |
| 377 | } |
Chris Lattner | 4dcb540 | 2004-03-29 02:37:53 +0000 | [diff] [blame] | 378 | |
Chris Lattner | c971025 | 2004-05-25 05:32:43 +0000 | [diff] [blame] | 379 | Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) { |
| 380 | if (C1->getType()->isSigned()) return getShr(C1, C2); |
| 381 | return getCast(getShr(getCast(C1, |
| 382 | C1->getType()->getSignedVersion()), C2), C1->getType()); |
| 383 | } |
Chris Lattner | f4ba6c7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 385 | |
| 386 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 387 | // isValueValidForType implementations |
| 388 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 389 | bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 390 | switch (Ty->getTypeID()) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 391 | default: |
| 392 | return false; // These can't be represented as integers!!! |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 393 | // Signed types... |
| 394 | case Type::SByteTyID: |
| 395 | return (Val <= INT8_MAX && Val >= INT8_MIN); |
| 396 | case Type::ShortTyID: |
| 397 | return (Val <= INT16_MAX && Val >= INT16_MIN); |
| 398 | case Type::IntTyID: |
Chris Lattner | 2a50201 | 2004-06-08 23:21:39 +0000 | [diff] [blame] | 399 | return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 400 | case Type::LongTyID: |
| 401 | return true; // This is the largest type... |
| 402 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 405 | bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 406 | switch (Ty->getTypeID()) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 407 | default: |
| 408 | return false; // These can't be represented as integers!!! |
| 409 | |
| 410 | // Unsigned types... |
| 411 | case Type::UByteTyID: |
| 412 | return (Val <= UINT8_MAX); |
| 413 | case Type::UShortTyID: |
| 414 | return (Val <= UINT16_MAX); |
| 415 | case Type::UIntTyID: |
| 416 | return (Val <= UINT32_MAX); |
| 417 | case Type::ULongTyID: |
| 418 | return true; // This is the largest type... |
| 419 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 422 | bool ConstantFP::isValueValidForType(const Type *Ty, double Val) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 423 | switch (Ty->getTypeID()) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 424 | default: |
| 425 | return false; // These can't be represented as floating point! |
| 426 | |
| 427 | // TODO: Figure out how to test if a double can be cast to a float! |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 428 | case Type::FloatTyID: |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 429 | case Type::DoubleTyID: |
| 430 | return true; // This is the largest type... |
| 431 | } |
| 432 | }; |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 433 | |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 434 | //===----------------------------------------------------------------------===// |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 435 | // replaceUsesOfWithOnConstant implementations |
| 436 | |
Chris Lattner | 23f3a49 | 2003-08-29 05:36:46 +0000 | [diff] [blame] | 437 | void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, |
| 438 | bool DisableChecking) { |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 439 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 440 | |
| 441 | std::vector<Constant*> Values; |
Alkis Evlogimenos | 9e4c681 | 2004-08-04 08:02:59 +0000 | [diff] [blame] | 442 | Values.reserve(getNumOperands()); // Build replacement array... |
| 443 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 444 | Constant *Val = getOperand(i); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 445 | if (Val == From) Val = cast<Constant>(To); |
| 446 | Values.push_back(Val); |
| 447 | } |
| 448 | |
Chris Lattner | 3710644 | 2004-02-15 04:05:58 +0000 | [diff] [blame] | 449 | Constant *Replacement = ConstantArray::get(getType(), Values); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 450 | assert(Replacement != this && "I didn't contain From!"); |
| 451 | |
| 452 | // Everyone using this now uses the replacement... |
Chris Lattner | 23f3a49 | 2003-08-29 05:36:46 +0000 | [diff] [blame] | 453 | if (DisableChecking) |
| 454 | uncheckedReplaceAllUsesWith(Replacement); |
| 455 | else |
| 456 | replaceAllUsesWith(Replacement); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 457 | |
| 458 | // Delete the old constant! |
| 459 | destroyConstant(); |
| 460 | } |
| 461 | |
Chris Lattner | 23f3a49 | 2003-08-29 05:36:46 +0000 | [diff] [blame] | 462 | void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, |
| 463 | bool DisableChecking) { |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 464 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 465 | |
| 466 | std::vector<Constant*> Values; |
Alkis Evlogimenos | 9e4c681 | 2004-08-04 08:02:59 +0000 | [diff] [blame] | 467 | Values.reserve(getNumOperands()); // Build replacement array... |
| 468 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 469 | Constant *Val = getOperand(i); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 470 | if (Val == From) Val = cast<Constant>(To); |
| 471 | Values.push_back(Val); |
| 472 | } |
| 473 | |
Chris Lattner | 04d1fb6 | 2004-02-15 04:07:32 +0000 | [diff] [blame] | 474 | Constant *Replacement = ConstantStruct::get(getType(), Values); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 475 | assert(Replacement != this && "I didn't contain From!"); |
| 476 | |
| 477 | // Everyone using this now uses the replacement... |
Chris Lattner | 23f3a49 | 2003-08-29 05:36:46 +0000 | [diff] [blame] | 478 | if (DisableChecking) |
| 479 | uncheckedReplaceAllUsesWith(Replacement); |
| 480 | else |
| 481 | replaceAllUsesWith(Replacement); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 482 | |
| 483 | // Delete the old constant! |
| 484 | destroyConstant(); |
| 485 | } |
| 486 | |
Chris Lattner | 23f3a49 | 2003-08-29 05:36:46 +0000 | [diff] [blame] | 487 | void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, |
| 488 | bool DisableChecking) { |
Chris Lattner | 3b9922f | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 489 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); |
| 490 | Constant *To = cast<Constant>(ToV); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 491 | |
Chris Lattner | fb242b6 | 2003-04-16 22:40:51 +0000 | [diff] [blame] | 492 | Constant *Replacement = 0; |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 493 | if (getOpcode() == Instruction::GetElementPtr) { |
| 494 | std::vector<Constant*> Indices; |
Chris Lattner | 3b9922f | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 495 | Constant *Pointer = getOperand(0); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 496 | Indices.reserve(getNumOperands()-1); |
Chris Lattner | 3b9922f | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 497 | if (Pointer == From) Pointer = To; |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 498 | |
| 499 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Chris Lattner | 3b9922f | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 500 | Constant *Val = getOperand(i); |
| 501 | if (Val == From) Val = To; |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 502 | Indices.push_back(Val); |
| 503 | } |
| 504 | Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices); |
| 505 | } else if (getOpcode() == Instruction::Cast) { |
| 506 | assert(getOperand(0) == From && "Cast only has one use!"); |
Chris Lattner | 3b9922f | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 507 | Replacement = ConstantExpr::getCast(To, getType()); |
Chris Lattner | 4b2f08a | 2004-03-31 02:56:11 +0000 | [diff] [blame] | 508 | } else if (getOpcode() == Instruction::Select) { |
| 509 | Constant *C1 = getOperand(0); |
| 510 | Constant *C2 = getOperand(1); |
| 511 | Constant *C3 = getOperand(2); |
| 512 | if (C1 == From) C1 = To; |
| 513 | if (C2 == From) C2 = To; |
| 514 | if (C3 == From) C3 = To; |
| 515 | Replacement = ConstantExpr::getSelect(C1, C2, C3); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 516 | } else if (getNumOperands() == 2) { |
Chris Lattner | 3b9922f | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 517 | Constant *C1 = getOperand(0); |
| 518 | Constant *C2 = getOperand(1); |
| 519 | if (C1 == From) C1 = To; |
| 520 | if (C2 == From) C2 = To; |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 521 | Replacement = ConstantExpr::get(getOpcode(), C1, C2); |
| 522 | } else { |
| 523 | assert(0 && "Unknown ConstantExpr type!"); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | assert(Replacement != this && "I didn't contain From!"); |
| 528 | |
| 529 | // Everyone using this now uses the replacement... |
Chris Lattner | 23f3a49 | 2003-08-29 05:36:46 +0000 | [diff] [blame] | 530 | if (DisableChecking) |
| 531 | uncheckedReplaceAllUsesWith(Replacement); |
| 532 | else |
| 533 | replaceAllUsesWith(Replacement); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 534 | |
| 535 | // Delete the old constant! |
| 536 | destroyConstant(); |
| 537 | } |
| 538 | |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 539 | //===----------------------------------------------------------------------===// |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 540 | // Factory Function Implementation |
| 541 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 542 | // ConstantCreator - A class that is used to create constants by |
| 543 | // ValueMap*. This class should be partially specialized if there is |
| 544 | // something strange that needs to be done to interface to the ctor for the |
| 545 | // constant. |
| 546 | // |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 547 | namespace llvm { |
| 548 | template<class ConstantClass, class TypeClass, class ValType> |
| 549 | struct ConstantCreator { |
| 550 | static ConstantClass *create(const TypeClass *Ty, const ValType &V) { |
| 551 | return new ConstantClass(Ty, V); |
| 552 | } |
| 553 | }; |
| 554 | |
| 555 | template<class ConstantClass, class TypeClass> |
| 556 | struct ConvertConstantType { |
| 557 | static void convert(ConstantClass *OldC, const TypeClass *NewTy) { |
| 558 | assert(0 && "This type cannot be converted!\n"); |
| 559 | abort(); |
| 560 | } |
| 561 | }; |
| 562 | } |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 563 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 564 | namespace { |
| 565 | template<class ValType, class TypeClass, class ConstantClass> |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 566 | class ValueMap : public AbstractTypeUser { |
| 567 | typedef std::pair<const TypeClass*, ValType> MapKey; |
| 568 | typedef std::map<MapKey, ConstantClass *> MapTy; |
| 569 | typedef typename MapTy::iterator MapIterator; |
| 570 | MapTy Map; |
| 571 | |
| 572 | typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy; |
| 573 | AbstractTypeMapTy AbstractTypeMap; |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 574 | public: |
| 575 | // getOrCreate - Return the specified constant from the map, creating it if |
| 576 | // necessary. |
| 577 | ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) { |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 578 | MapKey Lookup(Ty, V); |
| 579 | MapIterator I = Map.lower_bound(Lookup); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 580 | if (I != Map.end() && I->first == Lookup) |
| 581 | return I->second; // Is it in the map? |
| 582 | |
| 583 | // If no preexisting value, create one now... |
| 584 | ConstantClass *Result = |
| 585 | ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V); |
| 586 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 587 | |
| 588 | /// FIXME: why does this assert fail when loading 176.gcc? |
| 589 | //assert(Result->getType() == Ty && "Type specified is not correct!"); |
| 590 | I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result)); |
| 591 | |
| 592 | // If the type of the constant is abstract, make sure that an entry exists |
| 593 | // for it in the AbstractTypeMap. |
| 594 | if (Ty->isAbstract()) { |
| 595 | typename AbstractTypeMapTy::iterator TI = |
| 596 | AbstractTypeMap.lower_bound(Ty); |
| 597 | |
| 598 | if (TI == AbstractTypeMap.end() || TI->first != Ty) { |
| 599 | // Add ourselves to the ATU list of the type. |
| 600 | cast<DerivedType>(Ty)->addAbstractTypeUser(this); |
| 601 | |
| 602 | AbstractTypeMap.insert(TI, std::make_pair(Ty, I)); |
| 603 | } |
| 604 | } |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 605 | return Result; |
| 606 | } |
| 607 | |
| 608 | void remove(ConstantClass *CP) { |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 609 | MapIterator I = Map.find(MapKey((TypeClass*)CP->getRawType(), |
| 610 | getValType(CP))); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 611 | assert(I != Map.end() && "Constant not found in constant table!"); |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 612 | assert(I->second == CP && "Didn't find correct element?"); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 613 | |
| 614 | // Now that we found the entry, make sure this isn't the entry that |
| 615 | // the AbstractTypeMap points to. |
| 616 | const TypeClass *Ty = I->first.first; |
| 617 | if (Ty->isAbstract()) { |
| 618 | assert(AbstractTypeMap.count(Ty) && |
| 619 | "Abstract type not in AbstractTypeMap?"); |
| 620 | MapIterator &ATMEntryIt = AbstractTypeMap[Ty]; |
| 621 | if (ATMEntryIt == I) { |
| 622 | // Yes, we are removing the representative entry for this type. |
| 623 | // See if there are any other entries of the same type. |
| 624 | MapIterator TmpIt = ATMEntryIt; |
| 625 | |
| 626 | // First check the entry before this one... |
| 627 | if (TmpIt != Map.begin()) { |
| 628 | --TmpIt; |
| 629 | if (TmpIt->first.first != Ty) // Not the same type, move back... |
| 630 | ++TmpIt; |
| 631 | } |
| 632 | |
| 633 | // If we didn't find the same type, try to move forward... |
| 634 | if (TmpIt == ATMEntryIt) { |
| 635 | ++TmpIt; |
| 636 | if (TmpIt == Map.end() || TmpIt->first.first != Ty) |
| 637 | --TmpIt; // No entry afterwards with the same type |
| 638 | } |
| 639 | |
| 640 | // If there is another entry in the map of the same abstract type, |
| 641 | // update the AbstractTypeMap entry now. |
| 642 | if (TmpIt != ATMEntryIt) { |
| 643 | ATMEntryIt = TmpIt; |
| 644 | } else { |
| 645 | // Otherwise, we are removing the last instance of this type |
| 646 | // from the table. Remove from the ATM, and from user list. |
| 647 | cast<DerivedType>(Ty)->removeAbstractTypeUser(this); |
| 648 | AbstractTypeMap.erase(Ty); |
| 649 | } |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 650 | } |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | Map.erase(I); |
| 654 | } |
| 655 | |
| 656 | void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) { |
| 657 | typename AbstractTypeMapTy::iterator I = |
| 658 | AbstractTypeMap.find(cast<TypeClass>(OldTy)); |
| 659 | |
| 660 | assert(I != AbstractTypeMap.end() && |
| 661 | "Abstract type not in AbstractTypeMap?"); |
| 662 | |
| 663 | // Convert a constant at a time until the last one is gone. The last one |
| 664 | // leaving will remove() itself, causing the AbstractTypeMapEntry to be |
| 665 | // eliminated eventually. |
| 666 | do { |
| 667 | ConvertConstantType<ConstantClass, |
| 668 | TypeClass>::convert(I->second->second, |
| 669 | cast<TypeClass>(NewTy)); |
| 670 | |
| 671 | I = AbstractTypeMap.find(cast<TypeClass>(OldTy)); |
| 672 | } while (I != AbstractTypeMap.end()); |
| 673 | } |
| 674 | |
| 675 | // If the type became concrete without being refined to any other existing |
| 676 | // type, we just remove ourselves from the ATU list. |
| 677 | void typeBecameConcrete(const DerivedType *AbsTy) { |
| 678 | AbsTy->removeAbstractTypeUser(this); |
| 679 | } |
| 680 | |
| 681 | void dump() const { |
| 682 | std::cerr << "Constant.cpp: ValueMap\n"; |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 683 | } |
| 684 | }; |
| 685 | } |
| 686 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 687 | //---- ConstantUInt::get() and ConstantSInt::get() implementations... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 688 | // |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 689 | static ValueMap< int64_t, Type, ConstantSInt> SIntConstants; |
| 690 | static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants; |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 691 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 692 | ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) { |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 693 | return SIntConstants.getOrCreate(Ty, V); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 696 | ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) { |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 697 | return UIntConstants.getOrCreate(Ty, V); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 700 | ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) { |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 701 | assert(V <= 127 && "Can only be used with very small positive constants!"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 702 | if (Ty->isSigned()) return ConstantSInt::get(Ty, V); |
| 703 | return ConstantUInt::get(Ty, V); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 706 | //---- ConstantFP::get() implementation... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 707 | // |
Chris Lattner | cfdf241 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 708 | namespace llvm { |
| 709 | template<> |
| 710 | struct ConstantCreator<ConstantFP, Type, uint64_t> { |
| 711 | static ConstantFP *create(const Type *Ty, uint64_t V) { |
| 712 | assert(Ty == Type::DoubleTy); |
| 713 | union { |
| 714 | double F; |
| 715 | uint64_t I; |
| 716 | } T; |
| 717 | T.I = V; |
| 718 | return new ConstantFP(Ty, T.F); |
| 719 | } |
| 720 | }; |
| 721 | template<> |
| 722 | struct ConstantCreator<ConstantFP, Type, uint32_t> { |
| 723 | static ConstantFP *create(const Type *Ty, uint32_t V) { |
| 724 | assert(Ty == Type::FloatTy); |
| 725 | union { |
| 726 | float F; |
| 727 | uint32_t I; |
| 728 | } T; |
| 729 | T.I = V; |
| 730 | return new ConstantFP(Ty, T.F); |
| 731 | } |
| 732 | }; |
| 733 | } |
| 734 | |
| 735 | static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants; |
| 736 | static ValueMap<uint32_t, Type, ConstantFP> FloatConstants; |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 737 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 738 | ConstantFP *ConstantFP::get(const Type *Ty, double V) { |
Chris Lattner | 90fb19e | 2004-01-23 00:55:21 +0000 | [diff] [blame] | 739 | if (Ty == Type::FloatTy) { |
| 740 | // Force the value through memory to normalize it. |
Chris Lattner | cfdf241 | 2004-02-01 22:49:04 +0000 | [diff] [blame] | 741 | union { |
| 742 | float F; |
| 743 | uint32_t I; |
| 744 | } T; |
| 745 | T.F = (float)V; |
| 746 | return FloatConstants.getOrCreate(Ty, T.I); |
| 747 | } else { |
| 748 | assert(Ty == Type::DoubleTy); |
| 749 | union { |
| 750 | double F; |
| 751 | uint64_t I; |
| 752 | } T; |
| 753 | T.F = V; |
| 754 | return DoubleConstants.getOrCreate(Ty, T.I); |
Chris Lattner | 90fb19e | 2004-01-23 00:55:21 +0000 | [diff] [blame] | 755 | } |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 758 | //---- ConstantAggregateZero::get() implementation... |
| 759 | // |
| 760 | namespace llvm { |
| 761 | // ConstantAggregateZero does not take extra "value" argument... |
| 762 | template<class ValType> |
| 763 | struct ConstantCreator<ConstantAggregateZero, Type, ValType> { |
| 764 | static ConstantAggregateZero *create(const Type *Ty, const ValType &V){ |
| 765 | return new ConstantAggregateZero(Ty); |
| 766 | } |
| 767 | }; |
| 768 | |
| 769 | template<> |
| 770 | struct ConvertConstantType<ConstantAggregateZero, Type> { |
| 771 | static void convert(ConstantAggregateZero *OldC, const Type *NewTy) { |
| 772 | // Make everyone now use a constant of the new type... |
| 773 | Constant *New = ConstantAggregateZero::get(NewTy); |
| 774 | assert(New != OldC && "Didn't replace constant??"); |
| 775 | OldC->uncheckedReplaceAllUsesWith(New); |
| 776 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 777 | } |
| 778 | }; |
| 779 | } |
| 780 | |
| 781 | static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants; |
| 782 | |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 783 | static char getValType(ConstantAggregateZero *CPZ) { return 0; } |
| 784 | |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 785 | Constant *ConstantAggregateZero::get(const Type *Ty) { |
| 786 | return AggZeroConstants.getOrCreate(Ty, 0); |
| 787 | } |
| 788 | |
| 789 | // destroyConstant - Remove the constant from the constant table... |
| 790 | // |
| 791 | void ConstantAggregateZero::destroyConstant() { |
| 792 | AggZeroConstants.remove(this); |
| 793 | destroyConstantImpl(); |
| 794 | } |
| 795 | |
| 796 | void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To, |
| 797 | bool DisableChecking) { |
| 798 | assert(0 && "No uses!"); |
| 799 | abort(); |
| 800 | } |
| 801 | |
| 802 | |
| 803 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 804 | //---- ConstantArray::get() implementation... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 805 | // |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 806 | namespace llvm { |
| 807 | template<> |
| 808 | struct ConvertConstantType<ConstantArray, ArrayType> { |
| 809 | static void convert(ConstantArray *OldC, const ArrayType *NewTy) { |
| 810 | // Make everyone now use a constant of the new type... |
| 811 | std::vector<Constant*> C; |
| 812 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 813 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
| 814 | Constant *New = ConstantArray::get(NewTy, C); |
| 815 | assert(New != OldC && "Didn't replace constant??"); |
| 816 | OldC->uncheckedReplaceAllUsesWith(New); |
| 817 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 818 | } |
| 819 | }; |
| 820 | } |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 821 | |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 822 | static std::vector<Constant*> getValType(ConstantArray *CA) { |
| 823 | std::vector<Constant*> Elements; |
| 824 | Elements.reserve(CA->getNumOperands()); |
| 825 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
| 826 | Elements.push_back(cast<Constant>(CA->getOperand(i))); |
| 827 | return Elements; |
| 828 | } |
| 829 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 830 | static ValueMap<std::vector<Constant*>, ArrayType, |
| 831 | ConstantArray> ArrayConstants; |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 832 | |
Chris Lattner | ca705fa | 2004-02-15 04:14:47 +0000 | [diff] [blame] | 833 | Constant *ConstantArray::get(const ArrayType *Ty, |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 834 | const std::vector<Constant*> &V) { |
| 835 | // If this is an all-zero array, return a ConstantAggregateZero object |
| 836 | if (!V.empty()) { |
| 837 | Constant *C = V[0]; |
| 838 | if (!C->isNullValue()) |
| 839 | return ArrayConstants.getOrCreate(Ty, V); |
| 840 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 841 | if (V[i] != C) |
| 842 | return ArrayConstants.getOrCreate(Ty, V); |
| 843 | } |
| 844 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 847 | // destroyConstant - Remove the constant from the constant table... |
| 848 | // |
| 849 | void ConstantArray::destroyConstant() { |
| 850 | ArrayConstants.remove(this); |
| 851 | destroyConstantImpl(); |
| 852 | } |
| 853 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 854 | // ConstantArray::get(const string&) - Return an array that is initialized to |
Chris Lattner | c5bdb24 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 855 | // contain the specified string. A null terminator is added to the specified |
| 856 | // string so that it may be used in a natural way... |
| 857 | // |
Chris Lattner | ca705fa | 2004-02-15 04:14:47 +0000 | [diff] [blame] | 858 | Constant *ConstantArray::get(const std::string &Str) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 859 | std::vector<Constant*> ElementVals; |
Chris Lattner | c5bdb24 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 860 | |
| 861 | for (unsigned i = 0; i < Str.length(); ++i) |
Chris Lattner | a2644f6 | 2001-12-14 16:41:18 +0000 | [diff] [blame] | 862 | ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i])); |
Chris Lattner | c5bdb24 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 863 | |
| 864 | // Add a null terminator to the string... |
Chris Lattner | a2644f6 | 2001-12-14 16:41:18 +0000 | [diff] [blame] | 865 | ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0)); |
Chris Lattner | c5bdb24 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 866 | |
Chris Lattner | a2644f6 | 2001-12-14 16:41:18 +0000 | [diff] [blame] | 867 | ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 868 | return ConstantArray::get(ATy, ElementVals); |
Vikram S. Adve | db2da49 | 2001-10-14 23:17:20 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Chris Lattner | 13cfdea | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 871 | /// isString - This method returns true if the array is an array of sbyte or |
| 872 | /// ubyte, and if the elements of the array are all ConstantInt's. |
| 873 | bool ConstantArray::isString() const { |
| 874 | // Check the element type for sbyte or ubyte... |
Chris Lattner | 7aa162b | 2004-01-14 17:51:53 +0000 | [diff] [blame] | 875 | if (getType()->getElementType() != Type::UByteTy && |
Chris Lattner | 13cfdea | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 876 | getType()->getElementType() != Type::SByteTy) |
| 877 | return false; |
| 878 | // Check the elements to make sure they are all integers, not constant |
| 879 | // expressions. |
| 880 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 881 | if (!isa<ConstantInt>(getOperand(i))) |
| 882 | return false; |
| 883 | return true; |
| 884 | } |
| 885 | |
Chris Lattner | 93aeea3 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 886 | // getAsString - If the sub-element type of this array is either sbyte or ubyte, |
| 887 | // then this method converts the array to an std::string and returns it. |
| 888 | // Otherwise, it asserts out. |
| 889 | // |
| 890 | std::string ConstantArray::getAsString() const { |
Chris Lattner | 13cfdea | 2004-01-14 17:06:38 +0000 | [diff] [blame] | 891 | assert(isString() && "Not a string!"); |
Chris Lattner | 93aeea3 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 892 | std::string Result; |
Chris Lattner | c07736a | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 893 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 894 | Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue(); |
Chris Lattner | 93aeea3 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 895 | return Result; |
| 896 | } |
| 897 | |
| 898 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 899 | //---- ConstantStruct::get() implementation... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 900 | // |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 901 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 902 | namespace llvm { |
| 903 | template<> |
| 904 | struct ConvertConstantType<ConstantStruct, StructType> { |
| 905 | static void convert(ConstantStruct *OldC, const StructType *NewTy) { |
| 906 | // Make everyone now use a constant of the new type... |
| 907 | std::vector<Constant*> C; |
| 908 | for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i) |
| 909 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
| 910 | Constant *New = ConstantStruct::get(NewTy, C); |
| 911 | assert(New != OldC && "Didn't replace constant??"); |
| 912 | |
| 913 | OldC->uncheckedReplaceAllUsesWith(New); |
| 914 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 915 | } |
| 916 | }; |
| 917 | } |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 918 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 919 | static ValueMap<std::vector<Constant*>, StructType, |
| 920 | ConstantStruct> StructConstants; |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 921 | |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 922 | static std::vector<Constant*> getValType(ConstantStruct *CS) { |
| 923 | std::vector<Constant*> Elements; |
| 924 | Elements.reserve(CS->getNumOperands()); |
| 925 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) |
| 926 | Elements.push_back(cast<Constant>(CS->getOperand(i))); |
| 927 | return Elements; |
| 928 | } |
| 929 | |
Chris Lattner | ca705fa | 2004-02-15 04:14:47 +0000 | [diff] [blame] | 930 | Constant *ConstantStruct::get(const StructType *Ty, |
| 931 | const std::vector<Constant*> &V) { |
Chris Lattner | 40bbeb5 | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 932 | // Create a ConstantAggregateZero value if all elements are zeros... |
| 933 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 934 | if (!V[i]->isNullValue()) |
| 935 | return StructConstants.getOrCreate(Ty, V); |
| 936 | |
| 937 | return ConstantAggregateZero::get(Ty); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 938 | } |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 939 | |
Chris Lattner | b370c7a | 2004-07-12 20:35:11 +0000 | [diff] [blame] | 940 | Constant *ConstantStruct::get(const std::vector<Constant*> &V) { |
| 941 | std::vector<const Type*> StructEls; |
| 942 | StructEls.reserve(V.size()); |
| 943 | for (unsigned i = 0, e = V.size(); i != e; ++i) |
| 944 | StructEls.push_back(V[i]->getType()); |
| 945 | return get(StructType::get(StructEls), V); |
| 946 | } |
| 947 | |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 948 | // destroyConstant - Remove the constant from the constant table... |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 949 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 950 | void ConstantStruct::destroyConstant() { |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 951 | StructConstants.remove(this); |
| 952 | destroyConstantImpl(); |
| 953 | } |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 954 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 955 | //---- ConstantPointerNull::get() implementation... |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 956 | // |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 957 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 958 | namespace llvm { |
| 959 | // ConstantPointerNull does not take extra "value" argument... |
| 960 | template<class ValType> |
| 961 | struct ConstantCreator<ConstantPointerNull, PointerType, ValType> { |
| 962 | static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){ |
| 963 | return new ConstantPointerNull(Ty); |
| 964 | } |
| 965 | }; |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 966 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 967 | template<> |
| 968 | struct ConvertConstantType<ConstantPointerNull, PointerType> { |
| 969 | static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) { |
| 970 | // Make everyone now use a constant of the new type... |
| 971 | Constant *New = ConstantPointerNull::get(NewTy); |
| 972 | assert(New != OldC && "Didn't replace constant??"); |
| 973 | OldC->uncheckedReplaceAllUsesWith(New); |
| 974 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 975 | } |
| 976 | }; |
| 977 | } |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 978 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 979 | static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants; |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 980 | |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 981 | static char getValType(ConstantPointerNull *) { |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 986 | ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) { |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 987 | return NullPtrConstants.getOrCreate(Ty, 0); |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 990 | // destroyConstant - Remove the constant from the constant table... |
| 991 | // |
| 992 | void ConstantPointerNull::destroyConstant() { |
| 993 | NullPtrConstants.remove(this); |
| 994 | destroyConstantImpl(); |
| 995 | } |
| 996 | |
| 997 | |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 998 | //---- ConstantExpr::get() implementations... |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 999 | // |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1000 | typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType; |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1002 | namespace llvm { |
| 1003 | template<> |
| 1004 | struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { |
| 1005 | static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) { |
| 1006 | if (V.first == Instruction::Cast) |
| 1007 | return new ConstantExpr(Instruction::Cast, V.second[0], Ty); |
| 1008 | if ((V.first >= Instruction::BinaryOpsBegin && |
| 1009 | V.first < Instruction::BinaryOpsEnd) || |
| 1010 | V.first == Instruction::Shl || V.first == Instruction::Shr) |
| 1011 | return new ConstantExpr(V.first, V.second[0], V.second[1]); |
Chris Lattner | 92e8422 | 2004-03-30 22:51:03 +0000 | [diff] [blame] | 1012 | if (V.first == Instruction::Select) |
| 1013 | return new ConstantExpr(V.second[0], V.second[1], V.second[2]); |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1014 | |
| 1015 | assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!"); |
| 1016 | |
| 1017 | std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end()); |
| 1018 | return new ConstantExpr(V.second[0], IdxList, Ty); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1019 | } |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1020 | }; |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1021 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1022 | template<> |
| 1023 | struct ConvertConstantType<ConstantExpr, Type> { |
| 1024 | static void convert(ConstantExpr *OldC, const Type *NewTy) { |
| 1025 | Constant *New; |
| 1026 | switch (OldC->getOpcode()) { |
| 1027 | case Instruction::Cast: |
| 1028 | New = ConstantExpr::getCast(OldC->getOperand(0), NewTy); |
| 1029 | break; |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1030 | case Instruction::Select: |
| 1031 | New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0), |
| 1032 | OldC->getOperand(1), |
| 1033 | OldC->getOperand(2)); |
| 1034 | break; |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 1035 | case Instruction::Shl: |
| 1036 | case Instruction::Shr: |
| 1037 | New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(), |
| 1038 | OldC->getOperand(0), OldC->getOperand(1)); |
| 1039 | break; |
| 1040 | default: |
| 1041 | assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin && |
| 1042 | OldC->getOpcode() < Instruction::BinaryOpsEnd); |
| 1043 | New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0), |
| 1044 | OldC->getOperand(1)); |
| 1045 | break; |
| 1046 | case Instruction::GetElementPtr: |
| 1047 | // Make everyone now use a constant of the new type... |
| 1048 | std::vector<Constant*> C; |
| 1049 | for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i) |
| 1050 | C.push_back(cast<Constant>(OldC->getOperand(i))); |
| 1051 | New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C); |
| 1052 | break; |
| 1053 | } |
| 1054 | |
| 1055 | assert(New != OldC && "Didn't replace constant??"); |
| 1056 | OldC->uncheckedReplaceAllUsesWith(New); |
| 1057 | OldC->destroyConstant(); // This constant is now dead, destroy it. |
| 1058 | } |
| 1059 | }; |
| 1060 | } // end namespace llvm |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1061 | |
| 1062 | |
Chris Lattner | 6823c9f | 2004-08-04 04:48:01 +0000 | [diff] [blame] | 1063 | static ExprMapKeyType getValType(ConstantExpr *CE) { |
| 1064 | std::vector<Constant*> Operands; |
| 1065 | Operands.reserve(CE->getNumOperands()); |
| 1066 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) |
| 1067 | Operands.push_back(cast<Constant>(CE->getOperand(i))); |
| 1068 | return ExprMapKeyType(CE->getOpcode(), Operands); |
| 1069 | } |
| 1070 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1071 | static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants; |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1072 | |
Chris Lattner | fb242b6 | 2003-04-16 22:40:51 +0000 | [diff] [blame] | 1073 | Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) { |
Chris Lattner | 9eacf8a | 2003-10-07 22:19:19 +0000 | [diff] [blame] | 1074 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
| 1075 | |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1076 | if (Constant *FC = ConstantFoldCastInstruction(C, Ty)) |
| 1077 | return FC; // Fold a few common cases... |
| 1078 | |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1079 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1080 | std::vector<Constant*> argVec(1, C); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1081 | ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec); |
| 1082 | return ExprConstants.getOrCreate(Ty, Key); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1083 | } |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1084 | |
Chris Lattner | d144f42 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1085 | Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) { |
| 1086 | assert(C->getType()->isInteger() && Ty->isInteger() && |
| 1087 | C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() && |
| 1088 | "This is an illegal sign extension!"); |
| 1089 | C = ConstantExpr::getCast(C, C->getType()->getSignedVersion()); |
| 1090 | return ConstantExpr::getCast(C, Ty); |
| 1091 | } |
| 1092 | |
| 1093 | Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) { |
| 1094 | assert(C->getType()->isInteger() && Ty->isInteger() && |
| 1095 | C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() && |
| 1096 | "This is an illegal zero extension!"); |
| 1097 | C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion()); |
| 1098 | return ConstantExpr::getCast(C, Ty); |
| 1099 | } |
| 1100 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1101 | Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode, |
| 1102 | Constant *C1, Constant *C2) { |
Chris Lattner | 3edd397 | 2004-01-12 19:04:55 +0000 | [diff] [blame] | 1103 | if (Opcode == Instruction::Shl || Opcode == Instruction::Shr) |
| 1104 | return getShiftTy(ReqTy, Opcode, C1, C2); |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1105 | // Check the operands for consistency first |
| 1106 | assert((Opcode >= Instruction::BinaryOpsBegin && |
| 1107 | Opcode < Instruction::BinaryOpsEnd) && |
| 1108 | "Invalid opcode in binary constant expression"); |
| 1109 | assert(C1->getType() == C2->getType() && |
| 1110 | "Operand types in binary constant expression should match"); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | c3d12f0 | 2004-08-04 18:50:09 +0000 | [diff] [blame^] | 1112 | if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) && |
| 1113 | ReqTy == Type::BoolTy)) |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1114 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
| 1115 | return FC; // Fold a few common cases... |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1116 | |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1117 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1118 | ExprMapKeyType Key = std::make_pair(Opcode, argVec); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1119 | return ExprConstants.getOrCreate(ReqTy, Key); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Chris Lattner | c3d12f0 | 2004-08-04 18:50:09 +0000 | [diff] [blame^] | 1122 | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) { |
| 1123 | if (Instruction::isRelational(Opcode)) |
| 1124 | return getTy(Type::BoolTy, Opcode, C1, C2); |
| 1125 | else |
| 1126 | return getTy(C1->getType(), Opcode, C1, C2); |
| 1127 | } |
| 1128 | |
Chris Lattner | 08a45cc | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1129 | Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C, |
| 1130 | Constant *V1, Constant *V2) { |
| 1131 | assert(C->getType() == Type::BoolTy && "Select condition must be bool!"); |
| 1132 | assert(V1->getType() == V2->getType() && "Select value types must match!"); |
| 1133 | assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!"); |
| 1134 | |
| 1135 | if (ReqTy == V1->getType()) |
| 1136 | if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2)) |
| 1137 | return SC; // Fold common cases |
| 1138 | |
| 1139 | std::vector<Constant*> argVec(3, C); |
| 1140 | argVec[1] = V1; |
| 1141 | argVec[2] = V2; |
| 1142 | ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec); |
| 1143 | return ExprConstants.getOrCreate(ReqTy, Key); |
| 1144 | } |
| 1145 | |
Chris Lattner | 69b9e89 | 2004-01-12 19:12:58 +0000 | [diff] [blame] | 1146 | /// getShiftTy - Return a shift left or shift right constant expr |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1147 | Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode, |
| 1148 | Constant *C1, Constant *C2) { |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1149 | // Check the operands for consistency first |
| 1150 | assert((Opcode == Instruction::Shl || |
| 1151 | Opcode == Instruction::Shr) && |
| 1152 | "Invalid opcode in binary constant expression"); |
| 1153 | assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy && |
| 1154 | "Invalid operand types for Shift constant expr!"); |
| 1155 | |
Chris Lattner | d4403b4 | 2004-01-12 20:40:42 +0000 | [diff] [blame] | 1156 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1157 | return FC; // Fold a few common cases... |
| 1158 | |
| 1159 | // Look up the constant in the table first to ensure uniqueness |
| 1160 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1161 | ExprMapKeyType Key = std::make_pair(Opcode, argVec); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1162 | return ExprConstants.getOrCreate(ReqTy, Key); |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1166 | Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C, |
| 1167 | const std::vector<Constant*> &IdxList) { |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1168 | assert(GetElementPtrInst::getIndexedType(C->getType(), |
| 1169 | std::vector<Value*>(IdxList.begin(), IdxList.end()), true) && |
| 1170 | "GEP indices invalid!"); |
| 1171 | |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1172 | if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList)) |
| 1173 | return FC; // Fold a few common cases... |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1174 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1175 | assert(isa<PointerType>(C->getType()) && |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1176 | "Non-pointer type for constant GetElementPtr expression"); |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1177 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1178 | std::vector<Constant*> argVec(1, C); |
Chris Lattner | 6183b92 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 1179 | argVec.insert(argVec.end(), IdxList.begin(), IdxList.end()); |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1180 | const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec); |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1181 | return ExprConstants.getOrCreate(ReqTy, Key); |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Chris Lattner | ed468e37 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1184 | Constant *ConstantExpr::getGetElementPtr(Constant *C, |
| 1185 | const std::vector<Constant*> &IdxList){ |
| 1186 | // Get the result type of the getelementptr! |
| 1187 | std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end()); |
| 1188 | |
| 1189 | const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList, |
| 1190 | true); |
| 1191 | assert(Ty && "GEP indices invalid!"); |
| 1192 | return getGetElementPtrTy(PointerType::get(Ty), C, IdxList); |
| 1193 | } |
| 1194 | |
| 1195 | |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 1196 | // destroyConstant - Remove the constant from the constant table... |
| 1197 | // |
| 1198 | void ConstantExpr::destroyConstant() { |
| 1199 | ExprConstants.remove(this); |
| 1200 | destroyConstantImpl(); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 1203 | const char *ConstantExpr::getOpcodeName() const { |
| 1204 | return Instruction::getOpcodeName(getOpcode()); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1205 | } |
Reid Spencer | 1c9c8e6 | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 1206 | |