blob: becaf2e44f4c4d0f7fe3852969c9b56707505c0b [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;
20
Chris Lattner3462ae32001-12-03 22:26:30 +000021ConstantBool *ConstantBool::True = new ConstantBool(true);
22ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000023
Chris Lattner9655e542001-07-20 19:16:02 +000024
Chris Lattner2f7c9632001-06-06 20:29:01 +000025//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000026// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000027//===----------------------------------------------------------------------===//
28
29// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000030void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000031 assert(ST && "Type::setName - Must provide symbol table argument!");
32
33 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000034}
35
36// Static constructor to create a '0' constant of arbitrary type...
Chris Lattnera85386f2002-04-27 02:25:43 +000037Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000038 switch (Ty->getPrimitiveID()) {
Chris Lattner3462ae32001-12-03 22:26:30 +000039 case Type::BoolTyID: return ConstantBool::get(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000040 case Type::SByteTyID:
41 case Type::ShortTyID:
42 case Type::IntTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000043 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000044
45 case Type::UByteTyID:
46 case Type::UShortTyID:
47 case Type::UIntTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000048 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000049
50 case Type::FloatTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000051 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
Chris Lattner436248f2001-09-30 20:14:07 +000052
53 case Type::PointerTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000054 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2f7c9632001-06-06 20:29:01 +000055 default:
56 return 0;
57 }
58}
59
Chris Lattner3462ae32001-12-03 22:26:30 +000060void Constant::destroyConstantImpl() {
61 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000062 // references to the constant by other constants in the constant pool. These
63 // constants are implicitly dependant on the module that is being deleted,
64 // but they don't know that. Because we only find out when the CPV is
65 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000066 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000067 //
68 while (!use_empty()) {
69 Value *V = use_back();
70#ifndef NDEBUG // Only in -g mode...
Chris Lattner3462ae32001-12-03 22:26:30 +000071 if (!isa<Constant>(V)) {
Chris Lattnere026e922002-04-07 22:32:25 +000072 std::cerr << "While deleting: ";
73 dump();
74 std::cerr << "\nUse still stuck around after Def is destroyed: ";
75 V->dump();
76 std::cerr << "\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000077 }
78#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000079 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000080 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000081 CPV->destroyConstant();
82
83 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000084 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000085 }
86
87 // Value has no outstanding references it is safe to delete it now...
88 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000089}
Chris Lattner2f7c9632001-06-06 20:29:01 +000090
91//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000092// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +000093//===----------------------------------------------------------------------===//
94
95//===----------------------------------------------------------------------===//
96// Normal Constructors
97
Chris Lattner3462ae32001-12-03 22:26:30 +000098ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000099 Val = V;
100}
Chris Lattner49d855c2001-09-07 16:46:31 +0000101
Chris Lattner3462ae32001-12-03 22:26:30 +0000102ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000103 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000104}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000105
Chris Lattner3462ae32001-12-03 22:26:30 +0000106ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000107 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000108}
109
Chris Lattner3462ae32001-12-03 22:26:30 +0000110ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000111 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000112}
113
Chris Lattner3462ae32001-12-03 22:26:30 +0000114ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000115 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116 Val = V;
117}
118
Chris Lattner3462ae32001-12-03 22:26:30 +0000119ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000120 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000121 for (unsigned i = 0; i < V.size(); i++) {
122 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000123 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000124 }
125}
126
Chris Lattner3462ae32001-12-03 22:26:30 +0000127ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000128 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000130 assert(V.size() == ETypes.size() &&
131 "Invalid initializer vector for constant structure");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000132 for (unsigned i = 0; i < V.size(); i++) {
133 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000134 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000135 }
136}
137
Chris Lattner3462ae32001-12-03 22:26:30 +0000138ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
139 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000140 Operands.push_back(Use(GV, this));
141}
142
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000143ConstantExpr::ConstantExpr(unsigned opCode, Constant *C, const Type *Ty)
144 : Constant(Ty), iType(opCode) {
145 Operands.push_back(Use(C, this));
146}
147
148ConstantExpr::ConstantExpr(unsigned opCode, Constant* C1,
149 Constant* C2, const Type *Ty)
150 : Constant(Ty), iType(opCode) {
151 Operands.push_back(Use(C1, this));
152 Operands.push_back(Use(C2, this));
153}
154
155ConstantExpr::ConstantExpr(unsigned opCode, Constant* C,
Vikram S. Adve4c485332002-07-15 18:19:33 +0000156 const std::vector<Value*>& IdxList, const Type *Ty)
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000157 : Constant(Ty), iType(opCode) {
158 Operands.reserve(1+IdxList.size());
159 Operands.push_back(Use(C, this));
160 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
161 Operands.push_back(Use(IdxList[i], this));
162}
163
Chris Lattner60e0dd72001-10-03 06:12:09 +0000164
Chris Lattner2f7c9632001-06-06 20:29:01 +0000165
166//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000167// classof implementations
168
Chris Lattner3462ae32001-12-03 22:26:30 +0000169bool ConstantInt::classof(const Constant *CPV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000170 return CPV->getType()->isIntegral() && ! isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000171}
Chris Lattner3462ae32001-12-03 22:26:30 +0000172bool ConstantSInt::classof(const Constant *CPV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000173 return CPV->getType()->isSigned() && ! isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000174}
Chris Lattner3462ae32001-12-03 22:26:30 +0000175bool ConstantUInt::classof(const Constant *CPV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000176 return CPV->getType()->isUnsigned() && ! isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000177}
Chris Lattner3462ae32001-12-03 22:26:30 +0000178bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000179 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000180 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
181 ! isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000182}
Chris Lattner3462ae32001-12-03 22:26:30 +0000183bool ConstantArray::classof(const Constant *CPV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000184 return isa<ArrayType>(CPV->getType()) && ! isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000185}
Chris Lattner3462ae32001-12-03 22:26:30 +0000186bool ConstantStruct::classof(const Constant *CPV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000187 return isa<StructType>(CPV->getType()) && ! isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000188}
Chris Lattner3462ae32001-12-03 22:26:30 +0000189bool ConstantPointer::classof(const Constant *CPV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000190 return (isa<PointerType>(CPV->getType()) && ! isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000191}
192
193
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000194
Chris Lattner2f7c9632001-06-06 20:29:01 +0000195//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196// isValueValidForType implementations
197
Chris Lattner3462ae32001-12-03 22:26:30 +0000198bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000199 switch (Ty->getPrimitiveID()) {
200 default:
201 return false; // These can't be represented as integers!!!
202
203 // Signed types...
204 case Type::SByteTyID:
205 return (Val <= INT8_MAX && Val >= INT8_MIN);
206 case Type::ShortTyID:
207 return (Val <= INT16_MAX && Val >= INT16_MIN);
208 case Type::IntTyID:
209 return (Val <= INT32_MAX && Val >= INT32_MIN);
210 case Type::LongTyID:
211 return true; // This is the largest type...
212 }
213 assert(0 && "WTF?");
214 return false;
215}
216
Chris Lattner3462ae32001-12-03 22:26:30 +0000217bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218 switch (Ty->getPrimitiveID()) {
219 default:
220 return false; // These can't be represented as integers!!!
221
222 // Unsigned types...
223 case Type::UByteTyID:
224 return (Val <= UINT8_MAX);
225 case Type::UShortTyID:
226 return (Val <= UINT16_MAX);
227 case Type::UIntTyID:
228 return (Val <= UINT32_MAX);
229 case Type::ULongTyID:
230 return true; // This is the largest type...
231 }
232 assert(0 && "WTF?");
233 return false;
234}
235
Chris Lattner3462ae32001-12-03 22:26:30 +0000236bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237 switch (Ty->getPrimitiveID()) {
238 default:
239 return false; // These can't be represented as floating point!
240
241 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000242 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000243 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000244 return (Val <= UINT8_MAX);
245 */
246 case Type::DoubleTyID:
247 return true; // This is the largest type...
248 }
249};
Chris Lattner9655e542001-07-20 19:16:02 +0000250
Chris Lattner49d855c2001-09-07 16:46:31 +0000251//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000252// Factory Function Implementation
253
Chris Lattner3462ae32001-12-03 22:26:30 +0000254template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000255struct ValueMap {
256 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000257 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000258
Chris Lattner3462ae32001-12-03 22:26:30 +0000259 inline ConstantClass *get(const Type *Ty, ValType V) {
260 map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000261 Map.find(ConstHashKey(Ty, V));
262 return (I != Map.end()) ? I->second : 0;
263 }
264
Chris Lattner3462ae32001-12-03 22:26:30 +0000265 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000266 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
267 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000268
Chris Lattner3462ae32001-12-03 22:26:30 +0000269 inline void remove(ConstantClass *CP) {
270 for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000271 E = Map.end(); I != E;++I)
272 if (I->second == CP) {
273 Map.erase(I);
274 return;
275 }
276 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000277};
278
Chris Lattner3462ae32001-12-03 22:26:30 +0000279//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000280//
Chris Lattner3462ae32001-12-03 22:26:30 +0000281static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000282
Chris Lattner3462ae32001-12-03 22:26:30 +0000283ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
284 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000285 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000286 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000287 return Result;
288}
289
Chris Lattner3462ae32001-12-03 22:26:30 +0000290ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
291 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000292 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000293 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000294 return Result;
295}
296
Chris Lattner3462ae32001-12-03 22:26:30 +0000297ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000298 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000299 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
300 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000301}
302
Chris Lattner3462ae32001-12-03 22:26:30 +0000303//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000304//
Chris Lattner3462ae32001-12-03 22:26:30 +0000305static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000306
Chris Lattner3462ae32001-12-03 22:26:30 +0000307ConstantFP *ConstantFP::get(const Type *Ty, double V) {
308 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000309 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000310 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000311 return Result;
312}
313
Chris Lattner3462ae32001-12-03 22:26:30 +0000314//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000315//
Chris Lattner7f74a562002-01-20 22:54:45 +0000316static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000317
Chris Lattner3462ae32001-12-03 22:26:30 +0000318ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000319 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000320 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000321 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000322 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000323 return Result;
324}
325
Chris Lattner3462ae32001-12-03 22:26:30 +0000326// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000327// contain the specified string. A null terminator is added to the specified
328// string so that it may be used in a natural way...
329//
Chris Lattner7f74a562002-01-20 22:54:45 +0000330ConstantArray *ConstantArray::get(const std::string &Str) {
331 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000332
333 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000334 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000335
336 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000337 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000338
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000339 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000340 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000341}
342
343
Chris Lattnerd7a73302001-10-13 06:57:33 +0000344// destroyConstant - Remove the constant from the constant table...
345//
Chris Lattner3462ae32001-12-03 22:26:30 +0000346void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000347 ArrayConstants.remove(this);
348 destroyConstantImpl();
349}
350
Chris Lattner3462ae32001-12-03 22:26:30 +0000351//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000352//
Chris Lattner7f74a562002-01-20 22:54:45 +0000353static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000354
Chris Lattner3462ae32001-12-03 22:26:30 +0000355ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000356 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000357 ConstantStruct *Result = StructConstants.get(Ty, 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 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000360 return Result;
361}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000362
Chris Lattnerd7a73302001-10-13 06:57:33 +0000363// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000364//
Chris Lattner3462ae32001-12-03 22:26:30 +0000365void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000366 StructConstants.remove(this);
367 destroyConstantImpl();
368}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000369
Chris Lattner3462ae32001-12-03 22:26:30 +0000370//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000371//
Chris Lattner3462ae32001-12-03 22:26:30 +0000372static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000373
Chris Lattner3462ae32001-12-03 22:26:30 +0000374ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
375 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000376 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000377 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000378 return Result;
379}
380
Chris Lattner3462ae32001-12-03 22:26:30 +0000381//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000382//
Chris Lattner3462ae32001-12-03 22:26:30 +0000383ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000384 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000385
Chris Lattnerd7a73302001-10-13 06:57:33 +0000386 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000387 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000388}
389
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000390//---- ConstantExpr::get() implementations...
391// Return NULL on invalid expressions.
392//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000393typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
394static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
395
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000396ConstantExpr*
397ConstantExpr::get(unsigned opCode, Constant *C, const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000398
399 // Look up the constant in the table first to ensure uniqueness
400 vector<Constant*> argVec(1, C);
401 const ExprMapKeyType& key = make_pair(opCode, argVec);
402 ConstantExpr* result = ExprConstants.get(Ty, key);
403 if (result)
404 return result;
405
406 // Its not in the table so create a new one and put it in the table.
407 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000408 if (opCode != Instruction::Cast &&
409 (opCode < Instruction::FirstUnaryOp ||
410 opCode >= Instruction::NumUnaryOps)) {
411 cerr << "Invalid opcode " << ConstantExpr::getOpcodeName(opCode)
412 << " in unary constant expression" << endl;
413 return NULL; // Not Cast or other unary opcode
414 }
415 // type of operand will not match result for Cast operation
416 if (opCode != Instruction::Cast && Ty != C->getType()) {
417 cerr << "Type of operand in unary constant expression should match result" << endl;
418 return NULL;
419 }
Vikram S. Adve4c485332002-07-15 18:19:33 +0000420
421 result = new ConstantExpr(opCode, C, Ty);
422 ExprConstants.add(Ty, key, result);
423 return result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000424}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000425
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000426ConstantExpr*
427ConstantExpr::get(unsigned opCode, Constant *C1, Constant *C2,const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000428
429 // Look up the constant in the table first to ensure uniqueness
430 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
431 const ExprMapKeyType& key = make_pair(opCode, argVec);
432 ConstantExpr* result = ExprConstants.get(Ty, key);
433 if (result)
434 return result;
435
436 // Its not in the table so create a new one and put it in the table.
437 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000438 if (opCode < Instruction::FirstBinaryOp ||
439 opCode >= Instruction::NumBinaryOps) {
440 cerr << "Invalid opcode " << ConstantExpr::getOpcodeName(opCode)
441 << " in binary constant expression" << endl;
442 return NULL;
443 }
444 if (Ty != C1->getType() || Ty != C2->getType()) {
445 cerr << "Types of both operands in binary constant expression should match result" << endl;
446 return NULL;
447 }
Vikram S. Adve4c485332002-07-15 18:19:33 +0000448
449 result = new ConstantExpr(opCode, C1, C2, Ty);
450 ExprConstants.add(Ty, key, result);
451 return result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000452}
453
454ConstantExpr*
455ConstantExpr::get(unsigned opCode, Constant*C,
Vikram S. Adve4c485332002-07-15 18:19:33 +0000456 const std::vector<Value*>& idxList, const Type *Ty) {
457
458 // Look up the constant in the table first to ensure uniqueness
459 vector<Constant*> argVec(1, C);
460 for(vector<Value*>::const_iterator VI=idxList.begin(), VE=idxList.end();
461 VI != VE; ++VI)
462 if (Constant *C = dyn_cast<Constant>(*VI))
463 argVec.push_back(C);
464 else {
465 cerr << "Non-constant index in constant GetElementPtr expr";
466 return NULL;
467 }
468
469 const ExprMapKeyType& key = make_pair(opCode, argVec);
470 ConstantExpr* result = ExprConstants.get(Ty, key);
471 if (result)
472 return result;
473
474 // Its not in the table so create a new one and put it in the table.
475 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000476 // Must be a getElementPtr. Check for valid getElementPtr expression.
477 //
478 if (opCode != Instruction::GetElementPtr) {
479 cerr << "operator other than GetElementPtr used with an index list" << endl;
480 return NULL;
481 }
482 if (!isa<ConstantPointer>(C)) {
483 cerr << "Constant GelElementPtr expression using something other than a constant pointer" << endl;
484 return NULL;
485 }
486 if (!isa<PointerType>(Ty)) {
487 cerr << "Non-pointer type for constant GelElementPtr expression" << endl;
488 return NULL;
489 }
490 const Type* fldType = GetElementPtrInst::getIndexedType(C->getType(),
491 idxList, true);
492 if (!fldType) {
493 cerr << "Invalid index list for constant GelElementPtr expression" << endl;
494 return NULL;
495 }
496 if (cast<PointerType>(Ty)->getElementType() != fldType) {
497 cerr << "Type for constant GelElementPtr expression does not match field type" << endl;
498 return NULL;
499 }
500
Vikram S. Adve4c485332002-07-15 18:19:33 +0000501 result = new ConstantExpr(opCode, C, idxList, Ty);
502 ExprConstants.add(Ty, key, result);
503 return result;
504}
505
506// destroyConstant - Remove the constant from the constant table...
507//
508void ConstantExpr::destroyConstant() {
509 ExprConstants.remove(this);
510 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000511}
512
513const char*
514ConstantExpr::getOpcodeName(unsigned opCode) {
515 return Instruction::getOpcodeName(opCode);
516}
517
518
519//---- ConstantPointerRef::mutateReferences() implementation...
520//
521unsigned
522ConstantPointerRef::mutateReferences(Value* OldV, Value *NewV) {
523 assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
524 GlobalValue* NewGV = cast<GlobalValue>(NewV);
Chris Lattner3462ae32001-12-03 22:26:30 +0000525 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000526 Operands[0] = NewGV;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000527 return 1;
528}
529
530
531//---- ConstantPointerExpr::mutateReferences() implementation...
532//
533unsigned
534ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
535 unsigned numReplaced = 0;
536 Constant* NewC = cast<Constant>(NewV);
537 for (unsigned i=0, N = getNumOperands(); i < N; ++i)
538 if (Operands[i] == OldV) {
539 ++numReplaced;
540 Operands[i] = NewC;
541 }
542 return numReplaced;
Chris Lattner25033252001-10-03 19:28:15 +0000543}