blob: e18f9b2e543f57094ddf9f67328a263c03da1266 [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 Lattner5a945e32004-01-12 21:13:12 +000015#include "ConstantFolding.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 Lattner189d19f2003-11-21 20:23:48 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner3462ae32001-12-03 22:26:30 +000024ConstantBool *ConstantBool::True = new ConstantBool(true);
25ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000026
Chris Lattner9655e542001-07-20 19:16:02 +000027
Chris Lattner2f7c9632001-06-06 20:29:01 +000028//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000029// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000030//===----------------------------------------------------------------------===//
31
32// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000033void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000034 assert(ST && "Type::setName - Must provide symbol table argument!");
35
36 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000037}
38
Chris Lattner3462ae32001-12-03 22:26:30 +000039void Constant::destroyConstantImpl() {
40 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000041 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000042 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000043 // but they don't know that. Because we only find out when the CPV is
44 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000045 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000046 //
47 while (!use_empty()) {
48 Value *V = use_back();
49#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000050 if (!isa<Constant>(V))
51 std::cerr << "While deleting: " << *this
52 << "\n\nUse still stuck around after Def is destroyed: "
53 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000054#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000055 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000056 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000057 CPV->destroyConstant();
58
59 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000060 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000061 }
62
63 // Value has no outstanding references it is safe to delete it now...
64 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000065}
Chris Lattner2f7c9632001-06-06 20:29:01 +000066
Chris Lattnerb1585a92002-08-13 17:50:20 +000067// Static constructor to create a '0' constant of arbitrary type...
68Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000069 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000070 case Type::BoolTyID: {
71 static Constant *NullBool = ConstantBool::get(false);
72 return NullBool;
73 }
74 case Type::SByteTyID: {
75 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
76 return NullSByte;
77 }
78 case Type::UByteTyID: {
79 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
80 return NullUByte;
81 }
82 case Type::ShortTyID: {
83 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
84 return NullShort;
85 }
86 case Type::UShortTyID: {
87 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
88 return NullUShort;
89 }
90 case Type::IntTyID: {
91 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
92 return NullInt;
93 }
94 case Type::UIntTyID: {
95 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
96 return NullUInt;
97 }
98 case Type::LongTyID: {
99 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
100 return NullLong;
101 }
102 case Type::ULongTyID: {
103 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
104 return NullULong;
105 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000106
Chris Lattner3e88ef92003-10-03 19:34:51 +0000107 case Type::FloatTyID: {
108 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
109 return NullFloat;
110 }
111 case Type::DoubleTyID: {
112 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
113 return NullDouble;
114 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000115
116 case Type::PointerTyID:
117 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000118
Chris Lattner9fba3da2004-02-15 05:53:04 +0000119 case Type::StructTyID:
120 case Type::ArrayTyID:
121 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000122 default:
Chris Lattnerc33ae672003-03-06 21:02:18 +0000123 // Function, Type, Label, or Opaque type?
124 assert(0 && "Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000125 return 0;
126 }
127}
128
129// Static constructor to create the maximum constant of an integral type...
130ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000131 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000132 case Type::BoolTyID: return ConstantBool::True;
133 case Type::SByteTyID:
134 case Type::ShortTyID:
135 case Type::IntTyID:
136 case Type::LongTyID: {
137 // Calculate 011111111111111...
138 unsigned TypeBits = Ty->getPrimitiveSize()*8;
139 int64_t Val = INT64_MAX; // All ones
140 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
141 return ConstantSInt::get(Ty, Val);
142 }
143
144 case Type::UByteTyID:
145 case Type::UShortTyID:
146 case Type::UIntTyID:
147 case Type::ULongTyID: return getAllOnesValue(Ty);
148
Chris Lattner31408f72002-08-14 17:12:13 +0000149 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000150 }
151}
152
153// Static constructor to create the minimum constant for an integral type...
154ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000155 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000156 case Type::BoolTyID: return ConstantBool::False;
157 case Type::SByteTyID:
158 case Type::ShortTyID:
159 case Type::IntTyID:
160 case Type::LongTyID: {
161 // Calculate 1111111111000000000000
162 unsigned TypeBits = Ty->getPrimitiveSize()*8;
163 int64_t Val = -1; // All ones
164 Val <<= TypeBits-1; // Shift over to the right spot
165 return ConstantSInt::get(Ty, Val);
166 }
167
168 case Type::UByteTyID:
169 case Type::UShortTyID:
170 case Type::UIntTyID:
171 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
172
Chris Lattner31408f72002-08-14 17:12:13 +0000173 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000174 }
175}
176
177// Static constructor to create an integral constant with all bits set
178ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000179 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000180 case Type::BoolTyID: return ConstantBool::True;
181 case Type::SByteTyID:
182 case Type::ShortTyID:
183 case Type::IntTyID:
184 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
185
186 case Type::UByteTyID:
187 case Type::UShortTyID:
188 case Type::UIntTyID:
189 case Type::ULongTyID: {
190 // Calculate ~0 of the right type...
191 unsigned TypeBits = Ty->getPrimitiveSize()*8;
192 uint64_t Val = ~0ULL; // All ones
193 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
194 return ConstantUInt::get(Ty, Val);
195 }
Chris Lattner31408f72002-08-14 17:12:13 +0000196 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000197 }
198}
199
Chris Lattner83e5d392003-03-10 22:39:02 +0000200bool ConstantUInt::isAllOnesValue() const {
201 unsigned TypeBits = getType()->getPrimitiveSize()*8;
202 uint64_t Val = ~0ULL; // All ones
203 Val >>= 64-TypeBits; // Shift out inappropriate bits
204 return getValue() == Val;
205}
206
Chris Lattnerb1585a92002-08-13 17:50:20 +0000207
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000209// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000210//===----------------------------------------------------------------------===//
211
212//===----------------------------------------------------------------------===//
213// Normal Constructors
214
Chris Lattnerb1585a92002-08-13 17:50:20 +0000215ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216 Val = V;
217}
Chris Lattner49d855c2001-09-07 16:46:31 +0000218
Chris Lattnerb1585a92002-08-13 17:50:20 +0000219ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000220 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000221}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222
Chris Lattner3462ae32001-12-03 22:26:30 +0000223ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000224 assert(Ty->isInteger() && Ty->isSigned() &&
225 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000226 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227}
228
Chris Lattner3462ae32001-12-03 22:26:30 +0000229ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000230 assert(Ty->isInteger() && Ty->isUnsigned() &&
231 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000232 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233}
234
Chris Lattner3462ae32001-12-03 22:26:30 +0000235ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000236 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237 Val = V;
238}
239
Chris Lattner3462ae32001-12-03 22:26:30 +0000240ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000241 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000242 Operands.reserve(V.size());
243 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerab69ecc2003-08-18 16:54:48 +0000244 assert(V[i]->getType() == T->getElementType() ||
245 (T->isAbstract() &&
Chris Lattner6b727592004-06-17 18:19:28 +0000246 V[i]->getType()->getTypeID() == T->getElementType()->getTypeID()));
Chris Lattnera073acb2001-07-07 08:36:50 +0000247 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000248 }
249}
250
Chris Lattner3462ae32001-12-03 22:26:30 +0000251ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000252 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000253 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000254 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000255 Operands.reserve(V.size());
256 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000257 assert((V[i]->getType() == T->getElementType(i) ||
258 ((T->getElementType(i)->isAbstract() ||
259 V[i]->getType()->isAbstract()) &&
Chris Lattner6b727592004-06-17 18:19:28 +0000260 T->getElementType(i)->getTypeID() == V[i]->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000261 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000262 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000263 }
264}
265
Chris Lattner3462ae32001-12-03 22:26:30 +0000266ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
Chris Lattnere120a732003-11-17 19:47:21 +0000267 : Constant(GV->getType()) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000268 Operands.reserve(1);
Chris Lattner60e0dd72001-10-03 06:12:09 +0000269 Operands.push_back(Use(GV, this));
270}
271
Chris Lattner3cd8c562002-07-30 18:54:25 +0000272ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
273 : Constant(Ty), iType(Opcode) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000274 Operands.reserve(1);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000275 Operands.push_back(Use(C, this));
276}
277
Chris Lattner6e415c02004-03-12 05:54:04 +0000278// Select instruction creation ctor
279ConstantExpr::ConstantExpr(Constant *C, Constant *V1, Constant *V2)
280 : Constant(V1->getType()), iType(Instruction::Select) {
281 Operands.reserve(3);
282 Operands.push_back(Use(C, this));
283 Operands.push_back(Use(V1, this));
284 Operands.push_back(Use(V2, this));
285}
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) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000296 Operands.reserve(2);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000297 Operands.push_back(Use(C1, this));
298 Operands.push_back(Use(C2, this));
299}
300
Chris Lattner3cd8c562002-07-30 18:54:25 +0000301ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
302 const Type *DestTy)
303 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000304 Operands.reserve(1+IdxList.size());
305 Operands.push_back(Use(C, this));
306 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
307 Operands.push_back(Use(IdxList[i], this));
308}
309
Chris Lattner817175f2004-03-29 02:37:53 +0000310/// ConstantExpr::get* - Return some common constants without having to
311/// specify the full Instruction::OPCODE identifier.
312///
313Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000314 if (!C->getType()->isFloatingPoint())
315 return get(Instruction::Sub, getNullValue(C->getType()), C);
316 else
317 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000318}
319Constant *ConstantExpr::getNot(Constant *C) {
320 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
321 return get(Instruction::Xor, C,
322 ConstantIntegral::getAllOnesValue(C->getType()));
323}
324Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
325 return get(Instruction::Add, C1, C2);
326}
327Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
328 return get(Instruction::Sub, C1, C2);
329}
330Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
331 return get(Instruction::Mul, C1, C2);
332}
333Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
334 return get(Instruction::Div, C1, C2);
335}
336Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
337 return get(Instruction::Rem, C1, C2);
338}
339Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
340 return get(Instruction::And, C1, C2);
341}
342Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
343 return get(Instruction::Or, C1, C2);
344}
345Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
346 return get(Instruction::Xor, C1, C2);
347}
348Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
349 return get(Instruction::SetEQ, C1, C2);
350}
351Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
352 return get(Instruction::SetNE, C1, C2);
353}
354Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
355 return get(Instruction::SetLT, C1, C2);
356}
357Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
358 return get(Instruction::SetGT, C1, C2);
359}
360Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
361 return get(Instruction::SetLE, C1, C2);
362}
363Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
364 return get(Instruction::SetGE, C1, C2);
365}
366Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
367 return get(Instruction::Shl, C1, C2);
368}
369Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
370 return get(Instruction::Shr, C1, C2);
371}
372
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000373Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
374 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
375 return getCast(getShr(getCast(C1,
376 C1->getType()->getUnsignedVersion()), C2), C1->getType());
377}
Chris Lattner817175f2004-03-29 02:37:53 +0000378
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000379Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
380 if (C1->getType()->isSigned()) return getShr(C1, C2);
381 return getCast(getShr(getCast(C1,
382 C1->getType()->getSignedVersion()), C2), C1->getType());
383}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000384
Chris Lattner2f7c9632001-06-06 20:29:01 +0000385
386//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000387// classof implementations
388
Chris Lattnerb1585a92002-08-13 17:50:20 +0000389bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000390 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000391}
392
Chris Lattner3462ae32001-12-03 22:26:30 +0000393bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000394 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000395}
Chris Lattner3462ae32001-12-03 22:26:30 +0000396bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000397 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000398}
Chris Lattner3462ae32001-12-03 22:26:30 +0000399bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000400 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000401}
Chris Lattner3462ae32001-12-03 22:26:30 +0000402bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000403 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000404 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000405 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000406}
Chris Lattner9fba3da2004-02-15 05:53:04 +0000407bool ConstantAggregateZero::classof(const Constant *CPV) {
408 return (isa<ArrayType>(CPV->getType()) || isa<StructType>(CPV->getType())) &&
409 CPV->isNullValue();
410}
Chris Lattner3462ae32001-12-03 22:26:30 +0000411bool ConstantArray::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000412 return isa<ArrayType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000413}
Chris Lattner3462ae32001-12-03 22:26:30 +0000414bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000415 return isa<StructType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000416}
Chris Lattnere120a732003-11-17 19:47:21 +0000417
418bool ConstantPointerNull::classof(const Constant *CPV) {
419 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
420 CPV->getNumOperands() == 0;
421}
422
423bool ConstantPointerRef::classof(const Constant *CPV) {
424 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
425 CPV->getNumOperands() == 1;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000426}
427
428
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000429
Chris Lattner2f7c9632001-06-06 20:29:01 +0000430//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000431// isValueValidForType implementations
432
Chris Lattner3462ae32001-12-03 22:26:30 +0000433bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000434 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000435 default:
436 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000437 // Signed types...
438 case Type::SByteTyID:
439 return (Val <= INT8_MAX && Val >= INT8_MIN);
440 case Type::ShortTyID:
441 return (Val <= INT16_MAX && Val >= INT16_MIN);
442 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000443 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444 case Type::LongTyID:
445 return true; // This is the largest type...
446 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000447}
448
Chris Lattner3462ae32001-12-03 22:26:30 +0000449bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000450 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000451 default:
452 return false; // These can't be represented as integers!!!
453
454 // Unsigned types...
455 case Type::UByteTyID:
456 return (Val <= UINT8_MAX);
457 case Type::UShortTyID:
458 return (Val <= UINT16_MAX);
459 case Type::UIntTyID:
460 return (Val <= UINT32_MAX);
461 case Type::ULongTyID:
462 return true; // This is the largest type...
463 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000464}
465
Chris Lattner3462ae32001-12-03 22:26:30 +0000466bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000467 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000468 default:
469 return false; // These can't be represented as floating point!
470
471 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000472 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000473 case Type::DoubleTyID:
474 return true; // This is the largest type...
475 }
476};
Chris Lattner9655e542001-07-20 19:16:02 +0000477
Chris Lattner49d855c2001-09-07 16:46:31 +0000478//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000479// replaceUsesOfWithOnConstant implementations
480
Chris Lattnerc27038d2003-08-29 05:36:46 +0000481void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
482 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000483 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
484
485 std::vector<Constant*> Values;
486 Values.reserve(getValues().size()); // Build replacement array...
487 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
488 Constant *Val = cast<Constant>(getValues()[i]);
489 if (Val == From) Val = cast<Constant>(To);
490 Values.push_back(Val);
491 }
492
Chris Lattnerc75bf522004-02-15 04:05:58 +0000493 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000494 assert(Replacement != this && "I didn't contain From!");
495
496 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000497 if (DisableChecking)
498 uncheckedReplaceAllUsesWith(Replacement);
499 else
500 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000501
502 // Delete the old constant!
503 destroyConstant();
504}
505
Chris Lattnerc27038d2003-08-29 05:36:46 +0000506void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
507 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000508 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
509
510 std::vector<Constant*> Values;
511 Values.reserve(getValues().size());
512 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
513 Constant *Val = cast<Constant>(getValues()[i]);
514 if (Val == From) Val = cast<Constant>(To);
515 Values.push_back(Val);
516 }
517
Chris Lattner37a716f2004-02-15 04:07:32 +0000518 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000519 assert(Replacement != this && "I didn't contain From!");
520
521 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000522 if (DisableChecking)
523 uncheckedReplaceAllUsesWith(Replacement);
524 else
525 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000526
527 // Delete the old constant!
528 destroyConstant();
529}
530
Chris Lattnerc27038d2003-08-29 05:36:46 +0000531void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
532 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000533 if (isa<GlobalValue>(To)) {
534 assert(From == getOperand(0) && "Doesn't contain from!");
535 ConstantPointerRef *Replacement =
536 ConstantPointerRef::get(cast<GlobalValue>(To));
537
538 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000539 if (DisableChecking)
540 uncheckedReplaceAllUsesWith(Replacement);
541 else
542 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000543
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000544 } else {
545 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000546 if (DisableChecking)
547 uncheckedReplaceAllUsesWith(To);
548 else
549 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000550 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000551
552 // Delete the old constant!
553 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000554}
555
Chris Lattnerc27038d2003-08-29 05:36:46 +0000556void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
557 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000558 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
559 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000560
Chris Lattner46b3d302003-04-16 22:40:51 +0000561 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000562 if (getOpcode() == Instruction::GetElementPtr) {
563 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000564 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000565 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000566 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000567
568 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000569 Constant *Val = getOperand(i);
570 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000571 Indices.push_back(Val);
572 }
573 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
574 } else if (getOpcode() == Instruction::Cast) {
575 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000576 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattner467cb2b2004-03-31 02:56:11 +0000577 } else if (getOpcode() == Instruction::Select) {
578 Constant *C1 = getOperand(0);
579 Constant *C2 = getOperand(1);
580 Constant *C3 = getOperand(2);
581 if (C1 == From) C1 = To;
582 if (C2 == From) C2 = To;
583 if (C3 == From) C3 = To;
584 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000585 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000586 Constant *C1 = getOperand(0);
587 Constant *C2 = getOperand(1);
588 if (C1 == From) C1 = To;
589 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000590 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
591 } else {
592 assert(0 && "Unknown ConstantExpr type!");
593 return;
594 }
595
596 assert(Replacement != this && "I didn't contain From!");
597
598 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000599 if (DisableChecking)
600 uncheckedReplaceAllUsesWith(Replacement);
601 else
602 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000603
604 // Delete the old constant!
605 destroyConstant();
606}
607
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000608//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000609// Factory Function Implementation
610
Chris Lattner98fa07b2003-05-23 20:03:32 +0000611// ConstantCreator - A class that is used to create constants by
612// ValueMap*. This class should be partially specialized if there is
613// something strange that needs to be done to interface to the ctor for the
614// constant.
615//
Chris Lattner189d19f2003-11-21 20:23:48 +0000616namespace llvm {
617 template<class ConstantClass, class TypeClass, class ValType>
618 struct ConstantCreator {
619 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
620 return new ConstantClass(Ty, V);
621 }
622 };
623
624 template<class ConstantClass, class TypeClass>
625 struct ConvertConstantType {
626 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
627 assert(0 && "This type cannot be converted!\n");
628 abort();
629 }
630 };
631}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000632
Chris Lattner98fa07b2003-05-23 20:03:32 +0000633namespace {
634 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000635 class ValueMap : public AbstractTypeUser {
636 typedef std::pair<const TypeClass*, ValType> MapKey;
637 typedef std::map<MapKey, ConstantClass *> MapTy;
638 typedef typename MapTy::iterator MapIterator;
639 MapTy Map;
640
641 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
642 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000643 public:
644 // getOrCreate - Return the specified constant from the map, creating it if
645 // necessary.
646 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000647 MapKey Lookup(Ty, V);
648 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000649 if (I != Map.end() && I->first == Lookup)
650 return I->second; // Is it in the map?
651
652 // If no preexisting value, create one now...
653 ConstantClass *Result =
654 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
655
Chris Lattnerb50d1352003-10-05 00:17:43 +0000656
657 /// FIXME: why does this assert fail when loading 176.gcc?
658 //assert(Result->getType() == Ty && "Type specified is not correct!");
659 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
660
661 // If the type of the constant is abstract, make sure that an entry exists
662 // for it in the AbstractTypeMap.
663 if (Ty->isAbstract()) {
664 typename AbstractTypeMapTy::iterator TI =
665 AbstractTypeMap.lower_bound(Ty);
666
667 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
668 // Add ourselves to the ATU list of the type.
669 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
670
671 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
672 }
673 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000674 return Result;
675 }
676
677 void remove(ConstantClass *CP) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000678 // FIXME: This should not use a linear scan. If this gets to be a
679 // performance problem, someone should look at this.
680 MapIterator I = Map.begin();
681 for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
682 /* empty */;
683
684 assert(I != Map.end() && "Constant not found in constant table!");
685
686 // Now that we found the entry, make sure this isn't the entry that
687 // the AbstractTypeMap points to.
688 const TypeClass *Ty = I->first.first;
689 if (Ty->isAbstract()) {
690 assert(AbstractTypeMap.count(Ty) &&
691 "Abstract type not in AbstractTypeMap?");
692 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
693 if (ATMEntryIt == I) {
694 // Yes, we are removing the representative entry for this type.
695 // See if there are any other entries of the same type.
696 MapIterator TmpIt = ATMEntryIt;
697
698 // First check the entry before this one...
699 if (TmpIt != Map.begin()) {
700 --TmpIt;
701 if (TmpIt->first.first != Ty) // Not the same type, move back...
702 ++TmpIt;
703 }
704
705 // If we didn't find the same type, try to move forward...
706 if (TmpIt == ATMEntryIt) {
707 ++TmpIt;
708 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
709 --TmpIt; // No entry afterwards with the same type
710 }
711
712 // If there is another entry in the map of the same abstract type,
713 // update the AbstractTypeMap entry now.
714 if (TmpIt != ATMEntryIt) {
715 ATMEntryIt = TmpIt;
716 } else {
717 // Otherwise, we are removing the last instance of this type
718 // from the table. Remove from the ATM, and from user list.
719 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
720 AbstractTypeMap.erase(Ty);
721 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000722 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000723 }
724
725 Map.erase(I);
726 }
727
728 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
729 typename AbstractTypeMapTy::iterator I =
730 AbstractTypeMap.find(cast<TypeClass>(OldTy));
731
732 assert(I != AbstractTypeMap.end() &&
733 "Abstract type not in AbstractTypeMap?");
734
735 // Convert a constant at a time until the last one is gone. The last one
736 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
737 // eliminated eventually.
738 do {
739 ConvertConstantType<ConstantClass,
740 TypeClass>::convert(I->second->second,
741 cast<TypeClass>(NewTy));
742
743 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
744 } while (I != AbstractTypeMap.end());
745 }
746
747 // If the type became concrete without being refined to any other existing
748 // type, we just remove ourselves from the ATU list.
749 void typeBecameConcrete(const DerivedType *AbsTy) {
750 AbsTy->removeAbstractTypeUser(this);
751 }
752
753 void dump() const {
754 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000755 }
756 };
757}
758
759
760
Chris Lattner3462ae32001-12-03 22:26:30 +0000761//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000762//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000763static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
764static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000765
Chris Lattner3462ae32001-12-03 22:26:30 +0000766ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000767 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000768}
769
Chris Lattner3462ae32001-12-03 22:26:30 +0000770ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000771 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000772}
773
Chris Lattner3462ae32001-12-03 22:26:30 +0000774ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000775 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000776 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
777 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000778}
779
Chris Lattner3462ae32001-12-03 22:26:30 +0000780//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000781//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000782namespace llvm {
783 template<>
784 struct ConstantCreator<ConstantFP, Type, uint64_t> {
785 static ConstantFP *create(const Type *Ty, uint64_t V) {
786 assert(Ty == Type::DoubleTy);
787 union {
788 double F;
789 uint64_t I;
790 } T;
791 T.I = V;
792 return new ConstantFP(Ty, T.F);
793 }
794 };
795 template<>
796 struct ConstantCreator<ConstantFP, Type, uint32_t> {
797 static ConstantFP *create(const Type *Ty, uint32_t V) {
798 assert(Ty == Type::FloatTy);
799 union {
800 float F;
801 uint32_t I;
802 } T;
803 T.I = V;
804 return new ConstantFP(Ty, T.F);
805 }
806 };
807}
808
809static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
810static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000811
Chris Lattner3462ae32001-12-03 22:26:30 +0000812ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000813 if (Ty == Type::FloatTy) {
814 // Force the value through memory to normalize it.
Chris Lattnerac80ea42004-02-01 22:49:04 +0000815 union {
816 float F;
817 uint32_t I;
818 } T;
819 T.F = (float)V;
820 return FloatConstants.getOrCreate(Ty, T.I);
821 } else {
822 assert(Ty == Type::DoubleTy);
823 union {
824 double F;
825 uint64_t I;
826 } T;
827 T.F = V;
828 return DoubleConstants.getOrCreate(Ty, T.I);
Chris Lattner241ed4c2004-01-23 00:55:21 +0000829 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000830}
831
Chris Lattner9fba3da2004-02-15 05:53:04 +0000832//---- ConstantAggregateZero::get() implementation...
833//
834namespace llvm {
835 // ConstantAggregateZero does not take extra "value" argument...
836 template<class ValType>
837 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
838 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
839 return new ConstantAggregateZero(Ty);
840 }
841 };
842
843 template<>
844 struct ConvertConstantType<ConstantAggregateZero, Type> {
845 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
846 // Make everyone now use a constant of the new type...
847 Constant *New = ConstantAggregateZero::get(NewTy);
848 assert(New != OldC && "Didn't replace constant??");
849 OldC->uncheckedReplaceAllUsesWith(New);
850 OldC->destroyConstant(); // This constant is now dead, destroy it.
851 }
852 };
853}
854
855static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
856
857Constant *ConstantAggregateZero::get(const Type *Ty) {
858 return AggZeroConstants.getOrCreate(Ty, 0);
859}
860
861// destroyConstant - Remove the constant from the constant table...
862//
863void ConstantAggregateZero::destroyConstant() {
864 AggZeroConstants.remove(this);
865 destroyConstantImpl();
866}
867
868void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
869 bool DisableChecking) {
870 assert(0 && "No uses!");
871 abort();
872}
873
874
875
Chris Lattner3462ae32001-12-03 22:26:30 +0000876//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000877//
Chris Lattner189d19f2003-11-21 20:23:48 +0000878namespace llvm {
879 template<>
880 struct ConvertConstantType<ConstantArray, ArrayType> {
881 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
882 // Make everyone now use a constant of the new type...
883 std::vector<Constant*> C;
884 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
885 C.push_back(cast<Constant>(OldC->getOperand(i)));
886 Constant *New = ConstantArray::get(NewTy, C);
887 assert(New != OldC && "Didn't replace constant??");
888 OldC->uncheckedReplaceAllUsesWith(New);
889 OldC->destroyConstant(); // This constant is now dead, destroy it.
890 }
891 };
892}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000893
Chris Lattner98fa07b2003-05-23 20:03:32 +0000894static ValueMap<std::vector<Constant*>, ArrayType,
895 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000896
Chris Lattner015e8212004-02-15 04:14:47 +0000897Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000898 const std::vector<Constant*> &V) {
899 // If this is an all-zero array, return a ConstantAggregateZero object
900 if (!V.empty()) {
901 Constant *C = V[0];
902 if (!C->isNullValue())
903 return ArrayConstants.getOrCreate(Ty, V);
904 for (unsigned i = 1, e = V.size(); i != e; ++i)
905 if (V[i] != C)
906 return ArrayConstants.getOrCreate(Ty, V);
907 }
908 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000909}
910
Chris Lattner98fa07b2003-05-23 20:03:32 +0000911// destroyConstant - Remove the constant from the constant table...
912//
913void ConstantArray::destroyConstant() {
914 ArrayConstants.remove(this);
915 destroyConstantImpl();
916}
917
Chris Lattner3462ae32001-12-03 22:26:30 +0000918// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000919// contain the specified string. A null terminator is added to the specified
920// string so that it may be used in a natural way...
921//
Chris Lattner015e8212004-02-15 04:14:47 +0000922Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000923 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000924
925 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000926 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000927
928 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000929 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000930
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000931 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000932 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000933}
934
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000935/// isString - This method returns true if the array is an array of sbyte or
936/// ubyte, and if the elements of the array are all ConstantInt's.
937bool ConstantArray::isString() const {
938 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000939 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000940 getType()->getElementType() != Type::SByteTy)
941 return false;
942 // Check the elements to make sure they are all integers, not constant
943 // expressions.
944 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
945 if (!isa<ConstantInt>(getOperand(i)))
946 return false;
947 return true;
948}
949
Chris Lattner81fabb02002-08-26 17:53:56 +0000950// getAsString - If the sub-element type of this array is either sbyte or ubyte,
951// then this method converts the array to an std::string and returns it.
952// Otherwise, it asserts out.
953//
954std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000955 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000956 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000957 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
958 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000959 return Result;
960}
961
962
Chris Lattner3462ae32001-12-03 22:26:30 +0000963//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000964//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000965
Chris Lattner189d19f2003-11-21 20:23:48 +0000966namespace llvm {
967 template<>
968 struct ConvertConstantType<ConstantStruct, StructType> {
969 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
970 // Make everyone now use a constant of the new type...
971 std::vector<Constant*> C;
972 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
973 C.push_back(cast<Constant>(OldC->getOperand(i)));
974 Constant *New = ConstantStruct::get(NewTy, C);
975 assert(New != OldC && "Didn't replace constant??");
976
977 OldC->uncheckedReplaceAllUsesWith(New);
978 OldC->destroyConstant(); // This constant is now dead, destroy it.
979 }
980 };
981}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000982
Chris Lattner98fa07b2003-05-23 20:03:32 +0000983static ValueMap<std::vector<Constant*>, StructType,
984 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000985
Chris Lattner015e8212004-02-15 04:14:47 +0000986Constant *ConstantStruct::get(const StructType *Ty,
987 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000988 // Create a ConstantAggregateZero value if all elements are zeros...
989 for (unsigned i = 0, e = V.size(); i != e; ++i)
990 if (!V[i]->isNullValue())
991 return StructConstants.getOrCreate(Ty, V);
992
993 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000994}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000995
Chris Lattnerd7a73302001-10-13 06:57:33 +0000996// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000997//
Chris Lattner3462ae32001-12-03 22:26:30 +0000998void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000999 StructConstants.remove(this);
1000 destroyConstantImpl();
1001}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001002
Chris Lattner3462ae32001-12-03 22:26:30 +00001003//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001004//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001005
Chris Lattner189d19f2003-11-21 20:23:48 +00001006namespace llvm {
1007 // ConstantPointerNull does not take extra "value" argument...
1008 template<class ValType>
1009 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1010 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1011 return new ConstantPointerNull(Ty);
1012 }
1013 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001014
Chris Lattner189d19f2003-11-21 20:23:48 +00001015 template<>
1016 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1017 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1018 // Make everyone now use a constant of the new type...
1019 Constant *New = ConstantPointerNull::get(NewTy);
1020 assert(New != OldC && "Didn't replace constant??");
1021 OldC->uncheckedReplaceAllUsesWith(New);
1022 OldC->destroyConstant(); // This constant is now dead, destroy it.
1023 }
1024 };
1025}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001026
Chris Lattner98fa07b2003-05-23 20:03:32 +00001027static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001028
Chris Lattner3462ae32001-12-03 22:26:30 +00001029ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001030 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001031}
1032
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001033// destroyConstant - Remove the constant from the constant table...
1034//
1035void ConstantPointerNull::destroyConstant() {
1036 NullPtrConstants.remove(this);
1037 destroyConstantImpl();
1038}
1039
1040
Chris Lattner3462ae32001-12-03 22:26:30 +00001041//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +00001042//
Chris Lattner3462ae32001-12-03 22:26:30 +00001043ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001044 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001045
Chris Lattnerd7a73302001-10-13 06:57:33 +00001046 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +00001047 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001048}
1049
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001050// destroyConstant - Remove the constant from the constant table...
1051//
1052void ConstantPointerRef::destroyConstant() {
1053 getValue()->getParent()->destroyConstantPointerRef(this);
1054 destroyConstantImpl();
1055}
1056
1057
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001058//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001059//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001060typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001061
Chris Lattner189d19f2003-11-21 20:23:48 +00001062namespace llvm {
1063 template<>
1064 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1065 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1066 if (V.first == Instruction::Cast)
1067 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
1068 if ((V.first >= Instruction::BinaryOpsBegin &&
1069 V.first < Instruction::BinaryOpsEnd) ||
1070 V.first == Instruction::Shl || V.first == Instruction::Shr)
1071 return new ConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001072 if (V.first == Instruction::Select)
1073 return new ConstantExpr(V.second[0], V.second[1], V.second[2]);
Chris Lattner189d19f2003-11-21 20:23:48 +00001074
1075 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
1076
1077 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
1078 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001079 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001080 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001081
Chris Lattner189d19f2003-11-21 20:23:48 +00001082 template<>
1083 struct ConvertConstantType<ConstantExpr, Type> {
1084 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1085 Constant *New;
1086 switch (OldC->getOpcode()) {
1087 case Instruction::Cast:
1088 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1089 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001090 case Instruction::Select:
1091 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1092 OldC->getOperand(1),
1093 OldC->getOperand(2));
1094 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001095 case Instruction::Shl:
1096 case Instruction::Shr:
1097 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1098 OldC->getOperand(0), OldC->getOperand(1));
1099 break;
1100 default:
1101 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1102 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1103 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1104 OldC->getOperand(1));
1105 break;
1106 case Instruction::GetElementPtr:
1107 // Make everyone now use a constant of the new type...
1108 std::vector<Constant*> C;
1109 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
1110 C.push_back(cast<Constant>(OldC->getOperand(i)));
1111 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
1112 break;
1113 }
1114
1115 assert(New != OldC && "Didn't replace constant??");
1116 OldC->uncheckedReplaceAllUsesWith(New);
1117 OldC->destroyConstant(); // This constant is now dead, destroy it.
1118 }
1119 };
1120} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001121
1122
Chris Lattner98fa07b2003-05-23 20:03:32 +00001123static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001124
Chris Lattner46b3d302003-04-16 22:40:51 +00001125Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001126 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1127
Chris Lattneracdbe712003-04-17 19:24:48 +00001128 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1129 return FC; // Fold a few common cases...
1130
Vikram S. Adve4c485332002-07-15 18:19:33 +00001131 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001132 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001133 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1134 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001135}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001136
Chris Lattnerdd284742004-04-04 23:20:30 +00001137Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
1138 assert(C->getType()->isInteger() && Ty->isInteger() &&
1139 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1140 "This is an illegal sign extension!");
1141 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1142 return ConstantExpr::getCast(C, Ty);
1143}
1144
1145Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
1146 assert(C->getType()->isInteger() && Ty->isInteger() &&
1147 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1148 "This is an illegal zero extension!");
1149 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
1150 return ConstantExpr::getCast(C, Ty);
1151}
1152
Chris Lattnerb50d1352003-10-05 00:17:43 +00001153Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1154 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001155 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1156 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001157 // Check the operands for consistency first
1158 assert((Opcode >= Instruction::BinaryOpsBegin &&
1159 Opcode < Instruction::BinaryOpsEnd) &&
1160 "Invalid opcode in binary constant expression");
1161 assert(C1->getType() == C2->getType() &&
1162 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001163
1164 if (ReqTy == C1->getType())
1165 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1166 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001167
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001168 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001169 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001170 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001171}
1172
Chris Lattner6e415c02004-03-12 05:54:04 +00001173Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1174 Constant *V1, Constant *V2) {
1175 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1176 assert(V1->getType() == V2->getType() && "Select value types must match!");
1177 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1178
1179 if (ReqTy == V1->getType())
1180 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1181 return SC; // Fold common cases
1182
1183 std::vector<Constant*> argVec(3, C);
1184 argVec[1] = V1;
1185 argVec[2] = V2;
1186 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1187 return ExprConstants.getOrCreate(ReqTy, Key);
1188}
1189
Chris Lattner9eb2b522004-01-12 19:12:58 +00001190/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001191Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1192 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001193 // Check the operands for consistency first
1194 assert((Opcode == Instruction::Shl ||
1195 Opcode == Instruction::Shr) &&
1196 "Invalid opcode in binary constant expression");
1197 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1198 "Invalid operand types for Shift constant expr!");
1199
Chris Lattner0bba7712004-01-12 20:40:42 +00001200 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001201 return FC; // Fold a few common cases...
1202
1203 // Look up the constant in the table first to ensure uniqueness
1204 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001205 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001206 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001207}
1208
1209
Chris Lattnerb50d1352003-10-05 00:17:43 +00001210Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1211 const std::vector<Constant*> &IdxList) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001212 assert(GetElementPtrInst::getIndexedType(C->getType(),
1213 std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
1214 "GEP indices invalid!");
1215
Chris Lattneracdbe712003-04-17 19:24:48 +00001216 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1217 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001218
Chris Lattnerb50d1352003-10-05 00:17:43 +00001219 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001220 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001221 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001222 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +00001223 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001224 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001225 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001226}
1227
Chris Lattnerb50d1352003-10-05 00:17:43 +00001228Constant *ConstantExpr::getGetElementPtr(Constant *C,
1229 const std::vector<Constant*> &IdxList){
1230 // Get the result type of the getelementptr!
1231 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1232
1233 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1234 true);
1235 assert(Ty && "GEP indices invalid!");
1236 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1237}
1238
1239
Vikram S. Adve4c485332002-07-15 18:19:33 +00001240// destroyConstant - Remove the constant from the constant table...
1241//
1242void ConstantExpr::destroyConstant() {
1243 ExprConstants.remove(this);
1244 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001245}
1246
Chris Lattner3cd8c562002-07-30 18:54:25 +00001247const char *ConstantExpr::getOpcodeName() const {
1248 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001249}