blob: 410276683d15db17cf283ce2e7da5cfd90639045 [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>
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 Lattner3462ae32001-12-03 22:26:30 +000037Constant *Constant::getNullConstant(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 Lattnerd7a73302001-10-13 06:57:33 +000060#ifndef NDEBUG
61#include "llvm/Assembly/Writer.h"
Chris Lattner7f74a562002-01-20 22:54:45 +000062using std::cerr;
Chris Lattnerd7a73302001-10-13 06:57:33 +000063#endif
64
Chris Lattner3462ae32001-12-03 22:26:30 +000065void Constant::destroyConstantImpl() {
66 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000067 // references to the constant by other constants in the constant pool. These
68 // constants are implicitly dependant on the module that is being deleted,
69 // but they don't know that. Because we only find out when the CPV is
70 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000071 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000072 //
73 while (!use_empty()) {
74 Value *V = use_back();
75#ifndef NDEBUG // Only in -g mode...
Chris Lattner3462ae32001-12-03 22:26:30 +000076 if (!isa<Constant>(V)) {
Chris Lattner7f74a562002-01-20 22:54:45 +000077 cerr << "While deleting: " << this << "\n";
78 cerr << "Use still stuck around after Def is destroyed: " << V << "\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000079 }
80#endif
Chris Lattner3462ae32001-12-03 22:26:30 +000081 assert(isa<Constant>(V) && "References remain to ConstantPointerRef!");
82 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000083 CPV->destroyConstant();
84
85 // The constant should remove itself from our use list...
86 assert((use_empty() || use_back() == V) && "Constant not removed!");
87 }
88
89 // Value has no outstanding references it is safe to delete it now...
90 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000091}
Chris Lattner2f7c9632001-06-06 20:29:01 +000092
93//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000094// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +000095//===----------------------------------------------------------------------===//
96
97//===----------------------------------------------------------------------===//
98// Normal Constructors
99
Chris Lattner3462ae32001-12-03 22:26:30 +0000100ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000101 Val = V;
102}
Chris Lattner49d855c2001-09-07 16:46:31 +0000103
Chris Lattner3462ae32001-12-03 22:26:30 +0000104ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000105 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000106}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000107
Chris Lattner3462ae32001-12-03 22:26:30 +0000108ConstantSInt::ConstantSInt(const Type *Ty, int64_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 +0000112ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
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}
115
Chris Lattner3462ae32001-12-03 22:26:30 +0000116ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000117 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000118 Val = V;
119}
120
Chris Lattner3462ae32001-12-03 22:26:30 +0000121ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000122 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000123 for (unsigned i = 0; i < V.size(); i++) {
124 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000125 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000126 }
127}
128
Chris Lattner3462ae32001-12-03 22:26:30 +0000129ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000130 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000131 const StructType::ElementTypes &ETypes = T->getElementTypes();
Chris Lattner49d855c2001-09-07 16:46:31 +0000132
Chris Lattner2f7c9632001-06-06 20:29:01 +0000133 for (unsigned i = 0; i < V.size(); i++) {
134 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000135 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136 }
137}
138
Chris Lattner3462ae32001-12-03 22:26:30 +0000139ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
140 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000141 Operands.push_back(Use(GV, this));
142}
143
144
Chris Lattner2f7c9632001-06-06 20:29:01 +0000145
146//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000147// getStrValue implementations
148
Chris Lattner7f74a562002-01-20 22:54:45 +0000149std::string ConstantBool::getStrValue() const {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000150 return Val ? "true" : "false";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000151}
152
Chris Lattner7f74a562002-01-20 22:54:45 +0000153std::string ConstantSInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000154 return itostr(Val.Signed);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000155}
156
Chris Lattner7f74a562002-01-20 22:54:45 +0000157std::string ConstantUInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000158 return utostr(Val.Unsigned);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000159}
160
Chris Lattner9a11be82002-04-07 08:37:11 +0000161// ConstantFP::getStrValue - We would like to output the FP constant value in
162// exponential notation, but we cannot do this if doing so will lose precision.
163// Check here to make sure that we only output it in exponential format if we
164// can parse the value back and get the same value.
165//
Chris Lattner7f74a562002-01-20 22:54:45 +0000166std::string ConstantFP::getStrValue() const {
Chris Lattner9a11be82002-04-07 08:37:11 +0000167 std::string StrVal = ftostr(Val);
168 double TestVal = atof(StrVal.c_str()); // Reparse stringized version!
169 if (TestVal == Val)
170 return StrVal;
171
172 // Otherwise we could not reparse it to exactly the same value, so we must
173 // output the string in hexadecimal format!
174 //
175 // Behave nicely in the face of C TBAA rules... see:
176 // http://www.nullstone.com/htmls/category/aliastyp.htm
177 //
178 char *Ptr = (char*)&Val;
179 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
180 "assuming that double is 64 bits!");
181 return "0x"+utohexstr(*(uint64_t*)Ptr);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000182}
183
Chris Lattner7f74a562002-01-20 22:54:45 +0000184std::string ConstantArray::getStrValue() const {
185 std::string Result;
Vikram S. Adveeada6b12001-10-28 21:48:05 +0000186
Chris Lattner239a1bd2001-10-29 13:27:09 +0000187 // As a special case, print the array as a string if it is an array of
188 // ubytes or an array of sbytes with positive values.
189 //
190 const Type *ETy = cast<ArrayType>(getType())->getElementType();
191 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
192
193 if (ETy == Type::SByteTy) {
194 for (unsigned i = 0; i < Operands.size(); ++i)
195 if (ETy == Type::SByteTy &&
Chris Lattner3462ae32001-12-03 22:26:30 +0000196 cast<ConstantSInt>(Operands[i])->getValue() < 0) {
Chris Lattner239a1bd2001-10-29 13:27:09 +0000197 isString = false;
198 break;
199 }
200 }
201
202 if (isString) {
203 Result = "c\"";
Chris Lattner78e11ae2001-10-15 00:05:03 +0000204 for (unsigned i = 0; i < Operands.size(); ++i) {
Chris Lattner8f80fe02001-10-14 23:54:12 +0000205 unsigned char C = (ETy == Type::SByteTy) ?
Chris Lattner3462ae32001-12-03 22:26:30 +0000206 (unsigned char)cast<ConstantSInt>(Operands[i])->getValue() :
207 (unsigned char)cast<ConstantUInt>(Operands[i])->getValue();
Chris Lattner8f80fe02001-10-14 23:54:12 +0000208
209 if (isprint(C)) {
210 Result += C;
Chris Lattner239a1bd2001-10-29 13:27:09 +0000211 } else {
212 Result += '\\';
213 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
214 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
Chris Lattner8f80fe02001-10-14 23:54:12 +0000215 }
216 }
Vikram S. Adve34410432001-10-14 23:17:20 +0000217 Result += "\"";
Chris Lattner8f80fe02001-10-14 23:54:12 +0000218
Vikram S. Adve34410432001-10-14 23:17:20 +0000219 } else {
220 Result = "[";
221 if (Operands.size()) {
222 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000223 " " + cast<Constant>(Operands[0])->getStrValue();
Vikram S. Adve34410432001-10-14 23:17:20 +0000224 for (unsigned i = 1; i < Operands.size(); i++)
225 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000226 " " + cast<Constant>(Operands[i])->getStrValue();
Vikram S. Adve34410432001-10-14 23:17:20 +0000227 }
228 Result += " ]";
229 }
230
231 return Result;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000232}
233
Chris Lattner7f74a562002-01-20 22:54:45 +0000234std::string ConstantStruct::getStrValue() const {
235 std::string Result = "{";
Chris Lattnera073acb2001-07-07 08:36:50 +0000236 if (Operands.size()) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000237 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000238 " " + cast<Constant>(Operands[0])->getStrValue();
Chris Lattnera073acb2001-07-07 08:36:50 +0000239 for (unsigned i = 1; i < Operands.size(); i++)
Chris Lattner49d855c2001-09-07 16:46:31 +0000240 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000241 " " + cast<Constant>(Operands[i])->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000242 }
243
244 return Result + " }";
245}
246
Chris Lattner7f74a562002-01-20 22:54:45 +0000247std::string ConstantPointerNull::getStrValue() const {
Chris Lattner436248f2001-09-30 20:14:07 +0000248 return "null";
249}
250
Chris Lattner7f74a562002-01-20 22:54:45 +0000251std::string ConstantPointerRef::getStrValue() const {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000252 const GlobalValue *V = getValue();
253 if (V->hasName()) return "%" + V->getName();
254
255 SlotCalculator *Table = new SlotCalculator(V->getParent(), true);
256 int Slot = Table->getValSlot(V);
257 delete Table;
258
Chris Lattner7f74a562002-01-20 22:54:45 +0000259 if (Slot >= 0) return std::string(" %") + itostr(Slot);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000260 else return "<pointer reference badref>";
Chris Lattner60e0dd72001-10-03 06:12:09 +0000261}
262
Chris Lattnerd7a73302001-10-13 06:57:33 +0000263
264//===----------------------------------------------------------------------===//
265// classof implementations
266
Chris Lattner3462ae32001-12-03 22:26:30 +0000267bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000268 return CPV->getType()->isIntegral();
269}
Chris Lattner3462ae32001-12-03 22:26:30 +0000270bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000271 return CPV->getType()->isSigned();
272}
Chris Lattner3462ae32001-12-03 22:26:30 +0000273bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000274 return CPV->getType()->isUnsigned();
275}
Chris Lattner3462ae32001-12-03 22:26:30 +0000276bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000277 const Type *Ty = CPV->getType();
278 return Ty == Type::FloatTy || Ty == Type::DoubleTy;
279}
Chris Lattner3462ae32001-12-03 22:26:30 +0000280bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000281 return isa<ArrayType>(CPV->getType());
282}
Chris Lattner3462ae32001-12-03 22:26:30 +0000283bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000284 return isa<StructType>(CPV->getType());
285}
Chris Lattner3462ae32001-12-03 22:26:30 +0000286bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000287 return isa<PointerType>(CPV->getType());
288}
289
290
Chris Lattner2f7c9632001-06-06 20:29:01 +0000291//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000292// isValueValidForType implementations
293
Chris Lattner3462ae32001-12-03 22:26:30 +0000294bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000295 switch (Ty->getPrimitiveID()) {
296 default:
297 return false; // These can't be represented as integers!!!
298
299 // Signed types...
300 case Type::SByteTyID:
301 return (Val <= INT8_MAX && Val >= INT8_MIN);
302 case Type::ShortTyID:
303 return (Val <= INT16_MAX && Val >= INT16_MIN);
304 case Type::IntTyID:
305 return (Val <= INT32_MAX && Val >= INT32_MIN);
306 case Type::LongTyID:
307 return true; // This is the largest type...
308 }
309 assert(0 && "WTF?");
310 return false;
311}
312
Chris Lattner3462ae32001-12-03 22:26:30 +0000313bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000314 switch (Ty->getPrimitiveID()) {
315 default:
316 return false; // These can't be represented as integers!!!
317
318 // Unsigned types...
319 case Type::UByteTyID:
320 return (Val <= UINT8_MAX);
321 case Type::UShortTyID:
322 return (Val <= UINT16_MAX);
323 case Type::UIntTyID:
324 return (Val <= UINT32_MAX);
325 case Type::ULongTyID:
326 return true; // This is the largest type...
327 }
328 assert(0 && "WTF?");
329 return false;
330}
331
Chris Lattner3462ae32001-12-03 22:26:30 +0000332bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000333 switch (Ty->getPrimitiveID()) {
334 default:
335 return false; // These can't be represented as floating point!
336
337 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000338 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000339 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000340 return (Val <= UINT8_MAX);
341 */
342 case Type::DoubleTyID:
343 return true; // This is the largest type...
344 }
345};
Chris Lattner9655e542001-07-20 19:16:02 +0000346
Chris Lattner49d855c2001-09-07 16:46:31 +0000347//===----------------------------------------------------------------------===//
348// Hash Function Implementations
349#if 0
Chris Lattner3462ae32001-12-03 22:26:30 +0000350unsigned ConstantSInt::hash(const Type *Ty, int64_t V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000351 return unsigned(Ty->getPrimitiveID() ^ V);
352}
353
Chris Lattner3462ae32001-12-03 22:26:30 +0000354unsigned ConstantUInt::hash(const Type *Ty, uint64_t V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000355 return unsigned(Ty->getPrimitiveID() ^ V);
356}
357
Chris Lattner3462ae32001-12-03 22:26:30 +0000358unsigned ConstantFP::hash(const Type *Ty, double V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000359 return Ty->getPrimitiveID() ^ unsigned(V);
360}
361
Chris Lattner3462ae32001-12-03 22:26:30 +0000362unsigned ConstantArray::hash(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000363 const std::vector<Constant*> &V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000364 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
365 for (unsigned i = 0; i < V.size(); ++i)
366 Result ^= V[i]->getHash() << (i & 7);
367 return Result;
368}
369
Chris Lattner3462ae32001-12-03 22:26:30 +0000370unsigned ConstantStruct::hash(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000371 const std::vector<Constant*> &V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000372 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
373 for (unsigned i = 0; i < V.size(); ++i)
374 Result ^= V[i]->getHash() << (i & 7);
375 return Result;
376}
377#endif
378
379//===----------------------------------------------------------------------===//
380// Factory Function Implementation
381
Chris Lattner3462ae32001-12-03 22:26:30 +0000382template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000383struct ValueMap {
384 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000385 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000386
Chris Lattner3462ae32001-12-03 22:26:30 +0000387 inline ConstantClass *get(const Type *Ty, ValType V) {
388 map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000389 Map.find(ConstHashKey(Ty, V));
390 return (I != Map.end()) ? I->second : 0;
391 }
392
Chris Lattner3462ae32001-12-03 22:26:30 +0000393 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000394 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
395 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000396
Chris Lattner3462ae32001-12-03 22:26:30 +0000397 inline void remove(ConstantClass *CP) {
398 for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000399 E = Map.end(); I != E;++I)
400 if (I->second == CP) {
401 Map.erase(I);
402 return;
403 }
404 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000405};
406
Chris Lattner3462ae32001-12-03 22:26:30 +0000407//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000408//
Chris Lattner3462ae32001-12-03 22:26:30 +0000409static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000410
Chris Lattner3462ae32001-12-03 22:26:30 +0000411ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
412 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000413 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000414 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000415 return Result;
416}
417
Chris Lattner3462ae32001-12-03 22:26:30 +0000418ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
419 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000420 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000421 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000422 return Result;
423}
424
Chris Lattner3462ae32001-12-03 22:26:30 +0000425ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000426 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000427 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
428 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000429}
430
Chris Lattner3462ae32001-12-03 22:26:30 +0000431//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000432//
Chris Lattner3462ae32001-12-03 22:26:30 +0000433static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000434
Chris Lattner3462ae32001-12-03 22:26:30 +0000435ConstantFP *ConstantFP::get(const Type *Ty, double V) {
436 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000437 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000438 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000439 return Result;
440}
441
Chris Lattner3462ae32001-12-03 22:26:30 +0000442//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000443//
Chris Lattner7f74a562002-01-20 22:54:45 +0000444static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000445
Chris Lattner3462ae32001-12-03 22:26:30 +0000446ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000447 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000448 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000449 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000450 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000451 return Result;
452}
453
Chris Lattner3462ae32001-12-03 22:26:30 +0000454// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000455// contain the specified string. A null terminator is added to the specified
456// string so that it may be used in a natural way...
457//
Chris Lattner7f74a562002-01-20 22:54:45 +0000458ConstantArray *ConstantArray::get(const std::string &Str) {
459 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000460
461 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000462 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000463
464 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000465 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000466
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000467 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000468 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000469}
470
471
Chris Lattnerd7a73302001-10-13 06:57:33 +0000472// destroyConstant - Remove the constant from the constant table...
473//
Chris Lattner3462ae32001-12-03 22:26:30 +0000474void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000475 ArrayConstants.remove(this);
476 destroyConstantImpl();
477}
478
Chris Lattner3462ae32001-12-03 22:26:30 +0000479//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000480//
Chris Lattner7f74a562002-01-20 22:54:45 +0000481static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000482
Chris Lattner3462ae32001-12-03 22:26:30 +0000483ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000484 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000485 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000486 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000487 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000488 return Result;
489}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000490
Chris Lattnerd7a73302001-10-13 06:57:33 +0000491// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000492//
Chris Lattner3462ae32001-12-03 22:26:30 +0000493void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000494 StructConstants.remove(this);
495 destroyConstantImpl();
496}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000497
Chris Lattner3462ae32001-12-03 22:26:30 +0000498//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000499//
Chris Lattner3462ae32001-12-03 22:26:30 +0000500static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000501
Chris Lattner3462ae32001-12-03 22:26:30 +0000502ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
503 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000504 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000505 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000506 return Result;
507}
508
Chris Lattner3462ae32001-12-03 22:26:30 +0000509//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000510//
Chris Lattner3462ae32001-12-03 22:26:30 +0000511ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000512 assert(GV->getParent() && "Global Value must be attached to a module!");
513
514 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000515 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000516}
517
518
Chris Lattner3462ae32001-12-03 22:26:30 +0000519void ConstantPointerRef::mutateReference(GlobalValue *NewGV) {
520 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000521 Operands[0] = NewGV;
Chris Lattner25033252001-10-03 19:28:15 +0000522}