blob: 05bbef16b3f5555d97b800cc503118bad5af43c1 [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)
Chris Lattnere120a732003-11-17 19:47:21 +0000281 : Constant(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 Lattnere120a732003-11-17 19:47:21 +0000340
341bool ConstantPointerNull::classof(const Constant *CPV) {
342 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
343 CPV->getNumOperands() == 0;
344}
345
346bool ConstantPointerRef::classof(const Constant *CPV) {
347 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
348 CPV->getNumOperands() == 1;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000349}
350
351
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000352
Chris Lattner2f7c9632001-06-06 20:29:01 +0000353//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000354// isValueValidForType implementations
355
Chris Lattner3462ae32001-12-03 22:26:30 +0000356bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000357 switch (Ty->getPrimitiveID()) {
358 default:
359 return false; // These can't be represented as integers!!!
360
361 // Signed types...
362 case Type::SByteTyID:
363 return (Val <= INT8_MAX && Val >= INT8_MIN);
364 case Type::ShortTyID:
365 return (Val <= INT16_MAX && Val >= INT16_MIN);
366 case Type::IntTyID:
367 return (Val <= INT32_MAX && Val >= INT32_MIN);
368 case Type::LongTyID:
369 return true; // This is the largest type...
370 }
371 assert(0 && "WTF?");
372 return false;
373}
374
Chris Lattner3462ae32001-12-03 22:26:30 +0000375bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000376 switch (Ty->getPrimitiveID()) {
377 default:
378 return false; // These can't be represented as integers!!!
379
380 // Unsigned types...
381 case Type::UByteTyID:
382 return (Val <= UINT8_MAX);
383 case Type::UShortTyID:
384 return (Val <= UINT16_MAX);
385 case Type::UIntTyID:
386 return (Val <= UINT32_MAX);
387 case Type::ULongTyID:
388 return true; // This is the largest type...
389 }
390 assert(0 && "WTF?");
391 return false;
392}
393
Chris Lattner3462ae32001-12-03 22:26:30 +0000394bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000395 switch (Ty->getPrimitiveID()) {
396 default:
397 return false; // These can't be represented as floating point!
398
399 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000400 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000401 case Type::DoubleTyID:
402 return true; // This is the largest type...
403 }
404};
Chris Lattner9655e542001-07-20 19:16:02 +0000405
Chris Lattner49d855c2001-09-07 16:46:31 +0000406//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000407// replaceUsesOfWithOnConstant implementations
408
Chris Lattnerc27038d2003-08-29 05:36:46 +0000409void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
410 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000411 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
412
413 std::vector<Constant*> Values;
414 Values.reserve(getValues().size()); // Build replacement array...
415 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
416 Constant *Val = cast<Constant>(getValues()[i]);
417 if (Val == From) Val = cast<Constant>(To);
418 Values.push_back(Val);
419 }
420
421 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
422 assert(Replacement != this && "I didn't contain From!");
423
424 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000425 if (DisableChecking)
426 uncheckedReplaceAllUsesWith(Replacement);
427 else
428 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000429
430 // Delete the old constant!
431 destroyConstant();
432}
433
Chris Lattnerc27038d2003-08-29 05:36:46 +0000434void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
435 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000436 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
437
438 std::vector<Constant*> Values;
439 Values.reserve(getValues().size());
440 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
441 Constant *Val = cast<Constant>(getValues()[i]);
442 if (Val == From) Val = cast<Constant>(To);
443 Values.push_back(Val);
444 }
445
446 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
447 assert(Replacement != this && "I didn't contain From!");
448
449 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000450 if (DisableChecking)
451 uncheckedReplaceAllUsesWith(Replacement);
452 else
453 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000454
455 // Delete the old constant!
456 destroyConstant();
457}
458
Chris Lattnerc27038d2003-08-29 05:36:46 +0000459void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
460 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000461 if (isa<GlobalValue>(To)) {
462 assert(From == getOperand(0) && "Doesn't contain from!");
463 ConstantPointerRef *Replacement =
464 ConstantPointerRef::get(cast<GlobalValue>(To));
465
466 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000467 if (DisableChecking)
468 uncheckedReplaceAllUsesWith(Replacement);
469 else
470 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000471
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000472 } else {
473 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000474 if (DisableChecking)
475 uncheckedReplaceAllUsesWith(To);
476 else
477 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000478 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000479
480 // Delete the old constant!
481 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000482}
483
Chris Lattnerc27038d2003-08-29 05:36:46 +0000484void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
485 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000486 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
487 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000488
Chris Lattner46b3d302003-04-16 22:40:51 +0000489 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000490 if (getOpcode() == Instruction::GetElementPtr) {
491 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000492 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000493 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000494 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000495
496 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000497 Constant *Val = getOperand(i);
498 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000499 Indices.push_back(Val);
500 }
501 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
502 } else if (getOpcode() == Instruction::Cast) {
503 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000504 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb36e8a12003-11-05 19:53:03 +0000505 } else if (getOpcode() == Instruction::Shl ||
506 getOpcode() == Instruction::Shr) {
507 Constant *C1 = getOperand(0);
508 Constant *C2 = getOperand(1);
509 if (C1 == From) C1 = To;
510 if (C2 == From) C2 = To;
511 Replacement = ConstantExpr::getShift(getOpcode(), C1, C2);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000512 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000513 Constant *C1 = getOperand(0);
514 Constant *C2 = getOperand(1);
515 if (C1 == From) C1 = To;
516 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000517 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
518 } else {
519 assert(0 && "Unknown ConstantExpr type!");
520 return;
521 }
522
523 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 Lattnerb1dd9bb2002-10-09 23:12:25 +0000535//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000536// Factory Function Implementation
537
Chris Lattner98fa07b2003-05-23 20:03:32 +0000538// ConstantCreator - A class that is used to create constants by
539// ValueMap*. This class should be partially specialized if there is
540// something strange that needs to be done to interface to the ctor for the
541// constant.
542//
543template<class ConstantClass, class TypeClass, class ValType>
544struct ConstantCreator {
545 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
546 return new ConstantClass(Ty, V);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000547 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000548};
549
Chris Lattnerb50d1352003-10-05 00:17:43 +0000550template<class ConstantClass, class TypeClass>
551struct ConvertConstantType {
552 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
553 assert(0 && "This type cannot be converted!\n");
554 abort();
555 }
556};
557
Chris Lattner98fa07b2003-05-23 20:03:32 +0000558namespace {
559 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000560 class ValueMap : public AbstractTypeUser {
561 typedef std::pair<const TypeClass*, ValType> MapKey;
562 typedef std::map<MapKey, ConstantClass *> MapTy;
563 typedef typename MapTy::iterator MapIterator;
564 MapTy Map;
565
566 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
567 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000568 public:
569 // getOrCreate - Return the specified constant from the map, creating it if
570 // necessary.
571 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000572 MapKey Lookup(Ty, V);
573 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000574 if (I != Map.end() && I->first == Lookup)
575 return I->second; // Is it in the map?
576
577 // If no preexisting value, create one now...
578 ConstantClass *Result =
579 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
580
Chris Lattnerb50d1352003-10-05 00:17:43 +0000581
582 /// FIXME: why does this assert fail when loading 176.gcc?
583 //assert(Result->getType() == Ty && "Type specified is not correct!");
584 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
585
586 // If the type of the constant is abstract, make sure that an entry exists
587 // for it in the AbstractTypeMap.
588 if (Ty->isAbstract()) {
589 typename AbstractTypeMapTy::iterator TI =
590 AbstractTypeMap.lower_bound(Ty);
591
592 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
593 // Add ourselves to the ATU list of the type.
594 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
595
596 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
597 }
598 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000599 return Result;
600 }
601
602 void remove(ConstantClass *CP) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000603 // FIXME: This should not use a linear scan. If this gets to be a
604 // performance problem, someone should look at this.
605 MapIterator I = Map.begin();
606 for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
607 /* empty */;
608
609 assert(I != Map.end() && "Constant not found in constant table!");
610
611 // Now that we found the entry, make sure this isn't the entry that
612 // the AbstractTypeMap points to.
613 const TypeClass *Ty = I->first.first;
614 if (Ty->isAbstract()) {
615 assert(AbstractTypeMap.count(Ty) &&
616 "Abstract type not in AbstractTypeMap?");
617 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
618 if (ATMEntryIt == I) {
619 // Yes, we are removing the representative entry for this type.
620 // See if there are any other entries of the same type.
621 MapIterator TmpIt = ATMEntryIt;
622
623 // First check the entry before this one...
624 if (TmpIt != Map.begin()) {
625 --TmpIt;
626 if (TmpIt->first.first != Ty) // Not the same type, move back...
627 ++TmpIt;
628 }
629
630 // If we didn't find the same type, try to move forward...
631 if (TmpIt == ATMEntryIt) {
632 ++TmpIt;
633 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
634 --TmpIt; // No entry afterwards with the same type
635 }
636
637 // If there is another entry in the map of the same abstract type,
638 // update the AbstractTypeMap entry now.
639 if (TmpIt != ATMEntryIt) {
640 ATMEntryIt = TmpIt;
641 } else {
642 // Otherwise, we are removing the last instance of this type
643 // from the table. Remove from the ATM, and from user list.
644 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
645 AbstractTypeMap.erase(Ty);
646 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000647 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000648 }
649
650 Map.erase(I);
651 }
652
653 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
654 typename AbstractTypeMapTy::iterator I =
655 AbstractTypeMap.find(cast<TypeClass>(OldTy));
656
657 assert(I != AbstractTypeMap.end() &&
658 "Abstract type not in AbstractTypeMap?");
659
660 // Convert a constant at a time until the last one is gone. The last one
661 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
662 // eliminated eventually.
663 do {
664 ConvertConstantType<ConstantClass,
665 TypeClass>::convert(I->second->second,
666 cast<TypeClass>(NewTy));
667
668 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
669 } while (I != AbstractTypeMap.end());
670 }
671
672 // If the type became concrete without being refined to any other existing
673 // type, we just remove ourselves from the ATU list.
674 void typeBecameConcrete(const DerivedType *AbsTy) {
675 AbsTy->removeAbstractTypeUser(this);
676 }
677
678 void dump() const {
679 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000680 }
681 };
682}
683
684
685
Chris Lattner3462ae32001-12-03 22:26:30 +0000686//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000687//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000688static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
689static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000690
Chris Lattner3462ae32001-12-03 22:26:30 +0000691ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000692 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000693}
694
Chris Lattner3462ae32001-12-03 22:26:30 +0000695ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000696 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000697}
698
Chris Lattner3462ae32001-12-03 22:26:30 +0000699ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000700 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000701 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
702 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000703}
704
Chris Lattner3462ae32001-12-03 22:26:30 +0000705//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000706//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000707static ValueMap<double, Type, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000708
Chris Lattner3462ae32001-12-03 22:26:30 +0000709ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000710 return FPConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000711}
712
Chris Lattner3462ae32001-12-03 22:26:30 +0000713//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000714//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000715
716template<>
717struct ConvertConstantType<ConstantArray, ArrayType> {
718 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
719 // Make everyone now use a constant of the new type...
720 std::vector<Constant*> C;
721 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
722 C.push_back(cast<Constant>(OldC->getOperand(i)));
723 Constant *New = ConstantArray::get(NewTy, C);
724 assert(New != OldC && "Didn't replace constant??");
725 OldC->uncheckedReplaceAllUsesWith(New);
726 OldC->destroyConstant(); // This constant is now dead, destroy it.
727 }
728};
729
730
Chris Lattner98fa07b2003-05-23 20:03:32 +0000731static ValueMap<std::vector<Constant*>, ArrayType,
732 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000733
Chris Lattner3462ae32001-12-03 22:26:30 +0000734ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000735 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000736 return ArrayConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000737}
738
Chris Lattner98fa07b2003-05-23 20:03:32 +0000739// destroyConstant - Remove the constant from the constant table...
740//
741void ConstantArray::destroyConstant() {
742 ArrayConstants.remove(this);
743 destroyConstantImpl();
744}
745
Chris Lattner3462ae32001-12-03 22:26:30 +0000746// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000747// contain the specified string. A null terminator is added to the specified
748// string so that it may be used in a natural way...
749//
Chris Lattner7f74a562002-01-20 22:54:45 +0000750ConstantArray *ConstantArray::get(const std::string &Str) {
751 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000752
753 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000754 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000755
756 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000757 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000758
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000759 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000760 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000761}
762
Chris Lattner81fabb02002-08-26 17:53:56 +0000763// getAsString - If the sub-element type of this array is either sbyte or ubyte,
764// then this method converts the array to an std::string and returns it.
765// Otherwise, it asserts out.
766//
767std::string ConstantArray::getAsString() const {
Chris Lattner6077c312003-07-23 15:22:26 +0000768 assert((getType()->getElementType() == Type::UByteTy ||
769 getType()->getElementType() == Type::SByteTy) && "Not a string!");
770
Chris Lattner81fabb02002-08-26 17:53:56 +0000771 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000772 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
773 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000774 return Result;
775}
776
777
Chris Lattner3462ae32001-12-03 22:26:30 +0000778//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000779//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000780
781template<>
782struct ConvertConstantType<ConstantStruct, StructType> {
783 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
784 // Make everyone now use a constant of the new type...
785 std::vector<Constant*> C;
786 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
787 C.push_back(cast<Constant>(OldC->getOperand(i)));
788 Constant *New = ConstantStruct::get(NewTy, C);
789 assert(New != OldC && "Didn't replace constant??");
790
791 OldC->uncheckedReplaceAllUsesWith(New);
792 OldC->destroyConstant(); // This constant is now dead, destroy it.
793 }
794};
795
Chris Lattner98fa07b2003-05-23 20:03:32 +0000796static ValueMap<std::vector<Constant*>, StructType,
797 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000798
Chris Lattner3462ae32001-12-03 22:26:30 +0000799ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000800 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000801 return StructConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000802}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000803
Chris Lattnerd7a73302001-10-13 06:57:33 +0000804// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000805//
Chris Lattner3462ae32001-12-03 22:26:30 +0000806void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000807 StructConstants.remove(this);
808 destroyConstantImpl();
809}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000810
Chris Lattner3462ae32001-12-03 22:26:30 +0000811//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000812//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000813
814// ConstantPointerNull does not take extra "value" argument...
815template<class ValType>
816struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
817 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
818 return new ConstantPointerNull(Ty);
819 }
820};
821
Chris Lattnerb50d1352003-10-05 00:17:43 +0000822template<>
823struct ConvertConstantType<ConstantPointerNull, PointerType> {
824 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
825 // Make everyone now use a constant of the new type...
826 Constant *New = ConstantPointerNull::get(NewTy);
827 assert(New != OldC && "Didn't replace constant??");
828 OldC->uncheckedReplaceAllUsesWith(New);
829 OldC->destroyConstant(); // This constant is now dead, destroy it.
830 }
831};
832
Chris Lattner98fa07b2003-05-23 20:03:32 +0000833static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000834
Chris Lattner3462ae32001-12-03 22:26:30 +0000835ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000836 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000837}
838
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000839// destroyConstant - Remove the constant from the constant table...
840//
841void ConstantPointerNull::destroyConstant() {
842 NullPtrConstants.remove(this);
843 destroyConstantImpl();
844}
845
846
Chris Lattner3462ae32001-12-03 22:26:30 +0000847//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000848//
Chris Lattner3462ae32001-12-03 22:26:30 +0000849ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000850 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000851
Chris Lattnerd7a73302001-10-13 06:57:33 +0000852 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000853 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000854}
855
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000856// destroyConstant - Remove the constant from the constant table...
857//
858void ConstantPointerRef::destroyConstant() {
859 getValue()->getParent()->destroyConstantPointerRef(this);
860 destroyConstantImpl();
861}
862
863
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000864//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000865//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000866typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000867
868template<>
869struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
870 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
871 if (V.first == Instruction::Cast)
872 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
873 if ((V.first >= Instruction::BinaryOpsBegin &&
874 V.first < Instruction::BinaryOpsEnd) ||
875 V.first == Instruction::Shl || V.first == Instruction::Shr)
876 return new ConstantExpr(V.first, V.second[0], V.second[1]);
877
878 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
879
Chris Lattner98fa07b2003-05-23 20:03:32 +0000880 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerb50d1352003-10-05 00:17:43 +0000881 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000882 }
883};
884
Chris Lattnerb50d1352003-10-05 00:17:43 +0000885template<>
886struct ConvertConstantType<ConstantExpr, Type> {
887 static void convert(ConstantExpr *OldC, const Type *NewTy) {
888 Constant *New;
889 switch (OldC->getOpcode()) {
890 case Instruction::Cast:
891 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
892 break;
893 case Instruction::Shl:
894 case Instruction::Shr:
895 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
896 OldC->getOperand(0), OldC->getOperand(1));
897 break;
898 default:
899 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
900 OldC->getOpcode() < Instruction::BinaryOpsEnd);
901 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
902 OldC->getOperand(1));
903 break;
904 case Instruction::GetElementPtr:
905 // Make everyone now use a constant of the new type...
906 std::vector<Constant*> C;
907 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
908 C.push_back(cast<Constant>(OldC->getOperand(i)));
909 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
910 break;
911 }
912
913 assert(New != OldC && "Didn't replace constant??");
914 OldC->uncheckedReplaceAllUsesWith(New);
915 OldC->destroyConstant(); // This constant is now dead, destroy it.
916 }
917};
918
919
Chris Lattner98fa07b2003-05-23 20:03:32 +0000920static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000921
Chris Lattner46b3d302003-04-16 22:40:51 +0000922Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +0000923 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
924
Chris Lattneracdbe712003-04-17 19:24:48 +0000925 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
926 return FC; // Fold a few common cases...
927
Vikram S. Adve4c485332002-07-15 18:19:33 +0000928 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000929 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000930 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
931 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000932}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000933
Chris Lattnerb50d1352003-10-05 00:17:43 +0000934Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
935 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000936 // Check the operands for consistency first
937 assert((Opcode >= Instruction::BinaryOpsBegin &&
938 Opcode < Instruction::BinaryOpsEnd) &&
939 "Invalid opcode in binary constant expression");
940 assert(C1->getType() == C2->getType() &&
941 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000942
943 if (ReqTy == C1->getType())
944 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
945 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +0000946
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000947 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000948 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000949 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000950}
951
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000952/// getShift - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +0000953Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
954 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000955 // Check the operands for consistency first
956 assert((Opcode == Instruction::Shl ||
957 Opcode == Instruction::Shr) &&
958 "Invalid opcode in binary constant expression");
959 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
960 "Invalid operand types for Shift constant expr!");
961
962 if (Constant *FC = ConstantFoldShiftInstruction(Opcode, C1, C2))
963 return FC; // Fold a few common cases...
964
965 // Look up the constant in the table first to ensure uniqueness
966 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000967 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000968 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000969}
970
971
Chris Lattnerb50d1352003-10-05 00:17:43 +0000972Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
973 const std::vector<Constant*> &IdxList) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000974 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
975 return FC; // Fold a few common cases...
Chris Lattnerb50d1352003-10-05 00:17:43 +0000976 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +0000977 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000978
979 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000980 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000981 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000982 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000983 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +0000984}
985
Chris Lattnerb50d1352003-10-05 00:17:43 +0000986Constant *ConstantExpr::getGetElementPtr(Constant *C,
987 const std::vector<Constant*> &IdxList){
988 // Get the result type of the getelementptr!
989 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
990
991 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
992 true);
993 assert(Ty && "GEP indices invalid!");
Chris Lattner8a552622003-10-23 05:21:48 +0000994
995 if (C->isNullValue()) {
996 bool isNull = true;
997 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
998 if (!IdxList[i]->isNullValue()) {
999 isNull = false;
1000 break;
1001 }
1002 if (isNull) return ConstantPointerNull::get(PointerType::get(Ty));
1003 }
1004
Chris Lattnerb50d1352003-10-05 00:17:43 +00001005 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1006}
1007
1008
Vikram S. Adve4c485332002-07-15 18:19:33 +00001009// destroyConstant - Remove the constant from the constant table...
1010//
1011void ConstantExpr::destroyConstant() {
1012 ExprConstants.remove(this);
1013 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001014}
1015
Chris Lattner3cd8c562002-07-30 18:54:25 +00001016const char *ConstantExpr::getOpcodeName() const {
1017 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001018}
1019
Chris Lattner163b8902002-10-14 03:30:23 +00001020unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
1021 // Uses of constant pointer refs are global values, not constants!
1022 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1023 GlobalValue *NewGV = cast<GlobalValue>(NewV);
1024 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001025
Chris Lattner163b8902002-10-14 03:30:23 +00001026 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattner163b8902002-10-14 03:30:23 +00001027 Operands[0] = NewGV;
Chris Lattnercb4d26f2003-05-15 19:37:21 +00001028 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
Chris Lattner163b8902002-10-14 03:30:23 +00001029 return 1;
1030 } else {
1031 Constant *NewC = cast<Constant>(NewV);
1032 unsigned NumReplaced = 0;
1033 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
1034 if (Operands[i] == OldV) {
1035 ++NumReplaced;
1036 Operands[i] = NewC;
1037 }
1038 return NumReplaced;
1039 }
Chris Lattner25033252001-10-03 19:28:15 +00001040}
Brian Gaeke960707c2003-11-11 22:41:34 +00001041
1042} // End llvm namespace