blob: faef1473da087cfb8bf41ff62cf25d0e27adf291 [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
Chris Lattner3cd8c562002-07-30 18:54:25 +0000141ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
142 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000143 Operands.push_back(Use(C, this));
144}
145
Chris Lattner3cd8c562002-07-30 18:54:25 +0000146ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
147 : Constant(C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000148 Operands.push_back(Use(C1, this));
149 Operands.push_back(Use(C2, this));
150}
151
Chris Lattner3cd8c562002-07-30 18:54:25 +0000152ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
153 const Type *DestTy)
154 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000155 Operands.reserve(1+IdxList.size());
156 Operands.push_back(Use(C, this));
157 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
158 Operands.push_back(Use(IdxList[i], this));
159}
160
Chris Lattner60e0dd72001-10-03 06:12:09 +0000161
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162
163//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000164// classof implementations
165
Chris Lattner3462ae32001-12-03 22:26:30 +0000166bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000167 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000168}
Chris Lattner3462ae32001-12-03 22:26:30 +0000169bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000170 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000171}
Chris Lattner3462ae32001-12-03 22:26:30 +0000172bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000173 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000174}
Chris Lattner3462ae32001-12-03 22:26:30 +0000175bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000176 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000177 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000178 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000179}
Chris Lattner3462ae32001-12-03 22:26:30 +0000180bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000181 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000182}
Chris Lattner3462ae32001-12-03 22:26:30 +0000183bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000184 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000185}
Chris Lattner3462ae32001-12-03 22:26:30 +0000186bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000187 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000188}
189
190
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000191
Chris Lattner2f7c9632001-06-06 20:29:01 +0000192//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000193// isValueValidForType implementations
194
Chris Lattner3462ae32001-12-03 22:26:30 +0000195bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196 switch (Ty->getPrimitiveID()) {
197 default:
198 return false; // These can't be represented as integers!!!
199
200 // Signed types...
201 case Type::SByteTyID:
202 return (Val <= INT8_MAX && Val >= INT8_MIN);
203 case Type::ShortTyID:
204 return (Val <= INT16_MAX && Val >= INT16_MIN);
205 case Type::IntTyID:
206 return (Val <= INT32_MAX && Val >= INT32_MIN);
207 case Type::LongTyID:
208 return true; // This is the largest type...
209 }
210 assert(0 && "WTF?");
211 return false;
212}
213
Chris Lattner3462ae32001-12-03 22:26:30 +0000214bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215 switch (Ty->getPrimitiveID()) {
216 default:
217 return false; // These can't be represented as integers!!!
218
219 // Unsigned types...
220 case Type::UByteTyID:
221 return (Val <= UINT8_MAX);
222 case Type::UShortTyID:
223 return (Val <= UINT16_MAX);
224 case Type::UIntTyID:
225 return (Val <= UINT32_MAX);
226 case Type::ULongTyID:
227 return true; // This is the largest type...
228 }
229 assert(0 && "WTF?");
230 return false;
231}
232
Chris Lattner3462ae32001-12-03 22:26:30 +0000233bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000234 switch (Ty->getPrimitiveID()) {
235 default:
236 return false; // These can't be represented as floating point!
237
238 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000239 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000240 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000241 return (Val <= UINT8_MAX);
242 */
243 case Type::DoubleTyID:
244 return true; // This is the largest type...
245 }
246};
Chris Lattner9655e542001-07-20 19:16:02 +0000247
Chris Lattner49d855c2001-09-07 16:46:31 +0000248//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000249// Factory Function Implementation
250
Chris Lattner3462ae32001-12-03 22:26:30 +0000251template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000252struct ValueMap {
253 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000254 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000255
Chris Lattner3462ae32001-12-03 22:26:30 +0000256 inline ConstantClass *get(const Type *Ty, ValType V) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000257 typename map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000258 Map.find(ConstHashKey(Ty, V));
259 return (I != Map.end()) ? I->second : 0;
260 }
261
Chris Lattner3462ae32001-12-03 22:26:30 +0000262 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000263 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
264 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000265
Chris Lattner3462ae32001-12-03 22:26:30 +0000266 inline void remove(ConstantClass *CP) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000267 for (typename map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000268 E = Map.end(); I != E;++I)
269 if (I->second == CP) {
270 Map.erase(I);
271 return;
272 }
273 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000274};
275
Chris Lattner3462ae32001-12-03 22:26:30 +0000276//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000277//
Chris Lattner3462ae32001-12-03 22:26:30 +0000278static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000279
Chris Lattner3462ae32001-12-03 22:26:30 +0000280ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
281 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000282 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000283 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000284 return Result;
285}
286
Chris Lattner3462ae32001-12-03 22:26:30 +0000287ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
288 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000289 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000290 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000291 return Result;
292}
293
Chris Lattner3462ae32001-12-03 22:26:30 +0000294ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000295 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000296 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
297 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000298}
299
Chris Lattner3462ae32001-12-03 22:26:30 +0000300//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000301//
Chris Lattner3462ae32001-12-03 22:26:30 +0000302static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000303
Chris Lattner3462ae32001-12-03 22:26:30 +0000304ConstantFP *ConstantFP::get(const Type *Ty, double V) {
305 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000306 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000307 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000308 return Result;
309}
310
Chris Lattner3462ae32001-12-03 22:26:30 +0000311//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000312//
Chris Lattner7f74a562002-01-20 22:54:45 +0000313static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000314
Chris Lattner3462ae32001-12-03 22:26:30 +0000315ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000316 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000317 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000318 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000319 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000320 return Result;
321}
322
Chris Lattner3462ae32001-12-03 22:26:30 +0000323// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000324// contain the specified string. A null terminator is added to the specified
325// string so that it may be used in a natural way...
326//
Chris Lattner7f74a562002-01-20 22:54:45 +0000327ConstantArray *ConstantArray::get(const std::string &Str) {
328 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000329
330 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000331 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000332
333 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000334 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000335
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000336 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000337 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000338}
339
340
Chris Lattnerd7a73302001-10-13 06:57:33 +0000341// destroyConstant - Remove the constant from the constant table...
342//
Chris Lattner3462ae32001-12-03 22:26:30 +0000343void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000344 ArrayConstants.remove(this);
345 destroyConstantImpl();
346}
347
Chris Lattner3462ae32001-12-03 22:26:30 +0000348//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000349//
Chris Lattner7f74a562002-01-20 22:54:45 +0000350static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000351
Chris Lattner3462ae32001-12-03 22:26:30 +0000352ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000353 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000354 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000355 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000356 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000357 return Result;
358}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000359
Chris Lattnerd7a73302001-10-13 06:57:33 +0000360// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000361//
Chris Lattner3462ae32001-12-03 22:26:30 +0000362void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000363 StructConstants.remove(this);
364 destroyConstantImpl();
365}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000366
Chris Lattner3462ae32001-12-03 22:26:30 +0000367//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000368//
Chris Lattner3462ae32001-12-03 22:26:30 +0000369static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000370
Chris Lattner3462ae32001-12-03 22:26:30 +0000371ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
372 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000373 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000374 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000375 return Result;
376}
377
Chris Lattner3462ae32001-12-03 22:26:30 +0000378//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000379//
Chris Lattner3462ae32001-12-03 22:26:30 +0000380ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000381 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000382
Chris Lattnerd7a73302001-10-13 06:57:33 +0000383 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000384 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000385}
386
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000387//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000388//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000389typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
390static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
391
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000392ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C, const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000393
394 // Look up the constant in the table first to ensure uniqueness
395 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000396 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
397 ConstantExpr *Result = ExprConstants.get(Ty, Key);
398 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000399
400 // Its not in the table so create a new one and put it in the table.
401 // Check the operands for consistency first
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000402 assert(Opcode == Instruction::Cast ||
403 (Opcode >= Instruction::FirstUnaryOp &&
404 Opcode < Instruction::NumUnaryOps) &&
405 "Invalid opcode in unary ConstantExpr!");
406
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000407 // type of operand will not match result for Cast operation
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000408 assert((Opcode == Instruction::Cast || Ty == C->getType()) &&
409 "Type of operand in unary constant expression should match result");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000410
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000411 Result = new ConstantExpr(Opcode, C, Ty);
412 ExprConstants.add(Ty, Key, Result);
413 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000414}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000415
Chris Lattner3cd8c562002-07-30 18:54:25 +0000416ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000417 // Look up the constant in the table first to ensure uniqueness
418 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000419 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
Chris Lattner3cd8c562002-07-30 18:54:25 +0000420 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000421 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000422
423 // Its not in the table so create a new one and put it in the table.
424 // Check the operands for consistency first
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000425 assert((Opcode >= Instruction::FirstBinaryOp &&
426 Opcode < Instruction::NumBinaryOps) &&
Chris Lattner3cd8c562002-07-30 18:54:25 +0000427 "Invalid opcode in binary constant expression");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000428
Chris Lattner3cd8c562002-07-30 18:54:25 +0000429 assert(C1->getType() == C2->getType() &&
430 "Operand types in binary constant expression should match");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000431
Chris Lattner3cd8c562002-07-30 18:54:25 +0000432 Result = new ConstantExpr(Opcode, C1, C2);
433 ExprConstants.add(C1->getType(), Key, Result);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000434 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000435}
436
Chris Lattner3cd8c562002-07-30 18:54:25 +0000437ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
438 const std::vector<Constant*> &IdxList) {
439 const Type *Ty = C->getType();
Vikram S. Adve4c485332002-07-15 18:19:33 +0000440
441 // Look up the constant in the table first to ensure uniqueness
442 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000443 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000444
Chris Lattner3cd8c562002-07-30 18:54:25 +0000445 const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000446 ConstantExpr *Result = ExprConstants.get(Ty, Key);
447 if (Result) return Result;
Chris Lattner3cd8c562002-07-30 18:54:25 +0000448
Vikram S. Adve4c485332002-07-15 18:19:33 +0000449 // Its not in the table so create a new one and put it in the table.
450 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000451 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000452 assert(isa<PointerType>(Ty) &&
453 "Non-pointer type for constant GelElementPtr expression");
454
Chris Lattner3cd8c562002-07-30 18:54:25 +0000455 // Check that the indices list is valid...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000456 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
Chris Lattner3cd8c562002-07-30 18:54:25 +0000457 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
458 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000459
Chris Lattner3cd8c562002-07-30 18:54:25 +0000460 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000461 ExprConstants.add(Ty, Key, Result);
462 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000463}
464
465// destroyConstant - Remove the constant from the constant table...
466//
467void ConstantExpr::destroyConstant() {
468 ExprConstants.remove(this);
469 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000470}
471
Chris Lattner3cd8c562002-07-30 18:54:25 +0000472const char *ConstantExpr::getOpcodeName() const {
473 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000474}
475
476
477//---- ConstantPointerRef::mutateReferences() implementation...
478//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000479unsigned ConstantPointerRef::mutateReferences(Value *OldV, Value *NewV) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000480 assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000481 GlobalValue *NewGV = cast<GlobalValue>(NewV);
Chris Lattner3462ae32001-12-03 22:26:30 +0000482 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000483 Operands[0] = NewGV;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000484 return 1;
485}
486
487
488//---- ConstantPointerExpr::mutateReferences() implementation...
489//
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000490unsigned ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
491 unsigned NumReplaced = 0;
492 Constant *NewC = cast<Constant>(NewV);
493 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000494 if (Operands[i] == OldV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000495 ++NumReplaced;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000496 Operands[i] = NewC;
497 }
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000498 return NumReplaced;
Chris Lattner25033252001-10-03 19:28:15 +0000499}