blob: 8148f44b0331546a122f36f45be3f045673dd911 [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"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000022#include <algorithm>
Reid Spencercf394bf2004-07-04 11:51:24 +000023#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner3462ae32001-12-03 22:26:30 +000026ConstantBool *ConstantBool::True = new ConstantBool(true);
27ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000028
Chris Lattner9655e542001-07-20 19:16:02 +000029
Chris Lattner2f7c9632001-06-06 20:29:01 +000030//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000031// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000032//===----------------------------------------------------------------------===//
33
34// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000035void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000036 assert(ST && "Type::setName - Must provide symbol table argument!");
37
38 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000039}
40
Chris Lattner3462ae32001-12-03 22:26:30 +000041void Constant::destroyConstantImpl() {
42 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000043 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000044 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000045 // but they don't know that. Because we only find out when the CPV is
46 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000047 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000048 //
49 while (!use_empty()) {
50 Value *V = use_back();
51#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000052 if (!isa<Constant>(V))
53 std::cerr << "While deleting: " << *this
54 << "\n\nUse still stuck around after Def is destroyed: "
55 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000056#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000057 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000058 Constant *CV = cast<Constant>(V);
59 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000060
61 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000062 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000063 }
64
65 // Value has no outstanding references it is safe to delete it now...
66 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000067}
Chris Lattner2f7c9632001-06-06 20:29:01 +000068
Chris Lattnerb1585a92002-08-13 17:50:20 +000069// Static constructor to create a '0' constant of arbitrary type...
70Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000071 switch (Ty->getTypeID()) {
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 Lattner9fba3da2004-02-15 05:53:04 +0000121 case Type::StructTyID:
122 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000123 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000124 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000125 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000126 // Function, Label, or Opaque type?
127 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000128 return 0;
129 }
130}
131
132// Static constructor to create the maximum constant of an integral type...
133ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000134 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000135 case Type::BoolTyID: return ConstantBool::True;
136 case Type::SByteTyID:
137 case Type::ShortTyID:
138 case Type::IntTyID:
139 case Type::LongTyID: {
140 // Calculate 011111111111111...
141 unsigned TypeBits = Ty->getPrimitiveSize()*8;
142 int64_t Val = INT64_MAX; // All ones
143 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
144 return ConstantSInt::get(Ty, Val);
145 }
146
147 case Type::UByteTyID:
148 case Type::UShortTyID:
149 case Type::UIntTyID:
150 case Type::ULongTyID: return getAllOnesValue(Ty);
151
Chris Lattner31408f72002-08-14 17:12:13 +0000152 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000153 }
154}
155
156// Static constructor to create the minimum constant for an integral type...
157ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000158 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000159 case Type::BoolTyID: return ConstantBool::False;
160 case Type::SByteTyID:
161 case Type::ShortTyID:
162 case Type::IntTyID:
163 case Type::LongTyID: {
164 // Calculate 1111111111000000000000
165 unsigned TypeBits = Ty->getPrimitiveSize()*8;
166 int64_t Val = -1; // All ones
167 Val <<= TypeBits-1; // Shift over to the right spot
168 return ConstantSInt::get(Ty, Val);
169 }
170
171 case Type::UByteTyID:
172 case Type::UShortTyID:
173 case Type::UIntTyID:
174 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
175
Chris Lattner31408f72002-08-14 17:12:13 +0000176 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000177 }
178}
179
180// Static constructor to create an integral constant with all bits set
181ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000182 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000183 case Type::BoolTyID: return ConstantBool::True;
184 case Type::SByteTyID:
185 case Type::ShortTyID:
186 case Type::IntTyID:
187 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
188
189 case Type::UByteTyID:
190 case Type::UShortTyID:
191 case Type::UIntTyID:
192 case Type::ULongTyID: {
193 // Calculate ~0 of the right type...
194 unsigned TypeBits = Ty->getPrimitiveSize()*8;
195 uint64_t Val = ~0ULL; // All ones
196 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
197 return ConstantUInt::get(Ty, Val);
198 }
Chris Lattner31408f72002-08-14 17:12:13 +0000199 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000200 }
201}
202
Chris Lattner83e5d392003-03-10 22:39:02 +0000203bool ConstantUInt::isAllOnesValue() const {
204 unsigned TypeBits = getType()->getPrimitiveSize()*8;
205 uint64_t Val = ~0ULL; // All ones
206 Val >>= 64-TypeBits; // Shift out inappropriate bits
207 return getValue() == Val;
208}
209
Chris Lattnerb1585a92002-08-13 17:50:20 +0000210
Chris Lattner2f7c9632001-06-06 20:29:01 +0000211//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000212// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213//===----------------------------------------------------------------------===//
214
215//===----------------------------------------------------------------------===//
216// Normal Constructors
217
Chris Lattner265eb642004-06-21 12:12:12 +0000218ConstantIntegral::ConstantIntegral(const Type *Ty, uint64_t V)
219 : Constant(Ty) {
220 Val.Unsigned = V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221}
Chris Lattner49d855c2001-09-07 16:46:31 +0000222
Chris Lattner265eb642004-06-21 12:12:12 +0000223ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy, V) {
224}
225
226ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty, V) {
Chris Lattner7309d662001-07-21 19:16:08 +0000227}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228
Chris Lattner3462ae32001-12-03 22:26:30 +0000229ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000230 assert(Ty->isInteger() && Ty->isSigned() &&
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 +0000235ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000236 assert(Ty->isInteger() && Ty->isUnsigned() &&
237 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000238 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000239}
240
Chris Lattner3462ae32001-12-03 22:26:30 +0000241ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000242 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000243 Val = V;
244}
245
Chris Lattner3462ae32001-12-03 22:26:30 +0000246ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000247 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000248 Operands.reserve(V.size());
249 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000250 assert((V[i]->getType() == T->getElementType() ||
251 (T->isAbstract() &&
252 V[i]->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
253 "Initializer for array element doesn't match array element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000254 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000255 }
256}
257
Chris Lattner3462ae32001-12-03 22:26:30 +0000258ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000259 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000260 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000261 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000262 Operands.reserve(V.size());
263 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000264 assert((V[i]->getType() == T->getElementType(i) ||
265 ((T->getElementType(i)->isAbstract() ||
266 V[i]->getType()->isAbstract()) &&
Chris Lattner6b727592004-06-17 18:19:28 +0000267 T->getElementType(i)->getTypeID() == V[i]->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000268 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000269 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000270 }
271}
272
Brian Gaeke02209042004-08-20 06:00:58 +0000273ConstantPacked::ConstantPacked(const PackedType *T,
274 const std::vector<Constant*> &V) : Constant(T) {
275 Operands.reserve(V.size());
276 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000277 assert((V[i]->getType() == T->getElementType() ||
278 (T->isAbstract() &&
279 V[i]->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
280 "Initializer for packed element doesn't match packed element type!");
Brian Gaeke02209042004-08-20 06:00:58 +0000281 Operands.push_back(Use(V[i], this));
282 }
283}
284
Chris Lattner3cd8c562002-07-30 18:54:25 +0000285ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Chris Lattner5230e702004-07-19 00:59:10 +0000286 : Constant(Ty, ConstantExprVal), iType(Opcode) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000287 Operands.reserve(1);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000288 Operands.push_back(Use(C, this));
289}
290
Chris Lattner6e415c02004-03-12 05:54:04 +0000291// Select instruction creation ctor
292ConstantExpr::ConstantExpr(Constant *C, Constant *V1, Constant *V2)
Chris Lattner5230e702004-07-19 00:59:10 +0000293 : Constant(V1->getType(), ConstantExprVal), iType(Instruction::Select) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000294 Operands.reserve(3);
295 Operands.push_back(Use(C, this));
296 Operands.push_back(Use(V1, this));
297 Operands.push_back(Use(V2, this));
298}
299
300
Chris Lattner22ced562003-06-22 20:48:30 +0000301static bool isSetCC(unsigned Opcode) {
302 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
303 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
304 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
305}
306
Chris Lattner3cd8c562002-07-30 18:54:25 +0000307ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner5230e702004-07-19 00:59:10 +0000308 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType(), ConstantExprVal),
309 iType(Opcode) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000310 Operands.reserve(2);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000311 Operands.push_back(Use(C1, this));
312 Operands.push_back(Use(C2, this));
313}
314
Chris Lattner3cd8c562002-07-30 18:54:25 +0000315ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
316 const Type *DestTy)
Chris Lattner5230e702004-07-19 00:59:10 +0000317 : Constant(DestTy, ConstantExprVal), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000318 Operands.reserve(1+IdxList.size());
319 Operands.push_back(Use(C, this));
320 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
321 Operands.push_back(Use(IdxList[i], this));
322}
323
Chris Lattner817175f2004-03-29 02:37:53 +0000324/// ConstantExpr::get* - Return some common constants without having to
325/// specify the full Instruction::OPCODE identifier.
326///
327Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000328 if (!C->getType()->isFloatingPoint())
329 return get(Instruction::Sub, getNullValue(C->getType()), C);
330 else
331 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000332}
333Constant *ConstantExpr::getNot(Constant *C) {
334 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
335 return get(Instruction::Xor, C,
336 ConstantIntegral::getAllOnesValue(C->getType()));
337}
338Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
339 return get(Instruction::Add, C1, C2);
340}
341Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
342 return get(Instruction::Sub, C1, C2);
343}
344Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
345 return get(Instruction::Mul, C1, C2);
346}
347Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
348 return get(Instruction::Div, C1, C2);
349}
350Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
351 return get(Instruction::Rem, C1, C2);
352}
353Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
354 return get(Instruction::And, C1, C2);
355}
356Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
357 return get(Instruction::Or, C1, C2);
358}
359Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
360 return get(Instruction::Xor, C1, C2);
361}
362Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
363 return get(Instruction::SetEQ, C1, C2);
364}
365Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
366 return get(Instruction::SetNE, C1, C2);
367}
368Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
369 return get(Instruction::SetLT, C1, C2);
370}
371Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
372 return get(Instruction::SetGT, C1, C2);
373}
374Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
375 return get(Instruction::SetLE, C1, C2);
376}
377Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
378 return get(Instruction::SetGE, C1, C2);
379}
380Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
381 return get(Instruction::Shl, C1, C2);
382}
383Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
384 return get(Instruction::Shr, C1, C2);
385}
386
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000387Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
388 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
389 return getCast(getShr(getCast(C1,
390 C1->getType()->getUnsignedVersion()), C2), C1->getType());
391}
Chris Lattner817175f2004-03-29 02:37:53 +0000392
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000393Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
394 if (C1->getType()->isSigned()) return getShr(C1, C2);
395 return getCast(getShr(getCast(C1,
396 C1->getType()->getSignedVersion()), C2), C1->getType());
397}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000398
Chris Lattner2f7c9632001-06-06 20:29:01 +0000399
400//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000401// isValueValidForType implementations
402
Chris Lattner3462ae32001-12-03 22:26:30 +0000403bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000404 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000405 default:
406 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000407 // Signed types...
408 case Type::SByteTyID:
409 return (Val <= INT8_MAX && Val >= INT8_MIN);
410 case Type::ShortTyID:
411 return (Val <= INT16_MAX && Val >= INT16_MIN);
412 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000413 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000414 case Type::LongTyID:
415 return true; // This is the largest type...
416 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000417}
418
Chris Lattner3462ae32001-12-03 22:26:30 +0000419bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000420 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000421 default:
422 return false; // These can't be represented as integers!!!
423
424 // Unsigned types...
425 case Type::UByteTyID:
426 return (Val <= UINT8_MAX);
427 case Type::UShortTyID:
428 return (Val <= UINT16_MAX);
429 case Type::UIntTyID:
430 return (Val <= UINT32_MAX);
431 case Type::ULongTyID:
432 return true; // This is the largest type...
433 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000434}
435
Chris Lattner3462ae32001-12-03 22:26:30 +0000436bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000437 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000438 default:
439 return false; // These can't be represented as floating point!
440
441 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000442 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000443 case Type::DoubleTyID:
444 return true; // This is the largest type...
445 }
446};
Chris Lattner9655e542001-07-20 19:16:02 +0000447
Chris Lattner49d855c2001-09-07 16:46:31 +0000448//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000449// replaceUsesOfWithOnConstant implementations
450
Chris Lattnerc27038d2003-08-29 05:36:46 +0000451void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
452 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000453 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
454
455 std::vector<Constant*> Values;
Alkis Evlogimenosf0cc8142004-08-04 08:02:59 +0000456 Values.reserve(getNumOperands()); // Build replacement array...
457 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
458 Constant *Val = getOperand(i);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000459 if (Val == From) Val = cast<Constant>(To);
460 Values.push_back(Val);
461 }
462
Chris Lattnerc75bf522004-02-15 04:05:58 +0000463 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000464 assert(Replacement != this && "I didn't contain From!");
465
466 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000467 if (DisableChecking)
468 uncheckedReplaceAllUsesWith(Replacement);
469 else
470 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000471
472 // Delete the old constant!
473 destroyConstant();
474}
475
Chris Lattnerc27038d2003-08-29 05:36:46 +0000476void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
477 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000478 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
479
480 std::vector<Constant*> Values;
Alkis Evlogimenosf0cc8142004-08-04 08:02:59 +0000481 Values.reserve(getNumOperands()); // Build replacement array...
482 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
483 Constant *Val = getOperand(i);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000484 if (Val == From) Val = cast<Constant>(To);
485 Values.push_back(Val);
486 }
487
Chris Lattner37a716f2004-02-15 04:07:32 +0000488 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000489 assert(Replacement != this && "I didn't contain From!");
490
491 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000492 if (DisableChecking)
493 uncheckedReplaceAllUsesWith(Replacement);
494 else
495 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000496
497 // Delete the old constant!
498 destroyConstant();
499}
500
Brian Gaeke02209042004-08-20 06:00:58 +0000501void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
502 bool DisableChecking) {
503 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
504
505 std::vector<Constant*> Values;
506 Values.reserve(getNumOperands()); // Build replacement array...
507 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
508 Constant *Val = getOperand(i);
509 if (Val == From) Val = cast<Constant>(To);
510 Values.push_back(Val);
511 }
512
513 Constant *Replacement = ConstantPacked::get(getType(), Values);
514 assert(Replacement != this && "I didn't contain From!");
515
516 // Everyone using this now uses the replacement...
517 if (DisableChecking)
518 uncheckedReplaceAllUsesWith(Replacement);
519 else
520 replaceAllUsesWith(Replacement);
521
522 // Delete the old constant!
523 destroyConstant();
524}
525
Chris Lattnerc27038d2003-08-29 05:36:46 +0000526void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
527 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000528 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
529 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000530
Chris Lattner46b3d302003-04-16 22:40:51 +0000531 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000532 if (getOpcode() == Instruction::GetElementPtr) {
533 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000534 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000535 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000536 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000537
538 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000539 Constant *Val = getOperand(i);
540 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000541 Indices.push_back(Val);
542 }
543 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
544 } else if (getOpcode() == Instruction::Cast) {
545 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000546 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattner467cb2b2004-03-31 02:56:11 +0000547 } else if (getOpcode() == Instruction::Select) {
548 Constant *C1 = getOperand(0);
549 Constant *C2 = getOperand(1);
550 Constant *C3 = getOperand(2);
551 if (C1 == From) C1 = To;
552 if (C2 == From) C2 = To;
553 if (C3 == From) C3 = To;
554 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000555 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000556 Constant *C1 = getOperand(0);
557 Constant *C2 = getOperand(1);
558 if (C1 == From) C1 = To;
559 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000560 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
561 } else {
562 assert(0 && "Unknown ConstantExpr type!");
563 return;
564 }
565
566 assert(Replacement != this && "I didn't contain From!");
567
568 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000569 if (DisableChecking)
570 uncheckedReplaceAllUsesWith(Replacement);
571 else
572 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000573
574 // Delete the old constant!
575 destroyConstant();
576}
577
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000578//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000579// Factory Function Implementation
580
Chris Lattner98fa07b2003-05-23 20:03:32 +0000581// ConstantCreator - A class that is used to create constants by
582// ValueMap*. This class should be partially specialized if there is
583// something strange that needs to be done to interface to the ctor for the
584// constant.
585//
Chris Lattner189d19f2003-11-21 20:23:48 +0000586namespace llvm {
587 template<class ConstantClass, class TypeClass, class ValType>
588 struct ConstantCreator {
589 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
590 return new ConstantClass(Ty, V);
591 }
592 };
593
594 template<class ConstantClass, class TypeClass>
595 struct ConvertConstantType {
596 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
597 assert(0 && "This type cannot be converted!\n");
598 abort();
599 }
600 };
601}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000602
Chris Lattner98fa07b2003-05-23 20:03:32 +0000603namespace {
604 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000605 class ValueMap : public AbstractTypeUser {
606 typedef std::pair<const TypeClass*, ValType> MapKey;
607 typedef std::map<MapKey, ConstantClass *> MapTy;
608 typedef typename MapTy::iterator MapIterator;
609 MapTy Map;
610
611 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
612 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000613 public:
614 // getOrCreate - Return the specified constant from the map, creating it if
615 // necessary.
616 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000617 MapKey Lookup(Ty, V);
618 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000619 if (I != Map.end() && I->first == Lookup)
620 return I->second; // Is it in the map?
621
622 // If no preexisting value, create one now...
623 ConstantClass *Result =
624 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
625
Chris Lattnerb50d1352003-10-05 00:17:43 +0000626
627 /// FIXME: why does this assert fail when loading 176.gcc?
628 //assert(Result->getType() == Ty && "Type specified is not correct!");
629 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
630
631 // If the type of the constant is abstract, make sure that an entry exists
632 // for it in the AbstractTypeMap.
633 if (Ty->isAbstract()) {
634 typename AbstractTypeMapTy::iterator TI =
635 AbstractTypeMap.lower_bound(Ty);
636
637 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
638 // Add ourselves to the ATU list of the type.
639 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
640
641 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
642 }
643 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000644 return Result;
645 }
646
647 void remove(ConstantClass *CP) {
Chris Lattner3e650af2004-08-04 04:48:01 +0000648 MapIterator I = Map.find(MapKey((TypeClass*)CP->getRawType(),
649 getValType(CP)));
Chris Lattner20a4dab2004-08-04 22:26:13 +0000650 if (I == Map.end() || I->second != CP) {
651 // FIXME: This should not use a linear scan. If this gets to be a
652 // performance problem, someone should look at this.
653 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
654 /* empty */;
655 }
656
Chris Lattnerb50d1352003-10-05 00:17:43 +0000657 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000658 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000659
660 // Now that we found the entry, make sure this isn't the entry that
661 // the AbstractTypeMap points to.
662 const TypeClass *Ty = I->first.first;
663 if (Ty->isAbstract()) {
664 assert(AbstractTypeMap.count(Ty) &&
665 "Abstract type not in AbstractTypeMap?");
666 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
667 if (ATMEntryIt == I) {
668 // Yes, we are removing the representative entry for this type.
669 // See if there are any other entries of the same type.
670 MapIterator TmpIt = ATMEntryIt;
671
672 // First check the entry before this one...
673 if (TmpIt != Map.begin()) {
674 --TmpIt;
675 if (TmpIt->first.first != Ty) // Not the same type, move back...
676 ++TmpIt;
677 }
678
679 // If we didn't find the same type, try to move forward...
680 if (TmpIt == ATMEntryIt) {
681 ++TmpIt;
682 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
683 --TmpIt; // No entry afterwards with the same type
684 }
685
686 // If there is another entry in the map of the same abstract type,
687 // update the AbstractTypeMap entry now.
688 if (TmpIt != ATMEntryIt) {
689 ATMEntryIt = TmpIt;
690 } else {
691 // Otherwise, we are removing the last instance of this type
692 // from the table. Remove from the ATM, and from user list.
693 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
694 AbstractTypeMap.erase(Ty);
695 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000696 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000697 }
698
699 Map.erase(I);
700 }
701
702 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
703 typename AbstractTypeMapTy::iterator I =
704 AbstractTypeMap.find(cast<TypeClass>(OldTy));
705
706 assert(I != AbstractTypeMap.end() &&
707 "Abstract type not in AbstractTypeMap?");
708
709 // Convert a constant at a time until the last one is gone. The last one
710 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
711 // eliminated eventually.
712 do {
713 ConvertConstantType<ConstantClass,
714 TypeClass>::convert(I->second->second,
715 cast<TypeClass>(NewTy));
716
717 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
718 } while (I != AbstractTypeMap.end());
719 }
720
721 // If the type became concrete without being refined to any other existing
722 // type, we just remove ourselves from the ATU list.
723 void typeBecameConcrete(const DerivedType *AbsTy) {
724 AbsTy->removeAbstractTypeUser(this);
725 }
726
727 void dump() const {
728 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000729 }
730 };
731}
732
Chris Lattner3462ae32001-12-03 22:26:30 +0000733//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000734//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000735static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
736static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000737
Chris Lattner3462ae32001-12-03 22:26:30 +0000738ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000739 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000740}
741
Chris Lattner3462ae32001-12-03 22:26:30 +0000742ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000743 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000744}
745
Chris Lattner3462ae32001-12-03 22:26:30 +0000746ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000747 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000748 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
749 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000750}
751
Chris Lattner3462ae32001-12-03 22:26:30 +0000752//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000753//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000754namespace llvm {
755 template<>
756 struct ConstantCreator<ConstantFP, Type, uint64_t> {
757 static ConstantFP *create(const Type *Ty, uint64_t V) {
758 assert(Ty == Type::DoubleTy);
759 union {
760 double F;
761 uint64_t I;
762 } T;
763 T.I = V;
764 return new ConstantFP(Ty, T.F);
765 }
766 };
767 template<>
768 struct ConstantCreator<ConstantFP, Type, uint32_t> {
769 static ConstantFP *create(const Type *Ty, uint32_t V) {
770 assert(Ty == Type::FloatTy);
771 union {
772 float F;
773 uint32_t I;
774 } T;
775 T.I = V;
776 return new ConstantFP(Ty, T.F);
777 }
778 };
779}
780
781static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
782static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000783
Chris Lattner3462ae32001-12-03 22:26:30 +0000784ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000785 if (Ty == Type::FloatTy) {
786 // Force the value through memory to normalize it.
Chris Lattnerac80ea42004-02-01 22:49:04 +0000787 union {
788 float F;
789 uint32_t I;
790 } T;
791 T.F = (float)V;
792 return FloatConstants.getOrCreate(Ty, T.I);
793 } else {
794 assert(Ty == Type::DoubleTy);
795 union {
796 double F;
797 uint64_t I;
798 } T;
799 T.F = V;
800 return DoubleConstants.getOrCreate(Ty, T.I);
Chris Lattner241ed4c2004-01-23 00:55:21 +0000801 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000802}
803
Chris Lattner9fba3da2004-02-15 05:53:04 +0000804//---- ConstantAggregateZero::get() implementation...
805//
806namespace llvm {
807 // ConstantAggregateZero does not take extra "value" argument...
808 template<class ValType>
809 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
810 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
811 return new ConstantAggregateZero(Ty);
812 }
813 };
814
815 template<>
816 struct ConvertConstantType<ConstantAggregateZero, Type> {
817 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
818 // Make everyone now use a constant of the new type...
819 Constant *New = ConstantAggregateZero::get(NewTy);
820 assert(New != OldC && "Didn't replace constant??");
821 OldC->uncheckedReplaceAllUsesWith(New);
822 OldC->destroyConstant(); // This constant is now dead, destroy it.
823 }
824 };
825}
826
827static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
828
Chris Lattner3e650af2004-08-04 04:48:01 +0000829static char getValType(ConstantAggregateZero *CPZ) { return 0; }
830
Chris Lattner9fba3da2004-02-15 05:53:04 +0000831Constant *ConstantAggregateZero::get(const Type *Ty) {
832 return AggZeroConstants.getOrCreate(Ty, 0);
833}
834
835// destroyConstant - Remove the constant from the constant table...
836//
837void ConstantAggregateZero::destroyConstant() {
838 AggZeroConstants.remove(this);
839 destroyConstantImpl();
840}
841
842void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
843 bool DisableChecking) {
844 assert(0 && "No uses!");
845 abort();
846}
847
848
849
Chris Lattner3462ae32001-12-03 22:26:30 +0000850//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000851//
Chris Lattner189d19f2003-11-21 20:23:48 +0000852namespace llvm {
853 template<>
854 struct ConvertConstantType<ConstantArray, ArrayType> {
855 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
856 // Make everyone now use a constant of the new type...
857 std::vector<Constant*> C;
858 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
859 C.push_back(cast<Constant>(OldC->getOperand(i)));
860 Constant *New = ConstantArray::get(NewTy, C);
861 assert(New != OldC && "Didn't replace constant??");
862 OldC->uncheckedReplaceAllUsesWith(New);
863 OldC->destroyConstant(); // This constant is now dead, destroy it.
864 }
865 };
866}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000867
Chris Lattner3e650af2004-08-04 04:48:01 +0000868static std::vector<Constant*> getValType(ConstantArray *CA) {
869 std::vector<Constant*> Elements;
870 Elements.reserve(CA->getNumOperands());
871 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
872 Elements.push_back(cast<Constant>(CA->getOperand(i)));
873 return Elements;
874}
875
Chris Lattner98fa07b2003-05-23 20:03:32 +0000876static ValueMap<std::vector<Constant*>, ArrayType,
877 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000878
Chris Lattner015e8212004-02-15 04:14:47 +0000879Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000880 const std::vector<Constant*> &V) {
881 // If this is an all-zero array, return a ConstantAggregateZero object
882 if (!V.empty()) {
883 Constant *C = V[0];
884 if (!C->isNullValue())
885 return ArrayConstants.getOrCreate(Ty, V);
886 for (unsigned i = 1, e = V.size(); i != e; ++i)
887 if (V[i] != C)
888 return ArrayConstants.getOrCreate(Ty, V);
889 }
890 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000891}
892
Chris Lattner98fa07b2003-05-23 20:03:32 +0000893// destroyConstant - Remove the constant from the constant table...
894//
895void ConstantArray::destroyConstant() {
896 ArrayConstants.remove(this);
897 destroyConstantImpl();
898}
899
Chris Lattner3462ae32001-12-03 22:26:30 +0000900// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000901// contain the specified string. A null terminator is added to the specified
902// string so that it may be used in a natural way...
903//
Chris Lattner015e8212004-02-15 04:14:47 +0000904Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000905 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000906
907 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000908 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000909
910 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000911 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000912
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000913 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000914 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000915}
916
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000917/// isString - This method returns true if the array is an array of sbyte or
918/// ubyte, and if the elements of the array are all ConstantInt's.
919bool ConstantArray::isString() const {
920 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000921 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000922 getType()->getElementType() != Type::SByteTy)
923 return false;
924 // Check the elements to make sure they are all integers, not constant
925 // expressions.
926 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
927 if (!isa<ConstantInt>(getOperand(i)))
928 return false;
929 return true;
930}
931
Chris Lattner81fabb02002-08-26 17:53:56 +0000932// getAsString - If the sub-element type of this array is either sbyte or ubyte,
933// then this method converts the array to an std::string and returns it.
934// Otherwise, it asserts out.
935//
936std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000937 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000938 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000939 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
940 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000941 return Result;
942}
943
944
Chris Lattner3462ae32001-12-03 22:26:30 +0000945//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000946//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000947
Chris Lattner189d19f2003-11-21 20:23:48 +0000948namespace llvm {
949 template<>
950 struct ConvertConstantType<ConstantStruct, StructType> {
951 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
952 // Make everyone now use a constant of the new type...
953 std::vector<Constant*> C;
954 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
955 C.push_back(cast<Constant>(OldC->getOperand(i)));
956 Constant *New = ConstantStruct::get(NewTy, C);
957 assert(New != OldC && "Didn't replace constant??");
958
959 OldC->uncheckedReplaceAllUsesWith(New);
960 OldC->destroyConstant(); // This constant is now dead, destroy it.
961 }
962 };
963}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000964
Chris Lattner98fa07b2003-05-23 20:03:32 +0000965static ValueMap<std::vector<Constant*>, StructType,
966 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000967
Chris Lattner3e650af2004-08-04 04:48:01 +0000968static std::vector<Constant*> getValType(ConstantStruct *CS) {
969 std::vector<Constant*> Elements;
970 Elements.reserve(CS->getNumOperands());
971 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
972 Elements.push_back(cast<Constant>(CS->getOperand(i)));
973 return Elements;
974}
975
Chris Lattner015e8212004-02-15 04:14:47 +0000976Constant *ConstantStruct::get(const StructType *Ty,
977 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000978 // Create a ConstantAggregateZero value if all elements are zeros...
979 for (unsigned i = 0, e = V.size(); i != e; ++i)
980 if (!V[i]->isNullValue())
981 return StructConstants.getOrCreate(Ty, V);
982
983 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000984}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000985
Chris Lattnerd6108ca2004-07-12 20:35:11 +0000986Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
987 std::vector<const Type*> StructEls;
988 StructEls.reserve(V.size());
989 for (unsigned i = 0, e = V.size(); i != e; ++i)
990 StructEls.push_back(V[i]->getType());
991 return get(StructType::get(StructEls), V);
992}
993
Chris Lattnerd7a73302001-10-13 06:57:33 +0000994// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000995//
Chris Lattner3462ae32001-12-03 22:26:30 +0000996void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000997 StructConstants.remove(this);
998 destroyConstantImpl();
999}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001000
Brian Gaeke02209042004-08-20 06:00:58 +00001001//---- ConstantPacked::get() implementation...
1002//
1003namespace llvm {
1004 template<>
1005 struct ConvertConstantType<ConstantPacked, PackedType> {
1006 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1007 // Make everyone now use a constant of the new type...
1008 std::vector<Constant*> C;
1009 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1010 C.push_back(cast<Constant>(OldC->getOperand(i)));
1011 Constant *New = ConstantPacked::get(NewTy, C);
1012 assert(New != OldC && "Didn't replace constant??");
1013 OldC->uncheckedReplaceAllUsesWith(New);
1014 OldC->destroyConstant(); // This constant is now dead, destroy it.
1015 }
1016 };
1017}
1018
1019static std::vector<Constant*> getValType(ConstantPacked *CP) {
1020 std::vector<Constant*> Elements;
1021 Elements.reserve(CP->getNumOperands());
1022 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1023 Elements.push_back(CP->getOperand(i));
1024 return Elements;
1025}
1026
1027static ValueMap<std::vector<Constant*>, PackedType,
1028 ConstantPacked> PackedConstants;
1029
1030Constant *ConstantPacked::get(const PackedType *Ty,
1031 const std::vector<Constant*> &V) {
1032 // If this is an all-zero packed, return a ConstantAggregateZero object
1033 if (!V.empty()) {
1034 Constant *C = V[0];
1035 if (!C->isNullValue())
1036 return PackedConstants.getOrCreate(Ty, V);
1037 for (unsigned i = 1, e = V.size(); i != e; ++i)
1038 if (V[i] != C)
1039 return PackedConstants.getOrCreate(Ty, V);
1040 }
1041 return ConstantAggregateZero::get(Ty);
1042}
1043
1044Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1045 assert(!V.empty() && "Cannot infer type if V is empty");
1046 return get(PackedType::get(V.front()->getType(),V.size()), V);
1047}
1048
1049// destroyConstant - Remove the constant from the constant table...
1050//
1051void ConstantPacked::destroyConstant() {
1052 PackedConstants.remove(this);
1053 destroyConstantImpl();
1054}
1055
Chris Lattner3462ae32001-12-03 22:26:30 +00001056//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001057//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001058
Chris Lattner189d19f2003-11-21 20:23:48 +00001059namespace llvm {
1060 // ConstantPointerNull does not take extra "value" argument...
1061 template<class ValType>
1062 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1063 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1064 return new ConstantPointerNull(Ty);
1065 }
1066 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001067
Chris Lattner189d19f2003-11-21 20:23:48 +00001068 template<>
1069 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1070 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1071 // Make everyone now use a constant of the new type...
1072 Constant *New = ConstantPointerNull::get(NewTy);
1073 assert(New != OldC && "Didn't replace constant??");
1074 OldC->uncheckedReplaceAllUsesWith(New);
1075 OldC->destroyConstant(); // This constant is now dead, destroy it.
1076 }
1077 };
1078}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001079
Chris Lattner98fa07b2003-05-23 20:03:32 +00001080static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001081
Chris Lattner3e650af2004-08-04 04:48:01 +00001082static char getValType(ConstantPointerNull *) {
1083 return 0;
1084}
1085
1086
Chris Lattner3462ae32001-12-03 22:26:30 +00001087ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001088 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001089}
1090
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001091// destroyConstant - Remove the constant from the constant table...
1092//
1093void ConstantPointerNull::destroyConstant() {
1094 NullPtrConstants.remove(this);
1095 destroyConstantImpl();
1096}
1097
1098
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001099//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001100//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001101typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001102
Chris Lattner189d19f2003-11-21 20:23:48 +00001103namespace llvm {
1104 template<>
1105 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1106 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1107 if (V.first == Instruction::Cast)
1108 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
1109 if ((V.first >= Instruction::BinaryOpsBegin &&
1110 V.first < Instruction::BinaryOpsEnd) ||
1111 V.first == Instruction::Shl || V.first == Instruction::Shr)
1112 return new ConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001113 if (V.first == Instruction::Select)
1114 return new ConstantExpr(V.second[0], V.second[1], V.second[2]);
Chris Lattner189d19f2003-11-21 20:23:48 +00001115
1116 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
1117
1118 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
1119 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001120 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001121 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001122
Chris Lattner189d19f2003-11-21 20:23:48 +00001123 template<>
1124 struct ConvertConstantType<ConstantExpr, Type> {
1125 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1126 Constant *New;
1127 switch (OldC->getOpcode()) {
1128 case Instruction::Cast:
1129 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1130 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001131 case Instruction::Select:
1132 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1133 OldC->getOperand(1),
1134 OldC->getOperand(2));
1135 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001136 case Instruction::Shl:
1137 case Instruction::Shr:
1138 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1139 OldC->getOperand(0), OldC->getOperand(1));
1140 break;
1141 default:
1142 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1143 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1144 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1145 OldC->getOperand(1));
1146 break;
1147 case Instruction::GetElementPtr:
1148 // Make everyone now use a constant of the new type...
1149 std::vector<Constant*> C;
1150 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
1151 C.push_back(cast<Constant>(OldC->getOperand(i)));
1152 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
1153 break;
1154 }
1155
1156 assert(New != OldC && "Didn't replace constant??");
1157 OldC->uncheckedReplaceAllUsesWith(New);
1158 OldC->destroyConstant(); // This constant is now dead, destroy it.
1159 }
1160 };
1161} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001162
1163
Chris Lattner3e650af2004-08-04 04:48:01 +00001164static ExprMapKeyType getValType(ConstantExpr *CE) {
1165 std::vector<Constant*> Operands;
1166 Operands.reserve(CE->getNumOperands());
1167 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1168 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1169 return ExprMapKeyType(CE->getOpcode(), Operands);
1170}
1171
Chris Lattner98fa07b2003-05-23 20:03:32 +00001172static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001173
Chris Lattner46b3d302003-04-16 22:40:51 +00001174Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001175 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1176
Chris Lattneracdbe712003-04-17 19:24:48 +00001177 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1178 return FC; // Fold a few common cases...
1179
Vikram S. Adve4c485332002-07-15 18:19:33 +00001180 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001181 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001182 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1183 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001184}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001185
Chris Lattnerdd284742004-04-04 23:20:30 +00001186Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
1187 assert(C->getType()->isInteger() && Ty->isInteger() &&
1188 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1189 "This is an illegal sign extension!");
1190 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1191 return ConstantExpr::getCast(C, Ty);
1192}
1193
1194Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
1195 assert(C->getType()->isInteger() && Ty->isInteger() &&
1196 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1197 "This is an illegal zero extension!");
1198 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
1199 return ConstantExpr::getCast(C, Ty);
1200}
1201
Chris Lattnerb50d1352003-10-05 00:17:43 +00001202Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1203 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001204 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1205 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001206 // Check the operands for consistency first
1207 assert((Opcode >= Instruction::BinaryOpsBegin &&
1208 Opcode < Instruction::BinaryOpsEnd) &&
1209 "Invalid opcode in binary constant expression");
1210 assert(C1->getType() == C2->getType() &&
1211 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001212
Chris Lattner29ca2c62004-08-04 18:50:09 +00001213 if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) &&
1214 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001215 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1216 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001217
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001218 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001219 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001220 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001221}
1222
Chris Lattner29ca2c62004-08-04 18:50:09 +00001223Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001224#ifndef NDEBUG
1225 switch (Opcode) {
1226 case Instruction::Add: case Instruction::Sub:
1227 case Instruction::Mul: case Instruction::Div:
1228 case Instruction::Rem:
1229 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1230 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint()) &&
1231 "Tried to create an arithmetic operation on a non-arithmetic type!");
1232 break;
1233 case Instruction::And:
1234 case Instruction::Or:
1235 case Instruction::Xor:
1236 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1237 assert(C1->getType()->isIntegral() &&
1238 "Tried to create an logical operation on a non-integral type!");
1239 break;
1240 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1241 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1242 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1243 break;
1244 case Instruction::Shl:
1245 case Instruction::Shr:
1246 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
1247 assert(C1->getType()->isInteger() &&
1248 "Tried to create a shift operation on a non-integer type!");
1249 break;
1250 default:
1251 break;
1252 }
1253#endif
1254
Chris Lattner29ca2c62004-08-04 18:50:09 +00001255 if (Instruction::isRelational(Opcode))
1256 return getTy(Type::BoolTy, Opcode, C1, C2);
1257 else
1258 return getTy(C1->getType(), Opcode, C1, C2);
1259}
1260
Chris Lattner6e415c02004-03-12 05:54:04 +00001261Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1262 Constant *V1, Constant *V2) {
1263 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1264 assert(V1->getType() == V2->getType() && "Select value types must match!");
1265 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1266
1267 if (ReqTy == V1->getType())
1268 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1269 return SC; // Fold common cases
1270
1271 std::vector<Constant*> argVec(3, C);
1272 argVec[1] = V1;
1273 argVec[2] = V2;
1274 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1275 return ExprConstants.getOrCreate(ReqTy, Key);
1276}
1277
Chris Lattner9eb2b522004-01-12 19:12:58 +00001278/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001279Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1280 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001281 // Check the operands for consistency first
1282 assert((Opcode == Instruction::Shl ||
1283 Opcode == Instruction::Shr) &&
1284 "Invalid opcode in binary constant expression");
1285 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1286 "Invalid operand types for Shift constant expr!");
1287
Chris Lattner0bba7712004-01-12 20:40:42 +00001288 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001289 return FC; // Fold a few common cases...
1290
1291 // Look up the constant in the table first to ensure uniqueness
1292 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001293 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001294 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001295}
1296
1297
Chris Lattnerb50d1352003-10-05 00:17:43 +00001298Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1299 const std::vector<Constant*> &IdxList) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001300 assert(GetElementPtrInst::getIndexedType(C->getType(),
1301 std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
1302 "GEP indices invalid!");
1303
Chris Lattneracdbe712003-04-17 19:24:48 +00001304 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1305 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001306
Chris Lattnerb50d1352003-10-05 00:17:43 +00001307 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001308 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001309 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001310 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +00001311 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001312 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001313 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001314}
1315
Chris Lattnerb50d1352003-10-05 00:17:43 +00001316Constant *ConstantExpr::getGetElementPtr(Constant *C,
1317 const std::vector<Constant*> &IdxList){
1318 // Get the result type of the getelementptr!
1319 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1320
1321 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1322 true);
1323 assert(Ty && "GEP indices invalid!");
1324 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1325}
1326
1327
Vikram S. Adve4c485332002-07-15 18:19:33 +00001328// destroyConstant - Remove the constant from the constant table...
1329//
1330void ConstantExpr::destroyConstant() {
1331 ExprConstants.remove(this);
1332 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001333}
1334
Chris Lattner3cd8c562002-07-30 18:54:25 +00001335const char *ConstantExpr::getOpcodeName() const {
1336 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001337}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001338