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