blob: 31a82e59f2f0390b561c4a7148c034318011aaf4 [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
Owen Andersonedb4a702009-07-24 23:12:02 +000014#include "LLVMContextImpl.h"
Chris Lattnerca142372002-04-28 19:55:58 +000015#include "llvm/Constants.h"
Chris Lattner33e93b82007-02-27 03:05:06 +000016#include "ConstantFold.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000018#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000020#include "llvm/MDNode.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000021#include "llvm/Module.h"
Dan Gohman7d82e132009-07-18 01:49:22 +000022#include "llvm/Operator.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000023#include "llvm/ADT/FoldingSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/ADT/StringExtras.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000025#include "llvm/ADT/StringMap.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000027#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000029#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000030#include "llvm/Support/MathExtras.h"
Owen Anderson0d2de8c2009-06-20 00:24:58 +000031#include "llvm/System/Mutex.h"
Owen Anderson2d7231d2009-06-17 18:40:29 +000032#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000033#include "llvm/System/Threading.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000034#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000035#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000036#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000037#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000038using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000039
Chris Lattner2f7c9632001-06-06 20:29:01 +000040//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000041// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000042//===----------------------------------------------------------------------===//
43
Owen Andersond830eb82009-06-18 19:10:19 +000044// Becomes a no-op when multithreading is disabled.
45ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson2d7231d2009-06-17 18:40:29 +000046
Chris Lattner3462ae32001-12-03 22:26:30 +000047void Constant::destroyConstantImpl() {
48 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000049 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000050 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000051 // but they don't know that. Because we only find out when the CPV is
52 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000053 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000054 //
55 while (!use_empty()) {
56 Value *V = use_back();
57#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000058 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000059 DOUT << "While deleting: " << *this
60 << "\n\nUse still stuck around after Def is destroyed: "
61 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000062#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000063 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000064 Constant *CV = cast<Constant>(V);
65 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000066
67 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000068 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000069 }
70
71 // Value has no outstanding references it is safe to delete it now...
72 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000073}
Chris Lattner2f7c9632001-06-06 20:29:01 +000074
Chris Lattner23dd1f62006-10-20 00:27:06 +000075/// canTrap - Return true if evaluation of this constant could trap. This is
76/// true for things like constant expressions that could divide by zero.
77bool Constant::canTrap() const {
78 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
79 // The only thing that could possibly trap are constant exprs.
80 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
81 if (!CE) return false;
82
83 // ConstantExpr traps if any operands can trap.
84 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
85 if (getOperand(i)->canTrap())
86 return true;
87
88 // Otherwise, only specific operations can trap.
89 switch (CE->getOpcode()) {
90 default:
91 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000092 case Instruction::UDiv:
93 case Instruction::SDiv:
94 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000095 case Instruction::URem:
96 case Instruction::SRem:
97 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000098 // Div and rem can trap if the RHS is not known to be non-zero.
99 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
100 return true;
101 return false;
102 }
103}
104
Chris Lattner4565ef52009-07-22 00:05:44 +0000105
106/// getRelocationInfo - This method classifies the entry according to
107/// whether or not it may generate a relocation entry. This must be
108/// conservative, so if it might codegen to a relocatable entry, it should say
109/// so. The return values are:
110///
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000111/// NoRelocation: This constant pool entry is guaranteed to never have a
112/// relocation applied to it (because it holds a simple constant like
113/// '4').
114/// LocalRelocation: This entry has relocations, but the entries are
115/// guaranteed to be resolvable by the static linker, so the dynamic
116/// linker will never see them.
117/// GlobalRelocations: This entry may have arbitrary relocations.
Chris Lattner4565ef52009-07-22 00:05:44 +0000118///
119/// FIXME: This really should not be in VMCore.
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000120Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
121 if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Chris Lattner4565ef52009-07-22 00:05:44 +0000122 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000123 return LocalRelocation; // Local to this file/library.
124 return GlobalRelocations; // Global reference.
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000125 }
Chris Lattner4565ef52009-07-22 00:05:44 +0000126
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000127 PossibleRelocationsTy Result = NoRelocation;
Evan Chengf9e003b2007-03-08 00:59:12 +0000128 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner4565ef52009-07-22 00:05:44 +0000129 Result = std::max(Result, getOperand(i)->getRelocationInfo());
130
131 return Result;
Evan Chengf9e003b2007-03-08 00:59:12 +0000132}
133
Chris Lattner4565ef52009-07-22 00:05:44 +0000134
Chris Lattner2105d662008-07-10 00:28:11 +0000135/// getVectorElements - This method, which is only valid on constant of vector
136/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000137/// This handles breaking down a vector undef into undef elements, etc. For
138/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson53a52212009-07-13 04:09:18 +0000139void Constant::getVectorElements(LLVMContext &Context,
140 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner2105d662008-07-10 00:28:11 +0000141 assert(isa<VectorType>(getType()) && "Not a vector constant!");
142
143 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
144 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
145 Elts.push_back(CV->getOperand(i));
146 return;
147 }
148
149 const VectorType *VT = cast<VectorType>(getType());
150 if (isa<ConstantAggregateZero>(this)) {
151 Elts.assign(VT->getNumElements(),
Owen Anderson53a52212009-07-13 04:09:18 +0000152 Context.getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000153 return;
154 }
155
Chris Lattnerc5098a22008-07-14 05:10:41 +0000156 if (isa<UndefValue>(this)) {
Owen Anderson53a52212009-07-13 04:09:18 +0000157 Elts.assign(VT->getNumElements(), Context.getUndef(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000158 return;
159 }
160
161 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000162}
163
164
165
Chris Lattner2f7c9632001-06-06 20:29:01 +0000166//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000167// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168//===----------------------------------------------------------------------===//
169
Reid Spencerb31bffe2007-02-26 23:54:03 +0000170ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000171 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000172 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000173}
174
Owen Andersonedb4a702009-07-24 23:12:02 +0000175// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
176// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
177// operator== and operator!= to ensure that the DenseMap doesn't attempt to
178// compare APInt's of different widths, which would violate an APInt class
179// invariant which generates an assertion.
180ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) {
181 // Get the corresponding integer type for the bit width of the value.
182 const IntegerType *ITy = Context.getIntegerType(V.getBitWidth());
183 // get an existing value or the insertion position
184 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
185
186 Context.pImpl->ConstantsLock.reader_acquire();
187 ConstantInt *&Slot = Context.pImpl->IntConstants[Key];
188 Context.pImpl->ConstantsLock.reader_release();
189
190 if (!Slot) {
191 sys::SmartScopedWriter<true> Writer(Context.pImpl->ConstantsLock);
192 ConstantInt *&NewSlot = Context.pImpl->IntConstants[Key];
193 if (!Slot) {
194 NewSlot = new ConstantInt(ITy, V);
195 }
196
197 return NewSlot;
198 } else {
199 return Slot;
200 }
201}
202
203Constant* ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) {
204 Constant *C = get(cast<IntegerType>(Ty->getScalarType()),
205 V, isSigned);
206
207 // For vectors, broadcast the value.
208 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000209 return ConstantVector::get(
Owen Andersonedb4a702009-07-24 23:12:02 +0000210 std::vector<Constant *>(VTy->getNumElements(), C));
211
212 return C;
213}
214
215ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V,
216 bool isSigned) {
217 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
218}
219
220ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) {
221 return get(Ty, V, true);
222}
223
224Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) {
225 return get(Ty, V, true);
226}
227
228Constant* ConstantInt::get(const Type* Ty, const APInt& V) {
229 ConstantInt *C = get(Ty->getContext(), V);
230 assert(C->getType() == Ty->getScalarType() &&
231 "ConstantInt type doesn't match the type implied by its value!");
232
233 // For vectors, broadcast the value.
234 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000235 return ConstantVector::get(
Owen Andersonedb4a702009-07-24 23:12:02 +0000236 std::vector<Constant *>(VTy->getNumElements(), C));
237
238 return C;
239}
240
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000241//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000242// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000243//===----------------------------------------------------------------------===//
244
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000245static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
246 if (Ty == Type::FloatTy)
247 return &APFloat::IEEEsingle;
248 if (Ty == Type::DoubleTy)
249 return &APFloat::IEEEdouble;
250 if (Ty == Type::X86_FP80Ty)
251 return &APFloat::x87DoubleExtended;
252 else if (Ty == Type::FP128Ty)
253 return &APFloat::IEEEquad;
254
255 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
256 return &APFloat::PPCDoubleDouble;
257}
258
Owen Anderson69c464d2009-07-27 20:59:43 +0000259/// get() - This returns a constant fp for the specified value in the
260/// specified type. This should only be used for simple constant values like
261/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
262Constant* ConstantFP::get(const Type* Ty, double V) {
263 LLVMContext &Context = Ty->getContext();
264
265 APFloat FV(V);
266 bool ignored;
267 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
268 APFloat::rmNearestTiesToEven, &ignored);
269 Constant *C = get(Context, FV);
270
271 // For vectors, broadcast the value.
272 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000273 return ConstantVector::get(
Owen Anderson69c464d2009-07-27 20:59:43 +0000274 std::vector<Constant *>(VTy->getNumElements(), C));
275
276 return C;
277}
278
279ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
280 LLVMContext &Context = Ty->getContext();
281 APFloat apf = cast <ConstantFP>(Context.getNullValue(Ty))->getValueAPF();
282 apf.changeSign();
283 return get(Context, apf);
284}
285
286
287Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) {
288 LLVMContext &Context = Ty->getContext();
289 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
290 if (PTy->getElementType()->isFloatingPoint()) {
291 std::vector<Constant*> zeros(PTy->getNumElements(),
292 getNegativeZero(PTy->getElementType()));
Owen Anderson4aa32952009-07-28 21:19:26 +0000293 return ConstantVector::get(PTy, zeros);
Owen Anderson69c464d2009-07-27 20:59:43 +0000294 }
295
296 if (Ty->isFloatingPoint())
297 return getNegativeZero(Ty);
298
299 return Context.getNullValue(Ty);
300}
301
302
303// ConstantFP accessors.
304ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
305 DenseMapAPFloatKeyInfo::KeyTy Key(V);
306
307 LLVMContextImpl* pImpl = Context.pImpl;
308
309 pImpl->ConstantsLock.reader_acquire();
310 ConstantFP *&Slot = pImpl->FPConstants[Key];
311 pImpl->ConstantsLock.reader_release();
312
313 if (!Slot) {
314 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
315 ConstantFP *&NewSlot = pImpl->FPConstants[Key];
316 if (!NewSlot) {
317 const Type *Ty;
318 if (&V.getSemantics() == &APFloat::IEEEsingle)
319 Ty = Type::FloatTy;
320 else if (&V.getSemantics() == &APFloat::IEEEdouble)
321 Ty = Type::DoubleTy;
322 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
323 Ty = Type::X86_FP80Ty;
324 else if (&V.getSemantics() == &APFloat::IEEEquad)
325 Ty = Type::FP128Ty;
326 else {
327 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
328 "Unknown FP format");
329 Ty = Type::PPC_FP128Ty;
330 }
331 NewSlot = new ConstantFP(Ty, V);
332 }
333
334 return NewSlot;
335 }
336
337 return Slot;
338}
339
Dale Johannesend246b2c2007-08-30 00:23:21 +0000340ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
341 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000342 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
343 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000344}
345
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000346bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000347 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000348}
349
Dale Johannesend246b2c2007-08-30 00:23:21 +0000350bool ConstantFP::isExactlyValue(const APFloat& V) const {
351 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000352}
353
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000354//===----------------------------------------------------------------------===//
355// ConstantXXX Classes
356//===----------------------------------------------------------------------===//
357
358
Chris Lattner3462ae32001-12-03 22:26:30 +0000359ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000360 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000361 : Constant(T, ConstantArrayVal,
362 OperandTraits<ConstantArray>::op_end(this) - V.size(),
363 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000364 assert(V.size() == T->getNumElements() &&
365 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000366 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000367 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
368 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000369 Constant *C = *I;
370 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000371 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000372 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000373 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000374 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000375 }
376}
377
Owen Andersonc2c79322009-07-28 18:32:17 +0000378Constant *ConstantArray::get(const ArrayType *Ty,
379 const std::vector<Constant*> &V) {
380 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
381 // If this is an all-zero array, return a ConstantAggregateZero object
382 if (!V.empty()) {
383 Constant *C = V[0];
384 if (!C->isNullValue()) {
385 // Implicitly locked.
386 return pImpl->ArrayConstants.getOrCreate(Ty, V);
387 }
388 for (unsigned i = 1, e = V.size(); i != e; ++i)
389 if (V[i] != C) {
390 // Implicitly locked.
391 return pImpl->ArrayConstants.getOrCreate(Ty, V);
392 }
393 }
394
395 return Ty->getContext().getConstantAggregateZero(Ty);
396}
397
398
399Constant* ConstantArray::get(const ArrayType* T, Constant* const* Vals,
400 unsigned NumVals) {
401 // FIXME: make this the primary ctor method.
402 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
403}
404
405/// ConstantArray::get(const string&) - Return an array that is initialized to
406/// contain the specified string. If length is zero then a null terminator is
407/// added to the specified string so that it may be used in a natural way.
408/// Otherwise, the length parameter specifies how much of the string to use
409/// and it won't be null terminated.
410///
411Constant* ConstantArray::get(const StringRef &Str, bool AddNull) {
412 std::vector<Constant*> ElementVals;
413 for (unsigned i = 0; i < Str.size(); ++i)
414 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
415
416 // Add a null terminator to the string...
417 if (AddNull) {
418 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
419 }
420
421 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
422 return get(ATy, ElementVals);
423}
424
425
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000426
Chris Lattner3462ae32001-12-03 22:26:30 +0000427ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000428 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000429 : Constant(T, ConstantStructVal,
430 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
431 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000432 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000433 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000434 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000435 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
436 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000437 Constant *C = *I;
438 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000439 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000440 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000441 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000442 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000443 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000444 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000445 }
446}
447
Owen Anderson45308b52009-07-27 22:29:26 +0000448// ConstantStruct accessors.
449Constant* ConstantStruct::get(const StructType* T,
450 const std::vector<Constant*>& V) {
451 LLVMContextImpl* pImpl = T->getContext().pImpl;
452
453 // Create a ConstantAggregateZero value if all elements are zeros...
454 for (unsigned i = 0, e = V.size(); i != e; ++i)
455 if (!V[i]->isNullValue())
456 // Implicitly locked.
457 return pImpl->StructConstants.getOrCreate(T, V);
458
459 return T->getContext().getConstantAggregateZero(T);
460}
461
462Constant* ConstantStruct::get(const std::vector<Constant*>& V, bool packed) {
463 std::vector<const Type*> StructEls;
464 StructEls.reserve(V.size());
465 for (unsigned i = 0, e = V.size(); i != e; ++i)
466 StructEls.push_back(V[i]->getType());
467 return get(StructType::get(StructEls, packed), V);
468}
469
470Constant* ConstantStruct::get(Constant* const *Vals, unsigned NumVals,
471 bool Packed) {
472 // FIXME: make this the primary ctor method.
473 return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
474}
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000475
Reid Spencerd84d35b2007-02-15 02:26:10 +0000476ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000477 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000478 : Constant(T, ConstantVectorVal,
479 OperandTraits<ConstantVector>::op_end(this) - V.size(),
480 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000481 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000482 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
483 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000484 Constant *C = *I;
485 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000486 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000487 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000488 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000489 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000490 }
491}
492
Owen Anderson4aa32952009-07-28 21:19:26 +0000493// ConstantVector accessors.
494Constant* ConstantVector::get(const VectorType* T,
495 const std::vector<Constant*>& V) {
496 assert(!V.empty() && "Vectors can't be empty");
497 LLVMContext &Context = T->getContext();
498 LLVMContextImpl *pImpl = Context.pImpl;
499
500 // If this is an all-undef or alll-zero vector, return a
501 // ConstantAggregateZero or UndefValue.
502 Constant *C = V[0];
503 bool isZero = C->isNullValue();
504 bool isUndef = isa<UndefValue>(C);
505
506 if (isZero || isUndef) {
507 for (unsigned i = 1, e = V.size(); i != e; ++i)
508 if (V[i] != C) {
509 isZero = isUndef = false;
510 break;
511 }
512 }
513
514 if (isZero)
515 return Context.getConstantAggregateZero(T);
516 if (isUndef)
517 return Context.getUndef(T);
518
519 // Implicitly locked.
520 return pImpl->VectorConstants.getOrCreate(T, V);
521}
522
523Constant* ConstantVector::get(const std::vector<Constant*>& V) {
524 assert(!V.empty() && "Cannot infer type if V is empty");
525 return get(VectorType::get(V.front()->getType(),V.size()), V);
526}
527
528Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) {
529 // FIXME: make this the primary ctor method.
530 return get(std::vector<Constant*>(Vals, Vals+NumVals));
531}
532
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000533
Gabor Greiff6caff662008-05-10 08:32:32 +0000534namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000535// We declare several classes private to this file, so use an anonymous
536// namespace
537namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000538
Gordon Henriksen14a55692007-12-10 02:14:30 +0000539/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
540/// behind the scenes to implement unary constant exprs.
541class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000542 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000543public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000544 // allocate space for exactly one operand
545 void *operator new(size_t s) {
546 return User::operator new(s, 1);
547 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000548 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000549 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
550 Op<0>() = C;
551 }
552 /// Transparently provide more efficient getOperand methods.
553 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000554};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000555
Gordon Henriksen14a55692007-12-10 02:14:30 +0000556/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
557/// behind the scenes to implement binary constant exprs.
558class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000559 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000560public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000561 // allocate space for exactly two operands
562 void *operator new(size_t s) {
563 return User::operator new(s, 2);
564 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000565 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000566 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000567 Op<0>() = C1;
568 Op<1>() = C2;
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};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000573
Gordon Henriksen14a55692007-12-10 02:14:30 +0000574/// SelectConstantExpr - This class is private to Constants.cpp, and is used
575/// behind the scenes to implement select constant exprs.
576class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000577 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000578public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000579 // allocate space for exactly three operands
580 void *operator new(size_t s) {
581 return User::operator new(s, 3);
582 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000583 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000584 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000585 Op<0>() = C1;
586 Op<1>() = C2;
587 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000588 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000589 /// Transparently provide more efficient getOperand methods.
590 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000591};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000592
Gordon Henriksen14a55692007-12-10 02:14:30 +0000593/// ExtractElementConstantExpr - This class is private to
594/// Constants.cpp, and is used behind the scenes to implement
595/// extractelement constant exprs.
596class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000597 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000598public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000599 // allocate space for exactly two operands
600 void *operator new(size_t s) {
601 return User::operator new(s, 2);
602 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000603 ExtractElementConstantExpr(Constant *C1, Constant *C2)
604 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000605 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000606 Op<0>() = C1;
607 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000608 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000609 /// Transparently provide more efficient getOperand methods.
610 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000611};
Robert Bocchino23004482006-01-10 19:05:34 +0000612
Gordon Henriksen14a55692007-12-10 02:14:30 +0000613/// InsertElementConstantExpr - This class is private to
614/// Constants.cpp, and is used behind the scenes to implement
615/// insertelement constant exprs.
616class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000617 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000618public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000619 // allocate space for exactly three operands
620 void *operator new(size_t s) {
621 return User::operator new(s, 3);
622 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000623 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
624 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000625 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000626 Op<0>() = C1;
627 Op<1>() = C2;
628 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000629 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000630 /// Transparently provide more efficient getOperand methods.
631 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000632};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000633
Gordon Henriksen14a55692007-12-10 02:14:30 +0000634/// ShuffleVectorConstantExpr - This class is private to
635/// Constants.cpp, and is used behind the scenes to implement
636/// shufflevector constant exprs.
637class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000638 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000639public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000640 // allocate space for exactly three operands
641 void *operator new(size_t s) {
642 return User::operator new(s, 3);
643 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000644 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000645 : ConstantExpr(VectorType::get(
646 cast<VectorType>(C1->getType())->getElementType(),
647 cast<VectorType>(C3->getType())->getNumElements()),
648 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000649 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000650 Op<0>() = C1;
651 Op<1>() = C2;
652 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000653 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000654 /// Transparently provide more efficient getOperand methods.
655 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000656};
657
Dan Gohman12fce772008-05-15 19:50:34 +0000658/// ExtractValueConstantExpr - This class is private to
659/// Constants.cpp, and is used behind the scenes to implement
660/// extractvalue constant exprs.
661class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000662 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000663public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000664 // allocate space for exactly one operand
665 void *operator new(size_t s) {
666 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000667 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000668 ExtractValueConstantExpr(Constant *Agg,
669 const SmallVector<unsigned, 4> &IdxList,
670 const Type *DestTy)
671 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
672 Indices(IdxList) {
673 Op<0>() = Agg;
674 }
675
Dan Gohman7bb04502008-05-31 19:09:08 +0000676 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000677 const SmallVector<unsigned, 4> Indices;
678
Dan Gohman12fce772008-05-15 19:50:34 +0000679 /// Transparently provide more efficient getOperand methods.
680 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
681};
682
683/// InsertValueConstantExpr - This class is private to
684/// Constants.cpp, and is used behind the scenes to implement
685/// insertvalue constant exprs.
686class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000687 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000688public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000689 // allocate space for exactly one operand
690 void *operator new(size_t s) {
691 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000692 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000693 InsertValueConstantExpr(Constant *Agg, Constant *Val,
694 const SmallVector<unsigned, 4> &IdxList,
695 const Type *DestTy)
696 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
697 Indices(IdxList) {
698 Op<0>() = Agg;
699 Op<1>() = Val;
700 }
701
Dan Gohman7bb04502008-05-31 19:09:08 +0000702 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000703 const SmallVector<unsigned, 4> Indices;
704
Dan Gohman12fce772008-05-15 19:50:34 +0000705 /// Transparently provide more efficient getOperand methods.
706 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
707};
708
709
Gordon Henriksen14a55692007-12-10 02:14:30 +0000710/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
711/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000712class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000713 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000714 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000715public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000716 static GetElementPtrConstantExpr *Create(Constant *C,
717 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000718 const Type *DestTy) {
Dan Gohman33a3fd02009-07-20 17:43:30 +0000719 return
720 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000721 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000722 /// Transparently provide more efficient getOperand methods.
723 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000724};
725
726// CompareConstantExpr - This class is private to Constants.cpp, and is used
727// behind the scenes to implement ICmp and FCmp constant expressions. This is
728// needed in order to store the predicate value for these instructions.
729struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000730 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
731 // allocate space for exactly two operands
732 void *operator new(size_t s) {
733 return User::operator new(s, 2);
734 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000735 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000736 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
737 unsigned short pred, Constant* LHS, Constant* RHS)
738 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000739 Op<0>() = LHS;
740 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000741 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000742 /// Transparently provide more efficient getOperand methods.
743 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000744};
745
746} // end anonymous namespace
747
Gabor Greiff6caff662008-05-10 08:32:32 +0000748template <>
749struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
750};
751DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
752
753template <>
754struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
755};
756DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
757
758template <>
759struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
760};
761DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
762
763template <>
764struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
765};
766DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
767
768template <>
769struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
770};
771DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
772
773template <>
774struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
775};
776DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
777
Dan Gohman12fce772008-05-15 19:50:34 +0000778template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000779struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000780};
Dan Gohman12fce772008-05-15 19:50:34 +0000781DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
782
783template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000784struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000785};
Dan Gohman12fce772008-05-15 19:50:34 +0000786DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
787
Gabor Greiff6caff662008-05-10 08:32:32 +0000788template <>
789struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
790};
791
792GetElementPtrConstantExpr::GetElementPtrConstantExpr
793 (Constant *C,
794 const std::vector<Constant*> &IdxList,
795 const Type *DestTy)
796 : ConstantExpr(DestTy, Instruction::GetElementPtr,
797 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
798 - (IdxList.size()+1),
799 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000800 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000801 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000802 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000803}
804
805DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
806
807
808template <>
809struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
810};
811DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
812
813
814} // End llvm namespace
815
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000816
817// Utility function for determining if a ConstantExpr is a CastOp or not. This
818// can't be inline because we don't want to #include Instruction.h into
819// Constant.h
820bool ConstantExpr::isCast() const {
821 return Instruction::isCast(getOpcode());
822}
823
Reid Spenceree3c9912006-12-04 05:19:50 +0000824bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000825 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000826}
827
Dan Gohman1ecaf452008-05-31 00:58:22 +0000828bool ConstantExpr::hasIndices() const {
829 return getOpcode() == Instruction::ExtractValue ||
830 getOpcode() == Instruction::InsertValue;
831}
832
833const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
834 if (const ExtractValueConstantExpr *EVCE =
835 dyn_cast<ExtractValueConstantExpr>(this))
836 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000837
838 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000839}
840
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000841unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000842 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000843 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000844 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000845}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000846
Chris Lattner7c1018a2006-07-14 19:37:40 +0000847/// getWithOperandReplaced - Return a constant expression identical to this
848/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000849Constant *
850ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000851 assert(OpNo < getNumOperands() && "Operand num is out of range!");
852 assert(Op->getType() == getOperand(OpNo)->getType() &&
853 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000854 if (getOperand(OpNo) == Op)
855 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000856
Chris Lattner227816342006-07-14 22:20:01 +0000857 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000858 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000859 case Instruction::Trunc:
860 case Instruction::ZExt:
861 case Instruction::SExt:
862 case Instruction::FPTrunc:
863 case Instruction::FPExt:
864 case Instruction::UIToFP:
865 case Instruction::SIToFP:
866 case Instruction::FPToUI:
867 case Instruction::FPToSI:
868 case Instruction::PtrToInt:
869 case Instruction::IntToPtr:
870 case Instruction::BitCast:
871 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000872 case Instruction::Select:
873 Op0 = (OpNo == 0) ? Op : getOperand(0);
874 Op1 = (OpNo == 1) ? Op : getOperand(1);
875 Op2 = (OpNo == 2) ? Op : getOperand(2);
876 return ConstantExpr::getSelect(Op0, Op1, Op2);
877 case Instruction::InsertElement:
878 Op0 = (OpNo == 0) ? Op : getOperand(0);
879 Op1 = (OpNo == 1) ? Op : getOperand(1);
880 Op2 = (OpNo == 2) ? Op : getOperand(2);
881 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
882 case Instruction::ExtractElement:
883 Op0 = (OpNo == 0) ? Op : getOperand(0);
884 Op1 = (OpNo == 1) ? Op : getOperand(1);
885 return ConstantExpr::getExtractElement(Op0, Op1);
886 case Instruction::ShuffleVector:
887 Op0 = (OpNo == 0) ? Op : getOperand(0);
888 Op1 = (OpNo == 1) ? Op : getOperand(1);
889 Op2 = (OpNo == 2) ? Op : getOperand(2);
890 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000891 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000892 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000893 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000894 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000895 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000896 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000897 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000898 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000899 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000900 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000901 default:
902 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000903 Op0 = (OpNo == 0) ? Op : getOperand(0);
904 Op1 = (OpNo == 1) ? Op : getOperand(1);
905 return ConstantExpr::get(getOpcode(), Op0, Op1);
906 }
907}
908
909/// getWithOperands - This returns the current constant expression with the
910/// operands replaced with the specified values. The specified operands must
911/// match count and type with the existing ones.
912Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000913getWithOperands(Constant* const *Ops, unsigned NumOps) const {
914 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000915 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000916 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000917 assert(Ops[i]->getType() == getOperand(i)->getType() &&
918 "Operand type mismatch!");
919 AnyChange |= Ops[i] != getOperand(i);
920 }
921 if (!AnyChange) // No operands changed, return self.
922 return const_cast<ConstantExpr*>(this);
923
924 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000925 case Instruction::Trunc:
926 case Instruction::ZExt:
927 case Instruction::SExt:
928 case Instruction::FPTrunc:
929 case Instruction::FPExt:
930 case Instruction::UIToFP:
931 case Instruction::SIToFP:
932 case Instruction::FPToUI:
933 case Instruction::FPToSI:
934 case Instruction::PtrToInt:
935 case Instruction::IntToPtr:
936 case Instruction::BitCast:
937 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000938 case Instruction::Select:
939 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
940 case Instruction::InsertElement:
941 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
942 case Instruction::ExtractElement:
943 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
944 case Instruction::ShuffleVector:
945 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000946 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000947 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000948 case Instruction::ICmp:
949 case Instruction::FCmp:
950 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000951 default:
952 assert(getNumOperands() == 2 && "Must be binary operator?");
953 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000954 }
955}
956
Chris Lattner2f7c9632001-06-06 20:29:01 +0000957
958//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000959// isValueValidForType implementations
960
Reid Spencere7334722006-12-19 01:28:19 +0000961bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000962 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000963 if (Ty == Type::Int1Ty)
964 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000965 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000966 return true; // always true, has to fit in largest type
967 uint64_t Max = (1ll << NumBits) - 1;
968 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000969}
970
Reid Spencere0fc4df2006-10-20 07:07:24 +0000971bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000972 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000973 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000974 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000975 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000976 return true; // always true, has to fit in largest type
977 int64_t Min = -(1ll << (NumBits-1));
978 int64_t Max = (1ll << (NumBits-1)) - 1;
979 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000980}
981
Dale Johannesend246b2c2007-08-30 00:23:21 +0000982bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
983 // convert modifies in place, so make a copy.
984 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000985 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000986 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000987 default:
988 return false; // These can't be represented as floating point!
989
Dale Johannesend246b2c2007-08-30 00:23:21 +0000990 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000991 case Type::FloatTyID: {
992 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
993 return true;
994 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
995 return !losesInfo;
996 }
997 case Type::DoubleTyID: {
998 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
999 &Val2.getSemantics() == &APFloat::IEEEdouble)
1000 return true;
1001 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1002 return !losesInfo;
1003 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001004 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001005 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1006 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1007 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +00001008 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001009 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1010 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1011 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +00001012 case Type::PPC_FP128TyID:
1013 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1014 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1015 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001016 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001017}
Chris Lattner9655e542001-07-20 19:16:02 +00001018
Chris Lattner49d855c2001-09-07 16:46:31 +00001019//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001020// Factory Function Implementation
1021
Dan Gohman92b551b2009-03-03 02:55:14 +00001022/// destroyConstant - Remove the constant from the constant table...
1023///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001024void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001025 // Implicitly locked.
Owen Anderson39ede7b2009-07-21 20:13:12 +00001026 getType()->getContext().erase(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001027 destroyConstantImpl();
1028}
1029
Dan Gohman92b551b2009-03-03 02:55:14 +00001030/// destroyConstant - Remove the constant from the constant table...
1031///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001032void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001033 // Implicitly locked.
Owen Andersonc2c79322009-07-28 18:32:17 +00001034 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001035 destroyConstantImpl();
1036}
1037
Reid Spencer2546b762007-01-26 07:37:34 +00001038/// isString - This method returns true if the array is an array of i8, and
1039/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001040bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001041 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001042 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001043 return false;
1044 // Check the elements to make sure they are all integers, not constant
1045 // expressions.
1046 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1047 if (!isa<ConstantInt>(getOperand(i)))
1048 return false;
1049 return true;
1050}
1051
Evan Cheng3763c5b2006-10-26 19:15:05 +00001052/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001053/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001054/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001055bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001056 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001057 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001058 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001059
Evan Chenge974da62006-10-26 21:48:03 +00001060 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001061 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001062 return false;
1063 // Other elements must be non-null integers.
1064 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1065 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001066 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001067 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001068 return false;
1069 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001070 return true;
1071}
1072
1073
Dan Gohman92b551b2009-03-03 02:55:14 +00001074/// getAsString - If the sub-element type of this array is i8
1075/// then this method converts the array to an std::string and returns it.
1076/// Otherwise, it asserts out.
1077///
Chris Lattner81fabb02002-08-26 17:53:56 +00001078std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001079 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001080 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001081 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001082 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001083 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001084 return Result;
1085}
1086
1087
Chris Lattner3462ae32001-12-03 22:26:30 +00001088//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001089//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001090
Chris Lattner189d19f2003-11-21 20:23:48 +00001091namespace llvm {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001092
Chris Lattner49d855c2001-09-07 16:46:31 +00001093}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001094
Chris Lattnerd7a73302001-10-13 06:57:33 +00001095// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001096//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001097void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001098 // Implicitly locked.
Owen Anderson45308b52009-07-27 22:29:26 +00001099 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001100 destroyConstantImpl();
1101}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001102
Brian Gaeke02209042004-08-20 06:00:58 +00001103// destroyConstant - Remove the constant from the constant table...
1104//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001105void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001106 // Implicitly locked.
Owen Anderson4aa32952009-07-28 21:19:26 +00001107 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001108 destroyConstantImpl();
1109}
1110
Dan Gohman30978072007-05-24 14:36:04 +00001111/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001112/// is set to all ones.
1113/// @returns true iff this constant's emements are all set to all ones.
1114/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001115bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001116 // Check out first element.
1117 const Constant *Elt = getOperand(0);
1118 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1119 if (!CI || !CI->isAllOnesValue()) return false;
1120 // Then make sure all remaining elements point to the same value.
1121 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1122 if (getOperand(I) != Elt) return false;
1123 }
1124 return true;
1125}
1126
Dan Gohman07159202007-10-17 17:51:30 +00001127/// getSplatValue - If this is a splat constant, where all of the
1128/// elements have the same value, return that value. Otherwise return null.
1129Constant *ConstantVector::getSplatValue() {
1130 // Check out first element.
1131 Constant *Elt = getOperand(0);
1132 // Then make sure all remaining elements point to the same value.
1133 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1134 if (getOperand(I) != Elt) return 0;
1135 return Elt;
1136}
1137
Chris Lattner3462ae32001-12-03 22:26:30 +00001138//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001139//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001140
Chris Lattner189d19f2003-11-21 20:23:48 +00001141namespace llvm {
1142 // ConstantPointerNull does not take extra "value" argument...
1143 template<class ValType>
1144 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1145 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1146 return new ConstantPointerNull(Ty);
1147 }
1148 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001149
Chris Lattner189d19f2003-11-21 20:23:48 +00001150 template<>
1151 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1152 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1153 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001154 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001155 assert(New != OldC && "Didn't replace constant??");
1156 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001157 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001158 }
1159 };
1160}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001161
Chris Lattner69edc982006-09-28 00:35:06 +00001162static ManagedStatic<ValueMap<char, PointerType,
1163 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001164
Chris Lattner3e650af2004-08-04 04:48:01 +00001165static char getValType(ConstantPointerNull *) {
1166 return 0;
1167}
1168
1169
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001170ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001171 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001172 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001173}
1174
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001175// destroyConstant - Remove the constant from the constant table...
1176//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001177void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001178 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001179 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001180 destroyConstantImpl();
1181}
1182
1183
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001184//---- UndefValue::get() implementation...
1185//
1186
1187namespace llvm {
1188 // UndefValue does not take extra "value" argument...
1189 template<class ValType>
1190 struct ConstantCreator<UndefValue, Type, ValType> {
1191 static UndefValue *create(const Type *Ty, const ValType &V) {
1192 return new UndefValue(Ty);
1193 }
1194 };
1195
1196 template<>
1197 struct ConvertConstantType<UndefValue, Type> {
1198 static void convert(UndefValue *OldC, const Type *NewTy) {
1199 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001200 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001201 assert(New != OldC && "Didn't replace constant??");
1202 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001203 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001204 }
1205 };
1206}
1207
Chris Lattner69edc982006-09-28 00:35:06 +00001208static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001209
1210static char getValType(UndefValue *) {
1211 return 0;
1212}
1213
1214
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001215UndefValue *UndefValue::get(const Type *Ty) {
1216 // Implicitly locked.
1217 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001218}
1219
1220// destroyConstant - Remove the constant from the constant table.
1221//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001222void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001223 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001224 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001225 destroyConstantImpl();
1226}
1227
Nick Lewycky49f89192009-04-04 07:22:01 +00001228//---- MDNode::get() implementation
1229//
1230
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001231MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Devang Patele059ba6e2009-07-23 01:07:34 +00001232 : MetadataBase(Type::MetadataTy, Value::MDNodeVal) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001233 for (unsigned i = 0; i != NumVals; ++i)
Devang Patele059ba6e2009-07-23 01:07:34 +00001234 Node.push_back(WeakVH(Vals[i]));
Nick Lewycky49f89192009-04-04 07:22:01 +00001235}
1236
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001237void MDNode::Profile(FoldingSetNodeID &ID) const {
1238 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001239 ID.AddPointer(*I);
1240}
1241
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001242//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001243//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001244
Dan Gohmand78c4002008-05-13 00:00:25 +00001245namespace {
1246
Reid Spenceree3c9912006-12-04 05:19:50 +00001247struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001248 typedef SmallVector<unsigned, 4> IndexList;
1249
1250 ExprMapKeyType(unsigned opc,
1251 const std::vector<Constant*> &ops,
1252 unsigned short pred = 0,
1253 const IndexList &inds = IndexList())
1254 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001255 uint16_t opcode;
1256 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001257 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001258 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001259 bool operator==(const ExprMapKeyType& that) const {
1260 return this->opcode == that.opcode &&
1261 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001262 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001263 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001264 }
1265 bool operator<(const ExprMapKeyType & that) const {
1266 return this->opcode < that.opcode ||
1267 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1268 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001269 this->operands < that.operands) ||
1270 (this->opcode == that.opcode && this->predicate == that.predicate &&
1271 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001272 }
1273
1274 bool operator!=(const ExprMapKeyType& that) const {
1275 return !(*this == that);
1276 }
1277};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001278
Dan Gohmand78c4002008-05-13 00:00:25 +00001279}
1280
Chris Lattner189d19f2003-11-21 20:23:48 +00001281namespace llvm {
1282 template<>
1283 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001284 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1285 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001286 if (Instruction::isCast(V.opcode))
1287 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1288 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001289 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001290 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1291 if (V.opcode == Instruction::Select)
1292 return new SelectConstantExpr(V.operands[0], V.operands[1],
1293 V.operands[2]);
1294 if (V.opcode == Instruction::ExtractElement)
1295 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1296 if (V.opcode == Instruction::InsertElement)
1297 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1298 V.operands[2]);
1299 if (V.opcode == Instruction::ShuffleVector)
1300 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1301 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001302 if (V.opcode == Instruction::InsertValue)
1303 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1304 V.indices, Ty);
1305 if (V.opcode == Instruction::ExtractValue)
1306 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001307 if (V.opcode == Instruction::GetElementPtr) {
1308 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001309 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001310 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001311
Reid Spenceree3c9912006-12-04 05:19:50 +00001312 // The compare instructions are weird. We have to encode the predicate
1313 // value and it is combined with the instruction opcode by multiplying
1314 // the opcode by one hundred. We must decode this to get the predicate.
1315 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001316 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001317 V.operands[0], V.operands[1]);
1318 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001319 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1320 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001321 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001322 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001323 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001324 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001325
Chris Lattner189d19f2003-11-21 20:23:48 +00001326 template<>
1327 struct ConvertConstantType<ConstantExpr, Type> {
1328 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1329 Constant *New;
1330 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001331 case Instruction::Trunc:
1332 case Instruction::ZExt:
1333 case Instruction::SExt:
1334 case Instruction::FPTrunc:
1335 case Instruction::FPExt:
1336 case Instruction::UIToFP:
1337 case Instruction::SIToFP:
1338 case Instruction::FPToUI:
1339 case Instruction::FPToSI:
1340 case Instruction::PtrToInt:
1341 case Instruction::IntToPtr:
1342 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001343 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001344 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001345 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001346 case Instruction::Select:
1347 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1348 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001349 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001350 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001351 default:
1352 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001353 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001354 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001355 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001356 break;
1357 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001358 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001359 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001360 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001361 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001362 break;
1363 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001364
Chris Lattner189d19f2003-11-21 20:23:48 +00001365 assert(New != OldC && "Didn't replace constant??");
1366 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001367 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001368 }
1369 };
1370} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001371
1372
Chris Lattner3e650af2004-08-04 04:48:01 +00001373static ExprMapKeyType getValType(ConstantExpr *CE) {
1374 std::vector<Constant*> Operands;
1375 Operands.reserve(CE->getNumOperands());
1376 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1377 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001378 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001379 CE->isCompare() ? CE->getPredicate() : 0,
1380 CE->hasIndices() ?
1381 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001382}
1383
Chris Lattner69edc982006-09-28 00:35:06 +00001384static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1385 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001386
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001387/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001388/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001389static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001390 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001391 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001392 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001393 if (Constant *FC =
1394 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001395 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001396
Vikram S. Adve4c485332002-07-15 18:19:33 +00001397 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001398 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001399 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001400
Owen Anderson61794042009-06-17 20:10:08 +00001401 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001402 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001403}
Reid Spencerf37dc652006-12-05 19:14:13 +00001404
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001405Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001406 Instruction::CastOps opc = Instruction::CastOps(oc);
1407 assert(Instruction::isCast(opc) && "opcode out of range");
1408 assert(C && Ty && "Null arguments to getCast");
1409 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1410
1411 switch (opc) {
1412 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001413 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001414 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001415 case Instruction::Trunc: return getTrunc(C, Ty);
1416 case Instruction::ZExt: return getZExt(C, Ty);
1417 case Instruction::SExt: return getSExt(C, Ty);
1418 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1419 case Instruction::FPExt: return getFPExtend(C, Ty);
1420 case Instruction::UIToFP: return getUIToFP(C, Ty);
1421 case Instruction::SIToFP: return getSIToFP(C, Ty);
1422 case Instruction::FPToUI: return getFPToUI(C, Ty);
1423 case Instruction::FPToSI: return getFPToSI(C, Ty);
1424 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1425 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1426 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001427 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001428 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001429}
1430
Reid Spencer5c140882006-12-04 20:17:56 +00001431Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001432 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001433 return getCast(Instruction::BitCast, C, Ty);
1434 return getCast(Instruction::ZExt, C, Ty);
1435}
1436
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001437Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001438 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001439 return getCast(Instruction::BitCast, C, Ty);
1440 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001441}
1442
1443Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001444 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001445 return getCast(Instruction::BitCast, C, Ty);
1446 return getCast(Instruction::Trunc, C, Ty);
1447}
1448
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001449Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001450 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001451 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001452
Chris Lattner03c49532007-01-15 02:27:26 +00001453 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001454 return getCast(Instruction::PtrToInt, S, Ty);
1455 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001456}
1457
Reid Spencer56521c42006-12-12 00:51:07 +00001458Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1459 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001460 assert(C->getType()->isIntOrIntVector() &&
1461 Ty->isIntOrIntVector() && "Invalid cast");
1462 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1463 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001464 Instruction::CastOps opcode =
1465 (SrcBits == DstBits ? Instruction::BitCast :
1466 (SrcBits > DstBits ? Instruction::Trunc :
1467 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1468 return getCast(opcode, C, Ty);
1469}
1470
1471Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001472 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001473 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001474 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1475 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001476 if (SrcBits == DstBits)
1477 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001478 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001479 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001480 return getCast(opcode, C, Ty);
1481}
1482
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001483Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001484#ifndef NDEBUG
1485 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1486 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1487#endif
1488 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1489 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1490 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1491 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001492 "SrcTy must be larger than DestTy for Trunc!");
1493
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001494 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001495}
1496
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001497Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001498#ifndef NDEBUG
1499 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1500 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1501#endif
1502 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1503 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1504 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1505 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001506 "SrcTy must be smaller than DestTy for SExt!");
1507
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001508 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001509}
1510
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001511Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001512#ifndef NDEBUG
1513 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1514 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1515#endif
1516 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1517 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1518 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1519 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001520 "SrcTy must be smaller than DestTy for ZExt!");
1521
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001522 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001523}
1524
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001525Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001526#ifndef NDEBUG
1527 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1528 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1529#endif
1530 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1531 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1532 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001533 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001534 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001535}
1536
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001537Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001538#ifndef NDEBUG
1539 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1540 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1541#endif
1542 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1543 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1544 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001545 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001546 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001547}
1548
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001549Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001550#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001551 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1552 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001553#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001554 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1555 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1556 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001557 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001558}
1559
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001560Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001561#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001562 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1563 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001564#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001565 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1566 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001567 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001568 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001569}
1570
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001571Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001572#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001573 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1574 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001575#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001576 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1577 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1578 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001579 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001580}
1581
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001582Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001583#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001584 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1585 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001586#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001587 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1588 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1589 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001590 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001591}
1592
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001593Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001594 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001595 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001596 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001597}
1598
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001599Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001600 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001601 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001602 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001603}
1604
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001605Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001606 // BitCast implies a no-op cast of type only. No bits change. However, you
1607 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001608#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001609 const Type *SrcTy = C->getType();
1610 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001611 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001612
1613 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1614 // or nonptr->ptr). For all the other types, the cast is okay if source and
1615 // destination bit widths are identical.
1616 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1617 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001618#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001619 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001620
1621 // It is common to ask for a bitcast of a value to its own type, handle this
1622 // speedily.
1623 if (C->getType() == DstTy) return C;
1624
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001625 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001626}
1627
Chris Lattnerb50d1352003-10-05 00:17:43 +00001628Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001629 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001630 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001631 assert(Opcode >= Instruction::BinaryOpsBegin &&
1632 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001633 "Invalid opcode in binary constant expression");
1634 assert(C1->getType() == C2->getType() &&
1635 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001636
Reid Spencer542964f2007-01-11 18:21:29 +00001637 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001638 if (Constant *FC = ConstantFoldBinaryInstruction(
1639 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001640 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001641
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001642 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001643 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001644
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001645 // Implicitly locked.
1646 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001647}
1648
Reid Spencer266e42b2006-12-23 06:05:41 +00001649Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001650 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001651 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001652 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001653 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1654 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1655 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1656 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1657 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1658 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001659 return getFCmp(predicate, C1, C2);
1660
Nate Begemanc96e2e42008-07-25 17:35:37 +00001661 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1662 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1663 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1664 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001665 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001666 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001667}
1668
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001669Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001670 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1671 if (C1->getType()->isFPOrFPVector()) {
1672 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1673 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1674 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1675 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001676#ifndef NDEBUG
1677 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001678 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001679 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001680 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001681 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001682 assert(C1->getType()->isIntOrIntVector() &&
1683 "Tried to create an integer operation on a non-integer type!");
1684 break;
1685 case Instruction::FAdd:
1686 case Instruction::FSub:
1687 case Instruction::FMul:
1688 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1689 assert(C1->getType()->isFPOrFPVector() &&
1690 "Tried to create a floating-point operation on a "
1691 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001692 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001693 case Instruction::UDiv:
1694 case Instruction::SDiv:
1695 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001696 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001697 "Tried to create an arithmetic operation on a non-arithmetic type!");
1698 break;
1699 case Instruction::FDiv:
1700 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001701 assert(C1->getType()->isFPOrFPVector() &&
1702 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001703 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001704 case Instruction::URem:
1705 case Instruction::SRem:
1706 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001707 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001708 "Tried to create an arithmetic operation on a non-arithmetic type!");
1709 break;
1710 case Instruction::FRem:
1711 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001712 assert(C1->getType()->isFPOrFPVector() &&
1713 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001714 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001715 case Instruction::And:
1716 case Instruction::Or:
1717 case Instruction::Xor:
1718 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001719 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001720 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001721 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001722 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001723 case Instruction::LShr:
1724 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001725 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001726 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001727 "Tried to create a shift operation on a non-integer type!");
1728 break;
1729 default:
1730 break;
1731 }
1732#endif
1733
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001734 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001735}
1736
Reid Spencer266e42b2006-12-23 06:05:41 +00001737Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001738 Constant *C1, Constant *C2) {
1739 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001740 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001741}
1742
Chris Lattner6e415c02004-03-12 05:54:04 +00001743Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001744 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001745 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001746
1747 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001748 if (Constant *SC = ConstantFoldSelectInstruction(
1749 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001750 return SC; // Fold common cases
1751
1752 std::vector<Constant*> argVec(3, C);
1753 argVec[1] = V1;
1754 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001755 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001756
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001757 // Implicitly locked.
1758 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001759}
1760
Chris Lattnerb50d1352003-10-05 00:17:43 +00001761Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001762 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001763 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001764 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1765 Idxs+NumIdx) ==
1766 cast<PointerType>(ReqTy)->getElementType() &&
1767 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001768
Owen Anderson53a52212009-07-13 04:09:18 +00001769 if (Constant *FC = ConstantFoldGetElementPtr(
1770 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001771 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001772
Chris Lattnerb50d1352003-10-05 00:17:43 +00001773 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001774 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001775 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001776 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001777 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001778 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001779 for (unsigned i = 0; i != NumIdx; ++i)
1780 ArgVec.push_back(cast<Constant>(Idxs[i]));
1781 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001782
1783 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001784 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001785}
1786
Chris Lattner302116a2007-01-31 04:40:28 +00001787Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001788 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001789 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001790 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00001791 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001792 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001793 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001794 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001795}
1796
Chris Lattner302116a2007-01-31 04:40:28 +00001797Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001798 unsigned NumIdx) {
1799 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001800}
1801
Chris Lattner302116a2007-01-31 04:40:28 +00001802
Reid Spenceree3c9912006-12-04 05:19:50 +00001803Constant *
1804ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1805 assert(LHS->getType() == RHS->getType());
1806 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1807 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1808
Owen Anderson53a52212009-07-13 04:09:18 +00001809 if (Constant *FC = ConstantFoldCompareInstruction(
1810 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001811 return FC; // Fold a few common cases...
1812
1813 // Look up the constant in the table first to ensure uniqueness
1814 std::vector<Constant*> ArgVec;
1815 ArgVec.push_back(LHS);
1816 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001817 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001818 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001819
1820 // Implicitly locked.
1821 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001822}
1823
1824Constant *
1825ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1826 assert(LHS->getType() == RHS->getType());
1827 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1828
Owen Anderson53a52212009-07-13 04:09:18 +00001829 if (Constant *FC = ConstantFoldCompareInstruction(
1830 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001831 return FC; // Fold a few common cases...
1832
1833 // Look up the constant in the table first to ensure uniqueness
1834 std::vector<Constant*> ArgVec;
1835 ArgVec.push_back(LHS);
1836 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001837 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001838 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001839
1840 // Implicitly locked.
1841 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001842}
1843
Robert Bocchino23004482006-01-10 19:05:34 +00001844Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1845 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001846 if (Constant *FC = ConstantFoldExtractElementInstruction(
1847 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001848 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001849 // Look up the constant in the table first to ensure uniqueness
1850 std::vector<Constant*> ArgVec(1, Val);
1851 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001852 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001853
1854 // Implicitly locked.
1855 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001856}
1857
1858Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001859 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001860 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001861 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001862 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001863 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001864 Val, Idx);
1865}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001866
Robert Bocchinoca27f032006-01-17 20:07:22 +00001867Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1868 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001869 if (Constant *FC = ConstantFoldInsertElementInstruction(
1870 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00001871 return FC; // Fold a few common cases...
1872 // Look up the constant in the table first to ensure uniqueness
1873 std::vector<Constant*> ArgVec(1, Val);
1874 ArgVec.push_back(Elt);
1875 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001876 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001877
1878 // Implicitly locked.
1879 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001880}
1881
1882Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1883 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001884 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001885 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001886 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001887 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001888 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001889 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00001890 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001891}
1892
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001893Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1894 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00001895 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
1896 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001897 return FC; // Fold a few common cases...
1898 // Look up the constant in the table first to ensure uniqueness
1899 std::vector<Constant*> ArgVec(1, V1);
1900 ArgVec.push_back(V2);
1901 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001902 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001903
1904 // Implicitly locked.
1905 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001906}
1907
1908Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1909 Constant *Mask) {
1910 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1911 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00001912
1913 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1914 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1915 const Type *ShufTy = VectorType::get(EltTy, NElts);
1916 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001917}
1918
Dan Gohman12fce772008-05-15 19:50:34 +00001919Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1920 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001921 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001922 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1923 Idxs+NumIdx) == Val->getType() &&
1924 "insertvalue indices invalid!");
1925 assert(Agg->getType() == ReqTy &&
1926 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001927 assert(Agg->getType()->isFirstClassType() &&
1928 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00001929 Constant *FC = ConstantFoldInsertValueInstruction(
1930 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001931 assert(FC && "InsertValue constant expr couldn't be folded!");
1932 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001933}
1934
1935Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001936 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001937 assert(Agg->getType()->isFirstClassType() &&
1938 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001939
Dan Gohman0752bff2008-05-23 00:36:11 +00001940 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00001941#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00001942 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00001943 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00001944#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00001945 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00001946 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
1947}
1948
1949Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001950 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001951 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1952 Idxs+NumIdx) == ReqTy &&
1953 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001954 assert(Agg->getType()->isFirstClassType() &&
1955 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00001956 Constant *FC = ConstantFoldExtractValueInstruction(
1957 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001958 assert(FC && "ExtractValue constant expr couldn't be folded!");
1959 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001960}
1961
1962Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001963 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001964 assert(Agg->getType()->isFirstClassType() &&
1965 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001966
1967 const Type *ReqTy =
1968 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
1969 assert(ReqTy && "extractvalue indices invalid!");
1970 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
1971}
1972
Vikram S. Adve4c485332002-07-15 18:19:33 +00001973// destroyConstant - Remove the constant from the constant table...
1974//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001975void ConstantExpr::destroyConstant() {
1976 // Implicitly locked.
1977 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001978 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001979}
1980
Chris Lattner3cd8c562002-07-30 18:54:25 +00001981const char *ConstantExpr::getOpcodeName() const {
1982 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001983}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001984
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001985//===----------------------------------------------------------------------===//
1986// replaceUsesOfWithOnConstant implementations
1987
Chris Lattner913849b2007-08-21 00:55:23 +00001988/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
1989/// 'From' to be uses of 'To'. This must update the uniquing data structures
1990/// etc.
1991///
1992/// Note that we intentionally replace all uses of From with To here. Consider
1993/// a large array that uses 'From' 1000 times. By handling this case all here,
1994/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
1995/// single invocation handles all 1000 uses. Handling them one at a time would
1996/// work, but would be really slow because it would have to unique each updated
1997/// array instance.
Owen Andersonc2c79322009-07-28 18:32:17 +00001998
1999static std::vector<Constant*> getValType(ConstantArray *CA) {
2000 std::vector<Constant*> Elements;
2001 Elements.reserve(CA->getNumOperands());
2002 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
2003 Elements.push_back(cast<Constant>(CA->getOperand(i)));
2004 return Elements;
2005}
2006
2007
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002008void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002009 Use *U) {
Owen Andersonc2c79322009-07-28 18:32:17 +00002010 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2011 Constant *ToC = cast<Constant>(To);
2012
2013 LLVMContext &Context = getType()->getContext();
2014 LLVMContextImpl *pImpl = Context.pImpl;
2015
2016 std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, Constant*> Lookup;
2017 Lookup.first.first = getType();
2018 Lookup.second = this;
2019
2020 std::vector<Constant*> &Values = Lookup.first.second;
2021 Values.reserve(getNumOperands()); // Build replacement array.
2022
2023 // Fill values with the modified operands of the constant array. Also,
2024 // compute whether this turns into an all-zeros array.
2025 bool isAllZeros = false;
2026 unsigned NumUpdated = 0;
2027 if (!ToC->isNullValue()) {
2028 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2029 Constant *Val = cast<Constant>(O->get());
2030 if (Val == From) {
2031 Val = ToC;
2032 ++NumUpdated;
2033 }
2034 Values.push_back(Val);
2035 }
2036 } else {
2037 isAllZeros = true;
2038 for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
2039 Constant *Val = cast<Constant>(O->get());
2040 if (Val == From) {
2041 Val = ToC;
2042 ++NumUpdated;
2043 }
2044 Values.push_back(Val);
2045 if (isAllZeros) isAllZeros = Val->isNullValue();
2046 }
2047 }
2048
2049 Constant *Replacement = 0;
2050 if (isAllZeros) {
2051 Replacement = Context.getConstantAggregateZero(getType());
2052 } else {
2053 // Check to see if we have this array type already.
2054 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
2055 bool Exists;
2056 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
2057 pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
2058
2059 if (Exists) {
2060 Replacement = I->second;
2061 } else {
2062 // Okay, the new shape doesn't exist in the system yet. Instead of
2063 // creating a new constant array, inserting it, replaceallusesof'ing the
2064 // old with the new, then deleting the old... just update the current one
2065 // in place!
2066 pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
2067
2068 // Update to the new value. Optimize for the case when we have a single
2069 // operand that we're changing, but handle bulk updates efficiently.
2070 if (NumUpdated == 1) {
2071 unsigned OperandToUpdate = U - OperandList;
2072 assert(getOperand(OperandToUpdate) == From &&
2073 "ReplaceAllUsesWith broken!");
2074 setOperand(OperandToUpdate, ToC);
2075 } else {
2076 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2077 if (getOperand(i) == From)
2078 setOperand(i, ToC);
2079 }
2080 return;
2081 }
2082 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002083
2084 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002085 assert(Replacement != this && "I didn't contain From!");
2086
Chris Lattner7a1450d2005-10-04 18:13:04 +00002087 // Everyone using this now uses the replacement.
2088 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002089
2090 // Delete the old constant!
2091 destroyConstant();
2092}
2093
Owen Anderson45308b52009-07-27 22:29:26 +00002094static std::vector<Constant*> getValType(ConstantStruct *CS) {
2095 std::vector<Constant*> Elements;
2096 Elements.reserve(CS->getNumOperands());
2097 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
2098 Elements.push_back(cast<Constant>(CS->getOperand(i)));
2099 return Elements;
2100}
2101
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002102void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002103 Use *U) {
Owen Anderson45308b52009-07-27 22:29:26 +00002104 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2105 Constant *ToC = cast<Constant>(To);
2106
2107 unsigned OperandToUpdate = U-OperandList;
2108 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2109
2110 std::pair<LLVMContextImpl::StructConstantsTy::MapKey, Constant*> Lookup;
2111 Lookup.first.first = getType();
2112 Lookup.second = this;
2113 std::vector<Constant*> &Values = Lookup.first.second;
2114 Values.reserve(getNumOperands()); // Build replacement struct.
2115
2116
2117 // Fill values with the modified operands of the constant struct. Also,
2118 // compute whether this turns into an all-zeros struct.
2119 bool isAllZeros = false;
2120 if (!ToC->isNullValue()) {
2121 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
2122 Values.push_back(cast<Constant>(O->get()));
2123 } else {
2124 isAllZeros = true;
2125 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2126 Constant *Val = cast<Constant>(O->get());
2127 Values.push_back(Val);
2128 if (isAllZeros) isAllZeros = Val->isNullValue();
2129 }
2130 }
2131 Values[OperandToUpdate] = ToC;
2132
2133 LLVMContext &Context = getType()->getContext();
2134 LLVMContextImpl *pImpl = Context.pImpl;
2135
2136 Constant *Replacement = 0;
2137 if (isAllZeros) {
2138 Replacement = Context.getConstantAggregateZero(getType());
2139 } else {
2140 // Check to see if we have this array type already.
2141 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
2142 bool Exists;
2143 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2144 pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
2145
2146 if (Exists) {
2147 Replacement = I->second;
2148 } else {
2149 // Okay, the new shape doesn't exist in the system yet. Instead of
2150 // creating a new constant struct, inserting it, replaceallusesof'ing the
2151 // old with the new, then deleting the old... just update the current one
2152 // in place!
2153 pImpl->StructConstants.MoveConstantToNewSlot(this, I);
2154
2155 // Update to the new value.
2156 setOperand(OperandToUpdate, ToC);
2157 return;
2158 }
2159 }
2160
2161 assert(Replacement != this && "I didn't contain From!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002162
Chris Lattner7a1450d2005-10-04 18:13:04 +00002163 // Everyone using this now uses the replacement.
2164 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002165
2166 // Delete the old constant!
2167 destroyConstant();
2168}
2169
Owen Anderson4aa32952009-07-28 21:19:26 +00002170static std::vector<Constant*> getValType(ConstantVector *CP) {
2171 std::vector<Constant*> Elements;
2172 Elements.reserve(CP->getNumOperands());
2173 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
2174 Elements.push_back(CP->getOperand(i));
2175 return Elements;
2176}
2177
Reid Spencerd84d35b2007-02-15 02:26:10 +00002178void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002179 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002180 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2181
2182 std::vector<Constant*> Values;
2183 Values.reserve(getNumOperands()); // Build replacement array...
2184 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2185 Constant *Val = getOperand(i);
2186 if (Val == From) Val = cast<Constant>(To);
2187 Values.push_back(Val);
2188 }
2189
Owen Anderson4aa32952009-07-28 21:19:26 +00002190 Constant *Replacement = get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002191 assert(Replacement != this && "I didn't contain From!");
2192
Chris Lattner7a1450d2005-10-04 18:13:04 +00002193 // Everyone using this now uses the replacement.
2194 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002195
2196 // Delete the old constant!
2197 destroyConstant();
2198}
2199
2200void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002201 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002202 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2203 Constant *To = cast<Constant>(ToV);
2204
2205 Constant *Replacement = 0;
2206 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002207 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002208 Constant *Pointer = getOperand(0);
2209 Indices.reserve(getNumOperands()-1);
2210 if (Pointer == From) Pointer = To;
2211
2212 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2213 Constant *Val = getOperand(i);
2214 if (Val == From) Val = To;
2215 Indices.push_back(Val);
2216 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002217 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2218 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002219 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002220 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002221 if (Agg == From) Agg = To;
2222
Dan Gohman1ecaf452008-05-31 00:58:22 +00002223 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002224 Replacement = ConstantExpr::getExtractValue(Agg,
2225 &Indices[0], Indices.size());
2226 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002227 Constant *Agg = getOperand(0);
2228 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002229 if (Agg == From) Agg = To;
2230 if (Val == From) Val = To;
2231
Dan Gohman1ecaf452008-05-31 00:58:22 +00002232 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002233 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2234 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002235 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002236 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002238 } else if (getOpcode() == Instruction::Select) {
2239 Constant *C1 = getOperand(0);
2240 Constant *C2 = getOperand(1);
2241 Constant *C3 = getOperand(2);
2242 if (C1 == From) C1 = To;
2243 if (C2 == From) C2 = To;
2244 if (C3 == From) C3 = To;
2245 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002246 } else if (getOpcode() == Instruction::ExtractElement) {
2247 Constant *C1 = getOperand(0);
2248 Constant *C2 = getOperand(1);
2249 if (C1 == From) C1 = To;
2250 if (C2 == From) C2 = To;
2251 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002252 } else if (getOpcode() == Instruction::InsertElement) {
2253 Constant *C1 = getOperand(0);
2254 Constant *C2 = getOperand(1);
2255 Constant *C3 = getOperand(1);
2256 if (C1 == From) C1 = To;
2257 if (C2 == From) C2 = To;
2258 if (C3 == From) C3 = To;
2259 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2260 } else if (getOpcode() == Instruction::ShuffleVector) {
2261 Constant *C1 = getOperand(0);
2262 Constant *C2 = getOperand(1);
2263 Constant *C3 = getOperand(2);
2264 if (C1 == From) C1 = To;
2265 if (C2 == From) C2 = To;
2266 if (C3 == From) C3 = To;
2267 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002268 } else if (isCompare()) {
2269 Constant *C1 = getOperand(0);
2270 Constant *C2 = getOperand(1);
2271 if (C1 == From) C1 = To;
2272 if (C2 == From) C2 = To;
2273 if (getOpcode() == Instruction::ICmp)
2274 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002275 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002276 assert(getOpcode() == Instruction::FCmp);
2277 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002278 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002279 } else if (getNumOperands() == 2) {
2280 Constant *C1 = getOperand(0);
2281 Constant *C2 = getOperand(1);
2282 if (C1 == From) C1 = To;
2283 if (C2 == From) C2 = To;
2284 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2285 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002286 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002287 return;
2288 }
2289
2290 assert(Replacement != this && "I didn't contain From!");
2291
Chris Lattner7a1450d2005-10-04 18:13:04 +00002292 // Everyone using this now uses the replacement.
2293 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002294
2295 // Delete the old constant!
2296 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002297}
Nick Lewycky49f89192009-04-04 07:22:01 +00002298
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002299void MDNode::replaceElement(Value *From, Value *To) {
2300 SmallVector<Value*, 4> Values;
2301 Values.reserve(getNumElements()); // Build replacement array...
2302 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2303 Value *Val = getElement(i);
2304 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002305 Values.push_back(Val);
2306 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002307
Owen Anderson4118dde2009-07-16 23:44:30 +00002308 MDNode *Replacement =
2309 getType()->getContext().getMDNode(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002310 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002311
Nick Lewycky49f89192009-04-04 07:22:01 +00002312 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewycky49f89192009-04-04 07:22:01 +00002313}