1 //===-- iConstPool.cpp - Implement ConstPool instructions --------*- C++ -*--=//
3 // This file implements the ConstPool* classes...
5 //===----------------------------------------------------------------------===//
7 #define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends...
8 #include "llvm/ConstPoolVals.h"
9 #include "llvm/Support/StringExtras.h" // itostr
10 #include "llvm/DerivedTypes.h"
11 #include "llvm/SymbolTable.h"
12 #include "llvm/GlobalValue.h"
13 #include "llvm/Module.h"
14 #include "llvm/Analysis/SlotCalculator.h"
18 ConstPoolBool *ConstPoolBool::True = new ConstPoolBool(true);
19 ConstPoolBool *ConstPoolBool::False = new ConstPoolBool(false);
22 //===----------------------------------------------------------------------===//
24 //===----------------------------------------------------------------------===//
26 // Specialize setName to take care of symbol table majik
27 void ConstPoolVal::setName(const string &Name, SymbolTable *ST) {
28 assert(ST && "Type::setName - Must provide symbol table argument!");
30 if (Name.size()) ST->insert(Name, this);
33 // Static constructor to create a '0' constant of arbitrary type...
34 ConstPoolVal *ConstPoolVal::getNullConstant(const Type *Ty) {
35 switch (Ty->getPrimitiveID()) {
36 case Type::BoolTyID: return ConstPoolBool::get(false);
40 case Type::LongTyID: return ConstPoolSInt::get(Ty, 0);
43 case Type::UShortTyID:
45 case Type::ULongTyID: return ConstPoolUInt::get(Ty, 0);
48 case Type::DoubleTyID: return ConstPoolFP::get(Ty, 0);
50 case Type::PointerTyID:
51 return ConstPoolPointerNull::get(cast<PointerType>(Ty));
58 #include "llvm/Assembly/Writer.h"
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.
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;
77 assert(isa<ConstPoolVal>(V) && "References remain to ConstPoolPointerRef!");
78 ConstPoolVal *CPV = cast<ConstPoolVal>(V);
79 CPV->destroyConstant();
81 // The constant should remove itself from our use list...
82 assert((use_empty() || use_back() == V) && "Constant not removed!");
85 // Value has no outstanding references it is safe to delete it now...
89 //===----------------------------------------------------------------------===//
90 // ConstPoolXXX Classes
91 //===----------------------------------------------------------------------===//
93 //===----------------------------------------------------------------------===//
94 // Normal Constructors
96 ConstPoolBool::ConstPoolBool(bool V) : ConstPoolVal(Type::BoolTy) {
100 ConstPoolInt::ConstPoolInt(const Type *Ty, uint64_t V) : ConstPoolVal(Ty) {
104 ConstPoolSInt::ConstPoolSInt(const Type *Ty, int64_t V) : ConstPoolInt(Ty, V) {
105 assert(isValueValidForType(Ty, V) && "Value too large for type!");
108 ConstPoolUInt::ConstPoolUInt(const Type *Ty, uint64_t V) : ConstPoolInt(Ty, V) {
109 assert(isValueValidForType(Ty, V) && "Value too large for type!");
112 ConstPoolFP::ConstPoolFP(const Type *Ty, double V) : ConstPoolVal(Ty) {
113 assert(isValueValidForType(Ty, V) && "Value too large for type!");
117 ConstPoolArray::ConstPoolArray(const ArrayType *T,
118 const vector<ConstPoolVal*> &V)
120 for (unsigned i = 0; i < V.size(); i++) {
121 assert(V[i]->getType() == T->getElementType());
122 Operands.push_back(Use(V[i], this));
126 ConstPoolStruct::ConstPoolStruct(const StructType *T,
127 const vector<ConstPoolVal*> &V)
129 const StructType::ElementTypes &ETypes = T->getElementTypes();
131 for (unsigned i = 0; i < V.size(); i++) {
132 assert(V[i]->getType() == ETypes[i]);
133 Operands.push_back(Use(V[i], this));
137 ConstPoolPointerRef::ConstPoolPointerRef(GlobalValue *GV)
138 : ConstPoolPointer(GV->getType()) {
139 Operands.push_back(Use(GV, this));
144 //===----------------------------------------------------------------------===//
145 // getStrValue implementations
147 string ConstPoolBool::getStrValue() const {
148 return Val ? "true" : "false";
151 string ConstPoolSInt::getStrValue() const {
152 return itostr(Val.Signed);
155 string ConstPoolUInt::getStrValue() const {
156 return utostr(Val.Unsigned);
159 string ConstPoolFP::getStrValue() const {
163 string ConstPoolArray::getStrValue() const {
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.
169 const Type *ETy = cast<ArrayType>(getType())->getElementType();
170 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
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) {
183 for (unsigned i = 0; i < Operands.size(); ++i) {
184 unsigned char C = (ETy == Type::SByteTy) ?
185 (unsigned char)cast<ConstPoolSInt>(Operands[i])->getValue() :
186 (unsigned char)cast<ConstPoolUInt>(Operands[i])->getValue();
192 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
193 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
200 if (Operands.size()) {
201 Result += " " + Operands[0]->getType()->getDescription() +
202 " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
203 for (unsigned i = 1; i < Operands.size(); i++)
204 Result += ", " + Operands[i]->getType()->getDescription() +
205 " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
213 string ConstPoolStruct::getStrValue() const {
215 if (Operands.size()) {
216 Result += " " + Operands[0]->getType()->getDescription() +
217 " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
218 for (unsigned i = 1; i < Operands.size(); i++)
219 Result += ", " + Operands[i]->getType()->getDescription() +
220 " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
223 return Result + " }";
226 string ConstPoolPointerNull::getStrValue() const {
230 string ConstPoolPointerRef::getStrValue() const {
231 const GlobalValue *V = getValue();
232 if (V->hasName()) return "%" + V->getName();
234 SlotCalculator *Table = new SlotCalculator(V->getParent(), true);
235 int Slot = Table->getValSlot(V);
238 if (Slot >= 0) return string(" %") + itostr(Slot);
239 else return "<pointer reference badref>";
243 //===----------------------------------------------------------------------===//
244 // classof implementations
246 bool ConstPoolInt::classof(const ConstPoolVal *CPV) {
247 return CPV->getType()->isIntegral();
249 bool ConstPoolSInt::classof(const ConstPoolVal *CPV) {
250 return CPV->getType()->isSigned();
252 bool ConstPoolUInt::classof(const ConstPoolVal *CPV) {
253 return CPV->getType()->isUnsigned();
255 bool ConstPoolFP::classof(const ConstPoolVal *CPV) {
256 const Type *Ty = CPV->getType();
257 return Ty == Type::FloatTy || Ty == Type::DoubleTy;
259 bool ConstPoolArray::classof(const ConstPoolVal *CPV) {
260 return isa<ArrayType>(CPV->getType());
262 bool ConstPoolStruct::classof(const ConstPoolVal *CPV) {
263 return isa<StructType>(CPV->getType());
265 bool ConstPoolPointer::classof(const ConstPoolVal *CPV) {
266 return isa<PointerType>(CPV->getType());
270 //===----------------------------------------------------------------------===//
271 // isValueValidForType implementations
273 bool ConstPoolSInt::isValueValidForType(const Type *Ty, int64_t Val) {
274 switch (Ty->getPrimitiveID()) {
276 return false; // These can't be represented as integers!!!
279 case Type::SByteTyID:
280 return (Val <= INT8_MAX && Val >= INT8_MIN);
281 case Type::ShortTyID:
282 return (Val <= INT16_MAX && Val >= INT16_MIN);
284 return (Val <= INT32_MAX && Val >= INT32_MIN);
286 return true; // This is the largest type...
292 bool ConstPoolUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
293 switch (Ty->getPrimitiveID()) {
295 return false; // These can't be represented as integers!!!
298 case Type::UByteTyID:
299 return (Val <= UINT8_MAX);
300 case Type::UShortTyID:
301 return (Val <= UINT16_MAX);
303 return (Val <= UINT32_MAX);
304 case Type::ULongTyID:
305 return true; // This is the largest type...
311 bool ConstPoolFP::isValueValidForType(const Type *Ty, double Val) {
312 switch (Ty->getPrimitiveID()) {
314 return false; // These can't be represented as floating point!
316 // TODO: Figure out how to test if a double can be cast to a float!
317 case Type::FloatTyID:
319 return (Val <= UINT8_MAX);
321 case Type::DoubleTyID:
322 return true; // This is the largest type...
326 //===----------------------------------------------------------------------===//
327 // Hash Function Implementations
329 unsigned ConstPoolSInt::hash(const Type *Ty, int64_t V) {
330 return unsigned(Ty->getPrimitiveID() ^ V);
333 unsigned ConstPoolUInt::hash(const Type *Ty, uint64_t V) {
334 return unsigned(Ty->getPrimitiveID() ^ V);
337 unsigned ConstPoolFP::hash(const Type *Ty, double V) {
338 return Ty->getPrimitiveID() ^ unsigned(V);
341 unsigned 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);
349 unsigned 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);
358 //===----------------------------------------------------------------------===//
359 // Factory Function Implementation
361 template<class ValType, class ConstPoolClass>
363 typedef pair<const Type*, ValType> ConstHashKey;
364 map<ConstHashKey, ConstPoolClass *> Map;
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;
372 inline void add(const Type *Ty, ValType V, ConstPoolClass *CP) {
373 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
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) {
386 //---- ConstPoolUInt::get() and ConstPoolSInt::get() implementations...
388 static ValueMap<uint64_t, ConstPoolInt> IntConstants;
390 ConstPoolSInt *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));
397 ConstPoolUInt *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));
404 ConstPoolInt *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);
410 //---- ConstPoolFP::get() implementation...
412 static ValueMap<double, ConstPoolFP> FPConstants;
414 ConstPoolFP *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));
421 //---- ConstPoolArray::get() implementation...
423 static ValueMap<vector<ConstPoolVal*>, ConstPoolArray> ArrayConstants;
425 ConstPoolArray *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));
433 // 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...
437 ConstPoolArray *ConstPoolArray::get(const string &Str) {
438 vector<ConstPoolVal*> ElementVals;
440 for (unsigned i = 0; i < Str.length(); ++i)
441 ElementVals.push_back(ConstPoolUInt::get(Type::UByteTy, Str[i]));
443 // Add a null terminator to the string...
444 ElementVals.push_back(ConstPoolUInt::get(Type::UByteTy, 0));
446 ArrayType *ATy = ArrayType::get(Type::UByteTy/*,stringConstant.length()*/);
447 return ConstPoolArray::get(ATy, ElementVals);
451 // destroyConstant - Remove the constant from the constant table...
453 void ConstPoolArray::destroyConstant() {
454 ArrayConstants.remove(this);
455 destroyConstantImpl();
458 //---- ConstPoolStruct::get() implementation...
460 static ValueMap<vector<ConstPoolVal*>, ConstPoolStruct> StructConstants;
462 ConstPoolStruct *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));
470 // destroyConstant - Remove the constant from the constant table...
472 void ConstPoolStruct::destroyConstant() {
473 StructConstants.remove(this);
474 destroyConstantImpl();
477 //---- ConstPoolPointerNull::get() implementation...
479 static ValueMap<char, ConstPoolPointerNull> NullPtrConstants;
481 ConstPoolPointerNull *ConstPoolPointerNull::get(const PointerType *Ty) {
482 ConstPoolPointerNull *Result = NullPtrConstants.get(Ty, 0);
483 if (!Result) // If no preexisting value, create one now...
484 NullPtrConstants.add(Ty, 0, Result = new ConstPoolPointerNull(Ty));
488 //---- ConstPoolPointerRef::get() implementation...
490 ConstPoolPointerRef *ConstPoolPointerRef::get(GlobalValue *GV) {
491 assert(GV->getParent() && "Global Value must be attached to a module!");
493 // The Module handles the pointer reference sharing...
494 return GV->getParent()->getConstPoolPointerRef(GV);
498 void ConstPoolPointerRef::mutateReference(GlobalValue *NewGV) {
499 getValue()->getParent()->mutateConstPoolPointerRef(getValue(), NewGV);