1 //===-- Constants.cpp - Implement Constant nodes --------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the Constant* classes.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/Constants.h"
14 #include "LLVMContextImpl.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/IR/BasicBlock.h"
19 #include "llvm/IR/ConstantFold.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GetElementPtrTypeIterator.h"
23 #include "llvm/IR/GlobalAlias.h"
24 #include "llvm/IR/GlobalIFunc.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/Operator.h"
29 #include "llvm/IR/PatternMatch.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/Support/raw_ostream.h"
36 using namespace PatternMatch
;
38 //===----------------------------------------------------------------------===//
40 //===----------------------------------------------------------------------===//
42 bool Constant::isNegativeZeroValue() const {
43 // Floating point values have an explicit -0.0 value.
44 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
45 return CFP
->isZero() && CFP
->isNegative();
47 // Equivalent for a vector of -0.0's.
48 if (getType()->isVectorTy())
49 if (const auto *SplatCFP
= dyn_cast_or_null
<ConstantFP
>(getSplatValue()))
50 return SplatCFP
->isNegativeZeroValue();
52 // We've already handled true FP case; any other FP vectors can't represent -0.0.
53 if (getType()->isFPOrFPVectorTy())
56 // Otherwise, just use +0.0.
60 // Return true iff this constant is positive zero (floating point), negative
61 // zero (floating point), or a null value.
62 bool Constant::isZeroValue() const {
63 // Floating point values have an explicit -0.0 value.
64 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
67 // Check for constant splat vectors of 1 values.
68 if (getType()->isVectorTy())
69 if (const auto *SplatCFP
= dyn_cast_or_null
<ConstantFP
>(getSplatValue()))
70 return SplatCFP
->isZero();
72 // Otherwise, just use +0.0.
76 bool Constant::isNullValue() const {
78 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
82 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
83 // ppc_fp128 determine isZero using high order double only
84 // Should check the bitwise value to make sure all bits are zero.
85 return CFP
->isExactlyValue(+0.0);
87 // constant zero is zero for aggregates, cpnull is null for pointers, none for
89 return isa
<ConstantAggregateZero
>(this) || isa
<ConstantPointerNull
>(this) ||
90 isa
<ConstantTokenNone
>(this) || isa
<ConstantTargetNone
>(this);
93 bool Constant::isAllOnesValue() const {
94 // Check for -1 integers
95 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
96 return CI
->isMinusOne();
98 // Check for FP which are bitcasted from -1 integers
99 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
100 return CFP
->getValueAPF().bitcastToAPInt().isAllOnes();
102 // Check for constant splat vectors of 1 values.
103 if (getType()->isVectorTy())
104 if (const auto *SplatVal
= getSplatValue())
105 return SplatVal
->isAllOnesValue();
110 bool Constant::isOneValue() const {
111 // Check for 1 integers
112 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
115 // Check for FP which are bitcasted from 1 integers
116 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
117 return CFP
->getValueAPF().bitcastToAPInt().isOne();
119 // Check for constant splat vectors of 1 values.
120 if (getType()->isVectorTy())
121 if (const auto *SplatVal
= getSplatValue())
122 return SplatVal
->isOneValue();
127 bool Constant::isNotOneValue() const {
128 // Check for 1 integers
129 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
130 return !CI
->isOneValue();
132 // Check for FP which are bitcasted from 1 integers
133 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
134 return !CFP
->getValueAPF().bitcastToAPInt().isOne();
136 // Check that vectors don't contain 1
137 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
138 for (unsigned I
= 0, E
= VTy
->getNumElements(); I
!= E
; ++I
) {
139 Constant
*Elt
= getAggregateElement(I
);
140 if (!Elt
|| !Elt
->isNotOneValue())
146 // Check for splats that don't contain 1
147 if (getType()->isVectorTy())
148 if (const auto *SplatVal
= getSplatValue())
149 return SplatVal
->isNotOneValue();
151 // It *may* contain 1, we can't tell.
155 bool Constant::isMinSignedValue() const {
156 // Check for INT_MIN integers
157 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
158 return CI
->isMinValue(/*isSigned=*/true);
160 // Check for FP which are bitcasted from INT_MIN integers
161 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
162 return CFP
->getValueAPF().bitcastToAPInt().isMinSignedValue();
164 // Check for splats of INT_MIN values.
165 if (getType()->isVectorTy())
166 if (const auto *SplatVal
= getSplatValue())
167 return SplatVal
->isMinSignedValue();
172 bool Constant::isNotMinSignedValue() const {
173 // Check for INT_MIN integers
174 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
175 return !CI
->isMinValue(/*isSigned=*/true);
177 // Check for FP which are bitcasted from INT_MIN integers
178 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
179 return !CFP
->getValueAPF().bitcastToAPInt().isMinSignedValue();
181 // Check that vectors don't contain INT_MIN
182 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
183 for (unsigned I
= 0, E
= VTy
->getNumElements(); I
!= E
; ++I
) {
184 Constant
*Elt
= getAggregateElement(I
);
185 if (!Elt
|| !Elt
->isNotMinSignedValue())
191 // Check for splats that aren't INT_MIN
192 if (getType()->isVectorTy())
193 if (const auto *SplatVal
= getSplatValue())
194 return SplatVal
->isNotMinSignedValue();
196 // It *may* contain INT_MIN, we can't tell.
200 bool Constant::isFiniteNonZeroFP() const {
201 if (auto *CFP
= dyn_cast
<ConstantFP
>(this))
202 return CFP
->getValueAPF().isFiniteNonZero();
204 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
205 for (unsigned I
= 0, E
= VTy
->getNumElements(); I
!= E
; ++I
) {
206 auto *CFP
= dyn_cast_or_null
<ConstantFP
>(getAggregateElement(I
));
207 if (!CFP
|| !CFP
->getValueAPF().isFiniteNonZero())
213 if (getType()->isVectorTy())
214 if (const auto *SplatCFP
= dyn_cast_or_null
<ConstantFP
>(getSplatValue()))
215 return SplatCFP
->isFiniteNonZeroFP();
217 // It *may* contain finite non-zero, we can't tell.
221 bool Constant::isNormalFP() const {
222 if (auto *CFP
= dyn_cast
<ConstantFP
>(this))
223 return CFP
->getValueAPF().isNormal();
225 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
226 for (unsigned I
= 0, E
= VTy
->getNumElements(); I
!= E
; ++I
) {
227 auto *CFP
= dyn_cast_or_null
<ConstantFP
>(getAggregateElement(I
));
228 if (!CFP
|| !CFP
->getValueAPF().isNormal())
234 if (getType()->isVectorTy())
235 if (const auto *SplatCFP
= dyn_cast_or_null
<ConstantFP
>(getSplatValue()))
236 return SplatCFP
->isNormalFP();
238 // It *may* contain a normal fp value, we can't tell.
242 bool Constant::hasExactInverseFP() const {
243 if (auto *CFP
= dyn_cast
<ConstantFP
>(this))
244 return CFP
->getValueAPF().getExactInverse(nullptr);
246 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
247 for (unsigned I
= 0, E
= VTy
->getNumElements(); I
!= E
; ++I
) {
248 auto *CFP
= dyn_cast_or_null
<ConstantFP
>(getAggregateElement(I
));
249 if (!CFP
|| !CFP
->getValueAPF().getExactInverse(nullptr))
255 if (getType()->isVectorTy())
256 if (const auto *SplatCFP
= dyn_cast_or_null
<ConstantFP
>(getSplatValue()))
257 return SplatCFP
->hasExactInverseFP();
259 // It *may* have an exact inverse fp value, we can't tell.
263 bool Constant::isNaN() const {
264 if (auto *CFP
= dyn_cast
<ConstantFP
>(this))
267 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
268 for (unsigned I
= 0, E
= VTy
->getNumElements(); I
!= E
; ++I
) {
269 auto *CFP
= dyn_cast_or_null
<ConstantFP
>(getAggregateElement(I
));
270 if (!CFP
|| !CFP
->isNaN())
276 if (getType()->isVectorTy())
277 if (const auto *SplatCFP
= dyn_cast_or_null
<ConstantFP
>(getSplatValue()))
278 return SplatCFP
->isNaN();
280 // It *may* be NaN, we can't tell.
284 bool Constant::isElementWiseEqual(Value
*Y
) const {
285 // Are they fully identical?
289 // The input value must be a vector constant with the same type.
290 auto *VTy
= dyn_cast
<VectorType
>(getType());
291 if (!isa
<Constant
>(Y
) || !VTy
|| VTy
!= Y
->getType())
294 // TODO: Compare pointer constants?
295 if (!(VTy
->getElementType()->isIntegerTy() ||
296 VTy
->getElementType()->isFloatingPointTy()))
299 // They may still be identical element-wise (if they have `undef`s).
300 // Bitcast to integer to allow exact bitwise comparison for all types.
301 Type
*IntTy
= VectorType::getInteger(VTy
);
302 Constant
*C0
= ConstantExpr::getBitCast(const_cast<Constant
*>(this), IntTy
);
303 Constant
*C1
= ConstantExpr::getBitCast(cast
<Constant
>(Y
), IntTy
);
304 Constant
*CmpEq
= ConstantExpr::getICmp(ICmpInst::ICMP_EQ
, C0
, C1
);
305 return isa
<UndefValue
>(CmpEq
) || match(CmpEq
, m_One());
309 containsUndefinedElement(const Constant
*C
,
310 function_ref
<bool(const Constant
*)> HasFn
) {
311 if (auto *VTy
= dyn_cast
<VectorType
>(C
->getType())) {
314 if (isa
<ConstantAggregateZero
>(C
))
316 if (isa
<ScalableVectorType
>(C
->getType()))
319 for (unsigned i
= 0, e
= cast
<FixedVectorType
>(VTy
)->getNumElements();
321 if (Constant
*Elem
= C
->getAggregateElement(i
))
330 bool Constant::containsUndefOrPoisonElement() const {
331 return containsUndefinedElement(
332 this, [&](const auto *C
) { return isa
<UndefValue
>(C
); });
335 bool Constant::containsPoisonElement() const {
336 return containsUndefinedElement(
337 this, [&](const auto *C
) { return isa
<PoisonValue
>(C
); });
340 bool Constant::containsUndefElement() const {
341 return containsUndefinedElement(this, [&](const auto *C
) {
342 return isa
<UndefValue
>(C
) && !isa
<PoisonValue
>(C
);
346 bool Constant::containsConstantExpression() const {
347 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
348 for (unsigned i
= 0, e
= VTy
->getNumElements(); i
!= e
; ++i
)
349 if (isa
<ConstantExpr
>(getAggregateElement(i
)))
355 /// Constructor to create a '0' constant of arbitrary type.
356 Constant
*Constant::getNullValue(Type
*Ty
) {
357 switch (Ty
->getTypeID()) {
358 case Type::IntegerTyID
:
359 return ConstantInt::get(Ty
, 0);
361 case Type::BFloatTyID
:
362 case Type::FloatTyID
:
363 case Type::DoubleTyID
:
364 case Type::X86_FP80TyID
:
365 case Type::FP128TyID
:
366 case Type::PPC_FP128TyID
:
367 return ConstantFP::get(Ty
->getContext(),
368 APFloat::getZero(Ty
->getFltSemantics()));
369 case Type::PointerTyID
:
370 return ConstantPointerNull::get(cast
<PointerType
>(Ty
));
371 case Type::StructTyID
:
372 case Type::ArrayTyID
:
373 case Type::FixedVectorTyID
:
374 case Type::ScalableVectorTyID
:
375 return ConstantAggregateZero::get(Ty
);
376 case Type::TokenTyID
:
377 return ConstantTokenNone::get(Ty
->getContext());
378 case Type::TargetExtTyID
:
379 return ConstantTargetNone::get(cast
<TargetExtType
>(Ty
));
381 // Function, Label, or Opaque type?
382 llvm_unreachable("Cannot create a null constant of that type!");
386 Constant
*Constant::getIntegerValue(Type
*Ty
, const APInt
&V
) {
387 Type
*ScalarTy
= Ty
->getScalarType();
389 // Create the base integer constant.
390 Constant
*C
= ConstantInt::get(Ty
->getContext(), V
);
392 // Convert an integer to a pointer, if necessary.
393 if (PointerType
*PTy
= dyn_cast
<PointerType
>(ScalarTy
))
394 C
= ConstantExpr::getIntToPtr(C
, PTy
);
396 // Broadcast a scalar to a vector, if necessary.
397 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
398 C
= ConstantVector::getSplat(VTy
->getElementCount(), C
);
403 Constant
*Constant::getAllOnesValue(Type
*Ty
) {
404 if (IntegerType
*ITy
= dyn_cast
<IntegerType
>(Ty
))
405 return ConstantInt::get(Ty
->getContext(),
406 APInt::getAllOnes(ITy
->getBitWidth()));
408 if (Ty
->isFloatingPointTy()) {
409 APFloat FL
= APFloat::getAllOnesValue(Ty
->getFltSemantics());
410 return ConstantFP::get(Ty
->getContext(), FL
);
413 VectorType
*VTy
= cast
<VectorType
>(Ty
);
414 return ConstantVector::getSplat(VTy
->getElementCount(),
415 getAllOnesValue(VTy
->getElementType()));
418 Constant
*Constant::getAggregateElement(unsigned Elt
) const {
419 assert((getType()->isAggregateType() || getType()->isVectorTy()) &&
420 "Must be an aggregate/vector constant");
422 if (const auto *CC
= dyn_cast
<ConstantAggregate
>(this))
423 return Elt
< CC
->getNumOperands() ? CC
->getOperand(Elt
) : nullptr;
425 if (const auto *CAZ
= dyn_cast
<ConstantAggregateZero
>(this))
426 return Elt
< CAZ
->getElementCount().getKnownMinValue()
427 ? CAZ
->getElementValue(Elt
)
430 // FIXME: getNumElements() will fail for non-fixed vector types.
431 if (isa
<ScalableVectorType
>(getType()))
434 if (const auto *PV
= dyn_cast
<PoisonValue
>(this))
435 return Elt
< PV
->getNumElements() ? PV
->getElementValue(Elt
) : nullptr;
437 if (const auto *UV
= dyn_cast
<UndefValue
>(this))
438 return Elt
< UV
->getNumElements() ? UV
->getElementValue(Elt
) : nullptr;
440 if (const auto *CDS
= dyn_cast
<ConstantDataSequential
>(this))
441 return Elt
< CDS
->getNumElements() ? CDS
->getElementAsConstant(Elt
)
447 Constant
*Constant::getAggregateElement(Constant
*Elt
) const {
448 assert(isa
<IntegerType
>(Elt
->getType()) && "Index must be an integer");
449 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(Elt
)) {
450 // Check if the constant fits into an uint64_t.
451 if (CI
->getValue().getActiveBits() > 64)
453 return getAggregateElement(CI
->getZExtValue());
458 void Constant::destroyConstant() {
459 /// First call destroyConstantImpl on the subclass. This gives the subclass
460 /// a chance to remove the constant from any maps/pools it's contained in.
461 switch (getValueID()) {
463 llvm_unreachable("Not a constant!");
464 #define HANDLE_CONSTANT(Name) \
465 case Value::Name##Val: \
466 cast<Name>(this)->destroyConstantImpl(); \
468 #include "llvm/IR/Value.def"
471 // When a Constant is destroyed, there may be lingering
472 // references to the constant by other constants in the constant pool. These
473 // constants are implicitly dependent on the module that is being deleted,
474 // but they don't know that. Because we only find out when the CPV is
475 // deleted, we must now notify all of our users (that should only be
476 // Constants) that they are, in fact, invalid now and should be deleted.
478 while (!use_empty()) {
479 Value
*V
= user_back();
480 #ifndef NDEBUG // Only in -g mode...
481 if (!isa
<Constant
>(V
)) {
482 dbgs() << "While deleting: " << *this
483 << "\n\nUse still stuck around after Def is destroyed: " << *V
487 assert(isa
<Constant
>(V
) && "References remain to Constant being destroyed");
488 cast
<Constant
>(V
)->destroyConstant();
490 // The constant should remove itself from our use list...
491 assert((use_empty() || user_back() != V
) && "Constant not removed!");
494 // Value has no outstanding references it is safe to delete it now...
495 deleteConstant(this);
498 void llvm::deleteConstant(Constant
*C
) {
499 switch (C
->getValueID()) {
500 case Constant::ConstantIntVal
:
501 delete static_cast<ConstantInt
*>(C
);
503 case Constant::ConstantFPVal
:
504 delete static_cast<ConstantFP
*>(C
);
506 case Constant::ConstantAggregateZeroVal
:
507 delete static_cast<ConstantAggregateZero
*>(C
);
509 case Constant::ConstantArrayVal
:
510 delete static_cast<ConstantArray
*>(C
);
512 case Constant::ConstantStructVal
:
513 delete static_cast<ConstantStruct
*>(C
);
515 case Constant::ConstantVectorVal
:
516 delete static_cast<ConstantVector
*>(C
);
518 case Constant::ConstantPointerNullVal
:
519 delete static_cast<ConstantPointerNull
*>(C
);
521 case Constant::ConstantDataArrayVal
:
522 delete static_cast<ConstantDataArray
*>(C
);
524 case Constant::ConstantDataVectorVal
:
525 delete static_cast<ConstantDataVector
*>(C
);
527 case Constant::ConstantTokenNoneVal
:
528 delete static_cast<ConstantTokenNone
*>(C
);
530 case Constant::BlockAddressVal
:
531 delete static_cast<BlockAddress
*>(C
);
533 case Constant::DSOLocalEquivalentVal
:
534 delete static_cast<DSOLocalEquivalent
*>(C
);
536 case Constant::NoCFIValueVal
:
537 delete static_cast<NoCFIValue
*>(C
);
539 case Constant::UndefValueVal
:
540 delete static_cast<UndefValue
*>(C
);
542 case Constant::PoisonValueVal
:
543 delete static_cast<PoisonValue
*>(C
);
545 case Constant::ConstantExprVal
:
546 if (isa
<CastConstantExpr
>(C
))
547 delete static_cast<CastConstantExpr
*>(C
);
548 else if (isa
<BinaryConstantExpr
>(C
))
549 delete static_cast<BinaryConstantExpr
*>(C
);
550 else if (isa
<ExtractElementConstantExpr
>(C
))
551 delete static_cast<ExtractElementConstantExpr
*>(C
);
552 else if (isa
<InsertElementConstantExpr
>(C
))
553 delete static_cast<InsertElementConstantExpr
*>(C
);
554 else if (isa
<ShuffleVectorConstantExpr
>(C
))
555 delete static_cast<ShuffleVectorConstantExpr
*>(C
);
556 else if (isa
<GetElementPtrConstantExpr
>(C
))
557 delete static_cast<GetElementPtrConstantExpr
*>(C
);
558 else if (isa
<CompareConstantExpr
>(C
))
559 delete static_cast<CompareConstantExpr
*>(C
);
561 llvm_unreachable("Unexpected constant expr");
564 llvm_unreachable("Unexpected constant");
568 /// Check if C contains a GlobalValue for which Predicate is true.
570 ConstHasGlobalValuePredicate(const Constant
*C
,
571 bool (*Predicate
)(const GlobalValue
*)) {
572 SmallPtrSet
<const Constant
*, 8> Visited
;
573 SmallVector
<const Constant
*, 8> WorkList
;
574 WorkList
.push_back(C
);
577 while (!WorkList
.empty()) {
578 const Constant
*WorkItem
= WorkList
.pop_back_val();
579 if (const auto *GV
= dyn_cast
<GlobalValue
>(WorkItem
))
582 for (const Value
*Op
: WorkItem
->operands()) {
583 const Constant
*ConstOp
= dyn_cast
<Constant
>(Op
);
586 if (Visited
.insert(ConstOp
).second
)
587 WorkList
.push_back(ConstOp
);
593 bool Constant::isThreadDependent() const {
594 auto DLLImportPredicate
= [](const GlobalValue
*GV
) {
595 return GV
->isThreadLocal();
597 return ConstHasGlobalValuePredicate(this, DLLImportPredicate
);
600 bool Constant::isDLLImportDependent() const {
601 auto DLLImportPredicate
= [](const GlobalValue
*GV
) {
602 return GV
->hasDLLImportStorageClass();
604 return ConstHasGlobalValuePredicate(this, DLLImportPredicate
);
607 bool Constant::isConstantUsed() const {
608 for (const User
*U
: users()) {
609 const Constant
*UC
= dyn_cast
<Constant
>(U
);
610 if (!UC
|| isa
<GlobalValue
>(UC
))
613 if (UC
->isConstantUsed())
619 bool Constant::needsDynamicRelocation() const {
620 return getRelocationInfo() == GlobalRelocation
;
623 bool Constant::needsRelocation() const {
624 return getRelocationInfo() != NoRelocation
;
627 Constant::PossibleRelocationsTy
Constant::getRelocationInfo() const {
628 if (isa
<GlobalValue
>(this))
629 return GlobalRelocation
; // Global reference.
631 if (const BlockAddress
*BA
= dyn_cast
<BlockAddress
>(this))
632 return BA
->getFunction()->getRelocationInfo();
634 if (const ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(this)) {
635 if (CE
->getOpcode() == Instruction::Sub
) {
636 ConstantExpr
*LHS
= dyn_cast
<ConstantExpr
>(CE
->getOperand(0));
637 ConstantExpr
*RHS
= dyn_cast
<ConstantExpr
>(CE
->getOperand(1));
638 if (LHS
&& RHS
&& LHS
->getOpcode() == Instruction::PtrToInt
&&
639 RHS
->getOpcode() == Instruction::PtrToInt
) {
640 Constant
*LHSOp0
= LHS
->getOperand(0);
641 Constant
*RHSOp0
= RHS
->getOperand(0);
643 // While raw uses of blockaddress need to be relocated, differences
644 // between two of them don't when they are for labels in the same
645 // function. This is a common idiom when creating a table for the
646 // indirect goto extension, so we handle it efficiently here.
647 if (isa
<BlockAddress
>(LHSOp0
) && isa
<BlockAddress
>(RHSOp0
) &&
648 cast
<BlockAddress
>(LHSOp0
)->getFunction() ==
649 cast
<BlockAddress
>(RHSOp0
)->getFunction())
652 // Relative pointers do not need to be dynamically relocated.
654 dyn_cast
<GlobalValue
>(RHSOp0
->stripInBoundsConstantOffsets())) {
655 auto *LHS
= LHSOp0
->stripInBoundsConstantOffsets();
656 if (auto *LHSGV
= dyn_cast
<GlobalValue
>(LHS
)) {
657 if (LHSGV
->isDSOLocal() && RHSGV
->isDSOLocal())
658 return LocalRelocation
;
659 } else if (isa
<DSOLocalEquivalent
>(LHS
)) {
660 if (RHSGV
->isDSOLocal())
661 return LocalRelocation
;
668 PossibleRelocationsTy Result
= NoRelocation
;
669 for (unsigned i
= 0, e
= getNumOperands(); i
!= e
; ++i
)
671 std::max(cast
<Constant
>(getOperand(i
))->getRelocationInfo(), Result
);
676 /// Return true if the specified constantexpr is dead. This involves
677 /// recursively traversing users of the constantexpr.
678 /// If RemoveDeadUsers is true, also remove dead users at the same time.
679 static bool constantIsDead(const Constant
*C
, bool RemoveDeadUsers
) {
680 if (isa
<GlobalValue
>(C
)) return false; // Cannot remove this
682 Value::const_user_iterator I
= C
->user_begin(), E
= C
->user_end();
684 const Constant
*User
= dyn_cast
<Constant
>(*I
);
685 if (!User
) return false; // Non-constant usage;
686 if (!constantIsDead(User
, RemoveDeadUsers
))
687 return false; // Constant wasn't dead
689 // Just removed User, so the iterator was invalidated.
690 // Since we return immediately upon finding a live user, we can always
691 // restart from user_begin().
698 if (RemoveDeadUsers
) {
699 // If C is only used by metadata, it should not be preserved but should
700 // have its uses replaced.
701 ReplaceableMetadataImpl::SalvageDebugInfo(*C
);
702 const_cast<Constant
*>(C
)->destroyConstant();
708 void Constant::removeDeadConstantUsers() const {
709 Value::const_user_iterator I
= user_begin(), E
= user_end();
710 Value::const_user_iterator LastNonDeadUser
= E
;
712 const Constant
*User
= dyn_cast
<Constant
>(*I
);
719 if (!constantIsDead(User
, /* RemoveDeadUsers= */ true)) {
720 // If the constant wasn't dead, remember that this was the last live use
721 // and move on to the next constant.
727 // If the constant was dead, then the iterator is invalidated.
728 if (LastNonDeadUser
== E
)
731 I
= std::next(LastNonDeadUser
);
735 bool Constant::hasOneLiveUse() const { return hasNLiveUses(1); }
737 bool Constant::hasZeroLiveUses() const { return hasNLiveUses(0); }
739 bool Constant::hasNLiveUses(unsigned N
) const {
740 unsigned NumUses
= 0;
741 for (const Use
&U
: uses()) {
742 const Constant
*User
= dyn_cast
<Constant
>(U
.getUser());
743 if (!User
|| !constantIsDead(User
, /* RemoveDeadUsers= */ false)) {
753 Constant
*Constant::replaceUndefsWith(Constant
*C
, Constant
*Replacement
) {
754 assert(C
&& Replacement
&& "Expected non-nullptr constant arguments");
755 Type
*Ty
= C
->getType();
756 if (match(C
, m_Undef())) {
757 assert(Ty
== Replacement
->getType() && "Expected matching types");
761 // Don't know how to deal with this constant.
762 auto *VTy
= dyn_cast
<FixedVectorType
>(Ty
);
766 unsigned NumElts
= VTy
->getNumElements();
767 SmallVector
<Constant
*, 32> NewC(NumElts
);
768 for (unsigned i
= 0; i
!= NumElts
; ++i
) {
769 Constant
*EltC
= C
->getAggregateElement(i
);
770 assert((!EltC
|| EltC
->getType() == Replacement
->getType()) &&
771 "Expected matching types");
772 NewC
[i
] = EltC
&& match(EltC
, m_Undef()) ? Replacement
: EltC
;
774 return ConstantVector::get(NewC
);
777 Constant
*Constant::mergeUndefsWith(Constant
*C
, Constant
*Other
) {
778 assert(C
&& Other
&& "Expected non-nullptr constant arguments");
779 if (match(C
, m_Undef()))
782 Type
*Ty
= C
->getType();
783 if (match(Other
, m_Undef()))
784 return UndefValue::get(Ty
);
786 auto *VTy
= dyn_cast
<FixedVectorType
>(Ty
);
790 Type
*EltTy
= VTy
->getElementType();
791 unsigned NumElts
= VTy
->getNumElements();
792 assert(isa
<FixedVectorType
>(Other
->getType()) &&
793 cast
<FixedVectorType
>(Other
->getType())->getNumElements() == NumElts
&&
796 bool FoundExtraUndef
= false;
797 SmallVector
<Constant
*, 32> NewC(NumElts
);
798 for (unsigned I
= 0; I
!= NumElts
; ++I
) {
799 NewC
[I
] = C
->getAggregateElement(I
);
800 Constant
*OtherEltC
= Other
->getAggregateElement(I
);
801 assert(NewC
[I
] && OtherEltC
&& "Unknown vector element");
802 if (!match(NewC
[I
], m_Undef()) && match(OtherEltC
, m_Undef())) {
803 NewC
[I
] = UndefValue::get(EltTy
);
804 FoundExtraUndef
= true;
808 return ConstantVector::get(NewC
);
812 bool Constant::isManifestConstant() const {
813 if (isa
<ConstantData
>(this))
815 if (isa
<ConstantAggregate
>(this) || isa
<ConstantExpr
>(this)) {
816 for (const Value
*Op
: operand_values())
817 if (!cast
<Constant
>(Op
)->isManifestConstant())
824 //===----------------------------------------------------------------------===//
826 //===----------------------------------------------------------------------===//
828 ConstantInt::ConstantInt(IntegerType
*Ty
, const APInt
&V
)
829 : ConstantData(Ty
, ConstantIntVal
), Val(V
) {
830 assert(V
.getBitWidth() == Ty
->getBitWidth() && "Invalid constant for type");
833 ConstantInt
*ConstantInt::getTrue(LLVMContext
&Context
) {
834 LLVMContextImpl
*pImpl
= Context
.pImpl
;
835 if (!pImpl
->TheTrueVal
)
836 pImpl
->TheTrueVal
= ConstantInt::get(Type::getInt1Ty(Context
), 1);
837 return pImpl
->TheTrueVal
;
840 ConstantInt
*ConstantInt::getFalse(LLVMContext
&Context
) {
841 LLVMContextImpl
*pImpl
= Context
.pImpl
;
842 if (!pImpl
->TheFalseVal
)
843 pImpl
->TheFalseVal
= ConstantInt::get(Type::getInt1Ty(Context
), 0);
844 return pImpl
->TheFalseVal
;
847 ConstantInt
*ConstantInt::getBool(LLVMContext
&Context
, bool V
) {
848 return V
? getTrue(Context
) : getFalse(Context
);
851 Constant
*ConstantInt::getTrue(Type
*Ty
) {
852 assert(Ty
->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
853 ConstantInt
*TrueC
= ConstantInt::getTrue(Ty
->getContext());
854 if (auto *VTy
= dyn_cast
<VectorType
>(Ty
))
855 return ConstantVector::getSplat(VTy
->getElementCount(), TrueC
);
859 Constant
*ConstantInt::getFalse(Type
*Ty
) {
860 assert(Ty
->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
861 ConstantInt
*FalseC
= ConstantInt::getFalse(Ty
->getContext());
862 if (auto *VTy
= dyn_cast
<VectorType
>(Ty
))
863 return ConstantVector::getSplat(VTy
->getElementCount(), FalseC
);
867 Constant
*ConstantInt::getBool(Type
*Ty
, bool V
) {
868 return V
? getTrue(Ty
) : getFalse(Ty
);
871 // Get a ConstantInt from an APInt.
872 ConstantInt
*ConstantInt::get(LLVMContext
&Context
, const APInt
&V
) {
873 // get an existing value or the insertion position
874 LLVMContextImpl
*pImpl
= Context
.pImpl
;
875 std::unique_ptr
<ConstantInt
> &Slot
=
876 V
.isZero() ? pImpl
->IntZeroConstants
[V
.getBitWidth()]
877 : V
.isOne() ? pImpl
->IntOneConstants
[V
.getBitWidth()]
878 : pImpl
->IntConstants
[V
];
880 // Get the corresponding integer type for the bit width of the value.
881 IntegerType
*ITy
= IntegerType::get(Context
, V
.getBitWidth());
882 Slot
.reset(new ConstantInt(ITy
, V
));
884 assert(Slot
->getType() == IntegerType::get(Context
, V
.getBitWidth()));
888 Constant
*ConstantInt::get(Type
*Ty
, uint64_t V
, bool isSigned
) {
889 Constant
*C
= get(cast
<IntegerType
>(Ty
->getScalarType()), V
, isSigned
);
891 // For vectors, broadcast the value.
892 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
893 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
898 ConstantInt
*ConstantInt::get(IntegerType
*Ty
, uint64_t V
, bool isSigned
) {
899 return get(Ty
->getContext(), APInt(Ty
->getBitWidth(), V
, isSigned
));
902 Constant
*ConstantInt::get(Type
*Ty
, const APInt
& V
) {
903 ConstantInt
*C
= get(Ty
->getContext(), V
);
904 assert(C
->getType() == Ty
->getScalarType() &&
905 "ConstantInt type doesn't match the type implied by its value!");
907 // For vectors, broadcast the value.
908 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
909 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
914 ConstantInt
*ConstantInt::get(IntegerType
* Ty
, StringRef Str
, uint8_t radix
) {
915 return get(Ty
->getContext(), APInt(Ty
->getBitWidth(), Str
, radix
));
918 /// Remove the constant from the constant table.
919 void ConstantInt::destroyConstantImpl() {
920 llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
923 //===----------------------------------------------------------------------===//
925 //===----------------------------------------------------------------------===//
927 Constant
*ConstantFP::get(Type
*Ty
, double V
) {
928 LLVMContext
&Context
= Ty
->getContext();
932 FV
.convert(Ty
->getScalarType()->getFltSemantics(),
933 APFloat::rmNearestTiesToEven
, &ignored
);
934 Constant
*C
= get(Context
, FV
);
936 // For vectors, broadcast the value.
937 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
938 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
943 Constant
*ConstantFP::get(Type
*Ty
, const APFloat
&V
) {
944 ConstantFP
*C
= get(Ty
->getContext(), V
);
945 assert(C
->getType() == Ty
->getScalarType() &&
946 "ConstantFP type doesn't match the type implied by its value!");
948 // For vectors, broadcast the value.
949 if (auto *VTy
= dyn_cast
<VectorType
>(Ty
))
950 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
955 Constant
*ConstantFP::get(Type
*Ty
, StringRef Str
) {
956 LLVMContext
&Context
= Ty
->getContext();
958 APFloat
FV(Ty
->getScalarType()->getFltSemantics(), Str
);
959 Constant
*C
= get(Context
, FV
);
961 // For vectors, broadcast the value.
962 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
963 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
968 Constant
*ConstantFP::getNaN(Type
*Ty
, bool Negative
, uint64_t Payload
) {
969 const fltSemantics
&Semantics
= Ty
->getScalarType()->getFltSemantics();
970 APFloat NaN
= APFloat::getNaN(Semantics
, Negative
, Payload
);
971 Constant
*C
= get(Ty
->getContext(), NaN
);
973 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
974 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
979 Constant
*ConstantFP::getQNaN(Type
*Ty
, bool Negative
, APInt
*Payload
) {
980 const fltSemantics
&Semantics
= Ty
->getScalarType()->getFltSemantics();
981 APFloat NaN
= APFloat::getQNaN(Semantics
, Negative
, Payload
);
982 Constant
*C
= get(Ty
->getContext(), NaN
);
984 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
985 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
990 Constant
*ConstantFP::getSNaN(Type
*Ty
, bool Negative
, APInt
*Payload
) {
991 const fltSemantics
&Semantics
= Ty
->getScalarType()->getFltSemantics();
992 APFloat NaN
= APFloat::getSNaN(Semantics
, Negative
, Payload
);
993 Constant
*C
= get(Ty
->getContext(), NaN
);
995 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
996 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
1001 Constant
*ConstantFP::getZero(Type
*Ty
, bool Negative
) {
1002 const fltSemantics
&Semantics
= Ty
->getScalarType()->getFltSemantics();
1003 APFloat NegZero
= APFloat::getZero(Semantics
, Negative
);
1004 Constant
*C
= get(Ty
->getContext(), NegZero
);
1006 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
1007 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
1013 // ConstantFP accessors.
1014 ConstantFP
* ConstantFP::get(LLVMContext
&Context
, const APFloat
& V
) {
1015 LLVMContextImpl
* pImpl
= Context
.pImpl
;
1017 std::unique_ptr
<ConstantFP
> &Slot
= pImpl
->FPConstants
[V
];
1020 Type
*Ty
= Type::getFloatingPointTy(Context
, V
.getSemantics());
1021 Slot
.reset(new ConstantFP(Ty
, V
));
1027 Constant
*ConstantFP::getInfinity(Type
*Ty
, bool Negative
) {
1028 const fltSemantics
&Semantics
= Ty
->getScalarType()->getFltSemantics();
1029 Constant
*C
= get(Ty
->getContext(), APFloat::getInf(Semantics
, Negative
));
1031 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
1032 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
1037 ConstantFP::ConstantFP(Type
*Ty
, const APFloat
&V
)
1038 : ConstantData(Ty
, ConstantFPVal
), Val(V
) {
1039 assert(&V
.getSemantics() == &Ty
->getFltSemantics() &&
1040 "FP type Mismatch");
1043 bool ConstantFP::isExactlyValue(const APFloat
&V
) const {
1044 return Val
.bitwiseIsEqual(V
);
1047 /// Remove the constant from the constant table.
1048 void ConstantFP::destroyConstantImpl() {
1049 llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
1052 //===----------------------------------------------------------------------===//
1053 // ConstantAggregateZero Implementation
1054 //===----------------------------------------------------------------------===//
1056 Constant
*ConstantAggregateZero::getSequentialElement() const {
1057 if (auto *AT
= dyn_cast
<ArrayType
>(getType()))
1058 return Constant::getNullValue(AT
->getElementType());
1059 return Constant::getNullValue(cast
<VectorType
>(getType())->getElementType());
1062 Constant
*ConstantAggregateZero::getStructElement(unsigned Elt
) const {
1063 return Constant::getNullValue(getType()->getStructElementType(Elt
));
1066 Constant
*ConstantAggregateZero::getElementValue(Constant
*C
) const {
1067 if (isa
<ArrayType
>(getType()) || isa
<VectorType
>(getType()))
1068 return getSequentialElement();
1069 return getStructElement(cast
<ConstantInt
>(C
)->getZExtValue());
1072 Constant
*ConstantAggregateZero::getElementValue(unsigned Idx
) const {
1073 if (isa
<ArrayType
>(getType()) || isa
<VectorType
>(getType()))
1074 return getSequentialElement();
1075 return getStructElement(Idx
);
1078 ElementCount
ConstantAggregateZero::getElementCount() const {
1079 Type
*Ty
= getType();
1080 if (auto *AT
= dyn_cast
<ArrayType
>(Ty
))
1081 return ElementCount::getFixed(AT
->getNumElements());
1082 if (auto *VT
= dyn_cast
<VectorType
>(Ty
))
1083 return VT
->getElementCount();
1084 return ElementCount::getFixed(Ty
->getStructNumElements());
1087 //===----------------------------------------------------------------------===//
1088 // UndefValue Implementation
1089 //===----------------------------------------------------------------------===//
1091 UndefValue
*UndefValue::getSequentialElement() const {
1092 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(getType()))
1093 return UndefValue::get(ATy
->getElementType());
1094 return UndefValue::get(cast
<VectorType
>(getType())->getElementType());
1097 UndefValue
*UndefValue::getStructElement(unsigned Elt
) const {
1098 return UndefValue::get(getType()->getStructElementType(Elt
));
1101 UndefValue
*UndefValue::getElementValue(Constant
*C
) const {
1102 if (isa
<ArrayType
>(getType()) || isa
<VectorType
>(getType()))
1103 return getSequentialElement();
1104 return getStructElement(cast
<ConstantInt
>(C
)->getZExtValue());
1107 UndefValue
*UndefValue::getElementValue(unsigned Idx
) const {
1108 if (isa
<ArrayType
>(getType()) || isa
<VectorType
>(getType()))
1109 return getSequentialElement();
1110 return getStructElement(Idx
);
1113 unsigned UndefValue::getNumElements() const {
1114 Type
*Ty
= getType();
1115 if (auto *AT
= dyn_cast
<ArrayType
>(Ty
))
1116 return AT
->getNumElements();
1117 if (auto *VT
= dyn_cast
<VectorType
>(Ty
))
1118 return cast
<FixedVectorType
>(VT
)->getNumElements();
1119 return Ty
->getStructNumElements();
1122 //===----------------------------------------------------------------------===//
1123 // PoisonValue Implementation
1124 //===----------------------------------------------------------------------===//
1126 PoisonValue
*PoisonValue::getSequentialElement() const {
1127 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(getType()))
1128 return PoisonValue::get(ATy
->getElementType());
1129 return PoisonValue::get(cast
<VectorType
>(getType())->getElementType());
1132 PoisonValue
*PoisonValue::getStructElement(unsigned Elt
) const {
1133 return PoisonValue::get(getType()->getStructElementType(Elt
));
1136 PoisonValue
*PoisonValue::getElementValue(Constant
*C
) const {
1137 if (isa
<ArrayType
>(getType()) || isa
<VectorType
>(getType()))
1138 return getSequentialElement();
1139 return getStructElement(cast
<ConstantInt
>(C
)->getZExtValue());
1142 PoisonValue
*PoisonValue::getElementValue(unsigned Idx
) const {
1143 if (isa
<ArrayType
>(getType()) || isa
<VectorType
>(getType()))
1144 return getSequentialElement();
1145 return getStructElement(Idx
);
1148 //===----------------------------------------------------------------------===//
1149 // ConstantXXX Classes
1150 //===----------------------------------------------------------------------===//
1152 template <typename ItTy
, typename EltTy
>
1153 static bool rangeOnlyContains(ItTy Start
, ItTy End
, EltTy Elt
) {
1154 for (; Start
!= End
; ++Start
)
1160 template <typename SequentialTy
, typename ElementTy
>
1161 static Constant
*getIntSequenceIfElementsMatch(ArrayRef
<Constant
*> V
) {
1162 assert(!V
.empty() && "Cannot get empty int sequence.");
1164 SmallVector
<ElementTy
, 16> Elts
;
1165 for (Constant
*C
: V
)
1166 if (auto *CI
= dyn_cast
<ConstantInt
>(C
))
1167 Elts
.push_back(CI
->getZExtValue());
1170 return SequentialTy::get(V
[0]->getContext(), Elts
);
1173 template <typename SequentialTy
, typename ElementTy
>
1174 static Constant
*getFPSequenceIfElementsMatch(ArrayRef
<Constant
*> V
) {
1175 assert(!V
.empty() && "Cannot get empty FP sequence.");
1177 SmallVector
<ElementTy
, 16> Elts
;
1178 for (Constant
*C
: V
)
1179 if (auto *CFP
= dyn_cast
<ConstantFP
>(C
))
1180 Elts
.push_back(CFP
->getValueAPF().bitcastToAPInt().getLimitedValue());
1183 return SequentialTy::getFP(V
[0]->getType(), Elts
);
1186 template <typename SequenceTy
>
1187 static Constant
*getSequenceIfElementsMatch(Constant
*C
,
1188 ArrayRef
<Constant
*> V
) {
1189 // We speculatively build the elements here even if it turns out that there is
1190 // a constantexpr or something else weird, since it is so uncommon for that to
1192 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(C
)) {
1193 if (CI
->getType()->isIntegerTy(8))
1194 return getIntSequenceIfElementsMatch
<SequenceTy
, uint8_t>(V
);
1195 else if (CI
->getType()->isIntegerTy(16))
1196 return getIntSequenceIfElementsMatch
<SequenceTy
, uint16_t>(V
);
1197 else if (CI
->getType()->isIntegerTy(32))
1198 return getIntSequenceIfElementsMatch
<SequenceTy
, uint32_t>(V
);
1199 else if (CI
->getType()->isIntegerTy(64))
1200 return getIntSequenceIfElementsMatch
<SequenceTy
, uint64_t>(V
);
1201 } else if (ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(C
)) {
1202 if (CFP
->getType()->isHalfTy() || CFP
->getType()->isBFloatTy())
1203 return getFPSequenceIfElementsMatch
<SequenceTy
, uint16_t>(V
);
1204 else if (CFP
->getType()->isFloatTy())
1205 return getFPSequenceIfElementsMatch
<SequenceTy
, uint32_t>(V
);
1206 else if (CFP
->getType()->isDoubleTy())
1207 return getFPSequenceIfElementsMatch
<SequenceTy
, uint64_t>(V
);
1213 ConstantAggregate::ConstantAggregate(Type
*T
, ValueTy VT
,
1214 ArrayRef
<Constant
*> V
)
1215 : Constant(T
, VT
, OperandTraits
<ConstantAggregate
>::op_end(this) - V
.size(),
1217 llvm::copy(V
, op_begin());
1219 // Check that types match, unless this is an opaque struct.
1220 if (auto *ST
= dyn_cast
<StructType
>(T
)) {
1223 for (unsigned I
= 0, E
= V
.size(); I
!= E
; ++I
)
1224 assert(V
[I
]->getType() == ST
->getTypeAtIndex(I
) &&
1225 "Initializer for struct element doesn't match!");
1229 ConstantArray::ConstantArray(ArrayType
*T
, ArrayRef
<Constant
*> V
)
1230 : ConstantAggregate(T
, ConstantArrayVal
, V
) {
1231 assert(V
.size() == T
->getNumElements() &&
1232 "Invalid initializer for constant array");
1235 Constant
*ConstantArray::get(ArrayType
*Ty
, ArrayRef
<Constant
*> V
) {
1236 if (Constant
*C
= getImpl(Ty
, V
))
1238 return Ty
->getContext().pImpl
->ArrayConstants
.getOrCreate(Ty
, V
);
1241 Constant
*ConstantArray::getImpl(ArrayType
*Ty
, ArrayRef
<Constant
*> V
) {
1242 // Empty arrays are canonicalized to ConstantAggregateZero.
1244 return ConstantAggregateZero::get(Ty
);
1246 for (Constant
*C
: V
) {
1247 assert(C
->getType() == Ty
->getElementType() &&
1248 "Wrong type in array element initializer");
1252 // If this is an all-zero array, return a ConstantAggregateZero object. If
1253 // all undef, return an UndefValue, if "all simple", then return a
1254 // ConstantDataArray.
1256 if (isa
<PoisonValue
>(C
) && rangeOnlyContains(V
.begin(), V
.end(), C
))
1257 return PoisonValue::get(Ty
);
1259 if (isa
<UndefValue
>(C
) && rangeOnlyContains(V
.begin(), V
.end(), C
))
1260 return UndefValue::get(Ty
);
1262 if (C
->isNullValue() && rangeOnlyContains(V
.begin(), V
.end(), C
))
1263 return ConstantAggregateZero::get(Ty
);
1265 // Check to see if all of the elements are ConstantFP or ConstantInt and if
1266 // the element type is compatible with ConstantDataVector. If so, use it.
1267 if (ConstantDataSequential::isElementTypeCompatible(C
->getType()))
1268 return getSequenceIfElementsMatch
<ConstantDataArray
>(C
, V
);
1270 // Otherwise, we really do want to create a ConstantArray.
1274 StructType
*ConstantStruct::getTypeForElements(LLVMContext
&Context
,
1275 ArrayRef
<Constant
*> V
,
1277 unsigned VecSize
= V
.size();
1278 SmallVector
<Type
*, 16> EltTypes(VecSize
);
1279 for (unsigned i
= 0; i
!= VecSize
; ++i
)
1280 EltTypes
[i
] = V
[i
]->getType();
1282 return StructType::get(Context
, EltTypes
, Packed
);
1286 StructType
*ConstantStruct::getTypeForElements(ArrayRef
<Constant
*> V
,
1288 assert(!V
.empty() &&
1289 "ConstantStruct::getTypeForElements cannot be called on empty list");
1290 return getTypeForElements(V
[0]->getContext(), V
, Packed
);
1293 ConstantStruct::ConstantStruct(StructType
*T
, ArrayRef
<Constant
*> V
)
1294 : ConstantAggregate(T
, ConstantStructVal
, V
) {
1295 assert((T
->isOpaque() || V
.size() == T
->getNumElements()) &&
1296 "Invalid initializer for constant struct");
1299 // ConstantStruct accessors.
1300 Constant
*ConstantStruct::get(StructType
*ST
, ArrayRef
<Constant
*> V
) {
1301 assert((ST
->isOpaque() || ST
->getNumElements() == V
.size()) &&
1302 "Incorrect # elements specified to ConstantStruct::get");
1304 // Create a ConstantAggregateZero value if all elements are zeros.
1306 bool isUndef
= false;
1307 bool isPoison
= false;
1310 isUndef
= isa
<UndefValue
>(V
[0]);
1311 isPoison
= isa
<PoisonValue
>(V
[0]);
1312 isZero
= V
[0]->isNullValue();
1313 // PoisonValue inherits UndefValue, so its check is not necessary.
1314 if (isUndef
|| isZero
) {
1315 for (Constant
*C
: V
) {
1316 if (!C
->isNullValue())
1318 if (!isa
<PoisonValue
>(C
))
1320 if (isa
<PoisonValue
>(C
) || !isa
<UndefValue
>(C
))
1326 return ConstantAggregateZero::get(ST
);
1328 return PoisonValue::get(ST
);
1330 return UndefValue::get(ST
);
1332 return ST
->getContext().pImpl
->StructConstants
.getOrCreate(ST
, V
);
1335 ConstantVector::ConstantVector(VectorType
*T
, ArrayRef
<Constant
*> V
)
1336 : ConstantAggregate(T
, ConstantVectorVal
, V
) {
1337 assert(V
.size() == cast
<FixedVectorType
>(T
)->getNumElements() &&
1338 "Invalid initializer for constant vector");
1341 // ConstantVector accessors.
1342 Constant
*ConstantVector::get(ArrayRef
<Constant
*> V
) {
1343 if (Constant
*C
= getImpl(V
))
1345 auto *Ty
= FixedVectorType::get(V
.front()->getType(), V
.size());
1346 return Ty
->getContext().pImpl
->VectorConstants
.getOrCreate(Ty
, V
);
1349 Constant
*ConstantVector::getImpl(ArrayRef
<Constant
*> V
) {
1350 assert(!V
.empty() && "Vectors can't be empty");
1351 auto *T
= FixedVectorType::get(V
.front()->getType(), V
.size());
1353 // If this is an all-undef or all-zero vector, return a
1354 // ConstantAggregateZero or UndefValue.
1356 bool isZero
= C
->isNullValue();
1357 bool isUndef
= isa
<UndefValue
>(C
);
1358 bool isPoison
= isa
<PoisonValue
>(C
);
1360 if (isZero
|| isUndef
) {
1361 for (unsigned i
= 1, e
= V
.size(); i
!= e
; ++i
)
1363 isZero
= isUndef
= isPoison
= false;
1369 return ConstantAggregateZero::get(T
);
1371 return PoisonValue::get(T
);
1373 return UndefValue::get(T
);
1375 // Check to see if all of the elements are ConstantFP or ConstantInt and if
1376 // the element type is compatible with ConstantDataVector. If so, use it.
1377 if (ConstantDataSequential::isElementTypeCompatible(C
->getType()))
1378 return getSequenceIfElementsMatch
<ConstantDataVector
>(C
, V
);
1380 // Otherwise, the element type isn't compatible with ConstantDataVector, or
1381 // the operand list contains a ConstantExpr or something else strange.
1385 Constant
*ConstantVector::getSplat(ElementCount EC
, Constant
*V
) {
1386 if (!EC
.isScalable()) {
1387 // If this splat is compatible with ConstantDataVector, use it instead of
1389 if ((isa
<ConstantFP
>(V
) || isa
<ConstantInt
>(V
)) &&
1390 ConstantDataSequential::isElementTypeCompatible(V
->getType()))
1391 return ConstantDataVector::getSplat(EC
.getKnownMinValue(), V
);
1393 SmallVector
<Constant
*, 32> Elts(EC
.getKnownMinValue(), V
);
1397 Type
*VTy
= VectorType::get(V
->getType(), EC
);
1399 if (V
->isNullValue())
1400 return ConstantAggregateZero::get(VTy
);
1401 else if (isa
<UndefValue
>(V
))
1402 return UndefValue::get(VTy
);
1404 Type
*IdxTy
= Type::getInt64Ty(VTy
->getContext());
1406 // Move scalar into vector.
1407 Constant
*PoisonV
= PoisonValue::get(VTy
);
1408 V
= ConstantExpr::getInsertElement(PoisonV
, V
, ConstantInt::get(IdxTy
, 0));
1409 // Build shuffle mask to perform the splat.
1410 SmallVector
<int, 8> Zeros(EC
.getKnownMinValue(), 0);
1412 return ConstantExpr::getShuffleVector(V
, PoisonV
, Zeros
);
1415 ConstantTokenNone
*ConstantTokenNone::get(LLVMContext
&Context
) {
1416 LLVMContextImpl
*pImpl
= Context
.pImpl
;
1417 if (!pImpl
->TheNoneToken
)
1418 pImpl
->TheNoneToken
.reset(new ConstantTokenNone(Context
));
1419 return pImpl
->TheNoneToken
.get();
1422 /// Remove the constant from the constant table.
1423 void ConstantTokenNone::destroyConstantImpl() {
1424 llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
1427 // Utility function for determining if a ConstantExpr is a CastOp or not. This
1428 // can't be inline because we don't want to #include Instruction.h into
1430 bool ConstantExpr::isCast() const {
1431 return Instruction::isCast(getOpcode());
1434 bool ConstantExpr::isCompare() const {
1435 return getOpcode() == Instruction::ICmp
|| getOpcode() == Instruction::FCmp
;
1438 unsigned ConstantExpr::getPredicate() const {
1439 return cast
<CompareConstantExpr
>(this)->predicate
;
1442 ArrayRef
<int> ConstantExpr::getShuffleMask() const {
1443 return cast
<ShuffleVectorConstantExpr
>(this)->ShuffleMask
;
1446 Constant
*ConstantExpr::getShuffleMaskForBitcode() const {
1447 return cast
<ShuffleVectorConstantExpr
>(this)->ShuffleMaskForBitcode
;
1450 Constant
*ConstantExpr::getWithOperands(ArrayRef
<Constant
*> Ops
, Type
*Ty
,
1451 bool OnlyIfReduced
, Type
*SrcTy
) const {
1452 assert(Ops
.size() == getNumOperands() && "Operand count mismatch!");
1454 // If no operands changed return self.
1455 if (Ty
== getType() && std::equal(Ops
.begin(), Ops
.end(), op_begin()))
1456 return const_cast<ConstantExpr
*>(this);
1458 Type
*OnlyIfReducedTy
= OnlyIfReduced
? Ty
: nullptr;
1459 switch (getOpcode()) {
1460 case Instruction::Trunc
:
1461 case Instruction::ZExt
:
1462 case Instruction::SExt
:
1463 case Instruction::FPTrunc
:
1464 case Instruction::FPExt
:
1465 case Instruction::UIToFP
:
1466 case Instruction::SIToFP
:
1467 case Instruction::FPToUI
:
1468 case Instruction::FPToSI
:
1469 case Instruction::PtrToInt
:
1470 case Instruction::IntToPtr
:
1471 case Instruction::BitCast
:
1472 case Instruction::AddrSpaceCast
:
1473 return ConstantExpr::getCast(getOpcode(), Ops
[0], Ty
, OnlyIfReduced
);
1474 case Instruction::InsertElement
:
1475 return ConstantExpr::getInsertElement(Ops
[0], Ops
[1], Ops
[2],
1477 case Instruction::ExtractElement
:
1478 return ConstantExpr::getExtractElement(Ops
[0], Ops
[1], OnlyIfReducedTy
);
1479 case Instruction::ShuffleVector
:
1480 return ConstantExpr::getShuffleVector(Ops
[0], Ops
[1], getShuffleMask(),
1482 case Instruction::GetElementPtr
: {
1483 auto *GEPO
= cast
<GEPOperator
>(this);
1484 assert(SrcTy
|| (Ops
[0]->getType() == getOperand(0)->getType()));
1485 return ConstantExpr::getGetElementPtr(
1486 SrcTy
? SrcTy
: GEPO
->getSourceElementType(), Ops
[0], Ops
.slice(1),
1487 GEPO
->isInBounds(), GEPO
->getInRangeIndex(), OnlyIfReducedTy
);
1489 case Instruction::ICmp
:
1490 case Instruction::FCmp
:
1491 return ConstantExpr::getCompare(getPredicate(), Ops
[0], Ops
[1],
1494 assert(getNumOperands() == 2 && "Must be binary operator?");
1495 return ConstantExpr::get(getOpcode(), Ops
[0], Ops
[1], SubclassOptionalData
,
1501 //===----------------------------------------------------------------------===//
1502 // isValueValidForType implementations
1504 bool ConstantInt::isValueValidForType(Type
*Ty
, uint64_t Val
) {
1505 unsigned NumBits
= Ty
->getIntegerBitWidth(); // assert okay
1506 if (Ty
->isIntegerTy(1))
1507 return Val
== 0 || Val
== 1;
1508 return isUIntN(NumBits
, Val
);
1511 bool ConstantInt::isValueValidForType(Type
*Ty
, int64_t Val
) {
1512 unsigned NumBits
= Ty
->getIntegerBitWidth();
1513 if (Ty
->isIntegerTy(1))
1514 return Val
== 0 || Val
== 1 || Val
== -1;
1515 return isIntN(NumBits
, Val
);
1518 bool ConstantFP::isValueValidForType(Type
*Ty
, const APFloat
& Val
) {
1519 // convert modifies in place, so make a copy.
1520 APFloat Val2
= APFloat(Val
);
1522 switch (Ty
->getTypeID()) {
1524 return false; // These can't be represented as floating point!
1526 // FIXME rounding mode needs to be more flexible
1527 case Type::HalfTyID
: {
1528 if (&Val2
.getSemantics() == &APFloat::IEEEhalf())
1530 Val2
.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven
, &losesInfo
);
1533 case Type::BFloatTyID
: {
1534 if (&Val2
.getSemantics() == &APFloat::BFloat())
1536 Val2
.convert(APFloat::BFloat(), APFloat::rmNearestTiesToEven
, &losesInfo
);
1539 case Type::FloatTyID
: {
1540 if (&Val2
.getSemantics() == &APFloat::IEEEsingle())
1542 Val2
.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven
, &losesInfo
);
1545 case Type::DoubleTyID
: {
1546 if (&Val2
.getSemantics() == &APFloat::IEEEhalf() ||
1547 &Val2
.getSemantics() == &APFloat::BFloat() ||
1548 &Val2
.getSemantics() == &APFloat::IEEEsingle() ||
1549 &Val2
.getSemantics() == &APFloat::IEEEdouble())
1551 Val2
.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven
, &losesInfo
);
1554 case Type::X86_FP80TyID
:
1555 return &Val2
.getSemantics() == &APFloat::IEEEhalf() ||
1556 &Val2
.getSemantics() == &APFloat::BFloat() ||
1557 &Val2
.getSemantics() == &APFloat::IEEEsingle() ||
1558 &Val2
.getSemantics() == &APFloat::IEEEdouble() ||
1559 &Val2
.getSemantics() == &APFloat::x87DoubleExtended();
1560 case Type::FP128TyID
:
1561 return &Val2
.getSemantics() == &APFloat::IEEEhalf() ||
1562 &Val2
.getSemantics() == &APFloat::BFloat() ||
1563 &Val2
.getSemantics() == &APFloat::IEEEsingle() ||
1564 &Val2
.getSemantics() == &APFloat::IEEEdouble() ||
1565 &Val2
.getSemantics() == &APFloat::IEEEquad();
1566 case Type::PPC_FP128TyID
:
1567 return &Val2
.getSemantics() == &APFloat::IEEEhalf() ||
1568 &Val2
.getSemantics() == &APFloat::BFloat() ||
1569 &Val2
.getSemantics() == &APFloat::IEEEsingle() ||
1570 &Val2
.getSemantics() == &APFloat::IEEEdouble() ||
1571 &Val2
.getSemantics() == &APFloat::PPCDoubleDouble();
1576 //===----------------------------------------------------------------------===//
1577 // Factory Function Implementation
1579 ConstantAggregateZero
*ConstantAggregateZero::get(Type
*Ty
) {
1580 assert((Ty
->isStructTy() || Ty
->isArrayTy() || Ty
->isVectorTy()) &&
1581 "Cannot create an aggregate zero of non-aggregate type!");
1583 std::unique_ptr
<ConstantAggregateZero
> &Entry
=
1584 Ty
->getContext().pImpl
->CAZConstants
[Ty
];
1586 Entry
.reset(new ConstantAggregateZero(Ty
));
1591 /// Remove the constant from the constant table.
1592 void ConstantAggregateZero::destroyConstantImpl() {
1593 getContext().pImpl
->CAZConstants
.erase(getType());
1596 /// Remove the constant from the constant table.
1597 void ConstantArray::destroyConstantImpl() {
1598 getType()->getContext().pImpl
->ArrayConstants
.remove(this);
1602 //---- ConstantStruct::get() implementation...
1605 /// Remove the constant from the constant table.
1606 void ConstantStruct::destroyConstantImpl() {
1607 getType()->getContext().pImpl
->StructConstants
.remove(this);
1610 /// Remove the constant from the constant table.
1611 void ConstantVector::destroyConstantImpl() {
1612 getType()->getContext().pImpl
->VectorConstants
.remove(this);
1615 Constant
*Constant::getSplatValue(bool AllowUndefs
) const {
1616 assert(this->getType()->isVectorTy() && "Only valid for vectors!");
1617 if (isa
<ConstantAggregateZero
>(this))
1618 return getNullValue(cast
<VectorType
>(getType())->getElementType());
1619 if (const ConstantDataVector
*CV
= dyn_cast
<ConstantDataVector
>(this))
1620 return CV
->getSplatValue();
1621 if (const ConstantVector
*CV
= dyn_cast
<ConstantVector
>(this))
1622 return CV
->getSplatValue(AllowUndefs
);
1624 // Check if this is a constant expression splat of the form returned by
1625 // ConstantVector::getSplat()
1626 const auto *Shuf
= dyn_cast
<ConstantExpr
>(this);
1627 if (Shuf
&& Shuf
->getOpcode() == Instruction::ShuffleVector
&&
1628 isa
<UndefValue
>(Shuf
->getOperand(1))) {
1630 const auto *IElt
= dyn_cast
<ConstantExpr
>(Shuf
->getOperand(0));
1631 if (IElt
&& IElt
->getOpcode() == Instruction::InsertElement
&&
1632 isa
<UndefValue
>(IElt
->getOperand(0))) {
1634 ArrayRef
<int> Mask
= Shuf
->getShuffleMask();
1635 Constant
*SplatVal
= IElt
->getOperand(1);
1636 ConstantInt
*Index
= dyn_cast
<ConstantInt
>(IElt
->getOperand(2));
1638 if (Index
&& Index
->getValue() == 0 &&
1639 llvm::all_of(Mask
, [](int I
) { return I
== 0; }))
1647 Constant
*ConstantVector::getSplatValue(bool AllowUndefs
) const {
1648 // Check out first element.
1649 Constant
*Elt
= getOperand(0);
1650 // Then make sure all remaining elements point to the same value.
1651 for (unsigned I
= 1, E
= getNumOperands(); I
< E
; ++I
) {
1652 Constant
*OpC
= getOperand(I
);
1656 // Strict mode: any mismatch is not a splat.
1660 // Allow undefs mode: ignore undefined elements.
1661 if (isa
<UndefValue
>(OpC
))
1664 // If we do not have a defined element yet, use the current operand.
1665 if (isa
<UndefValue
>(Elt
))
1674 const APInt
&Constant::getUniqueInteger() const {
1675 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
1676 return CI
->getValue();
1677 // Scalable vectors can use a ConstantExpr to build a splat.
1678 if (isa
<ConstantExpr
>(this))
1679 return cast
<ConstantInt
>(this->getSplatValue())->getValue();
1680 // For non-ConstantExpr we use getAggregateElement as a fast path to avoid
1681 // calling getSplatValue in release builds.
1682 assert(this->getSplatValue() && "Doesn't contain a unique integer!");
1683 const Constant
*C
= this->getAggregateElement(0U);
1684 assert(C
&& isa
<ConstantInt
>(C
) && "Not a vector of numbers!");
1685 return cast
<ConstantInt
>(C
)->getValue();
1688 //---- ConstantPointerNull::get() implementation.
1691 ConstantPointerNull
*ConstantPointerNull::get(PointerType
*Ty
) {
1692 std::unique_ptr
<ConstantPointerNull
> &Entry
=
1693 Ty
->getContext().pImpl
->CPNConstants
[Ty
];
1695 Entry
.reset(new ConstantPointerNull(Ty
));
1700 /// Remove the constant from the constant table.
1701 void ConstantPointerNull::destroyConstantImpl() {
1702 getContext().pImpl
->CPNConstants
.erase(getType());
1705 //---- ConstantTargetNone::get() implementation.
1708 ConstantTargetNone
*ConstantTargetNone::get(TargetExtType
*Ty
) {
1709 assert(Ty
->hasProperty(TargetExtType::HasZeroInit
) &&
1710 "Target extension type not allowed to have a zeroinitializer");
1711 std::unique_ptr
<ConstantTargetNone
> &Entry
=
1712 Ty
->getContext().pImpl
->CTNConstants
[Ty
];
1714 Entry
.reset(new ConstantTargetNone(Ty
));
1719 /// Remove the constant from the constant table.
1720 void ConstantTargetNone::destroyConstantImpl() {
1721 getContext().pImpl
->CTNConstants
.erase(getType());
1724 UndefValue
*UndefValue::get(Type
*Ty
) {
1725 std::unique_ptr
<UndefValue
> &Entry
= Ty
->getContext().pImpl
->UVConstants
[Ty
];
1727 Entry
.reset(new UndefValue(Ty
));
1732 /// Remove the constant from the constant table.
1733 void UndefValue::destroyConstantImpl() {
1734 // Free the constant and any dangling references to it.
1735 if (getValueID() == UndefValueVal
) {
1736 getContext().pImpl
->UVConstants
.erase(getType());
1737 } else if (getValueID() == PoisonValueVal
) {
1738 getContext().pImpl
->PVConstants
.erase(getType());
1740 llvm_unreachable("Not a undef or a poison!");
1743 PoisonValue
*PoisonValue::get(Type
*Ty
) {
1744 std::unique_ptr
<PoisonValue
> &Entry
= Ty
->getContext().pImpl
->PVConstants
[Ty
];
1746 Entry
.reset(new PoisonValue(Ty
));
1751 /// Remove the constant from the constant table.
1752 void PoisonValue::destroyConstantImpl() {
1753 // Free the constant and any dangling references to it.
1754 getContext().pImpl
->PVConstants
.erase(getType());
1757 BlockAddress
*BlockAddress::get(BasicBlock
*BB
) {
1758 assert(BB
->getParent() && "Block must have a parent");
1759 return get(BB
->getParent(), BB
);
1762 BlockAddress
*BlockAddress::get(Function
*F
, BasicBlock
*BB
) {
1764 F
->getContext().pImpl
->BlockAddresses
[std::make_pair(F
, BB
)];
1766 BA
= new BlockAddress(F
, BB
);
1768 assert(BA
->getFunction() == F
&& "Basic block moved between functions");
1772 BlockAddress::BlockAddress(Function
*F
, BasicBlock
*BB
)
1773 : Constant(PointerType::get(F
->getContext(), F
->getAddressSpace()),
1774 Value::BlockAddressVal
, &Op
<0>(), 2) {
1777 BB
->AdjustBlockAddressRefCount(1);
1780 BlockAddress
*BlockAddress::lookup(const BasicBlock
*BB
) {
1781 if (!BB
->hasAddressTaken())
1784 const Function
*F
= BB
->getParent();
1785 assert(F
&& "Block must have a parent");
1787 F
->getContext().pImpl
->BlockAddresses
.lookup(std::make_pair(F
, BB
));
1788 assert(BA
&& "Refcount and block address map disagree!");
1792 /// Remove the constant from the constant table.
1793 void BlockAddress::destroyConstantImpl() {
1794 getFunction()->getType()->getContext().pImpl
1795 ->BlockAddresses
.erase(std::make_pair(getFunction(), getBasicBlock()));
1796 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1799 Value
*BlockAddress::handleOperandChangeImpl(Value
*From
, Value
*To
) {
1800 // This could be replacing either the Basic Block or the Function. In either
1801 // case, we have to remove the map entry.
1802 Function
*NewF
= getFunction();
1803 BasicBlock
*NewBB
= getBasicBlock();
1806 NewF
= cast
<Function
>(To
->stripPointerCasts());
1808 assert(From
== NewBB
&& "From does not match any operand");
1809 NewBB
= cast
<BasicBlock
>(To
);
1812 // See if the 'new' entry already exists, if not, just update this in place
1813 // and return early.
1814 BlockAddress
*&NewBA
=
1815 getContext().pImpl
->BlockAddresses
[std::make_pair(NewF
, NewBB
)];
1819 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1821 // Remove the old entry, this can't cause the map to rehash (just a
1822 // tombstone will get added).
1823 getContext().pImpl
->BlockAddresses
.erase(std::make_pair(getFunction(),
1826 setOperand(0, NewF
);
1827 setOperand(1, NewBB
);
1828 getBasicBlock()->AdjustBlockAddressRefCount(1);
1830 // If we just want to keep the existing value, then return null.
1831 // Callers know that this means we shouldn't delete this value.
1835 DSOLocalEquivalent
*DSOLocalEquivalent::get(GlobalValue
*GV
) {
1836 DSOLocalEquivalent
*&Equiv
= GV
->getContext().pImpl
->DSOLocalEquivalents
[GV
];
1838 Equiv
= new DSOLocalEquivalent(GV
);
1840 assert(Equiv
->getGlobalValue() == GV
&&
1841 "DSOLocalFunction does not match the expected global value");
1845 DSOLocalEquivalent::DSOLocalEquivalent(GlobalValue
*GV
)
1846 : Constant(GV
->getType(), Value::DSOLocalEquivalentVal
, &Op
<0>(), 1) {
1850 /// Remove the constant from the constant table.
1851 void DSOLocalEquivalent::destroyConstantImpl() {
1852 const GlobalValue
*GV
= getGlobalValue();
1853 GV
->getContext().pImpl
->DSOLocalEquivalents
.erase(GV
);
1856 Value
*DSOLocalEquivalent::handleOperandChangeImpl(Value
*From
, Value
*To
) {
1857 assert(From
== getGlobalValue() && "Changing value does not match operand.");
1858 assert(isa
<Constant
>(To
) && "Can only replace the operands with a constant");
1860 // The replacement is with another global value.
1861 if (const auto *ToObj
= dyn_cast
<GlobalValue
>(To
)) {
1862 DSOLocalEquivalent
*&NewEquiv
=
1863 getContext().pImpl
->DSOLocalEquivalents
[ToObj
];
1865 return llvm::ConstantExpr::getBitCast(NewEquiv
, getType());
1868 // If the argument is replaced with a null value, just replace this constant
1869 // with a null value.
1870 if (cast
<Constant
>(To
)->isNullValue())
1873 // The replacement could be a bitcast or an alias to another function. We can
1874 // replace it with a bitcast to the dso_local_equivalent of that function.
1875 auto *Func
= cast
<Function
>(To
->stripPointerCastsAndAliases());
1876 DSOLocalEquivalent
*&NewEquiv
= getContext().pImpl
->DSOLocalEquivalents
[Func
];
1878 return llvm::ConstantExpr::getBitCast(NewEquiv
, getType());
1880 // Replace this with the new one.
1881 getContext().pImpl
->DSOLocalEquivalents
.erase(getGlobalValue());
1883 setOperand(0, Func
);
1885 if (Func
->getType() != getType()) {
1886 // It is ok to mutate the type here because this constant should always
1887 // reflect the type of the function it's holding.
1888 mutateType(Func
->getType());
1893 NoCFIValue
*NoCFIValue::get(GlobalValue
*GV
) {
1894 NoCFIValue
*&NC
= GV
->getContext().pImpl
->NoCFIValues
[GV
];
1896 NC
= new NoCFIValue(GV
);
1898 assert(NC
->getGlobalValue() == GV
&&
1899 "NoCFIValue does not match the expected global value");
1903 NoCFIValue::NoCFIValue(GlobalValue
*GV
)
1904 : Constant(GV
->getType(), Value::NoCFIValueVal
, &Op
<0>(), 1) {
1908 /// Remove the constant from the constant table.
1909 void NoCFIValue::destroyConstantImpl() {
1910 const GlobalValue
*GV
= getGlobalValue();
1911 GV
->getContext().pImpl
->NoCFIValues
.erase(GV
);
1914 Value
*NoCFIValue::handleOperandChangeImpl(Value
*From
, Value
*To
) {
1915 assert(From
== getGlobalValue() && "Changing value does not match operand.");
1917 GlobalValue
*GV
= dyn_cast
<GlobalValue
>(To
->stripPointerCasts());
1918 assert(GV
&& "Can only replace the operands with a global value");
1920 NoCFIValue
*&NewNC
= getContext().pImpl
->NoCFIValues
[GV
];
1922 return llvm::ConstantExpr::getBitCast(NewNC
, getType());
1924 getContext().pImpl
->NoCFIValues
.erase(getGlobalValue());
1928 if (GV
->getType() != getType())
1929 mutateType(GV
->getType());
1934 //---- ConstantExpr::get() implementations.
1937 /// This is a utility function to handle folding of casts and lookup of the
1938 /// cast in the ExprConstants map. It is used by the various get* methods below.
1939 static Constant
*getFoldedCast(Instruction::CastOps opc
, Constant
*C
, Type
*Ty
,
1940 bool OnlyIfReduced
= false) {
1941 assert(Ty
->isFirstClassType() && "Cannot cast to an aggregate type!");
1942 // Fold a few common cases
1943 if (Constant
*FC
= ConstantFoldCastInstruction(opc
, C
, Ty
))
1949 LLVMContextImpl
*pImpl
= Ty
->getContext().pImpl
;
1951 // Look up the constant in the table first to ensure uniqueness.
1952 ConstantExprKeyType
Key(opc
, C
);
1954 return pImpl
->ExprConstants
.getOrCreate(Ty
, Key
);
1957 Constant
*ConstantExpr::getCast(unsigned oc
, Constant
*C
, Type
*Ty
,
1958 bool OnlyIfReduced
) {
1959 Instruction::CastOps opc
= Instruction::CastOps(oc
);
1960 assert(Instruction::isCast(opc
) && "opcode out of range");
1961 assert(isSupportedCastOp(opc
) &&
1962 "Cast opcode not supported as constant expression");
1963 assert(C
&& Ty
&& "Null arguments to getCast");
1964 assert(CastInst::castIsValid(opc
, C
, Ty
) && "Invalid constantexpr cast!");
1968 llvm_unreachable("Invalid cast opcode");
1969 case Instruction::Trunc
:
1970 return getTrunc(C
, Ty
, OnlyIfReduced
);
1971 case Instruction::PtrToInt
:
1972 return getPtrToInt(C
, Ty
, OnlyIfReduced
);
1973 case Instruction::IntToPtr
:
1974 return getIntToPtr(C
, Ty
, OnlyIfReduced
);
1975 case Instruction::BitCast
:
1976 return getBitCast(C
, Ty
, OnlyIfReduced
);
1977 case Instruction::AddrSpaceCast
:
1978 return getAddrSpaceCast(C
, Ty
, OnlyIfReduced
);
1982 Constant
*ConstantExpr::getTruncOrBitCast(Constant
*C
, Type
*Ty
) {
1983 if (C
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
1984 return getBitCast(C
, Ty
);
1985 return getTrunc(C
, Ty
);
1988 Constant
*ConstantExpr::getPointerCast(Constant
*S
, Type
*Ty
) {
1989 assert(S
->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1990 assert((Ty
->isIntOrIntVectorTy() || Ty
->isPtrOrPtrVectorTy()) &&
1993 if (Ty
->isIntOrIntVectorTy())
1994 return getPtrToInt(S
, Ty
);
1996 unsigned SrcAS
= S
->getType()->getPointerAddressSpace();
1997 if (Ty
->isPtrOrPtrVectorTy() && SrcAS
!= Ty
->getPointerAddressSpace())
1998 return getAddrSpaceCast(S
, Ty
);
2000 return getBitCast(S
, Ty
);
2003 Constant
*ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant
*S
,
2005 assert(S
->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2006 assert(Ty
->isPtrOrPtrVectorTy() && "Invalid cast");
2008 if (S
->getType()->getPointerAddressSpace() != Ty
->getPointerAddressSpace())
2009 return getAddrSpaceCast(S
, Ty
);
2011 return getBitCast(S
, Ty
);
2014 Constant
*ConstantExpr::getTrunc(Constant
*C
, Type
*Ty
, bool OnlyIfReduced
) {
2016 bool fromVec
= isa
<VectorType
>(C
->getType());
2017 bool toVec
= isa
<VectorType
>(Ty
);
2019 assert((fromVec
== toVec
) && "Cannot convert from scalar to/from vector");
2020 assert(C
->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
2021 assert(Ty
->isIntOrIntVectorTy() && "Trunc produces only integral");
2022 assert(C
->getType()->getScalarSizeInBits() > Ty
->getScalarSizeInBits()&&
2023 "SrcTy must be larger than DestTy for Trunc!");
2025 return getFoldedCast(Instruction::Trunc
, C
, Ty
, OnlyIfReduced
);
2028 Constant
*ConstantExpr::getPtrToInt(Constant
*C
, Type
*DstTy
,
2029 bool OnlyIfReduced
) {
2030 assert(C
->getType()->isPtrOrPtrVectorTy() &&
2031 "PtrToInt source must be pointer or pointer vector");
2032 assert(DstTy
->isIntOrIntVectorTy() &&
2033 "PtrToInt destination must be integer or integer vector");
2034 assert(isa
<VectorType
>(C
->getType()) == isa
<VectorType
>(DstTy
));
2035 if (isa
<VectorType
>(C
->getType()))
2036 assert(cast
<VectorType
>(C
->getType())->getElementCount() ==
2037 cast
<VectorType
>(DstTy
)->getElementCount() &&
2038 "Invalid cast between a different number of vector elements");
2039 return getFoldedCast(Instruction::PtrToInt
, C
, DstTy
, OnlyIfReduced
);
2042 Constant
*ConstantExpr::getIntToPtr(Constant
*C
, Type
*DstTy
,
2043 bool OnlyIfReduced
) {
2044 assert(C
->getType()->isIntOrIntVectorTy() &&
2045 "IntToPtr source must be integer or integer vector");
2046 assert(DstTy
->isPtrOrPtrVectorTy() &&
2047 "IntToPtr destination must be a pointer or pointer vector");
2048 assert(isa
<VectorType
>(C
->getType()) == isa
<VectorType
>(DstTy
));
2049 if (isa
<VectorType
>(C
->getType()))
2050 assert(cast
<VectorType
>(C
->getType())->getElementCount() ==
2051 cast
<VectorType
>(DstTy
)->getElementCount() &&
2052 "Invalid cast between a different number of vector elements");
2053 return getFoldedCast(Instruction::IntToPtr
, C
, DstTy
, OnlyIfReduced
);
2056 Constant
*ConstantExpr::getBitCast(Constant
*C
, Type
*DstTy
,
2057 bool OnlyIfReduced
) {
2058 assert(CastInst::castIsValid(Instruction::BitCast
, C
, DstTy
) &&
2059 "Invalid constantexpr bitcast!");
2061 // It is common to ask for a bitcast of a value to its own type, handle this
2063 if (C
->getType() == DstTy
) return C
;
2065 return getFoldedCast(Instruction::BitCast
, C
, DstTy
, OnlyIfReduced
);
2068 Constant
*ConstantExpr::getAddrSpaceCast(Constant
*C
, Type
*DstTy
,
2069 bool OnlyIfReduced
) {
2070 assert(CastInst::castIsValid(Instruction::AddrSpaceCast
, C
, DstTy
) &&
2071 "Invalid constantexpr addrspacecast!");
2072 return getFoldedCast(Instruction::AddrSpaceCast
, C
, DstTy
, OnlyIfReduced
);
2075 Constant
*ConstantExpr::get(unsigned Opcode
, Constant
*C1
, Constant
*C2
,
2076 unsigned Flags
, Type
*OnlyIfReducedTy
) {
2077 // Check the operands for consistency first.
2078 assert(Instruction::isBinaryOp(Opcode
) &&
2079 "Invalid opcode in binary constant expression");
2080 assert(isSupportedBinOp(Opcode
) &&
2081 "Binop not supported as constant expression");
2082 assert(C1
->getType() == C2
->getType() &&
2083 "Operand types in binary constant expression should match");
2087 case Instruction::Add
:
2088 case Instruction::Sub
:
2089 case Instruction::Mul
:
2090 assert(C1
->getType()->isIntOrIntVectorTy() &&
2091 "Tried to create an integer operation on a non-integer type!");
2093 case Instruction::And
:
2094 case Instruction::Or
:
2095 case Instruction::Xor
:
2096 assert(C1
->getType()->isIntOrIntVectorTy() &&
2097 "Tried to create a logical operation on a non-integral type!");
2099 case Instruction::Shl
:
2100 case Instruction::LShr
:
2101 case Instruction::AShr
:
2102 assert(C1
->getType()->isIntOrIntVectorTy() &&
2103 "Tried to create a shift operation on a non-integer type!");
2110 if (Constant
*FC
= ConstantFoldBinaryInstruction(Opcode
, C1
, C2
))
2113 if (OnlyIfReducedTy
== C1
->getType())
2116 Constant
*ArgVec
[] = { C1
, C2
};
2117 ConstantExprKeyType
Key(Opcode
, ArgVec
, 0, Flags
);
2119 LLVMContextImpl
*pImpl
= C1
->getContext().pImpl
;
2120 return pImpl
->ExprConstants
.getOrCreate(C1
->getType(), Key
);
2123 bool ConstantExpr::isDesirableBinOp(unsigned Opcode
) {
2125 case Instruction::UDiv
:
2126 case Instruction::SDiv
:
2127 case Instruction::URem
:
2128 case Instruction::SRem
:
2129 case Instruction::FAdd
:
2130 case Instruction::FSub
:
2131 case Instruction::FMul
:
2132 case Instruction::FDiv
:
2133 case Instruction::FRem
:
2134 case Instruction::And
:
2135 case Instruction::Or
:
2136 case Instruction::LShr
:
2137 case Instruction::AShr
:
2139 case Instruction::Add
:
2140 case Instruction::Sub
:
2141 case Instruction::Mul
:
2142 case Instruction::Shl
:
2143 case Instruction::Xor
:
2146 llvm_unreachable("Argument must be binop opcode");
2150 bool ConstantExpr::isSupportedBinOp(unsigned Opcode
) {
2152 case Instruction::UDiv
:
2153 case Instruction::SDiv
:
2154 case Instruction::URem
:
2155 case Instruction::SRem
:
2156 case Instruction::FAdd
:
2157 case Instruction::FSub
:
2158 case Instruction::FMul
:
2159 case Instruction::FDiv
:
2160 case Instruction::FRem
:
2161 case Instruction::And
:
2162 case Instruction::Or
:
2163 case Instruction::LShr
:
2164 case Instruction::AShr
:
2166 case Instruction::Add
:
2167 case Instruction::Sub
:
2168 case Instruction::Mul
:
2169 case Instruction::Shl
:
2170 case Instruction::Xor
:
2173 llvm_unreachable("Argument must be binop opcode");
2177 bool ConstantExpr::isDesirableCastOp(unsigned Opcode
) {
2179 case Instruction::ZExt
:
2180 case Instruction::SExt
:
2181 case Instruction::FPTrunc
:
2182 case Instruction::FPExt
:
2183 case Instruction::UIToFP
:
2184 case Instruction::SIToFP
:
2185 case Instruction::FPToUI
:
2186 case Instruction::FPToSI
:
2188 case Instruction::Trunc
:
2189 case Instruction::PtrToInt
:
2190 case Instruction::IntToPtr
:
2191 case Instruction::BitCast
:
2192 case Instruction::AddrSpaceCast
:
2195 llvm_unreachable("Argument must be cast opcode");
2199 bool ConstantExpr::isSupportedCastOp(unsigned Opcode
) {
2201 case Instruction::ZExt
:
2202 case Instruction::SExt
:
2203 case Instruction::FPTrunc
:
2204 case Instruction::FPExt
:
2205 case Instruction::UIToFP
:
2206 case Instruction::SIToFP
:
2207 case Instruction::FPToUI
:
2208 case Instruction::FPToSI
:
2210 case Instruction::Trunc
:
2211 case Instruction::PtrToInt
:
2212 case Instruction::IntToPtr
:
2213 case Instruction::BitCast
:
2214 case Instruction::AddrSpaceCast
:
2217 llvm_unreachable("Argument must be cast opcode");
2221 Constant
*ConstantExpr::getSizeOf(Type
* Ty
) {
2222 // sizeof is implemented as: (i64) gep (Ty*)null, 1
2223 // Note that a non-inbounds gep is used, as null isn't within any object.
2224 Constant
*GEPIdx
= ConstantInt::get(Type::getInt32Ty(Ty
->getContext()), 1);
2225 Constant
*GEP
= getGetElementPtr(
2226 Ty
, Constant::getNullValue(PointerType::getUnqual(Ty
)), GEPIdx
);
2227 return getPtrToInt(GEP
,
2228 Type::getInt64Ty(Ty
->getContext()));
2231 Constant
*ConstantExpr::getAlignOf(Type
* Ty
) {
2232 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
2233 // Note that a non-inbounds gep is used, as null isn't within any object.
2234 Type
*AligningTy
= StructType::get(Type::getInt1Ty(Ty
->getContext()), Ty
);
2235 Constant
*NullPtr
= Constant::getNullValue(PointerType::getUnqual(AligningTy
->getContext()));
2236 Constant
*Zero
= ConstantInt::get(Type::getInt64Ty(Ty
->getContext()), 0);
2237 Constant
*One
= ConstantInt::get(Type::getInt32Ty(Ty
->getContext()), 1);
2238 Constant
*Indices
[2] = { Zero
, One
};
2239 Constant
*GEP
= getGetElementPtr(AligningTy
, NullPtr
, Indices
);
2240 return getPtrToInt(GEP
,
2241 Type::getInt64Ty(Ty
->getContext()));
2244 Constant
*ConstantExpr::getCompare(unsigned short Predicate
, Constant
*C1
,
2245 Constant
*C2
, bool OnlyIfReduced
) {
2246 assert(C1
->getType() == C2
->getType() && "Op types should be identical!");
2248 switch (Predicate
) {
2249 default: llvm_unreachable("Invalid CmpInst predicate");
2250 case CmpInst::FCMP_FALSE
: case CmpInst::FCMP_OEQ
: case CmpInst::FCMP_OGT
:
2251 case CmpInst::FCMP_OGE
: case CmpInst::FCMP_OLT
: case CmpInst::FCMP_OLE
:
2252 case CmpInst::FCMP_ONE
: case CmpInst::FCMP_ORD
: case CmpInst::FCMP_UNO
:
2253 case CmpInst::FCMP_UEQ
: case CmpInst::FCMP_UGT
: case CmpInst::FCMP_UGE
:
2254 case CmpInst::FCMP_ULT
: case CmpInst::FCMP_ULE
: case CmpInst::FCMP_UNE
:
2255 case CmpInst::FCMP_TRUE
:
2256 return getFCmp(Predicate
, C1
, C2
, OnlyIfReduced
);
2258 case CmpInst::ICMP_EQ
: case CmpInst::ICMP_NE
: case CmpInst::ICMP_UGT
:
2259 case CmpInst::ICMP_UGE
: case CmpInst::ICMP_ULT
: case CmpInst::ICMP_ULE
:
2260 case CmpInst::ICMP_SGT
: case CmpInst::ICMP_SGE
: case CmpInst::ICMP_SLT
:
2261 case CmpInst::ICMP_SLE
:
2262 return getICmp(Predicate
, C1
, C2
, OnlyIfReduced
);
2266 Constant
*ConstantExpr::getGetElementPtr(Type
*Ty
, Constant
*C
,
2267 ArrayRef
<Value
*> Idxs
, bool InBounds
,
2268 std::optional
<unsigned> InRangeIndex
,
2269 Type
*OnlyIfReducedTy
) {
2270 assert(Ty
&& "Must specify element type");
2271 assert(isSupportedGetElementPtr(Ty
) && "Element type is unsupported!");
2274 ConstantFoldGetElementPtr(Ty
, C
, InBounds
, InRangeIndex
, Idxs
))
2275 return FC
; // Fold a few common cases.
2277 assert(GetElementPtrInst::getIndexedType(Ty
, Idxs
) &&
2278 "GEP indices invalid!");;
2280 // Get the result type of the getelementptr!
2281 Type
*ReqTy
= GetElementPtrInst::getGEPReturnType(C
, Idxs
);
2282 if (OnlyIfReducedTy
== ReqTy
)
2285 auto EltCount
= ElementCount::getFixed(0);
2286 if (VectorType
*VecTy
= dyn_cast
<VectorType
>(ReqTy
))
2287 EltCount
= VecTy
->getElementCount();
2289 // Look up the constant in the table first to ensure uniqueness
2290 std::vector
<Constant
*> ArgVec
;
2291 ArgVec
.reserve(1 + Idxs
.size());
2292 ArgVec
.push_back(C
);
2293 auto GTI
= gep_type_begin(Ty
, Idxs
), GTE
= gep_type_end(Ty
, Idxs
);
2294 for (; GTI
!= GTE
; ++GTI
) {
2295 auto *Idx
= cast
<Constant
>(GTI
.getOperand());
2297 (!isa
<VectorType
>(Idx
->getType()) ||
2298 cast
<VectorType
>(Idx
->getType())->getElementCount() == EltCount
) &&
2299 "getelementptr index type missmatch");
2301 if (GTI
.isStruct() && Idx
->getType()->isVectorTy()) {
2302 Idx
= Idx
->getSplatValue();
2303 } else if (GTI
.isSequential() && EltCount
.isNonZero() &&
2304 !Idx
->getType()->isVectorTy()) {
2305 Idx
= ConstantVector::getSplat(EltCount
, Idx
);
2307 ArgVec
.push_back(Idx
);
2310 unsigned SubClassOptionalData
= InBounds
? GEPOperator::IsInBounds
: 0;
2311 if (InRangeIndex
&& *InRangeIndex
< 63)
2312 SubClassOptionalData
|= (*InRangeIndex
+ 1) << 1;
2313 const ConstantExprKeyType
Key(Instruction::GetElementPtr
, ArgVec
, 0,
2314 SubClassOptionalData
, std::nullopt
, Ty
);
2316 LLVMContextImpl
*pImpl
= C
->getContext().pImpl
;
2317 return pImpl
->ExprConstants
.getOrCreate(ReqTy
, Key
);
2320 Constant
*ConstantExpr::getICmp(unsigned short pred
, Constant
*LHS
,
2321 Constant
*RHS
, bool OnlyIfReduced
) {
2322 auto Predicate
= static_cast<CmpInst::Predicate
>(pred
);
2323 assert(LHS
->getType() == RHS
->getType());
2324 assert(CmpInst::isIntPredicate(Predicate
) && "Invalid ICmp Predicate");
2326 if (Constant
*FC
= ConstantFoldCompareInstruction(Predicate
, LHS
, RHS
))
2327 return FC
; // Fold a few common cases...
2332 // Look up the constant in the table first to ensure uniqueness
2333 Constant
*ArgVec
[] = { LHS
, RHS
};
2334 // Get the key type with both the opcode and predicate
2335 const ConstantExprKeyType
Key(Instruction::ICmp
, ArgVec
, Predicate
);
2337 Type
*ResultTy
= Type::getInt1Ty(LHS
->getContext());
2338 if (VectorType
*VT
= dyn_cast
<VectorType
>(LHS
->getType()))
2339 ResultTy
= VectorType::get(ResultTy
, VT
->getElementCount());
2341 LLVMContextImpl
*pImpl
= LHS
->getType()->getContext().pImpl
;
2342 return pImpl
->ExprConstants
.getOrCreate(ResultTy
, Key
);
2345 Constant
*ConstantExpr::getFCmp(unsigned short pred
, Constant
*LHS
,
2346 Constant
*RHS
, bool OnlyIfReduced
) {
2347 auto Predicate
= static_cast<CmpInst::Predicate
>(pred
);
2348 assert(LHS
->getType() == RHS
->getType());
2349 assert(CmpInst::isFPPredicate(Predicate
) && "Invalid FCmp Predicate");
2351 if (Constant
*FC
= ConstantFoldCompareInstruction(Predicate
, LHS
, RHS
))
2352 return FC
; // Fold a few common cases...
2357 // Look up the constant in the table first to ensure uniqueness
2358 Constant
*ArgVec
[] = { LHS
, RHS
};
2359 // Get the key type with both the opcode and predicate
2360 const ConstantExprKeyType
Key(Instruction::FCmp
, ArgVec
, Predicate
);
2362 Type
*ResultTy
= Type::getInt1Ty(LHS
->getContext());
2363 if (VectorType
*VT
= dyn_cast
<VectorType
>(LHS
->getType()))
2364 ResultTy
= VectorType::get(ResultTy
, VT
->getElementCount());
2366 LLVMContextImpl
*pImpl
= LHS
->getType()->getContext().pImpl
;
2367 return pImpl
->ExprConstants
.getOrCreate(ResultTy
, Key
);
2370 Constant
*ConstantExpr::getExtractElement(Constant
*Val
, Constant
*Idx
,
2371 Type
*OnlyIfReducedTy
) {
2372 assert(Val
->getType()->isVectorTy() &&
2373 "Tried to create extractelement operation on non-vector type!");
2374 assert(Idx
->getType()->isIntegerTy() &&
2375 "Extractelement index must be an integer type!");
2377 if (Constant
*FC
= ConstantFoldExtractElementInstruction(Val
, Idx
))
2378 return FC
; // Fold a few common cases.
2380 Type
*ReqTy
= cast
<VectorType
>(Val
->getType())->getElementType();
2381 if (OnlyIfReducedTy
== ReqTy
)
2384 // Look up the constant in the table first to ensure uniqueness
2385 Constant
*ArgVec
[] = { Val
, Idx
};
2386 const ConstantExprKeyType
Key(Instruction::ExtractElement
, ArgVec
);
2388 LLVMContextImpl
*pImpl
= Val
->getContext().pImpl
;
2389 return pImpl
->ExprConstants
.getOrCreate(ReqTy
, Key
);
2392 Constant
*ConstantExpr::getInsertElement(Constant
*Val
, Constant
*Elt
,
2393 Constant
*Idx
, Type
*OnlyIfReducedTy
) {
2394 assert(Val
->getType()->isVectorTy() &&
2395 "Tried to create insertelement operation on non-vector type!");
2396 assert(Elt
->getType() == cast
<VectorType
>(Val
->getType())->getElementType() &&
2397 "Insertelement types must match!");
2398 assert(Idx
->getType()->isIntegerTy() &&
2399 "Insertelement index must be i32 type!");
2401 if (Constant
*FC
= ConstantFoldInsertElementInstruction(Val
, Elt
, Idx
))
2402 return FC
; // Fold a few common cases.
2404 if (OnlyIfReducedTy
== Val
->getType())
2407 // Look up the constant in the table first to ensure uniqueness
2408 Constant
*ArgVec
[] = { Val
, Elt
, Idx
};
2409 const ConstantExprKeyType
Key(Instruction::InsertElement
, ArgVec
);
2411 LLVMContextImpl
*pImpl
= Val
->getContext().pImpl
;
2412 return pImpl
->ExprConstants
.getOrCreate(Val
->getType(), Key
);
2415 Constant
*ConstantExpr::getShuffleVector(Constant
*V1
, Constant
*V2
,
2417 Type
*OnlyIfReducedTy
) {
2418 assert(ShuffleVectorInst::isValidOperands(V1
, V2
, Mask
) &&
2419 "Invalid shuffle vector constant expr operands!");
2421 if (Constant
*FC
= ConstantFoldShuffleVectorInstruction(V1
, V2
, Mask
))
2422 return FC
; // Fold a few common cases.
2424 unsigned NElts
= Mask
.size();
2425 auto V1VTy
= cast
<VectorType
>(V1
->getType());
2426 Type
*EltTy
= V1VTy
->getElementType();
2427 bool TypeIsScalable
= isa
<ScalableVectorType
>(V1VTy
);
2428 Type
*ShufTy
= VectorType::get(EltTy
, NElts
, TypeIsScalable
);
2430 if (OnlyIfReducedTy
== ShufTy
)
2433 // Look up the constant in the table first to ensure uniqueness
2434 Constant
*ArgVec
[] = {V1
, V2
};
2435 ConstantExprKeyType
Key(Instruction::ShuffleVector
, ArgVec
, 0, 0, Mask
);
2437 LLVMContextImpl
*pImpl
= ShufTy
->getContext().pImpl
;
2438 return pImpl
->ExprConstants
.getOrCreate(ShufTy
, Key
);
2441 Constant
*ConstantExpr::getNeg(Constant
*C
, bool HasNUW
, bool HasNSW
) {
2442 assert(C
->getType()->isIntOrIntVectorTy() &&
2443 "Cannot NEG a nonintegral value!");
2444 return getSub(ConstantInt::get(C
->getType(), 0), C
, HasNUW
, HasNSW
);
2447 Constant
*ConstantExpr::getNot(Constant
*C
) {
2448 assert(C
->getType()->isIntOrIntVectorTy() &&
2449 "Cannot NOT a nonintegral value!");
2450 return get(Instruction::Xor
, C
, Constant::getAllOnesValue(C
->getType()));
2453 Constant
*ConstantExpr::getAdd(Constant
*C1
, Constant
*C2
,
2454 bool HasNUW
, bool HasNSW
) {
2455 unsigned Flags
= (HasNUW
? OverflowingBinaryOperator::NoUnsignedWrap
: 0) |
2456 (HasNSW
? OverflowingBinaryOperator::NoSignedWrap
: 0);
2457 return get(Instruction::Add
, C1
, C2
, Flags
);
2460 Constant
*ConstantExpr::getSub(Constant
*C1
, Constant
*C2
,
2461 bool HasNUW
, bool HasNSW
) {
2462 unsigned Flags
= (HasNUW
? OverflowingBinaryOperator::NoUnsignedWrap
: 0) |
2463 (HasNSW
? OverflowingBinaryOperator::NoSignedWrap
: 0);
2464 return get(Instruction::Sub
, C1
, C2
, Flags
);
2467 Constant
*ConstantExpr::getMul(Constant
*C1
, Constant
*C2
,
2468 bool HasNUW
, bool HasNSW
) {
2469 unsigned Flags
= (HasNUW
? OverflowingBinaryOperator::NoUnsignedWrap
: 0) |
2470 (HasNSW
? OverflowingBinaryOperator::NoSignedWrap
: 0);
2471 return get(Instruction::Mul
, C1
, C2
, Flags
);
2474 Constant
*ConstantExpr::getXor(Constant
*C1
, Constant
*C2
) {
2475 return get(Instruction::Xor
, C1
, C2
);
2478 Constant
*ConstantExpr::getShl(Constant
*C1
, Constant
*C2
,
2479 bool HasNUW
, bool HasNSW
) {
2480 unsigned Flags
= (HasNUW
? OverflowingBinaryOperator::NoUnsignedWrap
: 0) |
2481 (HasNSW
? OverflowingBinaryOperator::NoSignedWrap
: 0);
2482 return get(Instruction::Shl
, C1
, C2
, Flags
);
2485 Constant
*ConstantExpr::getExactLogBase2(Constant
*C
) {
2486 Type
*Ty
= C
->getType();
2488 if (match(C
, m_APInt(IVal
)) && IVal
->isPowerOf2())
2489 return ConstantInt::get(Ty
, IVal
->logBase2());
2491 // FIXME: We can extract pow of 2 of splat constant for scalable vectors.
2492 auto *VecTy
= dyn_cast
<FixedVectorType
>(Ty
);
2496 SmallVector
<Constant
*, 4> Elts
;
2497 for (unsigned I
= 0, E
= VecTy
->getNumElements(); I
!= E
; ++I
) {
2498 Constant
*Elt
= C
->getAggregateElement(I
);
2501 // Note that log2(iN undef) is *NOT* iN undef, because log2(iN undef) u< N.
2502 if (isa
<UndefValue
>(Elt
)) {
2503 Elts
.push_back(Constant::getNullValue(Ty
->getScalarType()));
2506 if (!match(Elt
, m_APInt(IVal
)) || !IVal
->isPowerOf2())
2508 Elts
.push_back(ConstantInt::get(Ty
->getScalarType(), IVal
->logBase2()));
2511 return ConstantVector::get(Elts
);
2514 Constant
*ConstantExpr::getBinOpIdentity(unsigned Opcode
, Type
*Ty
,
2515 bool AllowRHSConstant
, bool NSZ
) {
2516 assert(Instruction::isBinaryOp(Opcode
) && "Only binops allowed");
2518 // Commutative opcodes: it does not matter if AllowRHSConstant is set.
2519 if (Instruction::isCommutative(Opcode
)) {
2521 case Instruction::Add
: // X + 0 = X
2522 case Instruction::Or
: // X | 0 = X
2523 case Instruction::Xor
: // X ^ 0 = X
2524 return Constant::getNullValue(Ty
);
2525 case Instruction::Mul
: // X * 1 = X
2526 return ConstantInt::get(Ty
, 1);
2527 case Instruction::And
: // X & -1 = X
2528 return Constant::getAllOnesValue(Ty
);
2529 case Instruction::FAdd
: // X + -0.0 = X
2530 return ConstantFP::getZero(Ty
, !NSZ
);
2531 case Instruction::FMul
: // X * 1.0 = X
2532 return ConstantFP::get(Ty
, 1.0);
2534 llvm_unreachable("Every commutative binop has an identity constant");
2538 // Non-commutative opcodes: AllowRHSConstant must be set.
2539 if (!AllowRHSConstant
)
2543 case Instruction::Sub
: // X - 0 = X
2544 case Instruction::Shl
: // X << 0 = X
2545 case Instruction::LShr
: // X >>u 0 = X
2546 case Instruction::AShr
: // X >> 0 = X
2547 case Instruction::FSub
: // X - 0.0 = X
2548 return Constant::getNullValue(Ty
);
2549 case Instruction::SDiv
: // X / 1 = X
2550 case Instruction::UDiv
: // X /u 1 = X
2551 return ConstantInt::get(Ty
, 1);
2552 case Instruction::FDiv
: // X / 1.0 = X
2553 return ConstantFP::get(Ty
, 1.0);
2559 Constant
*ConstantExpr::getIntrinsicIdentity(Intrinsic::ID ID
, Type
*Ty
) {
2561 case Intrinsic::umax
:
2562 return Constant::getNullValue(Ty
);
2563 case Intrinsic::umin
:
2564 return Constant::getAllOnesValue(Ty
);
2565 case Intrinsic::smax
:
2566 return Constant::getIntegerValue(
2567 Ty
, APInt::getSignedMinValue(Ty
->getIntegerBitWidth()));
2568 case Intrinsic::smin
:
2569 return Constant::getIntegerValue(
2570 Ty
, APInt::getSignedMaxValue(Ty
->getIntegerBitWidth()));
2576 Constant
*ConstantExpr::getIdentity(Instruction
*I
, Type
*Ty
,
2577 bool AllowRHSConstant
, bool NSZ
) {
2578 if (I
->isBinaryOp())
2579 return getBinOpIdentity(I
->getOpcode(), Ty
, AllowRHSConstant
, NSZ
);
2580 if (IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(I
))
2581 return getIntrinsicIdentity(II
->getIntrinsicID(), Ty
);
2585 Constant
*ConstantExpr::getBinOpAbsorber(unsigned Opcode
, Type
*Ty
) {
2588 // Doesn't have an absorber.
2591 case Instruction::Or
:
2592 return Constant::getAllOnesValue(Ty
);
2594 case Instruction::And
:
2595 case Instruction::Mul
:
2596 return Constant::getNullValue(Ty
);
2600 /// Remove the constant from the constant table.
2601 void ConstantExpr::destroyConstantImpl() {
2602 getType()->getContext().pImpl
->ExprConstants
.remove(this);
2605 const char *ConstantExpr::getOpcodeName() const {
2606 return Instruction::getOpcodeName(getOpcode());
2609 GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2610 Type
*SrcElementTy
, Constant
*C
, ArrayRef
<Constant
*> IdxList
, Type
*DestTy
)
2611 : ConstantExpr(DestTy
, Instruction::GetElementPtr
,
2612 OperandTraits
<GetElementPtrConstantExpr
>::op_end(this) -
2613 (IdxList
.size() + 1),
2614 IdxList
.size() + 1),
2615 SrcElementTy(SrcElementTy
),
2616 ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy
, IdxList
)) {
2618 Use
*OperandList
= getOperandList();
2619 for (unsigned i
= 0, E
= IdxList
.size(); i
!= E
; ++i
)
2620 OperandList
[i
+1] = IdxList
[i
];
2623 Type
*GetElementPtrConstantExpr::getSourceElementType() const {
2624 return SrcElementTy
;
2627 Type
*GetElementPtrConstantExpr::getResultElementType() const {
2628 return ResElementTy
;
2631 //===----------------------------------------------------------------------===//
2632 // ConstantData* implementations
2634 Type
*ConstantDataSequential::getElementType() const {
2635 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(getType()))
2636 return ATy
->getElementType();
2637 return cast
<VectorType
>(getType())->getElementType();
2640 StringRef
ConstantDataSequential::getRawDataValues() const {
2641 return StringRef(DataElements
, getNumElements()*getElementByteSize());
2644 bool ConstantDataSequential::isElementTypeCompatible(Type
*Ty
) {
2645 if (Ty
->isHalfTy() || Ty
->isBFloatTy() || Ty
->isFloatTy() || Ty
->isDoubleTy())
2647 if (auto *IT
= dyn_cast
<IntegerType
>(Ty
)) {
2648 switch (IT
->getBitWidth()) {
2660 unsigned ConstantDataSequential::getNumElements() const {
2661 if (ArrayType
*AT
= dyn_cast
<ArrayType
>(getType()))
2662 return AT
->getNumElements();
2663 return cast
<FixedVectorType
>(getType())->getNumElements();
2667 uint64_t ConstantDataSequential::getElementByteSize() const {
2668 return getElementType()->getPrimitiveSizeInBits()/8;
2671 /// Return the start of the specified element.
2672 const char *ConstantDataSequential::getElementPointer(unsigned Elt
) const {
2673 assert(Elt
< getNumElements() && "Invalid Elt");
2674 return DataElements
+Elt
*getElementByteSize();
2678 /// Return true if the array is empty or all zeros.
2679 static bool isAllZeros(StringRef Arr
) {
2686 /// This is the underlying implementation of all of the
2687 /// ConstantDataSequential::get methods. They all thunk down to here, providing
2688 /// the correct element type. We take the bytes in as a StringRef because
2689 /// we *want* an underlying "char*" to avoid TBAA type punning violations.
2690 Constant
*ConstantDataSequential::getImpl(StringRef Elements
, Type
*Ty
) {
2692 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(Ty
))
2693 assert(isElementTypeCompatible(ATy
->getElementType()));
2695 assert(isElementTypeCompatible(cast
<VectorType
>(Ty
)->getElementType()));
2697 // If the elements are all zero or there are no elements, return a CAZ, which
2698 // is more dense and canonical.
2699 if (isAllZeros(Elements
))
2700 return ConstantAggregateZero::get(Ty
);
2702 // Do a lookup to see if we have already formed one of these.
2705 .pImpl
->CDSConstants
.insert(std::make_pair(Elements
, nullptr))
2708 // The bucket can point to a linked list of different CDS's that have the same
2709 // body but different types. For example, 0,0,0,1 could be a 4 element array
2710 // of i8, or a 1-element array of i32. They'll both end up in the same
2711 /// StringMap bucket, linked up by their Next pointers. Walk the list.
2712 std::unique_ptr
<ConstantDataSequential
> *Entry
= &Slot
.second
;
2713 for (; *Entry
; Entry
= &(*Entry
)->Next
)
2714 if ((*Entry
)->getType() == Ty
)
2715 return Entry
->get();
2717 // Okay, we didn't get a hit. Create a node of the right class, link it in,
2719 if (isa
<ArrayType
>(Ty
)) {
2720 // Use reset because std::make_unique can't access the constructor.
2721 Entry
->reset(new ConstantDataArray(Ty
, Slot
.first().data()));
2722 return Entry
->get();
2725 assert(isa
<VectorType
>(Ty
));
2726 // Use reset because std::make_unique can't access the constructor.
2727 Entry
->reset(new ConstantDataVector(Ty
, Slot
.first().data()));
2728 return Entry
->get();
2731 void ConstantDataSequential::destroyConstantImpl() {
2732 // Remove the constant from the StringMap.
2733 StringMap
<std::unique_ptr
<ConstantDataSequential
>> &CDSConstants
=
2734 getType()->getContext().pImpl
->CDSConstants
;
2736 auto Slot
= CDSConstants
.find(getRawDataValues());
2738 assert(Slot
!= CDSConstants
.end() && "CDS not found in uniquing table");
2740 std::unique_ptr
<ConstantDataSequential
> *Entry
= &Slot
->getValue();
2742 // Remove the entry from the hash table.
2743 if (!(*Entry
)->Next
) {
2744 // If there is only one value in the bucket (common case) it must be this
2745 // entry, and removing the entry should remove the bucket completely.
2746 assert(Entry
->get() == this && "Hash mismatch in ConstantDataSequential");
2747 getContext().pImpl
->CDSConstants
.erase(Slot
);
2751 // Otherwise, there are multiple entries linked off the bucket, unlink the
2752 // node we care about but keep the bucket around.
2754 std::unique_ptr
<ConstantDataSequential
> &Node
= *Entry
;
2755 assert(Node
&& "Didn't find entry in its uniquing hash table!");
2756 // If we found our entry, unlink it from the list and we're done.
2757 if (Node
.get() == this) {
2758 Node
= std::move(Node
->Next
);
2762 Entry
= &Node
->Next
;
2766 /// getFP() constructors - Return a constant of array type with a float
2767 /// element type taken from argument `ElementType', and count taken from
2768 /// argument `Elts'. The amount of bits of the contained type must match the
2769 /// number of bits of the type contained in the passed in ArrayRef.
2770 /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
2771 /// that this can return a ConstantAggregateZero object.
2772 Constant
*ConstantDataArray::getFP(Type
*ElementType
, ArrayRef
<uint16_t> Elts
) {
2773 assert((ElementType
->isHalfTy() || ElementType
->isBFloatTy()) &&
2774 "Element type is not a 16-bit float type");
2775 Type
*Ty
= ArrayType::get(ElementType
, Elts
.size());
2776 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
2777 return getImpl(StringRef(Data
, Elts
.size() * 2), Ty
);
2779 Constant
*ConstantDataArray::getFP(Type
*ElementType
, ArrayRef
<uint32_t> Elts
) {
2780 assert(ElementType
->isFloatTy() && "Element type is not a 32-bit float type");
2781 Type
*Ty
= ArrayType::get(ElementType
, Elts
.size());
2782 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
2783 return getImpl(StringRef(Data
, Elts
.size() * 4), Ty
);
2785 Constant
*ConstantDataArray::getFP(Type
*ElementType
, ArrayRef
<uint64_t> Elts
) {
2786 assert(ElementType
->isDoubleTy() &&
2787 "Element type is not a 64-bit float type");
2788 Type
*Ty
= ArrayType::get(ElementType
, Elts
.size());
2789 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
2790 return getImpl(StringRef(Data
, Elts
.size() * 8), Ty
);
2793 Constant
*ConstantDataArray::getString(LLVMContext
&Context
,
2794 StringRef Str
, bool AddNull
) {
2796 const uint8_t *Data
= Str
.bytes_begin();
2797 return get(Context
, ArrayRef(Data
, Str
.size()));
2800 SmallVector
<uint8_t, 64> ElementVals
;
2801 ElementVals
.append(Str
.begin(), Str
.end());
2802 ElementVals
.push_back(0);
2803 return get(Context
, ElementVals
);
2806 /// get() constructors - Return a constant with vector type with an element
2807 /// count and element type matching the ArrayRef passed in. Note that this
2808 /// can return a ConstantAggregateZero object.
2809 Constant
*ConstantDataVector::get(LLVMContext
&Context
, ArrayRef
<uint8_t> Elts
){
2810 auto *Ty
= FixedVectorType::get(Type::getInt8Ty(Context
), Elts
.size());
2811 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
2812 return getImpl(StringRef(Data
, Elts
.size() * 1), Ty
);
2814 Constant
*ConstantDataVector::get(LLVMContext
&Context
, ArrayRef
<uint16_t> Elts
){
2815 auto *Ty
= FixedVectorType::get(Type::getInt16Ty(Context
), Elts
.size());
2816 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
2817 return getImpl(StringRef(Data
, Elts
.size() * 2), Ty
);
2819 Constant
*ConstantDataVector::get(LLVMContext
&Context
, ArrayRef
<uint32_t> Elts
){
2820 auto *Ty
= FixedVectorType::get(Type::getInt32Ty(Context
), Elts
.size());
2821 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
2822 return getImpl(StringRef(Data
, Elts
.size() * 4), Ty
);
2824 Constant
*ConstantDataVector::get(LLVMContext
&Context
, ArrayRef
<uint64_t> Elts
){
2825 auto *Ty
= FixedVectorType::get(Type::getInt64Ty(Context
), Elts
.size());
2826 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
2827 return getImpl(StringRef(Data
, Elts
.size() * 8), Ty
);
2829 Constant
*ConstantDataVector::get(LLVMContext
&Context
, ArrayRef
<float> Elts
) {
2830 auto *Ty
= FixedVectorType::get(Type::getFloatTy(Context
), Elts
.size());
2831 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
2832 return getImpl(StringRef(Data
, Elts
.size() * 4), Ty
);
2834 Constant
*ConstantDataVector::get(LLVMContext
&Context
, ArrayRef
<double> Elts
) {
2835 auto *Ty
= FixedVectorType::get(Type::getDoubleTy(Context
), Elts
.size());
2836 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
2837 return getImpl(StringRef(Data
, Elts
.size() * 8), Ty
);
2840 /// getFP() constructors - Return a constant of vector type with a float
2841 /// element type taken from argument `ElementType', and count taken from
2842 /// argument `Elts'. The amount of bits of the contained type must match the
2843 /// number of bits of the type contained in the passed in ArrayRef.
2844 /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
2845 /// that this can return a ConstantAggregateZero object.
2846 Constant
*ConstantDataVector::getFP(Type
*ElementType
,
2847 ArrayRef
<uint16_t> Elts
) {
2848 assert((ElementType
->isHalfTy() || ElementType
->isBFloatTy()) &&
2849 "Element type is not a 16-bit float type");
2850 auto *Ty
= FixedVectorType::get(ElementType
, Elts
.size());
2851 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
2852 return getImpl(StringRef(Data
, Elts
.size() * 2), Ty
);
2854 Constant
*ConstantDataVector::getFP(Type
*ElementType
,
2855 ArrayRef
<uint32_t> Elts
) {
2856 assert(ElementType
->isFloatTy() && "Element type is not a 32-bit float type");
2857 auto *Ty
= FixedVectorType::get(ElementType
, Elts
.size());
2858 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
2859 return getImpl(StringRef(Data
, Elts
.size() * 4), Ty
);
2861 Constant
*ConstantDataVector::getFP(Type
*ElementType
,
2862 ArrayRef
<uint64_t> Elts
) {
2863 assert(ElementType
->isDoubleTy() &&
2864 "Element type is not a 64-bit float type");
2865 auto *Ty
= FixedVectorType::get(ElementType
, Elts
.size());
2866 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
2867 return getImpl(StringRef(Data
, Elts
.size() * 8), Ty
);
2870 Constant
*ConstantDataVector::getSplat(unsigned NumElts
, Constant
*V
) {
2871 assert(isElementTypeCompatible(V
->getType()) &&
2872 "Element type not compatible with ConstantData");
2873 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(V
)) {
2874 if (CI
->getType()->isIntegerTy(8)) {
2875 SmallVector
<uint8_t, 16> Elts(NumElts
, CI
->getZExtValue());
2876 return get(V
->getContext(), Elts
);
2878 if (CI
->getType()->isIntegerTy(16)) {
2879 SmallVector
<uint16_t, 16> Elts(NumElts
, CI
->getZExtValue());
2880 return get(V
->getContext(), Elts
);
2882 if (CI
->getType()->isIntegerTy(32)) {
2883 SmallVector
<uint32_t, 16> Elts(NumElts
, CI
->getZExtValue());
2884 return get(V
->getContext(), Elts
);
2886 assert(CI
->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
2887 SmallVector
<uint64_t, 16> Elts(NumElts
, CI
->getZExtValue());
2888 return get(V
->getContext(), Elts
);
2891 if (ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(V
)) {
2892 if (CFP
->getType()->isHalfTy()) {
2893 SmallVector
<uint16_t, 16> Elts(
2894 NumElts
, CFP
->getValueAPF().bitcastToAPInt().getLimitedValue());
2895 return getFP(V
->getType(), Elts
);
2897 if (CFP
->getType()->isBFloatTy()) {
2898 SmallVector
<uint16_t, 16> Elts(
2899 NumElts
, CFP
->getValueAPF().bitcastToAPInt().getLimitedValue());
2900 return getFP(V
->getType(), Elts
);
2902 if (CFP
->getType()->isFloatTy()) {
2903 SmallVector
<uint32_t, 16> Elts(
2904 NumElts
, CFP
->getValueAPF().bitcastToAPInt().getLimitedValue());
2905 return getFP(V
->getType(), Elts
);
2907 if (CFP
->getType()->isDoubleTy()) {
2908 SmallVector
<uint64_t, 16> Elts(
2909 NumElts
, CFP
->getValueAPF().bitcastToAPInt().getLimitedValue());
2910 return getFP(V
->getType(), Elts
);
2913 return ConstantVector::getSplat(ElementCount::getFixed(NumElts
), V
);
2917 uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt
) const {
2918 assert(isa
<IntegerType
>(getElementType()) &&
2919 "Accessor can only be used when element is an integer");
2920 const char *EltPtr
= getElementPointer(Elt
);
2922 // The data is stored in host byte order, make sure to cast back to the right
2923 // type to load with the right endianness.
2924 switch (getElementType()->getIntegerBitWidth()) {
2925 default: llvm_unreachable("Invalid bitwidth for CDS");
2927 return *reinterpret_cast<const uint8_t *>(EltPtr
);
2929 return *reinterpret_cast<const uint16_t *>(EltPtr
);
2931 return *reinterpret_cast<const uint32_t *>(EltPtr
);
2933 return *reinterpret_cast<const uint64_t *>(EltPtr
);
2937 APInt
ConstantDataSequential::getElementAsAPInt(unsigned Elt
) const {
2938 assert(isa
<IntegerType
>(getElementType()) &&
2939 "Accessor can only be used when element is an integer");
2940 const char *EltPtr
= getElementPointer(Elt
);
2942 // The data is stored in host byte order, make sure to cast back to the right
2943 // type to load with the right endianness.
2944 switch (getElementType()->getIntegerBitWidth()) {
2945 default: llvm_unreachable("Invalid bitwidth for CDS");
2947 auto EltVal
= *reinterpret_cast<const uint8_t *>(EltPtr
);
2948 return APInt(8, EltVal
);
2951 auto EltVal
= *reinterpret_cast<const uint16_t *>(EltPtr
);
2952 return APInt(16, EltVal
);
2955 auto EltVal
= *reinterpret_cast<const uint32_t *>(EltPtr
);
2956 return APInt(32, EltVal
);
2959 auto EltVal
= *reinterpret_cast<const uint64_t *>(EltPtr
);
2960 return APInt(64, EltVal
);
2965 APFloat
ConstantDataSequential::getElementAsAPFloat(unsigned Elt
) const {
2966 const char *EltPtr
= getElementPointer(Elt
);
2968 switch (getElementType()->getTypeID()) {
2970 llvm_unreachable("Accessor can only be used when element is float/double!");
2971 case Type::HalfTyID
: {
2972 auto EltVal
= *reinterpret_cast<const uint16_t *>(EltPtr
);
2973 return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal
));
2975 case Type::BFloatTyID
: {
2976 auto EltVal
= *reinterpret_cast<const uint16_t *>(EltPtr
);
2977 return APFloat(APFloat::BFloat(), APInt(16, EltVal
));
2979 case Type::FloatTyID
: {
2980 auto EltVal
= *reinterpret_cast<const uint32_t *>(EltPtr
);
2981 return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal
));
2983 case Type::DoubleTyID
: {
2984 auto EltVal
= *reinterpret_cast<const uint64_t *>(EltPtr
);
2985 return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal
));
2990 float ConstantDataSequential::getElementAsFloat(unsigned Elt
) const {
2991 assert(getElementType()->isFloatTy() &&
2992 "Accessor can only be used when element is a 'float'");
2993 return *reinterpret_cast<const float *>(getElementPointer(Elt
));
2996 double ConstantDataSequential::getElementAsDouble(unsigned Elt
) const {
2997 assert(getElementType()->isDoubleTy() &&
2998 "Accessor can only be used when element is a 'float'");
2999 return *reinterpret_cast<const double *>(getElementPointer(Elt
));
3002 Constant
*ConstantDataSequential::getElementAsConstant(unsigned Elt
) const {
3003 if (getElementType()->isHalfTy() || getElementType()->isBFloatTy() ||
3004 getElementType()->isFloatTy() || getElementType()->isDoubleTy())
3005 return ConstantFP::get(getContext(), getElementAsAPFloat(Elt
));
3007 return ConstantInt::get(getElementType(), getElementAsInteger(Elt
));
3010 bool ConstantDataSequential::isString(unsigned CharSize
) const {
3011 return isa
<ArrayType
>(getType()) && getElementType()->isIntegerTy(CharSize
);
3014 bool ConstantDataSequential::isCString() const {
3018 StringRef Str
= getAsString();
3020 // The last value must be nul.
3021 if (Str
.back() != 0) return false;
3023 // Other elements must be non-nul.
3024 return !Str
.drop_back().contains(0);
3027 bool ConstantDataVector::isSplatData() const {
3028 const char *Base
= getRawDataValues().data();
3030 // Compare elements 1+ to the 0'th element.
3031 unsigned EltSize
= getElementByteSize();
3032 for (unsigned i
= 1, e
= getNumElements(); i
!= e
; ++i
)
3033 if (memcmp(Base
, Base
+i
*EltSize
, EltSize
))
3039 bool ConstantDataVector::isSplat() const {
3042 IsSplat
= isSplatData();
3047 Constant
*ConstantDataVector::getSplatValue() const {
3048 // If they're all the same, return the 0th one as a representative.
3049 return isSplat() ? getElementAsConstant(0) : nullptr;
3052 //===----------------------------------------------------------------------===//
3053 // handleOperandChange implementations
3055 /// Update this constant array to change uses of
3056 /// 'From' to be uses of 'To'. This must update the uniquing data structures
3059 /// Note that we intentionally replace all uses of From with To here. Consider
3060 /// a large array that uses 'From' 1000 times. By handling this case all here,
3061 /// ConstantArray::handleOperandChange is only invoked once, and that
3062 /// single invocation handles all 1000 uses. Handling them one at a time would
3063 /// work, but would be really slow because it would have to unique each updated
3066 void Constant::handleOperandChange(Value
*From
, Value
*To
) {
3067 Value
*Replacement
= nullptr;
3068 switch (getValueID()) {
3070 llvm_unreachable("Not a constant!");
3071 #define HANDLE_CONSTANT(Name) \
3072 case Value::Name##Val: \
3073 Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
3075 #include "llvm/IR/Value.def"
3078 // If handleOperandChangeImpl returned nullptr, then it handled
3079 // replacing itself and we don't want to delete or replace anything else here.
3083 // I do need to replace this with an existing value.
3084 assert(Replacement
!= this && "I didn't contain From!");
3086 // Everyone using this now uses the replacement.
3087 replaceAllUsesWith(Replacement
);
3089 // Delete the old constant!
3093 Value
*ConstantArray::handleOperandChangeImpl(Value
*From
, Value
*To
) {
3094 assert(isa
<Constant
>(To
) && "Cannot make Constant refer to non-constant!");
3095 Constant
*ToC
= cast
<Constant
>(To
);
3097 SmallVector
<Constant
*, 8> Values
;
3098 Values
.reserve(getNumOperands()); // Build replacement array.
3100 // Fill values with the modified operands of the constant array. Also,
3101 // compute whether this turns into an all-zeros array.
3102 unsigned NumUpdated
= 0;
3104 // Keep track of whether all the values in the array are "ToC".
3105 bool AllSame
= true;
3106 Use
*OperandList
= getOperandList();
3107 unsigned OperandNo
= 0;
3108 for (Use
*O
= OperandList
, *E
= OperandList
+getNumOperands(); O
!= E
; ++O
) {
3109 Constant
*Val
= cast
<Constant
>(O
->get());
3111 OperandNo
= (O
- OperandList
);
3115 Values
.push_back(Val
);
3116 AllSame
&= Val
== ToC
;
3119 if (AllSame
&& ToC
->isNullValue())
3120 return ConstantAggregateZero::get(getType());
3122 if (AllSame
&& isa
<UndefValue
>(ToC
))
3123 return UndefValue::get(getType());
3125 // Check for any other type of constant-folding.
3126 if (Constant
*C
= getImpl(getType(), Values
))
3129 // Update to the new value.
3130 return getContext().pImpl
->ArrayConstants
.replaceOperandsInPlace(
3131 Values
, this, From
, ToC
, NumUpdated
, OperandNo
);
3134 Value
*ConstantStruct::handleOperandChangeImpl(Value
*From
, Value
*To
) {
3135 assert(isa
<Constant
>(To
) && "Cannot make Constant refer to non-constant!");
3136 Constant
*ToC
= cast
<Constant
>(To
);
3138 Use
*OperandList
= getOperandList();
3140 SmallVector
<Constant
*, 8> Values
;
3141 Values
.reserve(getNumOperands()); // Build replacement struct.
3143 // Fill values with the modified operands of the constant struct. Also,
3144 // compute whether this turns into an all-zeros struct.
3145 unsigned NumUpdated
= 0;
3146 bool AllSame
= true;
3147 unsigned OperandNo
= 0;
3148 for (Use
*O
= OperandList
, *E
= OperandList
+ getNumOperands(); O
!= E
; ++O
) {
3149 Constant
*Val
= cast
<Constant
>(O
->get());
3151 OperandNo
= (O
- OperandList
);
3155 Values
.push_back(Val
);
3156 AllSame
&= Val
== ToC
;
3159 if (AllSame
&& ToC
->isNullValue())
3160 return ConstantAggregateZero::get(getType());
3162 if (AllSame
&& isa
<UndefValue
>(ToC
))
3163 return UndefValue::get(getType());
3165 // Update to the new value.
3166 return getContext().pImpl
->StructConstants
.replaceOperandsInPlace(
3167 Values
, this, From
, ToC
, NumUpdated
, OperandNo
);
3170 Value
*ConstantVector::handleOperandChangeImpl(Value
*From
, Value
*To
) {
3171 assert(isa
<Constant
>(To
) && "Cannot make Constant refer to non-constant!");
3172 Constant
*ToC
= cast
<Constant
>(To
);
3174 SmallVector
<Constant
*, 8> Values
;
3175 Values
.reserve(getNumOperands()); // Build replacement array...
3176 unsigned NumUpdated
= 0;
3177 unsigned OperandNo
= 0;
3178 for (unsigned i
= 0, e
= getNumOperands(); i
!= e
; ++i
) {
3179 Constant
*Val
= getOperand(i
);
3185 Values
.push_back(Val
);
3188 if (Constant
*C
= getImpl(Values
))
3191 // Update to the new value.
3192 return getContext().pImpl
->VectorConstants
.replaceOperandsInPlace(
3193 Values
, this, From
, ToC
, NumUpdated
, OperandNo
);
3196 Value
*ConstantExpr::handleOperandChangeImpl(Value
*From
, Value
*ToV
) {
3197 assert(isa
<Constant
>(ToV
) && "Cannot make Constant refer to non-constant!");
3198 Constant
*To
= cast
<Constant
>(ToV
);
3200 SmallVector
<Constant
*, 8> NewOps
;
3201 unsigned NumUpdated
= 0;
3202 unsigned OperandNo
= 0;
3203 for (unsigned i
= 0, e
= getNumOperands(); i
!= e
; ++i
) {
3204 Constant
*Op
= getOperand(i
);
3210 NewOps
.push_back(Op
);
3212 assert(NumUpdated
&& "I didn't contain From!");
3214 if (Constant
*C
= getWithOperands(NewOps
, getType(), true))
3217 // Update to the new value.
3218 return getContext().pImpl
->ExprConstants
.replaceOperandsInPlace(
3219 NewOps
, this, From
, To
, NumUpdated
, OperandNo
);
3222 Instruction
*ConstantExpr::getAsInstruction(Instruction
*InsertBefore
) const {
3223 SmallVector
<Value
*, 4> ValueOperands(operands());
3224 ArrayRef
<Value
*> Ops(ValueOperands
);
3226 switch (getOpcode()) {
3227 case Instruction::Trunc
:
3228 case Instruction::ZExt
:
3229 case Instruction::SExt
:
3230 case Instruction::FPTrunc
:
3231 case Instruction::FPExt
:
3232 case Instruction::UIToFP
:
3233 case Instruction::SIToFP
:
3234 case Instruction::FPToUI
:
3235 case Instruction::FPToSI
:
3236 case Instruction::PtrToInt
:
3237 case Instruction::IntToPtr
:
3238 case Instruction::BitCast
:
3239 case Instruction::AddrSpaceCast
:
3240 return CastInst::Create((Instruction::CastOps
)getOpcode(), Ops
[0],
3241 getType(), "", InsertBefore
);
3242 case Instruction::InsertElement
:
3243 return InsertElementInst::Create(Ops
[0], Ops
[1], Ops
[2], "", InsertBefore
);
3244 case Instruction::ExtractElement
:
3245 return ExtractElementInst::Create(Ops
[0], Ops
[1], "", InsertBefore
);
3246 case Instruction::ShuffleVector
:
3247 return new ShuffleVectorInst(Ops
[0], Ops
[1], getShuffleMask(), "",
3250 case Instruction::GetElementPtr
: {
3251 const auto *GO
= cast
<GEPOperator
>(this);
3252 if (GO
->isInBounds())
3253 return GetElementPtrInst::CreateInBounds(
3254 GO
->getSourceElementType(), Ops
[0], Ops
.slice(1), "", InsertBefore
);
3255 return GetElementPtrInst::Create(GO
->getSourceElementType(), Ops
[0],
3256 Ops
.slice(1), "", InsertBefore
);
3258 case Instruction::ICmp
:
3259 case Instruction::FCmp
:
3260 return CmpInst::Create((Instruction::OtherOps
)getOpcode(),
3261 (CmpInst::Predicate
)getPredicate(), Ops
[0], Ops
[1],
3264 assert(getNumOperands() == 2 && "Must be binary operator?");
3265 BinaryOperator
*BO
= BinaryOperator::Create(
3266 (Instruction::BinaryOps
)getOpcode(), Ops
[0], Ops
[1], "", InsertBefore
);
3267 if (isa
<OverflowingBinaryOperator
>(BO
)) {
3268 BO
->setHasNoUnsignedWrap(SubclassOptionalData
&
3269 OverflowingBinaryOperator::NoUnsignedWrap
);
3270 BO
->setHasNoSignedWrap(SubclassOptionalData
&
3271 OverflowingBinaryOperator::NoSignedWrap
);
3273 if (isa
<PossiblyExactOperator
>(BO
))
3274 BO
->setIsExact(SubclassOptionalData
& PossiblyExactOperator::IsExact
);