Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1 | //===-- Constants.cpp - Implement Constant nodes --------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 3 | // This file implements the Constant* classes... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 7 | #include "llvm/Constants.h" |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 8 | #include "llvm/ConstantHandling.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | #include "llvm/DerivedTypes.h" |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 10 | #include "llvm/iMemory.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | #include "llvm/SymbolTable.h" |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 12 | #include "llvm/Module.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 13 | #include "Support/StringExtras.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | #include <algorithm> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 16 | ConstantBool *ConstantBool::True = new ConstantBool(true); |
| 17 | ConstantBool *ConstantBool::False = new ConstantBool(false); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 21 | // Constant Class |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | // Specialize setName to take care of symbol table majik |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 25 | void Constant::setName(const std::string &Name, SymbolTable *ST) { |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 26 | assert(ST && "Type::setName - Must provide symbol table argument!"); |
| 27 | |
| 28 | if (Name.size()) ST->insert(Name, this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 31 | void Constant::destroyConstantImpl() { |
| 32 | // When a Constant is destroyed, there may be lingering |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 33 | // references to the constant by other constants in the constant pool. These |
| 34 | // constants are implicitly dependant on the module that is being deleted, |
| 35 | // but they don't know that. Because we only find out when the CPV is |
| 36 | // 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] | 37 | // Constants) that they are, in fact, invalid now and should be deleted. |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 38 | // |
| 39 | while (!use_empty()) { |
| 40 | Value *V = use_back(); |
| 41 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | 6183b92 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 42 | if (!isa<Constant>(V)) |
| 43 | std::cerr << "While deleting: " << *this |
| 44 | << "\n\nUse still stuck around after Def is destroyed: " |
| 45 | << *V << "\n\n"; |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 46 | #endif |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 47 | assert(isa<Constant>(V) && "References remain to Constant being destroyed"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 48 | Constant *CPV = cast<Constant>(V); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 49 | CPV->destroyConstant(); |
| 50 | |
| 51 | // The constant should remove itself from our use list... |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 52 | assert((use_empty() || use_back() != V) && "Constant not removed!"); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // Value has no outstanding references it is safe to delete it now... |
| 56 | delete this; |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 57 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 59 | // Static constructor to create a '0' constant of arbitrary type... |
| 60 | Constant *Constant::getNullValue(const Type *Ty) { |
| 61 | switch (Ty->getPrimitiveID()) { |
| 62 | case Type::BoolTyID: return ConstantBool::get(false); |
| 63 | case Type::SByteTyID: |
| 64 | case Type::ShortTyID: |
| 65 | case Type::IntTyID: |
| 66 | case Type::LongTyID: return ConstantSInt::get(Ty, 0); |
| 67 | |
| 68 | case Type::UByteTyID: |
| 69 | case Type::UShortTyID: |
| 70 | case Type::UIntTyID: |
| 71 | case Type::ULongTyID: return ConstantUInt::get(Ty, 0); |
| 72 | |
| 73 | case Type::FloatTyID: |
| 74 | case Type::DoubleTyID: return ConstantFP::get(Ty, 0); |
| 75 | |
| 76 | case Type::PointerTyID: |
| 77 | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
Chris Lattner | 27accf7 | 2003-03-06 21:02:18 +0000 | [diff] [blame] | 78 | case Type::StructTyID: { |
| 79 | const StructType *ST = cast<StructType>(Ty); |
| 80 | |
| 81 | const StructType::ElementTypes &ETs = ST->getElementTypes(); |
| 82 | std::vector<Constant*> Elements; |
| 83 | Elements.resize(ETs.size()); |
| 84 | for (unsigned i = 0, e = ETs.size(); i != e; ++i) |
| 85 | Elements[i] = Constant::getNullValue(ETs[i]); |
| 86 | return ConstantStruct::get(ST, Elements); |
| 87 | } |
| 88 | case Type::ArrayTyID: { |
| 89 | const ArrayType *AT = cast<ArrayType>(Ty); |
| 90 | Constant *El = Constant::getNullValue(AT->getElementType()); |
| 91 | unsigned NumElements = AT->getNumElements(); |
| 92 | return ConstantArray::get(AT, std::vector<Constant*>(NumElements, El)); |
| 93 | } |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 94 | default: |
Chris Lattner | 27accf7 | 2003-03-06 21:02:18 +0000 | [diff] [blame] | 95 | // Function, Type, Label, or Opaque type? |
| 96 | assert(0 && "Cannot create a null constant of that type!"); |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Static constructor to create the maximum constant of an integral type... |
| 102 | ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) { |
| 103 | switch (Ty->getPrimitiveID()) { |
| 104 | case Type::BoolTyID: return ConstantBool::True; |
| 105 | case Type::SByteTyID: |
| 106 | case Type::ShortTyID: |
| 107 | case Type::IntTyID: |
| 108 | case Type::LongTyID: { |
| 109 | // Calculate 011111111111111... |
| 110 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 111 | int64_t Val = INT64_MAX; // All ones |
| 112 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 113 | return ConstantSInt::get(Ty, Val); |
| 114 | } |
| 115 | |
| 116 | case Type::UByteTyID: |
| 117 | case Type::UShortTyID: |
| 118 | case Type::UIntTyID: |
| 119 | case Type::ULongTyID: return getAllOnesValue(Ty); |
| 120 | |
Chris Lattner | 227b86c | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 121 | default: return 0; |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | // Static constructor to create the minimum constant for an integral type... |
| 126 | ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) { |
| 127 | switch (Ty->getPrimitiveID()) { |
| 128 | case Type::BoolTyID: return ConstantBool::False; |
| 129 | case Type::SByteTyID: |
| 130 | case Type::ShortTyID: |
| 131 | case Type::IntTyID: |
| 132 | case Type::LongTyID: { |
| 133 | // Calculate 1111111111000000000000 |
| 134 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 135 | int64_t Val = -1; // All ones |
| 136 | Val <<= TypeBits-1; // Shift over to the right spot |
| 137 | return ConstantSInt::get(Ty, Val); |
| 138 | } |
| 139 | |
| 140 | case Type::UByteTyID: |
| 141 | case Type::UShortTyID: |
| 142 | case Type::UIntTyID: |
| 143 | case Type::ULongTyID: return ConstantUInt::get(Ty, 0); |
| 144 | |
Chris Lattner | 227b86c | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 145 | default: return 0; |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | // Static constructor to create an integral constant with all bits set |
| 150 | ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) { |
| 151 | switch (Ty->getPrimitiveID()) { |
| 152 | case Type::BoolTyID: return ConstantBool::True; |
| 153 | case Type::SByteTyID: |
| 154 | case Type::ShortTyID: |
| 155 | case Type::IntTyID: |
| 156 | case Type::LongTyID: return ConstantSInt::get(Ty, -1); |
| 157 | |
| 158 | case Type::UByteTyID: |
| 159 | case Type::UShortTyID: |
| 160 | case Type::UIntTyID: |
| 161 | case Type::ULongTyID: { |
| 162 | // Calculate ~0 of the right type... |
| 163 | unsigned TypeBits = Ty->getPrimitiveSize()*8; |
| 164 | uint64_t Val = ~0ULL; // All ones |
| 165 | Val >>= 64-TypeBits; // Shift out unwanted 1 bits... |
| 166 | return ConstantUInt::get(Ty, Val); |
| 167 | } |
Chris Lattner | 227b86c | 2002-08-14 17:12:13 +0000 | [diff] [blame] | 168 | default: return 0; |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Chris Lattner | 1680312 | 2003-03-10 22:39:02 +0000 | [diff] [blame] | 172 | bool ConstantUInt::isAllOnesValue() const { |
| 173 | unsigned TypeBits = getType()->getPrimitiveSize()*8; |
| 174 | uint64_t Val = ~0ULL; // All ones |
| 175 | Val >>= 64-TypeBits; // Shift out inappropriate bits |
| 176 | return getValue() == Val; |
| 177 | } |
| 178 | |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 180 | //===----------------------------------------------------------------------===// |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 181 | // ConstantXXX Classes |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 182 | //===----------------------------------------------------------------------===// |
| 183 | |
| 184 | //===----------------------------------------------------------------------===// |
| 185 | // Normal Constructors |
| 186 | |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 187 | ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 188 | Val = V; |
| 189 | } |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 191 | ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) { |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 192 | Val.Unsigned = V; |
Chris Lattner | d7fa0fd | 2001-07-21 19:16:08 +0000 | [diff] [blame] | 193 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 194 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 195 | ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) { |
Chris Lattner | 562219d | 2002-09-11 01:21:04 +0000 | [diff] [blame] | 196 | assert(Ty->isInteger() && Ty->isSigned() && |
| 197 | "Illegal type for unsigned integer constant!"); |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 198 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 201 | ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) { |
Chris Lattner | 562219d | 2002-09-11 01:21:04 +0000 | [diff] [blame] | 202 | assert(Ty->isInteger() && Ty->isUnsigned() && |
| 203 | "Illegal type for unsigned integer constant!"); |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 204 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 207 | ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) { |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 208 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 209 | Val = V; |
| 210 | } |
| 211 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 212 | ConstantArray::ConstantArray(const ArrayType *T, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 213 | const std::vector<Constant*> &V) : Constant(T) { |
Chris Lattner | d43c7d2 | 2002-10-08 23:33:52 +0000 | [diff] [blame] | 214 | Operands.reserve(V.size()); |
| 215 | for (unsigned i = 0, e = V.size(); i != e; ++i) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 216 | assert(V[i]->getType() == T->getElementType()); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 217 | Operands.push_back(Use(V[i], this)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 221 | ConstantStruct::ConstantStruct(const StructType *T, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 222 | const std::vector<Constant*> &V) : Constant(T) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 223 | const StructType::ElementTypes &ETypes = T->getElementTypes(); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 224 | assert(V.size() == ETypes.size() && |
| 225 | "Invalid initializer vector for constant structure"); |
Chris Lattner | d43c7d2 | 2002-10-08 23:33:52 +0000 | [diff] [blame] | 226 | Operands.reserve(V.size()); |
| 227 | for (unsigned i = 0, e = V.size(); i != e; ++i) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 228 | assert(V[i]->getType() == ETypes[i]); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 229 | Operands.push_back(Use(V[i], this)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 233 | ConstantPointerRef::ConstantPointerRef(GlobalValue *GV) |
| 234 | : ConstantPointer(GV->getType()) { |
Chris Lattner | f4ba6c7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 235 | Operands.push_back(Use(GV, this)); |
| 236 | } |
| 237 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 238 | ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty) |
| 239 | : Constant(Ty), iType(Opcode) { |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 240 | Operands.push_back(Use(C, this)); |
| 241 | } |
| 242 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 243 | ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2) |
| 244 | : Constant(C1->getType()), iType(Opcode) { |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 245 | Operands.push_back(Use(C1, this)); |
| 246 | Operands.push_back(Use(C2, this)); |
| 247 | } |
| 248 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 249 | ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, |
| 250 | const Type *DestTy) |
| 251 | : Constant(DestTy), iType(Instruction::GetElementPtr) { |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 252 | Operands.reserve(1+IdxList.size()); |
| 253 | Operands.push_back(Use(C, this)); |
| 254 | for (unsigned i = 0, E = IdxList.size(); i != E; ++i) |
| 255 | Operands.push_back(Use(IdxList[i], this)); |
| 256 | } |
| 257 | |
Chris Lattner | f4ba6c7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 259 | |
| 260 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 261 | // classof implementations |
| 262 | |
Chris Lattner | 9fb9641 | 2002-08-13 17:50:20 +0000 | [diff] [blame] | 263 | bool ConstantIntegral::classof(const Constant *CPV) { |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 264 | return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV); |
Chris Lattner | 994b9f3 | 2002-08-12 21:21:21 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 267 | bool ConstantInt::classof(const Constant *CPV) { |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 268 | return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 269 | } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 270 | bool ConstantSInt::classof(const Constant *CPV) { |
Chris Lattner | 6183b92 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 271 | return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 272 | } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 273 | bool ConstantUInt::classof(const Constant *CPV) { |
Chris Lattner | 6183b92 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 274 | return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 275 | } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 276 | bool ConstantFP::classof(const Constant *CPV) { |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 277 | const Type *Ty = CPV->getType(); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 278 | return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) && |
Chris Lattner | 6183b92 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 279 | !isa<ConstantExpr>(CPV)); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 280 | } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 281 | bool ConstantArray::classof(const Constant *CPV) { |
Chris Lattner | 6183b92 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 282 | return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 283 | } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 284 | bool ConstantStruct::classof(const Constant *CPV) { |
Chris Lattner | 6183b92 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 285 | return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 286 | } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 287 | bool ConstantPointer::classof(const Constant *CPV) { |
Chris Lattner | 6183b92 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 288 | return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV)); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 293 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 294 | // isValueValidForType implementations |
| 295 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 296 | bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 297 | switch (Ty->getPrimitiveID()) { |
| 298 | default: |
| 299 | return false; // These can't be represented as integers!!! |
| 300 | |
| 301 | // Signed types... |
| 302 | case Type::SByteTyID: |
| 303 | return (Val <= INT8_MAX && Val >= INT8_MIN); |
| 304 | case Type::ShortTyID: |
| 305 | return (Val <= INT16_MAX && Val >= INT16_MIN); |
| 306 | case Type::IntTyID: |
| 307 | return (Val <= INT32_MAX && Val >= INT32_MIN); |
| 308 | case Type::LongTyID: |
| 309 | return true; // This is the largest type... |
| 310 | } |
| 311 | assert(0 && "WTF?"); |
| 312 | return false; |
| 313 | } |
| 314 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 315 | bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 316 | switch (Ty->getPrimitiveID()) { |
| 317 | default: |
| 318 | return false; // These can't be represented as integers!!! |
| 319 | |
| 320 | // Unsigned types... |
| 321 | case Type::UByteTyID: |
| 322 | return (Val <= UINT8_MAX); |
| 323 | case Type::UShortTyID: |
| 324 | return (Val <= UINT16_MAX); |
| 325 | case Type::UIntTyID: |
| 326 | return (Val <= UINT32_MAX); |
| 327 | case Type::ULongTyID: |
| 328 | return true; // This is the largest type... |
| 329 | } |
| 330 | assert(0 && "WTF?"); |
| 331 | return false; |
| 332 | } |
| 333 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 334 | bool ConstantFP::isValueValidForType(const Type *Ty, double Val) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 335 | switch (Ty->getPrimitiveID()) { |
| 336 | default: |
| 337 | return false; // These can't be represented as floating point! |
| 338 | |
| 339 | // 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] | 340 | case Type::FloatTyID: |
Chris Lattner | a59c266 | 2001-07-15 00:18:39 +0000 | [diff] [blame] | 341 | /* |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 342 | return (Val <= UINT8_MAX); |
| 343 | */ |
| 344 | case Type::DoubleTyID: |
| 345 | return true; // This is the largest type... |
| 346 | } |
| 347 | }; |
Chris Lattner | 37bf630 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 349 | //===----------------------------------------------------------------------===// |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 350 | // replaceUsesOfWithOnConstant implementations |
| 351 | |
| 352 | void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) { |
| 353 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 354 | |
| 355 | std::vector<Constant*> Values; |
| 356 | Values.reserve(getValues().size()); // Build replacement array... |
| 357 | for (unsigned i = 0, e = getValues().size(); i != e; ++i) { |
| 358 | Constant *Val = cast<Constant>(getValues()[i]); |
| 359 | if (Val == From) Val = cast<Constant>(To); |
| 360 | Values.push_back(Val); |
| 361 | } |
| 362 | |
| 363 | ConstantArray *Replacement = ConstantArray::get(getType(), Values); |
| 364 | assert(Replacement != this && "I didn't contain From!"); |
| 365 | |
| 366 | // Everyone using this now uses the replacement... |
| 367 | replaceAllUsesWith(Replacement); |
| 368 | |
| 369 | // Delete the old constant! |
| 370 | destroyConstant(); |
| 371 | } |
| 372 | |
| 373 | void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) { |
| 374 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 375 | |
| 376 | std::vector<Constant*> Values; |
| 377 | Values.reserve(getValues().size()); |
| 378 | for (unsigned i = 0, e = getValues().size(); i != e; ++i) { |
| 379 | Constant *Val = cast<Constant>(getValues()[i]); |
| 380 | if (Val == From) Val = cast<Constant>(To); |
| 381 | Values.push_back(Val); |
| 382 | } |
| 383 | |
| 384 | ConstantStruct *Replacement = ConstantStruct::get(getType(), Values); |
| 385 | assert(Replacement != this && "I didn't contain From!"); |
| 386 | |
| 387 | // Everyone using this now uses the replacement... |
| 388 | replaceAllUsesWith(Replacement); |
| 389 | |
| 390 | // Delete the old constant! |
| 391 | destroyConstant(); |
| 392 | } |
| 393 | |
| 394 | void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) { |
| 395 | if (isa<GlobalValue>(To)) { |
| 396 | assert(From == getOperand(0) && "Doesn't contain from!"); |
| 397 | ConstantPointerRef *Replacement = |
| 398 | ConstantPointerRef::get(cast<GlobalValue>(To)); |
| 399 | |
| 400 | // Everyone using this now uses the replacement... |
| 401 | replaceAllUsesWith(Replacement); |
| 402 | |
| 403 | // Delete the old constant! |
| 404 | destroyConstant(); |
| 405 | } else { |
| 406 | // Just replace ourselves with the To value specified. |
| 407 | replaceAllUsesWith(To); |
| 408 | |
| 409 | // Delete the old constant! |
| 410 | destroyConstant(); |
| 411 | } |
| 412 | } |
| 413 | |
Chris Lattner | 3b9922f | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 414 | void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV) { |
| 415 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); |
| 416 | Constant *To = cast<Constant>(ToV); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 417 | |
Chris Lattner | fb242b6 | 2003-04-16 22:40:51 +0000 | [diff] [blame] | 418 | Constant *Replacement = 0; |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 419 | if (getOpcode() == Instruction::GetElementPtr) { |
| 420 | std::vector<Constant*> Indices; |
Chris Lattner | 3b9922f | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 421 | Constant *Pointer = getOperand(0); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 422 | Indices.reserve(getNumOperands()-1); |
Chris Lattner | 3b9922f | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 423 | if (Pointer == From) Pointer = To; |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 424 | |
| 425 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Chris Lattner | 3b9922f | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 426 | Constant *Val = getOperand(i); |
| 427 | if (Val == From) Val = To; |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 428 | Indices.push_back(Val); |
| 429 | } |
| 430 | Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices); |
| 431 | } else if (getOpcode() == Instruction::Cast) { |
| 432 | assert(getOperand(0) == From && "Cast only has one use!"); |
Chris Lattner | 3b9922f | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 433 | Replacement = ConstantExpr::getCast(To, getType()); |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 434 | } else if (getNumOperands() == 2) { |
Chris Lattner | 3b9922f | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 435 | Constant *C1 = getOperand(0); |
| 436 | Constant *C2 = getOperand(1); |
| 437 | if (C1 == From) C1 = To; |
| 438 | if (C2 == From) C2 = To; |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 439 | Replacement = ConstantExpr::get(getOpcode(), C1, C2); |
| 440 | } else { |
| 441 | assert(0 && "Unknown ConstantExpr type!"); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | assert(Replacement != this && "I didn't contain From!"); |
| 446 | |
| 447 | // Everyone using this now uses the replacement... |
| 448 | replaceAllUsesWith(Replacement); |
| 449 | |
| 450 | // Delete the old constant! |
| 451 | destroyConstant(); |
| 452 | } |
| 453 | |
Chris Lattner | c251f9e | 2002-10-09 23:12:25 +0000 | [diff] [blame] | 454 | //===----------------------------------------------------------------------===// |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 455 | // Factory Function Implementation |
| 456 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 457 | // ConstantCreator - A class that is used to create constants by |
| 458 | // ValueMap*. This class should be partially specialized if there is |
| 459 | // something strange that needs to be done to interface to the ctor for the |
| 460 | // constant. |
| 461 | // |
| 462 | template<class ConstantClass, class TypeClass, class ValType> |
| 463 | struct ConstantCreator { |
| 464 | static ConstantClass *create(const TypeClass *Ty, const ValType &V) { |
| 465 | return new ConstantClass(Ty, V); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 466 | } |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 467 | }; |
| 468 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 469 | namespace { |
| 470 | template<class ValType, class TypeClass, class ConstantClass> |
| 471 | class ValueMap { |
| 472 | protected: |
| 473 | typedef std::pair<const TypeClass*, ValType> ConstHashKey; |
| 474 | std::map<ConstHashKey, ConstantClass *> Map; |
| 475 | public: |
| 476 | // getOrCreate - Return the specified constant from the map, creating it if |
| 477 | // necessary. |
| 478 | ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) { |
| 479 | ConstHashKey Lookup(Ty, V); |
| 480 | typename std::map<ConstHashKey,ConstantClass *>::iterator I = |
| 481 | Map.lower_bound(Lookup); |
| 482 | if (I != Map.end() && I->first == Lookup) |
| 483 | return I->second; // Is it in the map? |
| 484 | |
| 485 | // If no preexisting value, create one now... |
| 486 | ConstantClass *Result = |
| 487 | ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V); |
| 488 | |
| 489 | Map.insert(I, std::make_pair(ConstHashKey(Ty, V), Result)); |
| 490 | return Result; |
| 491 | } |
| 492 | |
| 493 | void remove(ConstantClass *CP) { |
| 494 | // FIXME: This could be sped up a LOT. If this gets to be a performance |
| 495 | // problem, someone should look at this. |
| 496 | for (typename std::map<ConstHashKey, ConstantClass*>::iterator |
| 497 | I = Map.begin(), E = Map.end(); I != E; ++I) |
| 498 | if (I->second == CP) { |
| 499 | Map.erase(I); |
| 500 | return; |
| 501 | } |
| 502 | assert(0 && "Constant not found in constant table!"); |
| 503 | } |
| 504 | }; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 509 | //---- ConstantUInt::get() and ConstantSInt::get() implementations... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 510 | // |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 511 | static ValueMap< int64_t, Type, ConstantSInt> SIntConstants; |
| 512 | static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants; |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 513 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 514 | ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) { |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 515 | return SIntConstants.getOrCreate(Ty, V); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 518 | ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) { |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 519 | return UIntConstants.getOrCreate(Ty, V); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 522 | ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) { |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 523 | assert(V <= 127 && "Can only be used with very small positive constants!"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 524 | if (Ty->isSigned()) return ConstantSInt::get(Ty, V); |
| 525 | return ConstantUInt::get(Ty, V); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 528 | //---- ConstantFP::get() implementation... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 529 | // |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 530 | static ValueMap<double, Type, ConstantFP> FPConstants; |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 531 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 532 | ConstantFP *ConstantFP::get(const Type *Ty, double V) { |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 533 | return FPConstants.getOrCreate(Ty, V); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 536 | //---- ConstantArray::get() implementation... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 537 | // |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 538 | static ValueMap<std::vector<Constant*>, ArrayType, |
| 539 | ConstantArray> ArrayConstants; |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 540 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 541 | ConstantArray *ConstantArray::get(const ArrayType *Ty, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 542 | const std::vector<Constant*> &V) { |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 543 | return ArrayConstants.getOrCreate(Ty, V); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 546 | // destroyConstant - Remove the constant from the constant table... |
| 547 | // |
| 548 | void ConstantArray::destroyConstant() { |
| 549 | ArrayConstants.remove(this); |
| 550 | destroyConstantImpl(); |
| 551 | } |
| 552 | |
| 553 | /// refineAbstractType - If this callback is invoked, then this constant is of a |
| 554 | /// derived type, change all users to use a concrete constant of the new type. |
| 555 | /// |
| 556 | void ConstantArray::refineAbstractType(const DerivedType *OldTy, |
| 557 | const Type *NewTy) { |
| 558 | Value::refineAbstractType(OldTy, NewTy); |
| 559 | |
| 560 | // Make everyone now use a constant of the new type... |
| 561 | std::vector<Constant*> C; |
| 562 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 563 | C.push_back(cast<Constant>(getOperand(i))); |
| 564 | replaceAllUsesWith(ConstantArray::get(cast<ArrayType>(NewTy), |
| 565 | C)); |
| 566 | destroyConstant(); // This constant is now dead, destroy it. |
| 567 | } |
| 568 | |
| 569 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 570 | // ConstantArray::get(const string&) - Return an array that is initialized to |
Chris Lattner | c5bdb24 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 571 | // contain the specified string. A null terminator is added to the specified |
| 572 | // string so that it may be used in a natural way... |
| 573 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 574 | ConstantArray *ConstantArray::get(const std::string &Str) { |
| 575 | std::vector<Constant*> ElementVals; |
Chris Lattner | c5bdb24 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 576 | |
| 577 | for (unsigned i = 0; i < Str.length(); ++i) |
Chris Lattner | a2644f6 | 2001-12-14 16:41:18 +0000 | [diff] [blame] | 578 | ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i])); |
Chris Lattner | c5bdb24 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 579 | |
| 580 | // Add a null terminator to the string... |
Chris Lattner | a2644f6 | 2001-12-14 16:41:18 +0000 | [diff] [blame] | 581 | ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0)); |
Chris Lattner | c5bdb24 | 2001-10-14 23:54:12 +0000 | [diff] [blame] | 582 | |
Chris Lattner | a2644f6 | 2001-12-14 16:41:18 +0000 | [diff] [blame] | 583 | ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 584 | return ConstantArray::get(ATy, ElementVals); |
Vikram S. Adve | db2da49 | 2001-10-14 23:17:20 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Chris Lattner | 93aeea3 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 587 | // getAsString - If the sub-element type of this array is either sbyte or ubyte, |
| 588 | // then this method converts the array to an std::string and returns it. |
| 589 | // Otherwise, it asserts out. |
| 590 | // |
| 591 | std::string ConstantArray::getAsString() const { |
| 592 | std::string Result; |
| 593 | if (getType()->getElementType() == Type::SByteTy) |
| 594 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 595 | Result += (char)cast<ConstantSInt>(getOperand(i))->getValue(); |
| 596 | else { |
| 597 | assert(getType()->getElementType() == Type::UByteTy && "Not a string!"); |
| 598 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 599 | Result += (char)cast<ConstantUInt>(getOperand(i))->getValue(); |
| 600 | } |
| 601 | return Result; |
| 602 | } |
| 603 | |
| 604 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 605 | //---- ConstantStruct::get() implementation... |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 606 | // |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 607 | static ValueMap<std::vector<Constant*>, StructType, |
| 608 | ConstantStruct> StructConstants; |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 609 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 610 | ConstantStruct *ConstantStruct::get(const StructType *Ty, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 611 | const std::vector<Constant*> &V) { |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 612 | return StructConstants.getOrCreate(Ty, V); |
Chris Lattner | 531daef | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 613 | } |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 614 | |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 615 | // destroyConstant - Remove the constant from the constant table... |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 616 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 617 | void ConstantStruct::destroyConstant() { |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 618 | StructConstants.remove(this); |
| 619 | destroyConstantImpl(); |
| 620 | } |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 621 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 622 | /// refineAbstractType - If this callback is invoked, then this constant is of a |
| 623 | /// derived type, change all users to use a concrete constant of the new type. |
| 624 | /// |
| 625 | void ConstantStruct::refineAbstractType(const DerivedType *OldTy, |
| 626 | const Type *NewTy) { |
| 627 | Value::refineAbstractType(OldTy, NewTy); |
| 628 | |
| 629 | // Make everyone now use a constant of the new type... |
| 630 | std::vector<Constant*> C; |
| 631 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 632 | C.push_back(cast<Constant>(getOperand(i))); |
| 633 | replaceAllUsesWith(ConstantStruct::get(cast<StructType>(NewTy), |
| 634 | C)); |
| 635 | destroyConstant(); // This constant is now dead, destroy it. |
| 636 | } |
| 637 | |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 638 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 639 | //---- ConstantPointerNull::get() implementation... |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 640 | // |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 641 | |
| 642 | // ConstantPointerNull does not take extra "value" argument... |
| 643 | template<class ValType> |
| 644 | struct ConstantCreator<ConstantPointerNull, PointerType, ValType> { |
| 645 | static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){ |
| 646 | return new ConstantPointerNull(Ty); |
| 647 | } |
| 648 | }; |
| 649 | |
| 650 | static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants; |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 651 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 652 | ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) { |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 653 | return NullPtrConstants.getOrCreate(Ty, 0); |
Chris Lattner | 6a57baa | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 656 | // destroyConstant - Remove the constant from the constant table... |
| 657 | // |
| 658 | void ConstantPointerNull::destroyConstant() { |
| 659 | NullPtrConstants.remove(this); |
| 660 | destroyConstantImpl(); |
| 661 | } |
| 662 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 663 | /// refineAbstractType - If this callback is invoked, then this constant is of a |
| 664 | /// derived type, change all users to use a concrete constant of the new type. |
| 665 | /// |
| 666 | void ConstantPointerNull::refineAbstractType(const DerivedType *OldTy, |
| 667 | const Type *NewTy) { |
| 668 | Value::refineAbstractType(OldTy, NewTy); |
| 669 | |
| 670 | // Make everyone now use a constant of the new type... |
| 671 | replaceAllUsesWith(ConstantPointerNull::get(cast<PointerType>(NewTy))); |
| 672 | |
| 673 | // This constant is now dead, destroy it. |
| 674 | destroyConstant(); |
| 675 | } |
| 676 | |
| 677 | |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 678 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 679 | //---- ConstantPointerRef::get() implementation... |
Chris Lattner | 4387370 | 2001-10-03 19:28:15 +0000 | [diff] [blame] | 680 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 681 | ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) { |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 682 | assert(GV->getParent() && "Global Value must be attached to a module!"); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 683 | |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 684 | // The Module handles the pointer reference sharing... |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 685 | return GV->getParent()->getConstantPointerRef(GV); |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 688 | // destroyConstant - Remove the constant from the constant table... |
| 689 | // |
| 690 | void ConstantPointerRef::destroyConstant() { |
| 691 | getValue()->getParent()->destroyConstantPointerRef(this); |
| 692 | destroyConstantImpl(); |
| 693 | } |
| 694 | |
| 695 | |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 696 | //---- ConstantExpr::get() implementations... |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 697 | // |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 698 | typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType; |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 699 | |
| 700 | template<> |
| 701 | struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { |
| 702 | static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) { |
| 703 | if (V.first == Instruction::Cast) |
| 704 | return new ConstantExpr(Instruction::Cast, V.second[0], Ty); |
| 705 | if ((V.first >= Instruction::BinaryOpsBegin && |
| 706 | V.first < Instruction::BinaryOpsEnd) || |
| 707 | V.first == Instruction::Shl || V.first == Instruction::Shr) |
| 708 | return new ConstantExpr(V.first, V.second[0], V.second[1]); |
| 709 | |
| 710 | assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!"); |
| 711 | |
| 712 | // Check that the indices list is valid... |
| 713 | std::vector<Value*> ValIdxList(V.second.begin()+1, V.second.end()); |
| 714 | const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, |
| 715 | true); |
| 716 | assert(DestTy && "Invalid index list for GetElementPtr expression"); |
| 717 | |
| 718 | std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end()); |
| 719 | return new ConstantExpr(V.second[0], IdxList, PointerType::get(DestTy)); |
| 720 | } |
| 721 | }; |
| 722 | |
| 723 | static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants; |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 724 | |
Chris Lattner | fb242b6 | 2003-04-16 22:40:51 +0000 | [diff] [blame] | 725 | Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) { |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 726 | if (Constant *FC = ConstantFoldCastInstruction(C, Ty)) |
| 727 | return FC; // Fold a few common cases... |
| 728 | |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 729 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 730 | std::vector<Constant*> argVec(1, C); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 731 | ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec); |
| 732 | return ExprConstants.getOrCreate(Ty, Key); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 733 | } |
Chris Lattner | f5ec48d | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 734 | |
Chris Lattner | fb242b6 | 2003-04-16 22:40:51 +0000 | [diff] [blame] | 735 | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) { |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 736 | // Check the operands for consistency first |
| 737 | assert((Opcode >= Instruction::BinaryOpsBegin && |
| 738 | Opcode < Instruction::BinaryOpsEnd) && |
| 739 | "Invalid opcode in binary constant expression"); |
| 740 | assert(C1->getType() == C2->getType() && |
| 741 | "Operand types in binary constant expression should match"); |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 742 | |
| 743 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
| 744 | return FC; // Fold a few common cases... |
| 745 | |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 746 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 747 | ExprMapKeyType Key = std::make_pair(Opcode, argVec); |
| 748 | return ExprConstants.getOrCreate(C1->getType(), Key); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 751 | /// getShift - Return a shift left or shift right constant expr |
| 752 | Constant *ConstantExpr::getShift(unsigned Opcode, Constant *C1, Constant *C2) { |
| 753 | // Check the operands for consistency first |
| 754 | assert((Opcode == Instruction::Shl || |
| 755 | Opcode == Instruction::Shr) && |
| 756 | "Invalid opcode in binary constant expression"); |
| 757 | assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy && |
| 758 | "Invalid operand types for Shift constant expr!"); |
| 759 | |
| 760 | if (Constant *FC = ConstantFoldShiftInstruction(Opcode, C1, C2)) |
| 761 | return FC; // Fold a few common cases... |
| 762 | |
| 763 | // Look up the constant in the table first to ensure uniqueness |
| 764 | std::vector<Constant*> argVec(1, C1); argVec.push_back(C2); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 765 | ExprMapKeyType Key = std::make_pair(Opcode, argVec); |
| 766 | return ExprConstants.getOrCreate(C1->getType(), Key); |
Chris Lattner | f31f583 | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | |
Chris Lattner | fb242b6 | 2003-04-16 22:40:51 +0000 | [diff] [blame] | 770 | Constant *ConstantExpr::getGetElementPtr(Constant *C, |
| 771 | const std::vector<Constant*> &IdxList){ |
Chris Lattner | d628f6a | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 772 | if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList)) |
| 773 | return FC; // Fold a few common cases... |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 774 | const Type *Ty = C->getType(); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 775 | assert(isa<PointerType>(Ty) && |
| 776 | "Non-pointer type for constant GetElementPtr expression"); |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 777 | |
| 778 | // Look up the constant in the table first to ensure uniqueness |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 779 | std::vector<Constant*> argVec(1, C); |
Chris Lattner | 6183b92 | 2002-07-18 00:14:50 +0000 | [diff] [blame] | 780 | argVec.insert(argVec.end(), IdxList.begin(), IdxList.end()); |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 781 | |
Chris Lattner | 9bc02a4 | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 782 | const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec); |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 783 | return ExprConstants.getOrCreate(Ty, Key); |
Vikram S. Adve | d0b1bb0 | 2002-07-15 18:19:33 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | // destroyConstant - Remove the constant from the constant table... |
| 787 | // |
| 788 | void ConstantExpr::destroyConstant() { |
| 789 | ExprConstants.remove(this); |
| 790 | destroyConstantImpl(); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Chris Lattner | 02ec5ed | 2003-05-23 20:03:32 +0000 | [diff] [blame^] | 793 | /// refineAbstractType - If this callback is invoked, then this constant is of a |
| 794 | /// derived type, change all users to use a concrete constant of the new type. |
| 795 | /// |
| 796 | void ConstantExpr::refineAbstractType(const DerivedType *OldTy, |
| 797 | const Type *NewTy) { |
| 798 | Value::refineAbstractType(OldTy, NewTy); |
| 799 | |
| 800 | // FIXME: These need to use a lower-level implementation method, because the |
| 801 | // ::get methods intuit the type of the result based on the types of the |
| 802 | // operands. The operand types may not have had their types resolved yet. |
| 803 | // |
| 804 | if (getOpcode() == Instruction::Cast) { |
| 805 | replaceAllUsesWith(getCast(getOperand(0), NewTy)); |
| 806 | } else if (getOpcode() >= Instruction::BinaryOpsBegin && |
| 807 | getOpcode() < Instruction::BinaryOpsEnd) { |
| 808 | replaceAllUsesWith(get(getOpcode(), getOperand(0), getOperand(0))); |
| 809 | } else if (getOpcode() == Instruction::Shl || getOpcode() ==Instruction::Shr){ |
| 810 | replaceAllUsesWith(getShift(getOpcode(), getOperand(0), getOperand(0))); |
| 811 | } else { |
| 812 | assert(getOpcode() == Instruction::GetElementPtr); |
| 813 | |
| 814 | // Make everyone now use a constant of the new type... |
| 815 | std::vector<Constant*> C; |
| 816 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) |
| 817 | C.push_back(cast<Constant>(getOperand(i))); |
| 818 | replaceAllUsesWith(ConstantExpr::getGetElementPtr(getOperand(0), |
| 819 | C)); |
| 820 | } |
| 821 | destroyConstant(); // This constant is now dead, destroy it. |
| 822 | } |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 827 | const char *ConstantExpr::getOpcodeName() const { |
| 828 | return Instruction::getOpcodeName(getOpcode()); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Chris Lattner | 34048e2 | 2002-10-14 03:30:23 +0000 | [diff] [blame] | 831 | unsigned Constant::mutateReferences(Value *OldV, Value *NewV) { |
| 832 | // Uses of constant pointer refs are global values, not constants! |
| 833 | if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) { |
| 834 | GlobalValue *NewGV = cast<GlobalValue>(NewV); |
| 835 | GlobalValue *OldGV = CPR->getValue(); |
Vikram S. Adve | 345e0cf | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 836 | |
Chris Lattner | 34048e2 | 2002-10-14 03:30:23 +0000 | [diff] [blame] | 837 | 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] | 838 | Operands[0] = NewGV; |
Chris Lattner | 608f4b0 | 2003-05-15 19:37:21 +0000 | [diff] [blame] | 839 | OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV); |
Chris Lattner | 34048e2 | 2002-10-14 03:30:23 +0000 | [diff] [blame] | 840 | return 1; |
| 841 | } else { |
| 842 | Constant *NewC = cast<Constant>(NewV); |
| 843 | unsigned NumReplaced = 0; |
| 844 | for (unsigned i = 0, N = getNumOperands(); i != N; ++i) |
| 845 | if (Operands[i] == OldV) { |
| 846 | ++NumReplaced; |
| 847 | Operands[i] = NewC; |
| 848 | } |
| 849 | return NumReplaced; |
| 850 | } |
Chris Lattner | 4387370 | 2001-10-03 19:28:15 +0000 | [diff] [blame] | 851 | } |