blob: d91e21841dd233c4e18c34df169cbb613effc845 [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"
Chris Lattner3d27be12006-08-27 12:54:02 +000022#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000023#include "llvm/Support/Debug.h"
Chris Lattner69edc982006-09-28 00:35:06 +000024#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000025#include "llvm/Support/MathExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000026#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner2f7c9632001-06-06 20:29:01 +000029//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000030// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000031//===----------------------------------------------------------------------===//
32
Chris Lattner3462ae32001-12-03 22:26:30 +000033void Constant::destroyConstantImpl() {
34 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000035 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000036 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000037 // but they don't know that. Because we only find out when the CPV is
38 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000039 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000040 //
41 while (!use_empty()) {
42 Value *V = use_back();
43#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000044 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000045 DOUT << "While deleting: " << *this
46 << "\n\nUse still stuck around after Def is destroyed: "
47 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000048#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000049 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000050 Constant *CV = cast<Constant>(V);
51 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000052
53 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000054 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000055 }
56
57 // Value has no outstanding references it is safe to delete it now...
58 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000059}
Chris Lattner2f7c9632001-06-06 20:29:01 +000060
Chris Lattner23dd1f62006-10-20 00:27:06 +000061/// canTrap - Return true if evaluation of this constant could trap. This is
62/// true for things like constant expressions that could divide by zero.
63bool Constant::canTrap() const {
64 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
65 // The only thing that could possibly trap are constant exprs.
66 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
67 if (!CE) return false;
68
69 // ConstantExpr traps if any operands can trap.
70 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
71 if (getOperand(i)->canTrap())
72 return true;
73
74 // Otherwise, only specific operations can trap.
75 switch (CE->getOpcode()) {
76 default:
77 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000078 case Instruction::UDiv:
79 case Instruction::SDiv:
80 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000081 case Instruction::URem:
82 case Instruction::SRem:
83 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000084 // Div and rem can trap if the RHS is not known to be non-zero.
85 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
86 return true;
87 return false;
88 }
89}
90
91
Chris Lattnerb1585a92002-08-13 17:50:20 +000092// Static constructor to create a '0' constant of arbitrary type...
93Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000094 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000095 case Type::BoolTyID: {
96 static Constant *NullBool = ConstantBool::get(false);
97 return NullBool;
98 }
99 case Type::SByteTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000100 static Constant *NullSByte = ConstantInt::get(Type::SByteTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000101 return NullSByte;
102 }
103 case Type::UByteTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000104 static Constant *NullUByte = ConstantInt::get(Type::UByteTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000105 return NullUByte;
106 }
107 case Type::ShortTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000108 static Constant *NullShort = ConstantInt::get(Type::ShortTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000109 return NullShort;
110 }
111 case Type::UShortTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000112 static Constant *NullUShort = ConstantInt::get(Type::UShortTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000113 return NullUShort;
114 }
115 case Type::IntTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000116 static Constant *NullInt = ConstantInt::get(Type::IntTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000117 return NullInt;
118 }
119 case Type::UIntTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000120 static Constant *NullUInt = ConstantInt::get(Type::UIntTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000121 return NullUInt;
122 }
123 case Type::LongTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000124 static Constant *NullLong = ConstantInt::get(Type::LongTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000125 return NullLong;
126 }
127 case Type::ULongTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000128 static Constant *NullULong = ConstantInt::get(Type::ULongTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000129 return NullULong;
130 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000131
Chris Lattner3e88ef92003-10-03 19:34:51 +0000132 case Type::FloatTyID: {
133 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
134 return NullFloat;
135 }
136 case Type::DoubleTyID: {
137 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
138 return NullDouble;
139 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000140
Misha Brukmanb1c93172005-04-21 23:48:37 +0000141 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000142 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000143
Chris Lattner9fba3da2004-02-15 05:53:04 +0000144 case Type::StructTyID:
145 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000146 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000147 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000148 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000149 // Function, Label, or Opaque type?
150 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000151 return 0;
152 }
153}
154
155// Static constructor to create the maximum constant of an integral type...
156ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000157 switch (Ty->getTypeID()) {
Chris Lattnera84df0a22006-09-28 23:36:21 +0000158 case Type::BoolTyID: return ConstantBool::getTrue();
Chris Lattnerb1585a92002-08-13 17:50:20 +0000159 case Type::SByteTyID:
160 case Type::ShortTyID:
161 case Type::IntTyID:
162 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000163 // Calculate 011111111111111...
Chris Lattnerb1585a92002-08-13 17:50:20 +0000164 unsigned TypeBits = Ty->getPrimitiveSize()*8;
165 int64_t Val = INT64_MAX; // All ones
166 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
Reid Spencere0fc4df2006-10-20 07:07:24 +0000167 return ConstantInt::get(Ty, Val);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000168 }
169
170 case Type::UByteTyID:
171 case Type::UShortTyID:
172 case Type::UIntTyID:
173 case Type::ULongTyID: return getAllOnesValue(Ty);
174
Chris Lattner31408f72002-08-14 17:12:13 +0000175 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000176 }
177}
178
179// Static constructor to create the minimum constant for an integral type...
180ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000181 switch (Ty->getTypeID()) {
Chris Lattnera84df0a22006-09-28 23:36:21 +0000182 case Type::BoolTyID: return ConstantBool::getFalse();
Chris Lattnerb1585a92002-08-13 17:50:20 +0000183 case Type::SByteTyID:
184 case Type::ShortTyID:
185 case Type::IntTyID:
186 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000187 // Calculate 1111111111000000000000
Chris Lattnerb1585a92002-08-13 17:50:20 +0000188 unsigned TypeBits = Ty->getPrimitiveSize()*8;
189 int64_t Val = -1; // All ones
190 Val <<= TypeBits-1; // Shift over to the right spot
Reid Spencere0fc4df2006-10-20 07:07:24 +0000191 return ConstantInt::get(Ty, Val);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000192 }
193
194 case Type::UByteTyID:
195 case Type::UShortTyID:
196 case Type::UIntTyID:
Reid Spencere0fc4df2006-10-20 07:07:24 +0000197 case Type::ULongTyID: return ConstantInt::get(Ty, 0);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000198
Chris Lattner31408f72002-08-14 17:12:13 +0000199 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000200 }
201}
202
203// Static constructor to create an integral constant with all bits set
204ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000205 switch (Ty->getTypeID()) {
Chris Lattnera84df0a22006-09-28 23:36:21 +0000206 case Type::BoolTyID: return ConstantBool::getTrue();
Chris Lattnerb1585a92002-08-13 17:50:20 +0000207 case Type::SByteTyID:
208 case Type::ShortTyID:
209 case Type::IntTyID:
Reid Spencere0fc4df2006-10-20 07:07:24 +0000210 case Type::LongTyID: return ConstantInt::get(Ty, -1);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000211
212 case Type::UByteTyID:
213 case Type::UShortTyID:
214 case Type::UIntTyID:
215 case Type::ULongTyID: {
216 // Calculate ~0 of the right type...
217 unsigned TypeBits = Ty->getPrimitiveSize()*8;
218 uint64_t Val = ~0ULL; // All ones
219 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
Reid Spencere0fc4df2006-10-20 07:07:24 +0000220 return ConstantInt::get(Ty, Val);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000221 }
Chris Lattner31408f72002-08-14 17:12:13 +0000222 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000223 }
224}
225
Chris Lattner2f7c9632001-06-06 20:29:01 +0000226//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000227// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228//===----------------------------------------------------------------------===//
229
230//===----------------------------------------------------------------------===//
231// Normal Constructors
232
Chris Lattnere7e139e2005-09-27 06:09:08 +0000233ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000234 : Constant(Ty, VT, 0, 0), Val(V) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235}
Chris Lattner49d855c2001-09-07 16:46:31 +0000236
Chris Lattnere7e139e2005-09-27 06:09:08 +0000237ConstantBool::ConstantBool(bool V)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000238 : ConstantIntegral(Type::BoolTy, ConstantBoolVal, uint64_t(V)) {
Chris Lattner265eb642004-06-21 12:12:12 +0000239}
240
Reid Spencere0fc4df2006-10-20 07:07:24 +0000241ConstantInt::ConstantInt(const Type *Ty, uint64_t V)
242 : ConstantIntegral(Ty, ConstantIntVal, V) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000243}
244
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000245ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000246 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000247 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000248 Val = V;
249}
250
Chris Lattner3462ae32001-12-03 22:26:30 +0000251ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000252 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000253 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000254 assert(V.size() == T->getNumElements() &&
255 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000256 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000257 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
258 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000259 Constant *C = *I;
260 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000261 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000262 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000263 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000264 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000265 }
266}
267
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000268ConstantArray::~ConstantArray() {
269 delete [] OperandList;
270}
271
Chris Lattner3462ae32001-12-03 22:26:30 +0000272ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000273 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000274 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000275 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000276 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000277 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000278 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
279 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000280 Constant *C = *I;
281 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000282 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000283 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000284 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000285 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000286 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000287 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000288 }
289}
290
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000291ConstantStruct::~ConstantStruct() {
292 delete [] OperandList;
293}
294
295
Brian Gaeke02209042004-08-20 06:00:58 +0000296ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000297 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000298 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000299 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000300 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
301 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000302 Constant *C = *I;
303 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000304 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000305 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000306 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000307 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000308 }
309}
310
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000311ConstantPacked::~ConstantPacked() {
312 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000313}
314
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000315/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
316/// behind the scenes to implement unary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000317namespace {
318class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000319 Use Op;
320public:
321 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
322 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
323};
Chris Lattner02157b02006-06-28 21:38:54 +0000324}
Chris Lattner6e415c02004-03-12 05:54:04 +0000325
Chris Lattner22ced562003-06-22 20:48:30 +0000326static bool isSetCC(unsigned Opcode) {
327 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
328 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
329 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
330}
331
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000332/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
333/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000334namespace {
335class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000336 Use Ops[2];
337public:
338 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
339 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
340 Opcode, Ops, 2) {
341 Ops[0].init(C1, this);
342 Ops[1].init(C2, this);
343 }
344};
Chris Lattner02157b02006-06-28 21:38:54 +0000345}
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000346
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000347/// SelectConstantExpr - This class is private to Constants.cpp, and is used
348/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000349namespace {
350class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000351 Use Ops[3];
352public:
353 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
354 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
355 Ops[0].init(C1, this);
356 Ops[1].init(C2, this);
357 Ops[2].init(C3, this);
358 }
359};
Chris Lattner02157b02006-06-28 21:38:54 +0000360}
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000361
Robert Bocchinoca27f032006-01-17 20:07:22 +0000362/// ExtractElementConstantExpr - This class is private to
363/// Constants.cpp, and is used behind the scenes to implement
364/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000365namespace {
366class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000367 Use Ops[2];
368public:
369 ExtractElementConstantExpr(Constant *C1, Constant *C2)
370 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(),
371 Instruction::ExtractElement, Ops, 2) {
372 Ops[0].init(C1, this);
373 Ops[1].init(C2, this);
374 }
375};
Chris Lattner02157b02006-06-28 21:38:54 +0000376}
Robert Bocchino23004482006-01-10 19:05:34 +0000377
Robert Bocchinoca27f032006-01-17 20:07:22 +0000378/// InsertElementConstantExpr - This class is private to
379/// Constants.cpp, and is used behind the scenes to implement
380/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000381namespace {
382class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000383 Use Ops[3];
384public:
385 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
386 : ConstantExpr(C1->getType(), Instruction::InsertElement,
387 Ops, 3) {
388 Ops[0].init(C1, this);
389 Ops[1].init(C2, this);
390 Ops[2].init(C3, this);
391 }
392};
Chris Lattner02157b02006-06-28 21:38:54 +0000393}
Robert Bocchinoca27f032006-01-17 20:07:22 +0000394
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000395/// ShuffleVectorConstantExpr - This class is private to
396/// Constants.cpp, and is used behind the scenes to implement
397/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000398namespace {
399class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000400 Use Ops[3];
401public:
402 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
403 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
404 Ops, 3) {
405 Ops[0].init(C1, this);
406 Ops[1].init(C2, this);
407 Ops[2].init(C3, this);
408 }
409};
Chris Lattner02157b02006-06-28 21:38:54 +0000410}
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000411
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000412/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
413/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000414namespace {
415struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000416 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
417 const Type *DestTy)
418 : ConstantExpr(DestTy, Instruction::GetElementPtr,
419 new Use[IdxList.size()+1], IdxList.size()+1) {
420 OperandList[0].init(C, this);
421 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
422 OperandList[i+1].init(IdxList[i], this);
423 }
424 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000425 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000426 }
427};
Chris Lattner02157b02006-06-28 21:38:54 +0000428}
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000429
Chris Lattner817175f2004-03-29 02:37:53 +0000430/// ConstantExpr::get* - Return some common constants without having to
431/// specify the full Instruction::OPCODE identifier.
432///
433Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000434 if (!C->getType()->isFloatingPoint())
435 return get(Instruction::Sub, getNullValue(C->getType()), C);
436 else
437 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000438}
439Constant *ConstantExpr::getNot(Constant *C) {
440 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
441 return get(Instruction::Xor, C,
442 ConstantIntegral::getAllOnesValue(C->getType()));
443}
444Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
445 return get(Instruction::Add, C1, C2);
446}
447Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
448 return get(Instruction::Sub, C1, C2);
449}
450Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
451 return get(Instruction::Mul, C1, C2);
452}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000453Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
454 return get(Instruction::UDiv, C1, C2);
455}
456Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
457 return get(Instruction::SDiv, C1, C2);
458}
459Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
460 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000461}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000462Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
463 return get(Instruction::URem, C1, C2);
464}
465Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
466 return get(Instruction::SRem, C1, C2);
467}
468Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
469 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000470}
471Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
472 return get(Instruction::And, C1, C2);
473}
474Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
475 return get(Instruction::Or, C1, C2);
476}
477Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
478 return get(Instruction::Xor, C1, C2);
479}
480Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
481 return get(Instruction::SetEQ, C1, C2);
482}
483Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
484 return get(Instruction::SetNE, C1, C2);
485}
486Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
487 return get(Instruction::SetLT, C1, C2);
488}
489Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
490 return get(Instruction::SetGT, C1, C2);
491}
492Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
493 return get(Instruction::SetLE, C1, C2);
494}
495Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
496 return get(Instruction::SetGE, C1, C2);
497}
498Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
499 return get(Instruction::Shl, C1, C2);
500}
Reid Spencerfdff9382006-11-08 06:47:33 +0000501Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
502 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000503}
Reid Spencerfdff9382006-11-08 06:47:33 +0000504Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
505 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000506}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000507
Chris Lattner7c1018a2006-07-14 19:37:40 +0000508/// getWithOperandReplaced - Return a constant expression identical to this
509/// one, but with the specified operand set to the specified value.
510Constant *ConstantExpr::getWithOperandReplaced(unsigned OpNo,
511 Constant *Op) const {
512 assert(OpNo < getNumOperands() && "Operand num is out of range!");
513 assert(Op->getType() == getOperand(OpNo)->getType() &&
514 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000515 if (getOperand(OpNo) == Op)
516 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000517
Chris Lattner227816342006-07-14 22:20:01 +0000518 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000519 switch (getOpcode()) {
520 case Instruction::Cast:
521 return ConstantExpr::getCast(Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000522 case Instruction::Select:
523 Op0 = (OpNo == 0) ? Op : getOperand(0);
524 Op1 = (OpNo == 1) ? Op : getOperand(1);
525 Op2 = (OpNo == 2) ? Op : getOperand(2);
526 return ConstantExpr::getSelect(Op0, Op1, Op2);
527 case Instruction::InsertElement:
528 Op0 = (OpNo == 0) ? Op : getOperand(0);
529 Op1 = (OpNo == 1) ? Op : getOperand(1);
530 Op2 = (OpNo == 2) ? Op : getOperand(2);
531 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
532 case Instruction::ExtractElement:
533 Op0 = (OpNo == 0) ? Op : getOperand(0);
534 Op1 = (OpNo == 1) ? Op : getOperand(1);
535 return ConstantExpr::getExtractElement(Op0, Op1);
536 case Instruction::ShuffleVector:
537 Op0 = (OpNo == 0) ? Op : getOperand(0);
538 Op1 = (OpNo == 1) ? Op : getOperand(1);
539 Op2 = (OpNo == 2) ? Op : getOperand(2);
540 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000541 case Instruction::GetElementPtr: {
542 std::vector<Constant*> Ops;
543 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
544 Ops.push_back(getOperand(i));
545 if (OpNo == 0)
546 return ConstantExpr::getGetElementPtr(Op, Ops);
547 Ops[OpNo-1] = Op;
548 return ConstantExpr::getGetElementPtr(getOperand(0), Ops);
549 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000550 default:
551 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000552 Op0 = (OpNo == 0) ? Op : getOperand(0);
553 Op1 = (OpNo == 1) ? Op : getOperand(1);
554 return ConstantExpr::get(getOpcode(), Op0, Op1);
555 }
556}
557
558/// getWithOperands - This returns the current constant expression with the
559/// operands replaced with the specified values. The specified operands must
560/// match count and type with the existing ones.
561Constant *ConstantExpr::
562getWithOperands(const std::vector<Constant*> &Ops) const {
563 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
564 bool AnyChange = false;
565 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
566 assert(Ops[i]->getType() == getOperand(i)->getType() &&
567 "Operand type mismatch!");
568 AnyChange |= Ops[i] != getOperand(i);
569 }
570 if (!AnyChange) // No operands changed, return self.
571 return const_cast<ConstantExpr*>(this);
572
573 switch (getOpcode()) {
574 case Instruction::Cast:
575 return ConstantExpr::getCast(Ops[0], getType());
576 case Instruction::Select:
577 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
578 case Instruction::InsertElement:
579 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
580 case Instruction::ExtractElement:
581 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
582 case Instruction::ShuffleVector:
583 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
584 case Instruction::GetElementPtr: {
585 std::vector<Constant*> ActualOps(Ops.begin()+1, Ops.end());
586 return ConstantExpr::getGetElementPtr(Ops[0], ActualOps);
587 }
588 default:
589 assert(getNumOperands() == 2 && "Must be binary operator?");
590 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000591 }
592}
593
Chris Lattner2f7c9632001-06-06 20:29:01 +0000594
595//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000596// isValueValidForType implementations
597
Reid Spencere0fc4df2006-10-20 07:07:24 +0000598bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000599 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000600 default:
601 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000602 // Signed types...
603 case Type::SByteTyID:
604 return (Val <= INT8_MAX && Val >= INT8_MIN);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000605 case Type::UByteTyID:
606 return (Val >= 0) && (Val <= UINT8_MAX);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000607 case Type::ShortTyID:
608 return (Val <= INT16_MAX && Val >= INT16_MIN);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000609 case Type::UShortTyID:
610 return (Val >= 0) && (Val <= UINT16_MAX);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000611 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000612 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000613 case Type::UIntTyID:
Reid Spencere0fc4df2006-10-20 07:07:24 +0000614 return (Val >= 0) && (Val <= UINT32_MAX);
615 case Type::LongTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000616 case Type::ULongTyID:
Reid Spencere0fc4df2006-10-20 07:07:24 +0000617 return true; // always true, has to fit in largest type
Chris Lattner2f7c9632001-06-06 20:29:01 +0000618 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000619}
620
Chris Lattner3462ae32001-12-03 22:26:30 +0000621bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000622 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000623 default:
624 return false; // These can't be represented as floating point!
625
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000626 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000627 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000628 case Type::DoubleTyID:
629 return true; // This is the largest type...
630 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000631}
Chris Lattner9655e542001-07-20 19:16:02 +0000632
Chris Lattner49d855c2001-09-07 16:46:31 +0000633//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000634// Factory Function Implementation
635
Chris Lattner98fa07b2003-05-23 20:03:32 +0000636// ConstantCreator - A class that is used to create constants by
637// ValueMap*. This class should be partially specialized if there is
638// something strange that needs to be done to interface to the ctor for the
639// constant.
640//
Chris Lattner189d19f2003-11-21 20:23:48 +0000641namespace llvm {
642 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000643 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000644 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
645 return new ConstantClass(Ty, V);
646 }
647 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000648
Chris Lattner189d19f2003-11-21 20:23:48 +0000649 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000650 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000651 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
652 assert(0 && "This type cannot be converted!\n");
653 abort();
654 }
655 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000656
Chris Lattner935aa922005-10-04 17:48:46 +0000657 template<class ValType, class TypeClass, class ConstantClass,
658 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000659 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000660 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000661 typedef std::pair<const Type*, ValType> MapKey;
662 typedef std::map<MapKey, Constant *> MapTy;
663 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
664 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000665 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000666 /// Map - This is the main map from the element descriptor to the Constants.
667 /// This is the primary way we avoid creating two of the same shape
668 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000669 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000670
671 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
672 /// from the constants to their element in Map. This is important for
673 /// removal of constants from the array, which would otherwise have to scan
674 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000675 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000676
Jim Laskeyc03caef2006-07-17 17:38:29 +0000677 /// AbstractTypeMap - Map for abstract type constants.
678 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000679 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000680
Chris Lattner99a669b2004-11-19 16:39:44 +0000681 private:
682 void clear(std::vector<Constant *> &Constants) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000683 for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I)
Chris Lattner99a669b2004-11-19 16:39:44 +0000684 Constants.push_back(I->second);
685 Map.clear();
686 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000687 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000688 }
689
Chris Lattner98fa07b2003-05-23 20:03:32 +0000690 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000691 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000692
693 /// InsertOrGetItem - Return an iterator for the specified element.
694 /// If the element exists in the map, the returned iterator points to the
695 /// entry and Exists=true. If not, the iterator points to the newly
696 /// inserted entry and returns Exists=false. Newly inserted entries have
697 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000698 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
699 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000700 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000701 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000702 Exists = !IP.second;
703 return IP.first;
704 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000705
Chris Lattner935aa922005-10-04 17:48:46 +0000706private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000707 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000708 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000709 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000710 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
711 IMI->second->second == CP &&
712 "InverseMap corrupt!");
713 return IMI->second;
714 }
715
Jim Laskeyc03caef2006-07-17 17:38:29 +0000716 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +0000717 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000718 if (I == Map.end() || I->second != CP) {
719 // FIXME: This should not use a linear scan. If this gets to be a
720 // performance problem, someone should look at this.
721 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
722 /* empty */;
723 }
Chris Lattner935aa922005-10-04 17:48:46 +0000724 return I;
725 }
726public:
727
Chris Lattnerb64419a2005-10-03 22:51:37 +0000728 /// getOrCreate - Return the specified constant from the map, creating it if
729 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000730 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000731 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000732 typename MapTy::iterator I = Map.lower_bound(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000733 // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +0000734 if (I != Map.end() && I->first == Lookup)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000735 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000736
737 // If no preexisting value, create one now...
738 ConstantClass *Result =
739 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
740
Chris Lattnerb50d1352003-10-05 00:17:43 +0000741 /// FIXME: why does this assert fail when loading 176.gcc?
742 //assert(Result->getType() == Ty && "Type specified is not correct!");
743 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
744
Chris Lattner935aa922005-10-04 17:48:46 +0000745 if (HasLargeKey) // Remember the reverse mapping if needed.
746 InverseMap.insert(std::make_pair(Result, I));
747
Chris Lattnerb50d1352003-10-05 00:17:43 +0000748 // If the type of the constant is abstract, make sure that an entry exists
749 // for it in the AbstractTypeMap.
750 if (Ty->isAbstract()) {
751 typename AbstractTypeMapTy::iterator TI =
752 AbstractTypeMap.lower_bound(Ty);
753
754 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
755 // Add ourselves to the ATU list of the type.
756 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
757
758 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
759 }
760 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000761 return Result;
762 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000763
Chris Lattner98fa07b2003-05-23 20:03:32 +0000764 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000765 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000766 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000767 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000768
Chris Lattner935aa922005-10-04 17:48:46 +0000769 if (HasLargeKey) // Remember the reverse mapping if needed.
770 InverseMap.erase(CP);
771
Chris Lattnerb50d1352003-10-05 00:17:43 +0000772 // Now that we found the entry, make sure this isn't the entry that
773 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000774 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000775 if (Ty->isAbstract()) {
776 assert(AbstractTypeMap.count(Ty) &&
777 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000778 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000779 if (ATMEntryIt == I) {
780 // Yes, we are removing the representative entry for this type.
781 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000782 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000783
Chris Lattnerb50d1352003-10-05 00:17:43 +0000784 // First check the entry before this one...
785 if (TmpIt != Map.begin()) {
786 --TmpIt;
787 if (TmpIt->first.first != Ty) // Not the same type, move back...
788 ++TmpIt;
789 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000790
Chris Lattnerb50d1352003-10-05 00:17:43 +0000791 // If we didn't find the same type, try to move forward...
792 if (TmpIt == ATMEntryIt) {
793 ++TmpIt;
794 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
795 --TmpIt; // No entry afterwards with the same type
796 }
797
798 // If there is another entry in the map of the same abstract type,
799 // update the AbstractTypeMap entry now.
800 if (TmpIt != ATMEntryIt) {
801 ATMEntryIt = TmpIt;
802 } else {
803 // Otherwise, we are removing the last instance of this type
804 // from the table. Remove from the ATM, and from user list.
805 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
806 AbstractTypeMap.erase(Ty);
807 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000808 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000809 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000810
Chris Lattnerb50d1352003-10-05 00:17:43 +0000811 Map.erase(I);
812 }
813
Chris Lattner3b793c62005-10-04 21:35:50 +0000814
815 /// MoveConstantToNewSlot - If we are about to change C to be the element
816 /// specified by I, update our internal data structures to reflect this
817 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000818 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000819 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000820 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000821 assert(OldI != Map.end() && "Constant not found in constant table!");
822 assert(OldI->second == C && "Didn't find correct element?");
823
824 // If this constant is the representative element for its abstract type,
825 // update the AbstractTypeMap so that the representative element is I.
826 if (C->getType()->isAbstract()) {
827 typename AbstractTypeMapTy::iterator ATI =
828 AbstractTypeMap.find(C->getType());
829 assert(ATI != AbstractTypeMap.end() &&
830 "Abstract type not in AbstractTypeMap?");
831 if (ATI->second == OldI)
832 ATI->second = I;
833 }
834
835 // Remove the old entry from the map.
836 Map.erase(OldI);
837
838 // Update the inverse map so that we know that this constant is now
839 // located at descriptor I.
840 if (HasLargeKey) {
841 assert(I->second == C && "Bad inversemap entry!");
842 InverseMap[C] = I;
843 }
844 }
845
Chris Lattnerb50d1352003-10-05 00:17:43 +0000846 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000847 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000848 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000849
850 assert(I != AbstractTypeMap.end() &&
851 "Abstract type not in AbstractTypeMap?");
852
853 // Convert a constant at a time until the last one is gone. The last one
854 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
855 // eliminated eventually.
856 do {
857 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +0000858 TypeClass>::convert(
859 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +0000860 cast<TypeClass>(NewTy));
861
Jim Laskeyc03caef2006-07-17 17:38:29 +0000862 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000863 } while (I != AbstractTypeMap.end());
864 }
865
866 // If the type became concrete without being refined to any other existing
867 // type, we just remove ourselves from the ATU list.
868 void typeBecameConcrete(const DerivedType *AbsTy) {
869 AbsTy->removeAbstractTypeUser(this);
870 }
871
872 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +0000873 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000874 }
875 };
876}
877
Chris Lattnera84df0a22006-09-28 23:36:21 +0000878
879//---- ConstantBool::get*() implementation.
880
881ConstantBool *ConstantBool::getTrue() {
882 static ConstantBool *T = 0;
883 if (T) return T;
884 return T = new ConstantBool(true);
885}
886ConstantBool *ConstantBool::getFalse() {
887 static ConstantBool *F = 0;
888 if (F) return F;
889 return F = new ConstantBool(false);
890}
891
Reid Spencere0fc4df2006-10-20 07:07:24 +0000892//---- ConstantInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000893//
Reid Spencere0fc4df2006-10-20 07:07:24 +0000894static ManagedStatic<ValueMap<uint64_t, Type, ConstantInt> > IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000895
Reid Spencere0fc4df2006-10-20 07:07:24 +0000896// Get a ConstantInt from an int64_t. Note here that we canoncialize the value
897// to a uint64_t value that has been zero extended down to the size of the
898// integer type of the ConstantInt. This allows the getZExtValue method to
899// just return the stored value while getSExtValue has to convert back to sign
900// extended. getZExtValue is more common in LLVM than getSExtValue().
901ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) {
902 unsigned Size = Ty->getPrimitiveSizeInBits();
903 uint64_t ZeroExtendedCanonicalization = V & (~uint64_t(0UL) >> (64-Size));
904 return IntConstants->getOrCreate(Ty, ZeroExtendedCanonicalization );
Chris Lattner49d855c2001-09-07 16:46:31 +0000905}
906
Chris Lattner3462ae32001-12-03 22:26:30 +0000907//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000908//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000909namespace llvm {
910 template<>
911 struct ConstantCreator<ConstantFP, Type, uint64_t> {
912 static ConstantFP *create(const Type *Ty, uint64_t V) {
913 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000914 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000915 }
916 };
917 template<>
918 struct ConstantCreator<ConstantFP, Type, uint32_t> {
919 static ConstantFP *create(const Type *Ty, uint32_t V) {
920 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000921 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000922 }
923 };
924}
925
Chris Lattner69edc982006-09-28 00:35:06 +0000926static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
927static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000928
Jim Laskey8ad8f712005-08-17 20:06:22 +0000929bool ConstantFP::isNullValue() const {
930 return DoubleToBits(Val) == 0;
931}
932
933bool ConstantFP::isExactlyValue(double V) const {
934 return DoubleToBits(V) == DoubleToBits(Val);
935}
936
937
Chris Lattner3462ae32001-12-03 22:26:30 +0000938ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000939 if (Ty == Type::FloatTy) {
940 // Force the value through memory to normalize it.
Chris Lattner69edc982006-09-28 00:35:06 +0000941 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000942 } else {
943 assert(Ty == Type::DoubleTy);
Chris Lattner69edc982006-09-28 00:35:06 +0000944 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000945 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000946}
947
Chris Lattner9fba3da2004-02-15 05:53:04 +0000948//---- ConstantAggregateZero::get() implementation...
949//
950namespace llvm {
951 // ConstantAggregateZero does not take extra "value" argument...
952 template<class ValType>
953 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
954 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
955 return new ConstantAggregateZero(Ty);
956 }
957 };
958
959 template<>
960 struct ConvertConstantType<ConstantAggregateZero, Type> {
961 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
962 // Make everyone now use a constant of the new type...
963 Constant *New = ConstantAggregateZero::get(NewTy);
964 assert(New != OldC && "Didn't replace constant??");
965 OldC->uncheckedReplaceAllUsesWith(New);
966 OldC->destroyConstant(); // This constant is now dead, destroy it.
967 }
968 };
969}
970
Chris Lattner69edc982006-09-28 00:35:06 +0000971static ManagedStatic<ValueMap<char, Type,
972 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +0000973
Chris Lattner3e650af2004-08-04 04:48:01 +0000974static char getValType(ConstantAggregateZero *CPZ) { return 0; }
975
Chris Lattner9fba3da2004-02-15 05:53:04 +0000976Constant *ConstantAggregateZero::get(const Type *Ty) {
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000977 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) &&
978 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +0000979 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000980}
981
982// destroyConstant - Remove the constant from the constant table...
983//
984void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000985 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000986 destroyConstantImpl();
987}
988
Chris Lattner3462ae32001-12-03 22:26:30 +0000989//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000990//
Chris Lattner189d19f2003-11-21 20:23:48 +0000991namespace llvm {
992 template<>
993 struct ConvertConstantType<ConstantArray, ArrayType> {
994 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
995 // Make everyone now use a constant of the new type...
996 std::vector<Constant*> C;
997 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
998 C.push_back(cast<Constant>(OldC->getOperand(i)));
999 Constant *New = ConstantArray::get(NewTy, C);
1000 assert(New != OldC && "Didn't replace constant??");
1001 OldC->uncheckedReplaceAllUsesWith(New);
1002 OldC->destroyConstant(); // This constant is now dead, destroy it.
1003 }
1004 };
1005}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001006
Chris Lattner3e650af2004-08-04 04:48:01 +00001007static std::vector<Constant*> getValType(ConstantArray *CA) {
1008 std::vector<Constant*> Elements;
1009 Elements.reserve(CA->getNumOperands());
1010 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1011 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1012 return Elements;
1013}
1014
Chris Lattnerb64419a2005-10-03 22:51:37 +00001015typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001016 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001017static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001018
Chris Lattner015e8212004-02-15 04:14:47 +00001019Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +00001020 const std::vector<Constant*> &V) {
1021 // If this is an all-zero array, return a ConstantAggregateZero object
1022 if (!V.empty()) {
1023 Constant *C = V[0];
1024 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001025 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001026 for (unsigned i = 1, e = V.size(); i != e; ++i)
1027 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001028 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001029 }
1030 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001031}
1032
Chris Lattner98fa07b2003-05-23 20:03:32 +00001033// destroyConstant - Remove the constant from the constant table...
1034//
1035void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001036 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001037 destroyConstantImpl();
1038}
1039
Reid Spencer6f614532006-05-30 08:23:18 +00001040/// ConstantArray::get(const string&) - Return an array that is initialized to
1041/// contain the specified string. If length is zero then a null terminator is
1042/// added to the specified string so that it may be used in a natural way.
1043/// Otherwise, the length parameter specifies how much of the string to use
1044/// and it won't be null terminated.
1045///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001046Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001047 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001048 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001049 ElementVals.push_back(ConstantInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001050
1051 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001052 if (AddNull) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00001053 ElementVals.push_back(ConstantInt::get(Type::SByteTy, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001054 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001055
Reid Spencer82ebaba2006-05-30 18:15:07 +00001056 ArrayType *ATy = ArrayType::get(Type::SByteTy, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001057 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001058}
1059
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001060/// isString - This method returns true if the array is an array of sbyte or
1061/// ubyte, and if the elements of the array are all ConstantInt's.
1062bool ConstantArray::isString() const {
1063 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +00001064 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001065 getType()->getElementType() != Type::SByteTy)
1066 return false;
1067 // Check the elements to make sure they are all integers, not constant
1068 // expressions.
1069 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1070 if (!isa<ConstantInt>(getOperand(i)))
1071 return false;
1072 return true;
1073}
1074
Evan Cheng3763c5b2006-10-26 19:15:05 +00001075/// isCString - This method returns true if the array is a string (see
1076/// isString) and it ends in a null byte \0 and does not contains any other
1077/// null bytes except its terminator.
1078bool ConstantArray::isCString() const {
Evan Chenge974da62006-10-26 21:48:03 +00001079 // Check the element type for sbyte or ubyte...
1080 if (getType()->getElementType() != Type::UByteTy &&
1081 getType()->getElementType() != Type::SByteTy)
1082 return false;
1083 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1084 // Last element must be a null.
1085 if (getOperand(getNumOperands()-1) != Zero)
1086 return false;
1087 // Other elements must be non-null integers.
1088 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1089 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001090 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001091 if (getOperand(i) == Zero)
1092 return false;
1093 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001094 return true;
1095}
1096
1097
Chris Lattner81fabb02002-08-26 17:53:56 +00001098// getAsString - If the sub-element type of this array is either sbyte or ubyte,
1099// then this method converts the array to an std::string and returns it.
1100// Otherwise, it asserts out.
1101//
1102std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001103 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001104 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001105 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001106 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001107 return Result;
1108}
1109
1110
Chris Lattner3462ae32001-12-03 22:26:30 +00001111//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001112//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001113
Chris Lattner189d19f2003-11-21 20:23:48 +00001114namespace llvm {
1115 template<>
1116 struct ConvertConstantType<ConstantStruct, StructType> {
1117 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1118 // Make everyone now use a constant of the new type...
1119 std::vector<Constant*> C;
1120 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1121 C.push_back(cast<Constant>(OldC->getOperand(i)));
1122 Constant *New = ConstantStruct::get(NewTy, C);
1123 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001124
Chris Lattner189d19f2003-11-21 20:23:48 +00001125 OldC->uncheckedReplaceAllUsesWith(New);
1126 OldC->destroyConstant(); // This constant is now dead, destroy it.
1127 }
1128 };
1129}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001130
Chris Lattner8760ec72005-10-04 01:17:50 +00001131typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001132 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001133static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001134
Chris Lattner3e650af2004-08-04 04:48:01 +00001135static std::vector<Constant*> getValType(ConstantStruct *CS) {
1136 std::vector<Constant*> Elements;
1137 Elements.reserve(CS->getNumOperands());
1138 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1139 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1140 return Elements;
1141}
1142
Chris Lattner015e8212004-02-15 04:14:47 +00001143Constant *ConstantStruct::get(const StructType *Ty,
1144 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001145 // Create a ConstantAggregateZero value if all elements are zeros...
1146 for (unsigned i = 0, e = V.size(); i != e; ++i)
1147 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001148 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001149
1150 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001151}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001152
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001153Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1154 std::vector<const Type*> StructEls;
1155 StructEls.reserve(V.size());
1156 for (unsigned i = 0, e = V.size(); i != e; ++i)
1157 StructEls.push_back(V[i]->getType());
1158 return get(StructType::get(StructEls), V);
1159}
1160
Chris Lattnerd7a73302001-10-13 06:57:33 +00001161// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001162//
Chris Lattner3462ae32001-12-03 22:26:30 +00001163void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001164 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001165 destroyConstantImpl();
1166}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001167
Brian Gaeke02209042004-08-20 06:00:58 +00001168//---- ConstantPacked::get() implementation...
1169//
1170namespace llvm {
1171 template<>
1172 struct ConvertConstantType<ConstantPacked, PackedType> {
1173 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1174 // Make everyone now use a constant of the new type...
1175 std::vector<Constant*> C;
1176 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1177 C.push_back(cast<Constant>(OldC->getOperand(i)));
1178 Constant *New = ConstantPacked::get(NewTy, C);
1179 assert(New != OldC && "Didn't replace constant??");
1180 OldC->uncheckedReplaceAllUsesWith(New);
1181 OldC->destroyConstant(); // This constant is now dead, destroy it.
1182 }
1183 };
1184}
1185
1186static std::vector<Constant*> getValType(ConstantPacked *CP) {
1187 std::vector<Constant*> Elements;
1188 Elements.reserve(CP->getNumOperands());
1189 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1190 Elements.push_back(CP->getOperand(i));
1191 return Elements;
1192}
1193
Chris Lattner69edc982006-09-28 00:35:06 +00001194static ManagedStatic<ValueMap<std::vector<Constant*>, PackedType,
1195 ConstantPacked> > PackedConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001196
1197Constant *ConstantPacked::get(const PackedType *Ty,
1198 const std::vector<Constant*> &V) {
1199 // If this is an all-zero packed, return a ConstantAggregateZero object
1200 if (!V.empty()) {
1201 Constant *C = V[0];
1202 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001203 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001204 for (unsigned i = 1, e = V.size(); i != e; ++i)
1205 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001206 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001207 }
1208 return ConstantAggregateZero::get(Ty);
1209}
1210
1211Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1212 assert(!V.empty() && "Cannot infer type if V is empty");
1213 return get(PackedType::get(V.front()->getType(),V.size()), V);
1214}
1215
1216// destroyConstant - Remove the constant from the constant table...
1217//
1218void ConstantPacked::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001219 PackedConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001220 destroyConstantImpl();
1221}
1222
Chris Lattner3462ae32001-12-03 22:26:30 +00001223//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001224//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001225
Chris Lattner189d19f2003-11-21 20:23:48 +00001226namespace llvm {
1227 // ConstantPointerNull does not take extra "value" argument...
1228 template<class ValType>
1229 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1230 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1231 return new ConstantPointerNull(Ty);
1232 }
1233 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001234
Chris Lattner189d19f2003-11-21 20:23:48 +00001235 template<>
1236 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1237 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1238 // Make everyone now use a constant of the new type...
1239 Constant *New = ConstantPointerNull::get(NewTy);
1240 assert(New != OldC && "Didn't replace constant??");
1241 OldC->uncheckedReplaceAllUsesWith(New);
1242 OldC->destroyConstant(); // This constant is now dead, destroy it.
1243 }
1244 };
1245}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001246
Chris Lattner69edc982006-09-28 00:35:06 +00001247static ManagedStatic<ValueMap<char, PointerType,
1248 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001249
Chris Lattner3e650af2004-08-04 04:48:01 +00001250static char getValType(ConstantPointerNull *) {
1251 return 0;
1252}
1253
1254
Chris Lattner3462ae32001-12-03 22:26:30 +00001255ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001256 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001257}
1258
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001259// destroyConstant - Remove the constant from the constant table...
1260//
1261void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001262 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001263 destroyConstantImpl();
1264}
1265
1266
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001267//---- UndefValue::get() implementation...
1268//
1269
1270namespace llvm {
1271 // UndefValue does not take extra "value" argument...
1272 template<class ValType>
1273 struct ConstantCreator<UndefValue, Type, ValType> {
1274 static UndefValue *create(const Type *Ty, const ValType &V) {
1275 return new UndefValue(Ty);
1276 }
1277 };
1278
1279 template<>
1280 struct ConvertConstantType<UndefValue, Type> {
1281 static void convert(UndefValue *OldC, const Type *NewTy) {
1282 // Make everyone now use a constant of the new type.
1283 Constant *New = UndefValue::get(NewTy);
1284 assert(New != OldC && "Didn't replace constant??");
1285 OldC->uncheckedReplaceAllUsesWith(New);
1286 OldC->destroyConstant(); // This constant is now dead, destroy it.
1287 }
1288 };
1289}
1290
Chris Lattner69edc982006-09-28 00:35:06 +00001291static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001292
1293static char getValType(UndefValue *) {
1294 return 0;
1295}
1296
1297
1298UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001299 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001300}
1301
1302// destroyConstant - Remove the constant from the constant table.
1303//
1304void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001305 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001306 destroyConstantImpl();
1307}
1308
1309
1310
1311
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001312//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001313//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001314typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001315
Chris Lattner189d19f2003-11-21 20:23:48 +00001316namespace llvm {
1317 template<>
1318 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1319 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1320 if (V.first == Instruction::Cast)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001321 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
Chris Lattner189d19f2003-11-21 20:23:48 +00001322 if ((V.first >= Instruction::BinaryOpsBegin &&
1323 V.first < Instruction::BinaryOpsEnd) ||
Reid Spencerfdff9382006-11-08 06:47:33 +00001324 V.first == Instruction::Shl ||
1325 V.first == Instruction::LShr ||
1326 V.first == Instruction::AShr)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001327 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001328 if (V.first == Instruction::Select)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001329 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Robert Bocchino23004482006-01-10 19:05:34 +00001330 if (V.first == Instruction::ExtractElement)
1331 return new ExtractElementConstantExpr(V.second[0], V.second[1]);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001332 if (V.first == Instruction::InsertElement)
1333 return new InsertElementConstantExpr(V.second[0], V.second[1],
1334 V.second[2]);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001335 if (V.first == Instruction::ShuffleVector)
1336 return new ShuffleVectorConstantExpr(V.second[0], V.second[1],
1337 V.second[2]);
1338
Chris Lattner189d19f2003-11-21 20:23:48 +00001339 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001340
Chris Lattner189d19f2003-11-21 20:23:48 +00001341 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001342 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001343 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001344 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001345
Chris Lattner189d19f2003-11-21 20:23:48 +00001346 template<>
1347 struct ConvertConstantType<ConstantExpr, Type> {
1348 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1349 Constant *New;
1350 switch (OldC->getOpcode()) {
1351 case Instruction::Cast:
1352 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1353 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001354 case Instruction::Select:
1355 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1356 OldC->getOperand(1),
1357 OldC->getOperand(2));
1358 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001359 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001360 case Instruction::LShr:
1361 case Instruction::AShr:
Chris Lattner189d19f2003-11-21 20:23:48 +00001362 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1363 OldC->getOperand(0), OldC->getOperand(1));
1364 break;
1365 default:
1366 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001367 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001368 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1369 OldC->getOperand(1));
1370 break;
1371 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001372 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001373 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1374 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001375 break;
1376 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001377
Chris Lattner189d19f2003-11-21 20:23:48 +00001378 assert(New != OldC && "Didn't replace constant??");
1379 OldC->uncheckedReplaceAllUsesWith(New);
1380 OldC->destroyConstant(); // This constant is now dead, destroy it.
1381 }
1382 };
1383} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001384
1385
Chris Lattner3e650af2004-08-04 04:48:01 +00001386static ExprMapKeyType getValType(ConstantExpr *CE) {
1387 std::vector<Constant*> Operands;
1388 Operands.reserve(CE->getNumOperands());
1389 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1390 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1391 return ExprMapKeyType(CE->getOpcode(), Operands);
1392}
1393
Chris Lattner69edc982006-09-28 00:35:06 +00001394static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1395 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001396
Chris Lattner46b3d302003-04-16 22:40:51 +00001397Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001398 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1399
Chris Lattneracdbe712003-04-17 19:24:48 +00001400 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1401 return FC; // Fold a few common cases...
1402
Vikram S. Adve4c485332002-07-15 18:19:33 +00001403 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001404 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001405 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001406 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001407}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001408
Chris Lattnerdd284742004-04-04 23:20:30 +00001409Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001410 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001411 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1412 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001413 if (C->getType() != Type::BoolTy) {
1414 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1415 return ConstantExpr::getCast(C, Ty);
1416 } else {
Chris Lattnera84df0a22006-09-28 23:36:21 +00001417 if (C == ConstantBool::getTrue())
Chris Lattner1ece6f82005-01-01 15:59:57 +00001418 return ConstantIntegral::getAllOnesValue(Ty);
1419 else
1420 return ConstantIntegral::getNullValue(Ty);
1421 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001422}
1423
1424Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001425 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001426 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1427 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001428 if (C->getType() != Type::BoolTy)
1429 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001430 return ConstantExpr::getCast(C, Ty);
1431}
1432
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001433Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001434 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001435 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001436 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1437 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1438 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001439}
1440
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001441Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1442 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
Reid Spencere0fc4df2006-10-20 07:07:24 +00001443 static std::vector<Constant*> Indices(2, ConstantInt::get(Type::UIntTy, 0));
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001444
1445 return ConstantExpr::getGetElementPtr(C, Indices);
1446}
1447
Chris Lattnerb50d1352003-10-05 00:17:43 +00001448Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1449 Constant *C1, Constant *C2) {
Reid Spencerfdff9382006-11-08 06:47:33 +00001450 if (Opcode == Instruction::Shl || Opcode == Instruction::LShr ||
1451 Opcode == Instruction::AShr)
Chris Lattner5645e8a2004-01-12 19:04:55 +00001452 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001453 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001454 assert(Opcode >= Instruction::BinaryOpsBegin &&
1455 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001456 "Invalid opcode in binary constant expression");
1457 assert(C1->getType() == C2->getType() &&
1458 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001459
Chris Lattnere1496fb2006-09-17 19:14:47 +00001460 if (ReqTy == C1->getType() || (Instruction::isComparison(Opcode) &&
Chris Lattner29ca2c62004-08-04 18:50:09 +00001461 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001462 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1463 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001464
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001465 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001466 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001467 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001468}
1469
Chris Lattner29ca2c62004-08-04 18:50:09 +00001470Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001471#ifndef NDEBUG
1472 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00001473 case Instruction::Add:
1474 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001475 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001476 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001477 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
1478 isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001479 "Tried to create an arithmetic operation on a non-arithmetic type!");
1480 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001481 case Instruction::UDiv:
1482 case Instruction::SDiv:
1483 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1484 assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) &&
1485 cast<PackedType>(C1->getType())->getElementType()->isInteger())) &&
1486 "Tried to create an arithmetic operation on a non-arithmetic type!");
1487 break;
1488 case Instruction::FDiv:
1489 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1490 assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType())
1491 && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint()))
1492 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1493 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001494 case Instruction::URem:
1495 case Instruction::SRem:
1496 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1497 assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) &&
1498 cast<PackedType>(C1->getType())->getElementType()->isInteger())) &&
1499 "Tried to create an arithmetic operation on a non-arithmetic type!");
1500 break;
1501 case Instruction::FRem:
1502 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1503 assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType())
1504 && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint()))
1505 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1506 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001507 case Instruction::And:
1508 case Instruction::Or:
1509 case Instruction::Xor:
1510 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001511 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001512 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001513 break;
1514 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1515 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1516 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1517 break;
1518 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001519 case Instruction::LShr:
1520 case Instruction::AShr:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001521 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
Reid Spencerfdff9382006-11-08 06:47:33 +00001522 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001523 "Tried to create a shift operation on a non-integer type!");
1524 break;
1525 default:
1526 break;
1527 }
1528#endif
1529
Chris Lattnere1496fb2006-09-17 19:14:47 +00001530 if (Instruction::isComparison(Opcode))
Chris Lattner29ca2c62004-08-04 18:50:09 +00001531 return getTy(Type::BoolTy, Opcode, C1, C2);
1532 else
1533 return getTy(C1->getType(), Opcode, C1, C2);
1534}
1535
Chris Lattner6e415c02004-03-12 05:54:04 +00001536Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1537 Constant *V1, Constant *V2) {
1538 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1539 assert(V1->getType() == V2->getType() && "Select value types must match!");
1540 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1541
1542 if (ReqTy == V1->getType())
1543 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1544 return SC; // Fold common cases
1545
1546 std::vector<Constant*> argVec(3, C);
1547 argVec[1] = V1;
1548 argVec[2] = V2;
1549 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001550 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001551}
1552
Chris Lattner9eb2b522004-01-12 19:12:58 +00001553/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001554Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1555 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001556 // Check the operands for consistency first
Reid Spencerfdff9382006-11-08 06:47:33 +00001557 assert((Opcode == Instruction::Shl ||
1558 Opcode == Instruction::LShr ||
1559 Opcode == Instruction::AShr) &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001560 "Invalid opcode in binary constant expression");
1561 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1562 "Invalid operand types for Shift constant expr!");
1563
Chris Lattner0bba7712004-01-12 20:40:42 +00001564 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001565 return FC; // Fold a few common cases...
1566
1567 // Look up the constant in the table first to ensure uniqueness
1568 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001569 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001570 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001571}
1572
1573
Chris Lattnerb50d1352003-10-05 00:17:43 +00001574Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001575 const std::vector<Value*> &IdxList) {
1576 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001577 "GEP indices invalid!");
1578
Chris Lattneracdbe712003-04-17 19:24:48 +00001579 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1580 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001581
Chris Lattnerb50d1352003-10-05 00:17:43 +00001582 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001583 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001584 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001585 std::vector<Constant*> ArgVec;
1586 ArgVec.reserve(IdxList.size()+1);
1587 ArgVec.push_back(C);
1588 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1589 ArgVec.push_back(cast<Constant>(IdxList[i]));
1590 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001591 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001592}
1593
Chris Lattnerb50d1352003-10-05 00:17:43 +00001594Constant *ConstantExpr::getGetElementPtr(Constant *C,
1595 const std::vector<Constant*> &IdxList){
1596 // Get the result type of the getelementptr!
1597 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1598
1599 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1600 true);
1601 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001602 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1603}
1604
1605Constant *ConstantExpr::getGetElementPtr(Constant *C,
1606 const std::vector<Value*> &IdxList) {
1607 // Get the result type of the getelementptr!
1608 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1609 true);
1610 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001611 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1612}
1613
Robert Bocchino23004482006-01-10 19:05:34 +00001614Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1615 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001616 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1617 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001618 // Look up the constant in the table first to ensure uniqueness
1619 std::vector<Constant*> ArgVec(1, Val);
1620 ArgVec.push_back(Idx);
1621 const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001622 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001623}
1624
1625Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1626 assert(isa<PackedType>(Val->getType()) &&
1627 "Tried to create extractelement operation on non-packed type!");
1628 assert(Idx->getType() == Type::UIntTy &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001629 "Extractelement index must be uint type!");
Robert Bocchino23004482006-01-10 19:05:34 +00001630 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1631 Val, Idx);
1632}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001633
Robert Bocchinoca27f032006-01-17 20:07:22 +00001634Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1635 Constant *Elt, Constant *Idx) {
1636 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1637 return FC; // Fold a few common cases...
1638 // Look up the constant in the table first to ensure uniqueness
1639 std::vector<Constant*> ArgVec(1, Val);
1640 ArgVec.push_back(Elt);
1641 ArgVec.push_back(Idx);
1642 const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001643 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001644}
1645
1646Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1647 Constant *Idx) {
1648 assert(isa<PackedType>(Val->getType()) &&
1649 "Tried to create insertelement operation on non-packed type!");
1650 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
1651 && "Insertelement types must match!");
1652 assert(Idx->getType() == Type::UIntTy &&
1653 "Insertelement index must be uint type!");
1654 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
1655 Val, Elt, Idx);
1656}
1657
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001658Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1659 Constant *V2, Constant *Mask) {
1660 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1661 return FC; // Fold a few common cases...
1662 // Look up the constant in the table first to ensure uniqueness
1663 std::vector<Constant*> ArgVec(1, V1);
1664 ArgVec.push_back(V2);
1665 ArgVec.push_back(Mask);
1666 const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001667 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001668}
1669
1670Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1671 Constant *Mask) {
1672 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1673 "Invalid shuffle vector constant expr operands!");
1674 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1675}
1676
1677
Vikram S. Adve4c485332002-07-15 18:19:33 +00001678// destroyConstant - Remove the constant from the constant table...
1679//
1680void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001681 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001682 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001683}
1684
Chris Lattner3cd8c562002-07-30 18:54:25 +00001685const char *ConstantExpr::getOpcodeName() const {
1686 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001687}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001688
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001689//===----------------------------------------------------------------------===//
1690// replaceUsesOfWithOnConstant implementations
1691
1692void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001693 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001694 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001695 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001696
1697 unsigned OperandToUpdate = U-OperandList;
1698 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1699
Jim Laskeyc03caef2006-07-17 17:38:29 +00001700 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001701 Lookup.first.first = getType();
1702 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001703
Chris Lattnerb64419a2005-10-03 22:51:37 +00001704 std::vector<Constant*> &Values = Lookup.first.second;
1705 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001706
Chris Lattner8760ec72005-10-04 01:17:50 +00001707 // Fill values with the modified operands of the constant array. Also,
1708 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001709 bool isAllZeros = false;
1710 if (!ToC->isNullValue()) {
1711 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1712 Values.push_back(cast<Constant>(O->get()));
1713 } else {
1714 isAllZeros = true;
1715 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1716 Constant *Val = cast<Constant>(O->get());
1717 Values.push_back(Val);
1718 if (isAllZeros) isAllZeros = Val->isNullValue();
1719 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001720 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001721 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001722
Chris Lattnerb64419a2005-10-03 22:51:37 +00001723 Constant *Replacement = 0;
1724 if (isAllZeros) {
1725 Replacement = ConstantAggregateZero::get(getType());
1726 } else {
1727 // Check to see if we have this array type already.
1728 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001729 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001730 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001731
1732 if (Exists) {
1733 Replacement = I->second;
1734 } else {
1735 // Okay, the new shape doesn't exist in the system yet. Instead of
1736 // creating a new constant array, inserting it, replaceallusesof'ing the
1737 // old with the new, then deleting the old... just update the current one
1738 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001739 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001740
Chris Lattnerdff59112005-10-04 18:47:09 +00001741 // Update to the new value.
1742 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001743 return;
1744 }
1745 }
1746
1747 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001748 assert(Replacement != this && "I didn't contain From!");
1749
Chris Lattner7a1450d2005-10-04 18:13:04 +00001750 // Everyone using this now uses the replacement.
1751 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001752
1753 // Delete the old constant!
1754 destroyConstant();
1755}
1756
1757void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001758 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001759 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001760 Constant *ToC = cast<Constant>(To);
1761
Chris Lattnerdff59112005-10-04 18:47:09 +00001762 unsigned OperandToUpdate = U-OperandList;
1763 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1764
Jim Laskeyc03caef2006-07-17 17:38:29 +00001765 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001766 Lookup.first.first = getType();
1767 Lookup.second = this;
1768 std::vector<Constant*> &Values = Lookup.first.second;
1769 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001770
Chris Lattnerdff59112005-10-04 18:47:09 +00001771
Chris Lattner8760ec72005-10-04 01:17:50 +00001772 // Fill values with the modified operands of the constant struct. Also,
1773 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001774 bool isAllZeros = false;
1775 if (!ToC->isNullValue()) {
1776 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1777 Values.push_back(cast<Constant>(O->get()));
1778 } else {
1779 isAllZeros = true;
1780 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1781 Constant *Val = cast<Constant>(O->get());
1782 Values.push_back(Val);
1783 if (isAllZeros) isAllZeros = Val->isNullValue();
1784 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001785 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001786 Values[OperandToUpdate] = ToC;
1787
Chris Lattner8760ec72005-10-04 01:17:50 +00001788 Constant *Replacement = 0;
1789 if (isAllZeros) {
1790 Replacement = ConstantAggregateZero::get(getType());
1791 } else {
1792 // Check to see if we have this array type already.
1793 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001794 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001795 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001796
1797 if (Exists) {
1798 Replacement = I->second;
1799 } else {
1800 // Okay, the new shape doesn't exist in the system yet. Instead of
1801 // creating a new constant struct, inserting it, replaceallusesof'ing the
1802 // old with the new, then deleting the old... just update the current one
1803 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001804 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001805
Chris Lattnerdff59112005-10-04 18:47:09 +00001806 // Update to the new value.
1807 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001808 return;
1809 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001810 }
1811
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001812 assert(Replacement != this && "I didn't contain From!");
1813
Chris Lattner7a1450d2005-10-04 18:13:04 +00001814 // Everyone using this now uses the replacement.
1815 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001816
1817 // Delete the old constant!
1818 destroyConstant();
1819}
1820
1821void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001822 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001823 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1824
1825 std::vector<Constant*> Values;
1826 Values.reserve(getNumOperands()); // Build replacement array...
1827 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1828 Constant *Val = getOperand(i);
1829 if (Val == From) Val = cast<Constant>(To);
1830 Values.push_back(Val);
1831 }
1832
1833 Constant *Replacement = ConstantPacked::get(getType(), Values);
1834 assert(Replacement != this && "I didn't contain From!");
1835
Chris Lattner7a1450d2005-10-04 18:13:04 +00001836 // Everyone using this now uses the replacement.
1837 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001838
1839 // Delete the old constant!
1840 destroyConstant();
1841}
1842
1843void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001844 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001845 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1846 Constant *To = cast<Constant>(ToV);
1847
1848 Constant *Replacement = 0;
1849 if (getOpcode() == Instruction::GetElementPtr) {
1850 std::vector<Constant*> Indices;
1851 Constant *Pointer = getOperand(0);
1852 Indices.reserve(getNumOperands()-1);
1853 if (Pointer == From) Pointer = To;
1854
1855 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1856 Constant *Val = getOperand(i);
1857 if (Val == From) Val = To;
1858 Indices.push_back(Val);
1859 }
1860 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
1861 } else if (getOpcode() == Instruction::Cast) {
1862 assert(getOperand(0) == From && "Cast only has one use!");
1863 Replacement = ConstantExpr::getCast(To, getType());
1864 } else if (getOpcode() == Instruction::Select) {
1865 Constant *C1 = getOperand(0);
1866 Constant *C2 = getOperand(1);
1867 Constant *C3 = getOperand(2);
1868 if (C1 == From) C1 = To;
1869 if (C2 == From) C2 = To;
1870 if (C3 == From) C3 = To;
1871 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00001872 } else if (getOpcode() == Instruction::ExtractElement) {
1873 Constant *C1 = getOperand(0);
1874 Constant *C2 = getOperand(1);
1875 if (C1 == From) C1 = To;
1876 if (C2 == From) C2 = To;
1877 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00001878 } else if (getOpcode() == Instruction::InsertElement) {
1879 Constant *C1 = getOperand(0);
1880 Constant *C2 = getOperand(1);
1881 Constant *C3 = getOperand(1);
1882 if (C1 == From) C1 = To;
1883 if (C2 == From) C2 = To;
1884 if (C3 == From) C3 = To;
1885 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
1886 } else if (getOpcode() == Instruction::ShuffleVector) {
1887 Constant *C1 = getOperand(0);
1888 Constant *C2 = getOperand(1);
1889 Constant *C3 = getOperand(2);
1890 if (C1 == From) C1 = To;
1891 if (C2 == From) C2 = To;
1892 if (C3 == From) C3 = To;
1893 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001894 } else if (getNumOperands() == 2) {
1895 Constant *C1 = getOperand(0);
1896 Constant *C2 = getOperand(1);
1897 if (C1 == From) C1 = To;
1898 if (C2 == From) C2 = To;
1899 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
1900 } else {
1901 assert(0 && "Unknown ConstantExpr type!");
1902 return;
1903 }
1904
1905 assert(Replacement != this && "I didn't contain From!");
1906
Chris Lattner7a1450d2005-10-04 18:13:04 +00001907 // Everyone using this now uses the replacement.
1908 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001909
1910 // Delete the old constant!
1911 destroyConstant();
1912}
1913
1914
Jim Laskey2698f0d2006-03-08 18:11:07 +00001915/// getStringValue - Turn an LLVM constant pointer that eventually points to a
1916/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00001917/// Parameter Chop determines if the result is chopped at the first null
1918/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00001919///
Evan Cheng38280c02006-03-10 23:52:03 +00001920std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00001921 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
1922 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
1923 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
1924 if (Init->isString()) {
1925 std::string Result = Init->getAsString();
1926 if (Offset < Result.size()) {
1927 // If we are pointing INTO The string, erase the beginning...
1928 Result.erase(Result.begin(), Result.begin()+Offset);
1929
1930 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00001931 if (Chop) {
1932 std::string::size_type NullPos = Result.find_first_of((char)0);
1933 if (NullPos != std::string::npos)
1934 Result.erase(Result.begin()+NullPos, Result.end());
1935 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00001936 return Result;
1937 }
1938 }
1939 }
1940 } else if (Constant *C = dyn_cast<Constant>(this)) {
1941 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00001942 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00001943 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1944 if (CE->getOpcode() == Instruction::GetElementPtr) {
1945 // Turn a gep into the specified offset.
1946 if (CE->getNumOperands() == 3 &&
1947 cast<Constant>(CE->getOperand(1))->isNullValue() &&
1948 isa<ConstantInt>(CE->getOperand(2))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00001949 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00001950 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00001951 }
1952 }
1953 }
1954 }
1955 return "";
1956}
1957