blob: 856cd1ab16ebfa3adbcde028a0a9461263af0679 [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 Lattner41e99a02002-08-12 21:21:21 +000096ConstantBool::ConstantBool(bool V) : ConstantGenericIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000097 Val = V;
98}
Chris Lattner49d855c2001-09-07 16:46:31 +000099
Chris Lattner41e99a02002-08-12 21:21:21 +0000100ConstantInt::ConstantInt(const Type *Ty, uint64_t V)
101 : ConstantGenericIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000102 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000103}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000104
Chris Lattner3462ae32001-12-03 22:26:30 +0000105ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner9655e542001-07-20 19:16:02 +0000106 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000107}
108
Chris Lattner3462ae32001-12-03 22:26:30 +0000109ConstantUInt::ConstantUInt(const Type *Ty, uint64_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 +0000113ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
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 Val = V;
116}
117
Chris Lattner3462ae32001-12-03 22:26:30 +0000118ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000119 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120 for (unsigned i = 0; i < V.size(); i++) {
121 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000122 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000123 }
124}
125
Chris Lattner3462ae32001-12-03 22:26:30 +0000126ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000127 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000128 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000129 assert(V.size() == ETypes.size() &&
130 "Invalid initializer vector for constant structure");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000131 for (unsigned i = 0; i < V.size(); i++) {
132 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000133 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000134 }
135}
136
Chris Lattner3462ae32001-12-03 22:26:30 +0000137ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
138 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000139 Operands.push_back(Use(GV, this));
140}
141
Chris Lattner3cd8c562002-07-30 18:54:25 +0000142ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
143 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000144 Operands.push_back(Use(C, this));
145}
146
Chris Lattner3cd8c562002-07-30 18:54:25 +0000147ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
148 : Constant(C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000149 Operands.push_back(Use(C1, this));
150 Operands.push_back(Use(C2, this));
151}
152
Chris Lattner3cd8c562002-07-30 18:54:25 +0000153ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
154 const Type *DestTy)
155 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000156 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 Lattner41e99a02002-08-12 21:21:21 +0000167bool ConstantGenericIntegral::classof(const Constant *CPV) {
168 return (CPV->getType()->isIntegral() || CPV->getType() == Type::BoolTy) &&
169 !isa<ConstantExpr>(CPV);
170}
171
Chris Lattner3462ae32001-12-03 22:26:30 +0000172bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +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) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +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) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +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) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000184 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000185}
Chris Lattner3462ae32001-12-03 22:26:30 +0000186bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +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) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +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) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +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) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000263 typename 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) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000273 for (typename 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...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000394//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000395typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
396static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
397
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000398ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C, const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000399
400 // Look up the constant in the table first to ensure uniqueness
401 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000402 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
403 ConstantExpr *Result = ExprConstants.get(Ty, Key);
404 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000405
406 // Its not in the table so create a new one and put it in the table.
407 // Check the operands for consistency first
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000408 assert(Opcode == Instruction::Cast ||
409 (Opcode >= Instruction::FirstUnaryOp &&
410 Opcode < Instruction::NumUnaryOps) &&
411 "Invalid opcode in unary ConstantExpr!");
412
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000413 // type of operand will not match result for Cast operation
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000414 assert((Opcode == Instruction::Cast || Ty == C->getType()) &&
415 "Type of operand in unary constant expression should match result");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000416
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000417 Result = new ConstantExpr(Opcode, C, Ty);
418 ExprConstants.add(Ty, Key, Result);
419 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000420}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000421
Chris Lattner3cd8c562002-07-30 18:54:25 +0000422ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000423 // Look up the constant in the table first to ensure uniqueness
424 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000425 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
Chris Lattner3cd8c562002-07-30 18:54:25 +0000426 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000427 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000428
429 // Its not in the table so create a new one and put it in the table.
430 // Check the operands for consistency first
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000431 assert((Opcode >= Instruction::FirstBinaryOp &&
432 Opcode < Instruction::NumBinaryOps) &&
Chris Lattner3cd8c562002-07-30 18:54:25 +0000433 "Invalid opcode in binary constant expression");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000434
Chris Lattner3cd8c562002-07-30 18:54:25 +0000435 assert(C1->getType() == C2->getType() &&
436 "Operand types in binary constant expression should match");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000437
Chris Lattner3cd8c562002-07-30 18:54:25 +0000438 Result = new ConstantExpr(Opcode, C1, C2);
439 ExprConstants.add(C1->getType(), Key, Result);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000440 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000441}
442
Chris Lattner3cd8c562002-07-30 18:54:25 +0000443ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
444 const std::vector<Constant*> &IdxList) {
445 const Type *Ty = C->getType();
Vikram S. Adve4c485332002-07-15 18:19:33 +0000446
447 // Look up the constant in the table first to ensure uniqueness
448 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000449 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000450
Chris Lattner3cd8c562002-07-30 18:54:25 +0000451 const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000452 ConstantExpr *Result = ExprConstants.get(Ty, Key);
453 if (Result) return Result;
Chris Lattner3cd8c562002-07-30 18:54:25 +0000454
Vikram S. Adve4c485332002-07-15 18:19:33 +0000455 // Its not in the table so create a new one and put it in the table.
456 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000457 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000458 assert(isa<PointerType>(Ty) &&
459 "Non-pointer type for constant GelElementPtr expression");
460
Chris Lattner3cd8c562002-07-30 18:54:25 +0000461 // Check that the indices list is valid...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000462 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
Chris Lattner3cd8c562002-07-30 18:54:25 +0000463 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
464 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000465
Chris Lattner3cd8c562002-07-30 18:54:25 +0000466 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000467 ExprConstants.add(Ty, Key, Result);
468 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000469}
470
471// destroyConstant - Remove the constant from the constant table...
472//
473void ConstantExpr::destroyConstant() {
474 ExprConstants.remove(this);
475 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000476}
477
Chris Lattner3cd8c562002-07-30 18:54:25 +0000478const char *ConstantExpr::getOpcodeName() const {
479 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000480}
481
482
483//---- ConstantPointerRef::mutateReferences() implementation...
484//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000485unsigned ConstantPointerRef::mutateReferences(Value *OldV, Value *NewV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000486 assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000487 GlobalValue *NewGV = cast<GlobalValue>(NewV);
Chris Lattner3462ae32001-12-03 22:26:30 +0000488 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000489 Operands[0] = NewGV;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000490 return 1;
491}
492
493
494//---- ConstantPointerExpr::mutateReferences() implementation...
495//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000496unsigned ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
497 unsigned NumReplaced = 0;
498 Constant *NewC = cast<Constant>(NewV);
499 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000500 if (Operands[i] == OldV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000501 ++NumReplaced;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000502 Operands[i] = NewC;
503 }
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000504 return NumReplaced;
Chris Lattner25033252001-10-03 19:28:15 +0000505}