blob: dd301716fb1c532642490a9d95fa6bc3982cd74e [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- 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 Lattner2f7c9632001-06-06 20:29:01 +00009#include "llvm/DerivedTypes.h"
10#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000011#include "llvm/GlobalValue.h"
12#include "llvm/Module.h"
13#include "llvm/Analysis/SlotCalculator.h"
Chris Lattner5de22042001-11-27 00:03:19 +000014#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000015#include <algorithm>
16#include <assert.h>
17
Chris Lattner49d855c2001-09-07 16:46:31 +000018ConstPoolBool *ConstPoolBool::True = new ConstPoolBool(true);
19ConstPoolBool *ConstPoolBool::False = new ConstPoolBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000020
Chris Lattner9655e542001-07-20 19:16:02 +000021
Chris Lattner2f7c9632001-06-06 20:29:01 +000022//===----------------------------------------------------------------------===//
23// ConstPoolVal Class
24//===----------------------------------------------------------------------===//
25
26// Specialize setName to take care of symbol table majik
Chris Lattner49d855c2001-09-07 16:46:31 +000027void 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 Lattner2f7c9632001-06-06 20:29:01 +000031}
32
33// Static constructor to create a '0' constant of arbitrary type...
34ConstPoolVal *ConstPoolVal::getNullConstant(const Type *Ty) {
35 switch (Ty->getPrimitiveID()) {
Chris Lattner49d855c2001-09-07 16:46:31 +000036 case Type::BoolTyID: return ConstPoolBool::get(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000037 case Type::SByteTyID:
38 case Type::ShortTyID:
39 case Type::IntTyID:
Chris Lattner49d855c2001-09-07 16:46:31 +000040 case Type::LongTyID: return ConstPoolSInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000041
42 case Type::UByteTyID:
43 case Type::UShortTyID:
44 case Type::UIntTyID:
Chris Lattner49d855c2001-09-07 16:46:31 +000045 case Type::ULongTyID: return ConstPoolUInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000046
47 case Type::FloatTyID:
Chris Lattner49d855c2001-09-07 16:46:31 +000048 case Type::DoubleTyID: return ConstPoolFP::get(Ty, 0);
Chris Lattner436248f2001-09-30 20:14:07 +000049
50 case Type::PointerTyID:
Chris Lattnerd7a73302001-10-13 06:57:33 +000051 return ConstPoolPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2f7c9632001-06-06 20:29:01 +000052 default:
53 return 0;
54 }
55}
56
Chris Lattnerd7a73302001-10-13 06:57:33 +000057#ifndef NDEBUG
58#include "llvm/Assembly/Writer.h"
59#endif
60
61void 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 Lattner38569342001-10-01 20:11:19 +000087}
Chris Lattner2f7c9632001-06-06 20:29:01 +000088
89//===----------------------------------------------------------------------===//
90// ConstPoolXXX Classes
91//===----------------------------------------------------------------------===//
92
93//===----------------------------------------------------------------------===//
94// Normal Constructors
95
Chris Lattner49d855c2001-09-07 16:46:31 +000096ConstPoolBool::ConstPoolBool(bool V) : ConstPoolVal(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000097 Val = V;
98}
Chris Lattner49d855c2001-09-07 16:46:31 +000099
100ConstPoolInt::ConstPoolInt(const Type *Ty, uint64_t V) : ConstPoolVal(Ty) {
101 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000102}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000103
Chris Lattner49d855c2001-09-07 16:46:31 +0000104ConstPoolSInt::ConstPoolSInt(const Type *Ty, int64_t V) : ConstPoolInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000105 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000106}
107
Chris Lattner49d855c2001-09-07 16:46:31 +0000108ConstPoolUInt::ConstPoolUInt(const Type *Ty, uint64_t V) : ConstPoolInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000109 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000110}
111
Chris Lattner49d855c2001-09-07 16:46:31 +0000112ConstPoolFP::ConstPoolFP(const Type *Ty, double V) : ConstPoolVal(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000113 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000114 Val = V;
115}
116
Chris Lattner49d855c2001-09-07 16:46:31 +0000117ConstPoolArray::ConstPoolArray(const ArrayType *T,
118 const vector<ConstPoolVal*> &V)
119 : ConstPoolVal(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120 for (unsigned i = 0; i < V.size(); i++) {
121 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000122 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000123 }
124}
125
Chris Lattner49d855c2001-09-07 16:46:31 +0000126ConstPoolStruct::ConstPoolStruct(const StructType *T,
127 const vector<ConstPoolVal*> &V)
128 : ConstPoolVal(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129 const StructType::ElementTypes &ETypes = T->getElementTypes();
Chris Lattner49d855c2001-09-07 16:46:31 +0000130
Chris Lattner2f7c9632001-06-06 20:29:01 +0000131 for (unsigned i = 0; i < V.size(); i++) {
132 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000133 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000134 }
135}
136
Chris Lattner162ed4d2001-10-15 13:21:42 +0000137ConstPoolPointerRef::ConstPoolPointerRef(GlobalValue *GV)
Chris Lattner60e0dd72001-10-03 06:12:09 +0000138 : ConstPoolPointer(GV->getType()) {
139 Operands.push_back(Use(GV, this));
140}
141
142
Chris Lattner2f7c9632001-06-06 20:29:01 +0000143
144//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000145// getStrValue implementations
146
Chris Lattner239a1bd2001-10-29 13:27:09 +0000147string ConstPoolBool::getStrValue() const {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000148 return Val ? "true" : "false";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149}
150
Chris Lattner239a1bd2001-10-29 13:27:09 +0000151string ConstPoolSInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000152 return itostr(Val.Signed);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000153}
154
Chris Lattner239a1bd2001-10-29 13:27:09 +0000155string ConstPoolUInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000156 return utostr(Val.Unsigned);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000157}
158
Chris Lattner239a1bd2001-10-29 13:27:09 +0000159string ConstPoolFP::getStrValue() const {
Chris Lattnerd06dd692001-07-15 00:18:39 +0000160 return ftostr(Val);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000161}
162
Chris Lattner239a1bd2001-10-29 13:27:09 +0000163string ConstPoolArray::getStrValue() const {
Vikram S. Adveeada6b12001-10-28 21:48:05 +0000164 string Result;
165
Chris Lattner239a1bd2001-10-29 13:27:09 +0000166 // 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 //
169 const Type *ETy = cast<ArrayType>(getType())->getElementType();
170 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
171
172 if (ETy == Type::SByteTy) {
173 for (unsigned i = 0; i < Operands.size(); ++i)
174 if (ETy == Type::SByteTy &&
175 cast<ConstPoolSInt>(Operands[i])->getValue() < 0) {
176 isString = false;
177 break;
178 }
179 }
180
181 if (isString) {
182 Result = "c\"";
Chris Lattner78e11ae2001-10-15 00:05:03 +0000183 for (unsigned i = 0; i < Operands.size(); ++i) {
Chris Lattner8f80fe02001-10-14 23:54:12 +0000184 unsigned char C = (ETy == Type::SByteTy) ?
185 (unsigned char)cast<ConstPoolSInt>(Operands[i])->getValue() :
186 (unsigned char)cast<ConstPoolUInt>(Operands[i])->getValue();
187
188 if (isprint(C)) {
189 Result += C;
Chris Lattner239a1bd2001-10-29 13:27:09 +0000190 } else {
191 Result += '\\';
192 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
193 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
Chris Lattner8f80fe02001-10-14 23:54:12 +0000194 }
195 }
Vikram S. Adve34410432001-10-14 23:17:20 +0000196 Result += "\"";
Chris Lattner8f80fe02001-10-14 23:54:12 +0000197
Vikram S. Adve34410432001-10-14 23:17:20 +0000198 } else {
199 Result = "[";
200 if (Operands.size()) {
201 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner239a1bd2001-10-29 13:27:09 +0000202 " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
Vikram S. Adve34410432001-10-14 23:17:20 +0000203 for (unsigned i = 1; i < Operands.size(); i++)
204 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner239a1bd2001-10-29 13:27:09 +0000205 " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
Vikram S. Adve34410432001-10-14 23:17:20 +0000206 }
207 Result += " ]";
208 }
209
210 return Result;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000211}
212
Chris Lattner239a1bd2001-10-29 13:27:09 +0000213string ConstPoolStruct::getStrValue() const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214 string Result = "{";
Chris Lattnera073acb2001-07-07 08:36:50 +0000215 if (Operands.size()) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000216 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner239a1bd2001-10-29 13:27:09 +0000217 " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
Chris Lattnera073acb2001-07-07 08:36:50 +0000218 for (unsigned i = 1; i < Operands.size(); i++)
Chris Lattner49d855c2001-09-07 16:46:31 +0000219 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner239a1bd2001-10-29 13:27:09 +0000220 " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221 }
222
223 return Result + " }";
224}
225
Chris Lattner239a1bd2001-10-29 13:27:09 +0000226string ConstPoolPointerNull::getStrValue() const {
Chris Lattner436248f2001-09-30 20:14:07 +0000227 return "null";
228}
229
Chris Lattner239a1bd2001-10-29 13:27:09 +0000230string ConstPoolPointerRef::getStrValue() const {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000231 const GlobalValue *V = getValue();
232 if (V->hasName()) return "%" + V->getName();
233
234 SlotCalculator *Table = new SlotCalculator(V->getParent(), true);
235 int Slot = Table->getValSlot(V);
236 delete Table;
237
238 if (Slot >= 0) return string(" %") + itostr(Slot);
239 else return "<pointer reference badref>";
Chris Lattner60e0dd72001-10-03 06:12:09 +0000240}
241
Chris Lattnerd7a73302001-10-13 06:57:33 +0000242
243//===----------------------------------------------------------------------===//
244// classof implementations
245
246bool ConstPoolInt::classof(const ConstPoolVal *CPV) {
247 return CPV->getType()->isIntegral();
248}
249bool ConstPoolSInt::classof(const ConstPoolVal *CPV) {
250 return CPV->getType()->isSigned();
251}
252bool ConstPoolUInt::classof(const ConstPoolVal *CPV) {
253 return CPV->getType()->isUnsigned();
254}
255bool ConstPoolFP::classof(const ConstPoolVal *CPV) {
256 const Type *Ty = CPV->getType();
257 return Ty == Type::FloatTy || Ty == Type::DoubleTy;
258}
259bool ConstPoolArray::classof(const ConstPoolVal *CPV) {
260 return isa<ArrayType>(CPV->getType());
261}
262bool ConstPoolStruct::classof(const ConstPoolVal *CPV) {
263 return isa<StructType>(CPV->getType());
264}
265bool ConstPoolPointer::classof(const ConstPoolVal *CPV) {
266 return isa<PointerType>(CPV->getType());
267}
268
269
Chris Lattner2f7c9632001-06-06 20:29:01 +0000270//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000271// isValueValidForType implementations
272
273bool ConstPoolSInt::isValueValidForType(const Type *Ty, int64_t Val) {
274 switch (Ty->getPrimitiveID()) {
275 default:
276 return false; // These can't be represented as integers!!!
277
278 // Signed types...
279 case Type::SByteTyID:
280 return (Val <= INT8_MAX && Val >= INT8_MIN);
281 case Type::ShortTyID:
282 return (Val <= INT16_MAX && Val >= INT16_MIN);
283 case Type::IntTyID:
284 return (Val <= INT32_MAX && Val >= INT32_MIN);
285 case Type::LongTyID:
286 return true; // This is the largest type...
287 }
288 assert(0 && "WTF?");
289 return false;
290}
291
292bool ConstPoolUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
293 switch (Ty->getPrimitiveID()) {
294 default:
295 return false; // These can't be represented as integers!!!
296
297 // Unsigned types...
298 case Type::UByteTyID:
299 return (Val <= UINT8_MAX);
300 case Type::UShortTyID:
301 return (Val <= UINT16_MAX);
302 case Type::UIntTyID:
303 return (Val <= UINT32_MAX);
304 case Type::ULongTyID:
305 return true; // This is the largest type...
306 }
307 assert(0 && "WTF?");
308 return false;
309}
310
311bool ConstPoolFP::isValueValidForType(const Type *Ty, double Val) {
312 switch (Ty->getPrimitiveID()) {
313 default:
314 return false; // These can't be represented as floating point!
315
316 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000317 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000318 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000319 return (Val <= UINT8_MAX);
320 */
321 case Type::DoubleTyID:
322 return true; // This is the largest type...
323 }
324};
Chris Lattner9655e542001-07-20 19:16:02 +0000325
Chris Lattner49d855c2001-09-07 16:46:31 +0000326//===----------------------------------------------------------------------===//
327// Hash Function Implementations
328#if 0
329unsigned ConstPoolSInt::hash(const Type *Ty, int64_t V) {
330 return unsigned(Ty->getPrimitiveID() ^ V);
331}
332
333unsigned ConstPoolUInt::hash(const Type *Ty, uint64_t V) {
334 return unsigned(Ty->getPrimitiveID() ^ V);
335}
336
337unsigned ConstPoolFP::hash(const Type *Ty, double V) {
338 return Ty->getPrimitiveID() ^ unsigned(V);
339}
340
341unsigned ConstPoolArray::hash(const ArrayType *Ty,
342 const vector<ConstPoolVal*> &V) {
343 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
344 for (unsigned i = 0; i < V.size(); ++i)
345 Result ^= V[i]->getHash() << (i & 7);
346 return Result;
347}
348
349unsigned ConstPoolStruct::hash(const StructType *Ty,
350 const vector<ConstPoolVal*> &V) {
351 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
352 for (unsigned i = 0; i < V.size(); ++i)
353 Result ^= V[i]->getHash() << (i & 7);
354 return Result;
355}
356#endif
357
358//===----------------------------------------------------------------------===//
359// Factory Function Implementation
360
361template<class ValType, class ConstPoolClass>
362struct ValueMap {
363 typedef pair<const Type*, ValType> ConstHashKey;
364 map<ConstHashKey, ConstPoolClass *> Map;
365
366 inline ConstPoolClass *get(const Type *Ty, ValType V) {
367 map<ConstHashKey,ConstPoolClass *>::iterator I =
368 Map.find(ConstHashKey(Ty, V));
369 return (I != Map.end()) ? I->second : 0;
370 }
371
372 inline void add(const Type *Ty, ValType V, ConstPoolClass *CP) {
373 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
374 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000375
376 inline void remove(ConstPoolClass *CP) {
377 for (map<ConstHashKey,ConstPoolClass *>::iterator I = Map.begin(),
378 E = Map.end(); I != E;++I)
379 if (I->second == CP) {
380 Map.erase(I);
381 return;
382 }
383 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000384};
385
386//---- ConstPoolUInt::get() and ConstPoolSInt::get() implementations...
387//
388static ValueMap<uint64_t, ConstPoolInt> IntConstants;
389
390ConstPoolSInt *ConstPoolSInt::get(const Type *Ty, int64_t V) {
391 ConstPoolSInt *Result = (ConstPoolSInt*)IntConstants.get(Ty, (uint64_t)V);
392 if (!Result) // If no preexisting value, create one now...
393 IntConstants.add(Ty, V, Result = new ConstPoolSInt(Ty, V));
394 return Result;
395}
396
397ConstPoolUInt *ConstPoolUInt::get(const Type *Ty, uint64_t V) {
398 ConstPoolUInt *Result = (ConstPoolUInt*)IntConstants.get(Ty, V);
399 if (!Result) // If no preexisting value, create one now...
400 IntConstants.add(Ty, V, Result = new ConstPoolUInt(Ty, V));
401 return Result;
402}
403
404ConstPoolInt *ConstPoolInt::get(const Type *Ty, unsigned char V) {
405 assert(V <= 127 && "Can only be used with very small positive constants!");
406 if (Ty->isSigned()) return ConstPoolSInt::get(Ty, V);
407 return ConstPoolUInt::get(Ty, V);
408}
409
410//---- ConstPoolFP::get() implementation...
411//
412static ValueMap<double, ConstPoolFP> FPConstants;
413
414ConstPoolFP *ConstPoolFP::get(const Type *Ty, double V) {
415 ConstPoolFP *Result = FPConstants.get(Ty, V);
416 if (!Result) // If no preexisting value, create one now...
417 FPConstants.add(Ty, V, Result = new ConstPoolFP(Ty, V));
418 return Result;
419}
420
421//---- ConstPoolArray::get() implementation...
422//
423static ValueMap<vector<ConstPoolVal*>, ConstPoolArray> ArrayConstants;
424
425ConstPoolArray *ConstPoolArray::get(const ArrayType *Ty,
426 const vector<ConstPoolVal*> &V) {
427 ConstPoolArray *Result = ArrayConstants.get(Ty, V);
428 if (!Result) // If no preexisting value, create one now...
429 ArrayConstants.add(Ty, V, Result = new ConstPoolArray(Ty, V));
430 return Result;
431}
432
Chris Lattner8f80fe02001-10-14 23:54:12 +0000433// ConstPoolArray::get(const string&) - Return an array that is initialized to
434// contain the specified string. A null terminator is added to the specified
435// string so that it may be used in a natural way...
436//
437ConstPoolArray *ConstPoolArray::get(const string &Str) {
438 vector<ConstPoolVal*> ElementVals;
439
440 for (unsigned i = 0; i < Str.length(); ++i)
441 ElementVals.push_back(ConstPoolUInt::get(Type::UByteTy, Str[i]));
442
443 // Add a null terminator to the string...
444 ElementVals.push_back(ConstPoolUInt::get(Type::UByteTy, 0));
445
446 ArrayType *ATy = ArrayType::get(Type::UByteTy/*,stringConstant.length()*/);
447 return ConstPoolArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000448}
449
450
Chris Lattnerd7a73302001-10-13 06:57:33 +0000451// destroyConstant - Remove the constant from the constant table...
452//
453void ConstPoolArray::destroyConstant() {
454 ArrayConstants.remove(this);
455 destroyConstantImpl();
456}
457
Chris Lattner49d855c2001-09-07 16:46:31 +0000458//---- ConstPoolStruct::get() implementation...
459//
460static ValueMap<vector<ConstPoolVal*>, ConstPoolStruct> StructConstants;
461
462ConstPoolStruct *ConstPoolStruct::get(const StructType *Ty,
463 const vector<ConstPoolVal*> &V) {
464 ConstPoolStruct *Result = StructConstants.get(Ty, V);
465 if (!Result) // If no preexisting value, create one now...
466 StructConstants.add(Ty, V, Result = new ConstPoolStruct(Ty, V));
467 return Result;
468}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000469
Chris Lattnerd7a73302001-10-13 06:57:33 +0000470// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000471//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000472void ConstPoolStruct::destroyConstant() {
473 StructConstants.remove(this);
474 destroyConstantImpl();
475}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000476
Chris Lattnerd7a73302001-10-13 06:57:33 +0000477//---- ConstPoolPointerNull::get() implementation...
478//
479static ValueMap<char, ConstPoolPointerNull> NullPtrConstants;
480
481ConstPoolPointerNull *ConstPoolPointerNull::get(const PointerType *Ty) {
482 ConstPoolPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000483 if (!Result) // If no preexisting value, create one now...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000484 NullPtrConstants.add(Ty, 0, Result = new ConstPoolPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000485 return Result;
486}
487
Chris Lattner162ed4d2001-10-15 13:21:42 +0000488//---- ConstPoolPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000489//
Chris Lattner162ed4d2001-10-15 13:21:42 +0000490ConstPoolPointerRef *ConstPoolPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000491 assert(GV->getParent() && "Global Value must be attached to a module!");
492
493 // The Module handles the pointer reference sharing...
Chris Lattner162ed4d2001-10-15 13:21:42 +0000494 return GV->getParent()->getConstPoolPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000495}
496
497
Chris Lattner162ed4d2001-10-15 13:21:42 +0000498void ConstPoolPointerRef::mutateReference(GlobalValue *NewGV) {
499 getValue()->getParent()->mutateConstPoolPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000500 Operands[0] = NewGV;
Chris Lattner25033252001-10-03 19:28:15 +0000501}