blob: 5bbf087e486de5f30661b7b85b713022561b616b [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 Lattnerc559a9f2009-11-01 03:03:03 +000010// This file implements the Constant* classes.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner78683a72009-08-23 04:02:03 +000015#include "LLVMContextImpl.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"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Dan Gohman7d82e132009-07-18 01:49:22 +000021#include "llvm/Operator.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000022#include "llvm/ADT/FoldingSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/ADT/StringExtras.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000024#include "llvm/ADT/StringMap.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000025#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000026#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000028#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000029#include "llvm/Support/MathExtras.h"
Chris Lattner78683a72009-08-23 04:02:03 +000030#include "llvm/Support/raw_ostream.h"
Dan Gohman7190d482009-09-10 23:37:55 +000031#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000034#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000035#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000036using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000037
Chris Lattner2f7c9632001-06-06 20:29:01 +000038//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000039// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000040//===----------------------------------------------------------------------===//
41
Owen Anderson5a1acd92009-07-31 20:28:14 +000042// Constructor to create a '0' constant of arbitrary type...
43static const uint64_t zero[2] = {0, 0};
Chris Lattner74eb5d72009-10-30 22:33:29 +000044Constant *Constant::getNullValue(const Type *Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +000045 switch (Ty->getTypeID()) {
46 case Type::IntegerTyID:
47 return ConstantInt::get(Ty, 0);
48 case Type::FloatTyID:
49 return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
50 case Type::DoubleTyID:
51 return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
52 case Type::X86_FP80TyID:
53 return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
54 case Type::FP128TyID:
55 return ConstantFP::get(Ty->getContext(),
56 APFloat(APInt(128, 2, zero), true));
57 case Type::PPC_FP128TyID:
58 return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
59 case Type::PointerTyID:
60 return ConstantPointerNull::get(cast<PointerType>(Ty));
61 case Type::StructTyID:
62 case Type::ArrayTyID:
63 case Type::VectorTyID:
64 return ConstantAggregateZero::get(Ty);
65 default:
66 // Function, Label, or Opaque type?
67 assert(!"Cannot create a null constant of that type!");
68 return 0;
69 }
70}
71
Chris Lattner74eb5d72009-10-30 22:33:29 +000072Constant* Constant::getIntegerValue(const Type *Ty, const APInt &V) {
Dan Gohmanf011f5a2009-08-03 22:07:33 +000073 const Type *ScalarTy = Ty->getScalarType();
74
75 // Create the base integer constant.
76 Constant *C = ConstantInt::get(Ty->getContext(), V);
77
78 // Convert an integer to a pointer, if necessary.
79 if (const PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
80 C = ConstantExpr::getIntToPtr(C, PTy);
81
82 // Broadcast a scalar to a vector, if necessary.
83 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
84 C = ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
85
86 return C;
87}
88
Chris Lattner74eb5d72009-10-30 22:33:29 +000089Constant* Constant::getAllOnesValue(const Type *Ty) {
90 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
Owen Anderson5a1acd92009-07-31 20:28:14 +000091 return ConstantInt::get(Ty->getContext(),
92 APInt::getAllOnesValue(ITy->getBitWidth()));
93
94 std::vector<Constant*> Elts;
Chris Lattner74eb5d72009-10-30 22:33:29 +000095 const VectorType *VTy = cast<VectorType>(Ty);
Owen Anderson5a1acd92009-07-31 20:28:14 +000096 Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
97 assert(Elts[0] && "Not a vector integer type!");
98 return cast<ConstantVector>(ConstantVector::get(Elts));
99}
100
Chris Lattner3462ae32001-12-03 22:26:30 +0000101void Constant::destroyConstantImpl() {
102 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +0000103 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +0000104 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +0000105 // but they don't know that. Because we only find out when the CPV is
106 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +0000107 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +0000108 //
109 while (!use_empty()) {
110 Value *V = use_back();
111#ifndef NDEBUG // Only in -g mode...
Chris Lattner78683a72009-08-23 04:02:03 +0000112 if (!isa<Constant>(V)) {
David Greene1e27a132010-01-05 01:29:19 +0000113 dbgs() << "While deleting: " << *this
Chris Lattner78683a72009-08-23 04:02:03 +0000114 << "\n\nUse still stuck around after Def is destroyed: "
115 << *V << "\n\n";
116 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000117#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000118 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +0000119 Constant *CV = cast<Constant>(V);
120 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000121
122 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000123 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +0000124 }
125
126 // Value has no outstanding references it is safe to delete it now...
127 delete this;
Chris Lattner38569342001-10-01 20:11:19 +0000128}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129
Chris Lattner23dd1f62006-10-20 00:27:06 +0000130/// canTrap - Return true if evaluation of this constant could trap. This is
131/// true for things like constant expressions that could divide by zero.
132bool Constant::canTrap() const {
133 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
134 // The only thing that could possibly trap are constant exprs.
135 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
136 if (!CE) return false;
137
138 // ConstantExpr traps if any operands can trap.
139 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattnera91a5632009-10-28 05:14:34 +0000140 if (CE->getOperand(i)->canTrap())
Chris Lattner23dd1f62006-10-20 00:27:06 +0000141 return true;
142
143 // Otherwise, only specific operations can trap.
144 switch (CE->getOpcode()) {
145 default:
146 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000147 case Instruction::UDiv:
148 case Instruction::SDiv:
149 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +0000150 case Instruction::URem:
151 case Instruction::SRem:
152 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +0000153 // Div and rem can trap if the RHS is not known to be non-zero.
Chris Lattnera91a5632009-10-28 05:14:34 +0000154 if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
Chris Lattner23dd1f62006-10-20 00:27:06 +0000155 return true;
156 return false;
157 }
158}
159
Chris Lattner253bc772009-11-01 18:11:50 +0000160/// isConstantUsed - Return true if the constant has users other than constant
161/// exprs and other dangling things.
162bool Constant::isConstantUsed() const {
Gabor Greifc78d7202010-03-25 23:06:16 +0000163 for (const_use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Chris Lattner253bc772009-11-01 18:11:50 +0000164 const Constant *UC = dyn_cast<Constant>(*UI);
165 if (UC == 0 || isa<GlobalValue>(UC))
166 return true;
167
168 if (UC->isConstantUsed())
169 return true;
170 }
171 return false;
172}
173
174
Chris Lattner4565ef52009-07-22 00:05:44 +0000175
176/// getRelocationInfo - This method classifies the entry according to
177/// whether or not it may generate a relocation entry. This must be
178/// conservative, so if it might codegen to a relocatable entry, it should say
179/// so. The return values are:
180///
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000181/// NoRelocation: This constant pool entry is guaranteed to never have a
182/// relocation applied to it (because it holds a simple constant like
183/// '4').
184/// LocalRelocation: This entry has relocations, but the entries are
185/// guaranteed to be resolvable by the static linker, so the dynamic
186/// linker will never see them.
187/// GlobalRelocations: This entry may have arbitrary relocations.
Chris Lattner4565ef52009-07-22 00:05:44 +0000188///
189/// FIXME: This really should not be in VMCore.
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000190Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
191 if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Chris Lattner4565ef52009-07-22 00:05:44 +0000192 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000193 return LocalRelocation; // Local to this file/library.
194 return GlobalRelocations; // Global reference.
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000195 }
Chris Lattner4565ef52009-07-22 00:05:44 +0000196
Chris Lattner2cb85b42009-10-28 04:12:16 +0000197 if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
198 return BA->getFunction()->getRelocationInfo();
199
Chris Lattnera7cfc432010-01-03 18:09:40 +0000200 // While raw uses of blockaddress need to be relocated, differences between
201 // two of them don't when they are for labels in the same function. This is a
202 // common idiom when creating a table for the indirect goto extension, so we
203 // handle it efficiently here.
204 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
205 if (CE->getOpcode() == Instruction::Sub) {
206 ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
207 ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
208 if (LHS && RHS &&
209 LHS->getOpcode() == Instruction::PtrToInt &&
210 RHS->getOpcode() == Instruction::PtrToInt &&
211 isa<BlockAddress>(LHS->getOperand(0)) &&
212 isa<BlockAddress>(RHS->getOperand(0)) &&
213 cast<BlockAddress>(LHS->getOperand(0))->getFunction() ==
214 cast<BlockAddress>(RHS->getOperand(0))->getFunction())
215 return NoRelocation;
216 }
217
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000218 PossibleRelocationsTy Result = NoRelocation;
Evan Chengf9e003b2007-03-08 00:59:12 +0000219 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattnera91a5632009-10-28 05:14:34 +0000220 Result = std::max(Result,
221 cast<Constant>(getOperand(i))->getRelocationInfo());
Chris Lattner4565ef52009-07-22 00:05:44 +0000222
223 return Result;
Evan Chengf9e003b2007-03-08 00:59:12 +0000224}
225
Chris Lattner4565ef52009-07-22 00:05:44 +0000226
Chris Lattner2105d662008-07-10 00:28:11 +0000227/// getVectorElements - This method, which is only valid on constant of vector
228/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000229/// This handles breaking down a vector undef into undef elements, etc. For
230/// constant exprs and other cases we can't handle, we return an empty vector.
Chris Lattnerf5edeeb2010-02-01 20:48:08 +0000231void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000232 assert(getType()->isVectorTy() && "Not a vector constant!");
Chris Lattner2105d662008-07-10 00:28:11 +0000233
234 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
235 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
236 Elts.push_back(CV->getOperand(i));
237 return;
238 }
239
240 const VectorType *VT = cast<VectorType>(getType());
241 if (isa<ConstantAggregateZero>(this)) {
242 Elts.assign(VT->getNumElements(),
Owen Anderson5a1acd92009-07-31 20:28:14 +0000243 Constant::getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000244 return;
245 }
246
Chris Lattnerc5098a22008-07-14 05:10:41 +0000247 if (isa<UndefValue>(this)) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000248 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000249 return;
250 }
251
252 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000253}
254
255
256
Chris Lattner2f7c9632001-06-06 20:29:01 +0000257//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000258// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000259//===----------------------------------------------------------------------===//
260
Reid Spencerb31bffe2007-02-26 23:54:03 +0000261ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000262 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000263 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000264}
265
Owen Anderson23a204d2009-07-31 17:39:07 +0000266ConstantInt* ConstantInt::getTrue(LLVMContext &Context) {
267 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000268 if (!pImpl->TheTrueVal)
269 pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
270 return pImpl->TheTrueVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000271}
272
273ConstantInt* ConstantInt::getFalse(LLVMContext &Context) {
274 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000275 if (!pImpl->TheFalseVal)
276 pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
277 return pImpl->TheFalseVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000278}
279
280
Owen Andersonedb4a702009-07-24 23:12:02 +0000281// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
282// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
283// operator== and operator!= to ensure that the DenseMap doesn't attempt to
284// compare APInt's of different widths, which would violate an APInt class
285// invariant which generates an assertion.
286ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) {
287 // Get the corresponding integer type for the bit width of the value.
Owen Anderson55f1c092009-08-13 21:58:54 +0000288 const IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
Owen Andersonedb4a702009-07-24 23:12:02 +0000289 // get an existing value or the insertion position
290 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Owen Andersonedb4a702009-07-24 23:12:02 +0000291 ConstantInt *&Slot = Context.pImpl->IntConstants[Key];
Owen Anderson5dab84c2009-10-19 20:11:52 +0000292 if (!Slot) Slot = new ConstantInt(ITy, V);
293 return Slot;
Owen Andersonedb4a702009-07-24 23:12:02 +0000294}
295
296Constant* ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) {
297 Constant *C = get(cast<IntegerType>(Ty->getScalarType()),
298 V, isSigned);
299
300 // For vectors, broadcast the value.
301 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000302 return ConstantVector::get(
Owen Andersonedb4a702009-07-24 23:12:02 +0000303 std::vector<Constant *>(VTy->getNumElements(), C));
304
305 return C;
306}
307
308ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V,
309 bool isSigned) {
310 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
311}
312
313ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) {
314 return get(Ty, V, true);
315}
316
317Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) {
318 return get(Ty, V, true);
319}
320
321Constant* ConstantInt::get(const Type* Ty, const APInt& V) {
322 ConstantInt *C = get(Ty->getContext(), V);
323 assert(C->getType() == Ty->getScalarType() &&
324 "ConstantInt type doesn't match the type implied by its value!");
325
326 // For vectors, broadcast the value.
327 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000328 return ConstantVector::get(
Owen Andersonedb4a702009-07-24 23:12:02 +0000329 std::vector<Constant *>(VTy->getNumElements(), C));
330
331 return C;
332}
333
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000334ConstantInt* ConstantInt::get(const IntegerType* Ty, StringRef Str,
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000335 uint8_t radix) {
336 return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
337}
338
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000339//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000340// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000341//===----------------------------------------------------------------------===//
342
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000343static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
Chris Lattnerfdd87902009-10-05 05:54:46 +0000344 if (Ty->isFloatTy())
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000345 return &APFloat::IEEEsingle;
Chris Lattnerfdd87902009-10-05 05:54:46 +0000346 if (Ty->isDoubleTy())
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000347 return &APFloat::IEEEdouble;
Chris Lattnerfdd87902009-10-05 05:54:46 +0000348 if (Ty->isX86_FP80Ty())
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000349 return &APFloat::x87DoubleExtended;
Chris Lattnerfdd87902009-10-05 05:54:46 +0000350 else if (Ty->isFP128Ty())
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000351 return &APFloat::IEEEquad;
352
Chris Lattnerfdd87902009-10-05 05:54:46 +0000353 assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000354 return &APFloat::PPCDoubleDouble;
355}
356
Owen Anderson69c464d2009-07-27 20:59:43 +0000357/// get() - This returns a constant fp for the specified value in the
358/// specified type. This should only be used for simple constant values like
359/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
360Constant* ConstantFP::get(const Type* Ty, double V) {
361 LLVMContext &Context = Ty->getContext();
362
363 APFloat FV(V);
364 bool ignored;
365 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
366 APFloat::rmNearestTiesToEven, &ignored);
367 Constant *C = get(Context, FV);
368
369 // For vectors, broadcast the value.
370 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000371 return ConstantVector::get(
Owen Anderson69c464d2009-07-27 20:59:43 +0000372 std::vector<Constant *>(VTy->getNumElements(), C));
373
374 return C;
375}
376
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000377
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000378Constant* ConstantFP::get(const Type* Ty, StringRef Str) {
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000379 LLVMContext &Context = Ty->getContext();
380
381 APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
382 Constant *C = get(Context, FV);
383
384 // For vectors, broadcast the value.
385 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
386 return ConstantVector::get(
387 std::vector<Constant *>(VTy->getNumElements(), C));
388
389 return C;
390}
391
392
Owen Anderson69c464d2009-07-27 20:59:43 +0000393ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
394 LLVMContext &Context = Ty->getContext();
Owen Anderson5a1acd92009-07-31 20:28:14 +0000395 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
Owen Anderson69c464d2009-07-27 20:59:43 +0000396 apf.changeSign();
397 return get(Context, apf);
398}
399
400
401Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000402 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Duncan Sands9dff9be2010-02-15 16:12:20 +0000403 if (PTy->getElementType()->isFloatingPointTy()) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000404 std::vector<Constant*> zeros(PTy->getNumElements(),
405 getNegativeZero(PTy->getElementType()));
Owen Anderson4aa32952009-07-28 21:19:26 +0000406 return ConstantVector::get(PTy, zeros);
Owen Anderson69c464d2009-07-27 20:59:43 +0000407 }
408
Duncan Sands9dff9be2010-02-15 16:12:20 +0000409 if (Ty->isFloatingPointTy())
Owen Anderson69c464d2009-07-27 20:59:43 +0000410 return getNegativeZero(Ty);
411
Owen Anderson5a1acd92009-07-31 20:28:14 +0000412 return Constant::getNullValue(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000413}
414
415
416// ConstantFP accessors.
417ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
418 DenseMapAPFloatKeyInfo::KeyTy Key(V);
419
420 LLVMContextImpl* pImpl = Context.pImpl;
421
Owen Anderson69c464d2009-07-27 20:59:43 +0000422 ConstantFP *&Slot = pImpl->FPConstants[Key];
Owen Anderson69c464d2009-07-27 20:59:43 +0000423
424 if (!Slot) {
Owen Anderson5dab84c2009-10-19 20:11:52 +0000425 const Type *Ty;
426 if (&V.getSemantics() == &APFloat::IEEEsingle)
427 Ty = Type::getFloatTy(Context);
428 else if (&V.getSemantics() == &APFloat::IEEEdouble)
429 Ty = Type::getDoubleTy(Context);
430 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
431 Ty = Type::getX86_FP80Ty(Context);
432 else if (&V.getSemantics() == &APFloat::IEEEquad)
433 Ty = Type::getFP128Ty(Context);
434 else {
435 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
436 "Unknown FP format");
437 Ty = Type::getPPC_FP128Ty(Context);
Owen Anderson69c464d2009-07-27 20:59:43 +0000438 }
Owen Anderson5dab84c2009-10-19 20:11:52 +0000439 Slot = new ConstantFP(Ty, V);
Owen Anderson69c464d2009-07-27 20:59:43 +0000440 }
441
442 return Slot;
443}
444
Dan Gohman394468d2009-09-25 23:40:21 +0000445ConstantFP *ConstantFP::getInfinity(const Type *Ty, bool Negative) {
Dan Gohmanfeb50212009-09-25 23:00:48 +0000446 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty);
447 return ConstantFP::get(Ty->getContext(),
448 APFloat::getInf(Semantics, Negative));
449}
450
Dale Johannesend246b2c2007-08-30 00:23:21 +0000451ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
452 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000453 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
454 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000455}
456
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000457bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000458 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000459}
460
Dale Johannesend246b2c2007-08-30 00:23:21 +0000461bool ConstantFP::isExactlyValue(const APFloat& V) const {
462 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000463}
464
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000465//===----------------------------------------------------------------------===//
466// ConstantXXX Classes
467//===----------------------------------------------------------------------===//
468
469
Chris Lattner3462ae32001-12-03 22:26:30 +0000470ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000471 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000472 : Constant(T, ConstantArrayVal,
473 OperandTraits<ConstantArray>::op_end(this) - V.size(),
474 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000475 assert(V.size() == T->getNumElements() &&
476 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000477 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000478 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
479 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000480 Constant *C = *I;
Nick Lewyckyae4617c2009-10-03 19:30:43 +0000481 assert(C->getType() == T->getElementType() &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000482 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000483 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000484 }
485}
486
Owen Andersonc2c79322009-07-28 18:32:17 +0000487Constant *ConstantArray::get(const ArrayType *Ty,
488 const std::vector<Constant*> &V) {
Jeffrey Yasskin8ce67f82009-09-30 21:08:08 +0000489 for (unsigned i = 0, e = V.size(); i != e; ++i) {
490 assert(V[i]->getType() == Ty->getElementType() &&
491 "Wrong type in array element initializer");
492 }
Owen Andersonc2c79322009-07-28 18:32:17 +0000493 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
494 // If this is an all-zero array, return a ConstantAggregateZero object
495 if (!V.empty()) {
496 Constant *C = V[0];
Chris Lattner09660c92009-12-30 20:25:09 +0000497 if (!C->isNullValue())
Owen Andersonc2c79322009-07-28 18:32:17 +0000498 return pImpl->ArrayConstants.getOrCreate(Ty, V);
Chris Lattner09660c92009-12-30 20:25:09 +0000499
Owen Andersonc2c79322009-07-28 18:32:17 +0000500 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattner09660c92009-12-30 20:25:09 +0000501 if (V[i] != C)
Owen Andersonc2c79322009-07-28 18:32:17 +0000502 return pImpl->ArrayConstants.getOrCreate(Ty, V);
Owen Andersonc2c79322009-07-28 18:32:17 +0000503 }
504
Owen Andersonb292b8c2009-07-30 23:03:37 +0000505 return ConstantAggregateZero::get(Ty);
Owen Andersonc2c79322009-07-28 18:32:17 +0000506}
507
508
509Constant* ConstantArray::get(const ArrayType* T, Constant* const* Vals,
510 unsigned NumVals) {
511 // FIXME: make this the primary ctor method.
512 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
513}
514
515/// ConstantArray::get(const string&) - Return an array that is initialized to
516/// contain the specified string. If length is zero then a null terminator is
517/// added to the specified string so that it may be used in a natural way.
518/// Otherwise, the length parameter specifies how much of the string to use
519/// and it won't be null terminated.
520///
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000521Constant* ConstantArray::get(LLVMContext &Context, StringRef Str,
Owen Anderson55f1c092009-08-13 21:58:54 +0000522 bool AddNull) {
Owen Andersonc2c79322009-07-28 18:32:17 +0000523 std::vector<Constant*> ElementVals;
Benjamin Kramer3d4af4e2010-08-01 11:43:26 +0000524 ElementVals.reserve(Str.size() + size_t(AddNull));
Owen Andersonc2c79322009-07-28 18:32:17 +0000525 for (unsigned i = 0; i < Str.size(); ++i)
Owen Anderson55f1c092009-08-13 21:58:54 +0000526 ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), Str[i]));
Owen Andersonc2c79322009-07-28 18:32:17 +0000527
528 // Add a null terminator to the string...
529 if (AddNull) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000530 ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0));
Owen Andersonc2c79322009-07-28 18:32:17 +0000531 }
532
Owen Anderson55f1c092009-08-13 21:58:54 +0000533 ArrayType *ATy = ArrayType::get(Type::getInt8Ty(Context), ElementVals.size());
Owen Andersonc2c79322009-07-28 18:32:17 +0000534 return get(ATy, ElementVals);
535}
536
537
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000538
Chris Lattner3462ae32001-12-03 22:26:30 +0000539ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000540 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000541 : Constant(T, ConstantStructVal,
542 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
543 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000544 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000545 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000546 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000547 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
548 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000549 Constant *C = *I;
Nick Lewyckyae4617c2009-10-03 19:30:43 +0000550 assert(C->getType() == T->getElementType(I-V.begin()) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000551 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000552 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000553 }
554}
555
Owen Anderson45308b52009-07-27 22:29:26 +0000556// ConstantStruct accessors.
557Constant* ConstantStruct::get(const StructType* T,
558 const std::vector<Constant*>& V) {
559 LLVMContextImpl* pImpl = T->getContext().pImpl;
560
561 // Create a ConstantAggregateZero value if all elements are zeros...
562 for (unsigned i = 0, e = V.size(); i != e; ++i)
563 if (!V[i]->isNullValue())
Owen Anderson45308b52009-07-27 22:29:26 +0000564 return pImpl->StructConstants.getOrCreate(T, V);
565
Owen Andersonb292b8c2009-07-30 23:03:37 +0000566 return ConstantAggregateZero::get(T);
Owen Anderson45308b52009-07-27 22:29:26 +0000567}
568
Owen Anderson03cb69f2009-08-05 23:16:16 +0000569Constant* ConstantStruct::get(LLVMContext &Context,
570 const std::vector<Constant*>& V, bool packed) {
Owen Anderson45308b52009-07-27 22:29:26 +0000571 std::vector<const Type*> StructEls;
572 StructEls.reserve(V.size());
573 for (unsigned i = 0, e = V.size(); i != e; ++i)
574 StructEls.push_back(V[i]->getType());
Owen Anderson03cb69f2009-08-05 23:16:16 +0000575 return get(StructType::get(Context, StructEls, packed), V);
Owen Anderson45308b52009-07-27 22:29:26 +0000576}
577
Owen Anderson03cb69f2009-08-05 23:16:16 +0000578Constant* ConstantStruct::get(LLVMContext &Context,
579 Constant* const *Vals, unsigned NumVals,
Owen Anderson45308b52009-07-27 22:29:26 +0000580 bool Packed) {
581 // FIXME: make this the primary ctor method.
Owen Anderson03cb69f2009-08-05 23:16:16 +0000582 return get(Context, std::vector<Constant*>(Vals, Vals+NumVals), Packed);
Owen Anderson45308b52009-07-27 22:29:26 +0000583}
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000584
Reid Spencerd84d35b2007-02-15 02:26:10 +0000585ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000586 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000587 : Constant(T, ConstantVectorVal,
588 OperandTraits<ConstantVector>::op_end(this) - V.size(),
589 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000590 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000591 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
592 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000593 Constant *C = *I;
Nick Lewyckyae4617c2009-10-03 19:30:43 +0000594 assert(C->getType() == T->getElementType() &&
Dan Gohman30978072007-05-24 14:36:04 +0000595 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000596 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000597 }
598}
599
Owen Anderson4aa32952009-07-28 21:19:26 +0000600// ConstantVector accessors.
601Constant* ConstantVector::get(const VectorType* T,
602 const std::vector<Constant*>& V) {
603 assert(!V.empty() && "Vectors can't be empty");
604 LLVMContext &Context = T->getContext();
605 LLVMContextImpl *pImpl = Context.pImpl;
606
607 // If this is an all-undef or alll-zero vector, return a
608 // ConstantAggregateZero or UndefValue.
609 Constant *C = V[0];
610 bool isZero = C->isNullValue();
611 bool isUndef = isa<UndefValue>(C);
612
613 if (isZero || isUndef) {
614 for (unsigned i = 1, e = V.size(); i != e; ++i)
615 if (V[i] != C) {
616 isZero = isUndef = false;
617 break;
618 }
619 }
620
621 if (isZero)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000622 return ConstantAggregateZero::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +0000623 if (isUndef)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000624 return UndefValue::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +0000625
Owen Anderson4aa32952009-07-28 21:19:26 +0000626 return pImpl->VectorConstants.getOrCreate(T, V);
627}
628
629Constant* ConstantVector::get(const std::vector<Constant*>& V) {
630 assert(!V.empty() && "Cannot infer type if V is empty");
631 return get(VectorType::get(V.front()->getType(),V.size()), V);
632}
633
634Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) {
635 // FIXME: make this the primary ctor method.
636 return get(std::vector<Constant*>(Vals, Vals+NumVals));
637}
638
Dan Gohman4ab44202009-12-18 02:58:50 +0000639Constant* ConstantExpr::getNSWNeg(Constant* C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000640 assert(C->getType()->isIntOrIntVectorTy() &&
Dan Gohman4ab44202009-12-18 02:58:50 +0000641 "Cannot NEG a nonintegral value!");
642 return getNSWSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
643}
644
Duncan Sandsfa5f5962010-02-02 12:53:04 +0000645Constant* ConstantExpr::getNUWNeg(Constant* C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000646 assert(C->getType()->isIntOrIntVectorTy() &&
Duncan Sandsfa5f5962010-02-02 12:53:04 +0000647 "Cannot NEG a nonintegral value!");
648 return getNUWSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
649}
650
Dan Gohmane4ca02d2009-09-03 23:34:49 +0000651Constant* ConstantExpr::getNSWAdd(Constant* C1, Constant* C2) {
Dan Gohman1b849082009-09-07 23:54:19 +0000652 return getTy(C1->getType(), Instruction::Add, C1, C2,
653 OverflowingBinaryOperator::NoSignedWrap);
Dan Gohmane4ca02d2009-09-03 23:34:49 +0000654}
655
Duncan Sandsfa5f5962010-02-02 12:53:04 +0000656Constant* ConstantExpr::getNUWAdd(Constant* C1, Constant* C2) {
657 return getTy(C1->getType(), Instruction::Add, C1, C2,
658 OverflowingBinaryOperator::NoUnsignedWrap);
659}
660
Duncan Sands129de482009-09-26 15:35:35 +0000661Constant* ConstantExpr::getNSWSub(Constant* C1, Constant* C2) {
662 return getTy(C1->getType(), Instruction::Sub, C1, C2,
663 OverflowingBinaryOperator::NoSignedWrap);
664}
665
Duncan Sandsfa5f5962010-02-02 12:53:04 +0000666Constant* ConstantExpr::getNUWSub(Constant* C1, Constant* C2) {
667 return getTy(C1->getType(), Instruction::Sub, C1, C2,
Dan Gohmande047f22010-02-01 16:38:14 +0000668 OverflowingBinaryOperator::NoUnsignedWrap);
669}
670
Dan Gohman4e3b29e2009-12-18 03:10:26 +0000671Constant* ConstantExpr::getNSWMul(Constant* C1, Constant* C2) {
672 return getTy(C1->getType(), Instruction::Mul, C1, C2,
673 OverflowingBinaryOperator::NoSignedWrap);
674}
675
Duncan Sandsfa5f5962010-02-02 12:53:04 +0000676Constant* ConstantExpr::getNUWMul(Constant* C1, Constant* C2) {
677 return getTy(C1->getType(), Instruction::Mul, C1, C2,
678 OverflowingBinaryOperator::NoUnsignedWrap);
679}
680
Dan Gohmane4ca02d2009-09-03 23:34:49 +0000681Constant* ConstantExpr::getExactSDiv(Constant* C1, Constant* C2) {
Dan Gohman1b849082009-09-07 23:54:19 +0000682 return getTy(C1->getType(), Instruction::SDiv, C1, C2,
683 SDivOperator::IsExact);
Dan Gohmane4ca02d2009-09-03 23:34:49 +0000684}
685
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000686// Utility function for determining if a ConstantExpr is a CastOp or not. This
687// can't be inline because we don't want to #include Instruction.h into
688// Constant.h
689bool ConstantExpr::isCast() const {
690 return Instruction::isCast(getOpcode());
691}
692
Reid Spenceree3c9912006-12-04 05:19:50 +0000693bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000694 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000695}
696
Dan Gohman7190d482009-09-10 23:37:55 +0000697bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
698 if (getOpcode() != Instruction::GetElementPtr) return false;
699
700 gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
Oscar Fuentes40b31ad2010-08-02 06:00:15 +0000701 User::const_op_iterator OI = llvm::next(this->op_begin());
Dan Gohman7190d482009-09-10 23:37:55 +0000702
703 // Skip the first index, as it has no static limit.
704 ++GEPI;
705 ++OI;
706
707 // The remaining indices must be compile-time known integers within the
708 // bounds of the corresponding notional static array types.
709 for (; GEPI != E; ++GEPI, ++OI) {
710 ConstantInt *CI = dyn_cast<ConstantInt>(*OI);
711 if (!CI) return false;
712 if (const ArrayType *ATy = dyn_cast<ArrayType>(*GEPI))
713 if (CI->getValue().getActiveBits() > 64 ||
714 CI->getZExtValue() >= ATy->getNumElements())
715 return false;
716 }
717
718 // All the indices checked out.
719 return true;
720}
721
Dan Gohman1ecaf452008-05-31 00:58:22 +0000722bool ConstantExpr::hasIndices() const {
723 return getOpcode() == Instruction::ExtractValue ||
724 getOpcode() == Instruction::InsertValue;
725}
726
727const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
728 if (const ExtractValueConstantExpr *EVCE =
729 dyn_cast<ExtractValueConstantExpr>(this))
730 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000731
732 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000733}
734
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000735unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000736 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000737 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000738 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000739}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000740
Chris Lattner7c1018a2006-07-14 19:37:40 +0000741/// getWithOperandReplaced - Return a constant expression identical to this
742/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000743Constant *
744ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000745 assert(OpNo < getNumOperands() && "Operand num is out of range!");
746 assert(Op->getType() == getOperand(OpNo)->getType() &&
747 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000748 if (getOperand(OpNo) == Op)
749 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000750
Chris Lattner227816342006-07-14 22:20:01 +0000751 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000752 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000753 case Instruction::Trunc:
754 case Instruction::ZExt:
755 case Instruction::SExt:
756 case Instruction::FPTrunc:
757 case Instruction::FPExt:
758 case Instruction::UIToFP:
759 case Instruction::SIToFP:
760 case Instruction::FPToUI:
761 case Instruction::FPToSI:
762 case Instruction::PtrToInt:
763 case Instruction::IntToPtr:
764 case Instruction::BitCast:
765 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000766 case Instruction::Select:
767 Op0 = (OpNo == 0) ? Op : getOperand(0);
768 Op1 = (OpNo == 1) ? Op : getOperand(1);
769 Op2 = (OpNo == 2) ? Op : getOperand(2);
770 return ConstantExpr::getSelect(Op0, Op1, Op2);
771 case Instruction::InsertElement:
772 Op0 = (OpNo == 0) ? Op : getOperand(0);
773 Op1 = (OpNo == 1) ? Op : getOperand(1);
774 Op2 = (OpNo == 2) ? Op : getOperand(2);
775 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
776 case Instruction::ExtractElement:
777 Op0 = (OpNo == 0) ? Op : getOperand(0);
778 Op1 = (OpNo == 1) ? Op : getOperand(1);
779 return ConstantExpr::getExtractElement(Op0, Op1);
780 case Instruction::ShuffleVector:
781 Op0 = (OpNo == 0) ? Op : getOperand(0);
782 Op1 = (OpNo == 1) ? Op : getOperand(1);
783 Op2 = (OpNo == 2) ? Op : getOperand(2);
784 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000785 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000786 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000787 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000788 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000789 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000790 if (OpNo == 0)
Dan Gohman1b849082009-09-07 23:54:19 +0000791 return cast<GEPOperator>(this)->isInBounds() ?
792 ConstantExpr::getInBoundsGetElementPtr(Op, &Ops[0], Ops.size()) :
793 ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000794 Ops[OpNo-1] = Op;
Dan Gohman1b849082009-09-07 23:54:19 +0000795 return cast<GEPOperator>(this)->isInBounds() ?
Chris Lattnerb9c86512009-12-29 02:14:09 +0000796 ConstantExpr::getInBoundsGetElementPtr(getOperand(0), &Ops[0],Ops.size()):
Dan Gohman1b849082009-09-07 23:54:19 +0000797 ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000798 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000799 default:
800 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000801 Op0 = (OpNo == 0) ? Op : getOperand(0);
802 Op1 = (OpNo == 1) ? Op : getOperand(1);
Chris Lattnerb9c86512009-12-29 02:14:09 +0000803 return ConstantExpr::get(getOpcode(), Op0, Op1, SubclassOptionalData);
Chris Lattner227816342006-07-14 22:20:01 +0000804 }
805}
806
807/// getWithOperands - This returns the current constant expression with the
808/// operands replaced with the specified values. The specified operands must
809/// match count and type with the existing ones.
810Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000811getWithOperands(Constant* const *Ops, unsigned NumOps) const {
812 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000813 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000814 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000815 assert(Ops[i]->getType() == getOperand(i)->getType() &&
816 "Operand type mismatch!");
817 AnyChange |= Ops[i] != getOperand(i);
818 }
819 if (!AnyChange) // No operands changed, return self.
820 return const_cast<ConstantExpr*>(this);
821
822 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000823 case Instruction::Trunc:
824 case Instruction::ZExt:
825 case Instruction::SExt:
826 case Instruction::FPTrunc:
827 case Instruction::FPExt:
828 case Instruction::UIToFP:
829 case Instruction::SIToFP:
830 case Instruction::FPToUI:
831 case Instruction::FPToSI:
832 case Instruction::PtrToInt:
833 case Instruction::IntToPtr:
834 case Instruction::BitCast:
835 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000836 case Instruction::Select:
837 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
838 case Instruction::InsertElement:
839 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
840 case Instruction::ExtractElement:
841 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
842 case Instruction::ShuffleVector:
843 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000844 case Instruction::GetElementPtr:
Dan Gohman1b849082009-09-07 23:54:19 +0000845 return cast<GEPOperator>(this)->isInBounds() ?
846 ConstantExpr::getInBoundsGetElementPtr(Ops[0], &Ops[1], NumOps-1) :
847 ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000848 case Instruction::ICmp:
849 case Instruction::FCmp:
850 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000851 default:
852 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattnerb9c86512009-12-29 02:14:09 +0000853 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000854 }
855}
856
Chris Lattner2f7c9632001-06-06 20:29:01 +0000857
858//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000859// isValueValidForType implementations
860
Reid Spencere7334722006-12-19 01:28:19 +0000861bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000862 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Owen Anderson55f1c092009-08-13 21:58:54 +0000863 if (Ty == Type::getInt1Ty(Ty->getContext()))
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000864 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000865 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000866 return true; // always true, has to fit in largest type
867 uint64_t Max = (1ll << NumBits) - 1;
868 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000869}
870
Reid Spencere0fc4df2006-10-20 07:07:24 +0000871bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000872 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Owen Anderson55f1c092009-08-13 21:58:54 +0000873 if (Ty == Type::getInt1Ty(Ty->getContext()))
Reid Spencera94d3942007-01-19 21:13:56 +0000874 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000875 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000876 return true; // always true, has to fit in largest type
877 int64_t Min = -(1ll << (NumBits-1));
878 int64_t Max = (1ll << (NumBits-1)) - 1;
879 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000880}
881
Dale Johannesend246b2c2007-08-30 00:23:21 +0000882bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
883 // convert modifies in place, so make a copy.
884 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000885 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000886 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000887 default:
888 return false; // These can't be represented as floating point!
889
Dale Johannesend246b2c2007-08-30 00:23:21 +0000890 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000891 case Type::FloatTyID: {
892 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
893 return true;
894 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
895 return !losesInfo;
896 }
897 case Type::DoubleTyID: {
898 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
899 &Val2.getSemantics() == &APFloat::IEEEdouble)
900 return true;
901 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
902 return !losesInfo;
903 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000904 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000905 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
906 &Val2.getSemantics() == &APFloat::IEEEdouble ||
907 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000908 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000909 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
910 &Val2.getSemantics() == &APFloat::IEEEdouble ||
911 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000912 case Type::PPC_FP128TyID:
913 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
914 &Val2.getSemantics() == &APFloat::IEEEdouble ||
915 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000916 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000917}
Chris Lattner9655e542001-07-20 19:16:02 +0000918
Chris Lattner49d855c2001-09-07 16:46:31 +0000919//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000920// Factory Function Implementation
921
Owen Andersonb292b8c2009-07-30 23:03:37 +0000922ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) {
Chris Lattner13ee7952010-08-28 04:09:24 +0000923 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
Owen Andersonb292b8c2009-07-30 23:03:37 +0000924 "Cannot create an aggregate zero of non-aggregate type!");
925
926 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
Owen Andersonb292b8c2009-07-30 23:03:37 +0000927 return pImpl->AggZeroConstants.getOrCreate(Ty, 0);
928}
929
Dan Gohman92b551b2009-03-03 02:55:14 +0000930/// destroyConstant - Remove the constant from the constant table...
931///
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000932void ConstantAggregateZero::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +0000933 getRawType()->getContext().pImpl->AggZeroConstants.remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000934 destroyConstantImpl();
935}
936
Dan Gohman92b551b2009-03-03 02:55:14 +0000937/// destroyConstant - Remove the constant from the constant table...
938///
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000939void ConstantArray::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +0000940 getRawType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000941 destroyConstantImpl();
942}
943
Reid Spencer2546b762007-01-26 07:37:34 +0000944/// isString - This method returns true if the array is an array of i8, and
945/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000946bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +0000947 // Check the element type for i8...
Duncan Sands9dff9be2010-02-15 16:12:20 +0000948 if (!getType()->getElementType()->isIntegerTy(8))
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000949 return false;
950 // Check the elements to make sure they are all integers, not constant
951 // expressions.
952 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
953 if (!isa<ConstantInt>(getOperand(i)))
954 return false;
955 return true;
956}
957
Evan Cheng3763c5b2006-10-26 19:15:05 +0000958/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +0000959/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +0000960/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +0000961bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +0000962 // Check the element type for i8...
Duncan Sands9dff9be2010-02-15 16:12:20 +0000963 if (!getType()->getElementType()->isIntegerTy(8))
Evan Chenge974da62006-10-26 21:48:03 +0000964 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +0000965
Evan Chenge974da62006-10-26 21:48:03 +0000966 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +0000967 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +0000968 return false;
969 // Other elements must be non-null integers.
970 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
971 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +0000972 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +0000973 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +0000974 return false;
975 }
Evan Cheng3763c5b2006-10-26 19:15:05 +0000976 return true;
977}
978
979
Dan Gohman92b551b2009-03-03 02:55:14 +0000980/// getAsString - If the sub-element type of this array is i8
981/// then this method converts the array to an std::string and returns it.
982/// Otherwise, it asserts out.
983///
Chris Lattner81fabb02002-08-26 17:53:56 +0000984std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000985 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000986 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +0000987 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +0000988 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +0000989 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +0000990 return Result;
991}
992
993
Chris Lattner3462ae32001-12-03 22:26:30 +0000994//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000995//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000996
Chris Lattner189d19f2003-11-21 20:23:48 +0000997namespace llvm {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000998
Chris Lattner49d855c2001-09-07 16:46:31 +0000999}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001000
Chris Lattnerd7a73302001-10-13 06:57:33 +00001001// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001002//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001003void ConstantStruct::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +00001004 getRawType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001005 destroyConstantImpl();
1006}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001007
Brian Gaeke02209042004-08-20 06:00:58 +00001008// destroyConstant - Remove the constant from the constant table...
1009//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001010void ConstantVector::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +00001011 getRawType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001012 destroyConstantImpl();
1013}
1014
Dan Gohman30978072007-05-24 14:36:04 +00001015/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001016/// is set to all ones.
1017/// @returns true iff this constant's emements are all set to all ones.
1018/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001019bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001020 // Check out first element.
1021 const Constant *Elt = getOperand(0);
1022 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1023 if (!CI || !CI->isAllOnesValue()) return false;
1024 // Then make sure all remaining elements point to the same value.
1025 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1026 if (getOperand(I) != Elt) return false;
1027 }
1028 return true;
1029}
1030
Dan Gohman07159202007-10-17 17:51:30 +00001031/// getSplatValue - If this is a splat constant, where all of the
1032/// elements have the same value, return that value. Otherwise return null.
1033Constant *ConstantVector::getSplatValue() {
1034 // Check out first element.
1035 Constant *Elt = getOperand(0);
1036 // Then make sure all remaining elements point to the same value.
1037 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1038 if (getOperand(I) != Elt) return 0;
1039 return Elt;
1040}
1041
Chris Lattner31b132c2009-10-28 00:01:44 +00001042//---- ConstantPointerNull::get() implementation.
Chris Lattnerd7a73302001-10-13 06:57:33 +00001043//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001044
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001045ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Andersonc8c30262009-07-31 22:45:43 +00001046 return Ty->getContext().pImpl->NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001047}
1048
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001049// destroyConstant - Remove the constant from the constant table...
1050//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001051void ConstantPointerNull::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +00001052 getRawType()->getContext().pImpl->NullPtrConstants.remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001053 destroyConstantImpl();
1054}
1055
1056
Chris Lattner31b132c2009-10-28 00:01:44 +00001057//---- UndefValue::get() implementation.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001058//
1059
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001060UndefValue *UndefValue::get(const Type *Ty) {
Owen Andersonc8c30262009-07-31 22:45:43 +00001061 return Ty->getContext().pImpl->UndefValueConstants.getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001062}
1063
1064// destroyConstant - Remove the constant from the constant table.
1065//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001066void UndefValue::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +00001067 getRawType()->getContext().pImpl->UndefValueConstants.remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001068 destroyConstantImpl();
1069}
1070
Chris Lattner31b132c2009-10-28 00:01:44 +00001071//---- BlockAddress::get() implementation.
1072//
1073
1074BlockAddress *BlockAddress::get(BasicBlock *BB) {
1075 assert(BB->getParent() != 0 && "Block must have a parent");
1076 return get(BB->getParent(), BB);
1077}
1078
1079BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1080 BlockAddress *&BA =
1081 F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
1082 if (BA == 0)
1083 BA = new BlockAddress(F, BB);
1084
1085 assert(BA->getFunction() == F && "Basic block moved between functions");
1086 return BA;
1087}
1088
1089BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1090: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1091 &Op<0>(), 2) {
Chris Lattnerc559a9f2009-11-01 03:03:03 +00001092 setOperand(0, F);
1093 setOperand(1, BB);
Chris Lattneraa99c942009-11-01 01:27:45 +00001094 BB->AdjustBlockAddressRefCount(1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001095}
1096
1097
1098// destroyConstant - Remove the constant from the constant table.
1099//
1100void BlockAddress::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +00001101 getFunction()->getRawType()->getContext().pImpl
Chris Lattner31b132c2009-10-28 00:01:44 +00001102 ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
Chris Lattneraa99c942009-11-01 01:27:45 +00001103 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001104 destroyConstantImpl();
1105}
1106
1107void BlockAddress::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
1108 // This could be replacing either the Basic Block or the Function. In either
1109 // case, we have to remove the map entry.
1110 Function *NewF = getFunction();
1111 BasicBlock *NewBB = getBasicBlock();
1112
1113 if (U == &Op<0>())
1114 NewF = cast<Function>(To);
1115 else
1116 NewBB = cast<BasicBlock>(To);
1117
1118 // See if the 'new' entry already exists, if not, just update this in place
1119 // and return early.
1120 BlockAddress *&NewBA =
1121 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
1122 if (NewBA == 0) {
Chris Lattnerc559a9f2009-11-01 03:03:03 +00001123 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1124
Chris Lattner31b132c2009-10-28 00:01:44 +00001125 // Remove the old entry, this can't cause the map to rehash (just a
1126 // tombstone will get added).
1127 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1128 getBasicBlock()));
1129 NewBA = this;
Chris Lattnerc559a9f2009-11-01 03:03:03 +00001130 setOperand(0, NewF);
1131 setOperand(1, NewBB);
1132 getBasicBlock()->AdjustBlockAddressRefCount(1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001133 return;
1134 }
1135
1136 // Otherwise, I do need to replace this with an existing value.
1137 assert(NewBA != this && "I didn't contain From!");
1138
1139 // Everyone using this now uses the replacement.
1140 uncheckedReplaceAllUsesWith(NewBA);
1141
1142 destroyConstant();
1143}
1144
1145//---- ConstantExpr::get() implementations.
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001146//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001147
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001148/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001149/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001150static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001151 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001152 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001153 // Fold a few common cases
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001154 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001155 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001156
Owen Anderson1584a292009-08-04 20:25:11 +00001157 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1158
Vikram S. Adve4c485332002-07-15 18:19:33 +00001159 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001160 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001161 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001162
Owen Anderson1584a292009-08-04 20:25:11 +00001163 return pImpl->ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001164}
Reid Spencerf37dc652006-12-05 19:14:13 +00001165
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001166Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001167 Instruction::CastOps opc = Instruction::CastOps(oc);
1168 assert(Instruction::isCast(opc) && "opcode out of range");
1169 assert(C && Ty && "Null arguments to getCast");
Chris Lattner37bc78a2010-01-26 21:51:43 +00001170 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001171
1172 switch (opc) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001173 default:
1174 llvm_unreachable("Invalid cast opcode");
1175 break;
1176 case Instruction::Trunc: return getTrunc(C, Ty);
1177 case Instruction::ZExt: return getZExt(C, Ty);
1178 case Instruction::SExt: return getSExt(C, Ty);
1179 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1180 case Instruction::FPExt: return getFPExtend(C, Ty);
1181 case Instruction::UIToFP: return getUIToFP(C, Ty);
1182 case Instruction::SIToFP: return getSIToFP(C, Ty);
1183 case Instruction::FPToUI: return getFPToUI(C, Ty);
1184 case Instruction::FPToSI: return getFPToSI(C, Ty);
1185 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1186 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1187 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001188 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001189 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001190}
1191
Reid Spencer5c140882006-12-04 20:17:56 +00001192Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001193 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001194 return getBitCast(C, Ty);
1195 return getZExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001196}
1197
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001198Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001199 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001200 return getBitCast(C, Ty);
1201 return getSExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001202}
1203
1204Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001205 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001206 return getBitCast(C, Ty);
1207 return getTrunc(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001208}
1209
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001210Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001211 assert(S->getType()->isPointerTy() && "Invalid cast");
1212 assert((Ty->isIntegerTy() || Ty->isPointerTy()) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001213
Duncan Sands9dff9be2010-02-15 16:12:20 +00001214 if (Ty->isIntegerTy())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001215 return getPtrToInt(S, Ty);
1216 return getBitCast(S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001217}
1218
Reid Spencer56521c42006-12-12 00:51:07 +00001219Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1220 bool isSigned) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001221 assert(C->getType()->isIntOrIntVectorTy() &&
1222 Ty->isIntOrIntVectorTy() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001223 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1224 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001225 Instruction::CastOps opcode =
1226 (SrcBits == DstBits ? Instruction::BitCast :
1227 (SrcBits > DstBits ? Instruction::Trunc :
1228 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1229 return getCast(opcode, C, Ty);
1230}
1231
1232Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001233 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001234 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001235 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1236 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001237 if (SrcBits == DstBits)
1238 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001239 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001240 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001241 return getCast(opcode, C, Ty);
1242}
1243
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001244Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001245#ifndef NDEBUG
1246 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1247 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1248#endif
1249 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001250 assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
1251 assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001252 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001253 "SrcTy must be larger than DestTy for Trunc!");
1254
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001255 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001256}
1257
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001258Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001259#ifndef NDEBUG
1260 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1261 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1262#endif
1263 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001264 assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
1265 assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001266 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001267 "SrcTy must be smaller than DestTy for SExt!");
1268
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001269 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001270}
1271
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001272Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001273#ifndef NDEBUG
1274 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1275 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1276#endif
1277 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001278 assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
1279 assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001280 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001281 "SrcTy must be smaller than DestTy for ZExt!");
1282
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001283 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001284}
1285
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001286Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001287#ifndef NDEBUG
1288 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1289 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1290#endif
1291 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001292 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001293 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001294 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001295 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001296}
1297
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001298Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001299#ifndef NDEBUG
1300 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1301 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1302#endif
1303 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001304 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001305 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001306 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001307 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001308}
1309
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001310Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001311#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001312 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1313 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001314#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001315 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001316 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001317 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001318 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001319}
1320
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001321Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001322#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001323 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1324 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001325#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001326 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001327 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001328 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001329 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001330}
1331
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001332Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001333#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001334 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1335 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001336#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001337 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001338 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001339 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001340 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001341}
1342
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001343Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001344#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001345 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1346 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001347#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001348 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001349 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001350 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001351 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001352}
1353
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001354Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001355 assert(C->getType()->isPointerTy() && "PtrToInt source must be pointer");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001356 assert(DstTy->isIntegerTy() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001357 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001358}
1359
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001360Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001361 assert(C->getType()->isIntegerTy() && "IntToPtr source must be integral");
Duncan Sands19d0b472010-02-16 11:11:14 +00001362 assert(DstTy->isPointerTy() && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001363 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001364}
1365
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001366Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001367 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1368 "Invalid constantexpr bitcast!");
Chris Lattnercbeda872009-03-21 06:55:54 +00001369
1370 // It is common to ask for a bitcast of a value to its own type, handle this
1371 // speedily.
1372 if (C->getType() == DstTy) return C;
1373
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001374 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001375}
1376
Chris Lattnerb50d1352003-10-05 00:17:43 +00001377Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Dan Gohman1b849082009-09-07 23:54:19 +00001378 Constant *C1, Constant *C2,
1379 unsigned Flags) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001380 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001381 assert(Opcode >= Instruction::BinaryOpsBegin &&
1382 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001383 "Invalid opcode in binary constant expression");
1384 assert(C1->getType() == C2->getType() &&
1385 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001386
Owen Anderson55f1c092009-08-13 21:58:54 +00001387 if (ReqTy == C1->getType() || ReqTy == Type::getInt1Ty(ReqTy->getContext()))
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001388 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001389 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001390
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001391 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Dan Gohman1b849082009-09-07 23:54:19 +00001392 ExprMapKeyType Key(Opcode, argVec, 0, Flags);
Owen Anderson61794042009-06-17 20:10:08 +00001393
Owen Anderson1584a292009-08-04 20:25:11 +00001394 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001395 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001396}
1397
Reid Spencer266e42b2006-12-23 06:05:41 +00001398Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001399 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001400 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001401 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001402 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1403 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1404 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1405 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1406 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1407 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001408 return getFCmp(predicate, C1, C2);
1409
Nate Begemanc96e2e42008-07-25 17:35:37 +00001410 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1411 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1412 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1413 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001414 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001415 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001416}
1417
Dan Gohman1b849082009-09-07 23:54:19 +00001418Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
1419 unsigned Flags) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001420#ifndef NDEBUG
1421 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001422 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001423 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001424 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001425 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001426 assert(C1->getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001427 "Tried to create an integer operation on a non-integer type!");
1428 break;
1429 case Instruction::FAdd:
1430 case Instruction::FSub:
1431 case Instruction::FMul:
1432 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001433 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001434 "Tried to create a floating-point operation on a "
1435 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001436 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001437 case Instruction::UDiv:
1438 case Instruction::SDiv:
1439 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001440 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001441 "Tried to create an arithmetic operation on a non-arithmetic type!");
1442 break;
1443 case Instruction::FDiv:
1444 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001445 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001446 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001447 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001448 case Instruction::URem:
1449 case Instruction::SRem:
1450 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001451 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001452 "Tried to create an arithmetic operation on a non-arithmetic type!");
1453 break;
1454 case Instruction::FRem:
1455 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001456 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001457 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001458 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001459 case Instruction::And:
1460 case Instruction::Or:
1461 case Instruction::Xor:
1462 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001463 assert(C1->getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001464 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001465 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001466 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001467 case Instruction::LShr:
1468 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001469 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001470 assert(C1->getType()->isIntOrIntVectorTy() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001471 "Tried to create a shift operation on a non-integer type!");
1472 break;
1473 default:
1474 break;
1475 }
1476#endif
1477
Dan Gohman1b849082009-09-07 23:54:19 +00001478 return getTy(C1->getType(), Opcode, C1, C2, Flags);
Reid Spencera009d0d2006-12-04 21:35:24 +00001479}
1480
Owen Anderson487375e2009-07-29 18:55:55 +00001481Constant* ConstantExpr::getSizeOf(const Type* Ty) {
1482 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1483 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson55f1c092009-08-13 21:58:54 +00001484 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001485 Constant *GEP = getGetElementPtr(
Owen Anderson5a1acd92009-07-31 20:28:14 +00001486 Constant::getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001487 return getPtrToInt(GEP,
1488 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001489}
1490
1491Constant* ConstantExpr::getAlignOf(const Type* Ty) {
Dan Gohmancf913832010-01-28 02:15:55 +00001492 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
Dan Gohman50c09d02009-08-11 17:57:01 +00001493 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson03cb69f2009-08-05 23:16:16 +00001494 const Type *AligningTy = StructType::get(Ty->getContext(),
Dan Gohmancf913832010-01-28 02:15:55 +00001495 Type::getInt1Ty(Ty->getContext()), Ty, NULL);
Owen Anderson5a1acd92009-07-31 20:28:14 +00001496 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo());
Dan Gohmana9be7392010-01-28 02:43:22 +00001497 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
Owen Anderson55f1c092009-08-13 21:58:54 +00001498 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001499 Constant *Indices[2] = { Zero, One };
1500 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001501 return getPtrToInt(GEP,
1502 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001503}
1504
Dan Gohmanff3af7252009-08-16 21:26:11 +00001505Constant* ConstantExpr::getOffsetOf(const StructType* STy, unsigned FieldNo) {
Dan Gohmanede94e62010-02-01 16:37:38 +00001506 return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1507 FieldNo));
1508}
1509
1510Constant* ConstantExpr::getOffsetOf(const Type* Ty, Constant *FieldNo) {
Dan Gohmanff3af7252009-08-16 21:26:11 +00001511 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1512 // Note that a non-inbounds gep is used, as null isn't within any object.
1513 Constant *GEPIdx[] = {
Dan Gohmanede94e62010-02-01 16:37:38 +00001514 ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1515 FieldNo
Dan Gohmanff3af7252009-08-16 21:26:11 +00001516 };
1517 Constant *GEP = getGetElementPtr(
Dan Gohmanede94e62010-02-01 16:37:38 +00001518 Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx, 2);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001519 return getPtrToInt(GEP,
1520 Type::getInt64Ty(Ty->getContext()));
Dan Gohmanff3af7252009-08-16 21:26:11 +00001521}
Owen Anderson487375e2009-07-29 18:55:55 +00001522
Reid Spencer266e42b2006-12-23 06:05:41 +00001523Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001524 Constant *C1, Constant *C2) {
1525 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001526 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001527}
1528
Chris Lattner6e415c02004-03-12 05:54:04 +00001529Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001530 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001531 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001532
1533 if (ReqTy == V1->getType())
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001534 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001535 return SC; // Fold common cases
1536
1537 std::vector<Constant*> argVec(3, C);
1538 argVec[1] = V1;
1539 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001540 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001541
Owen Anderson1584a292009-08-04 20:25:11 +00001542 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001543 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001544}
1545
Chris Lattnerb50d1352003-10-05 00:17:43 +00001546Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001547 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001548 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001549 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1550 Idxs+NumIdx) ==
1551 cast<PointerType>(ReqTy)->getElementType() &&
1552 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001553
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001554 if (Constant *FC = ConstantFoldGetElementPtr(C, /*inBounds=*/false,
1555 (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001556 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001557
Duncan Sands19d0b472010-02-16 11:11:14 +00001558 assert(C->getType()->isPointerTy() &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001559 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001560 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001561 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001562 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001563 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001564 for (unsigned i = 0; i != NumIdx; ++i)
1565 ArgVec.push_back(cast<Constant>(Idxs[i]));
1566 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001567
Owen Anderson1584a292009-08-04 20:25:11 +00001568 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001569 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001570}
1571
Dan Gohman1b849082009-09-07 23:54:19 +00001572Constant *ConstantExpr::getInBoundsGetElementPtrTy(const Type *ReqTy,
1573 Constant *C,
Chris Lattnerff095592009-12-08 05:31:46 +00001574 Value *const *Idxs,
Dan Gohman1b849082009-09-07 23:54:19 +00001575 unsigned NumIdx) {
1576 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1577 Idxs+NumIdx) ==
1578 cast<PointerType>(ReqTy)->getElementType() &&
1579 "GEP indices invalid!");
1580
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001581 if (Constant *FC = ConstantFoldGetElementPtr(C, /*inBounds=*/true,
1582 (Constant**)Idxs, NumIdx))
Dan Gohman1b849082009-09-07 23:54:19 +00001583 return FC; // Fold a few common cases...
1584
Duncan Sands19d0b472010-02-16 11:11:14 +00001585 assert(C->getType()->isPointerTy() &&
Dan Gohman1b849082009-09-07 23:54:19 +00001586 "Non-pointer type for constant GetElementPtr expression");
1587 // Look up the constant in the table first to ensure uniqueness
1588 std::vector<Constant*> ArgVec;
1589 ArgVec.reserve(NumIdx+1);
1590 ArgVec.push_back(C);
1591 for (unsigned i = 0; i != NumIdx; ++i)
1592 ArgVec.push_back(cast<Constant>(Idxs[i]));
1593 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
1594 GEPOperator::IsInBounds);
1595
1596 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Dan Gohman1b849082009-09-07 23:54:19 +00001597 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
1598}
1599
Chris Lattner302116a2007-01-31 04:40:28 +00001600Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001601 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001602 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001603 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00001604 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001605 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001606 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001607 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001608}
1609
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001610Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
1611 Value* const *Idxs,
1612 unsigned NumIdx) {
Dan Gohman1b849082009-09-07 23:54:19 +00001613 // Get the result type of the getelementptr!
1614 const Type *Ty =
1615 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
1616 assert(Ty && "GEP indices invalid!");
1617 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
1618 return getInBoundsGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001619}
1620
Chris Lattner302116a2007-01-31 04:40:28 +00001621Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001622 unsigned NumIdx) {
1623 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001624}
1625
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001626Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
1627 Constant* const *Idxs,
1628 unsigned NumIdx) {
1629 return getInBoundsGetElementPtr(C, (Value* const *)Idxs, NumIdx);
1630}
1631
Reid Spenceree3c9912006-12-04 05:19:50 +00001632Constant *
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001633ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001634 assert(LHS->getType() == RHS->getType());
1635 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1636 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1637
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001638 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001639 return FC; // Fold a few common cases...
1640
1641 // Look up the constant in the table first to ensure uniqueness
1642 std::vector<Constant*> ArgVec;
1643 ArgVec.push_back(LHS);
1644 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001645 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001646 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001647
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001648 const Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1649 if (const VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
1650 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1651
Owen Anderson1584a292009-08-04 20:25:11 +00001652 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001653 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001654}
1655
1656Constant *
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001657ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001658 assert(LHS->getType() == RHS->getType());
1659 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1660
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001661 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001662 return FC; // Fold a few common cases...
1663
1664 // Look up the constant in the table first to ensure uniqueness
1665 std::vector<Constant*> ArgVec;
1666 ArgVec.push_back(LHS);
1667 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001668 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001669 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001670
1671 const Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1672 if (const VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
1673 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1674
Owen Anderson1584a292009-08-04 20:25:11 +00001675 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001676 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001677}
1678
Robert Bocchino23004482006-01-10 19:05:34 +00001679Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1680 Constant *Idx) {
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001681 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
Chris Lattner09660c92009-12-30 20:25:09 +00001682 return FC; // Fold a few common cases.
Robert Bocchino23004482006-01-10 19:05:34 +00001683 // Look up the constant in the table first to ensure uniqueness
1684 std::vector<Constant*> ArgVec(1, Val);
1685 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001686 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001687
Owen Anderson1584a292009-08-04 20:25:11 +00001688 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001689 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001690}
1691
1692Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001693 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001694 "Tried to create extractelement operation on non-vector type!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001695 assert(Idx->getType()->isIntegerTy(32) &&
Reid Spencer2546b762007-01-26 07:37:34 +00001696 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001697 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001698 Val, Idx);
1699}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001700
Robert Bocchinoca27f032006-01-17 20:07:22 +00001701Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1702 Constant *Elt, Constant *Idx) {
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001703 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
Chris Lattner09660c92009-12-30 20:25:09 +00001704 return FC; // Fold a few common cases.
Robert Bocchinoca27f032006-01-17 20:07:22 +00001705 // Look up the constant in the table first to ensure uniqueness
1706 std::vector<Constant*> ArgVec(1, Val);
1707 ArgVec.push_back(Elt);
1708 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001709 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001710
Owen Anderson1584a292009-08-04 20:25:11 +00001711 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001712 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001713}
1714
1715Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1716 Constant *Idx) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001717 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001718 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001719 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001720 && "Insertelement types must match!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001721 assert(Idx->getType()->isIntegerTy(32) &&
Reid Spencer2546b762007-01-26 07:37:34 +00001722 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00001723 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001724}
1725
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001726Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1727 Constant *V2, Constant *Mask) {
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001728 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001729 return FC; // Fold a few common cases...
1730 // Look up the constant in the table first to ensure uniqueness
1731 std::vector<Constant*> ArgVec(1, V1);
1732 ArgVec.push_back(V2);
1733 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001734 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001735
Owen Anderson1584a292009-08-04 20:25:11 +00001736 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001737 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001738}
1739
1740Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1741 Constant *Mask) {
1742 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1743 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00001744
1745 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1746 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1747 const Type *ShufTy = VectorType::get(EltTy, NElts);
1748 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001749}
1750
Dan Gohman12fce772008-05-15 19:50:34 +00001751Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1752 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001753 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001754 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1755 Idxs+NumIdx) == Val->getType() &&
1756 "insertvalue indices invalid!");
1757 assert(Agg->getType() == ReqTy &&
1758 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001759 assert(Agg->getType()->isFirstClassType() &&
1760 "Non-first-class type for constant InsertValue expression");
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001761 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001762 assert(FC && "InsertValue constant expr couldn't be folded!");
1763 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001764}
1765
1766Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001767 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001768 assert(Agg->getType()->isFirstClassType() &&
1769 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001770
Dan Gohman0752bff2008-05-23 00:36:11 +00001771 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00001772#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00001773 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00001774 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00001775#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00001776 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00001777 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
1778}
1779
1780Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001781 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001782 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1783 Idxs+NumIdx) == ReqTy &&
1784 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001785 assert(Agg->getType()->isFirstClassType() &&
1786 "Non-first-class type for constant extractvalue expression");
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001787 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001788 assert(FC && "ExtractValue constant expr couldn't be folded!");
1789 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001790}
1791
1792Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001793 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001794 assert(Agg->getType()->isFirstClassType() &&
1795 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001796
1797 const Type *ReqTy =
1798 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
1799 assert(ReqTy && "extractvalue indices invalid!");
1800 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
1801}
1802
Owen Anderson487375e2009-07-29 18:55:55 +00001803Constant* ConstantExpr::getNeg(Constant* C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001804 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00001805 "Cannot NEG a nonintegral value!");
1806 return get(Instruction::Sub,
1807 ConstantFP::getZeroValueForNegation(C->getType()),
1808 C);
1809}
1810
1811Constant* ConstantExpr::getFNeg(Constant* C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001812 assert(C->getType()->isFPOrFPVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00001813 "Cannot FNEG a non-floating-point value!");
1814 return get(Instruction::FSub,
1815 ConstantFP::getZeroValueForNegation(C->getType()),
1816 C);
1817}
1818
1819Constant* ConstantExpr::getNot(Constant* C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001820 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00001821 "Cannot NOT a nonintegral value!");
Owen Anderson5a1acd92009-07-31 20:28:14 +00001822 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Anderson487375e2009-07-29 18:55:55 +00001823}
1824
1825Constant* ConstantExpr::getAdd(Constant* C1, Constant* C2) {
1826 return get(Instruction::Add, C1, C2);
1827}
1828
1829Constant* ConstantExpr::getFAdd(Constant* C1, Constant* C2) {
1830 return get(Instruction::FAdd, C1, C2);
1831}
1832
1833Constant* ConstantExpr::getSub(Constant* C1, Constant* C2) {
1834 return get(Instruction::Sub, C1, C2);
1835}
1836
1837Constant* ConstantExpr::getFSub(Constant* C1, Constant* C2) {
1838 return get(Instruction::FSub, C1, C2);
1839}
1840
1841Constant* ConstantExpr::getMul(Constant* C1, Constant* C2) {
1842 return get(Instruction::Mul, C1, C2);
1843}
1844
1845Constant* ConstantExpr::getFMul(Constant* C1, Constant* C2) {
1846 return get(Instruction::FMul, C1, C2);
1847}
1848
1849Constant* ConstantExpr::getUDiv(Constant* C1, Constant* C2) {
1850 return get(Instruction::UDiv, C1, C2);
1851}
1852
1853Constant* ConstantExpr::getSDiv(Constant* C1, Constant* C2) {
1854 return get(Instruction::SDiv, C1, C2);
1855}
1856
1857Constant* ConstantExpr::getFDiv(Constant* C1, Constant* C2) {
1858 return get(Instruction::FDiv, C1, C2);
1859}
1860
1861Constant* ConstantExpr::getURem(Constant* C1, Constant* C2) {
1862 return get(Instruction::URem, C1, C2);
1863}
1864
1865Constant* ConstantExpr::getSRem(Constant* C1, Constant* C2) {
1866 return get(Instruction::SRem, C1, C2);
1867}
1868
1869Constant* ConstantExpr::getFRem(Constant* C1, Constant* C2) {
1870 return get(Instruction::FRem, C1, C2);
1871}
1872
1873Constant* ConstantExpr::getAnd(Constant* C1, Constant* C2) {
1874 return get(Instruction::And, C1, C2);
1875}
1876
1877Constant* ConstantExpr::getOr(Constant* C1, Constant* C2) {
1878 return get(Instruction::Or, C1, C2);
1879}
1880
1881Constant* ConstantExpr::getXor(Constant* C1, Constant* C2) {
1882 return get(Instruction::Xor, C1, C2);
1883}
1884
1885Constant* ConstantExpr::getShl(Constant* C1, Constant* C2) {
1886 return get(Instruction::Shl, C1, C2);
1887}
1888
1889Constant* ConstantExpr::getLShr(Constant* C1, Constant* C2) {
1890 return get(Instruction::LShr, C1, C2);
1891}
1892
1893Constant* ConstantExpr::getAShr(Constant* C1, Constant* C2) {
1894 return get(Instruction::AShr, C1, C2);
1895}
1896
Vikram S. Adve4c485332002-07-15 18:19:33 +00001897// destroyConstant - Remove the constant from the constant table...
1898//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001899void ConstantExpr::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +00001900 getRawType()->getContext().pImpl->ExprConstants.remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001901 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001902}
1903
Chris Lattner3cd8c562002-07-30 18:54:25 +00001904const char *ConstantExpr::getOpcodeName() const {
1905 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001906}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001907
Chris Lattnera3b94ba2010-03-30 20:48:48 +00001908
1909
1910GetElementPtrConstantExpr::
1911GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
1912 const Type *DestTy)
1913 : ConstantExpr(DestTy, Instruction::GetElementPtr,
1914 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
1915 - (IdxList.size()+1), IdxList.size()+1) {
1916 OperandList[0] = C;
1917 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
1918 OperandList[i+1] = IdxList[i];
1919}
1920
1921
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001922//===----------------------------------------------------------------------===//
1923// replaceUsesOfWithOnConstant implementations
1924
Chris Lattner913849b2007-08-21 00:55:23 +00001925/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
1926/// 'From' to be uses of 'To'. This must update the uniquing data structures
1927/// etc.
1928///
1929/// Note that we intentionally replace all uses of From with To here. Consider
1930/// a large array that uses 'From' 1000 times. By handling this case all here,
1931/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
1932/// single invocation handles all 1000 uses. Handling them one at a time would
1933/// work, but would be really slow because it would have to unique each updated
1934/// array instance.
Chris Lattner31b132c2009-10-28 00:01:44 +00001935///
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001936void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001937 Use *U) {
Owen Andersonc2c79322009-07-28 18:32:17 +00001938 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1939 Constant *ToC = cast<Constant>(To);
1940
Chris Lattner718da702010-07-17 06:13:52 +00001941 LLVMContextImpl *pImpl = getRawType()->getContext().pImpl;
Owen Andersonc2c79322009-07-28 18:32:17 +00001942
Dan Gohmane4532f32009-09-15 15:58:07 +00001943 std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, ConstantArray*> Lookup;
Chris Lattner718da702010-07-17 06:13:52 +00001944 Lookup.first.first = cast<ArrayType>(getRawType());
Owen Andersonc2c79322009-07-28 18:32:17 +00001945 Lookup.second = this;
1946
1947 std::vector<Constant*> &Values = Lookup.first.second;
1948 Values.reserve(getNumOperands()); // Build replacement array.
1949
1950 // Fill values with the modified operands of the constant array. Also,
1951 // compute whether this turns into an all-zeros array.
1952 bool isAllZeros = false;
1953 unsigned NumUpdated = 0;
1954 if (!ToC->isNullValue()) {
1955 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1956 Constant *Val = cast<Constant>(O->get());
1957 if (Val == From) {
1958 Val = ToC;
1959 ++NumUpdated;
1960 }
1961 Values.push_back(Val);
1962 }
1963 } else {
1964 isAllZeros = true;
1965 for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
1966 Constant *Val = cast<Constant>(O->get());
1967 if (Val == From) {
1968 Val = ToC;
1969 ++NumUpdated;
1970 }
1971 Values.push_back(Val);
1972 if (isAllZeros) isAllZeros = Val->isNullValue();
1973 }
1974 }
1975
1976 Constant *Replacement = 0;
1977 if (isAllZeros) {
Chris Lattner718da702010-07-17 06:13:52 +00001978 Replacement = ConstantAggregateZero::get(getRawType());
Owen Andersonc2c79322009-07-28 18:32:17 +00001979 } else {
1980 // Check to see if we have this array type already.
Owen Andersonc2c79322009-07-28 18:32:17 +00001981 bool Exists;
1982 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
1983 pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
1984
1985 if (Exists) {
Devang Patelf7188322009-09-03 01:39:20 +00001986 Replacement = I->second;
Owen Andersonc2c79322009-07-28 18:32:17 +00001987 } else {
1988 // Okay, the new shape doesn't exist in the system yet. Instead of
1989 // creating a new constant array, inserting it, replaceallusesof'ing the
1990 // old with the new, then deleting the old... just update the current one
1991 // in place!
1992 pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
1993
1994 // Update to the new value. Optimize for the case when we have a single
1995 // operand that we're changing, but handle bulk updates efficiently.
1996 if (NumUpdated == 1) {
1997 unsigned OperandToUpdate = U - OperandList;
1998 assert(getOperand(OperandToUpdate) == From &&
1999 "ReplaceAllUsesWith broken!");
2000 setOperand(OperandToUpdate, ToC);
2001 } else {
2002 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2003 if (getOperand(i) == From)
2004 setOperand(i, ToC);
2005 }
2006 return;
2007 }
2008 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002009
2010 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002011 assert(Replacement != this && "I didn't contain From!");
2012
Chris Lattner7a1450d2005-10-04 18:13:04 +00002013 // Everyone using this now uses the replacement.
2014 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002015
2016 // Delete the old constant!
2017 destroyConstant();
2018}
2019
2020void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002021 Use *U) {
Owen Anderson45308b52009-07-27 22:29:26 +00002022 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2023 Constant *ToC = cast<Constant>(To);
2024
2025 unsigned OperandToUpdate = U-OperandList;
2026 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2027
Dan Gohmane4532f32009-09-15 15:58:07 +00002028 std::pair<LLVMContextImpl::StructConstantsTy::MapKey, ConstantStruct*> Lookup;
Chris Lattner718da702010-07-17 06:13:52 +00002029 Lookup.first.first = cast<StructType>(getRawType());
Owen Anderson45308b52009-07-27 22:29:26 +00002030 Lookup.second = this;
2031 std::vector<Constant*> &Values = Lookup.first.second;
2032 Values.reserve(getNumOperands()); // Build replacement struct.
2033
2034
2035 // Fill values with the modified operands of the constant struct. Also,
2036 // compute whether this turns into an all-zeros struct.
2037 bool isAllZeros = false;
2038 if (!ToC->isNullValue()) {
2039 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
2040 Values.push_back(cast<Constant>(O->get()));
2041 } else {
2042 isAllZeros = true;
2043 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2044 Constant *Val = cast<Constant>(O->get());
2045 Values.push_back(Val);
2046 if (isAllZeros) isAllZeros = Val->isNullValue();
2047 }
2048 }
2049 Values[OperandToUpdate] = ToC;
2050
Chris Lattner718da702010-07-17 06:13:52 +00002051 LLVMContextImpl *pImpl = getRawType()->getContext().pImpl;
Owen Anderson45308b52009-07-27 22:29:26 +00002052
2053 Constant *Replacement = 0;
2054 if (isAllZeros) {
Chris Lattner718da702010-07-17 06:13:52 +00002055 Replacement = ConstantAggregateZero::get(getRawType());
Owen Anderson45308b52009-07-27 22:29:26 +00002056 } else {
Chris Lattner718da702010-07-17 06:13:52 +00002057 // Check to see if we have this struct type already.
Owen Anderson45308b52009-07-27 22:29:26 +00002058 bool Exists;
2059 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2060 pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
2061
2062 if (Exists) {
Devang Patelf7188322009-09-03 01:39:20 +00002063 Replacement = I->second;
Owen Anderson45308b52009-07-27 22:29:26 +00002064 } else {
2065 // Okay, the new shape doesn't exist in the system yet. Instead of
2066 // creating a new constant struct, inserting it, replaceallusesof'ing the
2067 // old with the new, then deleting the old... just update the current one
2068 // in place!
2069 pImpl->StructConstants.MoveConstantToNewSlot(this, I);
2070
2071 // Update to the new value.
2072 setOperand(OperandToUpdate, ToC);
2073 return;
2074 }
2075 }
2076
2077 assert(Replacement != this && "I didn't contain From!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002078
Chris Lattner7a1450d2005-10-04 18:13:04 +00002079 // Everyone using this now uses the replacement.
2080 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002081
2082 // Delete the old constant!
2083 destroyConstant();
2084}
2085
Reid Spencerd84d35b2007-02-15 02:26:10 +00002086void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002087 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002088 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2089
2090 std::vector<Constant*> Values;
2091 Values.reserve(getNumOperands()); // Build replacement array...
2092 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2093 Constant *Val = getOperand(i);
2094 if (Val == From) Val = cast<Constant>(To);
2095 Values.push_back(Val);
2096 }
2097
Chris Lattner718da702010-07-17 06:13:52 +00002098 Constant *Replacement = get(cast<VectorType>(getRawType()), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002099 assert(Replacement != this && "I didn't contain From!");
2100
Chris Lattner7a1450d2005-10-04 18:13:04 +00002101 // Everyone using this now uses the replacement.
2102 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002103
2104 // Delete the old constant!
2105 destroyConstant();
2106}
2107
2108void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002109 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002110 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2111 Constant *To = cast<Constant>(ToV);
2112
2113 Constant *Replacement = 0;
2114 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002115 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002116 Constant *Pointer = getOperand(0);
2117 Indices.reserve(getNumOperands()-1);
2118 if (Pointer == From) Pointer = To;
2119
2120 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2121 Constant *Val = getOperand(i);
2122 if (Val == From) Val = To;
2123 Indices.push_back(Val);
2124 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002125 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2126 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002127 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002128 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002129 if (Agg == From) Agg = To;
2130
Dan Gohman1ecaf452008-05-31 00:58:22 +00002131 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002132 Replacement = ConstantExpr::getExtractValue(Agg,
2133 &Indices[0], Indices.size());
2134 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002135 Constant *Agg = getOperand(0);
2136 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002137 if (Agg == From) Agg = To;
2138 if (Val == From) Val = To;
2139
Dan Gohman1ecaf452008-05-31 00:58:22 +00002140 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002141 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2142 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002143 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002144 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner718da702010-07-17 06:13:52 +00002145 Replacement = ConstantExpr::getCast(getOpcode(), To, getRawType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002146 } else if (getOpcode() == Instruction::Select) {
2147 Constant *C1 = getOperand(0);
2148 Constant *C2 = getOperand(1);
2149 Constant *C3 = getOperand(2);
2150 if (C1 == From) C1 = To;
2151 if (C2 == From) C2 = To;
2152 if (C3 == From) C3 = To;
2153 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002154 } else if (getOpcode() == Instruction::ExtractElement) {
2155 Constant *C1 = getOperand(0);
2156 Constant *C2 = getOperand(1);
2157 if (C1 == From) C1 = To;
2158 if (C2 == From) C2 = To;
2159 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002160 } else if (getOpcode() == Instruction::InsertElement) {
2161 Constant *C1 = getOperand(0);
2162 Constant *C2 = getOperand(1);
2163 Constant *C3 = getOperand(1);
2164 if (C1 == From) C1 = To;
2165 if (C2 == From) C2 = To;
2166 if (C3 == From) C3 = To;
2167 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2168 } else if (getOpcode() == Instruction::ShuffleVector) {
2169 Constant *C1 = getOperand(0);
2170 Constant *C2 = getOperand(1);
2171 Constant *C3 = getOperand(2);
2172 if (C1 == From) C1 = To;
2173 if (C2 == From) C2 = To;
2174 if (C3 == From) C3 = To;
2175 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002176 } else if (isCompare()) {
2177 Constant *C1 = getOperand(0);
2178 Constant *C2 = getOperand(1);
2179 if (C1 == From) C1 = To;
2180 if (C2 == From) C2 = To;
2181 if (getOpcode() == Instruction::ICmp)
2182 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002183 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002184 assert(getOpcode() == Instruction::FCmp);
2185 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002186 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002187 } else if (getNumOperands() == 2) {
2188 Constant *C1 = getOperand(0);
2189 Constant *C2 = getOperand(1);
2190 if (C1 == From) C1 = To;
2191 if (C2 == From) C2 = To;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002192 Replacement = ConstantExpr::get(getOpcode(), C1, C2, SubclassOptionalData);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002193 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002194 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002195 return;
2196 }
2197
2198 assert(Replacement != this && "I didn't contain From!");
2199
Chris Lattner7a1450d2005-10-04 18:13:04 +00002200 // Everyone using this now uses the replacement.
2201 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002202
2203 // Delete the old constant!
2204 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002205}