blob: de61c37d6e32b4cb0e888677d83b9fe8d14c58eb [file] [log] [blame]
Chris Lattnerca142372002-04-28 19:55:58 +00001//===-- Constants.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
Chris Lattnerca142372002-04-28 19:55:58 +00007#include "llvm/Constants.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00008#include "llvm/DerivedTypes.h"
Vikram S. Adve4e537b22002-07-14 23:13:17 +00009#include "llvm/iMemory.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000010#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000011#include "llvm/Module.h"
Chris Lattner6915f8f2002-04-07 22:49:37 +000012#include "llvm/SlotCalculator.h"
Chris Lattner5de22042001-11-27 00:03:19 +000013#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000015
Chris Lattner7f74a562002-01-20 22:54:45 +000016using std::map;
17using std::pair;
18using std::make_pair;
Anand Shukla991873f2002-07-16 00:02:17 +000019using std::vector;
Chris Lattner7f74a562002-01-20 22:54:45 +000020
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
Chris Lattner3462ae32001-12-03 22:26:30 +000036void Constant::destroyConstantImpl() {
37 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000038 // references to the constant by other constants in the constant pool. These
39 // constants are implicitly dependant on the module that is being deleted,
40 // but they don't know that. Because we only find out when the CPV is
41 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000042 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000043 //
44 while (!use_empty()) {
45 Value *V = use_back();
46#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000047 if (!isa<Constant>(V))
48 std::cerr << "While deleting: " << *this
49 << "\n\nUse still stuck around after Def is destroyed: "
50 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000051#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000052 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000053 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000054 CPV->destroyConstant();
55
56 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000057 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000058 }
59
60 // Value has no outstanding references it is safe to delete it now...
61 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000062}
Chris Lattner2f7c9632001-06-06 20:29:01 +000063
Chris Lattnerb1585a92002-08-13 17:50:20 +000064// Static constructor to create a '0' constant of arbitrary type...
65Constant *Constant::getNullValue(const Type *Ty) {
66 switch (Ty->getPrimitiveID()) {
67 case Type::BoolTyID: return ConstantBool::get(false);
68 case Type::SByteTyID:
69 case Type::ShortTyID:
70 case Type::IntTyID:
71 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
72
73 case Type::UByteTyID:
74 case Type::UShortTyID:
75 case Type::UIntTyID:
76 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
77
78 case Type::FloatTyID:
79 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
80
81 case Type::PointerTyID:
82 return ConstantPointerNull::get(cast<PointerType>(Ty));
83 default:
84 return 0;
85 }
86}
87
88// Static constructor to create the maximum constant of an integral type...
89ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
90 switch (Ty->getPrimitiveID()) {
91 case Type::BoolTyID: return ConstantBool::True;
92 case Type::SByteTyID:
93 case Type::ShortTyID:
94 case Type::IntTyID:
95 case Type::LongTyID: {
96 // Calculate 011111111111111...
97 unsigned TypeBits = Ty->getPrimitiveSize()*8;
98 int64_t Val = INT64_MAX; // All ones
99 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
100 return ConstantSInt::get(Ty, Val);
101 }
102
103 case Type::UByteTyID:
104 case Type::UShortTyID:
105 case Type::UIntTyID:
106 case Type::ULongTyID: return getAllOnesValue(Ty);
107
Chris Lattner31408f72002-08-14 17:12:13 +0000108 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000109 }
110}
111
112// Static constructor to create the minimum constant for an integral type...
113ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
114 switch (Ty->getPrimitiveID()) {
115 case Type::BoolTyID: return ConstantBool::False;
116 case Type::SByteTyID:
117 case Type::ShortTyID:
118 case Type::IntTyID:
119 case Type::LongTyID: {
120 // Calculate 1111111111000000000000
121 unsigned TypeBits = Ty->getPrimitiveSize()*8;
122 int64_t Val = -1; // All ones
123 Val <<= TypeBits-1; // Shift over to the right spot
124 return ConstantSInt::get(Ty, Val);
125 }
126
127 case Type::UByteTyID:
128 case Type::UShortTyID:
129 case Type::UIntTyID:
130 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
131
Chris Lattner31408f72002-08-14 17:12:13 +0000132 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000133 }
134}
135
136// Static constructor to create an integral constant with all bits set
137ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
138 switch (Ty->getPrimitiveID()) {
139 case Type::BoolTyID: return ConstantBool::True;
140 case Type::SByteTyID:
141 case Type::ShortTyID:
142 case Type::IntTyID:
143 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
144
145 case Type::UByteTyID:
146 case Type::UShortTyID:
147 case Type::UIntTyID:
148 case Type::ULongTyID: {
149 // Calculate ~0 of the right type...
150 unsigned TypeBits = Ty->getPrimitiveSize()*8;
151 uint64_t Val = ~0ULL; // All ones
152 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
153 return ConstantUInt::get(Ty, Val);
154 }
Chris Lattner31408f72002-08-14 17:12:13 +0000155 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000156 }
157}
158
159
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000161// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162//===----------------------------------------------------------------------===//
163
164//===----------------------------------------------------------------------===//
165// Normal Constructors
166
Chris Lattnerb1585a92002-08-13 17:50:20 +0000167ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168 Val = V;
169}
Chris Lattner49d855c2001-09-07 16:46:31 +0000170
Chris Lattnerb1585a92002-08-13 17:50:20 +0000171ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000172 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000173}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000174
Chris Lattner3462ae32001-12-03 22:26:30 +0000175ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000176 assert(Ty->isInteger() && Ty->isSigned() &&
177 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000178 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000179}
180
Chris Lattner3462ae32001-12-03 22:26:30 +0000181ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000182 assert(Ty->isInteger() && Ty->isUnsigned() &&
183 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000184 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185}
186
Chris Lattner3462ae32001-12-03 22:26:30 +0000187ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000188 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000189 Val = V;
190}
191
Chris Lattner3462ae32001-12-03 22:26:30 +0000192ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000193 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000194 Operands.reserve(V.size());
195 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000197 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198 }
199}
200
Chris Lattner3462ae32001-12-03 22:26:30 +0000201ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000202 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000204 assert(V.size() == ETypes.size() &&
205 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000206 Operands.reserve(V.size());
207 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000209 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000210 }
211}
212
Chris Lattner3462ae32001-12-03 22:26:30 +0000213ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
214 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000215 Operands.push_back(Use(GV, this));
216}
217
Chris Lattner3cd8c562002-07-30 18:54:25 +0000218ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
219 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000220 Operands.push_back(Use(C, this));
221}
222
Chris Lattner3cd8c562002-07-30 18:54:25 +0000223ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
224 : Constant(C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000225 Operands.push_back(Use(C1, this));
226 Operands.push_back(Use(C2, this));
227}
228
Chris Lattner3cd8c562002-07-30 18:54:25 +0000229ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
230 const Type *DestTy)
231 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000232 Operands.reserve(1+IdxList.size());
233 Operands.push_back(Use(C, this));
234 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
235 Operands.push_back(Use(IdxList[i], this));
236}
237
Chris Lattner60e0dd72001-10-03 06:12:09 +0000238
Chris Lattner2f7c9632001-06-06 20:29:01 +0000239
240//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000241// classof implementations
242
Chris Lattnerb1585a92002-08-13 17:50:20 +0000243bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000244 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000245}
246
Chris Lattner3462ae32001-12-03 22:26:30 +0000247bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000248 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000249}
Chris Lattner3462ae32001-12-03 22:26:30 +0000250bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000251 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000252}
Chris Lattner3462ae32001-12-03 22:26:30 +0000253bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000254 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000255}
Chris Lattner3462ae32001-12-03 22:26:30 +0000256bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000257 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000258 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000259 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000260}
Chris Lattner3462ae32001-12-03 22:26:30 +0000261bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000262 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000263}
Chris Lattner3462ae32001-12-03 22:26:30 +0000264bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000265 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000266}
Chris Lattner3462ae32001-12-03 22:26:30 +0000267bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000268 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000269}
270
271
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000272
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//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000330// Factory Function Implementation
331
Chris Lattner3462ae32001-12-03 22:26:30 +0000332template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000333struct ValueMap {
334 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000335 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000336
Chris Lattner3462ae32001-12-03 22:26:30 +0000337 inline ConstantClass *get(const Type *Ty, ValType V) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000338 typename map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000339 Map.find(ConstHashKey(Ty, V));
340 return (I != Map.end()) ? I->second : 0;
341 }
342
Chris Lattner3462ae32001-12-03 22:26:30 +0000343 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000344 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
345 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000346
Chris Lattner3462ae32001-12-03 22:26:30 +0000347 inline void remove(ConstantClass *CP) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000348 for (typename map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000349 E = Map.end(); I != E;++I)
350 if (I->second == CP) {
351 Map.erase(I);
352 return;
353 }
354 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000355};
356
Chris Lattner3462ae32001-12-03 22:26:30 +0000357//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000358//
Chris Lattner3462ae32001-12-03 22:26:30 +0000359static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000360
Chris Lattner3462ae32001-12-03 22:26:30 +0000361ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
362 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000363 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000364 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000365 return Result;
366}
367
Chris Lattner3462ae32001-12-03 22:26:30 +0000368ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
369 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000370 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000371 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000372 return Result;
373}
374
Chris Lattner3462ae32001-12-03 22:26:30 +0000375ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000376 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000377 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
378 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000379}
380
Chris Lattner3462ae32001-12-03 22:26:30 +0000381//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000382//
Chris Lattner3462ae32001-12-03 22:26:30 +0000383static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000384
Chris Lattner3462ae32001-12-03 22:26:30 +0000385ConstantFP *ConstantFP::get(const Type *Ty, double V) {
386 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000387 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000388 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000389 return Result;
390}
391
Chris Lattner3462ae32001-12-03 22:26:30 +0000392//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000393//
Chris Lattner7f74a562002-01-20 22:54:45 +0000394static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000395
Chris Lattner3462ae32001-12-03 22:26:30 +0000396ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000397 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000398 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000399 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000400 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000401 return Result;
402}
403
Chris Lattner3462ae32001-12-03 22:26:30 +0000404// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000405// contain the specified string. A null terminator is added to the specified
406// string so that it may be used in a natural way...
407//
Chris Lattner7f74a562002-01-20 22:54:45 +0000408ConstantArray *ConstantArray::get(const std::string &Str) {
409 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000410
411 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000412 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000413
414 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000415 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000416
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000417 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000418 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000419}
420
421
Chris Lattnerd7a73302001-10-13 06:57:33 +0000422// destroyConstant - Remove the constant from the constant table...
423//
Chris Lattner3462ae32001-12-03 22:26:30 +0000424void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000425 ArrayConstants.remove(this);
426 destroyConstantImpl();
427}
428
Chris Lattner81fabb02002-08-26 17:53:56 +0000429// getAsString - If the sub-element type of this array is either sbyte or ubyte,
430// then this method converts the array to an std::string and returns it.
431// Otherwise, it asserts out.
432//
433std::string ConstantArray::getAsString() const {
434 std::string Result;
435 if (getType()->getElementType() == Type::SByteTy)
436 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
437 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
438 else {
439 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
440 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
441 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
442 }
443 return Result;
444}
445
446
Chris Lattner3462ae32001-12-03 22:26:30 +0000447//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000448//
Chris Lattner7f74a562002-01-20 22:54:45 +0000449static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000450
Chris Lattner3462ae32001-12-03 22:26:30 +0000451ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000452 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000453 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000454 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000455 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000456 return Result;
457}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000458
Chris Lattnerd7a73302001-10-13 06:57:33 +0000459// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000460//
Chris Lattner3462ae32001-12-03 22:26:30 +0000461void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000462 StructConstants.remove(this);
463 destroyConstantImpl();
464}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000465
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000466
Chris Lattner3462ae32001-12-03 22:26:30 +0000467//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000468//
Chris Lattner3462ae32001-12-03 22:26:30 +0000469static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000470
Chris Lattner3462ae32001-12-03 22:26:30 +0000471ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
472 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000473 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000474 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000475 return Result;
476}
477
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000478// destroyConstant - Remove the constant from the constant table...
479//
480void ConstantPointerNull::destroyConstant() {
481 NullPtrConstants.remove(this);
482 destroyConstantImpl();
483}
484
485
Chris Lattner3462ae32001-12-03 22:26:30 +0000486//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000487//
Chris Lattner3462ae32001-12-03 22:26:30 +0000488ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000489 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000490
Chris Lattnerd7a73302001-10-13 06:57:33 +0000491 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000492 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000493}
494
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000495// destroyConstant - Remove the constant from the constant table...
496//
497void ConstantPointerRef::destroyConstant() {
498 getValue()->getParent()->destroyConstantPointerRef(this);
499 destroyConstantImpl();
500}
501
502
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000503//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000504//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000505typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
506static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
507
Chris Lattner330b7ac2002-08-14 18:24:09 +0000508ConstantExpr *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000509
510 // Look up the constant in the table first to ensure uniqueness
511 vector<Constant*> argVec(1, C);
Chris Lattner330b7ac2002-08-14 18:24:09 +0000512 const ExprMapKeyType &Key = make_pair(Instruction::Cast, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000513 ConstantExpr *Result = ExprConstants.get(Ty, Key);
514 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000515
516 // Its not in the table so create a new one and put it in the table.
Chris Lattner330b7ac2002-08-14 18:24:09 +0000517 Result = new ConstantExpr(Instruction::Cast, C, Ty);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000518 ExprConstants.add(Ty, Key, Result);
519 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000520}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000521
Chris Lattner3cd8c562002-07-30 18:54:25 +0000522ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000523 // Look up the constant in the table first to ensure uniqueness
524 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000525 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
Chris Lattner3cd8c562002-07-30 18:54:25 +0000526 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000527 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000528
529 // Its not in the table so create a new one and put it in the table.
530 // Check the operands for consistency first
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000531 assert((Opcode >= Instruction::FirstBinaryOp &&
532 Opcode < Instruction::NumBinaryOps) &&
Chris Lattner3cd8c562002-07-30 18:54:25 +0000533 "Invalid opcode in binary constant expression");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000534
Chris Lattner3cd8c562002-07-30 18:54:25 +0000535 assert(C1->getType() == C2->getType() &&
536 "Operand types in binary constant expression should match");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000537
Chris Lattner3cd8c562002-07-30 18:54:25 +0000538 Result = new ConstantExpr(Opcode, C1, C2);
539 ExprConstants.add(C1->getType(), Key, Result);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000540 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000541}
542
Chris Lattner3cd8c562002-07-30 18:54:25 +0000543ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
544 const std::vector<Constant*> &IdxList) {
545 const Type *Ty = C->getType();
Vikram S. Adve4c485332002-07-15 18:19:33 +0000546
547 // Look up the constant in the table first to ensure uniqueness
548 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000549 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000550
Chris Lattner3cd8c562002-07-30 18:54:25 +0000551 const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000552 ConstantExpr *Result = ExprConstants.get(Ty, Key);
553 if (Result) return Result;
Chris Lattner3cd8c562002-07-30 18:54:25 +0000554
Vikram S. Adve4c485332002-07-15 18:19:33 +0000555 // Its not in the table so create a new one and put it in the table.
556 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000557 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000558 assert(isa<PointerType>(Ty) &&
559 "Non-pointer type for constant GelElementPtr expression");
560
Chris Lattner3cd8c562002-07-30 18:54:25 +0000561 // Check that the indices list is valid...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000562 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
Chris Lattner3cd8c562002-07-30 18:54:25 +0000563 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
564 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000565
Chris Lattner3cd8c562002-07-30 18:54:25 +0000566 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000567 ExprConstants.add(Ty, Key, Result);
568 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000569}
570
571// destroyConstant - Remove the constant from the constant table...
572//
573void ConstantExpr::destroyConstant() {
574 ExprConstants.remove(this);
575 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000576}
577
Chris Lattner3cd8c562002-07-30 18:54:25 +0000578const char *ConstantExpr::getOpcodeName() const {
579 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000580}
581
582
583//---- ConstantPointerRef::mutateReferences() implementation...
584//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000585unsigned ConstantPointerRef::mutateReferences(Value *OldV, Value *NewV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000586 assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000587 GlobalValue *NewGV = cast<GlobalValue>(NewV);
Chris Lattner3462ae32001-12-03 22:26:30 +0000588 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000589 Operands[0] = NewGV;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000590 return 1;
591}
592
593
594//---- ConstantPointerExpr::mutateReferences() implementation...
595//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000596unsigned ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
597 unsigned NumReplaced = 0;
598 Constant *NewC = cast<Constant>(NewV);
599 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000600 if (Operands[i] == OldV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000601 ++NumReplaced;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000602 Operands[i] = NewC;
603 }
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000604 return NumReplaced;
Chris Lattner25033252001-10-03 19:28:15 +0000605}