blob: e5417f524564d25043711b4896a423fb20633d7b [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 Lattner33e93b82007-02-27 03:05:06 +000015#include "ConstantFold.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 Lattnerd7a73302001-10-13 06:57:33 +000019#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000021#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000022#include "llvm/Support/Debug.h"
Chris Lattner69edc982006-09-28 00:35:06 +000023#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000024#include "llvm/Support/MathExtras.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000025#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000026#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000027#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000028#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chris Lattner2f7c9632001-06-06 20:29:01 +000031//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000032// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000033//===----------------------------------------------------------------------===//
34
Chris Lattner3462ae32001-12-03 22:26:30 +000035void Constant::destroyConstantImpl() {
36 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000037 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000038 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000039 // but they don't know that. Because we only find out when the CPV is
40 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000041 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000042 //
43 while (!use_empty()) {
44 Value *V = use_back();
45#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000046 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000047 DOUT << "While deleting: " << *this
48 << "\n\nUse still stuck around after Def is destroyed: "
49 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000050#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000051 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000052 Constant *CV = cast<Constant>(V);
53 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000054
55 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000056 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000057 }
58
59 // Value has no outstanding references it is safe to delete it now...
60 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000061}
Chris Lattner2f7c9632001-06-06 20:29:01 +000062
Chris Lattner23dd1f62006-10-20 00:27:06 +000063/// canTrap - Return true if evaluation of this constant could trap. This is
64/// true for things like constant expressions that could divide by zero.
65bool Constant::canTrap() const {
66 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
67 // The only thing that could possibly trap are constant exprs.
68 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
69 if (!CE) return false;
70
71 // ConstantExpr traps if any operands can trap.
72 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
73 if (getOperand(i)->canTrap())
74 return true;
75
76 // Otherwise, only specific operations can trap.
77 switch (CE->getOpcode()) {
78 default:
79 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000080 case Instruction::UDiv:
81 case Instruction::SDiv:
82 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000083 case Instruction::URem:
84 case Instruction::SRem:
85 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000086 // Div and rem can trap if the RHS is not known to be non-zero.
87 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
88 return true;
89 return false;
90 }
91}
92
Evan Chengf9e003b2007-03-08 00:59:12 +000093/// ContaintsRelocations - Return true if the constant value contains
94/// relocations which cannot be resolved at compile time.
95bool Constant::ContainsRelocations() const {
96 if (isa<GlobalValue>(this))
97 return true;
98 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
99 if (getOperand(i)->ContainsRelocations())
100 return true;
101 return false;
102}
103
Chris Lattnerb1585a92002-08-13 17:50:20 +0000104// Static constructor to create a '0' constant of arbitrary type...
105Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000106 switch (Ty->getTypeID()) {
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000107 case Type::IntegerTyID:
108 return ConstantInt::get(Ty, 0);
109 case Type::FloatTyID:
110 case Type::DoubleTyID:
111 return ConstantFP::get(Ty, 0.0);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000112 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000113 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner9fba3da2004-02-15 05:53:04 +0000114 case Type::StructTyID:
115 case Type::ArrayTyID:
Reid Spencerd84d35b2007-02-15 02:26:10 +0000116 case Type::VectorTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000117 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000118 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000119 // Function, Label, or Opaque type?
120 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000121 return 0;
122 }
123}
124
Chris Lattnerb1585a92002-08-13 17:50:20 +0000125
126// Static constructor to create an integral constant with all bits set
Zhou Sheng75b871f2007-01-11 12:24:14 +0000127ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000128 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000129 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000130 return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000131}
132
Chris Lattnerecab54c2007-01-04 01:49:26 +0000133/// @returns the value for an packed integer constant of the given type that
134/// has all its bits set to true.
135/// @brief Get the all ones value
Reid Spencerd84d35b2007-02-15 02:26:10 +0000136ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) {
Chris Lattnerecab54c2007-01-04 01:49:26 +0000137 std::vector<Constant*> Elts;
138 Elts.resize(Ty->getNumElements(),
Zhou Sheng75b871f2007-01-11 12:24:14 +0000139 ConstantInt::getAllOnesValue(Ty->getElementType()));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000140 assert(Elts[0] && "Not a packed integer type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +0000141 return cast<ConstantVector>(ConstantVector::get(Elts));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000142}
143
144
Chris Lattner2f7c9632001-06-06 20:29:01 +0000145//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000146// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000147//===----------------------------------------------------------------------===//
148
Reid Spencerb31bffe2007-02-26 23:54:03 +0000149ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000150 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000151 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000152}
153
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000154ConstantInt *ConstantInt::TheTrueVal = 0;
155ConstantInt *ConstantInt::TheFalseVal = 0;
156
157namespace llvm {
158 void CleanupTrueFalse(void *) {
159 ConstantInt::ResetTrueFalse();
160 }
161}
162
163static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
164
165ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
166 assert(TheTrueVal == 0 && TheFalseVal == 0);
167 TheTrueVal = get(Type::Int1Ty, 1);
168 TheFalseVal = get(Type::Int1Ty, 0);
169
170 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
171 TrueFalseCleanup.Register();
172
173 return WhichOne ? TheTrueVal : TheFalseVal;
174}
175
176
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000177namespace {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000178 struct DenseMapAPIntKeyInfo {
179 struct KeyTy {
180 APInt val;
181 const Type* type;
182 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
183 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
184 bool operator==(const KeyTy& that) const {
185 return type == that.type && this->val == that.val;
186 }
187 bool operator!=(const KeyTy& that) const {
188 return !this->operator==(that);
189 }
190 };
191 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
192 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000193 static unsigned getHashValue(const KeyTy &Key) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000194 return DenseMapKeyInfo<void*>::getHashValue(Key.type) ^
195 Key.val.getHashValue();
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000196 }
197 static bool isPod() { return true; }
198 };
199}
200
201
Reid Spencerb31bffe2007-02-26 23:54:03 +0000202typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
203 DenseMapAPIntKeyInfo> IntMapTy;
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000204static ManagedStatic<IntMapTy> IntConstants;
205
Reid Spencer362fb292007-03-19 20:39:08 +0000206ConstantInt *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000207 const IntegerType *ITy = cast<IntegerType>(Ty);
Reid Spencer362fb292007-03-19 20:39:08 +0000208 return get(APInt(ITy->getBitWidth(), V, isSigned));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000209}
210
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000211// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
212// as the key, is a DensMapAPIntKeyInfo::KeyTy which has provided the
Reid Spencerb31bffe2007-02-26 23:54:03 +0000213// operator== and operator!= to ensure that the DenseMap doesn't attempt to
214// compare APInt's of different widths, which would violate an APInt class
215// invariant which generates an assertion.
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000216ConstantInt *ConstantInt::get(const APInt& V) {
217 // Get the corresponding integer type for the bit width of the value.
218 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Reid Spencerb31bffe2007-02-26 23:54:03 +0000219 // get an existing value or the insertion position
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000220 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Reid Spencerb31bffe2007-02-26 23:54:03 +0000221 ConstantInt *&Slot = (*IntConstants)[Key];
222 // if it exists, return it.
223 if (Slot)
224 return Slot;
225 // otherwise create a new one, insert it, and return it.
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000226 return Slot = new ConstantInt(ITy, V);
227}
228
229//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000230// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000231//===----------------------------------------------------------------------===//
232
233
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000234ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000235 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000236 Val = V;
237}
238
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000239bool ConstantFP::isNullValue() const {
240 return DoubleToBits(Val) == 0;
241}
242
243bool ConstantFP::isExactlyValue(double V) const {
244 return DoubleToBits(V) == DoubleToBits(Val);
245}
246
247
248namespace {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000249 struct DenseMapInt64KeyInfo {
250 typedef std::pair<uint64_t, const Type*> KeyTy;
251 static inline KeyTy getEmptyKey() { return KeyTy(0, 0); }
252 static inline KeyTy getTombstoneKey() { return KeyTy(1, 0); }
253 static unsigned getHashValue(const KeyTy &Key) {
254 return DenseMapKeyInfo<void*>::getHashValue(Key.second) ^ Key.first;
255 }
256 static bool isPod() { return true; }
257 };
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000258 struct DenseMapInt32KeyInfo {
259 typedef std::pair<uint32_t, const Type*> KeyTy;
260 static inline KeyTy getEmptyKey() { return KeyTy(0, 0); }
261 static inline KeyTy getTombstoneKey() { return KeyTy(1, 0); }
262 static unsigned getHashValue(const KeyTy &Key) {
263 return DenseMapKeyInfo<void*>::getHashValue(Key.second) ^ Key.first;
264 }
265 static bool isPod() { return true; }
266 };
267}
268
269//---- ConstantFP::get() implementation...
270//
271typedef DenseMap<DenseMapInt32KeyInfo::KeyTy, ConstantFP*,
272 DenseMapInt32KeyInfo> FloatMapTy;
273typedef DenseMap<DenseMapInt64KeyInfo::KeyTy, ConstantFP*,
274 DenseMapInt64KeyInfo> DoubleMapTy;
275
276static ManagedStatic<FloatMapTy> FloatConstants;
277static ManagedStatic<DoubleMapTy> DoubleConstants;
278
279ConstantFP *ConstantFP::get(const Type *Ty, double V) {
280 if (Ty == Type::FloatTy) {
281 uint32_t IntVal = FloatToBits((float)V);
282
283 ConstantFP *&Slot = (*FloatConstants)[std::make_pair(IntVal, Ty)];
284 if (Slot) return Slot;
285 return Slot = new ConstantFP(Ty, (float)V);
286 } else {
287 assert(Ty == Type::DoubleTy);
288 uint64_t IntVal = DoubleToBits(V);
289 ConstantFP *&Slot = (*DoubleConstants)[std::make_pair(IntVal, Ty)];
290 if (Slot) return Slot;
Evan Cheng71b87232007-02-20 21:30:56 +0000291 return Slot = new ConstantFP(Ty, V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000292 }
293}
294
295
296//===----------------------------------------------------------------------===//
297// ConstantXXX Classes
298//===----------------------------------------------------------------------===//
299
300
Chris Lattner3462ae32001-12-03 22:26:30 +0000301ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000302 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000303 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000304 assert(V.size() == T->getNumElements() &&
305 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000306 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000307 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
308 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000309 Constant *C = *I;
310 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000311 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000312 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000313 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000314 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000315 }
316}
317
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000318ConstantArray::~ConstantArray() {
319 delete [] OperandList;
320}
321
Chris Lattner3462ae32001-12-03 22:26:30 +0000322ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000323 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000324 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000325 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000326 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000327 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000328 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
329 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000330 Constant *C = *I;
331 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000332 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000333 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000334 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000335 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000336 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000337 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000338 }
339}
340
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000341ConstantStruct::~ConstantStruct() {
342 delete [] OperandList;
343}
344
345
Reid Spencerd84d35b2007-02-15 02:26:10 +0000346ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000347 const std::vector<Constant*> &V)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000348 : Constant(T, ConstantVectorVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000349 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000350 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
351 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000352 Constant *C = *I;
353 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000354 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000355 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000356 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000357 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000358 }
359}
360
Reid Spencerd84d35b2007-02-15 02:26:10 +0000361ConstantVector::~ConstantVector() {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000362 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000363}
364
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000365// We declare several classes private to this file, so use an anonymous
366// namespace
367namespace {
368
369/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
370/// behind the scenes to implement unary constant exprs.
371class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
372 Use Op;
373public:
374 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
375 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
376};
377
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000378/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
379/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000380class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000381 Use Ops[2];
382public:
383 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Reid Spencer266e42b2006-12-23 06:05:41 +0000384 : ConstantExpr(C1->getType(), Opcode, Ops, 2) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000385 Ops[0].init(C1, this);
386 Ops[1].init(C2, this);
387 }
388};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000389
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000390/// SelectConstantExpr - This class is private to Constants.cpp, and is used
391/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000392class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000393 Use Ops[3];
394public:
395 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
396 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
397 Ops[0].init(C1, this);
398 Ops[1].init(C2, this);
399 Ops[2].init(C3, this);
400 }
401};
402
Robert Bocchinoca27f032006-01-17 20:07:22 +0000403/// ExtractElementConstantExpr - This class is private to
404/// Constants.cpp, and is used behind the scenes to implement
405/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000406class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000407 Use Ops[2];
408public:
409 ExtractElementConstantExpr(Constant *C1, Constant *C2)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000410 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +0000411 Instruction::ExtractElement, Ops, 2) {
412 Ops[0].init(C1, this);
413 Ops[1].init(C2, this);
414 }
415};
416
Robert Bocchinoca27f032006-01-17 20:07:22 +0000417/// InsertElementConstantExpr - This class is private to
418/// Constants.cpp, and is used behind the scenes to implement
419/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000420class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000421 Use Ops[3];
422public:
423 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
424 : ConstantExpr(C1->getType(), Instruction::InsertElement,
425 Ops, 3) {
426 Ops[0].init(C1, this);
427 Ops[1].init(C2, this);
428 Ops[2].init(C3, this);
429 }
430};
431
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000432/// ShuffleVectorConstantExpr - This class is private to
433/// Constants.cpp, and is used behind the scenes to implement
434/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000435class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000436 Use Ops[3];
437public:
438 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
439 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
440 Ops, 3) {
441 Ops[0].init(C1, this);
442 Ops[1].init(C2, this);
443 Ops[2].init(C3, this);
444 }
445};
446
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000447/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
448/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000449struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000450 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
451 const Type *DestTy)
452 : ConstantExpr(DestTy, Instruction::GetElementPtr,
453 new Use[IdxList.size()+1], IdxList.size()+1) {
454 OperandList[0].init(C, this);
455 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
456 OperandList[i+1].init(IdxList[i], this);
457 }
458 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000459 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000460 }
461};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000462
463// CompareConstantExpr - This class is private to Constants.cpp, and is used
464// behind the scenes to implement ICmp and FCmp constant expressions. This is
465// needed in order to store the predicate value for these instructions.
466struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
467 unsigned short predicate;
468 Use Ops[2];
469 CompareConstantExpr(Instruction::OtherOps opc, unsigned short pred,
470 Constant* LHS, Constant* RHS)
Reid Spencer542964f2007-01-11 18:21:29 +0000471 : ConstantExpr(Type::Int1Ty, opc, Ops, 2), predicate(pred) {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000472 OperandList[0].init(LHS, this);
473 OperandList[1].init(RHS, this);
474 }
475};
476
477} // end anonymous namespace
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000478
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000479
480// Utility function for determining if a ConstantExpr is a CastOp or not. This
481// can't be inline because we don't want to #include Instruction.h into
482// Constant.h
483bool ConstantExpr::isCast() const {
484 return Instruction::isCast(getOpcode());
485}
486
Reid Spenceree3c9912006-12-04 05:19:50 +0000487bool ConstantExpr::isCompare() const {
488 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
489}
490
Chris Lattner817175f2004-03-29 02:37:53 +0000491/// ConstantExpr::get* - Return some common constants without having to
492/// specify the full Instruction::OPCODE identifier.
493///
494Constant *ConstantExpr::getNeg(Constant *C) {
Reid Spencer2eadb532007-01-21 00:29:26 +0000495 return get(Instruction::Sub,
496 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
497 C);
Chris Lattner817175f2004-03-29 02:37:53 +0000498}
499Constant *ConstantExpr::getNot(Constant *C) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000500 assert(isa<ConstantInt>(C) && "Cannot NOT a nonintegral type!");
Chris Lattner817175f2004-03-29 02:37:53 +0000501 return get(Instruction::Xor, C,
Zhou Sheng75b871f2007-01-11 12:24:14 +0000502 ConstantInt::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000503}
504Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
505 return get(Instruction::Add, C1, C2);
506}
507Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
508 return get(Instruction::Sub, C1, C2);
509}
510Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
511 return get(Instruction::Mul, C1, C2);
512}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000513Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
514 return get(Instruction::UDiv, C1, C2);
515}
516Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
517 return get(Instruction::SDiv, C1, C2);
518}
519Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
520 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000521}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000522Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
523 return get(Instruction::URem, C1, C2);
524}
525Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
526 return get(Instruction::SRem, C1, C2);
527}
528Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
529 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000530}
531Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
532 return get(Instruction::And, C1, C2);
533}
534Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
535 return get(Instruction::Or, C1, C2);
536}
537Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
538 return get(Instruction::Xor, C1, C2);
539}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000540unsigned ConstantExpr::getPredicate() const {
541 assert(getOpcode() == Instruction::FCmp || getOpcode() == Instruction::ICmp);
542 return dynamic_cast<const CompareConstantExpr*>(this)->predicate;
543}
Chris Lattner817175f2004-03-29 02:37:53 +0000544Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
545 return get(Instruction::Shl, C1, C2);
546}
Reid Spencerfdff9382006-11-08 06:47:33 +0000547Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
548 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000549}
Reid Spencerfdff9382006-11-08 06:47:33 +0000550Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
551 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000552}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000553
Chris Lattner7c1018a2006-07-14 19:37:40 +0000554/// getWithOperandReplaced - Return a constant expression identical to this
555/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000556Constant *
557ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000558 assert(OpNo < getNumOperands() && "Operand num is out of range!");
559 assert(Op->getType() == getOperand(OpNo)->getType() &&
560 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000561 if (getOperand(OpNo) == Op)
562 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000563
Chris Lattner227816342006-07-14 22:20:01 +0000564 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000565 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000566 case Instruction::Trunc:
567 case Instruction::ZExt:
568 case Instruction::SExt:
569 case Instruction::FPTrunc:
570 case Instruction::FPExt:
571 case Instruction::UIToFP:
572 case Instruction::SIToFP:
573 case Instruction::FPToUI:
574 case Instruction::FPToSI:
575 case Instruction::PtrToInt:
576 case Instruction::IntToPtr:
577 case Instruction::BitCast:
578 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000579 case Instruction::Select:
580 Op0 = (OpNo == 0) ? Op : getOperand(0);
581 Op1 = (OpNo == 1) ? Op : getOperand(1);
582 Op2 = (OpNo == 2) ? Op : getOperand(2);
583 return ConstantExpr::getSelect(Op0, Op1, Op2);
584 case Instruction::InsertElement:
585 Op0 = (OpNo == 0) ? Op : getOperand(0);
586 Op1 = (OpNo == 1) ? Op : getOperand(1);
587 Op2 = (OpNo == 2) ? Op : getOperand(2);
588 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
589 case Instruction::ExtractElement:
590 Op0 = (OpNo == 0) ? Op : getOperand(0);
591 Op1 = (OpNo == 1) ? Op : getOperand(1);
592 return ConstantExpr::getExtractElement(Op0, Op1);
593 case Instruction::ShuffleVector:
594 Op0 = (OpNo == 0) ? Op : getOperand(0);
595 Op1 = (OpNo == 1) ? Op : getOperand(1);
596 Op2 = (OpNo == 2) ? Op : getOperand(2);
597 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000598 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000599 SmallVector<Constant*, 8> Ops;
600 Ops.resize(getNumOperands());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000601 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000602 Ops[i] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000603 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000604 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000605 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000606 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000607 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000608 default:
609 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000610 Op0 = (OpNo == 0) ? Op : getOperand(0);
611 Op1 = (OpNo == 1) ? Op : getOperand(1);
612 return ConstantExpr::get(getOpcode(), Op0, Op1);
613 }
614}
615
616/// getWithOperands - This returns the current constant expression with the
617/// operands replaced with the specified values. The specified operands must
618/// match count and type with the existing ones.
619Constant *ConstantExpr::
620getWithOperands(const std::vector<Constant*> &Ops) const {
621 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
622 bool AnyChange = false;
623 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
624 assert(Ops[i]->getType() == getOperand(i)->getType() &&
625 "Operand type mismatch!");
626 AnyChange |= Ops[i] != getOperand(i);
627 }
628 if (!AnyChange) // No operands changed, return self.
629 return const_cast<ConstantExpr*>(this);
630
631 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000632 case Instruction::Trunc:
633 case Instruction::ZExt:
634 case Instruction::SExt:
635 case Instruction::FPTrunc:
636 case Instruction::FPExt:
637 case Instruction::UIToFP:
638 case Instruction::SIToFP:
639 case Instruction::FPToUI:
640 case Instruction::FPToSI:
641 case Instruction::PtrToInt:
642 case Instruction::IntToPtr:
643 case Instruction::BitCast:
644 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000645 case Instruction::Select:
646 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
647 case Instruction::InsertElement:
648 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
649 case Instruction::ExtractElement:
650 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
651 case Instruction::ShuffleVector:
652 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000653 case Instruction::GetElementPtr:
654 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], Ops.size()-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000655 case Instruction::ICmp:
656 case Instruction::FCmp:
657 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000658 default:
659 assert(getNumOperands() == 2 && "Must be binary operator?");
660 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000661 }
662}
663
Chris Lattner2f7c9632001-06-06 20:29:01 +0000664
665//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000666// isValueValidForType implementations
667
Reid Spencere7334722006-12-19 01:28:19 +0000668bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000669 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000670 if (Ty == Type::Int1Ty)
671 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000672 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000673 return true; // always true, has to fit in largest type
674 uint64_t Max = (1ll << NumBits) - 1;
675 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000676}
677
Reid Spencere0fc4df2006-10-20 07:07:24 +0000678bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000679 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000680 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000681 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000682 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000683 return true; // always true, has to fit in largest type
684 int64_t Min = -(1ll << (NumBits-1));
685 int64_t Max = (1ll << (NumBits-1)) - 1;
686 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000687}
688
Chris Lattner3462ae32001-12-03 22:26:30 +0000689bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000690 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000691 default:
692 return false; // These can't be represented as floating point!
693
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000694 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000695 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000696 case Type::DoubleTyID:
697 return true; // This is the largest type...
698 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000699}
Chris Lattner9655e542001-07-20 19:16:02 +0000700
Chris Lattner49d855c2001-09-07 16:46:31 +0000701//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000702// Factory Function Implementation
703
Chris Lattner98fa07b2003-05-23 20:03:32 +0000704// ConstantCreator - A class that is used to create constants by
705// ValueMap*. This class should be partially specialized if there is
706// something strange that needs to be done to interface to the ctor for the
707// constant.
708//
Chris Lattner189d19f2003-11-21 20:23:48 +0000709namespace llvm {
710 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000711 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000712 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
713 return new ConstantClass(Ty, V);
714 }
715 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000716
Chris Lattner189d19f2003-11-21 20:23:48 +0000717 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000718 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000719 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
720 assert(0 && "This type cannot be converted!\n");
721 abort();
722 }
723 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000724
Chris Lattner935aa922005-10-04 17:48:46 +0000725 template<class ValType, class TypeClass, class ConstantClass,
726 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000727 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000728 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000729 typedef std::pair<const Type*, ValType> MapKey;
730 typedef std::map<MapKey, Constant *> MapTy;
731 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
732 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000733 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000734 /// Map - This is the main map from the element descriptor to the Constants.
735 /// This is the primary way we avoid creating two of the same shape
736 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000737 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000738
739 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
740 /// from the constants to their element in Map. This is important for
741 /// removal of constants from the array, which would otherwise have to scan
742 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000743 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000744
Jim Laskeyc03caef2006-07-17 17:38:29 +0000745 /// AbstractTypeMap - Map for abstract type constants.
746 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000747 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000748
Chris Lattner98fa07b2003-05-23 20:03:32 +0000749 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000750 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000751
752 /// InsertOrGetItem - Return an iterator for the specified element.
753 /// If the element exists in the map, the returned iterator points to the
754 /// entry and Exists=true. If not, the iterator points to the newly
755 /// inserted entry and returns Exists=false. Newly inserted entries have
756 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000757 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
758 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000759 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000760 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000761 Exists = !IP.second;
762 return IP.first;
763 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000764
Chris Lattner935aa922005-10-04 17:48:46 +0000765private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000766 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000767 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000768 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000769 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
770 IMI->second->second == CP &&
771 "InverseMap corrupt!");
772 return IMI->second;
773 }
774
Jim Laskeyc03caef2006-07-17 17:38:29 +0000775 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +0000776 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000777 if (I == Map.end() || I->second != CP) {
778 // FIXME: This should not use a linear scan. If this gets to be a
779 // performance problem, someone should look at this.
780 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
781 /* empty */;
782 }
Chris Lattner935aa922005-10-04 17:48:46 +0000783 return I;
784 }
785public:
786
Chris Lattnerb64419a2005-10-03 22:51:37 +0000787 /// getOrCreate - Return the specified constant from the map, creating it if
788 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000789 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000790 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000791 typename MapTy::iterator I = Map.lower_bound(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000792 // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +0000793 if (I != Map.end() && I->first == Lookup)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000794 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000795
796 // If no preexisting value, create one now...
797 ConstantClass *Result =
798 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
799
Chris Lattnerb50d1352003-10-05 00:17:43 +0000800 /// FIXME: why does this assert fail when loading 176.gcc?
801 //assert(Result->getType() == Ty && "Type specified is not correct!");
802 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
803
Chris Lattner935aa922005-10-04 17:48:46 +0000804 if (HasLargeKey) // Remember the reverse mapping if needed.
805 InverseMap.insert(std::make_pair(Result, I));
806
Chris Lattnerb50d1352003-10-05 00:17:43 +0000807 // If the type of the constant is abstract, make sure that an entry exists
808 // for it in the AbstractTypeMap.
809 if (Ty->isAbstract()) {
810 typename AbstractTypeMapTy::iterator TI =
811 AbstractTypeMap.lower_bound(Ty);
812
813 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
814 // Add ourselves to the ATU list of the type.
815 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
816
817 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
818 }
819 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000820 return Result;
821 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000822
Chris Lattner98fa07b2003-05-23 20:03:32 +0000823 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000824 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000825 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000826 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000827
Chris Lattner935aa922005-10-04 17:48:46 +0000828 if (HasLargeKey) // Remember the reverse mapping if needed.
829 InverseMap.erase(CP);
830
Chris Lattnerb50d1352003-10-05 00:17:43 +0000831 // Now that we found the entry, make sure this isn't the entry that
832 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000833 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000834 if (Ty->isAbstract()) {
835 assert(AbstractTypeMap.count(Ty) &&
836 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000837 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000838 if (ATMEntryIt == I) {
839 // Yes, we are removing the representative entry for this type.
840 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000841 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000842
Chris Lattnerb50d1352003-10-05 00:17:43 +0000843 // First check the entry before this one...
844 if (TmpIt != Map.begin()) {
845 --TmpIt;
846 if (TmpIt->first.first != Ty) // Not the same type, move back...
847 ++TmpIt;
848 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000849
Chris Lattnerb50d1352003-10-05 00:17:43 +0000850 // If we didn't find the same type, try to move forward...
851 if (TmpIt == ATMEntryIt) {
852 ++TmpIt;
853 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
854 --TmpIt; // No entry afterwards with the same type
855 }
856
857 // If there is another entry in the map of the same abstract type,
858 // update the AbstractTypeMap entry now.
859 if (TmpIt != ATMEntryIt) {
860 ATMEntryIt = TmpIt;
861 } else {
862 // Otherwise, we are removing the last instance of this type
863 // from the table. Remove from the ATM, and from user list.
864 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
865 AbstractTypeMap.erase(Ty);
866 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000867 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000868 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000869
Chris Lattnerb50d1352003-10-05 00:17:43 +0000870 Map.erase(I);
871 }
872
Chris Lattner3b793c62005-10-04 21:35:50 +0000873
874 /// MoveConstantToNewSlot - If we are about to change C to be the element
875 /// specified by I, update our internal data structures to reflect this
876 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000877 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000878 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000879 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000880 assert(OldI != Map.end() && "Constant not found in constant table!");
881 assert(OldI->second == C && "Didn't find correct element?");
882
883 // If this constant is the representative element for its abstract type,
884 // update the AbstractTypeMap so that the representative element is I.
885 if (C->getType()->isAbstract()) {
886 typename AbstractTypeMapTy::iterator ATI =
887 AbstractTypeMap.find(C->getType());
888 assert(ATI != AbstractTypeMap.end() &&
889 "Abstract type not in AbstractTypeMap?");
890 if (ATI->second == OldI)
891 ATI->second = I;
892 }
893
894 // Remove the old entry from the map.
895 Map.erase(OldI);
896
897 // Update the inverse map so that we know that this constant is now
898 // located at descriptor I.
899 if (HasLargeKey) {
900 assert(I->second == C && "Bad inversemap entry!");
901 InverseMap[C] = I;
902 }
903 }
904
Chris Lattnerb50d1352003-10-05 00:17:43 +0000905 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000906 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000907 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000908
909 assert(I != AbstractTypeMap.end() &&
910 "Abstract type not in AbstractTypeMap?");
911
912 // Convert a constant at a time until the last one is gone. The last one
913 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
914 // eliminated eventually.
915 do {
916 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +0000917 TypeClass>::convert(
918 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +0000919 cast<TypeClass>(NewTy));
920
Jim Laskeyc03caef2006-07-17 17:38:29 +0000921 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000922 } while (I != AbstractTypeMap.end());
923 }
924
925 // If the type became concrete without being refined to any other existing
926 // type, we just remove ourselves from the ATU list.
927 void typeBecameConcrete(const DerivedType *AbsTy) {
928 AbsTy->removeAbstractTypeUser(this);
929 }
930
931 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +0000932 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000933 }
934 };
935}
936
Chris Lattnera84df0a22006-09-28 23:36:21 +0000937
Chris Lattner28173502007-02-20 06:11:36 +0000938
Chris Lattner9fba3da2004-02-15 05:53:04 +0000939//---- ConstantAggregateZero::get() implementation...
940//
941namespace llvm {
942 // ConstantAggregateZero does not take extra "value" argument...
943 template<class ValType>
944 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
945 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
946 return new ConstantAggregateZero(Ty);
947 }
948 };
949
950 template<>
951 struct ConvertConstantType<ConstantAggregateZero, Type> {
952 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
953 // Make everyone now use a constant of the new type...
954 Constant *New = ConstantAggregateZero::get(NewTy);
955 assert(New != OldC && "Didn't replace constant??");
956 OldC->uncheckedReplaceAllUsesWith(New);
957 OldC->destroyConstant(); // This constant is now dead, destroy it.
958 }
959 };
960}
961
Chris Lattner69edc982006-09-28 00:35:06 +0000962static ManagedStatic<ValueMap<char, Type,
963 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +0000964
Chris Lattner3e650af2004-08-04 04:48:01 +0000965static char getValType(ConstantAggregateZero *CPZ) { return 0; }
966
Chris Lattner9fba3da2004-02-15 05:53:04 +0000967Constant *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +0000968 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000969 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +0000970 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000971}
972
973// destroyConstant - Remove the constant from the constant table...
974//
975void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000976 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000977 destroyConstantImpl();
978}
979
Chris Lattner3462ae32001-12-03 22:26:30 +0000980//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000981//
Chris Lattner189d19f2003-11-21 20:23:48 +0000982namespace llvm {
983 template<>
984 struct ConvertConstantType<ConstantArray, ArrayType> {
985 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
986 // Make everyone now use a constant of the new type...
987 std::vector<Constant*> C;
988 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
989 C.push_back(cast<Constant>(OldC->getOperand(i)));
990 Constant *New = ConstantArray::get(NewTy, C);
991 assert(New != OldC && "Didn't replace constant??");
992 OldC->uncheckedReplaceAllUsesWith(New);
993 OldC->destroyConstant(); // This constant is now dead, destroy it.
994 }
995 };
996}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000997
Chris Lattner3e650af2004-08-04 04:48:01 +0000998static std::vector<Constant*> getValType(ConstantArray *CA) {
999 std::vector<Constant*> Elements;
1000 Elements.reserve(CA->getNumOperands());
1001 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1002 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1003 return Elements;
1004}
1005
Chris Lattnerb64419a2005-10-03 22:51:37 +00001006typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001007 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001008static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001009
Chris Lattner015e8212004-02-15 04:14:47 +00001010Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +00001011 const std::vector<Constant*> &V) {
1012 // If this is an all-zero array, return a ConstantAggregateZero object
1013 if (!V.empty()) {
1014 Constant *C = V[0];
1015 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001016 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001017 for (unsigned i = 1, e = V.size(); i != e; ++i)
1018 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001019 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001020 }
1021 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001022}
1023
Chris Lattner98fa07b2003-05-23 20:03:32 +00001024// destroyConstant - Remove the constant from the constant table...
1025//
1026void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001027 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001028 destroyConstantImpl();
1029}
1030
Reid Spencer6f614532006-05-30 08:23:18 +00001031/// ConstantArray::get(const string&) - Return an array that is initialized to
1032/// contain the specified string. If length is zero then a null terminator is
1033/// added to the specified string so that it may be used in a natural way.
1034/// Otherwise, the length parameter specifies how much of the string to use
1035/// and it won't be null terminated.
1036///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001037Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001038 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001039 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +00001040 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001041
1042 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001043 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001044 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001045 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001046
Reid Spencer8d9336d2006-12-31 05:26:44 +00001047 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001048 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001049}
1050
Reid Spencer2546b762007-01-26 07:37:34 +00001051/// isString - This method returns true if the array is an array of i8, and
1052/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001053bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001054 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001055 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001056 return false;
1057 // Check the elements to make sure they are all integers, not constant
1058 // expressions.
1059 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1060 if (!isa<ConstantInt>(getOperand(i)))
1061 return false;
1062 return true;
1063}
1064
Evan Cheng3763c5b2006-10-26 19:15:05 +00001065/// isCString - This method returns true if the array is a string (see
1066/// isString) and it ends in a null byte \0 and does not contains any other
1067/// null bytes except its terminator.
1068bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001069 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001070 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001071 return false;
1072 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1073 // Last element must be a null.
1074 if (getOperand(getNumOperands()-1) != Zero)
1075 return false;
1076 // Other elements must be non-null integers.
1077 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1078 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001079 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001080 if (getOperand(i) == Zero)
1081 return false;
1082 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001083 return true;
1084}
1085
1086
Reid Spencer2546b762007-01-26 07:37:34 +00001087// getAsString - If the sub-element type of this array is i8
Chris Lattner81fabb02002-08-26 17:53:56 +00001088// then this method converts the array to an std::string and returns it.
1089// Otherwise, it asserts out.
1090//
1091std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001092 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001093 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001094 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001095 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001096 return Result;
1097}
1098
1099
Chris Lattner3462ae32001-12-03 22:26:30 +00001100//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001101//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001102
Chris Lattner189d19f2003-11-21 20:23:48 +00001103namespace llvm {
1104 template<>
1105 struct ConvertConstantType<ConstantStruct, StructType> {
1106 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1107 // Make everyone now use a constant of the new type...
1108 std::vector<Constant*> C;
1109 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1110 C.push_back(cast<Constant>(OldC->getOperand(i)));
1111 Constant *New = ConstantStruct::get(NewTy, C);
1112 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001113
Chris Lattner189d19f2003-11-21 20:23:48 +00001114 OldC->uncheckedReplaceAllUsesWith(New);
1115 OldC->destroyConstant(); // This constant is now dead, destroy it.
1116 }
1117 };
1118}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001119
Chris Lattner8760ec72005-10-04 01:17:50 +00001120typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001121 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001122static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001123
Chris Lattner3e650af2004-08-04 04:48:01 +00001124static std::vector<Constant*> getValType(ConstantStruct *CS) {
1125 std::vector<Constant*> Elements;
1126 Elements.reserve(CS->getNumOperands());
1127 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1128 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1129 return Elements;
1130}
1131
Chris Lattner015e8212004-02-15 04:14:47 +00001132Constant *ConstantStruct::get(const StructType *Ty,
1133 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001134 // Create a ConstantAggregateZero value if all elements are zeros...
1135 for (unsigned i = 0, e = V.size(); i != e; ++i)
1136 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001137 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001138
1139 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001140}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001141
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001142Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001143 std::vector<const Type*> StructEls;
1144 StructEls.reserve(V.size());
1145 for (unsigned i = 0, e = V.size(); i != e; ++i)
1146 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001147 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001148}
1149
Chris Lattnerd7a73302001-10-13 06:57:33 +00001150// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001151//
Chris Lattner3462ae32001-12-03 22:26:30 +00001152void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001153 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001154 destroyConstantImpl();
1155}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001156
Reid Spencerd84d35b2007-02-15 02:26:10 +00001157//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001158//
1159namespace llvm {
1160 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001161 struct ConvertConstantType<ConstantVector, VectorType> {
1162 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001163 // Make everyone now use a constant of the new type...
1164 std::vector<Constant*> C;
1165 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1166 C.push_back(cast<Constant>(OldC->getOperand(i)));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001167 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001168 assert(New != OldC && "Didn't replace constant??");
1169 OldC->uncheckedReplaceAllUsesWith(New);
1170 OldC->destroyConstant(); // This constant is now dead, destroy it.
1171 }
1172 };
1173}
1174
Reid Spencerd84d35b2007-02-15 02:26:10 +00001175static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001176 std::vector<Constant*> Elements;
1177 Elements.reserve(CP->getNumOperands());
1178 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1179 Elements.push_back(CP->getOperand(i));
1180 return Elements;
1181}
1182
Reid Spencerd84d35b2007-02-15 02:26:10 +00001183static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001184 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001185
Reid Spencerd84d35b2007-02-15 02:26:10 +00001186Constant *ConstantVector::get(const VectorType *Ty,
Brian Gaeke02209042004-08-20 06:00:58 +00001187 const std::vector<Constant*> &V) {
1188 // If this is an all-zero packed, return a ConstantAggregateZero object
1189 if (!V.empty()) {
1190 Constant *C = V[0];
1191 if (!C->isNullValue())
Reid Spencer09575ba2007-02-15 03:39:18 +00001192 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001193 for (unsigned i = 1, e = V.size(); i != e; ++i)
1194 if (V[i] != C)
Reid Spencer09575ba2007-02-15 03:39:18 +00001195 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001196 }
1197 return ConstantAggregateZero::get(Ty);
1198}
1199
Reid Spencerd84d35b2007-02-15 02:26:10 +00001200Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001201 assert(!V.empty() && "Cannot infer type if V is empty");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001202 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001203}
1204
1205// destroyConstant - Remove the constant from the constant table...
1206//
Reid Spencerd84d35b2007-02-15 02:26:10 +00001207void ConstantVector::destroyConstant() {
Reid Spencer09575ba2007-02-15 03:39:18 +00001208 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001209 destroyConstantImpl();
1210}
1211
Jim Laskeyf0478822007-01-12 22:39:14 +00001212/// This function will return true iff every element in this packed constant
1213/// is set to all ones.
1214/// @returns true iff this constant's emements are all set to all ones.
1215/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001216bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001217 // Check out first element.
1218 const Constant *Elt = getOperand(0);
1219 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1220 if (!CI || !CI->isAllOnesValue()) return false;
1221 // Then make sure all remaining elements point to the same value.
1222 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1223 if (getOperand(I) != Elt) return false;
1224 }
1225 return true;
1226}
1227
Chris Lattner3462ae32001-12-03 22:26:30 +00001228//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001229//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001230
Chris Lattner189d19f2003-11-21 20:23:48 +00001231namespace llvm {
1232 // ConstantPointerNull does not take extra "value" argument...
1233 template<class ValType>
1234 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1235 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1236 return new ConstantPointerNull(Ty);
1237 }
1238 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001239
Chris Lattner189d19f2003-11-21 20:23:48 +00001240 template<>
1241 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1242 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1243 // Make everyone now use a constant of the new type...
1244 Constant *New = ConstantPointerNull::get(NewTy);
1245 assert(New != OldC && "Didn't replace constant??");
1246 OldC->uncheckedReplaceAllUsesWith(New);
1247 OldC->destroyConstant(); // This constant is now dead, destroy it.
1248 }
1249 };
1250}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001251
Chris Lattner69edc982006-09-28 00:35:06 +00001252static ManagedStatic<ValueMap<char, PointerType,
1253 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001254
Chris Lattner3e650af2004-08-04 04:48:01 +00001255static char getValType(ConstantPointerNull *) {
1256 return 0;
1257}
1258
1259
Chris Lattner3462ae32001-12-03 22:26:30 +00001260ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001261 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001262}
1263
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001264// destroyConstant - Remove the constant from the constant table...
1265//
1266void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001267 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001268 destroyConstantImpl();
1269}
1270
1271
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001272//---- UndefValue::get() implementation...
1273//
1274
1275namespace llvm {
1276 // UndefValue does not take extra "value" argument...
1277 template<class ValType>
1278 struct ConstantCreator<UndefValue, Type, ValType> {
1279 static UndefValue *create(const Type *Ty, const ValType &V) {
1280 return new UndefValue(Ty);
1281 }
1282 };
1283
1284 template<>
1285 struct ConvertConstantType<UndefValue, Type> {
1286 static void convert(UndefValue *OldC, const Type *NewTy) {
1287 // Make everyone now use a constant of the new type.
1288 Constant *New = UndefValue::get(NewTy);
1289 assert(New != OldC && "Didn't replace constant??");
1290 OldC->uncheckedReplaceAllUsesWith(New);
1291 OldC->destroyConstant(); // This constant is now dead, destroy it.
1292 }
1293 };
1294}
1295
Chris Lattner69edc982006-09-28 00:35:06 +00001296static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001297
1298static char getValType(UndefValue *) {
1299 return 0;
1300}
1301
1302
1303UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001304 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001305}
1306
1307// destroyConstant - Remove the constant from the constant table.
1308//
1309void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001310 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001311 destroyConstantImpl();
1312}
1313
1314
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001315//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001316//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001317
Reid Spenceree3c9912006-12-04 05:19:50 +00001318struct ExprMapKeyType {
1319 explicit ExprMapKeyType(unsigned opc, std::vector<Constant*> ops,
Reid Spencerdba6aa42006-12-04 18:38:05 +00001320 unsigned short pred = 0) : opcode(opc), predicate(pred), operands(ops) { }
1321 uint16_t opcode;
1322 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001323 std::vector<Constant*> operands;
Reid Spenceree3c9912006-12-04 05:19:50 +00001324 bool operator==(const ExprMapKeyType& that) const {
1325 return this->opcode == that.opcode &&
1326 this->predicate == that.predicate &&
1327 this->operands == that.operands;
1328 }
1329 bool operator<(const ExprMapKeyType & that) const {
1330 return this->opcode < that.opcode ||
1331 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1332 (this->opcode == that.opcode && this->predicate == that.predicate &&
1333 this->operands < that.operands);
1334 }
1335
1336 bool operator!=(const ExprMapKeyType& that) const {
1337 return !(*this == that);
1338 }
1339};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001340
Chris Lattner189d19f2003-11-21 20:23:48 +00001341namespace llvm {
1342 template<>
1343 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001344 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1345 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001346 if (Instruction::isCast(V.opcode))
1347 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1348 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001349 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001350 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1351 if (V.opcode == Instruction::Select)
1352 return new SelectConstantExpr(V.operands[0], V.operands[1],
1353 V.operands[2]);
1354 if (V.opcode == Instruction::ExtractElement)
1355 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1356 if (V.opcode == Instruction::InsertElement)
1357 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1358 V.operands[2]);
1359 if (V.opcode == Instruction::ShuffleVector)
1360 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1361 V.operands[2]);
1362 if (V.opcode == Instruction::GetElementPtr) {
1363 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
1364 return new GetElementPtrConstantExpr(V.operands[0], IdxList, Ty);
1365 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001366
Reid Spenceree3c9912006-12-04 05:19:50 +00001367 // The compare instructions are weird. We have to encode the predicate
1368 // value and it is combined with the instruction opcode by multiplying
1369 // the opcode by one hundred. We must decode this to get the predicate.
1370 if (V.opcode == Instruction::ICmp)
1371 return new CompareConstantExpr(Instruction::ICmp, V.predicate,
1372 V.operands[0], V.operands[1]);
1373 if (V.opcode == Instruction::FCmp)
1374 return new CompareConstantExpr(Instruction::FCmp, V.predicate,
1375 V.operands[0], V.operands[1]);
1376 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001377 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001378 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001379 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001380
Chris Lattner189d19f2003-11-21 20:23:48 +00001381 template<>
1382 struct ConvertConstantType<ConstantExpr, Type> {
1383 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1384 Constant *New;
1385 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001386 case Instruction::Trunc:
1387 case Instruction::ZExt:
1388 case Instruction::SExt:
1389 case Instruction::FPTrunc:
1390 case Instruction::FPExt:
1391 case Instruction::UIToFP:
1392 case Instruction::SIToFP:
1393 case Instruction::FPToUI:
1394 case Instruction::FPToSI:
1395 case Instruction::PtrToInt:
1396 case Instruction::IntToPtr:
1397 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001398 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1399 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001400 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001401 case Instruction::Select:
1402 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1403 OldC->getOperand(1),
1404 OldC->getOperand(2));
1405 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001406 default:
1407 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001408 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001409 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1410 OldC->getOperand(1));
1411 break;
1412 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001413 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001414 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001415 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
1416 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001417 break;
1418 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001419
Chris Lattner189d19f2003-11-21 20:23:48 +00001420 assert(New != OldC && "Didn't replace constant??");
1421 OldC->uncheckedReplaceAllUsesWith(New);
1422 OldC->destroyConstant(); // This constant is now dead, destroy it.
1423 }
1424 };
1425} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001426
1427
Chris Lattner3e650af2004-08-04 04:48:01 +00001428static ExprMapKeyType getValType(ConstantExpr *CE) {
1429 std::vector<Constant*> Operands;
1430 Operands.reserve(CE->getNumOperands());
1431 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1432 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001433 return ExprMapKeyType(CE->getOpcode(), Operands,
1434 CE->isCompare() ? CE->getPredicate() : 0);
Chris Lattner3e650af2004-08-04 04:48:01 +00001435}
1436
Chris Lattner69edc982006-09-28 00:35:06 +00001437static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1438 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001439
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001440/// This is a utility function to handle folding of casts and lookup of the
1441/// cast in the ExprConstants map. It is usedby the various get* methods below.
1442static inline Constant *getFoldedCast(
1443 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001444 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001445 // Fold a few common cases
1446 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1447 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001448
Vikram S. Adve4c485332002-07-15 18:19:33 +00001449 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001450 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001451 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001452 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001453}
Reid Spencerf37dc652006-12-05 19:14:13 +00001454
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001455Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1456 Instruction::CastOps opc = Instruction::CastOps(oc);
1457 assert(Instruction::isCast(opc) && "opcode out of range");
1458 assert(C && Ty && "Null arguments to getCast");
1459 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1460
1461 switch (opc) {
1462 default:
1463 assert(0 && "Invalid cast opcode");
1464 break;
1465 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001466 case Instruction::ZExt: return getZExt(C, Ty);
1467 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001468 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1469 case Instruction::FPExt: return getFPExtend(C, Ty);
1470 case Instruction::UIToFP: return getUIToFP(C, Ty);
1471 case Instruction::SIToFP: return getSIToFP(C, Ty);
1472 case Instruction::FPToUI: return getFPToUI(C, Ty);
1473 case Instruction::FPToSI: return getFPToSI(C, Ty);
1474 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1475 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1476 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001477 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001478 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001479}
1480
Reid Spencer5c140882006-12-04 20:17:56 +00001481Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1482 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1483 return getCast(Instruction::BitCast, C, Ty);
1484 return getCast(Instruction::ZExt, C, Ty);
1485}
1486
1487Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1488 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1489 return getCast(Instruction::BitCast, C, Ty);
1490 return getCast(Instruction::SExt, C, Ty);
1491}
1492
1493Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1494 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1495 return getCast(Instruction::BitCast, C, Ty);
1496 return getCast(Instruction::Trunc, C, Ty);
1497}
1498
Reid Spencerbc245a02006-12-05 03:25:26 +00001499Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1500 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001501 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001502
Chris Lattner03c49532007-01-15 02:27:26 +00001503 if (Ty->isInteger())
Reid Spencerbc245a02006-12-05 03:25:26 +00001504 return getCast(Instruction::PtrToInt, S, Ty);
1505 return getCast(Instruction::BitCast, S, Ty);
1506}
1507
Reid Spencer56521c42006-12-12 00:51:07 +00001508Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1509 bool isSigned) {
Chris Lattner03c49532007-01-15 02:27:26 +00001510 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer56521c42006-12-12 00:51:07 +00001511 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1512 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1513 Instruction::CastOps opcode =
1514 (SrcBits == DstBits ? Instruction::BitCast :
1515 (SrcBits > DstBits ? Instruction::Trunc :
1516 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1517 return getCast(opcode, C, Ty);
1518}
1519
1520Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1521 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1522 "Invalid cast");
1523 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1524 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001525 if (SrcBits == DstBits)
1526 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001527 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001528 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001529 return getCast(opcode, C, Ty);
1530}
1531
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001532Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001533 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1534 assert(Ty->isInteger() && "Trunc produces only integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001535 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1536 "SrcTy must be larger than DestTy for Trunc!");
1537
1538 return getFoldedCast(Instruction::Trunc, C, Ty);
1539}
1540
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001541Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001542 assert(C->getType()->isInteger() && "SEXt operand must be integral");
1543 assert(Ty->isInteger() && "SExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001544 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1545 "SrcTy must be smaller than DestTy for SExt!");
1546
1547 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001548}
1549
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001550Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001551 assert(C->getType()->isInteger() && "ZEXt operand must be integral");
1552 assert(Ty->isInteger() && "ZExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001553 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1554 "SrcTy must be smaller than DestTy for ZExt!");
1555
1556 return getFoldedCast(Instruction::ZExt, C, Ty);
1557}
1558
1559Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1560 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1561 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1562 "This is an illegal floating point truncation!");
1563 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1564}
1565
1566Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1567 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1568 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1569 "This is an illegal floating point extension!");
1570 return getFoldedCast(Instruction::FPExt, C, Ty);
1571}
1572
1573Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001574 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001575 "This is an illegal i32 to floating point cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001576 return getFoldedCast(Instruction::UIToFP, C, Ty);
1577}
1578
1579Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001580 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001581 "This is an illegal sint to floating point cast!");
1582 return getFoldedCast(Instruction::SIToFP, C, Ty);
1583}
1584
1585Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001586 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001587 "This is an illegal floating point to i32 cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001588 return getFoldedCast(Instruction::FPToUI, C, Ty);
1589}
1590
1591Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001592 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001593 "This is an illegal floating point to i32 cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001594 return getFoldedCast(Instruction::FPToSI, C, Ty);
1595}
1596
1597Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1598 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001599 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001600 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1601}
1602
1603Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001604 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001605 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1606 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1607}
1608
1609Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1610 // BitCast implies a no-op cast of type only. No bits change. However, you
1611 // can't cast pointers to anything but pointers.
1612 const Type *SrcTy = C->getType();
1613 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001614 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001615
1616 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1617 // or nonptr->ptr). For all the other types, the cast is okay if source and
1618 // destination bit widths are identical.
1619 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1620 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Reid Spencer5c140882006-12-04 20:17:56 +00001621 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001622 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001623}
1624
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001625Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001626 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00001627 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1628 Constant *GEP =
1629 getGetElementPtr(getNullValue(PointerType::get(Ty)), &GEPIdx, 1);
1630 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001631}
1632
Chris Lattnerb50d1352003-10-05 00:17:43 +00001633Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00001634 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001635 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001636 assert(Opcode >= Instruction::BinaryOpsBegin &&
1637 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001638 "Invalid opcode in binary constant expression");
1639 assert(C1->getType() == C2->getType() &&
1640 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001641
Reid Spencer542964f2007-01-11 18:21:29 +00001642 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00001643 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1644 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001645
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001646 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001647 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001648 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001649}
1650
Reid Spencer266e42b2006-12-23 06:05:41 +00001651Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Reid Spencera009d0d2006-12-04 21:35:24 +00001652 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001653 switch (predicate) {
1654 default: assert(0 && "Invalid CmpInst predicate");
1655 case FCmpInst::FCMP_FALSE: case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_OGT:
1656 case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OLE:
1657 case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_ORD: case FCmpInst::FCMP_UNO:
1658 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UGT: case FCmpInst::FCMP_UGE:
1659 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_ULE: case FCmpInst::FCMP_UNE:
1660 case FCmpInst::FCMP_TRUE:
1661 return getFCmp(predicate, C1, C2);
1662 case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGT:
1663 case ICmpInst::ICMP_UGE: case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE:
1664 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SLT:
1665 case ICmpInst::ICMP_SLE:
1666 return getICmp(predicate, C1, C2);
1667 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001668}
1669
1670Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001671#ifndef NDEBUG
1672 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00001673 case Instruction::Add:
1674 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001675 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001676 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001677 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001678 isa<VectorType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001679 "Tried to create an arithmetic operation on a non-arithmetic type!");
1680 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001681 case Instruction::UDiv:
1682 case Instruction::SDiv:
1683 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001684 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
1685 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001686 "Tried to create an arithmetic operation on a non-arithmetic type!");
1687 break;
1688 case Instruction::FDiv:
1689 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001690 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
1691 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001692 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1693 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001694 case Instruction::URem:
1695 case Instruction::SRem:
1696 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001697 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
1698 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001699 "Tried to create an arithmetic operation on a non-arithmetic type!");
1700 break;
1701 case Instruction::FRem:
1702 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001703 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
1704 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001705 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1706 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001707 case Instruction::And:
1708 case Instruction::Or:
1709 case Instruction::Xor:
1710 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001711 assert((C1->getType()->isInteger() || isa<VectorType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001712 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001713 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001714 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001715 case Instruction::LShr:
1716 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001717 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001718 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001719 "Tried to create a shift operation on a non-integer type!");
1720 break;
1721 default:
1722 break;
1723 }
1724#endif
1725
Reid Spencera009d0d2006-12-04 21:35:24 +00001726 return getTy(C1->getType(), Opcode, C1, C2);
1727}
1728
Reid Spencer266e42b2006-12-23 06:05:41 +00001729Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001730 Constant *C1, Constant *C2) {
1731 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001732 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001733}
1734
Chris Lattner6e415c02004-03-12 05:54:04 +00001735Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1736 Constant *V1, Constant *V2) {
Reid Spencer2546b762007-01-26 07:37:34 +00001737 assert(C->getType() == Type::Int1Ty && "Select condition must be i1!");
Chris Lattner6e415c02004-03-12 05:54:04 +00001738 assert(V1->getType() == V2->getType() && "Select value types must match!");
1739 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1740
1741 if (ReqTy == V1->getType())
1742 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1743 return SC; // Fold common cases
1744
1745 std::vector<Constant*> argVec(3, C);
1746 argVec[1] = V1;
1747 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001748 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001749 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001750}
1751
Chris Lattnerb50d1352003-10-05 00:17:43 +00001752Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001753 Value* const *Idxs,
1754 unsigned NumIdx) {
1755 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001756 "GEP indices invalid!");
1757
Chris Lattner302116a2007-01-31 04:40:28 +00001758 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001759 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001760
Chris Lattnerb50d1352003-10-05 00:17:43 +00001761 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001762 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001763 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001764 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001765 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001766 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001767 for (unsigned i = 0; i != NumIdx; ++i)
1768 ArgVec.push_back(cast<Constant>(Idxs[i]));
1769 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001770 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001771}
1772
Chris Lattner302116a2007-01-31 04:40:28 +00001773Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
1774 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001775 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001776 const Type *Ty =
1777 GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001778 assert(Ty && "GEP indices invalid!");
Chris Lattner302116a2007-01-31 04:40:28 +00001779 return getGetElementPtrTy(PointerType::get(Ty), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001780}
1781
Chris Lattner302116a2007-01-31 04:40:28 +00001782Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
1783 unsigned NumIdx) {
1784 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001785}
1786
Chris Lattner302116a2007-01-31 04:40:28 +00001787
Reid Spenceree3c9912006-12-04 05:19:50 +00001788Constant *
1789ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1790 assert(LHS->getType() == RHS->getType());
1791 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1792 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1793
Reid Spencer266e42b2006-12-23 06:05:41 +00001794 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001795 return FC; // Fold a few common cases...
1796
1797 // Look up the constant in the table first to ensure uniqueness
1798 std::vector<Constant*> ArgVec;
1799 ArgVec.push_back(LHS);
1800 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001801 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001802 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001803 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001804}
1805
1806Constant *
1807ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1808 assert(LHS->getType() == RHS->getType());
1809 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1810
Reid Spencer266e42b2006-12-23 06:05:41 +00001811 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001812 return FC; // Fold a few common cases...
1813
1814 // Look up the constant in the table first to ensure uniqueness
1815 std::vector<Constant*> ArgVec;
1816 ArgVec.push_back(LHS);
1817 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001818 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001819 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001820 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001821}
1822
Robert Bocchino23004482006-01-10 19:05:34 +00001823Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1824 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001825 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1826 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001827 // Look up the constant in the table first to ensure uniqueness
1828 std::vector<Constant*> ArgVec(1, Val);
1829 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001830 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001831 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001832}
1833
1834Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001835 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001836 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001837 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001838 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001839 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001840 Val, Idx);
1841}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001842
Robert Bocchinoca27f032006-01-17 20:07:22 +00001843Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1844 Constant *Elt, Constant *Idx) {
1845 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1846 return FC; // Fold a few common cases...
1847 // Look up the constant in the table first to ensure uniqueness
1848 std::vector<Constant*> ArgVec(1, Val);
1849 ArgVec.push_back(Elt);
1850 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001851 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001852 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001853}
1854
1855Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1856 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001857 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001858 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001859 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001860 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001861 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001862 "Insertelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001863 return getInsertElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinoca27f032006-01-17 20:07:22 +00001864 Val, Elt, Idx);
1865}
1866
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001867Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1868 Constant *V2, Constant *Mask) {
1869 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1870 return FC; // Fold a few common cases...
1871 // Look up the constant in the table first to ensure uniqueness
1872 std::vector<Constant*> ArgVec(1, V1);
1873 ArgVec.push_back(V2);
1874 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001875 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001876 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001877}
1878
1879Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1880 Constant *Mask) {
1881 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1882 "Invalid shuffle vector constant expr operands!");
1883 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1884}
1885
Reid Spencer2eadb532007-01-21 00:29:26 +00001886Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001887 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00001888 if (PTy->getElementType()->isFloatingPoint()) {
1889 std::vector<Constant*> zeros(PTy->getNumElements(),
1890 ConstantFP::get(PTy->getElementType(),-0.0));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001891 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00001892 }
Reid Spencer2eadb532007-01-21 00:29:26 +00001893
1894 if (Ty->isFloatingPoint())
1895 return ConstantFP::get(Ty, -0.0);
1896
1897 return Constant::getNullValue(Ty);
1898}
1899
Vikram S. Adve4c485332002-07-15 18:19:33 +00001900// destroyConstant - Remove the constant from the constant table...
1901//
1902void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001903 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001904 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001905}
1906
Chris Lattner3cd8c562002-07-30 18:54:25 +00001907const char *ConstantExpr::getOpcodeName() const {
1908 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001909}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001910
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001911//===----------------------------------------------------------------------===//
1912// replaceUsesOfWithOnConstant implementations
1913
1914void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001915 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001916 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001917 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001918
1919 unsigned OperandToUpdate = U-OperandList;
1920 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1921
Jim Laskeyc03caef2006-07-17 17:38:29 +00001922 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001923 Lookup.first.first = getType();
1924 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001925
Chris Lattnerb64419a2005-10-03 22:51:37 +00001926 std::vector<Constant*> &Values = Lookup.first.second;
1927 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001928
Chris Lattner8760ec72005-10-04 01:17:50 +00001929 // Fill values with the modified operands of the constant array. Also,
1930 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001931 bool isAllZeros = false;
1932 if (!ToC->isNullValue()) {
1933 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1934 Values.push_back(cast<Constant>(O->get()));
1935 } else {
1936 isAllZeros = true;
1937 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1938 Constant *Val = cast<Constant>(O->get());
1939 Values.push_back(Val);
1940 if (isAllZeros) isAllZeros = Val->isNullValue();
1941 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001942 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001943 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001944
Chris Lattnerb64419a2005-10-03 22:51:37 +00001945 Constant *Replacement = 0;
1946 if (isAllZeros) {
1947 Replacement = ConstantAggregateZero::get(getType());
1948 } else {
1949 // Check to see if we have this array type already.
1950 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001951 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001952 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001953
1954 if (Exists) {
1955 Replacement = I->second;
1956 } else {
1957 // Okay, the new shape doesn't exist in the system yet. Instead of
1958 // creating a new constant array, inserting it, replaceallusesof'ing the
1959 // old with the new, then deleting the old... just update the current one
1960 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001961 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001962
Chris Lattnerdff59112005-10-04 18:47:09 +00001963 // Update to the new value.
1964 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001965 return;
1966 }
1967 }
1968
1969 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001970 assert(Replacement != this && "I didn't contain From!");
1971
Chris Lattner7a1450d2005-10-04 18:13:04 +00001972 // Everyone using this now uses the replacement.
1973 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001974
1975 // Delete the old constant!
1976 destroyConstant();
1977}
1978
1979void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001980 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001981 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001982 Constant *ToC = cast<Constant>(To);
1983
Chris Lattnerdff59112005-10-04 18:47:09 +00001984 unsigned OperandToUpdate = U-OperandList;
1985 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1986
Jim Laskeyc03caef2006-07-17 17:38:29 +00001987 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001988 Lookup.first.first = getType();
1989 Lookup.second = this;
1990 std::vector<Constant*> &Values = Lookup.first.second;
1991 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001992
Chris Lattnerdff59112005-10-04 18:47:09 +00001993
Chris Lattner8760ec72005-10-04 01:17:50 +00001994 // Fill values with the modified operands of the constant struct. Also,
1995 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001996 bool isAllZeros = false;
1997 if (!ToC->isNullValue()) {
1998 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1999 Values.push_back(cast<Constant>(O->get()));
2000 } else {
2001 isAllZeros = true;
2002 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2003 Constant *Val = cast<Constant>(O->get());
2004 Values.push_back(Val);
2005 if (isAllZeros) isAllZeros = Val->isNullValue();
2006 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002007 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002008 Values[OperandToUpdate] = ToC;
2009
Chris Lattner8760ec72005-10-04 01:17:50 +00002010 Constant *Replacement = 0;
2011 if (isAllZeros) {
2012 Replacement = ConstantAggregateZero::get(getType());
2013 } else {
2014 // Check to see if we have this array type already.
2015 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002016 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002017 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002018
2019 if (Exists) {
2020 Replacement = I->second;
2021 } else {
2022 // Okay, the new shape doesn't exist in the system yet. Instead of
2023 // creating a new constant struct, inserting it, replaceallusesof'ing the
2024 // old with the new, then deleting the old... just update the current one
2025 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002026 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002027
Chris Lattnerdff59112005-10-04 18:47:09 +00002028 // Update to the new value.
2029 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002030 return;
2031 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002032 }
2033
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002034 assert(Replacement != this && "I didn't contain From!");
2035
Chris Lattner7a1450d2005-10-04 18:13:04 +00002036 // Everyone using this now uses the replacement.
2037 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002038
2039 // Delete the old constant!
2040 destroyConstant();
2041}
2042
Reid Spencerd84d35b2007-02-15 02:26:10 +00002043void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002044 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002045 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2046
2047 std::vector<Constant*> Values;
2048 Values.reserve(getNumOperands()); // Build replacement array...
2049 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2050 Constant *Val = getOperand(i);
2051 if (Val == From) Val = cast<Constant>(To);
2052 Values.push_back(Val);
2053 }
2054
Reid Spencerd84d35b2007-02-15 02:26:10 +00002055 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002056 assert(Replacement != this && "I didn't contain From!");
2057
Chris Lattner7a1450d2005-10-04 18:13:04 +00002058 // Everyone using this now uses the replacement.
2059 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002060
2061 // Delete the old constant!
2062 destroyConstant();
2063}
2064
2065void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002066 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002067 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2068 Constant *To = cast<Constant>(ToV);
2069
2070 Constant *Replacement = 0;
2071 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002072 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002073 Constant *Pointer = getOperand(0);
2074 Indices.reserve(getNumOperands()-1);
2075 if (Pointer == From) Pointer = To;
2076
2077 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2078 Constant *Val = getOperand(i);
2079 if (Val == From) Val = To;
2080 Indices.push_back(Val);
2081 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002082 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2083 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002084 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002085 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002086 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002087 } else if (getOpcode() == Instruction::Select) {
2088 Constant *C1 = getOperand(0);
2089 Constant *C2 = getOperand(1);
2090 Constant *C3 = getOperand(2);
2091 if (C1 == From) C1 = To;
2092 if (C2 == From) C2 = To;
2093 if (C3 == From) C3 = To;
2094 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002095 } else if (getOpcode() == Instruction::ExtractElement) {
2096 Constant *C1 = getOperand(0);
2097 Constant *C2 = getOperand(1);
2098 if (C1 == From) C1 = To;
2099 if (C2 == From) C2 = To;
2100 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002101 } else if (getOpcode() == Instruction::InsertElement) {
2102 Constant *C1 = getOperand(0);
2103 Constant *C2 = getOperand(1);
2104 Constant *C3 = getOperand(1);
2105 if (C1 == From) C1 = To;
2106 if (C2 == From) C2 = To;
2107 if (C3 == From) C3 = To;
2108 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2109 } else if (getOpcode() == Instruction::ShuffleVector) {
2110 Constant *C1 = getOperand(0);
2111 Constant *C2 = getOperand(1);
2112 Constant *C3 = getOperand(2);
2113 if (C1 == From) C1 = To;
2114 if (C2 == From) C2 = To;
2115 if (C3 == From) C3 = To;
2116 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002117 } else if (isCompare()) {
2118 Constant *C1 = getOperand(0);
2119 Constant *C2 = getOperand(1);
2120 if (C1 == From) C1 = To;
2121 if (C2 == From) C2 = To;
2122 if (getOpcode() == Instruction::ICmp)
2123 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
2124 else
2125 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002126 } else if (getNumOperands() == 2) {
2127 Constant *C1 = getOperand(0);
2128 Constant *C2 = getOperand(1);
2129 if (C1 == From) C1 = To;
2130 if (C2 == From) C2 = To;
2131 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2132 } else {
2133 assert(0 && "Unknown ConstantExpr type!");
2134 return;
2135 }
2136
2137 assert(Replacement != this && "I didn't contain From!");
2138
Chris Lattner7a1450d2005-10-04 18:13:04 +00002139 // Everyone using this now uses the replacement.
2140 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002141
2142 // Delete the old constant!
2143 destroyConstant();
2144}
2145
2146
Jim Laskey2698f0d2006-03-08 18:11:07 +00002147/// getStringValue - Turn an LLVM constant pointer that eventually points to a
2148/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00002149/// Parameter Chop determines if the result is chopped at the first null
2150/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00002151///
Evan Cheng38280c02006-03-10 23:52:03 +00002152std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00002153 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
2154 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
2155 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
2156 if (Init->isString()) {
2157 std::string Result = Init->getAsString();
2158 if (Offset < Result.size()) {
2159 // If we are pointing INTO The string, erase the beginning...
2160 Result.erase(Result.begin(), Result.begin()+Offset);
2161
2162 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00002163 if (Chop) {
2164 std::string::size_type NullPos = Result.find_first_of((char)0);
2165 if (NullPos != std::string::npos)
2166 Result.erase(Result.begin()+NullPos, Result.end());
2167 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00002168 return Result;
2169 }
2170 }
2171 }
2172 } else if (Constant *C = dyn_cast<Constant>(this)) {
2173 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00002174 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002175 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2176 if (CE->getOpcode() == Instruction::GetElementPtr) {
2177 // Turn a gep into the specified offset.
2178 if (CE->getNumOperands() == 3 &&
2179 cast<Constant>(CE->getOperand(1))->isNullValue() &&
2180 isa<ConstantInt>(CE->getOperand(2))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00002181 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00002182 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002183 }
2184 }
2185 }
2186 }
2187 return "";
2188}