blob: f3f3f49bf63cd7160235366c82edf1902688e054 [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"
Chris Lattner6915f8f2002-04-07 22:49:37 +000013#include "llvm/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 Lattner3462ae32001-12-03 22:26:30 +000060void Constant::destroyConstantImpl() {
61 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000062 // references to the constant by other constants in the constant pool. These
63 // constants are implicitly dependant on the module that is being deleted,
64 // but they don't know that. Because we only find out when the CPV is
65 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000066 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000067 //
68 while (!use_empty()) {
69 Value *V = use_back();
70#ifndef NDEBUG // Only in -g mode...
Chris Lattner3462ae32001-12-03 22:26:30 +000071 if (!isa<Constant>(V)) {
Chris Lattnere026e922002-04-07 22:32:25 +000072 std::cerr << "While deleting: ";
73 dump();
74 std::cerr << "\nUse still stuck around after Def is destroyed: ";
75 V->dump();
76 std::cerr << "\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000077 }
78#endif
Chris Lattner3462ae32001-12-03 22:26:30 +000079 assert(isa<Constant>(V) && "References remain to ConstantPointerRef!");
80 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000081 CPV->destroyConstant();
82
83 // The constant should remove itself from our use list...
84 assert((use_empty() || use_back() == V) && "Constant not removed!");
85 }
86
87 // Value has no outstanding references it is safe to delete it now...
88 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000089}
Chris Lattner2f7c9632001-06-06 20:29:01 +000090
91//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000092// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +000093//===----------------------------------------------------------------------===//
94
95//===----------------------------------------------------------------------===//
96// Normal Constructors
97
Chris Lattner3462ae32001-12-03 22:26:30 +000098ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000099 Val = V;
100}
Chris Lattner49d855c2001-09-07 16:46:31 +0000101
Chris Lattner3462ae32001-12-03 22:26:30 +0000102ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000103 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000104}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000105
Chris Lattner3462ae32001-12-03 22:26:30 +0000106ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000107 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000108}
109
Chris Lattner3462ae32001-12-03 22:26:30 +0000110ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000111 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000112}
113
Chris Lattner3462ae32001-12-03 22:26:30 +0000114ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000115 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116 Val = V;
117}
118
Chris Lattner3462ae32001-12-03 22:26:30 +0000119ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000120 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000121 for (unsigned i = 0; i < V.size(); i++) {
122 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000123 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000124 }
125}
126
Chris Lattner3462ae32001-12-03 22:26:30 +0000127ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000128 const std::vector<Constant*> &V) : Constant(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 Lattner3462ae32001-12-03 22:26:30 +0000137ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
138 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000139 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
Chris Lattner7f74a562002-01-20 22:54:45 +0000147std::string ConstantBool::getStrValue() const {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000148 return Val ? "true" : "false";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149}
150
Chris Lattner7f74a562002-01-20 22:54:45 +0000151std::string ConstantSInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000152 return itostr(Val.Signed);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000153}
154
Chris Lattner7f74a562002-01-20 22:54:45 +0000155std::string ConstantUInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000156 return utostr(Val.Unsigned);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000157}
158
Chris Lattner9a11be82002-04-07 08:37:11 +0000159// ConstantFP::getStrValue - We would like to output the FP constant value in
160// exponential notation, but we cannot do this if doing so will lose precision.
161// Check here to make sure that we only output it in exponential format if we
162// can parse the value back and get the same value.
163//
Chris Lattner7f74a562002-01-20 22:54:45 +0000164std::string ConstantFP::getStrValue() const {
Chris Lattner9a11be82002-04-07 08:37:11 +0000165 std::string StrVal = ftostr(Val);
Chris Lattner34208392002-04-07 08:42:53 +0000166
167 // Check to make sure that the stringized number is not some string like "Inf"
168 // or NaN, that atof will accept, but the lexer will not. Check that the
169 // string matches the "[-+]?[0-9]" regex.
170 //
171 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
172 ((StrVal[0] == '-' || StrVal[0] == '+') &&
173 (StrVal[0] >= '0' && StrVal[0] <= '9'))) {
174 double TestVal = atof(StrVal.c_str()); // Reparse stringized version!
175 if (TestVal == Val)
176 return StrVal;
177 }
Chris Lattner9a11be82002-04-07 08:37:11 +0000178
179 // Otherwise we could not reparse it to exactly the same value, so we must
180 // output the string in hexadecimal format!
181 //
182 // Behave nicely in the face of C TBAA rules... see:
183 // http://www.nullstone.com/htmls/category/aliastyp.htm
184 //
185 char *Ptr = (char*)&Val;
186 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
187 "assuming that double is 64 bits!");
188 return "0x"+utohexstr(*(uint64_t*)Ptr);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000189}
190
Chris Lattner7f74a562002-01-20 22:54:45 +0000191std::string ConstantArray::getStrValue() const {
192 std::string Result;
Vikram S. Adveeada6b12001-10-28 21:48:05 +0000193
Chris Lattner239a1bd2001-10-29 13:27:09 +0000194 // As a special case, print the array as a string if it is an array of
195 // ubytes or an array of sbytes with positive values.
196 //
197 const Type *ETy = cast<ArrayType>(getType())->getElementType();
198 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
199
200 if (ETy == Type::SByteTy) {
201 for (unsigned i = 0; i < Operands.size(); ++i)
202 if (ETy == Type::SByteTy &&
Chris Lattner3462ae32001-12-03 22:26:30 +0000203 cast<ConstantSInt>(Operands[i])->getValue() < 0) {
Chris Lattner239a1bd2001-10-29 13:27:09 +0000204 isString = false;
205 break;
206 }
207 }
208
209 if (isString) {
210 Result = "c\"";
Chris Lattner78e11ae2001-10-15 00:05:03 +0000211 for (unsigned i = 0; i < Operands.size(); ++i) {
Chris Lattner8f80fe02001-10-14 23:54:12 +0000212 unsigned char C = (ETy == Type::SByteTy) ?
Chris Lattner3462ae32001-12-03 22:26:30 +0000213 (unsigned char)cast<ConstantSInt>(Operands[i])->getValue() :
214 (unsigned char)cast<ConstantUInt>(Operands[i])->getValue();
Chris Lattner8f80fe02001-10-14 23:54:12 +0000215
216 if (isprint(C)) {
217 Result += C;
Chris Lattner239a1bd2001-10-29 13:27:09 +0000218 } else {
219 Result += '\\';
220 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
221 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
Chris Lattner8f80fe02001-10-14 23:54:12 +0000222 }
223 }
Vikram S. Adve34410432001-10-14 23:17:20 +0000224 Result += "\"";
Chris Lattner8f80fe02001-10-14 23:54:12 +0000225
Vikram S. Adve34410432001-10-14 23:17:20 +0000226 } else {
227 Result = "[";
228 if (Operands.size()) {
229 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000230 " " + cast<Constant>(Operands[0])->getStrValue();
Vikram S. Adve34410432001-10-14 23:17:20 +0000231 for (unsigned i = 1; i < Operands.size(); i++)
232 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000233 " " + cast<Constant>(Operands[i])->getStrValue();
Vikram S. Adve34410432001-10-14 23:17:20 +0000234 }
235 Result += " ]";
236 }
237
238 return Result;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000239}
240
Chris Lattner7f74a562002-01-20 22:54:45 +0000241std::string ConstantStruct::getStrValue() const {
242 std::string Result = "{";
Chris Lattnera073acb2001-07-07 08:36:50 +0000243 if (Operands.size()) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000244 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000245 " " + cast<Constant>(Operands[0])->getStrValue();
Chris Lattnera073acb2001-07-07 08:36:50 +0000246 for (unsigned i = 1; i < Operands.size(); i++)
Chris Lattner49d855c2001-09-07 16:46:31 +0000247 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000248 " " + cast<Constant>(Operands[i])->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249 }
250
251 return Result + " }";
252}
253
Chris Lattner7f74a562002-01-20 22:54:45 +0000254std::string ConstantPointerNull::getStrValue() const {
Chris Lattner436248f2001-09-30 20:14:07 +0000255 return "null";
256}
257
Chris Lattner7f74a562002-01-20 22:54:45 +0000258std::string ConstantPointerRef::getStrValue() const {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000259 const GlobalValue *V = getValue();
260 if (V->hasName()) return "%" + V->getName();
261
Chris Lattner6915f8f2002-04-07 22:49:37 +0000262 // FIXME: This is a gross hack.
Chris Lattnerd7a73302001-10-13 06:57:33 +0000263 SlotCalculator *Table = new SlotCalculator(V->getParent(), true);
264 int Slot = Table->getValSlot(V);
265 delete Table;
266
Chris Lattner7f74a562002-01-20 22:54:45 +0000267 if (Slot >= 0) return std::string(" %") + itostr(Slot);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000268 else return "<pointer reference badref>";
Chris Lattner60e0dd72001-10-03 06:12:09 +0000269}
270
Chris Lattnerd7a73302001-10-13 06:57:33 +0000271
272//===----------------------------------------------------------------------===//
273// classof implementations
274
Chris Lattner3462ae32001-12-03 22:26:30 +0000275bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000276 return CPV->getType()->isIntegral();
277}
Chris Lattner3462ae32001-12-03 22:26:30 +0000278bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000279 return CPV->getType()->isSigned();
280}
Chris Lattner3462ae32001-12-03 22:26:30 +0000281bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000282 return CPV->getType()->isUnsigned();
283}
Chris Lattner3462ae32001-12-03 22:26:30 +0000284bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000285 const Type *Ty = CPV->getType();
286 return Ty == Type::FloatTy || Ty == Type::DoubleTy;
287}
Chris Lattner3462ae32001-12-03 22:26:30 +0000288bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000289 return isa<ArrayType>(CPV->getType());
290}
Chris Lattner3462ae32001-12-03 22:26:30 +0000291bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000292 return isa<StructType>(CPV->getType());
293}
Chris Lattner3462ae32001-12-03 22:26:30 +0000294bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000295 return isa<PointerType>(CPV->getType());
296}
297
298
Chris Lattner2f7c9632001-06-06 20:29:01 +0000299//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000300// isValueValidForType implementations
301
Chris Lattner3462ae32001-12-03 22:26:30 +0000302bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000303 switch (Ty->getPrimitiveID()) {
304 default:
305 return false; // These can't be represented as integers!!!
306
307 // Signed types...
308 case Type::SByteTyID:
309 return (Val <= INT8_MAX && Val >= INT8_MIN);
310 case Type::ShortTyID:
311 return (Val <= INT16_MAX && Val >= INT16_MIN);
312 case Type::IntTyID:
313 return (Val <= INT32_MAX && Val >= INT32_MIN);
314 case Type::LongTyID:
315 return true; // This is the largest type...
316 }
317 assert(0 && "WTF?");
318 return false;
319}
320
Chris Lattner3462ae32001-12-03 22:26:30 +0000321bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000322 switch (Ty->getPrimitiveID()) {
323 default:
324 return false; // These can't be represented as integers!!!
325
326 // Unsigned types...
327 case Type::UByteTyID:
328 return (Val <= UINT8_MAX);
329 case Type::UShortTyID:
330 return (Val <= UINT16_MAX);
331 case Type::UIntTyID:
332 return (Val <= UINT32_MAX);
333 case Type::ULongTyID:
334 return true; // This is the largest type...
335 }
336 assert(0 && "WTF?");
337 return false;
338}
339
Chris Lattner3462ae32001-12-03 22:26:30 +0000340bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000341 switch (Ty->getPrimitiveID()) {
342 default:
343 return false; // These can't be represented as floating point!
344
345 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000346 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000347 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000348 return (Val <= UINT8_MAX);
349 */
350 case Type::DoubleTyID:
351 return true; // This is the largest type...
352 }
353};
Chris Lattner9655e542001-07-20 19:16:02 +0000354
Chris Lattner49d855c2001-09-07 16:46:31 +0000355//===----------------------------------------------------------------------===//
356// Hash Function Implementations
357#if 0
Chris Lattner3462ae32001-12-03 22:26:30 +0000358unsigned ConstantSInt::hash(const Type *Ty, int64_t V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000359 return unsigned(Ty->getPrimitiveID() ^ V);
360}
361
Chris Lattner3462ae32001-12-03 22:26:30 +0000362unsigned ConstantUInt::hash(const Type *Ty, uint64_t V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000363 return unsigned(Ty->getPrimitiveID() ^ V);
364}
365
Chris Lattner3462ae32001-12-03 22:26:30 +0000366unsigned ConstantFP::hash(const Type *Ty, double V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000367 return Ty->getPrimitiveID() ^ unsigned(V);
368}
369
Chris Lattner3462ae32001-12-03 22:26:30 +0000370unsigned ConstantArray::hash(const ArrayType *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
Chris Lattner3462ae32001-12-03 22:26:30 +0000378unsigned ConstantStruct::hash(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000379 const std::vector<Constant*> &V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000380 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
381 for (unsigned i = 0; i < V.size(); ++i)
382 Result ^= V[i]->getHash() << (i & 7);
383 return Result;
384}
385#endif
386
387//===----------------------------------------------------------------------===//
388// Factory Function Implementation
389
Chris Lattner3462ae32001-12-03 22:26:30 +0000390template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000391struct ValueMap {
392 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000393 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000394
Chris Lattner3462ae32001-12-03 22:26:30 +0000395 inline ConstantClass *get(const Type *Ty, ValType V) {
396 map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000397 Map.find(ConstHashKey(Ty, V));
398 return (I != Map.end()) ? I->second : 0;
399 }
400
Chris Lattner3462ae32001-12-03 22:26:30 +0000401 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000402 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
403 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000404
Chris Lattner3462ae32001-12-03 22:26:30 +0000405 inline void remove(ConstantClass *CP) {
406 for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000407 E = Map.end(); I != E;++I)
408 if (I->second == CP) {
409 Map.erase(I);
410 return;
411 }
412 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000413};
414
Chris Lattner3462ae32001-12-03 22:26:30 +0000415//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000416//
Chris Lattner3462ae32001-12-03 22:26:30 +0000417static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000418
Chris Lattner3462ae32001-12-03 22:26:30 +0000419ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
420 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000421 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000422 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000423 return Result;
424}
425
Chris Lattner3462ae32001-12-03 22:26:30 +0000426ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
427 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000428 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000429 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000430 return Result;
431}
432
Chris Lattner3462ae32001-12-03 22:26:30 +0000433ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000434 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000435 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
436 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000437}
438
Chris Lattner3462ae32001-12-03 22:26:30 +0000439//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000440//
Chris Lattner3462ae32001-12-03 22:26:30 +0000441static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000442
Chris Lattner3462ae32001-12-03 22:26:30 +0000443ConstantFP *ConstantFP::get(const Type *Ty, double V) {
444 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000445 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000446 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000447 return Result;
448}
449
Chris Lattner3462ae32001-12-03 22:26:30 +0000450//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000451//
Chris Lattner7f74a562002-01-20 22:54:45 +0000452static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000453
Chris Lattner3462ae32001-12-03 22:26:30 +0000454ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000455 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000456 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000457 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000458 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000459 return Result;
460}
461
Chris Lattner3462ae32001-12-03 22:26:30 +0000462// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000463// contain the specified string. A null terminator is added to the specified
464// string so that it may be used in a natural way...
465//
Chris Lattner7f74a562002-01-20 22:54:45 +0000466ConstantArray *ConstantArray::get(const std::string &Str) {
467 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000468
469 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000470 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000471
472 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000473 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000474
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000475 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000476 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000477}
478
479
Chris Lattnerd7a73302001-10-13 06:57:33 +0000480// destroyConstant - Remove the constant from the constant table...
481//
Chris Lattner3462ae32001-12-03 22:26:30 +0000482void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000483 ArrayConstants.remove(this);
484 destroyConstantImpl();
485}
486
Chris Lattner3462ae32001-12-03 22:26:30 +0000487//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000488//
Chris Lattner7f74a562002-01-20 22:54:45 +0000489static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000490
Chris Lattner3462ae32001-12-03 22:26:30 +0000491ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000492 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000493 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000494 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000495 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000496 return Result;
497}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000498
Chris Lattnerd7a73302001-10-13 06:57:33 +0000499// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000500//
Chris Lattner3462ae32001-12-03 22:26:30 +0000501void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000502 StructConstants.remove(this);
503 destroyConstantImpl();
504}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000505
Chris Lattner3462ae32001-12-03 22:26:30 +0000506//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000507//
Chris Lattner3462ae32001-12-03 22:26:30 +0000508static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000509
Chris Lattner3462ae32001-12-03 22:26:30 +0000510ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
511 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000512 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000513 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000514 return Result;
515}
516
Chris Lattner3462ae32001-12-03 22:26:30 +0000517//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000518//
Chris Lattner3462ae32001-12-03 22:26:30 +0000519ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000520 assert(GV->getParent() && "Global Value must be attached to a module!");
521
522 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000523 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000524}
525
526
Chris Lattner3462ae32001-12-03 22:26:30 +0000527void ConstantPointerRef::mutateReference(GlobalValue *NewGV) {
528 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000529 Operands[0] = NewGV;
Chris Lattner25033252001-10-03 19:28:15 +0000530}