blob: 68a3c83aec87ed893090532a4099c58e5db2c6c5 [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;
21using std::cerr;
22using std::endl;
Chris Lattner7f74a562002-01-20 22:54:45 +000023
Chris Lattner3462ae32001-12-03 22:26:30 +000024ConstantBool *ConstantBool::True = new ConstantBool(true);
25ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000026
Chris Lattner9655e542001-07-20 19:16:02 +000027
Chris Lattner2f7c9632001-06-06 20:29:01 +000028//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000029// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000030//===----------------------------------------------------------------------===//
31
32// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000033void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000034 assert(ST && "Type::setName - Must provide symbol table argument!");
35
36 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000037}
38
39// Static constructor to create a '0' constant of arbitrary type...
Chris Lattnera85386f2002-04-27 02:25:43 +000040Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000041 switch (Ty->getPrimitiveID()) {
Chris Lattner3462ae32001-12-03 22:26:30 +000042 case Type::BoolTyID: return ConstantBool::get(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000043 case Type::SByteTyID:
44 case Type::ShortTyID:
45 case Type::IntTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000046 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000047
48 case Type::UByteTyID:
49 case Type::UShortTyID:
50 case Type::UIntTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000051 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000052
53 case Type::FloatTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000054 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
Chris Lattner436248f2001-09-30 20:14:07 +000055
56 case Type::PointerTyID:
Chris Lattner3462ae32001-12-03 22:26:30 +000057 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2f7c9632001-06-06 20:29:01 +000058 default:
59 return 0;
60 }
61}
62
Chris Lattner3462ae32001-12-03 22:26:30 +000063void Constant::destroyConstantImpl() {
64 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000065 // references to the constant by other constants in the constant pool. These
66 // constants are implicitly dependant on the module that is being deleted,
67 // but they don't know that. Because we only find out when the CPV is
68 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000069 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000070 //
71 while (!use_empty()) {
72 Value *V = use_back();
73#ifndef NDEBUG // Only in -g mode...
Chris Lattner3462ae32001-12-03 22:26:30 +000074 if (!isa<Constant>(V)) {
Chris Lattnere026e922002-04-07 22:32:25 +000075 std::cerr << "While deleting: ";
76 dump();
77 std::cerr << "\nUse still stuck around after Def is destroyed: ";
78 V->dump();
79 std::cerr << "\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000080 }
81#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000082 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000083 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000084 CPV->destroyConstant();
85
86 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000087 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000088 }
89
90 // Value has no outstanding references it is safe to delete it now...
91 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000092}
Chris Lattner2f7c9632001-06-06 20:29:01 +000093
94//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000095// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +000096//===----------------------------------------------------------------------===//
97
98//===----------------------------------------------------------------------===//
99// Normal Constructors
100
Chris Lattner3462ae32001-12-03 22:26:30 +0000101ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000102 Val = V;
103}
Chris Lattner49d855c2001-09-07 16:46:31 +0000104
Chris Lattner3462ae32001-12-03 22:26:30 +0000105ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000106 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000107}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000108
Chris Lattner3462ae32001-12-03 22:26:30 +0000109ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000110 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000111}
112
Chris Lattner3462ae32001-12-03 22:26:30 +0000113ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000114 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115}
116
Chris Lattner3462ae32001-12-03 22:26:30 +0000117ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000118 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119 Val = V;
120}
121
Chris Lattner3462ae32001-12-03 22:26:30 +0000122ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000123 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000124 for (unsigned i = 0; i < V.size(); i++) {
125 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000126 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127 }
128}
129
Chris Lattner3462ae32001-12-03 22:26:30 +0000130ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000131 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000132 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000133 assert(V.size() == ETypes.size() &&
134 "Invalid initializer vector for constant structure");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000135 for (unsigned i = 0; i < V.size(); i++) {
136 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000137 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000138 }
139}
140
Chris Lattner3462ae32001-12-03 22:26:30 +0000141ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
142 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000143 Operands.push_back(Use(GV, this));
144}
145
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000146ConstantExpr::ConstantExpr(unsigned opCode, Constant *C, const Type *Ty)
147 : Constant(Ty), iType(opCode) {
148 Operands.push_back(Use(C, this));
149}
150
151ConstantExpr::ConstantExpr(unsigned opCode, Constant* C1,
152 Constant* C2, const Type *Ty)
153 : Constant(Ty), iType(opCode) {
154 Operands.push_back(Use(C1, this));
155 Operands.push_back(Use(C2, this));
156}
157
158ConstantExpr::ConstantExpr(unsigned opCode, Constant* C,
Vikram S. Adve4c485332002-07-15 18:19:33 +0000159 const std::vector<Value*>& IdxList, const Type *Ty)
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000160 : Constant(Ty), iType(opCode) {
161 Operands.reserve(1+IdxList.size());
162 Operands.push_back(Use(C, this));
163 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
164 Operands.push_back(Use(IdxList[i], this));
165}
166
Chris Lattner60e0dd72001-10-03 06:12:09 +0000167
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168
169//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000170// classof implementations
171
Chris Lattner3462ae32001-12-03 22:26:30 +0000172bool ConstantInt::classof(const Constant *CPV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000173 return CPV->getType()->isIntegral() && ! isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000174}
Chris Lattner3462ae32001-12-03 22:26:30 +0000175bool ConstantSInt::classof(const Constant *CPV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000176 return CPV->getType()->isSigned() && ! isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000177}
Chris Lattner3462ae32001-12-03 22:26:30 +0000178bool ConstantUInt::classof(const Constant *CPV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000179 return CPV->getType()->isUnsigned() && ! isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000180}
Chris Lattner3462ae32001-12-03 22:26:30 +0000181bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000182 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000183 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
184 ! isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000185}
Chris Lattner3462ae32001-12-03 22:26:30 +0000186bool ConstantArray::classof(const Constant *CPV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000187 return isa<ArrayType>(CPV->getType()) && ! isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000188}
Chris Lattner3462ae32001-12-03 22:26:30 +0000189bool ConstantStruct::classof(const Constant *CPV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000190 return isa<StructType>(CPV->getType()) && ! isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000191}
Chris Lattner3462ae32001-12-03 22:26:30 +0000192bool ConstantPointer::classof(const Constant *CPV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000193 return (isa<PointerType>(CPV->getType()) && ! isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000194}
195
196
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000197
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000199// isValueValidForType implementations
200
Chris Lattner3462ae32001-12-03 22:26:30 +0000201bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000202 switch (Ty->getPrimitiveID()) {
203 default:
204 return false; // These can't be represented as integers!!!
205
206 // Signed types...
207 case Type::SByteTyID:
208 return (Val <= INT8_MAX && Val >= INT8_MIN);
209 case Type::ShortTyID:
210 return (Val <= INT16_MAX && Val >= INT16_MIN);
211 case Type::IntTyID:
212 return (Val <= INT32_MAX && Val >= INT32_MIN);
213 case Type::LongTyID:
214 return true; // This is the largest type...
215 }
216 assert(0 && "WTF?");
217 return false;
218}
219
Chris Lattner3462ae32001-12-03 22:26:30 +0000220bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221 switch (Ty->getPrimitiveID()) {
222 default:
223 return false; // These can't be represented as integers!!!
224
225 // Unsigned types...
226 case Type::UByteTyID:
227 return (Val <= UINT8_MAX);
228 case Type::UShortTyID:
229 return (Val <= UINT16_MAX);
230 case Type::UIntTyID:
231 return (Val <= UINT32_MAX);
232 case Type::ULongTyID:
233 return true; // This is the largest type...
234 }
235 assert(0 && "WTF?");
236 return false;
237}
238
Chris Lattner3462ae32001-12-03 22:26:30 +0000239bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000240 switch (Ty->getPrimitiveID()) {
241 default:
242 return false; // These can't be represented as floating point!
243
244 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000245 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000246 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000247 return (Val <= UINT8_MAX);
248 */
249 case Type::DoubleTyID:
250 return true; // This is the largest type...
251 }
252};
Chris Lattner9655e542001-07-20 19:16:02 +0000253
Chris Lattner49d855c2001-09-07 16:46:31 +0000254//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000255// Factory Function Implementation
256
Chris Lattner3462ae32001-12-03 22:26:30 +0000257template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000258struct ValueMap {
259 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000260 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000261
Chris Lattner3462ae32001-12-03 22:26:30 +0000262 inline ConstantClass *get(const Type *Ty, ValType V) {
263 map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000264 Map.find(ConstHashKey(Ty, V));
265 return (I != Map.end()) ? I->second : 0;
266 }
267
Chris Lattner3462ae32001-12-03 22:26:30 +0000268 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000269 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
270 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000271
Chris Lattner3462ae32001-12-03 22:26:30 +0000272 inline void remove(ConstantClass *CP) {
273 for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000274 E = Map.end(); I != E;++I)
275 if (I->second == CP) {
276 Map.erase(I);
277 return;
278 }
279 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000280};
281
Chris Lattner3462ae32001-12-03 22:26:30 +0000282//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000283//
Chris Lattner3462ae32001-12-03 22:26:30 +0000284static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000285
Chris Lattner3462ae32001-12-03 22:26:30 +0000286ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
287 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000288 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000289 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000290 return Result;
291}
292
Chris Lattner3462ae32001-12-03 22:26:30 +0000293ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
294 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000295 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000296 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000297 return Result;
298}
299
Chris Lattner3462ae32001-12-03 22:26:30 +0000300ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000301 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000302 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
303 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000304}
305
Chris Lattner3462ae32001-12-03 22:26:30 +0000306//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000307//
Chris Lattner3462ae32001-12-03 22:26:30 +0000308static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000309
Chris Lattner3462ae32001-12-03 22:26:30 +0000310ConstantFP *ConstantFP::get(const Type *Ty, double V) {
311 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000312 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000313 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000314 return Result;
315}
316
Chris Lattner3462ae32001-12-03 22:26:30 +0000317//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000318//
Chris Lattner7f74a562002-01-20 22:54:45 +0000319static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000320
Chris Lattner3462ae32001-12-03 22:26:30 +0000321ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000322 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000323 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000324 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000325 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000326 return Result;
327}
328
Chris Lattner3462ae32001-12-03 22:26:30 +0000329// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000330// contain the specified string. A null terminator is added to the specified
331// string so that it may be used in a natural way...
332//
Chris Lattner7f74a562002-01-20 22:54:45 +0000333ConstantArray *ConstantArray::get(const std::string &Str) {
334 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000335
336 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000337 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000338
339 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000340 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000341
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000342 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000343 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000344}
345
346
Chris Lattnerd7a73302001-10-13 06:57:33 +0000347// destroyConstant - Remove the constant from the constant table...
348//
Chris Lattner3462ae32001-12-03 22:26:30 +0000349void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000350 ArrayConstants.remove(this);
351 destroyConstantImpl();
352}
353
Chris Lattner3462ae32001-12-03 22:26:30 +0000354//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000355//
Chris Lattner7f74a562002-01-20 22:54:45 +0000356static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000357
Chris Lattner3462ae32001-12-03 22:26:30 +0000358ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000359 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000360 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000361 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000362 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000363 return Result;
364}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000365
Chris Lattnerd7a73302001-10-13 06:57:33 +0000366// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000367//
Chris Lattner3462ae32001-12-03 22:26:30 +0000368void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000369 StructConstants.remove(this);
370 destroyConstantImpl();
371}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000372
Chris Lattner3462ae32001-12-03 22:26:30 +0000373//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000374//
Chris Lattner3462ae32001-12-03 22:26:30 +0000375static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000376
Chris Lattner3462ae32001-12-03 22:26:30 +0000377ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
378 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000379 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000380 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000381 return Result;
382}
383
Chris Lattner3462ae32001-12-03 22:26:30 +0000384//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000385//
Chris Lattner3462ae32001-12-03 22:26:30 +0000386ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000387 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000388
Chris Lattnerd7a73302001-10-13 06:57:33 +0000389 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000390 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000391}
392
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000393//---- ConstantExpr::get() implementations...
394// Return NULL on invalid expressions.
395//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000396typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
397static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
398
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000399ConstantExpr*
400ConstantExpr::get(unsigned opCode, Constant *C, const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000401
402 // Look up the constant in the table first to ensure uniqueness
403 vector<Constant*> argVec(1, C);
404 const ExprMapKeyType& key = make_pair(opCode, argVec);
405 ConstantExpr* result = ExprConstants.get(Ty, key);
406 if (result)
407 return result;
408
409 // Its not in the table so create a new one and put it in the table.
410 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000411 if (opCode != Instruction::Cast &&
412 (opCode < Instruction::FirstUnaryOp ||
413 opCode >= Instruction::NumUnaryOps)) {
Anand Shukla991873f2002-07-16 00:02:17 +0000414 std::cerr << "Invalid opcode " << ConstantExpr::getOpcodeName(opCode)
415 << " in unary constant expression" << std::endl;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000416 return NULL; // Not Cast or other unary opcode
417 }
418 // type of operand will not match result for Cast operation
419 if (opCode != Instruction::Cast && Ty != C->getType()) {
420 cerr << "Type of operand in unary constant expression should match result" << endl;
421 return NULL;
422 }
Vikram S. Adve4c485332002-07-15 18:19:33 +0000423
424 result = new ConstantExpr(opCode, C, Ty);
425 ExprConstants.add(Ty, key, result);
426 return result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000427}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000428
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000429ConstantExpr*
430ConstantExpr::get(unsigned opCode, Constant *C1, Constant *C2,const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000431
432 // Look up the constant in the table first to ensure uniqueness
433 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
434 const ExprMapKeyType& key = make_pair(opCode, argVec);
435 ConstantExpr* result = ExprConstants.get(Ty, key);
436 if (result)
437 return result;
438
439 // Its not in the table so create a new one and put it in the table.
440 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000441 if (opCode < Instruction::FirstBinaryOp ||
442 opCode >= Instruction::NumBinaryOps) {
443 cerr << "Invalid opcode " << ConstantExpr::getOpcodeName(opCode)
444 << " in binary constant expression" << endl;
445 return NULL;
446 }
447 if (Ty != C1->getType() || Ty != C2->getType()) {
448 cerr << "Types of both operands in binary constant expression should match result" << endl;
449 return NULL;
450 }
Vikram S. Adve4c485332002-07-15 18:19:33 +0000451
452 result = new ConstantExpr(opCode, C1, C2, Ty);
453 ExprConstants.add(Ty, key, result);
454 return result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000455}
456
457ConstantExpr*
458ConstantExpr::get(unsigned opCode, Constant*C,
Vikram S. Adve4c485332002-07-15 18:19:33 +0000459 const std::vector<Value*>& idxList, const Type *Ty) {
460
461 // Look up the constant in the table first to ensure uniqueness
462 vector<Constant*> argVec(1, C);
463 for(vector<Value*>::const_iterator VI=idxList.begin(), VE=idxList.end();
464 VI != VE; ++VI)
465 if (Constant *C = dyn_cast<Constant>(*VI))
466 argVec.push_back(C);
467 else {
468 cerr << "Non-constant index in constant GetElementPtr expr";
469 return NULL;
470 }
471
472 const ExprMapKeyType& key = make_pair(opCode, argVec);
473 ConstantExpr* result = ExprConstants.get(Ty, key);
474 if (result)
475 return result;
476
477 // Its not in the table so create a new one and put it in the table.
478 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000479 // Must be a getElementPtr. Check for valid getElementPtr expression.
480 //
481 if (opCode != Instruction::GetElementPtr) {
482 cerr << "operator other than GetElementPtr used with an index list" << endl;
483 return NULL;
484 }
485 if (!isa<ConstantPointer>(C)) {
486 cerr << "Constant GelElementPtr expression using something other than a constant pointer" << endl;
487 return NULL;
488 }
489 if (!isa<PointerType>(Ty)) {
490 cerr << "Non-pointer type for constant GelElementPtr expression" << endl;
491 return NULL;
492 }
493 const Type* fldType = GetElementPtrInst::getIndexedType(C->getType(),
494 idxList, true);
495 if (!fldType) {
496 cerr << "Invalid index list for constant GelElementPtr expression" << endl;
497 return NULL;
498 }
499 if (cast<PointerType>(Ty)->getElementType() != fldType) {
500 cerr << "Type for constant GelElementPtr expression does not match field type" << endl;
501 return NULL;
502 }
503
Vikram S. Adve4c485332002-07-15 18:19:33 +0000504 result = new ConstantExpr(opCode, C, idxList, Ty);
505 ExprConstants.add(Ty, key, result);
506 return result;
507}
508
509// destroyConstant - Remove the constant from the constant table...
510//
511void ConstantExpr::destroyConstant() {
512 ExprConstants.remove(this);
513 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000514}
515
516const char*
517ConstantExpr::getOpcodeName(unsigned opCode) {
518 return Instruction::getOpcodeName(opCode);
519}
520
521
522//---- ConstantPointerRef::mutateReferences() implementation...
523//
524unsigned
525ConstantPointerRef::mutateReferences(Value* OldV, Value *NewV) {
526 assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
527 GlobalValue* NewGV = cast<GlobalValue>(NewV);
Chris Lattner3462ae32001-12-03 22:26:30 +0000528 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000529 Operands[0] = NewGV;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000530 return 1;
531}
532
533
534//---- ConstantPointerExpr::mutateReferences() implementation...
535//
536unsigned
537ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
538 unsigned numReplaced = 0;
539 Constant* NewC = cast<Constant>(NewV);
540 for (unsigned i=0, N = getNumOperands(); i < N; ++i)
541 if (Operands[i] == OldV) {
542 ++numReplaced;
543 Operands[i] = NewC;
544 }
545 return numReplaced;
Chris Lattner25033252001-10-03 19:28:15 +0000546}