blob: e13a1daad52d8a9907076e2ac40ba3e1d26587d3 [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 Lattner5d56c472004-02-15 02:46:46 +000067static std::map<const Type *, Constant*> NullValues;
68
Chris Lattnerb1585a92002-08-13 17:50:20 +000069// Static constructor to create a '0' constant of arbitrary type...
70Constant *Constant::getNullValue(const Type *Ty) {
71 switch (Ty->getPrimitiveID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000072 case Type::BoolTyID: {
73 static Constant *NullBool = ConstantBool::get(false);
74 return NullBool;
75 }
76 case Type::SByteTyID: {
77 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
78 return NullSByte;
79 }
80 case Type::UByteTyID: {
81 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
82 return NullUByte;
83 }
84 case Type::ShortTyID: {
85 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
86 return NullShort;
87 }
88 case Type::UShortTyID: {
89 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
90 return NullUShort;
91 }
92 case Type::IntTyID: {
93 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
94 return NullInt;
95 }
96 case Type::UIntTyID: {
97 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
98 return NullUInt;
99 }
100 case Type::LongTyID: {
101 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
102 return NullLong;
103 }
104 case Type::ULongTyID: {
105 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
106 return NullULong;
107 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000108
Chris Lattner3e88ef92003-10-03 19:34:51 +0000109 case Type::FloatTyID: {
110 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
111 return NullFloat;
112 }
113 case Type::DoubleTyID: {
114 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
115 return NullDouble;
116 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000117
118 case Type::PointerTyID:
119 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000120
Chris Lattnerc33ae672003-03-06 21:02:18 +0000121 case Type::StructTyID: {
Chris Lattner5d56c472004-02-15 02:46:46 +0000122 if (!Ty->isAbstract())
123 if (Constant *V = NullValues[Ty])
124 return V;
125
Chris Lattnerc33ae672003-03-06 21:02:18 +0000126 const StructType *ST = cast<StructType>(Ty);
Chris Lattnerc33ae672003-03-06 21:02:18 +0000127 std::vector<Constant*> Elements;
Chris Lattnerac6db752004-02-09 04:37:31 +0000128 Elements.resize(ST->getNumElements());
129 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i)
130 Elements[i] = Constant::getNullValue(ST->getElementType(i));
Chris Lattner5d56c472004-02-15 02:46:46 +0000131 Constant *Ret = ConstantStruct::get(ST, Elements);
132 if (!Ty->isAbstract())
133 NullValues[Ty] = Ret;
134 return Ret;
Chris Lattnerc33ae672003-03-06 21:02:18 +0000135 }
136 case Type::ArrayTyID: {
Chris Lattner5d56c472004-02-15 02:46:46 +0000137 if (!Ty->isAbstract())
138 if (Constant *V = NullValues[Ty])
139 return V;
140
Chris Lattnerc33ae672003-03-06 21:02:18 +0000141 const ArrayType *AT = cast<ArrayType>(Ty);
142 Constant *El = Constant::getNullValue(AT->getElementType());
143 unsigned NumElements = AT->getNumElements();
Chris Lattner5d56c472004-02-15 02:46:46 +0000144 Constant *Ret = ConstantArray::get(AT,
145 std::vector<Constant*>(NumElements, El));
146 if (!Ty->isAbstract())
147 NullValues[Ty] = Ret;
148 return Ret;
Chris Lattnerc33ae672003-03-06 21:02:18 +0000149 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000150 default:
Chris Lattnerc33ae672003-03-06 21:02:18 +0000151 // Function, Type, Label, or Opaque type?
152 assert(0 && "Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000153 return 0;
154 }
155}
156
157// Static constructor to create the maximum constant of an integral type...
158ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
159 switch (Ty->getPrimitiveID()) {
160 case Type::BoolTyID: return ConstantBool::True;
161 case Type::SByteTyID:
162 case Type::ShortTyID:
163 case Type::IntTyID:
164 case Type::LongTyID: {
165 // Calculate 011111111111111...
166 unsigned TypeBits = Ty->getPrimitiveSize()*8;
167 int64_t Val = INT64_MAX; // All ones
168 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
169 return ConstantSInt::get(Ty, Val);
170 }
171
172 case Type::UByteTyID:
173 case Type::UShortTyID:
174 case Type::UIntTyID:
175 case Type::ULongTyID: return getAllOnesValue(Ty);
176
Chris Lattner31408f72002-08-14 17:12:13 +0000177 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000178 }
179}
180
181// Static constructor to create the minimum constant for an integral type...
182ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
183 switch (Ty->getPrimitiveID()) {
184 case Type::BoolTyID: return ConstantBool::False;
185 case Type::SByteTyID:
186 case Type::ShortTyID:
187 case Type::IntTyID:
188 case Type::LongTyID: {
189 // Calculate 1111111111000000000000
190 unsigned TypeBits = Ty->getPrimitiveSize()*8;
191 int64_t Val = -1; // All ones
192 Val <<= TypeBits-1; // Shift over to the right spot
193 return ConstantSInt::get(Ty, Val);
194 }
195
196 case Type::UByteTyID:
197 case Type::UShortTyID:
198 case Type::UIntTyID:
199 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
200
Chris Lattner31408f72002-08-14 17:12:13 +0000201 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000202 }
203}
204
205// Static constructor to create an integral constant with all bits set
206ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
207 switch (Ty->getPrimitiveID()) {
208 case Type::BoolTyID: return ConstantBool::True;
209 case Type::SByteTyID:
210 case Type::ShortTyID:
211 case Type::IntTyID:
212 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
213
214 case Type::UByteTyID:
215 case Type::UShortTyID:
216 case Type::UIntTyID:
217 case Type::ULongTyID: {
218 // Calculate ~0 of the right type...
219 unsigned TypeBits = Ty->getPrimitiveSize()*8;
220 uint64_t Val = ~0ULL; // All ones
221 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
222 return ConstantUInt::get(Ty, Val);
223 }
Chris Lattner31408f72002-08-14 17:12:13 +0000224 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000225 }
226}
227
Chris Lattner83e5d392003-03-10 22:39:02 +0000228bool ConstantUInt::isAllOnesValue() const {
229 unsigned TypeBits = getType()->getPrimitiveSize()*8;
230 uint64_t Val = ~0ULL; // All ones
231 Val >>= 64-TypeBits; // Shift out inappropriate bits
232 return getValue() == Val;
233}
234
Chris Lattnerb1585a92002-08-13 17:50:20 +0000235
Chris Lattner2f7c9632001-06-06 20:29:01 +0000236//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000237// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238//===----------------------------------------------------------------------===//
239
240//===----------------------------------------------------------------------===//
241// Normal Constructors
242
Chris Lattnerb1585a92002-08-13 17:50:20 +0000243ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000244 Val = V;
245}
Chris Lattner49d855c2001-09-07 16:46:31 +0000246
Chris Lattnerb1585a92002-08-13 17:50:20 +0000247ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000248 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000249}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000250
Chris Lattner3462ae32001-12-03 22:26:30 +0000251ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000252 assert(Ty->isInteger() && Ty->isSigned() &&
253 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000254 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000255}
256
Chris Lattner3462ae32001-12-03 22:26:30 +0000257ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000258 assert(Ty->isInteger() && Ty->isUnsigned() &&
259 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000260 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000261}
262
Chris Lattner3462ae32001-12-03 22:26:30 +0000263ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000264 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000265 Val = V;
266}
267
Chris Lattner3462ae32001-12-03 22:26:30 +0000268ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000269 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000270 Operands.reserve(V.size());
271 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerab69ecc2003-08-18 16:54:48 +0000272 assert(V[i]->getType() == T->getElementType() ||
273 (T->isAbstract() &&
274 V[i]->getType()->getPrimitiveID() ==
275 T->getElementType()->getPrimitiveID()));
Chris Lattnera073acb2001-07-07 08:36:50 +0000276 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000277 }
278}
279
Chris Lattner3462ae32001-12-03 22:26:30 +0000280ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000281 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000282 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000283 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000284 Operands.reserve(V.size());
285 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000286 assert((V[i]->getType() == T->getElementType(i) ||
287 ((T->getElementType(i)->isAbstract() ||
288 V[i]->getType()->isAbstract()) &&
289 T->getElementType(i)->getPrimitiveID() ==
290 V[i]->getType()->getPrimitiveID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000291 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000292 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000293 }
294}
295
Chris Lattner3462ae32001-12-03 22:26:30 +0000296ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
Chris Lattnere120a732003-11-17 19:47:21 +0000297 : Constant(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000298 Operands.push_back(Use(GV, this));
299}
300
Chris Lattner3cd8c562002-07-30 18:54:25 +0000301ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
302 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000303 Operands.push_back(Use(C, this));
304}
305
Chris Lattner22ced562003-06-22 20:48:30 +0000306static bool isSetCC(unsigned Opcode) {
307 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
308 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
309 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
310}
311
Chris Lattner3cd8c562002-07-30 18:54:25 +0000312ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner22ced562003-06-22 20:48:30 +0000313 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000314 Operands.push_back(Use(C1, this));
315 Operands.push_back(Use(C2, this));
316}
317
Chris Lattner3cd8c562002-07-30 18:54:25 +0000318ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
319 const Type *DestTy)
320 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000321 Operands.reserve(1+IdxList.size());
322 Operands.push_back(Use(C, this));
323 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
324 Operands.push_back(Use(IdxList[i], this));
325}
326
Chris Lattner60e0dd72001-10-03 06:12:09 +0000327
Chris Lattner2f7c9632001-06-06 20:29:01 +0000328
329//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000330// classof implementations
331
Chris Lattnerb1585a92002-08-13 17:50:20 +0000332bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000333 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000334}
335
Chris Lattner3462ae32001-12-03 22:26:30 +0000336bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000337 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000338}
Chris Lattner3462ae32001-12-03 22:26:30 +0000339bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000340 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000341}
Chris Lattner3462ae32001-12-03 22:26:30 +0000342bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000343 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000344}
Chris Lattner3462ae32001-12-03 22:26:30 +0000345bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000346 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000347 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000348 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000349}
Chris Lattner3462ae32001-12-03 22:26:30 +0000350bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000351 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000352}
Chris Lattner3462ae32001-12-03 22:26:30 +0000353bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000354 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000355}
Chris Lattnere120a732003-11-17 19:47:21 +0000356
357bool ConstantPointerNull::classof(const Constant *CPV) {
358 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
359 CPV->getNumOperands() == 0;
360}
361
362bool ConstantPointerRef::classof(const Constant *CPV) {
363 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
364 CPV->getNumOperands() == 1;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000365}
366
367
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000368
Chris Lattner2f7c9632001-06-06 20:29:01 +0000369//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000370// isValueValidForType implementations
371
Chris Lattner3462ae32001-12-03 22:26:30 +0000372bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000373 switch (Ty->getPrimitiveID()) {
374 default:
375 return false; // These can't be represented as integers!!!
376
377 // Signed types...
378 case Type::SByteTyID:
379 return (Val <= INT8_MAX && Val >= INT8_MIN);
380 case Type::ShortTyID:
381 return (Val <= INT16_MAX && Val >= INT16_MIN);
382 case Type::IntTyID:
383 return (Val <= INT32_MAX && Val >= INT32_MIN);
384 case Type::LongTyID:
385 return true; // This is the largest type...
386 }
387 assert(0 && "WTF?");
388 return false;
389}
390
Chris Lattner3462ae32001-12-03 22:26:30 +0000391bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000392 switch (Ty->getPrimitiveID()) {
393 default:
394 return false; // These can't be represented as integers!!!
395
396 // Unsigned types...
397 case Type::UByteTyID:
398 return (Val <= UINT8_MAX);
399 case Type::UShortTyID:
400 return (Val <= UINT16_MAX);
401 case Type::UIntTyID:
402 return (Val <= UINT32_MAX);
403 case Type::ULongTyID:
404 return true; // This is the largest type...
405 }
406 assert(0 && "WTF?");
407 return false;
408}
409
Chris Lattner3462ae32001-12-03 22:26:30 +0000410bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000411 switch (Ty->getPrimitiveID()) {
412 default:
413 return false; // These can't be represented as floating point!
414
415 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000416 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000417 case Type::DoubleTyID:
418 return true; // This is the largest type...
419 }
420};
Chris Lattner9655e542001-07-20 19:16:02 +0000421
Chris Lattner49d855c2001-09-07 16:46:31 +0000422//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000423// replaceUsesOfWithOnConstant implementations
424
Chris Lattnerc27038d2003-08-29 05:36:46 +0000425void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
426 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000427 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
428
429 std::vector<Constant*> Values;
430 Values.reserve(getValues().size()); // Build replacement array...
431 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
432 Constant *Val = cast<Constant>(getValues()[i]);
433 if (Val == From) Val = cast<Constant>(To);
434 Values.push_back(Val);
435 }
436
Chris Lattnerc75bf522004-02-15 04:05:58 +0000437 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000438 assert(Replacement != this && "I didn't contain From!");
439
440 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000441 if (DisableChecking)
442 uncheckedReplaceAllUsesWith(Replacement);
443 else
444 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000445
446 // Delete the old constant!
447 destroyConstant();
448}
449
Chris Lattnerc27038d2003-08-29 05:36:46 +0000450void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
451 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000452 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
453
454 std::vector<Constant*> Values;
455 Values.reserve(getValues().size());
456 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
457 Constant *Val = cast<Constant>(getValues()[i]);
458 if (Val == From) Val = cast<Constant>(To);
459 Values.push_back(Val);
460 }
461
Chris Lattner37a716f2004-02-15 04:07:32 +0000462 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000463 assert(Replacement != this && "I didn't contain From!");
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
471 // Delete the old constant!
472 destroyConstant();
473}
474
Chris Lattnerc27038d2003-08-29 05:36:46 +0000475void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
476 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000477 if (isa<GlobalValue>(To)) {
478 assert(From == getOperand(0) && "Doesn't contain from!");
479 ConstantPointerRef *Replacement =
480 ConstantPointerRef::get(cast<GlobalValue>(To));
481
482 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000483 if (DisableChecking)
484 uncheckedReplaceAllUsesWith(Replacement);
485 else
486 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000487
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000488 } else {
489 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000490 if (DisableChecking)
491 uncheckedReplaceAllUsesWith(To);
492 else
493 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000494 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000495
496 // Delete the old constant!
497 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000498}
499
Chris Lattnerc27038d2003-08-29 05:36:46 +0000500void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
501 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000502 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
503 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000504
Chris Lattner46b3d302003-04-16 22:40:51 +0000505 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000506 if (getOpcode() == Instruction::GetElementPtr) {
507 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000508 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000509 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000510 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000511
512 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000513 Constant *Val = getOperand(i);
514 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000515 Indices.push_back(Val);
516 }
517 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
518 } else if (getOpcode() == Instruction::Cast) {
519 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000520 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000521 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000522 Constant *C1 = getOperand(0);
523 Constant *C2 = getOperand(1);
524 if (C1 == From) C1 = To;
525 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000526 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
527 } else {
528 assert(0 && "Unknown ConstantExpr type!");
529 return;
530 }
531
532 assert(Replacement != this && "I didn't contain From!");
533
534 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000535 if (DisableChecking)
536 uncheckedReplaceAllUsesWith(Replacement);
537 else
538 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000539
540 // Delete the old constant!
541 destroyConstant();
542}
543
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000544//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000545// Factory Function Implementation
546
Chris Lattner98fa07b2003-05-23 20:03:32 +0000547// ConstantCreator - A class that is used to create constants by
548// ValueMap*. This class should be partially specialized if there is
549// something strange that needs to be done to interface to the ctor for the
550// constant.
551//
Chris Lattner189d19f2003-11-21 20:23:48 +0000552namespace llvm {
553 template<class ConstantClass, class TypeClass, class ValType>
554 struct ConstantCreator {
555 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
556 return new ConstantClass(Ty, V);
557 }
558 };
559
560 template<class ConstantClass, class TypeClass>
561 struct ConvertConstantType {
562 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
563 assert(0 && "This type cannot be converted!\n");
564 abort();
565 }
566 };
567}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000568
Chris Lattner98fa07b2003-05-23 20:03:32 +0000569namespace {
570 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000571 class ValueMap : public AbstractTypeUser {
572 typedef std::pair<const TypeClass*, ValType> MapKey;
573 typedef std::map<MapKey, ConstantClass *> MapTy;
574 typedef typename MapTy::iterator MapIterator;
575 MapTy Map;
576
577 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
578 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000579 public:
580 // getOrCreate - Return the specified constant from the map, creating it if
581 // necessary.
582 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000583 MapKey Lookup(Ty, V);
584 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000585 if (I != Map.end() && I->first == Lookup)
586 return I->second; // Is it in the map?
587
588 // If no preexisting value, create one now...
589 ConstantClass *Result =
590 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
591
Chris Lattnerb50d1352003-10-05 00:17:43 +0000592
593 /// FIXME: why does this assert fail when loading 176.gcc?
594 //assert(Result->getType() == Ty && "Type specified is not correct!");
595 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
596
597 // If the type of the constant is abstract, make sure that an entry exists
598 // for it in the AbstractTypeMap.
599 if (Ty->isAbstract()) {
600 typename AbstractTypeMapTy::iterator TI =
601 AbstractTypeMap.lower_bound(Ty);
602
603 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
604 // Add ourselves to the ATU list of the type.
605 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
606
607 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
608 }
609 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000610 return Result;
611 }
612
613 void remove(ConstantClass *CP) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000614 // FIXME: This should not use a linear scan. If this gets to be a
615 // performance problem, someone should look at this.
616 MapIterator I = Map.begin();
617 for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
618 /* empty */;
619
620 assert(I != Map.end() && "Constant not found in constant table!");
621
622 // Now that we found the entry, make sure this isn't the entry that
623 // the AbstractTypeMap points to.
624 const TypeClass *Ty = I->first.first;
625 if (Ty->isAbstract()) {
626 assert(AbstractTypeMap.count(Ty) &&
627 "Abstract type not in AbstractTypeMap?");
628 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
629 if (ATMEntryIt == I) {
630 // Yes, we are removing the representative entry for this type.
631 // See if there are any other entries of the same type.
632 MapIterator TmpIt = ATMEntryIt;
633
634 // First check the entry before this one...
635 if (TmpIt != Map.begin()) {
636 --TmpIt;
637 if (TmpIt->first.first != Ty) // Not the same type, move back...
638 ++TmpIt;
639 }
640
641 // If we didn't find the same type, try to move forward...
642 if (TmpIt == ATMEntryIt) {
643 ++TmpIt;
644 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
645 --TmpIt; // No entry afterwards with the same type
646 }
647
648 // If there is another entry in the map of the same abstract type,
649 // update the AbstractTypeMap entry now.
650 if (TmpIt != ATMEntryIt) {
651 ATMEntryIt = TmpIt;
652 } else {
653 // Otherwise, we are removing the last instance of this type
654 // from the table. Remove from the ATM, and from user list.
655 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
656 AbstractTypeMap.erase(Ty);
657 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000658 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000659 }
660
661 Map.erase(I);
662 }
663
664 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
665 typename AbstractTypeMapTy::iterator I =
666 AbstractTypeMap.find(cast<TypeClass>(OldTy));
667
668 assert(I != AbstractTypeMap.end() &&
669 "Abstract type not in AbstractTypeMap?");
670
671 // Convert a constant at a time until the last one is gone. The last one
672 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
673 // eliminated eventually.
674 do {
675 ConvertConstantType<ConstantClass,
676 TypeClass>::convert(I->second->second,
677 cast<TypeClass>(NewTy));
678
679 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
680 } while (I != AbstractTypeMap.end());
681 }
682
683 // If the type became concrete without being refined to any other existing
684 // type, we just remove ourselves from the ATU list.
685 void typeBecameConcrete(const DerivedType *AbsTy) {
686 AbsTy->removeAbstractTypeUser(this);
687 }
688
689 void dump() const {
690 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000691 }
692 };
693}
694
695
696
Chris Lattner3462ae32001-12-03 22:26:30 +0000697//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000698//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000699static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
700static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000701
Chris Lattner3462ae32001-12-03 22:26:30 +0000702ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000703 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000704}
705
Chris Lattner3462ae32001-12-03 22:26:30 +0000706ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000707 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000708}
709
Chris Lattner3462ae32001-12-03 22:26:30 +0000710ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000711 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000712 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
713 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000714}
715
Chris Lattner3462ae32001-12-03 22:26:30 +0000716//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000717//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000718namespace llvm {
719 template<>
720 struct ConstantCreator<ConstantFP, Type, uint64_t> {
721 static ConstantFP *create(const Type *Ty, uint64_t V) {
722 assert(Ty == Type::DoubleTy);
723 union {
724 double F;
725 uint64_t I;
726 } T;
727 T.I = V;
728 return new ConstantFP(Ty, T.F);
729 }
730 };
731 template<>
732 struct ConstantCreator<ConstantFP, Type, uint32_t> {
733 static ConstantFP *create(const Type *Ty, uint32_t V) {
734 assert(Ty == Type::FloatTy);
735 union {
736 float F;
737 uint32_t I;
738 } T;
739 T.I = V;
740 return new ConstantFP(Ty, T.F);
741 }
742 };
743}
744
745static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
746static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000747
Chris Lattner3462ae32001-12-03 22:26:30 +0000748ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000749 if (Ty == Type::FloatTy) {
750 // Force the value through memory to normalize it.
Chris Lattnerac80ea42004-02-01 22:49:04 +0000751 union {
752 float F;
753 uint32_t I;
754 } T;
755 T.F = (float)V;
756 return FloatConstants.getOrCreate(Ty, T.I);
757 } else {
758 assert(Ty == Type::DoubleTy);
759 union {
760 double F;
761 uint64_t I;
762 } T;
763 T.F = V;
764 return DoubleConstants.getOrCreate(Ty, T.I);
Chris Lattner241ed4c2004-01-23 00:55:21 +0000765 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000766}
767
Chris Lattner3462ae32001-12-03 22:26:30 +0000768//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000769//
Chris Lattner189d19f2003-11-21 20:23:48 +0000770namespace llvm {
771 template<>
772 struct ConvertConstantType<ConstantArray, ArrayType> {
773 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
774 // Make everyone now use a constant of the new type...
775 std::vector<Constant*> C;
776 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
777 C.push_back(cast<Constant>(OldC->getOperand(i)));
778 Constant *New = ConstantArray::get(NewTy, C);
779 assert(New != OldC && "Didn't replace constant??");
780 OldC->uncheckedReplaceAllUsesWith(New);
781 OldC->destroyConstant(); // This constant is now dead, destroy it.
782 }
783 };
784}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000785
Chris Lattner98fa07b2003-05-23 20:03:32 +0000786static ValueMap<std::vector<Constant*>, ArrayType,
787 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000788
Chris Lattner015e8212004-02-15 04:14:47 +0000789Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000790 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000791 return ArrayConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000792}
793
Chris Lattner98fa07b2003-05-23 20:03:32 +0000794// destroyConstant - Remove the constant from the constant table...
795//
796void ConstantArray::destroyConstant() {
797 ArrayConstants.remove(this);
798 destroyConstantImpl();
799}
800
Chris Lattner3462ae32001-12-03 22:26:30 +0000801// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000802// contain the specified string. A null terminator is added to the specified
803// string so that it may be used in a natural way...
804//
Chris Lattner015e8212004-02-15 04:14:47 +0000805Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000806 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000807
808 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000809 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000810
811 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000812 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000813
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000814 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000815 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000816}
817
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000818/// isString - This method returns true if the array is an array of sbyte or
819/// ubyte, and if the elements of the array are all ConstantInt's.
820bool ConstantArray::isString() const {
821 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000822 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000823 getType()->getElementType() != Type::SByteTy)
824 return false;
825 // Check the elements to make sure they are all integers, not constant
826 // expressions.
827 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
828 if (!isa<ConstantInt>(getOperand(i)))
829 return false;
830 return true;
831}
832
Chris Lattner81fabb02002-08-26 17:53:56 +0000833// getAsString - If the sub-element type of this array is either sbyte or ubyte,
834// then this method converts the array to an std::string and returns it.
835// Otherwise, it asserts out.
836//
837std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000838 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000839 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000840 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
841 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000842 return Result;
843}
844
845
Chris Lattner3462ae32001-12-03 22:26:30 +0000846//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000847//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000848
Chris Lattner189d19f2003-11-21 20:23:48 +0000849namespace llvm {
850 template<>
851 struct ConvertConstantType<ConstantStruct, StructType> {
852 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
853 // Make everyone now use a constant of the new type...
854 std::vector<Constant*> C;
855 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
856 C.push_back(cast<Constant>(OldC->getOperand(i)));
857 Constant *New = ConstantStruct::get(NewTy, C);
858 assert(New != OldC && "Didn't replace constant??");
859
860 OldC->uncheckedReplaceAllUsesWith(New);
861 OldC->destroyConstant(); // This constant is now dead, destroy it.
862 }
863 };
864}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000865
Chris Lattner98fa07b2003-05-23 20:03:32 +0000866static ValueMap<std::vector<Constant*>, StructType,
867 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000868
Chris Lattner015e8212004-02-15 04:14:47 +0000869Constant *ConstantStruct::get(const StructType *Ty,
870 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000871 return StructConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000872}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000873
Chris Lattnerd7a73302001-10-13 06:57:33 +0000874// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000875//
Chris Lattner3462ae32001-12-03 22:26:30 +0000876void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000877 StructConstants.remove(this);
878 destroyConstantImpl();
879}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000880
Chris Lattner3462ae32001-12-03 22:26:30 +0000881//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000882//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000883
Chris Lattner189d19f2003-11-21 20:23:48 +0000884namespace llvm {
885 // ConstantPointerNull does not take extra "value" argument...
886 template<class ValType>
887 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
888 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
889 return new ConstantPointerNull(Ty);
890 }
891 };
Chris Lattner98fa07b2003-05-23 20:03:32 +0000892
Chris Lattner189d19f2003-11-21 20:23:48 +0000893 template<>
894 struct ConvertConstantType<ConstantPointerNull, PointerType> {
895 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
896 // Make everyone now use a constant of the new type...
897 Constant *New = ConstantPointerNull::get(NewTy);
898 assert(New != OldC && "Didn't replace constant??");
899 OldC->uncheckedReplaceAllUsesWith(New);
900 OldC->destroyConstant(); // This constant is now dead, destroy it.
901 }
902 };
903}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000904
Chris Lattner98fa07b2003-05-23 20:03:32 +0000905static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000906
Chris Lattner3462ae32001-12-03 22:26:30 +0000907ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000908 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000909}
910
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000911// destroyConstant - Remove the constant from the constant table...
912//
913void ConstantPointerNull::destroyConstant() {
914 NullPtrConstants.remove(this);
915 destroyConstantImpl();
916}
917
918
Chris Lattner3462ae32001-12-03 22:26:30 +0000919//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000920//
Chris Lattner3462ae32001-12-03 22:26:30 +0000921ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000922 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000923
Chris Lattnerd7a73302001-10-13 06:57:33 +0000924 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000925 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000926}
927
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000928// destroyConstant - Remove the constant from the constant table...
929//
930void ConstantPointerRef::destroyConstant() {
931 getValue()->getParent()->destroyConstantPointerRef(this);
932 destroyConstantImpl();
933}
934
935
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000936//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000937//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000938typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000939
Chris Lattner189d19f2003-11-21 20:23:48 +0000940namespace llvm {
941 template<>
942 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
943 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
944 if (V.first == Instruction::Cast)
945 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
946 if ((V.first >= Instruction::BinaryOpsBegin &&
947 V.first < Instruction::BinaryOpsEnd) ||
948 V.first == Instruction::Shl || V.first == Instruction::Shr)
949 return new ConstantExpr(V.first, V.second[0], V.second[1]);
950
951 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
952
953 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
954 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000955 }
Chris Lattner189d19f2003-11-21 20:23:48 +0000956 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000957
Chris Lattner189d19f2003-11-21 20:23:48 +0000958 template<>
959 struct ConvertConstantType<ConstantExpr, Type> {
960 static void convert(ConstantExpr *OldC, const Type *NewTy) {
961 Constant *New;
962 switch (OldC->getOpcode()) {
963 case Instruction::Cast:
964 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
965 break;
966 case Instruction::Shl:
967 case Instruction::Shr:
968 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
969 OldC->getOperand(0), OldC->getOperand(1));
970 break;
971 default:
972 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
973 OldC->getOpcode() < Instruction::BinaryOpsEnd);
974 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
975 OldC->getOperand(1));
976 break;
977 case Instruction::GetElementPtr:
978 // Make everyone now use a constant of the new type...
979 std::vector<Constant*> C;
980 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
981 C.push_back(cast<Constant>(OldC->getOperand(i)));
982 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
983 break;
984 }
985
986 assert(New != OldC && "Didn't replace constant??");
987 OldC->uncheckedReplaceAllUsesWith(New);
988 OldC->destroyConstant(); // This constant is now dead, destroy it.
989 }
990 };
991} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +0000992
993
Chris Lattner98fa07b2003-05-23 20:03:32 +0000994static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000995
Chris Lattner46b3d302003-04-16 22:40:51 +0000996Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +0000997 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
998
Chris Lattneracdbe712003-04-17 19:24:48 +0000999 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1000 return FC; // Fold a few common cases...
1001
Vikram S. Adve4c485332002-07-15 18:19:33 +00001002 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001003 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001004 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1005 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001006}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001007
Chris Lattnerb50d1352003-10-05 00:17:43 +00001008Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1009 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001010 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1011 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001012 // Check the operands for consistency first
1013 assert((Opcode >= Instruction::BinaryOpsBegin &&
1014 Opcode < Instruction::BinaryOpsEnd) &&
1015 "Invalid opcode in binary constant expression");
1016 assert(C1->getType() == C2->getType() &&
1017 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001018
1019 if (ReqTy == C1->getType())
1020 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1021 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001022
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001023 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001024 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001025 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001026}
1027
Chris Lattner9eb2b522004-01-12 19:12:58 +00001028/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001029Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1030 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001031 // Check the operands for consistency first
1032 assert((Opcode == Instruction::Shl ||
1033 Opcode == Instruction::Shr) &&
1034 "Invalid opcode in binary constant expression");
1035 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1036 "Invalid operand types for Shift constant expr!");
1037
Chris Lattner0bba7712004-01-12 20:40:42 +00001038 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001039 return FC; // Fold a few common cases...
1040
1041 // Look up the constant in the table first to ensure uniqueness
1042 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001043 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001044 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001045}
1046
1047
Chris Lattnerb50d1352003-10-05 00:17:43 +00001048Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1049 const std::vector<Constant*> &IdxList) {
Chris Lattneracdbe712003-04-17 19:24:48 +00001050 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1051 return FC; // Fold a few common cases...
Chris Lattnerb50d1352003-10-05 00:17:43 +00001052 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001053 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001054
1055 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001056 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +00001057 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001058 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001059 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001060}
1061
Chris Lattnerb50d1352003-10-05 00:17:43 +00001062Constant *ConstantExpr::getGetElementPtr(Constant *C,
1063 const std::vector<Constant*> &IdxList){
1064 // Get the result type of the getelementptr!
1065 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1066
1067 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1068 true);
1069 assert(Ty && "GEP indices invalid!");
Chris Lattner8a552622003-10-23 05:21:48 +00001070
1071 if (C->isNullValue()) {
1072 bool isNull = true;
1073 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1074 if (!IdxList[i]->isNullValue()) {
1075 isNull = false;
1076 break;
1077 }
1078 if (isNull) return ConstantPointerNull::get(PointerType::get(Ty));
1079 }
1080
Chris Lattnerb50d1352003-10-05 00:17:43 +00001081 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1082}
1083
1084
Vikram S. Adve4c485332002-07-15 18:19:33 +00001085// destroyConstant - Remove the constant from the constant table...
1086//
1087void ConstantExpr::destroyConstant() {
1088 ExprConstants.remove(this);
1089 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001090}
1091
Chris Lattner3cd8c562002-07-30 18:54:25 +00001092const char *ConstantExpr::getOpcodeName() const {
1093 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001094}
1095
Chris Lattner163b8902002-10-14 03:30:23 +00001096unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
1097 // Uses of constant pointer refs are global values, not constants!
1098 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1099 GlobalValue *NewGV = cast<GlobalValue>(NewV);
1100 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001101
Chris Lattner163b8902002-10-14 03:30:23 +00001102 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattner163b8902002-10-14 03:30:23 +00001103 Operands[0] = NewGV;
Chris Lattnercb4d26f2003-05-15 19:37:21 +00001104 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
Chris Lattner163b8902002-10-14 03:30:23 +00001105 return 1;
1106 } else {
1107 Constant *NewC = cast<Constant>(NewV);
1108 unsigned NumReplaced = 0;
1109 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
1110 if (Operands[i] == OldV) {
1111 ++NumReplaced;
1112 Operands[i] = NewC;
1113 }
1114 return NumReplaced;
1115 }
Chris Lattner25033252001-10-03 19:28:15 +00001116}
Brian Gaeke960707c2003-11-11 22:41:34 +00001117