blob: d187125233c50843ada17db15bb64b54f2765b7d [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Jim Laskeyb74c6662005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000023#include "llvm/Support/Compiler.h"
Chris Lattner69edc982006-09-28 00:35:06 +000024#include "llvm/Support/ManagedStatic.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000025#include <algorithm>
Reid Spencercf394bf2004-07-04 11:51:24 +000026#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner2f7c9632001-06-06 20:29:01 +000029//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000030// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000031//===----------------------------------------------------------------------===//
32
Chris Lattner3462ae32001-12-03 22:26:30 +000033void Constant::destroyConstantImpl() {
34 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000035 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000036 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000037 // but they don't know that. Because we only find out when the CPV is
38 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000039 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000040 //
41 while (!use_empty()) {
42 Value *V = use_back();
43#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000044 if (!isa<Constant>(V))
45 std::cerr << "While deleting: " << *this
46 << "\n\nUse still stuck around after Def is destroyed: "
47 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000048#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000049 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000050 Constant *CV = cast<Constant>(V);
51 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000052
53 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000054 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000055 }
56
57 // Value has no outstanding references it is safe to delete it now...
58 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000059}
Chris Lattner2f7c9632001-06-06 20:29:01 +000060
Chris Lattnerb1585a92002-08-13 17:50:20 +000061// Static constructor to create a '0' constant of arbitrary type...
62Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000063 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000064 case Type::BoolTyID: {
65 static Constant *NullBool = ConstantBool::get(false);
66 return NullBool;
67 }
68 case Type::SByteTyID: {
69 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
70 return NullSByte;
71 }
72 case Type::UByteTyID: {
73 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
74 return NullUByte;
75 }
76 case Type::ShortTyID: {
77 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
78 return NullShort;
79 }
80 case Type::UShortTyID: {
81 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
82 return NullUShort;
83 }
84 case Type::IntTyID: {
85 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
86 return NullInt;
87 }
88 case Type::UIntTyID: {
89 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
90 return NullUInt;
91 }
92 case Type::LongTyID: {
93 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
94 return NullLong;
95 }
96 case Type::ULongTyID: {
97 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
98 return NullULong;
99 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000100
Chris Lattner3e88ef92003-10-03 19:34:51 +0000101 case Type::FloatTyID: {
102 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
103 return NullFloat;
104 }
105 case Type::DoubleTyID: {
106 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
107 return NullDouble;
108 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000109
Misha Brukmanb1c93172005-04-21 23:48:37 +0000110 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000111 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000112
Chris Lattner9fba3da2004-02-15 05:53:04 +0000113 case Type::StructTyID:
114 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000115 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000116 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000117 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000118 // Function, Label, or Opaque type?
119 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000120 return 0;
121 }
122}
123
124// Static constructor to create the maximum constant of an integral type...
125ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000126 switch (Ty->getTypeID()) {
Chris Lattnera84df0a22006-09-28 23:36:21 +0000127 case Type::BoolTyID: return ConstantBool::getTrue();
Chris Lattnerb1585a92002-08-13 17:50:20 +0000128 case Type::SByteTyID:
129 case Type::ShortTyID:
130 case Type::IntTyID:
131 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000132 // Calculate 011111111111111...
Chris Lattnerb1585a92002-08-13 17:50:20 +0000133 unsigned TypeBits = Ty->getPrimitiveSize()*8;
134 int64_t Val = INT64_MAX; // All ones
135 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
136 return ConstantSInt::get(Ty, Val);
137 }
138
139 case Type::UByteTyID:
140 case Type::UShortTyID:
141 case Type::UIntTyID:
142 case Type::ULongTyID: return getAllOnesValue(Ty);
143
Chris Lattner31408f72002-08-14 17:12:13 +0000144 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000145 }
146}
147
148// Static constructor to create the minimum constant for an integral type...
149ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000150 switch (Ty->getTypeID()) {
Chris Lattnera84df0a22006-09-28 23:36:21 +0000151 case Type::BoolTyID: return ConstantBool::getFalse();
Chris Lattnerb1585a92002-08-13 17:50:20 +0000152 case Type::SByteTyID:
153 case Type::ShortTyID:
154 case Type::IntTyID:
155 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000156 // Calculate 1111111111000000000000
Chris Lattnerb1585a92002-08-13 17:50:20 +0000157 unsigned TypeBits = Ty->getPrimitiveSize()*8;
158 int64_t Val = -1; // All ones
159 Val <<= TypeBits-1; // Shift over to the right spot
160 return ConstantSInt::get(Ty, Val);
161 }
162
163 case Type::UByteTyID:
164 case Type::UShortTyID:
165 case Type::UIntTyID:
166 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
167
Chris Lattner31408f72002-08-14 17:12:13 +0000168 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000169 }
170}
171
172// Static constructor to create an integral constant with all bits set
173ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000174 switch (Ty->getTypeID()) {
Chris Lattnera84df0a22006-09-28 23:36:21 +0000175 case Type::BoolTyID: return ConstantBool::getTrue();
Chris Lattnerb1585a92002-08-13 17:50:20 +0000176 case Type::SByteTyID:
177 case Type::ShortTyID:
178 case Type::IntTyID:
179 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
180
181 case Type::UByteTyID:
182 case Type::UShortTyID:
183 case Type::UIntTyID:
184 case Type::ULongTyID: {
185 // Calculate ~0 of the right type...
186 unsigned TypeBits = Ty->getPrimitiveSize()*8;
187 uint64_t Val = ~0ULL; // All ones
188 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
189 return ConstantUInt::get(Ty, Val);
190 }
Chris Lattner31408f72002-08-14 17:12:13 +0000191 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000192 }
193}
194
Chris Lattner83e5d392003-03-10 22:39:02 +0000195bool ConstantUInt::isAllOnesValue() const {
196 unsigned TypeBits = getType()->getPrimitiveSize()*8;
197 uint64_t Val = ~0ULL; // All ones
198 Val >>= 64-TypeBits; // Shift out inappropriate bits
199 return getValue() == Val;
200}
201
Chris Lattnerb1585a92002-08-13 17:50:20 +0000202
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000204// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000205//===----------------------------------------------------------------------===//
206
207//===----------------------------------------------------------------------===//
208// Normal Constructors
209
Chris Lattnere7e139e2005-09-27 06:09:08 +0000210ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V)
211 : Constant(Ty, VT, 0, 0) {
Chris Lattner265eb642004-06-21 12:12:12 +0000212 Val.Unsigned = V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213}
Chris Lattner49d855c2001-09-07 16:46:31 +0000214
Chris Lattnere7e139e2005-09-27 06:09:08 +0000215ConstantBool::ConstantBool(bool V)
216 : ConstantIntegral(Type::BoolTy, ConstantBoolVal, V) {
Chris Lattner265eb642004-06-21 12:12:12 +0000217}
218
Chris Lattnere7e139e2005-09-27 06:09:08 +0000219ConstantInt::ConstantInt(const Type *Ty, ValueTy VT, uint64_t V)
220 : ConstantIntegral(Ty, VT, V) {
Chris Lattner7309d662001-07-21 19:16:08 +0000221}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222
Chris Lattnere7e139e2005-09-27 06:09:08 +0000223ConstantSInt::ConstantSInt(const Type *Ty, int64_t V)
224 : ConstantInt(Ty, ConstantSIntVal, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000225 assert(Ty->isInteger() && Ty->isSigned() &&
Reid Spencerf064bb22005-03-09 15:19:41 +0000226 "Illegal type for signed integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000227 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228}
229
Chris Lattnere7e139e2005-09-27 06:09:08 +0000230ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V)
231 : ConstantInt(Ty, ConstantUIntVal, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000232 assert(Ty->isInteger() && Ty->isUnsigned() &&
233 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000234 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235}
236
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000237ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000238 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000239 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000240 Val = V;
241}
242
Chris Lattner3462ae32001-12-03 22:26:30 +0000243ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000244 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000245 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000246 assert(V.size() == T->getNumElements() &&
247 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000248 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000249 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
250 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000251 Constant *C = *I;
252 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000253 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000254 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000255 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000256 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000257 }
258}
259
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000260ConstantArray::~ConstantArray() {
261 delete [] OperandList;
262}
263
Chris Lattner3462ae32001-12-03 22:26:30 +0000264ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000265 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000266 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000267 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000268 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000269 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000270 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
271 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000272 Constant *C = *I;
273 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000274 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000275 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000276 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000277 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000278 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000279 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000280 }
281}
282
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000283ConstantStruct::~ConstantStruct() {
284 delete [] OperandList;
285}
286
287
Brian Gaeke02209042004-08-20 06:00:58 +0000288ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000289 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000290 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000291 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000292 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
293 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000294 Constant *C = *I;
295 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000296 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000297 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000298 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000299 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000300 }
301}
302
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000303ConstantPacked::~ConstantPacked() {
304 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000305}
306
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000307/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
308/// behind the scenes to implement unary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000309namespace {
310class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000311 Use Op;
312public:
313 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
314 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
315};
Chris Lattner02157b02006-06-28 21:38:54 +0000316}
Chris Lattner6e415c02004-03-12 05:54:04 +0000317
Chris Lattner22ced562003-06-22 20:48:30 +0000318static bool isSetCC(unsigned Opcode) {
319 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
320 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
321 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
322}
323
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000324/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
325/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000326namespace {
327class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000328 Use Ops[2];
329public:
330 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
331 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
332 Opcode, Ops, 2) {
333 Ops[0].init(C1, this);
334 Ops[1].init(C2, this);
335 }
336};
Chris Lattner02157b02006-06-28 21:38:54 +0000337}
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000338
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000339/// SelectConstantExpr - This class is private to Constants.cpp, and is used
340/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000341namespace {
342class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000343 Use Ops[3];
344public:
345 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
346 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
347 Ops[0].init(C1, this);
348 Ops[1].init(C2, this);
349 Ops[2].init(C3, this);
350 }
351};
Chris Lattner02157b02006-06-28 21:38:54 +0000352}
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000353
Robert Bocchinoca27f032006-01-17 20:07:22 +0000354/// ExtractElementConstantExpr - This class is private to
355/// Constants.cpp, and is used behind the scenes to implement
356/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000357namespace {
358class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000359 Use Ops[2];
360public:
361 ExtractElementConstantExpr(Constant *C1, Constant *C2)
362 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(),
363 Instruction::ExtractElement, Ops, 2) {
364 Ops[0].init(C1, this);
365 Ops[1].init(C2, this);
366 }
367};
Chris Lattner02157b02006-06-28 21:38:54 +0000368}
Robert Bocchino23004482006-01-10 19:05:34 +0000369
Robert Bocchinoca27f032006-01-17 20:07:22 +0000370/// InsertElementConstantExpr - This class is private to
371/// Constants.cpp, and is used behind the scenes to implement
372/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000373namespace {
374class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000375 Use Ops[3];
376public:
377 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
378 : ConstantExpr(C1->getType(), Instruction::InsertElement,
379 Ops, 3) {
380 Ops[0].init(C1, this);
381 Ops[1].init(C2, this);
382 Ops[2].init(C3, this);
383 }
384};
Chris Lattner02157b02006-06-28 21:38:54 +0000385}
Robert Bocchinoca27f032006-01-17 20:07:22 +0000386
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000387/// ShuffleVectorConstantExpr - This class is private to
388/// Constants.cpp, and is used behind the scenes to implement
389/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000390namespace {
391class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000392 Use Ops[3];
393public:
394 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
395 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
396 Ops, 3) {
397 Ops[0].init(C1, this);
398 Ops[1].init(C2, this);
399 Ops[2].init(C3, this);
400 }
401};
Chris Lattner02157b02006-06-28 21:38:54 +0000402}
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000403
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000404/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
405/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000406namespace {
407struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000408 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
409 const Type *DestTy)
410 : ConstantExpr(DestTy, Instruction::GetElementPtr,
411 new Use[IdxList.size()+1], IdxList.size()+1) {
412 OperandList[0].init(C, this);
413 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
414 OperandList[i+1].init(IdxList[i], this);
415 }
416 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000417 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000418 }
419};
Chris Lattner02157b02006-06-28 21:38:54 +0000420}
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000421
Chris Lattner817175f2004-03-29 02:37:53 +0000422/// ConstantExpr::get* - Return some common constants without having to
423/// specify the full Instruction::OPCODE identifier.
424///
425Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000426 if (!C->getType()->isFloatingPoint())
427 return get(Instruction::Sub, getNullValue(C->getType()), C);
428 else
429 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000430}
431Constant *ConstantExpr::getNot(Constant *C) {
432 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
433 return get(Instruction::Xor, C,
434 ConstantIntegral::getAllOnesValue(C->getType()));
435}
436Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
437 return get(Instruction::Add, C1, C2);
438}
439Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
440 return get(Instruction::Sub, C1, C2);
441}
442Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
443 return get(Instruction::Mul, C1, C2);
444}
445Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
446 return get(Instruction::Div, C1, C2);
447}
448Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
449 return get(Instruction::Rem, C1, C2);
450}
451Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
452 return get(Instruction::And, C1, C2);
453}
454Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
455 return get(Instruction::Or, C1, C2);
456}
457Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
458 return get(Instruction::Xor, C1, C2);
459}
460Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
461 return get(Instruction::SetEQ, C1, C2);
462}
463Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
464 return get(Instruction::SetNE, C1, C2);
465}
466Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
467 return get(Instruction::SetLT, C1, C2);
468}
469Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
470 return get(Instruction::SetGT, C1, C2);
471}
472Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
473 return get(Instruction::SetLE, C1, C2);
474}
475Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
476 return get(Instruction::SetGE, C1, C2);
477}
478Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
479 return get(Instruction::Shl, C1, C2);
480}
481Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
482 return get(Instruction::Shr, C1, C2);
483}
484
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000485Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
486 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
487 return getCast(getShr(getCast(C1,
488 C1->getType()->getUnsignedVersion()), C2), C1->getType());
489}
Chris Lattner817175f2004-03-29 02:37:53 +0000490
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000491Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
492 if (C1->getType()->isSigned()) return getShr(C1, C2);
493 return getCast(getShr(getCast(C1,
494 C1->getType()->getSignedVersion()), C2), C1->getType());
495}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000496
Chris Lattner7c1018a2006-07-14 19:37:40 +0000497/// getWithOperandReplaced - Return a constant expression identical to this
498/// one, but with the specified operand set to the specified value.
499Constant *ConstantExpr::getWithOperandReplaced(unsigned OpNo,
500 Constant *Op) const {
501 assert(OpNo < getNumOperands() && "Operand num is out of range!");
502 assert(Op->getType() == getOperand(OpNo)->getType() &&
503 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000504 if (getOperand(OpNo) == Op)
505 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000506
Chris Lattner227816342006-07-14 22:20:01 +0000507 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000508 switch (getOpcode()) {
509 case Instruction::Cast:
510 return ConstantExpr::getCast(Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000511 case Instruction::Select:
512 Op0 = (OpNo == 0) ? Op : getOperand(0);
513 Op1 = (OpNo == 1) ? Op : getOperand(1);
514 Op2 = (OpNo == 2) ? Op : getOperand(2);
515 return ConstantExpr::getSelect(Op0, Op1, Op2);
516 case Instruction::InsertElement:
517 Op0 = (OpNo == 0) ? Op : getOperand(0);
518 Op1 = (OpNo == 1) ? Op : getOperand(1);
519 Op2 = (OpNo == 2) ? Op : getOperand(2);
520 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
521 case Instruction::ExtractElement:
522 Op0 = (OpNo == 0) ? Op : getOperand(0);
523 Op1 = (OpNo == 1) ? Op : getOperand(1);
524 return ConstantExpr::getExtractElement(Op0, Op1);
525 case Instruction::ShuffleVector:
526 Op0 = (OpNo == 0) ? Op : getOperand(0);
527 Op1 = (OpNo == 1) ? Op : getOperand(1);
528 Op2 = (OpNo == 2) ? Op : getOperand(2);
529 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000530 case Instruction::GetElementPtr: {
531 std::vector<Constant*> Ops;
532 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
533 Ops.push_back(getOperand(i));
534 if (OpNo == 0)
535 return ConstantExpr::getGetElementPtr(Op, Ops);
536 Ops[OpNo-1] = Op;
537 return ConstantExpr::getGetElementPtr(getOperand(0), Ops);
538 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000539 default:
540 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000541 Op0 = (OpNo == 0) ? Op : getOperand(0);
542 Op1 = (OpNo == 1) ? Op : getOperand(1);
543 return ConstantExpr::get(getOpcode(), Op0, Op1);
544 }
545}
546
547/// getWithOperands - This returns the current constant expression with the
548/// operands replaced with the specified values. The specified operands must
549/// match count and type with the existing ones.
550Constant *ConstantExpr::
551getWithOperands(const std::vector<Constant*> &Ops) const {
552 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
553 bool AnyChange = false;
554 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
555 assert(Ops[i]->getType() == getOperand(i)->getType() &&
556 "Operand type mismatch!");
557 AnyChange |= Ops[i] != getOperand(i);
558 }
559 if (!AnyChange) // No operands changed, return self.
560 return const_cast<ConstantExpr*>(this);
561
562 switch (getOpcode()) {
563 case Instruction::Cast:
564 return ConstantExpr::getCast(Ops[0], getType());
565 case Instruction::Select:
566 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
567 case Instruction::InsertElement:
568 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
569 case Instruction::ExtractElement:
570 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
571 case Instruction::ShuffleVector:
572 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
573 case Instruction::GetElementPtr: {
574 std::vector<Constant*> ActualOps(Ops.begin()+1, Ops.end());
575 return ConstantExpr::getGetElementPtr(Ops[0], ActualOps);
576 }
577 default:
578 assert(getNumOperands() == 2 && "Must be binary operator?");
579 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000580 }
581}
582
Chris Lattner2f7c9632001-06-06 20:29:01 +0000583
584//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000585// isValueValidForType implementations
586
Chris Lattner3462ae32001-12-03 22:26:30 +0000587bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000588 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000589 default:
590 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000591 // Signed types...
592 case Type::SByteTyID:
593 return (Val <= INT8_MAX && Val >= INT8_MIN);
594 case Type::ShortTyID:
595 return (Val <= INT16_MAX && Val >= INT16_MIN);
596 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000597 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000598 case Type::LongTyID:
599 return true; // This is the largest type...
600 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000601}
602
Chris Lattner3462ae32001-12-03 22:26:30 +0000603bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000604 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000605 default:
606 return false; // These can't be represented as integers!!!
607
608 // Unsigned types...
609 case Type::UByteTyID:
610 return (Val <= UINT8_MAX);
611 case Type::UShortTyID:
612 return (Val <= UINT16_MAX);
613 case Type::UIntTyID:
614 return (Val <= UINT32_MAX);
615 case Type::ULongTyID:
616 return true; // This is the largest type...
617 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000618}
619
Chris Lattner3462ae32001-12-03 22:26:30 +0000620bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000621 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000622 default:
623 return false; // These can't be represented as floating point!
624
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000625 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000626 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000627 case Type::DoubleTyID:
628 return true; // This is the largest type...
629 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000630}
Chris Lattner9655e542001-07-20 19:16:02 +0000631
Chris Lattner49d855c2001-09-07 16:46:31 +0000632//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000633// Factory Function Implementation
634
Chris Lattner98fa07b2003-05-23 20:03:32 +0000635// ConstantCreator - A class that is used to create constants by
636// ValueMap*. This class should be partially specialized if there is
637// something strange that needs to be done to interface to the ctor for the
638// constant.
639//
Chris Lattner189d19f2003-11-21 20:23:48 +0000640namespace llvm {
641 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000642 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000643 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
644 return new ConstantClass(Ty, V);
645 }
646 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000647
Chris Lattner189d19f2003-11-21 20:23:48 +0000648 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000649 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000650 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
651 assert(0 && "This type cannot be converted!\n");
652 abort();
653 }
654 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000655
Chris Lattner935aa922005-10-04 17:48:46 +0000656 template<class ValType, class TypeClass, class ConstantClass,
657 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000658 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000659 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000660 typedef std::pair<const Type*, ValType> MapKey;
661 typedef std::map<MapKey, Constant *> MapTy;
662 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
663 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000664 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000665 /// Map - This is the main map from the element descriptor to the Constants.
666 /// This is the primary way we avoid creating two of the same shape
667 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000668 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000669
670 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
671 /// from the constants to their element in Map. This is important for
672 /// removal of constants from the array, which would otherwise have to scan
673 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000674 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000675
Jim Laskeyc03caef2006-07-17 17:38:29 +0000676 /// AbstractTypeMap - Map for abstract type constants.
677 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000678 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000679
Chris Lattner99a669b2004-11-19 16:39:44 +0000680 private:
681 void clear(std::vector<Constant *> &Constants) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000682 for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I)
Chris Lattner99a669b2004-11-19 16:39:44 +0000683 Constants.push_back(I->second);
684 Map.clear();
685 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000686 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000687 }
688
Chris Lattner98fa07b2003-05-23 20:03:32 +0000689 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000690 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000691
692 /// InsertOrGetItem - Return an iterator for the specified element.
693 /// If the element exists in the map, the returned iterator points to the
694 /// entry and Exists=true. If not, the iterator points to the newly
695 /// inserted entry and returns Exists=false. Newly inserted entries have
696 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000697 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
698 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000699 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000700 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000701 Exists = !IP.second;
702 return IP.first;
703 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000704
Chris Lattner935aa922005-10-04 17:48:46 +0000705private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000706 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000707 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000708 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000709 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
710 IMI->second->second == CP &&
711 "InverseMap corrupt!");
712 return IMI->second;
713 }
714
Jim Laskeyc03caef2006-07-17 17:38:29 +0000715 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +0000716 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000717 if (I == Map.end() || I->second != CP) {
718 // FIXME: This should not use a linear scan. If this gets to be a
719 // performance problem, someone should look at this.
720 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
721 /* empty */;
722 }
Chris Lattner935aa922005-10-04 17:48:46 +0000723 return I;
724 }
725public:
726
Chris Lattnerb64419a2005-10-03 22:51:37 +0000727 /// getOrCreate - Return the specified constant from the map, creating it if
728 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000729 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000730 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000731 typename MapTy::iterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000732 if (I != Map.end() && I->first == Lookup)
Jim Laskeyc03caef2006-07-17 17:38:29 +0000733 return static_cast<ConstantClass *>(I->second); // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +0000734
735 // If no preexisting value, create one now...
736 ConstantClass *Result =
737 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
738
Chris Lattnerb50d1352003-10-05 00:17:43 +0000739 /// FIXME: why does this assert fail when loading 176.gcc?
740 //assert(Result->getType() == Ty && "Type specified is not correct!");
741 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
742
Chris Lattner935aa922005-10-04 17:48:46 +0000743 if (HasLargeKey) // Remember the reverse mapping if needed.
744 InverseMap.insert(std::make_pair(Result, I));
745
Chris Lattnerb50d1352003-10-05 00:17:43 +0000746 // If the type of the constant is abstract, make sure that an entry exists
747 // for it in the AbstractTypeMap.
748 if (Ty->isAbstract()) {
749 typename AbstractTypeMapTy::iterator TI =
750 AbstractTypeMap.lower_bound(Ty);
751
752 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
753 // Add ourselves to the ATU list of the type.
754 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
755
756 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
757 }
758 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000759 return Result;
760 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000761
Chris Lattner98fa07b2003-05-23 20:03:32 +0000762 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000763 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000764 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000765 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000766
Chris Lattner935aa922005-10-04 17:48:46 +0000767 if (HasLargeKey) // Remember the reverse mapping if needed.
768 InverseMap.erase(CP);
769
Chris Lattnerb50d1352003-10-05 00:17:43 +0000770 // Now that we found the entry, make sure this isn't the entry that
771 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000772 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000773 if (Ty->isAbstract()) {
774 assert(AbstractTypeMap.count(Ty) &&
775 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000776 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000777 if (ATMEntryIt == I) {
778 // Yes, we are removing the representative entry for this type.
779 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000780 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000781
Chris Lattnerb50d1352003-10-05 00:17:43 +0000782 // First check the entry before this one...
783 if (TmpIt != Map.begin()) {
784 --TmpIt;
785 if (TmpIt->first.first != Ty) // Not the same type, move back...
786 ++TmpIt;
787 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000788
Chris Lattnerb50d1352003-10-05 00:17:43 +0000789 // If we didn't find the same type, try to move forward...
790 if (TmpIt == ATMEntryIt) {
791 ++TmpIt;
792 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
793 --TmpIt; // No entry afterwards with the same type
794 }
795
796 // If there is another entry in the map of the same abstract type,
797 // update the AbstractTypeMap entry now.
798 if (TmpIt != ATMEntryIt) {
799 ATMEntryIt = TmpIt;
800 } else {
801 // Otherwise, we are removing the last instance of this type
802 // from the table. Remove from the ATM, and from user list.
803 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
804 AbstractTypeMap.erase(Ty);
805 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000806 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000807 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000808
Chris Lattnerb50d1352003-10-05 00:17:43 +0000809 Map.erase(I);
810 }
811
Chris Lattner3b793c62005-10-04 21:35:50 +0000812
813 /// MoveConstantToNewSlot - If we are about to change C to be the element
814 /// specified by I, update our internal data structures to reflect this
815 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000816 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000817 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000818 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000819 assert(OldI != Map.end() && "Constant not found in constant table!");
820 assert(OldI->second == C && "Didn't find correct element?");
821
822 // If this constant is the representative element for its abstract type,
823 // update the AbstractTypeMap so that the representative element is I.
824 if (C->getType()->isAbstract()) {
825 typename AbstractTypeMapTy::iterator ATI =
826 AbstractTypeMap.find(C->getType());
827 assert(ATI != AbstractTypeMap.end() &&
828 "Abstract type not in AbstractTypeMap?");
829 if (ATI->second == OldI)
830 ATI->second = I;
831 }
832
833 // Remove the old entry from the map.
834 Map.erase(OldI);
835
836 // Update the inverse map so that we know that this constant is now
837 // located at descriptor I.
838 if (HasLargeKey) {
839 assert(I->second == C && "Bad inversemap entry!");
840 InverseMap[C] = I;
841 }
842 }
843
Chris Lattnerb50d1352003-10-05 00:17:43 +0000844 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000845 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000846 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000847
848 assert(I != AbstractTypeMap.end() &&
849 "Abstract type not in AbstractTypeMap?");
850
851 // Convert a constant at a time until the last one is gone. The last one
852 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
853 // eliminated eventually.
854 do {
855 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +0000856 TypeClass>::convert(
857 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +0000858 cast<TypeClass>(NewTy));
859
Jim Laskeyc03caef2006-07-17 17:38:29 +0000860 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000861 } while (I != AbstractTypeMap.end());
862 }
863
864 // If the type became concrete without being refined to any other existing
865 // type, we just remove ourselves from the ATU list.
866 void typeBecameConcrete(const DerivedType *AbsTy) {
867 AbsTy->removeAbstractTypeUser(this);
868 }
869
870 void dump() const {
871 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000872 }
873 };
874}
875
Chris Lattnera84df0a22006-09-28 23:36:21 +0000876
877//---- ConstantBool::get*() implementation.
878
879ConstantBool *ConstantBool::getTrue() {
880 static ConstantBool *T = 0;
881 if (T) return T;
882 return T = new ConstantBool(true);
883}
884ConstantBool *ConstantBool::getFalse() {
885 static ConstantBool *F = 0;
886 if (F) return F;
887 return F = new ConstantBool(false);
888}
889
Chris Lattner3462ae32001-12-03 22:26:30 +0000890//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000891//
Chris Lattner69edc982006-09-28 00:35:06 +0000892static ManagedStatic<ValueMap< int64_t, Type, ConstantSInt> > SIntConstants;
893static ManagedStatic<ValueMap<uint64_t, Type, ConstantUInt> > UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000894
Chris Lattner3462ae32001-12-03 22:26:30 +0000895ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner69edc982006-09-28 00:35:06 +0000896 return SIntConstants->getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000897}
898
Chris Lattner3462ae32001-12-03 22:26:30 +0000899ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner69edc982006-09-28 00:35:06 +0000900 return UIntConstants->getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000901}
902
Chris Lattner3462ae32001-12-03 22:26:30 +0000903ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000904 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000905 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
906 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000907}
908
Chris Lattner3462ae32001-12-03 22:26:30 +0000909//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000910//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000911namespace llvm {
912 template<>
913 struct ConstantCreator<ConstantFP, Type, uint64_t> {
914 static ConstantFP *create(const Type *Ty, uint64_t V) {
915 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000916 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000917 }
918 };
919 template<>
920 struct ConstantCreator<ConstantFP, Type, uint32_t> {
921 static ConstantFP *create(const Type *Ty, uint32_t V) {
922 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000923 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000924 }
925 };
926}
927
Chris Lattner69edc982006-09-28 00:35:06 +0000928static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
929static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000930
Jim Laskey8ad8f712005-08-17 20:06:22 +0000931bool ConstantFP::isNullValue() const {
932 return DoubleToBits(Val) == 0;
933}
934
935bool ConstantFP::isExactlyValue(double V) const {
936 return DoubleToBits(V) == DoubleToBits(Val);
937}
938
939
Chris Lattner3462ae32001-12-03 22:26:30 +0000940ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000941 if (Ty == Type::FloatTy) {
942 // Force the value through memory to normalize it.
Chris Lattner69edc982006-09-28 00:35:06 +0000943 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000944 } else {
945 assert(Ty == Type::DoubleTy);
Chris Lattner69edc982006-09-28 00:35:06 +0000946 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000947 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000948}
949
Chris Lattner9fba3da2004-02-15 05:53:04 +0000950//---- ConstantAggregateZero::get() implementation...
951//
952namespace llvm {
953 // ConstantAggregateZero does not take extra "value" argument...
954 template<class ValType>
955 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
956 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
957 return new ConstantAggregateZero(Ty);
958 }
959 };
960
961 template<>
962 struct ConvertConstantType<ConstantAggregateZero, Type> {
963 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
964 // Make everyone now use a constant of the new type...
965 Constant *New = ConstantAggregateZero::get(NewTy);
966 assert(New != OldC && "Didn't replace constant??");
967 OldC->uncheckedReplaceAllUsesWith(New);
968 OldC->destroyConstant(); // This constant is now dead, destroy it.
969 }
970 };
971}
972
Chris Lattner69edc982006-09-28 00:35:06 +0000973static ManagedStatic<ValueMap<char, Type,
974 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +0000975
Chris Lattner3e650af2004-08-04 04:48:01 +0000976static char getValType(ConstantAggregateZero *CPZ) { return 0; }
977
Chris Lattner9fba3da2004-02-15 05:53:04 +0000978Constant *ConstantAggregateZero::get(const Type *Ty) {
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000979 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) &&
980 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +0000981 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000982}
983
984// destroyConstant - Remove the constant from the constant table...
985//
986void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000987 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000988 destroyConstantImpl();
989}
990
Chris Lattner3462ae32001-12-03 22:26:30 +0000991//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000992//
Chris Lattner189d19f2003-11-21 20:23:48 +0000993namespace llvm {
994 template<>
995 struct ConvertConstantType<ConstantArray, ArrayType> {
996 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
997 // Make everyone now use a constant of the new type...
998 std::vector<Constant*> C;
999 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1000 C.push_back(cast<Constant>(OldC->getOperand(i)));
1001 Constant *New = ConstantArray::get(NewTy, C);
1002 assert(New != OldC && "Didn't replace constant??");
1003 OldC->uncheckedReplaceAllUsesWith(New);
1004 OldC->destroyConstant(); // This constant is now dead, destroy it.
1005 }
1006 };
1007}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001008
Chris Lattner3e650af2004-08-04 04:48:01 +00001009static std::vector<Constant*> getValType(ConstantArray *CA) {
1010 std::vector<Constant*> Elements;
1011 Elements.reserve(CA->getNumOperands());
1012 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1013 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1014 return Elements;
1015}
1016
Chris Lattnerb64419a2005-10-03 22:51:37 +00001017typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001018 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001019static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001020
Chris Lattner015e8212004-02-15 04:14:47 +00001021Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +00001022 const std::vector<Constant*> &V) {
1023 // If this is an all-zero array, return a ConstantAggregateZero object
1024 if (!V.empty()) {
1025 Constant *C = V[0];
1026 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001027 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001028 for (unsigned i = 1, e = V.size(); i != e; ++i)
1029 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001030 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001031 }
1032 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001033}
1034
Chris Lattner98fa07b2003-05-23 20:03:32 +00001035// destroyConstant - Remove the constant from the constant table...
1036//
1037void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001038 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001039 destroyConstantImpl();
1040}
1041
Reid Spencer6f614532006-05-30 08:23:18 +00001042/// ConstantArray::get(const string&) - Return an array that is initialized to
1043/// contain the specified string. If length is zero then a null terminator is
1044/// added to the specified string so that it may be used in a natural way.
1045/// Otherwise, the length parameter specifies how much of the string to use
1046/// and it won't be null terminated.
1047///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001048Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001049 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001050 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +00001051 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001052
1053 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001054 if (AddNull) {
Reid Spencer6f614532006-05-30 08:23:18 +00001055 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001056 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001057
Reid Spencer82ebaba2006-05-30 18:15:07 +00001058 ArrayType *ATy = ArrayType::get(Type::SByteTy, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001059 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001060}
1061
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001062/// isString - This method returns true if the array is an array of sbyte or
1063/// ubyte, and if the elements of the array are all ConstantInt's.
1064bool ConstantArray::isString() const {
1065 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +00001066 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001067 getType()->getElementType() != Type::SByteTy)
1068 return false;
1069 // Check the elements to make sure they are all integers, not constant
1070 // expressions.
1071 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1072 if (!isa<ConstantInt>(getOperand(i)))
1073 return false;
1074 return true;
1075}
1076
Chris Lattner81fabb02002-08-26 17:53:56 +00001077// getAsString - If the sub-element type of this array is either sbyte or ubyte,
1078// then this method converts the array to an std::string and returns it.
1079// Otherwise, it asserts out.
1080//
1081std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001082 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001083 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001084 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1085 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001086 return Result;
1087}
1088
1089
Chris Lattner3462ae32001-12-03 22:26:30 +00001090//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001091//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001092
Chris Lattner189d19f2003-11-21 20:23:48 +00001093namespace llvm {
1094 template<>
1095 struct ConvertConstantType<ConstantStruct, StructType> {
1096 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1097 // Make everyone now use a constant of the new type...
1098 std::vector<Constant*> C;
1099 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1100 C.push_back(cast<Constant>(OldC->getOperand(i)));
1101 Constant *New = ConstantStruct::get(NewTy, C);
1102 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001103
Chris Lattner189d19f2003-11-21 20:23:48 +00001104 OldC->uncheckedReplaceAllUsesWith(New);
1105 OldC->destroyConstant(); // This constant is now dead, destroy it.
1106 }
1107 };
1108}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001109
Chris Lattner8760ec72005-10-04 01:17:50 +00001110typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001111 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001112static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001113
Chris Lattner3e650af2004-08-04 04:48:01 +00001114static std::vector<Constant*> getValType(ConstantStruct *CS) {
1115 std::vector<Constant*> Elements;
1116 Elements.reserve(CS->getNumOperands());
1117 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1118 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1119 return Elements;
1120}
1121
Chris Lattner015e8212004-02-15 04:14:47 +00001122Constant *ConstantStruct::get(const StructType *Ty,
1123 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001124 // Create a ConstantAggregateZero value if all elements are zeros...
1125 for (unsigned i = 0, e = V.size(); i != e; ++i)
1126 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001127 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001128
1129 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001130}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001131
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001132Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1133 std::vector<const Type*> StructEls;
1134 StructEls.reserve(V.size());
1135 for (unsigned i = 0, e = V.size(); i != e; ++i)
1136 StructEls.push_back(V[i]->getType());
1137 return get(StructType::get(StructEls), V);
1138}
1139
Chris Lattnerd7a73302001-10-13 06:57:33 +00001140// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001141//
Chris Lattner3462ae32001-12-03 22:26:30 +00001142void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001143 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001144 destroyConstantImpl();
1145}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001146
Brian Gaeke02209042004-08-20 06:00:58 +00001147//---- ConstantPacked::get() implementation...
1148//
1149namespace llvm {
1150 template<>
1151 struct ConvertConstantType<ConstantPacked, PackedType> {
1152 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1153 // Make everyone now use a constant of the new type...
1154 std::vector<Constant*> C;
1155 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1156 C.push_back(cast<Constant>(OldC->getOperand(i)));
1157 Constant *New = ConstantPacked::get(NewTy, C);
1158 assert(New != OldC && "Didn't replace constant??");
1159 OldC->uncheckedReplaceAllUsesWith(New);
1160 OldC->destroyConstant(); // This constant is now dead, destroy it.
1161 }
1162 };
1163}
1164
1165static std::vector<Constant*> getValType(ConstantPacked *CP) {
1166 std::vector<Constant*> Elements;
1167 Elements.reserve(CP->getNumOperands());
1168 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1169 Elements.push_back(CP->getOperand(i));
1170 return Elements;
1171}
1172
Chris Lattner69edc982006-09-28 00:35:06 +00001173static ManagedStatic<ValueMap<std::vector<Constant*>, PackedType,
1174 ConstantPacked> > PackedConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001175
1176Constant *ConstantPacked::get(const PackedType *Ty,
1177 const std::vector<Constant*> &V) {
1178 // If this is an all-zero packed, return a ConstantAggregateZero object
1179 if (!V.empty()) {
1180 Constant *C = V[0];
1181 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001182 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001183 for (unsigned i = 1, e = V.size(); i != e; ++i)
1184 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001185 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001186 }
1187 return ConstantAggregateZero::get(Ty);
1188}
1189
1190Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1191 assert(!V.empty() && "Cannot infer type if V is empty");
1192 return get(PackedType::get(V.front()->getType(),V.size()), V);
1193}
1194
1195// destroyConstant - Remove the constant from the constant table...
1196//
1197void ConstantPacked::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001198 PackedConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001199 destroyConstantImpl();
1200}
1201
Chris Lattner3462ae32001-12-03 22:26:30 +00001202//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001203//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001204
Chris Lattner189d19f2003-11-21 20:23:48 +00001205namespace llvm {
1206 // ConstantPointerNull does not take extra "value" argument...
1207 template<class ValType>
1208 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1209 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1210 return new ConstantPointerNull(Ty);
1211 }
1212 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001213
Chris Lattner189d19f2003-11-21 20:23:48 +00001214 template<>
1215 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1216 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1217 // Make everyone now use a constant of the new type...
1218 Constant *New = ConstantPointerNull::get(NewTy);
1219 assert(New != OldC && "Didn't replace constant??");
1220 OldC->uncheckedReplaceAllUsesWith(New);
1221 OldC->destroyConstant(); // This constant is now dead, destroy it.
1222 }
1223 };
1224}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001225
Chris Lattner69edc982006-09-28 00:35:06 +00001226static ManagedStatic<ValueMap<char, PointerType,
1227 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001228
Chris Lattner3e650af2004-08-04 04:48:01 +00001229static char getValType(ConstantPointerNull *) {
1230 return 0;
1231}
1232
1233
Chris Lattner3462ae32001-12-03 22:26:30 +00001234ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001235 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001236}
1237
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001238// destroyConstant - Remove the constant from the constant table...
1239//
1240void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001241 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001242 destroyConstantImpl();
1243}
1244
1245
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001246//---- UndefValue::get() implementation...
1247//
1248
1249namespace llvm {
1250 // UndefValue does not take extra "value" argument...
1251 template<class ValType>
1252 struct ConstantCreator<UndefValue, Type, ValType> {
1253 static UndefValue *create(const Type *Ty, const ValType &V) {
1254 return new UndefValue(Ty);
1255 }
1256 };
1257
1258 template<>
1259 struct ConvertConstantType<UndefValue, Type> {
1260 static void convert(UndefValue *OldC, const Type *NewTy) {
1261 // Make everyone now use a constant of the new type.
1262 Constant *New = UndefValue::get(NewTy);
1263 assert(New != OldC && "Didn't replace constant??");
1264 OldC->uncheckedReplaceAllUsesWith(New);
1265 OldC->destroyConstant(); // This constant is now dead, destroy it.
1266 }
1267 };
1268}
1269
Chris Lattner69edc982006-09-28 00:35:06 +00001270static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001271
1272static char getValType(UndefValue *) {
1273 return 0;
1274}
1275
1276
1277UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001278 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001279}
1280
1281// destroyConstant - Remove the constant from the constant table.
1282//
1283void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001284 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001285 destroyConstantImpl();
1286}
1287
1288
1289
1290
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001291//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001292//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001293typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001294
Chris Lattner189d19f2003-11-21 20:23:48 +00001295namespace llvm {
1296 template<>
1297 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1298 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1299 if (V.first == Instruction::Cast)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001300 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
Chris Lattner189d19f2003-11-21 20:23:48 +00001301 if ((V.first >= Instruction::BinaryOpsBegin &&
1302 V.first < Instruction::BinaryOpsEnd) ||
1303 V.first == Instruction::Shl || V.first == Instruction::Shr)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001304 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001305 if (V.first == Instruction::Select)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001306 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Robert Bocchino23004482006-01-10 19:05:34 +00001307 if (V.first == Instruction::ExtractElement)
1308 return new ExtractElementConstantExpr(V.second[0], V.second[1]);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001309 if (V.first == Instruction::InsertElement)
1310 return new InsertElementConstantExpr(V.second[0], V.second[1],
1311 V.second[2]);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001312 if (V.first == Instruction::ShuffleVector)
1313 return new ShuffleVectorConstantExpr(V.second[0], V.second[1],
1314 V.second[2]);
1315
Chris Lattner189d19f2003-11-21 20:23:48 +00001316 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001317
Chris Lattner189d19f2003-11-21 20:23:48 +00001318 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001319 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001320 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001321 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001322
Chris Lattner189d19f2003-11-21 20:23:48 +00001323 template<>
1324 struct ConvertConstantType<ConstantExpr, Type> {
1325 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1326 Constant *New;
1327 switch (OldC->getOpcode()) {
1328 case Instruction::Cast:
1329 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1330 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001331 case Instruction::Select:
1332 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1333 OldC->getOperand(1),
1334 OldC->getOperand(2));
1335 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001336 case Instruction::Shl:
1337 case Instruction::Shr:
1338 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1339 OldC->getOperand(0), OldC->getOperand(1));
1340 break;
1341 default:
1342 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1343 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1344 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1345 OldC->getOperand(1));
1346 break;
1347 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001348 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001349 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1350 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001351 break;
1352 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001353
Chris Lattner189d19f2003-11-21 20:23:48 +00001354 assert(New != OldC && "Didn't replace constant??");
1355 OldC->uncheckedReplaceAllUsesWith(New);
1356 OldC->destroyConstant(); // This constant is now dead, destroy it.
1357 }
1358 };
1359} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001360
1361
Chris Lattner3e650af2004-08-04 04:48:01 +00001362static ExprMapKeyType getValType(ConstantExpr *CE) {
1363 std::vector<Constant*> Operands;
1364 Operands.reserve(CE->getNumOperands());
1365 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1366 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1367 return ExprMapKeyType(CE->getOpcode(), Operands);
1368}
1369
Chris Lattner69edc982006-09-28 00:35:06 +00001370static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1371 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001372
Chris Lattner46b3d302003-04-16 22:40:51 +00001373Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001374 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1375
Chris Lattneracdbe712003-04-17 19:24:48 +00001376 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1377 return FC; // Fold a few common cases...
1378
Vikram S. Adve4c485332002-07-15 18:19:33 +00001379 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001380 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001381 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001382 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001383}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001384
Chris Lattnerdd284742004-04-04 23:20:30 +00001385Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001386 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001387 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1388 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001389 if (C->getType() != Type::BoolTy) {
1390 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1391 return ConstantExpr::getCast(C, Ty);
1392 } else {
Chris Lattnera84df0a22006-09-28 23:36:21 +00001393 if (C == ConstantBool::getTrue())
Chris Lattner1ece6f82005-01-01 15:59:57 +00001394 return ConstantIntegral::getAllOnesValue(Ty);
1395 else
1396 return ConstantIntegral::getNullValue(Ty);
1397 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001398}
1399
1400Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001401 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001402 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1403 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001404 if (C->getType() != Type::BoolTy)
1405 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001406 return ConstantExpr::getCast(C, Ty);
1407}
1408
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001409Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001410 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001411 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001412 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1413 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1414 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001415}
1416
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001417Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1418 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
1419 static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0));
1420
1421 return ConstantExpr::getGetElementPtr(C, Indices);
1422}
1423
Chris Lattnerb50d1352003-10-05 00:17:43 +00001424Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1425 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001426 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1427 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001428 // Check the operands for consistency first
1429 assert((Opcode >= Instruction::BinaryOpsBegin &&
1430 Opcode < Instruction::BinaryOpsEnd) &&
1431 "Invalid opcode in binary constant expression");
1432 assert(C1->getType() == C2->getType() &&
1433 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001434
Chris Lattnere1496fb2006-09-17 19:14:47 +00001435 if (ReqTy == C1->getType() || (Instruction::isComparison(Opcode) &&
Chris Lattner29ca2c62004-08-04 18:50:09 +00001436 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001437 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1438 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001439
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001440 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001441 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001442 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001443}
1444
Chris Lattner29ca2c62004-08-04 18:50:09 +00001445Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001446#ifndef NDEBUG
1447 switch (Opcode) {
1448 case Instruction::Add: case Instruction::Sub:
1449 case Instruction::Mul: case Instruction::Div:
1450 case Instruction::Rem:
1451 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001452 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
1453 isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001454 "Tried to create an arithmetic operation on a non-arithmetic type!");
1455 break;
1456 case Instruction::And:
1457 case Instruction::Or:
1458 case Instruction::Xor:
1459 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001460 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001461 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001462 break;
1463 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1464 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1465 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1466 break;
1467 case Instruction::Shl:
1468 case Instruction::Shr:
1469 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001470 assert((C1->getType()->isInteger() || isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001471 "Tried to create a shift operation on a non-integer type!");
1472 break;
1473 default:
1474 break;
1475 }
1476#endif
1477
Chris Lattnere1496fb2006-09-17 19:14:47 +00001478 if (Instruction::isComparison(Opcode))
Chris Lattner29ca2c62004-08-04 18:50:09 +00001479 return getTy(Type::BoolTy, Opcode, C1, C2);
1480 else
1481 return getTy(C1->getType(), Opcode, C1, C2);
1482}
1483
Chris Lattner6e415c02004-03-12 05:54:04 +00001484Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1485 Constant *V1, Constant *V2) {
1486 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1487 assert(V1->getType() == V2->getType() && "Select value types must match!");
1488 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1489
1490 if (ReqTy == V1->getType())
1491 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1492 return SC; // Fold common cases
1493
1494 std::vector<Constant*> argVec(3, C);
1495 argVec[1] = V1;
1496 argVec[2] = V2;
1497 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001498 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001499}
1500
Chris Lattner9eb2b522004-01-12 19:12:58 +00001501/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001502Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1503 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001504 // Check the operands for consistency first
1505 assert((Opcode == Instruction::Shl ||
1506 Opcode == Instruction::Shr) &&
1507 "Invalid opcode in binary constant expression");
1508 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1509 "Invalid operand types for Shift constant expr!");
1510
Chris Lattner0bba7712004-01-12 20:40:42 +00001511 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001512 return FC; // Fold a few common cases...
1513
1514 // Look up the constant in the table first to ensure uniqueness
1515 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001516 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001517 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001518}
1519
1520
Chris Lattnerb50d1352003-10-05 00:17:43 +00001521Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001522 const std::vector<Value*> &IdxList) {
1523 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001524 "GEP indices invalid!");
1525
Chris Lattneracdbe712003-04-17 19:24:48 +00001526 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1527 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001528
Chris Lattnerb50d1352003-10-05 00:17:43 +00001529 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001530 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001531 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001532 std::vector<Constant*> ArgVec;
1533 ArgVec.reserve(IdxList.size()+1);
1534 ArgVec.push_back(C);
1535 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1536 ArgVec.push_back(cast<Constant>(IdxList[i]));
1537 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001538 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001539}
1540
Chris Lattnerb50d1352003-10-05 00:17:43 +00001541Constant *ConstantExpr::getGetElementPtr(Constant *C,
1542 const std::vector<Constant*> &IdxList){
1543 // Get the result type of the getelementptr!
1544 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1545
1546 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1547 true);
1548 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001549 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1550}
1551
1552Constant *ConstantExpr::getGetElementPtr(Constant *C,
1553 const std::vector<Value*> &IdxList) {
1554 // Get the result type of the getelementptr!
1555 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1556 true);
1557 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001558 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1559}
1560
Robert Bocchino23004482006-01-10 19:05:34 +00001561Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1562 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001563 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1564 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001565 // Look up the constant in the table first to ensure uniqueness
1566 std::vector<Constant*> ArgVec(1, Val);
1567 ArgVec.push_back(Idx);
1568 const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001569 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001570}
1571
1572Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1573 assert(isa<PackedType>(Val->getType()) &&
1574 "Tried to create extractelement operation on non-packed type!");
1575 assert(Idx->getType() == Type::UIntTy &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001576 "Extractelement index must be uint type!");
Robert Bocchino23004482006-01-10 19:05:34 +00001577 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1578 Val, Idx);
1579}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001580
Robert Bocchinoca27f032006-01-17 20:07:22 +00001581Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1582 Constant *Elt, Constant *Idx) {
1583 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1584 return FC; // Fold a few common cases...
1585 // Look up the constant in the table first to ensure uniqueness
1586 std::vector<Constant*> ArgVec(1, Val);
1587 ArgVec.push_back(Elt);
1588 ArgVec.push_back(Idx);
1589 const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001590 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001591}
1592
1593Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1594 Constant *Idx) {
1595 assert(isa<PackedType>(Val->getType()) &&
1596 "Tried to create insertelement operation on non-packed type!");
1597 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
1598 && "Insertelement types must match!");
1599 assert(Idx->getType() == Type::UIntTy &&
1600 "Insertelement index must be uint type!");
1601 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
1602 Val, Elt, Idx);
1603}
1604
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001605Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1606 Constant *V2, Constant *Mask) {
1607 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1608 return FC; // Fold a few common cases...
1609 // Look up the constant in the table first to ensure uniqueness
1610 std::vector<Constant*> ArgVec(1, V1);
1611 ArgVec.push_back(V2);
1612 ArgVec.push_back(Mask);
1613 const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001614 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001615}
1616
1617Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1618 Constant *Mask) {
1619 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1620 "Invalid shuffle vector constant expr operands!");
1621 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1622}
1623
1624
Vikram S. Adve4c485332002-07-15 18:19:33 +00001625// destroyConstant - Remove the constant from the constant table...
1626//
1627void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001628 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001629 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001630}
1631
Chris Lattner3cd8c562002-07-30 18:54:25 +00001632const char *ConstantExpr::getOpcodeName() const {
1633 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001634}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001635
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001636//===----------------------------------------------------------------------===//
1637// replaceUsesOfWithOnConstant implementations
1638
1639void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001640 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001641 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001642 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001643
1644 unsigned OperandToUpdate = U-OperandList;
1645 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1646
Jim Laskeyc03caef2006-07-17 17:38:29 +00001647 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001648 Lookup.first.first = getType();
1649 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001650
Chris Lattnerb64419a2005-10-03 22:51:37 +00001651 std::vector<Constant*> &Values = Lookup.first.second;
1652 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001653
Chris Lattner8760ec72005-10-04 01:17:50 +00001654 // Fill values with the modified operands of the constant array. Also,
1655 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001656 bool isAllZeros = false;
1657 if (!ToC->isNullValue()) {
1658 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1659 Values.push_back(cast<Constant>(O->get()));
1660 } else {
1661 isAllZeros = true;
1662 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1663 Constant *Val = cast<Constant>(O->get());
1664 Values.push_back(Val);
1665 if (isAllZeros) isAllZeros = Val->isNullValue();
1666 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001667 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001668 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001669
Chris Lattnerb64419a2005-10-03 22:51:37 +00001670 Constant *Replacement = 0;
1671 if (isAllZeros) {
1672 Replacement = ConstantAggregateZero::get(getType());
1673 } else {
1674 // Check to see if we have this array type already.
1675 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001676 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001677 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001678
1679 if (Exists) {
1680 Replacement = I->second;
1681 } else {
1682 // Okay, the new shape doesn't exist in the system yet. Instead of
1683 // creating a new constant array, inserting it, replaceallusesof'ing the
1684 // old with the new, then deleting the old... just update the current one
1685 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001686 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001687
Chris Lattnerdff59112005-10-04 18:47:09 +00001688 // Update to the new value.
1689 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001690 return;
1691 }
1692 }
1693
1694 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001695 assert(Replacement != this && "I didn't contain From!");
1696
Chris Lattner7a1450d2005-10-04 18:13:04 +00001697 // Everyone using this now uses the replacement.
1698 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001699
1700 // Delete the old constant!
1701 destroyConstant();
1702}
1703
1704void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001705 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001706 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001707 Constant *ToC = cast<Constant>(To);
1708
Chris Lattnerdff59112005-10-04 18:47:09 +00001709 unsigned OperandToUpdate = U-OperandList;
1710 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1711
Jim Laskeyc03caef2006-07-17 17:38:29 +00001712 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001713 Lookup.first.first = getType();
1714 Lookup.second = this;
1715 std::vector<Constant*> &Values = Lookup.first.second;
1716 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001717
Chris Lattnerdff59112005-10-04 18:47:09 +00001718
Chris Lattner8760ec72005-10-04 01:17:50 +00001719 // Fill values with the modified operands of the constant struct. Also,
1720 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001721 bool isAllZeros = false;
1722 if (!ToC->isNullValue()) {
1723 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1724 Values.push_back(cast<Constant>(O->get()));
1725 } else {
1726 isAllZeros = true;
1727 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1728 Constant *Val = cast<Constant>(O->get());
1729 Values.push_back(Val);
1730 if (isAllZeros) isAllZeros = Val->isNullValue();
1731 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001732 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001733 Values[OperandToUpdate] = ToC;
1734
Chris Lattner8760ec72005-10-04 01:17:50 +00001735 Constant *Replacement = 0;
1736 if (isAllZeros) {
1737 Replacement = ConstantAggregateZero::get(getType());
1738 } else {
1739 // Check to see if we have this array type already.
1740 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001741 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001742 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001743
1744 if (Exists) {
1745 Replacement = I->second;
1746 } else {
1747 // Okay, the new shape doesn't exist in the system yet. Instead of
1748 // creating a new constant struct, inserting it, replaceallusesof'ing the
1749 // old with the new, then deleting the old... just update the current one
1750 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001751 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001752
Chris Lattnerdff59112005-10-04 18:47:09 +00001753 // Update to the new value.
1754 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001755 return;
1756 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001757 }
1758
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001759 assert(Replacement != this && "I didn't contain From!");
1760
Chris Lattner7a1450d2005-10-04 18:13:04 +00001761 // Everyone using this now uses the replacement.
1762 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001763
1764 // Delete the old constant!
1765 destroyConstant();
1766}
1767
1768void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001769 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001770 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1771
1772 std::vector<Constant*> Values;
1773 Values.reserve(getNumOperands()); // Build replacement array...
1774 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1775 Constant *Val = getOperand(i);
1776 if (Val == From) Val = cast<Constant>(To);
1777 Values.push_back(Val);
1778 }
1779
1780 Constant *Replacement = ConstantPacked::get(getType(), Values);
1781 assert(Replacement != this && "I didn't contain From!");
1782
Chris Lattner7a1450d2005-10-04 18:13:04 +00001783 // Everyone using this now uses the replacement.
1784 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001785
1786 // Delete the old constant!
1787 destroyConstant();
1788}
1789
1790void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001791 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001792 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1793 Constant *To = cast<Constant>(ToV);
1794
1795 Constant *Replacement = 0;
1796 if (getOpcode() == Instruction::GetElementPtr) {
1797 std::vector<Constant*> Indices;
1798 Constant *Pointer = getOperand(0);
1799 Indices.reserve(getNumOperands()-1);
1800 if (Pointer == From) Pointer = To;
1801
1802 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1803 Constant *Val = getOperand(i);
1804 if (Val == From) Val = To;
1805 Indices.push_back(Val);
1806 }
1807 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
1808 } else if (getOpcode() == Instruction::Cast) {
1809 assert(getOperand(0) == From && "Cast only has one use!");
1810 Replacement = ConstantExpr::getCast(To, getType());
1811 } else if (getOpcode() == Instruction::Select) {
1812 Constant *C1 = getOperand(0);
1813 Constant *C2 = getOperand(1);
1814 Constant *C3 = getOperand(2);
1815 if (C1 == From) C1 = To;
1816 if (C2 == From) C2 = To;
1817 if (C3 == From) C3 = To;
1818 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00001819 } else if (getOpcode() == Instruction::ExtractElement) {
1820 Constant *C1 = getOperand(0);
1821 Constant *C2 = getOperand(1);
1822 if (C1 == From) C1 = To;
1823 if (C2 == From) C2 = To;
1824 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00001825 } else if (getOpcode() == Instruction::InsertElement) {
1826 Constant *C1 = getOperand(0);
1827 Constant *C2 = getOperand(1);
1828 Constant *C3 = getOperand(1);
1829 if (C1 == From) C1 = To;
1830 if (C2 == From) C2 = To;
1831 if (C3 == From) C3 = To;
1832 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
1833 } else if (getOpcode() == Instruction::ShuffleVector) {
1834 Constant *C1 = getOperand(0);
1835 Constant *C2 = getOperand(1);
1836 Constant *C3 = getOperand(2);
1837 if (C1 == From) C1 = To;
1838 if (C2 == From) C2 = To;
1839 if (C3 == From) C3 = To;
1840 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001841 } else if (getNumOperands() == 2) {
1842 Constant *C1 = getOperand(0);
1843 Constant *C2 = getOperand(1);
1844 if (C1 == From) C1 = To;
1845 if (C2 == From) C2 = To;
1846 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
1847 } else {
1848 assert(0 && "Unknown ConstantExpr type!");
1849 return;
1850 }
1851
1852 assert(Replacement != this && "I didn't contain From!");
1853
Chris Lattner7a1450d2005-10-04 18:13:04 +00001854 // Everyone using this now uses the replacement.
1855 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001856
1857 // Delete the old constant!
1858 destroyConstant();
1859}
1860
1861
Jim Laskey2698f0d2006-03-08 18:11:07 +00001862/// getStringValue - Turn an LLVM constant pointer that eventually points to a
1863/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00001864/// Parameter Chop determines if the result is chopped at the first null
1865/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00001866///
Evan Cheng38280c02006-03-10 23:52:03 +00001867std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00001868 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
1869 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
1870 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
1871 if (Init->isString()) {
1872 std::string Result = Init->getAsString();
1873 if (Offset < Result.size()) {
1874 // If we are pointing INTO The string, erase the beginning...
1875 Result.erase(Result.begin(), Result.begin()+Offset);
1876
1877 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00001878 if (Chop) {
1879 std::string::size_type NullPos = Result.find_first_of((char)0);
1880 if (NullPos != std::string::npos)
1881 Result.erase(Result.begin()+NullPos, Result.end());
1882 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00001883 return Result;
1884 }
1885 }
1886 }
1887 } else if (Constant *C = dyn_cast<Constant>(this)) {
1888 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00001889 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00001890 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1891 if (CE->getOpcode() == Instruction::GetElementPtr) {
1892 // Turn a gep into the specified offset.
1893 if (CE->getNumOperands() == 3 &&
1894 cast<Constant>(CE->getOperand(1))->isNullValue() &&
1895 isa<ConstantInt>(CE->getOperand(2))) {
1896 Offset += cast<ConstantInt>(CE->getOperand(2))->getRawValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00001897 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00001898 }
1899 }
1900 }
1901 }
1902 return "";
1903}
1904