blob: be1e582d157335b5cce66fcc4d924dec4cf46667 [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 Lattner60e0dd72001-10-03 06:12:09 +000012#include "llvm/GlobalVariable.h" // TODO make this GlobalValue.h
Chris Lattner2f7c9632001-06-06 20:29:01 +000013#include <algorithm>
14#include <assert.h>
15
Chris Lattner49d855c2001-09-07 16:46:31 +000016ConstPoolBool *ConstPoolBool::True = new ConstPoolBool(true);
17ConstPoolBool *ConstPoolBool::False = new ConstPoolBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000018
Chris Lattner9655e542001-07-20 19:16:02 +000019
Chris Lattner2f7c9632001-06-06 20:29:01 +000020//===----------------------------------------------------------------------===//
21// ConstPoolVal Class
22//===----------------------------------------------------------------------===//
23
24// Specialize setName to take care of symbol table majik
Chris Lattner49d855c2001-09-07 16:46:31 +000025void ConstPoolVal::setName(const string &Name, SymbolTable *ST) {
26 assert(ST && "Type::setName - Must provide symbol table argument!");
27
28 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000029}
30
31// Static constructor to create a '0' constant of arbitrary type...
32ConstPoolVal *ConstPoolVal::getNullConstant(const Type *Ty) {
33 switch (Ty->getPrimitiveID()) {
Chris Lattner49d855c2001-09-07 16:46:31 +000034 case Type::BoolTyID: return ConstPoolBool::get(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000035 case Type::SByteTyID:
36 case Type::ShortTyID:
37 case Type::IntTyID:
Chris Lattner49d855c2001-09-07 16:46:31 +000038 case Type::LongTyID: return ConstPoolSInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000039
40 case Type::UByteTyID:
41 case Type::UShortTyID:
42 case Type::UIntTyID:
Chris Lattner49d855c2001-09-07 16:46:31 +000043 case Type::ULongTyID: return ConstPoolUInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000044
45 case Type::FloatTyID:
Chris Lattner49d855c2001-09-07 16:46:31 +000046 case Type::DoubleTyID: return ConstPoolFP::get(Ty, 0);
Chris Lattner436248f2001-09-30 20:14:07 +000047
48 case Type::PointerTyID:
Chris Lattner883ad0b2001-10-03 15:39:36 +000049 return ConstPoolPointer::getNull(cast<PointerType>(Ty));
Chris Lattner2f7c9632001-06-06 20:29:01 +000050 default:
51 return 0;
52 }
53}
54
Chris Lattnerda558102001-10-02 03:41:24 +000055bool ConstPoolInt::classof(const ConstPoolVal *CPV) {
Chris Lattner38569342001-10-01 20:11:19 +000056 return CPV->getType()->isIntegral();
57}
Chris Lattner2f7c9632001-06-06 20:29:01 +000058
59//===----------------------------------------------------------------------===//
60// ConstPoolXXX Classes
61//===----------------------------------------------------------------------===//
62
63//===----------------------------------------------------------------------===//
64// Normal Constructors
65
Chris Lattner49d855c2001-09-07 16:46:31 +000066ConstPoolBool::ConstPoolBool(bool V) : ConstPoolVal(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000067 Val = V;
68}
Chris Lattner49d855c2001-09-07 16:46:31 +000069
70ConstPoolInt::ConstPoolInt(const Type *Ty, uint64_t V) : ConstPoolVal(Ty) {
71 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +000072}
Chris Lattner2f7c9632001-06-06 20:29:01 +000073
Chris Lattner49d855c2001-09-07 16:46:31 +000074ConstPoolSInt::ConstPoolSInt(const Type *Ty, int64_t V) : ConstPoolInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +000075 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000076}
77
Chris Lattner49d855c2001-09-07 16:46:31 +000078ConstPoolUInt::ConstPoolUInt(const Type *Ty, uint64_t V) : ConstPoolInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +000079 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000080}
81
Chris Lattner49d855c2001-09-07 16:46:31 +000082ConstPoolFP::ConstPoolFP(const Type *Ty, double V) : ConstPoolVal(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +000083 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000084 Val = V;
85}
86
Chris Lattner49d855c2001-09-07 16:46:31 +000087ConstPoolArray::ConstPoolArray(const ArrayType *T,
88 const vector<ConstPoolVal*> &V)
89 : ConstPoolVal(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000090 for (unsigned i = 0; i < V.size(); i++) {
91 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +000092 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +000093 }
94}
95
Chris Lattner49d855c2001-09-07 16:46:31 +000096ConstPoolStruct::ConstPoolStruct(const StructType *T,
97 const vector<ConstPoolVal*> &V)
98 : ConstPoolVal(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000099 const StructType::ElementTypes &ETypes = T->getElementTypes();
Chris Lattner49d855c2001-09-07 16:46:31 +0000100
Chris Lattner2f7c9632001-06-06 20:29:01 +0000101 for (unsigned i = 0; i < V.size(); i++) {
102 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000103 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000104 }
105}
106
Chris Lattner436248f2001-09-30 20:14:07 +0000107ConstPoolPointer::ConstPoolPointer(const PointerType *T) : ConstPoolVal(T) {}
108
Chris Lattner7fac0702001-10-03 14:53:21 +0000109ConstPoolPointerReference::ConstPoolPointerReference(GlobalValue *GV)
Chris Lattner60e0dd72001-10-03 06:12:09 +0000110 : ConstPoolPointer(GV->getType()) {
111 Operands.push_back(Use(GV, this));
112}
113
114
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115
116//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000117// getStrValue implementations
118
119string ConstPoolBool::getStrValue() const {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000120 return Val ? "true" : "false";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000121}
122
123string ConstPoolSInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000124 return itostr(Val.Signed);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000125}
126
127string ConstPoolUInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000128 return utostr(Val.Unsigned);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129}
130
131string ConstPoolFP::getStrValue() const {
Chris Lattnerd06dd692001-07-15 00:18:39 +0000132 return ftostr(Val);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000133}
134
Chris Lattner2f7c9632001-06-06 20:29:01 +0000135string ConstPoolArray::getStrValue() const {
136 string Result = "[";
Chris Lattnera073acb2001-07-07 08:36:50 +0000137 if (Operands.size()) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000138 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner8f191122001-10-01 18:26:53 +0000139 " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
Chris Lattnera073acb2001-07-07 08:36:50 +0000140 for (unsigned i = 1; i < Operands.size(); i++)
Chris Lattner49d855c2001-09-07 16:46:31 +0000141 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner8f191122001-10-01 18:26:53 +0000142 " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000143 }
144
145 return Result + " ]";
146}
147
148string ConstPoolStruct::getStrValue() const {
149 string Result = "{";
Chris Lattnera073acb2001-07-07 08:36:50 +0000150 if (Operands.size()) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000151 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner8f191122001-10-01 18:26:53 +0000152 " " + cast<ConstPoolVal>(Operands[0])->getStrValue();
Chris Lattnera073acb2001-07-07 08:36:50 +0000153 for (unsigned i = 1; i < Operands.size(); i++)
Chris Lattner49d855c2001-09-07 16:46:31 +0000154 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner8f191122001-10-01 18:26:53 +0000155 " " + cast<ConstPoolVal>(Operands[i])->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000156 }
157
158 return Result + " }";
159}
160
Chris Lattner436248f2001-09-30 20:14:07 +0000161string ConstPoolPointer::getStrValue() const {
162 return "null";
163}
164
Chris Lattner60e0dd72001-10-03 06:12:09 +0000165string ConstPoolPointerReference::getStrValue() const {
166 return "<pointer reference>";
167}
168
Chris Lattner2f7c9632001-06-06 20:29:01 +0000169//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000170// isValueValidForType implementations
171
172bool ConstPoolSInt::isValueValidForType(const Type *Ty, int64_t Val) {
173 switch (Ty->getPrimitiveID()) {
174 default:
175 return false; // These can't be represented as integers!!!
176
177 // Signed types...
178 case Type::SByteTyID:
179 return (Val <= INT8_MAX && Val >= INT8_MIN);
180 case Type::ShortTyID:
181 return (Val <= INT16_MAX && Val >= INT16_MIN);
182 case Type::IntTyID:
183 return (Val <= INT32_MAX && Val >= INT32_MIN);
184 case Type::LongTyID:
185 return true; // This is the largest type...
186 }
187 assert(0 && "WTF?");
188 return false;
189}
190
191bool ConstPoolUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
192 switch (Ty->getPrimitiveID()) {
193 default:
194 return false; // These can't be represented as integers!!!
195
196 // Unsigned types...
197 case Type::UByteTyID:
198 return (Val <= UINT8_MAX);
199 case Type::UShortTyID:
200 return (Val <= UINT16_MAX);
201 case Type::UIntTyID:
202 return (Val <= UINT32_MAX);
203 case Type::ULongTyID:
204 return true; // This is the largest type...
205 }
206 assert(0 && "WTF?");
207 return false;
208}
209
210bool ConstPoolFP::isValueValidForType(const Type *Ty, double Val) {
211 switch (Ty->getPrimitiveID()) {
212 default:
213 return false; // These can't be represented as floating point!
214
215 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000217 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218 return (Val <= UINT8_MAX);
219 */
220 case Type::DoubleTyID:
221 return true; // This is the largest type...
222 }
223};
Chris Lattner9655e542001-07-20 19:16:02 +0000224
Chris Lattner49d855c2001-09-07 16:46:31 +0000225//===----------------------------------------------------------------------===//
226// Hash Function Implementations
227#if 0
228unsigned ConstPoolSInt::hash(const Type *Ty, int64_t V) {
229 return unsigned(Ty->getPrimitiveID() ^ V);
230}
231
232unsigned ConstPoolUInt::hash(const Type *Ty, uint64_t V) {
233 return unsigned(Ty->getPrimitiveID() ^ V);
234}
235
236unsigned ConstPoolFP::hash(const Type *Ty, double V) {
237 return Ty->getPrimitiveID() ^ unsigned(V);
238}
239
240unsigned ConstPoolArray::hash(const ArrayType *Ty,
241 const vector<ConstPoolVal*> &V) {
242 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
243 for (unsigned i = 0; i < V.size(); ++i)
244 Result ^= V[i]->getHash() << (i & 7);
245 return Result;
246}
247
248unsigned ConstPoolStruct::hash(const StructType *Ty,
249 const vector<ConstPoolVal*> &V) {
250 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
251 for (unsigned i = 0; i < V.size(); ++i)
252 Result ^= V[i]->getHash() << (i & 7);
253 return Result;
254}
255#endif
256
257//===----------------------------------------------------------------------===//
258// Factory Function Implementation
259
260template<class ValType, class ConstPoolClass>
261struct ValueMap {
262 typedef pair<const Type*, ValType> ConstHashKey;
263 map<ConstHashKey, ConstPoolClass *> Map;
264
265 inline ConstPoolClass *get(const Type *Ty, ValType V) {
266 map<ConstHashKey,ConstPoolClass *>::iterator I =
267 Map.find(ConstHashKey(Ty, V));
268 return (I != Map.end()) ? I->second : 0;
269 }
270
271 inline void add(const Type *Ty, ValType V, ConstPoolClass *CP) {
272 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
273 }
274};
275
276//---- ConstPoolUInt::get() and ConstPoolSInt::get() implementations...
277//
278static ValueMap<uint64_t, ConstPoolInt> IntConstants;
279
280ConstPoolSInt *ConstPoolSInt::get(const Type *Ty, int64_t V) {
281 ConstPoolSInt *Result = (ConstPoolSInt*)IntConstants.get(Ty, (uint64_t)V);
282 if (!Result) // If no preexisting value, create one now...
283 IntConstants.add(Ty, V, Result = new ConstPoolSInt(Ty, V));
284 return Result;
285}
286
287ConstPoolUInt *ConstPoolUInt::get(const Type *Ty, uint64_t V) {
288 ConstPoolUInt *Result = (ConstPoolUInt*)IntConstants.get(Ty, V);
289 if (!Result) // If no preexisting value, create one now...
290 IntConstants.add(Ty, V, Result = new ConstPoolUInt(Ty, V));
291 return Result;
292}
293
294ConstPoolInt *ConstPoolInt::get(const Type *Ty, unsigned char V) {
295 assert(V <= 127 && "Can only be used with very small positive constants!");
296 if (Ty->isSigned()) return ConstPoolSInt::get(Ty, V);
297 return ConstPoolUInt::get(Ty, V);
298}
299
300//---- ConstPoolFP::get() implementation...
301//
302static ValueMap<double, ConstPoolFP> FPConstants;
303
304ConstPoolFP *ConstPoolFP::get(const Type *Ty, double V) {
305 ConstPoolFP *Result = FPConstants.get(Ty, V);
306 if (!Result) // If no preexisting value, create one now...
307 FPConstants.add(Ty, V, Result = new ConstPoolFP(Ty, V));
308 return Result;
309}
310
311//---- ConstPoolArray::get() implementation...
312//
313static ValueMap<vector<ConstPoolVal*>, ConstPoolArray> ArrayConstants;
314
315ConstPoolArray *ConstPoolArray::get(const ArrayType *Ty,
316 const vector<ConstPoolVal*> &V) {
317 ConstPoolArray *Result = ArrayConstants.get(Ty, V);
318 if (!Result) // If no preexisting value, create one now...
319 ArrayConstants.add(Ty, V, Result = new ConstPoolArray(Ty, V));
320 return Result;
321}
322
323//---- ConstPoolStruct::get() implementation...
324//
325static ValueMap<vector<ConstPoolVal*>, ConstPoolStruct> StructConstants;
326
327ConstPoolStruct *ConstPoolStruct::get(const StructType *Ty,
328 const vector<ConstPoolVal*> &V) {
329 ConstPoolStruct *Result = StructConstants.get(Ty, V);
330 if (!Result) // If no preexisting value, create one now...
331 StructConstants.add(Ty, V, Result = new ConstPoolStruct(Ty, V));
332 return Result;
333}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000334
335//---- ConstPoolPointer::get() implementation...
336//
337static ValueMap<char, ConstPoolPointer> NullPtrConstants;
338
339ConstPoolPointer *ConstPoolPointer::getNull(const PointerType *Ty) {
340 ConstPoolPointer *Result = NullPtrConstants.get(Ty, 0);
341 if (!Result) // If no preexisting value, create one now...
342 NullPtrConstants.add(Ty, 0, Result = new ConstPoolPointer(Ty));
343 return Result;
344}
345
Chris Lattner25033252001-10-03 19:28:15 +0000346//---- ConstPoolPointerReference::get() implementation...
347//
348ConstPoolPointerReference *ConstPoolPointerReference::get(GlobalValue *GV) {
349 assert(GV->getParent());
350 // FIXME: These should all be shared!
351 return new ConstPoolPointerReference(GV);
352}