blob: 5a617fad9ae517de582aa82a4f5f23d4d40e9b51 [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 Lattner9fba3da2004-02-15 05:53:04 +0000119 case Type::StructTyID:
120 case Type::ArrayTyID:
121 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000122 default:
Chris Lattnerc33ae672003-03-06 21:02:18 +0000123 // Function, Type, Label, or Opaque type?
124 assert(0 && "Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000125 return 0;
126 }
127}
128
129// Static constructor to create the maximum constant of an integral type...
130ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
131 switch (Ty->getPrimitiveID()) {
132 case Type::BoolTyID: return ConstantBool::True;
133 case Type::SByteTyID:
134 case Type::ShortTyID:
135 case Type::IntTyID:
136 case Type::LongTyID: {
137 // Calculate 011111111111111...
138 unsigned TypeBits = Ty->getPrimitiveSize()*8;
139 int64_t Val = INT64_MAX; // All ones
140 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
141 return ConstantSInt::get(Ty, Val);
142 }
143
144 case Type::UByteTyID:
145 case Type::UShortTyID:
146 case Type::UIntTyID:
147 case Type::ULongTyID: return getAllOnesValue(Ty);
148
Chris Lattner31408f72002-08-14 17:12:13 +0000149 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000150 }
151}
152
153// Static constructor to create the minimum constant for an integral type...
154ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
155 switch (Ty->getPrimitiveID()) {
156 case Type::BoolTyID: return ConstantBool::False;
157 case Type::SByteTyID:
158 case Type::ShortTyID:
159 case Type::IntTyID:
160 case Type::LongTyID: {
161 // Calculate 1111111111000000000000
162 unsigned TypeBits = Ty->getPrimitiveSize()*8;
163 int64_t Val = -1; // All ones
164 Val <<= TypeBits-1; // Shift over to the right spot
165 return ConstantSInt::get(Ty, Val);
166 }
167
168 case Type::UByteTyID:
169 case Type::UShortTyID:
170 case Type::UIntTyID:
171 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
172
Chris Lattner31408f72002-08-14 17:12:13 +0000173 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000174 }
175}
176
177// Static constructor to create an integral constant with all bits set
178ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
179 switch (Ty->getPrimitiveID()) {
180 case Type::BoolTyID: return ConstantBool::True;
181 case Type::SByteTyID:
182 case Type::ShortTyID:
183 case Type::IntTyID:
184 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
185
186 case Type::UByteTyID:
187 case Type::UShortTyID:
188 case Type::UIntTyID:
189 case Type::ULongTyID: {
190 // Calculate ~0 of the right type...
191 unsigned TypeBits = Ty->getPrimitiveSize()*8;
192 uint64_t Val = ~0ULL; // All ones
193 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
194 return ConstantUInt::get(Ty, Val);
195 }
Chris Lattner31408f72002-08-14 17:12:13 +0000196 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000197 }
198}
199
Chris Lattner83e5d392003-03-10 22:39:02 +0000200bool ConstantUInt::isAllOnesValue() const {
201 unsigned TypeBits = getType()->getPrimitiveSize()*8;
202 uint64_t Val = ~0ULL; // All ones
203 Val >>= 64-TypeBits; // Shift out inappropriate bits
204 return getValue() == Val;
205}
206
Chris Lattnerb1585a92002-08-13 17:50:20 +0000207
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000209// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000210//===----------------------------------------------------------------------===//
211
212//===----------------------------------------------------------------------===//
213// Normal Constructors
214
Chris Lattnerb1585a92002-08-13 17:50:20 +0000215ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216 Val = V;
217}
Chris Lattner49d855c2001-09-07 16:46:31 +0000218
Chris Lattnerb1585a92002-08-13 17:50:20 +0000219ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000220 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000221}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222
Chris Lattner3462ae32001-12-03 22:26:30 +0000223ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000224 assert(Ty->isInteger() && Ty->isSigned() &&
225 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000226 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227}
228
Chris Lattner3462ae32001-12-03 22:26:30 +0000229ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000230 assert(Ty->isInteger() && Ty->isUnsigned() &&
231 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000232 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233}
234
Chris Lattner3462ae32001-12-03 22:26:30 +0000235ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000236 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237 Val = V;
238}
239
Chris Lattner3462ae32001-12-03 22:26:30 +0000240ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000241 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000242 Operands.reserve(V.size());
243 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerab69ecc2003-08-18 16:54:48 +0000244 assert(V[i]->getType() == T->getElementType() ||
245 (T->isAbstract() &&
246 V[i]->getType()->getPrimitiveID() ==
247 T->getElementType()->getPrimitiveID()));
Chris Lattnera073acb2001-07-07 08:36:50 +0000248 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249 }
250}
251
Chris Lattner3462ae32001-12-03 22:26:30 +0000252ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000253 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000254 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000255 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000256 Operands.reserve(V.size());
257 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000258 assert((V[i]->getType() == T->getElementType(i) ||
259 ((T->getElementType(i)->isAbstract() ||
260 V[i]->getType()->isAbstract()) &&
261 T->getElementType(i)->getPrimitiveID() ==
262 V[i]->getType()->getPrimitiveID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000263 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000264 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000265 }
266}
267
Chris Lattner3462ae32001-12-03 22:26:30 +0000268ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
Chris Lattnere120a732003-11-17 19:47:21 +0000269 : Constant(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000270 Operands.push_back(Use(GV, this));
271}
272
Chris Lattner3cd8c562002-07-30 18:54:25 +0000273ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
274 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000275 Operands.push_back(Use(C, this));
276}
277
Chris Lattner22ced562003-06-22 20:48:30 +0000278static bool isSetCC(unsigned Opcode) {
279 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
280 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
281 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
282}
283
Chris Lattner3cd8c562002-07-30 18:54:25 +0000284ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner22ced562003-06-22 20:48:30 +0000285 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000286 Operands.push_back(Use(C1, this));
287 Operands.push_back(Use(C2, this));
288}
289
Chris Lattner3cd8c562002-07-30 18:54:25 +0000290ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
291 const Type *DestTy)
292 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000293 Operands.reserve(1+IdxList.size());
294 Operands.push_back(Use(C, this));
295 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
296 Operands.push_back(Use(IdxList[i], this));
297}
298
Chris Lattner60e0dd72001-10-03 06:12:09 +0000299
Chris Lattner2f7c9632001-06-06 20:29:01 +0000300
301//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000302// classof implementations
303
Chris Lattnerb1585a92002-08-13 17:50:20 +0000304bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000305 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000306}
307
Chris Lattner3462ae32001-12-03 22:26:30 +0000308bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000309 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000310}
Chris Lattner3462ae32001-12-03 22:26:30 +0000311bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000312 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000313}
Chris Lattner3462ae32001-12-03 22:26:30 +0000314bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000315 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000316}
Chris Lattner3462ae32001-12-03 22:26:30 +0000317bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000318 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000319 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000320 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000321}
Chris Lattner9fba3da2004-02-15 05:53:04 +0000322bool ConstantAggregateZero::classof(const Constant *CPV) {
323 return (isa<ArrayType>(CPV->getType()) || isa<StructType>(CPV->getType())) &&
324 CPV->isNullValue();
325}
Chris Lattner3462ae32001-12-03 22:26:30 +0000326bool ConstantArray::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000327 return isa<ArrayType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000328}
Chris Lattner3462ae32001-12-03 22:26:30 +0000329bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000330 return isa<StructType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000331}
Chris Lattnere120a732003-11-17 19:47:21 +0000332
333bool ConstantPointerNull::classof(const Constant *CPV) {
334 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
335 CPV->getNumOperands() == 0;
336}
337
338bool ConstantPointerRef::classof(const Constant *CPV) {
339 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
340 CPV->getNumOperands() == 1;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000341}
342
343
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000344
Chris Lattner2f7c9632001-06-06 20:29:01 +0000345//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000346// isValueValidForType implementations
347
Chris Lattner3462ae32001-12-03 22:26:30 +0000348bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000349 switch (Ty->getPrimitiveID()) {
350 default:
351 return false; // These can't be represented as integers!!!
352
353 // Signed types...
354 case Type::SByteTyID:
355 return (Val <= INT8_MAX && Val >= INT8_MIN);
356 case Type::ShortTyID:
357 return (Val <= INT16_MAX && Val >= INT16_MIN);
358 case Type::IntTyID:
359 return (Val <= INT32_MAX && Val >= INT32_MIN);
360 case Type::LongTyID:
361 return true; // This is the largest type...
362 }
363 assert(0 && "WTF?");
364 return false;
365}
366
Chris Lattner3462ae32001-12-03 22:26:30 +0000367bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000368 switch (Ty->getPrimitiveID()) {
369 default:
370 return false; // These can't be represented as integers!!!
371
372 // Unsigned types...
373 case Type::UByteTyID:
374 return (Val <= UINT8_MAX);
375 case Type::UShortTyID:
376 return (Val <= UINT16_MAX);
377 case Type::UIntTyID:
378 return (Val <= UINT32_MAX);
379 case Type::ULongTyID:
380 return true; // This is the largest type...
381 }
382 assert(0 && "WTF?");
383 return false;
384}
385
Chris Lattner3462ae32001-12-03 22:26:30 +0000386bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000387 switch (Ty->getPrimitiveID()) {
388 default:
389 return false; // These can't be represented as floating point!
390
391 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000392 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000393 case Type::DoubleTyID:
394 return true; // This is the largest type...
395 }
396};
Chris Lattner9655e542001-07-20 19:16:02 +0000397
Chris Lattner49d855c2001-09-07 16:46:31 +0000398//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000399// replaceUsesOfWithOnConstant implementations
400
Chris Lattnerc27038d2003-08-29 05:36:46 +0000401void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
402 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000403 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
404
405 std::vector<Constant*> Values;
406 Values.reserve(getValues().size()); // Build replacement array...
407 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
408 Constant *Val = cast<Constant>(getValues()[i]);
409 if (Val == From) Val = cast<Constant>(To);
410 Values.push_back(Val);
411 }
412
Chris Lattnerc75bf522004-02-15 04:05:58 +0000413 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000414 assert(Replacement != this && "I didn't contain From!");
415
416 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000417 if (DisableChecking)
418 uncheckedReplaceAllUsesWith(Replacement);
419 else
420 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000421
422 // Delete the old constant!
423 destroyConstant();
424}
425
Chris Lattnerc27038d2003-08-29 05:36:46 +0000426void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
427 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000428 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
429
430 std::vector<Constant*> Values;
431 Values.reserve(getValues().size());
432 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
433 Constant *Val = cast<Constant>(getValues()[i]);
434 if (Val == From) Val = cast<Constant>(To);
435 Values.push_back(Val);
436 }
437
Chris Lattner37a716f2004-02-15 04:07:32 +0000438 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000439 assert(Replacement != this && "I didn't contain From!");
440
441 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000442 if (DisableChecking)
443 uncheckedReplaceAllUsesWith(Replacement);
444 else
445 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000446
447 // Delete the old constant!
448 destroyConstant();
449}
450
Chris Lattnerc27038d2003-08-29 05:36:46 +0000451void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
452 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000453 if (isa<GlobalValue>(To)) {
454 assert(From == getOperand(0) && "Doesn't contain from!");
455 ConstantPointerRef *Replacement =
456 ConstantPointerRef::get(cast<GlobalValue>(To));
457
458 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000459 if (DisableChecking)
460 uncheckedReplaceAllUsesWith(Replacement);
461 else
462 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000463
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000464 } else {
465 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000466 if (DisableChecking)
467 uncheckedReplaceAllUsesWith(To);
468 else
469 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000470 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000471
472 // Delete the old constant!
473 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000474}
475
Chris Lattnerc27038d2003-08-29 05:36:46 +0000476void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
477 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000478 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
479 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000480
Chris Lattner46b3d302003-04-16 22:40:51 +0000481 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000482 if (getOpcode() == Instruction::GetElementPtr) {
483 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000484 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000485 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000486 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000487
488 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000489 Constant *Val = getOperand(i);
490 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000491 Indices.push_back(Val);
492 }
493 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
494 } else if (getOpcode() == Instruction::Cast) {
495 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000496 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000497 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000498 Constant *C1 = getOperand(0);
499 Constant *C2 = getOperand(1);
500 if (C1 == From) C1 = To;
501 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000502 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
503 } else {
504 assert(0 && "Unknown ConstantExpr type!");
505 return;
506 }
507
508 assert(Replacement != this && "I didn't contain From!");
509
510 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000511 if (DisableChecking)
512 uncheckedReplaceAllUsesWith(Replacement);
513 else
514 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000515
516 // Delete the old constant!
517 destroyConstant();
518}
519
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000520//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000521// Factory Function Implementation
522
Chris Lattner98fa07b2003-05-23 20:03:32 +0000523// ConstantCreator - A class that is used to create constants by
524// ValueMap*. This class should be partially specialized if there is
525// something strange that needs to be done to interface to the ctor for the
526// constant.
527//
Chris Lattner189d19f2003-11-21 20:23:48 +0000528namespace llvm {
529 template<class ConstantClass, class TypeClass, class ValType>
530 struct ConstantCreator {
531 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
532 return new ConstantClass(Ty, V);
533 }
534 };
535
536 template<class ConstantClass, class TypeClass>
537 struct ConvertConstantType {
538 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
539 assert(0 && "This type cannot be converted!\n");
540 abort();
541 }
542 };
543}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000544
Chris Lattner98fa07b2003-05-23 20:03:32 +0000545namespace {
546 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000547 class ValueMap : public AbstractTypeUser {
548 typedef std::pair<const TypeClass*, ValType> MapKey;
549 typedef std::map<MapKey, ConstantClass *> MapTy;
550 typedef typename MapTy::iterator MapIterator;
551 MapTy Map;
552
553 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
554 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000555 public:
556 // getOrCreate - Return the specified constant from the map, creating it if
557 // necessary.
558 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000559 MapKey Lookup(Ty, V);
560 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000561 if (I != Map.end() && I->first == Lookup)
562 return I->second; // Is it in the map?
563
564 // If no preexisting value, create one now...
565 ConstantClass *Result =
566 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
567
Chris Lattnerb50d1352003-10-05 00:17:43 +0000568
569 /// FIXME: why does this assert fail when loading 176.gcc?
570 //assert(Result->getType() == Ty && "Type specified is not correct!");
571 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
572
573 // If the type of the constant is abstract, make sure that an entry exists
574 // for it in the AbstractTypeMap.
575 if (Ty->isAbstract()) {
576 typename AbstractTypeMapTy::iterator TI =
577 AbstractTypeMap.lower_bound(Ty);
578
579 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
580 // Add ourselves to the ATU list of the type.
581 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
582
583 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
584 }
585 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000586 return Result;
587 }
588
589 void remove(ConstantClass *CP) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000590 // FIXME: This should not use a linear scan. If this gets to be a
591 // performance problem, someone should look at this.
592 MapIterator I = Map.begin();
593 for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
594 /* empty */;
595
596 assert(I != Map.end() && "Constant not found in constant table!");
597
598 // Now that we found the entry, make sure this isn't the entry that
599 // the AbstractTypeMap points to.
600 const TypeClass *Ty = I->first.first;
601 if (Ty->isAbstract()) {
602 assert(AbstractTypeMap.count(Ty) &&
603 "Abstract type not in AbstractTypeMap?");
604 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
605 if (ATMEntryIt == I) {
606 // Yes, we are removing the representative entry for this type.
607 // See if there are any other entries of the same type.
608 MapIterator TmpIt = ATMEntryIt;
609
610 // First check the entry before this one...
611 if (TmpIt != Map.begin()) {
612 --TmpIt;
613 if (TmpIt->first.first != Ty) // Not the same type, move back...
614 ++TmpIt;
615 }
616
617 // If we didn't find the same type, try to move forward...
618 if (TmpIt == ATMEntryIt) {
619 ++TmpIt;
620 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
621 --TmpIt; // No entry afterwards with the same type
622 }
623
624 // If there is another entry in the map of the same abstract type,
625 // update the AbstractTypeMap entry now.
626 if (TmpIt != ATMEntryIt) {
627 ATMEntryIt = TmpIt;
628 } else {
629 // Otherwise, we are removing the last instance of this type
630 // from the table. Remove from the ATM, and from user list.
631 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
632 AbstractTypeMap.erase(Ty);
633 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000634 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000635 }
636
637 Map.erase(I);
638 }
639
640 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
641 typename AbstractTypeMapTy::iterator I =
642 AbstractTypeMap.find(cast<TypeClass>(OldTy));
643
644 assert(I != AbstractTypeMap.end() &&
645 "Abstract type not in AbstractTypeMap?");
646
647 // Convert a constant at a time until the last one is gone. The last one
648 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
649 // eliminated eventually.
650 do {
651 ConvertConstantType<ConstantClass,
652 TypeClass>::convert(I->second->second,
653 cast<TypeClass>(NewTy));
654
655 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
656 } while (I != AbstractTypeMap.end());
657 }
658
659 // If the type became concrete without being refined to any other existing
660 // type, we just remove ourselves from the ATU list.
661 void typeBecameConcrete(const DerivedType *AbsTy) {
662 AbsTy->removeAbstractTypeUser(this);
663 }
664
665 void dump() const {
666 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000667 }
668 };
669}
670
671
672
Chris Lattner3462ae32001-12-03 22:26:30 +0000673//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000674//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000675static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
676static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000677
Chris Lattner3462ae32001-12-03 22:26:30 +0000678ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000679 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000680}
681
Chris Lattner3462ae32001-12-03 22:26:30 +0000682ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000683 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000684}
685
Chris Lattner3462ae32001-12-03 22:26:30 +0000686ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000687 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000688 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
689 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000690}
691
Chris Lattner3462ae32001-12-03 22:26:30 +0000692//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000693//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000694namespace llvm {
695 template<>
696 struct ConstantCreator<ConstantFP, Type, uint64_t> {
697 static ConstantFP *create(const Type *Ty, uint64_t V) {
698 assert(Ty == Type::DoubleTy);
699 union {
700 double F;
701 uint64_t I;
702 } T;
703 T.I = V;
704 return new ConstantFP(Ty, T.F);
705 }
706 };
707 template<>
708 struct ConstantCreator<ConstantFP, Type, uint32_t> {
709 static ConstantFP *create(const Type *Ty, uint32_t V) {
710 assert(Ty == Type::FloatTy);
711 union {
712 float F;
713 uint32_t I;
714 } T;
715 T.I = V;
716 return new ConstantFP(Ty, T.F);
717 }
718 };
719}
720
721static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
722static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000723
Chris Lattner3462ae32001-12-03 22:26:30 +0000724ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000725 if (Ty == Type::FloatTy) {
726 // Force the value through memory to normalize it.
Chris Lattnerac80ea42004-02-01 22:49:04 +0000727 union {
728 float F;
729 uint32_t I;
730 } T;
731 T.F = (float)V;
732 return FloatConstants.getOrCreate(Ty, T.I);
733 } else {
734 assert(Ty == Type::DoubleTy);
735 union {
736 double F;
737 uint64_t I;
738 } T;
739 T.F = V;
740 return DoubleConstants.getOrCreate(Ty, T.I);
Chris Lattner241ed4c2004-01-23 00:55:21 +0000741 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000742}
743
Chris Lattner9fba3da2004-02-15 05:53:04 +0000744//---- ConstantAggregateZero::get() implementation...
745//
746namespace llvm {
747 // ConstantAggregateZero does not take extra "value" argument...
748 template<class ValType>
749 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
750 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
751 return new ConstantAggregateZero(Ty);
752 }
753 };
754
755 template<>
756 struct ConvertConstantType<ConstantAggregateZero, Type> {
757 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
758 // Make everyone now use a constant of the new type...
759 Constant *New = ConstantAggregateZero::get(NewTy);
760 assert(New != OldC && "Didn't replace constant??");
761 OldC->uncheckedReplaceAllUsesWith(New);
762 OldC->destroyConstant(); // This constant is now dead, destroy it.
763 }
764 };
765}
766
767static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
768
769Constant *ConstantAggregateZero::get(const Type *Ty) {
770 return AggZeroConstants.getOrCreate(Ty, 0);
771}
772
773// destroyConstant - Remove the constant from the constant table...
774//
775void ConstantAggregateZero::destroyConstant() {
776 AggZeroConstants.remove(this);
777 destroyConstantImpl();
778}
779
780void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
781 bool DisableChecking) {
782 assert(0 && "No uses!");
783 abort();
784}
785
786
787
Chris Lattner3462ae32001-12-03 22:26:30 +0000788//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000789//
Chris Lattner189d19f2003-11-21 20:23:48 +0000790namespace llvm {
791 template<>
792 struct ConvertConstantType<ConstantArray, ArrayType> {
793 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
794 // Make everyone now use a constant of the new type...
795 std::vector<Constant*> C;
796 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
797 C.push_back(cast<Constant>(OldC->getOperand(i)));
798 Constant *New = ConstantArray::get(NewTy, C);
799 assert(New != OldC && "Didn't replace constant??");
800 OldC->uncheckedReplaceAllUsesWith(New);
801 OldC->destroyConstant(); // This constant is now dead, destroy it.
802 }
803 };
804}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000805
Chris Lattner98fa07b2003-05-23 20:03:32 +0000806static ValueMap<std::vector<Constant*>, ArrayType,
807 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000808
Chris Lattner015e8212004-02-15 04:14:47 +0000809Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000810 const std::vector<Constant*> &V) {
811 // If this is an all-zero array, return a ConstantAggregateZero object
812 if (!V.empty()) {
813 Constant *C = V[0];
814 if (!C->isNullValue())
815 return ArrayConstants.getOrCreate(Ty, V);
816 for (unsigned i = 1, e = V.size(); i != e; ++i)
817 if (V[i] != C)
818 return ArrayConstants.getOrCreate(Ty, V);
819 }
820 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000821}
822
Chris Lattner98fa07b2003-05-23 20:03:32 +0000823// destroyConstant - Remove the constant from the constant table...
824//
825void ConstantArray::destroyConstant() {
826 ArrayConstants.remove(this);
827 destroyConstantImpl();
828}
829
Chris Lattner3462ae32001-12-03 22:26:30 +0000830// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000831// contain the specified string. A null terminator is added to the specified
832// string so that it may be used in a natural way...
833//
Chris Lattner015e8212004-02-15 04:14:47 +0000834Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000835 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000836
837 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000838 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000839
840 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000841 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000842
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000843 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000844 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000845}
846
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000847/// isString - This method returns true if the array is an array of sbyte or
848/// ubyte, and if the elements of the array are all ConstantInt's.
849bool ConstantArray::isString() const {
850 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000851 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000852 getType()->getElementType() != Type::SByteTy)
853 return false;
854 // Check the elements to make sure they are all integers, not constant
855 // expressions.
856 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
857 if (!isa<ConstantInt>(getOperand(i)))
858 return false;
859 return true;
860}
861
Chris Lattner81fabb02002-08-26 17:53:56 +0000862// getAsString - If the sub-element type of this array is either sbyte or ubyte,
863// then this method converts the array to an std::string and returns it.
864// Otherwise, it asserts out.
865//
866std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000867 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000868 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000869 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
870 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000871 return Result;
872}
873
874
Chris Lattner3462ae32001-12-03 22:26:30 +0000875//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000876//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000877
Chris Lattner189d19f2003-11-21 20:23:48 +0000878namespace llvm {
879 template<>
880 struct ConvertConstantType<ConstantStruct, StructType> {
881 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
882 // Make everyone now use a constant of the new type...
883 std::vector<Constant*> C;
884 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
885 C.push_back(cast<Constant>(OldC->getOperand(i)));
886 Constant *New = ConstantStruct::get(NewTy, C);
887 assert(New != OldC && "Didn't replace constant??");
888
889 OldC->uncheckedReplaceAllUsesWith(New);
890 OldC->destroyConstant(); // This constant is now dead, destroy it.
891 }
892 };
893}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000894
Chris Lattner98fa07b2003-05-23 20:03:32 +0000895static ValueMap<std::vector<Constant*>, StructType,
896 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000897
Chris Lattner015e8212004-02-15 04:14:47 +0000898Constant *ConstantStruct::get(const StructType *Ty,
899 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000900 // Create a ConstantAggregateZero value if all elements are zeros...
901 for (unsigned i = 0, e = V.size(); i != e; ++i)
902 if (!V[i]->isNullValue())
903 return StructConstants.getOrCreate(Ty, V);
904
905 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000906}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000907
Chris Lattnerd7a73302001-10-13 06:57:33 +0000908// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000909//
Chris Lattner3462ae32001-12-03 22:26:30 +0000910void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000911 StructConstants.remove(this);
912 destroyConstantImpl();
913}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000914
Chris Lattner3462ae32001-12-03 22:26:30 +0000915//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000916//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000917
Chris Lattner189d19f2003-11-21 20:23:48 +0000918namespace llvm {
919 // ConstantPointerNull does not take extra "value" argument...
920 template<class ValType>
921 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
922 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
923 return new ConstantPointerNull(Ty);
924 }
925 };
Chris Lattner98fa07b2003-05-23 20:03:32 +0000926
Chris Lattner189d19f2003-11-21 20:23:48 +0000927 template<>
928 struct ConvertConstantType<ConstantPointerNull, PointerType> {
929 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
930 // Make everyone now use a constant of the new type...
931 Constant *New = ConstantPointerNull::get(NewTy);
932 assert(New != OldC && "Didn't replace constant??");
933 OldC->uncheckedReplaceAllUsesWith(New);
934 OldC->destroyConstant(); // This constant is now dead, destroy it.
935 }
936 };
937}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000938
Chris Lattner98fa07b2003-05-23 20:03:32 +0000939static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000940
Chris Lattner3462ae32001-12-03 22:26:30 +0000941ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000942 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000943}
944
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000945// destroyConstant - Remove the constant from the constant table...
946//
947void ConstantPointerNull::destroyConstant() {
948 NullPtrConstants.remove(this);
949 destroyConstantImpl();
950}
951
952
Chris Lattner3462ae32001-12-03 22:26:30 +0000953//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000954//
Chris Lattner3462ae32001-12-03 22:26:30 +0000955ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000956 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000957
Chris Lattnerd7a73302001-10-13 06:57:33 +0000958 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000959 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000960}
961
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000962// destroyConstant - Remove the constant from the constant table...
963//
964void ConstantPointerRef::destroyConstant() {
965 getValue()->getParent()->destroyConstantPointerRef(this);
966 destroyConstantImpl();
967}
968
969
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000970//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000971//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000972typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000973
Chris Lattner189d19f2003-11-21 20:23:48 +0000974namespace llvm {
975 template<>
976 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
977 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
978 if (V.first == Instruction::Cast)
979 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
980 if ((V.first >= Instruction::BinaryOpsBegin &&
981 V.first < Instruction::BinaryOpsEnd) ||
982 V.first == Instruction::Shl || V.first == Instruction::Shr)
983 return new ConstantExpr(V.first, V.second[0], V.second[1]);
984
985 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
986
987 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
988 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000989 }
Chris Lattner189d19f2003-11-21 20:23:48 +0000990 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000991
Chris Lattner189d19f2003-11-21 20:23:48 +0000992 template<>
993 struct ConvertConstantType<ConstantExpr, Type> {
994 static void convert(ConstantExpr *OldC, const Type *NewTy) {
995 Constant *New;
996 switch (OldC->getOpcode()) {
997 case Instruction::Cast:
998 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
999 break;
1000 case Instruction::Shl:
1001 case Instruction::Shr:
1002 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1003 OldC->getOperand(0), OldC->getOperand(1));
1004 break;
1005 default:
1006 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1007 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1008 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1009 OldC->getOperand(1));
1010 break;
1011 case Instruction::GetElementPtr:
1012 // Make everyone now use a constant of the new type...
1013 std::vector<Constant*> C;
1014 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
1015 C.push_back(cast<Constant>(OldC->getOperand(i)));
1016 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
1017 break;
1018 }
1019
1020 assert(New != OldC && "Didn't replace constant??");
1021 OldC->uncheckedReplaceAllUsesWith(New);
1022 OldC->destroyConstant(); // This constant is now dead, destroy it.
1023 }
1024 };
1025} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001026
1027
Chris Lattner98fa07b2003-05-23 20:03:32 +00001028static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001029
Chris Lattner46b3d302003-04-16 22:40:51 +00001030Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001031 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1032
Chris Lattneracdbe712003-04-17 19:24:48 +00001033 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1034 return FC; // Fold a few common cases...
1035
Vikram S. Adve4c485332002-07-15 18:19:33 +00001036 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001037 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001038 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1039 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001040}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001041
Chris Lattnerb50d1352003-10-05 00:17:43 +00001042Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1043 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001044 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1045 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001046 // Check the operands for consistency first
1047 assert((Opcode >= Instruction::BinaryOpsBegin &&
1048 Opcode < Instruction::BinaryOpsEnd) &&
1049 "Invalid opcode in binary constant expression");
1050 assert(C1->getType() == C2->getType() &&
1051 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001052
1053 if (ReqTy == C1->getType())
1054 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1055 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001056
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001057 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001058 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001059 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001060}
1061
Chris Lattner9eb2b522004-01-12 19:12:58 +00001062/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001063Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1064 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001065 // Check the operands for consistency first
1066 assert((Opcode == Instruction::Shl ||
1067 Opcode == Instruction::Shr) &&
1068 "Invalid opcode in binary constant expression");
1069 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1070 "Invalid operand types for Shift constant expr!");
1071
Chris Lattner0bba7712004-01-12 20:40:42 +00001072 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001073 return FC; // Fold a few common cases...
1074
1075 // Look up the constant in the table first to ensure uniqueness
1076 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001077 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001078 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001079}
1080
1081
Chris Lattnerb50d1352003-10-05 00:17:43 +00001082Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1083 const std::vector<Constant*> &IdxList) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001084 assert(GetElementPtrInst::getIndexedType(C->getType(),
1085 std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
1086 "GEP indices invalid!");
1087
Chris Lattneracdbe712003-04-17 19:24:48 +00001088 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1089 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001090
Chris Lattnerb50d1352003-10-05 00:17:43 +00001091 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001092 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001093 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001094 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +00001095 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001096 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001097 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001098}
1099
Chris Lattnerb50d1352003-10-05 00:17:43 +00001100Constant *ConstantExpr::getGetElementPtr(Constant *C,
1101 const std::vector<Constant*> &IdxList){
1102 // Get the result type of the getelementptr!
1103 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1104
1105 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1106 true);
1107 assert(Ty && "GEP indices invalid!");
1108 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1109}
1110
1111
Vikram S. Adve4c485332002-07-15 18:19:33 +00001112// destroyConstant - Remove the constant from the constant table...
1113//
1114void ConstantExpr::destroyConstant() {
1115 ExprConstants.remove(this);
1116 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001117}
1118
Chris Lattner3cd8c562002-07-30 18:54:25 +00001119const char *ConstantExpr::getOpcodeName() const {
1120 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001121}
1122
Chris Lattner163b8902002-10-14 03:30:23 +00001123unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
1124 // Uses of constant pointer refs are global values, not constants!
1125 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1126 GlobalValue *NewGV = cast<GlobalValue>(NewV);
1127 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001128
Chris Lattner163b8902002-10-14 03:30:23 +00001129 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattner163b8902002-10-14 03:30:23 +00001130 Operands[0] = NewGV;
Chris Lattnercb4d26f2003-05-15 19:37:21 +00001131 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
Chris Lattner163b8902002-10-14 03:30:23 +00001132 return 1;
1133 } else {
1134 Constant *NewC = cast<Constant>(NewV);
1135 unsigned NumReplaced = 0;
1136 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
1137 if (Operands[i] == OldV) {
1138 ++NumReplaced;
1139 Operands[i] = NewC;
1140 }
1141 return NumReplaced;
1142 }
Chris Lattner25033252001-10-03 19:28:15 +00001143}
Brian Gaeke960707c2003-11-11 22:41:34 +00001144