blob: 40c714ec82df2abcc5f539b307c33524563bcf74 [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 Lattner9655e542001-07-20 19:16:02 +0000177 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000178}
179
Chris Lattner3462ae32001-12-03 22:26:30 +0000180ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000181 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000182}
183
Chris Lattner3462ae32001-12-03 22:26:30 +0000184ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
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 Val = V;
187}
188
Chris Lattner3462ae32001-12-03 22:26:30 +0000189ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000190 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000191 for (unsigned i = 0; i < V.size(); i++) {
192 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000193 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194 }
195}
196
Chris Lattner3462ae32001-12-03 22:26:30 +0000197ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000198 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000199 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000200 assert(V.size() == ETypes.size() &&
201 "Invalid initializer vector for constant structure");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000202 for (unsigned i = 0; i < V.size(); i++) {
203 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000204 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000205 }
206}
207
Chris Lattner3462ae32001-12-03 22:26:30 +0000208ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
209 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000210 Operands.push_back(Use(GV, this));
211}
212
Chris Lattner3cd8c562002-07-30 18:54:25 +0000213ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
214 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000215 Operands.push_back(Use(C, this));
216}
217
Chris Lattner3cd8c562002-07-30 18:54:25 +0000218ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
219 : Constant(C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000220 Operands.push_back(Use(C1, this));
221 Operands.push_back(Use(C2, this));
222}
223
Chris Lattner3cd8c562002-07-30 18:54:25 +0000224ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
225 const Type *DestTy)
226 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000227 Operands.reserve(1+IdxList.size());
228 Operands.push_back(Use(C, this));
229 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
230 Operands.push_back(Use(IdxList[i], this));
231}
232
Chris Lattner60e0dd72001-10-03 06:12:09 +0000233
Chris Lattner2f7c9632001-06-06 20:29:01 +0000234
235//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000236// classof implementations
237
Chris Lattnerb1585a92002-08-13 17:50:20 +0000238bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattner41e99a02002-08-12 21:21:21 +0000239 return (CPV->getType()->isIntegral() || CPV->getType() == Type::BoolTy) &&
240 !isa<ConstantExpr>(CPV);
241}
242
Chris Lattner3462ae32001-12-03 22:26:30 +0000243bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000244 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000245}
Chris Lattner3462ae32001-12-03 22:26:30 +0000246bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000247 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000248}
Chris Lattner3462ae32001-12-03 22:26:30 +0000249bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000250 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000251}
Chris Lattner3462ae32001-12-03 22:26:30 +0000252bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000253 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000254 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000255 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000256}
Chris Lattner3462ae32001-12-03 22:26:30 +0000257bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000258 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000259}
Chris Lattner3462ae32001-12-03 22:26:30 +0000260bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000261 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000262}
Chris Lattner3462ae32001-12-03 22:26:30 +0000263bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000264 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000265}
266
267
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000268
Chris Lattner2f7c9632001-06-06 20:29:01 +0000269//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000270// isValueValidForType implementations
271
Chris Lattner3462ae32001-12-03 22:26:30 +0000272bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000273 switch (Ty->getPrimitiveID()) {
274 default:
275 return false; // These can't be represented as integers!!!
276
277 // Signed types...
278 case Type::SByteTyID:
279 return (Val <= INT8_MAX && Val >= INT8_MIN);
280 case Type::ShortTyID:
281 return (Val <= INT16_MAX && Val >= INT16_MIN);
282 case Type::IntTyID:
283 return (Val <= INT32_MAX && Val >= INT32_MIN);
284 case Type::LongTyID:
285 return true; // This is the largest type...
286 }
287 assert(0 && "WTF?");
288 return false;
289}
290
Chris Lattner3462ae32001-12-03 22:26:30 +0000291bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000292 switch (Ty->getPrimitiveID()) {
293 default:
294 return false; // These can't be represented as integers!!!
295
296 // Unsigned types...
297 case Type::UByteTyID:
298 return (Val <= UINT8_MAX);
299 case Type::UShortTyID:
300 return (Val <= UINT16_MAX);
301 case Type::UIntTyID:
302 return (Val <= UINT32_MAX);
303 case Type::ULongTyID:
304 return true; // This is the largest type...
305 }
306 assert(0 && "WTF?");
307 return false;
308}
309
Chris Lattner3462ae32001-12-03 22:26:30 +0000310bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000311 switch (Ty->getPrimitiveID()) {
312 default:
313 return false; // These can't be represented as floating point!
314
315 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000316 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000317 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000318 return (Val <= UINT8_MAX);
319 */
320 case Type::DoubleTyID:
321 return true; // This is the largest type...
322 }
323};
Chris Lattner9655e542001-07-20 19:16:02 +0000324
Chris Lattner49d855c2001-09-07 16:46:31 +0000325//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000326// Factory Function Implementation
327
Chris Lattner3462ae32001-12-03 22:26:30 +0000328template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000329struct ValueMap {
330 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000331 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000332
Chris Lattner3462ae32001-12-03 22:26:30 +0000333 inline ConstantClass *get(const Type *Ty, ValType V) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000334 typename map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000335 Map.find(ConstHashKey(Ty, V));
336 return (I != Map.end()) ? I->second : 0;
337 }
338
Chris Lattner3462ae32001-12-03 22:26:30 +0000339 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000340 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
341 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000342
Chris Lattner3462ae32001-12-03 22:26:30 +0000343 inline void remove(ConstantClass *CP) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000344 for (typename map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000345 E = Map.end(); I != E;++I)
346 if (I->second == CP) {
347 Map.erase(I);
348 return;
349 }
350 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000351};
352
Chris Lattner3462ae32001-12-03 22:26:30 +0000353//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000354//
Chris Lattner3462ae32001-12-03 22:26:30 +0000355static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000356
Chris Lattner3462ae32001-12-03 22:26:30 +0000357ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
358 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000359 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000360 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000361 return Result;
362}
363
Chris Lattner3462ae32001-12-03 22:26:30 +0000364ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
365 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000366 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000367 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000368 return Result;
369}
370
Chris Lattner3462ae32001-12-03 22:26:30 +0000371ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000372 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000373 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
374 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000375}
376
Chris Lattner3462ae32001-12-03 22:26:30 +0000377//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000378//
Chris Lattner3462ae32001-12-03 22:26:30 +0000379static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000380
Chris Lattner3462ae32001-12-03 22:26:30 +0000381ConstantFP *ConstantFP::get(const Type *Ty, double V) {
382 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000383 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000384 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000385 return Result;
386}
387
Chris Lattner3462ae32001-12-03 22:26:30 +0000388//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000389//
Chris Lattner7f74a562002-01-20 22:54:45 +0000390static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000391
Chris Lattner3462ae32001-12-03 22:26:30 +0000392ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000393 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000394 ConstantArray *Result = ArrayConstants.get(Ty, 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 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000397 return Result;
398}
399
Chris Lattner3462ae32001-12-03 22:26:30 +0000400// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000401// contain the specified string. A null terminator is added to the specified
402// string so that it may be used in a natural way...
403//
Chris Lattner7f74a562002-01-20 22:54:45 +0000404ConstantArray *ConstantArray::get(const std::string &Str) {
405 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000406
407 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000408 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000409
410 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000411 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000412
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000413 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000414 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000415}
416
417
Chris Lattnerd7a73302001-10-13 06:57:33 +0000418// destroyConstant - Remove the constant from the constant table...
419//
Chris Lattner3462ae32001-12-03 22:26:30 +0000420void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000421 ArrayConstants.remove(this);
422 destroyConstantImpl();
423}
424
Chris Lattner81fabb02002-08-26 17:53:56 +0000425// getAsString - If the sub-element type of this array is either sbyte or ubyte,
426// then this method converts the array to an std::string and returns it.
427// Otherwise, it asserts out.
428//
429std::string ConstantArray::getAsString() const {
430 std::string Result;
431 if (getType()->getElementType() == Type::SByteTy)
432 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
433 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
434 else {
435 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
436 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
437 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
438 }
439 return Result;
440}
441
442
Chris Lattner3462ae32001-12-03 22:26:30 +0000443//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000444//
Chris Lattner7f74a562002-01-20 22:54:45 +0000445static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000446
Chris Lattner3462ae32001-12-03 22:26:30 +0000447ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000448 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000449 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000450 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000451 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000452 return Result;
453}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000454
Chris Lattnerd7a73302001-10-13 06:57:33 +0000455// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000456//
Chris Lattner3462ae32001-12-03 22:26:30 +0000457void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000458 StructConstants.remove(this);
459 destroyConstantImpl();
460}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000461
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000462
Chris Lattner3462ae32001-12-03 22:26:30 +0000463//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000464//
Chris Lattner3462ae32001-12-03 22:26:30 +0000465static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000466
Chris Lattner3462ae32001-12-03 22:26:30 +0000467ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
468 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000469 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000470 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000471 return Result;
472}
473
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000474// destroyConstant - Remove the constant from the constant table...
475//
476void ConstantPointerNull::destroyConstant() {
477 NullPtrConstants.remove(this);
478 destroyConstantImpl();
479}
480
481
Chris Lattner3462ae32001-12-03 22:26:30 +0000482//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000483//
Chris Lattner3462ae32001-12-03 22:26:30 +0000484ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000485 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000486
Chris Lattnerd7a73302001-10-13 06:57:33 +0000487 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000488 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000489}
490
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000491// destroyConstant - Remove the constant from the constant table...
492//
493void ConstantPointerRef::destroyConstant() {
494 getValue()->getParent()->destroyConstantPointerRef(this);
495 destroyConstantImpl();
496}
497
498
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000499//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000500//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000501typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
502static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
503
Chris Lattner330b7ac2002-08-14 18:24:09 +0000504ConstantExpr *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000505
506 // Look up the constant in the table first to ensure uniqueness
507 vector<Constant*> argVec(1, C);
Chris Lattner330b7ac2002-08-14 18:24:09 +0000508 const ExprMapKeyType &Key = make_pair(Instruction::Cast, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000509 ConstantExpr *Result = ExprConstants.get(Ty, Key);
510 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000511
512 // Its not in the table so create a new one and put it in the table.
Chris Lattner330b7ac2002-08-14 18:24:09 +0000513 Result = new ConstantExpr(Instruction::Cast, C, Ty);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000514 ExprConstants.add(Ty, Key, Result);
515 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000516}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000517
Chris Lattner3cd8c562002-07-30 18:54:25 +0000518ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000519 // Look up the constant in the table first to ensure uniqueness
520 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000521 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
Chris Lattner3cd8c562002-07-30 18:54:25 +0000522 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000523 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000524
525 // Its not in the table so create a new one and put it in the table.
526 // Check the operands for consistency first
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000527 assert((Opcode >= Instruction::FirstBinaryOp &&
528 Opcode < Instruction::NumBinaryOps) &&
Chris Lattner3cd8c562002-07-30 18:54:25 +0000529 "Invalid opcode in binary constant expression");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000530
Chris Lattner3cd8c562002-07-30 18:54:25 +0000531 assert(C1->getType() == C2->getType() &&
532 "Operand types in binary constant expression should match");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000533
Chris Lattner3cd8c562002-07-30 18:54:25 +0000534 Result = new ConstantExpr(Opcode, C1, C2);
535 ExprConstants.add(C1->getType(), Key, Result);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000536 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000537}
538
Chris Lattner3cd8c562002-07-30 18:54:25 +0000539ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
540 const std::vector<Constant*> &IdxList) {
541 const Type *Ty = C->getType();
Vikram S. Adve4c485332002-07-15 18:19:33 +0000542
543 // Look up the constant in the table first to ensure uniqueness
544 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000545 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000546
Chris Lattner3cd8c562002-07-30 18:54:25 +0000547 const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000548 ConstantExpr *Result = ExprConstants.get(Ty, Key);
549 if (Result) return Result;
Chris Lattner3cd8c562002-07-30 18:54:25 +0000550
Vikram S. Adve4c485332002-07-15 18:19:33 +0000551 // Its not in the table so create a new one and put it in the table.
552 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000553 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000554 assert(isa<PointerType>(Ty) &&
555 "Non-pointer type for constant GelElementPtr expression");
556
Chris Lattner3cd8c562002-07-30 18:54:25 +0000557 // Check that the indices list is valid...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000558 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
Chris Lattner3cd8c562002-07-30 18:54:25 +0000559 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
560 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000561
Chris Lattner3cd8c562002-07-30 18:54:25 +0000562 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000563 ExprConstants.add(Ty, Key, Result);
564 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000565}
566
567// destroyConstant - Remove the constant from the constant table...
568//
569void ConstantExpr::destroyConstant() {
570 ExprConstants.remove(this);
571 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000572}
573
Chris Lattner3cd8c562002-07-30 18:54:25 +0000574const char *ConstantExpr::getOpcodeName() const {
575 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000576}
577
578
579//---- ConstantPointerRef::mutateReferences() implementation...
580//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000581unsigned ConstantPointerRef::mutateReferences(Value *OldV, Value *NewV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000582 assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000583 GlobalValue *NewGV = cast<GlobalValue>(NewV);
Chris Lattner3462ae32001-12-03 22:26:30 +0000584 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000585 Operands[0] = NewGV;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000586 return 1;
587}
588
589
590//---- ConstantPointerExpr::mutateReferences() implementation...
591//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000592unsigned ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
593 unsigned NumReplaced = 0;
594 Constant *NewC = cast<Constant>(NewV);
595 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000596 if (Operands[i] == OldV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000597 ++NumReplaced;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000598 Operands[i] = NewC;
599 }
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000600 return NumReplaced;
Chris Lattner25033252001-10-03 19:28:15 +0000601}