blob: 3308b9a6d40e1c4aee8b4e2c8b6975dce621e3d1 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner5a945e32004-01-12 21:13:12 +000015#include "ConstantFolding.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/ADT/StringExtras.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000023#include "llvm/Support/Compiler.h"
Chris Lattner69edc982006-09-28 00:35:06 +000024#include "llvm/Support/ManagedStatic.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000025#include <algorithm>
Reid Spencercf394bf2004-07-04 11:51:24 +000026#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner3462ae32001-12-03 22:26:30 +000029ConstantBool *ConstantBool::True = new ConstantBool(true);
30ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000031
Chris Lattner9655e542001-07-20 19:16:02 +000032
Chris Lattner2f7c9632001-06-06 20:29:01 +000033//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000034// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000035//===----------------------------------------------------------------------===//
36
Chris Lattner3462ae32001-12-03 22:26:30 +000037void Constant::destroyConstantImpl() {
38 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000039 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000040 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000041 // but they don't know that. Because we only find out when the CPV is
42 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000043 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000044 //
45 while (!use_empty()) {
46 Value *V = use_back();
47#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000048 if (!isa<Constant>(V))
49 std::cerr << "While deleting: " << *this
50 << "\n\nUse still stuck around after Def is destroyed: "
51 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000052#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000053 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000054 Constant *CV = cast<Constant>(V);
55 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000056
57 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000058 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000059 }
60
61 // Value has no outstanding references it is safe to delete it now...
62 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000063}
Chris Lattner2f7c9632001-06-06 20:29:01 +000064
Chris Lattnerb1585a92002-08-13 17:50:20 +000065// Static constructor to create a '0' constant of arbitrary type...
66Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000067 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000068 case Type::BoolTyID: {
69 static Constant *NullBool = ConstantBool::get(false);
70 return NullBool;
71 }
72 case Type::SByteTyID: {
73 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
74 return NullSByte;
75 }
76 case Type::UByteTyID: {
77 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
78 return NullUByte;
79 }
80 case Type::ShortTyID: {
81 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
82 return NullShort;
83 }
84 case Type::UShortTyID: {
85 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
86 return NullUShort;
87 }
88 case Type::IntTyID: {
89 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
90 return NullInt;
91 }
92 case Type::UIntTyID: {
93 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
94 return NullUInt;
95 }
96 case Type::LongTyID: {
97 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
98 return NullLong;
99 }
100 case Type::ULongTyID: {
101 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
102 return NullULong;
103 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000104
Chris Lattner3e88ef92003-10-03 19:34:51 +0000105 case Type::FloatTyID: {
106 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
107 return NullFloat;
108 }
109 case Type::DoubleTyID: {
110 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
111 return NullDouble;
112 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000113
Misha Brukmanb1c93172005-04-21 23:48:37 +0000114 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000115 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000116
Chris Lattner9fba3da2004-02-15 05:53:04 +0000117 case Type::StructTyID:
118 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000119 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000120 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000121 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000122 // Function, Label, or Opaque type?
123 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000124 return 0;
125 }
126}
127
128// Static constructor to create the maximum constant of an integral type...
129ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000130 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000131 case Type::BoolTyID: return ConstantBool::True;
132 case Type::SByteTyID:
133 case Type::ShortTyID:
134 case Type::IntTyID:
135 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000136 // Calculate 011111111111111...
Chris Lattnerb1585a92002-08-13 17:50:20 +0000137 unsigned TypeBits = Ty->getPrimitiveSize()*8;
138 int64_t Val = INT64_MAX; // All ones
139 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
140 return ConstantSInt::get(Ty, Val);
141 }
142
143 case Type::UByteTyID:
144 case Type::UShortTyID:
145 case Type::UIntTyID:
146 case Type::ULongTyID: return getAllOnesValue(Ty);
147
Chris Lattner31408f72002-08-14 17:12:13 +0000148 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000149 }
150}
151
152// Static constructor to create the minimum constant for an integral type...
153ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000154 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000155 case Type::BoolTyID: return ConstantBool::False;
156 case Type::SByteTyID:
157 case Type::ShortTyID:
158 case Type::IntTyID:
159 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000160 // Calculate 1111111111000000000000
Chris Lattnerb1585a92002-08-13 17:50:20 +0000161 unsigned TypeBits = Ty->getPrimitiveSize()*8;
162 int64_t Val = -1; // All ones
163 Val <<= TypeBits-1; // Shift over to the right spot
164 return ConstantSInt::get(Ty, Val);
165 }
166
167 case Type::UByteTyID:
168 case Type::UShortTyID:
169 case Type::UIntTyID:
170 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
171
Chris Lattner31408f72002-08-14 17:12:13 +0000172 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000173 }
174}
175
176// Static constructor to create an integral constant with all bits set
177ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000178 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000179 case Type::BoolTyID: return ConstantBool::True;
180 case Type::SByteTyID:
181 case Type::ShortTyID:
182 case Type::IntTyID:
183 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
184
185 case Type::UByteTyID:
186 case Type::UShortTyID:
187 case Type::UIntTyID:
188 case Type::ULongTyID: {
189 // Calculate ~0 of the right type...
190 unsigned TypeBits = Ty->getPrimitiveSize()*8;
191 uint64_t Val = ~0ULL; // All ones
192 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
193 return ConstantUInt::get(Ty, Val);
194 }
Chris Lattner31408f72002-08-14 17:12:13 +0000195 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000196 }
197}
198
Chris Lattner83e5d392003-03-10 22:39:02 +0000199bool ConstantUInt::isAllOnesValue() const {
200 unsigned TypeBits = getType()->getPrimitiveSize()*8;
201 uint64_t Val = ~0ULL; // All ones
202 Val >>= 64-TypeBits; // Shift out inappropriate bits
203 return getValue() == Val;
204}
205
Chris Lattnerb1585a92002-08-13 17:50:20 +0000206
Chris Lattner2f7c9632001-06-06 20:29:01 +0000207//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000208// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209//===----------------------------------------------------------------------===//
210
211//===----------------------------------------------------------------------===//
212// Normal Constructors
213
Chris Lattnere7e139e2005-09-27 06:09:08 +0000214ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V)
215 : Constant(Ty, VT, 0, 0) {
Chris Lattner265eb642004-06-21 12:12:12 +0000216 Val.Unsigned = V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000217}
Chris Lattner49d855c2001-09-07 16:46:31 +0000218
Chris Lattnere7e139e2005-09-27 06:09:08 +0000219ConstantBool::ConstantBool(bool V)
220 : ConstantIntegral(Type::BoolTy, ConstantBoolVal, V) {
Chris Lattner265eb642004-06-21 12:12:12 +0000221}
222
Chris Lattnere7e139e2005-09-27 06:09:08 +0000223ConstantInt::ConstantInt(const Type *Ty, ValueTy VT, uint64_t V)
224 : ConstantIntegral(Ty, VT, V) {
Chris Lattner7309d662001-07-21 19:16:08 +0000225}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000226
Chris Lattnere7e139e2005-09-27 06:09:08 +0000227ConstantSInt::ConstantSInt(const Type *Ty, int64_t V)
228 : ConstantInt(Ty, ConstantSIntVal, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000229 assert(Ty->isInteger() && Ty->isSigned() &&
Reid Spencerf064bb22005-03-09 15:19:41 +0000230 "Illegal type for signed integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000231 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000232}
233
Chris Lattnere7e139e2005-09-27 06:09:08 +0000234ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V)
235 : ConstantInt(Ty, ConstantUIntVal, 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 Lattnerd0df99c2005-01-29 00:34:39 +0000241ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000242 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000243 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000244 Val = V;
245}
246
Chris Lattner3462ae32001-12-03 22:26:30 +0000247ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000248 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000249 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000250 assert(V.size() == T->getNumElements() &&
251 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000252 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000253 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
254 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000255 Constant *C = *I;
256 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000257 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000258 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000259 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000260 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000261 }
262}
263
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000264ConstantArray::~ConstantArray() {
265 delete [] OperandList;
266}
267
Chris Lattner3462ae32001-12-03 22:26:30 +0000268ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000269 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000270 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000271 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000272 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000273 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000274 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
275 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000276 Constant *C = *I;
277 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000278 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000279 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000280 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000281 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000282 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000283 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000284 }
285}
286
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000287ConstantStruct::~ConstantStruct() {
288 delete [] OperandList;
289}
290
291
Brian Gaeke02209042004-08-20 06:00:58 +0000292ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000293 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000294 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000295 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000296 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
297 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000298 Constant *C = *I;
299 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000300 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000301 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000302 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000303 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000304 }
305}
306
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000307ConstantPacked::~ConstantPacked() {
308 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000309}
310
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000311/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
312/// behind the scenes to implement unary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000313namespace {
314class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000315 Use Op;
316public:
317 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
318 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
319};
Chris Lattner02157b02006-06-28 21:38:54 +0000320}
Chris Lattner6e415c02004-03-12 05:54:04 +0000321
Chris Lattner22ced562003-06-22 20:48:30 +0000322static bool isSetCC(unsigned Opcode) {
323 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
324 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
325 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
326}
327
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000328/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
329/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000330namespace {
331class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000332 Use Ops[2];
333public:
334 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
335 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
336 Opcode, Ops, 2) {
337 Ops[0].init(C1, this);
338 Ops[1].init(C2, this);
339 }
340};
Chris Lattner02157b02006-06-28 21:38:54 +0000341}
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000342
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000343/// SelectConstantExpr - This class is private to Constants.cpp, and is used
344/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000345namespace {
346class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000347 Use Ops[3];
348public:
349 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
350 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
351 Ops[0].init(C1, this);
352 Ops[1].init(C2, this);
353 Ops[2].init(C3, this);
354 }
355};
Chris Lattner02157b02006-06-28 21:38:54 +0000356}
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000357
Robert Bocchinoca27f032006-01-17 20:07:22 +0000358/// ExtractElementConstantExpr - This class is private to
359/// Constants.cpp, and is used behind the scenes to implement
360/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000361namespace {
362class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000363 Use Ops[2];
364public:
365 ExtractElementConstantExpr(Constant *C1, Constant *C2)
366 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(),
367 Instruction::ExtractElement, Ops, 2) {
368 Ops[0].init(C1, this);
369 Ops[1].init(C2, this);
370 }
371};
Chris Lattner02157b02006-06-28 21:38:54 +0000372}
Robert Bocchino23004482006-01-10 19:05:34 +0000373
Robert Bocchinoca27f032006-01-17 20:07:22 +0000374/// InsertElementConstantExpr - This class is private to
375/// Constants.cpp, and is used behind the scenes to implement
376/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000377namespace {
378class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000379 Use Ops[3];
380public:
381 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
382 : ConstantExpr(C1->getType(), Instruction::InsertElement,
383 Ops, 3) {
384 Ops[0].init(C1, this);
385 Ops[1].init(C2, this);
386 Ops[2].init(C3, this);
387 }
388};
Chris Lattner02157b02006-06-28 21:38:54 +0000389}
Robert Bocchinoca27f032006-01-17 20:07:22 +0000390
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000391/// ShuffleVectorConstantExpr - This class is private to
392/// Constants.cpp, and is used behind the scenes to implement
393/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000394namespace {
395class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000396 Use Ops[3];
397public:
398 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
399 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
400 Ops, 3) {
401 Ops[0].init(C1, this);
402 Ops[1].init(C2, this);
403 Ops[2].init(C3, this);
404 }
405};
Chris Lattner02157b02006-06-28 21:38:54 +0000406}
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000407
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000408/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
409/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000410namespace {
411struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000412 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
413 const Type *DestTy)
414 : ConstantExpr(DestTy, Instruction::GetElementPtr,
415 new Use[IdxList.size()+1], IdxList.size()+1) {
416 OperandList[0].init(C, this);
417 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
418 OperandList[i+1].init(IdxList[i], this);
419 }
420 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000421 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000422 }
423};
Chris Lattner02157b02006-06-28 21:38:54 +0000424}
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000425
Chris Lattner817175f2004-03-29 02:37:53 +0000426/// ConstantExpr::get* - Return some common constants without having to
427/// specify the full Instruction::OPCODE identifier.
428///
429Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000430 if (!C->getType()->isFloatingPoint())
431 return get(Instruction::Sub, getNullValue(C->getType()), C);
432 else
433 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000434}
435Constant *ConstantExpr::getNot(Constant *C) {
436 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
437 return get(Instruction::Xor, C,
438 ConstantIntegral::getAllOnesValue(C->getType()));
439}
440Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
441 return get(Instruction::Add, C1, C2);
442}
443Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
444 return get(Instruction::Sub, C1, C2);
445}
446Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
447 return get(Instruction::Mul, C1, C2);
448}
449Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
450 return get(Instruction::Div, C1, C2);
451}
452Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
453 return get(Instruction::Rem, C1, C2);
454}
455Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
456 return get(Instruction::And, C1, C2);
457}
458Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
459 return get(Instruction::Or, C1, C2);
460}
461Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
462 return get(Instruction::Xor, C1, C2);
463}
464Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
465 return get(Instruction::SetEQ, C1, C2);
466}
467Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
468 return get(Instruction::SetNE, C1, C2);
469}
470Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
471 return get(Instruction::SetLT, C1, C2);
472}
473Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
474 return get(Instruction::SetGT, C1, C2);
475}
476Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
477 return get(Instruction::SetLE, C1, C2);
478}
479Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
480 return get(Instruction::SetGE, C1, C2);
481}
482Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
483 return get(Instruction::Shl, C1, C2);
484}
485Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
486 return get(Instruction::Shr, C1, C2);
487}
488
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000489Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
490 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
491 return getCast(getShr(getCast(C1,
492 C1->getType()->getUnsignedVersion()), C2), C1->getType());
493}
Chris Lattner817175f2004-03-29 02:37:53 +0000494
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000495Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
496 if (C1->getType()->isSigned()) return getShr(C1, C2);
497 return getCast(getShr(getCast(C1,
498 C1->getType()->getSignedVersion()), C2), C1->getType());
499}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000500
Chris Lattner7c1018a2006-07-14 19:37:40 +0000501/// getWithOperandReplaced - Return a constant expression identical to this
502/// one, but with the specified operand set to the specified value.
503Constant *ConstantExpr::getWithOperandReplaced(unsigned OpNo,
504 Constant *Op) const {
505 assert(OpNo < getNumOperands() && "Operand num is out of range!");
506 assert(Op->getType() == getOperand(OpNo)->getType() &&
507 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000508 if (getOperand(OpNo) == Op)
509 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000510
Chris Lattner227816342006-07-14 22:20:01 +0000511 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000512 switch (getOpcode()) {
513 case Instruction::Cast:
514 return ConstantExpr::getCast(Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000515 case Instruction::Select:
516 Op0 = (OpNo == 0) ? Op : getOperand(0);
517 Op1 = (OpNo == 1) ? Op : getOperand(1);
518 Op2 = (OpNo == 2) ? Op : getOperand(2);
519 return ConstantExpr::getSelect(Op0, Op1, Op2);
520 case Instruction::InsertElement:
521 Op0 = (OpNo == 0) ? Op : getOperand(0);
522 Op1 = (OpNo == 1) ? Op : getOperand(1);
523 Op2 = (OpNo == 2) ? Op : getOperand(2);
524 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
525 case Instruction::ExtractElement:
526 Op0 = (OpNo == 0) ? Op : getOperand(0);
527 Op1 = (OpNo == 1) ? Op : getOperand(1);
528 return ConstantExpr::getExtractElement(Op0, Op1);
529 case Instruction::ShuffleVector:
530 Op0 = (OpNo == 0) ? Op : getOperand(0);
531 Op1 = (OpNo == 1) ? Op : getOperand(1);
532 Op2 = (OpNo == 2) ? Op : getOperand(2);
533 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000534 case Instruction::GetElementPtr: {
535 std::vector<Constant*> Ops;
536 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
537 Ops.push_back(getOperand(i));
538 if (OpNo == 0)
539 return ConstantExpr::getGetElementPtr(Op, Ops);
540 Ops[OpNo-1] = Op;
541 return ConstantExpr::getGetElementPtr(getOperand(0), Ops);
542 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000543 default:
544 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000545 Op0 = (OpNo == 0) ? Op : getOperand(0);
546 Op1 = (OpNo == 1) ? Op : getOperand(1);
547 return ConstantExpr::get(getOpcode(), Op0, Op1);
548 }
549}
550
551/// getWithOperands - This returns the current constant expression with the
552/// operands replaced with the specified values. The specified operands must
553/// match count and type with the existing ones.
554Constant *ConstantExpr::
555getWithOperands(const std::vector<Constant*> &Ops) const {
556 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
557 bool AnyChange = false;
558 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
559 assert(Ops[i]->getType() == getOperand(i)->getType() &&
560 "Operand type mismatch!");
561 AnyChange |= Ops[i] != getOperand(i);
562 }
563 if (!AnyChange) // No operands changed, return self.
564 return const_cast<ConstantExpr*>(this);
565
566 switch (getOpcode()) {
567 case Instruction::Cast:
568 return ConstantExpr::getCast(Ops[0], getType());
569 case Instruction::Select:
570 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
571 case Instruction::InsertElement:
572 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
573 case Instruction::ExtractElement:
574 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
575 case Instruction::ShuffleVector:
576 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
577 case Instruction::GetElementPtr: {
578 std::vector<Constant*> ActualOps(Ops.begin()+1, Ops.end());
579 return ConstantExpr::getGetElementPtr(Ops[0], ActualOps);
580 }
581 default:
582 assert(getNumOperands() == 2 && "Must be binary operator?");
583 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000584 }
585}
586
Chris Lattner2f7c9632001-06-06 20:29:01 +0000587
588//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000589// isValueValidForType implementations
590
Chris Lattner3462ae32001-12-03 22:26:30 +0000591bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000592 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000593 default:
594 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000595 // Signed types...
596 case Type::SByteTyID:
597 return (Val <= INT8_MAX && Val >= INT8_MIN);
598 case Type::ShortTyID:
599 return (Val <= INT16_MAX && Val >= INT16_MIN);
600 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000601 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000602 case Type::LongTyID:
603 return true; // This is the largest type...
604 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000605}
606
Chris Lattner3462ae32001-12-03 22:26:30 +0000607bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000608 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000609 default:
610 return false; // These can't be represented as integers!!!
611
612 // Unsigned types...
613 case Type::UByteTyID:
614 return (Val <= UINT8_MAX);
615 case Type::UShortTyID:
616 return (Val <= UINT16_MAX);
617 case Type::UIntTyID:
618 return (Val <= UINT32_MAX);
619 case Type::ULongTyID:
620 return true; // This is the largest type...
621 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000622}
623
Chris Lattner3462ae32001-12-03 22:26:30 +0000624bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000625 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000626 default:
627 return false; // These can't be represented as floating point!
628
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000629 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000630 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000631 case Type::DoubleTyID:
632 return true; // This is the largest type...
633 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000634}
Chris Lattner9655e542001-07-20 19:16:02 +0000635
Chris Lattner49d855c2001-09-07 16:46:31 +0000636//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000637// Factory Function Implementation
638
Chris Lattner98fa07b2003-05-23 20:03:32 +0000639// ConstantCreator - A class that is used to create constants by
640// ValueMap*. This class should be partially specialized if there is
641// something strange that needs to be done to interface to the ctor for the
642// constant.
643//
Chris Lattner189d19f2003-11-21 20:23:48 +0000644namespace llvm {
645 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000646 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000647 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
648 return new ConstantClass(Ty, V);
649 }
650 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000651
Chris Lattner189d19f2003-11-21 20:23:48 +0000652 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000653 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000654 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
655 assert(0 && "This type cannot be converted!\n");
656 abort();
657 }
658 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000659
Chris Lattner935aa922005-10-04 17:48:46 +0000660 template<class ValType, class TypeClass, class ConstantClass,
661 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000662 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000663 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000664 typedef std::pair<const Type*, ValType> MapKey;
665 typedef std::map<MapKey, Constant *> MapTy;
666 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
667 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000668 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000669 /// Map - This is the main map from the element descriptor to the Constants.
670 /// This is the primary way we avoid creating two of the same shape
671 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000672 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000673
674 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
675 /// from the constants to their element in Map. This is important for
676 /// removal of constants from the array, which would otherwise have to scan
677 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000678 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000679
Jim Laskeyc03caef2006-07-17 17:38:29 +0000680 /// AbstractTypeMap - Map for abstract type constants.
681 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000682 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000683
Chris Lattner99a669b2004-11-19 16:39:44 +0000684 private:
685 void clear(std::vector<Constant *> &Constants) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000686 for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I)
Chris Lattner99a669b2004-11-19 16:39:44 +0000687 Constants.push_back(I->second);
688 Map.clear();
689 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000690 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000691 }
692
Chris Lattner98fa07b2003-05-23 20:03:32 +0000693 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000694 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000695
696 /// InsertOrGetItem - Return an iterator for the specified element.
697 /// If the element exists in the map, the returned iterator points to the
698 /// entry and Exists=true. If not, the iterator points to the newly
699 /// inserted entry and returns Exists=false. Newly inserted entries have
700 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000701 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
702 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000703 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000704 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000705 Exists = !IP.second;
706 return IP.first;
707 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000708
Chris Lattner935aa922005-10-04 17:48:46 +0000709private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000710 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000711 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000712 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000713 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
714 IMI->second->second == CP &&
715 "InverseMap corrupt!");
716 return IMI->second;
717 }
718
Jim Laskeyc03caef2006-07-17 17:38:29 +0000719 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +0000720 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000721 if (I == Map.end() || I->second != CP) {
722 // FIXME: This should not use a linear scan. If this gets to be a
723 // performance problem, someone should look at this.
724 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
725 /* empty */;
726 }
Chris Lattner935aa922005-10-04 17:48:46 +0000727 return I;
728 }
729public:
730
Chris Lattnerb64419a2005-10-03 22:51:37 +0000731 /// getOrCreate - Return the specified constant from the map, creating it if
732 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000733 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000734 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000735 typename MapTy::iterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000736 if (I != Map.end() && I->first == Lookup)
Jim Laskeyc03caef2006-07-17 17:38:29 +0000737 return static_cast<ConstantClass *>(I->second); // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +0000738
739 // If no preexisting value, create one now...
740 ConstantClass *Result =
741 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
742
Chris Lattnerb50d1352003-10-05 00:17:43 +0000743 /// FIXME: why does this assert fail when loading 176.gcc?
744 //assert(Result->getType() == Ty && "Type specified is not correct!");
745 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
746
Chris Lattner935aa922005-10-04 17:48:46 +0000747 if (HasLargeKey) // Remember the reverse mapping if needed.
748 InverseMap.insert(std::make_pair(Result, I));
749
Chris Lattnerb50d1352003-10-05 00:17:43 +0000750 // If the type of the constant is abstract, make sure that an entry exists
751 // for it in the AbstractTypeMap.
752 if (Ty->isAbstract()) {
753 typename AbstractTypeMapTy::iterator TI =
754 AbstractTypeMap.lower_bound(Ty);
755
756 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
757 // Add ourselves to the ATU list of the type.
758 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
759
760 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
761 }
762 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000763 return Result;
764 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000765
Chris Lattner98fa07b2003-05-23 20:03:32 +0000766 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000767 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000768 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000769 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000770
Chris Lattner935aa922005-10-04 17:48:46 +0000771 if (HasLargeKey) // Remember the reverse mapping if needed.
772 InverseMap.erase(CP);
773
Chris Lattnerb50d1352003-10-05 00:17:43 +0000774 // Now that we found the entry, make sure this isn't the entry that
775 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000776 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000777 if (Ty->isAbstract()) {
778 assert(AbstractTypeMap.count(Ty) &&
779 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000780 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000781 if (ATMEntryIt == I) {
782 // Yes, we are removing the representative entry for this type.
783 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000784 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000785
Chris Lattnerb50d1352003-10-05 00:17:43 +0000786 // First check the entry before this one...
787 if (TmpIt != Map.begin()) {
788 --TmpIt;
789 if (TmpIt->first.first != Ty) // Not the same type, move back...
790 ++TmpIt;
791 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000792
Chris Lattnerb50d1352003-10-05 00:17:43 +0000793 // If we didn't find the same type, try to move forward...
794 if (TmpIt == ATMEntryIt) {
795 ++TmpIt;
796 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
797 --TmpIt; // No entry afterwards with the same type
798 }
799
800 // If there is another entry in the map of the same abstract type,
801 // update the AbstractTypeMap entry now.
802 if (TmpIt != ATMEntryIt) {
803 ATMEntryIt = TmpIt;
804 } else {
805 // Otherwise, we are removing the last instance of this type
806 // from the table. Remove from the ATM, and from user list.
807 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
808 AbstractTypeMap.erase(Ty);
809 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000810 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000811 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000812
Chris Lattnerb50d1352003-10-05 00:17:43 +0000813 Map.erase(I);
814 }
815
Chris Lattner3b793c62005-10-04 21:35:50 +0000816
817 /// MoveConstantToNewSlot - If we are about to change C to be the element
818 /// specified by I, update our internal data structures to reflect this
819 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000820 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000821 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000822 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000823 assert(OldI != Map.end() && "Constant not found in constant table!");
824 assert(OldI->second == C && "Didn't find correct element?");
825
826 // If this constant is the representative element for its abstract type,
827 // update the AbstractTypeMap so that the representative element is I.
828 if (C->getType()->isAbstract()) {
829 typename AbstractTypeMapTy::iterator ATI =
830 AbstractTypeMap.find(C->getType());
831 assert(ATI != AbstractTypeMap.end() &&
832 "Abstract type not in AbstractTypeMap?");
833 if (ATI->second == OldI)
834 ATI->second = I;
835 }
836
837 // Remove the old entry from the map.
838 Map.erase(OldI);
839
840 // Update the inverse map so that we know that this constant is now
841 // located at descriptor I.
842 if (HasLargeKey) {
843 assert(I->second == C && "Bad inversemap entry!");
844 InverseMap[C] = I;
845 }
846 }
847
Chris Lattnerb50d1352003-10-05 00:17:43 +0000848 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000849 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000850 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000851
852 assert(I != AbstractTypeMap.end() &&
853 "Abstract type not in AbstractTypeMap?");
854
855 // Convert a constant at a time until the last one is gone. The last one
856 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
857 // eliminated eventually.
858 do {
859 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +0000860 TypeClass>::convert(
861 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +0000862 cast<TypeClass>(NewTy));
863
Jim Laskeyc03caef2006-07-17 17:38:29 +0000864 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000865 } while (I != AbstractTypeMap.end());
866 }
867
868 // If the type became concrete without being refined to any other existing
869 // type, we just remove ourselves from the ATU list.
870 void typeBecameConcrete(const DerivedType *AbsTy) {
871 AbsTy->removeAbstractTypeUser(this);
872 }
873
874 void dump() const {
875 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000876 }
877 };
878}
879
Chris Lattner3462ae32001-12-03 22:26:30 +0000880//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000881//
Chris Lattner69edc982006-09-28 00:35:06 +0000882static ManagedStatic<ValueMap< int64_t, Type, ConstantSInt> > SIntConstants;
883static ManagedStatic<ValueMap<uint64_t, Type, ConstantUInt> > UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000884
Chris Lattner3462ae32001-12-03 22:26:30 +0000885ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner69edc982006-09-28 00:35:06 +0000886 return SIntConstants->getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000887}
888
Chris Lattner3462ae32001-12-03 22:26:30 +0000889ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner69edc982006-09-28 00:35:06 +0000890 return UIntConstants->getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000891}
892
Chris Lattner3462ae32001-12-03 22:26:30 +0000893ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000894 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000895 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
896 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000897}
898
Chris Lattner3462ae32001-12-03 22:26:30 +0000899//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000900//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000901namespace llvm {
902 template<>
903 struct ConstantCreator<ConstantFP, Type, uint64_t> {
904 static ConstantFP *create(const Type *Ty, uint64_t V) {
905 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000906 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000907 }
908 };
909 template<>
910 struct ConstantCreator<ConstantFP, Type, uint32_t> {
911 static ConstantFP *create(const Type *Ty, uint32_t V) {
912 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000913 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000914 }
915 };
916}
917
Chris Lattner69edc982006-09-28 00:35:06 +0000918static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
919static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000920
Jim Laskey8ad8f712005-08-17 20:06:22 +0000921bool ConstantFP::isNullValue() const {
922 return DoubleToBits(Val) == 0;
923}
924
925bool ConstantFP::isExactlyValue(double V) const {
926 return DoubleToBits(V) == DoubleToBits(Val);
927}
928
929
Chris Lattner3462ae32001-12-03 22:26:30 +0000930ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000931 if (Ty == Type::FloatTy) {
932 // Force the value through memory to normalize it.
Chris Lattner69edc982006-09-28 00:35:06 +0000933 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000934 } else {
935 assert(Ty == Type::DoubleTy);
Chris Lattner69edc982006-09-28 00:35:06 +0000936 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000937 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000938}
939
Chris Lattner9fba3da2004-02-15 05:53:04 +0000940//---- ConstantAggregateZero::get() implementation...
941//
942namespace llvm {
943 // ConstantAggregateZero does not take extra "value" argument...
944 template<class ValType>
945 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
946 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
947 return new ConstantAggregateZero(Ty);
948 }
949 };
950
951 template<>
952 struct ConvertConstantType<ConstantAggregateZero, Type> {
953 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
954 // Make everyone now use a constant of the new type...
955 Constant *New = ConstantAggregateZero::get(NewTy);
956 assert(New != OldC && "Didn't replace constant??");
957 OldC->uncheckedReplaceAllUsesWith(New);
958 OldC->destroyConstant(); // This constant is now dead, destroy it.
959 }
960 };
961}
962
Chris Lattner69edc982006-09-28 00:35:06 +0000963static ManagedStatic<ValueMap<char, Type,
964 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +0000965
Chris Lattner3e650af2004-08-04 04:48:01 +0000966static char getValType(ConstantAggregateZero *CPZ) { return 0; }
967
Chris Lattner9fba3da2004-02-15 05:53:04 +0000968Constant *ConstantAggregateZero::get(const Type *Ty) {
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000969 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) &&
970 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +0000971 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000972}
973
974// destroyConstant - Remove the constant from the constant table...
975//
976void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000977 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000978 destroyConstantImpl();
979}
980
Chris Lattner3462ae32001-12-03 22:26:30 +0000981//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000982//
Chris Lattner189d19f2003-11-21 20:23:48 +0000983namespace llvm {
984 template<>
985 struct ConvertConstantType<ConstantArray, ArrayType> {
986 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
987 // Make everyone now use a constant of the new type...
988 std::vector<Constant*> C;
989 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
990 C.push_back(cast<Constant>(OldC->getOperand(i)));
991 Constant *New = ConstantArray::get(NewTy, C);
992 assert(New != OldC && "Didn't replace constant??");
993 OldC->uncheckedReplaceAllUsesWith(New);
994 OldC->destroyConstant(); // This constant is now dead, destroy it.
995 }
996 };
997}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000998
Chris Lattner3e650af2004-08-04 04:48:01 +0000999static std::vector<Constant*> getValType(ConstantArray *CA) {
1000 std::vector<Constant*> Elements;
1001 Elements.reserve(CA->getNumOperands());
1002 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1003 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1004 return Elements;
1005}
1006
Chris Lattnerb64419a2005-10-03 22:51:37 +00001007typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001008 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001009static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001010
Chris Lattner015e8212004-02-15 04:14:47 +00001011Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +00001012 const std::vector<Constant*> &V) {
1013 // If this is an all-zero array, return a ConstantAggregateZero object
1014 if (!V.empty()) {
1015 Constant *C = V[0];
1016 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001017 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001018 for (unsigned i = 1, e = V.size(); i != e; ++i)
1019 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001020 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001021 }
1022 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001023}
1024
Chris Lattner98fa07b2003-05-23 20:03:32 +00001025// destroyConstant - Remove the constant from the constant table...
1026//
1027void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001028 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001029 destroyConstantImpl();
1030}
1031
Reid Spencer6f614532006-05-30 08:23:18 +00001032/// ConstantArray::get(const string&) - Return an array that is initialized to
1033/// contain the specified string. If length is zero then a null terminator is
1034/// added to the specified string so that it may be used in a natural way.
1035/// Otherwise, the length parameter specifies how much of the string to use
1036/// and it won't be null terminated.
1037///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001038Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001039 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001040 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +00001041 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001042
1043 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001044 if (AddNull) {
Reid Spencer6f614532006-05-30 08:23:18 +00001045 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001046 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001047
Reid Spencer82ebaba2006-05-30 18:15:07 +00001048 ArrayType *ATy = ArrayType::get(Type::SByteTy, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001049 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001050}
1051
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001052/// isString - This method returns true if the array is an array of sbyte or
1053/// ubyte, and if the elements of the array are all ConstantInt's.
1054bool ConstantArray::isString() const {
1055 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +00001056 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001057 getType()->getElementType() != Type::SByteTy)
1058 return false;
1059 // Check the elements to make sure they are all integers, not constant
1060 // expressions.
1061 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1062 if (!isa<ConstantInt>(getOperand(i)))
1063 return false;
1064 return true;
1065}
1066
Chris Lattner81fabb02002-08-26 17:53:56 +00001067// getAsString - If the sub-element type of this array is either sbyte or ubyte,
1068// then this method converts the array to an std::string and returns it.
1069// Otherwise, it asserts out.
1070//
1071std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001072 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001073 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001074 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1075 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001076 return Result;
1077}
1078
1079
Chris Lattner3462ae32001-12-03 22:26:30 +00001080//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001081//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001082
Chris Lattner189d19f2003-11-21 20:23:48 +00001083namespace llvm {
1084 template<>
1085 struct ConvertConstantType<ConstantStruct, StructType> {
1086 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1087 // Make everyone now use a constant of the new type...
1088 std::vector<Constant*> C;
1089 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1090 C.push_back(cast<Constant>(OldC->getOperand(i)));
1091 Constant *New = ConstantStruct::get(NewTy, C);
1092 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001093
Chris Lattner189d19f2003-11-21 20:23:48 +00001094 OldC->uncheckedReplaceAllUsesWith(New);
1095 OldC->destroyConstant(); // This constant is now dead, destroy it.
1096 }
1097 };
1098}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001099
Chris Lattner8760ec72005-10-04 01:17:50 +00001100typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001101 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001102static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001103
Chris Lattner3e650af2004-08-04 04:48:01 +00001104static std::vector<Constant*> getValType(ConstantStruct *CS) {
1105 std::vector<Constant*> Elements;
1106 Elements.reserve(CS->getNumOperands());
1107 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1108 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1109 return Elements;
1110}
1111
Chris Lattner015e8212004-02-15 04:14:47 +00001112Constant *ConstantStruct::get(const StructType *Ty,
1113 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001114 // Create a ConstantAggregateZero value if all elements are zeros...
1115 for (unsigned i = 0, e = V.size(); i != e; ++i)
1116 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001117 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001118
1119 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001120}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001121
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001122Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1123 std::vector<const Type*> StructEls;
1124 StructEls.reserve(V.size());
1125 for (unsigned i = 0, e = V.size(); i != e; ++i)
1126 StructEls.push_back(V[i]->getType());
1127 return get(StructType::get(StructEls), V);
1128}
1129
Chris Lattnerd7a73302001-10-13 06:57:33 +00001130// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001131//
Chris Lattner3462ae32001-12-03 22:26:30 +00001132void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001133 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001134 destroyConstantImpl();
1135}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001136
Brian Gaeke02209042004-08-20 06:00:58 +00001137//---- ConstantPacked::get() implementation...
1138//
1139namespace llvm {
1140 template<>
1141 struct ConvertConstantType<ConstantPacked, PackedType> {
1142 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1143 // Make everyone now use a constant of the new type...
1144 std::vector<Constant*> C;
1145 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1146 C.push_back(cast<Constant>(OldC->getOperand(i)));
1147 Constant *New = ConstantPacked::get(NewTy, C);
1148 assert(New != OldC && "Didn't replace constant??");
1149 OldC->uncheckedReplaceAllUsesWith(New);
1150 OldC->destroyConstant(); // This constant is now dead, destroy it.
1151 }
1152 };
1153}
1154
1155static std::vector<Constant*> getValType(ConstantPacked *CP) {
1156 std::vector<Constant*> Elements;
1157 Elements.reserve(CP->getNumOperands());
1158 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1159 Elements.push_back(CP->getOperand(i));
1160 return Elements;
1161}
1162
Chris Lattner69edc982006-09-28 00:35:06 +00001163static ManagedStatic<ValueMap<std::vector<Constant*>, PackedType,
1164 ConstantPacked> > PackedConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001165
1166Constant *ConstantPacked::get(const PackedType *Ty,
1167 const std::vector<Constant*> &V) {
1168 // If this is an all-zero packed, return a ConstantAggregateZero object
1169 if (!V.empty()) {
1170 Constant *C = V[0];
1171 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001172 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001173 for (unsigned i = 1, e = V.size(); i != e; ++i)
1174 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001175 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001176 }
1177 return ConstantAggregateZero::get(Ty);
1178}
1179
1180Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1181 assert(!V.empty() && "Cannot infer type if V is empty");
1182 return get(PackedType::get(V.front()->getType(),V.size()), V);
1183}
1184
1185// destroyConstant - Remove the constant from the constant table...
1186//
1187void ConstantPacked::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001188 PackedConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001189 destroyConstantImpl();
1190}
1191
Chris Lattner3462ae32001-12-03 22:26:30 +00001192//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001193//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001194
Chris Lattner189d19f2003-11-21 20:23:48 +00001195namespace llvm {
1196 // ConstantPointerNull does not take extra "value" argument...
1197 template<class ValType>
1198 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1199 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1200 return new ConstantPointerNull(Ty);
1201 }
1202 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001203
Chris Lattner189d19f2003-11-21 20:23:48 +00001204 template<>
1205 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1206 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1207 // Make everyone now use a constant of the new type...
1208 Constant *New = ConstantPointerNull::get(NewTy);
1209 assert(New != OldC && "Didn't replace constant??");
1210 OldC->uncheckedReplaceAllUsesWith(New);
1211 OldC->destroyConstant(); // This constant is now dead, destroy it.
1212 }
1213 };
1214}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001215
Chris Lattner69edc982006-09-28 00:35:06 +00001216static ManagedStatic<ValueMap<char, PointerType,
1217 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001218
Chris Lattner3e650af2004-08-04 04:48:01 +00001219static char getValType(ConstantPointerNull *) {
1220 return 0;
1221}
1222
1223
Chris Lattner3462ae32001-12-03 22:26:30 +00001224ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001225 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001226}
1227
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001228// destroyConstant - Remove the constant from the constant table...
1229//
1230void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001231 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001232 destroyConstantImpl();
1233}
1234
1235
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001236//---- UndefValue::get() implementation...
1237//
1238
1239namespace llvm {
1240 // UndefValue does not take extra "value" argument...
1241 template<class ValType>
1242 struct ConstantCreator<UndefValue, Type, ValType> {
1243 static UndefValue *create(const Type *Ty, const ValType &V) {
1244 return new UndefValue(Ty);
1245 }
1246 };
1247
1248 template<>
1249 struct ConvertConstantType<UndefValue, Type> {
1250 static void convert(UndefValue *OldC, const Type *NewTy) {
1251 // Make everyone now use a constant of the new type.
1252 Constant *New = UndefValue::get(NewTy);
1253 assert(New != OldC && "Didn't replace constant??");
1254 OldC->uncheckedReplaceAllUsesWith(New);
1255 OldC->destroyConstant(); // This constant is now dead, destroy it.
1256 }
1257 };
1258}
1259
Chris Lattner69edc982006-09-28 00:35:06 +00001260static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001261
1262static char getValType(UndefValue *) {
1263 return 0;
1264}
1265
1266
1267UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001268 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001269}
1270
1271// destroyConstant - Remove the constant from the constant table.
1272//
1273void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001274 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001275 destroyConstantImpl();
1276}
1277
1278
1279
1280
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001281//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001282//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001283typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001284
Chris Lattner189d19f2003-11-21 20:23:48 +00001285namespace llvm {
1286 template<>
1287 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1288 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1289 if (V.first == Instruction::Cast)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001290 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
Chris Lattner189d19f2003-11-21 20:23:48 +00001291 if ((V.first >= Instruction::BinaryOpsBegin &&
1292 V.first < Instruction::BinaryOpsEnd) ||
1293 V.first == Instruction::Shl || V.first == Instruction::Shr)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001294 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001295 if (V.first == Instruction::Select)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001296 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Robert Bocchino23004482006-01-10 19:05:34 +00001297 if (V.first == Instruction::ExtractElement)
1298 return new ExtractElementConstantExpr(V.second[0], V.second[1]);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001299 if (V.first == Instruction::InsertElement)
1300 return new InsertElementConstantExpr(V.second[0], V.second[1],
1301 V.second[2]);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001302 if (V.first == Instruction::ShuffleVector)
1303 return new ShuffleVectorConstantExpr(V.second[0], V.second[1],
1304 V.second[2]);
1305
Chris Lattner189d19f2003-11-21 20:23:48 +00001306 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001307
Chris Lattner189d19f2003-11-21 20:23:48 +00001308 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001309 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001310 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001311 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001312
Chris Lattner189d19f2003-11-21 20:23:48 +00001313 template<>
1314 struct ConvertConstantType<ConstantExpr, Type> {
1315 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1316 Constant *New;
1317 switch (OldC->getOpcode()) {
1318 case Instruction::Cast:
1319 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1320 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001321 case Instruction::Select:
1322 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1323 OldC->getOperand(1),
1324 OldC->getOperand(2));
1325 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001326 case Instruction::Shl:
1327 case Instruction::Shr:
1328 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1329 OldC->getOperand(0), OldC->getOperand(1));
1330 break;
1331 default:
1332 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1333 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1334 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1335 OldC->getOperand(1));
1336 break;
1337 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001338 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001339 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1340 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001341 break;
1342 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001343
Chris Lattner189d19f2003-11-21 20:23:48 +00001344 assert(New != OldC && "Didn't replace constant??");
1345 OldC->uncheckedReplaceAllUsesWith(New);
1346 OldC->destroyConstant(); // This constant is now dead, destroy it.
1347 }
1348 };
1349} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001350
1351
Chris Lattner3e650af2004-08-04 04:48:01 +00001352static ExprMapKeyType getValType(ConstantExpr *CE) {
1353 std::vector<Constant*> Operands;
1354 Operands.reserve(CE->getNumOperands());
1355 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1356 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1357 return ExprMapKeyType(CE->getOpcode(), Operands);
1358}
1359
Chris Lattner69edc982006-09-28 00:35:06 +00001360static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1361 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001362
Chris Lattner46b3d302003-04-16 22:40:51 +00001363Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001364 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1365
Chris Lattneracdbe712003-04-17 19:24:48 +00001366 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1367 return FC; // Fold a few common cases...
1368
Vikram S. Adve4c485332002-07-15 18:19:33 +00001369 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001370 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001371 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001372 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001373}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001374
Chris Lattnerdd284742004-04-04 23:20:30 +00001375Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001376 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001377 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1378 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001379 if (C->getType() != Type::BoolTy) {
1380 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1381 return ConstantExpr::getCast(C, Ty);
1382 } else {
1383 if (C == ConstantBool::True)
1384 return ConstantIntegral::getAllOnesValue(Ty);
1385 else
1386 return ConstantIntegral::getNullValue(Ty);
1387 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001388}
1389
1390Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001391 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001392 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1393 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001394 if (C->getType() != Type::BoolTy)
1395 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001396 return ConstantExpr::getCast(C, Ty);
1397}
1398
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001399Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001400 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001401 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001402 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1403 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1404 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001405}
1406
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001407Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1408 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
1409 static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0));
1410
1411 return ConstantExpr::getGetElementPtr(C, Indices);
1412}
1413
Chris Lattnerb50d1352003-10-05 00:17:43 +00001414Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1415 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001416 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1417 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001418 // Check the operands for consistency first
1419 assert((Opcode >= Instruction::BinaryOpsBegin &&
1420 Opcode < Instruction::BinaryOpsEnd) &&
1421 "Invalid opcode in binary constant expression");
1422 assert(C1->getType() == C2->getType() &&
1423 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001424
Chris Lattnere1496fb2006-09-17 19:14:47 +00001425 if (ReqTy == C1->getType() || (Instruction::isComparison(Opcode) &&
Chris Lattner29ca2c62004-08-04 18:50:09 +00001426 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001427 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1428 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001429
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001430 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001431 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001432 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001433}
1434
Chris Lattner29ca2c62004-08-04 18:50:09 +00001435Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001436#ifndef NDEBUG
1437 switch (Opcode) {
1438 case Instruction::Add: case Instruction::Sub:
1439 case Instruction::Mul: case Instruction::Div:
1440 case Instruction::Rem:
1441 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001442 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
1443 isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001444 "Tried to create an arithmetic operation on a non-arithmetic type!");
1445 break;
1446 case Instruction::And:
1447 case Instruction::Or:
1448 case Instruction::Xor:
1449 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001450 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001451 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001452 break;
1453 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1454 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1455 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1456 break;
1457 case Instruction::Shl:
1458 case Instruction::Shr:
1459 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001460 assert((C1->getType()->isInteger() || isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001461 "Tried to create a shift operation on a non-integer type!");
1462 break;
1463 default:
1464 break;
1465 }
1466#endif
1467
Chris Lattnere1496fb2006-09-17 19:14:47 +00001468 if (Instruction::isComparison(Opcode))
Chris Lattner29ca2c62004-08-04 18:50:09 +00001469 return getTy(Type::BoolTy, Opcode, C1, C2);
1470 else
1471 return getTy(C1->getType(), Opcode, C1, C2);
1472}
1473
Chris Lattner6e415c02004-03-12 05:54:04 +00001474Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1475 Constant *V1, Constant *V2) {
1476 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1477 assert(V1->getType() == V2->getType() && "Select value types must match!");
1478 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1479
1480 if (ReqTy == V1->getType())
1481 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1482 return SC; // Fold common cases
1483
1484 std::vector<Constant*> argVec(3, C);
1485 argVec[1] = V1;
1486 argVec[2] = V2;
1487 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001488 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001489}
1490
Chris Lattner9eb2b522004-01-12 19:12:58 +00001491/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001492Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1493 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001494 // Check the operands for consistency first
1495 assert((Opcode == Instruction::Shl ||
1496 Opcode == Instruction::Shr) &&
1497 "Invalid opcode in binary constant expression");
1498 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1499 "Invalid operand types for Shift constant expr!");
1500
Chris Lattner0bba7712004-01-12 20:40:42 +00001501 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001502 return FC; // Fold a few common cases...
1503
1504 // Look up the constant in the table first to ensure uniqueness
1505 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001506 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001507 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001508}
1509
1510
Chris Lattnerb50d1352003-10-05 00:17:43 +00001511Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001512 const std::vector<Value*> &IdxList) {
1513 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001514 "GEP indices invalid!");
1515
Chris Lattneracdbe712003-04-17 19:24:48 +00001516 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1517 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001518
Chris Lattnerb50d1352003-10-05 00:17:43 +00001519 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001520 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001521 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001522 std::vector<Constant*> ArgVec;
1523 ArgVec.reserve(IdxList.size()+1);
1524 ArgVec.push_back(C);
1525 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1526 ArgVec.push_back(cast<Constant>(IdxList[i]));
1527 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001528 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001529}
1530
Chris Lattnerb50d1352003-10-05 00:17:43 +00001531Constant *ConstantExpr::getGetElementPtr(Constant *C,
1532 const std::vector<Constant*> &IdxList){
1533 // Get the result type of the getelementptr!
1534 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1535
1536 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1537 true);
1538 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001539 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1540}
1541
1542Constant *ConstantExpr::getGetElementPtr(Constant *C,
1543 const std::vector<Value*> &IdxList) {
1544 // Get the result type of the getelementptr!
1545 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1546 true);
1547 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001548 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1549}
1550
Robert Bocchino23004482006-01-10 19:05:34 +00001551Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1552 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001553 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1554 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001555 // Look up the constant in the table first to ensure uniqueness
1556 std::vector<Constant*> ArgVec(1, Val);
1557 ArgVec.push_back(Idx);
1558 const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001559 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001560}
1561
1562Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1563 assert(isa<PackedType>(Val->getType()) &&
1564 "Tried to create extractelement operation on non-packed type!");
1565 assert(Idx->getType() == Type::UIntTy &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001566 "Extractelement index must be uint type!");
Robert Bocchino23004482006-01-10 19:05:34 +00001567 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1568 Val, Idx);
1569}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001570
Robert Bocchinoca27f032006-01-17 20:07:22 +00001571Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1572 Constant *Elt, Constant *Idx) {
1573 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1574 return FC; // Fold a few common cases...
1575 // Look up the constant in the table first to ensure uniqueness
1576 std::vector<Constant*> ArgVec(1, Val);
1577 ArgVec.push_back(Elt);
1578 ArgVec.push_back(Idx);
1579 const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001580 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001581}
1582
1583Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1584 Constant *Idx) {
1585 assert(isa<PackedType>(Val->getType()) &&
1586 "Tried to create insertelement operation on non-packed type!");
1587 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
1588 && "Insertelement types must match!");
1589 assert(Idx->getType() == Type::UIntTy &&
1590 "Insertelement index must be uint type!");
1591 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
1592 Val, Elt, Idx);
1593}
1594
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001595Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1596 Constant *V2, Constant *Mask) {
1597 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1598 return FC; // Fold a few common cases...
1599 // Look up the constant in the table first to ensure uniqueness
1600 std::vector<Constant*> ArgVec(1, V1);
1601 ArgVec.push_back(V2);
1602 ArgVec.push_back(Mask);
1603 const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001604 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001605}
1606
1607Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1608 Constant *Mask) {
1609 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1610 "Invalid shuffle vector constant expr operands!");
1611 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1612}
1613
1614
Vikram S. Adve4c485332002-07-15 18:19:33 +00001615// destroyConstant - Remove the constant from the constant table...
1616//
1617void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001618 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001619 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001620}
1621
Chris Lattner3cd8c562002-07-30 18:54:25 +00001622const char *ConstantExpr::getOpcodeName() const {
1623 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001624}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001625
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001626//===----------------------------------------------------------------------===//
1627// replaceUsesOfWithOnConstant implementations
1628
1629void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001630 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001631 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001632 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001633
1634 unsigned OperandToUpdate = U-OperandList;
1635 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1636
Jim Laskeyc03caef2006-07-17 17:38:29 +00001637 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001638 Lookup.first.first = getType();
1639 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001640
Chris Lattnerb64419a2005-10-03 22:51:37 +00001641 std::vector<Constant*> &Values = Lookup.first.second;
1642 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001643
Chris Lattner8760ec72005-10-04 01:17:50 +00001644 // Fill values with the modified operands of the constant array. Also,
1645 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001646 bool isAllZeros = false;
1647 if (!ToC->isNullValue()) {
1648 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1649 Values.push_back(cast<Constant>(O->get()));
1650 } else {
1651 isAllZeros = true;
1652 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1653 Constant *Val = cast<Constant>(O->get());
1654 Values.push_back(Val);
1655 if (isAllZeros) isAllZeros = Val->isNullValue();
1656 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001657 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001658 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001659
Chris Lattnerb64419a2005-10-03 22:51:37 +00001660 Constant *Replacement = 0;
1661 if (isAllZeros) {
1662 Replacement = ConstantAggregateZero::get(getType());
1663 } else {
1664 // Check to see if we have this array type already.
1665 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001666 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001667 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001668
1669 if (Exists) {
1670 Replacement = I->second;
1671 } else {
1672 // Okay, the new shape doesn't exist in the system yet. Instead of
1673 // creating a new constant array, inserting it, replaceallusesof'ing the
1674 // old with the new, then deleting the old... just update the current one
1675 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001676 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001677
Chris Lattnerdff59112005-10-04 18:47:09 +00001678 // Update to the new value.
1679 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001680 return;
1681 }
1682 }
1683
1684 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001685 assert(Replacement != this && "I didn't contain From!");
1686
Chris Lattner7a1450d2005-10-04 18:13:04 +00001687 // Everyone using this now uses the replacement.
1688 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001689
1690 // Delete the old constant!
1691 destroyConstant();
1692}
1693
1694void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001695 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001696 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001697 Constant *ToC = cast<Constant>(To);
1698
Chris Lattnerdff59112005-10-04 18:47:09 +00001699 unsigned OperandToUpdate = U-OperandList;
1700 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1701
Jim Laskeyc03caef2006-07-17 17:38:29 +00001702 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001703 Lookup.first.first = getType();
1704 Lookup.second = this;
1705 std::vector<Constant*> &Values = Lookup.first.second;
1706 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001707
Chris Lattnerdff59112005-10-04 18:47:09 +00001708
Chris Lattner8760ec72005-10-04 01:17:50 +00001709 // Fill values with the modified operands of the constant struct. Also,
1710 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001711 bool isAllZeros = false;
1712 if (!ToC->isNullValue()) {
1713 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1714 Values.push_back(cast<Constant>(O->get()));
1715 } else {
1716 isAllZeros = true;
1717 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1718 Constant *Val = cast<Constant>(O->get());
1719 Values.push_back(Val);
1720 if (isAllZeros) isAllZeros = Val->isNullValue();
1721 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001722 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001723 Values[OperandToUpdate] = ToC;
1724
Chris Lattner8760ec72005-10-04 01:17:50 +00001725 Constant *Replacement = 0;
1726 if (isAllZeros) {
1727 Replacement = ConstantAggregateZero::get(getType());
1728 } else {
1729 // Check to see if we have this array type already.
1730 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001731 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001732 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001733
1734 if (Exists) {
1735 Replacement = I->second;
1736 } else {
1737 // Okay, the new shape doesn't exist in the system yet. Instead of
1738 // creating a new constant struct, inserting it, replaceallusesof'ing the
1739 // old with the new, then deleting the old... just update the current one
1740 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001741 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001742
Chris Lattnerdff59112005-10-04 18:47:09 +00001743 // Update to the new value.
1744 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001745 return;
1746 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001747 }
1748
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001749 assert(Replacement != this && "I didn't contain From!");
1750
Chris Lattner7a1450d2005-10-04 18:13:04 +00001751 // Everyone using this now uses the replacement.
1752 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001753
1754 // Delete the old constant!
1755 destroyConstant();
1756}
1757
1758void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001759 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001760 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1761
1762 std::vector<Constant*> Values;
1763 Values.reserve(getNumOperands()); // Build replacement array...
1764 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1765 Constant *Val = getOperand(i);
1766 if (Val == From) Val = cast<Constant>(To);
1767 Values.push_back(Val);
1768 }
1769
1770 Constant *Replacement = ConstantPacked::get(getType(), Values);
1771 assert(Replacement != this && "I didn't contain From!");
1772
Chris Lattner7a1450d2005-10-04 18:13:04 +00001773 // Everyone using this now uses the replacement.
1774 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001775
1776 // Delete the old constant!
1777 destroyConstant();
1778}
1779
1780void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001781 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001782 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1783 Constant *To = cast<Constant>(ToV);
1784
1785 Constant *Replacement = 0;
1786 if (getOpcode() == Instruction::GetElementPtr) {
1787 std::vector<Constant*> Indices;
1788 Constant *Pointer = getOperand(0);
1789 Indices.reserve(getNumOperands()-1);
1790 if (Pointer == From) Pointer = To;
1791
1792 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1793 Constant *Val = getOperand(i);
1794 if (Val == From) Val = To;
1795 Indices.push_back(Val);
1796 }
1797 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
1798 } else if (getOpcode() == Instruction::Cast) {
1799 assert(getOperand(0) == From && "Cast only has one use!");
1800 Replacement = ConstantExpr::getCast(To, getType());
1801 } else if (getOpcode() == Instruction::Select) {
1802 Constant *C1 = getOperand(0);
1803 Constant *C2 = getOperand(1);
1804 Constant *C3 = getOperand(2);
1805 if (C1 == From) C1 = To;
1806 if (C2 == From) C2 = To;
1807 if (C3 == From) C3 = To;
1808 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00001809 } else if (getOpcode() == Instruction::ExtractElement) {
1810 Constant *C1 = getOperand(0);
1811 Constant *C2 = getOperand(1);
1812 if (C1 == From) C1 = To;
1813 if (C2 == From) C2 = To;
1814 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00001815 } else if (getOpcode() == Instruction::InsertElement) {
1816 Constant *C1 = getOperand(0);
1817 Constant *C2 = getOperand(1);
1818 Constant *C3 = getOperand(1);
1819 if (C1 == From) C1 = To;
1820 if (C2 == From) C2 = To;
1821 if (C3 == From) C3 = To;
1822 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
1823 } else if (getOpcode() == Instruction::ShuffleVector) {
1824 Constant *C1 = getOperand(0);
1825 Constant *C2 = getOperand(1);
1826 Constant *C3 = getOperand(2);
1827 if (C1 == From) C1 = To;
1828 if (C2 == From) C2 = To;
1829 if (C3 == From) C3 = To;
1830 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001831 } else if (getNumOperands() == 2) {
1832 Constant *C1 = getOperand(0);
1833 Constant *C2 = getOperand(1);
1834 if (C1 == From) C1 = To;
1835 if (C2 == From) C2 = To;
1836 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
1837 } else {
1838 assert(0 && "Unknown ConstantExpr type!");
1839 return;
1840 }
1841
1842 assert(Replacement != this && "I didn't contain From!");
1843
Chris Lattner7a1450d2005-10-04 18:13:04 +00001844 // Everyone using this now uses the replacement.
1845 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001846
1847 // Delete the old constant!
1848 destroyConstant();
1849}
1850
1851
Jim Laskey2698f0d2006-03-08 18:11:07 +00001852/// getStringValue - Turn an LLVM constant pointer that eventually points to a
1853/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00001854/// Parameter Chop determines if the result is chopped at the first null
1855/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00001856///
Evan Cheng38280c02006-03-10 23:52:03 +00001857std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00001858 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
1859 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
1860 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
1861 if (Init->isString()) {
1862 std::string Result = Init->getAsString();
1863 if (Offset < Result.size()) {
1864 // If we are pointing INTO The string, erase the beginning...
1865 Result.erase(Result.begin(), Result.begin()+Offset);
1866
1867 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00001868 if (Chop) {
1869 std::string::size_type NullPos = Result.find_first_of((char)0);
1870 if (NullPos != std::string::npos)
1871 Result.erase(Result.begin()+NullPos, Result.end());
1872 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00001873 return Result;
1874 }
1875 }
1876 }
1877 } else if (Constant *C = dyn_cast<Constant>(this)) {
1878 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00001879 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00001880 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1881 if (CE->getOpcode() == Instruction::GetElementPtr) {
1882 // Turn a gep into the specified offset.
1883 if (CE->getNumOperands() == 3 &&
1884 cast<Constant>(CE->getOperand(1))->isNullValue() &&
1885 isa<ConstantInt>(CE->getOperand(2))) {
1886 Offset += cast<ConstantInt>(CE->getOperand(2))->getRawValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00001887 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00001888 }
1889 }
1890 }
1891 }
1892 return "";
1893}
1894