blob: 527eff5490802eb9525a321f1f9aed02451a5940 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattneracdbe712003-04-17 19:24:48 +000015#include "llvm/ConstantHandling.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Vikram S. Adve4e537b22002-07-14 23:13:17 +000017#include "llvm/iMemory.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000019#include "llvm/Module.h"
Chris Lattner5de22042001-11-27 00:03:19 +000020#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000021#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000022
Brian Gaeke960707c2003-11-11 22:41:34 +000023namespace llvm {
24
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) {
70 switch (Ty->getPrimitiveID()) {
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 Lattnerc33ae672003-03-06 21:02:18 +0000120 case Type::StructTyID: {
121 const StructType *ST = cast<StructType>(Ty);
Chris Lattnerc33ae672003-03-06 21:02:18 +0000122 const StructType::ElementTypes &ETs = ST->getElementTypes();
123 std::vector<Constant*> Elements;
124 Elements.resize(ETs.size());
125 for (unsigned i = 0, e = ETs.size(); i != e; ++i)
126 Elements[i] = Constant::getNullValue(ETs[i]);
127 return ConstantStruct::get(ST, Elements);
128 }
129 case Type::ArrayTyID: {
130 const ArrayType *AT = cast<ArrayType>(Ty);
131 Constant *El = Constant::getNullValue(AT->getElementType());
132 unsigned NumElements = AT->getNumElements();
133 return ConstantArray::get(AT, std::vector<Constant*>(NumElements, El));
134 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000135 default:
Chris Lattnerc33ae672003-03-06 21:02:18 +0000136 // Function, Type, Label, or Opaque type?
137 assert(0 && "Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000138 return 0;
139 }
140}
141
142// Static constructor to create the maximum constant of an integral type...
143ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
144 switch (Ty->getPrimitiveID()) {
145 case Type::BoolTyID: return ConstantBool::True;
146 case Type::SByteTyID:
147 case Type::ShortTyID:
148 case Type::IntTyID:
149 case Type::LongTyID: {
150 // Calculate 011111111111111...
151 unsigned TypeBits = Ty->getPrimitiveSize()*8;
152 int64_t Val = INT64_MAX; // All ones
153 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
154 return ConstantSInt::get(Ty, Val);
155 }
156
157 case Type::UByteTyID:
158 case Type::UShortTyID:
159 case Type::UIntTyID:
160 case Type::ULongTyID: return getAllOnesValue(Ty);
161
Chris Lattner31408f72002-08-14 17:12:13 +0000162 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000163 }
164}
165
166// Static constructor to create the minimum constant for an integral type...
167ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
168 switch (Ty->getPrimitiveID()) {
169 case Type::BoolTyID: return ConstantBool::False;
170 case Type::SByteTyID:
171 case Type::ShortTyID:
172 case Type::IntTyID:
173 case Type::LongTyID: {
174 // Calculate 1111111111000000000000
175 unsigned TypeBits = Ty->getPrimitiveSize()*8;
176 int64_t Val = -1; // All ones
177 Val <<= TypeBits-1; // Shift over to the right spot
178 return ConstantSInt::get(Ty, Val);
179 }
180
181 case Type::UByteTyID:
182 case Type::UShortTyID:
183 case Type::UIntTyID:
184 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
185
Chris Lattner31408f72002-08-14 17:12:13 +0000186 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000187 }
188}
189
190// Static constructor to create an integral constant with all bits set
191ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
192 switch (Ty->getPrimitiveID()) {
193 case Type::BoolTyID: return ConstantBool::True;
194 case Type::SByteTyID:
195 case Type::ShortTyID:
196 case Type::IntTyID:
197 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
198
199 case Type::UByteTyID:
200 case Type::UShortTyID:
201 case Type::UIntTyID:
202 case Type::ULongTyID: {
203 // Calculate ~0 of the right type...
204 unsigned TypeBits = Ty->getPrimitiveSize()*8;
205 uint64_t Val = ~0ULL; // All ones
206 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
207 return ConstantUInt::get(Ty, Val);
208 }
Chris Lattner31408f72002-08-14 17:12:13 +0000209 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000210 }
211}
212
Chris Lattner83e5d392003-03-10 22:39:02 +0000213bool ConstantUInt::isAllOnesValue() const {
214 unsigned TypeBits = getType()->getPrimitiveSize()*8;
215 uint64_t Val = ~0ULL; // All ones
216 Val >>= 64-TypeBits; // Shift out inappropriate bits
217 return getValue() == Val;
218}
219
Chris Lattnerb1585a92002-08-13 17:50:20 +0000220
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000222// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000223//===----------------------------------------------------------------------===//
224
225//===----------------------------------------------------------------------===//
226// Normal Constructors
227
Chris Lattnerb1585a92002-08-13 17:50:20 +0000228ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000229 Val = V;
230}
Chris Lattner49d855c2001-09-07 16:46:31 +0000231
Chris Lattnerb1585a92002-08-13 17:50:20 +0000232ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000233 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000234}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235
Chris Lattner3462ae32001-12-03 22:26:30 +0000236ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000237 assert(Ty->isInteger() && Ty->isSigned() &&
238 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000239 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000240}
241
Chris Lattner3462ae32001-12-03 22:26:30 +0000242ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000243 assert(Ty->isInteger() && Ty->isUnsigned() &&
244 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000245 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000246}
247
Chris Lattner3462ae32001-12-03 22:26:30 +0000248ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000249 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000250 Val = V;
251}
252
Chris Lattner3462ae32001-12-03 22:26:30 +0000253ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000254 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000255 Operands.reserve(V.size());
256 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerab69ecc2003-08-18 16:54:48 +0000257 assert(V[i]->getType() == T->getElementType() ||
258 (T->isAbstract() &&
259 V[i]->getType()->getPrimitiveID() ==
260 T->getElementType()->getPrimitiveID()));
Chris Lattnera073acb2001-07-07 08:36:50 +0000261 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000262 }
263}
264
Chris Lattner3462ae32001-12-03 22:26:30 +0000265ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000266 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000267 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000268 assert(V.size() == ETypes.size() &&
269 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000270 Operands.reserve(V.size());
271 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000272 assert((V[i]->getType() == ETypes[i] ||
Chris Lattner7267b352003-10-21 17:39:59 +0000273 ((ETypes[i]->isAbstract() || V[i]->getType()->isAbstract()) &&
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000274 ETypes[i]->getPrimitiveID()==V[i]->getType()->getPrimitiveID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000275 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000276 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000277 }
278}
279
Chris Lattner3462ae32001-12-03 22:26:30 +0000280ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
281 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000282 Operands.push_back(Use(GV, this));
283}
284
Chris Lattner3cd8c562002-07-30 18:54:25 +0000285ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
286 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000287 Operands.push_back(Use(C, this));
288}
289
Chris Lattner22ced562003-06-22 20:48:30 +0000290static bool isSetCC(unsigned Opcode) {
291 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
292 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
293 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
294}
295
Chris Lattner3cd8c562002-07-30 18:54:25 +0000296ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner22ced562003-06-22 20:48:30 +0000297 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000298 Operands.push_back(Use(C1, this));
299 Operands.push_back(Use(C2, this));
300}
301
Chris Lattner3cd8c562002-07-30 18:54:25 +0000302ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
303 const Type *DestTy)
304 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000305 Operands.reserve(1+IdxList.size());
306 Operands.push_back(Use(C, this));
307 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
308 Operands.push_back(Use(IdxList[i], this));
309}
310
Chris Lattner60e0dd72001-10-03 06:12:09 +0000311
Chris Lattner2f7c9632001-06-06 20:29:01 +0000312
313//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000314// classof implementations
315
Chris Lattnerb1585a92002-08-13 17:50:20 +0000316bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000317 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000318}
319
Chris Lattner3462ae32001-12-03 22:26:30 +0000320bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000321 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000322}
Chris Lattner3462ae32001-12-03 22:26:30 +0000323bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000324 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000325}
Chris Lattner3462ae32001-12-03 22:26:30 +0000326bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000327 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000328}
Chris Lattner3462ae32001-12-03 22:26:30 +0000329bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000330 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000331 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000332 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000333}
Chris Lattner3462ae32001-12-03 22:26:30 +0000334bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000335 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000336}
Chris Lattner3462ae32001-12-03 22:26:30 +0000337bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000338 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000339}
Chris Lattner3462ae32001-12-03 22:26:30 +0000340bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000341 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000342}
343
344
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000345
Chris Lattner2f7c9632001-06-06 20:29:01 +0000346//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000347// isValueValidForType implementations
348
Chris Lattner3462ae32001-12-03 22:26:30 +0000349bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000350 switch (Ty->getPrimitiveID()) {
351 default:
352 return false; // These can't be represented as integers!!!
353
354 // Signed types...
355 case Type::SByteTyID:
356 return (Val <= INT8_MAX && Val >= INT8_MIN);
357 case Type::ShortTyID:
358 return (Val <= INT16_MAX && Val >= INT16_MIN);
359 case Type::IntTyID:
360 return (Val <= INT32_MAX && Val >= INT32_MIN);
361 case Type::LongTyID:
362 return true; // This is the largest type...
363 }
364 assert(0 && "WTF?");
365 return false;
366}
367
Chris Lattner3462ae32001-12-03 22:26:30 +0000368bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000369 switch (Ty->getPrimitiveID()) {
370 default:
371 return false; // These can't be represented as integers!!!
372
373 // Unsigned types...
374 case Type::UByteTyID:
375 return (Val <= UINT8_MAX);
376 case Type::UShortTyID:
377 return (Val <= UINT16_MAX);
378 case Type::UIntTyID:
379 return (Val <= UINT32_MAX);
380 case Type::ULongTyID:
381 return true; // This is the largest type...
382 }
383 assert(0 && "WTF?");
384 return false;
385}
386
Chris Lattner3462ae32001-12-03 22:26:30 +0000387bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000388 switch (Ty->getPrimitiveID()) {
389 default:
390 return false; // These can't be represented as floating point!
391
392 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000393 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000394 case Type::DoubleTyID:
395 return true; // This is the largest type...
396 }
397};
Chris Lattner9655e542001-07-20 19:16:02 +0000398
Chris Lattner49d855c2001-09-07 16:46:31 +0000399//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000400// replaceUsesOfWithOnConstant implementations
401
Chris Lattnerc27038d2003-08-29 05:36:46 +0000402void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
403 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000404 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
405
406 std::vector<Constant*> Values;
407 Values.reserve(getValues().size()); // Build replacement array...
408 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
409 Constant *Val = cast<Constant>(getValues()[i]);
410 if (Val == From) Val = cast<Constant>(To);
411 Values.push_back(Val);
412 }
413
414 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
415 assert(Replacement != this && "I didn't contain From!");
416
417 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000418 if (DisableChecking)
419 uncheckedReplaceAllUsesWith(Replacement);
420 else
421 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000422
423 // Delete the old constant!
424 destroyConstant();
425}
426
Chris Lattnerc27038d2003-08-29 05:36:46 +0000427void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
428 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000429 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
430
431 std::vector<Constant*> Values;
432 Values.reserve(getValues().size());
433 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
434 Constant *Val = cast<Constant>(getValues()[i]);
435 if (Val == From) Val = cast<Constant>(To);
436 Values.push_back(Val);
437 }
438
439 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
440 assert(Replacement != this && "I didn't contain From!");
441
442 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000443 if (DisableChecking)
444 uncheckedReplaceAllUsesWith(Replacement);
445 else
446 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000447
448 // Delete the old constant!
449 destroyConstant();
450}
451
Chris Lattnerc27038d2003-08-29 05:36:46 +0000452void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
453 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000454 if (isa<GlobalValue>(To)) {
455 assert(From == getOperand(0) && "Doesn't contain from!");
456 ConstantPointerRef *Replacement =
457 ConstantPointerRef::get(cast<GlobalValue>(To));
458
459 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000460 if (DisableChecking)
461 uncheckedReplaceAllUsesWith(Replacement);
462 else
463 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000464
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000465 } else {
466 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000467 if (DisableChecking)
468 uncheckedReplaceAllUsesWith(To);
469 else
470 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000471 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000472
473 // Delete the old constant!
474 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000475}
476
Chris Lattnerc27038d2003-08-29 05:36:46 +0000477void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
478 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000479 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
480 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000481
Chris Lattner46b3d302003-04-16 22:40:51 +0000482 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000483 if (getOpcode() == Instruction::GetElementPtr) {
484 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000485 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000486 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000487 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000488
489 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000490 Constant *Val = getOperand(i);
491 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000492 Indices.push_back(Val);
493 }
494 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
495 } else if (getOpcode() == Instruction::Cast) {
496 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000497 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb36e8a12003-11-05 19:53:03 +0000498 } else if (getOpcode() == Instruction::Shl ||
499 getOpcode() == Instruction::Shr) {
500 Constant *C1 = getOperand(0);
501 Constant *C2 = getOperand(1);
502 if (C1 == From) C1 = To;
503 if (C2 == From) C2 = To;
504 Replacement = ConstantExpr::getShift(getOpcode(), C1, C2);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000505 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000506 Constant *C1 = getOperand(0);
507 Constant *C2 = getOperand(1);
508 if (C1 == From) C1 = To;
509 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000510 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
511 } else {
512 assert(0 && "Unknown ConstantExpr type!");
513 return;
514 }
515
516 assert(Replacement != this && "I didn't contain From!");
517
518 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000519 if (DisableChecking)
520 uncheckedReplaceAllUsesWith(Replacement);
521 else
522 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000523
524 // Delete the old constant!
525 destroyConstant();
526}
527
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000528//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000529// Factory Function Implementation
530
Chris Lattner98fa07b2003-05-23 20:03:32 +0000531// ConstantCreator - A class that is used to create constants by
532// ValueMap*. This class should be partially specialized if there is
533// something strange that needs to be done to interface to the ctor for the
534// constant.
535//
536template<class ConstantClass, class TypeClass, class ValType>
537struct ConstantCreator {
538 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
539 return new ConstantClass(Ty, V);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000540 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000541};
542
Chris Lattnerb50d1352003-10-05 00:17:43 +0000543template<class ConstantClass, class TypeClass>
544struct ConvertConstantType {
545 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
546 assert(0 && "This type cannot be converted!\n");
547 abort();
548 }
549};
550
Chris Lattner98fa07b2003-05-23 20:03:32 +0000551namespace {
552 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000553 class ValueMap : public AbstractTypeUser {
554 typedef std::pair<const TypeClass*, ValType> MapKey;
555 typedef std::map<MapKey, ConstantClass *> MapTy;
556 typedef typename MapTy::iterator MapIterator;
557 MapTy Map;
558
559 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
560 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000561 public:
562 // getOrCreate - Return the specified constant from the map, creating it if
563 // necessary.
564 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000565 MapKey Lookup(Ty, V);
566 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000567 if (I != Map.end() && I->first == Lookup)
568 return I->second; // Is it in the map?
569
570 // If no preexisting value, create one now...
571 ConstantClass *Result =
572 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
573
Chris Lattnerb50d1352003-10-05 00:17:43 +0000574
575 /// FIXME: why does this assert fail when loading 176.gcc?
576 //assert(Result->getType() == Ty && "Type specified is not correct!");
577 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
578
579 // If the type of the constant is abstract, make sure that an entry exists
580 // for it in the AbstractTypeMap.
581 if (Ty->isAbstract()) {
582 typename AbstractTypeMapTy::iterator TI =
583 AbstractTypeMap.lower_bound(Ty);
584
585 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
586 // Add ourselves to the ATU list of the type.
587 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
588
589 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
590 }
591 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000592 return Result;
593 }
594
595 void remove(ConstantClass *CP) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000596 // FIXME: This should not use a linear scan. If this gets to be a
597 // performance problem, someone should look at this.
598 MapIterator I = Map.begin();
599 for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
600 /* empty */;
601
602 assert(I != Map.end() && "Constant not found in constant table!");
603
604 // Now that we found the entry, make sure this isn't the entry that
605 // the AbstractTypeMap points to.
606 const TypeClass *Ty = I->first.first;
607 if (Ty->isAbstract()) {
608 assert(AbstractTypeMap.count(Ty) &&
609 "Abstract type not in AbstractTypeMap?");
610 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
611 if (ATMEntryIt == I) {
612 // Yes, we are removing the representative entry for this type.
613 // See if there are any other entries of the same type.
614 MapIterator TmpIt = ATMEntryIt;
615
616 // First check the entry before this one...
617 if (TmpIt != Map.begin()) {
618 --TmpIt;
619 if (TmpIt->first.first != Ty) // Not the same type, move back...
620 ++TmpIt;
621 }
622
623 // If we didn't find the same type, try to move forward...
624 if (TmpIt == ATMEntryIt) {
625 ++TmpIt;
626 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
627 --TmpIt; // No entry afterwards with the same type
628 }
629
630 // If there is another entry in the map of the same abstract type,
631 // update the AbstractTypeMap entry now.
632 if (TmpIt != ATMEntryIt) {
633 ATMEntryIt = TmpIt;
634 } else {
635 // Otherwise, we are removing the last instance of this type
636 // from the table. Remove from the ATM, and from user list.
637 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
638 AbstractTypeMap.erase(Ty);
639 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000640 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000641 }
642
643 Map.erase(I);
644 }
645
646 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
647 typename AbstractTypeMapTy::iterator I =
648 AbstractTypeMap.find(cast<TypeClass>(OldTy));
649
650 assert(I != AbstractTypeMap.end() &&
651 "Abstract type not in AbstractTypeMap?");
652
653 // Convert a constant at a time until the last one is gone. The last one
654 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
655 // eliminated eventually.
656 do {
657 ConvertConstantType<ConstantClass,
658 TypeClass>::convert(I->second->second,
659 cast<TypeClass>(NewTy));
660
661 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
662 } while (I != AbstractTypeMap.end());
663 }
664
665 // If the type became concrete without being refined to any other existing
666 // type, we just remove ourselves from the ATU list.
667 void typeBecameConcrete(const DerivedType *AbsTy) {
668 AbsTy->removeAbstractTypeUser(this);
669 }
670
671 void dump() const {
672 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000673 }
674 };
675}
676
677
678
Chris Lattner3462ae32001-12-03 22:26:30 +0000679//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000680//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000681static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
682static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000683
Chris Lattner3462ae32001-12-03 22:26:30 +0000684ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000685 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000686}
687
Chris Lattner3462ae32001-12-03 22:26:30 +0000688ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000689 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000690}
691
Chris Lattner3462ae32001-12-03 22:26:30 +0000692ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000693 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000694 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
695 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000696}
697
Chris Lattner3462ae32001-12-03 22:26:30 +0000698//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000699//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000700static ValueMap<double, Type, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000701
Chris Lattner3462ae32001-12-03 22:26:30 +0000702ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000703 return FPConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000704}
705
Chris Lattner3462ae32001-12-03 22:26:30 +0000706//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000707//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000708
709template<>
710struct ConvertConstantType<ConstantArray, ArrayType> {
711 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
712 // Make everyone now use a constant of the new type...
713 std::vector<Constant*> C;
714 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
715 C.push_back(cast<Constant>(OldC->getOperand(i)));
716 Constant *New = ConstantArray::get(NewTy, C);
717 assert(New != OldC && "Didn't replace constant??");
718 OldC->uncheckedReplaceAllUsesWith(New);
719 OldC->destroyConstant(); // This constant is now dead, destroy it.
720 }
721};
722
723
Chris Lattner98fa07b2003-05-23 20:03:32 +0000724static ValueMap<std::vector<Constant*>, ArrayType,
725 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000726
Chris Lattner3462ae32001-12-03 22:26:30 +0000727ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000728 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000729 return ArrayConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000730}
731
Chris Lattner98fa07b2003-05-23 20:03:32 +0000732// destroyConstant - Remove the constant from the constant table...
733//
734void ConstantArray::destroyConstant() {
735 ArrayConstants.remove(this);
736 destroyConstantImpl();
737}
738
Chris Lattner3462ae32001-12-03 22:26:30 +0000739// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000740// contain the specified string. A null terminator is added to the specified
741// string so that it may be used in a natural way...
742//
Chris Lattner7f74a562002-01-20 22:54:45 +0000743ConstantArray *ConstantArray::get(const std::string &Str) {
744 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000745
746 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000747 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000748
749 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000750 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000751
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000752 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000753 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000754}
755
Chris Lattner81fabb02002-08-26 17:53:56 +0000756// getAsString - If the sub-element type of this array is either sbyte or ubyte,
757// then this method converts the array to an std::string and returns it.
758// Otherwise, it asserts out.
759//
760std::string ConstantArray::getAsString() const {
Chris Lattner6077c312003-07-23 15:22:26 +0000761 assert((getType()->getElementType() == Type::UByteTy ||
762 getType()->getElementType() == Type::SByteTy) && "Not a string!");
763
Chris Lattner81fabb02002-08-26 17:53:56 +0000764 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000765 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
766 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000767 return Result;
768}
769
770
Chris Lattner3462ae32001-12-03 22:26:30 +0000771//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000772//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000773
774template<>
775struct ConvertConstantType<ConstantStruct, StructType> {
776 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
777 // Make everyone now use a constant of the new type...
778 std::vector<Constant*> C;
779 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
780 C.push_back(cast<Constant>(OldC->getOperand(i)));
781 Constant *New = ConstantStruct::get(NewTy, C);
782 assert(New != OldC && "Didn't replace constant??");
783
784 OldC->uncheckedReplaceAllUsesWith(New);
785 OldC->destroyConstant(); // This constant is now dead, destroy it.
786 }
787};
788
Chris Lattner98fa07b2003-05-23 20:03:32 +0000789static ValueMap<std::vector<Constant*>, StructType,
790 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000791
Chris Lattner3462ae32001-12-03 22:26:30 +0000792ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000793 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000794 return StructConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000795}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000796
Chris Lattnerd7a73302001-10-13 06:57:33 +0000797// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000798//
Chris Lattner3462ae32001-12-03 22:26:30 +0000799void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000800 StructConstants.remove(this);
801 destroyConstantImpl();
802}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000803
Chris Lattner3462ae32001-12-03 22:26:30 +0000804//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000805//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000806
807// ConstantPointerNull does not take extra "value" argument...
808template<class ValType>
809struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
810 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
811 return new ConstantPointerNull(Ty);
812 }
813};
814
Chris Lattnerb50d1352003-10-05 00:17:43 +0000815template<>
816struct ConvertConstantType<ConstantPointerNull, PointerType> {
817 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
818 // Make everyone now use a constant of the new type...
819 Constant *New = ConstantPointerNull::get(NewTy);
820 assert(New != OldC && "Didn't replace constant??");
821 OldC->uncheckedReplaceAllUsesWith(New);
822 OldC->destroyConstant(); // This constant is now dead, destroy it.
823 }
824};
825
Chris Lattner98fa07b2003-05-23 20:03:32 +0000826static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000827
Chris Lattner3462ae32001-12-03 22:26:30 +0000828ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000829 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000830}
831
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000832// destroyConstant - Remove the constant from the constant table...
833//
834void ConstantPointerNull::destroyConstant() {
835 NullPtrConstants.remove(this);
836 destroyConstantImpl();
837}
838
839
Chris Lattner3462ae32001-12-03 22:26:30 +0000840//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000841//
Chris Lattner3462ae32001-12-03 22:26:30 +0000842ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000843 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000844
Chris Lattnerd7a73302001-10-13 06:57:33 +0000845 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000846 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000847}
848
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000849// destroyConstant - Remove the constant from the constant table...
850//
851void ConstantPointerRef::destroyConstant() {
852 getValue()->getParent()->destroyConstantPointerRef(this);
853 destroyConstantImpl();
854}
855
856
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000857//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000858//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000859typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000860
861template<>
862struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
863 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
864 if (V.first == Instruction::Cast)
865 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
866 if ((V.first >= Instruction::BinaryOpsBegin &&
867 V.first < Instruction::BinaryOpsEnd) ||
868 V.first == Instruction::Shl || V.first == Instruction::Shr)
869 return new ConstantExpr(V.first, V.second[0], V.second[1]);
870
871 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
872
Chris Lattner98fa07b2003-05-23 20:03:32 +0000873 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerb50d1352003-10-05 00:17:43 +0000874 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000875 }
876};
877
Chris Lattnerb50d1352003-10-05 00:17:43 +0000878template<>
879struct ConvertConstantType<ConstantExpr, Type> {
880 static void convert(ConstantExpr *OldC, const Type *NewTy) {
881 Constant *New;
882 switch (OldC->getOpcode()) {
883 case Instruction::Cast:
884 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
885 break;
886 case Instruction::Shl:
887 case Instruction::Shr:
888 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
889 OldC->getOperand(0), OldC->getOperand(1));
890 break;
891 default:
892 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
893 OldC->getOpcode() < Instruction::BinaryOpsEnd);
894 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
895 OldC->getOperand(1));
896 break;
897 case Instruction::GetElementPtr:
898 // Make everyone now use a constant of the new type...
899 std::vector<Constant*> C;
900 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
901 C.push_back(cast<Constant>(OldC->getOperand(i)));
902 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
903 break;
904 }
905
906 assert(New != OldC && "Didn't replace constant??");
907 OldC->uncheckedReplaceAllUsesWith(New);
908 OldC->destroyConstant(); // This constant is now dead, destroy it.
909 }
910};
911
912
Chris Lattner98fa07b2003-05-23 20:03:32 +0000913static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000914
Chris Lattner46b3d302003-04-16 22:40:51 +0000915Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +0000916 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
917
Chris Lattneracdbe712003-04-17 19:24:48 +0000918 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
919 return FC; // Fold a few common cases...
920
Vikram S. Adve4c485332002-07-15 18:19:33 +0000921 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000922 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000923 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
924 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000925}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000926
Chris Lattnerb50d1352003-10-05 00:17:43 +0000927Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
928 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000929 // Check the operands for consistency first
930 assert((Opcode >= Instruction::BinaryOpsBegin &&
931 Opcode < Instruction::BinaryOpsEnd) &&
932 "Invalid opcode in binary constant expression");
933 assert(C1->getType() == C2->getType() &&
934 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000935
936 if (ReqTy == C1->getType())
937 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
938 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +0000939
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000940 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000941 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000942 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000943}
944
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000945/// getShift - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +0000946Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
947 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000948 // Check the operands for consistency first
949 assert((Opcode == Instruction::Shl ||
950 Opcode == Instruction::Shr) &&
951 "Invalid opcode in binary constant expression");
952 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
953 "Invalid operand types for Shift constant expr!");
954
955 if (Constant *FC = ConstantFoldShiftInstruction(Opcode, C1, C2))
956 return FC; // Fold a few common cases...
957
958 // Look up the constant in the table first to ensure uniqueness
959 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000960 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000961 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000962}
963
964
Chris Lattnerb50d1352003-10-05 00:17:43 +0000965Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
966 const std::vector<Constant*> &IdxList) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000967 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
968 return FC; // Fold a few common cases...
Chris Lattnerb50d1352003-10-05 00:17:43 +0000969 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +0000970 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000971
972 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000973 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000974 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000975 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000976 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +0000977}
978
Chris Lattnerb50d1352003-10-05 00:17:43 +0000979Constant *ConstantExpr::getGetElementPtr(Constant *C,
980 const std::vector<Constant*> &IdxList){
981 // Get the result type of the getelementptr!
982 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
983
984 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
985 true);
986 assert(Ty && "GEP indices invalid!");
Chris Lattner8a552622003-10-23 05:21:48 +0000987
988 if (C->isNullValue()) {
989 bool isNull = true;
990 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
991 if (!IdxList[i]->isNullValue()) {
992 isNull = false;
993 break;
994 }
995 if (isNull) return ConstantPointerNull::get(PointerType::get(Ty));
996 }
997
Chris Lattnerb50d1352003-10-05 00:17:43 +0000998 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
999}
1000
1001
Vikram S. Adve4c485332002-07-15 18:19:33 +00001002// destroyConstant - Remove the constant from the constant table...
1003//
1004void ConstantExpr::destroyConstant() {
1005 ExprConstants.remove(this);
1006 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001007}
1008
Chris Lattner3cd8c562002-07-30 18:54:25 +00001009const char *ConstantExpr::getOpcodeName() const {
1010 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001011}
1012
Chris Lattner163b8902002-10-14 03:30:23 +00001013unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
1014 // Uses of constant pointer refs are global values, not constants!
1015 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1016 GlobalValue *NewGV = cast<GlobalValue>(NewV);
1017 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001018
Chris Lattner163b8902002-10-14 03:30:23 +00001019 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattner163b8902002-10-14 03:30:23 +00001020 Operands[0] = NewGV;
Chris Lattnercb4d26f2003-05-15 19:37:21 +00001021 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
Chris Lattner163b8902002-10-14 03:30:23 +00001022 return 1;
1023 } else {
1024 Constant *NewC = cast<Constant>(NewV);
1025 unsigned NumReplaced = 0;
1026 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
1027 if (Operands[i] == OldV) {
1028 ++NumReplaced;
1029 Operands[i] = NewC;
1030 }
1031 return NumReplaced;
1032 }
Chris Lattner25033252001-10-03 19:28:15 +00001033}
Brian Gaeke960707c2003-11-11 22:41:34 +00001034
1035} // End llvm namespace