[Demangle] Add a few more options to the microsoft demangler
[llvm-complete.git] / lib / IR / Constants.cpp
blobf792f01efc1aa4a815ffb76b07c6a42095f0a86c
1 //===-- Constants.cpp - Implement Constant nodes --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Constant* classes.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/Constants.h"
14 #include "ConstantFold.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/GetElementPtrTypeIterator.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Operator.h"
25 #include "llvm/IR/PatternMatch.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/MathExtras.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <algorithm>
33 using namespace llvm;
35 //===----------------------------------------------------------------------===//
36 // Constant Class
37 //===----------------------------------------------------------------------===//
39 bool Constant::isNegativeZeroValue() const {
40 // Floating point values have an explicit -0.0 value.
41 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
42 return CFP->isZero() && CFP->isNegative();
44 // Equivalent for a vector of -0.0's.
45 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
46 if (CV->getElementType()->isFloatingPointTy() && CV->isSplat())
47 if (CV->getElementAsAPFloat(0).isNegZero())
48 return true;
50 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
51 if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
52 if (SplatCFP && SplatCFP->isZero() && SplatCFP->isNegative())
53 return true;
55 // We've already handled true FP case; any other FP vectors can't represent -0.0.
56 if (getType()->isFPOrFPVectorTy())
57 return false;
59 // Otherwise, just use +0.0.
60 return isNullValue();
63 // Return true iff this constant is positive zero (floating point), negative
64 // zero (floating point), or a null value.
65 bool Constant::isZeroValue() const {
66 // Floating point values have an explicit -0.0 value.
67 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
68 return CFP->isZero();
70 // Equivalent for a vector of -0.0's.
71 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
72 if (CV->getElementType()->isFloatingPointTy() && CV->isSplat())
73 if (CV->getElementAsAPFloat(0).isZero())
74 return true;
76 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
77 if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
78 if (SplatCFP && SplatCFP->isZero())
79 return true;
81 // Otherwise, just use +0.0.
82 return isNullValue();
85 bool Constant::isNullValue() const {
86 // 0 is null.
87 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
88 return CI->isZero();
90 // +0.0 is null.
91 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
92 return CFP->isZero() && !CFP->isNegative();
94 // constant zero is zero for aggregates, cpnull is null for pointers, none for
95 // tokens.
96 return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this) ||
97 isa<ConstantTokenNone>(this);
100 bool Constant::isAllOnesValue() const {
101 // Check for -1 integers
102 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
103 return CI->isMinusOne();
105 // Check for FP which are bitcasted from -1 integers
106 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
107 return CFP->getValueAPF().bitcastToAPInt().isAllOnesValue();
109 // Check for constant vectors which are splats of -1 values.
110 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
111 if (Constant *Splat = CV->getSplatValue())
112 return Splat->isAllOnesValue();
114 // Check for constant vectors which are splats of -1 values.
115 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
116 if (CV->isSplat()) {
117 if (CV->getElementType()->isFloatingPointTy())
118 return CV->getElementAsAPFloat(0).bitcastToAPInt().isAllOnesValue();
119 return CV->getElementAsAPInt(0).isAllOnesValue();
123 return false;
126 bool Constant::isOneValue() const {
127 // Check for 1 integers
128 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
129 return CI->isOne();
131 // Check for FP which are bitcasted from 1 integers
132 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
133 return CFP->getValueAPF().bitcastToAPInt().isOneValue();
135 // Check for constant vectors which are splats of 1 values.
136 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
137 if (Constant *Splat = CV->getSplatValue())
138 return Splat->isOneValue();
140 // Check for constant vectors which are splats of 1 values.
141 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
142 if (CV->isSplat()) {
143 if (CV->getElementType()->isFloatingPointTy())
144 return CV->getElementAsAPFloat(0).bitcastToAPInt().isOneValue();
145 return CV->getElementAsAPInt(0).isOneValue();
149 return false;
152 bool Constant::isMinSignedValue() const {
153 // Check for INT_MIN integers
154 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
155 return CI->isMinValue(/*isSigned=*/true);
157 // Check for FP which are bitcasted from INT_MIN integers
158 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
159 return CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
161 // Check for constant vectors which are splats of INT_MIN values.
162 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
163 if (Constant *Splat = CV->getSplatValue())
164 return Splat->isMinSignedValue();
166 // Check for constant vectors which are splats of INT_MIN values.
167 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
168 if (CV->isSplat()) {
169 if (CV->getElementType()->isFloatingPointTy())
170 return CV->getElementAsAPFloat(0).bitcastToAPInt().isMinSignedValue();
171 return CV->getElementAsAPInt(0).isMinSignedValue();
175 return false;
178 bool Constant::isNotMinSignedValue() const {
179 // Check for INT_MIN integers
180 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
181 return !CI->isMinValue(/*isSigned=*/true);
183 // Check for FP which are bitcasted from INT_MIN integers
184 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
185 return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
187 // Check that vectors don't contain INT_MIN
188 if (this->getType()->isVectorTy()) {
189 unsigned NumElts = this->getType()->getVectorNumElements();
190 for (unsigned i = 0; i != NumElts; ++i) {
191 Constant *Elt = this->getAggregateElement(i);
192 if (!Elt || !Elt->isNotMinSignedValue())
193 return false;
195 return true;
198 // It *may* contain INT_MIN, we can't tell.
199 return false;
202 bool Constant::isFiniteNonZeroFP() const {
203 if (auto *CFP = dyn_cast<ConstantFP>(this))
204 return CFP->getValueAPF().isFiniteNonZero();
205 if (!getType()->isVectorTy())
206 return false;
207 for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
208 auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
209 if (!CFP || !CFP->getValueAPF().isFiniteNonZero())
210 return false;
212 return true;
215 bool Constant::isNormalFP() const {
216 if (auto *CFP = dyn_cast<ConstantFP>(this))
217 return CFP->getValueAPF().isNormal();
218 if (!getType()->isVectorTy())
219 return false;
220 for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
221 auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
222 if (!CFP || !CFP->getValueAPF().isNormal())
223 return false;
225 return true;
228 bool Constant::hasExactInverseFP() const {
229 if (auto *CFP = dyn_cast<ConstantFP>(this))
230 return CFP->getValueAPF().getExactInverse(nullptr);
231 if (!getType()->isVectorTy())
232 return false;
233 for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
234 auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
235 if (!CFP || !CFP->getValueAPF().getExactInverse(nullptr))
236 return false;
238 return true;
241 bool Constant::isNaN() const {
242 if (auto *CFP = dyn_cast<ConstantFP>(this))
243 return CFP->isNaN();
244 if (!getType()->isVectorTy())
245 return false;
246 for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
247 auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
248 if (!CFP || !CFP->isNaN())
249 return false;
251 return true;
254 bool Constant::isElementWiseEqual(Value *Y) const {
255 // Are they fully identical?
256 if (this == Y)
257 return true;
258 // They may still be identical element-wise (if they have `undef`s).
259 auto *Cy = dyn_cast<Constant>(Y);
260 if (!Cy)
261 return false;
262 return PatternMatch::match(ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_EQ,
263 const_cast<Constant *>(this),
264 Cy),
265 PatternMatch::m_One());
268 bool Constant::containsUndefElement() const {
269 if (!getType()->isVectorTy())
270 return false;
271 for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i)
272 if (isa<UndefValue>(getAggregateElement(i)))
273 return true;
275 return false;
278 bool Constant::containsConstantExpression() const {
279 if (!getType()->isVectorTy())
280 return false;
281 for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i)
282 if (isa<ConstantExpr>(getAggregateElement(i)))
283 return true;
285 return false;
288 /// Constructor to create a '0' constant of arbitrary type.
289 Constant *Constant::getNullValue(Type *Ty) {
290 switch (Ty->getTypeID()) {
291 case Type::IntegerTyID:
292 return ConstantInt::get(Ty, 0);
293 case Type::HalfTyID:
294 return ConstantFP::get(Ty->getContext(),
295 APFloat::getZero(APFloat::IEEEhalf()));
296 case Type::FloatTyID:
297 return ConstantFP::get(Ty->getContext(),
298 APFloat::getZero(APFloat::IEEEsingle()));
299 case Type::DoubleTyID:
300 return ConstantFP::get(Ty->getContext(),
301 APFloat::getZero(APFloat::IEEEdouble()));
302 case Type::X86_FP80TyID:
303 return ConstantFP::get(Ty->getContext(),
304 APFloat::getZero(APFloat::x87DoubleExtended()));
305 case Type::FP128TyID:
306 return ConstantFP::get(Ty->getContext(),
307 APFloat::getZero(APFloat::IEEEquad()));
308 case Type::PPC_FP128TyID:
309 return ConstantFP::get(Ty->getContext(),
310 APFloat(APFloat::PPCDoubleDouble(),
311 APInt::getNullValue(128)));
312 case Type::PointerTyID:
313 return ConstantPointerNull::get(cast<PointerType>(Ty));
314 case Type::StructTyID:
315 case Type::ArrayTyID:
316 case Type::VectorTyID:
317 return ConstantAggregateZero::get(Ty);
318 case Type::TokenTyID:
319 return ConstantTokenNone::get(Ty->getContext());
320 default:
321 // Function, Label, or Opaque type?
322 llvm_unreachable("Cannot create a null constant of that type!");
326 Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
327 Type *ScalarTy = Ty->getScalarType();
329 // Create the base integer constant.
330 Constant *C = ConstantInt::get(Ty->getContext(), V);
332 // Convert an integer to a pointer, if necessary.
333 if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
334 C = ConstantExpr::getIntToPtr(C, PTy);
336 // Broadcast a scalar to a vector, if necessary.
337 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
338 C = ConstantVector::getSplat(VTy->getNumElements(), C);
340 return C;
343 Constant *Constant::getAllOnesValue(Type *Ty) {
344 if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
345 return ConstantInt::get(Ty->getContext(),
346 APInt::getAllOnesValue(ITy->getBitWidth()));
348 if (Ty->isFloatingPointTy()) {
349 APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(),
350 !Ty->isPPC_FP128Ty());
351 return ConstantFP::get(Ty->getContext(), FL);
354 VectorType *VTy = cast<VectorType>(Ty);
355 return ConstantVector::getSplat(VTy->getNumElements(),
356 getAllOnesValue(VTy->getElementType()));
359 Constant *Constant::getAggregateElement(unsigned Elt) const {
360 if (const ConstantAggregate *CC = dyn_cast<ConstantAggregate>(this))
361 return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr;
363 if (const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(this))
364 return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr;
366 if (const UndefValue *UV = dyn_cast<UndefValue>(this))
367 return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
369 if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this))
370 return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
371 : nullptr;
372 return nullptr;
375 Constant *Constant::getAggregateElement(Constant *Elt) const {
376 assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer");
377 if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt)) {
378 // Check if the constant fits into an uint64_t.
379 if (CI->getValue().getActiveBits() > 64)
380 return nullptr;
381 return getAggregateElement(CI->getZExtValue());
383 return nullptr;
386 void Constant::destroyConstant() {
387 /// First call destroyConstantImpl on the subclass. This gives the subclass
388 /// a chance to remove the constant from any maps/pools it's contained in.
389 switch (getValueID()) {
390 default:
391 llvm_unreachable("Not a constant!");
392 #define HANDLE_CONSTANT(Name) \
393 case Value::Name##Val: \
394 cast<Name>(this)->destroyConstantImpl(); \
395 break;
396 #include "llvm/IR/Value.def"
399 // When a Constant is destroyed, there may be lingering
400 // references to the constant by other constants in the constant pool. These
401 // constants are implicitly dependent on the module that is being deleted,
402 // but they don't know that. Because we only find out when the CPV is
403 // deleted, we must now notify all of our users (that should only be
404 // Constants) that they are, in fact, invalid now and should be deleted.
406 while (!use_empty()) {
407 Value *V = user_back();
408 #ifndef NDEBUG // Only in -g mode...
409 if (!isa<Constant>(V)) {
410 dbgs() << "While deleting: " << *this
411 << "\n\nUse still stuck around after Def is destroyed: " << *V
412 << "\n\n";
414 #endif
415 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
416 cast<Constant>(V)->destroyConstant();
418 // The constant should remove itself from our use list...
419 assert((use_empty() || user_back() != V) && "Constant not removed!");
422 // Value has no outstanding references it is safe to delete it now...
423 delete this;
426 static bool canTrapImpl(const Constant *C,
427 SmallPtrSetImpl<const ConstantExpr *> &NonTrappingOps) {
428 assert(C->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
429 // The only thing that could possibly trap are constant exprs.
430 const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
431 if (!CE)
432 return false;
434 // ConstantExpr traps if any operands can trap.
435 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
436 if (ConstantExpr *Op = dyn_cast<ConstantExpr>(CE->getOperand(i))) {
437 if (NonTrappingOps.insert(Op).second && canTrapImpl(Op, NonTrappingOps))
438 return true;
442 // Otherwise, only specific operations can trap.
443 switch (CE->getOpcode()) {
444 default:
445 return false;
446 case Instruction::UDiv:
447 case Instruction::SDiv:
448 case Instruction::URem:
449 case Instruction::SRem:
450 // Div and rem can trap if the RHS is not known to be non-zero.
451 if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
452 return true;
453 return false;
457 bool Constant::canTrap() const {
458 SmallPtrSet<const ConstantExpr *, 4> NonTrappingOps;
459 return canTrapImpl(this, NonTrappingOps);
462 /// Check if C contains a GlobalValue for which Predicate is true.
463 static bool
464 ConstHasGlobalValuePredicate(const Constant *C,
465 bool (*Predicate)(const GlobalValue *)) {
466 SmallPtrSet<const Constant *, 8> Visited;
467 SmallVector<const Constant *, 8> WorkList;
468 WorkList.push_back(C);
469 Visited.insert(C);
471 while (!WorkList.empty()) {
472 const Constant *WorkItem = WorkList.pop_back_val();
473 if (const auto *GV = dyn_cast<GlobalValue>(WorkItem))
474 if (Predicate(GV))
475 return true;
476 for (const Value *Op : WorkItem->operands()) {
477 const Constant *ConstOp = dyn_cast<Constant>(Op);
478 if (!ConstOp)
479 continue;
480 if (Visited.insert(ConstOp).second)
481 WorkList.push_back(ConstOp);
484 return false;
487 bool Constant::isThreadDependent() const {
488 auto DLLImportPredicate = [](const GlobalValue *GV) {
489 return GV->isThreadLocal();
491 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
494 bool Constant::isDLLImportDependent() const {
495 auto DLLImportPredicate = [](const GlobalValue *GV) {
496 return GV->hasDLLImportStorageClass();
498 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
501 bool Constant::isConstantUsed() const {
502 for (const User *U : users()) {
503 const Constant *UC = dyn_cast<Constant>(U);
504 if (!UC || isa<GlobalValue>(UC))
505 return true;
507 if (UC->isConstantUsed())
508 return true;
510 return false;
513 bool Constant::needsRelocation() const {
514 if (isa<GlobalValue>(this))
515 return true; // Global reference.
517 if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
518 return BA->getFunction()->needsRelocation();
520 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) {
521 if (CE->getOpcode() == Instruction::Sub) {
522 ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
523 ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
524 if (LHS && RHS && LHS->getOpcode() == Instruction::PtrToInt &&
525 RHS->getOpcode() == Instruction::PtrToInt) {
526 Constant *LHSOp0 = LHS->getOperand(0);
527 Constant *RHSOp0 = RHS->getOperand(0);
529 // While raw uses of blockaddress need to be relocated, differences
530 // between two of them don't when they are for labels in the same
531 // function. This is a common idiom when creating a table for the
532 // indirect goto extension, so we handle it efficiently here.
533 if (isa<BlockAddress>(LHSOp0) && isa<BlockAddress>(RHSOp0) &&
534 cast<BlockAddress>(LHSOp0)->getFunction() ==
535 cast<BlockAddress>(RHSOp0)->getFunction())
536 return false;
538 // Relative pointers do not need to be dynamically relocated.
539 if (auto *LHSGV = dyn_cast<GlobalValue>(LHSOp0->stripPointerCasts()))
540 if (auto *RHSGV = dyn_cast<GlobalValue>(RHSOp0->stripPointerCasts()))
541 if (LHSGV->isDSOLocal() && RHSGV->isDSOLocal())
542 return false;
547 bool Result = false;
548 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
549 Result |= cast<Constant>(getOperand(i))->needsRelocation();
551 return Result;
554 /// If the specified constantexpr is dead, remove it. This involves recursively
555 /// eliminating any dead users of the constantexpr.
556 static bool removeDeadUsersOfConstant(const Constant *C) {
557 if (isa<GlobalValue>(C)) return false; // Cannot remove this
559 while (!C->use_empty()) {
560 const Constant *User = dyn_cast<Constant>(C->user_back());
561 if (!User) return false; // Non-constant usage;
562 if (!removeDeadUsersOfConstant(User))
563 return false; // Constant wasn't dead
566 const_cast<Constant*>(C)->destroyConstant();
567 return true;
571 void Constant::removeDeadConstantUsers() const {
572 Value::const_user_iterator I = user_begin(), E = user_end();
573 Value::const_user_iterator LastNonDeadUser = E;
574 while (I != E) {
575 const Constant *User = dyn_cast<Constant>(*I);
576 if (!User) {
577 LastNonDeadUser = I;
578 ++I;
579 continue;
582 if (!removeDeadUsersOfConstant(User)) {
583 // If the constant wasn't dead, remember that this was the last live use
584 // and move on to the next constant.
585 LastNonDeadUser = I;
586 ++I;
587 continue;
590 // If the constant was dead, then the iterator is invalidated.
591 if (LastNonDeadUser == E)
592 I = user_begin();
593 else
594 I = std::next(LastNonDeadUser);
600 //===----------------------------------------------------------------------===//
601 // ConstantInt
602 //===----------------------------------------------------------------------===//
604 ConstantInt::ConstantInt(IntegerType *Ty, const APInt &V)
605 : ConstantData(Ty, ConstantIntVal), Val(V) {
606 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
609 ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
610 LLVMContextImpl *pImpl = Context.pImpl;
611 if (!pImpl->TheTrueVal)
612 pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
613 return pImpl->TheTrueVal;
616 ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
617 LLVMContextImpl *pImpl = Context.pImpl;
618 if (!pImpl->TheFalseVal)
619 pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
620 return pImpl->TheFalseVal;
623 Constant *ConstantInt::getTrue(Type *Ty) {
624 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
625 ConstantInt *TrueC = ConstantInt::getTrue(Ty->getContext());
626 if (auto *VTy = dyn_cast<VectorType>(Ty))
627 return ConstantVector::getSplat(VTy->getNumElements(), TrueC);
628 return TrueC;
631 Constant *ConstantInt::getFalse(Type *Ty) {
632 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
633 ConstantInt *FalseC = ConstantInt::getFalse(Ty->getContext());
634 if (auto *VTy = dyn_cast<VectorType>(Ty))
635 return ConstantVector::getSplat(VTy->getNumElements(), FalseC);
636 return FalseC;
639 // Get a ConstantInt from an APInt.
640 ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
641 // get an existing value or the insertion position
642 LLVMContextImpl *pImpl = Context.pImpl;
643 std::unique_ptr<ConstantInt> &Slot = pImpl->IntConstants[V];
644 if (!Slot) {
645 // Get the corresponding integer type for the bit width of the value.
646 IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
647 Slot.reset(new ConstantInt(ITy, V));
649 assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth()));
650 return Slot.get();
653 Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
654 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
656 // For vectors, broadcast the value.
657 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
658 return ConstantVector::getSplat(VTy->getNumElements(), C);
660 return C;
663 ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) {
664 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
667 ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) {
668 return get(Ty, V, true);
671 Constant *ConstantInt::getSigned(Type *Ty, int64_t V) {
672 return get(Ty, V, true);
675 Constant *ConstantInt::get(Type *Ty, const APInt& V) {
676 ConstantInt *C = get(Ty->getContext(), V);
677 assert(C->getType() == Ty->getScalarType() &&
678 "ConstantInt type doesn't match the type implied by its value!");
680 // For vectors, broadcast the value.
681 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
682 return ConstantVector::getSplat(VTy->getNumElements(), C);
684 return C;
687 ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) {
688 return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
691 /// Remove the constant from the constant table.
692 void ConstantInt::destroyConstantImpl() {
693 llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
696 //===----------------------------------------------------------------------===//
697 // ConstantFP
698 //===----------------------------------------------------------------------===//
700 static const fltSemantics *TypeToFloatSemantics(Type *Ty) {
701 if (Ty->isHalfTy())
702 return &APFloat::IEEEhalf();
703 if (Ty->isFloatTy())
704 return &APFloat::IEEEsingle();
705 if (Ty->isDoubleTy())
706 return &APFloat::IEEEdouble();
707 if (Ty->isX86_FP80Ty())
708 return &APFloat::x87DoubleExtended();
709 else if (Ty->isFP128Ty())
710 return &APFloat::IEEEquad();
712 assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
713 return &APFloat::PPCDoubleDouble();
716 Constant *ConstantFP::get(Type *Ty, double V) {
717 LLVMContext &Context = Ty->getContext();
719 APFloat FV(V);
720 bool ignored;
721 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
722 APFloat::rmNearestTiesToEven, &ignored);
723 Constant *C = get(Context, FV);
725 // For vectors, broadcast the value.
726 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
727 return ConstantVector::getSplat(VTy->getNumElements(), C);
729 return C;
732 Constant *ConstantFP::get(Type *Ty, const APFloat &V) {
733 ConstantFP *C = get(Ty->getContext(), V);
734 assert(C->getType() == Ty->getScalarType() &&
735 "ConstantFP type doesn't match the type implied by its value!");
737 // For vectors, broadcast the value.
738 if (auto *VTy = dyn_cast<VectorType>(Ty))
739 return ConstantVector::getSplat(VTy->getNumElements(), C);
741 return C;
744 Constant *ConstantFP::get(Type *Ty, StringRef Str) {
745 LLVMContext &Context = Ty->getContext();
747 APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
748 Constant *C = get(Context, FV);
750 // For vectors, broadcast the value.
751 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
752 return ConstantVector::getSplat(VTy->getNumElements(), C);
754 return C;
757 Constant *ConstantFP::getNaN(Type *Ty, bool Negative, uint64_t Payload) {
758 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
759 APFloat NaN = APFloat::getNaN(Semantics, Negative, Payload);
760 Constant *C = get(Ty->getContext(), NaN);
762 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
763 return ConstantVector::getSplat(VTy->getNumElements(), C);
765 return C;
768 Constant *ConstantFP::getQNaN(Type *Ty, bool Negative, APInt *Payload) {
769 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
770 APFloat NaN = APFloat::getQNaN(Semantics, Negative, Payload);
771 Constant *C = get(Ty->getContext(), NaN);
773 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
774 return ConstantVector::getSplat(VTy->getNumElements(), C);
776 return C;
779 Constant *ConstantFP::getSNaN(Type *Ty, bool Negative, APInt *Payload) {
780 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
781 APFloat NaN = APFloat::getSNaN(Semantics, Negative, Payload);
782 Constant *C = get(Ty->getContext(), NaN);
784 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
785 return ConstantVector::getSplat(VTy->getNumElements(), C);
787 return C;
790 Constant *ConstantFP::getNegativeZero(Type *Ty) {
791 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
792 APFloat NegZero = APFloat::getZero(Semantics, /*Negative=*/true);
793 Constant *C = get(Ty->getContext(), NegZero);
795 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
796 return ConstantVector::getSplat(VTy->getNumElements(), C);
798 return C;
802 Constant *ConstantFP::getZeroValueForNegation(Type *Ty) {
803 if (Ty->isFPOrFPVectorTy())
804 return getNegativeZero(Ty);
806 return Constant::getNullValue(Ty);
810 // ConstantFP accessors.
811 ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
812 LLVMContextImpl* pImpl = Context.pImpl;
814 std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];
816 if (!Slot) {
817 Type *Ty;
818 if (&V.getSemantics() == &APFloat::IEEEhalf())
819 Ty = Type::getHalfTy(Context);
820 else if (&V.getSemantics() == &APFloat::IEEEsingle())
821 Ty = Type::getFloatTy(Context);
822 else if (&V.getSemantics() == &APFloat::IEEEdouble())
823 Ty = Type::getDoubleTy(Context);
824 else if (&V.getSemantics() == &APFloat::x87DoubleExtended())
825 Ty = Type::getX86_FP80Ty(Context);
826 else if (&V.getSemantics() == &APFloat::IEEEquad())
827 Ty = Type::getFP128Ty(Context);
828 else {
829 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble() &&
830 "Unknown FP format");
831 Ty = Type::getPPC_FP128Ty(Context);
833 Slot.reset(new ConstantFP(Ty, V));
836 return Slot.get();
839 Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
840 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
841 Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
843 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
844 return ConstantVector::getSplat(VTy->getNumElements(), C);
846 return C;
849 ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
850 : ConstantData(Ty, ConstantFPVal), Val(V) {
851 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
852 "FP type Mismatch");
855 bool ConstantFP::isExactlyValue(const APFloat &V) const {
856 return Val.bitwiseIsEqual(V);
859 /// Remove the constant from the constant table.
860 void ConstantFP::destroyConstantImpl() {
861 llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
864 //===----------------------------------------------------------------------===//
865 // ConstantAggregateZero Implementation
866 //===----------------------------------------------------------------------===//
868 Constant *ConstantAggregateZero::getSequentialElement() const {
869 return Constant::getNullValue(getType()->getSequentialElementType());
872 Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
873 return Constant::getNullValue(getType()->getStructElementType(Elt));
876 Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
877 if (isa<SequentialType>(getType()))
878 return getSequentialElement();
879 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
882 Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
883 if (isa<SequentialType>(getType()))
884 return getSequentialElement();
885 return getStructElement(Idx);
888 unsigned ConstantAggregateZero::getNumElements() const {
889 Type *Ty = getType();
890 if (auto *AT = dyn_cast<ArrayType>(Ty))
891 return AT->getNumElements();
892 if (auto *VT = dyn_cast<VectorType>(Ty))
893 return VT->getNumElements();
894 return Ty->getStructNumElements();
897 //===----------------------------------------------------------------------===//
898 // UndefValue Implementation
899 //===----------------------------------------------------------------------===//
901 UndefValue *UndefValue::getSequentialElement() const {
902 return UndefValue::get(getType()->getSequentialElementType());
905 UndefValue *UndefValue::getStructElement(unsigned Elt) const {
906 return UndefValue::get(getType()->getStructElementType(Elt));
909 UndefValue *UndefValue::getElementValue(Constant *C) const {
910 if (isa<SequentialType>(getType()))
911 return getSequentialElement();
912 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
915 UndefValue *UndefValue::getElementValue(unsigned Idx) const {
916 if (isa<SequentialType>(getType()))
917 return getSequentialElement();
918 return getStructElement(Idx);
921 unsigned UndefValue::getNumElements() const {
922 Type *Ty = getType();
923 if (auto *ST = dyn_cast<SequentialType>(Ty))
924 return ST->getNumElements();
925 return Ty->getStructNumElements();
928 //===----------------------------------------------------------------------===//
929 // ConstantXXX Classes
930 //===----------------------------------------------------------------------===//
932 template <typename ItTy, typename EltTy>
933 static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
934 for (; Start != End; ++Start)
935 if (*Start != Elt)
936 return false;
937 return true;
940 template <typename SequentialTy, typename ElementTy>
941 static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {
942 assert(!V.empty() && "Cannot get empty int sequence.");
944 SmallVector<ElementTy, 16> Elts;
945 for (Constant *C : V)
946 if (auto *CI = dyn_cast<ConstantInt>(C))
947 Elts.push_back(CI->getZExtValue());
948 else
949 return nullptr;
950 return SequentialTy::get(V[0]->getContext(), Elts);
953 template <typename SequentialTy, typename ElementTy>
954 static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {
955 assert(!V.empty() && "Cannot get empty FP sequence.");
957 SmallVector<ElementTy, 16> Elts;
958 for (Constant *C : V)
959 if (auto *CFP = dyn_cast<ConstantFP>(C))
960 Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
961 else
962 return nullptr;
963 return SequentialTy::getFP(V[0]->getContext(), Elts);
966 template <typename SequenceTy>
967 static Constant *getSequenceIfElementsMatch(Constant *C,
968 ArrayRef<Constant *> V) {
969 // We speculatively build the elements here even if it turns out that there is
970 // a constantexpr or something else weird, since it is so uncommon for that to
971 // happen.
972 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
973 if (CI->getType()->isIntegerTy(8))
974 return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
975 else if (CI->getType()->isIntegerTy(16))
976 return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
977 else if (CI->getType()->isIntegerTy(32))
978 return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
979 else if (CI->getType()->isIntegerTy(64))
980 return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
981 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
982 if (CFP->getType()->isHalfTy())
983 return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
984 else if (CFP->getType()->isFloatTy())
985 return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
986 else if (CFP->getType()->isDoubleTy())
987 return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
990 return nullptr;
993 ConstantAggregate::ConstantAggregate(CompositeType *T, ValueTy VT,
994 ArrayRef<Constant *> V)
995 : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(),
996 V.size()) {
997 llvm::copy(V, op_begin());
999 // Check that types match, unless this is an opaque struct.
1000 if (auto *ST = dyn_cast<StructType>(T))
1001 if (ST->isOpaque())
1002 return;
1003 for (unsigned I = 0, E = V.size(); I != E; ++I)
1004 assert(V[I]->getType() == T->getTypeAtIndex(I) &&
1005 "Initializer for composite element doesn't match!");
1008 ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
1009 : ConstantAggregate(T, ConstantArrayVal, V) {
1010 assert(V.size() == T->getNumElements() &&
1011 "Invalid initializer for constant array");
1014 Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
1015 if (Constant *C = getImpl(Ty, V))
1016 return C;
1017 return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
1020 Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
1021 // Empty arrays are canonicalized to ConstantAggregateZero.
1022 if (V.empty())
1023 return ConstantAggregateZero::get(Ty);
1025 for (unsigned i = 0, e = V.size(); i != e; ++i) {
1026 assert(V[i]->getType() == Ty->getElementType() &&
1027 "Wrong type in array element initializer");
1030 // If this is an all-zero array, return a ConstantAggregateZero object. If
1031 // all undef, return an UndefValue, if "all simple", then return a
1032 // ConstantDataArray.
1033 Constant *C = V[0];
1034 if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
1035 return UndefValue::get(Ty);
1037 if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
1038 return ConstantAggregateZero::get(Ty);
1040 // Check to see if all of the elements are ConstantFP or ConstantInt and if
1041 // the element type is compatible with ConstantDataVector. If so, use it.
1042 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
1043 return getSequenceIfElementsMatch<ConstantDataArray>(C, V);
1045 // Otherwise, we really do want to create a ConstantArray.
1046 return nullptr;
1049 StructType *ConstantStruct::getTypeForElements(LLVMContext &Context,
1050 ArrayRef<Constant*> V,
1051 bool Packed) {
1052 unsigned VecSize = V.size();
1053 SmallVector<Type*, 16> EltTypes(VecSize);
1054 for (unsigned i = 0; i != VecSize; ++i)
1055 EltTypes[i] = V[i]->getType();
1057 return StructType::get(Context, EltTypes, Packed);
1061 StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
1062 bool Packed) {
1063 assert(!V.empty() &&
1064 "ConstantStruct::getTypeForElements cannot be called on empty list");
1065 return getTypeForElements(V[0]->getContext(), V, Packed);
1068 ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
1069 : ConstantAggregate(T, ConstantStructVal, V) {
1070 assert((T->isOpaque() || V.size() == T->getNumElements()) &&
1071 "Invalid initializer for constant struct");
1074 // ConstantStruct accessors.
1075 Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
1076 assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
1077 "Incorrect # elements specified to ConstantStruct::get");
1079 // Create a ConstantAggregateZero value if all elements are zeros.
1080 bool isZero = true;
1081 bool isUndef = false;
1083 if (!V.empty()) {
1084 isUndef = isa<UndefValue>(V[0]);
1085 isZero = V[0]->isNullValue();
1086 if (isUndef || isZero) {
1087 for (unsigned i = 0, e = V.size(); i != e; ++i) {
1088 if (!V[i]->isNullValue())
1089 isZero = false;
1090 if (!isa<UndefValue>(V[i]))
1091 isUndef = false;
1095 if (isZero)
1096 return ConstantAggregateZero::get(ST);
1097 if (isUndef)
1098 return UndefValue::get(ST);
1100 return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
1103 ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
1104 : ConstantAggregate(T, ConstantVectorVal, V) {
1105 assert(V.size() == T->getNumElements() &&
1106 "Invalid initializer for constant vector");
1109 // ConstantVector accessors.
1110 Constant *ConstantVector::get(ArrayRef<Constant*> V) {
1111 if (Constant *C = getImpl(V))
1112 return C;
1113 VectorType *Ty = VectorType::get(V.front()->getType(), V.size());
1114 return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
1117 Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
1118 assert(!V.empty() && "Vectors can't be empty");
1119 VectorType *T = VectorType::get(V.front()->getType(), V.size());
1121 // If this is an all-undef or all-zero vector, return a
1122 // ConstantAggregateZero or UndefValue.
1123 Constant *C = V[0];
1124 bool isZero = C->isNullValue();
1125 bool isUndef = isa<UndefValue>(C);
1127 if (isZero || isUndef) {
1128 for (unsigned i = 1, e = V.size(); i != e; ++i)
1129 if (V[i] != C) {
1130 isZero = isUndef = false;
1131 break;
1135 if (isZero)
1136 return ConstantAggregateZero::get(T);
1137 if (isUndef)
1138 return UndefValue::get(T);
1140 // Check to see if all of the elements are ConstantFP or ConstantInt and if
1141 // the element type is compatible with ConstantDataVector. If so, use it.
1142 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
1143 return getSequenceIfElementsMatch<ConstantDataVector>(C, V);
1145 // Otherwise, the element type isn't compatible with ConstantDataVector, or
1146 // the operand list contains a ConstantExpr or something else strange.
1147 return nullptr;
1150 Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
1151 // If this splat is compatible with ConstantDataVector, use it instead of
1152 // ConstantVector.
1153 if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) &&
1154 ConstantDataSequential::isElementTypeCompatible(V->getType()))
1155 return ConstantDataVector::getSplat(NumElts, V);
1157 SmallVector<Constant*, 32> Elts(NumElts, V);
1158 return get(Elts);
1161 ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) {
1162 LLVMContextImpl *pImpl = Context.pImpl;
1163 if (!pImpl->TheNoneToken)
1164 pImpl->TheNoneToken.reset(new ConstantTokenNone(Context));
1165 return pImpl->TheNoneToken.get();
1168 /// Remove the constant from the constant table.
1169 void ConstantTokenNone::destroyConstantImpl() {
1170 llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
1173 // Utility function for determining if a ConstantExpr is a CastOp or not. This
1174 // can't be inline because we don't want to #include Instruction.h into
1175 // Constant.h
1176 bool ConstantExpr::isCast() const {
1177 return Instruction::isCast(getOpcode());
1180 bool ConstantExpr::isCompare() const {
1181 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
1184 bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
1185 if (getOpcode() != Instruction::GetElementPtr) return false;
1187 gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
1188 User::const_op_iterator OI = std::next(this->op_begin());
1190 // The remaining indices may be compile-time known integers within the bounds
1191 // of the corresponding notional static array types.
1192 for (; GEPI != E; ++GEPI, ++OI) {
1193 if (isa<UndefValue>(*OI))
1194 continue;
1195 auto *CI = dyn_cast<ConstantInt>(*OI);
1196 if (!CI || (GEPI.isBoundedSequential() &&
1197 (CI->getValue().getActiveBits() > 64 ||
1198 CI->getZExtValue() >= GEPI.getSequentialNumElements())))
1199 return false;
1202 // All the indices checked out.
1203 return true;
1206 bool ConstantExpr::hasIndices() const {
1207 return getOpcode() == Instruction::ExtractValue ||
1208 getOpcode() == Instruction::InsertValue;
1211 ArrayRef<unsigned> ConstantExpr::getIndices() const {
1212 if (const ExtractValueConstantExpr *EVCE =
1213 dyn_cast<ExtractValueConstantExpr>(this))
1214 return EVCE->Indices;
1216 return cast<InsertValueConstantExpr>(this)->Indices;
1219 unsigned ConstantExpr::getPredicate() const {
1220 return cast<CompareConstantExpr>(this)->predicate;
1223 Constant *
1224 ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
1225 assert(Op->getType() == getOperand(OpNo)->getType() &&
1226 "Replacing operand with value of different type!");
1227 if (getOperand(OpNo) == Op)
1228 return const_cast<ConstantExpr*>(this);
1230 SmallVector<Constant*, 8> NewOps;
1231 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1232 NewOps.push_back(i == OpNo ? Op : getOperand(i));
1234 return getWithOperands(NewOps);
1237 Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
1238 bool OnlyIfReduced, Type *SrcTy) const {
1239 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
1241 // If no operands changed return self.
1242 if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin()))
1243 return const_cast<ConstantExpr*>(this);
1245 Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;
1246 switch (getOpcode()) {
1247 case Instruction::Trunc:
1248 case Instruction::ZExt:
1249 case Instruction::SExt:
1250 case Instruction::FPTrunc:
1251 case Instruction::FPExt:
1252 case Instruction::UIToFP:
1253 case Instruction::SIToFP:
1254 case Instruction::FPToUI:
1255 case Instruction::FPToSI:
1256 case Instruction::PtrToInt:
1257 case Instruction::IntToPtr:
1258 case Instruction::BitCast:
1259 case Instruction::AddrSpaceCast:
1260 return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced);
1261 case Instruction::Select:
1262 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2], OnlyIfReducedTy);
1263 case Instruction::InsertElement:
1264 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2],
1265 OnlyIfReducedTy);
1266 case Instruction::ExtractElement:
1267 return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);
1268 case Instruction::InsertValue:
1269 return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(),
1270 OnlyIfReducedTy);
1271 case Instruction::ExtractValue:
1272 return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy);
1273 case Instruction::ShuffleVector:
1274 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2],
1275 OnlyIfReducedTy);
1276 case Instruction::GetElementPtr: {
1277 auto *GEPO = cast<GEPOperator>(this);
1278 assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
1279 return ConstantExpr::getGetElementPtr(
1280 SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),
1281 GEPO->isInBounds(), GEPO->getInRangeIndex(), OnlyIfReducedTy);
1283 case Instruction::ICmp:
1284 case Instruction::FCmp:
1285 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1],
1286 OnlyIfReducedTy);
1287 default:
1288 assert(getNumOperands() == 2 && "Must be binary operator?");
1289 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData,
1290 OnlyIfReducedTy);
1295 //===----------------------------------------------------------------------===//
1296 // isValueValidForType implementations
1298 bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
1299 unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay
1300 if (Ty->isIntegerTy(1))
1301 return Val == 0 || Val == 1;
1302 return isUIntN(NumBits, Val);
1305 bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
1306 unsigned NumBits = Ty->getIntegerBitWidth();
1307 if (Ty->isIntegerTy(1))
1308 return Val == 0 || Val == 1 || Val == -1;
1309 return isIntN(NumBits, Val);
1312 bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
1313 // convert modifies in place, so make a copy.
1314 APFloat Val2 = APFloat(Val);
1315 bool losesInfo;
1316 switch (Ty->getTypeID()) {
1317 default:
1318 return false; // These can't be represented as floating point!
1320 // FIXME rounding mode needs to be more flexible
1321 case Type::HalfTyID: {
1322 if (&Val2.getSemantics() == &APFloat::IEEEhalf())
1323 return true;
1324 Val2.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &losesInfo);
1325 return !losesInfo;
1327 case Type::FloatTyID: {
1328 if (&Val2.getSemantics() == &APFloat::IEEEsingle())
1329 return true;
1330 Val2.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1331 return !losesInfo;
1333 case Type::DoubleTyID: {
1334 if (&Val2.getSemantics() == &APFloat::IEEEhalf() ||
1335 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1336 &Val2.getSemantics() == &APFloat::IEEEdouble())
1337 return true;
1338 Val2.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
1339 return !losesInfo;
1341 case Type::X86_FP80TyID:
1342 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1343 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1344 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1345 &Val2.getSemantics() == &APFloat::x87DoubleExtended();
1346 case Type::FP128TyID:
1347 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1348 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1349 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1350 &Val2.getSemantics() == &APFloat::IEEEquad();
1351 case Type::PPC_FP128TyID:
1352 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1353 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1354 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1355 &Val2.getSemantics() == &APFloat::PPCDoubleDouble();
1360 //===----------------------------------------------------------------------===//
1361 // Factory Function Implementation
1363 ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
1364 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
1365 "Cannot create an aggregate zero of non-aggregate type!");
1367 std::unique_ptr<ConstantAggregateZero> &Entry =
1368 Ty->getContext().pImpl->CAZConstants[Ty];
1369 if (!Entry)
1370 Entry.reset(new ConstantAggregateZero(Ty));
1372 return Entry.get();
1375 /// Remove the constant from the constant table.
1376 void ConstantAggregateZero::destroyConstantImpl() {
1377 getContext().pImpl->CAZConstants.erase(getType());
1380 /// Remove the constant from the constant table.
1381 void ConstantArray::destroyConstantImpl() {
1382 getType()->getContext().pImpl->ArrayConstants.remove(this);
1386 //---- ConstantStruct::get() implementation...
1389 /// Remove the constant from the constant table.
1390 void ConstantStruct::destroyConstantImpl() {
1391 getType()->getContext().pImpl->StructConstants.remove(this);
1394 /// Remove the constant from the constant table.
1395 void ConstantVector::destroyConstantImpl() {
1396 getType()->getContext().pImpl->VectorConstants.remove(this);
1399 Constant *Constant::getSplatValue() const {
1400 assert(this->getType()->isVectorTy() && "Only valid for vectors!");
1401 if (isa<ConstantAggregateZero>(this))
1402 return getNullValue(this->getType()->getVectorElementType());
1403 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
1404 return CV->getSplatValue();
1405 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
1406 return CV->getSplatValue();
1407 return nullptr;
1410 Constant *ConstantVector::getSplatValue() const {
1411 // Check out first element.
1412 Constant *Elt = getOperand(0);
1413 // Then make sure all remaining elements point to the same value.
1414 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1415 if (getOperand(I) != Elt)
1416 return nullptr;
1417 return Elt;
1420 const APInt &Constant::getUniqueInteger() const {
1421 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1422 return CI->getValue();
1423 assert(this->getSplatValue() && "Doesn't contain a unique integer!");
1424 const Constant *C = this->getAggregateElement(0U);
1425 assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");
1426 return cast<ConstantInt>(C)->getValue();
1429 //---- ConstantPointerNull::get() implementation.
1432 ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
1433 std::unique_ptr<ConstantPointerNull> &Entry =
1434 Ty->getContext().pImpl->CPNConstants[Ty];
1435 if (!Entry)
1436 Entry.reset(new ConstantPointerNull(Ty));
1438 return Entry.get();
1441 /// Remove the constant from the constant table.
1442 void ConstantPointerNull::destroyConstantImpl() {
1443 getContext().pImpl->CPNConstants.erase(getType());
1446 UndefValue *UndefValue::get(Type *Ty) {
1447 std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
1448 if (!Entry)
1449 Entry.reset(new UndefValue(Ty));
1451 return Entry.get();
1454 /// Remove the constant from the constant table.
1455 void UndefValue::destroyConstantImpl() {
1456 // Free the constant and any dangling references to it.
1457 getContext().pImpl->UVConstants.erase(getType());
1460 BlockAddress *BlockAddress::get(BasicBlock *BB) {
1461 assert(BB->getParent() && "Block must have a parent");
1462 return get(BB->getParent(), BB);
1465 BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1466 BlockAddress *&BA =
1467 F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
1468 if (!BA)
1469 BA = new BlockAddress(F, BB);
1471 assert(BA->getFunction() == F && "Basic block moved between functions");
1472 return BA;
1475 BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1476 : Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1477 &Op<0>(), 2) {
1478 setOperand(0, F);
1479 setOperand(1, BB);
1480 BB->AdjustBlockAddressRefCount(1);
1483 BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
1484 if (!BB->hasAddressTaken())
1485 return nullptr;
1487 const Function *F = BB->getParent();
1488 assert(F && "Block must have a parent");
1489 BlockAddress *BA =
1490 F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB));
1491 assert(BA && "Refcount and block address map disagree!");
1492 return BA;
1495 /// Remove the constant from the constant table.
1496 void BlockAddress::destroyConstantImpl() {
1497 getFunction()->getType()->getContext().pImpl
1498 ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
1499 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1502 Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {
1503 // This could be replacing either the Basic Block or the Function. In either
1504 // case, we have to remove the map entry.
1505 Function *NewF = getFunction();
1506 BasicBlock *NewBB = getBasicBlock();
1508 if (From == NewF)
1509 NewF = cast<Function>(To->stripPointerCasts());
1510 else {
1511 assert(From == NewBB && "From does not match any operand");
1512 NewBB = cast<BasicBlock>(To);
1515 // See if the 'new' entry already exists, if not, just update this in place
1516 // and return early.
1517 BlockAddress *&NewBA =
1518 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
1519 if (NewBA)
1520 return NewBA;
1522 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1524 // Remove the old entry, this can't cause the map to rehash (just a
1525 // tombstone will get added).
1526 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1527 getBasicBlock()));
1528 NewBA = this;
1529 setOperand(0, NewF);
1530 setOperand(1, NewBB);
1531 getBasicBlock()->AdjustBlockAddressRefCount(1);
1533 // If we just want to keep the existing value, then return null.
1534 // Callers know that this means we shouldn't delete this value.
1535 return nullptr;
1538 //---- ConstantExpr::get() implementations.
1541 /// This is a utility function to handle folding of casts and lookup of the
1542 /// cast in the ExprConstants map. It is used by the various get* methods below.
1543 static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
1544 bool OnlyIfReduced = false) {
1545 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1546 // Fold a few common cases
1547 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1548 return FC;
1550 if (OnlyIfReduced)
1551 return nullptr;
1553 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1555 // Look up the constant in the table first to ensure uniqueness.
1556 ConstantExprKeyType Key(opc, C);
1558 return pImpl->ExprConstants.getOrCreate(Ty, Key);
1561 Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
1562 bool OnlyIfReduced) {
1563 Instruction::CastOps opc = Instruction::CastOps(oc);
1564 assert(Instruction::isCast(opc) && "opcode out of range");
1565 assert(C && Ty && "Null arguments to getCast");
1566 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
1568 switch (opc) {
1569 default:
1570 llvm_unreachable("Invalid cast opcode");
1571 case Instruction::Trunc:
1572 return getTrunc(C, Ty, OnlyIfReduced);
1573 case Instruction::ZExt:
1574 return getZExt(C, Ty, OnlyIfReduced);
1575 case Instruction::SExt:
1576 return getSExt(C, Ty, OnlyIfReduced);
1577 case Instruction::FPTrunc:
1578 return getFPTrunc(C, Ty, OnlyIfReduced);
1579 case Instruction::FPExt:
1580 return getFPExtend(C, Ty, OnlyIfReduced);
1581 case Instruction::UIToFP:
1582 return getUIToFP(C, Ty, OnlyIfReduced);
1583 case Instruction::SIToFP:
1584 return getSIToFP(C, Ty, OnlyIfReduced);
1585 case Instruction::FPToUI:
1586 return getFPToUI(C, Ty, OnlyIfReduced);
1587 case Instruction::FPToSI:
1588 return getFPToSI(C, Ty, OnlyIfReduced);
1589 case Instruction::PtrToInt:
1590 return getPtrToInt(C, Ty, OnlyIfReduced);
1591 case Instruction::IntToPtr:
1592 return getIntToPtr(C, Ty, OnlyIfReduced);
1593 case Instruction::BitCast:
1594 return getBitCast(C, Ty, OnlyIfReduced);
1595 case Instruction::AddrSpaceCast:
1596 return getAddrSpaceCast(C, Ty, OnlyIfReduced);
1600 Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) {
1601 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
1602 return getBitCast(C, Ty);
1603 return getZExt(C, Ty);
1606 Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) {
1607 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
1608 return getBitCast(C, Ty);
1609 return getSExt(C, Ty);
1612 Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
1613 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
1614 return getBitCast(C, Ty);
1615 return getTrunc(C, Ty);
1618 Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
1619 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1620 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
1621 "Invalid cast");
1623 if (Ty->isIntOrIntVectorTy())
1624 return getPtrToInt(S, Ty);
1626 unsigned SrcAS = S->getType()->getPointerAddressSpace();
1627 if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
1628 return getAddrSpaceCast(S, Ty);
1630 return getBitCast(S, Ty);
1633 Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
1634 Type *Ty) {
1635 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1636 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
1638 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
1639 return getAddrSpaceCast(S, Ty);
1641 return getBitCast(S, Ty);
1644 Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty, bool isSigned) {
1645 assert(C->getType()->isIntOrIntVectorTy() &&
1646 Ty->isIntOrIntVectorTy() && "Invalid cast");
1647 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1648 unsigned DstBits = Ty->getScalarSizeInBits();
1649 Instruction::CastOps opcode =
1650 (SrcBits == DstBits ? Instruction::BitCast :
1651 (SrcBits > DstBits ? Instruction::Trunc :
1652 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1653 return getCast(opcode, C, Ty);
1656 Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) {
1657 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
1658 "Invalid cast");
1659 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1660 unsigned DstBits = Ty->getScalarSizeInBits();
1661 if (SrcBits == DstBits)
1662 return C; // Avoid a useless cast
1663 Instruction::CastOps opcode =
1664 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
1665 return getCast(opcode, C, Ty);
1668 Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
1669 #ifndef NDEBUG
1670 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1671 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1672 #endif
1673 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1674 assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
1675 assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
1676 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
1677 "SrcTy must be larger than DestTy for Trunc!");
1679 return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced);
1682 Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
1683 #ifndef NDEBUG
1684 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1685 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1686 #endif
1687 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1688 assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
1689 assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
1690 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
1691 "SrcTy must be smaller than DestTy for SExt!");
1693 return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced);
1696 Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
1697 #ifndef NDEBUG
1698 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1699 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1700 #endif
1701 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1702 assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
1703 assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
1704 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
1705 "SrcTy must be smaller than DestTy for ZExt!");
1707 return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced);
1710 Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
1711 #ifndef NDEBUG
1712 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1713 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1714 #endif
1715 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1716 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
1717 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
1718 "This is an illegal floating point truncation!");
1719 return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced);
1722 Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced) {
1723 #ifndef NDEBUG
1724 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1725 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1726 #endif
1727 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1728 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
1729 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
1730 "This is an illegal floating point extension!");
1731 return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced);
1734 Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
1735 #ifndef NDEBUG
1736 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1737 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1738 #endif
1739 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1740 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
1741 "This is an illegal uint to floating point cast!");
1742 return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced);
1745 Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
1746 #ifndef NDEBUG
1747 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1748 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1749 #endif
1750 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1751 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
1752 "This is an illegal sint to floating point cast!");
1753 return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced);
1756 Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced) {
1757 #ifndef NDEBUG
1758 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1759 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1760 #endif
1761 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1762 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
1763 "This is an illegal floating point to uint cast!");
1764 return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced);
1767 Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) {
1768 #ifndef NDEBUG
1769 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1770 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1771 #endif
1772 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1773 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
1774 "This is an illegal floating point to sint cast!");
1775 return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced);
1778 Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
1779 bool OnlyIfReduced) {
1780 assert(C->getType()->isPtrOrPtrVectorTy() &&
1781 "PtrToInt source must be pointer or pointer vector");
1782 assert(DstTy->isIntOrIntVectorTy() &&
1783 "PtrToInt destination must be integer or integer vector");
1784 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
1785 if (isa<VectorType>(C->getType()))
1786 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
1787 "Invalid cast between a different number of vector elements");
1788 return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);
1791 Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
1792 bool OnlyIfReduced) {
1793 assert(C->getType()->isIntOrIntVectorTy() &&
1794 "IntToPtr source must be integer or integer vector");
1795 assert(DstTy->isPtrOrPtrVectorTy() &&
1796 "IntToPtr destination must be a pointer or pointer vector");
1797 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
1798 if (isa<VectorType>(C->getType()))
1799 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
1800 "Invalid cast between a different number of vector elements");
1801 return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced);
1804 Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
1805 bool OnlyIfReduced) {
1806 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1807 "Invalid constantexpr bitcast!");
1809 // It is common to ask for a bitcast of a value to its own type, handle this
1810 // speedily.
1811 if (C->getType() == DstTy) return C;
1813 return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced);
1816 Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
1817 bool OnlyIfReduced) {
1818 assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
1819 "Invalid constantexpr addrspacecast!");
1821 // Canonicalize addrspacecasts between different pointer types by first
1822 // bitcasting the pointer type and then converting the address space.
1823 PointerType *SrcScalarTy = cast<PointerType>(C->getType()->getScalarType());
1824 PointerType *DstScalarTy = cast<PointerType>(DstTy->getScalarType());
1825 Type *DstElemTy = DstScalarTy->getElementType();
1826 if (SrcScalarTy->getElementType() != DstElemTy) {
1827 Type *MidTy = PointerType::get(DstElemTy, SrcScalarTy->getAddressSpace());
1828 if (VectorType *VT = dyn_cast<VectorType>(DstTy)) {
1829 // Handle vectors of pointers.
1830 MidTy = VectorType::get(MidTy, VT->getNumElements());
1832 C = getBitCast(C, MidTy);
1834 return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);
1837 Constant *ConstantExpr::get(unsigned Opcode, Constant *C, unsigned Flags,
1838 Type *OnlyIfReducedTy) {
1839 // Check the operands for consistency first.
1840 assert(Instruction::isUnaryOp(Opcode) &&
1841 "Invalid opcode in unary constant expression");
1843 #ifndef NDEBUG
1844 switch (Opcode) {
1845 case Instruction::FNeg:
1846 assert(C->getType()->isFPOrFPVectorTy() &&
1847 "Tried to create a floating-point operation on a "
1848 "non-floating-point type!");
1849 break;
1850 default:
1851 break;
1853 #endif
1855 if (Constant *FC = ConstantFoldUnaryInstruction(Opcode, C))
1856 return FC;
1858 if (OnlyIfReducedTy == C->getType())
1859 return nullptr;
1861 Constant *ArgVec[] = { C };
1862 ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
1864 LLVMContextImpl *pImpl = C->getContext().pImpl;
1865 return pImpl->ExprConstants.getOrCreate(C->getType(), Key);
1868 Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
1869 unsigned Flags, Type *OnlyIfReducedTy) {
1870 // Check the operands for consistency first.
1871 assert(Instruction::isBinaryOp(Opcode) &&
1872 "Invalid opcode in binary constant expression");
1873 assert(C1->getType() == C2->getType() &&
1874 "Operand types in binary constant expression should match");
1876 #ifndef NDEBUG
1877 switch (Opcode) {
1878 case Instruction::Add:
1879 case Instruction::Sub:
1880 case Instruction::Mul:
1881 case Instruction::UDiv:
1882 case Instruction::SDiv:
1883 case Instruction::URem:
1884 case Instruction::SRem:
1885 assert(C1->getType()->isIntOrIntVectorTy() &&
1886 "Tried to create an integer operation on a non-integer type!");
1887 break;
1888 case Instruction::FAdd:
1889 case Instruction::FSub:
1890 case Instruction::FMul:
1891 case Instruction::FDiv:
1892 case Instruction::FRem:
1893 assert(C1->getType()->isFPOrFPVectorTy() &&
1894 "Tried to create a floating-point operation on a "
1895 "non-floating-point type!");
1896 break;
1897 case Instruction::And:
1898 case Instruction::Or:
1899 case Instruction::Xor:
1900 assert(C1->getType()->isIntOrIntVectorTy() &&
1901 "Tried to create a logical operation on a non-integral type!");
1902 break;
1903 case Instruction::Shl:
1904 case Instruction::LShr:
1905 case Instruction::AShr:
1906 assert(C1->getType()->isIntOrIntVectorTy() &&
1907 "Tried to create a shift operation on a non-integer type!");
1908 break;
1909 default:
1910 break;
1912 #endif
1914 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1915 return FC;
1917 if (OnlyIfReducedTy == C1->getType())
1918 return nullptr;
1920 Constant *ArgVec[] = { C1, C2 };
1921 ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
1923 LLVMContextImpl *pImpl = C1->getContext().pImpl;
1924 return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
1927 Constant *ConstantExpr::getSizeOf(Type* Ty) {
1928 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1929 // Note that a non-inbounds gep is used, as null isn't within any object.
1930 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
1931 Constant *GEP = getGetElementPtr(
1932 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
1933 return getPtrToInt(GEP,
1934 Type::getInt64Ty(Ty->getContext()));
1937 Constant *ConstantExpr::getAlignOf(Type* Ty) {
1938 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
1939 // Note that a non-inbounds gep is used, as null isn't within any object.
1940 Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);
1941 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
1942 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
1943 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
1944 Constant *Indices[2] = { Zero, One };
1945 Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);
1946 return getPtrToInt(GEP,
1947 Type::getInt64Ty(Ty->getContext()));
1950 Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) {
1951 return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1952 FieldNo));
1955 Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) {
1956 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1957 // Note that a non-inbounds gep is used, as null isn't within any object.
1958 Constant *GEPIdx[] = {
1959 ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1960 FieldNo
1962 Constant *GEP = getGetElementPtr(
1963 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
1964 return getPtrToInt(GEP,
1965 Type::getInt64Ty(Ty->getContext()));
1968 Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1,
1969 Constant *C2, bool OnlyIfReduced) {
1970 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1972 switch (Predicate) {
1973 default: llvm_unreachable("Invalid CmpInst predicate");
1974 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1975 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1976 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1977 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1978 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1979 case CmpInst::FCMP_TRUE:
1980 return getFCmp(Predicate, C1, C2, OnlyIfReduced);
1982 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1983 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1984 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1985 case CmpInst::ICMP_SLE:
1986 return getICmp(Predicate, C1, C2, OnlyIfReduced);
1990 Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2,
1991 Type *OnlyIfReducedTy) {
1992 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
1994 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1995 return SC; // Fold common cases
1997 if (OnlyIfReducedTy == V1->getType())
1998 return nullptr;
2000 Constant *ArgVec[] = { C, V1, V2 };
2001 ConstantExprKeyType Key(Instruction::Select, ArgVec);
2003 LLVMContextImpl *pImpl = C->getContext().pImpl;
2004 return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
2007 Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
2008 ArrayRef<Value *> Idxs, bool InBounds,
2009 Optional<unsigned> InRangeIndex,
2010 Type *OnlyIfReducedTy) {
2011 if (!Ty)
2012 Ty = cast<PointerType>(C->getType()->getScalarType())->getElementType();
2013 else
2014 assert(Ty ==
2015 cast<PointerType>(C->getType()->getScalarType())->getElementType());
2017 if (Constant *FC =
2018 ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs))
2019 return FC; // Fold a few common cases.
2021 // Get the result type of the getelementptr!
2022 Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs);
2023 assert(DestTy && "GEP indices invalid!");
2024 unsigned AS = C->getType()->getPointerAddressSpace();
2025 Type *ReqTy = DestTy->getPointerTo(AS);
2027 unsigned NumVecElts = 0;
2028 if (C->getType()->isVectorTy())
2029 NumVecElts = C->getType()->getVectorNumElements();
2030 else for (auto Idx : Idxs)
2031 if (Idx->getType()->isVectorTy())
2032 NumVecElts = Idx->getType()->getVectorNumElements();
2034 if (NumVecElts)
2035 ReqTy = VectorType::get(ReqTy, NumVecElts);
2037 if (OnlyIfReducedTy == ReqTy)
2038 return nullptr;
2040 // Look up the constant in the table first to ensure uniqueness
2041 std::vector<Constant*> ArgVec;
2042 ArgVec.reserve(1 + Idxs.size());
2043 ArgVec.push_back(C);
2044 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
2045 assert((!Idxs[i]->getType()->isVectorTy() ||
2046 Idxs[i]->getType()->getVectorNumElements() == NumVecElts) &&
2047 "getelementptr index type missmatch");
2049 Constant *Idx = cast<Constant>(Idxs[i]);
2050 if (NumVecElts && !Idxs[i]->getType()->isVectorTy())
2051 Idx = ConstantVector::getSplat(NumVecElts, Idx);
2052 ArgVec.push_back(Idx);
2055 unsigned SubClassOptionalData = InBounds ? GEPOperator::IsInBounds : 0;
2056 if (InRangeIndex && *InRangeIndex < 63)
2057 SubClassOptionalData |= (*InRangeIndex + 1) << 1;
2058 const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
2059 SubClassOptionalData, None, Ty);
2061 LLVMContextImpl *pImpl = C->getContext().pImpl;
2062 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2065 Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS,
2066 Constant *RHS, bool OnlyIfReduced) {
2067 assert(LHS->getType() == RHS->getType());
2068 assert(CmpInst::isIntPredicate((CmpInst::Predicate)pred) &&
2069 "Invalid ICmp Predicate");
2071 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
2072 return FC; // Fold a few common cases...
2074 if (OnlyIfReduced)
2075 return nullptr;
2077 // Look up the constant in the table first to ensure uniqueness
2078 Constant *ArgVec[] = { LHS, RHS };
2079 // Get the key type with both the opcode and predicate
2080 const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
2082 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
2083 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
2084 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
2086 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
2087 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
2090 Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS,
2091 Constant *RHS, bool OnlyIfReduced) {
2092 assert(LHS->getType() == RHS->getType());
2093 assert(CmpInst::isFPPredicate((CmpInst::Predicate)pred) &&
2094 "Invalid FCmp Predicate");
2096 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
2097 return FC; // Fold a few common cases...
2099 if (OnlyIfReduced)
2100 return nullptr;
2102 // Look up the constant in the table first to ensure uniqueness
2103 Constant *ArgVec[] = { LHS, RHS };
2104 // Get the key type with both the opcode and predicate
2105 const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
2107 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
2108 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
2109 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
2111 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
2112 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
2115 Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
2116 Type *OnlyIfReducedTy) {
2117 assert(Val->getType()->isVectorTy() &&
2118 "Tried to create extractelement operation on non-vector type!");
2119 assert(Idx->getType()->isIntegerTy() &&
2120 "Extractelement index must be an integer type!");
2122 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2123 return FC; // Fold a few common cases.
2125 Type *ReqTy = Val->getType()->getVectorElementType();
2126 if (OnlyIfReducedTy == ReqTy)
2127 return nullptr;
2129 // Look up the constant in the table first to ensure uniqueness
2130 Constant *ArgVec[] = { Val, Idx };
2131 const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
2133 LLVMContextImpl *pImpl = Val->getContext().pImpl;
2134 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2137 Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2138 Constant *Idx, Type *OnlyIfReducedTy) {
2139 assert(Val->getType()->isVectorTy() &&
2140 "Tried to create insertelement operation on non-vector type!");
2141 assert(Elt->getType() == Val->getType()->getVectorElementType() &&
2142 "Insertelement types must match!");
2143 assert(Idx->getType()->isIntegerTy() &&
2144 "Insertelement index must be i32 type!");
2146 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2147 return FC; // Fold a few common cases.
2149 if (OnlyIfReducedTy == Val->getType())
2150 return nullptr;
2152 // Look up the constant in the table first to ensure uniqueness
2153 Constant *ArgVec[] = { Val, Elt, Idx };
2154 const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
2156 LLVMContextImpl *pImpl = Val->getContext().pImpl;
2157 return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
2160 Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2161 Constant *Mask, Type *OnlyIfReducedTy) {
2162 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2163 "Invalid shuffle vector constant expr operands!");
2165 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2166 return FC; // Fold a few common cases.
2168 unsigned NElts = Mask->getType()->getVectorNumElements();
2169 Type *EltTy = V1->getType()->getVectorElementType();
2170 Type *ShufTy = VectorType::get(EltTy, NElts);
2172 if (OnlyIfReducedTy == ShufTy)
2173 return nullptr;
2175 // Look up the constant in the table first to ensure uniqueness
2176 Constant *ArgVec[] = { V1, V2, Mask };
2177 const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec);
2179 LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
2180 return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
2183 Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
2184 ArrayRef<unsigned> Idxs,
2185 Type *OnlyIfReducedTy) {
2186 assert(Agg->getType()->isFirstClassType() &&
2187 "Non-first-class type for constant insertvalue expression");
2189 assert(ExtractValueInst::getIndexedType(Agg->getType(),
2190 Idxs) == Val->getType() &&
2191 "insertvalue indices invalid!");
2192 Type *ReqTy = Val->getType();
2194 if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
2195 return FC;
2197 if (OnlyIfReducedTy == ReqTy)
2198 return nullptr;
2200 Constant *ArgVec[] = { Agg, Val };
2201 const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
2203 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2204 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2207 Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
2208 Type *OnlyIfReducedTy) {
2209 assert(Agg->getType()->isFirstClassType() &&
2210 "Tried to create extractelement operation on non-first-class type!");
2212 Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
2213 (void)ReqTy;
2214 assert(ReqTy && "extractvalue indices invalid!");
2216 assert(Agg->getType()->isFirstClassType() &&
2217 "Non-first-class type for constant extractvalue expression");
2218 if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs))
2219 return FC;
2221 if (OnlyIfReducedTy == ReqTy)
2222 return nullptr;
2224 Constant *ArgVec[] = { Agg };
2225 const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
2227 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2228 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2231 Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
2232 assert(C->getType()->isIntOrIntVectorTy() &&
2233 "Cannot NEG a nonintegral value!");
2234 return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
2235 C, HasNUW, HasNSW);
2238 Constant *ConstantExpr::getFNeg(Constant *C) {
2239 assert(C->getType()->isFPOrFPVectorTy() &&
2240 "Cannot FNEG a non-floating-point value!");
2241 return get(Instruction::FNeg, C);
2244 Constant *ConstantExpr::getNot(Constant *C) {
2245 assert(C->getType()->isIntOrIntVectorTy() &&
2246 "Cannot NOT a nonintegral value!");
2247 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
2250 Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
2251 bool HasNUW, bool HasNSW) {
2252 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2253 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2254 return get(Instruction::Add, C1, C2, Flags);
2257 Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
2258 return get(Instruction::FAdd, C1, C2);
2261 Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
2262 bool HasNUW, bool HasNSW) {
2263 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2264 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2265 return get(Instruction::Sub, C1, C2, Flags);
2268 Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
2269 return get(Instruction::FSub, C1, C2);
2272 Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
2273 bool HasNUW, bool HasNSW) {
2274 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2275 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2276 return get(Instruction::Mul, C1, C2, Flags);
2279 Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
2280 return get(Instruction::FMul, C1, C2);
2283 Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
2284 return get(Instruction::UDiv, C1, C2,
2285 isExact ? PossiblyExactOperator::IsExact : 0);
2288 Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
2289 return get(Instruction::SDiv, C1, C2,
2290 isExact ? PossiblyExactOperator::IsExact : 0);
2293 Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
2294 return get(Instruction::FDiv, C1, C2);
2297 Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
2298 return get(Instruction::URem, C1, C2);
2301 Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
2302 return get(Instruction::SRem, C1, C2);
2305 Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
2306 return get(Instruction::FRem, C1, C2);
2309 Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
2310 return get(Instruction::And, C1, C2);
2313 Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
2314 return get(Instruction::Or, C1, C2);
2317 Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
2318 return get(Instruction::Xor, C1, C2);
2321 Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
2322 bool HasNUW, bool HasNSW) {
2323 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2324 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2325 return get(Instruction::Shl, C1, C2, Flags);
2328 Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
2329 return get(Instruction::LShr, C1, C2,
2330 isExact ? PossiblyExactOperator::IsExact : 0);
2333 Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
2334 return get(Instruction::AShr, C1, C2,
2335 isExact ? PossiblyExactOperator::IsExact : 0);
2338 Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty,
2339 bool AllowRHSConstant) {
2340 assert(Instruction::isBinaryOp(Opcode) && "Only binops allowed");
2342 // Commutative opcodes: it does not matter if AllowRHSConstant is set.
2343 if (Instruction::isCommutative(Opcode)) {
2344 switch (Opcode) {
2345 case Instruction::Add: // X + 0 = X
2346 case Instruction::Or: // X | 0 = X
2347 case Instruction::Xor: // X ^ 0 = X
2348 return Constant::getNullValue(Ty);
2349 case Instruction::Mul: // X * 1 = X
2350 return ConstantInt::get(Ty, 1);
2351 case Instruction::And: // X & -1 = X
2352 return Constant::getAllOnesValue(Ty);
2353 case Instruction::FAdd: // X + -0.0 = X
2354 // TODO: If the fadd has 'nsz', should we return +0.0?
2355 return ConstantFP::getNegativeZero(Ty);
2356 case Instruction::FMul: // X * 1.0 = X
2357 return ConstantFP::get(Ty, 1.0);
2358 default:
2359 llvm_unreachable("Every commutative binop has an identity constant");
2363 // Non-commutative opcodes: AllowRHSConstant must be set.
2364 if (!AllowRHSConstant)
2365 return nullptr;
2367 switch (Opcode) {
2368 case Instruction::Sub: // X - 0 = X
2369 case Instruction::Shl: // X << 0 = X
2370 case Instruction::LShr: // X >>u 0 = X
2371 case Instruction::AShr: // X >> 0 = X
2372 case Instruction::FSub: // X - 0.0 = X
2373 return Constant::getNullValue(Ty);
2374 case Instruction::SDiv: // X / 1 = X
2375 case Instruction::UDiv: // X /u 1 = X
2376 return ConstantInt::get(Ty, 1);
2377 case Instruction::FDiv: // X / 1.0 = X
2378 return ConstantFP::get(Ty, 1.0);
2379 default:
2380 return nullptr;
2384 Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
2385 switch (Opcode) {
2386 default:
2387 // Doesn't have an absorber.
2388 return nullptr;
2390 case Instruction::Or:
2391 return Constant::getAllOnesValue(Ty);
2393 case Instruction::And:
2394 case Instruction::Mul:
2395 return Constant::getNullValue(Ty);
2399 /// Remove the constant from the constant table.
2400 void ConstantExpr::destroyConstantImpl() {
2401 getType()->getContext().pImpl->ExprConstants.remove(this);
2404 const char *ConstantExpr::getOpcodeName() const {
2405 return Instruction::getOpcodeName(getOpcode());
2408 GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2409 Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy)
2410 : ConstantExpr(DestTy, Instruction::GetElementPtr,
2411 OperandTraits<GetElementPtrConstantExpr>::op_end(this) -
2412 (IdxList.size() + 1),
2413 IdxList.size() + 1),
2414 SrcElementTy(SrcElementTy),
2415 ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)) {
2416 Op<0>() = C;
2417 Use *OperandList = getOperandList();
2418 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
2419 OperandList[i+1] = IdxList[i];
2422 Type *GetElementPtrConstantExpr::getSourceElementType() const {
2423 return SrcElementTy;
2426 Type *GetElementPtrConstantExpr::getResultElementType() const {
2427 return ResElementTy;
2430 //===----------------------------------------------------------------------===//
2431 // ConstantData* implementations
2433 Type *ConstantDataSequential::getElementType() const {
2434 return getType()->getElementType();
2437 StringRef ConstantDataSequential::getRawDataValues() const {
2438 return StringRef(DataElements, getNumElements()*getElementByteSize());
2441 bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
2442 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) return true;
2443 if (auto *IT = dyn_cast<IntegerType>(Ty)) {
2444 switch (IT->getBitWidth()) {
2445 case 8:
2446 case 16:
2447 case 32:
2448 case 64:
2449 return true;
2450 default: break;
2453 return false;
2456 unsigned ConstantDataSequential::getNumElements() const {
2457 if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
2458 return AT->getNumElements();
2459 return getType()->getVectorNumElements();
2463 uint64_t ConstantDataSequential::getElementByteSize() const {
2464 return getElementType()->getPrimitiveSizeInBits()/8;
2467 /// Return the start of the specified element.
2468 const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
2469 assert(Elt < getNumElements() && "Invalid Elt");
2470 return DataElements+Elt*getElementByteSize();
2474 /// Return true if the array is empty or all zeros.
2475 static bool isAllZeros(StringRef Arr) {
2476 for (char I : Arr)
2477 if (I != 0)
2478 return false;
2479 return true;
2482 /// This is the underlying implementation of all of the
2483 /// ConstantDataSequential::get methods. They all thunk down to here, providing
2484 /// the correct element type. We take the bytes in as a StringRef because
2485 /// we *want* an underlying "char*" to avoid TBAA type punning violations.
2486 Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
2487 assert(isElementTypeCompatible(Ty->getSequentialElementType()));
2488 // If the elements are all zero or there are no elements, return a CAZ, which
2489 // is more dense and canonical.
2490 if (isAllZeros(Elements))
2491 return ConstantAggregateZero::get(Ty);
2493 // Do a lookup to see if we have already formed one of these.
2494 auto &Slot =
2495 *Ty->getContext()
2496 .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr))
2497 .first;
2499 // The bucket can point to a linked list of different CDS's that have the same
2500 // body but different types. For example, 0,0,0,1 could be a 4 element array
2501 // of i8, or a 1-element array of i32. They'll both end up in the same
2502 /// StringMap bucket, linked up by their Next pointers. Walk the list.
2503 ConstantDataSequential **Entry = &Slot.second;
2504 for (ConstantDataSequential *Node = *Entry; Node;
2505 Entry = &Node->Next, Node = *Entry)
2506 if (Node->getType() == Ty)
2507 return Node;
2509 // Okay, we didn't get a hit. Create a node of the right class, link it in,
2510 // and return it.
2511 if (isa<ArrayType>(Ty))
2512 return *Entry = new ConstantDataArray(Ty, Slot.first().data());
2514 assert(isa<VectorType>(Ty));
2515 return *Entry = new ConstantDataVector(Ty, Slot.first().data());
2518 void ConstantDataSequential::destroyConstantImpl() {
2519 // Remove the constant from the StringMap.
2520 StringMap<ConstantDataSequential*> &CDSConstants =
2521 getType()->getContext().pImpl->CDSConstants;
2523 StringMap<ConstantDataSequential*>::iterator Slot =
2524 CDSConstants.find(getRawDataValues());
2526 assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
2528 ConstantDataSequential **Entry = &Slot->getValue();
2530 // Remove the entry from the hash table.
2531 if (!(*Entry)->Next) {
2532 // If there is only one value in the bucket (common case) it must be this
2533 // entry, and removing the entry should remove the bucket completely.
2534 assert((*Entry) == this && "Hash mismatch in ConstantDataSequential");
2535 getContext().pImpl->CDSConstants.erase(Slot);
2536 } else {
2537 // Otherwise, there are multiple entries linked off the bucket, unlink the
2538 // node we care about but keep the bucket around.
2539 for (ConstantDataSequential *Node = *Entry; ;
2540 Entry = &Node->Next, Node = *Entry) {
2541 assert(Node && "Didn't find entry in its uniquing hash table!");
2542 // If we found our entry, unlink it from the list and we're done.
2543 if (Node == this) {
2544 *Entry = Node->Next;
2545 break;
2550 // If we were part of a list, make sure that we don't delete the list that is
2551 // still owned by the uniquing map.
2552 Next = nullptr;
2555 /// getFP() constructors - Return a constant with array type with an element
2556 /// count and element type of float with precision matching the number of
2557 /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2558 /// double for 64bits) Note that this can return a ConstantAggregateZero
2559 /// object.
2560 Constant *ConstantDataArray::getFP(LLVMContext &Context,
2561 ArrayRef<uint16_t> Elts) {
2562 Type *Ty = ArrayType::get(Type::getHalfTy(Context), Elts.size());
2563 const char *Data = reinterpret_cast<const char *>(Elts.data());
2564 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
2566 Constant *ConstantDataArray::getFP(LLVMContext &Context,
2567 ArrayRef<uint32_t> Elts) {
2568 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
2569 const char *Data = reinterpret_cast<const char *>(Elts.data());
2570 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
2572 Constant *ConstantDataArray::getFP(LLVMContext &Context,
2573 ArrayRef<uint64_t> Elts) {
2574 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
2575 const char *Data = reinterpret_cast<const char *>(Elts.data());
2576 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
2579 Constant *ConstantDataArray::getString(LLVMContext &Context,
2580 StringRef Str, bool AddNull) {
2581 if (!AddNull) {
2582 const uint8_t *Data = Str.bytes_begin();
2583 return get(Context, makeArrayRef(Data, Str.size()));
2586 SmallVector<uint8_t, 64> ElementVals;
2587 ElementVals.append(Str.begin(), Str.end());
2588 ElementVals.push_back(0);
2589 return get(Context, ElementVals);
2592 /// get() constructors - Return a constant with vector type with an element
2593 /// count and element type matching the ArrayRef passed in. Note that this
2594 /// can return a ConstantAggregateZero object.
2595 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
2596 Type *Ty = VectorType::get(Type::getInt8Ty(Context), Elts.size());
2597 const char *Data = reinterpret_cast<const char *>(Elts.data());
2598 return getImpl(StringRef(Data, Elts.size() * 1), Ty);
2600 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
2601 Type *Ty = VectorType::get(Type::getInt16Ty(Context), Elts.size());
2602 const char *Data = reinterpret_cast<const char *>(Elts.data());
2603 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
2605 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
2606 Type *Ty = VectorType::get(Type::getInt32Ty(Context), Elts.size());
2607 const char *Data = reinterpret_cast<const char *>(Elts.data());
2608 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
2610 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
2611 Type *Ty = VectorType::get(Type::getInt64Ty(Context), Elts.size());
2612 const char *Data = reinterpret_cast<const char *>(Elts.data());
2613 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
2615 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
2616 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
2617 const char *Data = reinterpret_cast<const char *>(Elts.data());
2618 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
2620 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
2621 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
2622 const char *Data = reinterpret_cast<const char *>(Elts.data());
2623 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
2626 /// getFP() constructors - Return a constant with vector type with an element
2627 /// count and element type of float with the precision matching the number of
2628 /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2629 /// double for 64bits) Note that this can return a ConstantAggregateZero
2630 /// object.
2631 Constant *ConstantDataVector::getFP(LLVMContext &Context,
2632 ArrayRef<uint16_t> Elts) {
2633 Type *Ty = VectorType::get(Type::getHalfTy(Context), Elts.size());
2634 const char *Data = reinterpret_cast<const char *>(Elts.data());
2635 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
2637 Constant *ConstantDataVector::getFP(LLVMContext &Context,
2638 ArrayRef<uint32_t> Elts) {
2639 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
2640 const char *Data = reinterpret_cast<const char *>(Elts.data());
2641 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
2643 Constant *ConstantDataVector::getFP(LLVMContext &Context,
2644 ArrayRef<uint64_t> Elts) {
2645 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
2646 const char *Data = reinterpret_cast<const char *>(Elts.data());
2647 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
2650 Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
2651 assert(isElementTypeCompatible(V->getType()) &&
2652 "Element type not compatible with ConstantData");
2653 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
2654 if (CI->getType()->isIntegerTy(8)) {
2655 SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
2656 return get(V->getContext(), Elts);
2658 if (CI->getType()->isIntegerTy(16)) {
2659 SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
2660 return get(V->getContext(), Elts);
2662 if (CI->getType()->isIntegerTy(32)) {
2663 SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
2664 return get(V->getContext(), Elts);
2666 assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
2667 SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
2668 return get(V->getContext(), Elts);
2671 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
2672 if (CFP->getType()->isHalfTy()) {
2673 SmallVector<uint16_t, 16> Elts(
2674 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2675 return getFP(V->getContext(), Elts);
2677 if (CFP->getType()->isFloatTy()) {
2678 SmallVector<uint32_t, 16> Elts(
2679 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2680 return getFP(V->getContext(), Elts);
2682 if (CFP->getType()->isDoubleTy()) {
2683 SmallVector<uint64_t, 16> Elts(
2684 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2685 return getFP(V->getContext(), Elts);
2688 return ConstantVector::getSplat(NumElts, V);
2692 uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
2693 assert(isa<IntegerType>(getElementType()) &&
2694 "Accessor can only be used when element is an integer");
2695 const char *EltPtr = getElementPointer(Elt);
2697 // The data is stored in host byte order, make sure to cast back to the right
2698 // type to load with the right endianness.
2699 switch (getElementType()->getIntegerBitWidth()) {
2700 default: llvm_unreachable("Invalid bitwidth for CDS");
2701 case 8:
2702 return *reinterpret_cast<const uint8_t *>(EltPtr);
2703 case 16:
2704 return *reinterpret_cast<const uint16_t *>(EltPtr);
2705 case 32:
2706 return *reinterpret_cast<const uint32_t *>(EltPtr);
2707 case 64:
2708 return *reinterpret_cast<const uint64_t *>(EltPtr);
2712 APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const {
2713 assert(isa<IntegerType>(getElementType()) &&
2714 "Accessor can only be used when element is an integer");
2715 const char *EltPtr = getElementPointer(Elt);
2717 // The data is stored in host byte order, make sure to cast back to the right
2718 // type to load with the right endianness.
2719 switch (getElementType()->getIntegerBitWidth()) {
2720 default: llvm_unreachable("Invalid bitwidth for CDS");
2721 case 8: {
2722 auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr);
2723 return APInt(8, EltVal);
2725 case 16: {
2726 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
2727 return APInt(16, EltVal);
2729 case 32: {
2730 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
2731 return APInt(32, EltVal);
2733 case 64: {
2734 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
2735 return APInt(64, EltVal);
2740 APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
2741 const char *EltPtr = getElementPointer(Elt);
2743 switch (getElementType()->getTypeID()) {
2744 default:
2745 llvm_unreachable("Accessor can only be used when element is float/double!");
2746 case Type::HalfTyID: {
2747 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
2748 return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal));
2750 case Type::FloatTyID: {
2751 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
2752 return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal));
2754 case Type::DoubleTyID: {
2755 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
2756 return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal));
2761 float ConstantDataSequential::getElementAsFloat(unsigned Elt) const {
2762 assert(getElementType()->isFloatTy() &&
2763 "Accessor can only be used when element is a 'float'");
2764 return *reinterpret_cast<const float *>(getElementPointer(Elt));
2767 double ConstantDataSequential::getElementAsDouble(unsigned Elt) const {
2768 assert(getElementType()->isDoubleTy() &&
2769 "Accessor can only be used when element is a 'float'");
2770 return *reinterpret_cast<const double *>(getElementPointer(Elt));
2773 Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
2774 if (getElementType()->isHalfTy() || getElementType()->isFloatTy() ||
2775 getElementType()->isDoubleTy())
2776 return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));
2778 return ConstantInt::get(getElementType(), getElementAsInteger(Elt));
2781 bool ConstantDataSequential::isString(unsigned CharSize) const {
2782 return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize);
2785 bool ConstantDataSequential::isCString() const {
2786 if (!isString())
2787 return false;
2789 StringRef Str = getAsString();
2791 // The last value must be nul.
2792 if (Str.back() != 0) return false;
2794 // Other elements must be non-nul.
2795 return Str.drop_back().find(0) == StringRef::npos;
2798 bool ConstantDataVector::isSplat() const {
2799 const char *Base = getRawDataValues().data();
2801 // Compare elements 1+ to the 0'th element.
2802 unsigned EltSize = getElementByteSize();
2803 for (unsigned i = 1, e = getNumElements(); i != e; ++i)
2804 if (memcmp(Base, Base+i*EltSize, EltSize))
2805 return false;
2807 return true;
2810 Constant *ConstantDataVector::getSplatValue() const {
2811 // If they're all the same, return the 0th one as a representative.
2812 return isSplat() ? getElementAsConstant(0) : nullptr;
2815 //===----------------------------------------------------------------------===//
2816 // handleOperandChange implementations
2818 /// Update this constant array to change uses of
2819 /// 'From' to be uses of 'To'. This must update the uniquing data structures
2820 /// etc.
2822 /// Note that we intentionally replace all uses of From with To here. Consider
2823 /// a large array that uses 'From' 1000 times. By handling this case all here,
2824 /// ConstantArray::handleOperandChange is only invoked once, and that
2825 /// single invocation handles all 1000 uses. Handling them one at a time would
2826 /// work, but would be really slow because it would have to unique each updated
2827 /// array instance.
2829 void Constant::handleOperandChange(Value *From, Value *To) {
2830 Value *Replacement = nullptr;
2831 switch (getValueID()) {
2832 default:
2833 llvm_unreachable("Not a constant!");
2834 #define HANDLE_CONSTANT(Name) \
2835 case Value::Name##Val: \
2836 Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
2837 break;
2838 #include "llvm/IR/Value.def"
2841 // If handleOperandChangeImpl returned nullptr, then it handled
2842 // replacing itself and we don't want to delete or replace anything else here.
2843 if (!Replacement)
2844 return;
2846 // I do need to replace this with an existing value.
2847 assert(Replacement != this && "I didn't contain From!");
2849 // Everyone using this now uses the replacement.
2850 replaceAllUsesWith(Replacement);
2852 // Delete the old constant!
2853 destroyConstant();
2856 Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
2857 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2858 Constant *ToC = cast<Constant>(To);
2860 SmallVector<Constant*, 8> Values;
2861 Values.reserve(getNumOperands()); // Build replacement array.
2863 // Fill values with the modified operands of the constant array. Also,
2864 // compute whether this turns into an all-zeros array.
2865 unsigned NumUpdated = 0;
2867 // Keep track of whether all the values in the array are "ToC".
2868 bool AllSame = true;
2869 Use *OperandList = getOperandList();
2870 unsigned OperandNo = 0;
2871 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2872 Constant *Val = cast<Constant>(O->get());
2873 if (Val == From) {
2874 OperandNo = (O - OperandList);
2875 Val = ToC;
2876 ++NumUpdated;
2878 Values.push_back(Val);
2879 AllSame &= Val == ToC;
2882 if (AllSame && ToC->isNullValue())
2883 return ConstantAggregateZero::get(getType());
2885 if (AllSame && isa<UndefValue>(ToC))
2886 return UndefValue::get(getType());
2888 // Check for any other type of constant-folding.
2889 if (Constant *C = getImpl(getType(), Values))
2890 return C;
2892 // Update to the new value.
2893 return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(
2894 Values, this, From, ToC, NumUpdated, OperandNo);
2897 Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
2898 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2899 Constant *ToC = cast<Constant>(To);
2901 Use *OperandList = getOperandList();
2903 SmallVector<Constant*, 8> Values;
2904 Values.reserve(getNumOperands()); // Build replacement struct.
2906 // Fill values with the modified operands of the constant struct. Also,
2907 // compute whether this turns into an all-zeros struct.
2908 unsigned NumUpdated = 0;
2909 bool AllSame = true;
2910 unsigned OperandNo = 0;
2911 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
2912 Constant *Val = cast<Constant>(O->get());
2913 if (Val == From) {
2914 OperandNo = (O - OperandList);
2915 Val = ToC;
2916 ++NumUpdated;
2918 Values.push_back(Val);
2919 AllSame &= Val == ToC;
2922 if (AllSame && ToC->isNullValue())
2923 return ConstantAggregateZero::get(getType());
2925 if (AllSame && isa<UndefValue>(ToC))
2926 return UndefValue::get(getType());
2928 // Update to the new value.
2929 return getContext().pImpl->StructConstants.replaceOperandsInPlace(
2930 Values, this, From, ToC, NumUpdated, OperandNo);
2933 Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
2934 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2935 Constant *ToC = cast<Constant>(To);
2937 SmallVector<Constant*, 8> Values;
2938 Values.reserve(getNumOperands()); // Build replacement array...
2939 unsigned NumUpdated = 0;
2940 unsigned OperandNo = 0;
2941 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2942 Constant *Val = getOperand(i);
2943 if (Val == From) {
2944 OperandNo = i;
2945 ++NumUpdated;
2946 Val = ToC;
2948 Values.push_back(Val);
2951 if (Constant *C = getImpl(Values))
2952 return C;
2954 // Update to the new value.
2955 return getContext().pImpl->VectorConstants.replaceOperandsInPlace(
2956 Values, this, From, ToC, NumUpdated, OperandNo);
2959 Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
2960 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2961 Constant *To = cast<Constant>(ToV);
2963 SmallVector<Constant*, 8> NewOps;
2964 unsigned NumUpdated = 0;
2965 unsigned OperandNo = 0;
2966 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2967 Constant *Op = getOperand(i);
2968 if (Op == From) {
2969 OperandNo = i;
2970 ++NumUpdated;
2971 Op = To;
2973 NewOps.push_back(Op);
2975 assert(NumUpdated && "I didn't contain From!");
2977 if (Constant *C = getWithOperands(NewOps, getType(), true))
2978 return C;
2980 // Update to the new value.
2981 return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
2982 NewOps, this, From, To, NumUpdated, OperandNo);
2985 Instruction *ConstantExpr::getAsInstruction() {
2986 SmallVector<Value *, 4> ValueOperands(op_begin(), op_end());
2987 ArrayRef<Value*> Ops(ValueOperands);
2989 switch (getOpcode()) {
2990 case Instruction::Trunc:
2991 case Instruction::ZExt:
2992 case Instruction::SExt:
2993 case Instruction::FPTrunc:
2994 case Instruction::FPExt:
2995 case Instruction::UIToFP:
2996 case Instruction::SIToFP:
2997 case Instruction::FPToUI:
2998 case Instruction::FPToSI:
2999 case Instruction::PtrToInt:
3000 case Instruction::IntToPtr:
3001 case Instruction::BitCast:
3002 case Instruction::AddrSpaceCast:
3003 return CastInst::Create((Instruction::CastOps)getOpcode(),
3004 Ops[0], getType());
3005 case Instruction::Select:
3006 return SelectInst::Create(Ops[0], Ops[1], Ops[2]);
3007 case Instruction::InsertElement:
3008 return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]);
3009 case Instruction::ExtractElement:
3010 return ExtractElementInst::Create(Ops[0], Ops[1]);
3011 case Instruction::InsertValue:
3012 return InsertValueInst::Create(Ops[0], Ops[1], getIndices());
3013 case Instruction::ExtractValue:
3014 return ExtractValueInst::Create(Ops[0], getIndices());
3015 case Instruction::ShuffleVector:
3016 return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]);
3018 case Instruction::GetElementPtr: {
3019 const auto *GO = cast<GEPOperator>(this);
3020 if (GO->isInBounds())
3021 return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
3022 Ops[0], Ops.slice(1));
3023 return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
3024 Ops.slice(1));
3026 case Instruction::ICmp:
3027 case Instruction::FCmp:
3028 return CmpInst::Create((Instruction::OtherOps)getOpcode(),
3029 (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]);
3030 case Instruction::FNeg:
3031 return UnaryOperator::Create((Instruction::UnaryOps)getOpcode(), Ops[0]);
3032 default:
3033 assert(getNumOperands() == 2 && "Must be binary operator?");
3034 BinaryOperator *BO =
3035 BinaryOperator::Create((Instruction::BinaryOps)getOpcode(),
3036 Ops[0], Ops[1]);
3037 if (isa<OverflowingBinaryOperator>(BO)) {
3038 BO->setHasNoUnsignedWrap(SubclassOptionalData &
3039 OverflowingBinaryOperator::NoUnsignedWrap);
3040 BO->setHasNoSignedWrap(SubclassOptionalData &
3041 OverflowingBinaryOperator::NoSignedWrap);
3043 if (isa<PossiblyExactOperator>(BO))
3044 BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);
3045 return BO;