1 //===-- Constants.cpp - Implement Constant nodes -----------------*- C++ -*--=//
3 // This file implements the Constant* classes...
5 //===----------------------------------------------------------------------===//
7 #define __STDC_LIMIT_MACROS // Get defs for INT64_MAX and friends...
8 #include "llvm/Constants.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/SymbolTable.h"
11 #include "llvm/Module.h"
12 #include "llvm/SlotCalculator.h"
13 #include "Support/StringExtras.h"
20 ConstantBool *ConstantBool::True = new ConstantBool(true);
21 ConstantBool *ConstantBool::False = new ConstantBool(false);
24 //===----------------------------------------------------------------------===//
26 //===----------------------------------------------------------------------===//
28 // Specialize setName to take care of symbol table majik
29 void Constant::setName(const std::string &Name, SymbolTable *ST) {
30 assert(ST && "Type::setName - Must provide symbol table argument!");
32 if (Name.size()) ST->insert(Name, this);
35 // Static constructor to create a '0' constant of arbitrary type...
36 Constant *Constant::getNullValue(const Type *Ty) {
37 switch (Ty->getPrimitiveID()) {
38 case Type::BoolTyID: return ConstantBool::get(false);
42 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
45 case Type::UShortTyID:
47 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
50 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
52 case Type::PointerTyID:
53 return ConstantPointerNull::get(cast<PointerType>(Ty));
59 void Constant::destroyConstantImpl() {
60 // When a Constant is destroyed, there may be lingering
61 // references to the constant by other constants in the constant pool. These
62 // constants are implicitly dependant on the module that is being deleted,
63 // but they don't know that. Because we only find out when the CPV is
64 // deleted, we must now notify all of our users (that should only be
65 // Constants) that they are, in fact, invalid now and should be deleted.
67 while (!use_empty()) {
68 Value *V = use_back();
69 #ifndef NDEBUG // Only in -g mode...
70 if (!isa<Constant>(V)) {
71 std::cerr << "While deleting: ";
73 std::cerr << "\nUse still stuck around after Def is destroyed: ";
78 assert(isa<Constant>(V) && "References remain to ConstantPointerRef!");
79 Constant *CPV = cast<Constant>(V);
80 CPV->destroyConstant();
82 // The constant should remove itself from our use list...
83 assert((use_empty() || use_back() == V) && "Constant not removed!");
86 // Value has no outstanding references it is safe to delete it now...
90 //===----------------------------------------------------------------------===//
91 // ConstantXXX Classes
92 //===----------------------------------------------------------------------===//
94 //===----------------------------------------------------------------------===//
95 // Normal Constructors
97 ConstantBool::ConstantBool(bool V) : Constant(Type::BoolTy) {
101 ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : Constant(Ty) {
105 ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
106 assert(isValueValidForType(Ty, V) && "Value too large for type!");
109 ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
110 assert(isValueValidForType(Ty, V) && "Value too large for type!");
113 ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
114 assert(isValueValidForType(Ty, V) && "Value too large for type!");
118 ConstantArray::ConstantArray(const ArrayType *T,
119 const std::vector<Constant*> &V) : Constant(T) {
120 for (unsigned i = 0; i < V.size(); i++) {
121 assert(V[i]->getType() == T->getElementType());
122 Operands.push_back(Use(V[i], this));
126 ConstantStruct::ConstantStruct(const StructType *T,
127 const std::vector<Constant*> &V) : Constant(T) {
128 const StructType::ElementTypes &ETypes = T->getElementTypes();
130 for (unsigned i = 0; i < V.size(); i++) {
131 assert(V[i]->getType() == ETypes[i]);
132 Operands.push_back(Use(V[i], this));
136 ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
137 : ConstantPointer(GV->getType()) {
138 Operands.push_back(Use(GV, this));
143 //===----------------------------------------------------------------------===//
144 // classof implementations
146 bool ConstantInt::classof(const Constant *CPV) {
147 return CPV->getType()->isIntegral();
149 bool ConstantSInt::classof(const Constant *CPV) {
150 return CPV->getType()->isSigned();
152 bool ConstantUInt::classof(const Constant *CPV) {
153 return CPV->getType()->isUnsigned();
155 bool ConstantFP::classof(const Constant *CPV) {
156 const Type *Ty = CPV->getType();
157 return Ty == Type::FloatTy || Ty == Type::DoubleTy;
159 bool ConstantArray::classof(const Constant *CPV) {
160 return isa<ArrayType>(CPV->getType());
162 bool ConstantStruct::classof(const Constant *CPV) {
163 return isa<StructType>(CPV->getType());
165 bool ConstantPointer::classof(const Constant *CPV) {
166 return isa<PointerType>(CPV->getType());
170 //===----------------------------------------------------------------------===//
171 // isValueValidForType implementations
173 bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
174 switch (Ty->getPrimitiveID()) {
176 return false; // These can't be represented as integers!!!
179 case Type::SByteTyID:
180 return (Val <= INT8_MAX && Val >= INT8_MIN);
181 case Type::ShortTyID:
182 return (Val <= INT16_MAX && Val >= INT16_MIN);
184 return (Val <= INT32_MAX && Val >= INT32_MIN);
186 return true; // This is the largest type...
192 bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
193 switch (Ty->getPrimitiveID()) {
195 return false; // These can't be represented as integers!!!
198 case Type::UByteTyID:
199 return (Val <= UINT8_MAX);
200 case Type::UShortTyID:
201 return (Val <= UINT16_MAX);
203 return (Val <= UINT32_MAX);
204 case Type::ULongTyID:
205 return true; // This is the largest type...
211 bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
212 switch (Ty->getPrimitiveID()) {
214 return false; // These can't be represented as floating point!
216 // TODO: Figure out how to test if a double can be cast to a float!
217 case Type::FloatTyID:
219 return (Val <= UINT8_MAX);
221 case Type::DoubleTyID:
222 return true; // This is the largest type...
226 //===----------------------------------------------------------------------===//
227 // Factory Function Implementation
229 template<class ValType, class ConstantClass>
231 typedef pair<const Type*, ValType> ConstHashKey;
232 map<ConstHashKey, ConstantClass *> Map;
234 inline ConstantClass *get(const Type *Ty, ValType V) {
235 map<ConstHashKey,ConstantClass *>::iterator I =
236 Map.find(ConstHashKey(Ty, V));
237 return (I != Map.end()) ? I->second : 0;
240 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
241 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
244 inline void remove(ConstantClass *CP) {
245 for (map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
246 E = Map.end(); I != E;++I)
247 if (I->second == CP) {
254 //---- ConstantUInt::get() and ConstantSInt::get() implementations...
256 static ValueMap<uint64_t, ConstantInt> IntConstants;
258 ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
259 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
260 if (!Result) // If no preexisting value, create one now...
261 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
265 ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
266 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
267 if (!Result) // If no preexisting value, create one now...
268 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
272 ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
273 assert(V <= 127 && "Can only be used with very small positive constants!");
274 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
275 return ConstantUInt::get(Ty, V);
278 //---- ConstantFP::get() implementation...
280 static ValueMap<double, ConstantFP> FPConstants;
282 ConstantFP *ConstantFP::get(const Type *Ty, double V) {
283 ConstantFP *Result = FPConstants.get(Ty, V);
284 if (!Result) // If no preexisting value, create one now...
285 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
289 //---- ConstantArray::get() implementation...
291 static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
293 ConstantArray *ConstantArray::get(const ArrayType *Ty,
294 const std::vector<Constant*> &V) {
295 ConstantArray *Result = ArrayConstants.get(Ty, V);
296 if (!Result) // If no preexisting value, create one now...
297 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
301 // ConstantArray::get(const string&) - Return an array that is initialized to
302 // contain the specified string. A null terminator is added to the specified
303 // string so that it may be used in a natural way...
305 ConstantArray *ConstantArray::get(const std::string &Str) {
306 std::vector<Constant*> ElementVals;
308 for (unsigned i = 0; i < Str.length(); ++i)
309 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
311 // Add a null terminator to the string...
312 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
314 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
315 return ConstantArray::get(ATy, ElementVals);
319 // destroyConstant - Remove the constant from the constant table...
321 void ConstantArray::destroyConstant() {
322 ArrayConstants.remove(this);
323 destroyConstantImpl();
326 //---- ConstantStruct::get() implementation...
328 static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
330 ConstantStruct *ConstantStruct::get(const StructType *Ty,
331 const std::vector<Constant*> &V) {
332 ConstantStruct *Result = StructConstants.get(Ty, V);
333 if (!Result) // If no preexisting value, create one now...
334 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
338 // destroyConstant - Remove the constant from the constant table...
340 void ConstantStruct::destroyConstant() {
341 StructConstants.remove(this);
342 destroyConstantImpl();
345 //---- ConstantPointerNull::get() implementation...
347 static ValueMap<char, ConstantPointerNull> NullPtrConstants;
349 ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
350 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
351 if (!Result) // If no preexisting value, create one now...
352 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
356 //---- ConstantPointerRef::get() implementation...
358 ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
359 assert(GV->getParent() && "Global Value must be attached to a module!");
361 // The Module handles the pointer reference sharing...
362 return GV->getParent()->getConstantPointerRef(GV);
366 void ConstantPointerRef::mutateReference(GlobalValue *NewGV) {
367 getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);