blob: ec1bf13e5d268203f8684c8efbdaaa69063ba7fd [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))
209 return Ty->getContext().getConstantVector(
210 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))
235 return Ty->getContext().getConstantVector(
236 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))
273 return Context.getConstantVector(
274 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()));
293 return Context.getConstantVector(PTy, zeros);
294 }
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
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000493
Gabor Greiff6caff662008-05-10 08:32:32 +0000494namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000495// We declare several classes private to this file, so use an anonymous
496// namespace
497namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000498
Gordon Henriksen14a55692007-12-10 02:14:30 +0000499/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
500/// behind the scenes to implement unary constant exprs.
501class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000502 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000503public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000504 // allocate space for exactly one operand
505 void *operator new(size_t s) {
506 return User::operator new(s, 1);
507 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000508 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000509 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
510 Op<0>() = C;
511 }
512 /// Transparently provide more efficient getOperand methods.
513 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000514};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000515
Gordon Henriksen14a55692007-12-10 02:14:30 +0000516/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
517/// behind the scenes to implement binary constant exprs.
518class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000519 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000520public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000521 // allocate space for exactly two operands
522 void *operator new(size_t s) {
523 return User::operator new(s, 2);
524 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000525 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000526 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000527 Op<0>() = C1;
528 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000529 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000530 /// Transparently provide more efficient getOperand methods.
531 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000532};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000533
Gordon Henriksen14a55692007-12-10 02:14:30 +0000534/// SelectConstantExpr - This class is private to Constants.cpp, and is used
535/// behind the scenes to implement select constant exprs.
536class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000537 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000538public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000539 // allocate space for exactly three operands
540 void *operator new(size_t s) {
541 return User::operator new(s, 3);
542 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000543 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000544 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000545 Op<0>() = C1;
546 Op<1>() = C2;
547 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000548 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000549 /// Transparently provide more efficient getOperand methods.
550 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000551};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000552
Gordon Henriksen14a55692007-12-10 02:14:30 +0000553/// ExtractElementConstantExpr - This class is private to
554/// Constants.cpp, and is used behind the scenes to implement
555/// extractelement constant exprs.
556class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000557 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000558public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000559 // allocate space for exactly two operands
560 void *operator new(size_t s) {
561 return User::operator new(s, 2);
562 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000563 ExtractElementConstantExpr(Constant *C1, Constant *C2)
564 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000565 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000566 Op<0>() = C1;
567 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000568 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000569 /// Transparently provide more efficient getOperand methods.
570 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000571};
Robert Bocchino23004482006-01-10 19:05:34 +0000572
Gordon Henriksen14a55692007-12-10 02:14:30 +0000573/// InsertElementConstantExpr - This class is private to
574/// Constants.cpp, and is used behind the scenes to implement
575/// insertelement constant exprs.
576class VISIBILITY_HIDDEN InsertElementConstantExpr : 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 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
584 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000585 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000586 Op<0>() = C1;
587 Op<1>() = C2;
588 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000589 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000590 /// Transparently provide more efficient getOperand methods.
591 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000592};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000593
Gordon Henriksen14a55692007-12-10 02:14:30 +0000594/// ShuffleVectorConstantExpr - This class is private to
595/// Constants.cpp, and is used behind the scenes to implement
596/// shufflevector constant exprs.
597class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000598 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000599public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000600 // allocate space for exactly three operands
601 void *operator new(size_t s) {
602 return User::operator new(s, 3);
603 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000604 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000605 : ConstantExpr(VectorType::get(
606 cast<VectorType>(C1->getType())->getElementType(),
607 cast<VectorType>(C3->getType())->getNumElements()),
608 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000609 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000610 Op<0>() = C1;
611 Op<1>() = C2;
612 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000613 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000614 /// Transparently provide more efficient getOperand methods.
615 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000616};
617
Dan Gohman12fce772008-05-15 19:50:34 +0000618/// ExtractValueConstantExpr - This class is private to
619/// Constants.cpp, and is used behind the scenes to implement
620/// extractvalue constant exprs.
621class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000622 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000623public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000624 // allocate space for exactly one operand
625 void *operator new(size_t s) {
626 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000627 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000628 ExtractValueConstantExpr(Constant *Agg,
629 const SmallVector<unsigned, 4> &IdxList,
630 const Type *DestTy)
631 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
632 Indices(IdxList) {
633 Op<0>() = Agg;
634 }
635
Dan Gohman7bb04502008-05-31 19:09:08 +0000636 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000637 const SmallVector<unsigned, 4> Indices;
638
Dan Gohman12fce772008-05-15 19:50:34 +0000639 /// Transparently provide more efficient getOperand methods.
640 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
641};
642
643/// InsertValueConstantExpr - This class is private to
644/// Constants.cpp, and is used behind the scenes to implement
645/// insertvalue constant exprs.
646class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000647 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000648public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000649 // allocate space for exactly one operand
650 void *operator new(size_t s) {
651 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000652 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000653 InsertValueConstantExpr(Constant *Agg, Constant *Val,
654 const SmallVector<unsigned, 4> &IdxList,
655 const Type *DestTy)
656 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
657 Indices(IdxList) {
658 Op<0>() = Agg;
659 Op<1>() = Val;
660 }
661
Dan Gohman7bb04502008-05-31 19:09:08 +0000662 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000663 const SmallVector<unsigned, 4> Indices;
664
Dan Gohman12fce772008-05-15 19:50:34 +0000665 /// Transparently provide more efficient getOperand methods.
666 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
667};
668
669
Gordon Henriksen14a55692007-12-10 02:14:30 +0000670/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
671/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000672class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000673 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000674 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000675public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000676 static GetElementPtrConstantExpr *Create(Constant *C,
677 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000678 const Type *DestTy) {
Dan Gohman33a3fd02009-07-20 17:43:30 +0000679 return
680 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000681 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000682 /// Transparently provide more efficient getOperand methods.
683 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000684};
685
686// CompareConstantExpr - This class is private to Constants.cpp, and is used
687// behind the scenes to implement ICmp and FCmp constant expressions. This is
688// needed in order to store the predicate value for these instructions.
689struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000690 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
691 // allocate space for exactly two operands
692 void *operator new(size_t s) {
693 return User::operator new(s, 2);
694 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000695 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000696 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
697 unsigned short pred, Constant* LHS, Constant* RHS)
698 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000699 Op<0>() = LHS;
700 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000701 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000702 /// Transparently provide more efficient getOperand methods.
703 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000704};
705
706} // end anonymous namespace
707
Gabor Greiff6caff662008-05-10 08:32:32 +0000708template <>
709struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
710};
711DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
712
713template <>
714struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
715};
716DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
717
718template <>
719struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
720};
721DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
722
723template <>
724struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
725};
726DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
727
728template <>
729struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
730};
731DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
732
733template <>
734struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
735};
736DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
737
Dan Gohman12fce772008-05-15 19:50:34 +0000738template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000739struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000740};
Dan Gohman12fce772008-05-15 19:50:34 +0000741DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
742
743template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000744struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000745};
Dan Gohman12fce772008-05-15 19:50:34 +0000746DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
747
Gabor Greiff6caff662008-05-10 08:32:32 +0000748template <>
749struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
750};
751
752GetElementPtrConstantExpr::GetElementPtrConstantExpr
753 (Constant *C,
754 const std::vector<Constant*> &IdxList,
755 const Type *DestTy)
756 : ConstantExpr(DestTy, Instruction::GetElementPtr,
757 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
758 - (IdxList.size()+1),
759 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000760 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000761 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000762 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000763}
764
765DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
766
767
768template <>
769struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
770};
771DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
772
773
774} // End llvm namespace
775
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000776
777// Utility function for determining if a ConstantExpr is a CastOp or not. This
778// can't be inline because we don't want to #include Instruction.h into
779// Constant.h
780bool ConstantExpr::isCast() const {
781 return Instruction::isCast(getOpcode());
782}
783
Reid Spenceree3c9912006-12-04 05:19:50 +0000784bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000785 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000786}
787
Dan Gohman1ecaf452008-05-31 00:58:22 +0000788bool ConstantExpr::hasIndices() const {
789 return getOpcode() == Instruction::ExtractValue ||
790 getOpcode() == Instruction::InsertValue;
791}
792
793const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
794 if (const ExtractValueConstantExpr *EVCE =
795 dyn_cast<ExtractValueConstantExpr>(this))
796 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000797
798 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000799}
800
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000801unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000802 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000803 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000804 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000805}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000806
Chris Lattner7c1018a2006-07-14 19:37:40 +0000807/// getWithOperandReplaced - Return a constant expression identical to this
808/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000809Constant *
810ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000811 assert(OpNo < getNumOperands() && "Operand num is out of range!");
812 assert(Op->getType() == getOperand(OpNo)->getType() &&
813 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000814 if (getOperand(OpNo) == Op)
815 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000816
Chris Lattner227816342006-07-14 22:20:01 +0000817 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000818 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000819 case Instruction::Trunc:
820 case Instruction::ZExt:
821 case Instruction::SExt:
822 case Instruction::FPTrunc:
823 case Instruction::FPExt:
824 case Instruction::UIToFP:
825 case Instruction::SIToFP:
826 case Instruction::FPToUI:
827 case Instruction::FPToSI:
828 case Instruction::PtrToInt:
829 case Instruction::IntToPtr:
830 case Instruction::BitCast:
831 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000832 case Instruction::Select:
833 Op0 = (OpNo == 0) ? Op : getOperand(0);
834 Op1 = (OpNo == 1) ? Op : getOperand(1);
835 Op2 = (OpNo == 2) ? Op : getOperand(2);
836 return ConstantExpr::getSelect(Op0, Op1, Op2);
837 case Instruction::InsertElement:
838 Op0 = (OpNo == 0) ? Op : getOperand(0);
839 Op1 = (OpNo == 1) ? Op : getOperand(1);
840 Op2 = (OpNo == 2) ? Op : getOperand(2);
841 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
842 case Instruction::ExtractElement:
843 Op0 = (OpNo == 0) ? Op : getOperand(0);
844 Op1 = (OpNo == 1) ? Op : getOperand(1);
845 return ConstantExpr::getExtractElement(Op0, Op1);
846 case Instruction::ShuffleVector:
847 Op0 = (OpNo == 0) ? Op : getOperand(0);
848 Op1 = (OpNo == 1) ? Op : getOperand(1);
849 Op2 = (OpNo == 2) ? Op : getOperand(2);
850 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000851 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000852 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000853 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000854 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000855 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000856 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000857 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000858 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000859 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000860 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000861 default:
862 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000863 Op0 = (OpNo == 0) ? Op : getOperand(0);
864 Op1 = (OpNo == 1) ? Op : getOperand(1);
865 return ConstantExpr::get(getOpcode(), Op0, Op1);
866 }
867}
868
869/// getWithOperands - This returns the current constant expression with the
870/// operands replaced with the specified values. The specified operands must
871/// match count and type with the existing ones.
872Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000873getWithOperands(Constant* const *Ops, unsigned NumOps) const {
874 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000875 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000876 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000877 assert(Ops[i]->getType() == getOperand(i)->getType() &&
878 "Operand type mismatch!");
879 AnyChange |= Ops[i] != getOperand(i);
880 }
881 if (!AnyChange) // No operands changed, return self.
882 return const_cast<ConstantExpr*>(this);
883
884 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000885 case Instruction::Trunc:
886 case Instruction::ZExt:
887 case Instruction::SExt:
888 case Instruction::FPTrunc:
889 case Instruction::FPExt:
890 case Instruction::UIToFP:
891 case Instruction::SIToFP:
892 case Instruction::FPToUI:
893 case Instruction::FPToSI:
894 case Instruction::PtrToInt:
895 case Instruction::IntToPtr:
896 case Instruction::BitCast:
897 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000898 case Instruction::Select:
899 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
900 case Instruction::InsertElement:
901 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
902 case Instruction::ExtractElement:
903 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
904 case Instruction::ShuffleVector:
905 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000906 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000907 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000908 case Instruction::ICmp:
909 case Instruction::FCmp:
910 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000911 default:
912 assert(getNumOperands() == 2 && "Must be binary operator?");
913 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000914 }
915}
916
Chris Lattner2f7c9632001-06-06 20:29:01 +0000917
918//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000919// isValueValidForType implementations
920
Reid Spencere7334722006-12-19 01:28:19 +0000921bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000922 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000923 if (Ty == Type::Int1Ty)
924 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000925 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000926 return true; // always true, has to fit in largest type
927 uint64_t Max = (1ll << NumBits) - 1;
928 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000929}
930
Reid Spencere0fc4df2006-10-20 07:07:24 +0000931bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000932 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000933 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000934 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000935 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000936 return true; // always true, has to fit in largest type
937 int64_t Min = -(1ll << (NumBits-1));
938 int64_t Max = (1ll << (NumBits-1)) - 1;
939 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000940}
941
Dale Johannesend246b2c2007-08-30 00:23:21 +0000942bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
943 // convert modifies in place, so make a copy.
944 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000945 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000946 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000947 default:
948 return false; // These can't be represented as floating point!
949
Dale Johannesend246b2c2007-08-30 00:23:21 +0000950 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000951 case Type::FloatTyID: {
952 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
953 return true;
954 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
955 return !losesInfo;
956 }
957 case Type::DoubleTyID: {
958 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
959 &Val2.getSemantics() == &APFloat::IEEEdouble)
960 return true;
961 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
962 return !losesInfo;
963 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000964 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000965 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
966 &Val2.getSemantics() == &APFloat::IEEEdouble ||
967 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000968 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000969 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
970 &Val2.getSemantics() == &APFloat::IEEEdouble ||
971 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000972 case Type::PPC_FP128TyID:
973 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
974 &Val2.getSemantics() == &APFloat::IEEEdouble ||
975 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000976 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000977}
Chris Lattner9655e542001-07-20 19:16:02 +0000978
Chris Lattner49d855c2001-09-07 16:46:31 +0000979//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000980// Factory Function Implementation
981
Dan Gohman92b551b2009-03-03 02:55:14 +0000982/// destroyConstant - Remove the constant from the constant table...
983///
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000984void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +0000985 // Implicitly locked.
Owen Anderson39ede7b2009-07-21 20:13:12 +0000986 getType()->getContext().erase(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000987 destroyConstantImpl();
988}
989
Dan Gohman92b551b2009-03-03 02:55:14 +0000990/// destroyConstant - Remove the constant from the constant table...
991///
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000992void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +0000993 // Implicitly locked.
Owen Andersonc2c79322009-07-28 18:32:17 +0000994 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000995 destroyConstantImpl();
996}
997
Reid Spencer2546b762007-01-26 07:37:34 +0000998/// isString - This method returns true if the array is an array of i8, and
999/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001000bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001001 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001002 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001003 return false;
1004 // Check the elements to make sure they are all integers, not constant
1005 // expressions.
1006 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1007 if (!isa<ConstantInt>(getOperand(i)))
1008 return false;
1009 return true;
1010}
1011
Evan Cheng3763c5b2006-10-26 19:15:05 +00001012/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001013/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001014/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001015bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001016 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001017 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001018 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001019
Evan Chenge974da62006-10-26 21:48:03 +00001020 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001021 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001022 return false;
1023 // Other elements must be non-null integers.
1024 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1025 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001026 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001027 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001028 return false;
1029 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001030 return true;
1031}
1032
1033
Dan Gohman92b551b2009-03-03 02:55:14 +00001034/// getAsString - If the sub-element type of this array is i8
1035/// then this method converts the array to an std::string and returns it.
1036/// Otherwise, it asserts out.
1037///
Chris Lattner81fabb02002-08-26 17:53:56 +00001038std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001039 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001040 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001041 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001042 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001043 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001044 return Result;
1045}
1046
1047
Chris Lattner3462ae32001-12-03 22:26:30 +00001048//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001049//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001050
Chris Lattner189d19f2003-11-21 20:23:48 +00001051namespace llvm {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001052
Chris Lattner49d855c2001-09-07 16:46:31 +00001053}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001054
Chris Lattnerd7a73302001-10-13 06:57:33 +00001055// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001056//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001057void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001058 // Implicitly locked.
Owen Anderson45308b52009-07-27 22:29:26 +00001059 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001060 destroyConstantImpl();
1061}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001062
Brian Gaeke02209042004-08-20 06:00:58 +00001063// destroyConstant - Remove the constant from the constant table...
1064//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001065void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001066 // Implicitly locked.
Owen Anderson0348a132009-07-24 00:36:24 +00001067 getType()->getContext().erase(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001068 destroyConstantImpl();
1069}
1070
Dan Gohman30978072007-05-24 14:36:04 +00001071/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001072/// is set to all ones.
1073/// @returns true iff this constant's emements are all set to all ones.
1074/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001075bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001076 // Check out first element.
1077 const Constant *Elt = getOperand(0);
1078 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1079 if (!CI || !CI->isAllOnesValue()) return false;
1080 // Then make sure all remaining elements point to the same value.
1081 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1082 if (getOperand(I) != Elt) return false;
1083 }
1084 return true;
1085}
1086
Dan Gohman07159202007-10-17 17:51:30 +00001087/// getSplatValue - If this is a splat constant, where all of the
1088/// elements have the same value, return that value. Otherwise return null.
1089Constant *ConstantVector::getSplatValue() {
1090 // Check out first element.
1091 Constant *Elt = getOperand(0);
1092 // Then make sure all remaining elements point to the same value.
1093 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1094 if (getOperand(I) != Elt) return 0;
1095 return Elt;
1096}
1097
Chris Lattner3462ae32001-12-03 22:26:30 +00001098//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001099//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001100
Chris Lattner189d19f2003-11-21 20:23:48 +00001101namespace llvm {
1102 // ConstantPointerNull does not take extra "value" argument...
1103 template<class ValType>
1104 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1105 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1106 return new ConstantPointerNull(Ty);
1107 }
1108 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001109
Chris Lattner189d19f2003-11-21 20:23:48 +00001110 template<>
1111 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1112 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1113 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001114 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001115 assert(New != OldC && "Didn't replace constant??");
1116 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001117 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001118 }
1119 };
1120}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001121
Chris Lattner69edc982006-09-28 00:35:06 +00001122static ManagedStatic<ValueMap<char, PointerType,
1123 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001124
Chris Lattner3e650af2004-08-04 04:48:01 +00001125static char getValType(ConstantPointerNull *) {
1126 return 0;
1127}
1128
1129
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001130ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001131 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001132 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001133}
1134
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001135// destroyConstant - Remove the constant from the constant table...
1136//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001137void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001138 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001139 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001140 destroyConstantImpl();
1141}
1142
1143
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001144//---- UndefValue::get() implementation...
1145//
1146
1147namespace llvm {
1148 // UndefValue does not take extra "value" argument...
1149 template<class ValType>
1150 struct ConstantCreator<UndefValue, Type, ValType> {
1151 static UndefValue *create(const Type *Ty, const ValType &V) {
1152 return new UndefValue(Ty);
1153 }
1154 };
1155
1156 template<>
1157 struct ConvertConstantType<UndefValue, Type> {
1158 static void convert(UndefValue *OldC, const Type *NewTy) {
1159 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001160 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001161 assert(New != OldC && "Didn't replace constant??");
1162 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001163 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001164 }
1165 };
1166}
1167
Chris Lattner69edc982006-09-28 00:35:06 +00001168static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001169
1170static char getValType(UndefValue *) {
1171 return 0;
1172}
1173
1174
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001175UndefValue *UndefValue::get(const Type *Ty) {
1176 // Implicitly locked.
1177 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001178}
1179
1180// destroyConstant - Remove the constant from the constant table.
1181//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001182void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001183 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001184 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001185 destroyConstantImpl();
1186}
1187
Nick Lewycky49f89192009-04-04 07:22:01 +00001188//---- MDNode::get() implementation
1189//
1190
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001191MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Devang Patele059ba6e2009-07-23 01:07:34 +00001192 : MetadataBase(Type::MetadataTy, Value::MDNodeVal) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001193 for (unsigned i = 0; i != NumVals; ++i)
Devang Patele059ba6e2009-07-23 01:07:34 +00001194 Node.push_back(WeakVH(Vals[i]));
Nick Lewycky49f89192009-04-04 07:22:01 +00001195}
1196
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001197void MDNode::Profile(FoldingSetNodeID &ID) const {
1198 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001199 ID.AddPointer(*I);
1200}
1201
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001202//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001203//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001204
Dan Gohmand78c4002008-05-13 00:00:25 +00001205namespace {
1206
Reid Spenceree3c9912006-12-04 05:19:50 +00001207struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001208 typedef SmallVector<unsigned, 4> IndexList;
1209
1210 ExprMapKeyType(unsigned opc,
1211 const std::vector<Constant*> &ops,
1212 unsigned short pred = 0,
1213 const IndexList &inds = IndexList())
1214 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001215 uint16_t opcode;
1216 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001217 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001218 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001219 bool operator==(const ExprMapKeyType& that) const {
1220 return this->opcode == that.opcode &&
1221 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001222 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001223 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001224 }
1225 bool operator<(const ExprMapKeyType & that) const {
1226 return this->opcode < that.opcode ||
1227 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1228 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001229 this->operands < that.operands) ||
1230 (this->opcode == that.opcode && this->predicate == that.predicate &&
1231 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001232 }
1233
1234 bool operator!=(const ExprMapKeyType& that) const {
1235 return !(*this == that);
1236 }
1237};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001238
Dan Gohmand78c4002008-05-13 00:00:25 +00001239}
1240
Chris Lattner189d19f2003-11-21 20:23:48 +00001241namespace llvm {
1242 template<>
1243 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001244 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1245 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001246 if (Instruction::isCast(V.opcode))
1247 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1248 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001249 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001250 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1251 if (V.opcode == Instruction::Select)
1252 return new SelectConstantExpr(V.operands[0], V.operands[1],
1253 V.operands[2]);
1254 if (V.opcode == Instruction::ExtractElement)
1255 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1256 if (V.opcode == Instruction::InsertElement)
1257 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1258 V.operands[2]);
1259 if (V.opcode == Instruction::ShuffleVector)
1260 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1261 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001262 if (V.opcode == Instruction::InsertValue)
1263 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1264 V.indices, Ty);
1265 if (V.opcode == Instruction::ExtractValue)
1266 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001267 if (V.opcode == Instruction::GetElementPtr) {
1268 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001269 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001270 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001271
Reid Spenceree3c9912006-12-04 05:19:50 +00001272 // The compare instructions are weird. We have to encode the predicate
1273 // value and it is combined with the instruction opcode by multiplying
1274 // the opcode by one hundred. We must decode this to get the predicate.
1275 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001276 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001277 V.operands[0], V.operands[1]);
1278 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001279 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1280 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001281 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001282 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001283 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001284 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001285
Chris Lattner189d19f2003-11-21 20:23:48 +00001286 template<>
1287 struct ConvertConstantType<ConstantExpr, Type> {
1288 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1289 Constant *New;
1290 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001291 case Instruction::Trunc:
1292 case Instruction::ZExt:
1293 case Instruction::SExt:
1294 case Instruction::FPTrunc:
1295 case Instruction::FPExt:
1296 case Instruction::UIToFP:
1297 case Instruction::SIToFP:
1298 case Instruction::FPToUI:
1299 case Instruction::FPToSI:
1300 case Instruction::PtrToInt:
1301 case Instruction::IntToPtr:
1302 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001303 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001304 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001305 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001306 case Instruction::Select:
1307 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1308 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001309 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001310 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001311 default:
1312 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001313 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001314 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001315 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001316 break;
1317 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001318 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001319 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001320 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001321 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001322 break;
1323 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001324
Chris Lattner189d19f2003-11-21 20:23:48 +00001325 assert(New != OldC && "Didn't replace constant??");
1326 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001327 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001328 }
1329 };
1330} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001331
1332
Chris Lattner3e650af2004-08-04 04:48:01 +00001333static ExprMapKeyType getValType(ConstantExpr *CE) {
1334 std::vector<Constant*> Operands;
1335 Operands.reserve(CE->getNumOperands());
1336 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1337 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001338 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001339 CE->isCompare() ? CE->getPredicate() : 0,
1340 CE->hasIndices() ?
1341 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001342}
1343
Chris Lattner69edc982006-09-28 00:35:06 +00001344static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1345 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001346
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001347/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001348/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001349static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001350 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001351 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001352 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001353 if (Constant *FC =
1354 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001355 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001356
Vikram S. Adve4c485332002-07-15 18:19:33 +00001357 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001358 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001359 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001360
Owen Anderson61794042009-06-17 20:10:08 +00001361 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001362 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001363}
Reid Spencerf37dc652006-12-05 19:14:13 +00001364
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001365Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001366 Instruction::CastOps opc = Instruction::CastOps(oc);
1367 assert(Instruction::isCast(opc) && "opcode out of range");
1368 assert(C && Ty && "Null arguments to getCast");
1369 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1370
1371 switch (opc) {
1372 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001373 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001374 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001375 case Instruction::Trunc: return getTrunc(C, Ty);
1376 case Instruction::ZExt: return getZExt(C, Ty);
1377 case Instruction::SExt: return getSExt(C, Ty);
1378 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1379 case Instruction::FPExt: return getFPExtend(C, Ty);
1380 case Instruction::UIToFP: return getUIToFP(C, Ty);
1381 case Instruction::SIToFP: return getSIToFP(C, Ty);
1382 case Instruction::FPToUI: return getFPToUI(C, Ty);
1383 case Instruction::FPToSI: return getFPToSI(C, Ty);
1384 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1385 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1386 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001387 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001388 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001389}
1390
Reid Spencer5c140882006-12-04 20:17:56 +00001391Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001392 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001393 return getCast(Instruction::BitCast, C, Ty);
1394 return getCast(Instruction::ZExt, C, Ty);
1395}
1396
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001397Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001398 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001399 return getCast(Instruction::BitCast, C, Ty);
1400 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001401}
1402
1403Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001404 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001405 return getCast(Instruction::BitCast, C, Ty);
1406 return getCast(Instruction::Trunc, C, Ty);
1407}
1408
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001409Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001410 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001411 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001412
Chris Lattner03c49532007-01-15 02:27:26 +00001413 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001414 return getCast(Instruction::PtrToInt, S, Ty);
1415 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001416}
1417
Reid Spencer56521c42006-12-12 00:51:07 +00001418Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1419 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001420 assert(C->getType()->isIntOrIntVector() &&
1421 Ty->isIntOrIntVector() && "Invalid cast");
1422 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1423 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001424 Instruction::CastOps opcode =
1425 (SrcBits == DstBits ? Instruction::BitCast :
1426 (SrcBits > DstBits ? Instruction::Trunc :
1427 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1428 return getCast(opcode, C, Ty);
1429}
1430
1431Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001432 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001433 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001434 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1435 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001436 if (SrcBits == DstBits)
1437 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001438 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001439 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001440 return getCast(opcode, C, Ty);
1441}
1442
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001443Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001444#ifndef NDEBUG
1445 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1446 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1447#endif
1448 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1449 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1450 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1451 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001452 "SrcTy must be larger than DestTy for Trunc!");
1453
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001454 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001455}
1456
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001457Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001458#ifndef NDEBUG
1459 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1460 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1461#endif
1462 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1463 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1464 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1465 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001466 "SrcTy must be smaller than DestTy for SExt!");
1467
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001468 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001469}
1470
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001471Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001472#ifndef NDEBUG
1473 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1474 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1475#endif
1476 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1477 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1478 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1479 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001480 "SrcTy must be smaller than DestTy for ZExt!");
1481
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001482 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001483}
1484
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001485Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001486#ifndef NDEBUG
1487 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1488 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1489#endif
1490 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1491 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1492 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001493 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001494 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001495}
1496
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001497Constant *ConstantExpr::getFPExtend(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()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1504 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001505 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001506 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001507}
1508
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001509Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001510#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001511 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1512 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001513#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001514 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1515 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1516 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001517 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001518}
1519
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001520Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001521#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001522 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1523 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001524#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001525 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1526 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001527 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001528 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001529}
1530
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001531Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001532#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001533 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1534 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001535#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001536 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1537 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1538 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001539 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001540}
1541
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001542Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001543#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001544 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1545 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001546#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001547 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1548 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1549 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001550 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001551}
1552
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001553Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001554 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001555 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001556 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001557}
1558
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001559Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001560 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001561 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001562 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001563}
1564
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001565Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001566 // BitCast implies a no-op cast of type only. No bits change. However, you
1567 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001568#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001569 const Type *SrcTy = C->getType();
1570 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001571 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001572
1573 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1574 // or nonptr->ptr). For all the other types, the cast is okay if source and
1575 // destination bit widths are identical.
1576 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1577 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001578#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001579 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001580
1581 // It is common to ask for a bitcast of a value to its own type, handle this
1582 // speedily.
1583 if (C->getType() == DstTy) return C;
1584
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001585 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001586}
1587
Chris Lattnerb50d1352003-10-05 00:17:43 +00001588Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001589 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001590 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001591 assert(Opcode >= Instruction::BinaryOpsBegin &&
1592 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001593 "Invalid opcode in binary constant expression");
1594 assert(C1->getType() == C2->getType() &&
1595 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001596
Reid Spencer542964f2007-01-11 18:21:29 +00001597 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001598 if (Constant *FC = ConstantFoldBinaryInstruction(
1599 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001600 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001601
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001602 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001603 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001604
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001605 // Implicitly locked.
1606 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001607}
1608
Reid Spencer266e42b2006-12-23 06:05:41 +00001609Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001610 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001611 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001612 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001613 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1614 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1615 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1616 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1617 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1618 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001619 return getFCmp(predicate, C1, C2);
1620
Nate Begemanc96e2e42008-07-25 17:35:37 +00001621 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1622 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1623 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1624 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001625 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001626 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001627}
1628
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001629Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001630 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1631 if (C1->getType()->isFPOrFPVector()) {
1632 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1633 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1634 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1635 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001636#ifndef NDEBUG
1637 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001638 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001639 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001640 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001641 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001642 assert(C1->getType()->isIntOrIntVector() &&
1643 "Tried to create an integer operation on a non-integer type!");
1644 break;
1645 case Instruction::FAdd:
1646 case Instruction::FSub:
1647 case Instruction::FMul:
1648 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1649 assert(C1->getType()->isFPOrFPVector() &&
1650 "Tried to create a floating-point operation on a "
1651 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001652 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001653 case Instruction::UDiv:
1654 case Instruction::SDiv:
1655 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001656 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001657 "Tried to create an arithmetic operation on a non-arithmetic type!");
1658 break;
1659 case Instruction::FDiv:
1660 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001661 assert(C1->getType()->isFPOrFPVector() &&
1662 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001663 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001664 case Instruction::URem:
1665 case Instruction::SRem:
1666 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001667 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001668 "Tried to create an arithmetic operation on a non-arithmetic type!");
1669 break;
1670 case Instruction::FRem:
1671 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001672 assert(C1->getType()->isFPOrFPVector() &&
1673 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001674 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001675 case Instruction::And:
1676 case Instruction::Or:
1677 case Instruction::Xor:
1678 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001679 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001680 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001681 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001682 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001683 case Instruction::LShr:
1684 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001685 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001686 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001687 "Tried to create a shift operation on a non-integer type!");
1688 break;
1689 default:
1690 break;
1691 }
1692#endif
1693
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001694 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001695}
1696
Reid Spencer266e42b2006-12-23 06:05:41 +00001697Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001698 Constant *C1, Constant *C2) {
1699 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001700 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001701}
1702
Chris Lattner6e415c02004-03-12 05:54:04 +00001703Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001704 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001705 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001706
1707 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001708 if (Constant *SC = ConstantFoldSelectInstruction(
1709 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001710 return SC; // Fold common cases
1711
1712 std::vector<Constant*> argVec(3, C);
1713 argVec[1] = V1;
1714 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001715 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001716
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001717 // Implicitly locked.
1718 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001719}
1720
Chris Lattnerb50d1352003-10-05 00:17:43 +00001721Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001722 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001723 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001724 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1725 Idxs+NumIdx) ==
1726 cast<PointerType>(ReqTy)->getElementType() &&
1727 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001728
Owen Anderson53a52212009-07-13 04:09:18 +00001729 if (Constant *FC = ConstantFoldGetElementPtr(
1730 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001731 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001732
Chris Lattnerb50d1352003-10-05 00:17:43 +00001733 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001734 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001735 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001736 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001737 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001738 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001739 for (unsigned i = 0; i != NumIdx; ++i)
1740 ArgVec.push_back(cast<Constant>(Idxs[i]));
1741 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001742
1743 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001744 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001745}
1746
Chris Lattner302116a2007-01-31 04:40:28 +00001747Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001748 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001749 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001750 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00001751 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001752 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001753 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001754 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001755}
1756
Chris Lattner302116a2007-01-31 04:40:28 +00001757Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001758 unsigned NumIdx) {
1759 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001760}
1761
Chris Lattner302116a2007-01-31 04:40:28 +00001762
Reid Spenceree3c9912006-12-04 05:19:50 +00001763Constant *
1764ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1765 assert(LHS->getType() == RHS->getType());
1766 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1767 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1768
Owen Anderson53a52212009-07-13 04:09:18 +00001769 if (Constant *FC = ConstantFoldCompareInstruction(
1770 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001771 return FC; // Fold a few common cases...
1772
1773 // Look up the constant in the table first to ensure uniqueness
1774 std::vector<Constant*> ArgVec;
1775 ArgVec.push_back(LHS);
1776 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001777 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001778 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001779
1780 // Implicitly locked.
1781 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001782}
1783
1784Constant *
1785ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1786 assert(LHS->getType() == RHS->getType());
1787 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1788
Owen Anderson53a52212009-07-13 04:09:18 +00001789 if (Constant *FC = ConstantFoldCompareInstruction(
1790 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001791 return FC; // Fold a few common cases...
1792
1793 // Look up the constant in the table first to ensure uniqueness
1794 std::vector<Constant*> ArgVec;
1795 ArgVec.push_back(LHS);
1796 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001797 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001798 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001799
1800 // Implicitly locked.
1801 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001802}
1803
Robert Bocchino23004482006-01-10 19:05:34 +00001804Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1805 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001806 if (Constant *FC = ConstantFoldExtractElementInstruction(
1807 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001808 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001809 // Look up the constant in the table first to ensure uniqueness
1810 std::vector<Constant*> ArgVec(1, Val);
1811 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001812 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001813
1814 // Implicitly locked.
1815 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001816}
1817
1818Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001819 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001820 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001821 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001822 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001823 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001824 Val, Idx);
1825}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001826
Robert Bocchinoca27f032006-01-17 20:07:22 +00001827Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1828 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001829 if (Constant *FC = ConstantFoldInsertElementInstruction(
1830 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00001831 return FC; // Fold a few common cases...
1832 // Look up the constant in the table first to ensure uniqueness
1833 std::vector<Constant*> ArgVec(1, Val);
1834 ArgVec.push_back(Elt);
1835 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001836 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001837
1838 // Implicitly locked.
1839 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001840}
1841
1842Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1843 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001844 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001845 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001846 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001847 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001848 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001849 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00001850 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001851}
1852
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001853Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1854 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00001855 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
1856 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001857 return FC; // Fold a few common cases...
1858 // Look up the constant in the table first to ensure uniqueness
1859 std::vector<Constant*> ArgVec(1, V1);
1860 ArgVec.push_back(V2);
1861 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001862 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001863
1864 // Implicitly locked.
1865 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001866}
1867
1868Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1869 Constant *Mask) {
1870 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1871 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00001872
1873 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1874 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1875 const Type *ShufTy = VectorType::get(EltTy, NElts);
1876 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001877}
1878
Dan Gohman12fce772008-05-15 19:50:34 +00001879Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1880 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001881 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001882 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1883 Idxs+NumIdx) == Val->getType() &&
1884 "insertvalue indices invalid!");
1885 assert(Agg->getType() == ReqTy &&
1886 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001887 assert(Agg->getType()->isFirstClassType() &&
1888 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00001889 Constant *FC = ConstantFoldInsertValueInstruction(
1890 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001891 assert(FC && "InsertValue constant expr couldn't be folded!");
1892 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001893}
1894
1895Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001896 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001897 assert(Agg->getType()->isFirstClassType() &&
1898 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001899
Dan Gohman0752bff2008-05-23 00:36:11 +00001900 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00001901#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00001902 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00001903 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00001904#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00001905 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00001906 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
1907}
1908
1909Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001910 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001911 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1912 Idxs+NumIdx) == ReqTy &&
1913 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001914 assert(Agg->getType()->isFirstClassType() &&
1915 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00001916 Constant *FC = ConstantFoldExtractValueInstruction(
1917 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001918 assert(FC && "ExtractValue constant expr couldn't be folded!");
1919 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001920}
1921
1922Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001923 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001924 assert(Agg->getType()->isFirstClassType() &&
1925 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001926
1927 const Type *ReqTy =
1928 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
1929 assert(ReqTy && "extractvalue indices invalid!");
1930 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
1931}
1932
Vikram S. Adve4c485332002-07-15 18:19:33 +00001933// destroyConstant - Remove the constant from the constant table...
1934//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001935void ConstantExpr::destroyConstant() {
1936 // Implicitly locked.
1937 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001938 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001939}
1940
Chris Lattner3cd8c562002-07-30 18:54:25 +00001941const char *ConstantExpr::getOpcodeName() const {
1942 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001943}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001944
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001945//===----------------------------------------------------------------------===//
1946// replaceUsesOfWithOnConstant implementations
1947
Chris Lattner913849b2007-08-21 00:55:23 +00001948/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
1949/// 'From' to be uses of 'To'. This must update the uniquing data structures
1950/// etc.
1951///
1952/// Note that we intentionally replace all uses of From with To here. Consider
1953/// a large array that uses 'From' 1000 times. By handling this case all here,
1954/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
1955/// single invocation handles all 1000 uses. Handling them one at a time would
1956/// work, but would be really slow because it would have to unique each updated
1957/// array instance.
Owen Andersonc2c79322009-07-28 18:32:17 +00001958
1959static std::vector<Constant*> getValType(ConstantArray *CA) {
1960 std::vector<Constant*> Elements;
1961 Elements.reserve(CA->getNumOperands());
1962 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1963 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1964 return Elements;
1965}
1966
1967
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001968void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001969 Use *U) {
Owen Andersonc2c79322009-07-28 18:32:17 +00001970 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1971 Constant *ToC = cast<Constant>(To);
1972
1973 LLVMContext &Context = getType()->getContext();
1974 LLVMContextImpl *pImpl = Context.pImpl;
1975
1976 std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, Constant*> Lookup;
1977 Lookup.first.first = getType();
1978 Lookup.second = this;
1979
1980 std::vector<Constant*> &Values = Lookup.first.second;
1981 Values.reserve(getNumOperands()); // Build replacement array.
1982
1983 // Fill values with the modified operands of the constant array. Also,
1984 // compute whether this turns into an all-zeros array.
1985 bool isAllZeros = false;
1986 unsigned NumUpdated = 0;
1987 if (!ToC->isNullValue()) {
1988 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1989 Constant *Val = cast<Constant>(O->get());
1990 if (Val == From) {
1991 Val = ToC;
1992 ++NumUpdated;
1993 }
1994 Values.push_back(Val);
1995 }
1996 } else {
1997 isAllZeros = true;
1998 for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
1999 Constant *Val = cast<Constant>(O->get());
2000 if (Val == From) {
2001 Val = ToC;
2002 ++NumUpdated;
2003 }
2004 Values.push_back(Val);
2005 if (isAllZeros) isAllZeros = Val->isNullValue();
2006 }
2007 }
2008
2009 Constant *Replacement = 0;
2010 if (isAllZeros) {
2011 Replacement = Context.getConstantAggregateZero(getType());
2012 } else {
2013 // Check to see if we have this array type already.
2014 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
2015 bool Exists;
2016 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
2017 pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
2018
2019 if (Exists) {
2020 Replacement = I->second;
2021 } else {
2022 // Okay, the new shape doesn't exist in the system yet. Instead of
2023 // creating a new constant array, inserting it, replaceallusesof'ing the
2024 // old with the new, then deleting the old... just update the current one
2025 // in place!
2026 pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
2027
2028 // Update to the new value. Optimize for the case when we have a single
2029 // operand that we're changing, but handle bulk updates efficiently.
2030 if (NumUpdated == 1) {
2031 unsigned OperandToUpdate = U - OperandList;
2032 assert(getOperand(OperandToUpdate) == From &&
2033 "ReplaceAllUsesWith broken!");
2034 setOperand(OperandToUpdate, ToC);
2035 } else {
2036 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2037 if (getOperand(i) == From)
2038 setOperand(i, ToC);
2039 }
2040 return;
2041 }
2042 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002043
2044 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002045 assert(Replacement != this && "I didn't contain From!");
2046
Chris Lattner7a1450d2005-10-04 18:13:04 +00002047 // Everyone using this now uses the replacement.
2048 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002049
2050 // Delete the old constant!
2051 destroyConstant();
2052}
2053
Owen Anderson45308b52009-07-27 22:29:26 +00002054static std::vector<Constant*> getValType(ConstantStruct *CS) {
2055 std::vector<Constant*> Elements;
2056 Elements.reserve(CS->getNumOperands());
2057 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
2058 Elements.push_back(cast<Constant>(CS->getOperand(i)));
2059 return Elements;
2060}
2061
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002062void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002063 Use *U) {
Owen Anderson45308b52009-07-27 22:29:26 +00002064 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2065 Constant *ToC = cast<Constant>(To);
2066
2067 unsigned OperandToUpdate = U-OperandList;
2068 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2069
2070 std::pair<LLVMContextImpl::StructConstantsTy::MapKey, Constant*> Lookup;
2071 Lookup.first.first = getType();
2072 Lookup.second = this;
2073 std::vector<Constant*> &Values = Lookup.first.second;
2074 Values.reserve(getNumOperands()); // Build replacement struct.
2075
2076
2077 // Fill values with the modified operands of the constant struct. Also,
2078 // compute whether this turns into an all-zeros struct.
2079 bool isAllZeros = false;
2080 if (!ToC->isNullValue()) {
2081 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
2082 Values.push_back(cast<Constant>(O->get()));
2083 } else {
2084 isAllZeros = true;
2085 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2086 Constant *Val = cast<Constant>(O->get());
2087 Values.push_back(Val);
2088 if (isAllZeros) isAllZeros = Val->isNullValue();
2089 }
2090 }
2091 Values[OperandToUpdate] = ToC;
2092
2093 LLVMContext &Context = getType()->getContext();
2094 LLVMContextImpl *pImpl = Context.pImpl;
2095
2096 Constant *Replacement = 0;
2097 if (isAllZeros) {
2098 Replacement = Context.getConstantAggregateZero(getType());
2099 } else {
2100 // Check to see if we have this array type already.
2101 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
2102 bool Exists;
2103 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2104 pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
2105
2106 if (Exists) {
2107 Replacement = I->second;
2108 } else {
2109 // Okay, the new shape doesn't exist in the system yet. Instead of
2110 // creating a new constant struct, inserting it, replaceallusesof'ing the
2111 // old with the new, then deleting the old... just update the current one
2112 // in place!
2113 pImpl->StructConstants.MoveConstantToNewSlot(this, I);
2114
2115 // Update to the new value.
2116 setOperand(OperandToUpdate, ToC);
2117 return;
2118 }
2119 }
2120
2121 assert(Replacement != this && "I didn't contain From!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002122
Chris Lattner7a1450d2005-10-04 18:13:04 +00002123 // Everyone using this now uses the replacement.
2124 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002125
2126 // Delete the old constant!
2127 destroyConstant();
2128}
2129
Reid Spencerd84d35b2007-02-15 02:26:10 +00002130void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002131 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002132 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2133
2134 std::vector<Constant*> Values;
2135 Values.reserve(getNumOperands()); // Build replacement array...
2136 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2137 Constant *Val = getOperand(i);
2138 if (Val == From) Val = cast<Constant>(To);
2139 Values.push_back(Val);
2140 }
2141
Owen Anderson0348a132009-07-24 00:36:24 +00002142 Constant *Replacement =
2143 getType()->getContext().getConstantVector(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002144 assert(Replacement != this && "I didn't contain From!");
2145
Chris Lattner7a1450d2005-10-04 18:13:04 +00002146 // Everyone using this now uses the replacement.
2147 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002148
2149 // Delete the old constant!
2150 destroyConstant();
2151}
2152
2153void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002154 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002155 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2156 Constant *To = cast<Constant>(ToV);
2157
2158 Constant *Replacement = 0;
2159 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002160 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002161 Constant *Pointer = getOperand(0);
2162 Indices.reserve(getNumOperands()-1);
2163 if (Pointer == From) Pointer = To;
2164
2165 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2166 Constant *Val = getOperand(i);
2167 if (Val == From) Val = To;
2168 Indices.push_back(Val);
2169 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002170 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2171 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002172 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002173 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002174 if (Agg == From) Agg = To;
2175
Dan Gohman1ecaf452008-05-31 00:58:22 +00002176 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002177 Replacement = ConstantExpr::getExtractValue(Agg,
2178 &Indices[0], Indices.size());
2179 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002180 Constant *Agg = getOperand(0);
2181 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002182 if (Agg == From) Agg = To;
2183 if (Val == From) Val = To;
2184
Dan Gohman1ecaf452008-05-31 00:58:22 +00002185 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002186 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2187 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002188 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002189 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002190 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002191 } else if (getOpcode() == Instruction::Select) {
2192 Constant *C1 = getOperand(0);
2193 Constant *C2 = getOperand(1);
2194 Constant *C3 = getOperand(2);
2195 if (C1 == From) C1 = To;
2196 if (C2 == From) C2 = To;
2197 if (C3 == From) C3 = To;
2198 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002199 } else if (getOpcode() == Instruction::ExtractElement) {
2200 Constant *C1 = getOperand(0);
2201 Constant *C2 = getOperand(1);
2202 if (C1 == From) C1 = To;
2203 if (C2 == From) C2 = To;
2204 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002205 } else if (getOpcode() == Instruction::InsertElement) {
2206 Constant *C1 = getOperand(0);
2207 Constant *C2 = getOperand(1);
2208 Constant *C3 = getOperand(1);
2209 if (C1 == From) C1 = To;
2210 if (C2 == From) C2 = To;
2211 if (C3 == From) C3 = To;
2212 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2213 } else if (getOpcode() == Instruction::ShuffleVector) {
2214 Constant *C1 = getOperand(0);
2215 Constant *C2 = getOperand(1);
2216 Constant *C3 = getOperand(2);
2217 if (C1 == From) C1 = To;
2218 if (C2 == From) C2 = To;
2219 if (C3 == From) C3 = To;
2220 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002221 } else if (isCompare()) {
2222 Constant *C1 = getOperand(0);
2223 Constant *C2 = getOperand(1);
2224 if (C1 == From) C1 = To;
2225 if (C2 == From) C2 = To;
2226 if (getOpcode() == Instruction::ICmp)
2227 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002228 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002229 assert(getOpcode() == Instruction::FCmp);
2230 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002231 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002232 } else if (getNumOperands() == 2) {
2233 Constant *C1 = getOperand(0);
2234 Constant *C2 = getOperand(1);
2235 if (C1 == From) C1 = To;
2236 if (C2 == From) C2 = To;
2237 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2238 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002239 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002240 return;
2241 }
2242
2243 assert(Replacement != this && "I didn't contain From!");
2244
Chris Lattner7a1450d2005-10-04 18:13:04 +00002245 // Everyone using this now uses the replacement.
2246 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002247
2248 // Delete the old constant!
2249 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002250}
Nick Lewycky49f89192009-04-04 07:22:01 +00002251
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002252void MDNode::replaceElement(Value *From, Value *To) {
2253 SmallVector<Value*, 4> Values;
2254 Values.reserve(getNumElements()); // Build replacement array...
2255 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2256 Value *Val = getElement(i);
2257 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002258 Values.push_back(Val);
2259 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002260
Owen Anderson4118dde2009-07-16 23:44:30 +00002261 MDNode *Replacement =
2262 getType()->getContext().getMDNode(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002263 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002264
Nick Lewycky49f89192009-04-04 07:22:01 +00002265 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewycky49f89192009-04-04 07:22:01 +00002266}