blob: bfac24fe9b96ab15ad817cbb55ce49480dab9001 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner5a945e32004-01-12 21:13:12 +000015#include "ConstantFolding.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Vikram S. Adve4e537b22002-07-14 23:13:17 +000017#include "llvm/iMemory.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000019#include "llvm/Module.h"
Chris Lattner5de22042001-11-27 00:03:19 +000020#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000021#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner3462ae32001-12-03 22:26:30 +000024ConstantBool *ConstantBool::True = new ConstantBool(true);
25ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000026
Chris Lattner9655e542001-07-20 19:16:02 +000027
Chris Lattner2f7c9632001-06-06 20:29:01 +000028//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000029// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000030//===----------------------------------------------------------------------===//
31
32// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000033void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000034 assert(ST && "Type::setName - Must provide symbol table argument!");
35
36 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000037}
38
Chris Lattner3462ae32001-12-03 22:26:30 +000039void Constant::destroyConstantImpl() {
40 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000041 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000042 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000043 // but they don't know that. Because we only find out when the CPV is
44 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000045 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000046 //
47 while (!use_empty()) {
48 Value *V = use_back();
49#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000050 if (!isa<Constant>(V))
51 std::cerr << "While deleting: " << *this
52 << "\n\nUse still stuck around after Def is destroyed: "
53 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000054#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000055 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000056 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000057 CPV->destroyConstant();
58
59 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000060 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000061 }
62
63 // Value has no outstanding references it is safe to delete it now...
64 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000065}
Chris Lattner2f7c9632001-06-06 20:29:01 +000066
Chris Lattnerb1585a92002-08-13 17:50:20 +000067// Static constructor to create a '0' constant of arbitrary type...
68Constant *Constant::getNullValue(const Type *Ty) {
69 switch (Ty->getPrimitiveID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000070 case Type::BoolTyID: {
71 static Constant *NullBool = ConstantBool::get(false);
72 return NullBool;
73 }
74 case Type::SByteTyID: {
75 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
76 return NullSByte;
77 }
78 case Type::UByteTyID: {
79 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
80 return NullUByte;
81 }
82 case Type::ShortTyID: {
83 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
84 return NullShort;
85 }
86 case Type::UShortTyID: {
87 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
88 return NullUShort;
89 }
90 case Type::IntTyID: {
91 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
92 return NullInt;
93 }
94 case Type::UIntTyID: {
95 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
96 return NullUInt;
97 }
98 case Type::LongTyID: {
99 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
100 return NullLong;
101 }
102 case Type::ULongTyID: {
103 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
104 return NullULong;
105 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000106
Chris Lattner3e88ef92003-10-03 19:34:51 +0000107 case Type::FloatTyID: {
108 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
109 return NullFloat;
110 }
111 case Type::DoubleTyID: {
112 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
113 return NullDouble;
114 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000115
116 case Type::PointerTyID:
117 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000118
Chris Lattnerc33ae672003-03-06 21:02:18 +0000119 case Type::StructTyID: {
120 const StructType *ST = cast<StructType>(Ty);
Chris Lattnerc33ae672003-03-06 21:02:18 +0000121 const StructType::ElementTypes &ETs = ST->getElementTypes();
122 std::vector<Constant*> Elements;
123 Elements.resize(ETs.size());
124 for (unsigned i = 0, e = ETs.size(); i != e; ++i)
125 Elements[i] = Constant::getNullValue(ETs[i]);
126 return ConstantStruct::get(ST, Elements);
127 }
128 case Type::ArrayTyID: {
129 const ArrayType *AT = cast<ArrayType>(Ty);
130 Constant *El = Constant::getNullValue(AT->getElementType());
131 unsigned NumElements = AT->getNumElements();
132 return ConstantArray::get(AT, std::vector<Constant*>(NumElements, El));
133 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000134 default:
Chris Lattnerc33ae672003-03-06 21:02:18 +0000135 // Function, Type, Label, or Opaque type?
136 assert(0 && "Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000137 return 0;
138 }
139}
140
141// Static constructor to create the maximum constant of an integral type...
142ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
143 switch (Ty->getPrimitiveID()) {
144 case Type::BoolTyID: return ConstantBool::True;
145 case Type::SByteTyID:
146 case Type::ShortTyID:
147 case Type::IntTyID:
148 case Type::LongTyID: {
149 // Calculate 011111111111111...
150 unsigned TypeBits = Ty->getPrimitiveSize()*8;
151 int64_t Val = INT64_MAX; // All ones
152 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
153 return ConstantSInt::get(Ty, Val);
154 }
155
156 case Type::UByteTyID:
157 case Type::UShortTyID:
158 case Type::UIntTyID:
159 case Type::ULongTyID: return getAllOnesValue(Ty);
160
Chris Lattner31408f72002-08-14 17:12:13 +0000161 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000162 }
163}
164
165// Static constructor to create the minimum constant for an integral type...
166ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
167 switch (Ty->getPrimitiveID()) {
168 case Type::BoolTyID: return ConstantBool::False;
169 case Type::SByteTyID:
170 case Type::ShortTyID:
171 case Type::IntTyID:
172 case Type::LongTyID: {
173 // Calculate 1111111111000000000000
174 unsigned TypeBits = Ty->getPrimitiveSize()*8;
175 int64_t Val = -1; // All ones
176 Val <<= TypeBits-1; // Shift over to the right spot
177 return ConstantSInt::get(Ty, Val);
178 }
179
180 case Type::UByteTyID:
181 case Type::UShortTyID:
182 case Type::UIntTyID:
183 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
184
Chris Lattner31408f72002-08-14 17:12:13 +0000185 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000186 }
187}
188
189// Static constructor to create an integral constant with all bits set
190ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
191 switch (Ty->getPrimitiveID()) {
192 case Type::BoolTyID: return ConstantBool::True;
193 case Type::SByteTyID:
194 case Type::ShortTyID:
195 case Type::IntTyID:
196 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
197
198 case Type::UByteTyID:
199 case Type::UShortTyID:
200 case Type::UIntTyID:
201 case Type::ULongTyID: {
202 // Calculate ~0 of the right type...
203 unsigned TypeBits = Ty->getPrimitiveSize()*8;
204 uint64_t Val = ~0ULL; // All ones
205 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
206 return ConstantUInt::get(Ty, Val);
207 }
Chris Lattner31408f72002-08-14 17:12:13 +0000208 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000209 }
210}
211
Chris Lattner83e5d392003-03-10 22:39:02 +0000212bool ConstantUInt::isAllOnesValue() const {
213 unsigned TypeBits = getType()->getPrimitiveSize()*8;
214 uint64_t Val = ~0ULL; // All ones
215 Val >>= 64-TypeBits; // Shift out inappropriate bits
216 return getValue() == Val;
217}
218
Chris Lattnerb1585a92002-08-13 17:50:20 +0000219
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000221// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222//===----------------------------------------------------------------------===//
223
224//===----------------------------------------------------------------------===//
225// Normal Constructors
226
Chris Lattnerb1585a92002-08-13 17:50:20 +0000227ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228 Val = V;
229}
Chris Lattner49d855c2001-09-07 16:46:31 +0000230
Chris Lattnerb1585a92002-08-13 17:50:20 +0000231ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000232 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000233}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000234
Chris Lattner3462ae32001-12-03 22:26:30 +0000235ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000236 assert(Ty->isInteger() && Ty->isSigned() &&
237 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000238 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000239}
240
Chris Lattner3462ae32001-12-03 22:26:30 +0000241ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000242 assert(Ty->isInteger() && Ty->isUnsigned() &&
243 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000244 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000245}
246
Chris Lattner3462ae32001-12-03 22:26:30 +0000247ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000248 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249 Val = V;
250}
251
Chris Lattner3462ae32001-12-03 22:26:30 +0000252ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000253 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000254 Operands.reserve(V.size());
255 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerab69ecc2003-08-18 16:54:48 +0000256 assert(V[i]->getType() == T->getElementType() ||
257 (T->isAbstract() &&
258 V[i]->getType()->getPrimitiveID() ==
259 T->getElementType()->getPrimitiveID()));
Chris Lattnera073acb2001-07-07 08:36:50 +0000260 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000261 }
262}
263
Chris Lattner3462ae32001-12-03 22:26:30 +0000264ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000265 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000266 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000267 assert(V.size() == ETypes.size() &&
268 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000269 Operands.reserve(V.size());
270 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000271 assert((V[i]->getType() == ETypes[i] ||
Chris Lattner7267b352003-10-21 17:39:59 +0000272 ((ETypes[i]->isAbstract() || V[i]->getType()->isAbstract()) &&
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000273 ETypes[i]->getPrimitiveID()==V[i]->getType()->getPrimitiveID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000274 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000275 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000276 }
277}
278
Chris Lattner3462ae32001-12-03 22:26:30 +0000279ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
Chris Lattnere120a732003-11-17 19:47:21 +0000280 : Constant(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000281 Operands.push_back(Use(GV, this));
282}
283
Chris Lattner3cd8c562002-07-30 18:54:25 +0000284ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
285 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000286 Operands.push_back(Use(C, this));
287}
288
Chris Lattner22ced562003-06-22 20:48:30 +0000289static bool isSetCC(unsigned Opcode) {
290 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
291 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
292 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
293}
294
Chris Lattner3cd8c562002-07-30 18:54:25 +0000295ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner22ced562003-06-22 20:48:30 +0000296 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000297 Operands.push_back(Use(C1, this));
298 Operands.push_back(Use(C2, this));
299}
300
Chris Lattner3cd8c562002-07-30 18:54:25 +0000301ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
302 const Type *DestTy)
303 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000304 Operands.reserve(1+IdxList.size());
305 Operands.push_back(Use(C, this));
306 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
307 Operands.push_back(Use(IdxList[i], this));
308}
309
Chris Lattner60e0dd72001-10-03 06:12:09 +0000310
Chris Lattner2f7c9632001-06-06 20:29:01 +0000311
312//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000313// classof implementations
314
Chris Lattnerb1585a92002-08-13 17:50:20 +0000315bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000316 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000317}
318
Chris Lattner3462ae32001-12-03 22:26:30 +0000319bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000320 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000321}
Chris Lattner3462ae32001-12-03 22:26:30 +0000322bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000323 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000324}
Chris Lattner3462ae32001-12-03 22:26:30 +0000325bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000326 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000327}
Chris Lattner3462ae32001-12-03 22:26:30 +0000328bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000329 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000330 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000331 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000332}
Chris Lattner3462ae32001-12-03 22:26:30 +0000333bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000334 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000335}
Chris Lattner3462ae32001-12-03 22:26:30 +0000336bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000337 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000338}
Chris Lattnere120a732003-11-17 19:47:21 +0000339
340bool ConstantPointerNull::classof(const Constant *CPV) {
341 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
342 CPV->getNumOperands() == 0;
343}
344
345bool ConstantPointerRef::classof(const Constant *CPV) {
346 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
347 CPV->getNumOperands() == 1;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000348}
349
350
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000351
Chris Lattner2f7c9632001-06-06 20:29:01 +0000352//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000353// isValueValidForType implementations
354
Chris Lattner3462ae32001-12-03 22:26:30 +0000355bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000356 switch (Ty->getPrimitiveID()) {
357 default:
358 return false; // These can't be represented as integers!!!
359
360 // Signed types...
361 case Type::SByteTyID:
362 return (Val <= INT8_MAX && Val >= INT8_MIN);
363 case Type::ShortTyID:
364 return (Val <= INT16_MAX && Val >= INT16_MIN);
365 case Type::IntTyID:
366 return (Val <= INT32_MAX && Val >= INT32_MIN);
367 case Type::LongTyID:
368 return true; // This is the largest type...
369 }
370 assert(0 && "WTF?");
371 return false;
372}
373
Chris Lattner3462ae32001-12-03 22:26:30 +0000374bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000375 switch (Ty->getPrimitiveID()) {
376 default:
377 return false; // These can't be represented as integers!!!
378
379 // Unsigned types...
380 case Type::UByteTyID:
381 return (Val <= UINT8_MAX);
382 case Type::UShortTyID:
383 return (Val <= UINT16_MAX);
384 case Type::UIntTyID:
385 return (Val <= UINT32_MAX);
386 case Type::ULongTyID:
387 return true; // This is the largest type...
388 }
389 assert(0 && "WTF?");
390 return false;
391}
392
Chris Lattner3462ae32001-12-03 22:26:30 +0000393bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000394 switch (Ty->getPrimitiveID()) {
395 default:
396 return false; // These can't be represented as floating point!
397
398 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000399 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000400 case Type::DoubleTyID:
401 return true; // This is the largest type...
402 }
403};
Chris Lattner9655e542001-07-20 19:16:02 +0000404
Chris Lattner49d855c2001-09-07 16:46:31 +0000405//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000406// replaceUsesOfWithOnConstant implementations
407
Chris Lattnerc27038d2003-08-29 05:36:46 +0000408void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
409 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000410 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
411
412 std::vector<Constant*> Values;
413 Values.reserve(getValues().size()); // Build replacement array...
414 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
415 Constant *Val = cast<Constant>(getValues()[i]);
416 if (Val == From) Val = cast<Constant>(To);
417 Values.push_back(Val);
418 }
419
420 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
421 assert(Replacement != this && "I didn't contain From!");
422
423 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000424 if (DisableChecking)
425 uncheckedReplaceAllUsesWith(Replacement);
426 else
427 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000428
429 // Delete the old constant!
430 destroyConstant();
431}
432
Chris Lattnerc27038d2003-08-29 05:36:46 +0000433void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
434 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000435 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
436
437 std::vector<Constant*> Values;
438 Values.reserve(getValues().size());
439 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
440 Constant *Val = cast<Constant>(getValues()[i]);
441 if (Val == From) Val = cast<Constant>(To);
442 Values.push_back(Val);
443 }
444
445 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
446 assert(Replacement != this && "I didn't contain From!");
447
448 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000449 if (DisableChecking)
450 uncheckedReplaceAllUsesWith(Replacement);
451 else
452 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000453
454 // Delete the old constant!
455 destroyConstant();
456}
457
Chris Lattnerc27038d2003-08-29 05:36:46 +0000458void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
459 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000460 if (isa<GlobalValue>(To)) {
461 assert(From == getOperand(0) && "Doesn't contain from!");
462 ConstantPointerRef *Replacement =
463 ConstantPointerRef::get(cast<GlobalValue>(To));
464
465 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000466 if (DisableChecking)
467 uncheckedReplaceAllUsesWith(Replacement);
468 else
469 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000470
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000471 } else {
472 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000473 if (DisableChecking)
474 uncheckedReplaceAllUsesWith(To);
475 else
476 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000477 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000478
479 // Delete the old constant!
480 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000481}
482
Chris Lattnerc27038d2003-08-29 05:36:46 +0000483void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
484 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000485 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
486 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000487
Chris Lattner46b3d302003-04-16 22:40:51 +0000488 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000489 if (getOpcode() == Instruction::GetElementPtr) {
490 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000491 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000492 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000493 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000494
495 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000496 Constant *Val = getOperand(i);
497 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000498 Indices.push_back(Val);
499 }
500 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
501 } else if (getOpcode() == Instruction::Cast) {
502 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000503 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000504 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000505 Constant *C1 = getOperand(0);
506 Constant *C2 = getOperand(1);
507 if (C1 == From) C1 = To;
508 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000509 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
510 } else {
511 assert(0 && "Unknown ConstantExpr type!");
512 return;
513 }
514
515 assert(Replacement != this && "I didn't contain From!");
516
517 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000518 if (DisableChecking)
519 uncheckedReplaceAllUsesWith(Replacement);
520 else
521 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000522
523 // Delete the old constant!
524 destroyConstant();
525}
526
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000527//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000528// Factory Function Implementation
529
Chris Lattner98fa07b2003-05-23 20:03:32 +0000530// ConstantCreator - A class that is used to create constants by
531// ValueMap*. This class should be partially specialized if there is
532// something strange that needs to be done to interface to the ctor for the
533// constant.
534//
Chris Lattner189d19f2003-11-21 20:23:48 +0000535namespace llvm {
536 template<class ConstantClass, class TypeClass, class ValType>
537 struct ConstantCreator {
538 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
539 return new ConstantClass(Ty, V);
540 }
541 };
542
543 template<class ConstantClass, class TypeClass>
544 struct 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 Lattnerb50d1352003-10-05 00:17:43 +0000551
Chris Lattner98fa07b2003-05-23 20:03:32 +0000552namespace {
553 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000554 class ValueMap : public AbstractTypeUser {
555 typedef std::pair<const TypeClass*, ValType> MapKey;
556 typedef std::map<MapKey, ConstantClass *> MapTy;
557 typedef typename MapTy::iterator MapIterator;
558 MapTy Map;
559
560 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
561 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000562 public:
563 // getOrCreate - Return the specified constant from the map, creating it if
564 // necessary.
565 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000566 MapKey Lookup(Ty, V);
567 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000568 if (I != Map.end() && I->first == Lookup)
569 return I->second; // Is it in the map?
570
571 // If no preexisting value, create one now...
572 ConstantClass *Result =
573 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
574
Chris Lattnerb50d1352003-10-05 00:17:43 +0000575
576 /// FIXME: why does this assert fail when loading 176.gcc?
577 //assert(Result->getType() == Ty && "Type specified is not correct!");
578 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
579
580 // If the type of the constant is abstract, make sure that an entry exists
581 // for it in the AbstractTypeMap.
582 if (Ty->isAbstract()) {
583 typename AbstractTypeMapTy::iterator TI =
584 AbstractTypeMap.lower_bound(Ty);
585
586 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
587 // Add ourselves to the ATU list of the type.
588 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
589
590 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
591 }
592 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000593 return Result;
594 }
595
596 void remove(ConstantClass *CP) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000597 // FIXME: This should not use a linear scan. If this gets to be a
598 // performance problem, someone should look at this.
599 MapIterator I = Map.begin();
600 for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
601 /* empty */;
602
603 assert(I != Map.end() && "Constant not found in constant table!");
604
605 // Now that we found the entry, make sure this isn't the entry that
606 // the AbstractTypeMap points to.
607 const TypeClass *Ty = I->first.first;
608 if (Ty->isAbstract()) {
609 assert(AbstractTypeMap.count(Ty) &&
610 "Abstract type not in AbstractTypeMap?");
611 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
612 if (ATMEntryIt == I) {
613 // Yes, we are removing the representative entry for this type.
614 // See if there are any other entries of the same type.
615 MapIterator TmpIt = ATMEntryIt;
616
617 // First check the entry before this one...
618 if (TmpIt != Map.begin()) {
619 --TmpIt;
620 if (TmpIt->first.first != Ty) // Not the same type, move back...
621 ++TmpIt;
622 }
623
624 // If we didn't find the same type, try to move forward...
625 if (TmpIt == ATMEntryIt) {
626 ++TmpIt;
627 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
628 --TmpIt; // No entry afterwards with the same type
629 }
630
631 // If there is another entry in the map of the same abstract type,
632 // update the AbstractTypeMap entry now.
633 if (TmpIt != ATMEntryIt) {
634 ATMEntryIt = TmpIt;
635 } else {
636 // Otherwise, we are removing the last instance of this type
637 // from the table. Remove from the ATM, and from user list.
638 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
639 AbstractTypeMap.erase(Ty);
640 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000641 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000642 }
643
644 Map.erase(I);
645 }
646
647 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
648 typename AbstractTypeMapTy::iterator I =
649 AbstractTypeMap.find(cast<TypeClass>(OldTy));
650
651 assert(I != AbstractTypeMap.end() &&
652 "Abstract type not in AbstractTypeMap?");
653
654 // Convert a constant at a time until the last one is gone. The last one
655 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
656 // eliminated eventually.
657 do {
658 ConvertConstantType<ConstantClass,
659 TypeClass>::convert(I->second->second,
660 cast<TypeClass>(NewTy));
661
662 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
663 } while (I != AbstractTypeMap.end());
664 }
665
666 // If the type became concrete without being refined to any other existing
667 // type, we just remove ourselves from the ATU list.
668 void typeBecameConcrete(const DerivedType *AbsTy) {
669 AbsTy->removeAbstractTypeUser(this);
670 }
671
672 void dump() const {
673 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000674 }
675 };
676}
677
678
679
Chris Lattner3462ae32001-12-03 22:26:30 +0000680//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000681//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000682static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
683static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000684
Chris Lattner3462ae32001-12-03 22:26:30 +0000685ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000686 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000687}
688
Chris Lattner3462ae32001-12-03 22:26:30 +0000689ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000690 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000691}
692
Chris Lattner3462ae32001-12-03 22:26:30 +0000693ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000694 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000695 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
696 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000697}
698
Chris Lattner3462ae32001-12-03 22:26:30 +0000699//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000700//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000701static ValueMap<double, Type, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000702
Chris Lattner3462ae32001-12-03 22:26:30 +0000703ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000704 return FPConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000705}
706
Chris Lattner3462ae32001-12-03 22:26:30 +0000707//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000708//
Chris Lattner189d19f2003-11-21 20:23:48 +0000709namespace llvm {
710 template<>
711 struct ConvertConstantType<ConstantArray, ArrayType> {
712 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
713 // Make everyone now use a constant of the new type...
714 std::vector<Constant*> C;
715 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
716 C.push_back(cast<Constant>(OldC->getOperand(i)));
717 Constant *New = ConstantArray::get(NewTy, C);
718 assert(New != OldC && "Didn't replace constant??");
719 OldC->uncheckedReplaceAllUsesWith(New);
720 OldC->destroyConstant(); // This constant is now dead, destroy it.
721 }
722 };
723}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000724
Chris Lattner98fa07b2003-05-23 20:03:32 +0000725static ValueMap<std::vector<Constant*>, ArrayType,
726 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000727
Chris Lattner3462ae32001-12-03 22:26:30 +0000728ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000729 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000730 return ArrayConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000731}
732
Chris Lattner98fa07b2003-05-23 20:03:32 +0000733// destroyConstant - Remove the constant from the constant table...
734//
735void ConstantArray::destroyConstant() {
736 ArrayConstants.remove(this);
737 destroyConstantImpl();
738}
739
Chris Lattner3462ae32001-12-03 22:26:30 +0000740// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000741// contain the specified string. A null terminator is added to the specified
742// string so that it may be used in a natural way...
743//
Chris Lattner7f74a562002-01-20 22:54:45 +0000744ConstantArray *ConstantArray::get(const std::string &Str) {
745 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000746
747 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000748 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000749
750 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000751 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000752
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000753 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000754 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000755}
756
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000757/// isString - This method returns true if the array is an array of sbyte or
758/// ubyte, and if the elements of the array are all ConstantInt's.
759bool ConstantArray::isString() const {
760 // Check the element type for sbyte or ubyte...
761 if (getType()->getElementType() != Type::UByteTy ||
762 getType()->getElementType() != Type::SByteTy)
763 return false;
764 // Check the elements to make sure they are all integers, not constant
765 // expressions.
766 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
767 if (!isa<ConstantInt>(getOperand(i)))
768 return false;
769 return true;
770}
771
Chris Lattner81fabb02002-08-26 17:53:56 +0000772// getAsString - If the sub-element type of this array is either sbyte or ubyte,
773// then this method converts the array to an std::string and returns it.
774// Otherwise, it asserts out.
775//
776std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000777 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000778 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000779 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
780 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000781 return Result;
782}
783
784
Chris Lattner3462ae32001-12-03 22:26:30 +0000785//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000786//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000787
Chris Lattner189d19f2003-11-21 20:23:48 +0000788namespace llvm {
789 template<>
790 struct ConvertConstantType<ConstantStruct, StructType> {
791 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
792 // Make everyone now use a constant of the new type...
793 std::vector<Constant*> C;
794 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
795 C.push_back(cast<Constant>(OldC->getOperand(i)));
796 Constant *New = ConstantStruct::get(NewTy, C);
797 assert(New != OldC && "Didn't replace constant??");
798
799 OldC->uncheckedReplaceAllUsesWith(New);
800 OldC->destroyConstant(); // This constant is now dead, destroy it.
801 }
802 };
803}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000804
Chris Lattner98fa07b2003-05-23 20:03:32 +0000805static ValueMap<std::vector<Constant*>, StructType,
806 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000807
Chris Lattner3462ae32001-12-03 22:26:30 +0000808ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000809 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000810 return StructConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000811}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000812
Chris Lattnerd7a73302001-10-13 06:57:33 +0000813// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000814//
Chris Lattner3462ae32001-12-03 22:26:30 +0000815void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000816 StructConstants.remove(this);
817 destroyConstantImpl();
818}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000819
Chris Lattner3462ae32001-12-03 22:26:30 +0000820//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000821//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000822
Chris Lattner189d19f2003-11-21 20:23:48 +0000823namespace llvm {
824 // ConstantPointerNull does not take extra "value" argument...
825 template<class ValType>
826 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
827 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
828 return new ConstantPointerNull(Ty);
829 }
830 };
Chris Lattner98fa07b2003-05-23 20:03:32 +0000831
Chris Lattner189d19f2003-11-21 20:23:48 +0000832 template<>
833 struct ConvertConstantType<ConstantPointerNull, PointerType> {
834 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
835 // Make everyone now use a constant of the new type...
836 Constant *New = ConstantPointerNull::get(NewTy);
837 assert(New != OldC && "Didn't replace constant??");
838 OldC->uncheckedReplaceAllUsesWith(New);
839 OldC->destroyConstant(); // This constant is now dead, destroy it.
840 }
841 };
842}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000843
Chris Lattner98fa07b2003-05-23 20:03:32 +0000844static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000845
Chris Lattner3462ae32001-12-03 22:26:30 +0000846ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000847 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000848}
849
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000850// destroyConstant - Remove the constant from the constant table...
851//
852void ConstantPointerNull::destroyConstant() {
853 NullPtrConstants.remove(this);
854 destroyConstantImpl();
855}
856
857
Chris Lattner3462ae32001-12-03 22:26:30 +0000858//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000859//
Chris Lattner3462ae32001-12-03 22:26:30 +0000860ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000861 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000862
Chris Lattnerd7a73302001-10-13 06:57:33 +0000863 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000864 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000865}
866
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000867// destroyConstant - Remove the constant from the constant table...
868//
869void ConstantPointerRef::destroyConstant() {
870 getValue()->getParent()->destroyConstantPointerRef(this);
871 destroyConstantImpl();
872}
873
874
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000875//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000876//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000877typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000878
Chris Lattner189d19f2003-11-21 20:23:48 +0000879namespace llvm {
880 template<>
881 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
882 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
883 if (V.first == Instruction::Cast)
884 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
885 if ((V.first >= Instruction::BinaryOpsBegin &&
886 V.first < Instruction::BinaryOpsEnd) ||
887 V.first == Instruction::Shl || V.first == Instruction::Shr)
888 return new ConstantExpr(V.first, V.second[0], V.second[1]);
889
890 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
891
892 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
893 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000894 }
Chris Lattner189d19f2003-11-21 20:23:48 +0000895 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000896
Chris Lattner189d19f2003-11-21 20:23:48 +0000897 template<>
898 struct ConvertConstantType<ConstantExpr, Type> {
899 static void convert(ConstantExpr *OldC, const Type *NewTy) {
900 Constant *New;
901 switch (OldC->getOpcode()) {
902 case Instruction::Cast:
903 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
904 break;
905 case Instruction::Shl:
906 case Instruction::Shr:
907 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
908 OldC->getOperand(0), OldC->getOperand(1));
909 break;
910 default:
911 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
912 OldC->getOpcode() < Instruction::BinaryOpsEnd);
913 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
914 OldC->getOperand(1));
915 break;
916 case Instruction::GetElementPtr:
917 // Make everyone now use a constant of the new type...
918 std::vector<Constant*> C;
919 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
920 C.push_back(cast<Constant>(OldC->getOperand(i)));
921 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
922 break;
923 }
924
925 assert(New != OldC && "Didn't replace constant??");
926 OldC->uncheckedReplaceAllUsesWith(New);
927 OldC->destroyConstant(); // This constant is now dead, destroy it.
928 }
929 };
930} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +0000931
932
Chris Lattner98fa07b2003-05-23 20:03:32 +0000933static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000934
Chris Lattner46b3d302003-04-16 22:40:51 +0000935Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +0000936 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
937
Chris Lattneracdbe712003-04-17 19:24:48 +0000938 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
939 return FC; // Fold a few common cases...
940
Vikram S. Adve4c485332002-07-15 18:19:33 +0000941 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000942 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000943 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
944 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000945}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000946
Chris Lattnerb50d1352003-10-05 00:17:43 +0000947Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
948 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +0000949 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
950 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000951 // Check the operands for consistency first
952 assert((Opcode >= Instruction::BinaryOpsBegin &&
953 Opcode < Instruction::BinaryOpsEnd) &&
954 "Invalid opcode in binary constant expression");
955 assert(C1->getType() == C2->getType() &&
956 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000957
958 if (ReqTy == C1->getType())
959 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
960 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +0000961
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000962 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000963 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000964 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000965}
966
Chris Lattner9eb2b522004-01-12 19:12:58 +0000967/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +0000968Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
969 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000970 // Check the operands for consistency first
971 assert((Opcode == Instruction::Shl ||
972 Opcode == Instruction::Shr) &&
973 "Invalid opcode in binary constant expression");
974 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
975 "Invalid operand types for Shift constant expr!");
976
Chris Lattner0bba7712004-01-12 20:40:42 +0000977 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000978 return FC; // Fold a few common cases...
979
980 // Look up the constant in the table first to ensure uniqueness
981 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000982 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000983 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000984}
985
986
Chris Lattnerb50d1352003-10-05 00:17:43 +0000987Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
988 const std::vector<Constant*> &IdxList) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000989 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
990 return FC; // Fold a few common cases...
Chris Lattnerb50d1352003-10-05 00:17:43 +0000991 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +0000992 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000993
994 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000995 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000996 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000997 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000998 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +0000999}
1000
Chris Lattnerb50d1352003-10-05 00:17:43 +00001001Constant *ConstantExpr::getGetElementPtr(Constant *C,
1002 const std::vector<Constant*> &IdxList){
1003 // Get the result type of the getelementptr!
1004 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1005
1006 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1007 true);
1008 assert(Ty && "GEP indices invalid!");
Chris Lattner8a552622003-10-23 05:21:48 +00001009
1010 if (C->isNullValue()) {
1011 bool isNull = true;
1012 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1013 if (!IdxList[i]->isNullValue()) {
1014 isNull = false;
1015 break;
1016 }
1017 if (isNull) return ConstantPointerNull::get(PointerType::get(Ty));
1018 }
1019
Chris Lattnerb50d1352003-10-05 00:17:43 +00001020 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1021}
1022
1023
Vikram S. Adve4c485332002-07-15 18:19:33 +00001024// destroyConstant - Remove the constant from the constant table...
1025//
1026void ConstantExpr::destroyConstant() {
1027 ExprConstants.remove(this);
1028 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001029}
1030
Chris Lattner3cd8c562002-07-30 18:54:25 +00001031const char *ConstantExpr::getOpcodeName() const {
1032 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001033}
1034
Chris Lattner163b8902002-10-14 03:30:23 +00001035unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
1036 // Uses of constant pointer refs are global values, not constants!
1037 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1038 GlobalValue *NewGV = cast<GlobalValue>(NewV);
1039 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001040
Chris Lattner163b8902002-10-14 03:30:23 +00001041 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattner163b8902002-10-14 03:30:23 +00001042 Operands[0] = NewGV;
Chris Lattnercb4d26f2003-05-15 19:37:21 +00001043 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
Chris Lattner163b8902002-10-14 03:30:23 +00001044 return 1;
1045 } else {
1046 Constant *NewC = cast<Constant>(NewV);
1047 unsigned NumReplaced = 0;
1048 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
1049 if (Operands[i] == OldV) {
1050 ++NumReplaced;
1051 Operands[i] = NewC;
1052 }
1053 return NumReplaced;
1054 }
Chris Lattner25033252001-10-03 19:28:15 +00001055}
Brian Gaeke960707c2003-11-11 22:41:34 +00001056