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