blob: 253c0abc483c689d2c66e002b594402e5ab7f872 [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 Lattnere2472bb2001-07-23 17:46:59 +00009#include "llvm/Support/StringExtras.h" // itostr
Chris Lattner2f7c9632001-06-06 20:29:01 +000010#include "llvm/DerivedTypes.h"
11#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000012#include "llvm/GlobalValue.h"
13#include "llvm/Module.h"
14#include "llvm/Analysis/SlotCalculator.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 Lattner7fac0702001-10-03 14:53:21 +0000137ConstPoolPointerReference::ConstPoolPointerReference(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
147string ConstPoolBool::getStrValue() const {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000148 return Val ? "true" : "false";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149}
150
151string ConstPoolSInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000152 return itostr(Val.Signed);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000153}
154
155string ConstPoolUInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000156 return utostr(Val.Unsigned);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000157}
158
159string ConstPoolFP::getStrValue() const {
Chris Lattnerd06dd692001-07-15 00:18:39 +0000160 return ftostr(Val);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000161}
162
Chris Lattner2f7c9632001-06-06 20:29:01 +0000163string ConstPoolArray::getStrValue() const {
164 string Result = "[";
Chris Lattnera073acb2001-07-07 08:36:50 +0000165 if (Operands.size()) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000166 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner8f191122001-10-01 18:26:53 +0000167 " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
Chris Lattnera073acb2001-07-07 08:36:50 +0000168 for (unsigned i = 1; i < Operands.size(); i++)
Chris Lattner49d855c2001-09-07 16:46:31 +0000169 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner8f191122001-10-01 18:26:53 +0000170 " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171 }
172
173 return Result + " ]";
174}
175
176string ConstPoolStruct::getStrValue() const {
177 string Result = "{";
Chris Lattnera073acb2001-07-07 08:36:50 +0000178 if (Operands.size()) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000179 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner8f191122001-10-01 18:26:53 +0000180 " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
Chris Lattnera073acb2001-07-07 08:36:50 +0000181 for (unsigned i = 1; i < Operands.size(); i++)
Chris Lattner49d855c2001-09-07 16:46:31 +0000182 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner8f191122001-10-01 18:26:53 +0000183 " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000184 }
185
186 return Result + " }";
187}
188
Chris Lattnerd7a73302001-10-13 06:57:33 +0000189string ConstPoolPointerNull::getStrValue() const {
Chris Lattner436248f2001-09-30 20:14:07 +0000190 return "null";
191}
192
Chris Lattner60e0dd72001-10-03 06:12:09 +0000193string ConstPoolPointerReference::getStrValue() const {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000194 const GlobalValue *V = getValue();
195 if (V->hasName()) return "%" + V->getName();
196
197 SlotCalculator *Table = new SlotCalculator(V->getParent(), true);
198 int Slot = Table->getValSlot(V);
199 delete Table;
200
201 if (Slot >= 0) return string(" %") + itostr(Slot);
202 else return "<pointer reference badref>";
Chris Lattner60e0dd72001-10-03 06:12:09 +0000203}
204
Chris Lattnerd7a73302001-10-13 06:57:33 +0000205
206//===----------------------------------------------------------------------===//
207// classof implementations
208
209bool ConstPoolInt::classof(const ConstPoolVal *CPV) {
210 return CPV->getType()->isIntegral();
211}
212bool ConstPoolSInt::classof(const ConstPoolVal *CPV) {
213 return CPV->getType()->isSigned();
214}
215bool ConstPoolUInt::classof(const ConstPoolVal *CPV) {
216 return CPV->getType()->isUnsigned();
217}
218bool ConstPoolFP::classof(const ConstPoolVal *CPV) {
219 const Type *Ty = CPV->getType();
220 return Ty == Type::FloatTy || Ty == Type::DoubleTy;
221}
222bool ConstPoolArray::classof(const ConstPoolVal *CPV) {
223 return isa<ArrayType>(CPV->getType());
224}
225bool ConstPoolStruct::classof(const ConstPoolVal *CPV) {
226 return isa<StructType>(CPV->getType());
227}
228bool ConstPoolPointer::classof(const ConstPoolVal *CPV) {
229 return isa<PointerType>(CPV->getType());
230}
231
232
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000234// isValueValidForType implementations
235
236bool ConstPoolSInt::isValueValidForType(const Type *Ty, int64_t Val) {
237 switch (Ty->getPrimitiveID()) {
238 default:
239 return false; // These can't be represented as integers!!!
240
241 // Signed types...
242 case Type::SByteTyID:
243 return (Val <= INT8_MAX && Val >= INT8_MIN);
244 case Type::ShortTyID:
245 return (Val <= INT16_MAX && Val >= INT16_MIN);
246 case Type::IntTyID:
247 return (Val <= INT32_MAX && Val >= INT32_MIN);
248 case Type::LongTyID:
249 return true; // This is the largest type...
250 }
251 assert(0 && "WTF?");
252 return false;
253}
254
255bool ConstPoolUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
256 switch (Ty->getPrimitiveID()) {
257 default:
258 return false; // These can't be represented as integers!!!
259
260 // Unsigned types...
261 case Type::UByteTyID:
262 return (Val <= UINT8_MAX);
263 case Type::UShortTyID:
264 return (Val <= UINT16_MAX);
265 case Type::UIntTyID:
266 return (Val <= UINT32_MAX);
267 case Type::ULongTyID:
268 return true; // This is the largest type...
269 }
270 assert(0 && "WTF?");
271 return false;
272}
273
274bool ConstPoolFP::isValueValidForType(const Type *Ty, double Val) {
275 switch (Ty->getPrimitiveID()) {
276 default:
277 return false; // These can't be represented as floating point!
278
279 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000280 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000281 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000282 return (Val <= UINT8_MAX);
283 */
284 case Type::DoubleTyID:
285 return true; // This is the largest type...
286 }
287};
Chris Lattner9655e542001-07-20 19:16:02 +0000288
Chris Lattner49d855c2001-09-07 16:46:31 +0000289//===----------------------------------------------------------------------===//
290// Hash Function Implementations
291#if 0
292unsigned ConstPoolSInt::hash(const Type *Ty, int64_t V) {
293 return unsigned(Ty->getPrimitiveID() ^ V);
294}
295
296unsigned ConstPoolUInt::hash(const Type *Ty, uint64_t V) {
297 return unsigned(Ty->getPrimitiveID() ^ V);
298}
299
300unsigned ConstPoolFP::hash(const Type *Ty, double V) {
301 return Ty->getPrimitiveID() ^ unsigned(V);
302}
303
304unsigned ConstPoolArray::hash(const ArrayType *Ty,
305 const vector<ConstPoolVal*> &V) {
306 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
307 for (unsigned i = 0; i < V.size(); ++i)
308 Result ^= V[i]->getHash() << (i & 7);
309 return Result;
310}
311
312unsigned ConstPoolStruct::hash(const StructType *Ty,
313 const vector<ConstPoolVal*> &V) {
314 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
315 for (unsigned i = 0; i < V.size(); ++i)
316 Result ^= V[i]->getHash() << (i & 7);
317 return Result;
318}
319#endif
320
321//===----------------------------------------------------------------------===//
322// Factory Function Implementation
323
324template<class ValType, class ConstPoolClass>
325struct ValueMap {
326 typedef pair<const Type*, ValType> ConstHashKey;
327 map<ConstHashKey, ConstPoolClass *> Map;
328
329 inline ConstPoolClass *get(const Type *Ty, ValType V) {
330 map<ConstHashKey,ConstPoolClass *>::iterator I =
331 Map.find(ConstHashKey(Ty, V));
332 return (I != Map.end()) ? I->second : 0;
333 }
334
335 inline void add(const Type *Ty, ValType V, ConstPoolClass *CP) {
336 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
337 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000338
339 inline void remove(ConstPoolClass *CP) {
340 for (map<ConstHashKey,ConstPoolClass *>::iterator I = Map.begin(),
341 E = Map.end(); I != E;++I)
342 if (I->second == CP) {
343 Map.erase(I);
344 return;
345 }
346 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000347};
348
349//---- ConstPoolUInt::get() and ConstPoolSInt::get() implementations...
350//
351static ValueMap<uint64_t, ConstPoolInt> IntConstants;
352
353ConstPoolSInt *ConstPoolSInt::get(const Type *Ty, int64_t V) {
354 ConstPoolSInt *Result = (ConstPoolSInt*)IntConstants.get(Ty, (uint64_t)V);
355 if (!Result) // If no preexisting value, create one now...
356 IntConstants.add(Ty, V, Result = new ConstPoolSInt(Ty, V));
357 return Result;
358}
359
360ConstPoolUInt *ConstPoolUInt::get(const Type *Ty, uint64_t V) {
361 ConstPoolUInt *Result = (ConstPoolUInt*)IntConstants.get(Ty, V);
362 if (!Result) // If no preexisting value, create one now...
363 IntConstants.add(Ty, V, Result = new ConstPoolUInt(Ty, V));
364 return Result;
365}
366
367ConstPoolInt *ConstPoolInt::get(const Type *Ty, unsigned char V) {
368 assert(V <= 127 && "Can only be used with very small positive constants!");
369 if (Ty->isSigned()) return ConstPoolSInt::get(Ty, V);
370 return ConstPoolUInt::get(Ty, V);
371}
372
373//---- ConstPoolFP::get() implementation...
374//
375static ValueMap<double, ConstPoolFP> FPConstants;
376
377ConstPoolFP *ConstPoolFP::get(const Type *Ty, double V) {
378 ConstPoolFP *Result = FPConstants.get(Ty, V);
379 if (!Result) // If no preexisting value, create one now...
380 FPConstants.add(Ty, V, Result = new ConstPoolFP(Ty, V));
381 return Result;
382}
383
384//---- ConstPoolArray::get() implementation...
385//
386static ValueMap<vector<ConstPoolVal*>, ConstPoolArray> ArrayConstants;
387
388ConstPoolArray *ConstPoolArray::get(const ArrayType *Ty,
389 const vector<ConstPoolVal*> &V) {
390 ConstPoolArray *Result = ArrayConstants.get(Ty, V);
391 if (!Result) // If no preexisting value, create one now...
392 ArrayConstants.add(Ty, V, Result = new ConstPoolArray(Ty, V));
393 return Result;
394}
395
Chris Lattnerd7a73302001-10-13 06:57:33 +0000396// destroyConstant - Remove the constant from the constant table...
397//
398void ConstPoolArray::destroyConstant() {
399 ArrayConstants.remove(this);
400 destroyConstantImpl();
401}
402
Chris Lattner49d855c2001-09-07 16:46:31 +0000403//---- ConstPoolStruct::get() implementation...
404//
405static ValueMap<vector<ConstPoolVal*>, ConstPoolStruct> StructConstants;
406
407ConstPoolStruct *ConstPoolStruct::get(const StructType *Ty,
408 const vector<ConstPoolVal*> &V) {
409 ConstPoolStruct *Result = StructConstants.get(Ty, V);
410 if (!Result) // If no preexisting value, create one now...
411 StructConstants.add(Ty, V, Result = new ConstPoolStruct(Ty, V));
412 return Result;
413}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000414
Chris Lattnerd7a73302001-10-13 06:57:33 +0000415// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000416//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000417void ConstPoolStruct::destroyConstant() {
418 StructConstants.remove(this);
419 destroyConstantImpl();
420}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000421
Chris Lattnerd7a73302001-10-13 06:57:33 +0000422//---- ConstPoolPointerNull::get() implementation...
423//
424static ValueMap<char, ConstPoolPointerNull> NullPtrConstants;
425
426ConstPoolPointerNull *ConstPoolPointerNull::get(const PointerType *Ty) {
427 ConstPoolPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000428 if (!Result) // If no preexisting value, create one now...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000429 NullPtrConstants.add(Ty, 0, Result = new ConstPoolPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000430 return Result;
431}
432
Chris Lattner25033252001-10-03 19:28:15 +0000433//---- ConstPoolPointerReference::get() implementation...
434//
435ConstPoolPointerReference *ConstPoolPointerReference::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000436 assert(GV->getParent() && "Global Value must be attached to a module!");
437
438 // The Module handles the pointer reference sharing...
439 return GV->getParent()->getConstPoolPointerReference(GV);
440}
441
442
443void ConstPoolPointerReference::mutateReference(GlobalValue *NewGV) {
444 getValue()->getParent()->mutateConstPoolPointerReference(getValue(), NewGV);
445 Operands[0] = NewGV;
Chris Lattner25033252001-10-03 19:28:15 +0000446}