blob: 3814311f98a7766d39adbef9a5818bac8b92729b [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"
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 Lattner3462ae32001-12-03 22:26:30 +000018ConstantBool *ConstantBool::True = new ConstantBool(true);
19ConstantBool *ConstantBool::False = new ConstantBool(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//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000023// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000024//===----------------------------------------------------------------------===//
25
26// Specialize setName to take care of symbol table majik
Chris Lattner3462ae32001-12-03 22:26:30 +000027void Constant::setName(const string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000028 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...
Chris Lattner3462ae32001-12-03 22:26:30 +000034Constant *Constant::getNullConstant(const Type *Ty) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000035 switch (Ty->getPrimitiveID()) {
Chris Lattner3462ae32001-12-03 22:26:30 +000036 case Type::BoolTyID: return ConstantBool::get(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000037 case Type::SByteTyID:
38 case Type::ShortTyID:
39 case Type::IntTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000040 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000041
42 case Type::UByteTyID:
43 case Type::UShortTyID:
44 case Type::UIntTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000045 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000046
47 case Type::FloatTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000048 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
Chris Lattner436248f2001-09-30 20:14:07 +000049
50 case Type::PointerTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000051 return ConstantPointerNull::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
Chris Lattner3462ae32001-12-03 22:26:30 +000061void Constant::destroyConstantImpl() {
62 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000063 // 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
Chris Lattner3462ae32001-12-03 22:26:30 +000067 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000068 //
69 while (!use_empty()) {
70 Value *V = use_back();
71#ifndef NDEBUG // Only in -g mode...
Chris Lattner3462ae32001-12-03 22:26:30 +000072 if (!isa<Constant>(V)) {
Chris Lattnerd7a73302001-10-13 06:57:33 +000073 cerr << "While deleting: " << this << endl;
74 cerr << "Use still stuck around after Def is destroyed: " << V << endl;
75 }
76#endif
Chris Lattner3462ae32001-12-03 22:26:30 +000077 assert(isa<Constant>(V) && "References remain to ConstantPointerRef!");
78 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000079 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//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000090// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +000091//===----------------------------------------------------------------------===//
92
93//===----------------------------------------------------------------------===//
94// Normal Constructors
95
Chris Lattner3462ae32001-12-03 22:26:30 +000096ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000097 Val = V;
98}
Chris Lattner49d855c2001-09-07 16:46:31 +000099
Chris Lattner3462ae32001-12-03 22:26:30 +0000100ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000101 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000102}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000103
Chris Lattner3462ae32001-12-03 22:26:30 +0000104ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(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 Lattner3462ae32001-12-03 22:26:30 +0000108ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(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 Lattner3462ae32001-12-03 22:26:30 +0000112ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(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 Lattner3462ae32001-12-03 22:26:30 +0000117ConstantArray::ConstantArray(const ArrayType *T,
118 const vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119 for (unsigned i = 0; i < V.size(); i++) {
120 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000121 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000122 }
123}
124
Chris Lattner3462ae32001-12-03 22:26:30 +0000125ConstantStruct::ConstantStruct(const StructType *T,
126 const vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127 const StructType::ElementTypes &ETypes = T->getElementTypes();
Chris Lattner49d855c2001-09-07 16:46:31 +0000128
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129 for (unsigned i = 0; i < V.size(); i++) {
130 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000131 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000132 }
133}
134
Chris Lattner3462ae32001-12-03 22:26:30 +0000135ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
136 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000137 Operands.push_back(Use(GV, this));
138}
139
140
Chris Lattner2f7c9632001-06-06 20:29:01 +0000141
142//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000143// getStrValue implementations
144
Chris Lattner3462ae32001-12-03 22:26:30 +0000145string ConstantBool::getStrValue() const {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000146 return Val ? "true" : "false";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000147}
148
Chris Lattner3462ae32001-12-03 22:26:30 +0000149string ConstantSInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000150 return itostr(Val.Signed);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000151}
152
Chris Lattner3462ae32001-12-03 22:26:30 +0000153string ConstantUInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000154 return utostr(Val.Unsigned);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000155}
156
Chris Lattner3462ae32001-12-03 22:26:30 +0000157string ConstantFP::getStrValue() const {
Chris Lattnerd06dd692001-07-15 00:18:39 +0000158 return ftostr(Val);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000159}
160
Chris Lattner3462ae32001-12-03 22:26:30 +0000161string ConstantArray::getStrValue() const {
Vikram S. Adveeada6b12001-10-28 21:48:05 +0000162 string Result;
163
Chris Lattner239a1bd2001-10-29 13:27:09 +0000164 // As a special case, print the array as a string if it is an array of
165 // ubytes or an array of sbytes with positive values.
166 //
167 const Type *ETy = cast<ArrayType>(getType())->getElementType();
168 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
169
170 if (ETy == Type::SByteTy) {
171 for (unsigned i = 0; i < Operands.size(); ++i)
172 if (ETy == Type::SByteTy &&
Chris Lattner3462ae32001-12-03 22:26:30 +0000173 cast<ConstantSInt>(Operands[i])->getValue() < 0) {
Chris Lattner239a1bd2001-10-29 13:27:09 +0000174 isString = false;
175 break;
176 }
177 }
178
179 if (isString) {
180 Result = "c\"";
Chris Lattner78e11ae2001-10-15 00:05:03 +0000181 for (unsigned i = 0; i < Operands.size(); ++i) {
Chris Lattner8f80fe02001-10-14 23:54:12 +0000182 unsigned char C = (ETy == Type::SByteTy) ?
Chris Lattner3462ae32001-12-03 22:26:30 +0000183 (unsigned char)cast<ConstantSInt>(Operands[i])->getValue() :
184 (unsigned char)cast<ConstantUInt>(Operands[i])->getValue();
Chris Lattner8f80fe02001-10-14 23:54:12 +0000185
186 if (isprint(C)) {
187 Result += C;
Chris Lattner239a1bd2001-10-29 13:27:09 +0000188 } else {
189 Result += '\\';
190 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
191 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
Chris Lattner8f80fe02001-10-14 23:54:12 +0000192 }
193 }
Vikram S. Adve34410432001-10-14 23:17:20 +0000194 Result += "\"";
Chris Lattner8f80fe02001-10-14 23:54:12 +0000195
Vikram S. Adve34410432001-10-14 23:17:20 +0000196 } else {
197 Result = "[";
198 if (Operands.size()) {
199 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000200 " " + cast<Constant>(Operands[0])->getStrValue();
Vikram S. Adve34410432001-10-14 23:17:20 +0000201 for (unsigned i = 1; i < Operands.size(); i++)
202 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000203 " " + cast<Constant>(Operands[i])->getStrValue();
Vikram S. Adve34410432001-10-14 23:17:20 +0000204 }
205 Result += " ]";
206 }
207
208 return Result;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209}
210
Chris Lattner3462ae32001-12-03 22:26:30 +0000211string ConstantStruct::getStrValue() const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000212 string Result = "{";
Chris Lattnera073acb2001-07-07 08:36:50 +0000213 if (Operands.size()) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000214 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000215 " " + cast<Constant>(Operands[0])->getStrValue();
Chris Lattnera073acb2001-07-07 08:36:50 +0000216 for (unsigned i = 1; i < Operands.size(); i++)
Chris Lattner49d855c2001-09-07 16:46:31 +0000217 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000218 " " + cast<Constant>(Operands[i])->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000219 }
220
221 return Result + " }";
222}
223
Chris Lattner3462ae32001-12-03 22:26:30 +0000224string ConstantPointerNull::getStrValue() const {
Chris Lattner436248f2001-09-30 20:14:07 +0000225 return "null";
226}
227
Chris Lattner3462ae32001-12-03 22:26:30 +0000228string ConstantPointerRef::getStrValue() const {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000229 const GlobalValue *V = getValue();
230 if (V->hasName()) return "%" + V->getName();
231
232 SlotCalculator *Table = new SlotCalculator(V->getParent(), true);
233 int Slot = Table->getValSlot(V);
234 delete Table;
235
236 if (Slot >= 0) return string(" %") + itostr(Slot);
237 else return "<pointer reference badref>";
Chris Lattner60e0dd72001-10-03 06:12:09 +0000238}
239
Chris Lattnerd7a73302001-10-13 06:57:33 +0000240
241//===----------------------------------------------------------------------===//
242// classof implementations
243
Chris Lattner3462ae32001-12-03 22:26:30 +0000244bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000245 return CPV->getType()->isIntegral();
246}
Chris Lattner3462ae32001-12-03 22:26:30 +0000247bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000248 return CPV->getType()->isSigned();
249}
Chris Lattner3462ae32001-12-03 22:26:30 +0000250bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000251 return CPV->getType()->isUnsigned();
252}
Chris Lattner3462ae32001-12-03 22:26:30 +0000253bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000254 const Type *Ty = CPV->getType();
255 return Ty == Type::FloatTy || Ty == Type::DoubleTy;
256}
Chris Lattner3462ae32001-12-03 22:26:30 +0000257bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000258 return isa<ArrayType>(CPV->getType());
259}
Chris Lattner3462ae32001-12-03 22:26:30 +0000260bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000261 return isa<StructType>(CPV->getType());
262}
Chris Lattner3462ae32001-12-03 22:26:30 +0000263bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000264 return isa<PointerType>(CPV->getType());
265}
266
267
Chris Lattner2f7c9632001-06-06 20:29:01 +0000268//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000269// isValueValidForType implementations
270
Chris Lattner3462ae32001-12-03 22:26:30 +0000271bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000272 switch (Ty->getPrimitiveID()) {
273 default:
274 return false; // These can't be represented as integers!!!
275
276 // Signed types...
277 case Type::SByteTyID:
278 return (Val <= INT8_MAX && Val >= INT8_MIN);
279 case Type::ShortTyID:
280 return (Val <= INT16_MAX && Val >= INT16_MIN);
281 case Type::IntTyID:
282 return (Val <= INT32_MAX && Val >= INT32_MIN);
283 case Type::LongTyID:
284 return true; // This is the largest type...
285 }
286 assert(0 && "WTF?");
287 return false;
288}
289
Chris Lattner3462ae32001-12-03 22:26:30 +0000290bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000291 switch (Ty->getPrimitiveID()) {
292 default:
293 return false; // These can't be represented as integers!!!
294
295 // Unsigned types...
296 case Type::UByteTyID:
297 return (Val <= UINT8_MAX);
298 case Type::UShortTyID:
299 return (Val <= UINT16_MAX);
300 case Type::UIntTyID:
301 return (Val <= UINT32_MAX);
302 case Type::ULongTyID:
303 return true; // This is the largest type...
304 }
305 assert(0 && "WTF?");
306 return false;
307}
308
Chris Lattner3462ae32001-12-03 22:26:30 +0000309bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000310 switch (Ty->getPrimitiveID()) {
311 default:
312 return false; // These can't be represented as floating point!
313
314 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000315 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000316 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000317 return (Val <= UINT8_MAX);
318 */
319 case Type::DoubleTyID:
320 return true; // This is the largest type...
321 }
322};
Chris Lattner9655e542001-07-20 19:16:02 +0000323
Chris Lattner49d855c2001-09-07 16:46:31 +0000324//===----------------------------------------------------------------------===//
325// Hash Function Implementations
326#if 0
Chris Lattner3462ae32001-12-03 22:26:30 +0000327unsigned ConstantSInt::hash(const Type *Ty, int64_t V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000328 return unsigned(Ty->getPrimitiveID() ^ V);
329}
330
Chris Lattner3462ae32001-12-03 22:26:30 +0000331unsigned ConstantUInt::hash(const Type *Ty, uint64_t V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000332 return unsigned(Ty->getPrimitiveID() ^ V);
333}
334
Chris Lattner3462ae32001-12-03 22:26:30 +0000335unsigned ConstantFP::hash(const Type *Ty, double V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000336 return Ty->getPrimitiveID() ^ unsigned(V);
337}
338
Chris Lattner3462ae32001-12-03 22:26:30 +0000339unsigned ConstantArray::hash(const ArrayType *Ty,
340 const vector<Constant*> &V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000341 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
342 for (unsigned i = 0; i < V.size(); ++i)
343 Result ^= V[i]->getHash() << (i & 7);
344 return Result;
345}
346
Chris Lattner3462ae32001-12-03 22:26:30 +0000347unsigned ConstantStruct::hash(const StructType *Ty,
348 const vector<Constant*> &V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000349 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
350 for (unsigned i = 0; i < V.size(); ++i)
351 Result ^= V[i]->getHash() << (i & 7);
352 return Result;
353}
354#endif
355
356//===----------------------------------------------------------------------===//
357// Factory Function Implementation
358
Chris Lattner3462ae32001-12-03 22:26:30 +0000359template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000360struct ValueMap {
361 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000362 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000363
Chris Lattner3462ae32001-12-03 22:26:30 +0000364 inline ConstantClass *get(const Type *Ty, ValType V) {
365 map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000366 Map.find(ConstHashKey(Ty, V));
367 return (I != Map.end()) ? I->second : 0;
368 }
369
Chris Lattner3462ae32001-12-03 22:26:30 +0000370 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000371 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
372 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000373
Chris Lattner3462ae32001-12-03 22:26:30 +0000374 inline void remove(ConstantClass *CP) {
375 for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000376 E = Map.end(); I != E;++I)
377 if (I->second == CP) {
378 Map.erase(I);
379 return;
380 }
381 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000382};
383
Chris Lattner3462ae32001-12-03 22:26:30 +0000384//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000385//
Chris Lattner3462ae32001-12-03 22:26:30 +0000386static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000387
Chris Lattner3462ae32001-12-03 22:26:30 +0000388ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
389 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000390 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000391 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000392 return Result;
393}
394
Chris Lattner3462ae32001-12-03 22:26:30 +0000395ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
396 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000397 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000398 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000399 return Result;
400}
401
Chris Lattner3462ae32001-12-03 22:26:30 +0000402ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000403 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000404 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
405 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000406}
407
Chris Lattner3462ae32001-12-03 22:26:30 +0000408//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000409//
Chris Lattner3462ae32001-12-03 22:26:30 +0000410static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000411
Chris Lattner3462ae32001-12-03 22:26:30 +0000412ConstantFP *ConstantFP::get(const Type *Ty, double V) {
413 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000414 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000415 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000416 return Result;
417}
418
Chris Lattner3462ae32001-12-03 22:26:30 +0000419//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000420//
Chris Lattner3462ae32001-12-03 22:26:30 +0000421static ValueMap<vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000422
Chris Lattner3462ae32001-12-03 22:26:30 +0000423ConstantArray *ConstantArray::get(const ArrayType *Ty,
424 const vector<Constant*> &V) {
425 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000426 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000427 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000428 return Result;
429}
430
Chris Lattner3462ae32001-12-03 22:26:30 +0000431// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000432// contain the specified string. A null terminator is added to the specified
433// string so that it may be used in a natural way...
434//
Chris Lattner3462ae32001-12-03 22:26:30 +0000435ConstantArray *ConstantArray::get(const string &Str) {
436 vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000437
438 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattner3462ae32001-12-03 22:26:30 +0000439 ElementVals.push_back(ConstantUInt::get(Type::UByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000440
441 // Add a null terminator to the string...
Chris Lattner3462ae32001-12-03 22:26:30 +0000442 ElementVals.push_back(ConstantUInt::get(Type::UByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000443
444 ArrayType *ATy = ArrayType::get(Type::UByteTy/*,stringConstant.length()*/);
Chris Lattner3462ae32001-12-03 22:26:30 +0000445 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000446}
447
448
Chris Lattnerd7a73302001-10-13 06:57:33 +0000449// destroyConstant - Remove the constant from the constant table...
450//
Chris Lattner3462ae32001-12-03 22:26:30 +0000451void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000452 ArrayConstants.remove(this);
453 destroyConstantImpl();
454}
455
Chris Lattner3462ae32001-12-03 22:26:30 +0000456//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000457//
Chris Lattner3462ae32001-12-03 22:26:30 +0000458static ValueMap<vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000459
Chris Lattner3462ae32001-12-03 22:26:30 +0000460ConstantStruct *ConstantStruct::get(const StructType *Ty,
461 const vector<Constant*> &V) {
462 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000463 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000464 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000465 return Result;
466}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000467
Chris Lattnerd7a73302001-10-13 06:57:33 +0000468// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000469//
Chris Lattner3462ae32001-12-03 22:26:30 +0000470void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000471 StructConstants.remove(this);
472 destroyConstantImpl();
473}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000474
Chris Lattner3462ae32001-12-03 22:26:30 +0000475//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000476//
Chris Lattner3462ae32001-12-03 22:26:30 +0000477static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000478
Chris Lattner3462ae32001-12-03 22:26:30 +0000479ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
480 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000481 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000482 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000483 return Result;
484}
485
Chris Lattner3462ae32001-12-03 22:26:30 +0000486//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000487//
Chris Lattner3462ae32001-12-03 22:26:30 +0000488ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000489 assert(GV->getParent() && "Global Value must be attached to a module!");
490
491 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000492 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000493}
494
495
Chris Lattner3462ae32001-12-03 22:26:30 +0000496void ConstantPointerRef::mutateReference(GlobalValue *NewGV) {
497 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000498 Operands[0] = NewGV;
Chris Lattner25033252001-10-03 19:28:15 +0000499}