blob: d9a2fa74314f6a8924d6de33a04c89b0b09401f2 [file] [log] [blame]
Chris Lattner3462ae32001-12-03 22:26:30 +00001//===-- ConstantVals.cpp - Implement Constant nodes --------------*- C++ -*--=//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
Chris Lattner3462ae32001-12-03 22:26:30 +00003// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +00004//
5//===----------------------------------------------------------------------===//
6
7#define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends...
Chris Lattner3462ae32001-12-03 22:26:30 +00008#include "llvm/ConstantVals.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00009#include "llvm/DerivedTypes.h"
10#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000011#include "llvm/GlobalValue.h"
12#include "llvm/Module.h"
13#include "llvm/Analysis/SlotCalculator.h"
Chris Lattner5de22042001-11-27 00:03:19 +000014#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000015#include <algorithm>
16#include <assert.h>
17
Chris Lattner7f74a562002-01-20 22:54:45 +000018using std::map;
19using std::pair;
20using std::make_pair;
21
Chris Lattner3462ae32001-12-03 22:26:30 +000022ConstantBool *ConstantBool::True = new ConstantBool(true);
23ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000024
Chris Lattner9655e542001-07-20 19:16:02 +000025
Chris Lattner2f7c9632001-06-06 20:29:01 +000026//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000027// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000028//===----------------------------------------------------------------------===//
29
30// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000031void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000032 assert(ST && "Type::setName - Must provide symbol table argument!");
33
34 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000035}
36
37// Static constructor to create a '0' constant of arbitrary type...
Chris Lattner3462ae32001-12-03 22:26:30 +000038Constant *Constant::getNullConstant(const Type *Ty) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000039 switch (Ty->getPrimitiveID()) {
Chris Lattner3462ae32001-12-03 22:26:30 +000040 case Type::BoolTyID: return ConstantBool::get(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000041 case Type::SByteTyID:
42 case Type::ShortTyID:
43 case Type::IntTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000044 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000045
46 case Type::UByteTyID:
47 case Type::UShortTyID:
48 case Type::UIntTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000049 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000050
51 case Type::FloatTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000052 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
Chris Lattner436248f2001-09-30 20:14:07 +000053
54 case Type::PointerTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000055 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2f7c9632001-06-06 20:29:01 +000056 default:
57 return 0;
58 }
59}
60
Chris Lattnerd7a73302001-10-13 06:57:33 +000061#ifndef NDEBUG
62#include "llvm/Assembly/Writer.h"
Chris Lattner7f74a562002-01-20 22:54:45 +000063using std::cerr;
Chris Lattnerd7a73302001-10-13 06:57:33 +000064#endif
65
Chris Lattner3462ae32001-12-03 22:26:30 +000066void Constant::destroyConstantImpl() {
67 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000068 // references to the constant by other constants in the constant pool. These
69 // constants are implicitly dependant on the module that is being deleted,
70 // but they don't know that. Because we only find out when the CPV is
71 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000072 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000073 //
74 while (!use_empty()) {
75 Value *V = use_back();
76#ifndef NDEBUG // Only in -g mode...
Chris Lattner3462ae32001-12-03 22:26:30 +000077 if (!isa<Constant>(V)) {
Chris Lattner7f74a562002-01-20 22:54:45 +000078 cerr << "While deleting: " << this << "\n";
79 cerr << "Use still stuck around after Def is destroyed: " << V << "\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000080 }
81#endif
Chris Lattner3462ae32001-12-03 22:26:30 +000082 assert(isa<Constant>(V) && "References remain to ConstantPointerRef!");
83 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000084 CPV->destroyConstant();
85
86 // The constant should remove itself from our use list...
87 assert((use_empty() || use_back() == V) && "Constant not removed!");
88 }
89
90 // Value has no outstanding references it is safe to delete it now...
91 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000092}
Chris Lattner2f7c9632001-06-06 20:29:01 +000093
94//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000095// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +000096//===----------------------------------------------------------------------===//
97
98//===----------------------------------------------------------------------===//
99// Normal Constructors
100
Chris Lattner3462ae32001-12-03 22:26:30 +0000101ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000102 Val = V;
103}
Chris Lattner49d855c2001-09-07 16:46:31 +0000104
Chris Lattner3462ae32001-12-03 22:26:30 +0000105ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000106 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000107}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000108
Chris Lattner3462ae32001-12-03 22:26:30 +0000109ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000110 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000111}
112
Chris Lattner3462ae32001-12-03 22:26:30 +0000113ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000114 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115}
116
Chris Lattner3462ae32001-12-03 22:26:30 +0000117ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000118 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119 Val = V;
120}
121
Chris Lattner3462ae32001-12-03 22:26:30 +0000122ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000123 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000124 for (unsigned i = 0; i < V.size(); i++) {
125 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000126 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127 }
128}
129
Chris Lattner3462ae32001-12-03 22:26:30 +0000130ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000131 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000132 const StructType::ElementTypes &ETypes = T->getElementTypes();
Chris Lattner49d855c2001-09-07 16:46:31 +0000133
Chris Lattner2f7c9632001-06-06 20:29:01 +0000134 for (unsigned i = 0; i < V.size(); i++) {
135 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000136 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000137 }
138}
139
Chris Lattner3462ae32001-12-03 22:26:30 +0000140ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
141 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000142 Operands.push_back(Use(GV, this));
143}
144
145
Chris Lattner2f7c9632001-06-06 20:29:01 +0000146
147//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000148// getStrValue implementations
149
Chris Lattner7f74a562002-01-20 22:54:45 +0000150std::string ConstantBool::getStrValue() const {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000151 return Val ? "true" : "false";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000152}
153
Chris Lattner7f74a562002-01-20 22:54:45 +0000154std::string ConstantSInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000155 return itostr(Val.Signed);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000156}
157
Chris Lattner7f74a562002-01-20 22:54:45 +0000158std::string ConstantUInt::getStrValue() const {
Chris Lattner9655e542001-07-20 19:16:02 +0000159 return utostr(Val.Unsigned);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160}
161
Chris Lattner7f74a562002-01-20 22:54:45 +0000162std::string ConstantFP::getStrValue() const {
Chris Lattnerd06dd692001-07-15 00:18:39 +0000163 return ftostr(Val);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000164}
165
Chris Lattner7f74a562002-01-20 22:54:45 +0000166std::string ConstantArray::getStrValue() const {
167 std::string Result;
Vikram S. Adveeada6b12001-10-28 21:48:05 +0000168
Chris Lattner239a1bd2001-10-29 13:27:09 +0000169 // As a special case, print the array as a string if it is an array of
170 // ubytes or an array of sbytes with positive values.
171 //
172 const Type *ETy = cast<ArrayType>(getType())->getElementType();
173 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
174
175 if (ETy == Type::SByteTy) {
176 for (unsigned i = 0; i < Operands.size(); ++i)
177 if (ETy == Type::SByteTy &&
Chris Lattner3462ae32001-12-03 22:26:30 +0000178 cast<ConstantSInt>(Operands[i])->getValue() < 0) {
Chris Lattner239a1bd2001-10-29 13:27:09 +0000179 isString = false;
180 break;
181 }
182 }
183
184 if (isString) {
185 Result = "c\"";
Chris Lattner78e11ae2001-10-15 00:05:03 +0000186 for (unsigned i = 0; i < Operands.size(); ++i) {
Chris Lattner8f80fe02001-10-14 23:54:12 +0000187 unsigned char C = (ETy == Type::SByteTy) ?
Chris Lattner3462ae32001-12-03 22:26:30 +0000188 (unsigned char)cast<ConstantSInt>(Operands[i])->getValue() :
189 (unsigned char)cast<ConstantUInt>(Operands[i])->getValue();
Chris Lattner8f80fe02001-10-14 23:54:12 +0000190
191 if (isprint(C)) {
192 Result += C;
Chris Lattner239a1bd2001-10-29 13:27:09 +0000193 } else {
194 Result += '\\';
195 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
196 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
Chris Lattner8f80fe02001-10-14 23:54:12 +0000197 }
198 }
Vikram S. Adve34410432001-10-14 23:17:20 +0000199 Result += "\"";
Chris Lattner8f80fe02001-10-14 23:54:12 +0000200
Vikram S. Adve34410432001-10-14 23:17:20 +0000201 } else {
202 Result = "[";
203 if (Operands.size()) {
204 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000205 " " + cast<Constant>(Operands[0])->getStrValue();
Vikram S. Adve34410432001-10-14 23:17:20 +0000206 for (unsigned i = 1; i < Operands.size(); i++)
207 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000208 " " + cast<Constant>(Operands[i])->getStrValue();
Vikram S. Adve34410432001-10-14 23:17:20 +0000209 }
210 Result += " ]";
211 }
212
213 return Result;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214}
215
Chris Lattner7f74a562002-01-20 22:54:45 +0000216std::string ConstantStruct::getStrValue() const {
217 std::string Result = "{";
Chris Lattnera073acb2001-07-07 08:36:50 +0000218 if (Operands.size()) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000219 Result += " " + Operands[0]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000220 " " + cast<Constant>(Operands[0])->getStrValue();
Chris Lattnera073acb2001-07-07 08:36:50 +0000221 for (unsigned i = 1; i < Operands.size(); i++)
Chris Lattner49d855c2001-09-07 16:46:31 +0000222 Result += ", " + Operands[i]->getType()->getDescription() +
Chris Lattner3462ae32001-12-03 22:26:30 +0000223 " " + cast<Constant>(Operands[i])->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000224 }
225
226 return Result + " }";
227}
228
Chris Lattner7f74a562002-01-20 22:54:45 +0000229std::string ConstantPointerNull::getStrValue() const {
Chris Lattner436248f2001-09-30 20:14:07 +0000230 return "null";
231}
232
Chris Lattner7f74a562002-01-20 22:54:45 +0000233std::string ConstantPointerRef::getStrValue() const {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000234 const GlobalValue *V = getValue();
235 if (V->hasName()) return "%" + V->getName();
236
237 SlotCalculator *Table = new SlotCalculator(V->getParent(), true);
238 int Slot = Table->getValSlot(V);
239 delete Table;
240
Chris Lattner7f74a562002-01-20 22:54:45 +0000241 if (Slot >= 0) return std::string(" %") + itostr(Slot);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000242 else return "<pointer reference badref>";
Chris Lattner60e0dd72001-10-03 06:12:09 +0000243}
244
Chris Lattnerd7a73302001-10-13 06:57:33 +0000245
246//===----------------------------------------------------------------------===//
247// classof implementations
248
Chris Lattner3462ae32001-12-03 22:26:30 +0000249bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000250 return CPV->getType()->isIntegral();
251}
Chris Lattner3462ae32001-12-03 22:26:30 +0000252bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000253 return CPV->getType()->isSigned();
254}
Chris Lattner3462ae32001-12-03 22:26:30 +0000255bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000256 return CPV->getType()->isUnsigned();
257}
Chris Lattner3462ae32001-12-03 22:26:30 +0000258bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000259 const Type *Ty = CPV->getType();
260 return Ty == Type::FloatTy || Ty == Type::DoubleTy;
261}
Chris Lattner3462ae32001-12-03 22:26:30 +0000262bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000263 return isa<ArrayType>(CPV->getType());
264}
Chris Lattner3462ae32001-12-03 22:26:30 +0000265bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000266 return isa<StructType>(CPV->getType());
267}
Chris Lattner3462ae32001-12-03 22:26:30 +0000268bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000269 return isa<PointerType>(CPV->getType());
270}
271
272
Chris Lattner2f7c9632001-06-06 20:29:01 +0000273//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000274// isValueValidForType implementations
275
Chris Lattner3462ae32001-12-03 22:26:30 +0000276bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000277 switch (Ty->getPrimitiveID()) {
278 default:
279 return false; // These can't be represented as integers!!!
280
281 // Signed types...
282 case Type::SByteTyID:
283 return (Val <= INT8_MAX && Val >= INT8_MIN);
284 case Type::ShortTyID:
285 return (Val <= INT16_MAX && Val >= INT16_MIN);
286 case Type::IntTyID:
287 return (Val <= INT32_MAX && Val >= INT32_MIN);
288 case Type::LongTyID:
289 return true; // This is the largest type...
290 }
291 assert(0 && "WTF?");
292 return false;
293}
294
Chris Lattner3462ae32001-12-03 22:26:30 +0000295bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000296 switch (Ty->getPrimitiveID()) {
297 default:
298 return false; // These can't be represented as integers!!!
299
300 // Unsigned types...
301 case Type::UByteTyID:
302 return (Val <= UINT8_MAX);
303 case Type::UShortTyID:
304 return (Val <= UINT16_MAX);
305 case Type::UIntTyID:
306 return (Val <= UINT32_MAX);
307 case Type::ULongTyID:
308 return true; // This is the largest type...
309 }
310 assert(0 && "WTF?");
311 return false;
312}
313
Chris Lattner3462ae32001-12-03 22:26:30 +0000314bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000315 switch (Ty->getPrimitiveID()) {
316 default:
317 return false; // These can't be represented as floating point!
318
319 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000320 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000321 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000322 return (Val <= UINT8_MAX);
323 */
324 case Type::DoubleTyID:
325 return true; // This is the largest type...
326 }
327};
Chris Lattner9655e542001-07-20 19:16:02 +0000328
Chris Lattner49d855c2001-09-07 16:46:31 +0000329//===----------------------------------------------------------------------===//
330// Hash Function Implementations
331#if 0
Chris Lattner3462ae32001-12-03 22:26:30 +0000332unsigned ConstantSInt::hash(const Type *Ty, int64_t V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000333 return unsigned(Ty->getPrimitiveID() ^ V);
334}
335
Chris Lattner3462ae32001-12-03 22:26:30 +0000336unsigned ConstantUInt::hash(const Type *Ty, uint64_t V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000337 return unsigned(Ty->getPrimitiveID() ^ V);
338}
339
Chris Lattner3462ae32001-12-03 22:26:30 +0000340unsigned ConstantFP::hash(const Type *Ty, double V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000341 return Ty->getPrimitiveID() ^ unsigned(V);
342}
343
Chris Lattner3462ae32001-12-03 22:26:30 +0000344unsigned ConstantArray::hash(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000345 const std::vector<Constant*> &V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000346 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
347 for (unsigned i = 0; i < V.size(); ++i)
348 Result ^= V[i]->getHash() << (i & 7);
349 return Result;
350}
351
Chris Lattner3462ae32001-12-03 22:26:30 +0000352unsigned ConstantStruct::hash(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000353 const std::vector<Constant*> &V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000354 unsigned Result = (Ty->getUniqueID() << 5) ^ (Ty->getUniqueID() * 7);
355 for (unsigned i = 0; i < V.size(); ++i)
356 Result ^= V[i]->getHash() << (i & 7);
357 return Result;
358}
359#endif
360
361//===----------------------------------------------------------------------===//
362// Factory Function Implementation
363
Chris Lattner3462ae32001-12-03 22:26:30 +0000364template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000365struct ValueMap {
366 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000367 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000368
Chris Lattner3462ae32001-12-03 22:26:30 +0000369 inline ConstantClass *get(const Type *Ty, ValType V) {
370 map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000371 Map.find(ConstHashKey(Ty, V));
372 return (I != Map.end()) ? I->second : 0;
373 }
374
Chris Lattner3462ae32001-12-03 22:26:30 +0000375 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000376 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
377 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000378
Chris Lattner3462ae32001-12-03 22:26:30 +0000379 inline void remove(ConstantClass *CP) {
380 for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000381 E = Map.end(); I != E;++I)
382 if (I->second == CP) {
383 Map.erase(I);
384 return;
385 }
386 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000387};
388
Chris Lattner3462ae32001-12-03 22:26:30 +0000389//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000390//
Chris Lattner3462ae32001-12-03 22:26:30 +0000391static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000392
Chris Lattner3462ae32001-12-03 22:26:30 +0000393ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
394 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000395 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000396 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000397 return Result;
398}
399
Chris Lattner3462ae32001-12-03 22:26:30 +0000400ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
401 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000402 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000403 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000404 return Result;
405}
406
Chris Lattner3462ae32001-12-03 22:26:30 +0000407ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000408 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000409 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
410 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000411}
412
Chris Lattner3462ae32001-12-03 22:26:30 +0000413//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000414//
Chris Lattner3462ae32001-12-03 22:26:30 +0000415static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000416
Chris Lattner3462ae32001-12-03 22:26:30 +0000417ConstantFP *ConstantFP::get(const Type *Ty, double V) {
418 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000419 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000420 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000421 return Result;
422}
423
Chris Lattner3462ae32001-12-03 22:26:30 +0000424//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000425//
Chris Lattner7f74a562002-01-20 22:54:45 +0000426static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000427
Chris Lattner3462ae32001-12-03 22:26:30 +0000428ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000429 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000430 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000431 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000432 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000433 return Result;
434}
435
Chris Lattner3462ae32001-12-03 22:26:30 +0000436// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000437// contain the specified string. A null terminator is added to the specified
438// string so that it may be used in a natural way...
439//
Chris Lattner7f74a562002-01-20 22:54:45 +0000440ConstantArray *ConstantArray::get(const std::string &Str) {
441 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000442
443 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000444 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000445
446 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000447 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000448
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000449 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000450 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000451}
452
453
Chris Lattnerd7a73302001-10-13 06:57:33 +0000454// destroyConstant - Remove the constant from the constant table...
455//
Chris Lattner3462ae32001-12-03 22:26:30 +0000456void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000457 ArrayConstants.remove(this);
458 destroyConstantImpl();
459}
460
Chris Lattner3462ae32001-12-03 22:26:30 +0000461//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000462//
Chris Lattner7f74a562002-01-20 22:54:45 +0000463static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000464
Chris Lattner3462ae32001-12-03 22:26:30 +0000465ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000466 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000467 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000468 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000469 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000470 return Result;
471}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000472
Chris Lattnerd7a73302001-10-13 06:57:33 +0000473// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000474//
Chris Lattner3462ae32001-12-03 22:26:30 +0000475void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000476 StructConstants.remove(this);
477 destroyConstantImpl();
478}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000479
Chris Lattner3462ae32001-12-03 22:26:30 +0000480//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000481//
Chris Lattner3462ae32001-12-03 22:26:30 +0000482static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000483
Chris Lattner3462ae32001-12-03 22:26:30 +0000484ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
485 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000486 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000487 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000488 return Result;
489}
490
Chris Lattner3462ae32001-12-03 22:26:30 +0000491//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000492//
Chris Lattner3462ae32001-12-03 22:26:30 +0000493ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000494 assert(GV->getParent() && "Global Value must be attached to a module!");
495
496 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000497 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000498}
499
500
Chris Lattner3462ae32001-12-03 22:26:30 +0000501void ConstantPointerRef::mutateReference(GlobalValue *NewGV) {
502 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000503 Operands[0] = NewGV;
Chris Lattner25033252001-10-03 19:28:15 +0000504}