Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- iConstPool.cpp - Implement ConstPool instructions --------*- C++ -*--=// |
| 2 | // |
| 3 | // This file implements the ConstPool* classes... |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends... |
| 8 | #include "llvm/ConstPoolVals.h" |
Chris Lattner | e2472bb | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 9 | #include "llvm/Support/StringExtras.h" // itostr |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 10 | #include "llvm/DerivedTypes.h" |
| 11 | #include "llvm/SymbolTable.h" |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 12 | #include "llvm/GlobalValue.h" |
| 13 | #include "llvm/Module.h" |
| 14 | #include "llvm/Analysis/SlotCalculator.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | #include <algorithm> |
Vikram S. Adve | 3441043 | 2001-10-14 23:17:20 +0000 | [diff] [blame] | 16 | #include <hash_map> |
| 17 | #include <strstream.h> |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | #include <assert.h> |
| 19 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 20 | ConstPoolBool *ConstPoolBool::True = new ConstPoolBool(true); |
| 21 | ConstPoolBool *ConstPoolBool::False = new ConstPoolBool(false); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // ConstPoolVal Class |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | // Specialize setName to take care of symbol table majik |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 29 | void ConstPoolVal::setName(const string &Name, SymbolTable *ST) { |
| 30 | assert(ST && "Type::setName - Must provide symbol table argument!"); |
| 31 | |
| 32 | if (Name.size()) ST->insert(Name, this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Static constructor to create a '0' constant of arbitrary type... |
| 36 | ConstPoolVal *ConstPoolVal::getNullConstant(const Type *Ty) { |
| 37 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 38 | case Type::BoolTyID: return ConstPoolBool::get(false); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | case Type::SByteTyID: |
| 40 | case Type::ShortTyID: |
| 41 | case Type::IntTyID: |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 42 | case Type::LongTyID: return ConstPoolSInt::get(Ty, 0); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 43 | |
| 44 | case Type::UByteTyID: |
| 45 | case Type::UShortTyID: |
| 46 | case Type::UIntTyID: |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 47 | case Type::ULongTyID: return ConstPoolUInt::get(Ty, 0); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 48 | |
| 49 | case Type::FloatTyID: |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 50 | case Type::DoubleTyID: return ConstPoolFP::get(Ty, 0); |
Chris Lattner | 436248f | 2001-09-30 20:14:07 +0000 | [diff] [blame] | 51 | |
| 52 | case Type::PointerTyID: |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 53 | return ConstPoolPointerNull::get(cast<PointerType>(Ty)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 54 | default: |
| 55 | return 0; |
| 56 | } |
| 57 | } |
| 58 | |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 59 | #ifndef NDEBUG |
| 60 | #include "llvm/Assembly/Writer.h" |
| 61 | #endif |
| 62 | |
| 63 | void ConstPoolVal::destroyConstantImpl() { |
| 64 | // When a ConstPoolVal is destroyed, there may be lingering |
| 65 | // references to the constant by other constants in the constant pool. These |
| 66 | // constants are implicitly dependant on the module that is being deleted, |
| 67 | // but they don't know that. Because we only find out when the CPV is |
| 68 | // deleted, we must now notify all of our users (that should only be |
| 69 | // ConstPoolVals) that they are, in fact, invalid now and should be deleted. |
| 70 | // |
| 71 | while (!use_empty()) { |
| 72 | Value *V = use_back(); |
| 73 | #ifndef NDEBUG // Only in -g mode... |
| 74 | if (!isa<ConstPoolVal>(V)) { |
| 75 | cerr << "While deleting: " << this << endl; |
| 76 | cerr << "Use still stuck around after Def is destroyed: " << V << endl; |
| 77 | } |
| 78 | #endif |
| 79 | assert(isa<ConstPoolVal>(V) && "References remain to ConstPoolPointerRef!"); |
| 80 | ConstPoolVal *CPV = cast<ConstPoolVal>(V); |
| 81 | CPV->destroyConstant(); |
| 82 | |
| 83 | // The constant should remove itself from our use list... |
| 84 | assert((use_empty() || use_back() == V) && "Constant not removed!"); |
| 85 | } |
| 86 | |
| 87 | // Value has no outstanding references it is safe to delete it now... |
| 88 | delete this; |
Chris Lattner | 3856934 | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 89 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 90 | |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | // ConstPoolXXX Classes |
| 93 | //===----------------------------------------------------------------------===// |
| 94 | |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | // Normal Constructors |
| 97 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 98 | ConstPoolBool::ConstPoolBool(bool V) : ConstPoolVal(Type::BoolTy) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 99 | Val = V; |
| 100 | } |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 101 | |
| 102 | ConstPoolInt::ConstPoolInt(const Type *Ty, uint64_t V) : ConstPoolVal(Ty) { |
| 103 | Val.Unsigned = V; |
Chris Lattner | 7309d66 | 2001-07-21 19:16:08 +0000 | [diff] [blame] | 104 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 106 | ConstPoolSInt::ConstPoolSInt(const Type *Ty, int64_t V) : ConstPoolInt(Ty, V) { |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 107 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 110 | ConstPoolUInt::ConstPoolUInt(const Type *Ty, uint64_t V) : ConstPoolInt(Ty, V) { |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 111 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 114 | ConstPoolFP::ConstPoolFP(const Type *Ty, double V) : ConstPoolVal(Ty) { |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 115 | assert(isValueValidForType(Ty, V) && "Value too large for type!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 116 | Val = V; |
| 117 | } |
| 118 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 119 | ConstPoolArray::ConstPoolArray(const ArrayType *T, |
| 120 | const vector<ConstPoolVal*> &V) |
| 121 | : ConstPoolVal(T) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 122 | for (unsigned i = 0; i < V.size(); i++) { |
| 123 | assert(V[i]->getType() == T->getElementType()); |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 124 | Operands.push_back(Use(V[i], this)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 128 | ConstPoolStruct::ConstPoolStruct(const StructType *T, |
| 129 | const vector<ConstPoolVal*> &V) |
| 130 | : ConstPoolVal(T) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 131 | const StructType::ElementTypes &ETypes = T->getElementTypes(); |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 133 | for (unsigned i = 0; i < V.size(); i++) { |
| 134 | assert(V[i]->getType() == ETypes[i]); |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 135 | Operands.push_back(Use(V[i], this)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Chris Lattner | 7fac070 | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 139 | ConstPoolPointerReference::ConstPoolPointerReference(GlobalValue *GV) |
Chris Lattner | 60e0dd7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 140 | : ConstPoolPointer(GV->getType()) { |
| 141 | Operands.push_back(Use(GV, this)); |
| 142 | } |
| 143 | |
| 144 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 145 | |
| 146 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 147 | // getStrValue implementations |
| 148 | |
| 149 | string ConstPoolBool::getStrValue() const { |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 150 | return Val ? "true" : "false"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | string ConstPoolSInt::getStrValue() const { |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 154 | return itostr(Val.Signed); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | string ConstPoolUInt::getStrValue() const { |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 158 | return utostr(Val.Unsigned); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | string ConstPoolFP::getStrValue() const { |
Chris Lattner | d06dd69 | 2001-07-15 00:18:39 +0000 | [diff] [blame] | 162 | return ftostr(Val); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 165 | string ConstPoolArray::getStrValue() const { |
Vikram S. Adve | 3441043 | 2001-10-14 23:17:20 +0000 | [diff] [blame] | 166 | string Result; |
| 167 | strstream makeString; |
| 168 | |
| 169 | // As a special case, print the array as a string if it is an array of |
| 170 | // ubytes or an array of sbytes with positive values. |
| 171 | // |
| 172 | const Type* elType = cast<ArrayType>(this->getType())->getElementType(); |
| 173 | bool isString = (elType == Type::SByteTy || elType == Type::UByteTy) |
| 174 | && Operands.size() > 0; |
| 175 | if (isString) { |
| 176 | for (unsigned i = 0; i < Operands.size(); i++) { |
| 177 | if (elType == Type::SByteTy && cast<ConstPoolSInt>(Operands[i]) < 0) |
| 178 | { |
| 179 | isString = false; |
| 180 | break; |
| 181 | } |
| 182 | // else it is a legal char and is safe to cast to an unsigned int |
| 183 | uint64_t c = ((elType == Type::SByteTy) |
| 184 | ? (uint64_t) cast<ConstPoolSInt>(Operands[i])->getValue() |
| 185 | : cast<ConstPoolUInt>(Operands[i])->getValue()); |
| 186 | makeString << (char) c; |
| 187 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 188 | } |
Vikram S. Adve | 3441043 | 2001-10-14 23:17:20 +0000 | [diff] [blame] | 189 | |
| 190 | if (isString) { |
| 191 | makeString << ends; |
| 192 | Result = "c\""; |
| 193 | Result += makeString.str(); |
| 194 | Result += "\""; |
| 195 | free(makeString.str()); |
| 196 | } else { |
| 197 | Result = "["; |
| 198 | if (Operands.size()) { |
| 199 | Result += " " + Operands[0]->getType()->getDescription() + |
| 200 | " " + cast<ConstPoolVal>(Operands[0])->getStrValue(); |
| 201 | for (unsigned i = 1; i < Operands.size(); i++) |
| 202 | Result += ", " + Operands[i]->getType()->getDescription() + |
| 203 | " " + cast<ConstPoolVal>(Operands[i])->getStrValue(); |
| 204 | } |
| 205 | Result += " ]"; |
| 206 | } |
| 207 | |
| 208 | return Result; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | string ConstPoolStruct::getStrValue() const { |
| 212 | string Result = "{"; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 213 | if (Operands.size()) { |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 214 | Result += " " + Operands[0]->getType()->getDescription() + |
Chris Lattner | 8f19112 | 2001-10-01 18:26:53 +0000 | [diff] [blame] | 215 | " " + cast<ConstPoolVal>(Operands[0])->getStrValue(); |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 216 | for (unsigned i = 1; i < Operands.size(); i++) |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 217 | Result += ", " + Operands[i]->getType()->getDescription() + |
Chris Lattner | 8f19112 | 2001-10-01 18:26:53 +0000 | [diff] [blame] | 218 | " " + cast<ConstPoolVal>(Operands[i])->getStrValue(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | return Result + " }"; |
| 222 | } |
| 223 | |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 224 | string ConstPoolPointerNull::getStrValue() const { |
Chris Lattner | 436248f | 2001-09-30 20:14:07 +0000 | [diff] [blame] | 225 | return "null"; |
| 226 | } |
| 227 | |
Chris Lattner | 60e0dd7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 228 | string ConstPoolPointerReference::getStrValue() const { |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 229 | const GlobalValue *V = getValue(); |
| 230 | if (V->hasName()) return "%" + V->getName(); |
| 231 | |
| 232 | SlotCalculator *Table = new SlotCalculator(V->getParent(), true); |
| 233 | int Slot = Table->getValSlot(V); |
| 234 | delete Table; |
| 235 | |
| 236 | if (Slot >= 0) return string(" %") + itostr(Slot); |
| 237 | else return "<pointer reference badref>"; |
Chris Lattner | 60e0dd7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 240 | |
| 241 | //===----------------------------------------------------------------------===// |
| 242 | // classof implementations |
| 243 | |
| 244 | bool ConstPoolInt::classof(const ConstPoolVal *CPV) { |
| 245 | return CPV->getType()->isIntegral(); |
| 246 | } |
| 247 | bool ConstPoolSInt::classof(const ConstPoolVal *CPV) { |
| 248 | return CPV->getType()->isSigned(); |
| 249 | } |
| 250 | bool ConstPoolUInt::classof(const ConstPoolVal *CPV) { |
| 251 | return CPV->getType()->isUnsigned(); |
| 252 | } |
| 253 | bool ConstPoolFP::classof(const ConstPoolVal *CPV) { |
| 254 | const Type *Ty = CPV->getType(); |
| 255 | return Ty == Type::FloatTy || Ty == Type::DoubleTy; |
| 256 | } |
| 257 | bool ConstPoolArray::classof(const ConstPoolVal *CPV) { |
| 258 | return isa<ArrayType>(CPV->getType()); |
| 259 | } |
| 260 | bool ConstPoolStruct::classof(const ConstPoolVal *CPV) { |
| 261 | return isa<StructType>(CPV->getType()); |
| 262 | } |
| 263 | bool ConstPoolPointer::classof(const ConstPoolVal *CPV) { |
| 264 | return isa<PointerType>(CPV->getType()); |
| 265 | } |
| 266 | |
| 267 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 268 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 269 | // isValueValidForType implementations |
| 270 | |
| 271 | bool ConstPoolSInt::isValueValidForType(const Type *Ty, int64_t Val) { |
| 272 | switch (Ty->getPrimitiveID()) { |
| 273 | default: |
| 274 | return false; // These can't be represented as integers!!! |
| 275 | |
| 276 | // Signed types... |
| 277 | case Type::SByteTyID: |
| 278 | return (Val <= INT8_MAX && Val >= INT8_MIN); |
| 279 | case Type::ShortTyID: |
| 280 | return (Val <= INT16_MAX && Val >= INT16_MIN); |
| 281 | case Type::IntTyID: |
| 282 | return (Val <= INT32_MAX && Val >= INT32_MIN); |
| 283 | case Type::LongTyID: |
| 284 | return true; // This is the largest type... |
| 285 | } |
| 286 | assert(0 && "WTF?"); |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | bool ConstPoolUInt::isValueValidForType(const Type *Ty, uint64_t Val) { |
| 291 | switch (Ty->getPrimitiveID()) { |
| 292 | default: |
| 293 | return false; // These can't be represented as integers!!! |
| 294 | |
| 295 | // Unsigned types... |
| 296 | case Type::UByteTyID: |
| 297 | return (Val <= UINT8_MAX); |
| 298 | case Type::UShortTyID: |
| 299 | return (Val <= UINT16_MAX); |
| 300 | case Type::UIntTyID: |
| 301 | return (Val <= UINT32_MAX); |
| 302 | case Type::ULongTyID: |
| 303 | return true; // This is the largest type... |
| 304 | } |
| 305 | assert(0 && "WTF?"); |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | bool ConstPoolFP::isValueValidForType(const Type *Ty, double Val) { |
| 310 | switch (Ty->getPrimitiveID()) { |
| 311 | default: |
| 312 | return false; // These can't be represented as floating point! |
| 313 | |
| 314 | // TODO: Figure out how to test if a double can be cast to a float! |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 315 | case Type::FloatTyID: |
Chris Lattner | d06dd69 | 2001-07-15 00:18:39 +0000 | [diff] [blame] | 316 | /* |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 317 | return (Val <= UINT8_MAX); |
| 318 | */ |
| 319 | case Type::DoubleTyID: |
| 320 | return true; // This is the largest type... |
| 321 | } |
| 322 | }; |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 324 | //===----------------------------------------------------------------------===// |
| 325 | // Hash Function Implementations |
| 326 | #if 0 |
| 327 | unsigned ConstPoolSInt::hash(const Type *Ty, int64_t V) { |
| 328 | return unsigned(Ty->getPrimitiveID() ^ V); |
| 329 | } |
| 330 | |
| 331 | unsigned ConstPoolUInt::hash(const Type *Ty, uint64_t V) { |
| 332 | return unsigned(Ty->getPrimitiveID() ^ V); |
| 333 | } |
| 334 | |
| 335 | unsigned ConstPoolFP::hash(const Type *Ty, double V) { |
| 336 | return Ty->getPrimitiveID() ^ unsigned(V); |
| 337 | } |
| 338 | |
| 339 | unsigned ConstPoolArray::hash(const ArrayType *Ty, |
| 340 | const vector<ConstPoolVal*> &V) { |
| 341 | unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7); |
| 342 | for (unsigned i = 0; i < V.size(); ++i) |
| 343 | Result ^= V[i]->getHash() << (i & 7); |
| 344 | return Result; |
| 345 | } |
| 346 | |
| 347 | unsigned ConstPoolStruct::hash(const StructType *Ty, |
| 348 | const vector<ConstPoolVal*> &V) { |
| 349 | unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7); |
| 350 | for (unsigned i = 0; i < V.size(); ++i) |
| 351 | Result ^= V[i]->getHash() << (i & 7); |
| 352 | return Result; |
| 353 | } |
| 354 | #endif |
| 355 | |
| 356 | //===----------------------------------------------------------------------===// |
| 357 | // Factory Function Implementation |
| 358 | |
| 359 | template<class ValType, class ConstPoolClass> |
| 360 | struct ValueMap { |
| 361 | typedef pair<const Type*, ValType> ConstHashKey; |
| 362 | map<ConstHashKey, ConstPoolClass *> Map; |
| 363 | |
| 364 | inline ConstPoolClass *get(const Type *Ty, ValType V) { |
| 365 | map<ConstHashKey,ConstPoolClass *>::iterator I = |
| 366 | Map.find(ConstHashKey(Ty, V)); |
| 367 | return (I != Map.end()) ? I->second : 0; |
| 368 | } |
| 369 | |
| 370 | inline void add(const Type *Ty, ValType V, ConstPoolClass *CP) { |
| 371 | Map.insert(make_pair(ConstHashKey(Ty, V), CP)); |
| 372 | } |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 373 | |
| 374 | inline void remove(ConstPoolClass *CP) { |
| 375 | for (map<ConstHashKey,ConstPoolClass *>::iterator I = Map.begin(), |
| 376 | E = Map.end(); I != E;++I) |
| 377 | if (I->second == CP) { |
| 378 | Map.erase(I); |
| 379 | return; |
| 380 | } |
| 381 | } |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | //---- ConstPoolUInt::get() and ConstPoolSInt::get() implementations... |
| 385 | // |
| 386 | static ValueMap<uint64_t, ConstPoolInt> IntConstants; |
| 387 | |
| 388 | ConstPoolSInt *ConstPoolSInt::get(const Type *Ty, int64_t V) { |
| 389 | ConstPoolSInt *Result = (ConstPoolSInt*)IntConstants.get(Ty, (uint64_t)V); |
| 390 | if (!Result) // If no preexisting value, create one now... |
| 391 | IntConstants.add(Ty, V, Result = new ConstPoolSInt(Ty, V)); |
| 392 | return Result; |
| 393 | } |
| 394 | |
| 395 | ConstPoolUInt *ConstPoolUInt::get(const Type *Ty, uint64_t V) { |
| 396 | ConstPoolUInt *Result = (ConstPoolUInt*)IntConstants.get(Ty, V); |
| 397 | if (!Result) // If no preexisting value, create one now... |
| 398 | IntConstants.add(Ty, V, Result = new ConstPoolUInt(Ty, V)); |
| 399 | return Result; |
| 400 | } |
| 401 | |
| 402 | ConstPoolInt *ConstPoolInt::get(const Type *Ty, unsigned char V) { |
| 403 | assert(V <= 127 && "Can only be used with very small positive constants!"); |
| 404 | if (Ty->isSigned()) return ConstPoolSInt::get(Ty, V); |
| 405 | return ConstPoolUInt::get(Ty, V); |
| 406 | } |
| 407 | |
| 408 | //---- ConstPoolFP::get() implementation... |
| 409 | // |
| 410 | static ValueMap<double, ConstPoolFP> FPConstants; |
| 411 | |
| 412 | ConstPoolFP *ConstPoolFP::get(const Type *Ty, double V) { |
| 413 | ConstPoolFP *Result = FPConstants.get(Ty, V); |
| 414 | if (!Result) // If no preexisting value, create one now... |
| 415 | FPConstants.add(Ty, V, Result = new ConstPoolFP(Ty, V)); |
| 416 | return Result; |
| 417 | } |
| 418 | |
| 419 | //---- ConstPoolArray::get() implementation... |
| 420 | // |
| 421 | static ValueMap<vector<ConstPoolVal*>, ConstPoolArray> ArrayConstants; |
Vikram S. Adve | 3441043 | 2001-10-14 23:17:20 +0000 | [diff] [blame] | 422 | static hash_map<const char*, ConstPoolArray*> StringConstants; |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 423 | |
| 424 | ConstPoolArray *ConstPoolArray::get(const ArrayType *Ty, |
| 425 | const vector<ConstPoolVal*> &V) { |
| 426 | ConstPoolArray *Result = ArrayConstants.get(Ty, V); |
| 427 | if (!Result) // If no preexisting value, create one now... |
| 428 | ArrayConstants.add(Ty, V, Result = new ConstPoolArray(Ty, V)); |
| 429 | return Result; |
| 430 | } |
| 431 | |
Vikram S. Adve | 3441043 | 2001-10-14 23:17:20 +0000 | [diff] [blame] | 432 | ConstPoolArray *ConstPoolArray::get(const string& stringConstant) { |
| 433 | ConstPoolArray *Result = StringConstants[stringConstant.c_str()]; |
| 434 | if (Result == NULL) { |
| 435 | ArrayType *aty = ArrayType::get(Type::UByteTy/*,stringConstant.length()*/); |
| 436 | vector<ConstPoolVal*> charVals; |
| 437 | for (const char *c = stringConstant.c_str(); *c; ++c) { |
| 438 | if (isprint(*c)) |
| 439 | charVals.push_back(ConstPoolUInt::get(Type::UByteTy, *c)); |
| 440 | else { |
| 441 | char charString[3]; |
| 442 | sprintf(charString, "\\%02X", *c); |
| 443 | charVals.push_back(ConstPoolUInt::get(Type::UByteTy, charString[0])); |
| 444 | charVals.push_back(ConstPoolUInt::get(Type::UByteTy, charString[1])); |
| 445 | charVals.push_back(ConstPoolUInt::get(Type::UByteTy, charString[2])); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | // Append "\00" which is the null character in LLVM |
| 450 | charVals.push_back(ConstPoolUInt::get(Type::UByteTy, '\\')); |
| 451 | charVals.push_back(ConstPoolUInt::get(Type::UByteTy, '0')); |
| 452 | charVals.push_back(ConstPoolUInt::get(Type::UByteTy, '0')); |
| 453 | |
| 454 | Result = ConstPoolArray::get(aty, charVals); |
| 455 | StringConstants[stringConstant.c_str()] = Result; |
| 456 | } |
| 457 | return Result; |
| 458 | } |
| 459 | |
| 460 | |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 461 | // destroyConstant - Remove the constant from the constant table... |
| 462 | // |
| 463 | void ConstPoolArray::destroyConstant() { |
| 464 | ArrayConstants.remove(this); |
| 465 | destroyConstantImpl(); |
| 466 | } |
| 467 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 468 | //---- ConstPoolStruct::get() implementation... |
| 469 | // |
| 470 | static ValueMap<vector<ConstPoolVal*>, ConstPoolStruct> StructConstants; |
| 471 | |
| 472 | ConstPoolStruct *ConstPoolStruct::get(const StructType *Ty, |
| 473 | const vector<ConstPoolVal*> &V) { |
| 474 | ConstPoolStruct *Result = StructConstants.get(Ty, V); |
| 475 | if (!Result) // If no preexisting value, create one now... |
| 476 | StructConstants.add(Ty, V, Result = new ConstPoolStruct(Ty, V)); |
| 477 | return Result; |
| 478 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 479 | |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 480 | // destroyConstant - Remove the constant from the constant table... |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 481 | // |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 482 | void ConstPoolStruct::destroyConstant() { |
| 483 | StructConstants.remove(this); |
| 484 | destroyConstantImpl(); |
| 485 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 486 | |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 487 | //---- ConstPoolPointerNull::get() implementation... |
| 488 | // |
| 489 | static ValueMap<char, ConstPoolPointerNull> NullPtrConstants; |
| 490 | |
| 491 | ConstPoolPointerNull *ConstPoolPointerNull::get(const PointerType *Ty) { |
| 492 | ConstPoolPointerNull *Result = NullPtrConstants.get(Ty, 0); |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 493 | if (!Result) // If no preexisting value, create one now... |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 494 | NullPtrConstants.add(Ty, 0, Result = new ConstPoolPointerNull(Ty)); |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 495 | return Result; |
| 496 | } |
| 497 | |
Chris Lattner | 2503325 | 2001-10-03 19:28:15 +0000 | [diff] [blame] | 498 | //---- ConstPoolPointerReference::get() implementation... |
| 499 | // |
| 500 | ConstPoolPointerReference *ConstPoolPointerReference::get(GlobalValue *GV) { |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 501 | assert(GV->getParent() && "Global Value must be attached to a module!"); |
| 502 | |
| 503 | // The Module handles the pointer reference sharing... |
| 504 | return GV->getParent()->getConstPoolPointerReference(GV); |
| 505 | } |
| 506 | |
| 507 | |
| 508 | void ConstPoolPointerReference::mutateReference(GlobalValue *NewGV) { |
| 509 | getValue()->getParent()->mutateConstPoolPointerReference(getValue(), NewGV); |
| 510 | Operands[0] = NewGV; |
Chris Lattner | 2503325 | 2001-10-03 19:28:15 +0000 | [diff] [blame] | 511 | } |