blob: 33c8f675f8dd8d560a17554557e3422cb0caa030 [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//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000019#include "llvm/MDNode.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000021#include "llvm/ADT/FoldingSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/ADT/StringExtras.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000023#include "llvm/ADT/StringMap.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000024#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000025#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000027#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000028#include "llvm/Support/MathExtras.h"
Owen Anderson0d2de8c2009-06-20 00:24:58 +000029#include "llvm/System/Mutex.h"
Owen Anderson2d7231d2009-06-17 18:40:29 +000030#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000031#include "llvm/System/Threading.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000034#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000035#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000036using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000037
Chris Lattner2f7c9632001-06-06 20:29:01 +000038//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000039// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000040//===----------------------------------------------------------------------===//
41
Owen Andersond830eb82009-06-18 19:10:19 +000042// Becomes a no-op when multithreading is disabled.
43ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson2d7231d2009-06-17 18:40:29 +000044
Chris Lattner3462ae32001-12-03 22:26:30 +000045void Constant::destroyConstantImpl() {
46 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000047 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000048 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000049 // but they don't know that. Because we only find out when the CPV is
50 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000051 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000052 //
53 while (!use_empty()) {
54 Value *V = use_back();
55#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000056 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000057 DOUT << "While deleting: " << *this
58 << "\n\nUse still stuck around after Def is destroyed: "
59 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000060#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000061 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000062 Constant *CV = cast<Constant>(V);
63 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000064
65 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000066 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000067 }
68
69 // Value has no outstanding references it is safe to delete it now...
70 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000071}
Chris Lattner2f7c9632001-06-06 20:29:01 +000072
Chris Lattner23dd1f62006-10-20 00:27:06 +000073/// canTrap - Return true if evaluation of this constant could trap. This is
74/// true for things like constant expressions that could divide by zero.
75bool Constant::canTrap() const {
76 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
77 // The only thing that could possibly trap are constant exprs.
78 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
79 if (!CE) return false;
80
81 // ConstantExpr traps if any operands can trap.
82 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
83 if (getOperand(i)->canTrap())
84 return true;
85
86 // Otherwise, only specific operations can trap.
87 switch (CE->getOpcode()) {
88 default:
89 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000090 case Instruction::UDiv:
91 case Instruction::SDiv:
92 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000093 case Instruction::URem:
94 case Instruction::SRem:
95 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000096 // Div and rem can trap if the RHS is not known to be non-zero.
97 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
98 return true;
99 return false;
100 }
101}
102
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000103/// ContainsRelocations - Return true if the constant value contains relocations
104/// which cannot be resolved at compile time. Kind argument is used to filter
105/// only 'interesting' sorts of relocations.
106bool Constant::ContainsRelocations(unsigned Kind) const {
107 if (const GlobalValue* GV = dyn_cast<GlobalValue>(this)) {
108 bool isLocal = GV->hasLocalLinkage();
109 if ((Kind & Reloc::Local) && isLocal) {
110 // Global has local linkage and 'local' kind of relocations are
111 // requested
112 return true;
113 }
114
115 if ((Kind & Reloc::Global) && !isLocal) {
116 // Global has non-local linkage and 'global' kind of relocations are
117 // requested
118 return true;
119 }
Anton Korobeynikov255a3cb2009-03-30 15:28:21 +0000120
121 return false;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000122 }
123
Evan Chengf9e003b2007-03-08 00:59:12 +0000124 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Anton Korobeynikovd5e8e932009-03-30 15:28:00 +0000125 if (getOperand(i)->ContainsRelocations(Kind))
Evan Chengf9e003b2007-03-08 00:59:12 +0000126 return true;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000127
Evan Chengf9e003b2007-03-08 00:59:12 +0000128 return false;
129}
130
Chris Lattner2105d662008-07-10 00:28:11 +0000131/// getVectorElements - This method, which is only valid on constant of vector
132/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000133/// This handles breaking down a vector undef into undef elements, etc. For
134/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson53a52212009-07-13 04:09:18 +0000135void Constant::getVectorElements(LLVMContext &Context,
136 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner2105d662008-07-10 00:28:11 +0000137 assert(isa<VectorType>(getType()) && "Not a vector constant!");
138
139 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
140 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
141 Elts.push_back(CV->getOperand(i));
142 return;
143 }
144
145 const VectorType *VT = cast<VectorType>(getType());
146 if (isa<ConstantAggregateZero>(this)) {
147 Elts.assign(VT->getNumElements(),
Owen Anderson53a52212009-07-13 04:09:18 +0000148 Context.getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000149 return;
150 }
151
Chris Lattnerc5098a22008-07-14 05:10:41 +0000152 if (isa<UndefValue>(this)) {
Owen Anderson53a52212009-07-13 04:09:18 +0000153 Elts.assign(VT->getNumElements(), Context.getUndef(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000154 return;
155 }
156
157 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000158}
159
160
161
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000163// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000164//===----------------------------------------------------------------------===//
165
Reid Spencerb31bffe2007-02-26 23:54:03 +0000166ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000167 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000168 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000169}
170
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000171ConstantInt *ConstantInt::TheTrueVal = 0;
172ConstantInt *ConstantInt::TheFalseVal = 0;
173
174namespace llvm {
175 void CleanupTrueFalse(void *) {
176 ConstantInt::ResetTrueFalse();
177 }
178}
179
180static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
181
182ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
183 assert(TheTrueVal == 0 && TheFalseVal == 0);
184 TheTrueVal = get(Type::Int1Ty, 1);
185 TheFalseVal = get(Type::Int1Ty, 0);
186
187 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
188 TrueFalseCleanup.Register();
189
190 return WhichOne ? TheTrueVal : TheFalseVal;
191}
192
193
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000194namespace {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000195 struct DenseMapAPIntKeyInfo {
196 struct KeyTy {
197 APInt val;
198 const Type* type;
199 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
200 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
201 bool operator==(const KeyTy& that) const {
202 return type == that.type && this->val == that.val;
203 }
204 bool operator!=(const KeyTy& that) const {
205 return !this->operator==(that);
206 }
207 };
208 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
209 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000210 static unsigned getHashValue(const KeyTy &Key) {
Chris Lattner0625bd62007-09-17 18:34:04 +0000211 return DenseMapInfo<void*>::getHashValue(Key.type) ^
Reid Spencerb31bffe2007-02-26 23:54:03 +0000212 Key.val.getHashValue();
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000213 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000214 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
215 return LHS == RHS;
216 }
Dale Johannesena719a602007-08-24 00:56:33 +0000217 static bool isPod() { return false; }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000218 };
219}
220
221
Reid Spencerb31bffe2007-02-26 23:54:03 +0000222typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
223 DenseMapAPIntKeyInfo> IntMapTy;
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000224static ManagedStatic<IntMapTy> IntConstants;
225
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000226ConstantInt *ConstantInt::get(const IntegerType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000227 uint64_t V, bool isSigned) {
228 return get(APInt(Ty->getBitWidth(), V, isSigned));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000229}
230
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000231Constant *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000232 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
233
234 // For vectors, broadcast the value.
235 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
236 return
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000237 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000238
239 return C;
Reid Spencerb31bffe2007-02-26 23:54:03 +0000240}
241
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000242// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
Dan Gohmanb3efe032008-02-07 02:30:40 +0000243// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
Reid Spencerb31bffe2007-02-26 23:54:03 +0000244// operator== and operator!= to ensure that the DenseMap doesn't attempt to
245// compare APInt's of different widths, which would violate an APInt class
246// invariant which generates an assertion.
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000247ConstantInt *ConstantInt::get(const APInt& V) {
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000248 // Get the corresponding integer type for the bit width of the value.
249 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Reid Spencerb31bffe2007-02-26 23:54:03 +0000250 // get an existing value or the insertion position
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000251 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000252
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000253 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +0000254 ConstantInt *&Slot = (*IntConstants)[Key];
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000255 ConstantsLock->reader_release();
Owen Anderson2d7231d2009-06-17 18:40:29 +0000256
Owen Andersond830eb82009-06-18 19:10:19 +0000257 if (!Slot) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000258 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000259 ConstantInt *&NewSlot = (*IntConstants)[Key];
260 if (!Slot) {
261 NewSlot = new ConstantInt(ITy, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000262 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000263
264 return NewSlot;
265 } else {
266 return Slot;
Owen Anderson2d7231d2009-06-17 18:40:29 +0000267 }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000268}
269
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000270Constant *ConstantInt::get(const Type *Ty, const APInt &V) {
271 ConstantInt *C = ConstantInt::get(V);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000272 assert(C->getType() == Ty->getScalarType() &&
273 "ConstantInt type doesn't match the type implied by its value!");
274
275 // For vectors, broadcast the value.
276 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
277 return
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000278 ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000279
280 return C;
281}
282
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000283//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000284// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000285//===----------------------------------------------------------------------===//
286
Chris Lattner98bd9392008-04-09 06:38:30 +0000287static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
288 if (Ty == Type::FloatTy)
289 return &APFloat::IEEEsingle;
290 if (Ty == Type::DoubleTy)
291 return &APFloat::IEEEdouble;
292 if (Ty == Type::X86_FP80Ty)
293 return &APFloat::x87DoubleExtended;
294 else if (Ty == Type::FP128Ty)
295 return &APFloat::IEEEquad;
296
297 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
298 return &APFloat::PPCDoubleDouble;
299}
300
Dale Johannesend246b2c2007-08-30 00:23:21 +0000301ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
302 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000303 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
304 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000305}
306
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000307bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000308 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000309}
310
Dale Johannesend246b2c2007-08-30 00:23:21 +0000311bool ConstantFP::isExactlyValue(const APFloat& V) const {
312 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000313}
314
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000315namespace {
Dale Johannesena719a602007-08-24 00:56:33 +0000316 struct DenseMapAPFloatKeyInfo {
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000317 struct KeyTy {
318 APFloat val;
319 KeyTy(const APFloat& V) : val(V){}
320 KeyTy(const KeyTy& that) : val(that.val) {}
321 bool operator==(const KeyTy& that) const {
322 return this->val.bitwiseIsEqual(that.val);
323 }
324 bool operator!=(const KeyTy& that) const {
325 return !this->operator==(that);
326 }
327 };
328 static inline KeyTy getEmptyKey() {
329 return KeyTy(APFloat(APFloat::Bogus,1));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000330 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000331 static inline KeyTy getTombstoneKey() {
332 return KeyTy(APFloat(APFloat::Bogus,2));
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000333 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000334 static unsigned getHashValue(const KeyTy &Key) {
335 return Key.val.getHashValue();
Dale Johannesena719a602007-08-24 00:56:33 +0000336 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000337 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
338 return LHS == RHS;
339 }
Dale Johannesena719a602007-08-24 00:56:33 +0000340 static bool isPod() { return false; }
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000341 };
342}
343
344//---- ConstantFP::get() implementation...
345//
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000346typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Dale Johannesena719a602007-08-24 00:56:33 +0000347 DenseMapAPFloatKeyInfo> FPMapTy;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000348
Dale Johannesena719a602007-08-24 00:56:33 +0000349static ManagedStatic<FPMapTy> FPConstants;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000350
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000351ConstantFP *ConstantFP::get(const APFloat &V) {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000352 DenseMapAPFloatKeyInfo::KeyTy Key(V);
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000353
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000354 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +0000355 ConstantFP *&Slot = (*FPConstants)[Key];
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000356 ConstantsLock->reader_release();
Owen Anderson2d7231d2009-06-17 18:40:29 +0000357
Owen Andersond830eb82009-06-18 19:10:19 +0000358 if (!Slot) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000359 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000360 ConstantFP *&NewSlot = (*FPConstants)[Key];
361 if (!NewSlot) {
Owen Andersond830eb82009-06-18 19:10:19 +0000362 const Type *Ty;
363 if (&V.getSemantics() == &APFloat::IEEEsingle)
364 Ty = Type::FloatTy;
365 else if (&V.getSemantics() == &APFloat::IEEEdouble)
366 Ty = Type::DoubleTy;
367 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
368 Ty = Type::X86_FP80Ty;
369 else if (&V.getSemantics() == &APFloat::IEEEquad)
370 Ty = Type::FP128Ty;
371 else {
372 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
373 "Unknown FP format");
374 Ty = Type::PPC_FP128Ty;
Owen Anderson2d7231d2009-06-17 18:40:29 +0000375 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000376 NewSlot = new ConstantFP(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +0000377 }
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000378
379 return NewSlot;
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000380 }
Owen Andersond830eb82009-06-18 19:10:19 +0000381
382 return Slot;
Dale Johannesend246b2c2007-08-30 00:23:21 +0000383}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000384
385//===----------------------------------------------------------------------===//
386// ConstantXXX Classes
387//===----------------------------------------------------------------------===//
388
389
Chris Lattner3462ae32001-12-03 22:26:30 +0000390ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000391 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000392 : Constant(T, ConstantArrayVal,
393 OperandTraits<ConstantArray>::op_end(this) - V.size(),
394 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000395 assert(V.size() == T->getNumElements() &&
396 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000397 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000398 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
399 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000400 Constant *C = *I;
401 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000402 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000403 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000404 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000405 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000406 }
407}
408
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000409
Chris Lattner3462ae32001-12-03 22:26:30 +0000410ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000411 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000412 : Constant(T, ConstantStructVal,
413 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
414 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000415 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000416 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000417 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000418 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
419 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000420 Constant *C = *I;
421 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000422 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000423 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000424 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000425 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000426 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000427 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000428 }
429}
430
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000431
Reid Spencerd84d35b2007-02-15 02:26:10 +0000432ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000433 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000434 : Constant(T, ConstantVectorVal,
435 OperandTraits<ConstantVector>::op_end(this) - V.size(),
436 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000437 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000438 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
439 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000440 Constant *C = *I;
441 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000442 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000443 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000444 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000445 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000446 }
447}
448
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000449
Gabor Greiff6caff662008-05-10 08:32:32 +0000450namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000451// We declare several classes private to this file, so use an anonymous
452// namespace
453namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000454
Gordon Henriksen14a55692007-12-10 02:14:30 +0000455/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
456/// behind the scenes to implement unary constant exprs.
457class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000458 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000459public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000460 // allocate space for exactly one operand
461 void *operator new(size_t s) {
462 return User::operator new(s, 1);
463 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000464 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000465 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
466 Op<0>() = C;
467 }
468 /// Transparently provide more efficient getOperand methods.
469 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000470};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000471
Gordon Henriksen14a55692007-12-10 02:14:30 +0000472/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
473/// behind the scenes to implement binary constant exprs.
474class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000475 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000476public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000477 // allocate space for exactly two operands
478 void *operator new(size_t s) {
479 return User::operator new(s, 2);
480 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000481 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000482 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000483 Op<0>() = C1;
484 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000485 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000486 /// Transparently provide more efficient getOperand methods.
487 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000488};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000489
Gordon Henriksen14a55692007-12-10 02:14:30 +0000490/// SelectConstantExpr - This class is private to Constants.cpp, and is used
491/// behind the scenes to implement select constant exprs.
492class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000493 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000494public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000495 // allocate space for exactly three operands
496 void *operator new(size_t s) {
497 return User::operator new(s, 3);
498 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000499 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000500 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000501 Op<0>() = C1;
502 Op<1>() = C2;
503 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000504 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000505 /// Transparently provide more efficient getOperand methods.
506 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000507};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000508
Gordon Henriksen14a55692007-12-10 02:14:30 +0000509/// ExtractElementConstantExpr - This class is private to
510/// Constants.cpp, and is used behind the scenes to implement
511/// extractelement constant exprs.
512class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000513 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000514public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000515 // allocate space for exactly two operands
516 void *operator new(size_t s) {
517 return User::operator new(s, 2);
518 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000519 ExtractElementConstantExpr(Constant *C1, Constant *C2)
520 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000521 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000522 Op<0>() = C1;
523 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000524 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000525 /// Transparently provide more efficient getOperand methods.
526 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000527};
Robert Bocchino23004482006-01-10 19:05:34 +0000528
Gordon Henriksen14a55692007-12-10 02:14:30 +0000529/// InsertElementConstantExpr - This class is private to
530/// Constants.cpp, and is used behind the scenes to implement
531/// insertelement constant exprs.
532class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000533 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000534public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000535 // allocate space for exactly three operands
536 void *operator new(size_t s) {
537 return User::operator new(s, 3);
538 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000539 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
540 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000541 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000542 Op<0>() = C1;
543 Op<1>() = C2;
544 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000545 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000546 /// Transparently provide more efficient getOperand methods.
547 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000548};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000549
Gordon Henriksen14a55692007-12-10 02:14:30 +0000550/// ShuffleVectorConstantExpr - This class is private to
551/// Constants.cpp, and is used behind the scenes to implement
552/// shufflevector constant exprs.
553class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000554 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000555public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000556 // allocate space for exactly three operands
557 void *operator new(size_t s) {
558 return User::operator new(s, 3);
559 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000560 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000561 : ConstantExpr(VectorType::get(
562 cast<VectorType>(C1->getType())->getElementType(),
563 cast<VectorType>(C3->getType())->getNumElements()),
564 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000565 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000566 Op<0>() = C1;
567 Op<1>() = C2;
568 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000569 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000570 /// Transparently provide more efficient getOperand methods.
571 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000572};
573
Dan Gohman12fce772008-05-15 19:50:34 +0000574/// ExtractValueConstantExpr - This class is private to
575/// Constants.cpp, and is used behind the scenes to implement
576/// extractvalue constant exprs.
577class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000578 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000579public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000580 // allocate space for exactly one operand
581 void *operator new(size_t s) {
582 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000583 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000584 ExtractValueConstantExpr(Constant *Agg,
585 const SmallVector<unsigned, 4> &IdxList,
586 const Type *DestTy)
587 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
588 Indices(IdxList) {
589 Op<0>() = Agg;
590 }
591
Dan Gohman7bb04502008-05-31 19:09:08 +0000592 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000593 const SmallVector<unsigned, 4> Indices;
594
Dan Gohman12fce772008-05-15 19:50:34 +0000595 /// Transparently provide more efficient getOperand methods.
596 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
597};
598
599/// InsertValueConstantExpr - This class is private to
600/// Constants.cpp, and is used behind the scenes to implement
601/// insertvalue constant exprs.
602class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000603 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000604public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000605 // allocate space for exactly one operand
606 void *operator new(size_t s) {
607 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000608 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000609 InsertValueConstantExpr(Constant *Agg, Constant *Val,
610 const SmallVector<unsigned, 4> &IdxList,
611 const Type *DestTy)
612 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
613 Indices(IdxList) {
614 Op<0>() = Agg;
615 Op<1>() = Val;
616 }
617
Dan Gohman7bb04502008-05-31 19:09:08 +0000618 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000619 const SmallVector<unsigned, 4> Indices;
620
Dan Gohman12fce772008-05-15 19:50:34 +0000621 /// Transparently provide more efficient getOperand methods.
622 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
623};
624
625
Gordon Henriksen14a55692007-12-10 02:14:30 +0000626/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
627/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000628class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000629 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000630 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000631public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000632 static GetElementPtrConstantExpr *Create(Constant *C,
633 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000634 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000635 return new(IdxList.size() + 1)
636 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000637 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000638 /// Transparently provide more efficient getOperand methods.
639 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000640};
641
642// CompareConstantExpr - This class is private to Constants.cpp, and is used
643// behind the scenes to implement ICmp and FCmp constant expressions. This is
644// needed in order to store the predicate value for these instructions.
645struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000646 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
647 // allocate space for exactly two operands
648 void *operator new(size_t s) {
649 return User::operator new(s, 2);
650 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000651 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000652 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
653 unsigned short pred, Constant* LHS, Constant* RHS)
654 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000655 Op<0>() = LHS;
656 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000657 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000658 /// Transparently provide more efficient getOperand methods.
659 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000660};
661
662} // end anonymous namespace
663
Gabor Greiff6caff662008-05-10 08:32:32 +0000664template <>
665struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
666};
667DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
668
669template <>
670struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
671};
672DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
673
674template <>
675struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
676};
677DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
678
679template <>
680struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
681};
682DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
683
684template <>
685struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
686};
687DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
688
689template <>
690struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
691};
692DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
693
Dan Gohman12fce772008-05-15 19:50:34 +0000694template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000695struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000696};
Dan Gohman12fce772008-05-15 19:50:34 +0000697DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
698
699template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000700struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000701};
Dan Gohman12fce772008-05-15 19:50:34 +0000702DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
703
Gabor Greiff6caff662008-05-10 08:32:32 +0000704template <>
705struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
706};
707
708GetElementPtrConstantExpr::GetElementPtrConstantExpr
709 (Constant *C,
710 const std::vector<Constant*> &IdxList,
711 const Type *DestTy)
712 : ConstantExpr(DestTy, Instruction::GetElementPtr,
713 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
714 - (IdxList.size()+1),
715 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000716 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000717 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000718 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000719}
720
721DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
722
723
724template <>
725struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
726};
727DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
728
729
730} // End llvm namespace
731
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000732
733// Utility function for determining if a ConstantExpr is a CastOp or not. This
734// can't be inline because we don't want to #include Instruction.h into
735// Constant.h
736bool ConstantExpr::isCast() const {
737 return Instruction::isCast(getOpcode());
738}
739
Reid Spenceree3c9912006-12-04 05:19:50 +0000740bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000741 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000742}
743
Dan Gohman1ecaf452008-05-31 00:58:22 +0000744bool ConstantExpr::hasIndices() const {
745 return getOpcode() == Instruction::ExtractValue ||
746 getOpcode() == Instruction::InsertValue;
747}
748
749const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
750 if (const ExtractValueConstantExpr *EVCE =
751 dyn_cast<ExtractValueConstantExpr>(this))
752 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000753
754 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000755}
756
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000757unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000758 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000759 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000760 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000761}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000762
Chris Lattner7c1018a2006-07-14 19:37:40 +0000763/// getWithOperandReplaced - Return a constant expression identical to this
764/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000765Constant *
766ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000767 assert(OpNo < getNumOperands() && "Operand num is out of range!");
768 assert(Op->getType() == getOperand(OpNo)->getType() &&
769 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000770 if (getOperand(OpNo) == Op)
771 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000772
Chris Lattner227816342006-07-14 22:20:01 +0000773 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000774 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000775 case Instruction::Trunc:
776 case Instruction::ZExt:
777 case Instruction::SExt:
778 case Instruction::FPTrunc:
779 case Instruction::FPExt:
780 case Instruction::UIToFP:
781 case Instruction::SIToFP:
782 case Instruction::FPToUI:
783 case Instruction::FPToSI:
784 case Instruction::PtrToInt:
785 case Instruction::IntToPtr:
786 case Instruction::BitCast:
787 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000788 case Instruction::Select:
789 Op0 = (OpNo == 0) ? Op : getOperand(0);
790 Op1 = (OpNo == 1) ? Op : getOperand(1);
791 Op2 = (OpNo == 2) ? Op : getOperand(2);
792 return ConstantExpr::getSelect(Op0, Op1, Op2);
793 case Instruction::InsertElement:
794 Op0 = (OpNo == 0) ? Op : getOperand(0);
795 Op1 = (OpNo == 1) ? Op : getOperand(1);
796 Op2 = (OpNo == 2) ? Op : getOperand(2);
797 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
798 case Instruction::ExtractElement:
799 Op0 = (OpNo == 0) ? Op : getOperand(0);
800 Op1 = (OpNo == 1) ? Op : getOperand(1);
801 return ConstantExpr::getExtractElement(Op0, Op1);
802 case Instruction::ShuffleVector:
803 Op0 = (OpNo == 0) ? Op : getOperand(0);
804 Op1 = (OpNo == 1) ? Op : getOperand(1);
805 Op2 = (OpNo == 2) ? Op : getOperand(2);
806 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000807 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000808 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000809 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000810 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000811 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000812 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000813 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000814 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000815 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000816 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000817 default:
818 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000819 Op0 = (OpNo == 0) ? Op : getOperand(0);
820 Op1 = (OpNo == 1) ? Op : getOperand(1);
821 return ConstantExpr::get(getOpcode(), Op0, Op1);
822 }
823}
824
825/// getWithOperands - This returns the current constant expression with the
826/// operands replaced with the specified values. The specified operands must
827/// match count and type with the existing ones.
828Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000829getWithOperands(Constant* const *Ops, unsigned NumOps) const {
830 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000831 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000832 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000833 assert(Ops[i]->getType() == getOperand(i)->getType() &&
834 "Operand type mismatch!");
835 AnyChange |= Ops[i] != getOperand(i);
836 }
837 if (!AnyChange) // No operands changed, return self.
838 return const_cast<ConstantExpr*>(this);
839
840 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000841 case Instruction::Trunc:
842 case Instruction::ZExt:
843 case Instruction::SExt:
844 case Instruction::FPTrunc:
845 case Instruction::FPExt:
846 case Instruction::UIToFP:
847 case Instruction::SIToFP:
848 case Instruction::FPToUI:
849 case Instruction::FPToSI:
850 case Instruction::PtrToInt:
851 case Instruction::IntToPtr:
852 case Instruction::BitCast:
853 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000854 case Instruction::Select:
855 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
856 case Instruction::InsertElement:
857 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
858 case Instruction::ExtractElement:
859 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
860 case Instruction::ShuffleVector:
861 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000862 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000863 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000864 case Instruction::ICmp:
865 case Instruction::FCmp:
866 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000867 default:
868 assert(getNumOperands() == 2 && "Must be binary operator?");
869 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000870 }
871}
872
Chris Lattner2f7c9632001-06-06 20:29:01 +0000873
874//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000875// isValueValidForType implementations
876
Reid Spencere7334722006-12-19 01:28:19 +0000877bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000878 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000879 if (Ty == Type::Int1Ty)
880 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000881 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000882 return true; // always true, has to fit in largest type
883 uint64_t Max = (1ll << NumBits) - 1;
884 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000885}
886
Reid Spencere0fc4df2006-10-20 07:07:24 +0000887bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000888 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000889 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000890 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000891 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000892 return true; // always true, has to fit in largest type
893 int64_t Min = -(1ll << (NumBits-1));
894 int64_t Max = (1ll << (NumBits-1)) - 1;
895 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000896}
897
Dale Johannesend246b2c2007-08-30 00:23:21 +0000898bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
899 // convert modifies in place, so make a copy.
900 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000901 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000902 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000903 default:
904 return false; // These can't be represented as floating point!
905
Dale Johannesend246b2c2007-08-30 00:23:21 +0000906 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000907 case Type::FloatTyID: {
908 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
909 return true;
910 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
911 return !losesInfo;
912 }
913 case Type::DoubleTyID: {
914 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
915 &Val2.getSemantics() == &APFloat::IEEEdouble)
916 return true;
917 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
918 return !losesInfo;
919 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000920 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000921 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
922 &Val2.getSemantics() == &APFloat::IEEEdouble ||
923 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000924 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000925 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
926 &Val2.getSemantics() == &APFloat::IEEEdouble ||
927 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000928 case Type::PPC_FP128TyID:
929 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
930 &Val2.getSemantics() == &APFloat::IEEEdouble ||
931 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000932 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000933}
Chris Lattner9655e542001-07-20 19:16:02 +0000934
Chris Lattner49d855c2001-09-07 16:46:31 +0000935//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000936// Factory Function Implementation
937
Gabor Greiff6caff662008-05-10 08:32:32 +0000938
939// The number of operands for each ConstantCreator::create method is
940// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000941// ConstantCreator - A class that is used to create constants by
942// ValueMap*. This class should be partially specialized if there is
943// something strange that needs to be done to interface to the ctor for the
944// constant.
945//
Chris Lattner189d19f2003-11-21 20:23:48 +0000946namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +0000947 template<class ValType>
948 struct ConstantTraits;
949
950 template<typename T, typename Alloc>
951 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
952 static unsigned uses(const std::vector<T, Alloc>& v) {
953 return v.size();
954 }
955 };
956
Chris Lattner189d19f2003-11-21 20:23:48 +0000957 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000958 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000959 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000960 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +0000961 }
962 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000963
Chris Lattner189d19f2003-11-21 20:23:48 +0000964 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000965 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000966 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000967 llvm_unreachable("This type cannot be converted!");
Chris Lattner189d19f2003-11-21 20:23:48 +0000968 }
969 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000970
Chris Lattner935aa922005-10-04 17:48:46 +0000971 template<class ValType, class TypeClass, class ConstantClass,
972 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000973 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000974 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000975 typedef std::pair<const Type*, ValType> MapKey;
976 typedef std::map<MapKey, Constant *> MapTy;
977 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
978 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000979 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000980 /// Map - This is the main map from the element descriptor to the Constants.
981 /// This is the primary way we avoid creating two of the same shape
982 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000983 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000984
985 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
986 /// from the constants to their element in Map. This is important for
987 /// removal of constants from the array, which would otherwise have to scan
988 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000989 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000990
Jim Laskeyc03caef2006-07-17 17:38:29 +0000991 /// AbstractTypeMap - Map for abstract type constants.
992 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000993 AbstractTypeMapTy AbstractTypeMap;
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000994
995 /// ValueMapLock - Mutex for this map.
996 sys::SmartMutex<true> ValueMapLock;
Chris Lattner99a669b2004-11-19 16:39:44 +0000997
Chris Lattner98fa07b2003-05-23 20:03:32 +0000998 public:
Owen Anderson61794042009-06-17 20:10:08 +0000999 // NOTE: This function is not locked. It is the caller's responsibility
1000 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001001 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +00001002
1003 /// InsertOrGetItem - Return an iterator for the specified element.
1004 /// If the element exists in the map, the returned iterator points to the
1005 /// entry and Exists=true. If not, the iterator points to the newly
1006 /// inserted entry and returns Exists=false. Newly inserted entries have
1007 /// I->second == 0, and should be filled in.
Owen Anderson61794042009-06-17 20:10:08 +00001008 /// NOTE: This function is not locked. It is the caller's responsibility
1009 // to enforce proper synchronization.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001010 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
1011 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +00001012 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001013 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001014 Exists = !IP.second;
1015 return IP.first;
1016 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001017
Chris Lattner935aa922005-10-04 17:48:46 +00001018private:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001019 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +00001020 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001021 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +00001022 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
1023 IMI->second->second == CP &&
1024 "InverseMap corrupt!");
1025 return IMI->second;
1026 }
1027
Jim Laskeyc03caef2006-07-17 17:38:29 +00001028 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +00001029 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
1030 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001031 if (I == Map.end() || I->second != CP) {
1032 // FIXME: This should not use a linear scan. If this gets to be a
1033 // performance problem, someone should look at this.
1034 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
1035 /* empty */;
1036 }
Chris Lattner935aa922005-10-04 17:48:46 +00001037 return I;
1038 }
Owen Andersonf89c38c2009-06-17 20:43:39 +00001039
1040 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
1041 typename MapTy::iterator I) {
1042 ConstantClass* Result =
1043 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
1044
1045 assert(Result->getType() == Ty && "Type specified is not correct!");
1046 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
1047
1048 if (HasLargeKey) // Remember the reverse mapping if needed.
1049 InverseMap.insert(std::make_pair(Result, I));
1050
1051 // If the type of the constant is abstract, make sure that an entry
1052 // exists for it in the AbstractTypeMap.
1053 if (Ty->isAbstract()) {
1054 typename AbstractTypeMapTy::iterator TI =
1055 AbstractTypeMap.find(Ty);
1056
1057 if (TI == AbstractTypeMap.end()) {
1058 // Add ourselves to the ATU list of the type.
1059 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
1060
1061 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
1062 }
1063 }
1064
1065 return Result;
1066 }
Chris Lattner935aa922005-10-04 17:48:46 +00001067public:
1068
Chris Lattnerb64419a2005-10-03 22:51:37 +00001069 /// getOrCreate - Return the specified constant from the map, creating it if
1070 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001071 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001072 sys::SmartScopedLock<true> Lock(ValueMapLock);
Owen Andersonb07dd952009-06-19 23:16:19 +00001073 MapKey Lookup(Ty, V);
1074 ConstantClass* Result = 0;
1075
1076 typename MapTy::iterator I = Map.find(Lookup);
1077 // Is it in the map?
1078 if (I != Map.end())
1079 Result = static_cast<ConstantClass *>(I->second);
1080
1081 if (!Result) {
1082 // If no preexisting value, create one now...
1083 Result = Create(Ty, V, I);
1084 }
1085
1086 return Result;
1087 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001088
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001089 void remove(ConstantClass *CP) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001090 sys::SmartScopedLock<true> Lock(ValueMapLock);
Jim Laskeyc03caef2006-07-17 17:38:29 +00001091 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001092 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +00001093 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001094
Chris Lattner935aa922005-10-04 17:48:46 +00001095 if (HasLargeKey) // Remember the reverse mapping if needed.
1096 InverseMap.erase(CP);
1097
Chris Lattnerb50d1352003-10-05 00:17:43 +00001098 // Now that we found the entry, make sure this isn't the entry that
1099 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001100 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001101 if (Ty->isAbstract()) {
1102 assert(AbstractTypeMap.count(Ty) &&
1103 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +00001104 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +00001105 if (ATMEntryIt == I) {
1106 // Yes, we are removing the representative entry for this type.
1107 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001108 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001109
Chris Lattnerb50d1352003-10-05 00:17:43 +00001110 // First check the entry before this one...
1111 if (TmpIt != Map.begin()) {
1112 --TmpIt;
1113 if (TmpIt->first.first != Ty) // Not the same type, move back...
1114 ++TmpIt;
1115 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001116
Chris Lattnerb50d1352003-10-05 00:17:43 +00001117 // If we didn't find the same type, try to move forward...
1118 if (TmpIt == ATMEntryIt) {
1119 ++TmpIt;
1120 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
1121 --TmpIt; // No entry afterwards with the same type
1122 }
1123
1124 // If there is another entry in the map of the same abstract type,
1125 // update the AbstractTypeMap entry now.
1126 if (TmpIt != ATMEntryIt) {
1127 ATMEntryIt = TmpIt;
1128 } else {
1129 // Otherwise, we are removing the last instance of this type
1130 // from the table. Remove from the ATM, and from user list.
1131 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
1132 AbstractTypeMap.erase(Ty);
1133 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001134 }
Chris Lattnerb50d1352003-10-05 00:17:43 +00001135 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001136
Chris Lattnerb50d1352003-10-05 00:17:43 +00001137 Map.erase(I);
1138 }
1139
Chris Lattner3b793c62005-10-04 21:35:50 +00001140
1141 /// MoveConstantToNewSlot - If we are about to change C to be the element
1142 /// specified by I, update our internal data structures to reflect this
1143 /// fact.
Owen Anderson61794042009-06-17 20:10:08 +00001144 /// NOTE: This function is not locked. It is the responsibility of the
1145 /// caller to enforce proper synchronization if using this method.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001146 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +00001147 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001148 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +00001149 assert(OldI != Map.end() && "Constant not found in constant table!");
1150 assert(OldI->second == C && "Didn't find correct element?");
1151
1152 // If this constant is the representative element for its abstract type,
1153 // update the AbstractTypeMap so that the representative element is I.
1154 if (C->getType()->isAbstract()) {
1155 typename AbstractTypeMapTy::iterator ATI =
1156 AbstractTypeMap.find(C->getType());
1157 assert(ATI != AbstractTypeMap.end() &&
1158 "Abstract type not in AbstractTypeMap?");
1159 if (ATI->second == OldI)
1160 ATI->second = I;
1161 }
1162
1163 // Remove the old entry from the map.
1164 Map.erase(OldI);
1165
1166 // Update the inverse map so that we know that this constant is now
1167 // located at descriptor I.
1168 if (HasLargeKey) {
1169 assert(I->second == C && "Bad inversemap entry!");
1170 InverseMap[C] = I;
1171 }
1172 }
1173
Chris Lattnerb50d1352003-10-05 00:17:43 +00001174 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001175 sys::SmartScopedLock<true> Lock(ValueMapLock);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001176 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001177 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001178
1179 assert(I != AbstractTypeMap.end() &&
1180 "Abstract type not in AbstractTypeMap?");
1181
1182 // Convert a constant at a time until the last one is gone. The last one
1183 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1184 // eliminated eventually.
1185 do {
1186 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001187 TypeClass>::convert(
1188 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001189 cast<TypeClass>(NewTy));
1190
Jim Laskeyc03caef2006-07-17 17:38:29 +00001191 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001192 } while (I != AbstractTypeMap.end());
1193 }
1194
1195 // If the type became concrete without being refined to any other existing
1196 // type, we just remove ourselves from the ATU list.
1197 void typeBecameConcrete(const DerivedType *AbsTy) {
Owen Andersond830eb82009-06-18 19:10:19 +00001198 AbsTy->removeAbstractTypeUser(this);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001199 }
1200
1201 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001202 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001203 }
1204 };
1205}
1206
Chris Lattnera84df0a22006-09-28 23:36:21 +00001207
Chris Lattner28173502007-02-20 06:11:36 +00001208
Chris Lattner9fba3da2004-02-15 05:53:04 +00001209//---- ConstantAggregateZero::get() implementation...
1210//
1211namespace llvm {
1212 // ConstantAggregateZero does not take extra "value" argument...
1213 template<class ValType>
1214 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1215 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1216 return new ConstantAggregateZero(Ty);
1217 }
1218 };
1219
1220 template<>
1221 struct ConvertConstantType<ConstantAggregateZero, Type> {
1222 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1223 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001224 Constant *New = ConstantAggregateZero::get(NewTy);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001225 assert(New != OldC && "Didn't replace constant??");
1226 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001227 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner9fba3da2004-02-15 05:53:04 +00001228 }
1229 };
1230}
1231
Chris Lattner69edc982006-09-28 00:35:06 +00001232static ManagedStatic<ValueMap<char, Type,
1233 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001234
Chris Lattner3e650af2004-08-04 04:48:01 +00001235static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1236
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001237ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001238 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001239 "Cannot create an aggregate zero of non-aggregate type!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001240
1241 // Implicitly locked.
1242 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001243}
1244
Dan Gohman92b551b2009-03-03 02:55:14 +00001245/// destroyConstant - Remove the constant from the constant table...
1246///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001247void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001248 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001249 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001250 destroyConstantImpl();
1251}
1252
Chris Lattner3462ae32001-12-03 22:26:30 +00001253//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001254//
Chris Lattner189d19f2003-11-21 20:23:48 +00001255namespace llvm {
1256 template<>
1257 struct ConvertConstantType<ConstantArray, ArrayType> {
1258 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1259 // Make everyone now use a constant of the new type...
1260 std::vector<Constant*> C;
1261 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1262 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001263 Constant *New = ConstantArray::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001264 assert(New != OldC && "Didn't replace constant??");
1265 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001266 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001267 }
1268 };
1269}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001270
Chris Lattner3e650af2004-08-04 04:48:01 +00001271static std::vector<Constant*> getValType(ConstantArray *CA) {
1272 std::vector<Constant*> Elements;
1273 Elements.reserve(CA->getNumOperands());
1274 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1275 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1276 return Elements;
1277}
1278
Chris Lattnerb64419a2005-10-03 22:51:37 +00001279typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001280 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001281static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001282
Chris Lattner015e8212004-02-15 04:14:47 +00001283Constant *ConstantArray::get(const ArrayType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001284 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001285 // If this is an all-zero array, return a ConstantAggregateZero object
1286 if (!V.empty()) {
1287 Constant *C = V[0];
Owen Anderson2d7231d2009-06-17 18:40:29 +00001288 if (!C->isNullValue()) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001289 // Implicitly locked.
1290 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001291 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001292 for (unsigned i = 1, e = V.size(); i != e; ++i)
Owen Anderson2d7231d2009-06-17 18:40:29 +00001293 if (V[i] != C) {
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001294 // Implicitly locked.
1295 return ArrayConstants->getOrCreate(Ty, V);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001296 }
Chris Lattner9fba3da2004-02-15 05:53:04 +00001297 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001298
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001299 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001300}
1301
Dan Gohman92b551b2009-03-03 02:55:14 +00001302/// destroyConstant - Remove the constant from the constant table...
1303///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001304void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001305 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001306 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001307 destroyConstantImpl();
1308}
1309
Reid Spencer6f614532006-05-30 08:23:18 +00001310/// ConstantArray::get(const string&) - Return an array that is initialized to
1311/// contain the specified string. If length is zero then a null terminator is
1312/// added to the specified string so that it may be used in a natural way.
1313/// Otherwise, the length parameter specifies how much of the string to use
1314/// and it won't be null terminated.
1315///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001316Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001317 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001318 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +00001319 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001320
1321 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001322 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001323 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001324 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001325
Reid Spencer8d9336d2006-12-31 05:26:44 +00001326 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001327 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001328}
1329
Reid Spencer2546b762007-01-26 07:37:34 +00001330/// isString - This method returns true if the array is an array of i8, and
1331/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001332bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001333 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001334 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001335 return false;
1336 // Check the elements to make sure they are all integers, not constant
1337 // expressions.
1338 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1339 if (!isa<ConstantInt>(getOperand(i)))
1340 return false;
1341 return true;
1342}
1343
Evan Cheng3763c5b2006-10-26 19:15:05 +00001344/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001345/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001346/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001347bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001348 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001349 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001350 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001351
Evan Chenge974da62006-10-26 21:48:03 +00001352 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001353 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001354 return false;
1355 // Other elements must be non-null integers.
1356 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1357 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001358 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001359 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001360 return false;
1361 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001362 return true;
1363}
1364
1365
Dan Gohman92b551b2009-03-03 02:55:14 +00001366/// getAsString - If the sub-element type of this array is i8
1367/// then this method converts the array to an std::string and returns it.
1368/// Otherwise, it asserts out.
1369///
Chris Lattner81fabb02002-08-26 17:53:56 +00001370std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001371 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001372 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001373 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001374 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001375 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001376 return Result;
1377}
1378
1379
Chris Lattner3462ae32001-12-03 22:26:30 +00001380//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001381//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001382
Chris Lattner189d19f2003-11-21 20:23:48 +00001383namespace llvm {
1384 template<>
1385 struct ConvertConstantType<ConstantStruct, StructType> {
1386 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1387 // Make everyone now use a constant of the new type...
1388 std::vector<Constant*> C;
1389 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1390 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001391 Constant *New = ConstantStruct::get(NewTy, C);
Chris Lattner189d19f2003-11-21 20:23:48 +00001392 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001393
Chris Lattner189d19f2003-11-21 20:23:48 +00001394 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001395 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001396 }
1397 };
1398}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001399
Chris Lattner8760ec72005-10-04 01:17:50 +00001400typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001401 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001402static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001403
Chris Lattner3e650af2004-08-04 04:48:01 +00001404static std::vector<Constant*> getValType(ConstantStruct *CS) {
1405 std::vector<Constant*> Elements;
1406 Elements.reserve(CS->getNumOperands());
1407 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1408 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1409 return Elements;
1410}
1411
Chris Lattner015e8212004-02-15 04:14:47 +00001412Constant *ConstantStruct::get(const StructType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001413 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001414 // Create a ConstantAggregateZero value if all elements are zeros...
1415 for (unsigned i = 0, e = V.size(); i != e; ++i)
Owen Anderson61794042009-06-17 20:10:08 +00001416 if (!V[i]->isNullValue())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001417 // Implicitly locked.
1418 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001419
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001420 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001421}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001422
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001423Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001424 std::vector<const Type*> StructEls;
1425 StructEls.reserve(V.size());
1426 for (unsigned i = 0, e = V.size(); i != e; ++i)
1427 StructEls.push_back(V[i]->getType());
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001428 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001429}
1430
Chris Lattnerd7a73302001-10-13 06:57:33 +00001431// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001432//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001433void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001434 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001435 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001436 destroyConstantImpl();
1437}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001438
Reid Spencerd84d35b2007-02-15 02:26:10 +00001439//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001440//
1441namespace llvm {
1442 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001443 struct ConvertConstantType<ConstantVector, VectorType> {
1444 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001445 // Make everyone now use a constant of the new type...
1446 std::vector<Constant*> C;
1447 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1448 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001449 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001450 assert(New != OldC && "Didn't replace constant??");
1451 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001452 OldC->destroyConstant(); // This constant is now dead, destroy it.
Brian Gaeke02209042004-08-20 06:00:58 +00001453 }
1454 };
1455}
1456
Reid Spencerd84d35b2007-02-15 02:26:10 +00001457static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001458 std::vector<Constant*> Elements;
1459 Elements.reserve(CP->getNumOperands());
1460 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1461 Elements.push_back(CP->getOperand(i));
1462 return Elements;
1463}
1464
Reid Spencerd84d35b2007-02-15 02:26:10 +00001465static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001466 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001467
Reid Spencerd84d35b2007-02-15 02:26:10 +00001468Constant *ConstantVector::get(const VectorType *Ty,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001469 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001470 assert(!V.empty() && "Vectors can't be empty");
1471 // If this is an all-undef or alll-zero vector, return a
1472 // ConstantAggregateZero or UndefValue.
1473 Constant *C = V[0];
1474 bool isZero = C->isNullValue();
1475 bool isUndef = isa<UndefValue>(C);
1476
1477 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001478 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001479 if (V[i] != C) {
1480 isZero = isUndef = false;
1481 break;
1482 }
Brian Gaeke02209042004-08-20 06:00:58 +00001483 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001484
1485 if (isZero)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001486 return ConstantAggregateZero::get(Ty);
Chris Lattnerd977c072008-07-10 00:44:03 +00001487 if (isUndef)
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001488 return UndefValue::get(Ty);
Owen Anderson61794042009-06-17 20:10:08 +00001489
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001490 // Implicitly locked.
1491 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001492}
1493
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001494Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001495 assert(!V.empty() && "Cannot infer type if V is empty");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001496 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001497}
1498
1499// destroyConstant - Remove the constant from the constant table...
1500//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001501void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001502 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001503 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001504 destroyConstantImpl();
1505}
1506
Dan Gohman30978072007-05-24 14:36:04 +00001507/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001508/// is set to all ones.
1509/// @returns true iff this constant's emements are all set to all ones.
1510/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001511bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001512 // Check out first element.
1513 const Constant *Elt = getOperand(0);
1514 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1515 if (!CI || !CI->isAllOnesValue()) return false;
1516 // Then make sure all remaining elements point to the same value.
1517 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1518 if (getOperand(I) != Elt) return false;
1519 }
1520 return true;
1521}
1522
Dan Gohman07159202007-10-17 17:51:30 +00001523/// getSplatValue - If this is a splat constant, where all of the
1524/// elements have the same value, return that value. Otherwise return null.
1525Constant *ConstantVector::getSplatValue() {
1526 // Check out first element.
1527 Constant *Elt = getOperand(0);
1528 // Then make sure all remaining elements point to the same value.
1529 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1530 if (getOperand(I) != Elt) return 0;
1531 return Elt;
1532}
1533
Chris Lattner3462ae32001-12-03 22:26:30 +00001534//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001535//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001536
Chris Lattner189d19f2003-11-21 20:23:48 +00001537namespace llvm {
1538 // ConstantPointerNull does not take extra "value" argument...
1539 template<class ValType>
1540 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1541 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1542 return new ConstantPointerNull(Ty);
1543 }
1544 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001545
Chris Lattner189d19f2003-11-21 20:23:48 +00001546 template<>
1547 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1548 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1549 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001550 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001551 assert(New != OldC && "Didn't replace constant??");
1552 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001553 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001554 }
1555 };
1556}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001557
Chris Lattner69edc982006-09-28 00:35:06 +00001558static ManagedStatic<ValueMap<char, PointerType,
1559 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001560
Chris Lattner3e650af2004-08-04 04:48:01 +00001561static char getValType(ConstantPointerNull *) {
1562 return 0;
1563}
1564
1565
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001566ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001567 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001568 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001569}
1570
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001571// destroyConstant - Remove the constant from the constant table...
1572//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001573void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001574 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001575 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001576 destroyConstantImpl();
1577}
1578
1579
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001580//---- UndefValue::get() implementation...
1581//
1582
1583namespace llvm {
1584 // UndefValue does not take extra "value" argument...
1585 template<class ValType>
1586 struct ConstantCreator<UndefValue, Type, ValType> {
1587 static UndefValue *create(const Type *Ty, const ValType &V) {
1588 return new UndefValue(Ty);
1589 }
1590 };
1591
1592 template<>
1593 struct ConvertConstantType<UndefValue, Type> {
1594 static void convert(UndefValue *OldC, const Type *NewTy) {
1595 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001596 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001597 assert(New != OldC && "Didn't replace constant??");
1598 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001599 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001600 }
1601 };
1602}
1603
Chris Lattner69edc982006-09-28 00:35:06 +00001604static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001605
1606static char getValType(UndefValue *) {
1607 return 0;
1608}
1609
1610
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001611UndefValue *UndefValue::get(const Type *Ty) {
1612 // Implicitly locked.
1613 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001614}
1615
1616// destroyConstant - Remove the constant from the constant table.
1617//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001618void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001619 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001620 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001621 destroyConstantImpl();
1622}
1623
Nick Lewycky49f89192009-04-04 07:22:01 +00001624//---- MDString::get() implementation
1625//
1626
1627MDString::MDString(const char *begin, const char *end)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001628 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky49f89192009-04-04 07:22:01 +00001629 StrBegin(begin), StrEnd(end) {}
1630
1631static ManagedStatic<StringMap<MDString*> > MDStringCache;
1632
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001633MDString *MDString::get(const char *StrBegin, const char *StrEnd) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001634 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001635 StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
1636 StrBegin, StrEnd);
1637 MDString *&S = Entry.getValue();
1638 if (!S) S = new MDString(Entry.getKeyData(),
1639 Entry.getKeyData() + Entry.getKeyLength());
Owen Anderson65c5cd72009-06-17 20:34:43 +00001640
Owen Andersond830eb82009-06-18 19:10:19 +00001641 return S;
Nick Lewycky49f89192009-04-04 07:22:01 +00001642}
1643
Devang Patel4c563162009-06-24 22:42:39 +00001644MDString *MDString::get(const std::string &Str) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001645 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Devang Patel4c563162009-06-24 22:42:39 +00001646 StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(
1647 Str.data(), Str.data() + Str.size());
1648 MDString *&S = Entry.getValue();
1649 if (!S) S = new MDString(Entry.getKeyData(),
1650 Entry.getKeyData() + Entry.getKeyLength());
1651
1652 return S;
1653}
1654
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001655void MDString::destroyConstant() {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001656 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001657 MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd));
Nick Lewycky49f89192009-04-04 07:22:01 +00001658 destroyConstantImpl();
1659}
1660
1661//---- MDNode::get() implementation
1662//
1663
1664static ManagedStatic<FoldingSet<MDNode> > MDNodeSet;
1665
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001666MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001667 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001668 for (unsigned i = 0; i != NumVals; ++i)
1669 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001670}
1671
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001672void MDNode::Profile(FoldingSetNodeID &ID) const {
1673 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001674 ID.AddPointer(*I);
1675}
1676
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001677MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) {
Nick Lewycky49f89192009-04-04 07:22:01 +00001678 FoldingSetNodeID ID;
1679 for (unsigned i = 0; i != NumVals; ++i)
1680 ID.AddPointer(Vals[i]);
1681
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001682 ConstantsLock->reader_acquire();
Owen Andersond830eb82009-06-18 19:10:19 +00001683 void *InsertPoint;
1684 MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001685 ConstantsLock->reader_release();
Owen Andersond830eb82009-06-18 19:10:19 +00001686
1687 if (!N) {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001688 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001689 N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
1690 if (!N) {
Owen Andersond830eb82009-06-18 19:10:19 +00001691 // InsertPoint will have been set by the FindNodeOrInsertPos call.
1692 N = new(0) MDNode(Vals, NumVals);
1693 MDNodeSet->InsertNode(N, InsertPoint);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001694 }
Owen Anderson2d7231d2009-06-17 18:40:29 +00001695 }
Owen Andersond830eb82009-06-18 19:10:19 +00001696 return N;
Nick Lewycky49f89192009-04-04 07:22:01 +00001697}
1698
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001699void MDNode::destroyConstant() {
Owen Anderson5c96ef72009-07-07 18:33:04 +00001700 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Owen Andersond830eb82009-06-18 19:10:19 +00001701 MDNodeSet->RemoveNode(this);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001702
Nick Lewycky49f89192009-04-04 07:22:01 +00001703 destroyConstantImpl();
1704}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001705
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001706//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001707//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001708
Dan Gohmand78c4002008-05-13 00:00:25 +00001709namespace {
1710
Reid Spenceree3c9912006-12-04 05:19:50 +00001711struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001712 typedef SmallVector<unsigned, 4> IndexList;
1713
1714 ExprMapKeyType(unsigned opc,
1715 const std::vector<Constant*> &ops,
1716 unsigned short pred = 0,
1717 const IndexList &inds = IndexList())
1718 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001719 uint16_t opcode;
1720 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001721 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001722 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001723 bool operator==(const ExprMapKeyType& that) const {
1724 return this->opcode == that.opcode &&
1725 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001726 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001727 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001728 }
1729 bool operator<(const ExprMapKeyType & that) const {
1730 return this->opcode < that.opcode ||
1731 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1732 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001733 this->operands < that.operands) ||
1734 (this->opcode == that.opcode && this->predicate == that.predicate &&
1735 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001736 }
1737
1738 bool operator!=(const ExprMapKeyType& that) const {
1739 return !(*this == that);
1740 }
1741};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001742
Dan Gohmand78c4002008-05-13 00:00:25 +00001743}
1744
Chris Lattner189d19f2003-11-21 20:23:48 +00001745namespace llvm {
1746 template<>
1747 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001748 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1749 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001750 if (Instruction::isCast(V.opcode))
1751 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1752 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001753 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001754 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1755 if (V.opcode == Instruction::Select)
1756 return new SelectConstantExpr(V.operands[0], V.operands[1],
1757 V.operands[2]);
1758 if (V.opcode == Instruction::ExtractElement)
1759 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1760 if (V.opcode == Instruction::InsertElement)
1761 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1762 V.operands[2]);
1763 if (V.opcode == Instruction::ShuffleVector)
1764 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1765 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001766 if (V.opcode == Instruction::InsertValue)
1767 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1768 V.indices, Ty);
1769 if (V.opcode == Instruction::ExtractValue)
1770 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001771 if (V.opcode == Instruction::GetElementPtr) {
1772 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001773 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001774 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001775
Reid Spenceree3c9912006-12-04 05:19:50 +00001776 // The compare instructions are weird. We have to encode the predicate
1777 // value and it is combined with the instruction opcode by multiplying
1778 // the opcode by one hundred. We must decode this to get the predicate.
1779 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001780 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001781 V.operands[0], V.operands[1]);
1782 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001783 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1784 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001785 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001786 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001787 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001788 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001789
Chris Lattner189d19f2003-11-21 20:23:48 +00001790 template<>
1791 struct ConvertConstantType<ConstantExpr, Type> {
1792 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1793 Constant *New;
1794 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001795 case Instruction::Trunc:
1796 case Instruction::ZExt:
1797 case Instruction::SExt:
1798 case Instruction::FPTrunc:
1799 case Instruction::FPExt:
1800 case Instruction::UIToFP:
1801 case Instruction::SIToFP:
1802 case Instruction::FPToUI:
1803 case Instruction::FPToSI:
1804 case Instruction::PtrToInt:
1805 case Instruction::IntToPtr:
1806 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001807 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001808 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001809 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001810 case Instruction::Select:
1811 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1812 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001813 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001814 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001815 default:
1816 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001817 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001818 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001819 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001820 break;
1821 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001822 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001823 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001824 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001825 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001826 break;
1827 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001828
Chris Lattner189d19f2003-11-21 20:23:48 +00001829 assert(New != OldC && "Didn't replace constant??");
1830 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001831 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001832 }
1833 };
1834} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001835
1836
Chris Lattner3e650af2004-08-04 04:48:01 +00001837static ExprMapKeyType getValType(ConstantExpr *CE) {
1838 std::vector<Constant*> Operands;
1839 Operands.reserve(CE->getNumOperands());
1840 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1841 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001842 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001843 CE->isCompare() ? CE->getPredicate() : 0,
1844 CE->hasIndices() ?
1845 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001846}
1847
Chris Lattner69edc982006-09-28 00:35:06 +00001848static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1849 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001850
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001851/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001852/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001853static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001854 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001855 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001856 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001857 if (Constant *FC =
1858 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001859 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001860
Vikram S. Adve4c485332002-07-15 18:19:33 +00001861 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001862 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001863 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001864
Owen Anderson61794042009-06-17 20:10:08 +00001865 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001866 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001867}
Reid Spencerf37dc652006-12-05 19:14:13 +00001868
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001869Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001870 Instruction::CastOps opc = Instruction::CastOps(oc);
1871 assert(Instruction::isCast(opc) && "opcode out of range");
1872 assert(C && Ty && "Null arguments to getCast");
1873 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1874
1875 switch (opc) {
1876 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001877 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001878 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001879 case Instruction::Trunc: return getTrunc(C, Ty);
1880 case Instruction::ZExt: return getZExt(C, Ty);
1881 case Instruction::SExt: return getSExt(C, Ty);
1882 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1883 case Instruction::FPExt: return getFPExtend(C, Ty);
1884 case Instruction::UIToFP: return getUIToFP(C, Ty);
1885 case Instruction::SIToFP: return getSIToFP(C, Ty);
1886 case Instruction::FPToUI: return getFPToUI(C, Ty);
1887 case Instruction::FPToSI: return getFPToSI(C, Ty);
1888 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1889 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1890 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001891 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001892 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001893}
1894
Reid Spencer5c140882006-12-04 20:17:56 +00001895Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001896 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001897 return getCast(Instruction::BitCast, C, Ty);
1898 return getCast(Instruction::ZExt, C, Ty);
1899}
1900
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001901Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001902 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001903 return getCast(Instruction::BitCast, C, Ty);
1904 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001905}
1906
1907Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001908 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001909 return getCast(Instruction::BitCast, C, Ty);
1910 return getCast(Instruction::Trunc, C, Ty);
1911}
1912
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001913Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001914 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001915 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001916
Chris Lattner03c49532007-01-15 02:27:26 +00001917 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001918 return getCast(Instruction::PtrToInt, S, Ty);
1919 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001920}
1921
Reid Spencer56521c42006-12-12 00:51:07 +00001922Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1923 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001924 assert(C->getType()->isIntOrIntVector() &&
1925 Ty->isIntOrIntVector() && "Invalid cast");
1926 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1927 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001928 Instruction::CastOps opcode =
1929 (SrcBits == DstBits ? Instruction::BitCast :
1930 (SrcBits > DstBits ? Instruction::Trunc :
1931 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1932 return getCast(opcode, C, Ty);
1933}
1934
1935Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001936 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001937 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001938 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1939 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001940 if (SrcBits == DstBits)
1941 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001942 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001943 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001944 return getCast(opcode, C, Ty);
1945}
1946
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001947Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001948#ifndef NDEBUG
1949 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1950 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1951#endif
1952 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1953 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1954 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1955 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001956 "SrcTy must be larger than DestTy for Trunc!");
1957
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001958 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001959}
1960
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001961Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001962#ifndef NDEBUG
1963 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1964 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1965#endif
1966 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1967 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1968 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1969 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001970 "SrcTy must be smaller than DestTy for SExt!");
1971
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001972 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001973}
1974
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001975Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001976#ifndef NDEBUG
1977 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1978 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1979#endif
1980 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1981 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1982 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1983 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001984 "SrcTy must be smaller than DestTy for ZExt!");
1985
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001986 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001987}
1988
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001989Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001990#ifndef NDEBUG
1991 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1992 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1993#endif
1994 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1995 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1996 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001997 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001998 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001999}
2000
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002001Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002002#ifndef NDEBUG
2003 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2004 bool toVec = Ty->getTypeID() == Type::VectorTyID;
2005#endif
2006 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2007 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
2008 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002009 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002010 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002011}
2012
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002013Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002014#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002015 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2016 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002017#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002018 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2019 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
2020 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002021 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002022}
2023
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002024Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002025#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002026 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2027 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002028#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002029 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2030 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002031 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002032 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002033}
2034
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002035Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002036#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002037 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2038 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002039#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002040 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2041 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
2042 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002043 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002044}
2045
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002046Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002047#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002048 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2049 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002050#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002051 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2052 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
2053 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002054 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002055}
2056
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002057Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002058 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00002059 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002060 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002061}
2062
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002063Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00002064 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002065 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002066 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002067}
2068
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002069Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002070 // BitCast implies a no-op cast of type only. No bits change. However, you
2071 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00002072#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002073 const Type *SrcTy = C->getType();
2074 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00002075 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002076
2077 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
2078 // or nonptr->ptr). For all the other types, the cast is okay if source and
2079 // destination bit widths are identical.
2080 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2081 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00002082#endif
Chris Lattnere4086012009-03-08 04:06:26 +00002083 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00002084
2085 // It is common to ask for a bitcast of a value to its own type, handle this
2086 // speedily.
2087 if (C->getType() == DstTy) return C;
2088
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002089 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00002090}
2091
Chris Lattnerb50d1352003-10-05 00:17:43 +00002092Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002093 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002094 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00002095 assert(Opcode >= Instruction::BinaryOpsBegin &&
2096 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002097 "Invalid opcode in binary constant expression");
2098 assert(C1->getType() == C2->getType() &&
2099 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00002100
Reid Spencer542964f2007-01-11 18:21:29 +00002101 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00002102 if (Constant *FC = ConstantFoldBinaryInstruction(
2103 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00002104 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00002105
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002106 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002107 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00002108
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002109 // Implicitly locked.
2110 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002111}
2112
Reid Spencer266e42b2006-12-23 06:05:41 +00002113Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00002114 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002115 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002116 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00002117 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
2118 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
2119 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
2120 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
2121 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
2122 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002123 return getFCmp(predicate, C1, C2);
2124
Nate Begemanc96e2e42008-07-25 17:35:37 +00002125 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
2126 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
2127 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
2128 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002129 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00002130 }
Reid Spencera009d0d2006-12-04 21:35:24 +00002131}
2132
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002133Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002134 // API compatibility: Adjust integer opcodes to floating-point opcodes.
2135 if (C1->getType()->isFPOrFPVector()) {
2136 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
2137 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
2138 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
2139 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002140#ifndef NDEBUG
2141 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002142 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002143 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002144 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002145 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00002146 assert(C1->getType()->isIntOrIntVector() &&
2147 "Tried to create an integer operation on a non-integer type!");
2148 break;
2149 case Instruction::FAdd:
2150 case Instruction::FSub:
2151 case Instruction::FMul:
2152 assert(C1->getType() == C2->getType() && "Op types should be identical!");
2153 assert(C1->getType()->isFPOrFPVector() &&
2154 "Tried to create a floating-point operation on a "
2155 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002156 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002157 case Instruction::UDiv:
2158 case Instruction::SDiv:
2159 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002160 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002161 "Tried to create an arithmetic operation on a non-arithmetic type!");
2162 break;
2163 case Instruction::FDiv:
2164 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002165 assert(C1->getType()->isFPOrFPVector() &&
2166 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002167 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002168 case Instruction::URem:
2169 case Instruction::SRem:
2170 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002171 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002172 "Tried to create an arithmetic operation on a non-arithmetic type!");
2173 break;
2174 case Instruction::FRem:
2175 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002176 assert(C1->getType()->isFPOrFPVector() &&
2177 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002178 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002179 case Instruction::And:
2180 case Instruction::Or:
2181 case Instruction::Xor:
2182 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00002183 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00002184 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002185 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002186 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00002187 case Instruction::LShr:
2188 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00002189 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00002190 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002191 "Tried to create a shift operation on a non-integer type!");
2192 break;
2193 default:
2194 break;
2195 }
2196#endif
2197
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002198 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002199}
2200
Reid Spencer266e42b2006-12-23 06:05:41 +00002201Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00002202 Constant *C1, Constant *C2) {
2203 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002204 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00002205}
2206
Chris Lattner6e415c02004-03-12 05:54:04 +00002207Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002208 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00002209 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00002210
2211 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00002212 if (Constant *SC = ConstantFoldSelectInstruction(
2213 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00002214 return SC; // Fold common cases
2215
2216 std::vector<Constant*> argVec(3, C);
2217 argVec[1] = V1;
2218 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00002219 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00002220
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002221 // Implicitly locked.
2222 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00002223}
2224
Chris Lattnerb50d1352003-10-05 00:17:43 +00002225Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00002226 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002227 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002228 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2229 Idxs+NumIdx) ==
2230 cast<PointerType>(ReqTy)->getElementType() &&
2231 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00002232
Owen Anderson53a52212009-07-13 04:09:18 +00002233 if (Constant *FC = ConstantFoldGetElementPtr(
2234 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00002235 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00002236
Chris Lattnerb50d1352003-10-05 00:17:43 +00002237 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002238 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002239 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002240 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002241 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002242 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002243 for (unsigned i = 0; i != NumIdx; ++i)
2244 ArgVec.push_back(cast<Constant>(Idxs[i]));
2245 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002246
2247 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002248 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002249}
2250
Chris Lattner302116a2007-01-31 04:40:28 +00002251Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002252 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002253 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002254 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002255 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002256 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002257 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002258 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002259}
2260
Chris Lattner302116a2007-01-31 04:40:28 +00002261Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002262 unsigned NumIdx) {
2263 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002264}
2265
Chris Lattner302116a2007-01-31 04:40:28 +00002266
Reid Spenceree3c9912006-12-04 05:19:50 +00002267Constant *
2268ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2269 assert(LHS->getType() == RHS->getType());
2270 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2271 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2272
Owen Anderson53a52212009-07-13 04:09:18 +00002273 if (Constant *FC = ConstantFoldCompareInstruction(
2274 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002275 return FC; // Fold a few common cases...
2276
2277 // Look up the constant in the table first to ensure uniqueness
2278 std::vector<Constant*> ArgVec;
2279 ArgVec.push_back(LHS);
2280 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002281 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002282 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002283
2284 // Implicitly locked.
2285 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002286}
2287
2288Constant *
2289ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2290 assert(LHS->getType() == RHS->getType());
2291 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2292
Owen Anderson53a52212009-07-13 04:09:18 +00002293 if (Constant *FC = ConstantFoldCompareInstruction(
2294 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002295 return FC; // Fold a few common cases...
2296
2297 // Look up the constant in the table first to ensure uniqueness
2298 std::vector<Constant*> ArgVec;
2299 ArgVec.push_back(LHS);
2300 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002301 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002302 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002303
2304 // Implicitly locked.
2305 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002306}
2307
Robert Bocchino23004482006-01-10 19:05:34 +00002308Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2309 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002310 if (Constant *FC = ConstantFoldExtractElementInstruction(
2311 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002312 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002313 // Look up the constant in the table first to ensure uniqueness
2314 std::vector<Constant*> ArgVec(1, Val);
2315 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002316 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002317
2318 // Implicitly locked.
2319 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002320}
2321
2322Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002323 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002324 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002325 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002326 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002327 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002328 Val, Idx);
2329}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002330
Robert Bocchinoca27f032006-01-17 20:07:22 +00002331Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2332 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00002333 if (Constant *FC = ConstantFoldInsertElementInstruction(
2334 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00002335 return FC; // Fold a few common cases...
2336 // Look up the constant in the table first to ensure uniqueness
2337 std::vector<Constant*> ArgVec(1, Val);
2338 ArgVec.push_back(Elt);
2339 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002340 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002341
2342 // Implicitly locked.
2343 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002344}
2345
2346Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2347 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002348 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002349 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002350 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002351 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002352 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002353 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002354 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002355}
2356
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002357Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2358 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00002359 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
2360 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002361 return FC; // Fold a few common cases...
2362 // Look up the constant in the table first to ensure uniqueness
2363 std::vector<Constant*> ArgVec(1, V1);
2364 ArgVec.push_back(V2);
2365 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002366 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00002367
2368 // Implicitly locked.
2369 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002370}
2371
2372Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2373 Constant *Mask) {
2374 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2375 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002376
2377 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2378 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2379 const Type *ShufTy = VectorType::get(EltTy, NElts);
2380 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002381}
2382
Dan Gohman12fce772008-05-15 19:50:34 +00002383Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2384 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002385 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002386 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2387 Idxs+NumIdx) == Val->getType() &&
2388 "insertvalue indices invalid!");
2389 assert(Agg->getType() == ReqTy &&
2390 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002391 assert(Agg->getType()->isFirstClassType() &&
2392 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002393 Constant *FC = ConstantFoldInsertValueInstruction(
2394 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002395 assert(FC && "InsertValue constant expr couldn't be folded!");
2396 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002397}
2398
2399Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002400 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002401 assert(Agg->getType()->isFirstClassType() &&
2402 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002403
Dan Gohman0752bff2008-05-23 00:36:11 +00002404 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002405#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002406 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002407 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002408#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002409 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002410 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2411}
2412
2413Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002414 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002415 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2416 Idxs+NumIdx) == ReqTy &&
2417 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002418 assert(Agg->getType()->isFirstClassType() &&
2419 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00002420 Constant *FC = ConstantFoldExtractValueInstruction(
2421 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00002422 assert(FC && "ExtractValue constant expr couldn't be folded!");
2423 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002424}
2425
2426Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002427 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002428 assert(Agg->getType()->isFirstClassType() &&
2429 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002430
2431 const Type *ReqTy =
2432 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2433 assert(ReqTy && "extractvalue indices invalid!");
2434 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2435}
2436
Vikram S. Adve4c485332002-07-15 18:19:33 +00002437// destroyConstant - Remove the constant from the constant table...
2438//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002439void ConstantExpr::destroyConstant() {
2440 // Implicitly locked.
2441 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002442 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002443}
2444
Chris Lattner3cd8c562002-07-30 18:54:25 +00002445const char *ConstantExpr::getOpcodeName() const {
2446 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002447}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002448
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002449//===----------------------------------------------------------------------===//
2450// replaceUsesOfWithOnConstant implementations
2451
Chris Lattner913849b2007-08-21 00:55:23 +00002452/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2453/// 'From' to be uses of 'To'. This must update the uniquing data structures
2454/// etc.
2455///
2456/// Note that we intentionally replace all uses of From with To here. Consider
2457/// a large array that uses 'From' 1000 times. By handling this case all here,
2458/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2459/// single invocation handles all 1000 uses. Handling them one at a time would
2460/// work, but would be really slow because it would have to unique each updated
2461/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002462void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002463 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002464 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002465 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002466
Jim Laskeyc03caef2006-07-17 17:38:29 +00002467 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002468 Lookup.first.first = getType();
2469 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002470
Chris Lattnerb64419a2005-10-03 22:51:37 +00002471 std::vector<Constant*> &Values = Lookup.first.second;
2472 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002473
Chris Lattner8760ec72005-10-04 01:17:50 +00002474 // Fill values with the modified operands of the constant array. Also,
2475 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002476 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002477 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002478 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002479 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2480 Constant *Val = cast<Constant>(O->get());
2481 if (Val == From) {
2482 Val = ToC;
2483 ++NumUpdated;
2484 }
2485 Values.push_back(Val);
2486 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002487 } else {
2488 isAllZeros = true;
2489 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2490 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002491 if (Val == From) {
2492 Val = ToC;
2493 ++NumUpdated;
2494 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002495 Values.push_back(Val);
2496 if (isAllZeros) isAllZeros = Val->isNullValue();
2497 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002498 }
2499
Chris Lattnerb64419a2005-10-03 22:51:37 +00002500 Constant *Replacement = 0;
2501 if (isAllZeros) {
2502 Replacement = ConstantAggregateZero::get(getType());
2503 } else {
2504 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002505 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002506 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002507 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002508 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002509
2510 if (Exists) {
2511 Replacement = I->second;
2512 } else {
2513 // Okay, the new shape doesn't exist in the system yet. Instead of
2514 // creating a new constant array, inserting it, replaceallusesof'ing the
2515 // old with the new, then deleting the old... just update the current one
2516 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002517 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002518
Chris Lattner913849b2007-08-21 00:55:23 +00002519 // Update to the new value. Optimize for the case when we have a single
2520 // operand that we're changing, but handle bulk updates efficiently.
2521 if (NumUpdated == 1) {
2522 unsigned OperandToUpdate = U-OperandList;
2523 assert(getOperand(OperandToUpdate) == From &&
2524 "ReplaceAllUsesWith broken!");
2525 setOperand(OperandToUpdate, ToC);
2526 } else {
2527 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2528 if (getOperand(i) == From)
2529 setOperand(i, ToC);
2530 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002531 return;
2532 }
2533 }
2534
2535 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002536 assert(Replacement != this && "I didn't contain From!");
2537
Chris Lattner7a1450d2005-10-04 18:13:04 +00002538 // Everyone using this now uses the replacement.
2539 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002540
2541 // Delete the old constant!
2542 destroyConstant();
2543}
2544
2545void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002546 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002547 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002548 Constant *ToC = cast<Constant>(To);
2549
Chris Lattnerdff59112005-10-04 18:47:09 +00002550 unsigned OperandToUpdate = U-OperandList;
2551 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2552
Jim Laskeyc03caef2006-07-17 17:38:29 +00002553 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002554 Lookup.first.first = getType();
2555 Lookup.second = this;
2556 std::vector<Constant*> &Values = Lookup.first.second;
2557 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002558
Chris Lattnerdff59112005-10-04 18:47:09 +00002559
Chris Lattner8760ec72005-10-04 01:17:50 +00002560 // Fill values with the modified operands of the constant struct. Also,
2561 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002562 bool isAllZeros = false;
2563 if (!ToC->isNullValue()) {
2564 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2565 Values.push_back(cast<Constant>(O->get()));
2566 } else {
2567 isAllZeros = true;
2568 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2569 Constant *Val = cast<Constant>(O->get());
2570 Values.push_back(Val);
2571 if (isAllZeros) isAllZeros = Val->isNullValue();
2572 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002573 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002574 Values[OperandToUpdate] = ToC;
2575
Chris Lattner8760ec72005-10-04 01:17:50 +00002576 Constant *Replacement = 0;
2577 if (isAllZeros) {
2578 Replacement = ConstantAggregateZero::get(getType());
2579 } else {
2580 // Check to see if we have this array type already.
Owen Anderson5c96ef72009-07-07 18:33:04 +00002581 sys::SmartScopedWriter<true> Writer(*ConstantsLock);
Chris Lattner8760ec72005-10-04 01:17:50 +00002582 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002583 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002584 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002585
2586 if (Exists) {
2587 Replacement = I->second;
2588 } else {
2589 // Okay, the new shape doesn't exist in the system yet. Instead of
2590 // creating a new constant struct, inserting it, replaceallusesof'ing the
2591 // old with the new, then deleting the old... just update the current one
2592 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002593 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002594
Chris Lattnerdff59112005-10-04 18:47:09 +00002595 // Update to the new value.
2596 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002597 return;
2598 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002599 }
2600
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002601 assert(Replacement != this && "I didn't contain From!");
2602
Chris Lattner7a1450d2005-10-04 18:13:04 +00002603 // Everyone using this now uses the replacement.
2604 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002605
2606 // Delete the old constant!
2607 destroyConstant();
2608}
2609
Reid Spencerd84d35b2007-02-15 02:26:10 +00002610void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002611 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002612 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2613
2614 std::vector<Constant*> Values;
2615 Values.reserve(getNumOperands()); // Build replacement array...
2616 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2617 Constant *Val = getOperand(i);
2618 if (Val == From) Val = cast<Constant>(To);
2619 Values.push_back(Val);
2620 }
2621
Reid Spencerd84d35b2007-02-15 02:26:10 +00002622 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002623 assert(Replacement != this && "I didn't contain From!");
2624
Chris Lattner7a1450d2005-10-04 18:13:04 +00002625 // Everyone using this now uses the replacement.
2626 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002627
2628 // Delete the old constant!
2629 destroyConstant();
2630}
2631
2632void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002633 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002634 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2635 Constant *To = cast<Constant>(ToV);
2636
2637 Constant *Replacement = 0;
2638 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002639 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002640 Constant *Pointer = getOperand(0);
2641 Indices.reserve(getNumOperands()-1);
2642 if (Pointer == From) Pointer = To;
2643
2644 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2645 Constant *Val = getOperand(i);
2646 if (Val == From) Val = To;
2647 Indices.push_back(Val);
2648 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002649 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2650 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002651 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002652 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002653 if (Agg == From) Agg = To;
2654
Dan Gohman1ecaf452008-05-31 00:58:22 +00002655 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002656 Replacement = ConstantExpr::getExtractValue(Agg,
2657 &Indices[0], Indices.size());
2658 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002659 Constant *Agg = getOperand(0);
2660 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002661 if (Agg == From) Agg = To;
2662 if (Val == From) Val = To;
2663
Dan Gohman1ecaf452008-05-31 00:58:22 +00002664 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002665 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2666 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002667 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002668 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002669 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002670 } else if (getOpcode() == Instruction::Select) {
2671 Constant *C1 = getOperand(0);
2672 Constant *C2 = getOperand(1);
2673 Constant *C3 = getOperand(2);
2674 if (C1 == From) C1 = To;
2675 if (C2 == From) C2 = To;
2676 if (C3 == From) C3 = To;
2677 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002678 } else if (getOpcode() == Instruction::ExtractElement) {
2679 Constant *C1 = getOperand(0);
2680 Constant *C2 = getOperand(1);
2681 if (C1 == From) C1 = To;
2682 if (C2 == From) C2 = To;
2683 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002684 } else if (getOpcode() == Instruction::InsertElement) {
2685 Constant *C1 = getOperand(0);
2686 Constant *C2 = getOperand(1);
2687 Constant *C3 = getOperand(1);
2688 if (C1 == From) C1 = To;
2689 if (C2 == From) C2 = To;
2690 if (C3 == From) C3 = To;
2691 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2692 } else if (getOpcode() == Instruction::ShuffleVector) {
2693 Constant *C1 = getOperand(0);
2694 Constant *C2 = getOperand(1);
2695 Constant *C3 = getOperand(2);
2696 if (C1 == From) C1 = To;
2697 if (C2 == From) C2 = To;
2698 if (C3 == From) C3 = To;
2699 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002700 } else if (isCompare()) {
2701 Constant *C1 = getOperand(0);
2702 Constant *C2 = getOperand(1);
2703 if (C1 == From) C1 = To;
2704 if (C2 == From) C2 = To;
2705 if (getOpcode() == Instruction::ICmp)
2706 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002707 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002708 assert(getOpcode() == Instruction::FCmp);
2709 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002710 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002711 } else if (getNumOperands() == 2) {
2712 Constant *C1 = getOperand(0);
2713 Constant *C2 = getOperand(1);
2714 if (C1 == From) C1 = To;
2715 if (C2 == From) C2 = To;
2716 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2717 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002718 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002719 return;
2720 }
2721
2722 assert(Replacement != this && "I didn't contain From!");
2723
Chris Lattner7a1450d2005-10-04 18:13:04 +00002724 // Everyone using this now uses the replacement.
2725 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002726
2727 // Delete the old constant!
2728 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002729}
Nick Lewycky49f89192009-04-04 07:22:01 +00002730
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002731void MDNode::replaceElement(Value *From, Value *To) {
2732 SmallVector<Value*, 4> Values;
2733 Values.reserve(getNumElements()); // Build replacement array...
2734 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2735 Value *Val = getElement(i);
2736 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002737 Values.push_back(Val);
2738 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002739
2740 MDNode *Replacement = MDNode::get(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002741 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002742
Nick Lewycky49f89192009-04-04 07:22:01 +00002743 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002744
Nick Lewycky49f89192009-04-04 07:22:01 +00002745 destroyConstant();
2746}