blob: 8bf1d9cc199ad6cb51730b344b12ff84ca7733e0 [file] [log] [blame]
Chris Lattner3462ae32001-12-03 22:26:30 +00001//===-- ConstantVals.cpp - Implement Constant nodes --------------*- C++ -*--=//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
Chris Lattner3462ae32001-12-03 22:26:30 +00003// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +00004//
5//===----------------------------------------------------------------------===//
6
7#define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends...
Chris Lattner3462ae32001-12-03 22:26:30 +00008#include "llvm/ConstantVals.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"
Chris Lattner6915f8f2002-04-07 22:49:37 +000013#include "llvm/SlotCalculator.h"
Chris Lattner5de22042001-11-27 00:03:19 +000014#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000015#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000016
Chris Lattner7f74a562002-01-20 22:54:45 +000017using std::map;
18using std::pair;
19using std::make_pair;
20
Chris Lattner3462ae32001-12-03 22:26:30 +000021ConstantBool *ConstantBool::True = new ConstantBool(true);
22ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000023
Chris Lattner9655e542001-07-20 19:16:02 +000024
Chris Lattner2f7c9632001-06-06 20:29:01 +000025//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000026// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000027//===----------------------------------------------------------------------===//
28
29// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000030void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000031 assert(ST && "Type::setName - Must provide symbol table argument!");
32
33 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000034}
35
36// Static constructor to create a '0' constant of arbitrary type...
Chris Lattnera85386f2002-04-27 02:25:43 +000037Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000038 switch (Ty->getPrimitiveID()) {
Chris Lattner3462ae32001-12-03 22:26:30 +000039 case Type::BoolTyID: return ConstantBool::get(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000040 case Type::SByteTyID:
41 case Type::ShortTyID:
42 case Type::IntTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000043 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000044
45 case Type::UByteTyID:
46 case Type::UShortTyID:
47 case Type::UIntTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000048 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000049
50 case Type::FloatTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000051 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
Chris Lattner436248f2001-09-30 20:14:07 +000052
53 case Type::PointerTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000054 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2f7c9632001-06-06 20:29:01 +000055 default:
56 return 0;
57 }
58}
59
Chris Lattner3462ae32001-12-03 22:26:30 +000060void Constant::destroyConstantImpl() {
61 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000062 // references to the constant by other constants in the constant pool. These
63 // constants are implicitly dependant on the module that is being deleted,
64 // but they don't know that. Because we only find out when the CPV is
65 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000066 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000067 //
68 while (!use_empty()) {
69 Value *V = use_back();
70#ifndef NDEBUG // Only in -g mode...
Chris Lattner3462ae32001-12-03 22:26:30 +000071 if (!isa<Constant>(V)) {
Chris Lattnere026e922002-04-07 22:32:25 +000072 std::cerr << "While deleting: ";
73 dump();
74 std::cerr << "\nUse still stuck around after Def is destroyed: ";
75 V->dump();
76 std::cerr << "\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000077 }
78#endif
Chris Lattner3462ae32001-12-03 22:26:30 +000079 assert(isa<Constant>(V) && "References remain to ConstantPointerRef!");
80 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000081 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 Lattner38569342001-10-01 20:11:19 +000089}
Chris Lattner2f7c9632001-06-06 20:29:01 +000090
91//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000092// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +000093//===----------------------------------------------------------------------===//
94
95//===----------------------------------------------------------------------===//
96// Normal Constructors
97
Chris Lattner3462ae32001-12-03 22:26:30 +000098ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000099 Val = V;
100}
Chris Lattner49d855c2001-09-07 16:46:31 +0000101
Chris Lattner3462ae32001-12-03 22:26:30 +0000102ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000103 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000104}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000105
Chris Lattner3462ae32001-12-03 22:26:30 +0000106ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000107 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000108}
109
Chris Lattner3462ae32001-12-03 22:26:30 +0000110ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000111 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000112}
113
Chris Lattner3462ae32001-12-03 22:26:30 +0000114ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000115 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116 Val = V;
117}
118
Chris Lattner3462ae32001-12-03 22:26:30 +0000119ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000120 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000121 for (unsigned i = 0; i < V.size(); i++) {
122 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000123 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000124 }
125}
126
Chris Lattner3462ae32001-12-03 22:26:30 +0000127ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000128 const std::vector<Constant*> &V) : Constant(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 Lattner3462ae32001-12-03 22:26:30 +0000137ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
138 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000139 Operands.push_back(Use(GV, this));
140}
141
142
Chris Lattner2f7c9632001-06-06 20:29:01 +0000143
144//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000145// classof implementations
146
Chris Lattner3462ae32001-12-03 22:26:30 +0000147bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000148 return CPV->getType()->isIntegral();
149}
Chris Lattner3462ae32001-12-03 22:26:30 +0000150bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000151 return CPV->getType()->isSigned();
152}
Chris Lattner3462ae32001-12-03 22:26:30 +0000153bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000154 return CPV->getType()->isUnsigned();
155}
Chris Lattner3462ae32001-12-03 22:26:30 +0000156bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000157 const Type *Ty = CPV->getType();
158 return Ty == Type::FloatTy || Ty == Type::DoubleTy;
159}
Chris Lattner3462ae32001-12-03 22:26:30 +0000160bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000161 return isa<ArrayType>(CPV->getType());
162}
Chris Lattner3462ae32001-12-03 22:26:30 +0000163bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000164 return isa<StructType>(CPV->getType());
165}
Chris Lattner3462ae32001-12-03 22:26:30 +0000166bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000167 return isa<PointerType>(CPV->getType());
168}
169
170
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000172// isValueValidForType implementations
173
Chris Lattner3462ae32001-12-03 22:26:30 +0000174bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000175 switch (Ty->getPrimitiveID()) {
176 default:
177 return false; // These can't be represented as integers!!!
178
179 // Signed types...
180 case Type::SByteTyID:
181 return (Val <= INT8_MAX && Val >= INT8_MIN);
182 case Type::ShortTyID:
183 return (Val <= INT16_MAX && Val >= INT16_MIN);
184 case Type::IntTyID:
185 return (Val <= INT32_MAX && Val >= INT32_MIN);
186 case Type::LongTyID:
187 return true; // This is the largest type...
188 }
189 assert(0 && "WTF?");
190 return false;
191}
192
Chris Lattner3462ae32001-12-03 22:26:30 +0000193bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194 switch (Ty->getPrimitiveID()) {
195 default:
196 return false; // These can't be represented as integers!!!
197
198 // Unsigned types...
199 case Type::UByteTyID:
200 return (Val <= UINT8_MAX);
201 case Type::UShortTyID:
202 return (Val <= UINT16_MAX);
203 case Type::UIntTyID:
204 return (Val <= UINT32_MAX);
205 case Type::ULongTyID:
206 return true; // This is the largest type...
207 }
208 assert(0 && "WTF?");
209 return false;
210}
211
Chris Lattner3462ae32001-12-03 22:26:30 +0000212bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213 switch (Ty->getPrimitiveID()) {
214 default:
215 return false; // These can't be represented as floating point!
216
217 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000219 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220 return (Val <= UINT8_MAX);
221 */
222 case Type::DoubleTyID:
223 return true; // This is the largest type...
224 }
225};
Chris Lattner9655e542001-07-20 19:16:02 +0000226
Chris Lattner49d855c2001-09-07 16:46:31 +0000227//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000228// Factory Function Implementation
229
Chris Lattner3462ae32001-12-03 22:26:30 +0000230template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000231struct ValueMap {
232 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000233 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000234
Chris Lattner3462ae32001-12-03 22:26:30 +0000235 inline ConstantClass *get(const Type *Ty, ValType V) {
236 map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000237 Map.find(ConstHashKey(Ty, V));
238 return (I != Map.end()) ? I->second : 0;
239 }
240
Chris Lattner3462ae32001-12-03 22:26:30 +0000241 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000242 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
243 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000244
Chris Lattner3462ae32001-12-03 22:26:30 +0000245 inline void remove(ConstantClass *CP) {
246 for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000247 E = Map.end(); I != E;++I)
248 if (I->second == CP) {
249 Map.erase(I);
250 return;
251 }
252 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000253};
254
Chris Lattner3462ae32001-12-03 22:26:30 +0000255//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000256//
Chris Lattner3462ae32001-12-03 22:26:30 +0000257static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000258
Chris Lattner3462ae32001-12-03 22:26:30 +0000259ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
260 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000261 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000262 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000263 return Result;
264}
265
Chris Lattner3462ae32001-12-03 22:26:30 +0000266ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
267 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000268 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000269 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000270 return Result;
271}
272
Chris Lattner3462ae32001-12-03 22:26:30 +0000273ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000274 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000275 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
276 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000277}
278
Chris Lattner3462ae32001-12-03 22:26:30 +0000279//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000280//
Chris Lattner3462ae32001-12-03 22:26:30 +0000281static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000282
Chris Lattner3462ae32001-12-03 22:26:30 +0000283ConstantFP *ConstantFP::get(const Type *Ty, double V) {
284 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000285 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000286 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000287 return Result;
288}
289
Chris Lattner3462ae32001-12-03 22:26:30 +0000290//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000291//
Chris Lattner7f74a562002-01-20 22:54:45 +0000292static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000293
Chris Lattner3462ae32001-12-03 22:26:30 +0000294ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000295 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000296 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000297 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000298 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000299 return Result;
300}
301
Chris Lattner3462ae32001-12-03 22:26:30 +0000302// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000303// contain the specified string. A null terminator is added to the specified
304// string so that it may be used in a natural way...
305//
Chris Lattner7f74a562002-01-20 22:54:45 +0000306ConstantArray *ConstantArray::get(const std::string &Str) {
307 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000308
309 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000310 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000311
312 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000313 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000314
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000315 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000316 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000317}
318
319
Chris Lattnerd7a73302001-10-13 06:57:33 +0000320// destroyConstant - Remove the constant from the constant table...
321//
Chris Lattner3462ae32001-12-03 22:26:30 +0000322void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000323 ArrayConstants.remove(this);
324 destroyConstantImpl();
325}
326
Chris Lattner3462ae32001-12-03 22:26:30 +0000327//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000328//
Chris Lattner7f74a562002-01-20 22:54:45 +0000329static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000330
Chris Lattner3462ae32001-12-03 22:26:30 +0000331ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000332 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000333 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000334 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000335 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000336 return Result;
337}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000338
Chris Lattnerd7a73302001-10-13 06:57:33 +0000339// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000340//
Chris Lattner3462ae32001-12-03 22:26:30 +0000341void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000342 StructConstants.remove(this);
343 destroyConstantImpl();
344}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000345
Chris Lattner3462ae32001-12-03 22:26:30 +0000346//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000347//
Chris Lattner3462ae32001-12-03 22:26:30 +0000348static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000349
Chris Lattner3462ae32001-12-03 22:26:30 +0000350ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
351 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000352 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000353 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000354 return Result;
355}
356
Chris Lattner3462ae32001-12-03 22:26:30 +0000357//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000358//
Chris Lattner3462ae32001-12-03 22:26:30 +0000359ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000360 assert(GV->getParent() && "Global Value must be attached to a module!");
361
362 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000363 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000364}
365
366
Chris Lattner3462ae32001-12-03 22:26:30 +0000367void ConstantPointerRef::mutateReference(GlobalValue *NewGV) {
368 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000369 Operands[0] = NewGV;
Chris Lattner25033252001-10-03 19:28:15 +0000370}