blob: 146f307a55a8fbbdfcc730f85c94185c7a3ff0dd [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 Lattnerb0b412e2002-09-03 01:08:28 +0000239 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000240}
241
Chris Lattner3462ae32001-12-03 22:26:30 +0000242bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000243 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000244}
Chris Lattner3462ae32001-12-03 22:26:30 +0000245bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000246 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000247}
Chris Lattner3462ae32001-12-03 22:26:30 +0000248bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000249 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000250}
Chris Lattner3462ae32001-12-03 22:26:30 +0000251bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000252 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000253 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000254 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000255}
Chris Lattner3462ae32001-12-03 22:26:30 +0000256bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000257 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000258}
Chris Lattner3462ae32001-12-03 22:26:30 +0000259bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000260 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000261}
Chris Lattner3462ae32001-12-03 22:26:30 +0000262bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000263 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000264}
265
266
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000267
Chris Lattner2f7c9632001-06-06 20:29:01 +0000268//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000269// isValueValidForType implementations
270
Chris Lattner3462ae32001-12-03 22:26:30 +0000271bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000272 switch (Ty->getPrimitiveID()) {
273 default:
274 return false; // These can't be represented as integers!!!
275
276 // Signed types...
277 case Type::SByteTyID:
278 return (Val <= INT8_MAX && Val >= INT8_MIN);
279 case Type::ShortTyID:
280 return (Val <= INT16_MAX && Val >= INT16_MIN);
281 case Type::IntTyID:
282 return (Val <= INT32_MAX && Val >= INT32_MIN);
283 case Type::LongTyID:
284 return true; // This is the largest type...
285 }
286 assert(0 && "WTF?");
287 return false;
288}
289
Chris Lattner3462ae32001-12-03 22:26:30 +0000290bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000291 switch (Ty->getPrimitiveID()) {
292 default:
293 return false; // These can't be represented as integers!!!
294
295 // Unsigned types...
296 case Type::UByteTyID:
297 return (Val <= UINT8_MAX);
298 case Type::UShortTyID:
299 return (Val <= UINT16_MAX);
300 case Type::UIntTyID:
301 return (Val <= UINT32_MAX);
302 case Type::ULongTyID:
303 return true; // This is the largest type...
304 }
305 assert(0 && "WTF?");
306 return false;
307}
308
Chris Lattner3462ae32001-12-03 22:26:30 +0000309bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000310 switch (Ty->getPrimitiveID()) {
311 default:
312 return false; // These can't be represented as floating point!
313
314 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000315 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000316 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000317 return (Val <= UINT8_MAX);
318 */
319 case Type::DoubleTyID:
320 return true; // This is the largest type...
321 }
322};
Chris Lattner9655e542001-07-20 19:16:02 +0000323
Chris Lattner49d855c2001-09-07 16:46:31 +0000324//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000325// Factory Function Implementation
326
Chris Lattner3462ae32001-12-03 22:26:30 +0000327template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000328struct ValueMap {
329 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000330 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000331
Chris Lattner3462ae32001-12-03 22:26:30 +0000332 inline ConstantClass *get(const Type *Ty, ValType V) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000333 typename map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000334 Map.find(ConstHashKey(Ty, V));
335 return (I != Map.end()) ? I->second : 0;
336 }
337
Chris Lattner3462ae32001-12-03 22:26:30 +0000338 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000339 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
340 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000341
Chris Lattner3462ae32001-12-03 22:26:30 +0000342 inline void remove(ConstantClass *CP) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000343 for (typename map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000344 E = Map.end(); I != E;++I)
345 if (I->second == CP) {
346 Map.erase(I);
347 return;
348 }
349 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000350};
351
Chris Lattner3462ae32001-12-03 22:26:30 +0000352//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000353//
Chris Lattner3462ae32001-12-03 22:26:30 +0000354static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000355
Chris Lattner3462ae32001-12-03 22:26:30 +0000356ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
357 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000358 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000359 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000360 return Result;
361}
362
Chris Lattner3462ae32001-12-03 22:26:30 +0000363ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
364 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000365 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000366 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000367 return Result;
368}
369
Chris Lattner3462ae32001-12-03 22:26:30 +0000370ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000371 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000372 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
373 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000374}
375
Chris Lattner3462ae32001-12-03 22:26:30 +0000376//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000377//
Chris Lattner3462ae32001-12-03 22:26:30 +0000378static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000379
Chris Lattner3462ae32001-12-03 22:26:30 +0000380ConstantFP *ConstantFP::get(const Type *Ty, double V) {
381 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000382 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000383 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000384 return Result;
385}
386
Chris Lattner3462ae32001-12-03 22:26:30 +0000387//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000388//
Chris Lattner7f74a562002-01-20 22:54:45 +0000389static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000390
Chris Lattner3462ae32001-12-03 22:26:30 +0000391ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000392 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000393 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000394 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000395 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000396 return Result;
397}
398
Chris Lattner3462ae32001-12-03 22:26:30 +0000399// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000400// contain the specified string. A null terminator is added to the specified
401// string so that it may be used in a natural way...
402//
Chris Lattner7f74a562002-01-20 22:54:45 +0000403ConstantArray *ConstantArray::get(const std::string &Str) {
404 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000405
406 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000407 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000408
409 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000410 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000411
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000412 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000413 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000414}
415
416
Chris Lattnerd7a73302001-10-13 06:57:33 +0000417// destroyConstant - Remove the constant from the constant table...
418//
Chris Lattner3462ae32001-12-03 22:26:30 +0000419void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000420 ArrayConstants.remove(this);
421 destroyConstantImpl();
422}
423
Chris Lattner81fabb02002-08-26 17:53:56 +0000424// getAsString - If the sub-element type of this array is either sbyte or ubyte,
425// then this method converts the array to an std::string and returns it.
426// Otherwise, it asserts out.
427//
428std::string ConstantArray::getAsString() const {
429 std::string Result;
430 if (getType()->getElementType() == Type::SByteTy)
431 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
432 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
433 else {
434 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
435 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
436 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
437 }
438 return Result;
439}
440
441
Chris Lattner3462ae32001-12-03 22:26:30 +0000442//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000443//
Chris Lattner7f74a562002-01-20 22:54:45 +0000444static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000445
Chris Lattner3462ae32001-12-03 22:26:30 +0000446ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000447 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000448 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000449 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000450 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000451 return Result;
452}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000453
Chris Lattnerd7a73302001-10-13 06:57:33 +0000454// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000455//
Chris Lattner3462ae32001-12-03 22:26:30 +0000456void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000457 StructConstants.remove(this);
458 destroyConstantImpl();
459}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000460
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000461
Chris Lattner3462ae32001-12-03 22:26:30 +0000462//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000463//
Chris Lattner3462ae32001-12-03 22:26:30 +0000464static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000465
Chris Lattner3462ae32001-12-03 22:26:30 +0000466ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
467 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000468 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000469 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000470 return Result;
471}
472
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000473// destroyConstant - Remove the constant from the constant table...
474//
475void ConstantPointerNull::destroyConstant() {
476 NullPtrConstants.remove(this);
477 destroyConstantImpl();
478}
479
480
Chris Lattner3462ae32001-12-03 22:26:30 +0000481//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000482//
Chris Lattner3462ae32001-12-03 22:26:30 +0000483ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000484 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000485
Chris Lattnerd7a73302001-10-13 06:57:33 +0000486 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000487 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000488}
489
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000490// destroyConstant - Remove the constant from the constant table...
491//
492void ConstantPointerRef::destroyConstant() {
493 getValue()->getParent()->destroyConstantPointerRef(this);
494 destroyConstantImpl();
495}
496
497
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000498//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000499//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000500typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
501static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
502
Chris Lattner330b7ac2002-08-14 18:24:09 +0000503ConstantExpr *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000504
505 // Look up the constant in the table first to ensure uniqueness
506 vector<Constant*> argVec(1, C);
Chris Lattner330b7ac2002-08-14 18:24:09 +0000507 const ExprMapKeyType &Key = make_pair(Instruction::Cast, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000508 ConstantExpr *Result = ExprConstants.get(Ty, Key);
509 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000510
511 // Its not in the table so create a new one and put it in the table.
Chris Lattner330b7ac2002-08-14 18:24:09 +0000512 Result = new ConstantExpr(Instruction::Cast, C, Ty);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000513 ExprConstants.add(Ty, Key, Result);
514 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000515}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000516
Chris Lattner3cd8c562002-07-30 18:54:25 +0000517ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000518 // Look up the constant in the table first to ensure uniqueness
519 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000520 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
Chris Lattner3cd8c562002-07-30 18:54:25 +0000521 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000522 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000523
524 // Its not in the table so create a new one and put it in the table.
525 // Check the operands for consistency first
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000526 assert((Opcode >= Instruction::FirstBinaryOp &&
527 Opcode < Instruction::NumBinaryOps) &&
Chris Lattner3cd8c562002-07-30 18:54:25 +0000528 "Invalid opcode in binary constant expression");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000529
Chris Lattner3cd8c562002-07-30 18:54:25 +0000530 assert(C1->getType() == C2->getType() &&
531 "Operand types in binary constant expression should match");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000532
Chris Lattner3cd8c562002-07-30 18:54:25 +0000533 Result = new ConstantExpr(Opcode, C1, C2);
534 ExprConstants.add(C1->getType(), Key, Result);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000535 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000536}
537
Chris Lattner3cd8c562002-07-30 18:54:25 +0000538ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
539 const std::vector<Constant*> &IdxList) {
540 const Type *Ty = C->getType();
Vikram S. Adve4c485332002-07-15 18:19:33 +0000541
542 // Look up the constant in the table first to ensure uniqueness
543 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000544 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000545
Chris Lattner3cd8c562002-07-30 18:54:25 +0000546 const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000547 ConstantExpr *Result = ExprConstants.get(Ty, Key);
548 if (Result) return Result;
Chris Lattner3cd8c562002-07-30 18:54:25 +0000549
Vikram S. Adve4c485332002-07-15 18:19:33 +0000550 // Its not in the table so create a new one and put it in the table.
551 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000552 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000553 assert(isa<PointerType>(Ty) &&
554 "Non-pointer type for constant GelElementPtr expression");
555
Chris Lattner3cd8c562002-07-30 18:54:25 +0000556 // Check that the indices list is valid...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000557 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
Chris Lattner3cd8c562002-07-30 18:54:25 +0000558 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
559 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000560
Chris Lattner3cd8c562002-07-30 18:54:25 +0000561 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000562 ExprConstants.add(Ty, Key, Result);
563 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000564}
565
566// destroyConstant - Remove the constant from the constant table...
567//
568void ConstantExpr::destroyConstant() {
569 ExprConstants.remove(this);
570 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000571}
572
Chris Lattner3cd8c562002-07-30 18:54:25 +0000573const char *ConstantExpr::getOpcodeName() const {
574 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000575}
576
577
578//---- ConstantPointerRef::mutateReferences() implementation...
579//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000580unsigned ConstantPointerRef::mutateReferences(Value *OldV, Value *NewV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000581 assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000582 GlobalValue *NewGV = cast<GlobalValue>(NewV);
Chris Lattner3462ae32001-12-03 22:26:30 +0000583 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000584 Operands[0] = NewGV;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000585 return 1;
586}
587
588
589//---- ConstantPointerExpr::mutateReferences() implementation...
590//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000591unsigned ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
592 unsigned NumReplaced = 0;
593 Constant *NewC = cast<Constant>(NewV);
594 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000595 if (Operands[i] == OldV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000596 ++NumReplaced;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000597 Operands[i] = NewC;
598 }
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000599 return NumReplaced;
Chris Lattner25033252001-10-03 19:28:15 +0000600}