blob: ecb62568a2e7d0ae95069f130fd040d4b1064ef6 [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
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000378
Chris Lattner3462ae32001-12-03 22:26:30 +0000379ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000380 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000381 : Constant(T, ConstantStructVal,
382 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
383 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000384 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000385 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000386 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000387 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
388 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000389 Constant *C = *I;
390 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000391 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000392 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000393 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000394 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000395 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000396 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000397 }
398}
399
Owen Anderson45308b52009-07-27 22:29:26 +0000400// ConstantStruct accessors.
401Constant* ConstantStruct::get(const StructType* T,
402 const std::vector<Constant*>& V) {
403 LLVMContextImpl* pImpl = T->getContext().pImpl;
404
405 // Create a ConstantAggregateZero value if all elements are zeros...
406 for (unsigned i = 0, e = V.size(); i != e; ++i)
407 if (!V[i]->isNullValue())
408 // Implicitly locked.
409 return pImpl->StructConstants.getOrCreate(T, V);
410
411 return T->getContext().getConstantAggregateZero(T);
412}
413
414Constant* ConstantStruct::get(const std::vector<Constant*>& V, bool packed) {
415 std::vector<const Type*> StructEls;
416 StructEls.reserve(V.size());
417 for (unsigned i = 0, e = V.size(); i != e; ++i)
418 StructEls.push_back(V[i]->getType());
419 return get(StructType::get(StructEls, packed), V);
420}
421
422Constant* ConstantStruct::get(Constant* const *Vals, unsigned NumVals,
423 bool Packed) {
424 // FIXME: make this the primary ctor method.
425 return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
426}
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000427
Reid Spencerd84d35b2007-02-15 02:26:10 +0000428ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000429 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000430 : Constant(T, ConstantVectorVal,
431 OperandTraits<ConstantVector>::op_end(this) - V.size(),
432 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000433 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000434 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
435 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000436 Constant *C = *I;
437 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000438 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000439 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000440 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000441 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000442 }
443}
444
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000445
Gabor Greiff6caff662008-05-10 08:32:32 +0000446namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000447// We declare several classes private to this file, so use an anonymous
448// namespace
449namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000450
Gordon Henriksen14a55692007-12-10 02:14:30 +0000451/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
452/// behind the scenes to implement unary constant exprs.
453class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000454 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000455public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000456 // allocate space for exactly one operand
457 void *operator new(size_t s) {
458 return User::operator new(s, 1);
459 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000460 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000461 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
462 Op<0>() = C;
463 }
464 /// Transparently provide more efficient getOperand methods.
465 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000466};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000467
Gordon Henriksen14a55692007-12-10 02:14:30 +0000468/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
469/// behind the scenes to implement binary constant exprs.
470class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000471 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000472public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000473 // allocate space for exactly two operands
474 void *operator new(size_t s) {
475 return User::operator new(s, 2);
476 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000477 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000478 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000479 Op<0>() = C1;
480 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000481 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000482 /// Transparently provide more efficient getOperand methods.
483 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000484};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000485
Gordon Henriksen14a55692007-12-10 02:14:30 +0000486/// SelectConstantExpr - This class is private to Constants.cpp, and is used
487/// behind the scenes to implement select constant exprs.
488class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000489 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000490public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000491 // allocate space for exactly three operands
492 void *operator new(size_t s) {
493 return User::operator new(s, 3);
494 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000495 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000496 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000497 Op<0>() = C1;
498 Op<1>() = C2;
499 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000500 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000501 /// Transparently provide more efficient getOperand methods.
502 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000503};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000504
Gordon Henriksen14a55692007-12-10 02:14:30 +0000505/// ExtractElementConstantExpr - This class is private to
506/// Constants.cpp, and is used behind the scenes to implement
507/// extractelement constant exprs.
508class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000509 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000510public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000511 // allocate space for exactly two operands
512 void *operator new(size_t s) {
513 return User::operator new(s, 2);
514 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000515 ExtractElementConstantExpr(Constant *C1, Constant *C2)
516 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000517 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000518 Op<0>() = C1;
519 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000520 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000521 /// Transparently provide more efficient getOperand methods.
522 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000523};
Robert Bocchino23004482006-01-10 19:05:34 +0000524
Gordon Henriksen14a55692007-12-10 02:14:30 +0000525/// InsertElementConstantExpr - This class is private to
526/// Constants.cpp, and is used behind the scenes to implement
527/// insertelement constant exprs.
528class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000529 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000530public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000531 // allocate space for exactly three operands
532 void *operator new(size_t s) {
533 return User::operator new(s, 3);
534 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000535 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
536 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000537 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000538 Op<0>() = C1;
539 Op<1>() = C2;
540 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000541 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000542 /// Transparently provide more efficient getOperand methods.
543 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000544};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000545
Gordon Henriksen14a55692007-12-10 02:14:30 +0000546/// ShuffleVectorConstantExpr - This class is private to
547/// Constants.cpp, and is used behind the scenes to implement
548/// shufflevector constant exprs.
549class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000550 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000551public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000552 // allocate space for exactly three operands
553 void *operator new(size_t s) {
554 return User::operator new(s, 3);
555 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000556 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000557 : ConstantExpr(VectorType::get(
558 cast<VectorType>(C1->getType())->getElementType(),
559 cast<VectorType>(C3->getType())->getNumElements()),
560 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000561 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000562 Op<0>() = C1;
563 Op<1>() = C2;
564 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000565 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000566 /// Transparently provide more efficient getOperand methods.
567 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000568};
569
Dan Gohman12fce772008-05-15 19:50:34 +0000570/// ExtractValueConstantExpr - This class is private to
571/// Constants.cpp, and is used behind the scenes to implement
572/// extractvalue constant exprs.
573class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000574 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000575public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000576 // allocate space for exactly one operand
577 void *operator new(size_t s) {
578 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000579 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000580 ExtractValueConstantExpr(Constant *Agg,
581 const SmallVector<unsigned, 4> &IdxList,
582 const Type *DestTy)
583 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
584 Indices(IdxList) {
585 Op<0>() = Agg;
586 }
587
Dan Gohman7bb04502008-05-31 19:09:08 +0000588 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000589 const SmallVector<unsigned, 4> Indices;
590
Dan Gohman12fce772008-05-15 19:50:34 +0000591 /// Transparently provide more efficient getOperand methods.
592 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
593};
594
595/// InsertValueConstantExpr - This class is private to
596/// Constants.cpp, and is used behind the scenes to implement
597/// insertvalue constant exprs.
598class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000599 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000600public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000601 // allocate space for exactly one operand
602 void *operator new(size_t s) {
603 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000604 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000605 InsertValueConstantExpr(Constant *Agg, Constant *Val,
606 const SmallVector<unsigned, 4> &IdxList,
607 const Type *DestTy)
608 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
609 Indices(IdxList) {
610 Op<0>() = Agg;
611 Op<1>() = Val;
612 }
613
Dan Gohman7bb04502008-05-31 19:09:08 +0000614 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000615 const SmallVector<unsigned, 4> Indices;
616
Dan Gohman12fce772008-05-15 19:50:34 +0000617 /// Transparently provide more efficient getOperand methods.
618 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
619};
620
621
Gordon Henriksen14a55692007-12-10 02:14:30 +0000622/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
623/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000624class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000625 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000626 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000627public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000628 static GetElementPtrConstantExpr *Create(Constant *C,
629 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000630 const Type *DestTy) {
Dan Gohman33a3fd02009-07-20 17:43:30 +0000631 return
632 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000633 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000634 /// Transparently provide more efficient getOperand methods.
635 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000636};
637
638// CompareConstantExpr - This class is private to Constants.cpp, and is used
639// behind the scenes to implement ICmp and FCmp constant expressions. This is
640// needed in order to store the predicate value for these instructions.
641struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000642 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
643 // allocate space for exactly two operands
644 void *operator new(size_t s) {
645 return User::operator new(s, 2);
646 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000647 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000648 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
649 unsigned short pred, Constant* LHS, Constant* RHS)
650 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000651 Op<0>() = LHS;
652 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000653 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000654 /// Transparently provide more efficient getOperand methods.
655 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000656};
657
658} // end anonymous namespace
659
Gabor Greiff6caff662008-05-10 08:32:32 +0000660template <>
661struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
662};
663DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
664
665template <>
666struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
667};
668DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
669
670template <>
671struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
672};
673DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
674
675template <>
676struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
677};
678DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
679
680template <>
681struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
682};
683DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
684
685template <>
686struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
687};
688DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
689
Dan Gohman12fce772008-05-15 19:50:34 +0000690template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000691struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000692};
Dan Gohman12fce772008-05-15 19:50:34 +0000693DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
694
695template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000696struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000697};
Dan Gohman12fce772008-05-15 19:50:34 +0000698DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
699
Gabor Greiff6caff662008-05-10 08:32:32 +0000700template <>
701struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
702};
703
704GetElementPtrConstantExpr::GetElementPtrConstantExpr
705 (Constant *C,
706 const std::vector<Constant*> &IdxList,
707 const Type *DestTy)
708 : ConstantExpr(DestTy, Instruction::GetElementPtr,
709 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
710 - (IdxList.size()+1),
711 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000712 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000713 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000714 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000715}
716
717DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
718
719
720template <>
721struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
722};
723DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
724
725
726} // End llvm namespace
727
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000728
729// Utility function for determining if a ConstantExpr is a CastOp or not. This
730// can't be inline because we don't want to #include Instruction.h into
731// Constant.h
732bool ConstantExpr::isCast() const {
733 return Instruction::isCast(getOpcode());
734}
735
Reid Spenceree3c9912006-12-04 05:19:50 +0000736bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000737 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000738}
739
Dan Gohman1ecaf452008-05-31 00:58:22 +0000740bool ConstantExpr::hasIndices() const {
741 return getOpcode() == Instruction::ExtractValue ||
742 getOpcode() == Instruction::InsertValue;
743}
744
745const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
746 if (const ExtractValueConstantExpr *EVCE =
747 dyn_cast<ExtractValueConstantExpr>(this))
748 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000749
750 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000751}
752
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000753unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000754 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000755 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000756 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000757}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000758
Chris Lattner7c1018a2006-07-14 19:37:40 +0000759/// getWithOperandReplaced - Return a constant expression identical to this
760/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000761Constant *
762ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000763 assert(OpNo < getNumOperands() && "Operand num is out of range!");
764 assert(Op->getType() == getOperand(OpNo)->getType() &&
765 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000766 if (getOperand(OpNo) == Op)
767 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000768
Chris Lattner227816342006-07-14 22:20:01 +0000769 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000770 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000771 case Instruction::Trunc:
772 case Instruction::ZExt:
773 case Instruction::SExt:
774 case Instruction::FPTrunc:
775 case Instruction::FPExt:
776 case Instruction::UIToFP:
777 case Instruction::SIToFP:
778 case Instruction::FPToUI:
779 case Instruction::FPToSI:
780 case Instruction::PtrToInt:
781 case Instruction::IntToPtr:
782 case Instruction::BitCast:
783 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000784 case Instruction::Select:
785 Op0 = (OpNo == 0) ? Op : getOperand(0);
786 Op1 = (OpNo == 1) ? Op : getOperand(1);
787 Op2 = (OpNo == 2) ? Op : getOperand(2);
788 return ConstantExpr::getSelect(Op0, Op1, Op2);
789 case Instruction::InsertElement:
790 Op0 = (OpNo == 0) ? Op : getOperand(0);
791 Op1 = (OpNo == 1) ? Op : getOperand(1);
792 Op2 = (OpNo == 2) ? Op : getOperand(2);
793 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
794 case Instruction::ExtractElement:
795 Op0 = (OpNo == 0) ? Op : getOperand(0);
796 Op1 = (OpNo == 1) ? Op : getOperand(1);
797 return ConstantExpr::getExtractElement(Op0, Op1);
798 case Instruction::ShuffleVector:
799 Op0 = (OpNo == 0) ? Op : getOperand(0);
800 Op1 = (OpNo == 1) ? Op : getOperand(1);
801 Op2 = (OpNo == 2) ? Op : getOperand(2);
802 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000803 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000804 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000805 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000806 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000807 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000808 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000809 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000810 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000811 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000812 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000813 default:
814 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000815 Op0 = (OpNo == 0) ? Op : getOperand(0);
816 Op1 = (OpNo == 1) ? Op : getOperand(1);
817 return ConstantExpr::get(getOpcode(), Op0, Op1);
818 }
819}
820
821/// getWithOperands - This returns the current constant expression with the
822/// operands replaced with the specified values. The specified operands must
823/// match count and type with the existing ones.
824Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000825getWithOperands(Constant* const *Ops, unsigned NumOps) const {
826 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000827 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000828 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000829 assert(Ops[i]->getType() == getOperand(i)->getType() &&
830 "Operand type mismatch!");
831 AnyChange |= Ops[i] != getOperand(i);
832 }
833 if (!AnyChange) // No operands changed, return self.
834 return const_cast<ConstantExpr*>(this);
835
836 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000837 case Instruction::Trunc:
838 case Instruction::ZExt:
839 case Instruction::SExt:
840 case Instruction::FPTrunc:
841 case Instruction::FPExt:
842 case Instruction::UIToFP:
843 case Instruction::SIToFP:
844 case Instruction::FPToUI:
845 case Instruction::FPToSI:
846 case Instruction::PtrToInt:
847 case Instruction::IntToPtr:
848 case Instruction::BitCast:
849 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000850 case Instruction::Select:
851 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
852 case Instruction::InsertElement:
853 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
854 case Instruction::ExtractElement:
855 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
856 case Instruction::ShuffleVector:
857 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000858 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000859 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000860 case Instruction::ICmp:
861 case Instruction::FCmp:
862 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000863 default:
864 assert(getNumOperands() == 2 && "Must be binary operator?");
865 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000866 }
867}
868
Chris Lattner2f7c9632001-06-06 20:29:01 +0000869
870//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000871// isValueValidForType implementations
872
Reid Spencere7334722006-12-19 01:28:19 +0000873bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000874 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000875 if (Ty == Type::Int1Ty)
876 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000877 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000878 return true; // always true, has to fit in largest type
879 uint64_t Max = (1ll << NumBits) - 1;
880 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000881}
882
Reid Spencere0fc4df2006-10-20 07:07:24 +0000883bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000884 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000885 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000886 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000887 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000888 return true; // always true, has to fit in largest type
889 int64_t Min = -(1ll << (NumBits-1));
890 int64_t Max = (1ll << (NumBits-1)) - 1;
891 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000892}
893
Dale Johannesend246b2c2007-08-30 00:23:21 +0000894bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
895 // convert modifies in place, so make a copy.
896 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000897 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000898 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000899 default:
900 return false; // These can't be represented as floating point!
901
Dale Johannesend246b2c2007-08-30 00:23:21 +0000902 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000903 case Type::FloatTyID: {
904 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
905 return true;
906 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
907 return !losesInfo;
908 }
909 case Type::DoubleTyID: {
910 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
911 &Val2.getSemantics() == &APFloat::IEEEdouble)
912 return true;
913 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
914 return !losesInfo;
915 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000916 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000917 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
918 &Val2.getSemantics() == &APFloat::IEEEdouble ||
919 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000920 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000921 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
922 &Val2.getSemantics() == &APFloat::IEEEdouble ||
923 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000924 case Type::PPC_FP128TyID:
925 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
926 &Val2.getSemantics() == &APFloat::IEEEdouble ||
927 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000928 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000929}
Chris Lattner9655e542001-07-20 19:16:02 +0000930
Chris Lattner49d855c2001-09-07 16:46:31 +0000931//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000932// Factory Function Implementation
933
Dan Gohman92b551b2009-03-03 02:55:14 +0000934/// destroyConstant - Remove the constant from the constant table...
935///
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000936void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +0000937 // Implicitly locked.
Owen Anderson39ede7b2009-07-21 20:13:12 +0000938 getType()->getContext().erase(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000939 destroyConstantImpl();
940}
941
Dan Gohman92b551b2009-03-03 02:55:14 +0000942/// destroyConstant - Remove the constant from the constant table...
943///
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000944void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +0000945 // Implicitly locked.
Owen Anderson3d344922009-07-21 20:55:28 +0000946 getType()->getContext().erase(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000947 destroyConstantImpl();
948}
949
Reid Spencer2546b762007-01-26 07:37:34 +0000950/// isString - This method returns true if the array is an array of i8, and
951/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000952bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +0000953 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +0000954 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000955 return false;
956 // Check the elements to make sure they are all integers, not constant
957 // expressions.
958 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
959 if (!isa<ConstantInt>(getOperand(i)))
960 return false;
961 return true;
962}
963
Evan Cheng3763c5b2006-10-26 19:15:05 +0000964/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +0000965/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +0000966/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +0000967bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +0000968 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +0000969 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +0000970 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +0000971
Evan Chenge974da62006-10-26 21:48:03 +0000972 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +0000973 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +0000974 return false;
975 // Other elements must be non-null integers.
976 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
977 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +0000978 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +0000979 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +0000980 return false;
981 }
Evan Cheng3763c5b2006-10-26 19:15:05 +0000982 return true;
983}
984
985
Dan Gohman92b551b2009-03-03 02:55:14 +0000986/// getAsString - If the sub-element type of this array is i8
987/// then this method converts the array to an std::string and returns it.
988/// Otherwise, it asserts out.
989///
Chris Lattner81fabb02002-08-26 17:53:56 +0000990std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000991 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000992 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +0000993 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +0000994 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +0000995 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +0000996 return Result;
997}
998
999
Chris Lattner3462ae32001-12-03 22:26:30 +00001000//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001001//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001002
Chris Lattner189d19f2003-11-21 20:23:48 +00001003namespace llvm {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001004
Chris Lattner49d855c2001-09-07 16:46:31 +00001005}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001006
Chris Lattnerd7a73302001-10-13 06:57:33 +00001007// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001008//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001009void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001010 // Implicitly locked.
Owen Anderson45308b52009-07-27 22:29:26 +00001011 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001012 destroyConstantImpl();
1013}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001014
Brian Gaeke02209042004-08-20 06:00:58 +00001015// destroyConstant - Remove the constant from the constant table...
1016//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001017void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001018 // Implicitly locked.
Owen Anderson0348a132009-07-24 00:36:24 +00001019 getType()->getContext().erase(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001020 destroyConstantImpl();
1021}
1022
Dan Gohman30978072007-05-24 14:36:04 +00001023/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001024/// is set to all ones.
1025/// @returns true iff this constant's emements are all set to all ones.
1026/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001027bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001028 // Check out first element.
1029 const Constant *Elt = getOperand(0);
1030 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1031 if (!CI || !CI->isAllOnesValue()) return false;
1032 // Then make sure all remaining elements point to the same value.
1033 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1034 if (getOperand(I) != Elt) return false;
1035 }
1036 return true;
1037}
1038
Dan Gohman07159202007-10-17 17:51:30 +00001039/// getSplatValue - If this is a splat constant, where all of the
1040/// elements have the same value, return that value. Otherwise return null.
1041Constant *ConstantVector::getSplatValue() {
1042 // Check out first element.
1043 Constant *Elt = getOperand(0);
1044 // Then make sure all remaining elements point to the same value.
1045 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1046 if (getOperand(I) != Elt) return 0;
1047 return Elt;
1048}
1049
Chris Lattner3462ae32001-12-03 22:26:30 +00001050//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001051//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001052
Chris Lattner189d19f2003-11-21 20:23:48 +00001053namespace llvm {
1054 // ConstantPointerNull does not take extra "value" argument...
1055 template<class ValType>
1056 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1057 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1058 return new ConstantPointerNull(Ty);
1059 }
1060 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001061
Chris Lattner189d19f2003-11-21 20:23:48 +00001062 template<>
1063 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1064 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1065 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001066 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001067 assert(New != OldC && "Didn't replace constant??");
1068 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001069 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001070 }
1071 };
1072}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001073
Chris Lattner69edc982006-09-28 00:35:06 +00001074static ManagedStatic<ValueMap<char, PointerType,
1075 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001076
Chris Lattner3e650af2004-08-04 04:48:01 +00001077static char getValType(ConstantPointerNull *) {
1078 return 0;
1079}
1080
1081
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001082ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001083 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001084 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001085}
1086
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001087// destroyConstant - Remove the constant from the constant table...
1088//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001089void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001090 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001091 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001092 destroyConstantImpl();
1093}
1094
1095
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001096//---- UndefValue::get() implementation...
1097//
1098
1099namespace llvm {
1100 // UndefValue does not take extra "value" argument...
1101 template<class ValType>
1102 struct ConstantCreator<UndefValue, Type, ValType> {
1103 static UndefValue *create(const Type *Ty, const ValType &V) {
1104 return new UndefValue(Ty);
1105 }
1106 };
1107
1108 template<>
1109 struct ConvertConstantType<UndefValue, Type> {
1110 static void convert(UndefValue *OldC, const Type *NewTy) {
1111 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001112 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001113 assert(New != OldC && "Didn't replace constant??");
1114 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001115 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001116 }
1117 };
1118}
1119
Chris Lattner69edc982006-09-28 00:35:06 +00001120static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001121
1122static char getValType(UndefValue *) {
1123 return 0;
1124}
1125
1126
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001127UndefValue *UndefValue::get(const Type *Ty) {
1128 // Implicitly locked.
1129 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001130}
1131
1132// destroyConstant - Remove the constant from the constant table.
1133//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001134void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001135 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001136 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001137 destroyConstantImpl();
1138}
1139
Nick Lewycky49f89192009-04-04 07:22:01 +00001140//---- MDNode::get() implementation
1141//
1142
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001143MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Devang Patele059ba6e2009-07-23 01:07:34 +00001144 : MetadataBase(Type::MetadataTy, Value::MDNodeVal) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001145 for (unsigned i = 0; i != NumVals; ++i)
Devang Patele059ba6e2009-07-23 01:07:34 +00001146 Node.push_back(WeakVH(Vals[i]));
Nick Lewycky49f89192009-04-04 07:22:01 +00001147}
1148
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001149void MDNode::Profile(FoldingSetNodeID &ID) const {
1150 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001151 ID.AddPointer(*I);
1152}
1153
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001154//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001155//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001156
Dan Gohmand78c4002008-05-13 00:00:25 +00001157namespace {
1158
Reid Spenceree3c9912006-12-04 05:19:50 +00001159struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001160 typedef SmallVector<unsigned, 4> IndexList;
1161
1162 ExprMapKeyType(unsigned opc,
1163 const std::vector<Constant*> &ops,
1164 unsigned short pred = 0,
1165 const IndexList &inds = IndexList())
1166 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001167 uint16_t opcode;
1168 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001169 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001170 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001171 bool operator==(const ExprMapKeyType& that) const {
1172 return this->opcode == that.opcode &&
1173 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001174 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001175 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001176 }
1177 bool operator<(const ExprMapKeyType & that) const {
1178 return this->opcode < that.opcode ||
1179 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1180 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001181 this->operands < that.operands) ||
1182 (this->opcode == that.opcode && this->predicate == that.predicate &&
1183 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001184 }
1185
1186 bool operator!=(const ExprMapKeyType& that) const {
1187 return !(*this == that);
1188 }
1189};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001190
Dan Gohmand78c4002008-05-13 00:00:25 +00001191}
1192
Chris Lattner189d19f2003-11-21 20:23:48 +00001193namespace llvm {
1194 template<>
1195 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001196 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1197 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001198 if (Instruction::isCast(V.opcode))
1199 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1200 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001201 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001202 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1203 if (V.opcode == Instruction::Select)
1204 return new SelectConstantExpr(V.operands[0], V.operands[1],
1205 V.operands[2]);
1206 if (V.opcode == Instruction::ExtractElement)
1207 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1208 if (V.opcode == Instruction::InsertElement)
1209 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1210 V.operands[2]);
1211 if (V.opcode == Instruction::ShuffleVector)
1212 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1213 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001214 if (V.opcode == Instruction::InsertValue)
1215 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1216 V.indices, Ty);
1217 if (V.opcode == Instruction::ExtractValue)
1218 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001219 if (V.opcode == Instruction::GetElementPtr) {
1220 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001221 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001222 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001223
Reid Spenceree3c9912006-12-04 05:19:50 +00001224 // The compare instructions are weird. We have to encode the predicate
1225 // value and it is combined with the instruction opcode by multiplying
1226 // the opcode by one hundred. We must decode this to get the predicate.
1227 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001228 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001229 V.operands[0], V.operands[1]);
1230 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001231 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1232 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001233 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001234 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001235 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001236 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001237
Chris Lattner189d19f2003-11-21 20:23:48 +00001238 template<>
1239 struct ConvertConstantType<ConstantExpr, Type> {
1240 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1241 Constant *New;
1242 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001243 case Instruction::Trunc:
1244 case Instruction::ZExt:
1245 case Instruction::SExt:
1246 case Instruction::FPTrunc:
1247 case Instruction::FPExt:
1248 case Instruction::UIToFP:
1249 case Instruction::SIToFP:
1250 case Instruction::FPToUI:
1251 case Instruction::FPToSI:
1252 case Instruction::PtrToInt:
1253 case Instruction::IntToPtr:
1254 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001255 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001256 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001257 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001258 case Instruction::Select:
1259 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1260 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001261 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001262 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001263 default:
1264 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001265 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001266 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001267 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001268 break;
1269 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001270 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001271 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001272 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001273 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001274 break;
1275 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001276
Chris Lattner189d19f2003-11-21 20:23:48 +00001277 assert(New != OldC && "Didn't replace constant??");
1278 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001279 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001280 }
1281 };
1282} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001283
1284
Chris Lattner3e650af2004-08-04 04:48:01 +00001285static ExprMapKeyType getValType(ConstantExpr *CE) {
1286 std::vector<Constant*> Operands;
1287 Operands.reserve(CE->getNumOperands());
1288 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1289 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001290 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001291 CE->isCompare() ? CE->getPredicate() : 0,
1292 CE->hasIndices() ?
1293 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001294}
1295
Chris Lattner69edc982006-09-28 00:35:06 +00001296static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1297 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001298
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001299/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001300/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001301static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001302 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001303 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001304 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001305 if (Constant *FC =
1306 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001307 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001308
Vikram S. Adve4c485332002-07-15 18:19:33 +00001309 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001310 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001311 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001312
Owen Anderson61794042009-06-17 20:10:08 +00001313 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001314 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001315}
Reid Spencerf37dc652006-12-05 19:14:13 +00001316
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001317Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001318 Instruction::CastOps opc = Instruction::CastOps(oc);
1319 assert(Instruction::isCast(opc) && "opcode out of range");
1320 assert(C && Ty && "Null arguments to getCast");
1321 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1322
1323 switch (opc) {
1324 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001325 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001326 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001327 case Instruction::Trunc: return getTrunc(C, Ty);
1328 case Instruction::ZExt: return getZExt(C, Ty);
1329 case Instruction::SExt: return getSExt(C, Ty);
1330 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1331 case Instruction::FPExt: return getFPExtend(C, Ty);
1332 case Instruction::UIToFP: return getUIToFP(C, Ty);
1333 case Instruction::SIToFP: return getSIToFP(C, Ty);
1334 case Instruction::FPToUI: return getFPToUI(C, Ty);
1335 case Instruction::FPToSI: return getFPToSI(C, Ty);
1336 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1337 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1338 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001339 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001340 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001341}
1342
Reid Spencer5c140882006-12-04 20:17:56 +00001343Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001344 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001345 return getCast(Instruction::BitCast, C, Ty);
1346 return getCast(Instruction::ZExt, C, Ty);
1347}
1348
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001349Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001350 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001351 return getCast(Instruction::BitCast, C, Ty);
1352 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001353}
1354
1355Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001356 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001357 return getCast(Instruction::BitCast, C, Ty);
1358 return getCast(Instruction::Trunc, C, Ty);
1359}
1360
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001361Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001362 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001363 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001364
Chris Lattner03c49532007-01-15 02:27:26 +00001365 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001366 return getCast(Instruction::PtrToInt, S, Ty);
1367 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001368}
1369
Reid Spencer56521c42006-12-12 00:51:07 +00001370Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1371 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001372 assert(C->getType()->isIntOrIntVector() &&
1373 Ty->isIntOrIntVector() && "Invalid cast");
1374 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1375 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001376 Instruction::CastOps opcode =
1377 (SrcBits == DstBits ? Instruction::BitCast :
1378 (SrcBits > DstBits ? Instruction::Trunc :
1379 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1380 return getCast(opcode, C, Ty);
1381}
1382
1383Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001384 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001385 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001386 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1387 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001388 if (SrcBits == DstBits)
1389 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001390 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001391 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001392 return getCast(opcode, C, Ty);
1393}
1394
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001395Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001396#ifndef NDEBUG
1397 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1398 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1399#endif
1400 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1401 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1402 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1403 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001404 "SrcTy must be larger than DestTy for Trunc!");
1405
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001406 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001407}
1408
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001409Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001410#ifndef NDEBUG
1411 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1412 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1413#endif
1414 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1415 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1416 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1417 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001418 "SrcTy must be smaller than DestTy for SExt!");
1419
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001420 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001421}
1422
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001423Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001424#ifndef NDEBUG
1425 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1426 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1427#endif
1428 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1429 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1430 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1431 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001432 "SrcTy must be smaller than DestTy for ZExt!");
1433
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001434 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001435}
1436
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001437Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001438#ifndef NDEBUG
1439 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1440 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1441#endif
1442 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1443 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1444 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001445 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001446 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001447}
1448
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001449Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001450#ifndef NDEBUG
1451 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1452 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1453#endif
1454 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1455 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1456 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001457 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001458 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001459}
1460
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001461Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001462#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001463 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1464 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001465#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001466 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1467 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1468 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001469 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001470}
1471
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001472Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001473#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001474 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1475 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001476#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001477 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1478 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001479 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001480 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001481}
1482
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001483Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001484#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001485 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1486 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001487#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001488 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1489 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1490 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001491 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001492}
1493
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001494Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001495#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001496 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1497 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001498#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001499 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1500 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1501 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001502 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001503}
1504
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001505Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001506 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001507 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001508 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001509}
1510
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001511Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001512 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001513 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001514 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001515}
1516
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001517Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001518 // BitCast implies a no-op cast of type only. No bits change. However, you
1519 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001520#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001521 const Type *SrcTy = C->getType();
1522 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001523 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001524
1525 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1526 // or nonptr->ptr). For all the other types, the cast is okay if source and
1527 // destination bit widths are identical.
1528 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1529 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001530#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001531 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001532
1533 // It is common to ask for a bitcast of a value to its own type, handle this
1534 // speedily.
1535 if (C->getType() == DstTy) return C;
1536
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001537 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001538}
1539
Chris Lattnerb50d1352003-10-05 00:17:43 +00001540Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001541 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001542 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001543 assert(Opcode >= Instruction::BinaryOpsBegin &&
1544 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001545 "Invalid opcode in binary constant expression");
1546 assert(C1->getType() == C2->getType() &&
1547 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001548
Reid Spencer542964f2007-01-11 18:21:29 +00001549 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001550 if (Constant *FC = ConstantFoldBinaryInstruction(
1551 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001552 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001553
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001554 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001555 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001556
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001557 // Implicitly locked.
1558 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001559}
1560
Reid Spencer266e42b2006-12-23 06:05:41 +00001561Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001562 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001563 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001564 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001565 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1566 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1567 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1568 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1569 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1570 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001571 return getFCmp(predicate, C1, C2);
1572
Nate Begemanc96e2e42008-07-25 17:35:37 +00001573 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1574 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1575 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1576 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001577 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001578 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001579}
1580
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001581Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001582 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1583 if (C1->getType()->isFPOrFPVector()) {
1584 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1585 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1586 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1587 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001588#ifndef NDEBUG
1589 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001590 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001591 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001592 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001593 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001594 assert(C1->getType()->isIntOrIntVector() &&
1595 "Tried to create an integer operation on a non-integer type!");
1596 break;
1597 case Instruction::FAdd:
1598 case Instruction::FSub:
1599 case Instruction::FMul:
1600 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1601 assert(C1->getType()->isFPOrFPVector() &&
1602 "Tried to create a floating-point operation on a "
1603 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001604 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001605 case Instruction::UDiv:
1606 case Instruction::SDiv:
1607 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001608 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001609 "Tried to create an arithmetic operation on a non-arithmetic type!");
1610 break;
1611 case Instruction::FDiv:
1612 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001613 assert(C1->getType()->isFPOrFPVector() &&
1614 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001615 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001616 case Instruction::URem:
1617 case Instruction::SRem:
1618 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001619 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001620 "Tried to create an arithmetic operation on a non-arithmetic type!");
1621 break;
1622 case Instruction::FRem:
1623 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001624 assert(C1->getType()->isFPOrFPVector() &&
1625 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001626 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001627 case Instruction::And:
1628 case Instruction::Or:
1629 case Instruction::Xor:
1630 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001631 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001632 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001633 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001634 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001635 case Instruction::LShr:
1636 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001637 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001638 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001639 "Tried to create a shift operation on a non-integer type!");
1640 break;
1641 default:
1642 break;
1643 }
1644#endif
1645
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001646 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001647}
1648
Reid Spencer266e42b2006-12-23 06:05:41 +00001649Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001650 Constant *C1, Constant *C2) {
1651 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001652 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001653}
1654
Chris Lattner6e415c02004-03-12 05:54:04 +00001655Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001656 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001657 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001658
1659 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001660 if (Constant *SC = ConstantFoldSelectInstruction(
1661 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001662 return SC; // Fold common cases
1663
1664 std::vector<Constant*> argVec(3, C);
1665 argVec[1] = V1;
1666 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001667 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001668
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001669 // Implicitly locked.
1670 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001671}
1672
Chris Lattnerb50d1352003-10-05 00:17:43 +00001673Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001674 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001675 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001676 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1677 Idxs+NumIdx) ==
1678 cast<PointerType>(ReqTy)->getElementType() &&
1679 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001680
Owen Anderson53a52212009-07-13 04:09:18 +00001681 if (Constant *FC = ConstantFoldGetElementPtr(
1682 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001683 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001684
Chris Lattnerb50d1352003-10-05 00:17:43 +00001685 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001686 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001687 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001688 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001689 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001690 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001691 for (unsigned i = 0; i != NumIdx; ++i)
1692 ArgVec.push_back(cast<Constant>(Idxs[i]));
1693 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001694
1695 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001696 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001697}
1698
Chris Lattner302116a2007-01-31 04:40:28 +00001699Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001700 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001701 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001702 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00001703 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001704 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001705 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001706 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001707}
1708
Chris Lattner302116a2007-01-31 04:40:28 +00001709Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001710 unsigned NumIdx) {
1711 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001712}
1713
Chris Lattner302116a2007-01-31 04:40:28 +00001714
Reid Spenceree3c9912006-12-04 05:19:50 +00001715Constant *
1716ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1717 assert(LHS->getType() == RHS->getType());
1718 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1719 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1720
Owen Anderson53a52212009-07-13 04:09:18 +00001721 if (Constant *FC = ConstantFoldCompareInstruction(
1722 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001723 return FC; // Fold a few common cases...
1724
1725 // Look up the constant in the table first to ensure uniqueness
1726 std::vector<Constant*> ArgVec;
1727 ArgVec.push_back(LHS);
1728 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001729 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001730 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001731
1732 // Implicitly locked.
1733 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001734}
1735
1736Constant *
1737ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1738 assert(LHS->getType() == RHS->getType());
1739 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1740
Owen Anderson53a52212009-07-13 04:09:18 +00001741 if (Constant *FC = ConstantFoldCompareInstruction(
1742 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001743 return FC; // Fold a few common cases...
1744
1745 // Look up the constant in the table first to ensure uniqueness
1746 std::vector<Constant*> ArgVec;
1747 ArgVec.push_back(LHS);
1748 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001749 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001750 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001751
1752 // Implicitly locked.
1753 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001754}
1755
Robert Bocchino23004482006-01-10 19:05:34 +00001756Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1757 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001758 if (Constant *FC = ConstantFoldExtractElementInstruction(
1759 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001760 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001761 // Look up the constant in the table first to ensure uniqueness
1762 std::vector<Constant*> ArgVec(1, Val);
1763 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001764 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001765
1766 // Implicitly locked.
1767 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001768}
1769
1770Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001771 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001772 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001773 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001774 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001775 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001776 Val, Idx);
1777}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001778
Robert Bocchinoca27f032006-01-17 20:07:22 +00001779Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1780 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001781 if (Constant *FC = ConstantFoldInsertElementInstruction(
1782 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00001783 return FC; // Fold a few common cases...
1784 // Look up the constant in the table first to ensure uniqueness
1785 std::vector<Constant*> ArgVec(1, Val);
1786 ArgVec.push_back(Elt);
1787 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001788 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001789
1790 // Implicitly locked.
1791 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001792}
1793
1794Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1795 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001796 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001797 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001798 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001799 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001800 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001801 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00001802 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001803}
1804
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001805Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1806 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00001807 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
1808 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001809 return FC; // Fold a few common cases...
1810 // Look up the constant in the table first to ensure uniqueness
1811 std::vector<Constant*> ArgVec(1, V1);
1812 ArgVec.push_back(V2);
1813 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001814 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001815
1816 // Implicitly locked.
1817 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001818}
1819
1820Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1821 Constant *Mask) {
1822 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1823 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00001824
1825 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1826 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1827 const Type *ShufTy = VectorType::get(EltTy, NElts);
1828 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001829}
1830
Dan Gohman12fce772008-05-15 19:50:34 +00001831Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1832 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001833 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001834 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1835 Idxs+NumIdx) == Val->getType() &&
1836 "insertvalue indices invalid!");
1837 assert(Agg->getType() == ReqTy &&
1838 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001839 assert(Agg->getType()->isFirstClassType() &&
1840 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00001841 Constant *FC = ConstantFoldInsertValueInstruction(
1842 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001843 assert(FC && "InsertValue constant expr couldn't be folded!");
1844 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001845}
1846
1847Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001848 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001849 assert(Agg->getType()->isFirstClassType() &&
1850 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001851
Dan Gohman0752bff2008-05-23 00:36:11 +00001852 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00001853#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00001854 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00001855 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00001856#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00001857 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00001858 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
1859}
1860
1861Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001862 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001863 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1864 Idxs+NumIdx) == ReqTy &&
1865 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001866 assert(Agg->getType()->isFirstClassType() &&
1867 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00001868 Constant *FC = ConstantFoldExtractValueInstruction(
1869 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001870 assert(FC && "ExtractValue constant expr couldn't be folded!");
1871 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001872}
1873
1874Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001875 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001876 assert(Agg->getType()->isFirstClassType() &&
1877 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001878
1879 const Type *ReqTy =
1880 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
1881 assert(ReqTy && "extractvalue indices invalid!");
1882 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
1883}
1884
Vikram S. Adve4c485332002-07-15 18:19:33 +00001885// destroyConstant - Remove the constant from the constant table...
1886//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001887void ConstantExpr::destroyConstant() {
1888 // Implicitly locked.
1889 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001890 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001891}
1892
Chris Lattner3cd8c562002-07-30 18:54:25 +00001893const char *ConstantExpr::getOpcodeName() const {
1894 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001895}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001896
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001897//===----------------------------------------------------------------------===//
1898// replaceUsesOfWithOnConstant implementations
1899
Chris Lattner913849b2007-08-21 00:55:23 +00001900/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
1901/// 'From' to be uses of 'To'. This must update the uniquing data structures
1902/// etc.
1903///
1904/// Note that we intentionally replace all uses of From with To here. Consider
1905/// a large array that uses 'From' 1000 times. By handling this case all here,
1906/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
1907/// single invocation handles all 1000 uses. Handling them one at a time would
1908/// work, but would be really slow because it would have to unique each updated
1909/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001910void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001911 Use *U) {
Owen Anderson3d344922009-07-21 20:55:28 +00001912 Constant *Replacement =
1913 getType()->getContext().replaceUsesOfWithOnConstant(this, From, To, U);
1914
1915 if (!Replacement) return;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001916
1917 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001918 assert(Replacement != this && "I didn't contain From!");
1919
Chris Lattner7a1450d2005-10-04 18:13:04 +00001920 // Everyone using this now uses the replacement.
1921 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001922
1923 // Delete the old constant!
1924 destroyConstant();
1925}
1926
Owen Anderson45308b52009-07-27 22:29:26 +00001927static std::vector<Constant*> getValType(ConstantStruct *CS) {
1928 std::vector<Constant*> Elements;
1929 Elements.reserve(CS->getNumOperands());
1930 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1931 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1932 return Elements;
1933}
1934
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001935void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001936 Use *U) {
Owen Anderson45308b52009-07-27 22:29:26 +00001937 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1938 Constant *ToC = cast<Constant>(To);
1939
1940 unsigned OperandToUpdate = U-OperandList;
1941 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1942
1943 std::pair<LLVMContextImpl::StructConstantsTy::MapKey, Constant*> Lookup;
1944 Lookup.first.first = getType();
1945 Lookup.second = this;
1946 std::vector<Constant*> &Values = Lookup.first.second;
1947 Values.reserve(getNumOperands()); // Build replacement struct.
1948
1949
1950 // Fill values with the modified operands of the constant struct. Also,
1951 // compute whether this turns into an all-zeros struct.
1952 bool isAllZeros = false;
1953 if (!ToC->isNullValue()) {
1954 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
1955 Values.push_back(cast<Constant>(O->get()));
1956 } else {
1957 isAllZeros = true;
1958 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1959 Constant *Val = cast<Constant>(O->get());
1960 Values.push_back(Val);
1961 if (isAllZeros) isAllZeros = Val->isNullValue();
1962 }
1963 }
1964 Values[OperandToUpdate] = ToC;
1965
1966 LLVMContext &Context = getType()->getContext();
1967 LLVMContextImpl *pImpl = Context.pImpl;
1968
1969 Constant *Replacement = 0;
1970 if (isAllZeros) {
1971 Replacement = Context.getConstantAggregateZero(getType());
1972 } else {
1973 // Check to see if we have this array type already.
1974 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
1975 bool Exists;
1976 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
1977 pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
1978
1979 if (Exists) {
1980 Replacement = I->second;
1981 } else {
1982 // Okay, the new shape doesn't exist in the system yet. Instead of
1983 // creating a new constant struct, inserting it, replaceallusesof'ing the
1984 // old with the new, then deleting the old... just update the current one
1985 // in place!
1986 pImpl->StructConstants.MoveConstantToNewSlot(this, I);
1987
1988 // Update to the new value.
1989 setOperand(OperandToUpdate, ToC);
1990 return;
1991 }
1992 }
1993
1994 assert(Replacement != this && "I didn't contain From!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001995
Chris Lattner7a1450d2005-10-04 18:13:04 +00001996 // Everyone using this now uses the replacement.
1997 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001998
1999 // Delete the old constant!
2000 destroyConstant();
2001}
2002
Reid Spencerd84d35b2007-02-15 02:26:10 +00002003void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002004 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002005 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2006
2007 std::vector<Constant*> Values;
2008 Values.reserve(getNumOperands()); // Build replacement array...
2009 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2010 Constant *Val = getOperand(i);
2011 if (Val == From) Val = cast<Constant>(To);
2012 Values.push_back(Val);
2013 }
2014
Owen Anderson0348a132009-07-24 00:36:24 +00002015 Constant *Replacement =
2016 getType()->getContext().getConstantVector(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002017 assert(Replacement != this && "I didn't contain From!");
2018
Chris Lattner7a1450d2005-10-04 18:13:04 +00002019 // Everyone using this now uses the replacement.
2020 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002021
2022 // Delete the old constant!
2023 destroyConstant();
2024}
2025
2026void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002027 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002028 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2029 Constant *To = cast<Constant>(ToV);
2030
2031 Constant *Replacement = 0;
2032 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002033 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002034 Constant *Pointer = getOperand(0);
2035 Indices.reserve(getNumOperands()-1);
2036 if (Pointer == From) Pointer = To;
2037
2038 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2039 Constant *Val = getOperand(i);
2040 if (Val == From) Val = To;
2041 Indices.push_back(Val);
2042 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002043 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2044 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002045 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002046 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002047 if (Agg == From) Agg = To;
2048
Dan Gohman1ecaf452008-05-31 00:58:22 +00002049 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002050 Replacement = ConstantExpr::getExtractValue(Agg,
2051 &Indices[0], Indices.size());
2052 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002053 Constant *Agg = getOperand(0);
2054 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002055 if (Agg == From) Agg = To;
2056 if (Val == From) Val = To;
2057
Dan Gohman1ecaf452008-05-31 00:58:22 +00002058 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002059 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2060 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002061 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002062 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002063 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002064 } else if (getOpcode() == Instruction::Select) {
2065 Constant *C1 = getOperand(0);
2066 Constant *C2 = getOperand(1);
2067 Constant *C3 = getOperand(2);
2068 if (C1 == From) C1 = To;
2069 if (C2 == From) C2 = To;
2070 if (C3 == From) C3 = To;
2071 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002072 } else if (getOpcode() == Instruction::ExtractElement) {
2073 Constant *C1 = getOperand(0);
2074 Constant *C2 = getOperand(1);
2075 if (C1 == From) C1 = To;
2076 if (C2 == From) C2 = To;
2077 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002078 } else if (getOpcode() == Instruction::InsertElement) {
2079 Constant *C1 = getOperand(0);
2080 Constant *C2 = getOperand(1);
2081 Constant *C3 = getOperand(1);
2082 if (C1 == From) C1 = To;
2083 if (C2 == From) C2 = To;
2084 if (C3 == From) C3 = To;
2085 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2086 } else if (getOpcode() == Instruction::ShuffleVector) {
2087 Constant *C1 = getOperand(0);
2088 Constant *C2 = getOperand(1);
2089 Constant *C3 = getOperand(2);
2090 if (C1 == From) C1 = To;
2091 if (C2 == From) C2 = To;
2092 if (C3 == From) C3 = To;
2093 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002094 } else if (isCompare()) {
2095 Constant *C1 = getOperand(0);
2096 Constant *C2 = getOperand(1);
2097 if (C1 == From) C1 = To;
2098 if (C2 == From) C2 = To;
2099 if (getOpcode() == Instruction::ICmp)
2100 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002101 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002102 assert(getOpcode() == Instruction::FCmp);
2103 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002104 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002105 } else if (getNumOperands() == 2) {
2106 Constant *C1 = getOperand(0);
2107 Constant *C2 = getOperand(1);
2108 if (C1 == From) C1 = To;
2109 if (C2 == From) C2 = To;
2110 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2111 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002112 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002113 return;
2114 }
2115
2116 assert(Replacement != this && "I didn't contain From!");
2117
Chris Lattner7a1450d2005-10-04 18:13:04 +00002118 // Everyone using this now uses the replacement.
2119 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002120
2121 // Delete the old constant!
2122 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002123}
Nick Lewycky49f89192009-04-04 07:22:01 +00002124
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002125void MDNode::replaceElement(Value *From, Value *To) {
2126 SmallVector<Value*, 4> Values;
2127 Values.reserve(getNumElements()); // Build replacement array...
2128 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2129 Value *Val = getElement(i);
2130 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002131 Values.push_back(Val);
2132 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002133
Owen Anderson4118dde2009-07-16 23:44:30 +00002134 MDNode *Replacement =
2135 getType()->getContext().getMDNode(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002136 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002137
Nick Lewycky49f89192009-04-04 07:22:01 +00002138 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewycky49f89192009-04-04 07:22:01 +00002139}