blob: c8e59609e9e2cfc0d72ba6335b4641a31a2501ee [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>
Reid Spencercf394bf2004-07-04 11:51:24 +000022#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chris Lattner3462ae32001-12-03 22:26:30 +000025ConstantBool *ConstantBool::True = new ConstantBool(true);
26ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000027
Chris Lattner9655e542001-07-20 19:16:02 +000028
Chris Lattner2f7c9632001-06-06 20:29:01 +000029//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000030// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000031//===----------------------------------------------------------------------===//
32
33// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000034void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000035 assert(ST && "Type::setName - Must provide symbol table argument!");
36
37 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000038}
39
Chris Lattner3462ae32001-12-03 22:26:30 +000040void Constant::destroyConstantImpl() {
41 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000042 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000043 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000044 // but they don't know that. Because we only find out when the CPV is
45 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000046 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000047 //
48 while (!use_empty()) {
49 Value *V = use_back();
50#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000051 if (!isa<Constant>(V))
52 std::cerr << "While deleting: " << *this
53 << "\n\nUse still stuck around after Def is destroyed: "
54 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000055#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000056 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000057 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000058 CPV->destroyConstant();
59
60 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000061 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000062 }
63
64 // Value has no outstanding references it is safe to delete it now...
65 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000066}
Chris Lattner2f7c9632001-06-06 20:29:01 +000067
Chris Lattnerb1585a92002-08-13 17:50:20 +000068// Static constructor to create a '0' constant of arbitrary type...
69Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000070 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000071 case Type::BoolTyID: {
72 static Constant *NullBool = ConstantBool::get(false);
73 return NullBool;
74 }
75 case Type::SByteTyID: {
76 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
77 return NullSByte;
78 }
79 case Type::UByteTyID: {
80 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
81 return NullUByte;
82 }
83 case Type::ShortTyID: {
84 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
85 return NullShort;
86 }
87 case Type::UShortTyID: {
88 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
89 return NullUShort;
90 }
91 case Type::IntTyID: {
92 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
93 return NullInt;
94 }
95 case Type::UIntTyID: {
96 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
97 return NullUInt;
98 }
99 case Type::LongTyID: {
100 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
101 return NullLong;
102 }
103 case Type::ULongTyID: {
104 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
105 return NullULong;
106 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000107
Chris Lattner3e88ef92003-10-03 19:34:51 +0000108 case Type::FloatTyID: {
109 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
110 return NullFloat;
111 }
112 case Type::DoubleTyID: {
113 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
114 return NullDouble;
115 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000116
117 case Type::PointerTyID:
118 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000119
Chris Lattner9fba3da2004-02-15 05:53:04 +0000120 case Type::StructTyID:
121 case Type::ArrayTyID:
122 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000123 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000124 // Function, Label, or Opaque type?
125 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000126 return 0;
127 }
128}
129
130// Static constructor to create the maximum constant of an integral type...
131ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000132 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000133 case Type::BoolTyID: return ConstantBool::True;
134 case Type::SByteTyID:
135 case Type::ShortTyID:
136 case Type::IntTyID:
137 case Type::LongTyID: {
138 // Calculate 011111111111111...
139 unsigned TypeBits = Ty->getPrimitiveSize()*8;
140 int64_t Val = INT64_MAX; // All ones
141 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
142 return ConstantSInt::get(Ty, Val);
143 }
144
145 case Type::UByteTyID:
146 case Type::UShortTyID:
147 case Type::UIntTyID:
148 case Type::ULongTyID: return getAllOnesValue(Ty);
149
Chris Lattner31408f72002-08-14 17:12:13 +0000150 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000151 }
152}
153
154// Static constructor to create the minimum constant for an integral type...
155ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000156 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000157 case Type::BoolTyID: return ConstantBool::False;
158 case Type::SByteTyID:
159 case Type::ShortTyID:
160 case Type::IntTyID:
161 case Type::LongTyID: {
162 // Calculate 1111111111000000000000
163 unsigned TypeBits = Ty->getPrimitiveSize()*8;
164 int64_t Val = -1; // All ones
165 Val <<= TypeBits-1; // Shift over to the right spot
166 return ConstantSInt::get(Ty, Val);
167 }
168
169 case Type::UByteTyID:
170 case Type::UShortTyID:
171 case Type::UIntTyID:
172 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
173
Chris Lattner31408f72002-08-14 17:12:13 +0000174 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000175 }
176}
177
178// Static constructor to create an integral constant with all bits set
179ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000180 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000181 case Type::BoolTyID: return ConstantBool::True;
182 case Type::SByteTyID:
183 case Type::ShortTyID:
184 case Type::IntTyID:
185 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
186
187 case Type::UByteTyID:
188 case Type::UShortTyID:
189 case Type::UIntTyID:
190 case Type::ULongTyID: {
191 // Calculate ~0 of the right type...
192 unsigned TypeBits = Ty->getPrimitiveSize()*8;
193 uint64_t Val = ~0ULL; // All ones
194 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
195 return ConstantUInt::get(Ty, Val);
196 }
Chris Lattner31408f72002-08-14 17:12:13 +0000197 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000198 }
199}
200
Chris Lattner83e5d392003-03-10 22:39:02 +0000201bool ConstantUInt::isAllOnesValue() const {
202 unsigned TypeBits = getType()->getPrimitiveSize()*8;
203 uint64_t Val = ~0ULL; // All ones
204 Val >>= 64-TypeBits; // Shift out inappropriate bits
205 return getValue() == Val;
206}
207
Chris Lattnerb1585a92002-08-13 17:50:20 +0000208
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000210// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000211//===----------------------------------------------------------------------===//
212
213//===----------------------------------------------------------------------===//
214// Normal Constructors
215
Chris Lattner265eb642004-06-21 12:12:12 +0000216ConstantIntegral::ConstantIntegral(const Type *Ty, uint64_t V)
217 : Constant(Ty) {
218 Val.Unsigned = V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000219}
Chris Lattner49d855c2001-09-07 16:46:31 +0000220
Chris Lattner265eb642004-06-21 12:12:12 +0000221ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy, V) {
222}
223
224ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty, V) {
Chris Lattner7309d662001-07-21 19:16:08 +0000225}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000226
Chris Lattner3462ae32001-12-03 22:26:30 +0000227ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000228 assert(Ty->isInteger() && Ty->isSigned() &&
229 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000230 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231}
232
Chris Lattner3462ae32001-12-03 22:26:30 +0000233ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000234 assert(Ty->isInteger() && Ty->isUnsigned() &&
235 "Illegal type for unsigned integer constant!");
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}
238
Chris Lattner3462ae32001-12-03 22:26:30 +0000239ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000240 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000241 Val = V;
242}
243
Chris Lattner3462ae32001-12-03 22:26:30 +0000244ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000245 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000246 Operands.reserve(V.size());
247 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerab69ecc2003-08-18 16:54:48 +0000248 assert(V[i]->getType() == T->getElementType() ||
249 (T->isAbstract() &&
Chris Lattner6b727592004-06-17 18:19:28 +0000250 V[i]->getType()->getTypeID() == T->getElementType()->getTypeID()));
Chris Lattnera073acb2001-07-07 08:36:50 +0000251 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000252 }
253}
254
Chris Lattner3462ae32001-12-03 22:26:30 +0000255ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000256 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000257 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000258 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000259 Operands.reserve(V.size());
260 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000261 assert((V[i]->getType() == T->getElementType(i) ||
262 ((T->getElementType(i)->isAbstract() ||
263 V[i]->getType()->isAbstract()) &&
Chris Lattner6b727592004-06-17 18:19:28 +0000264 T->getElementType(i)->getTypeID() == V[i]->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000265 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000266 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000267 }
268}
269
Chris Lattner3462ae32001-12-03 22:26:30 +0000270ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
Chris Lattnere120a732003-11-17 19:47:21 +0000271 : Constant(GV->getType()) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000272 Operands.reserve(1);
Chris Lattner60e0dd72001-10-03 06:12:09 +0000273 Operands.push_back(Use(GV, this));
274}
275
Chris Lattner3cd8c562002-07-30 18:54:25 +0000276ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
277 : Constant(Ty), iType(Opcode) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000278 Operands.reserve(1);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000279 Operands.push_back(Use(C, this));
280}
281
Chris Lattner6e415c02004-03-12 05:54:04 +0000282// Select instruction creation ctor
283ConstantExpr::ConstantExpr(Constant *C, Constant *V1, Constant *V2)
284 : Constant(V1->getType()), iType(Instruction::Select) {
285 Operands.reserve(3);
286 Operands.push_back(Use(C, this));
287 Operands.push_back(Use(V1, this));
288 Operands.push_back(Use(V2, this));
289}
290
291
Chris Lattner22ced562003-06-22 20:48:30 +0000292static bool isSetCC(unsigned Opcode) {
293 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
294 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
295 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
296}
297
Chris Lattner3cd8c562002-07-30 18:54:25 +0000298ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner22ced562003-06-22 20:48:30 +0000299 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000300 Operands.reserve(2);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000301 Operands.push_back(Use(C1, this));
302 Operands.push_back(Use(C2, this));
303}
304
Chris Lattner3cd8c562002-07-30 18:54:25 +0000305ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
306 const Type *DestTy)
307 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000308 Operands.reserve(1+IdxList.size());
309 Operands.push_back(Use(C, this));
310 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
311 Operands.push_back(Use(IdxList[i], this));
312}
313
Chris Lattner817175f2004-03-29 02:37:53 +0000314/// ConstantExpr::get* - Return some common constants without having to
315/// specify the full Instruction::OPCODE identifier.
316///
317Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000318 if (!C->getType()->isFloatingPoint())
319 return get(Instruction::Sub, getNullValue(C->getType()), C);
320 else
321 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000322}
323Constant *ConstantExpr::getNot(Constant *C) {
324 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
325 return get(Instruction::Xor, C,
326 ConstantIntegral::getAllOnesValue(C->getType()));
327}
328Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
329 return get(Instruction::Add, C1, C2);
330}
331Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
332 return get(Instruction::Sub, C1, C2);
333}
334Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
335 return get(Instruction::Mul, C1, C2);
336}
337Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
338 return get(Instruction::Div, C1, C2);
339}
340Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
341 return get(Instruction::Rem, C1, C2);
342}
343Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
344 return get(Instruction::And, C1, C2);
345}
346Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
347 return get(Instruction::Or, C1, C2);
348}
349Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
350 return get(Instruction::Xor, C1, C2);
351}
352Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
353 return get(Instruction::SetEQ, C1, C2);
354}
355Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
356 return get(Instruction::SetNE, C1, C2);
357}
358Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
359 return get(Instruction::SetLT, C1, C2);
360}
361Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
362 return get(Instruction::SetGT, C1, C2);
363}
364Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
365 return get(Instruction::SetLE, C1, C2);
366}
367Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
368 return get(Instruction::SetGE, C1, C2);
369}
370Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
371 return get(Instruction::Shl, C1, C2);
372}
373Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
374 return get(Instruction::Shr, C1, C2);
375}
376
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000377Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
378 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
379 return getCast(getShr(getCast(C1,
380 C1->getType()->getUnsignedVersion()), C2), C1->getType());
381}
Chris Lattner817175f2004-03-29 02:37:53 +0000382
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000383Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
384 if (C1->getType()->isSigned()) return getShr(C1, C2);
385 return getCast(getShr(getCast(C1,
386 C1->getType()->getSignedVersion()), C2), C1->getType());
387}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000388
Chris Lattner2f7c9632001-06-06 20:29:01 +0000389
390//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000391// classof implementations
392
Chris Lattnerb1585a92002-08-13 17:50:20 +0000393bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000394 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000395}
396
Chris Lattner3462ae32001-12-03 22:26:30 +0000397bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000398 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000399}
Chris Lattner3462ae32001-12-03 22:26:30 +0000400bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000401 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000402}
Chris Lattner3462ae32001-12-03 22:26:30 +0000403bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000404 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000405}
Chris Lattner3462ae32001-12-03 22:26:30 +0000406bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000407 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000408 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000409 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000410}
Chris Lattner9fba3da2004-02-15 05:53:04 +0000411bool ConstantAggregateZero::classof(const Constant *CPV) {
412 return (isa<ArrayType>(CPV->getType()) || isa<StructType>(CPV->getType())) &&
413 CPV->isNullValue();
414}
Chris Lattner3462ae32001-12-03 22:26:30 +0000415bool ConstantArray::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000416 return isa<ArrayType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000417}
Chris Lattner3462ae32001-12-03 22:26:30 +0000418bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000419 return isa<StructType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000420}
Chris Lattnere120a732003-11-17 19:47:21 +0000421
422bool ConstantPointerNull::classof(const Constant *CPV) {
423 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
424 CPV->getNumOperands() == 0;
425}
426
427bool ConstantPointerRef::classof(const Constant *CPV) {
428 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
429 CPV->getNumOperands() == 1;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000430}
431
432
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000433
Chris Lattner2f7c9632001-06-06 20:29:01 +0000434//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000435// isValueValidForType implementations
436
Chris Lattner3462ae32001-12-03 22:26:30 +0000437bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000438 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000439 default:
440 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000441 // Signed types...
442 case Type::SByteTyID:
443 return (Val <= INT8_MAX && Val >= INT8_MIN);
444 case Type::ShortTyID:
445 return (Val <= INT16_MAX && Val >= INT16_MIN);
446 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000447 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000448 case Type::LongTyID:
449 return true; // This is the largest type...
450 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000451}
452
Chris Lattner3462ae32001-12-03 22:26:30 +0000453bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000454 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000455 default:
456 return false; // These can't be represented as integers!!!
457
458 // Unsigned types...
459 case Type::UByteTyID:
460 return (Val <= UINT8_MAX);
461 case Type::UShortTyID:
462 return (Val <= UINT16_MAX);
463 case Type::UIntTyID:
464 return (Val <= UINT32_MAX);
465 case Type::ULongTyID:
466 return true; // This is the largest type...
467 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000468}
469
Chris Lattner3462ae32001-12-03 22:26:30 +0000470bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000471 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000472 default:
473 return false; // These can't be represented as floating point!
474
475 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000476 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000477 case Type::DoubleTyID:
478 return true; // This is the largest type...
479 }
480};
Chris Lattner9655e542001-07-20 19:16:02 +0000481
Chris Lattner49d855c2001-09-07 16:46:31 +0000482//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000483// replaceUsesOfWithOnConstant implementations
484
Chris Lattnerc27038d2003-08-29 05:36:46 +0000485void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
486 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000487 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
488
489 std::vector<Constant*> Values;
490 Values.reserve(getValues().size()); // Build replacement array...
491 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
492 Constant *Val = cast<Constant>(getValues()[i]);
493 if (Val == From) Val = cast<Constant>(To);
494 Values.push_back(Val);
495 }
496
Chris Lattnerc75bf522004-02-15 04:05:58 +0000497 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000498 assert(Replacement != this && "I didn't contain From!");
499
500 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000501 if (DisableChecking)
502 uncheckedReplaceAllUsesWith(Replacement);
503 else
504 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000505
506 // Delete the old constant!
507 destroyConstant();
508}
509
Chris Lattnerc27038d2003-08-29 05:36:46 +0000510void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
511 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000512 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
513
514 std::vector<Constant*> Values;
515 Values.reserve(getValues().size());
516 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
517 Constant *Val = cast<Constant>(getValues()[i]);
518 if (Val == From) Val = cast<Constant>(To);
519 Values.push_back(Val);
520 }
521
Chris Lattner37a716f2004-02-15 04:07:32 +0000522 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000523 assert(Replacement != this && "I didn't contain From!");
524
525 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000526 if (DisableChecking)
527 uncheckedReplaceAllUsesWith(Replacement);
528 else
529 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000530
531 // Delete the old constant!
532 destroyConstant();
533}
534
Chris Lattnerc27038d2003-08-29 05:36:46 +0000535void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
536 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000537 if (isa<GlobalValue>(To)) {
538 assert(From == getOperand(0) && "Doesn't contain from!");
539 ConstantPointerRef *Replacement =
540 ConstantPointerRef::get(cast<GlobalValue>(To));
541
542 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000543 if (DisableChecking)
544 uncheckedReplaceAllUsesWith(Replacement);
545 else
546 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000547
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000548 } else {
549 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000550 if (DisableChecking)
551 uncheckedReplaceAllUsesWith(To);
552 else
553 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000554 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000555
556 // Delete the old constant!
557 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000558}
559
Chris Lattnerc27038d2003-08-29 05:36:46 +0000560void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
561 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000562 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
563 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000564
Chris Lattner46b3d302003-04-16 22:40:51 +0000565 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000566 if (getOpcode() == Instruction::GetElementPtr) {
567 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000568 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000569 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000570 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000571
572 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000573 Constant *Val = getOperand(i);
574 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000575 Indices.push_back(Val);
576 }
577 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
578 } else if (getOpcode() == Instruction::Cast) {
579 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000580 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattner467cb2b2004-03-31 02:56:11 +0000581 } else if (getOpcode() == Instruction::Select) {
582 Constant *C1 = getOperand(0);
583 Constant *C2 = getOperand(1);
584 Constant *C3 = getOperand(2);
585 if (C1 == From) C1 = To;
586 if (C2 == From) C2 = To;
587 if (C3 == From) C3 = To;
588 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000589 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000590 Constant *C1 = getOperand(0);
591 Constant *C2 = getOperand(1);
592 if (C1 == From) C1 = To;
593 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000594 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
595 } else {
596 assert(0 && "Unknown ConstantExpr type!");
597 return;
598 }
599
600 assert(Replacement != this && "I didn't contain From!");
601
602 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000603 if (DisableChecking)
604 uncheckedReplaceAllUsesWith(Replacement);
605 else
606 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000607
608 // Delete the old constant!
609 destroyConstant();
610}
611
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000612//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000613// Factory Function Implementation
614
Chris Lattner98fa07b2003-05-23 20:03:32 +0000615// ConstantCreator - A class that is used to create constants by
616// ValueMap*. This class should be partially specialized if there is
617// something strange that needs to be done to interface to the ctor for the
618// constant.
619//
Chris Lattner189d19f2003-11-21 20:23:48 +0000620namespace llvm {
621 template<class ConstantClass, class TypeClass, class ValType>
622 struct ConstantCreator {
623 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
624 return new ConstantClass(Ty, V);
625 }
626 };
627
628 template<class ConstantClass, class TypeClass>
629 struct ConvertConstantType {
630 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
631 assert(0 && "This type cannot be converted!\n");
632 abort();
633 }
634 };
635}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000636
Chris Lattner98fa07b2003-05-23 20:03:32 +0000637namespace {
638 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000639 class ValueMap : public AbstractTypeUser {
640 typedef std::pair<const TypeClass*, ValType> MapKey;
641 typedef std::map<MapKey, ConstantClass *> MapTy;
642 typedef typename MapTy::iterator MapIterator;
643 MapTy Map;
644
645 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
646 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000647 public:
648 // getOrCreate - Return the specified constant from the map, creating it if
649 // necessary.
650 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000651 MapKey Lookup(Ty, V);
652 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000653 if (I != Map.end() && I->first == Lookup)
654 return I->second; // Is it in the map?
655
656 // If no preexisting value, create one now...
657 ConstantClass *Result =
658 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
659
Chris Lattnerb50d1352003-10-05 00:17:43 +0000660
661 /// FIXME: why does this assert fail when loading 176.gcc?
662 //assert(Result->getType() == Ty && "Type specified is not correct!");
663 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
664
665 // If the type of the constant is abstract, make sure that an entry exists
666 // for it in the AbstractTypeMap.
667 if (Ty->isAbstract()) {
668 typename AbstractTypeMapTy::iterator TI =
669 AbstractTypeMap.lower_bound(Ty);
670
671 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
672 // Add ourselves to the ATU list of the type.
673 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
674
675 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
676 }
677 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000678 return Result;
679 }
680
681 void remove(ConstantClass *CP) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000682 // FIXME: This should not use a linear scan. If this gets to be a
683 // performance problem, someone should look at this.
684 MapIterator I = Map.begin();
685 for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
686 /* empty */;
687
688 assert(I != Map.end() && "Constant not found in constant table!");
689
690 // Now that we found the entry, make sure this isn't the entry that
691 // the AbstractTypeMap points to.
692 const TypeClass *Ty = I->first.first;
693 if (Ty->isAbstract()) {
694 assert(AbstractTypeMap.count(Ty) &&
695 "Abstract type not in AbstractTypeMap?");
696 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
697 if (ATMEntryIt == I) {
698 // Yes, we are removing the representative entry for this type.
699 // See if there are any other entries of the same type.
700 MapIterator TmpIt = ATMEntryIt;
701
702 // First check the entry before this one...
703 if (TmpIt != Map.begin()) {
704 --TmpIt;
705 if (TmpIt->first.first != Ty) // Not the same type, move back...
706 ++TmpIt;
707 }
708
709 // If we didn't find the same type, try to move forward...
710 if (TmpIt == ATMEntryIt) {
711 ++TmpIt;
712 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
713 --TmpIt; // No entry afterwards with the same type
714 }
715
716 // If there is another entry in the map of the same abstract type,
717 // update the AbstractTypeMap entry now.
718 if (TmpIt != ATMEntryIt) {
719 ATMEntryIt = TmpIt;
720 } else {
721 // Otherwise, we are removing the last instance of this type
722 // from the table. Remove from the ATM, and from user list.
723 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
724 AbstractTypeMap.erase(Ty);
725 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000726 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000727 }
728
729 Map.erase(I);
730 }
731
732 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
733 typename AbstractTypeMapTy::iterator I =
734 AbstractTypeMap.find(cast<TypeClass>(OldTy));
735
736 assert(I != AbstractTypeMap.end() &&
737 "Abstract type not in AbstractTypeMap?");
738
739 // Convert a constant at a time until the last one is gone. The last one
740 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
741 // eliminated eventually.
742 do {
743 ConvertConstantType<ConstantClass,
744 TypeClass>::convert(I->second->second,
745 cast<TypeClass>(NewTy));
746
747 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
748 } while (I != AbstractTypeMap.end());
749 }
750
751 // If the type became concrete without being refined to any other existing
752 // type, we just remove ourselves from the ATU list.
753 void typeBecameConcrete(const DerivedType *AbsTy) {
754 AbsTy->removeAbstractTypeUser(this);
755 }
756
757 void dump() const {
758 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000759 }
760 };
761}
762
763
764
Chris Lattner3462ae32001-12-03 22:26:30 +0000765//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000766//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000767static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
768static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000769
Chris Lattner3462ae32001-12-03 22:26:30 +0000770ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000771 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000772}
773
Chris Lattner3462ae32001-12-03 22:26:30 +0000774ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000775 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000776}
777
Chris Lattner3462ae32001-12-03 22:26:30 +0000778ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000779 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000780 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
781 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000782}
783
Chris Lattner3462ae32001-12-03 22:26:30 +0000784//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000785//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000786namespace llvm {
787 template<>
788 struct ConstantCreator<ConstantFP, Type, uint64_t> {
789 static ConstantFP *create(const Type *Ty, uint64_t V) {
790 assert(Ty == Type::DoubleTy);
791 union {
792 double F;
793 uint64_t I;
794 } T;
795 T.I = V;
796 return new ConstantFP(Ty, T.F);
797 }
798 };
799 template<>
800 struct ConstantCreator<ConstantFP, Type, uint32_t> {
801 static ConstantFP *create(const Type *Ty, uint32_t V) {
802 assert(Ty == Type::FloatTy);
803 union {
804 float F;
805 uint32_t I;
806 } T;
807 T.I = V;
808 return new ConstantFP(Ty, T.F);
809 }
810 };
811}
812
813static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
814static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000815
Chris Lattner3462ae32001-12-03 22:26:30 +0000816ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000817 if (Ty == Type::FloatTy) {
818 // Force the value through memory to normalize it.
Chris Lattnerac80ea42004-02-01 22:49:04 +0000819 union {
820 float F;
821 uint32_t I;
822 } T;
823 T.F = (float)V;
824 return FloatConstants.getOrCreate(Ty, T.I);
825 } else {
826 assert(Ty == Type::DoubleTy);
827 union {
828 double F;
829 uint64_t I;
830 } T;
831 T.F = V;
832 return DoubleConstants.getOrCreate(Ty, T.I);
Chris Lattner241ed4c2004-01-23 00:55:21 +0000833 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000834}
835
Chris Lattner9fba3da2004-02-15 05:53:04 +0000836//---- ConstantAggregateZero::get() implementation...
837//
838namespace llvm {
839 // ConstantAggregateZero does not take extra "value" argument...
840 template<class ValType>
841 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
842 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
843 return new ConstantAggregateZero(Ty);
844 }
845 };
846
847 template<>
848 struct ConvertConstantType<ConstantAggregateZero, Type> {
849 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
850 // Make everyone now use a constant of the new type...
851 Constant *New = ConstantAggregateZero::get(NewTy);
852 assert(New != OldC && "Didn't replace constant??");
853 OldC->uncheckedReplaceAllUsesWith(New);
854 OldC->destroyConstant(); // This constant is now dead, destroy it.
855 }
856 };
857}
858
859static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
860
861Constant *ConstantAggregateZero::get(const Type *Ty) {
862 return AggZeroConstants.getOrCreate(Ty, 0);
863}
864
865// destroyConstant - Remove the constant from the constant table...
866//
867void ConstantAggregateZero::destroyConstant() {
868 AggZeroConstants.remove(this);
869 destroyConstantImpl();
870}
871
872void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
873 bool DisableChecking) {
874 assert(0 && "No uses!");
875 abort();
876}
877
878
879
Chris Lattner3462ae32001-12-03 22:26:30 +0000880//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000881//
Chris Lattner189d19f2003-11-21 20:23:48 +0000882namespace llvm {
883 template<>
884 struct ConvertConstantType<ConstantArray, ArrayType> {
885 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
886 // Make everyone now use a constant of the new type...
887 std::vector<Constant*> C;
888 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
889 C.push_back(cast<Constant>(OldC->getOperand(i)));
890 Constant *New = ConstantArray::get(NewTy, C);
891 assert(New != OldC && "Didn't replace constant??");
892 OldC->uncheckedReplaceAllUsesWith(New);
893 OldC->destroyConstant(); // This constant is now dead, destroy it.
894 }
895 };
896}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000897
Chris Lattner98fa07b2003-05-23 20:03:32 +0000898static ValueMap<std::vector<Constant*>, ArrayType,
899 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000900
Chris Lattner015e8212004-02-15 04:14:47 +0000901Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000902 const std::vector<Constant*> &V) {
903 // If this is an all-zero array, return a ConstantAggregateZero object
904 if (!V.empty()) {
905 Constant *C = V[0];
906 if (!C->isNullValue())
907 return ArrayConstants.getOrCreate(Ty, V);
908 for (unsigned i = 1, e = V.size(); i != e; ++i)
909 if (V[i] != C)
910 return ArrayConstants.getOrCreate(Ty, V);
911 }
912 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000913}
914
Chris Lattner98fa07b2003-05-23 20:03:32 +0000915// destroyConstant - Remove the constant from the constant table...
916//
917void ConstantArray::destroyConstant() {
918 ArrayConstants.remove(this);
919 destroyConstantImpl();
920}
921
Chris Lattner3462ae32001-12-03 22:26:30 +0000922// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000923// contain the specified string. A null terminator is added to the specified
924// string so that it may be used in a natural way...
925//
Chris Lattner015e8212004-02-15 04:14:47 +0000926Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000927 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000928
929 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000930 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000931
932 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000933 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000934
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000935 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000936 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000937}
938
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000939/// isString - This method returns true if the array is an array of sbyte or
940/// ubyte, and if the elements of the array are all ConstantInt's.
941bool ConstantArray::isString() const {
942 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000943 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000944 getType()->getElementType() != Type::SByteTy)
945 return false;
946 // Check the elements to make sure they are all integers, not constant
947 // expressions.
948 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
949 if (!isa<ConstantInt>(getOperand(i)))
950 return false;
951 return true;
952}
953
Chris Lattner81fabb02002-08-26 17:53:56 +0000954// getAsString - If the sub-element type of this array is either sbyte or ubyte,
955// then this method converts the array to an std::string and returns it.
956// Otherwise, it asserts out.
957//
958std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000959 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000960 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000961 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
962 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000963 return Result;
964}
965
966
Chris Lattner3462ae32001-12-03 22:26:30 +0000967//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000968//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000969
Chris Lattner189d19f2003-11-21 20:23:48 +0000970namespace llvm {
971 template<>
972 struct ConvertConstantType<ConstantStruct, StructType> {
973 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
974 // Make everyone now use a constant of the new type...
975 std::vector<Constant*> C;
976 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
977 C.push_back(cast<Constant>(OldC->getOperand(i)));
978 Constant *New = ConstantStruct::get(NewTy, C);
979 assert(New != OldC && "Didn't replace constant??");
980
981 OldC->uncheckedReplaceAllUsesWith(New);
982 OldC->destroyConstant(); // This constant is now dead, destroy it.
983 }
984 };
985}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000986
Chris Lattner98fa07b2003-05-23 20:03:32 +0000987static ValueMap<std::vector<Constant*>, StructType,
988 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000989
Chris Lattner015e8212004-02-15 04:14:47 +0000990Constant *ConstantStruct::get(const StructType *Ty,
991 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000992 // Create a ConstantAggregateZero value if all elements are zeros...
993 for (unsigned i = 0, e = V.size(); i != e; ++i)
994 if (!V[i]->isNullValue())
995 return StructConstants.getOrCreate(Ty, V);
996
997 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000998}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000999
Chris Lattnerd7a73302001-10-13 06:57:33 +00001000// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001001//
Chris Lattner3462ae32001-12-03 22:26:30 +00001002void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001003 StructConstants.remove(this);
1004 destroyConstantImpl();
1005}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001006
Chris Lattner3462ae32001-12-03 22:26:30 +00001007//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001008//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001009
Chris Lattner189d19f2003-11-21 20:23:48 +00001010namespace llvm {
1011 // ConstantPointerNull does not take extra "value" argument...
1012 template<class ValType>
1013 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1014 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1015 return new ConstantPointerNull(Ty);
1016 }
1017 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001018
Chris Lattner189d19f2003-11-21 20:23:48 +00001019 template<>
1020 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1021 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1022 // Make everyone now use a constant of the new type...
1023 Constant *New = ConstantPointerNull::get(NewTy);
1024 assert(New != OldC && "Didn't replace constant??");
1025 OldC->uncheckedReplaceAllUsesWith(New);
1026 OldC->destroyConstant(); // This constant is now dead, destroy it.
1027 }
1028 };
1029}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001030
Chris Lattner98fa07b2003-05-23 20:03:32 +00001031static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001032
Chris Lattner3462ae32001-12-03 22:26:30 +00001033ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001034 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001035}
1036
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001037// destroyConstant - Remove the constant from the constant table...
1038//
1039void ConstantPointerNull::destroyConstant() {
1040 NullPtrConstants.remove(this);
1041 destroyConstantImpl();
1042}
1043
1044
Chris Lattner3462ae32001-12-03 22:26:30 +00001045//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +00001046//
Chris Lattner3462ae32001-12-03 22:26:30 +00001047ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001048 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001049
Chris Lattnerd7a73302001-10-13 06:57:33 +00001050 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +00001051 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001052}
1053
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001054// destroyConstant - Remove the constant from the constant table...
1055//
1056void ConstantPointerRef::destroyConstant() {
1057 getValue()->getParent()->destroyConstantPointerRef(this);
1058 destroyConstantImpl();
1059}
1060
1061
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001062//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001063//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001064typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001065
Chris Lattner189d19f2003-11-21 20:23:48 +00001066namespace llvm {
1067 template<>
1068 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1069 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1070 if (V.first == Instruction::Cast)
1071 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
1072 if ((V.first >= Instruction::BinaryOpsBegin &&
1073 V.first < Instruction::BinaryOpsEnd) ||
1074 V.first == Instruction::Shl || V.first == Instruction::Shr)
1075 return new ConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001076 if (V.first == Instruction::Select)
1077 return new ConstantExpr(V.second[0], V.second[1], V.second[2]);
Chris Lattner189d19f2003-11-21 20:23:48 +00001078
1079 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
1080
1081 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
1082 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001083 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001084 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001085
Chris Lattner189d19f2003-11-21 20:23:48 +00001086 template<>
1087 struct ConvertConstantType<ConstantExpr, Type> {
1088 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1089 Constant *New;
1090 switch (OldC->getOpcode()) {
1091 case Instruction::Cast:
1092 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1093 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001094 case Instruction::Select:
1095 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1096 OldC->getOperand(1),
1097 OldC->getOperand(2));
1098 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001099 case Instruction::Shl:
1100 case Instruction::Shr:
1101 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1102 OldC->getOperand(0), OldC->getOperand(1));
1103 break;
1104 default:
1105 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1106 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1107 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1108 OldC->getOperand(1));
1109 break;
1110 case Instruction::GetElementPtr:
1111 // Make everyone now use a constant of the new type...
1112 std::vector<Constant*> C;
1113 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
1114 C.push_back(cast<Constant>(OldC->getOperand(i)));
1115 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
1116 break;
1117 }
1118
1119 assert(New != OldC && "Didn't replace constant??");
1120 OldC->uncheckedReplaceAllUsesWith(New);
1121 OldC->destroyConstant(); // This constant is now dead, destroy it.
1122 }
1123 };
1124} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001125
1126
Chris Lattner98fa07b2003-05-23 20:03:32 +00001127static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001128
Chris Lattner46b3d302003-04-16 22:40:51 +00001129Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001130 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1131
Chris Lattneracdbe712003-04-17 19:24:48 +00001132 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1133 return FC; // Fold a few common cases...
1134
Vikram S. Adve4c485332002-07-15 18:19:33 +00001135 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001136 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001137 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1138 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001139}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001140
Chris Lattnerdd284742004-04-04 23:20:30 +00001141Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
1142 assert(C->getType()->isInteger() && Ty->isInteger() &&
1143 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1144 "This is an illegal sign extension!");
1145 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1146 return ConstantExpr::getCast(C, Ty);
1147}
1148
1149Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
1150 assert(C->getType()->isInteger() && Ty->isInteger() &&
1151 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1152 "This is an illegal zero extension!");
1153 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
1154 return ConstantExpr::getCast(C, Ty);
1155}
1156
Chris Lattnerb50d1352003-10-05 00:17:43 +00001157Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1158 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001159 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1160 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001161 // Check the operands for consistency first
1162 assert((Opcode >= Instruction::BinaryOpsBegin &&
1163 Opcode < Instruction::BinaryOpsEnd) &&
1164 "Invalid opcode in binary constant expression");
1165 assert(C1->getType() == C2->getType() &&
1166 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001167
1168 if (ReqTy == C1->getType())
1169 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1170 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001171
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001172 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001173 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001174 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001175}
1176
Chris Lattner6e415c02004-03-12 05:54:04 +00001177Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1178 Constant *V1, Constant *V2) {
1179 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1180 assert(V1->getType() == V2->getType() && "Select value types must match!");
1181 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1182
1183 if (ReqTy == V1->getType())
1184 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1185 return SC; // Fold common cases
1186
1187 std::vector<Constant*> argVec(3, C);
1188 argVec[1] = V1;
1189 argVec[2] = V2;
1190 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1191 return ExprConstants.getOrCreate(ReqTy, Key);
1192}
1193
Chris Lattner9eb2b522004-01-12 19:12:58 +00001194/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001195Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1196 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001197 // Check the operands for consistency first
1198 assert((Opcode == Instruction::Shl ||
1199 Opcode == Instruction::Shr) &&
1200 "Invalid opcode in binary constant expression");
1201 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1202 "Invalid operand types for Shift constant expr!");
1203
Chris Lattner0bba7712004-01-12 20:40:42 +00001204 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001205 return FC; // Fold a few common cases...
1206
1207 // Look up the constant in the table first to ensure uniqueness
1208 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001209 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001210 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001211}
1212
1213
Chris Lattnerb50d1352003-10-05 00:17:43 +00001214Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1215 const std::vector<Constant*> &IdxList) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001216 assert(GetElementPtrInst::getIndexedType(C->getType(),
1217 std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
1218 "GEP indices invalid!");
1219
Chris Lattneracdbe712003-04-17 19:24:48 +00001220 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1221 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001222
Chris Lattnerb50d1352003-10-05 00:17:43 +00001223 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001224 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001225 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001226 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +00001227 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001228 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001229 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001230}
1231
Chris Lattnerb50d1352003-10-05 00:17:43 +00001232Constant *ConstantExpr::getGetElementPtr(Constant *C,
1233 const std::vector<Constant*> &IdxList){
1234 // Get the result type of the getelementptr!
1235 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1236
1237 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1238 true);
1239 assert(Ty && "GEP indices invalid!");
1240 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1241}
1242
1243
Vikram S. Adve4c485332002-07-15 18:19:33 +00001244// destroyConstant - Remove the constant from the constant table...
1245//
1246void ConstantExpr::destroyConstant() {
1247 ExprConstants.remove(this);
1248 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001249}
1250
Chris Lattner3cd8c562002-07-30 18:54:25 +00001251const char *ConstantExpr::getOpcodeName() const {
1252 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001253}