blob: 28cc9d24e036fb1dfa387f96e5779409f676abeb [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattneracdbe712003-04-17 19:24:48 +000015#include "llvm/ConstantHandling.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Vikram S. Adve4e537b22002-07-14 23:13:17 +000017#include "llvm/iMemory.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000019#include "llvm/Module.h"
Chris Lattner5de22042001-11-27 00:03:19 +000020#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000021#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000022
Chris Lattner3462ae32001-12-03 22:26:30 +000023ConstantBool *ConstantBool::True = new ConstantBool(true);
24ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000025
Chris Lattner9655e542001-07-20 19:16:02 +000026
Chris Lattner2f7c9632001-06-06 20:29:01 +000027//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000028// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000029//===----------------------------------------------------------------------===//
30
31// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000032void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000033 assert(ST && "Type::setName - Must provide symbol table argument!");
34
35 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000036}
37
Chris Lattner3462ae32001-12-03 22:26:30 +000038void Constant::destroyConstantImpl() {
39 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000040 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000041 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000042 // but they don't know that. Because we only find out when the CPV is
43 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000044 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000045 //
46 while (!use_empty()) {
47 Value *V = use_back();
48#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000049 if (!isa<Constant>(V))
50 std::cerr << "While deleting: " << *this
51 << "\n\nUse still stuck around after Def is destroyed: "
52 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000053#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000054 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000055 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000056 CPV->destroyConstant();
57
58 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000059 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000060 }
61
62 // Value has no outstanding references it is safe to delete it now...
63 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000064}
Chris Lattner2f7c9632001-06-06 20:29:01 +000065
Chris Lattnerb1585a92002-08-13 17:50:20 +000066// Static constructor to create a '0' constant of arbitrary type...
67Constant *Constant::getNullValue(const Type *Ty) {
68 switch (Ty->getPrimitiveID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000069 case Type::BoolTyID: {
70 static Constant *NullBool = ConstantBool::get(false);
71 return NullBool;
72 }
73 case Type::SByteTyID: {
74 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
75 return NullSByte;
76 }
77 case Type::UByteTyID: {
78 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
79 return NullUByte;
80 }
81 case Type::ShortTyID: {
82 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
83 return NullShort;
84 }
85 case Type::UShortTyID: {
86 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
87 return NullUShort;
88 }
89 case Type::IntTyID: {
90 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
91 return NullInt;
92 }
93 case Type::UIntTyID: {
94 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
95 return NullUInt;
96 }
97 case Type::LongTyID: {
98 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
99 return NullLong;
100 }
101 case Type::ULongTyID: {
102 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
103 return NullULong;
104 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000105
Chris Lattner3e88ef92003-10-03 19:34:51 +0000106 case Type::FloatTyID: {
107 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
108 return NullFloat;
109 }
110 case Type::DoubleTyID: {
111 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
112 return NullDouble;
113 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000114
115 case Type::PointerTyID:
116 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000117
Chris Lattnerc33ae672003-03-06 21:02:18 +0000118 case Type::StructTyID: {
119 const StructType *ST = cast<StructType>(Ty);
Chris Lattnerc33ae672003-03-06 21:02:18 +0000120 const StructType::ElementTypes &ETs = ST->getElementTypes();
121 std::vector<Constant*> Elements;
122 Elements.resize(ETs.size());
123 for (unsigned i = 0, e = ETs.size(); i != e; ++i)
124 Elements[i] = Constant::getNullValue(ETs[i]);
125 return ConstantStruct::get(ST, Elements);
126 }
127 case Type::ArrayTyID: {
128 const ArrayType *AT = cast<ArrayType>(Ty);
129 Constant *El = Constant::getNullValue(AT->getElementType());
130 unsigned NumElements = AT->getNumElements();
131 return ConstantArray::get(AT, std::vector<Constant*>(NumElements, El));
132 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000133 default:
Chris Lattnerc33ae672003-03-06 21:02:18 +0000134 // Function, Type, Label, or Opaque type?
135 assert(0 && "Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000136 return 0;
137 }
138}
139
140// Static constructor to create the maximum constant of an integral type...
141ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
142 switch (Ty->getPrimitiveID()) {
143 case Type::BoolTyID: return ConstantBool::True;
144 case Type::SByteTyID:
145 case Type::ShortTyID:
146 case Type::IntTyID:
147 case Type::LongTyID: {
148 // Calculate 011111111111111...
149 unsigned TypeBits = Ty->getPrimitiveSize()*8;
150 int64_t Val = INT64_MAX; // All ones
151 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
152 return ConstantSInt::get(Ty, Val);
153 }
154
155 case Type::UByteTyID:
156 case Type::UShortTyID:
157 case Type::UIntTyID:
158 case Type::ULongTyID: return getAllOnesValue(Ty);
159
Chris Lattner31408f72002-08-14 17:12:13 +0000160 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000161 }
162}
163
164// Static constructor to create the minimum constant for an integral type...
165ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
166 switch (Ty->getPrimitiveID()) {
167 case Type::BoolTyID: return ConstantBool::False;
168 case Type::SByteTyID:
169 case Type::ShortTyID:
170 case Type::IntTyID:
171 case Type::LongTyID: {
172 // Calculate 1111111111000000000000
173 unsigned TypeBits = Ty->getPrimitiveSize()*8;
174 int64_t Val = -1; // All ones
175 Val <<= TypeBits-1; // Shift over to the right spot
176 return ConstantSInt::get(Ty, Val);
177 }
178
179 case Type::UByteTyID:
180 case Type::UShortTyID:
181 case Type::UIntTyID:
182 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
183
Chris Lattner31408f72002-08-14 17:12:13 +0000184 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000185 }
186}
187
188// Static constructor to create an integral constant with all bits set
189ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
190 switch (Ty->getPrimitiveID()) {
191 case Type::BoolTyID: return ConstantBool::True;
192 case Type::SByteTyID:
193 case Type::ShortTyID:
194 case Type::IntTyID:
195 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
196
197 case Type::UByteTyID:
198 case Type::UShortTyID:
199 case Type::UIntTyID:
200 case Type::ULongTyID: {
201 // Calculate ~0 of the right type...
202 unsigned TypeBits = Ty->getPrimitiveSize()*8;
203 uint64_t Val = ~0ULL; // All ones
204 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
205 return ConstantUInt::get(Ty, Val);
206 }
Chris Lattner31408f72002-08-14 17:12:13 +0000207 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000208 }
209}
210
Chris Lattner83e5d392003-03-10 22:39:02 +0000211bool ConstantUInt::isAllOnesValue() const {
212 unsigned TypeBits = getType()->getPrimitiveSize()*8;
213 uint64_t Val = ~0ULL; // All ones
214 Val >>= 64-TypeBits; // Shift out inappropriate bits
215 return getValue() == Val;
216}
217
Chris Lattnerb1585a92002-08-13 17:50:20 +0000218
Chris Lattner2f7c9632001-06-06 20:29:01 +0000219//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000220// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221//===----------------------------------------------------------------------===//
222
223//===----------------------------------------------------------------------===//
224// Normal Constructors
225
Chris Lattnerb1585a92002-08-13 17:50:20 +0000226ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227 Val = V;
228}
Chris Lattner49d855c2001-09-07 16:46:31 +0000229
Chris Lattnerb1585a92002-08-13 17:50:20 +0000230ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000231 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000232}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233
Chris Lattner3462ae32001-12-03 22:26:30 +0000234ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000235 assert(Ty->isInteger() && Ty->isSigned() &&
236 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000237 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238}
239
Chris Lattner3462ae32001-12-03 22:26:30 +0000240ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000241 assert(Ty->isInteger() && Ty->isUnsigned() &&
242 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000243 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000244}
245
Chris Lattner3462ae32001-12-03 22:26:30 +0000246ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000247 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000248 Val = V;
249}
250
Chris Lattner3462ae32001-12-03 22:26:30 +0000251ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000252 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000253 Operands.reserve(V.size());
254 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerab69ecc2003-08-18 16:54:48 +0000255 assert(V[i]->getType() == T->getElementType() ||
256 (T->isAbstract() &&
257 V[i]->getType()->getPrimitiveID() ==
258 T->getElementType()->getPrimitiveID()));
Chris Lattnera073acb2001-07-07 08:36:50 +0000259 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000260 }
261}
262
Chris Lattner3462ae32001-12-03 22:26:30 +0000263ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000264 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000265 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000266 assert(V.size() == ETypes.size() &&
267 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000268 Operands.reserve(V.size());
269 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000270 assert((V[i]->getType() == ETypes[i] ||
Chris Lattner7267b352003-10-21 17:39:59 +0000271 ((ETypes[i]->isAbstract() || V[i]->getType()->isAbstract()) &&
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000272 ETypes[i]->getPrimitiveID()==V[i]->getType()->getPrimitiveID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000273 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000274 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000275 }
276}
277
Chris Lattner3462ae32001-12-03 22:26:30 +0000278ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
279 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000280 Operands.push_back(Use(GV, this));
281}
282
Chris Lattner3cd8c562002-07-30 18:54:25 +0000283ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
284 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000285 Operands.push_back(Use(C, this));
286}
287
Chris Lattner22ced562003-06-22 20:48:30 +0000288static bool isSetCC(unsigned Opcode) {
289 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
290 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
291 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
292}
293
Chris Lattner3cd8c562002-07-30 18:54:25 +0000294ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner22ced562003-06-22 20:48:30 +0000295 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000296 Operands.push_back(Use(C1, this));
297 Operands.push_back(Use(C2, this));
298}
299
Chris Lattner3cd8c562002-07-30 18:54:25 +0000300ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
301 const Type *DestTy)
302 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000303 Operands.reserve(1+IdxList.size());
304 Operands.push_back(Use(C, this));
305 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
306 Operands.push_back(Use(IdxList[i], this));
307}
308
Chris Lattner60e0dd72001-10-03 06:12:09 +0000309
Chris Lattner2f7c9632001-06-06 20:29:01 +0000310
311//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000312// classof implementations
313
Chris Lattnerb1585a92002-08-13 17:50:20 +0000314bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000315 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000316}
317
Chris Lattner3462ae32001-12-03 22:26:30 +0000318bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000319 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000320}
Chris Lattner3462ae32001-12-03 22:26:30 +0000321bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000322 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000323}
Chris Lattner3462ae32001-12-03 22:26:30 +0000324bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000325 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000326}
Chris Lattner3462ae32001-12-03 22:26:30 +0000327bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000328 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000329 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000330 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000331}
Chris Lattner3462ae32001-12-03 22:26:30 +0000332bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000333 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000334}
Chris Lattner3462ae32001-12-03 22:26:30 +0000335bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000336 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000337}
Chris Lattner3462ae32001-12-03 22:26:30 +0000338bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000339 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000340}
341
342
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000343
Chris Lattner2f7c9632001-06-06 20:29:01 +0000344//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000345// isValueValidForType implementations
346
Chris Lattner3462ae32001-12-03 22:26:30 +0000347bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000348 switch (Ty->getPrimitiveID()) {
349 default:
350 return false; // These can't be represented as integers!!!
351
352 // Signed types...
353 case Type::SByteTyID:
354 return (Val <= INT8_MAX && Val >= INT8_MIN);
355 case Type::ShortTyID:
356 return (Val <= INT16_MAX && Val >= INT16_MIN);
357 case Type::IntTyID:
358 return (Val <= INT32_MAX && Val >= INT32_MIN);
359 case Type::LongTyID:
360 return true; // This is the largest type...
361 }
362 assert(0 && "WTF?");
363 return false;
364}
365
Chris Lattner3462ae32001-12-03 22:26:30 +0000366bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000367 switch (Ty->getPrimitiveID()) {
368 default:
369 return false; // These can't be represented as integers!!!
370
371 // Unsigned types...
372 case Type::UByteTyID:
373 return (Val <= UINT8_MAX);
374 case Type::UShortTyID:
375 return (Val <= UINT16_MAX);
376 case Type::UIntTyID:
377 return (Val <= UINT32_MAX);
378 case Type::ULongTyID:
379 return true; // This is the largest type...
380 }
381 assert(0 && "WTF?");
382 return false;
383}
384
Chris Lattner3462ae32001-12-03 22:26:30 +0000385bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000386 switch (Ty->getPrimitiveID()) {
387 default:
388 return false; // These can't be represented as floating point!
389
390 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000391 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000392 case Type::DoubleTyID:
393 return true; // This is the largest type...
394 }
395};
Chris Lattner9655e542001-07-20 19:16:02 +0000396
Chris Lattner49d855c2001-09-07 16:46:31 +0000397//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000398// replaceUsesOfWithOnConstant implementations
399
Chris Lattnerc27038d2003-08-29 05:36:46 +0000400void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
401 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000402 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
403
404 std::vector<Constant*> Values;
405 Values.reserve(getValues().size()); // Build replacement array...
406 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
407 Constant *Val = cast<Constant>(getValues()[i]);
408 if (Val == From) Val = cast<Constant>(To);
409 Values.push_back(Val);
410 }
411
412 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
413 assert(Replacement != this && "I didn't contain From!");
414
415 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000416 if (DisableChecking)
417 uncheckedReplaceAllUsesWith(Replacement);
418 else
419 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000420
421 // Delete the old constant!
422 destroyConstant();
423}
424
Chris Lattnerc27038d2003-08-29 05:36:46 +0000425void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
426 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000427 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
428
429 std::vector<Constant*> Values;
430 Values.reserve(getValues().size());
431 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
432 Constant *Val = cast<Constant>(getValues()[i]);
433 if (Val == From) Val = cast<Constant>(To);
434 Values.push_back(Val);
435 }
436
437 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
438 assert(Replacement != this && "I didn't contain From!");
439
440 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000441 if (DisableChecking)
442 uncheckedReplaceAllUsesWith(Replacement);
443 else
444 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000445
446 // Delete the old constant!
447 destroyConstant();
448}
449
Chris Lattnerc27038d2003-08-29 05:36:46 +0000450void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
451 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000452 if (isa<GlobalValue>(To)) {
453 assert(From == getOperand(0) && "Doesn't contain from!");
454 ConstantPointerRef *Replacement =
455 ConstantPointerRef::get(cast<GlobalValue>(To));
456
457 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000458 if (DisableChecking)
459 uncheckedReplaceAllUsesWith(Replacement);
460 else
461 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000462
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000463 } else {
464 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000465 if (DisableChecking)
466 uncheckedReplaceAllUsesWith(To);
467 else
468 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000469 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000470
471 // Delete the old constant!
472 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000473}
474
Chris Lattnerc27038d2003-08-29 05:36:46 +0000475void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
476 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000477 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
478 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000479
Chris Lattner46b3d302003-04-16 22:40:51 +0000480 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000481 if (getOpcode() == Instruction::GetElementPtr) {
482 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000483 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000484 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000485 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000486
487 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000488 Constant *Val = getOperand(i);
489 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000490 Indices.push_back(Val);
491 }
492 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
493 } else if (getOpcode() == Instruction::Cast) {
494 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000495 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb36e8a12003-11-05 19:53:03 +0000496 } else if (getOpcode() == Instruction::Shl ||
497 getOpcode() == Instruction::Shr) {
498 Constant *C1 = getOperand(0);
499 Constant *C2 = getOperand(1);
500 if (C1 == From) C1 = To;
501 if (C2 == From) C2 = To;
502 Replacement = ConstantExpr::getShift(getOpcode(), C1, C2);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000503 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000504 Constant *C1 = getOperand(0);
505 Constant *C2 = getOperand(1);
506 if (C1 == From) C1 = To;
507 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000508 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
509 } else {
510 assert(0 && "Unknown ConstantExpr type!");
511 return;
512 }
513
514 assert(Replacement != this && "I didn't contain From!");
515
516 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000517 if (DisableChecking)
518 uncheckedReplaceAllUsesWith(Replacement);
519 else
520 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000521
522 // Delete the old constant!
523 destroyConstant();
524}
525
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000526//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000527// Factory Function Implementation
528
Chris Lattner98fa07b2003-05-23 20:03:32 +0000529// ConstantCreator - A class that is used to create constants by
530// ValueMap*. This class should be partially specialized if there is
531// something strange that needs to be done to interface to the ctor for the
532// constant.
533//
534template<class ConstantClass, class TypeClass, class ValType>
535struct ConstantCreator {
536 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
537 return new ConstantClass(Ty, V);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000538 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000539};
540
Chris Lattnerb50d1352003-10-05 00:17:43 +0000541template<class ConstantClass, class TypeClass>
542struct ConvertConstantType {
543 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
544 assert(0 && "This type cannot be converted!\n");
545 abort();
546 }
547};
548
Chris Lattner98fa07b2003-05-23 20:03:32 +0000549namespace {
550 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000551 class ValueMap : public AbstractTypeUser {
552 typedef std::pair<const TypeClass*, ValType> MapKey;
553 typedef std::map<MapKey, ConstantClass *> MapTy;
554 typedef typename MapTy::iterator MapIterator;
555 MapTy Map;
556
557 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
558 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000559 public:
560 // getOrCreate - Return the specified constant from the map, creating it if
561 // necessary.
562 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000563 MapKey Lookup(Ty, V);
564 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000565 if (I != Map.end() && I->first == Lookup)
566 return I->second; // Is it in the map?
567
568 // If no preexisting value, create one now...
569 ConstantClass *Result =
570 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
571
Chris Lattnerb50d1352003-10-05 00:17:43 +0000572
573 /// FIXME: why does this assert fail when loading 176.gcc?
574 //assert(Result->getType() == Ty && "Type specified is not correct!");
575 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
576
577 // If the type of the constant is abstract, make sure that an entry exists
578 // for it in the AbstractTypeMap.
579 if (Ty->isAbstract()) {
580 typename AbstractTypeMapTy::iterator TI =
581 AbstractTypeMap.lower_bound(Ty);
582
583 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
584 // Add ourselves to the ATU list of the type.
585 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
586
587 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
588 }
589 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000590 return Result;
591 }
592
593 void remove(ConstantClass *CP) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000594 // FIXME: This should not use a linear scan. If this gets to be a
595 // performance problem, someone should look at this.
596 MapIterator I = Map.begin();
597 for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
598 /* empty */;
599
600 assert(I != Map.end() && "Constant not found in constant table!");
601
602 // Now that we found the entry, make sure this isn't the entry that
603 // the AbstractTypeMap points to.
604 const TypeClass *Ty = I->first.first;
605 if (Ty->isAbstract()) {
606 assert(AbstractTypeMap.count(Ty) &&
607 "Abstract type not in AbstractTypeMap?");
608 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
609 if (ATMEntryIt == I) {
610 // Yes, we are removing the representative entry for this type.
611 // See if there are any other entries of the same type.
612 MapIterator TmpIt = ATMEntryIt;
613
614 // First check the entry before this one...
615 if (TmpIt != Map.begin()) {
616 --TmpIt;
617 if (TmpIt->first.first != Ty) // Not the same type, move back...
618 ++TmpIt;
619 }
620
621 // If we didn't find the same type, try to move forward...
622 if (TmpIt == ATMEntryIt) {
623 ++TmpIt;
624 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
625 --TmpIt; // No entry afterwards with the same type
626 }
627
628 // If there is another entry in the map of the same abstract type,
629 // update the AbstractTypeMap entry now.
630 if (TmpIt != ATMEntryIt) {
631 ATMEntryIt = TmpIt;
632 } else {
633 // Otherwise, we are removing the last instance of this type
634 // from the table. Remove from the ATM, and from user list.
635 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
636 AbstractTypeMap.erase(Ty);
637 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000638 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000639 }
640
641 Map.erase(I);
642 }
643
644 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
645 typename AbstractTypeMapTy::iterator I =
646 AbstractTypeMap.find(cast<TypeClass>(OldTy));
647
648 assert(I != AbstractTypeMap.end() &&
649 "Abstract type not in AbstractTypeMap?");
650
651 // Convert a constant at a time until the last one is gone. The last one
652 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
653 // eliminated eventually.
654 do {
655 ConvertConstantType<ConstantClass,
656 TypeClass>::convert(I->second->second,
657 cast<TypeClass>(NewTy));
658
659 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
660 } while (I != AbstractTypeMap.end());
661 }
662
663 // If the type became concrete without being refined to any other existing
664 // type, we just remove ourselves from the ATU list.
665 void typeBecameConcrete(const DerivedType *AbsTy) {
666 AbsTy->removeAbstractTypeUser(this);
667 }
668
669 void dump() const {
670 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000671 }
672 };
673}
674
675
676
Chris Lattner3462ae32001-12-03 22:26:30 +0000677//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000678//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000679static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
680static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000681
Chris Lattner3462ae32001-12-03 22:26:30 +0000682ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000683 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000684}
685
Chris Lattner3462ae32001-12-03 22:26:30 +0000686ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000687 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000688}
689
Chris Lattner3462ae32001-12-03 22:26:30 +0000690ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000691 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000692 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
693 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000694}
695
Chris Lattner3462ae32001-12-03 22:26:30 +0000696//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000697//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000698static ValueMap<double, Type, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000699
Chris Lattner3462ae32001-12-03 22:26:30 +0000700ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000701 return FPConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000702}
703
Chris Lattner3462ae32001-12-03 22:26:30 +0000704//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000705//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000706
707template<>
708struct ConvertConstantType<ConstantArray, ArrayType> {
709 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
710 // Make everyone now use a constant of the new type...
711 std::vector<Constant*> C;
712 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
713 C.push_back(cast<Constant>(OldC->getOperand(i)));
714 Constant *New = ConstantArray::get(NewTy, C);
715 assert(New != OldC && "Didn't replace constant??");
716 OldC->uncheckedReplaceAllUsesWith(New);
717 OldC->destroyConstant(); // This constant is now dead, destroy it.
718 }
719};
720
721
Chris Lattner98fa07b2003-05-23 20:03:32 +0000722static ValueMap<std::vector<Constant*>, ArrayType,
723 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000724
Chris Lattner3462ae32001-12-03 22:26:30 +0000725ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000726 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000727 return ArrayConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000728}
729
Chris Lattner98fa07b2003-05-23 20:03:32 +0000730// destroyConstant - Remove the constant from the constant table...
731//
732void ConstantArray::destroyConstant() {
733 ArrayConstants.remove(this);
734 destroyConstantImpl();
735}
736
Chris Lattner3462ae32001-12-03 22:26:30 +0000737// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000738// contain the specified string. A null terminator is added to the specified
739// string so that it may be used in a natural way...
740//
Chris Lattner7f74a562002-01-20 22:54:45 +0000741ConstantArray *ConstantArray::get(const std::string &Str) {
742 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000743
744 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000745 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000746
747 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000748 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000749
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000750 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000751 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000752}
753
Chris Lattner81fabb02002-08-26 17:53:56 +0000754// getAsString - If the sub-element type of this array is either sbyte or ubyte,
755// then this method converts the array to an std::string and returns it.
756// Otherwise, it asserts out.
757//
758std::string ConstantArray::getAsString() const {
Chris Lattner6077c312003-07-23 15:22:26 +0000759 assert((getType()->getElementType() == Type::UByteTy ||
760 getType()->getElementType() == Type::SByteTy) && "Not a string!");
761
Chris Lattner81fabb02002-08-26 17:53:56 +0000762 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000763 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
764 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000765 return Result;
766}
767
768
Chris Lattner3462ae32001-12-03 22:26:30 +0000769//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000770//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000771
772template<>
773struct ConvertConstantType<ConstantStruct, StructType> {
774 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
775 // Make everyone now use a constant of the new type...
776 std::vector<Constant*> C;
777 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
778 C.push_back(cast<Constant>(OldC->getOperand(i)));
779 Constant *New = ConstantStruct::get(NewTy, C);
780 assert(New != OldC && "Didn't replace constant??");
781
782 OldC->uncheckedReplaceAllUsesWith(New);
783 OldC->destroyConstant(); // This constant is now dead, destroy it.
784 }
785};
786
Chris Lattner98fa07b2003-05-23 20:03:32 +0000787static ValueMap<std::vector<Constant*>, StructType,
788 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000789
Chris Lattner3462ae32001-12-03 22:26:30 +0000790ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000791 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000792 return StructConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000793}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000794
Chris Lattnerd7a73302001-10-13 06:57:33 +0000795// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000796//
Chris Lattner3462ae32001-12-03 22:26:30 +0000797void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000798 StructConstants.remove(this);
799 destroyConstantImpl();
800}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000801
Chris Lattner3462ae32001-12-03 22:26:30 +0000802//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000803//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000804
805// ConstantPointerNull does not take extra "value" argument...
806template<class ValType>
807struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
808 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
809 return new ConstantPointerNull(Ty);
810 }
811};
812
Chris Lattnerb50d1352003-10-05 00:17:43 +0000813template<>
814struct ConvertConstantType<ConstantPointerNull, PointerType> {
815 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
816 // Make everyone now use a constant of the new type...
817 Constant *New = ConstantPointerNull::get(NewTy);
818 assert(New != OldC && "Didn't replace constant??");
819 OldC->uncheckedReplaceAllUsesWith(New);
820 OldC->destroyConstant(); // This constant is now dead, destroy it.
821 }
822};
823
Chris Lattner98fa07b2003-05-23 20:03:32 +0000824static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000825
Chris Lattner3462ae32001-12-03 22:26:30 +0000826ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000827 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000828}
829
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000830// destroyConstant - Remove the constant from the constant table...
831//
832void ConstantPointerNull::destroyConstant() {
833 NullPtrConstants.remove(this);
834 destroyConstantImpl();
835}
836
837
Chris Lattner3462ae32001-12-03 22:26:30 +0000838//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000839//
Chris Lattner3462ae32001-12-03 22:26:30 +0000840ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000841 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000842
Chris Lattnerd7a73302001-10-13 06:57:33 +0000843 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000844 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000845}
846
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000847// destroyConstant - Remove the constant from the constant table...
848//
849void ConstantPointerRef::destroyConstant() {
850 getValue()->getParent()->destroyConstantPointerRef(this);
851 destroyConstantImpl();
852}
853
854
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000855//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000856//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000857typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000858
859template<>
860struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
861 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
862 if (V.first == Instruction::Cast)
863 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
864 if ((V.first >= Instruction::BinaryOpsBegin &&
865 V.first < Instruction::BinaryOpsEnd) ||
866 V.first == Instruction::Shl || V.first == Instruction::Shr)
867 return new ConstantExpr(V.first, V.second[0], V.second[1]);
868
869 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
870
Chris Lattner98fa07b2003-05-23 20:03:32 +0000871 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerb50d1352003-10-05 00:17:43 +0000872 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000873 }
874};
875
Chris Lattnerb50d1352003-10-05 00:17:43 +0000876template<>
877struct ConvertConstantType<ConstantExpr, Type> {
878 static void convert(ConstantExpr *OldC, const Type *NewTy) {
879 Constant *New;
880 switch (OldC->getOpcode()) {
881 case Instruction::Cast:
882 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
883 break;
884 case Instruction::Shl:
885 case Instruction::Shr:
886 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
887 OldC->getOperand(0), OldC->getOperand(1));
888 break;
889 default:
890 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
891 OldC->getOpcode() < Instruction::BinaryOpsEnd);
892 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
893 OldC->getOperand(1));
894 break;
895 case Instruction::GetElementPtr:
896 // Make everyone now use a constant of the new type...
897 std::vector<Constant*> C;
898 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
899 C.push_back(cast<Constant>(OldC->getOperand(i)));
900 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
901 break;
902 }
903
904 assert(New != OldC && "Didn't replace constant??");
905 OldC->uncheckedReplaceAllUsesWith(New);
906 OldC->destroyConstant(); // This constant is now dead, destroy it.
907 }
908};
909
910
Chris Lattner98fa07b2003-05-23 20:03:32 +0000911static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000912
Chris Lattner46b3d302003-04-16 22:40:51 +0000913Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +0000914 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
915
Chris Lattneracdbe712003-04-17 19:24:48 +0000916 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
917 return FC; // Fold a few common cases...
918
Vikram S. Adve4c485332002-07-15 18:19:33 +0000919 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000920 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000921 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
922 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000923}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000924
Chris Lattnerb50d1352003-10-05 00:17:43 +0000925Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
926 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000927 // Check the operands for consistency first
928 assert((Opcode >= Instruction::BinaryOpsBegin &&
929 Opcode < Instruction::BinaryOpsEnd) &&
930 "Invalid opcode in binary constant expression");
931 assert(C1->getType() == C2->getType() &&
932 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000933
934 if (ReqTy == C1->getType())
935 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
936 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +0000937
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000938 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000939 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000940 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000941}
942
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000943/// getShift - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +0000944Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
945 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000946 // Check the operands for consistency first
947 assert((Opcode == Instruction::Shl ||
948 Opcode == Instruction::Shr) &&
949 "Invalid opcode in binary constant expression");
950 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
951 "Invalid operand types for Shift constant expr!");
952
953 if (Constant *FC = ConstantFoldShiftInstruction(Opcode, C1, C2))
954 return FC; // Fold a few common cases...
955
956 // Look up the constant in the table first to ensure uniqueness
957 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000958 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000959 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000960}
961
962
Chris Lattnerb50d1352003-10-05 00:17:43 +0000963Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
964 const std::vector<Constant*> &IdxList) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000965 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
966 return FC; // Fold a few common cases...
Chris Lattnerb50d1352003-10-05 00:17:43 +0000967 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +0000968 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000969
970 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000971 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000972 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000973 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000974 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +0000975}
976
Chris Lattnerb50d1352003-10-05 00:17:43 +0000977Constant *ConstantExpr::getGetElementPtr(Constant *C,
978 const std::vector<Constant*> &IdxList){
979 // Get the result type of the getelementptr!
980 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
981
982 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
983 true);
984 assert(Ty && "GEP indices invalid!");
Chris Lattner8a552622003-10-23 05:21:48 +0000985
986 if (C->isNullValue()) {
987 bool isNull = true;
988 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
989 if (!IdxList[i]->isNullValue()) {
990 isNull = false;
991 break;
992 }
993 if (isNull) return ConstantPointerNull::get(PointerType::get(Ty));
994 }
995
Chris Lattnerb50d1352003-10-05 00:17:43 +0000996 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
997}
998
999
Vikram S. Adve4c485332002-07-15 18:19:33 +00001000// destroyConstant - Remove the constant from the constant table...
1001//
1002void ConstantExpr::destroyConstant() {
1003 ExprConstants.remove(this);
1004 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001005}
1006
Chris Lattner3cd8c562002-07-30 18:54:25 +00001007const char *ConstantExpr::getOpcodeName() const {
1008 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001009}
1010
Chris Lattner163b8902002-10-14 03:30:23 +00001011unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
1012 // Uses of constant pointer refs are global values, not constants!
1013 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1014 GlobalValue *NewGV = cast<GlobalValue>(NewV);
1015 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001016
Chris Lattner163b8902002-10-14 03:30:23 +00001017 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattner163b8902002-10-14 03:30:23 +00001018 Operands[0] = NewGV;
Chris Lattnercb4d26f2003-05-15 19:37:21 +00001019 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
Chris Lattner163b8902002-10-14 03:30:23 +00001020 return 1;
1021 } else {
1022 Constant *NewC = cast<Constant>(NewV);
1023 unsigned NumReplaced = 0;
1024 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
1025 if (Operands[i] == OldV) {
1026 ++NumReplaced;
1027 Operands[i] = NewC;
1028 }
1029 return NumReplaced;
1030 }
Chris Lattner25033252001-10-03 19:28:15 +00001031}