blob: fe6af758bd713fc174a3a94e911766945d76a333 [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
7#define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends...
Chris Lattnerca142372002-04-28 19:55:58 +00008#include "llvm/Constants.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00009#include "llvm/DerivedTypes.h"
Vikram S. Adve4e537b22002-07-14 23:13:17 +000010#include "llvm/iMemory.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000011#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000012#include "llvm/Module.h"
Chris Lattner6915f8f2002-04-07 22:49:37 +000013#include "llvm/SlotCalculator.h"
Chris Lattner5de22042001-11-27 00:03:19 +000014#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000015#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000016
Chris Lattner7f74a562002-01-20 22:54:45 +000017using std::map;
18using std::pair;
19using std::make_pair;
Anand Shukla991873f2002-07-16 00:02:17 +000020using std::vector;
Chris Lattner7f74a562002-01-20 22:54:45 +000021
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
Chris Lattner3462ae32001-12-03 22:26:30 +000037void Constant::destroyConstantImpl() {
38 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000039 // references to the constant by other constants in the constant pool. These
40 // constants are implicitly dependant on the module that is being deleted,
41 // but they don't know that. Because we only find out when the CPV is
42 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000043 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000044 //
45 while (!use_empty()) {
46 Value *V = use_back();
47#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000048 if (!isa<Constant>(V))
49 std::cerr << "While deleting: " << *this
50 << "\n\nUse still stuck around after Def is destroyed: "
51 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000052#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000053 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000054 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000055 CPV->destroyConstant();
56
57 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000058 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000059 }
60
61 // Value has no outstanding references it is safe to delete it now...
62 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000063}
Chris Lattner2f7c9632001-06-06 20:29:01 +000064
Chris Lattnerb1585a92002-08-13 17:50:20 +000065// Static constructor to create a '0' constant of arbitrary type...
66Constant *Constant::getNullValue(const Type *Ty) {
67 switch (Ty->getPrimitiveID()) {
68 case Type::BoolTyID: return ConstantBool::get(false);
69 case Type::SByteTyID:
70 case Type::ShortTyID:
71 case Type::IntTyID:
72 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
73
74 case Type::UByteTyID:
75 case Type::UShortTyID:
76 case Type::UIntTyID:
77 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
78
79 case Type::FloatTyID:
80 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
81
82 case Type::PointerTyID:
83 return ConstantPointerNull::get(cast<PointerType>(Ty));
84 default:
85 return 0;
86 }
87}
88
89// Static constructor to create the maximum constant of an integral type...
90ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
91 switch (Ty->getPrimitiveID()) {
92 case Type::BoolTyID: return ConstantBool::True;
93 case Type::SByteTyID:
94 case Type::ShortTyID:
95 case Type::IntTyID:
96 case Type::LongTyID: {
97 // Calculate 011111111111111...
98 unsigned TypeBits = Ty->getPrimitiveSize()*8;
99 int64_t Val = INT64_MAX; // All ones
100 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
101 return ConstantSInt::get(Ty, Val);
102 }
103
104 case Type::UByteTyID:
105 case Type::UShortTyID:
106 case Type::UIntTyID:
107 case Type::ULongTyID: return getAllOnesValue(Ty);
108
Chris Lattner31408f72002-08-14 17:12:13 +0000109 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000110 }
111}
112
113// Static constructor to create the minimum constant for an integral type...
114ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
115 switch (Ty->getPrimitiveID()) {
116 case Type::BoolTyID: return ConstantBool::False;
117 case Type::SByteTyID:
118 case Type::ShortTyID:
119 case Type::IntTyID:
120 case Type::LongTyID: {
121 // Calculate 1111111111000000000000
122 unsigned TypeBits = Ty->getPrimitiveSize()*8;
123 int64_t Val = -1; // All ones
124 Val <<= TypeBits-1; // Shift over to the right spot
125 return ConstantSInt::get(Ty, Val);
126 }
127
128 case Type::UByteTyID:
129 case Type::UShortTyID:
130 case Type::UIntTyID:
131 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
132
Chris Lattner31408f72002-08-14 17:12:13 +0000133 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000134 }
135}
136
137// Static constructor to create an integral constant with all bits set
138ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
139 switch (Ty->getPrimitiveID()) {
140 case Type::BoolTyID: return ConstantBool::True;
141 case Type::SByteTyID:
142 case Type::ShortTyID:
143 case Type::IntTyID:
144 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
145
146 case Type::UByteTyID:
147 case Type::UShortTyID:
148 case Type::UIntTyID:
149 case Type::ULongTyID: {
150 // Calculate ~0 of the right type...
151 unsigned TypeBits = Ty->getPrimitiveSize()*8;
152 uint64_t Val = ~0ULL; // All ones
153 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
154 return ConstantUInt::get(Ty, Val);
155 }
Chris Lattner31408f72002-08-14 17:12:13 +0000156 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000157 }
158}
159
160
Chris Lattner2f7c9632001-06-06 20:29:01 +0000161//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000162// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000163//===----------------------------------------------------------------------===//
164
165//===----------------------------------------------------------------------===//
166// Normal Constructors
167
Chris Lattnerb1585a92002-08-13 17:50:20 +0000168ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000169 Val = V;
170}
Chris Lattner49d855c2001-09-07 16:46:31 +0000171
Chris Lattnerb1585a92002-08-13 17:50:20 +0000172ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000173 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000174}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000175
Chris Lattner3462ae32001-12-03 22:26:30 +0000176ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000177 assert(Ty->isInteger() && Ty->isSigned() &&
178 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000179 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180}
181
Chris Lattner3462ae32001-12-03 22:26:30 +0000182ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000183 assert(Ty->isInteger() && Ty->isUnsigned() &&
184 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000185 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000186}
187
Chris Lattner3462ae32001-12-03 22:26:30 +0000188ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000189 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000190 Val = V;
191}
192
Chris Lattner3462ae32001-12-03 22:26:30 +0000193ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000194 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000195 for (unsigned i = 0; i < V.size(); i++) {
196 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 Lattner2f7c9632001-06-06 20:29:01 +0000206 for (unsigned i = 0; i < V.size(); i++) {
207 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000208 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209 }
210}
211
Chris Lattner3462ae32001-12-03 22:26:30 +0000212ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
213 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000214 Operands.push_back(Use(GV, this));
215}
216
Chris Lattner3cd8c562002-07-30 18:54:25 +0000217ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
218 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000219 Operands.push_back(Use(C, this));
220}
221
Chris Lattner3cd8c562002-07-30 18:54:25 +0000222ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
223 : Constant(C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000224 Operands.push_back(Use(C1, this));
225 Operands.push_back(Use(C2, this));
226}
227
Chris Lattner3cd8c562002-07-30 18:54:25 +0000228ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
229 const Type *DestTy)
230 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000231 Operands.reserve(1+IdxList.size());
232 Operands.push_back(Use(C, this));
233 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
234 Operands.push_back(Use(IdxList[i], this));
235}
236
Chris Lattner60e0dd72001-10-03 06:12:09 +0000237
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238
239//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000240// classof implementations
241
Chris Lattnerb1585a92002-08-13 17:50:20 +0000242bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000243 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000244}
245
Chris Lattner3462ae32001-12-03 22:26:30 +0000246bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000247 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000248}
Chris Lattner3462ae32001-12-03 22:26:30 +0000249bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000250 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000251}
Chris Lattner3462ae32001-12-03 22:26:30 +0000252bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000253 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000254}
Chris Lattner3462ae32001-12-03 22:26:30 +0000255bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000256 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000257 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000258 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000259}
Chris Lattner3462ae32001-12-03 22:26:30 +0000260bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000261 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000262}
Chris Lattner3462ae32001-12-03 22:26:30 +0000263bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000264 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000265}
Chris Lattner3462ae32001-12-03 22:26:30 +0000266bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000267 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000268}
269
270
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000271
Chris Lattner2f7c9632001-06-06 20:29:01 +0000272//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000273// isValueValidForType implementations
274
Chris Lattner3462ae32001-12-03 22:26:30 +0000275bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000276 switch (Ty->getPrimitiveID()) {
277 default:
278 return false; // These can't be represented as integers!!!
279
280 // Signed types...
281 case Type::SByteTyID:
282 return (Val <= INT8_MAX && Val >= INT8_MIN);
283 case Type::ShortTyID:
284 return (Val <= INT16_MAX && Val >= INT16_MIN);
285 case Type::IntTyID:
286 return (Val <= INT32_MAX && Val >= INT32_MIN);
287 case Type::LongTyID:
288 return true; // This is the largest type...
289 }
290 assert(0 && "WTF?");
291 return false;
292}
293
Chris Lattner3462ae32001-12-03 22:26:30 +0000294bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000295 switch (Ty->getPrimitiveID()) {
296 default:
297 return false; // These can't be represented as integers!!!
298
299 // Unsigned types...
300 case Type::UByteTyID:
301 return (Val <= UINT8_MAX);
302 case Type::UShortTyID:
303 return (Val <= UINT16_MAX);
304 case Type::UIntTyID:
305 return (Val <= UINT32_MAX);
306 case Type::ULongTyID:
307 return true; // This is the largest type...
308 }
309 assert(0 && "WTF?");
310 return false;
311}
312
Chris Lattner3462ae32001-12-03 22:26:30 +0000313bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000314 switch (Ty->getPrimitiveID()) {
315 default:
316 return false; // These can't be represented as floating point!
317
318 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000319 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000320 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000321 return (Val <= UINT8_MAX);
322 */
323 case Type::DoubleTyID:
324 return true; // This is the largest type...
325 }
326};
Chris Lattner9655e542001-07-20 19:16:02 +0000327
Chris Lattner49d855c2001-09-07 16:46:31 +0000328//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000329// Factory Function Implementation
330
Chris Lattner3462ae32001-12-03 22:26:30 +0000331template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000332struct ValueMap {
333 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000334 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000335
Chris Lattner3462ae32001-12-03 22:26:30 +0000336 inline ConstantClass *get(const Type *Ty, ValType V) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000337 typename map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000338 Map.find(ConstHashKey(Ty, V));
339 return (I != Map.end()) ? I->second : 0;
340 }
341
Chris Lattner3462ae32001-12-03 22:26:30 +0000342 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000343 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
344 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000345
Chris Lattner3462ae32001-12-03 22:26:30 +0000346 inline void remove(ConstantClass *CP) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000347 for (typename map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000348 E = Map.end(); I != E;++I)
349 if (I->second == CP) {
350 Map.erase(I);
351 return;
352 }
353 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000354};
355
Chris Lattner3462ae32001-12-03 22:26:30 +0000356//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000357//
Chris Lattner3462ae32001-12-03 22:26:30 +0000358static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000359
Chris Lattner3462ae32001-12-03 22:26:30 +0000360ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
361 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000362 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000363 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000364 return Result;
365}
366
Chris Lattner3462ae32001-12-03 22:26:30 +0000367ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
368 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000369 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000370 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000371 return Result;
372}
373
Chris Lattner3462ae32001-12-03 22:26:30 +0000374ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000375 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000376 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
377 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000378}
379
Chris Lattner3462ae32001-12-03 22:26:30 +0000380//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000381//
Chris Lattner3462ae32001-12-03 22:26:30 +0000382static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000383
Chris Lattner3462ae32001-12-03 22:26:30 +0000384ConstantFP *ConstantFP::get(const Type *Ty, double V) {
385 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000386 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000387 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000388 return Result;
389}
390
Chris Lattner3462ae32001-12-03 22:26:30 +0000391//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000392//
Chris Lattner7f74a562002-01-20 22:54:45 +0000393static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000394
Chris Lattner3462ae32001-12-03 22:26:30 +0000395ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000396 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000397 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000398 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000399 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000400 return Result;
401}
402
Chris Lattner3462ae32001-12-03 22:26:30 +0000403// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000404// contain the specified string. A null terminator is added to the specified
405// string so that it may be used in a natural way...
406//
Chris Lattner7f74a562002-01-20 22:54:45 +0000407ConstantArray *ConstantArray::get(const std::string &Str) {
408 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000409
410 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000411 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000412
413 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000414 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000415
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000416 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000417 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000418}
419
420
Chris Lattnerd7a73302001-10-13 06:57:33 +0000421// destroyConstant - Remove the constant from the constant table...
422//
Chris Lattner3462ae32001-12-03 22:26:30 +0000423void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000424 ArrayConstants.remove(this);
425 destroyConstantImpl();
426}
427
Chris Lattner81fabb02002-08-26 17:53:56 +0000428// getAsString - If the sub-element type of this array is either sbyte or ubyte,
429// then this method converts the array to an std::string and returns it.
430// Otherwise, it asserts out.
431//
432std::string ConstantArray::getAsString() const {
433 std::string Result;
434 if (getType()->getElementType() == Type::SByteTy)
435 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
436 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
437 else {
438 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
439 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
440 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
441 }
442 return Result;
443}
444
445
Chris Lattner3462ae32001-12-03 22:26:30 +0000446//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000447//
Chris Lattner7f74a562002-01-20 22:54:45 +0000448static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000449
Chris Lattner3462ae32001-12-03 22:26:30 +0000450ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000451 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000452 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000453 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000454 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000455 return Result;
456}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000457
Chris Lattnerd7a73302001-10-13 06:57:33 +0000458// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000459//
Chris Lattner3462ae32001-12-03 22:26:30 +0000460void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000461 StructConstants.remove(this);
462 destroyConstantImpl();
463}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000464
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000465
Chris Lattner3462ae32001-12-03 22:26:30 +0000466//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000467//
Chris Lattner3462ae32001-12-03 22:26:30 +0000468static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000469
Chris Lattner3462ae32001-12-03 22:26:30 +0000470ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
471 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000472 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000473 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000474 return Result;
475}
476
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000477// destroyConstant - Remove the constant from the constant table...
478//
479void ConstantPointerNull::destroyConstant() {
480 NullPtrConstants.remove(this);
481 destroyConstantImpl();
482}
483
484
Chris Lattner3462ae32001-12-03 22:26:30 +0000485//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000486//
Chris Lattner3462ae32001-12-03 22:26:30 +0000487ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000488 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000489
Chris Lattnerd7a73302001-10-13 06:57:33 +0000490 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000491 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000492}
493
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000494// destroyConstant - Remove the constant from the constant table...
495//
496void ConstantPointerRef::destroyConstant() {
497 getValue()->getParent()->destroyConstantPointerRef(this);
498 destroyConstantImpl();
499}
500
501
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000502//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000503//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000504typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
505static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
506
Chris Lattner330b7ac2002-08-14 18:24:09 +0000507ConstantExpr *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000508
509 // Look up the constant in the table first to ensure uniqueness
510 vector<Constant*> argVec(1, C);
Chris Lattner330b7ac2002-08-14 18:24:09 +0000511 const ExprMapKeyType &Key = make_pair(Instruction::Cast, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000512 ConstantExpr *Result = ExprConstants.get(Ty, Key);
513 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000514
515 // Its not in the table so create a new one and put it in the table.
Chris Lattner330b7ac2002-08-14 18:24:09 +0000516 Result = new ConstantExpr(Instruction::Cast, C, Ty);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000517 ExprConstants.add(Ty, Key, Result);
518 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000519}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000520
Chris Lattner3cd8c562002-07-30 18:54:25 +0000521ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000522 // Look up the constant in the table first to ensure uniqueness
523 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000524 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
Chris Lattner3cd8c562002-07-30 18:54:25 +0000525 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000526 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000527
528 // Its not in the table so create a new one and put it in the table.
529 // Check the operands for consistency first
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000530 assert((Opcode >= Instruction::FirstBinaryOp &&
531 Opcode < Instruction::NumBinaryOps) &&
Chris Lattner3cd8c562002-07-30 18:54:25 +0000532 "Invalid opcode in binary constant expression");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000533
Chris Lattner3cd8c562002-07-30 18:54:25 +0000534 assert(C1->getType() == C2->getType() &&
535 "Operand types in binary constant expression should match");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000536
Chris Lattner3cd8c562002-07-30 18:54:25 +0000537 Result = new ConstantExpr(Opcode, C1, C2);
538 ExprConstants.add(C1->getType(), Key, Result);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000539 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000540}
541
Chris Lattner3cd8c562002-07-30 18:54:25 +0000542ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
543 const std::vector<Constant*> &IdxList) {
544 const Type *Ty = C->getType();
Vikram S. Adve4c485332002-07-15 18:19:33 +0000545
546 // Look up the constant in the table first to ensure uniqueness
547 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000548 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000549
Chris Lattner3cd8c562002-07-30 18:54:25 +0000550 const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000551 ConstantExpr *Result = ExprConstants.get(Ty, Key);
552 if (Result) return Result;
Chris Lattner3cd8c562002-07-30 18:54:25 +0000553
Vikram S. Adve4c485332002-07-15 18:19:33 +0000554 // Its not in the table so create a new one and put it in the table.
555 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000556 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000557 assert(isa<PointerType>(Ty) &&
558 "Non-pointer type for constant GelElementPtr expression");
559
Chris Lattner3cd8c562002-07-30 18:54:25 +0000560 // Check that the indices list is valid...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000561 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
Chris Lattner3cd8c562002-07-30 18:54:25 +0000562 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
563 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000564
Chris Lattner3cd8c562002-07-30 18:54:25 +0000565 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000566 ExprConstants.add(Ty, Key, Result);
567 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000568}
569
570// destroyConstant - Remove the constant from the constant table...
571//
572void ConstantExpr::destroyConstant() {
573 ExprConstants.remove(this);
574 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000575}
576
Chris Lattner3cd8c562002-07-30 18:54:25 +0000577const char *ConstantExpr::getOpcodeName() const {
578 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000579}
580
581
582//---- ConstantPointerRef::mutateReferences() implementation...
583//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000584unsigned ConstantPointerRef::mutateReferences(Value *OldV, Value *NewV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000585 assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000586 GlobalValue *NewGV = cast<GlobalValue>(NewV);
Chris Lattner3462ae32001-12-03 22:26:30 +0000587 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000588 Operands[0] = NewGV;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000589 return 1;
590}
591
592
593//---- ConstantPointerExpr::mutateReferences() implementation...
594//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000595unsigned ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
596 unsigned NumReplaced = 0;
597 Constant *NewC = cast<Constant>(NewV);
598 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000599 if (Operands[i] == OldV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000600 ++NumReplaced;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000601 Operands[i] = NewC;
602 }
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000603 return NumReplaced;
Chris Lattner25033252001-10-03 19:28:15 +0000604}