blob: d25000244140e233d59fa9d1fea2f878912a9cd8 [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 Lattner2f7c9632001-06-06 20:29:01 +000023#include <algorithm>
Reid Spencercf394bf2004-07-04 11:51:24 +000024#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattner3462ae32001-12-03 22:26:30 +000027ConstantBool *ConstantBool::True = new ConstantBool(true);
28ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000029
Chris Lattner9655e542001-07-20 19:16:02 +000030
Chris Lattner2f7c9632001-06-06 20:29:01 +000031//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000032// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000033//===----------------------------------------------------------------------===//
34
Chris Lattner3462ae32001-12-03 22:26:30 +000035void Constant::destroyConstantImpl() {
36 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000037 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000038 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000039 // but they don't know that. Because we only find out when the CPV is
40 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000041 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000042 //
43 while (!use_empty()) {
44 Value *V = use_back();
45#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000046 if (!isa<Constant>(V))
47 std::cerr << "While deleting: " << *this
48 << "\n\nUse still stuck around after Def is destroyed: "
49 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000050#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000051 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000052 Constant *CV = cast<Constant>(V);
53 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000054
55 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000056 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000057 }
58
59 // Value has no outstanding references it is safe to delete it now...
60 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000061}
Chris Lattner2f7c9632001-06-06 20:29:01 +000062
Chris Lattnerb1585a92002-08-13 17:50:20 +000063// Static constructor to create a '0' constant of arbitrary type...
64Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000065 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000066 case Type::BoolTyID: {
67 static Constant *NullBool = ConstantBool::get(false);
68 return NullBool;
69 }
70 case Type::SByteTyID: {
71 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
72 return NullSByte;
73 }
74 case Type::UByteTyID: {
75 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
76 return NullUByte;
77 }
78 case Type::ShortTyID: {
79 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
80 return NullShort;
81 }
82 case Type::UShortTyID: {
83 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
84 return NullUShort;
85 }
86 case Type::IntTyID: {
87 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
88 return NullInt;
89 }
90 case Type::UIntTyID: {
91 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
92 return NullUInt;
93 }
94 case Type::LongTyID: {
95 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
96 return NullLong;
97 }
98 case Type::ULongTyID: {
99 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
100 return NullULong;
101 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000102
Chris Lattner3e88ef92003-10-03 19:34:51 +0000103 case Type::FloatTyID: {
104 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
105 return NullFloat;
106 }
107 case Type::DoubleTyID: {
108 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
109 return NullDouble;
110 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000111
Misha Brukmanb1c93172005-04-21 23:48:37 +0000112 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000113 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000114
Chris Lattner9fba3da2004-02-15 05:53:04 +0000115 case Type::StructTyID:
116 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000117 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000118 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000119 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000120 // Function, Label, or Opaque type?
121 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000122 return 0;
123 }
124}
125
126// Static constructor to create the maximum constant of an integral type...
127ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000128 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000129 case Type::BoolTyID: return ConstantBool::True;
130 case Type::SByteTyID:
131 case Type::ShortTyID:
132 case Type::IntTyID:
133 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000134 // Calculate 011111111111111...
Chris Lattnerb1585a92002-08-13 17:50:20 +0000135 unsigned TypeBits = Ty->getPrimitiveSize()*8;
136 int64_t Val = INT64_MAX; // All ones
137 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
138 return ConstantSInt::get(Ty, Val);
139 }
140
141 case Type::UByteTyID:
142 case Type::UShortTyID:
143 case Type::UIntTyID:
144 case Type::ULongTyID: return getAllOnesValue(Ty);
145
Chris Lattner31408f72002-08-14 17:12:13 +0000146 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000147 }
148}
149
150// Static constructor to create the minimum constant for an integral type...
151ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000152 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000153 case Type::BoolTyID: return ConstantBool::False;
154 case Type::SByteTyID:
155 case Type::ShortTyID:
156 case Type::IntTyID:
157 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000158 // Calculate 1111111111000000000000
Chris Lattnerb1585a92002-08-13 17:50:20 +0000159 unsigned TypeBits = Ty->getPrimitiveSize()*8;
160 int64_t Val = -1; // All ones
161 Val <<= TypeBits-1; // Shift over to the right spot
162 return ConstantSInt::get(Ty, Val);
163 }
164
165 case Type::UByteTyID:
166 case Type::UShortTyID:
167 case Type::UIntTyID:
168 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
169
Chris Lattner31408f72002-08-14 17:12:13 +0000170 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000171 }
172}
173
174// Static constructor to create an integral constant with all bits set
175ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000176 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000177 case Type::BoolTyID: return ConstantBool::True;
178 case Type::SByteTyID:
179 case Type::ShortTyID:
180 case Type::IntTyID:
181 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
182
183 case Type::UByteTyID:
184 case Type::UShortTyID:
185 case Type::UIntTyID:
186 case Type::ULongTyID: {
187 // Calculate ~0 of the right type...
188 unsigned TypeBits = Ty->getPrimitiveSize()*8;
189 uint64_t Val = ~0ULL; // All ones
190 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
191 return ConstantUInt::get(Ty, Val);
192 }
Chris Lattner31408f72002-08-14 17:12:13 +0000193 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000194 }
195}
196
Chris Lattner83e5d392003-03-10 22:39:02 +0000197bool ConstantUInt::isAllOnesValue() const {
198 unsigned TypeBits = getType()->getPrimitiveSize()*8;
199 uint64_t Val = ~0ULL; // All ones
200 Val >>= 64-TypeBits; // Shift out inappropriate bits
201 return getValue() == Val;
202}
203
Chris Lattnerb1585a92002-08-13 17:50:20 +0000204
Chris Lattner2f7c9632001-06-06 20:29:01 +0000205//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000206// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000207//===----------------------------------------------------------------------===//
208
209//===----------------------------------------------------------------------===//
210// Normal Constructors
211
Chris Lattner265eb642004-06-21 12:12:12 +0000212ConstantIntegral::ConstantIntegral(const Type *Ty, uint64_t V)
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000213 : Constant(Ty, SimpleConstantVal, 0, 0) {
Chris Lattner265eb642004-06-21 12:12:12 +0000214 Val.Unsigned = V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215}
Chris Lattner49d855c2001-09-07 16:46:31 +0000216
Chris Lattner265eb642004-06-21 12:12:12 +0000217ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy, V) {
218}
219
220ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty, V) {
Chris Lattner7309d662001-07-21 19:16:08 +0000221}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222
Chris Lattner3462ae32001-12-03 22:26:30 +0000223ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000224 assert(Ty->isInteger() && Ty->isSigned() &&
Reid Spencerf064bb22005-03-09 15:19:41 +0000225 "Illegal type for signed integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000226 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227}
228
Chris Lattner3462ae32001-12-03 22:26:30 +0000229ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000230 assert(Ty->isInteger() && Ty->isUnsigned() &&
231 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000232 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233}
234
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000235ConstantFP::ConstantFP(const Type *Ty, double V)
236 : Constant(Ty, SimpleConstantVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000237 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238 Val = V;
239}
240
Chris Lattner3462ae32001-12-03 22:26:30 +0000241ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000242 const std::vector<Constant*> &V)
243 : Constant(T, SimpleConstantVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000244 assert(V.size() == T->getNumElements() &&
245 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000246 Use *OL = OperandList;
Chris Lattner0d779712002-10-08 23:33:52 +0000247 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000248 assert((V[i]->getType() == T->getElementType() ||
249 (T->isAbstract() &&
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000250 V[i]->getType()->getTypeID()==T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000251 "Initializer for array element doesn't match array element type!");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000252 OL[i].init(V[i], this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000253 }
254}
255
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000256ConstantArray::~ConstantArray() {
257 delete [] OperandList;
258}
259
Chris Lattner3462ae32001-12-03 22:26:30 +0000260ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000261 const std::vector<Constant*> &V)
262 : Constant(T, SimpleConstantVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000263 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000264 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000265 Use *OL = OperandList;
Chris Lattner0d779712002-10-08 23:33:52 +0000266 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000267 assert((V[i]->getType() == T->getElementType(i) ||
268 ((T->getElementType(i)->isAbstract() ||
269 V[i]->getType()->isAbstract()) &&
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000270 T->getElementType(i)->getTypeID()==V[i]->getType()->getTypeID()))&&
Chris Lattner93c8f142003-06-02 17:42:47 +0000271 "Initializer for struct element doesn't match struct element type!");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000272 OL[i].init(V[i], this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000273 }
274}
275
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000276ConstantStruct::~ConstantStruct() {
277 delete [] OperandList;
278}
279
280
Brian Gaeke02209042004-08-20 06:00:58 +0000281ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000282 const std::vector<Constant*> &V)
283 : Constant(T, SimpleConstantVal, new Use[V.size()], V.size()) {
284 Use *OL = OperandList;
Brian Gaeke02209042004-08-20 06:00:58 +0000285 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000286 assert((V[i]->getType() == T->getElementType() ||
287 (T->isAbstract() &&
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000288 V[i]->getType()->getTypeID()==T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000289 "Initializer for packed element doesn't match packed element type!");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000290 OL[i].init(V[i], this);
Brian Gaeke02209042004-08-20 06:00:58 +0000291 }
292}
293
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000294ConstantPacked::~ConstantPacked() {
295 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000296}
297
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000298/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
299/// behind the scenes to implement unary constant exprs.
300class UnaryConstantExpr : public ConstantExpr {
301 Use Op;
302public:
303 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
304 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
305};
Chris Lattner6e415c02004-03-12 05:54:04 +0000306
Chris Lattner22ced562003-06-22 20:48:30 +0000307static bool isSetCC(unsigned Opcode) {
308 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
309 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
310 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
311}
312
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000313/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
314/// behind the scenes to implement binary constant exprs.
315class BinaryConstantExpr : public ConstantExpr {
316 Use Ops[2];
317public:
318 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
319 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
320 Opcode, Ops, 2) {
321 Ops[0].init(C1, this);
322 Ops[1].init(C2, this);
323 }
324};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000325
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000326/// SelectConstantExpr - This class is private to Constants.cpp, and is used
327/// behind the scenes to implement select constant exprs.
328class SelectConstantExpr : public ConstantExpr {
329 Use Ops[3];
330public:
331 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
332 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
333 Ops[0].init(C1, this);
334 Ops[1].init(C2, this);
335 Ops[2].init(C3, this);
336 }
337};
338
339/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
340/// used behind the scenes to implement getelementpr constant exprs.
341struct GetElementPtrConstantExpr : public ConstantExpr {
342 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
343 const Type *DestTy)
344 : ConstantExpr(DestTy, Instruction::GetElementPtr,
345 new Use[IdxList.size()+1], IdxList.size()+1) {
346 OperandList[0].init(C, this);
347 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
348 OperandList[i+1].init(IdxList[i], this);
349 }
350 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000351 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000352 }
353};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000354
Chris Lattner817175f2004-03-29 02:37:53 +0000355/// ConstantExpr::get* - Return some common constants without having to
356/// specify the full Instruction::OPCODE identifier.
357///
358Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000359 if (!C->getType()->isFloatingPoint())
360 return get(Instruction::Sub, getNullValue(C->getType()), C);
361 else
362 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000363}
364Constant *ConstantExpr::getNot(Constant *C) {
365 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
366 return get(Instruction::Xor, C,
367 ConstantIntegral::getAllOnesValue(C->getType()));
368}
369Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
370 return get(Instruction::Add, C1, C2);
371}
372Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
373 return get(Instruction::Sub, C1, C2);
374}
375Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
376 return get(Instruction::Mul, C1, C2);
377}
378Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
379 return get(Instruction::Div, C1, C2);
380}
381Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
382 return get(Instruction::Rem, C1, C2);
383}
384Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
385 return get(Instruction::And, C1, C2);
386}
387Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
388 return get(Instruction::Or, C1, C2);
389}
390Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
391 return get(Instruction::Xor, C1, C2);
392}
393Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
394 return get(Instruction::SetEQ, C1, C2);
395}
396Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
397 return get(Instruction::SetNE, C1, C2);
398}
399Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
400 return get(Instruction::SetLT, C1, C2);
401}
402Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
403 return get(Instruction::SetGT, C1, C2);
404}
405Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
406 return get(Instruction::SetLE, C1, C2);
407}
408Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
409 return get(Instruction::SetGE, C1, C2);
410}
411Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
412 return get(Instruction::Shl, C1, C2);
413}
414Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
415 return get(Instruction::Shr, C1, C2);
416}
417
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000418Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
419 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
420 return getCast(getShr(getCast(C1,
421 C1->getType()->getUnsignedVersion()), C2), C1->getType());
422}
Chris Lattner817175f2004-03-29 02:37:53 +0000423
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000424Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
425 if (C1->getType()->isSigned()) return getShr(C1, C2);
426 return getCast(getShr(getCast(C1,
427 C1->getType()->getSignedVersion()), C2), C1->getType());
428}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000429
Chris Lattner2f7c9632001-06-06 20:29:01 +0000430
431//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000432// isValueValidForType implementations
433
Chris Lattner3462ae32001-12-03 22:26:30 +0000434bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000435 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000436 default:
437 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000438 // Signed types...
439 case Type::SByteTyID:
440 return (Val <= INT8_MAX && Val >= INT8_MIN);
441 case Type::ShortTyID:
442 return (Val <= INT16_MAX && Val >= INT16_MIN);
443 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000444 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000445 case Type::LongTyID:
446 return true; // This is the largest type...
447 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000448}
449
Chris Lattner3462ae32001-12-03 22:26:30 +0000450bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000451 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000452 default:
453 return false; // These can't be represented as integers!!!
454
455 // Unsigned types...
456 case Type::UByteTyID:
457 return (Val <= UINT8_MAX);
458 case Type::UShortTyID:
459 return (Val <= UINT16_MAX);
460 case Type::UIntTyID:
461 return (Val <= UINT32_MAX);
462 case Type::ULongTyID:
463 return true; // This is the largest type...
464 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000465}
466
Chris Lattner3462ae32001-12-03 22:26:30 +0000467bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000468 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000469 default:
470 return false; // These can't be represented as floating point!
471
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000472 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000473 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000474 case Type::DoubleTyID:
475 return true; // This is the largest type...
476 }
477};
Chris Lattner9655e542001-07-20 19:16:02 +0000478
Chris Lattner49d855c2001-09-07 16:46:31 +0000479//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000480// replaceUsesOfWithOnConstant implementations
481
Chris Lattnerc27038d2003-08-29 05:36:46 +0000482void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
483 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000484 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
485
486 std::vector<Constant*> Values;
Alkis Evlogimenosf0cc8142004-08-04 08:02:59 +0000487 Values.reserve(getNumOperands()); // Build replacement array...
488 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
489 Constant *Val = getOperand(i);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000490 if (Val == From) Val = cast<Constant>(To);
491 Values.push_back(Val);
492 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000493
Chris Lattnerc75bf522004-02-15 04:05:58 +0000494 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000495 assert(Replacement != this && "I didn't contain From!");
496
497 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000498 if (DisableChecking)
499 uncheckedReplaceAllUsesWith(Replacement);
500 else
501 replaceAllUsesWith(Replacement);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000502
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000503 // Delete the old constant!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000504 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000505}
506
Chris Lattnerc27038d2003-08-29 05:36:46 +0000507void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
508 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000509 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
510
511 std::vector<Constant*> Values;
Alkis Evlogimenosf0cc8142004-08-04 08:02:59 +0000512 Values.reserve(getNumOperands()); // Build replacement array...
513 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
514 Constant *Val = getOperand(i);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000515 if (Val == From) Val = cast<Constant>(To);
516 Values.push_back(Val);
517 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000518
Chris Lattner37a716f2004-02-15 04:07:32 +0000519 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000520 assert(Replacement != this && "I didn't contain From!");
521
522 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000523 if (DisableChecking)
524 uncheckedReplaceAllUsesWith(Replacement);
525 else
526 replaceAllUsesWith(Replacement);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000527
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000528 // Delete the old constant!
529 destroyConstant();
530}
531
Brian Gaeke02209042004-08-20 06:00:58 +0000532void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
533 bool DisableChecking) {
534 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
535
536 std::vector<Constant*> Values;
537 Values.reserve(getNumOperands()); // Build replacement array...
538 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
539 Constant *Val = getOperand(i);
540 if (Val == From) Val = cast<Constant>(To);
541 Values.push_back(Val);
542 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000543
Brian Gaeke02209042004-08-20 06:00:58 +0000544 Constant *Replacement = ConstantPacked::get(getType(), Values);
545 assert(Replacement != this && "I didn't contain From!");
546
547 // Everyone using this now uses the replacement...
548 if (DisableChecking)
549 uncheckedReplaceAllUsesWith(Replacement);
550 else
551 replaceAllUsesWith(Replacement);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000552
Brian Gaeke02209042004-08-20 06:00:58 +0000553 // Delete the old constant!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000554 destroyConstant();
Brian Gaeke02209042004-08-20 06:00:58 +0000555}
556
Chris Lattnerc27038d2003-08-29 05:36:46 +0000557void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
558 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000559 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
560 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000561
Chris Lattner46b3d302003-04-16 22:40:51 +0000562 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000563 if (getOpcode() == Instruction::GetElementPtr) {
564 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000565 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000566 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000567 if (Pointer == From) Pointer = To;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000568
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000569 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000570 Constant *Val = getOperand(i);
571 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000572 Indices.push_back(Val);
573 }
574 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
575 } else if (getOpcode() == Instruction::Cast) {
576 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000577 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattner467cb2b2004-03-31 02:56:11 +0000578 } else if (getOpcode() == Instruction::Select) {
579 Constant *C1 = getOperand(0);
580 Constant *C2 = getOperand(1);
581 Constant *C3 = getOperand(2);
582 if (C1 == From) C1 = To;
583 if (C2 == From) C2 = To;
584 if (C3 == From) C3 = To;
585 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000586 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000587 Constant *C1 = getOperand(0);
588 Constant *C2 = getOperand(1);
589 if (C1 == From) C1 = To;
590 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000591 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
592 } else {
593 assert(0 && "Unknown ConstantExpr type!");
594 return;
595 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000596
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000597 assert(Replacement != this && "I didn't contain From!");
598
599 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000600 if (DisableChecking)
601 uncheckedReplaceAllUsesWith(Replacement);
602 else
603 replaceAllUsesWith(Replacement);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000604
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000605 // Delete the old constant!
606 destroyConstant();
607}
608
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000609//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000610// Factory Function Implementation
611
Chris Lattner98fa07b2003-05-23 20:03:32 +0000612// ConstantCreator - A class that is used to create constants by
613// ValueMap*. This class should be partially specialized if there is
614// something strange that needs to be done to interface to the ctor for the
615// constant.
616//
Chris Lattner189d19f2003-11-21 20:23:48 +0000617namespace llvm {
618 template<class ConstantClass, class TypeClass, class ValType>
619 struct ConstantCreator {
620 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
621 return new ConstantClass(Ty, V);
622 }
623 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000624
Chris Lattner189d19f2003-11-21 20:23:48 +0000625 template<class ConstantClass, class TypeClass>
626 struct ConvertConstantType {
627 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
628 assert(0 && "This type cannot be converted!\n");
629 abort();
630 }
631 };
632}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000633
Chris Lattner98fa07b2003-05-23 20:03:32 +0000634namespace {
635 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000636 class ValueMap : public AbstractTypeUser {
637 typedef std::pair<const TypeClass*, ValType> MapKey;
638 typedef std::map<MapKey, ConstantClass *> MapTy;
639 typedef typename MapTy::iterator MapIterator;
640 MapTy Map;
641
642 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
643 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000644
645 friend void Constant::clearAllValueMaps();
646 private:
647 void clear(std::vector<Constant *> &Constants) {
648 for(MapIterator I = Map.begin(); I != Map.end(); ++I)
649 Constants.push_back(I->second);
650 Map.clear();
651 AbstractTypeMap.clear();
652 }
653
Chris Lattner98fa07b2003-05-23 20:03:32 +0000654 public:
655 // getOrCreate - Return the specified constant from the map, creating it if
656 // necessary.
657 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000658 MapKey Lookup(Ty, V);
659 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000660 if (I != Map.end() && I->first == Lookup)
661 return I->second; // Is it in the map?
662
663 // If no preexisting value, create one now...
664 ConstantClass *Result =
665 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
666
Chris Lattnerb50d1352003-10-05 00:17:43 +0000667
668 /// FIXME: why does this assert fail when loading 176.gcc?
669 //assert(Result->getType() == Ty && "Type specified is not correct!");
670 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
671
672 // If the type of the constant is abstract, make sure that an entry exists
673 // for it in the AbstractTypeMap.
674 if (Ty->isAbstract()) {
675 typename AbstractTypeMapTy::iterator TI =
676 AbstractTypeMap.lower_bound(Ty);
677
678 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
679 // Add ourselves to the ATU list of the type.
680 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
681
682 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
683 }
684 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000685 return Result;
686 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000687
Chris Lattner98fa07b2003-05-23 20:03:32 +0000688 void remove(ConstantClass *CP) {
Chris Lattner3e650af2004-08-04 04:48:01 +0000689 MapIterator I = Map.find(MapKey((TypeClass*)CP->getRawType(),
690 getValType(CP)));
Chris Lattner20a4dab2004-08-04 22:26:13 +0000691 if (I == Map.end() || I->second != CP) {
692 // FIXME: This should not use a linear scan. If this gets to be a
693 // performance problem, someone should look at this.
694 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
695 /* empty */;
696 }
697
Chris Lattnerb50d1352003-10-05 00:17:43 +0000698 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000699 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000700
701 // Now that we found the entry, make sure this isn't the entry that
702 // the AbstractTypeMap points to.
703 const TypeClass *Ty = I->first.first;
704 if (Ty->isAbstract()) {
705 assert(AbstractTypeMap.count(Ty) &&
706 "Abstract type not in AbstractTypeMap?");
707 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
708 if (ATMEntryIt == I) {
709 // Yes, we are removing the representative entry for this type.
710 // See if there are any other entries of the same type.
711 MapIterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000712
Chris Lattnerb50d1352003-10-05 00:17:43 +0000713 // First check the entry before this one...
714 if (TmpIt != Map.begin()) {
715 --TmpIt;
716 if (TmpIt->first.first != Ty) // Not the same type, move back...
717 ++TmpIt;
718 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000719
Chris Lattnerb50d1352003-10-05 00:17:43 +0000720 // If we didn't find the same type, try to move forward...
721 if (TmpIt == ATMEntryIt) {
722 ++TmpIt;
723 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
724 --TmpIt; // No entry afterwards with the same type
725 }
726
727 // If there is another entry in the map of the same abstract type,
728 // update the AbstractTypeMap entry now.
729 if (TmpIt != ATMEntryIt) {
730 ATMEntryIt = TmpIt;
731 } else {
732 // Otherwise, we are removing the last instance of this type
733 // from the table. Remove from the ATM, and from user list.
734 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
735 AbstractTypeMap.erase(Ty);
736 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000737 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000738 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000739
Chris Lattnerb50d1352003-10-05 00:17:43 +0000740 Map.erase(I);
741 }
742
743 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000744 typename AbstractTypeMapTy::iterator I =
Chris Lattnerb50d1352003-10-05 00:17:43 +0000745 AbstractTypeMap.find(cast<TypeClass>(OldTy));
746
747 assert(I != AbstractTypeMap.end() &&
748 "Abstract type not in AbstractTypeMap?");
749
750 // Convert a constant at a time until the last one is gone. The last one
751 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
752 // eliminated eventually.
753 do {
754 ConvertConstantType<ConstantClass,
755 TypeClass>::convert(I->second->second,
756 cast<TypeClass>(NewTy));
757
758 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
759 } while (I != AbstractTypeMap.end());
760 }
761
762 // If the type became concrete without being refined to any other existing
763 // type, we just remove ourselves from the ATU list.
764 void typeBecameConcrete(const DerivedType *AbsTy) {
765 AbsTy->removeAbstractTypeUser(this);
766 }
767
768 void dump() const {
769 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000770 }
771 };
772}
773
Chris Lattner3462ae32001-12-03 22:26:30 +0000774//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000775//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000776static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
777static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000778
Chris Lattner3462ae32001-12-03 22:26:30 +0000779ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000780 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000781}
782
Chris Lattner3462ae32001-12-03 22:26:30 +0000783ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000784 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000785}
786
Chris Lattner3462ae32001-12-03 22:26:30 +0000787ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000788 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000789 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
790 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000791}
792
Chris Lattner3462ae32001-12-03 22:26:30 +0000793//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000794//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000795namespace llvm {
796 template<>
797 struct ConstantCreator<ConstantFP, Type, uint64_t> {
798 static ConstantFP *create(const Type *Ty, uint64_t V) {
799 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000800 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000801 }
802 };
803 template<>
804 struct ConstantCreator<ConstantFP, Type, uint32_t> {
805 static ConstantFP *create(const Type *Ty, uint32_t V) {
806 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000807 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000808 }
809 };
810}
811
812static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
813static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000814
Jim Laskey8ad8f712005-08-17 20:06:22 +0000815bool ConstantFP::isNullValue() const {
816 return DoubleToBits(Val) == 0;
817}
818
819bool ConstantFP::isExactlyValue(double V) const {
820 return DoubleToBits(V) == DoubleToBits(Val);
821}
822
823
Chris Lattner3462ae32001-12-03 22:26:30 +0000824ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000825 if (Ty == Type::FloatTy) {
826 // Force the value through memory to normalize it.
Jim Laskeyb74c6662005-08-17 19:34:49 +0000827 return FloatConstants.getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000828 } else {
829 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000830 return DoubleConstants.getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000831 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000832}
833
Chris Lattner9fba3da2004-02-15 05:53:04 +0000834//---- ConstantAggregateZero::get() implementation...
835//
836namespace llvm {
837 // ConstantAggregateZero does not take extra "value" argument...
838 template<class ValType>
839 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
840 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
841 return new ConstantAggregateZero(Ty);
842 }
843 };
844
845 template<>
846 struct ConvertConstantType<ConstantAggregateZero, Type> {
847 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
848 // Make everyone now use a constant of the new type...
849 Constant *New = ConstantAggregateZero::get(NewTy);
850 assert(New != OldC && "Didn't replace constant??");
851 OldC->uncheckedReplaceAllUsesWith(New);
852 OldC->destroyConstant(); // This constant is now dead, destroy it.
853 }
854 };
855}
856
857static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
858
Chris Lattner3e650af2004-08-04 04:48:01 +0000859static char getValType(ConstantAggregateZero *CPZ) { return 0; }
860
Chris Lattner9fba3da2004-02-15 05:53:04 +0000861Constant *ConstantAggregateZero::get(const Type *Ty) {
862 return AggZeroConstants.getOrCreate(Ty, 0);
863}
864
865// destroyConstant - Remove the constant from the constant table...
866//
867void ConstantAggregateZero::destroyConstant() {
868 AggZeroConstants.remove(this);
869 destroyConstantImpl();
870}
871
872void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
873 bool DisableChecking) {
874 assert(0 && "No uses!");
875 abort();
876}
877
878
879
Chris Lattner3462ae32001-12-03 22:26:30 +0000880//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000881//
Chris Lattner189d19f2003-11-21 20:23:48 +0000882namespace llvm {
883 template<>
884 struct ConvertConstantType<ConstantArray, ArrayType> {
885 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
886 // Make everyone now use a constant of the new type...
887 std::vector<Constant*> C;
888 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
889 C.push_back(cast<Constant>(OldC->getOperand(i)));
890 Constant *New = ConstantArray::get(NewTy, C);
891 assert(New != OldC && "Didn't replace constant??");
892 OldC->uncheckedReplaceAllUsesWith(New);
893 OldC->destroyConstant(); // This constant is now dead, destroy it.
894 }
895 };
896}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000897
Chris Lattner3e650af2004-08-04 04:48:01 +0000898static std::vector<Constant*> getValType(ConstantArray *CA) {
899 std::vector<Constant*> Elements;
900 Elements.reserve(CA->getNumOperands());
901 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
902 Elements.push_back(cast<Constant>(CA->getOperand(i)));
903 return Elements;
904}
905
Chris Lattner98fa07b2003-05-23 20:03:32 +0000906static ValueMap<std::vector<Constant*>, ArrayType,
907 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000908
Chris Lattner015e8212004-02-15 04:14:47 +0000909Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000910 const std::vector<Constant*> &V) {
911 // If this is an all-zero array, return a ConstantAggregateZero object
912 if (!V.empty()) {
913 Constant *C = V[0];
914 if (!C->isNullValue())
915 return ArrayConstants.getOrCreate(Ty, V);
916 for (unsigned i = 1, e = V.size(); i != e; ++i)
917 if (V[i] != C)
918 return ArrayConstants.getOrCreate(Ty, V);
919 }
920 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000921}
922
Chris Lattner98fa07b2003-05-23 20:03:32 +0000923// destroyConstant - Remove the constant from the constant table...
924//
925void ConstantArray::destroyConstant() {
926 ArrayConstants.remove(this);
927 destroyConstantImpl();
928}
929
Chris Lattner3462ae32001-12-03 22:26:30 +0000930// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000931// contain the specified string. A null terminator is added to the specified
932// string so that it may be used in a natural way...
933//
Chris Lattner015e8212004-02-15 04:14:47 +0000934Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000935 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000936
937 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000938 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000939
940 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000941 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000942
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000943 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000944 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000945}
946
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000947/// isString - This method returns true if the array is an array of sbyte or
948/// ubyte, and if the elements of the array are all ConstantInt's.
949bool ConstantArray::isString() const {
950 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000951 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000952 getType()->getElementType() != Type::SByteTy)
953 return false;
954 // Check the elements to make sure they are all integers, not constant
955 // expressions.
956 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
957 if (!isa<ConstantInt>(getOperand(i)))
958 return false;
959 return true;
960}
961
Chris Lattner81fabb02002-08-26 17:53:56 +0000962// getAsString - If the sub-element type of this array is either sbyte or ubyte,
963// then this method converts the array to an std::string and returns it.
964// Otherwise, it asserts out.
965//
966std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000967 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000968 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000969 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
970 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000971 return Result;
972}
973
974
Chris Lattner3462ae32001-12-03 22:26:30 +0000975//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000976//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000977
Chris Lattner189d19f2003-11-21 20:23:48 +0000978namespace llvm {
979 template<>
980 struct ConvertConstantType<ConstantStruct, StructType> {
981 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
982 // Make everyone now use a constant of the new type...
983 std::vector<Constant*> C;
984 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
985 C.push_back(cast<Constant>(OldC->getOperand(i)));
986 Constant *New = ConstantStruct::get(NewTy, C);
987 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000988
Chris Lattner189d19f2003-11-21 20:23:48 +0000989 OldC->uncheckedReplaceAllUsesWith(New);
990 OldC->destroyConstant(); // This constant is now dead, destroy it.
991 }
992 };
993}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000994
Misha Brukmanb1c93172005-04-21 23:48:37 +0000995static ValueMap<std::vector<Constant*>, StructType,
Chris Lattner98fa07b2003-05-23 20:03:32 +0000996 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000997
Chris Lattner3e650af2004-08-04 04:48:01 +0000998static std::vector<Constant*> getValType(ConstantStruct *CS) {
999 std::vector<Constant*> Elements;
1000 Elements.reserve(CS->getNumOperands());
1001 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1002 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1003 return Elements;
1004}
1005
Chris Lattner015e8212004-02-15 04:14:47 +00001006Constant *ConstantStruct::get(const StructType *Ty,
1007 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001008 // Create a ConstantAggregateZero value if all elements are zeros...
1009 for (unsigned i = 0, e = V.size(); i != e; ++i)
1010 if (!V[i]->isNullValue())
1011 return StructConstants.getOrCreate(Ty, V);
1012
1013 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001014}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001015
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001016Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1017 std::vector<const Type*> StructEls;
1018 StructEls.reserve(V.size());
1019 for (unsigned i = 0, e = V.size(); i != e; ++i)
1020 StructEls.push_back(V[i]->getType());
1021 return get(StructType::get(StructEls), V);
1022}
1023
Chris Lattnerd7a73302001-10-13 06:57:33 +00001024// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001025//
Chris Lattner3462ae32001-12-03 22:26:30 +00001026void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001027 StructConstants.remove(this);
1028 destroyConstantImpl();
1029}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001030
Brian Gaeke02209042004-08-20 06:00:58 +00001031//---- ConstantPacked::get() implementation...
1032//
1033namespace llvm {
1034 template<>
1035 struct ConvertConstantType<ConstantPacked, PackedType> {
1036 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1037 // Make everyone now use a constant of the new type...
1038 std::vector<Constant*> C;
1039 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1040 C.push_back(cast<Constant>(OldC->getOperand(i)));
1041 Constant *New = ConstantPacked::get(NewTy, C);
1042 assert(New != OldC && "Didn't replace constant??");
1043 OldC->uncheckedReplaceAllUsesWith(New);
1044 OldC->destroyConstant(); // This constant is now dead, destroy it.
1045 }
1046 };
1047}
1048
1049static std::vector<Constant*> getValType(ConstantPacked *CP) {
1050 std::vector<Constant*> Elements;
1051 Elements.reserve(CP->getNumOperands());
1052 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1053 Elements.push_back(CP->getOperand(i));
1054 return Elements;
1055}
1056
1057static ValueMap<std::vector<Constant*>, PackedType,
1058 ConstantPacked> PackedConstants;
1059
1060Constant *ConstantPacked::get(const PackedType *Ty,
1061 const std::vector<Constant*> &V) {
1062 // If this is an all-zero packed, return a ConstantAggregateZero object
1063 if (!V.empty()) {
1064 Constant *C = V[0];
1065 if (!C->isNullValue())
1066 return PackedConstants.getOrCreate(Ty, V);
1067 for (unsigned i = 1, e = V.size(); i != e; ++i)
1068 if (V[i] != C)
1069 return PackedConstants.getOrCreate(Ty, V);
1070 }
1071 return ConstantAggregateZero::get(Ty);
1072}
1073
1074Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1075 assert(!V.empty() && "Cannot infer type if V is empty");
1076 return get(PackedType::get(V.front()->getType(),V.size()), V);
1077}
1078
1079// destroyConstant - Remove the constant from the constant table...
1080//
1081void ConstantPacked::destroyConstant() {
1082 PackedConstants.remove(this);
1083 destroyConstantImpl();
1084}
1085
Chris Lattner3462ae32001-12-03 22:26:30 +00001086//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001087//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001088
Chris Lattner189d19f2003-11-21 20:23:48 +00001089namespace llvm {
1090 // ConstantPointerNull does not take extra "value" argument...
1091 template<class ValType>
1092 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1093 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1094 return new ConstantPointerNull(Ty);
1095 }
1096 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001097
Chris Lattner189d19f2003-11-21 20:23:48 +00001098 template<>
1099 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1100 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1101 // Make everyone now use a constant of the new type...
1102 Constant *New = ConstantPointerNull::get(NewTy);
1103 assert(New != OldC && "Didn't replace constant??");
1104 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 Lattner98fa07b2003-05-23 20:03:32 +00001110static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001111
Chris Lattner3e650af2004-08-04 04:48:01 +00001112static char getValType(ConstantPointerNull *) {
1113 return 0;
1114}
1115
1116
Chris Lattner3462ae32001-12-03 22:26:30 +00001117ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001118 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001119}
1120
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001121// destroyConstant - Remove the constant from the constant table...
1122//
1123void ConstantPointerNull::destroyConstant() {
1124 NullPtrConstants.remove(this);
1125 destroyConstantImpl();
1126}
1127
1128
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001129//---- UndefValue::get() implementation...
1130//
1131
1132namespace llvm {
1133 // UndefValue does not take extra "value" argument...
1134 template<class ValType>
1135 struct ConstantCreator<UndefValue, Type, ValType> {
1136 static UndefValue *create(const Type *Ty, const ValType &V) {
1137 return new UndefValue(Ty);
1138 }
1139 };
1140
1141 template<>
1142 struct ConvertConstantType<UndefValue, Type> {
1143 static void convert(UndefValue *OldC, const Type *NewTy) {
1144 // Make everyone now use a constant of the new type.
1145 Constant *New = UndefValue::get(NewTy);
1146 assert(New != OldC && "Didn't replace constant??");
1147 OldC->uncheckedReplaceAllUsesWith(New);
1148 OldC->destroyConstant(); // This constant is now dead, destroy it.
1149 }
1150 };
1151}
1152
1153static ValueMap<char, Type, UndefValue> UndefValueConstants;
1154
1155static char getValType(UndefValue *) {
1156 return 0;
1157}
1158
1159
1160UndefValue *UndefValue::get(const Type *Ty) {
1161 return UndefValueConstants.getOrCreate(Ty, 0);
1162}
1163
1164// destroyConstant - Remove the constant from the constant table.
1165//
1166void UndefValue::destroyConstant() {
1167 UndefValueConstants.remove(this);
1168 destroyConstantImpl();
1169}
1170
1171
1172
1173
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001174//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001175//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001176typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001177
Chris Lattner189d19f2003-11-21 20:23:48 +00001178namespace llvm {
1179 template<>
1180 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1181 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1182 if (V.first == Instruction::Cast)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001183 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
Chris Lattner189d19f2003-11-21 20:23:48 +00001184 if ((V.first >= Instruction::BinaryOpsBegin &&
1185 V.first < Instruction::BinaryOpsEnd) ||
1186 V.first == Instruction::Shl || V.first == Instruction::Shr)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001187 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001188 if (V.first == Instruction::Select)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001189 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001190
Chris Lattner189d19f2003-11-21 20:23:48 +00001191 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001192
Chris Lattner189d19f2003-11-21 20:23:48 +00001193 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001194 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001195 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001196 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001197
Chris Lattner189d19f2003-11-21 20:23:48 +00001198 template<>
1199 struct ConvertConstantType<ConstantExpr, Type> {
1200 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1201 Constant *New;
1202 switch (OldC->getOpcode()) {
1203 case Instruction::Cast:
1204 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1205 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001206 case Instruction::Select:
1207 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1208 OldC->getOperand(1),
1209 OldC->getOperand(2));
1210 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001211 case Instruction::Shl:
1212 case Instruction::Shr:
1213 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1214 OldC->getOperand(0), OldC->getOperand(1));
1215 break;
1216 default:
1217 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1218 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1219 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1220 OldC->getOperand(1));
1221 break;
1222 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001223 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001224 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1225 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001226 break;
1227 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001228
Chris Lattner189d19f2003-11-21 20:23:48 +00001229 assert(New != OldC && "Didn't replace constant??");
1230 OldC->uncheckedReplaceAllUsesWith(New);
1231 OldC->destroyConstant(); // This constant is now dead, destroy it.
1232 }
1233 };
1234} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001235
1236
Chris Lattner3e650af2004-08-04 04:48:01 +00001237static ExprMapKeyType getValType(ConstantExpr *CE) {
1238 std::vector<Constant*> Operands;
1239 Operands.reserve(CE->getNumOperands());
1240 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1241 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1242 return ExprMapKeyType(CE->getOpcode(), Operands);
1243}
1244
Chris Lattner98fa07b2003-05-23 20:03:32 +00001245static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001246
Chris Lattner46b3d302003-04-16 22:40:51 +00001247Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001248 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1249
Chris Lattneracdbe712003-04-17 19:24:48 +00001250 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1251 return FC; // Fold a few common cases...
1252
Vikram S. Adve4c485332002-07-15 18:19:33 +00001253 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001254 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001255 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1256 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001257}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001258
Chris Lattnerdd284742004-04-04 23:20:30 +00001259Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001260 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001261 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1262 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001263 if (C->getType() != Type::BoolTy) {
1264 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1265 return ConstantExpr::getCast(C, Ty);
1266 } else {
1267 if (C == ConstantBool::True)
1268 return ConstantIntegral::getAllOnesValue(Ty);
1269 else
1270 return ConstantIntegral::getNullValue(Ty);
1271 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001272}
1273
1274Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001275 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001276 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1277 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001278 if (C->getType() != Type::BoolTy)
1279 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001280 return ConstantExpr::getCast(C, Ty);
1281}
1282
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001283Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001284 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001285 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001286 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1287 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1288 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001289}
1290
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001291Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1292 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
1293 static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0));
1294
1295 return ConstantExpr::getGetElementPtr(C, Indices);
1296}
1297
Chris Lattnerb50d1352003-10-05 00:17:43 +00001298Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1299 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001300 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1301 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001302 // Check the operands for consistency first
1303 assert((Opcode >= Instruction::BinaryOpsBegin &&
1304 Opcode < Instruction::BinaryOpsEnd) &&
1305 "Invalid opcode in binary constant expression");
1306 assert(C1->getType() == C2->getType() &&
1307 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001308
Chris Lattner29ca2c62004-08-04 18:50:09 +00001309 if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) &&
1310 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001311 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1312 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001313
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001314 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001315 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001316 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001317}
1318
Chris Lattner29ca2c62004-08-04 18:50:09 +00001319Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001320#ifndef NDEBUG
1321 switch (Opcode) {
1322 case Instruction::Add: case Instruction::Sub:
1323 case Instruction::Mul: case Instruction::Div:
1324 case Instruction::Rem:
1325 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001326 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint()) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001327 "Tried to create an arithmetic operation on a non-arithmetic type!");
1328 break;
1329 case Instruction::And:
1330 case Instruction::Or:
1331 case Instruction::Xor:
1332 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1333 assert(C1->getType()->isIntegral() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001334 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001335 break;
1336 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1337 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1338 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1339 break;
1340 case Instruction::Shl:
1341 case Instruction::Shr:
1342 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
1343 assert(C1->getType()->isInteger() &&
1344 "Tried to create a shift operation on a non-integer type!");
1345 break;
1346 default:
1347 break;
1348 }
1349#endif
1350
Chris Lattner29ca2c62004-08-04 18:50:09 +00001351 if (Instruction::isRelational(Opcode))
1352 return getTy(Type::BoolTy, Opcode, C1, C2);
1353 else
1354 return getTy(C1->getType(), Opcode, C1, C2);
1355}
1356
Chris Lattner6e415c02004-03-12 05:54:04 +00001357Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1358 Constant *V1, Constant *V2) {
1359 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1360 assert(V1->getType() == V2->getType() && "Select value types must match!");
1361 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1362
1363 if (ReqTy == V1->getType())
1364 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1365 return SC; // Fold common cases
1366
1367 std::vector<Constant*> argVec(3, C);
1368 argVec[1] = V1;
1369 argVec[2] = V2;
1370 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1371 return ExprConstants.getOrCreate(ReqTy, Key);
1372}
1373
Chris Lattner9eb2b522004-01-12 19:12:58 +00001374/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001375Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1376 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001377 // Check the operands for consistency first
1378 assert((Opcode == Instruction::Shl ||
1379 Opcode == Instruction::Shr) &&
1380 "Invalid opcode in binary constant expression");
1381 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1382 "Invalid operand types for Shift constant expr!");
1383
Chris Lattner0bba7712004-01-12 20:40:42 +00001384 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001385 return FC; // Fold a few common cases...
1386
1387 // Look up the constant in the table first to ensure uniqueness
1388 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001389 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001390 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001391}
1392
1393
Chris Lattnerb50d1352003-10-05 00:17:43 +00001394Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001395 const std::vector<Value*> &IdxList) {
1396 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001397 "GEP indices invalid!");
1398
Chris Lattneracdbe712003-04-17 19:24:48 +00001399 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1400 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001401
Chris Lattnerb50d1352003-10-05 00:17:43 +00001402 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001403 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001404 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001405 std::vector<Constant*> ArgVec;
1406 ArgVec.reserve(IdxList.size()+1);
1407 ArgVec.push_back(C);
1408 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1409 ArgVec.push_back(cast<Constant>(IdxList[i]));
1410 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001411 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001412}
1413
Chris Lattnerb50d1352003-10-05 00:17:43 +00001414Constant *ConstantExpr::getGetElementPtr(Constant *C,
1415 const std::vector<Constant*> &IdxList){
1416 // Get the result type of the getelementptr!
1417 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1418
1419 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1420 true);
1421 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001422 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1423}
1424
1425Constant *ConstantExpr::getGetElementPtr(Constant *C,
1426 const std::vector<Value*> &IdxList) {
1427 // Get the result type of the getelementptr!
1428 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1429 true);
1430 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001431 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1432}
1433
1434
Vikram S. Adve4c485332002-07-15 18:19:33 +00001435// destroyConstant - Remove the constant from the constant table...
1436//
1437void ConstantExpr::destroyConstant() {
1438 ExprConstants.remove(this);
1439 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001440}
1441
Chris Lattner3cd8c562002-07-30 18:54:25 +00001442const char *ConstantExpr::getOpcodeName() const {
1443 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001444}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001445
Chris Lattner99a669b2004-11-19 16:39:44 +00001446/// clearAllValueMaps - This method frees all internal memory used by the
1447/// constant subsystem, which can be used in environments where this memory
1448/// is otherwise reported as a leak.
1449void Constant::clearAllValueMaps() {
1450 std::vector<Constant *> Constants;
1451
1452 DoubleConstants.clear(Constants);
1453 FloatConstants.clear(Constants);
1454 SIntConstants.clear(Constants);
1455 UIntConstants.clear(Constants);
1456 AggZeroConstants.clear(Constants);
1457 ArrayConstants.clear(Constants);
1458 StructConstants.clear(Constants);
1459 PackedConstants.clear(Constants);
1460 NullPtrConstants.clear(Constants);
1461 UndefValueConstants.clear(Constants);
1462 ExprConstants.clear(Constants);
1463
Misha Brukmanb1c93172005-04-21 23:48:37 +00001464 for (std::vector<Constant *>::iterator I = Constants.begin(),
Chris Lattner99a669b2004-11-19 16:39:44 +00001465 E = Constants.end(); I != E; ++I)
1466 (*I)->dropAllReferences();
1467 for (std::vector<Constant *>::iterator I = Constants.begin(),
1468 E = Constants.end(); I != E; ++I)
1469 (*I)->destroyConstantImpl();
1470 Constants.clear();
1471}