blob: b73886cf1b5b09e8e02522fb6349477d96bde19f [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 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
262 SlotCalculator *Table = new SlotCalculator(V->getParent(), true);
263 int Slot = Table->getValSlot(V);
264 delete Table;
265
Chris Lattner7f74a562002-01-20 22:54:45 +0000266 if (Slot >= 0) return std::string(" %") + itostr(Slot);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000267 else return "<pointer reference badref>";
Chris Lattner60e0dd72001-10-03 06:12:09 +0000268}
269
Chris Lattnerd7a73302001-10-13 06:57:33 +0000270
271//===----------------------------------------------------------------------===//
272// classof implementations
273
Chris Lattner3462ae32001-12-03 22:26:30 +0000274bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000275 return CPV->getType()->isIntegral();
276}
Chris Lattner3462ae32001-12-03 22:26:30 +0000277bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000278 return CPV->getType()->isSigned();
279}
Chris Lattner3462ae32001-12-03 22:26:30 +0000280bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000281 return CPV->getType()->isUnsigned();
282}
Chris Lattner3462ae32001-12-03 22:26:30 +0000283bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000284 const Type *Ty = CPV->getType();
285 return Ty == Type::FloatTy || Ty == Type::DoubleTy;
286}
Chris Lattner3462ae32001-12-03 22:26:30 +0000287bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000288 return isa<ArrayType>(CPV->getType());
289}
Chris Lattner3462ae32001-12-03 22:26:30 +0000290bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000291 return isa<StructType>(CPV->getType());
292}
Chris Lattner3462ae32001-12-03 22:26:30 +0000293bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000294 return isa<PointerType>(CPV->getType());
295}
296
297
Chris Lattner2f7c9632001-06-06 20:29:01 +0000298//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000299// isValueValidForType implementations
300
Chris Lattner3462ae32001-12-03 22:26:30 +0000301bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000302 switch (Ty->getPrimitiveID()) {
303 default:
304 return false; // These can't be represented as integers!!!
305
306 // Signed types...
307 case Type::SByteTyID:
308 return (Val <= INT8_MAX && Val >= INT8_MIN);
309 case Type::ShortTyID:
310 return (Val <= INT16_MAX && Val >= INT16_MIN);
311 case Type::IntTyID:
312 return (Val <= INT32_MAX && Val >= INT32_MIN);
313 case Type::LongTyID:
314 return true; // This is the largest type...
315 }
316 assert(0 && "WTF?");
317 return false;
318}
319
Chris Lattner3462ae32001-12-03 22:26:30 +0000320bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000321 switch (Ty->getPrimitiveID()) {
322 default:
323 return false; // These can't be represented as integers!!!
324
325 // Unsigned types...
326 case Type::UByteTyID:
327 return (Val <= UINT8_MAX);
328 case Type::UShortTyID:
329 return (Val <= UINT16_MAX);
330 case Type::UIntTyID:
331 return (Val <= UINT32_MAX);
332 case Type::ULongTyID:
333 return true; // This is the largest type...
334 }
335 assert(0 && "WTF?");
336 return false;
337}
338
Chris Lattner3462ae32001-12-03 22:26:30 +0000339bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000340 switch (Ty->getPrimitiveID()) {
341 default:
342 return false; // These can't be represented as floating point!
343
344 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000345 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000346 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000347 return (Val <= UINT8_MAX);
348 */
349 case Type::DoubleTyID:
350 return true; // This is the largest type...
351 }
352};
Chris Lattner9655e542001-07-20 19:16:02 +0000353
Chris Lattner49d855c2001-09-07 16:46:31 +0000354//===----------------------------------------------------------------------===//
355// Hash Function Implementations
356#if 0
Chris Lattner3462ae32001-12-03 22:26:30 +0000357unsigned ConstantSInt::hash(const Type *Ty, int64_t V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000358 return unsigned(Ty->getPrimitiveID() ^ V);
359}
360
Chris Lattner3462ae32001-12-03 22:26:30 +0000361unsigned ConstantUInt::hash(const Type *Ty, uint64_t V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000362 return unsigned(Ty->getPrimitiveID() ^ V);
363}
364
Chris Lattner3462ae32001-12-03 22:26:30 +0000365unsigned ConstantFP::hash(const Type *Ty, double V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000366 return Ty->getPrimitiveID() ^ unsigned(V);
367}
368
Chris Lattner3462ae32001-12-03 22:26:30 +0000369unsigned ConstantArray::hash(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000370 const std::vector<Constant*> &V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000371 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
372 for (unsigned i = 0; i < V.size(); ++i)
373 Result ^= V[i]->getHash() << (i & 7);
374 return Result;
375}
376
Chris Lattner3462ae32001-12-03 22:26:30 +0000377unsigned ConstantStruct::hash(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000378 const std::vector<Constant*> &V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000379 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
380 for (unsigned i = 0; i < V.size(); ++i)
381 Result ^= V[i]->getHash() << (i & 7);
382 return Result;
383}
384#endif
385
386//===----------------------------------------------------------------------===//
387// Factory Function Implementation
388
Chris Lattner3462ae32001-12-03 22:26:30 +0000389template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000390struct ValueMap {
391 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000392 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000393
Chris Lattner3462ae32001-12-03 22:26:30 +0000394 inline ConstantClass *get(const Type *Ty, ValType V) {
395 map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000396 Map.find(ConstHashKey(Ty, V));
397 return (I != Map.end()) ? I->second : 0;
398 }
399
Chris Lattner3462ae32001-12-03 22:26:30 +0000400 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000401 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
402 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000403
Chris Lattner3462ae32001-12-03 22:26:30 +0000404 inline void remove(ConstantClass *CP) {
405 for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000406 E = Map.end(); I != E;++I)
407 if (I->second == CP) {
408 Map.erase(I);
409 return;
410 }
411 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000412};
413
Chris Lattner3462ae32001-12-03 22:26:30 +0000414//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000415//
Chris Lattner3462ae32001-12-03 22:26:30 +0000416static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000417
Chris Lattner3462ae32001-12-03 22:26:30 +0000418ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
419 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)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 ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000422 return Result;
423}
424
Chris Lattner3462ae32001-12-03 22:26:30 +0000425ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
426 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000427 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000428 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000429 return Result;
430}
431
Chris Lattner3462ae32001-12-03 22:26:30 +0000432ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000433 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000434 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
435 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000436}
437
Chris Lattner3462ae32001-12-03 22:26:30 +0000438//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000439//
Chris Lattner3462ae32001-12-03 22:26:30 +0000440static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000441
Chris Lattner3462ae32001-12-03 22:26:30 +0000442ConstantFP *ConstantFP::get(const Type *Ty, double V) {
443 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000444 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000445 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000446 return Result;
447}
448
Chris Lattner3462ae32001-12-03 22:26:30 +0000449//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000450//
Chris Lattner7f74a562002-01-20 22:54:45 +0000451static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000452
Chris Lattner3462ae32001-12-03 22:26:30 +0000453ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000454 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000455 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000456 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000457 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000458 return Result;
459}
460
Chris Lattner3462ae32001-12-03 22:26:30 +0000461// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000462// contain the specified string. A null terminator is added to the specified
463// string so that it may be used in a natural way...
464//
Chris Lattner7f74a562002-01-20 22:54:45 +0000465ConstantArray *ConstantArray::get(const std::string &Str) {
466 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000467
468 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000469 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000470
471 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000472 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000473
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000474 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000475 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000476}
477
478
Chris Lattnerd7a73302001-10-13 06:57:33 +0000479// destroyConstant - Remove the constant from the constant table...
480//
Chris Lattner3462ae32001-12-03 22:26:30 +0000481void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000482 ArrayConstants.remove(this);
483 destroyConstantImpl();
484}
485
Chris Lattner3462ae32001-12-03 22:26:30 +0000486//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000487//
Chris Lattner7f74a562002-01-20 22:54:45 +0000488static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000489
Chris Lattner3462ae32001-12-03 22:26:30 +0000490ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000491 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000492 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000493 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000494 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000495 return Result;
496}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000497
Chris Lattnerd7a73302001-10-13 06:57:33 +0000498// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000499//
Chris Lattner3462ae32001-12-03 22:26:30 +0000500void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000501 StructConstants.remove(this);
502 destroyConstantImpl();
503}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000504
Chris Lattner3462ae32001-12-03 22:26:30 +0000505//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000506//
Chris Lattner3462ae32001-12-03 22:26:30 +0000507static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000508
Chris Lattner3462ae32001-12-03 22:26:30 +0000509ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
510 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000511 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000512 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000513 return Result;
514}
515
Chris Lattner3462ae32001-12-03 22:26:30 +0000516//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000517//
Chris Lattner3462ae32001-12-03 22:26:30 +0000518ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000519 assert(GV->getParent() && "Global Value must be attached to a module!");
520
521 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000522 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000523}
524
525
Chris Lattner3462ae32001-12-03 22:26:30 +0000526void ConstantPointerRef::mutateReference(GlobalValue *NewGV) {
527 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000528 Operands[0] = NewGV;
Chris Lattner25033252001-10-03 19:28:15 +0000529}