blob: acec62b4f1ab327fdaa3413e5e8260c517122a9d [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);
Chris Lattner34208392002-04-07 08:42:53 +0000168
169 // Check to make sure that the stringized number is not some string like "Inf"
170 // or NaN, that atof will accept, but the lexer will not. Check that the
171 // string matches the "[-+]?[0-9]" regex.
172 //
173 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
174 ((StrVal[0] == '-' || StrVal[0] == '+') &&
175 (StrVal[0] >= '0' && StrVal[0] <= '9'))) {
176 double TestVal = atof(StrVal.c_str()); // Reparse stringized version!
177 if (TestVal == Val)
178 return StrVal;
179 }
Chris Lattner9a11be82002-04-07 08:37:11 +0000180
181 // Otherwise we could not reparse it to exactly the same value, so we must
182 // output the string in hexadecimal format!
183 //
184 // Behave nicely in the face of C TBAA rules... see:
185 // http://www.nullstone.com/htmls/category/aliastyp.htm
186 //
187 char *Ptr = (char*)&Val;
188 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
189 "assuming that double is 64 bits!");
190 return "0x"+utohexstr(*(uint64_t*)Ptr);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000191}
192
Chris Lattner7f74a562002-01-20 22:54:45 +0000193std::string ConstantArray::getStrValue() const {
194 std::string Result;
Vikram S. Adveeada6b12001-10-28 21:48:05 +0000195
Chris Lattner239a1bd2001-10-29 13:27:09 +0000196 // As a special case, print the array as a string if it is an array of
197 // ubytes or an array of sbytes with positive values.
198 //
199 const Type *ETy = cast<ArrayType>(getType())->getElementType();
200 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
201
202 if (ETy == Type::SByteTy) {
203 for (unsigned i = 0; i < Operands.size(); ++i)
204 if (ETy == Type::SByteTy &&
Chris Lattner3462ae32001-12-03 22:26:30 +0000205 cast<ConstantSInt>(Operands[i])->getValue() < 0) {
Chris Lattner239a1bd2001-10-29 13:27:09 +0000206 isString = false;
207 break;
208 }
209 }
210
211 if (isString) {
212 Result = "c\"";
Chris Lattner78e11ae2001-10-15 00:05:03 +0000213 for (unsigned i = 0; i < Operands.size(); ++i) {
Chris Lattner8f80fe02001-10-14 23:54:12 +0000214 unsigned char C = (ETy == Type::SByteTy) ?
Chris Lattner3462ae32001-12-03 22:26:30 +0000215 (unsigned char)cast<ConstantSInt>(Operands[i])->getValue() :
216 (unsigned char)cast<ConstantUInt>(Operands[i])->getValue();
Chris Lattner8f80fe02001-10-14 23:54:12 +0000217
218 if (isprint(C)) {
219 Result += C;
Chris Lattner239a1bd2001-10-29 13:27:09 +0000220 } else {
221 Result += '\\';
222 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
223 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
Chris Lattner8f80fe02001-10-14 23:54:12 +0000224 }
225 }
Vikram S. Adve34410432001-10-14 23:17:20 +0000226 Result += "\"";
Chris Lattner8f80fe02001-10-14 23:54:12 +0000227
Vikram S. Adve34410432001-10-14 23:17:20 +0000228 } else {
229 Result = "[";
230 if (Operands.size()) {
231 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000232 " " + cast<Constant>(Operands[0])->getStrValue();
Vikram S. Adve34410432001-10-14 23:17:20 +0000233 for (unsigned i = 1; i < Operands.size(); i++)
234 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000235 " " + cast<Constant>(Operands[i])->getStrValue();
Vikram S. Adve34410432001-10-14 23:17:20 +0000236 }
237 Result += " ]";
238 }
239
240 return Result;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000241}
242
Chris Lattner7f74a562002-01-20 22:54:45 +0000243std::string ConstantStruct::getStrValue() const {
244 std::string Result = "{";
Chris Lattnera073acb2001-07-07 08:36:50 +0000245 if (Operands.size()) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000246 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000247 " " + cast<Constant>(Operands[0])->getStrValue();
Chris Lattnera073acb2001-07-07 08:36:50 +0000248 for (unsigned i = 1; i < Operands.size(); i++)
Chris Lattner49d855c2001-09-07 16:46:31 +0000249 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000250 " " + cast<Constant>(Operands[i])->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000251 }
252
253 return Result + " }";
254}
255
Chris Lattner7f74a562002-01-20 22:54:45 +0000256std::string ConstantPointerNull::getStrValue() const {
Chris Lattner436248f2001-09-30 20:14:07 +0000257 return "null";
258}
259
Chris Lattner7f74a562002-01-20 22:54:45 +0000260std::string ConstantPointerRef::getStrValue() const {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000261 const GlobalValue *V = getValue();
262 if (V->hasName()) return "%" + V->getName();
263
264 SlotCalculator *Table = new SlotCalculator(V->getParent(), true);
265 int Slot = Table->getValSlot(V);
266 delete Table;
267
Chris Lattner7f74a562002-01-20 22:54:45 +0000268 if (Slot >= 0) return std::string(" %") + itostr(Slot);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000269 else return "<pointer reference badref>";
Chris Lattner60e0dd72001-10-03 06:12:09 +0000270}
271
Chris Lattnerd7a73302001-10-13 06:57:33 +0000272
273//===----------------------------------------------------------------------===//
274// classof implementations
275
Chris Lattner3462ae32001-12-03 22:26:30 +0000276bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000277 return CPV->getType()->isIntegral();
278}
Chris Lattner3462ae32001-12-03 22:26:30 +0000279bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000280 return CPV->getType()->isSigned();
281}
Chris Lattner3462ae32001-12-03 22:26:30 +0000282bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000283 return CPV->getType()->isUnsigned();
284}
Chris Lattner3462ae32001-12-03 22:26:30 +0000285bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000286 const Type *Ty = CPV->getType();
287 return Ty == Type::FloatTy || Ty == Type::DoubleTy;
288}
Chris Lattner3462ae32001-12-03 22:26:30 +0000289bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000290 return isa<ArrayType>(CPV->getType());
291}
Chris Lattner3462ae32001-12-03 22:26:30 +0000292bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000293 return isa<StructType>(CPV->getType());
294}
Chris Lattner3462ae32001-12-03 22:26:30 +0000295bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000296 return isa<PointerType>(CPV->getType());
297}
298
299
Chris Lattner2f7c9632001-06-06 20:29:01 +0000300//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000301// isValueValidForType implementations
302
Chris Lattner3462ae32001-12-03 22:26:30 +0000303bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000304 switch (Ty->getPrimitiveID()) {
305 default:
306 return false; // These can't be represented as integers!!!
307
308 // Signed types...
309 case Type::SByteTyID:
310 return (Val <= INT8_MAX && Val >= INT8_MIN);
311 case Type::ShortTyID:
312 return (Val <= INT16_MAX && Val >= INT16_MIN);
313 case Type::IntTyID:
314 return (Val <= INT32_MAX && Val >= INT32_MIN);
315 case Type::LongTyID:
316 return true; // This is the largest type...
317 }
318 assert(0 && "WTF?");
319 return false;
320}
321
Chris Lattner3462ae32001-12-03 22:26:30 +0000322bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000323 switch (Ty->getPrimitiveID()) {
324 default:
325 return false; // These can't be represented as integers!!!
326
327 // Unsigned types...
328 case Type::UByteTyID:
329 return (Val <= UINT8_MAX);
330 case Type::UShortTyID:
331 return (Val <= UINT16_MAX);
332 case Type::UIntTyID:
333 return (Val <= UINT32_MAX);
334 case Type::ULongTyID:
335 return true; // This is the largest type...
336 }
337 assert(0 && "WTF?");
338 return false;
339}
340
Chris Lattner3462ae32001-12-03 22:26:30 +0000341bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000342 switch (Ty->getPrimitiveID()) {
343 default:
344 return false; // These can't be represented as floating point!
345
346 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000347 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000348 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000349 return (Val <= UINT8_MAX);
350 */
351 case Type::DoubleTyID:
352 return true; // This is the largest type...
353 }
354};
Chris Lattner9655e542001-07-20 19:16:02 +0000355
Chris Lattner49d855c2001-09-07 16:46:31 +0000356//===----------------------------------------------------------------------===//
357// Hash Function Implementations
358#if 0
Chris Lattner3462ae32001-12-03 22:26:30 +0000359unsigned ConstantSInt::hash(const Type *Ty, int64_t V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000360 return unsigned(Ty->getPrimitiveID() ^ V);
361}
362
Chris Lattner3462ae32001-12-03 22:26:30 +0000363unsigned ConstantUInt::hash(const Type *Ty, uint64_t V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000364 return unsigned(Ty->getPrimitiveID() ^ V);
365}
366
Chris Lattner3462ae32001-12-03 22:26:30 +0000367unsigned ConstantFP::hash(const Type *Ty, double V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000368 return Ty->getPrimitiveID() ^ unsigned(V);
369}
370
Chris Lattner3462ae32001-12-03 22:26:30 +0000371unsigned ConstantArray::hash(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000372 const std::vector<Constant*> &V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000373 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
374 for (unsigned i = 0; i < V.size(); ++i)
375 Result ^= V[i]->getHash() << (i & 7);
376 return Result;
377}
378
Chris Lattner3462ae32001-12-03 22:26:30 +0000379unsigned ConstantStruct::hash(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000380 const std::vector<Constant*> &V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000381 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
382 for (unsigned i = 0; i < V.size(); ++i)
383 Result ^= V[i]->getHash() << (i & 7);
384 return Result;
385}
386#endif
387
388//===----------------------------------------------------------------------===//
389// Factory Function Implementation
390
Chris Lattner3462ae32001-12-03 22:26:30 +0000391template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000392struct ValueMap {
393 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000394 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000395
Chris Lattner3462ae32001-12-03 22:26:30 +0000396 inline ConstantClass *get(const Type *Ty, ValType V) {
397 map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000398 Map.find(ConstHashKey(Ty, V));
399 return (I != Map.end()) ? I->second : 0;
400 }
401
Chris Lattner3462ae32001-12-03 22:26:30 +0000402 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000403 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
404 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000405
Chris Lattner3462ae32001-12-03 22:26:30 +0000406 inline void remove(ConstantClass *CP) {
407 for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000408 E = Map.end(); I != E;++I)
409 if (I->second == CP) {
410 Map.erase(I);
411 return;
412 }
413 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000414};
415
Chris Lattner3462ae32001-12-03 22:26:30 +0000416//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000417//
Chris Lattner3462ae32001-12-03 22:26:30 +0000418static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000419
Chris Lattner3462ae32001-12-03 22:26:30 +0000420ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
421 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000422 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000423 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000424 return Result;
425}
426
Chris Lattner3462ae32001-12-03 22:26:30 +0000427ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
428 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000429 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000430 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000431 return Result;
432}
433
Chris Lattner3462ae32001-12-03 22:26:30 +0000434ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000435 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000436 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
437 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000438}
439
Chris Lattner3462ae32001-12-03 22:26:30 +0000440//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000441//
Chris Lattner3462ae32001-12-03 22:26:30 +0000442static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000443
Chris Lattner3462ae32001-12-03 22:26:30 +0000444ConstantFP *ConstantFP::get(const Type *Ty, double V) {
445 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000446 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000447 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000448 return Result;
449}
450
Chris Lattner3462ae32001-12-03 22:26:30 +0000451//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000452//
Chris Lattner7f74a562002-01-20 22:54:45 +0000453static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000454
Chris Lattner3462ae32001-12-03 22:26:30 +0000455ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000456 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000457 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000458 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000459 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000460 return Result;
461}
462
Chris Lattner3462ae32001-12-03 22:26:30 +0000463// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000464// contain the specified string. A null terminator is added to the specified
465// string so that it may be used in a natural way...
466//
Chris Lattner7f74a562002-01-20 22:54:45 +0000467ConstantArray *ConstantArray::get(const std::string &Str) {
468 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000469
470 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000471 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000472
473 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000474 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000475
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000476 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000477 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000478}
479
480
Chris Lattnerd7a73302001-10-13 06:57:33 +0000481// destroyConstant - Remove the constant from the constant table...
482//
Chris Lattner3462ae32001-12-03 22:26:30 +0000483void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000484 ArrayConstants.remove(this);
485 destroyConstantImpl();
486}
487
Chris Lattner3462ae32001-12-03 22:26:30 +0000488//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000489//
Chris Lattner7f74a562002-01-20 22:54:45 +0000490static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000491
Chris Lattner3462ae32001-12-03 22:26:30 +0000492ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000493 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000494 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000495 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000496 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000497 return Result;
498}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000499
Chris Lattnerd7a73302001-10-13 06:57:33 +0000500// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000501//
Chris Lattner3462ae32001-12-03 22:26:30 +0000502void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000503 StructConstants.remove(this);
504 destroyConstantImpl();
505}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000506
Chris Lattner3462ae32001-12-03 22:26:30 +0000507//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000508//
Chris Lattner3462ae32001-12-03 22:26:30 +0000509static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000510
Chris Lattner3462ae32001-12-03 22:26:30 +0000511ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
512 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000513 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000514 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000515 return Result;
516}
517
Chris Lattner3462ae32001-12-03 22:26:30 +0000518//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000519//
Chris Lattner3462ae32001-12-03 22:26:30 +0000520ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000521 assert(GV->getParent() && "Global Value must be attached to a module!");
522
523 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000524 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000525}
526
527
Chris Lattner3462ae32001-12-03 22:26:30 +0000528void ConstantPointerRef::mutateReference(GlobalValue *NewGV) {
529 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000530 Operands[0] = NewGV;
Chris Lattner25033252001-10-03 19:28:15 +0000531}