blob: 7af188d7352c8efe8d6e78a6b35b07fc6efe4b18 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner5a945e32004-01-12 21:13:12 +000015#include "ConstantFolding.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000022#include <algorithm>
Reid Spencercf394bf2004-07-04 11:51:24 +000023#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner3462ae32001-12-03 22:26:30 +000026ConstantBool *ConstantBool::True = new ConstantBool(true);
27ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000028
Chris Lattner9655e542001-07-20 19:16:02 +000029
Chris Lattner2f7c9632001-06-06 20:29:01 +000030//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000031// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000032//===----------------------------------------------------------------------===//
33
34// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000035void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000036 assert(ST && "Type::setName - Must provide symbol table argument!");
37
38 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000039}
40
Chris Lattner3462ae32001-12-03 22:26:30 +000041void Constant::destroyConstantImpl() {
42 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000043 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000044 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000045 // but they don't know that. Because we only find out when the CPV is
46 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000047 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000048 //
49 while (!use_empty()) {
50 Value *V = use_back();
51#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000052 if (!isa<Constant>(V))
53 std::cerr << "While deleting: " << *this
54 << "\n\nUse still stuck around after Def is destroyed: "
55 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000056#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000057 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000058 Constant *CV = cast<Constant>(V);
59 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000060
61 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000062 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000063 }
64
65 // Value has no outstanding references it is safe to delete it now...
66 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000067}
Chris Lattner2f7c9632001-06-06 20:29:01 +000068
Chris Lattnerb1585a92002-08-13 17:50:20 +000069// Static constructor to create a '0' constant of arbitrary type...
70Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000071 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000072 case Type::BoolTyID: {
73 static Constant *NullBool = ConstantBool::get(false);
74 return NullBool;
75 }
76 case Type::SByteTyID: {
77 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
78 return NullSByte;
79 }
80 case Type::UByteTyID: {
81 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
82 return NullUByte;
83 }
84 case Type::ShortTyID: {
85 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
86 return NullShort;
87 }
88 case Type::UShortTyID: {
89 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
90 return NullUShort;
91 }
92 case Type::IntTyID: {
93 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
94 return NullInt;
95 }
96 case Type::UIntTyID: {
97 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
98 return NullUInt;
99 }
100 case Type::LongTyID: {
101 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
102 return NullLong;
103 }
104 case Type::ULongTyID: {
105 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
106 return NullULong;
107 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000108
Chris Lattner3e88ef92003-10-03 19:34:51 +0000109 case Type::FloatTyID: {
110 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
111 return NullFloat;
112 }
113 case Type::DoubleTyID: {
114 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
115 return NullDouble;
116 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000117
118 case Type::PointerTyID:
119 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000120
Chris Lattner9fba3da2004-02-15 05:53:04 +0000121 case Type::StructTyID:
122 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000123 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000124 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000125 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000126 // Function, Label, or Opaque type?
127 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000128 return 0;
129 }
130}
131
132// Static constructor to create the maximum constant of an integral type...
133ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000134 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000135 case Type::BoolTyID: return ConstantBool::True;
136 case Type::SByteTyID:
137 case Type::ShortTyID:
138 case Type::IntTyID:
139 case Type::LongTyID: {
140 // Calculate 011111111111111...
141 unsigned TypeBits = Ty->getPrimitiveSize()*8;
142 int64_t Val = INT64_MAX; // All ones
143 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
144 return ConstantSInt::get(Ty, Val);
145 }
146
147 case Type::UByteTyID:
148 case Type::UShortTyID:
149 case Type::UIntTyID:
150 case Type::ULongTyID: return getAllOnesValue(Ty);
151
Chris Lattner31408f72002-08-14 17:12:13 +0000152 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000153 }
154}
155
156// Static constructor to create the minimum constant for an integral type...
157ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000158 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000159 case Type::BoolTyID: return ConstantBool::False;
160 case Type::SByteTyID:
161 case Type::ShortTyID:
162 case Type::IntTyID:
163 case Type::LongTyID: {
164 // Calculate 1111111111000000000000
165 unsigned TypeBits = Ty->getPrimitiveSize()*8;
166 int64_t Val = -1; // All ones
167 Val <<= TypeBits-1; // Shift over to the right spot
168 return ConstantSInt::get(Ty, Val);
169 }
170
171 case Type::UByteTyID:
172 case Type::UShortTyID:
173 case Type::UIntTyID:
174 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
175
Chris Lattner31408f72002-08-14 17:12:13 +0000176 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000177 }
178}
179
180// Static constructor to create an integral constant with all bits set
181ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000182 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000183 case Type::BoolTyID: return ConstantBool::True;
184 case Type::SByteTyID:
185 case Type::ShortTyID:
186 case Type::IntTyID:
187 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
188
189 case Type::UByteTyID:
190 case Type::UShortTyID:
191 case Type::UIntTyID:
192 case Type::ULongTyID: {
193 // Calculate ~0 of the right type...
194 unsigned TypeBits = Ty->getPrimitiveSize()*8;
195 uint64_t Val = ~0ULL; // All ones
196 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
197 return ConstantUInt::get(Ty, Val);
198 }
Chris Lattner31408f72002-08-14 17:12:13 +0000199 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000200 }
201}
202
Chris Lattner83e5d392003-03-10 22:39:02 +0000203bool ConstantUInt::isAllOnesValue() const {
204 unsigned TypeBits = getType()->getPrimitiveSize()*8;
205 uint64_t Val = ~0ULL; // All ones
206 Val >>= 64-TypeBits; // Shift out inappropriate bits
207 return getValue() == Val;
208}
209
Chris Lattnerb1585a92002-08-13 17:50:20 +0000210
Chris Lattner2f7c9632001-06-06 20:29:01 +0000211//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000212// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213//===----------------------------------------------------------------------===//
214
215//===----------------------------------------------------------------------===//
216// Normal Constructors
217
Chris Lattner265eb642004-06-21 12:12:12 +0000218ConstantIntegral::ConstantIntegral(const Type *Ty, uint64_t V)
219 : Constant(Ty) {
220 Val.Unsigned = V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221}
Chris Lattner49d855c2001-09-07 16:46:31 +0000222
Chris Lattner265eb642004-06-21 12:12:12 +0000223ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy, V) {
224}
225
226ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty, V) {
Chris Lattner7309d662001-07-21 19:16:08 +0000227}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228
Chris Lattner3462ae32001-12-03 22:26:30 +0000229ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000230 assert(Ty->isInteger() && Ty->isSigned() &&
231 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000232 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233}
234
Chris Lattner3462ae32001-12-03 22:26:30 +0000235ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000236 assert(Ty->isInteger() && Ty->isUnsigned() &&
237 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000238 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000239}
240
Chris Lattner3462ae32001-12-03 22:26:30 +0000241ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000242 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000243 Val = V;
244}
245
Chris Lattner3462ae32001-12-03 22:26:30 +0000246ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000247 const std::vector<Constant*> &V) : Constant(T) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000248 assert(V.size() == T->getNumElements() &&
249 "Invalid initializer vector for constant array");
Chris Lattner0d779712002-10-08 23:33:52 +0000250 Operands.reserve(V.size());
251 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000252 assert((V[i]->getType() == T->getElementType() ||
253 (T->isAbstract() &&
254 V[i]->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
255 "Initializer for array element doesn't match array element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000256 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000257 }
258}
259
Chris Lattner3462ae32001-12-03 22:26:30 +0000260ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000261 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000262 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000263 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000264 Operands.reserve(V.size());
265 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000266 assert((V[i]->getType() == T->getElementType(i) ||
267 ((T->getElementType(i)->isAbstract() ||
268 V[i]->getType()->isAbstract()) &&
Chris Lattner6b727592004-06-17 18:19:28 +0000269 T->getElementType(i)->getTypeID() == V[i]->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000270 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000271 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000272 }
273}
274
Brian Gaeke02209042004-08-20 06:00:58 +0000275ConstantPacked::ConstantPacked(const PackedType *T,
276 const std::vector<Constant*> &V) : Constant(T) {
277 Operands.reserve(V.size());
278 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000279 assert((V[i]->getType() == T->getElementType() ||
280 (T->isAbstract() &&
281 V[i]->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
282 "Initializer for packed element doesn't match packed element type!");
Brian Gaeke02209042004-08-20 06:00:58 +0000283 Operands.push_back(Use(V[i], this));
284 }
285}
286
Chris Lattner3cd8c562002-07-30 18:54:25 +0000287ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Chris Lattner5230e702004-07-19 00:59:10 +0000288 : Constant(Ty, ConstantExprVal), iType(Opcode) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000289 Operands.reserve(1);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000290 Operands.push_back(Use(C, this));
291}
292
Chris Lattner6e415c02004-03-12 05:54:04 +0000293// Select instruction creation ctor
294ConstantExpr::ConstantExpr(Constant *C, Constant *V1, Constant *V2)
Chris Lattner5230e702004-07-19 00:59:10 +0000295 : Constant(V1->getType(), ConstantExprVal), iType(Instruction::Select) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000296 Operands.reserve(3);
297 Operands.push_back(Use(C, this));
298 Operands.push_back(Use(V1, this));
299 Operands.push_back(Use(V2, this));
300}
301
302
Chris Lattner22ced562003-06-22 20:48:30 +0000303static bool isSetCC(unsigned Opcode) {
304 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
305 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
306 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
307}
308
Chris Lattner3cd8c562002-07-30 18:54:25 +0000309ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner5230e702004-07-19 00:59:10 +0000310 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType(), ConstantExprVal),
311 iType(Opcode) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000312 Operands.reserve(2);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000313 Operands.push_back(Use(C1, this));
314 Operands.push_back(Use(C2, this));
315}
316
Chris Lattner3cd8c562002-07-30 18:54:25 +0000317ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
318 const Type *DestTy)
Chris Lattner5230e702004-07-19 00:59:10 +0000319 : Constant(DestTy, ConstantExprVal), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000320 Operands.reserve(1+IdxList.size());
321 Operands.push_back(Use(C, this));
322 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
323 Operands.push_back(Use(IdxList[i], this));
324}
325
Chris Lattner817175f2004-03-29 02:37:53 +0000326/// ConstantExpr::get* - Return some common constants without having to
327/// specify the full Instruction::OPCODE identifier.
328///
329Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000330 if (!C->getType()->isFloatingPoint())
331 return get(Instruction::Sub, getNullValue(C->getType()), C);
332 else
333 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000334}
335Constant *ConstantExpr::getNot(Constant *C) {
336 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
337 return get(Instruction::Xor, C,
338 ConstantIntegral::getAllOnesValue(C->getType()));
339}
340Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
341 return get(Instruction::Add, C1, C2);
342}
343Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
344 return get(Instruction::Sub, C1, C2);
345}
346Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
347 return get(Instruction::Mul, C1, C2);
348}
349Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
350 return get(Instruction::Div, C1, C2);
351}
352Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
353 return get(Instruction::Rem, C1, C2);
354}
355Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
356 return get(Instruction::And, C1, C2);
357}
358Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
359 return get(Instruction::Or, C1, C2);
360}
361Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
362 return get(Instruction::Xor, C1, C2);
363}
364Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
365 return get(Instruction::SetEQ, C1, C2);
366}
367Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
368 return get(Instruction::SetNE, C1, C2);
369}
370Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
371 return get(Instruction::SetLT, C1, C2);
372}
373Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
374 return get(Instruction::SetGT, C1, C2);
375}
376Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
377 return get(Instruction::SetLE, C1, C2);
378}
379Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
380 return get(Instruction::SetGE, C1, C2);
381}
382Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
383 return get(Instruction::Shl, C1, C2);
384}
385Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
386 return get(Instruction::Shr, C1, C2);
387}
388
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000389Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
390 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
391 return getCast(getShr(getCast(C1,
392 C1->getType()->getUnsignedVersion()), C2), C1->getType());
393}
Chris Lattner817175f2004-03-29 02:37:53 +0000394
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000395Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
396 if (C1->getType()->isSigned()) return getShr(C1, C2);
397 return getCast(getShr(getCast(C1,
398 C1->getType()->getSignedVersion()), C2), C1->getType());
399}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000400
Chris Lattner2f7c9632001-06-06 20:29:01 +0000401
402//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000403// isValueValidForType implementations
404
Chris Lattner3462ae32001-12-03 22:26:30 +0000405bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000406 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000407 default:
408 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000409 // Signed types...
410 case Type::SByteTyID:
411 return (Val <= INT8_MAX && Val >= INT8_MIN);
412 case Type::ShortTyID:
413 return (Val <= INT16_MAX && Val >= INT16_MIN);
414 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000415 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000416 case Type::LongTyID:
417 return true; // This is the largest type...
418 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000419}
420
Chris Lattner3462ae32001-12-03 22:26:30 +0000421bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000422 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000423 default:
424 return false; // These can't be represented as integers!!!
425
426 // Unsigned types...
427 case Type::UByteTyID:
428 return (Val <= UINT8_MAX);
429 case Type::UShortTyID:
430 return (Val <= UINT16_MAX);
431 case Type::UIntTyID:
432 return (Val <= UINT32_MAX);
433 case Type::ULongTyID:
434 return true; // This is the largest type...
435 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000436}
437
Chris Lattner3462ae32001-12-03 22:26:30 +0000438bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000439 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000440 default:
441 return false; // These can't be represented as floating point!
442
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000443 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000445 case Type::DoubleTyID:
446 return true; // This is the largest type...
447 }
448};
Chris Lattner9655e542001-07-20 19:16:02 +0000449
Chris Lattner49d855c2001-09-07 16:46:31 +0000450//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000451// replaceUsesOfWithOnConstant implementations
452
Chris Lattnerc27038d2003-08-29 05:36:46 +0000453void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
454 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000455 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
456
457 std::vector<Constant*> Values;
Alkis Evlogimenosf0cc8142004-08-04 08:02:59 +0000458 Values.reserve(getNumOperands()); // Build replacement array...
459 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
460 Constant *Val = getOperand(i);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000461 if (Val == From) Val = cast<Constant>(To);
462 Values.push_back(Val);
463 }
464
Chris Lattnerc75bf522004-02-15 04:05:58 +0000465 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000466 assert(Replacement != this && "I didn't contain From!");
467
468 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000469 if (DisableChecking)
470 uncheckedReplaceAllUsesWith(Replacement);
471 else
472 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000473
474 // Delete the old constant!
475 destroyConstant();
476}
477
Chris Lattnerc27038d2003-08-29 05:36:46 +0000478void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
479 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000480 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
481
482 std::vector<Constant*> Values;
Alkis Evlogimenosf0cc8142004-08-04 08:02:59 +0000483 Values.reserve(getNumOperands()); // Build replacement array...
484 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
485 Constant *Val = getOperand(i);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000486 if (Val == From) Val = cast<Constant>(To);
487 Values.push_back(Val);
488 }
489
Chris Lattner37a716f2004-02-15 04:07:32 +0000490 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000491 assert(Replacement != this && "I didn't contain From!");
492
493 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000494 if (DisableChecking)
495 uncheckedReplaceAllUsesWith(Replacement);
496 else
497 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000498
499 // Delete the old constant!
500 destroyConstant();
501}
502
Brian Gaeke02209042004-08-20 06:00:58 +0000503void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
504 bool DisableChecking) {
505 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
506
507 std::vector<Constant*> Values;
508 Values.reserve(getNumOperands()); // Build replacement array...
509 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
510 Constant *Val = getOperand(i);
511 if (Val == From) Val = cast<Constant>(To);
512 Values.push_back(Val);
513 }
514
515 Constant *Replacement = ConstantPacked::get(getType(), Values);
516 assert(Replacement != this && "I didn't contain From!");
517
518 // Everyone using this now uses the replacement...
519 if (DisableChecking)
520 uncheckedReplaceAllUsesWith(Replacement);
521 else
522 replaceAllUsesWith(Replacement);
523
524 // Delete the old constant!
525 destroyConstant();
526}
527
Chris Lattnerc27038d2003-08-29 05:36:46 +0000528void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
529 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000530 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
531 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000532
Chris Lattner46b3d302003-04-16 22:40:51 +0000533 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000534 if (getOpcode() == Instruction::GetElementPtr) {
535 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000536 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000537 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000538 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000539
540 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000541 Constant *Val = getOperand(i);
542 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000543 Indices.push_back(Val);
544 }
545 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
546 } else if (getOpcode() == Instruction::Cast) {
547 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000548 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattner467cb2b2004-03-31 02:56:11 +0000549 } else if (getOpcode() == Instruction::Select) {
550 Constant *C1 = getOperand(0);
551 Constant *C2 = getOperand(1);
552 Constant *C3 = getOperand(2);
553 if (C1 == From) C1 = To;
554 if (C2 == From) C2 = To;
555 if (C3 == From) C3 = To;
556 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000557 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000558 Constant *C1 = getOperand(0);
559 Constant *C2 = getOperand(1);
560 if (C1 == From) C1 = To;
561 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000562 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
563 } else {
564 assert(0 && "Unknown ConstantExpr type!");
565 return;
566 }
567
568 assert(Replacement != this && "I didn't contain From!");
569
570 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000571 if (DisableChecking)
572 uncheckedReplaceAllUsesWith(Replacement);
573 else
574 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000575
576 // Delete the old constant!
577 destroyConstant();
578}
579
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000580//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000581// Factory Function Implementation
582
Chris Lattner98fa07b2003-05-23 20:03:32 +0000583// ConstantCreator - A class that is used to create constants by
584// ValueMap*. This class should be partially specialized if there is
585// something strange that needs to be done to interface to the ctor for the
586// constant.
587//
Chris Lattner189d19f2003-11-21 20:23:48 +0000588namespace llvm {
589 template<class ConstantClass, class TypeClass, class ValType>
590 struct ConstantCreator {
591 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
592 return new ConstantClass(Ty, V);
593 }
594 };
595
596 template<class ConstantClass, class TypeClass>
597 struct ConvertConstantType {
598 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
599 assert(0 && "This type cannot be converted!\n");
600 abort();
601 }
602 };
603}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000604
Chris Lattner98fa07b2003-05-23 20:03:32 +0000605namespace {
606 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000607 class ValueMap : public AbstractTypeUser {
608 typedef std::pair<const TypeClass*, ValType> MapKey;
609 typedef std::map<MapKey, ConstantClass *> MapTy;
610 typedef typename MapTy::iterator MapIterator;
611 MapTy Map;
612
613 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
614 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000615
616 friend void Constant::clearAllValueMaps();
617 private:
618 void clear(std::vector<Constant *> &Constants) {
619 for(MapIterator I = Map.begin(); I != Map.end(); ++I)
620 Constants.push_back(I->second);
621 Map.clear();
622 AbstractTypeMap.clear();
623 }
624
Chris Lattner98fa07b2003-05-23 20:03:32 +0000625 public:
626 // getOrCreate - Return the specified constant from the map, creating it if
627 // necessary.
628 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000629 MapKey Lookup(Ty, V);
630 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000631 if (I != Map.end() && I->first == Lookup)
632 return I->second; // Is it in the map?
633
634 // If no preexisting value, create one now...
635 ConstantClass *Result =
636 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
637
Chris Lattnerb50d1352003-10-05 00:17:43 +0000638
639 /// FIXME: why does this assert fail when loading 176.gcc?
640 //assert(Result->getType() == Ty && "Type specified is not correct!");
641 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
642
643 // If the type of the constant is abstract, make sure that an entry exists
644 // for it in the AbstractTypeMap.
645 if (Ty->isAbstract()) {
646 typename AbstractTypeMapTy::iterator TI =
647 AbstractTypeMap.lower_bound(Ty);
648
649 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
650 // Add ourselves to the ATU list of the type.
651 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
652
653 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
654 }
655 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000656 return Result;
657 }
658
659 void remove(ConstantClass *CP) {
Chris Lattner3e650af2004-08-04 04:48:01 +0000660 MapIterator I = Map.find(MapKey((TypeClass*)CP->getRawType(),
661 getValType(CP)));
Chris Lattner20a4dab2004-08-04 22:26:13 +0000662 if (I == Map.end() || I->second != CP) {
663 // FIXME: This should not use a linear scan. If this gets to be a
664 // performance problem, someone should look at this.
665 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
666 /* empty */;
667 }
668
Chris Lattnerb50d1352003-10-05 00:17:43 +0000669 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000670 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000671
672 // Now that we found the entry, make sure this isn't the entry that
673 // the AbstractTypeMap points to.
674 const TypeClass *Ty = I->first.first;
675 if (Ty->isAbstract()) {
676 assert(AbstractTypeMap.count(Ty) &&
677 "Abstract type not in AbstractTypeMap?");
678 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
679 if (ATMEntryIt == I) {
680 // Yes, we are removing the representative entry for this type.
681 // See if there are any other entries of the same type.
682 MapIterator TmpIt = ATMEntryIt;
683
684 // First check the entry before this one...
685 if (TmpIt != Map.begin()) {
686 --TmpIt;
687 if (TmpIt->first.first != Ty) // Not the same type, move back...
688 ++TmpIt;
689 }
690
691 // If we didn't find the same type, try to move forward...
692 if (TmpIt == ATMEntryIt) {
693 ++TmpIt;
694 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
695 --TmpIt; // No entry afterwards with the same type
696 }
697
698 // If there is another entry in the map of the same abstract type,
699 // update the AbstractTypeMap entry now.
700 if (TmpIt != ATMEntryIt) {
701 ATMEntryIt = TmpIt;
702 } else {
703 // Otherwise, we are removing the last instance of this type
704 // from the table. Remove from the ATM, and from user list.
705 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
706 AbstractTypeMap.erase(Ty);
707 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000708 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000709 }
710
711 Map.erase(I);
712 }
713
714 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
715 typename AbstractTypeMapTy::iterator I =
716 AbstractTypeMap.find(cast<TypeClass>(OldTy));
717
718 assert(I != AbstractTypeMap.end() &&
719 "Abstract type not in AbstractTypeMap?");
720
721 // Convert a constant at a time until the last one is gone. The last one
722 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
723 // eliminated eventually.
724 do {
725 ConvertConstantType<ConstantClass,
726 TypeClass>::convert(I->second->second,
727 cast<TypeClass>(NewTy));
728
729 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
730 } while (I != AbstractTypeMap.end());
731 }
732
733 // If the type became concrete without being refined to any other existing
734 // type, we just remove ourselves from the ATU list.
735 void typeBecameConcrete(const DerivedType *AbsTy) {
736 AbsTy->removeAbstractTypeUser(this);
737 }
738
739 void dump() const {
740 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000741 }
742 };
743}
744
Chris Lattner3462ae32001-12-03 22:26:30 +0000745//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000746//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000747static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
748static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000749
Chris Lattner3462ae32001-12-03 22:26:30 +0000750ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000751 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000752}
753
Chris Lattner3462ae32001-12-03 22:26:30 +0000754ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000755 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000756}
757
Chris Lattner3462ae32001-12-03 22:26:30 +0000758ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000759 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000760 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
761 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000762}
763
Chris Lattner3462ae32001-12-03 22:26:30 +0000764//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000765//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000766namespace llvm {
767 template<>
768 struct ConstantCreator<ConstantFP, Type, uint64_t> {
769 static ConstantFP *create(const Type *Ty, uint64_t V) {
770 assert(Ty == Type::DoubleTy);
771 union {
772 double F;
773 uint64_t I;
774 } T;
775 T.I = V;
776 return new ConstantFP(Ty, T.F);
777 }
778 };
779 template<>
780 struct ConstantCreator<ConstantFP, Type, uint32_t> {
781 static ConstantFP *create(const Type *Ty, uint32_t V) {
782 assert(Ty == Type::FloatTy);
783 union {
784 float F;
785 uint32_t I;
786 } T;
787 T.I = V;
788 return new ConstantFP(Ty, T.F);
789 }
790 };
791}
792
793static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
794static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000795
Chris Lattner3462ae32001-12-03 22:26:30 +0000796ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000797 if (Ty == Type::FloatTy) {
798 // Force the value through memory to normalize it.
Chris Lattnerac80ea42004-02-01 22:49:04 +0000799 union {
800 float F;
801 uint32_t I;
802 } T;
803 T.F = (float)V;
804 return FloatConstants.getOrCreate(Ty, T.I);
805 } else {
806 assert(Ty == Type::DoubleTy);
807 union {
808 double F;
809 uint64_t I;
810 } T;
811 T.F = V;
812 return DoubleConstants.getOrCreate(Ty, T.I);
Chris Lattner241ed4c2004-01-23 00:55:21 +0000813 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000814}
815
Chris Lattner9fba3da2004-02-15 05:53:04 +0000816//---- ConstantAggregateZero::get() implementation...
817//
818namespace llvm {
819 // ConstantAggregateZero does not take extra "value" argument...
820 template<class ValType>
821 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
822 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
823 return new ConstantAggregateZero(Ty);
824 }
825 };
826
827 template<>
828 struct ConvertConstantType<ConstantAggregateZero, Type> {
829 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
830 // Make everyone now use a constant of the new type...
831 Constant *New = ConstantAggregateZero::get(NewTy);
832 assert(New != OldC && "Didn't replace constant??");
833 OldC->uncheckedReplaceAllUsesWith(New);
834 OldC->destroyConstant(); // This constant is now dead, destroy it.
835 }
836 };
837}
838
839static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
840
Chris Lattner3e650af2004-08-04 04:48:01 +0000841static char getValType(ConstantAggregateZero *CPZ) { return 0; }
842
Chris Lattner9fba3da2004-02-15 05:53:04 +0000843Constant *ConstantAggregateZero::get(const Type *Ty) {
844 return AggZeroConstants.getOrCreate(Ty, 0);
845}
846
847// destroyConstant - Remove the constant from the constant table...
848//
849void ConstantAggregateZero::destroyConstant() {
850 AggZeroConstants.remove(this);
851 destroyConstantImpl();
852}
853
854void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
855 bool DisableChecking) {
856 assert(0 && "No uses!");
857 abort();
858}
859
860
861
Chris Lattner3462ae32001-12-03 22:26:30 +0000862//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000863//
Chris Lattner189d19f2003-11-21 20:23:48 +0000864namespace llvm {
865 template<>
866 struct ConvertConstantType<ConstantArray, ArrayType> {
867 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
868 // Make everyone now use a constant of the new type...
869 std::vector<Constant*> C;
870 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
871 C.push_back(cast<Constant>(OldC->getOperand(i)));
872 Constant *New = ConstantArray::get(NewTy, C);
873 assert(New != OldC && "Didn't replace constant??");
874 OldC->uncheckedReplaceAllUsesWith(New);
875 OldC->destroyConstant(); // This constant is now dead, destroy it.
876 }
877 };
878}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000879
Chris Lattner3e650af2004-08-04 04:48:01 +0000880static std::vector<Constant*> getValType(ConstantArray *CA) {
881 std::vector<Constant*> Elements;
882 Elements.reserve(CA->getNumOperands());
883 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
884 Elements.push_back(cast<Constant>(CA->getOperand(i)));
885 return Elements;
886}
887
Chris Lattner98fa07b2003-05-23 20:03:32 +0000888static ValueMap<std::vector<Constant*>, ArrayType,
889 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000890
Chris Lattner015e8212004-02-15 04:14:47 +0000891Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000892 const std::vector<Constant*> &V) {
893 // If this is an all-zero array, return a ConstantAggregateZero object
894 if (!V.empty()) {
895 Constant *C = V[0];
896 if (!C->isNullValue())
897 return ArrayConstants.getOrCreate(Ty, V);
898 for (unsigned i = 1, e = V.size(); i != e; ++i)
899 if (V[i] != C)
900 return ArrayConstants.getOrCreate(Ty, V);
901 }
902 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000903}
904
Chris Lattner98fa07b2003-05-23 20:03:32 +0000905// destroyConstant - Remove the constant from the constant table...
906//
907void ConstantArray::destroyConstant() {
908 ArrayConstants.remove(this);
909 destroyConstantImpl();
910}
911
Chris Lattner3462ae32001-12-03 22:26:30 +0000912// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000913// contain the specified string. A null terminator is added to the specified
914// string so that it may be used in a natural way...
915//
Chris Lattner015e8212004-02-15 04:14:47 +0000916Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000917 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000918
919 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000920 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000921
922 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000923 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000924
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000925 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000926 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000927}
928
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000929/// isString - This method returns true if the array is an array of sbyte or
930/// ubyte, and if the elements of the array are all ConstantInt's.
931bool ConstantArray::isString() const {
932 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000933 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000934 getType()->getElementType() != Type::SByteTy)
935 return false;
936 // Check the elements to make sure they are all integers, not constant
937 // expressions.
938 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
939 if (!isa<ConstantInt>(getOperand(i)))
940 return false;
941 return true;
942}
943
Chris Lattner81fabb02002-08-26 17:53:56 +0000944// getAsString - If the sub-element type of this array is either sbyte or ubyte,
945// then this method converts the array to an std::string and returns it.
946// Otherwise, it asserts out.
947//
948std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000949 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000950 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000951 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
952 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000953 return Result;
954}
955
956
Chris Lattner3462ae32001-12-03 22:26:30 +0000957//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000958//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000959
Chris Lattner189d19f2003-11-21 20:23:48 +0000960namespace llvm {
961 template<>
962 struct ConvertConstantType<ConstantStruct, StructType> {
963 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
964 // Make everyone now use a constant of the new type...
965 std::vector<Constant*> C;
966 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
967 C.push_back(cast<Constant>(OldC->getOperand(i)));
968 Constant *New = ConstantStruct::get(NewTy, C);
969 assert(New != OldC && "Didn't replace constant??");
970
971 OldC->uncheckedReplaceAllUsesWith(New);
972 OldC->destroyConstant(); // This constant is now dead, destroy it.
973 }
974 };
975}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000976
Chris Lattner98fa07b2003-05-23 20:03:32 +0000977static ValueMap<std::vector<Constant*>, StructType,
978 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000979
Chris Lattner3e650af2004-08-04 04:48:01 +0000980static std::vector<Constant*> getValType(ConstantStruct *CS) {
981 std::vector<Constant*> Elements;
982 Elements.reserve(CS->getNumOperands());
983 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
984 Elements.push_back(cast<Constant>(CS->getOperand(i)));
985 return Elements;
986}
987
Chris Lattner015e8212004-02-15 04:14:47 +0000988Constant *ConstantStruct::get(const StructType *Ty,
989 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000990 // Create a ConstantAggregateZero value if all elements are zeros...
991 for (unsigned i = 0, e = V.size(); i != e; ++i)
992 if (!V[i]->isNullValue())
993 return StructConstants.getOrCreate(Ty, V);
994
995 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000996}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000997
Chris Lattnerd6108ca2004-07-12 20:35:11 +0000998Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
999 std::vector<const Type*> StructEls;
1000 StructEls.reserve(V.size());
1001 for (unsigned i = 0, e = V.size(); i != e; ++i)
1002 StructEls.push_back(V[i]->getType());
1003 return get(StructType::get(StructEls), V);
1004}
1005
Chris Lattnerd7a73302001-10-13 06:57:33 +00001006// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001007//
Chris Lattner3462ae32001-12-03 22:26:30 +00001008void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001009 StructConstants.remove(this);
1010 destroyConstantImpl();
1011}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001012
Brian Gaeke02209042004-08-20 06:00:58 +00001013//---- ConstantPacked::get() implementation...
1014//
1015namespace llvm {
1016 template<>
1017 struct ConvertConstantType<ConstantPacked, PackedType> {
1018 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1019 // Make everyone now use a constant of the new type...
1020 std::vector<Constant*> C;
1021 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1022 C.push_back(cast<Constant>(OldC->getOperand(i)));
1023 Constant *New = ConstantPacked::get(NewTy, C);
1024 assert(New != OldC && "Didn't replace constant??");
1025 OldC->uncheckedReplaceAllUsesWith(New);
1026 OldC->destroyConstant(); // This constant is now dead, destroy it.
1027 }
1028 };
1029}
1030
1031static std::vector<Constant*> getValType(ConstantPacked *CP) {
1032 std::vector<Constant*> Elements;
1033 Elements.reserve(CP->getNumOperands());
1034 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1035 Elements.push_back(CP->getOperand(i));
1036 return Elements;
1037}
1038
1039static ValueMap<std::vector<Constant*>, PackedType,
1040 ConstantPacked> PackedConstants;
1041
1042Constant *ConstantPacked::get(const PackedType *Ty,
1043 const std::vector<Constant*> &V) {
1044 // If this is an all-zero packed, return a ConstantAggregateZero object
1045 if (!V.empty()) {
1046 Constant *C = V[0];
1047 if (!C->isNullValue())
1048 return PackedConstants.getOrCreate(Ty, V);
1049 for (unsigned i = 1, e = V.size(); i != e; ++i)
1050 if (V[i] != C)
1051 return PackedConstants.getOrCreate(Ty, V);
1052 }
1053 return ConstantAggregateZero::get(Ty);
1054}
1055
1056Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1057 assert(!V.empty() && "Cannot infer type if V is empty");
1058 return get(PackedType::get(V.front()->getType(),V.size()), V);
1059}
1060
1061// destroyConstant - Remove the constant from the constant table...
1062//
1063void ConstantPacked::destroyConstant() {
1064 PackedConstants.remove(this);
1065 destroyConstantImpl();
1066}
1067
Chris Lattner3462ae32001-12-03 22:26:30 +00001068//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001069//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001070
Chris Lattner189d19f2003-11-21 20:23:48 +00001071namespace llvm {
1072 // ConstantPointerNull does not take extra "value" argument...
1073 template<class ValType>
1074 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1075 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1076 return new ConstantPointerNull(Ty);
1077 }
1078 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001079
Chris Lattner189d19f2003-11-21 20:23:48 +00001080 template<>
1081 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1082 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1083 // Make everyone now use a constant of the new type...
1084 Constant *New = ConstantPointerNull::get(NewTy);
1085 assert(New != OldC && "Didn't replace constant??");
1086 OldC->uncheckedReplaceAllUsesWith(New);
1087 OldC->destroyConstant(); // This constant is now dead, destroy it.
1088 }
1089 };
1090}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001091
Chris Lattner98fa07b2003-05-23 20:03:32 +00001092static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001093
Chris Lattner3e650af2004-08-04 04:48:01 +00001094static char getValType(ConstantPointerNull *) {
1095 return 0;
1096}
1097
1098
Chris Lattner3462ae32001-12-03 22:26:30 +00001099ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001100 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001101}
1102
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001103// destroyConstant - Remove the constant from the constant table...
1104//
1105void ConstantPointerNull::destroyConstant() {
1106 NullPtrConstants.remove(this);
1107 destroyConstantImpl();
1108}
1109
1110
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001111//---- UndefValue::get() implementation...
1112//
1113
1114namespace llvm {
1115 // UndefValue does not take extra "value" argument...
1116 template<class ValType>
1117 struct ConstantCreator<UndefValue, Type, ValType> {
1118 static UndefValue *create(const Type *Ty, const ValType &V) {
1119 return new UndefValue(Ty);
1120 }
1121 };
1122
1123 template<>
1124 struct ConvertConstantType<UndefValue, Type> {
1125 static void convert(UndefValue *OldC, const Type *NewTy) {
1126 // Make everyone now use a constant of the new type.
1127 Constant *New = UndefValue::get(NewTy);
1128 assert(New != OldC && "Didn't replace constant??");
1129 OldC->uncheckedReplaceAllUsesWith(New);
1130 OldC->destroyConstant(); // This constant is now dead, destroy it.
1131 }
1132 };
1133}
1134
1135static ValueMap<char, Type, UndefValue> UndefValueConstants;
1136
1137static char getValType(UndefValue *) {
1138 return 0;
1139}
1140
1141
1142UndefValue *UndefValue::get(const Type *Ty) {
1143 return UndefValueConstants.getOrCreate(Ty, 0);
1144}
1145
1146// destroyConstant - Remove the constant from the constant table.
1147//
1148void UndefValue::destroyConstant() {
1149 UndefValueConstants.remove(this);
1150 destroyConstantImpl();
1151}
1152
1153
1154
1155
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001156//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001157//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001158typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001159
Chris Lattner189d19f2003-11-21 20:23:48 +00001160namespace llvm {
1161 template<>
1162 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1163 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1164 if (V.first == Instruction::Cast)
1165 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
1166 if ((V.first >= Instruction::BinaryOpsBegin &&
1167 V.first < Instruction::BinaryOpsEnd) ||
1168 V.first == Instruction::Shl || V.first == Instruction::Shr)
1169 return new ConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001170 if (V.first == Instruction::Select)
1171 return new ConstantExpr(V.second[0], V.second[1], V.second[2]);
Chris Lattner189d19f2003-11-21 20:23:48 +00001172
1173 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
1174
1175 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
1176 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001177 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001178 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001179
Chris Lattner189d19f2003-11-21 20:23:48 +00001180 template<>
1181 struct ConvertConstantType<ConstantExpr, Type> {
1182 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1183 Constant *New;
1184 switch (OldC->getOpcode()) {
1185 case Instruction::Cast:
1186 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1187 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001188 case Instruction::Select:
1189 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1190 OldC->getOperand(1),
1191 OldC->getOperand(2));
1192 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001193 case Instruction::Shl:
1194 case Instruction::Shr:
1195 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1196 OldC->getOperand(0), OldC->getOperand(1));
1197 break;
1198 default:
1199 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1200 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1201 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1202 OldC->getOperand(1));
1203 break;
1204 case Instruction::GetElementPtr:
1205 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001206 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1207 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001208 break;
1209 }
1210
1211 assert(New != OldC && "Didn't replace constant??");
1212 OldC->uncheckedReplaceAllUsesWith(New);
1213 OldC->destroyConstant(); // This constant is now dead, destroy it.
1214 }
1215 };
1216} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001217
1218
Chris Lattner3e650af2004-08-04 04:48:01 +00001219static ExprMapKeyType getValType(ConstantExpr *CE) {
1220 std::vector<Constant*> Operands;
1221 Operands.reserve(CE->getNumOperands());
1222 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1223 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1224 return ExprMapKeyType(CE->getOpcode(), Operands);
1225}
1226
Chris Lattner98fa07b2003-05-23 20:03:32 +00001227static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001228
Chris Lattner46b3d302003-04-16 22:40:51 +00001229Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001230 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1231
Chris Lattneracdbe712003-04-17 19:24:48 +00001232 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1233 return FC; // Fold a few common cases...
1234
Vikram S. Adve4c485332002-07-15 18:19:33 +00001235 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001236 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001237 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1238 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001239}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001240
Chris Lattnerdd284742004-04-04 23:20:30 +00001241Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001242 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001243 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1244 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001245 if (C->getType() != Type::BoolTy) {
1246 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1247 return ConstantExpr::getCast(C, Ty);
1248 } else {
1249 if (C == ConstantBool::True)
1250 return ConstantIntegral::getAllOnesValue(Ty);
1251 else
1252 return ConstantIntegral::getNullValue(Ty);
1253 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001254}
1255
1256Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001257 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001258 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1259 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001260 if (C->getType() != Type::BoolTy)
1261 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001262 return ConstantExpr::getCast(C, Ty);
1263}
1264
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001265Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001266 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001267 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001268 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1269 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1270 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001271}
1272
Chris Lattnerb50d1352003-10-05 00:17:43 +00001273Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1274 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001275 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1276 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001277 // Check the operands for consistency first
1278 assert((Opcode >= Instruction::BinaryOpsBegin &&
1279 Opcode < Instruction::BinaryOpsEnd) &&
1280 "Invalid opcode in binary constant expression");
1281 assert(C1->getType() == C2->getType() &&
1282 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001283
Chris Lattner29ca2c62004-08-04 18:50:09 +00001284 if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) &&
1285 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001286 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1287 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001288
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001289 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001290 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001291 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001292}
1293
Chris Lattner29ca2c62004-08-04 18:50:09 +00001294Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001295#ifndef NDEBUG
1296 switch (Opcode) {
1297 case Instruction::Add: case Instruction::Sub:
1298 case Instruction::Mul: case Instruction::Div:
1299 case Instruction::Rem:
1300 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1301 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint()) &&
1302 "Tried to create an arithmetic operation on a non-arithmetic type!");
1303 break;
1304 case Instruction::And:
1305 case Instruction::Or:
1306 case Instruction::Xor:
1307 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1308 assert(C1->getType()->isIntegral() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001309 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001310 break;
1311 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1312 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1313 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1314 break;
1315 case Instruction::Shl:
1316 case Instruction::Shr:
1317 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
1318 assert(C1->getType()->isInteger() &&
1319 "Tried to create a shift operation on a non-integer type!");
1320 break;
1321 default:
1322 break;
1323 }
1324#endif
1325
Chris Lattner29ca2c62004-08-04 18:50:09 +00001326 if (Instruction::isRelational(Opcode))
1327 return getTy(Type::BoolTy, Opcode, C1, C2);
1328 else
1329 return getTy(C1->getType(), Opcode, C1, C2);
1330}
1331
Chris Lattner6e415c02004-03-12 05:54:04 +00001332Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1333 Constant *V1, Constant *V2) {
1334 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1335 assert(V1->getType() == V2->getType() && "Select value types must match!");
1336 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1337
1338 if (ReqTy == V1->getType())
1339 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1340 return SC; // Fold common cases
1341
1342 std::vector<Constant*> argVec(3, C);
1343 argVec[1] = V1;
1344 argVec[2] = V2;
1345 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1346 return ExprConstants.getOrCreate(ReqTy, Key);
1347}
1348
Chris Lattner9eb2b522004-01-12 19:12:58 +00001349/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001350Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1351 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001352 // Check the operands for consistency first
1353 assert((Opcode == Instruction::Shl ||
1354 Opcode == Instruction::Shr) &&
1355 "Invalid opcode in binary constant expression");
1356 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1357 "Invalid operand types for Shift constant expr!");
1358
Chris Lattner0bba7712004-01-12 20:40:42 +00001359 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001360 return FC; // Fold a few common cases...
1361
1362 // Look up the constant in the table first to ensure uniqueness
1363 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001364 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001365 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001366}
1367
1368
Chris Lattnerb50d1352003-10-05 00:17:43 +00001369Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001370 const std::vector<Value*> &IdxList) {
1371 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001372 "GEP indices invalid!");
1373
Chris Lattneracdbe712003-04-17 19:24:48 +00001374 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1375 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001376
Chris Lattnerb50d1352003-10-05 00:17:43 +00001377 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001378 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001379 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001380 std::vector<Constant*> ArgVec;
1381 ArgVec.reserve(IdxList.size()+1);
1382 ArgVec.push_back(C);
1383 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1384 ArgVec.push_back(cast<Constant>(IdxList[i]));
1385 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001386 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001387}
1388
Chris Lattnerb50d1352003-10-05 00:17:43 +00001389Constant *ConstantExpr::getGetElementPtr(Constant *C,
1390 const std::vector<Constant*> &IdxList){
1391 // Get the result type of the getelementptr!
1392 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1393
1394 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1395 true);
1396 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001397 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1398}
1399
1400Constant *ConstantExpr::getGetElementPtr(Constant *C,
1401 const std::vector<Value*> &IdxList) {
1402 // Get the result type of the getelementptr!
1403 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1404 true);
1405 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001406 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1407}
1408
1409
Vikram S. Adve4c485332002-07-15 18:19:33 +00001410// destroyConstant - Remove the constant from the constant table...
1411//
1412void ConstantExpr::destroyConstant() {
1413 ExprConstants.remove(this);
1414 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001415}
1416
Chris Lattner3cd8c562002-07-30 18:54:25 +00001417const char *ConstantExpr::getOpcodeName() const {
1418 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001419}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001420
Chris Lattner99a669b2004-11-19 16:39:44 +00001421/// clearAllValueMaps - This method frees all internal memory used by the
1422/// constant subsystem, which can be used in environments where this memory
1423/// is otherwise reported as a leak.
1424void Constant::clearAllValueMaps() {
1425 std::vector<Constant *> Constants;
1426
1427 DoubleConstants.clear(Constants);
1428 FloatConstants.clear(Constants);
1429 SIntConstants.clear(Constants);
1430 UIntConstants.clear(Constants);
1431 AggZeroConstants.clear(Constants);
1432 ArrayConstants.clear(Constants);
1433 StructConstants.clear(Constants);
1434 PackedConstants.clear(Constants);
1435 NullPtrConstants.clear(Constants);
1436 UndefValueConstants.clear(Constants);
1437 ExprConstants.clear(Constants);
1438
1439 for (std::vector<Constant *>::iterator I = Constants.begin(),
1440 E = Constants.end(); I != E; ++I)
1441 (*I)->dropAllReferences();
1442 for (std::vector<Constant *>::iterator I = Constants.begin(),
1443 E = Constants.end(); I != E; ++I)
1444 (*I)->destroyConstantImpl();
1445 Constants.clear();
1446}