blob: bc521d7686de23d3400e67a8784e68a6134bdfaa [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
37// Static constructor to create a '0' constant of arbitrary type...
Chris Lattnera85386f2002-04-27 02:25:43 +000038Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000039 switch (Ty->getPrimitiveID()) {
Chris Lattner3462ae32001-12-03 22:26:30 +000040 case Type::BoolTyID: return ConstantBool::get(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000041 case Type::SByteTyID:
42 case Type::ShortTyID:
43 case Type::IntTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000044 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000045
46 case Type::UByteTyID:
47 case Type::UShortTyID:
48 case Type::UIntTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000049 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000050
51 case Type::FloatTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000052 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
Chris Lattner436248f2001-09-30 20:14:07 +000053
54 case Type::PointerTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000055 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2f7c9632001-06-06 20:29:01 +000056 default:
57 return 0;
58 }
59}
60
Chris Lattner3462ae32001-12-03 22:26:30 +000061void Constant::destroyConstantImpl() {
62 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000063 // references to the constant by other constants in the constant pool. These
64 // constants are implicitly dependant on the module that is being deleted,
65 // but they don't know that. Because we only find out when the CPV is
66 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000067 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000068 //
69 while (!use_empty()) {
70 Value *V = use_back();
71#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000072 if (!isa<Constant>(V))
73 std::cerr << "While deleting: " << *this
74 << "\n\nUse still stuck around after Def is destroyed: "
75 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000076#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000077 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000078 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000079 CPV->destroyConstant();
80
81 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000082 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000083 }
84
85 // Value has no outstanding references it is safe to delete it now...
86 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000087}
Chris Lattner2f7c9632001-06-06 20:29:01 +000088
89//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000090// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +000091//===----------------------------------------------------------------------===//
92
93//===----------------------------------------------------------------------===//
94// Normal Constructors
95
Chris Lattner3462ae32001-12-03 22:26:30 +000096ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000097 Val = V;
98}
Chris Lattner49d855c2001-09-07 16:46:31 +000099
Chris Lattner3462ae32001-12-03 22:26:30 +0000100ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000101 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000102}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000103
Chris Lattner3462ae32001-12-03 22:26:30 +0000104ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000105 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000106}
107
Chris Lattner3462ae32001-12-03 22:26:30 +0000108ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000109 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000110}
111
Chris Lattner3462ae32001-12-03 22:26:30 +0000112ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000113 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000114 Val = V;
115}
116
Chris Lattner3462ae32001-12-03 22:26:30 +0000117ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000118 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119 for (unsigned i = 0; i < V.size(); i++) {
120 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000121 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000122 }
123}
124
Chris Lattner3462ae32001-12-03 22:26:30 +0000125ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000126 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000128 assert(V.size() == ETypes.size() &&
129 "Invalid initializer vector for constant structure");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130 for (unsigned i = 0; i < V.size(); i++) {
131 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000132 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000133 }
134}
135
Chris Lattner3462ae32001-12-03 22:26:30 +0000136ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
137 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000138 Operands.push_back(Use(GV, this));
139}
140
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000141ConstantExpr::ConstantExpr(unsigned opCode, Constant *C, const Type *Ty)
142 : Constant(Ty), iType(opCode) {
143 Operands.push_back(Use(C, this));
144}
145
146ConstantExpr::ConstantExpr(unsigned opCode, Constant* C1,
147 Constant* C2, const Type *Ty)
148 : Constant(Ty), iType(opCode) {
149 Operands.push_back(Use(C1, this));
150 Operands.push_back(Use(C2, this));
151}
152
153ConstantExpr::ConstantExpr(unsigned opCode, Constant* C,
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000154 const std::vector<Constant*> &IdxList, const Type *Ty)
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000155 : Constant(Ty), iType(opCode) {
156 Operands.reserve(1+IdxList.size());
157 Operands.push_back(Use(C, this));
158 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
159 Operands.push_back(Use(IdxList[i], this));
160}
161
Chris Lattner60e0dd72001-10-03 06:12:09 +0000162
Chris Lattner2f7c9632001-06-06 20:29:01 +0000163
164//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000165// classof implementations
166
Chris Lattner3462ae32001-12-03 22:26:30 +0000167bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000168 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000169}
Chris Lattner3462ae32001-12-03 22:26:30 +0000170bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000171 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000172}
Chris Lattner3462ae32001-12-03 22:26:30 +0000173bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000174 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000175}
Chris Lattner3462ae32001-12-03 22:26:30 +0000176bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000177 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000178 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000179 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000180}
Chris Lattner3462ae32001-12-03 22:26:30 +0000181bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000182 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000183}
Chris Lattner3462ae32001-12-03 22:26:30 +0000184bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000185 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000186}
Chris Lattner3462ae32001-12-03 22:26:30 +0000187bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000188 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000189}
190
191
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000192
Chris Lattner2f7c9632001-06-06 20:29:01 +0000193//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194// isValueValidForType implementations
195
Chris Lattner3462ae32001-12-03 22:26:30 +0000196bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000197 switch (Ty->getPrimitiveID()) {
198 default:
199 return false; // These can't be represented as integers!!!
200
201 // Signed types...
202 case Type::SByteTyID:
203 return (Val <= INT8_MAX && Val >= INT8_MIN);
204 case Type::ShortTyID:
205 return (Val <= INT16_MAX && Val >= INT16_MIN);
206 case Type::IntTyID:
207 return (Val <= INT32_MAX && Val >= INT32_MIN);
208 case Type::LongTyID:
209 return true; // This is the largest type...
210 }
211 assert(0 && "WTF?");
212 return false;
213}
214
Chris Lattner3462ae32001-12-03 22:26:30 +0000215bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216 switch (Ty->getPrimitiveID()) {
217 default:
218 return false; // These can't be represented as integers!!!
219
220 // Unsigned types...
221 case Type::UByteTyID:
222 return (Val <= UINT8_MAX);
223 case Type::UShortTyID:
224 return (Val <= UINT16_MAX);
225 case Type::UIntTyID:
226 return (Val <= UINT32_MAX);
227 case Type::ULongTyID:
228 return true; // This is the largest type...
229 }
230 assert(0 && "WTF?");
231 return false;
232}
233
Chris Lattner3462ae32001-12-03 22:26:30 +0000234bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235 switch (Ty->getPrimitiveID()) {
236 default:
237 return false; // These can't be represented as floating point!
238
239 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000240 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000241 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000242 return (Val <= UINT8_MAX);
243 */
244 case Type::DoubleTyID:
245 return true; // This is the largest type...
246 }
247};
Chris Lattner9655e542001-07-20 19:16:02 +0000248
Chris Lattner49d855c2001-09-07 16:46:31 +0000249//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000250// Factory Function Implementation
251
Chris Lattner3462ae32001-12-03 22:26:30 +0000252template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000253struct ValueMap {
254 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000255 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000256
Chris Lattner3462ae32001-12-03 22:26:30 +0000257 inline ConstantClass *get(const Type *Ty, ValType V) {
258 map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000259 Map.find(ConstHashKey(Ty, V));
260 return (I != Map.end()) ? I->second : 0;
261 }
262
Chris Lattner3462ae32001-12-03 22:26:30 +0000263 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000264 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
265 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000266
Chris Lattner3462ae32001-12-03 22:26:30 +0000267 inline void remove(ConstantClass *CP) {
268 for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000269 E = Map.end(); I != E;++I)
270 if (I->second == CP) {
271 Map.erase(I);
272 return;
273 }
274 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000275};
276
Chris Lattner3462ae32001-12-03 22:26:30 +0000277//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000278//
Chris Lattner3462ae32001-12-03 22:26:30 +0000279static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000280
Chris Lattner3462ae32001-12-03 22:26:30 +0000281ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
282 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000283 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000284 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000285 return Result;
286}
287
Chris Lattner3462ae32001-12-03 22:26:30 +0000288ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
289 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000290 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000291 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000292 return Result;
293}
294
Chris Lattner3462ae32001-12-03 22:26:30 +0000295ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000296 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000297 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
298 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000299}
300
Chris Lattner3462ae32001-12-03 22:26:30 +0000301//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000302//
Chris Lattner3462ae32001-12-03 22:26:30 +0000303static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000304
Chris Lattner3462ae32001-12-03 22:26:30 +0000305ConstantFP *ConstantFP::get(const Type *Ty, double V) {
306 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000307 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000308 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000309 return Result;
310}
311
Chris Lattner3462ae32001-12-03 22:26:30 +0000312//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000313//
Chris Lattner7f74a562002-01-20 22:54:45 +0000314static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000315
Chris Lattner3462ae32001-12-03 22:26:30 +0000316ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000317 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000318 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000319 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000320 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000321 return Result;
322}
323
Chris Lattner3462ae32001-12-03 22:26:30 +0000324// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000325// contain the specified string. A null terminator is added to the specified
326// string so that it may be used in a natural way...
327//
Chris Lattner7f74a562002-01-20 22:54:45 +0000328ConstantArray *ConstantArray::get(const std::string &Str) {
329 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000330
331 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000332 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000333
334 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000335 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000336
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000337 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000338 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000339}
340
341
Chris Lattnerd7a73302001-10-13 06:57:33 +0000342// destroyConstant - Remove the constant from the constant table...
343//
Chris Lattner3462ae32001-12-03 22:26:30 +0000344void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000345 ArrayConstants.remove(this);
346 destroyConstantImpl();
347}
348
Chris Lattner3462ae32001-12-03 22:26:30 +0000349//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000350//
Chris Lattner7f74a562002-01-20 22:54:45 +0000351static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000352
Chris Lattner3462ae32001-12-03 22:26:30 +0000353ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000354 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000355 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000356 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000357 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000358 return Result;
359}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000360
Chris Lattnerd7a73302001-10-13 06:57:33 +0000361// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000362//
Chris Lattner3462ae32001-12-03 22:26:30 +0000363void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000364 StructConstants.remove(this);
365 destroyConstantImpl();
366}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000367
Chris Lattner3462ae32001-12-03 22:26:30 +0000368//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000369//
Chris Lattner3462ae32001-12-03 22:26:30 +0000370static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000371
Chris Lattner3462ae32001-12-03 22:26:30 +0000372ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
373 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000374 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000375 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000376 return Result;
377}
378
Chris Lattner3462ae32001-12-03 22:26:30 +0000379//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000380//
Chris Lattner3462ae32001-12-03 22:26:30 +0000381ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000382 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000383
Chris Lattnerd7a73302001-10-13 06:57:33 +0000384 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000385 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000386}
387
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000388//---- ConstantExpr::get() implementations...
389// Return NULL on invalid expressions.
390//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000391typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
392static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
393
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000394ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C, const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000395
396 // Look up the constant in the table first to ensure uniqueness
397 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000398 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
399 ConstantExpr *Result = ExprConstants.get(Ty, Key);
400 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000401
402 // Its not in the table so create a new one and put it in the table.
403 // Check the operands for consistency first
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000404 assert(Opcode == Instruction::Cast ||
405 (Opcode >= Instruction::FirstUnaryOp &&
406 Opcode < Instruction::NumUnaryOps) &&
407 "Invalid opcode in unary ConstantExpr!");
408
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000409 // type of operand will not match result for Cast operation
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000410 assert((Opcode == Instruction::Cast || Ty == C->getType()) &&
411 "Type of operand in unary constant expression should match result");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000412
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000413 Result = new ConstantExpr(Opcode, C, Ty);
414 ExprConstants.add(Ty, Key, Result);
415 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000416}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000417
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000418ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
419 const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000420
421 // Look up the constant in the table first to ensure uniqueness
422 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000423 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
424 ConstantExpr *Result = ExprConstants.get(Ty, Key);
425 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000426
427 // Its not in the table so create a new one and put it in the table.
428 // Check the operands for consistency first
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000429 assert((Opcode >= Instruction::FirstBinaryOp &&
430 Opcode < Instruction::NumBinaryOps) &&
431 "Invalid opcode in binary constant expression");
432
433 assert(Ty == C1->getType() && Ty == C2->getType() &&
434 "Operand types in binary constant expression should match result");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000435
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000436 Result = new ConstantExpr(Opcode, C1, C2, Ty);
437 ExprConstants.add(Ty, Key, Result);
438 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000439}
440
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000441ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C,
442 const std::vector<Constant*> &IdxList,
443 const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000444
445 // Look up the constant in the table first to ensure uniqueness
446 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000447 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000448
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000449 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
450 ConstantExpr *Result = ExprConstants.get(Ty, Key);
451 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000452
453 // Its not in the table so create a new one and put it in the table.
454 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000455 // Must be a getElementPtr. Check for valid getElementPtr expression.
456 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000457 assert(Opcode == Instruction::GetElementPtr &&
458 "Operator other than GetElementPtr used with an index list");
459
460 assert(isa<PointerType>(Ty) &&
461 "Non-pointer type for constant GelElementPtr expression");
462
463 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
464 const Type *fldType = GetElementPtrInst::getIndexedType(C->getType(),
465 ValIdxList, true);
466 assert(fldType && "Invalid index list for constant GelElementPtr expression");
467
468 assert(cast<PointerType>(Ty)->getElementType() == fldType &&
469 "Type for constant GelElementPtr expression doesn't match field type");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000470
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000471 Result = new ConstantExpr(Opcode, C, IdxList, Ty);
472 ExprConstants.add(Ty, Key, Result);
473 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000474}
475
476// destroyConstant - Remove the constant from the constant table...
477//
478void ConstantExpr::destroyConstant() {
479 ExprConstants.remove(this);
480 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000481}
482
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000483const char *ConstantExpr::getOpcodeName(unsigned Opcode) {
484 return Instruction::getOpcodeName(Opcode);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000485}
486
487
488//---- ConstantPointerRef::mutateReferences() implementation...
489//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000490unsigned ConstantPointerRef::mutateReferences(Value *OldV, Value *NewV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000491 assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000492 GlobalValue *NewGV = cast<GlobalValue>(NewV);
Chris Lattner3462ae32001-12-03 22:26:30 +0000493 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000494 Operands[0] = NewGV;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000495 return 1;
496}
497
498
499//---- ConstantPointerExpr::mutateReferences() implementation...
500//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000501unsigned ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
502 unsigned NumReplaced = 0;
503 Constant *NewC = cast<Constant>(NewV);
504 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000505 if (Operands[i] == OldV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000506 ++NumReplaced;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000507 Operands[i] = NewC;
508 }
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000509 return NumReplaced;
Chris Lattner25033252001-10-03 19:28:15 +0000510}