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 "ConstantFold.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/GetElementPtrTypeIterator.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Operator.h"
25 #include "llvm/IR/PatternMatch.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/MathExtras.h"
30 #include "llvm/Support/raw_ostream.h"
34 using namespace PatternMatch
;
36 //===----------------------------------------------------------------------===//
38 //===----------------------------------------------------------------------===//
40 bool Constant::isNegativeZeroValue() const {
41 // Floating point values have an explicit -0.0 value.
42 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
43 return CFP
->isZero() && CFP
->isNegative();
45 // Equivalent for a vector of -0.0's.
46 if (getType()->isVectorTy())
47 if (const auto *SplatCFP
= dyn_cast_or_null
<ConstantFP
>(getSplatValue()))
48 return SplatCFP
->isNegativeZeroValue();
50 // We've already handled true FP case; any other FP vectors can't represent -0.0.
51 if (getType()->isFPOrFPVectorTy())
54 // Otherwise, just use +0.0.
58 // Return true iff this constant is positive zero (floating point), negative
59 // zero (floating point), or a null value.
60 bool Constant::isZeroValue() const {
61 // Floating point values have an explicit -0.0 value.
62 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
65 // Check for constant splat vectors of 1 values.
66 if (getType()->isVectorTy())
67 if (const auto *SplatCFP
= dyn_cast_or_null
<ConstantFP
>(getSplatValue()))
68 return SplatCFP
->isZero();
70 // Otherwise, just use +0.0.
74 bool Constant::isNullValue() const {
76 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
80 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
81 // ppc_fp128 determine isZero using high order double only
82 // Should check the bitwise value to make sure all bits are zero.
83 return CFP
->isExactlyValue(+0.0);
85 // constant zero is zero for aggregates, cpnull is null for pointers, none for
87 return isa
<ConstantAggregateZero
>(this) || isa
<ConstantPointerNull
>(this) ||
88 isa
<ConstantTokenNone
>(this);
91 bool Constant::isAllOnesValue() const {
92 // Check for -1 integers
93 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
94 return CI
->isMinusOne();
96 // Check for FP which are bitcasted from -1 integers
97 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
98 return CFP
->getValueAPF().bitcastToAPInt().isAllOnesValue();
100 // Check for constant splat vectors of 1 values.
101 if (getType()->isVectorTy())
102 if (const auto *SplatVal
= getSplatValue())
103 return SplatVal
->isAllOnesValue();
108 bool Constant::isOneValue() const {
109 // Check for 1 integers
110 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
113 // Check for FP which are bitcasted from 1 integers
114 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
115 return CFP
->getValueAPF().bitcastToAPInt().isOneValue();
117 // Check for constant splat vectors of 1 values.
118 if (getType()->isVectorTy())
119 if (const auto *SplatVal
= getSplatValue())
120 return SplatVal
->isOneValue();
125 bool Constant::isNotOneValue() const {
126 // Check for 1 integers
127 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
128 return !CI
->isOneValue();
130 // Check for FP which are bitcasted from 1 integers
131 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
132 return !CFP
->getValueAPF().bitcastToAPInt().isOneValue();
134 // Check that vectors don't contain 1
135 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
136 for (unsigned I
= 0, E
= VTy
->getNumElements(); I
!= E
; ++I
) {
137 Constant
*Elt
= getAggregateElement(I
);
138 if (!Elt
|| !Elt
->isNotOneValue())
144 // Check for splats that don't contain 1
145 if (getType()->isVectorTy())
146 if (const auto *SplatVal
= getSplatValue())
147 return SplatVal
->isNotOneValue();
149 // It *may* contain 1, we can't tell.
153 bool Constant::isMinSignedValue() const {
154 // Check for INT_MIN integers
155 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
156 return CI
->isMinValue(/*isSigned=*/true);
158 // Check for FP which are bitcasted from INT_MIN integers
159 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
160 return CFP
->getValueAPF().bitcastToAPInt().isMinSignedValue();
162 // Check for splats of INT_MIN values.
163 if (getType()->isVectorTy())
164 if (const auto *SplatVal
= getSplatValue())
165 return SplatVal
->isMinSignedValue();
170 bool Constant::isNotMinSignedValue() const {
171 // Check for INT_MIN integers
172 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
173 return !CI
->isMinValue(/*isSigned=*/true);
175 // Check for FP which are bitcasted from INT_MIN integers
176 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(this))
177 return !CFP
->getValueAPF().bitcastToAPInt().isMinSignedValue();
179 // Check that vectors don't contain INT_MIN
180 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
181 for (unsigned I
= 0, E
= VTy
->getNumElements(); I
!= E
; ++I
) {
182 Constant
*Elt
= getAggregateElement(I
);
183 if (!Elt
|| !Elt
->isNotMinSignedValue())
189 // Check for splats that aren't INT_MIN
190 if (getType()->isVectorTy())
191 if (const auto *SplatVal
= getSplatValue())
192 return SplatVal
->isNotMinSignedValue();
194 // It *may* contain INT_MIN, we can't tell.
198 bool Constant::isFiniteNonZeroFP() const {
199 if (auto *CFP
= dyn_cast
<ConstantFP
>(this))
200 return CFP
->getValueAPF().isFiniteNonZero();
202 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
203 for (unsigned I
= 0, E
= VTy
->getNumElements(); I
!= E
; ++I
) {
204 auto *CFP
= dyn_cast_or_null
<ConstantFP
>(getAggregateElement(I
));
205 if (!CFP
|| !CFP
->getValueAPF().isFiniteNonZero())
211 if (getType()->isVectorTy())
212 if (const auto *SplatCFP
= dyn_cast_or_null
<ConstantFP
>(getSplatValue()))
213 return SplatCFP
->isFiniteNonZeroFP();
215 // It *may* contain finite non-zero, we can't tell.
219 bool Constant::isNormalFP() const {
220 if (auto *CFP
= dyn_cast
<ConstantFP
>(this))
221 return CFP
->getValueAPF().isNormal();
223 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
224 for (unsigned I
= 0, E
= VTy
->getNumElements(); I
!= E
; ++I
) {
225 auto *CFP
= dyn_cast_or_null
<ConstantFP
>(getAggregateElement(I
));
226 if (!CFP
|| !CFP
->getValueAPF().isNormal())
232 if (getType()->isVectorTy())
233 if (const auto *SplatCFP
= dyn_cast_or_null
<ConstantFP
>(getSplatValue()))
234 return SplatCFP
->isNormalFP();
236 // It *may* contain a normal fp value, we can't tell.
240 bool Constant::hasExactInverseFP() const {
241 if (auto *CFP
= dyn_cast
<ConstantFP
>(this))
242 return CFP
->getValueAPF().getExactInverse(nullptr);
244 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
245 for (unsigned I
= 0, E
= VTy
->getNumElements(); I
!= E
; ++I
) {
246 auto *CFP
= dyn_cast_or_null
<ConstantFP
>(getAggregateElement(I
));
247 if (!CFP
|| !CFP
->getValueAPF().getExactInverse(nullptr))
253 if (getType()->isVectorTy())
254 if (const auto *SplatCFP
= dyn_cast_or_null
<ConstantFP
>(getSplatValue()))
255 return SplatCFP
->hasExactInverseFP();
257 // It *may* have an exact inverse fp value, we can't tell.
261 bool Constant::isNaN() const {
262 if (auto *CFP
= dyn_cast
<ConstantFP
>(this))
265 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
266 for (unsigned I
= 0, E
= VTy
->getNumElements(); I
!= E
; ++I
) {
267 auto *CFP
= dyn_cast_or_null
<ConstantFP
>(getAggregateElement(I
));
268 if (!CFP
|| !CFP
->isNaN())
274 if (getType()->isVectorTy())
275 if (const auto *SplatCFP
= dyn_cast_or_null
<ConstantFP
>(getSplatValue()))
276 return SplatCFP
->isNaN();
278 // It *may* be NaN, we can't tell.
282 bool Constant::isElementWiseEqual(Value
*Y
) const {
283 // Are they fully identical?
287 // The input value must be a vector constant with the same type.
288 auto *VTy
= dyn_cast
<VectorType
>(getType());
289 if (!isa
<Constant
>(Y
) || !VTy
|| VTy
!= Y
->getType())
292 // TODO: Compare pointer constants?
293 if (!(VTy
->getElementType()->isIntegerTy() ||
294 VTy
->getElementType()->isFloatingPointTy()))
297 // They may still be identical element-wise (if they have `undef`s).
298 // Bitcast to integer to allow exact bitwise comparison for all types.
299 Type
*IntTy
= VectorType::getInteger(VTy
);
300 Constant
*C0
= ConstantExpr::getBitCast(const_cast<Constant
*>(this), IntTy
);
301 Constant
*C1
= ConstantExpr::getBitCast(cast
<Constant
>(Y
), IntTy
);
302 Constant
*CmpEq
= ConstantExpr::getICmp(ICmpInst::ICMP_EQ
, C0
, C1
);
303 return isa
<UndefValue
>(CmpEq
) || match(CmpEq
, m_One());
307 containsUndefinedElement(const Constant
*C
,
308 function_ref
<bool(const Constant
*)> HasFn
) {
309 if (auto *VTy
= dyn_cast
<VectorType
>(C
->getType())) {
312 if (isa
<ConstantAggregateZero
>(C
))
314 if (isa
<ScalableVectorType
>(C
->getType()))
317 for (unsigned i
= 0, e
= cast
<FixedVectorType
>(VTy
)->getNumElements();
319 if (HasFn(C
->getAggregateElement(i
)))
326 bool Constant::containsUndefOrPoisonElement() const {
327 return containsUndefinedElement(
328 this, [&](const auto *C
) { return isa
<UndefValue
>(C
); });
331 bool Constant::containsPoisonElement() const {
332 return containsUndefinedElement(
333 this, [&](const auto *C
) { return isa
<PoisonValue
>(C
); });
336 bool Constant::containsConstantExpression() const {
337 if (auto *VTy
= dyn_cast
<FixedVectorType
>(getType())) {
338 for (unsigned i
= 0, e
= VTy
->getNumElements(); i
!= e
; ++i
)
339 if (isa
<ConstantExpr
>(getAggregateElement(i
)))
345 /// Constructor to create a '0' constant of arbitrary type.
346 Constant
*Constant::getNullValue(Type
*Ty
) {
347 switch (Ty
->getTypeID()) {
348 case Type::IntegerTyID
:
349 return ConstantInt::get(Ty
, 0);
351 return ConstantFP::get(Ty
->getContext(),
352 APFloat::getZero(APFloat::IEEEhalf()));
353 case Type::BFloatTyID
:
354 return ConstantFP::get(Ty
->getContext(),
355 APFloat::getZero(APFloat::BFloat()));
356 case Type::FloatTyID
:
357 return ConstantFP::get(Ty
->getContext(),
358 APFloat::getZero(APFloat::IEEEsingle()));
359 case Type::DoubleTyID
:
360 return ConstantFP::get(Ty
->getContext(),
361 APFloat::getZero(APFloat::IEEEdouble()));
362 case Type::X86_FP80TyID
:
363 return ConstantFP::get(Ty
->getContext(),
364 APFloat::getZero(APFloat::x87DoubleExtended()));
365 case Type::FP128TyID
:
366 return ConstantFP::get(Ty
->getContext(),
367 APFloat::getZero(APFloat::IEEEquad()));
368 case Type::PPC_FP128TyID
:
369 return ConstantFP::get(Ty
->getContext(),
370 APFloat(APFloat::PPCDoubleDouble(),
371 APInt::getNullValue(128)));
372 case Type::PointerTyID
:
373 return ConstantPointerNull::get(cast
<PointerType
>(Ty
));
374 case Type::StructTyID
:
375 case Type::ArrayTyID
:
376 case Type::FixedVectorTyID
:
377 case Type::ScalableVectorTyID
:
378 return ConstantAggregateZero::get(Ty
);
379 case Type::TokenTyID
:
380 return ConstantTokenNone::get(Ty
->getContext());
382 // Function, Label, or Opaque type?
383 llvm_unreachable("Cannot create a null constant of that type!");
387 Constant
*Constant::getIntegerValue(Type
*Ty
, const APInt
&V
) {
388 Type
*ScalarTy
= Ty
->getScalarType();
390 // Create the base integer constant.
391 Constant
*C
= ConstantInt::get(Ty
->getContext(), V
);
393 // Convert an integer to a pointer, if necessary.
394 if (PointerType
*PTy
= dyn_cast
<PointerType
>(ScalarTy
))
395 C
= ConstantExpr::getIntToPtr(C
, PTy
);
397 // Broadcast a scalar to a vector, if necessary.
398 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
399 C
= ConstantVector::getSplat(VTy
->getElementCount(), C
);
404 Constant
*Constant::getAllOnesValue(Type
*Ty
) {
405 if (IntegerType
*ITy
= dyn_cast
<IntegerType
>(Ty
))
406 return ConstantInt::get(Ty
->getContext(),
407 APInt::getAllOnesValue(ITy
->getBitWidth()));
409 if (Ty
->isFloatingPointTy()) {
410 APFloat FL
= APFloat::getAllOnesValue(Ty
->getFltSemantics(),
411 Ty
->getPrimitiveSizeInBits());
412 return ConstantFP::get(Ty
->getContext(), FL
);
415 VectorType
*VTy
= cast
<VectorType
>(Ty
);
416 return ConstantVector::getSplat(VTy
->getElementCount(),
417 getAllOnesValue(VTy
->getElementType()));
420 Constant
*Constant::getAggregateElement(unsigned Elt
) const {
421 assert((getType()->isAggregateType() || getType()->isVectorTy()) &&
422 "Must be an aggregate/vector constant");
424 if (const auto *CC
= dyn_cast
<ConstantAggregate
>(this))
425 return Elt
< CC
->getNumOperands() ? CC
->getOperand(Elt
) : nullptr;
427 if (const auto *CAZ
= dyn_cast
<ConstantAggregateZero
>(this))
428 return Elt
< CAZ
->getElementCount().getKnownMinValue()
429 ? CAZ
->getElementValue(Elt
)
432 // FIXME: getNumElements() will fail for non-fixed vector types.
433 if (isa
<ScalableVectorType
>(getType()))
436 if (const auto *PV
= dyn_cast
<PoisonValue
>(this))
437 return Elt
< PV
->getNumElements() ? PV
->getElementValue(Elt
) : nullptr;
439 if (const auto *UV
= dyn_cast
<UndefValue
>(this))
440 return Elt
< UV
->getNumElements() ? UV
->getElementValue(Elt
) : nullptr;
442 if (const auto *CDS
= dyn_cast
<ConstantDataSequential
>(this))
443 return Elt
< CDS
->getNumElements() ? CDS
->getElementAsConstant(Elt
)
449 Constant
*Constant::getAggregateElement(Constant
*Elt
) const {
450 assert(isa
<IntegerType
>(Elt
->getType()) && "Index must be an integer");
451 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(Elt
)) {
452 // Check if the constant fits into an uint64_t.
453 if (CI
->getValue().getActiveBits() > 64)
455 return getAggregateElement(CI
->getZExtValue());
460 void Constant::destroyConstant() {
461 /// First call destroyConstantImpl on the subclass. This gives the subclass
462 /// a chance to remove the constant from any maps/pools it's contained in.
463 switch (getValueID()) {
465 llvm_unreachable("Not a constant!");
466 #define HANDLE_CONSTANT(Name) \
467 case Value::Name##Val: \
468 cast<Name>(this)->destroyConstantImpl(); \
470 #include "llvm/IR/Value.def"
473 // When a Constant is destroyed, there may be lingering
474 // references to the constant by other constants in the constant pool. These
475 // constants are implicitly dependent on the module that is being deleted,
476 // but they don't know that. Because we only find out when the CPV is
477 // deleted, we must now notify all of our users (that should only be
478 // Constants) that they are, in fact, invalid now and should be deleted.
480 while (!use_empty()) {
481 Value
*V
= user_back();
482 #ifndef NDEBUG // Only in -g mode...
483 if (!isa
<Constant
>(V
)) {
484 dbgs() << "While deleting: " << *this
485 << "\n\nUse still stuck around after Def is destroyed: " << *V
489 assert(isa
<Constant
>(V
) && "References remain to Constant being destroyed");
490 cast
<Constant
>(V
)->destroyConstant();
492 // The constant should remove itself from our use list...
493 assert((use_empty() || user_back() != V
) && "Constant not removed!");
496 // Value has no outstanding references it is safe to delete it now...
497 deleteConstant(this);
500 void llvm::deleteConstant(Constant
*C
) {
501 switch (C
->getValueID()) {
502 case Constant::ConstantIntVal
:
503 delete static_cast<ConstantInt
*>(C
);
505 case Constant::ConstantFPVal
:
506 delete static_cast<ConstantFP
*>(C
);
508 case Constant::ConstantAggregateZeroVal
:
509 delete static_cast<ConstantAggregateZero
*>(C
);
511 case Constant::ConstantArrayVal
:
512 delete static_cast<ConstantArray
*>(C
);
514 case Constant::ConstantStructVal
:
515 delete static_cast<ConstantStruct
*>(C
);
517 case Constant::ConstantVectorVal
:
518 delete static_cast<ConstantVector
*>(C
);
520 case Constant::ConstantPointerNullVal
:
521 delete static_cast<ConstantPointerNull
*>(C
);
523 case Constant::ConstantDataArrayVal
:
524 delete static_cast<ConstantDataArray
*>(C
);
526 case Constant::ConstantDataVectorVal
:
527 delete static_cast<ConstantDataVector
*>(C
);
529 case Constant::ConstantTokenNoneVal
:
530 delete static_cast<ConstantTokenNone
*>(C
);
532 case Constant::BlockAddressVal
:
533 delete static_cast<BlockAddress
*>(C
);
535 case Constant::DSOLocalEquivalentVal
:
536 delete static_cast<DSOLocalEquivalent
*>(C
);
538 case Constant::UndefValueVal
:
539 delete static_cast<UndefValue
*>(C
);
541 case Constant::PoisonValueVal
:
542 delete static_cast<PoisonValue
*>(C
);
544 case Constant::ConstantExprVal
:
545 if (isa
<UnaryConstantExpr
>(C
))
546 delete static_cast<UnaryConstantExpr
*>(C
);
547 else if (isa
<BinaryConstantExpr
>(C
))
548 delete static_cast<BinaryConstantExpr
*>(C
);
549 else if (isa
<SelectConstantExpr
>(C
))
550 delete static_cast<SelectConstantExpr
*>(C
);
551 else if (isa
<ExtractElementConstantExpr
>(C
))
552 delete static_cast<ExtractElementConstantExpr
*>(C
);
553 else if (isa
<InsertElementConstantExpr
>(C
))
554 delete static_cast<InsertElementConstantExpr
*>(C
);
555 else if (isa
<ShuffleVectorConstantExpr
>(C
))
556 delete static_cast<ShuffleVectorConstantExpr
*>(C
);
557 else if (isa
<ExtractValueConstantExpr
>(C
))
558 delete static_cast<ExtractValueConstantExpr
*>(C
);
559 else if (isa
<InsertValueConstantExpr
>(C
))
560 delete static_cast<InsertValueConstantExpr
*>(C
);
561 else if (isa
<GetElementPtrConstantExpr
>(C
))
562 delete static_cast<GetElementPtrConstantExpr
*>(C
);
563 else if (isa
<CompareConstantExpr
>(C
))
564 delete static_cast<CompareConstantExpr
*>(C
);
566 llvm_unreachable("Unexpected constant expr");
569 llvm_unreachable("Unexpected constant");
573 static bool canTrapImpl(const Constant
*C
,
574 SmallPtrSetImpl
<const ConstantExpr
*> &NonTrappingOps
) {
575 assert(C
->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
576 // The only thing that could possibly trap are constant exprs.
577 const ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(C
);
581 // ConstantExpr traps if any operands can trap.
582 for (unsigned i
= 0, e
= C
->getNumOperands(); i
!= e
; ++i
) {
583 if (ConstantExpr
*Op
= dyn_cast
<ConstantExpr
>(CE
->getOperand(i
))) {
584 if (NonTrappingOps
.insert(Op
).second
&& canTrapImpl(Op
, NonTrappingOps
))
589 // Otherwise, only specific operations can trap.
590 switch (CE
->getOpcode()) {
593 case Instruction::UDiv
:
594 case Instruction::SDiv
:
595 case Instruction::URem
:
596 case Instruction::SRem
:
597 // Div and rem can trap if the RHS is not known to be non-zero.
598 if (!isa
<ConstantInt
>(CE
->getOperand(1)) ||CE
->getOperand(1)->isNullValue())
604 bool Constant::canTrap() const {
605 SmallPtrSet
<const ConstantExpr
*, 4> NonTrappingOps
;
606 return canTrapImpl(this, NonTrappingOps
);
609 /// Check if C contains a GlobalValue for which Predicate is true.
611 ConstHasGlobalValuePredicate(const Constant
*C
,
612 bool (*Predicate
)(const GlobalValue
*)) {
613 SmallPtrSet
<const Constant
*, 8> Visited
;
614 SmallVector
<const Constant
*, 8> WorkList
;
615 WorkList
.push_back(C
);
618 while (!WorkList
.empty()) {
619 const Constant
*WorkItem
= WorkList
.pop_back_val();
620 if (const auto *GV
= dyn_cast
<GlobalValue
>(WorkItem
))
623 for (const Value
*Op
: WorkItem
->operands()) {
624 const Constant
*ConstOp
= dyn_cast
<Constant
>(Op
);
627 if (Visited
.insert(ConstOp
).second
)
628 WorkList
.push_back(ConstOp
);
634 bool Constant::isThreadDependent() const {
635 auto DLLImportPredicate
= [](const GlobalValue
*GV
) {
636 return GV
->isThreadLocal();
638 return ConstHasGlobalValuePredicate(this, DLLImportPredicate
);
641 bool Constant::isDLLImportDependent() const {
642 auto DLLImportPredicate
= [](const GlobalValue
*GV
) {
643 return GV
->hasDLLImportStorageClass();
645 return ConstHasGlobalValuePredicate(this, DLLImportPredicate
);
648 bool Constant::isConstantUsed() const {
649 for (const User
*U
: users()) {
650 const Constant
*UC
= dyn_cast
<Constant
>(U
);
651 if (!UC
|| isa
<GlobalValue
>(UC
))
654 if (UC
->isConstantUsed())
660 bool Constant::needsDynamicRelocation() const {
661 return getRelocationInfo() == GlobalRelocation
;
664 bool Constant::needsRelocation() const {
665 return getRelocationInfo() != NoRelocation
;
668 Constant::PossibleRelocationsTy
Constant::getRelocationInfo() const {
669 if (isa
<GlobalValue
>(this))
670 return GlobalRelocation
; // Global reference.
672 if (const BlockAddress
*BA
= dyn_cast
<BlockAddress
>(this))
673 return BA
->getFunction()->getRelocationInfo();
675 if (const ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(this)) {
676 if (CE
->getOpcode() == Instruction::Sub
) {
677 ConstantExpr
*LHS
= dyn_cast
<ConstantExpr
>(CE
->getOperand(0));
678 ConstantExpr
*RHS
= dyn_cast
<ConstantExpr
>(CE
->getOperand(1));
679 if (LHS
&& RHS
&& LHS
->getOpcode() == Instruction::PtrToInt
&&
680 RHS
->getOpcode() == Instruction::PtrToInt
) {
681 Constant
*LHSOp0
= LHS
->getOperand(0);
682 Constant
*RHSOp0
= RHS
->getOperand(0);
684 // While raw uses of blockaddress need to be relocated, differences
685 // between two of them don't when they are for labels in the same
686 // function. This is a common idiom when creating a table for the
687 // indirect goto extension, so we handle it efficiently here.
688 if (isa
<BlockAddress
>(LHSOp0
) && isa
<BlockAddress
>(RHSOp0
) &&
689 cast
<BlockAddress
>(LHSOp0
)->getFunction() ==
690 cast
<BlockAddress
>(RHSOp0
)->getFunction())
693 // Relative pointers do not need to be dynamically relocated.
695 dyn_cast
<GlobalValue
>(RHSOp0
->stripInBoundsConstantOffsets())) {
696 auto *LHS
= LHSOp0
->stripInBoundsConstantOffsets();
697 if (auto *LHSGV
= dyn_cast
<GlobalValue
>(LHS
)) {
698 if (LHSGV
->isDSOLocal() && RHSGV
->isDSOLocal())
699 return LocalRelocation
;
700 } else if (isa
<DSOLocalEquivalent
>(LHS
)) {
701 if (RHSGV
->isDSOLocal())
702 return LocalRelocation
;
709 PossibleRelocationsTy Result
= NoRelocation
;
710 for (unsigned i
= 0, e
= getNumOperands(); i
!= e
; ++i
)
712 std::max(cast
<Constant
>(getOperand(i
))->getRelocationInfo(), Result
);
717 /// If the specified constantexpr is dead, remove it. This involves recursively
718 /// eliminating any dead users of the constantexpr.
719 static bool removeDeadUsersOfConstant(const Constant
*C
) {
720 if (isa
<GlobalValue
>(C
)) return false; // Cannot remove this
722 while (!C
->use_empty()) {
723 const Constant
*User
= dyn_cast
<Constant
>(C
->user_back());
724 if (!User
) return false; // Non-constant usage;
725 if (!removeDeadUsersOfConstant(User
))
726 return false; // Constant wasn't dead
729 // If C is only used by metadata, it should not be preserved but should have
730 // its uses replaced.
731 if (C
->isUsedByMetadata()) {
732 const_cast<Constant
*>(C
)->replaceAllUsesWith(
733 UndefValue::get(C
->getType()));
735 const_cast<Constant
*>(C
)->destroyConstant();
740 void Constant::removeDeadConstantUsers() const {
741 Value::const_user_iterator I
= user_begin(), E
= user_end();
742 Value::const_user_iterator LastNonDeadUser
= E
;
744 const Constant
*User
= dyn_cast
<Constant
>(*I
);
751 if (!removeDeadUsersOfConstant(User
)) {
752 // If the constant wasn't dead, remember that this was the last live use
753 // and move on to the next constant.
759 // If the constant was dead, then the iterator is invalidated.
760 if (LastNonDeadUser
== E
)
763 I
= std::next(LastNonDeadUser
);
767 Constant
*Constant::replaceUndefsWith(Constant
*C
, Constant
*Replacement
) {
768 assert(C
&& Replacement
&& "Expected non-nullptr constant arguments");
769 Type
*Ty
= C
->getType();
770 if (match(C
, m_Undef())) {
771 assert(Ty
== Replacement
->getType() && "Expected matching types");
775 // Don't know how to deal with this constant.
776 auto *VTy
= dyn_cast
<FixedVectorType
>(Ty
);
780 unsigned NumElts
= VTy
->getNumElements();
781 SmallVector
<Constant
*, 32> NewC(NumElts
);
782 for (unsigned i
= 0; i
!= NumElts
; ++i
) {
783 Constant
*EltC
= C
->getAggregateElement(i
);
784 assert((!EltC
|| EltC
->getType() == Replacement
->getType()) &&
785 "Expected matching types");
786 NewC
[i
] = EltC
&& match(EltC
, m_Undef()) ? Replacement
: EltC
;
788 return ConstantVector::get(NewC
);
791 Constant
*Constant::mergeUndefsWith(Constant
*C
, Constant
*Other
) {
792 assert(C
&& Other
&& "Expected non-nullptr constant arguments");
793 if (match(C
, m_Undef()))
796 Type
*Ty
= C
->getType();
797 if (match(Other
, m_Undef()))
798 return UndefValue::get(Ty
);
800 auto *VTy
= dyn_cast
<FixedVectorType
>(Ty
);
804 Type
*EltTy
= VTy
->getElementType();
805 unsigned NumElts
= VTy
->getNumElements();
806 assert(isa
<FixedVectorType
>(Other
->getType()) &&
807 cast
<FixedVectorType
>(Other
->getType())->getNumElements() == NumElts
&&
810 bool FoundExtraUndef
= false;
811 SmallVector
<Constant
*, 32> NewC(NumElts
);
812 for (unsigned I
= 0; I
!= NumElts
; ++I
) {
813 NewC
[I
] = C
->getAggregateElement(I
);
814 Constant
*OtherEltC
= Other
->getAggregateElement(I
);
815 assert(NewC
[I
] && OtherEltC
&& "Unknown vector element");
816 if (!match(NewC
[I
], m_Undef()) && match(OtherEltC
, m_Undef())) {
817 NewC
[I
] = UndefValue::get(EltTy
);
818 FoundExtraUndef
= true;
822 return ConstantVector::get(NewC
);
826 bool Constant::isManifestConstant() const {
827 if (isa
<ConstantData
>(this))
829 if (isa
<ConstantAggregate
>(this) || isa
<ConstantExpr
>(this)) {
830 for (const Value
*Op
: operand_values())
831 if (!cast
<Constant
>(Op
)->isManifestConstant())
838 //===----------------------------------------------------------------------===//
840 //===----------------------------------------------------------------------===//
842 ConstantInt::ConstantInt(IntegerType
*Ty
, const APInt
&V
)
843 : ConstantData(Ty
, ConstantIntVal
), Val(V
) {
844 assert(V
.getBitWidth() == Ty
->getBitWidth() && "Invalid constant for type");
847 ConstantInt
*ConstantInt::getTrue(LLVMContext
&Context
) {
848 LLVMContextImpl
*pImpl
= Context
.pImpl
;
849 if (!pImpl
->TheTrueVal
)
850 pImpl
->TheTrueVal
= ConstantInt::get(Type::getInt1Ty(Context
), 1);
851 return pImpl
->TheTrueVal
;
854 ConstantInt
*ConstantInt::getFalse(LLVMContext
&Context
) {
855 LLVMContextImpl
*pImpl
= Context
.pImpl
;
856 if (!pImpl
->TheFalseVal
)
857 pImpl
->TheFalseVal
= ConstantInt::get(Type::getInt1Ty(Context
), 0);
858 return pImpl
->TheFalseVal
;
861 ConstantInt
*ConstantInt::getBool(LLVMContext
&Context
, bool V
) {
862 return V
? getTrue(Context
) : getFalse(Context
);
865 Constant
*ConstantInt::getTrue(Type
*Ty
) {
866 assert(Ty
->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
867 ConstantInt
*TrueC
= ConstantInt::getTrue(Ty
->getContext());
868 if (auto *VTy
= dyn_cast
<VectorType
>(Ty
))
869 return ConstantVector::getSplat(VTy
->getElementCount(), TrueC
);
873 Constant
*ConstantInt::getFalse(Type
*Ty
) {
874 assert(Ty
->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
875 ConstantInt
*FalseC
= ConstantInt::getFalse(Ty
->getContext());
876 if (auto *VTy
= dyn_cast
<VectorType
>(Ty
))
877 return ConstantVector::getSplat(VTy
->getElementCount(), FalseC
);
881 Constant
*ConstantInt::getBool(Type
*Ty
, bool V
) {
882 return V
? getTrue(Ty
) : getFalse(Ty
);
885 // Get a ConstantInt from an APInt.
886 ConstantInt
*ConstantInt::get(LLVMContext
&Context
, const APInt
&V
) {
887 // get an existing value or the insertion position
888 LLVMContextImpl
*pImpl
= Context
.pImpl
;
889 std::unique_ptr
<ConstantInt
> &Slot
= pImpl
->IntConstants
[V
];
891 // Get the corresponding integer type for the bit width of the value.
892 IntegerType
*ITy
= IntegerType::get(Context
, V
.getBitWidth());
893 Slot
.reset(new ConstantInt(ITy
, V
));
895 assert(Slot
->getType() == IntegerType::get(Context
, V
.getBitWidth()));
899 Constant
*ConstantInt::get(Type
*Ty
, uint64_t V
, bool isSigned
) {
900 Constant
*C
= get(cast
<IntegerType
>(Ty
->getScalarType()), V
, isSigned
);
902 // For vectors, broadcast the value.
903 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
904 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
909 ConstantInt
*ConstantInt::get(IntegerType
*Ty
, uint64_t V
, bool isSigned
) {
910 return get(Ty
->getContext(), APInt(Ty
->getBitWidth(), V
, isSigned
));
913 ConstantInt
*ConstantInt::getSigned(IntegerType
*Ty
, int64_t V
) {
914 return get(Ty
, V
, true);
917 Constant
*ConstantInt::getSigned(Type
*Ty
, int64_t V
) {
918 return get(Ty
, V
, true);
921 Constant
*ConstantInt::get(Type
*Ty
, const APInt
& V
) {
922 ConstantInt
*C
= get(Ty
->getContext(), V
);
923 assert(C
->getType() == Ty
->getScalarType() &&
924 "ConstantInt type doesn't match the type implied by its value!");
926 // For vectors, broadcast the value.
927 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
928 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
933 ConstantInt
*ConstantInt::get(IntegerType
* Ty
, StringRef Str
, uint8_t radix
) {
934 return get(Ty
->getContext(), APInt(Ty
->getBitWidth(), Str
, radix
));
937 /// Remove the constant from the constant table.
938 void ConstantInt::destroyConstantImpl() {
939 llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
942 //===----------------------------------------------------------------------===//
944 //===----------------------------------------------------------------------===//
946 Constant
*ConstantFP::get(Type
*Ty
, double V
) {
947 LLVMContext
&Context
= Ty
->getContext();
951 FV
.convert(Ty
->getScalarType()->getFltSemantics(),
952 APFloat::rmNearestTiesToEven
, &ignored
);
953 Constant
*C
= get(Context
, FV
);
955 // For vectors, broadcast the value.
956 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
957 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
962 Constant
*ConstantFP::get(Type
*Ty
, const APFloat
&V
) {
963 ConstantFP
*C
= get(Ty
->getContext(), V
);
964 assert(C
->getType() == Ty
->getScalarType() &&
965 "ConstantFP type doesn't match the type implied by its value!");
967 // For vectors, broadcast the value.
968 if (auto *VTy
= dyn_cast
<VectorType
>(Ty
))
969 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
974 Constant
*ConstantFP::get(Type
*Ty
, StringRef Str
) {
975 LLVMContext
&Context
= Ty
->getContext();
977 APFloat
FV(Ty
->getScalarType()->getFltSemantics(), Str
);
978 Constant
*C
= get(Context
, FV
);
980 // For vectors, broadcast the value.
981 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
982 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
987 Constant
*ConstantFP::getNaN(Type
*Ty
, bool Negative
, uint64_t Payload
) {
988 const fltSemantics
&Semantics
= Ty
->getScalarType()->getFltSemantics();
989 APFloat NaN
= APFloat::getNaN(Semantics
, Negative
, Payload
);
990 Constant
*C
= get(Ty
->getContext(), NaN
);
992 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
993 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
998 Constant
*ConstantFP::getQNaN(Type
*Ty
, bool Negative
, APInt
*Payload
) {
999 const fltSemantics
&Semantics
= Ty
->getScalarType()->getFltSemantics();
1000 APFloat NaN
= APFloat::getQNaN(Semantics
, Negative
, Payload
);
1001 Constant
*C
= get(Ty
->getContext(), NaN
);
1003 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
1004 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
1009 Constant
*ConstantFP::getSNaN(Type
*Ty
, bool Negative
, APInt
*Payload
) {
1010 const fltSemantics
&Semantics
= Ty
->getScalarType()->getFltSemantics();
1011 APFloat NaN
= APFloat::getSNaN(Semantics
, Negative
, Payload
);
1012 Constant
*C
= get(Ty
->getContext(), NaN
);
1014 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
1015 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
1020 Constant
*ConstantFP::getNegativeZero(Type
*Ty
) {
1021 const fltSemantics
&Semantics
= Ty
->getScalarType()->getFltSemantics();
1022 APFloat NegZero
= APFloat::getZero(Semantics
, /*Negative=*/true);
1023 Constant
*C
= get(Ty
->getContext(), NegZero
);
1025 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
1026 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
1032 Constant
*ConstantFP::getZeroValueForNegation(Type
*Ty
) {
1033 if (Ty
->isFPOrFPVectorTy())
1034 return getNegativeZero(Ty
);
1036 return Constant::getNullValue(Ty
);
1040 // ConstantFP accessors.
1041 ConstantFP
* ConstantFP::get(LLVMContext
&Context
, const APFloat
& V
) {
1042 LLVMContextImpl
* pImpl
= Context
.pImpl
;
1044 std::unique_ptr
<ConstantFP
> &Slot
= pImpl
->FPConstants
[V
];
1047 Type
*Ty
= Type::getFloatingPointTy(Context
, V
.getSemantics());
1048 Slot
.reset(new ConstantFP(Ty
, V
));
1054 Constant
*ConstantFP::getInfinity(Type
*Ty
, bool Negative
) {
1055 const fltSemantics
&Semantics
= Ty
->getScalarType()->getFltSemantics();
1056 Constant
*C
= get(Ty
->getContext(), APFloat::getInf(Semantics
, Negative
));
1058 if (VectorType
*VTy
= dyn_cast
<VectorType
>(Ty
))
1059 return ConstantVector::getSplat(VTy
->getElementCount(), C
);
1064 ConstantFP::ConstantFP(Type
*Ty
, const APFloat
&V
)
1065 : ConstantData(Ty
, ConstantFPVal
), Val(V
) {
1066 assert(&V
.getSemantics() == &Ty
->getFltSemantics() &&
1067 "FP type Mismatch");
1070 bool ConstantFP::isExactlyValue(const APFloat
&V
) const {
1071 return Val
.bitwiseIsEqual(V
);
1074 /// Remove the constant from the constant table.
1075 void ConstantFP::destroyConstantImpl() {
1076 llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
1079 //===----------------------------------------------------------------------===//
1080 // ConstantAggregateZero Implementation
1081 //===----------------------------------------------------------------------===//
1083 Constant
*ConstantAggregateZero::getSequentialElement() const {
1084 if (auto *AT
= dyn_cast
<ArrayType
>(getType()))
1085 return Constant::getNullValue(AT
->getElementType());
1086 return Constant::getNullValue(cast
<VectorType
>(getType())->getElementType());
1089 Constant
*ConstantAggregateZero::getStructElement(unsigned Elt
) const {
1090 return Constant::getNullValue(getType()->getStructElementType(Elt
));
1093 Constant
*ConstantAggregateZero::getElementValue(Constant
*C
) const {
1094 if (isa
<ArrayType
>(getType()) || isa
<VectorType
>(getType()))
1095 return getSequentialElement();
1096 return getStructElement(cast
<ConstantInt
>(C
)->getZExtValue());
1099 Constant
*ConstantAggregateZero::getElementValue(unsigned Idx
) const {
1100 if (isa
<ArrayType
>(getType()) || isa
<VectorType
>(getType()))
1101 return getSequentialElement();
1102 return getStructElement(Idx
);
1105 ElementCount
ConstantAggregateZero::getElementCount() const {
1106 Type
*Ty
= getType();
1107 if (auto *AT
= dyn_cast
<ArrayType
>(Ty
))
1108 return ElementCount::getFixed(AT
->getNumElements());
1109 if (auto *VT
= dyn_cast
<VectorType
>(Ty
))
1110 return VT
->getElementCount();
1111 return ElementCount::getFixed(Ty
->getStructNumElements());
1114 //===----------------------------------------------------------------------===//
1115 // UndefValue Implementation
1116 //===----------------------------------------------------------------------===//
1118 UndefValue
*UndefValue::getSequentialElement() const {
1119 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(getType()))
1120 return UndefValue::get(ATy
->getElementType());
1121 return UndefValue::get(cast
<VectorType
>(getType())->getElementType());
1124 UndefValue
*UndefValue::getStructElement(unsigned Elt
) const {
1125 return UndefValue::get(getType()->getStructElementType(Elt
));
1128 UndefValue
*UndefValue::getElementValue(Constant
*C
) const {
1129 if (isa
<ArrayType
>(getType()) || isa
<VectorType
>(getType()))
1130 return getSequentialElement();
1131 return getStructElement(cast
<ConstantInt
>(C
)->getZExtValue());
1134 UndefValue
*UndefValue::getElementValue(unsigned Idx
) const {
1135 if (isa
<ArrayType
>(getType()) || isa
<VectorType
>(getType()))
1136 return getSequentialElement();
1137 return getStructElement(Idx
);
1140 unsigned UndefValue::getNumElements() const {
1141 Type
*Ty
= getType();
1142 if (auto *AT
= dyn_cast
<ArrayType
>(Ty
))
1143 return AT
->getNumElements();
1144 if (auto *VT
= dyn_cast
<VectorType
>(Ty
))
1145 return cast
<FixedVectorType
>(VT
)->getNumElements();
1146 return Ty
->getStructNumElements();
1149 //===----------------------------------------------------------------------===//
1150 // PoisonValue Implementation
1151 //===----------------------------------------------------------------------===//
1153 PoisonValue
*PoisonValue::getSequentialElement() const {
1154 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(getType()))
1155 return PoisonValue::get(ATy
->getElementType());
1156 return PoisonValue::get(cast
<VectorType
>(getType())->getElementType());
1159 PoisonValue
*PoisonValue::getStructElement(unsigned Elt
) const {
1160 return PoisonValue::get(getType()->getStructElementType(Elt
));
1163 PoisonValue
*PoisonValue::getElementValue(Constant
*C
) const {
1164 if (isa
<ArrayType
>(getType()) || isa
<VectorType
>(getType()))
1165 return getSequentialElement();
1166 return getStructElement(cast
<ConstantInt
>(C
)->getZExtValue());
1169 PoisonValue
*PoisonValue::getElementValue(unsigned Idx
) const {
1170 if (isa
<ArrayType
>(getType()) || isa
<VectorType
>(getType()))
1171 return getSequentialElement();
1172 return getStructElement(Idx
);
1175 //===----------------------------------------------------------------------===//
1176 // ConstantXXX Classes
1177 //===----------------------------------------------------------------------===//
1179 template <typename ItTy
, typename EltTy
>
1180 static bool rangeOnlyContains(ItTy Start
, ItTy End
, EltTy Elt
) {
1181 for (; Start
!= End
; ++Start
)
1187 template <typename SequentialTy
, typename ElementTy
>
1188 static Constant
*getIntSequenceIfElementsMatch(ArrayRef
<Constant
*> V
) {
1189 assert(!V
.empty() && "Cannot get empty int sequence.");
1191 SmallVector
<ElementTy
, 16> Elts
;
1192 for (Constant
*C
: V
)
1193 if (auto *CI
= dyn_cast
<ConstantInt
>(C
))
1194 Elts
.push_back(CI
->getZExtValue());
1197 return SequentialTy::get(V
[0]->getContext(), Elts
);
1200 template <typename SequentialTy
, typename ElementTy
>
1201 static Constant
*getFPSequenceIfElementsMatch(ArrayRef
<Constant
*> V
) {
1202 assert(!V
.empty() && "Cannot get empty FP sequence.");
1204 SmallVector
<ElementTy
, 16> Elts
;
1205 for (Constant
*C
: V
)
1206 if (auto *CFP
= dyn_cast
<ConstantFP
>(C
))
1207 Elts
.push_back(CFP
->getValueAPF().bitcastToAPInt().getLimitedValue());
1210 return SequentialTy::getFP(V
[0]->getType(), Elts
);
1213 template <typename SequenceTy
>
1214 static Constant
*getSequenceIfElementsMatch(Constant
*C
,
1215 ArrayRef
<Constant
*> V
) {
1216 // We speculatively build the elements here even if it turns out that there is
1217 // a constantexpr or something else weird, since it is so uncommon for that to
1219 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(C
)) {
1220 if (CI
->getType()->isIntegerTy(8))
1221 return getIntSequenceIfElementsMatch
<SequenceTy
, uint8_t>(V
);
1222 else if (CI
->getType()->isIntegerTy(16))
1223 return getIntSequenceIfElementsMatch
<SequenceTy
, uint16_t>(V
);
1224 else if (CI
->getType()->isIntegerTy(32))
1225 return getIntSequenceIfElementsMatch
<SequenceTy
, uint32_t>(V
);
1226 else if (CI
->getType()->isIntegerTy(64))
1227 return getIntSequenceIfElementsMatch
<SequenceTy
, uint64_t>(V
);
1228 } else if (ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(C
)) {
1229 if (CFP
->getType()->isHalfTy() || CFP
->getType()->isBFloatTy())
1230 return getFPSequenceIfElementsMatch
<SequenceTy
, uint16_t>(V
);
1231 else if (CFP
->getType()->isFloatTy())
1232 return getFPSequenceIfElementsMatch
<SequenceTy
, uint32_t>(V
);
1233 else if (CFP
->getType()->isDoubleTy())
1234 return getFPSequenceIfElementsMatch
<SequenceTy
, uint64_t>(V
);
1240 ConstantAggregate::ConstantAggregate(Type
*T
, ValueTy VT
,
1241 ArrayRef
<Constant
*> V
)
1242 : Constant(T
, VT
, OperandTraits
<ConstantAggregate
>::op_end(this) - V
.size(),
1244 llvm::copy(V
, op_begin());
1246 // Check that types match, unless this is an opaque struct.
1247 if (auto *ST
= dyn_cast
<StructType
>(T
)) {
1250 for (unsigned I
= 0, E
= V
.size(); I
!= E
; ++I
)
1251 assert(V
[I
]->getType() == ST
->getTypeAtIndex(I
) &&
1252 "Initializer for struct element doesn't match!");
1256 ConstantArray::ConstantArray(ArrayType
*T
, ArrayRef
<Constant
*> V
)
1257 : ConstantAggregate(T
, ConstantArrayVal
, V
) {
1258 assert(V
.size() == T
->getNumElements() &&
1259 "Invalid initializer for constant array");
1262 Constant
*ConstantArray::get(ArrayType
*Ty
, ArrayRef
<Constant
*> V
) {
1263 if (Constant
*C
= getImpl(Ty
, V
))
1265 return Ty
->getContext().pImpl
->ArrayConstants
.getOrCreate(Ty
, V
);
1268 Constant
*ConstantArray::getImpl(ArrayType
*Ty
, ArrayRef
<Constant
*> V
) {
1269 // Empty arrays are canonicalized to ConstantAggregateZero.
1271 return ConstantAggregateZero::get(Ty
);
1273 for (unsigned i
= 0, e
= V
.size(); i
!= e
; ++i
) {
1274 assert(V
[i
]->getType() == Ty
->getElementType() &&
1275 "Wrong type in array element initializer");
1278 // If this is an all-zero array, return a ConstantAggregateZero object. If
1279 // all undef, return an UndefValue, if "all simple", then return a
1280 // ConstantDataArray.
1282 if (isa
<PoisonValue
>(C
) && rangeOnlyContains(V
.begin(), V
.end(), C
))
1283 return PoisonValue::get(Ty
);
1285 if (isa
<UndefValue
>(C
) && rangeOnlyContains(V
.begin(), V
.end(), C
))
1286 return UndefValue::get(Ty
);
1288 if (C
->isNullValue() && rangeOnlyContains(V
.begin(), V
.end(), C
))
1289 return ConstantAggregateZero::get(Ty
);
1291 // Check to see if all of the elements are ConstantFP or ConstantInt and if
1292 // the element type is compatible with ConstantDataVector. If so, use it.
1293 if (ConstantDataSequential::isElementTypeCompatible(C
->getType()))
1294 return getSequenceIfElementsMatch
<ConstantDataArray
>(C
, V
);
1296 // Otherwise, we really do want to create a ConstantArray.
1300 StructType
*ConstantStruct::getTypeForElements(LLVMContext
&Context
,
1301 ArrayRef
<Constant
*> V
,
1303 unsigned VecSize
= V
.size();
1304 SmallVector
<Type
*, 16> EltTypes(VecSize
);
1305 for (unsigned i
= 0; i
!= VecSize
; ++i
)
1306 EltTypes
[i
] = V
[i
]->getType();
1308 return StructType::get(Context
, EltTypes
, Packed
);
1312 StructType
*ConstantStruct::getTypeForElements(ArrayRef
<Constant
*> V
,
1314 assert(!V
.empty() &&
1315 "ConstantStruct::getTypeForElements cannot be called on empty list");
1316 return getTypeForElements(V
[0]->getContext(), V
, Packed
);
1319 ConstantStruct::ConstantStruct(StructType
*T
, ArrayRef
<Constant
*> V
)
1320 : ConstantAggregate(T
, ConstantStructVal
, V
) {
1321 assert((T
->isOpaque() || V
.size() == T
->getNumElements()) &&
1322 "Invalid initializer for constant struct");
1325 // ConstantStruct accessors.
1326 Constant
*ConstantStruct::get(StructType
*ST
, ArrayRef
<Constant
*> V
) {
1327 assert((ST
->isOpaque() || ST
->getNumElements() == V
.size()) &&
1328 "Incorrect # elements specified to ConstantStruct::get");
1330 // Create a ConstantAggregateZero value if all elements are zeros.
1332 bool isUndef
= false;
1333 bool isPoison
= false;
1336 isUndef
= isa
<UndefValue
>(V
[0]);
1337 isPoison
= isa
<PoisonValue
>(V
[0]);
1338 isZero
= V
[0]->isNullValue();
1339 // PoisonValue inherits UndefValue, so its check is not necessary.
1340 if (isUndef
|| isZero
) {
1341 for (unsigned i
= 0, e
= V
.size(); i
!= e
; ++i
) {
1342 if (!V
[i
]->isNullValue())
1344 if (!isa
<PoisonValue
>(V
[i
]))
1346 if (isa
<PoisonValue
>(V
[i
]) || !isa
<UndefValue
>(V
[i
]))
1352 return ConstantAggregateZero::get(ST
);
1354 return PoisonValue::get(ST
);
1356 return UndefValue::get(ST
);
1358 return ST
->getContext().pImpl
->StructConstants
.getOrCreate(ST
, V
);
1361 ConstantVector::ConstantVector(VectorType
*T
, ArrayRef
<Constant
*> V
)
1362 : ConstantAggregate(T
, ConstantVectorVal
, V
) {
1363 assert(V
.size() == cast
<FixedVectorType
>(T
)->getNumElements() &&
1364 "Invalid initializer for constant vector");
1367 // ConstantVector accessors.
1368 Constant
*ConstantVector::get(ArrayRef
<Constant
*> V
) {
1369 if (Constant
*C
= getImpl(V
))
1371 auto *Ty
= FixedVectorType::get(V
.front()->getType(), V
.size());
1372 return Ty
->getContext().pImpl
->VectorConstants
.getOrCreate(Ty
, V
);
1375 Constant
*ConstantVector::getImpl(ArrayRef
<Constant
*> V
) {
1376 assert(!V
.empty() && "Vectors can't be empty");
1377 auto *T
= FixedVectorType::get(V
.front()->getType(), V
.size());
1379 // If this is an all-undef or all-zero vector, return a
1380 // ConstantAggregateZero or UndefValue.
1382 bool isZero
= C
->isNullValue();
1383 bool isUndef
= isa
<UndefValue
>(C
);
1384 bool isPoison
= isa
<PoisonValue
>(C
);
1386 if (isZero
|| isUndef
) {
1387 for (unsigned i
= 1, e
= V
.size(); i
!= e
; ++i
)
1389 isZero
= isUndef
= isPoison
= false;
1395 return ConstantAggregateZero::get(T
);
1397 return PoisonValue::get(T
);
1399 return UndefValue::get(T
);
1401 // Check to see if all of the elements are ConstantFP or ConstantInt and if
1402 // the element type is compatible with ConstantDataVector. If so, use it.
1403 if (ConstantDataSequential::isElementTypeCompatible(C
->getType()))
1404 return getSequenceIfElementsMatch
<ConstantDataVector
>(C
, V
);
1406 // Otherwise, the element type isn't compatible with ConstantDataVector, or
1407 // the operand list contains a ConstantExpr or something else strange.
1411 Constant
*ConstantVector::getSplat(ElementCount EC
, Constant
*V
) {
1412 if (!EC
.isScalable()) {
1413 // If this splat is compatible with ConstantDataVector, use it instead of
1415 if ((isa
<ConstantFP
>(V
) || isa
<ConstantInt
>(V
)) &&
1416 ConstantDataSequential::isElementTypeCompatible(V
->getType()))
1417 return ConstantDataVector::getSplat(EC
.getKnownMinValue(), V
);
1419 SmallVector
<Constant
*, 32> Elts(EC
.getKnownMinValue(), V
);
1423 Type
*VTy
= VectorType::get(V
->getType(), EC
);
1425 if (V
->isNullValue())
1426 return ConstantAggregateZero::get(VTy
);
1427 else if (isa
<UndefValue
>(V
))
1428 return UndefValue::get(VTy
);
1430 Type
*I32Ty
= Type::getInt32Ty(VTy
->getContext());
1432 // Move scalar into vector.
1433 Constant
*PoisonV
= PoisonValue::get(VTy
);
1434 V
= ConstantExpr::getInsertElement(PoisonV
, V
, ConstantInt::get(I32Ty
, 0));
1435 // Build shuffle mask to perform the splat.
1436 SmallVector
<int, 8> Zeros(EC
.getKnownMinValue(), 0);
1438 return ConstantExpr::getShuffleVector(V
, PoisonV
, Zeros
);
1441 ConstantTokenNone
*ConstantTokenNone::get(LLVMContext
&Context
) {
1442 LLVMContextImpl
*pImpl
= Context
.pImpl
;
1443 if (!pImpl
->TheNoneToken
)
1444 pImpl
->TheNoneToken
.reset(new ConstantTokenNone(Context
));
1445 return pImpl
->TheNoneToken
.get();
1448 /// Remove the constant from the constant table.
1449 void ConstantTokenNone::destroyConstantImpl() {
1450 llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
1453 // Utility function for determining if a ConstantExpr is a CastOp or not. This
1454 // can't be inline because we don't want to #include Instruction.h into
1456 bool ConstantExpr::isCast() const {
1457 return Instruction::isCast(getOpcode());
1460 bool ConstantExpr::isCompare() const {
1461 return getOpcode() == Instruction::ICmp
|| getOpcode() == Instruction::FCmp
;
1464 bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
1465 if (getOpcode() != Instruction::GetElementPtr
) return false;
1467 gep_type_iterator GEPI
= gep_type_begin(this), E
= gep_type_end(this);
1468 User::const_op_iterator OI
= std::next(this->op_begin());
1470 // The remaining indices may be compile-time known integers within the bounds
1471 // of the corresponding notional static array types.
1472 for (; GEPI
!= E
; ++GEPI
, ++OI
) {
1473 if (isa
<UndefValue
>(*OI
))
1475 auto *CI
= dyn_cast
<ConstantInt
>(*OI
);
1476 if (!CI
|| (GEPI
.isBoundedSequential() &&
1477 (CI
->getValue().getActiveBits() > 64 ||
1478 CI
->getZExtValue() >= GEPI
.getSequentialNumElements())))
1482 // All the indices checked out.
1486 bool ConstantExpr::hasIndices() const {
1487 return getOpcode() == Instruction::ExtractValue
||
1488 getOpcode() == Instruction::InsertValue
;
1491 ArrayRef
<unsigned> ConstantExpr::getIndices() const {
1492 if (const ExtractValueConstantExpr
*EVCE
=
1493 dyn_cast
<ExtractValueConstantExpr
>(this))
1494 return EVCE
->Indices
;
1496 return cast
<InsertValueConstantExpr
>(this)->Indices
;
1499 unsigned ConstantExpr::getPredicate() const {
1500 return cast
<CompareConstantExpr
>(this)->predicate
;
1503 ArrayRef
<int> ConstantExpr::getShuffleMask() const {
1504 return cast
<ShuffleVectorConstantExpr
>(this)->ShuffleMask
;
1507 Constant
*ConstantExpr::getShuffleMaskForBitcode() const {
1508 return cast
<ShuffleVectorConstantExpr
>(this)->ShuffleMaskForBitcode
;
1512 ConstantExpr::getWithOperandReplaced(unsigned OpNo
, Constant
*Op
) const {
1513 assert(Op
->getType() == getOperand(OpNo
)->getType() &&
1514 "Replacing operand with value of different type!");
1515 if (getOperand(OpNo
) == Op
)
1516 return const_cast<ConstantExpr
*>(this);
1518 SmallVector
<Constant
*, 8> NewOps
;
1519 for (unsigned i
= 0, e
= getNumOperands(); i
!= e
; ++i
)
1520 NewOps
.push_back(i
== OpNo
? Op
: getOperand(i
));
1522 return getWithOperands(NewOps
);
1525 Constant
*ConstantExpr::getWithOperands(ArrayRef
<Constant
*> Ops
, Type
*Ty
,
1526 bool OnlyIfReduced
, Type
*SrcTy
) const {
1527 assert(Ops
.size() == getNumOperands() && "Operand count mismatch!");
1529 // If no operands changed return self.
1530 if (Ty
== getType() && std::equal(Ops
.begin(), Ops
.end(), op_begin()))
1531 return const_cast<ConstantExpr
*>(this);
1533 Type
*OnlyIfReducedTy
= OnlyIfReduced
? Ty
: nullptr;
1534 switch (getOpcode()) {
1535 case Instruction::Trunc
:
1536 case Instruction::ZExt
:
1537 case Instruction::SExt
:
1538 case Instruction::FPTrunc
:
1539 case Instruction::FPExt
:
1540 case Instruction::UIToFP
:
1541 case Instruction::SIToFP
:
1542 case Instruction::FPToUI
:
1543 case Instruction::FPToSI
:
1544 case Instruction::PtrToInt
:
1545 case Instruction::IntToPtr
:
1546 case Instruction::BitCast
:
1547 case Instruction::AddrSpaceCast
:
1548 return ConstantExpr::getCast(getOpcode(), Ops
[0], Ty
, OnlyIfReduced
);
1549 case Instruction::Select
:
1550 return ConstantExpr::getSelect(Ops
[0], Ops
[1], Ops
[2], OnlyIfReducedTy
);
1551 case Instruction::InsertElement
:
1552 return ConstantExpr::getInsertElement(Ops
[0], Ops
[1], Ops
[2],
1554 case Instruction::ExtractElement
:
1555 return ConstantExpr::getExtractElement(Ops
[0], Ops
[1], OnlyIfReducedTy
);
1556 case Instruction::InsertValue
:
1557 return ConstantExpr::getInsertValue(Ops
[0], Ops
[1], getIndices(),
1559 case Instruction::ExtractValue
:
1560 return ConstantExpr::getExtractValue(Ops
[0], getIndices(), OnlyIfReducedTy
);
1561 case Instruction::FNeg
:
1562 return ConstantExpr::getFNeg(Ops
[0]);
1563 case Instruction::ShuffleVector
:
1564 return ConstantExpr::getShuffleVector(Ops
[0], Ops
[1], getShuffleMask(),
1566 case Instruction::GetElementPtr
: {
1567 auto *GEPO
= cast
<GEPOperator
>(this);
1568 assert(SrcTy
|| (Ops
[0]->getType() == getOperand(0)->getType()));
1569 return ConstantExpr::getGetElementPtr(
1570 SrcTy
? SrcTy
: GEPO
->getSourceElementType(), Ops
[0], Ops
.slice(1),
1571 GEPO
->isInBounds(), GEPO
->getInRangeIndex(), OnlyIfReducedTy
);
1573 case Instruction::ICmp
:
1574 case Instruction::FCmp
:
1575 return ConstantExpr::getCompare(getPredicate(), Ops
[0], Ops
[1],
1578 assert(getNumOperands() == 2 && "Must be binary operator?");
1579 return ConstantExpr::get(getOpcode(), Ops
[0], Ops
[1], SubclassOptionalData
,
1585 //===----------------------------------------------------------------------===//
1586 // isValueValidForType implementations
1588 bool ConstantInt::isValueValidForType(Type
*Ty
, uint64_t Val
) {
1589 unsigned NumBits
= Ty
->getIntegerBitWidth(); // assert okay
1590 if (Ty
->isIntegerTy(1))
1591 return Val
== 0 || Val
== 1;
1592 return isUIntN(NumBits
, Val
);
1595 bool ConstantInt::isValueValidForType(Type
*Ty
, int64_t Val
) {
1596 unsigned NumBits
= Ty
->getIntegerBitWidth();
1597 if (Ty
->isIntegerTy(1))
1598 return Val
== 0 || Val
== 1 || Val
== -1;
1599 return isIntN(NumBits
, Val
);
1602 bool ConstantFP::isValueValidForType(Type
*Ty
, const APFloat
& Val
) {
1603 // convert modifies in place, so make a copy.
1604 APFloat Val2
= APFloat(Val
);
1606 switch (Ty
->getTypeID()) {
1608 return false; // These can't be represented as floating point!
1610 // FIXME rounding mode needs to be more flexible
1611 case Type::HalfTyID
: {
1612 if (&Val2
.getSemantics() == &APFloat::IEEEhalf())
1614 Val2
.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven
, &losesInfo
);
1617 case Type::BFloatTyID
: {
1618 if (&Val2
.getSemantics() == &APFloat::BFloat())
1620 Val2
.convert(APFloat::BFloat(), APFloat::rmNearestTiesToEven
, &losesInfo
);
1623 case Type::FloatTyID
: {
1624 if (&Val2
.getSemantics() == &APFloat::IEEEsingle())
1626 Val2
.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven
, &losesInfo
);
1629 case Type::DoubleTyID
: {
1630 if (&Val2
.getSemantics() == &APFloat::IEEEhalf() ||
1631 &Val2
.getSemantics() == &APFloat::BFloat() ||
1632 &Val2
.getSemantics() == &APFloat::IEEEsingle() ||
1633 &Val2
.getSemantics() == &APFloat::IEEEdouble())
1635 Val2
.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven
, &losesInfo
);
1638 case Type::X86_FP80TyID
:
1639 return &Val2
.getSemantics() == &APFloat::IEEEhalf() ||
1640 &Val2
.getSemantics() == &APFloat::BFloat() ||
1641 &Val2
.getSemantics() == &APFloat::IEEEsingle() ||
1642 &Val2
.getSemantics() == &APFloat::IEEEdouble() ||
1643 &Val2
.getSemantics() == &APFloat::x87DoubleExtended();
1644 case Type::FP128TyID
:
1645 return &Val2
.getSemantics() == &APFloat::IEEEhalf() ||
1646 &Val2
.getSemantics() == &APFloat::BFloat() ||
1647 &Val2
.getSemantics() == &APFloat::IEEEsingle() ||
1648 &Val2
.getSemantics() == &APFloat::IEEEdouble() ||
1649 &Val2
.getSemantics() == &APFloat::IEEEquad();
1650 case Type::PPC_FP128TyID
:
1651 return &Val2
.getSemantics() == &APFloat::IEEEhalf() ||
1652 &Val2
.getSemantics() == &APFloat::BFloat() ||
1653 &Val2
.getSemantics() == &APFloat::IEEEsingle() ||
1654 &Val2
.getSemantics() == &APFloat::IEEEdouble() ||
1655 &Val2
.getSemantics() == &APFloat::PPCDoubleDouble();
1660 //===----------------------------------------------------------------------===//
1661 // Factory Function Implementation
1663 ConstantAggregateZero
*ConstantAggregateZero::get(Type
*Ty
) {
1664 assert((Ty
->isStructTy() || Ty
->isArrayTy() || Ty
->isVectorTy()) &&
1665 "Cannot create an aggregate zero of non-aggregate type!");
1667 std::unique_ptr
<ConstantAggregateZero
> &Entry
=
1668 Ty
->getContext().pImpl
->CAZConstants
[Ty
];
1670 Entry
.reset(new ConstantAggregateZero(Ty
));
1675 /// Remove the constant from the constant table.
1676 void ConstantAggregateZero::destroyConstantImpl() {
1677 getContext().pImpl
->CAZConstants
.erase(getType());
1680 /// Remove the constant from the constant table.
1681 void ConstantArray::destroyConstantImpl() {
1682 getType()->getContext().pImpl
->ArrayConstants
.remove(this);
1686 //---- ConstantStruct::get() implementation...
1689 /// Remove the constant from the constant table.
1690 void ConstantStruct::destroyConstantImpl() {
1691 getType()->getContext().pImpl
->StructConstants
.remove(this);
1694 /// Remove the constant from the constant table.
1695 void ConstantVector::destroyConstantImpl() {
1696 getType()->getContext().pImpl
->VectorConstants
.remove(this);
1699 Constant
*Constant::getSplatValue(bool AllowUndefs
) const {
1700 assert(this->getType()->isVectorTy() && "Only valid for vectors!");
1701 if (isa
<ConstantAggregateZero
>(this))
1702 return getNullValue(cast
<VectorType
>(getType())->getElementType());
1703 if (const ConstantDataVector
*CV
= dyn_cast
<ConstantDataVector
>(this))
1704 return CV
->getSplatValue();
1705 if (const ConstantVector
*CV
= dyn_cast
<ConstantVector
>(this))
1706 return CV
->getSplatValue(AllowUndefs
);
1708 // Check if this is a constant expression splat of the form returned by
1709 // ConstantVector::getSplat()
1710 const auto *Shuf
= dyn_cast
<ConstantExpr
>(this);
1711 if (Shuf
&& Shuf
->getOpcode() == Instruction::ShuffleVector
&&
1712 isa
<UndefValue
>(Shuf
->getOperand(1))) {
1714 const auto *IElt
= dyn_cast
<ConstantExpr
>(Shuf
->getOperand(0));
1715 if (IElt
&& IElt
->getOpcode() == Instruction::InsertElement
&&
1716 isa
<UndefValue
>(IElt
->getOperand(0))) {
1718 ArrayRef
<int> Mask
= Shuf
->getShuffleMask();
1719 Constant
*SplatVal
= IElt
->getOperand(1);
1720 ConstantInt
*Index
= dyn_cast
<ConstantInt
>(IElt
->getOperand(2));
1722 if (Index
&& Index
->getValue() == 0 &&
1723 llvm::all_of(Mask
, [](int I
) { return I
== 0; }))
1731 Constant
*ConstantVector::getSplatValue(bool AllowUndefs
) const {
1732 // Check out first element.
1733 Constant
*Elt
= getOperand(0);
1734 // Then make sure all remaining elements point to the same value.
1735 for (unsigned I
= 1, E
= getNumOperands(); I
< E
; ++I
) {
1736 Constant
*OpC
= getOperand(I
);
1740 // Strict mode: any mismatch is not a splat.
1744 // Allow undefs mode: ignore undefined elements.
1745 if (isa
<UndefValue
>(OpC
))
1748 // If we do not have a defined element yet, use the current operand.
1749 if (isa
<UndefValue
>(Elt
))
1758 const APInt
&Constant::getUniqueInteger() const {
1759 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(this))
1760 return CI
->getValue();
1761 assert(this->getSplatValue() && "Doesn't contain a unique integer!");
1762 const Constant
*C
= this->getAggregateElement(0U);
1763 assert(C
&& isa
<ConstantInt
>(C
) && "Not a vector of numbers!");
1764 return cast
<ConstantInt
>(C
)->getValue();
1767 //---- ConstantPointerNull::get() implementation.
1770 ConstantPointerNull
*ConstantPointerNull::get(PointerType
*Ty
) {
1771 std::unique_ptr
<ConstantPointerNull
> &Entry
=
1772 Ty
->getContext().pImpl
->CPNConstants
[Ty
];
1774 Entry
.reset(new ConstantPointerNull(Ty
));
1779 /// Remove the constant from the constant table.
1780 void ConstantPointerNull::destroyConstantImpl() {
1781 getContext().pImpl
->CPNConstants
.erase(getType());
1784 UndefValue
*UndefValue::get(Type
*Ty
) {
1785 std::unique_ptr
<UndefValue
> &Entry
= Ty
->getContext().pImpl
->UVConstants
[Ty
];
1787 Entry
.reset(new UndefValue(Ty
));
1792 /// Remove the constant from the constant table.
1793 void UndefValue::destroyConstantImpl() {
1794 // Free the constant and any dangling references to it.
1795 if (getValueID() == UndefValueVal
) {
1796 getContext().pImpl
->UVConstants
.erase(getType());
1797 } else if (getValueID() == PoisonValueVal
) {
1798 getContext().pImpl
->PVConstants
.erase(getType());
1800 llvm_unreachable("Not a undef or a poison!");
1803 PoisonValue
*PoisonValue::get(Type
*Ty
) {
1804 std::unique_ptr
<PoisonValue
> &Entry
= Ty
->getContext().pImpl
->PVConstants
[Ty
];
1806 Entry
.reset(new PoisonValue(Ty
));
1811 /// Remove the constant from the constant table.
1812 void PoisonValue::destroyConstantImpl() {
1813 // Free the constant and any dangling references to it.
1814 getContext().pImpl
->PVConstants
.erase(getType());
1817 BlockAddress
*BlockAddress::get(BasicBlock
*BB
) {
1818 assert(BB
->getParent() && "Block must have a parent");
1819 return get(BB
->getParent(), BB
);
1822 BlockAddress
*BlockAddress::get(Function
*F
, BasicBlock
*BB
) {
1824 F
->getContext().pImpl
->BlockAddresses
[std::make_pair(F
, BB
)];
1826 BA
= new BlockAddress(F
, BB
);
1828 assert(BA
->getFunction() == F
&& "Basic block moved between functions");
1832 BlockAddress::BlockAddress(Function
*F
, BasicBlock
*BB
)
1833 : Constant(Type::getInt8PtrTy(F
->getContext(), F
->getAddressSpace()),
1834 Value::BlockAddressVal
, &Op
<0>(), 2) {
1837 BB
->AdjustBlockAddressRefCount(1);
1840 BlockAddress
*BlockAddress::lookup(const BasicBlock
*BB
) {
1841 if (!BB
->hasAddressTaken())
1844 const Function
*F
= BB
->getParent();
1845 assert(F
&& "Block must have a parent");
1847 F
->getContext().pImpl
->BlockAddresses
.lookup(std::make_pair(F
, BB
));
1848 assert(BA
&& "Refcount and block address map disagree!");
1852 /// Remove the constant from the constant table.
1853 void BlockAddress::destroyConstantImpl() {
1854 getFunction()->getType()->getContext().pImpl
1855 ->BlockAddresses
.erase(std::make_pair(getFunction(), getBasicBlock()));
1856 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1859 Value
*BlockAddress::handleOperandChangeImpl(Value
*From
, Value
*To
) {
1860 // This could be replacing either the Basic Block or the Function. In either
1861 // case, we have to remove the map entry.
1862 Function
*NewF
= getFunction();
1863 BasicBlock
*NewBB
= getBasicBlock();
1866 NewF
= cast
<Function
>(To
->stripPointerCasts());
1868 assert(From
== NewBB
&& "From does not match any operand");
1869 NewBB
= cast
<BasicBlock
>(To
);
1872 // See if the 'new' entry already exists, if not, just update this in place
1873 // and return early.
1874 BlockAddress
*&NewBA
=
1875 getContext().pImpl
->BlockAddresses
[std::make_pair(NewF
, NewBB
)];
1879 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1881 // Remove the old entry, this can't cause the map to rehash (just a
1882 // tombstone will get added).
1883 getContext().pImpl
->BlockAddresses
.erase(std::make_pair(getFunction(),
1886 setOperand(0, NewF
);
1887 setOperand(1, NewBB
);
1888 getBasicBlock()->AdjustBlockAddressRefCount(1);
1890 // If we just want to keep the existing value, then return null.
1891 // Callers know that this means we shouldn't delete this value.
1895 DSOLocalEquivalent
*DSOLocalEquivalent::get(GlobalValue
*GV
) {
1896 DSOLocalEquivalent
*&Equiv
= GV
->getContext().pImpl
->DSOLocalEquivalents
[GV
];
1898 Equiv
= new DSOLocalEquivalent(GV
);
1900 assert(Equiv
->getGlobalValue() == GV
&&
1901 "DSOLocalFunction does not match the expected global value");
1905 DSOLocalEquivalent::DSOLocalEquivalent(GlobalValue
*GV
)
1906 : Constant(GV
->getType(), Value::DSOLocalEquivalentVal
, &Op
<0>(), 1) {
1910 /// Remove the constant from the constant table.
1911 void DSOLocalEquivalent::destroyConstantImpl() {
1912 const GlobalValue
*GV
= getGlobalValue();
1913 GV
->getContext().pImpl
->DSOLocalEquivalents
.erase(GV
);
1916 Value
*DSOLocalEquivalent::handleOperandChangeImpl(Value
*From
, Value
*To
) {
1917 assert(From
== getGlobalValue() && "Changing value does not match operand.");
1918 assert(isa
<Constant
>(To
) && "Can only replace the operands with a constant");
1920 // The replacement is with another global value.
1921 if (const auto *ToObj
= dyn_cast
<GlobalValue
>(To
)) {
1922 DSOLocalEquivalent
*&NewEquiv
=
1923 getContext().pImpl
->DSOLocalEquivalents
[ToObj
];
1925 return llvm::ConstantExpr::getBitCast(NewEquiv
, getType());
1928 // If the argument is replaced with a null value, just replace this constant
1929 // with a null value.
1930 if (cast
<Constant
>(To
)->isNullValue())
1933 // The replacement could be a bitcast or an alias to another function. We can
1934 // replace it with a bitcast to the dso_local_equivalent of that function.
1935 auto *Func
= cast
<Function
>(To
->stripPointerCastsAndAliases());
1936 DSOLocalEquivalent
*&NewEquiv
= getContext().pImpl
->DSOLocalEquivalents
[Func
];
1938 return llvm::ConstantExpr::getBitCast(NewEquiv
, getType());
1940 // Replace this with the new one.
1941 getContext().pImpl
->DSOLocalEquivalents
.erase(getGlobalValue());
1943 setOperand(0, Func
);
1945 if (Func
->getType() != getType()) {
1946 // It is ok to mutate the type here because this constant should always
1947 // reflect the type of the function it's holding.
1948 mutateType(Func
->getType());
1953 //---- ConstantExpr::get() implementations.
1956 /// This is a utility function to handle folding of casts and lookup of the
1957 /// cast in the ExprConstants map. It is used by the various get* methods below.
1958 static Constant
*getFoldedCast(Instruction::CastOps opc
, Constant
*C
, Type
*Ty
,
1959 bool OnlyIfReduced
= false) {
1960 assert(Ty
->isFirstClassType() && "Cannot cast to an aggregate type!");
1961 // Fold a few common cases
1962 if (Constant
*FC
= ConstantFoldCastInstruction(opc
, C
, Ty
))
1968 LLVMContextImpl
*pImpl
= Ty
->getContext().pImpl
;
1970 // Look up the constant in the table first to ensure uniqueness.
1971 ConstantExprKeyType
Key(opc
, C
);
1973 return pImpl
->ExprConstants
.getOrCreate(Ty
, Key
);
1976 Constant
*ConstantExpr::getCast(unsigned oc
, Constant
*C
, Type
*Ty
,
1977 bool OnlyIfReduced
) {
1978 Instruction::CastOps opc
= Instruction::CastOps(oc
);
1979 assert(Instruction::isCast(opc
) && "opcode out of range");
1980 assert(C
&& Ty
&& "Null arguments to getCast");
1981 assert(CastInst::castIsValid(opc
, C
, Ty
) && "Invalid constantexpr cast!");
1985 llvm_unreachable("Invalid cast opcode");
1986 case Instruction::Trunc
:
1987 return getTrunc(C
, Ty
, OnlyIfReduced
);
1988 case Instruction::ZExt
:
1989 return getZExt(C
, Ty
, OnlyIfReduced
);
1990 case Instruction::SExt
:
1991 return getSExt(C
, Ty
, OnlyIfReduced
);
1992 case Instruction::FPTrunc
:
1993 return getFPTrunc(C
, Ty
, OnlyIfReduced
);
1994 case Instruction::FPExt
:
1995 return getFPExtend(C
, Ty
, OnlyIfReduced
);
1996 case Instruction::UIToFP
:
1997 return getUIToFP(C
, Ty
, OnlyIfReduced
);
1998 case Instruction::SIToFP
:
1999 return getSIToFP(C
, Ty
, OnlyIfReduced
);
2000 case Instruction::FPToUI
:
2001 return getFPToUI(C
, Ty
, OnlyIfReduced
);
2002 case Instruction::FPToSI
:
2003 return getFPToSI(C
, Ty
, OnlyIfReduced
);
2004 case Instruction::PtrToInt
:
2005 return getPtrToInt(C
, Ty
, OnlyIfReduced
);
2006 case Instruction::IntToPtr
:
2007 return getIntToPtr(C
, Ty
, OnlyIfReduced
);
2008 case Instruction::BitCast
:
2009 return getBitCast(C
, Ty
, OnlyIfReduced
);
2010 case Instruction::AddrSpaceCast
:
2011 return getAddrSpaceCast(C
, Ty
, OnlyIfReduced
);
2015 Constant
*ConstantExpr::getZExtOrBitCast(Constant
*C
, Type
*Ty
) {
2016 if (C
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2017 return getBitCast(C
, Ty
);
2018 return getZExt(C
, Ty
);
2021 Constant
*ConstantExpr::getSExtOrBitCast(Constant
*C
, Type
*Ty
) {
2022 if (C
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2023 return getBitCast(C
, Ty
);
2024 return getSExt(C
, Ty
);
2027 Constant
*ConstantExpr::getTruncOrBitCast(Constant
*C
, Type
*Ty
) {
2028 if (C
->getType()->getScalarSizeInBits() == Ty
->getScalarSizeInBits())
2029 return getBitCast(C
, Ty
);
2030 return getTrunc(C
, Ty
);
2033 Constant
*ConstantExpr::getPointerCast(Constant
*S
, Type
*Ty
) {
2034 assert(S
->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2035 assert((Ty
->isIntOrIntVectorTy() || Ty
->isPtrOrPtrVectorTy()) &&
2038 if (Ty
->isIntOrIntVectorTy())
2039 return getPtrToInt(S
, Ty
);
2041 unsigned SrcAS
= S
->getType()->getPointerAddressSpace();
2042 if (Ty
->isPtrOrPtrVectorTy() && SrcAS
!= Ty
->getPointerAddressSpace())
2043 return getAddrSpaceCast(S
, Ty
);
2045 return getBitCast(S
, Ty
);
2048 Constant
*ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant
*S
,
2050 assert(S
->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2051 assert(Ty
->isPtrOrPtrVectorTy() && "Invalid cast");
2053 if (S
->getType()->getPointerAddressSpace() != Ty
->getPointerAddressSpace())
2054 return getAddrSpaceCast(S
, Ty
);
2056 return getBitCast(S
, Ty
);
2059 Constant
*ConstantExpr::getIntegerCast(Constant
*C
, Type
*Ty
, bool isSigned
) {
2060 assert(C
->getType()->isIntOrIntVectorTy() &&
2061 Ty
->isIntOrIntVectorTy() && "Invalid cast");
2062 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2063 unsigned DstBits
= Ty
->getScalarSizeInBits();
2064 Instruction::CastOps opcode
=
2065 (SrcBits
== DstBits
? Instruction::BitCast
:
2066 (SrcBits
> DstBits
? Instruction::Trunc
:
2067 (isSigned
? Instruction::SExt
: Instruction::ZExt
)));
2068 return getCast(opcode
, C
, Ty
);
2071 Constant
*ConstantExpr::getFPCast(Constant
*C
, Type
*Ty
) {
2072 assert(C
->getType()->isFPOrFPVectorTy() && Ty
->isFPOrFPVectorTy() &&
2074 unsigned SrcBits
= C
->getType()->getScalarSizeInBits();
2075 unsigned DstBits
= Ty
->getScalarSizeInBits();
2076 if (SrcBits
== DstBits
)
2077 return C
; // Avoid a useless cast
2078 Instruction::CastOps opcode
=
2079 (SrcBits
> DstBits
? Instruction::FPTrunc
: Instruction::FPExt
);
2080 return getCast(opcode
, C
, Ty
);
2083 Constant
*ConstantExpr::getTrunc(Constant
*C
, Type
*Ty
, bool OnlyIfReduced
) {
2085 bool fromVec
= isa
<VectorType
>(C
->getType());
2086 bool toVec
= isa
<VectorType
>(Ty
);
2088 assert((fromVec
== toVec
) && "Cannot convert from scalar to/from vector");
2089 assert(C
->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
2090 assert(Ty
->isIntOrIntVectorTy() && "Trunc produces only integral");
2091 assert(C
->getType()->getScalarSizeInBits() > Ty
->getScalarSizeInBits()&&
2092 "SrcTy must be larger than DestTy for Trunc!");
2094 return getFoldedCast(Instruction::Trunc
, C
, Ty
, OnlyIfReduced
);
2097 Constant
*ConstantExpr::getSExt(Constant
*C
, Type
*Ty
, bool OnlyIfReduced
) {
2099 bool fromVec
= isa
<VectorType
>(C
->getType());
2100 bool toVec
= isa
<VectorType
>(Ty
);
2102 assert((fromVec
== toVec
) && "Cannot convert from scalar to/from vector");
2103 assert(C
->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
2104 assert(Ty
->isIntOrIntVectorTy() && "SExt produces only integer");
2105 assert(C
->getType()->getScalarSizeInBits() < Ty
->getScalarSizeInBits()&&
2106 "SrcTy must be smaller than DestTy for SExt!");
2108 return getFoldedCast(Instruction::SExt
, C
, Ty
, OnlyIfReduced
);
2111 Constant
*ConstantExpr::getZExt(Constant
*C
, Type
*Ty
, bool OnlyIfReduced
) {
2113 bool fromVec
= isa
<VectorType
>(C
->getType());
2114 bool toVec
= isa
<VectorType
>(Ty
);
2116 assert((fromVec
== toVec
) && "Cannot convert from scalar to/from vector");
2117 assert(C
->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
2118 assert(Ty
->isIntOrIntVectorTy() && "ZExt produces only integer");
2119 assert(C
->getType()->getScalarSizeInBits() < Ty
->getScalarSizeInBits()&&
2120 "SrcTy must be smaller than DestTy for ZExt!");
2122 return getFoldedCast(Instruction::ZExt
, C
, Ty
, OnlyIfReduced
);
2125 Constant
*ConstantExpr::getFPTrunc(Constant
*C
, Type
*Ty
, bool OnlyIfReduced
) {
2127 bool fromVec
= isa
<VectorType
>(C
->getType());
2128 bool toVec
= isa
<VectorType
>(Ty
);
2130 assert((fromVec
== toVec
) && "Cannot convert from scalar to/from vector");
2131 assert(C
->getType()->isFPOrFPVectorTy() && Ty
->isFPOrFPVectorTy() &&
2132 C
->getType()->getScalarSizeInBits() > Ty
->getScalarSizeInBits()&&
2133 "This is an illegal floating point truncation!");
2134 return getFoldedCast(Instruction::FPTrunc
, C
, Ty
, OnlyIfReduced
);
2137 Constant
*ConstantExpr::getFPExtend(Constant
*C
, Type
*Ty
, bool OnlyIfReduced
) {
2139 bool fromVec
= isa
<VectorType
>(C
->getType());
2140 bool toVec
= isa
<VectorType
>(Ty
);
2142 assert((fromVec
== toVec
) && "Cannot convert from scalar to/from vector");
2143 assert(C
->getType()->isFPOrFPVectorTy() && Ty
->isFPOrFPVectorTy() &&
2144 C
->getType()->getScalarSizeInBits() < Ty
->getScalarSizeInBits()&&
2145 "This is an illegal floating point extension!");
2146 return getFoldedCast(Instruction::FPExt
, C
, Ty
, OnlyIfReduced
);
2149 Constant
*ConstantExpr::getUIToFP(Constant
*C
, Type
*Ty
, bool OnlyIfReduced
) {
2151 bool fromVec
= isa
<VectorType
>(C
->getType());
2152 bool toVec
= isa
<VectorType
>(Ty
);
2154 assert((fromVec
== toVec
) && "Cannot convert from scalar to/from vector");
2155 assert(C
->getType()->isIntOrIntVectorTy() && Ty
->isFPOrFPVectorTy() &&
2156 "This is an illegal uint to floating point cast!");
2157 return getFoldedCast(Instruction::UIToFP
, C
, Ty
, OnlyIfReduced
);
2160 Constant
*ConstantExpr::getSIToFP(Constant
*C
, Type
*Ty
, bool OnlyIfReduced
) {
2162 bool fromVec
= isa
<VectorType
>(C
->getType());
2163 bool toVec
= isa
<VectorType
>(Ty
);
2165 assert((fromVec
== toVec
) && "Cannot convert from scalar to/from vector");
2166 assert(C
->getType()->isIntOrIntVectorTy() && Ty
->isFPOrFPVectorTy() &&
2167 "This is an illegal sint to floating point cast!");
2168 return getFoldedCast(Instruction::SIToFP
, C
, Ty
, OnlyIfReduced
);
2171 Constant
*ConstantExpr::getFPToUI(Constant
*C
, Type
*Ty
, bool OnlyIfReduced
) {
2173 bool fromVec
= isa
<VectorType
>(C
->getType());
2174 bool toVec
= isa
<VectorType
>(Ty
);
2176 assert((fromVec
== toVec
) && "Cannot convert from scalar to/from vector");
2177 assert(C
->getType()->isFPOrFPVectorTy() && Ty
->isIntOrIntVectorTy() &&
2178 "This is an illegal floating point to uint cast!");
2179 return getFoldedCast(Instruction::FPToUI
, C
, Ty
, OnlyIfReduced
);
2182 Constant
*ConstantExpr::getFPToSI(Constant
*C
, Type
*Ty
, bool OnlyIfReduced
) {
2184 bool fromVec
= isa
<VectorType
>(C
->getType());
2185 bool toVec
= isa
<VectorType
>(Ty
);
2187 assert((fromVec
== toVec
) && "Cannot convert from scalar to/from vector");
2188 assert(C
->getType()->isFPOrFPVectorTy() && Ty
->isIntOrIntVectorTy() &&
2189 "This is an illegal floating point to sint cast!");
2190 return getFoldedCast(Instruction::FPToSI
, C
, Ty
, OnlyIfReduced
);
2193 Constant
*ConstantExpr::getPtrToInt(Constant
*C
, Type
*DstTy
,
2194 bool OnlyIfReduced
) {
2195 assert(C
->getType()->isPtrOrPtrVectorTy() &&
2196 "PtrToInt source must be pointer or pointer vector");
2197 assert(DstTy
->isIntOrIntVectorTy() &&
2198 "PtrToInt destination must be integer or integer vector");
2199 assert(isa
<VectorType
>(C
->getType()) == isa
<VectorType
>(DstTy
));
2200 if (isa
<VectorType
>(C
->getType()))
2201 assert(cast
<FixedVectorType
>(C
->getType())->getNumElements() ==
2202 cast
<FixedVectorType
>(DstTy
)->getNumElements() &&
2203 "Invalid cast between a different number of vector elements");
2204 return getFoldedCast(Instruction::PtrToInt
, C
, DstTy
, OnlyIfReduced
);
2207 Constant
*ConstantExpr::getIntToPtr(Constant
*C
, Type
*DstTy
,
2208 bool OnlyIfReduced
) {
2209 assert(C
->getType()->isIntOrIntVectorTy() &&
2210 "IntToPtr source must be integer or integer vector");
2211 assert(DstTy
->isPtrOrPtrVectorTy() &&
2212 "IntToPtr destination must be a pointer or pointer vector");
2213 assert(isa
<VectorType
>(C
->getType()) == isa
<VectorType
>(DstTy
));
2214 if (isa
<VectorType
>(C
->getType()))
2215 assert(cast
<VectorType
>(C
->getType())->getElementCount() ==
2216 cast
<VectorType
>(DstTy
)->getElementCount() &&
2217 "Invalid cast between a different number of vector elements");
2218 return getFoldedCast(Instruction::IntToPtr
, C
, DstTy
, OnlyIfReduced
);
2221 Constant
*ConstantExpr::getBitCast(Constant
*C
, Type
*DstTy
,
2222 bool OnlyIfReduced
) {
2223 assert(CastInst::castIsValid(Instruction::BitCast
, C
, DstTy
) &&
2224 "Invalid constantexpr bitcast!");
2226 // It is common to ask for a bitcast of a value to its own type, handle this
2228 if (C
->getType() == DstTy
) return C
;
2230 return getFoldedCast(Instruction::BitCast
, C
, DstTy
, OnlyIfReduced
);
2233 Constant
*ConstantExpr::getAddrSpaceCast(Constant
*C
, Type
*DstTy
,
2234 bool OnlyIfReduced
) {
2235 assert(CastInst::castIsValid(Instruction::AddrSpaceCast
, C
, DstTy
) &&
2236 "Invalid constantexpr addrspacecast!");
2238 // Canonicalize addrspacecasts between different pointer types by first
2239 // bitcasting the pointer type and then converting the address space.
2240 PointerType
*SrcScalarTy
= cast
<PointerType
>(C
->getType()->getScalarType());
2241 PointerType
*DstScalarTy
= cast
<PointerType
>(DstTy
->getScalarType());
2242 if (!SrcScalarTy
->hasSameElementTypeAs(DstScalarTy
)) {
2243 Type
*MidTy
= PointerType::getWithSamePointeeType(
2244 DstScalarTy
, SrcScalarTy
->getAddressSpace());
2245 if (VectorType
*VT
= dyn_cast
<VectorType
>(DstTy
)) {
2246 // Handle vectors of pointers.
2247 MidTy
= FixedVectorType::get(MidTy
,
2248 cast
<FixedVectorType
>(VT
)->getNumElements());
2250 C
= getBitCast(C
, MidTy
);
2252 return getFoldedCast(Instruction::AddrSpaceCast
, C
, DstTy
, OnlyIfReduced
);
2255 Constant
*ConstantExpr::get(unsigned Opcode
, Constant
*C
, unsigned Flags
,
2256 Type
*OnlyIfReducedTy
) {
2257 // Check the operands for consistency first.
2258 assert(Instruction::isUnaryOp(Opcode
) &&
2259 "Invalid opcode in unary constant expression");
2263 case Instruction::FNeg
:
2264 assert(C
->getType()->isFPOrFPVectorTy() &&
2265 "Tried to create a floating-point operation on a "
2266 "non-floating-point type!");
2273 if (Constant
*FC
= ConstantFoldUnaryInstruction(Opcode
, C
))
2276 if (OnlyIfReducedTy
== C
->getType())
2279 Constant
*ArgVec
[] = { C
};
2280 ConstantExprKeyType
Key(Opcode
, ArgVec
, 0, Flags
);
2282 LLVMContextImpl
*pImpl
= C
->getContext().pImpl
;
2283 return pImpl
->ExprConstants
.getOrCreate(C
->getType(), Key
);
2286 Constant
*ConstantExpr::get(unsigned Opcode
, Constant
*C1
, Constant
*C2
,
2287 unsigned Flags
, Type
*OnlyIfReducedTy
) {
2288 // Check the operands for consistency first.
2289 assert(Instruction::isBinaryOp(Opcode
) &&
2290 "Invalid opcode in binary constant expression");
2291 assert(C1
->getType() == C2
->getType() &&
2292 "Operand types in binary constant expression should match");
2296 case Instruction::Add
:
2297 case Instruction::Sub
:
2298 case Instruction::Mul
:
2299 case Instruction::UDiv
:
2300 case Instruction::SDiv
:
2301 case Instruction::URem
:
2302 case Instruction::SRem
:
2303 assert(C1
->getType()->isIntOrIntVectorTy() &&
2304 "Tried to create an integer operation on a non-integer type!");
2306 case Instruction::FAdd
:
2307 case Instruction::FSub
:
2308 case Instruction::FMul
:
2309 case Instruction::FDiv
:
2310 case Instruction::FRem
:
2311 assert(C1
->getType()->isFPOrFPVectorTy() &&
2312 "Tried to create a floating-point operation on a "
2313 "non-floating-point type!");
2315 case Instruction::And
:
2316 case Instruction::Or
:
2317 case Instruction::Xor
:
2318 assert(C1
->getType()->isIntOrIntVectorTy() &&
2319 "Tried to create a logical operation on a non-integral type!");
2321 case Instruction::Shl
:
2322 case Instruction::LShr
:
2323 case Instruction::AShr
:
2324 assert(C1
->getType()->isIntOrIntVectorTy() &&
2325 "Tried to create a shift operation on a non-integer type!");
2332 if (Constant
*FC
= ConstantFoldBinaryInstruction(Opcode
, C1
, C2
))
2335 if (OnlyIfReducedTy
== C1
->getType())
2338 Constant
*ArgVec
[] = { C1
, C2
};
2339 ConstantExprKeyType
Key(Opcode
, ArgVec
, 0, Flags
);
2341 LLVMContextImpl
*pImpl
= C1
->getContext().pImpl
;
2342 return pImpl
->ExprConstants
.getOrCreate(C1
->getType(), Key
);
2345 Constant
*ConstantExpr::getSizeOf(Type
* Ty
) {
2346 // sizeof is implemented as: (i64) gep (Ty*)null, 1
2347 // Note that a non-inbounds gep is used, as null isn't within any object.
2348 Constant
*GEPIdx
= ConstantInt::get(Type::getInt32Ty(Ty
->getContext()), 1);
2349 Constant
*GEP
= getGetElementPtr(
2350 Ty
, Constant::getNullValue(PointerType::getUnqual(Ty
)), GEPIdx
);
2351 return getPtrToInt(GEP
,
2352 Type::getInt64Ty(Ty
->getContext()));
2355 Constant
*ConstantExpr::getAlignOf(Type
* Ty
) {
2356 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
2357 // Note that a non-inbounds gep is used, as null isn't within any object.
2358 Type
*AligningTy
= StructType::get(Type::getInt1Ty(Ty
->getContext()), Ty
);
2359 Constant
*NullPtr
= Constant::getNullValue(AligningTy
->getPointerTo(0));
2360 Constant
*Zero
= ConstantInt::get(Type::getInt64Ty(Ty
->getContext()), 0);
2361 Constant
*One
= ConstantInt::get(Type::getInt32Ty(Ty
->getContext()), 1);
2362 Constant
*Indices
[2] = { Zero
, One
};
2363 Constant
*GEP
= getGetElementPtr(AligningTy
, NullPtr
, Indices
);
2364 return getPtrToInt(GEP
,
2365 Type::getInt64Ty(Ty
->getContext()));
2368 Constant
*ConstantExpr::getOffsetOf(StructType
* STy
, unsigned FieldNo
) {
2369 return getOffsetOf(STy
, ConstantInt::get(Type::getInt32Ty(STy
->getContext()),
2373 Constant
*ConstantExpr::getOffsetOf(Type
* Ty
, Constant
*FieldNo
) {
2374 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
2375 // Note that a non-inbounds gep is used, as null isn't within any object.
2376 Constant
*GEPIdx
[] = {
2377 ConstantInt::get(Type::getInt64Ty(Ty
->getContext()), 0),
2380 Constant
*GEP
= getGetElementPtr(
2381 Ty
, Constant::getNullValue(PointerType::getUnqual(Ty
)), GEPIdx
);
2382 return getPtrToInt(GEP
,
2383 Type::getInt64Ty(Ty
->getContext()));
2386 Constant
*ConstantExpr::getCompare(unsigned short Predicate
, Constant
*C1
,
2387 Constant
*C2
, bool OnlyIfReduced
) {
2388 assert(C1
->getType() == C2
->getType() && "Op types should be identical!");
2390 switch (Predicate
) {
2391 default: llvm_unreachable("Invalid CmpInst predicate");
2392 case CmpInst::FCMP_FALSE
: case CmpInst::FCMP_OEQ
: case CmpInst::FCMP_OGT
:
2393 case CmpInst::FCMP_OGE
: case CmpInst::FCMP_OLT
: case CmpInst::FCMP_OLE
:
2394 case CmpInst::FCMP_ONE
: case CmpInst::FCMP_ORD
: case CmpInst::FCMP_UNO
:
2395 case CmpInst::FCMP_UEQ
: case CmpInst::FCMP_UGT
: case CmpInst::FCMP_UGE
:
2396 case CmpInst::FCMP_ULT
: case CmpInst::FCMP_ULE
: case CmpInst::FCMP_UNE
:
2397 case CmpInst::FCMP_TRUE
:
2398 return getFCmp(Predicate
, C1
, C2
, OnlyIfReduced
);
2400 case CmpInst::ICMP_EQ
: case CmpInst::ICMP_NE
: case CmpInst::ICMP_UGT
:
2401 case CmpInst::ICMP_UGE
: case CmpInst::ICMP_ULT
: case CmpInst::ICMP_ULE
:
2402 case CmpInst::ICMP_SGT
: case CmpInst::ICMP_SGE
: case CmpInst::ICMP_SLT
:
2403 case CmpInst::ICMP_SLE
:
2404 return getICmp(Predicate
, C1
, C2
, OnlyIfReduced
);
2408 Constant
*ConstantExpr::getSelect(Constant
*C
, Constant
*V1
, Constant
*V2
,
2409 Type
*OnlyIfReducedTy
) {
2410 assert(!SelectInst::areInvalidOperands(C
, V1
, V2
)&&"Invalid select operands");
2412 if (Constant
*SC
= ConstantFoldSelectInstruction(C
, V1
, V2
))
2413 return SC
; // Fold common cases
2415 if (OnlyIfReducedTy
== V1
->getType())
2418 Constant
*ArgVec
[] = { C
, V1
, V2
};
2419 ConstantExprKeyType
Key(Instruction::Select
, ArgVec
);
2421 LLVMContextImpl
*pImpl
= C
->getContext().pImpl
;
2422 return pImpl
->ExprConstants
.getOrCreate(V1
->getType(), Key
);
2425 Constant
*ConstantExpr::getGetElementPtr(Type
*Ty
, Constant
*C
,
2426 ArrayRef
<Value
*> Idxs
, bool InBounds
,
2427 Optional
<unsigned> InRangeIndex
,
2428 Type
*OnlyIfReducedTy
) {
2429 PointerType
*OrigPtrTy
= cast
<PointerType
>(C
->getType()->getScalarType());
2430 assert(Ty
&& "Must specify element type");
2431 assert(OrigPtrTy
->isOpaqueOrPointeeTypeMatches(Ty
));
2434 ConstantFoldGetElementPtr(Ty
, C
, InBounds
, InRangeIndex
, Idxs
))
2435 return FC
; // Fold a few common cases.
2437 // Get the result type of the getelementptr!
2438 Type
*DestTy
= GetElementPtrInst::getIndexedType(Ty
, Idxs
);
2439 assert(DestTy
&& "GEP indices invalid!");
2440 unsigned AS
= OrigPtrTy
->getAddressSpace();
2441 Type
*ReqTy
= OrigPtrTy
->isOpaque()
2442 ? PointerType::get(OrigPtrTy
->getContext(), AS
)
2443 : DestTy
->getPointerTo(AS
);
2445 auto EltCount
= ElementCount::getFixed(0);
2446 if (VectorType
*VecTy
= dyn_cast
<VectorType
>(C
->getType()))
2447 EltCount
= VecTy
->getElementCount();
2449 for (auto Idx
: Idxs
)
2450 if (VectorType
*VecTy
= dyn_cast
<VectorType
>(Idx
->getType()))
2451 EltCount
= VecTy
->getElementCount();
2453 if (EltCount
.isNonZero())
2454 ReqTy
= VectorType::get(ReqTy
, EltCount
);
2456 if (OnlyIfReducedTy
== ReqTy
)
2459 // Look up the constant in the table first to ensure uniqueness
2460 std::vector
<Constant
*> ArgVec
;
2461 ArgVec
.reserve(1 + Idxs
.size());
2462 ArgVec
.push_back(C
);
2463 auto GTI
= gep_type_begin(Ty
, Idxs
), GTE
= gep_type_end(Ty
, Idxs
);
2464 for (; GTI
!= GTE
; ++GTI
) {
2465 auto *Idx
= cast
<Constant
>(GTI
.getOperand());
2467 (!isa
<VectorType
>(Idx
->getType()) ||
2468 cast
<VectorType
>(Idx
->getType())->getElementCount() == EltCount
) &&
2469 "getelementptr index type missmatch");
2471 if (GTI
.isStruct() && Idx
->getType()->isVectorTy()) {
2472 Idx
= Idx
->getSplatValue();
2473 } else if (GTI
.isSequential() && EltCount
.isNonZero() &&
2474 !Idx
->getType()->isVectorTy()) {
2475 Idx
= ConstantVector::getSplat(EltCount
, Idx
);
2477 ArgVec
.push_back(Idx
);
2480 unsigned SubClassOptionalData
= InBounds
? GEPOperator::IsInBounds
: 0;
2481 if (InRangeIndex
&& *InRangeIndex
< 63)
2482 SubClassOptionalData
|= (*InRangeIndex
+ 1) << 1;
2483 const ConstantExprKeyType
Key(Instruction::GetElementPtr
, ArgVec
, 0,
2484 SubClassOptionalData
, None
, None
, Ty
);
2486 LLVMContextImpl
*pImpl
= C
->getContext().pImpl
;
2487 return pImpl
->ExprConstants
.getOrCreate(ReqTy
, Key
);
2490 Constant
*ConstantExpr::getICmp(unsigned short pred
, Constant
*LHS
,
2491 Constant
*RHS
, bool OnlyIfReduced
) {
2492 assert(LHS
->getType() == RHS
->getType());
2493 assert(CmpInst::isIntPredicate((CmpInst::Predicate
)pred
) &&
2494 "Invalid ICmp Predicate");
2496 if (Constant
*FC
= ConstantFoldCompareInstruction(pred
, LHS
, RHS
))
2497 return FC
; // Fold a few common cases...
2502 // Look up the constant in the table first to ensure uniqueness
2503 Constant
*ArgVec
[] = { LHS
, RHS
};
2504 // Get the key type with both the opcode and predicate
2505 const ConstantExprKeyType
Key(Instruction::ICmp
, ArgVec
, pred
);
2507 Type
*ResultTy
= Type::getInt1Ty(LHS
->getContext());
2508 if (VectorType
*VT
= dyn_cast
<VectorType
>(LHS
->getType()))
2509 ResultTy
= VectorType::get(ResultTy
, VT
->getElementCount());
2511 LLVMContextImpl
*pImpl
= LHS
->getType()->getContext().pImpl
;
2512 return pImpl
->ExprConstants
.getOrCreate(ResultTy
, Key
);
2515 Constant
*ConstantExpr::getFCmp(unsigned short pred
, Constant
*LHS
,
2516 Constant
*RHS
, bool OnlyIfReduced
) {
2517 assert(LHS
->getType() == RHS
->getType());
2518 assert(CmpInst::isFPPredicate((CmpInst::Predicate
)pred
) &&
2519 "Invalid FCmp Predicate");
2521 if (Constant
*FC
= ConstantFoldCompareInstruction(pred
, LHS
, RHS
))
2522 return FC
; // Fold a few common cases...
2527 // Look up the constant in the table first to ensure uniqueness
2528 Constant
*ArgVec
[] = { LHS
, RHS
};
2529 // Get the key type with both the opcode and predicate
2530 const ConstantExprKeyType
Key(Instruction::FCmp
, ArgVec
, pred
);
2532 Type
*ResultTy
= Type::getInt1Ty(LHS
->getContext());
2533 if (VectorType
*VT
= dyn_cast
<VectorType
>(LHS
->getType()))
2534 ResultTy
= VectorType::get(ResultTy
, VT
->getElementCount());
2536 LLVMContextImpl
*pImpl
= LHS
->getType()->getContext().pImpl
;
2537 return pImpl
->ExprConstants
.getOrCreate(ResultTy
, Key
);
2540 Constant
*ConstantExpr::getExtractElement(Constant
*Val
, Constant
*Idx
,
2541 Type
*OnlyIfReducedTy
) {
2542 assert(Val
->getType()->isVectorTy() &&
2543 "Tried to create extractelement operation on non-vector type!");
2544 assert(Idx
->getType()->isIntegerTy() &&
2545 "Extractelement index must be an integer type!");
2547 if (Constant
*FC
= ConstantFoldExtractElementInstruction(Val
, Idx
))
2548 return FC
; // Fold a few common cases.
2550 Type
*ReqTy
= cast
<VectorType
>(Val
->getType())->getElementType();
2551 if (OnlyIfReducedTy
== ReqTy
)
2554 // Look up the constant in the table first to ensure uniqueness
2555 Constant
*ArgVec
[] = { Val
, Idx
};
2556 const ConstantExprKeyType
Key(Instruction::ExtractElement
, ArgVec
);
2558 LLVMContextImpl
*pImpl
= Val
->getContext().pImpl
;
2559 return pImpl
->ExprConstants
.getOrCreate(ReqTy
, Key
);
2562 Constant
*ConstantExpr::getInsertElement(Constant
*Val
, Constant
*Elt
,
2563 Constant
*Idx
, Type
*OnlyIfReducedTy
) {
2564 assert(Val
->getType()->isVectorTy() &&
2565 "Tried to create insertelement operation on non-vector type!");
2566 assert(Elt
->getType() == cast
<VectorType
>(Val
->getType())->getElementType() &&
2567 "Insertelement types must match!");
2568 assert(Idx
->getType()->isIntegerTy() &&
2569 "Insertelement index must be i32 type!");
2571 if (Constant
*FC
= ConstantFoldInsertElementInstruction(Val
, Elt
, Idx
))
2572 return FC
; // Fold a few common cases.
2574 if (OnlyIfReducedTy
== Val
->getType())
2577 // Look up the constant in the table first to ensure uniqueness
2578 Constant
*ArgVec
[] = { Val
, Elt
, Idx
};
2579 const ConstantExprKeyType
Key(Instruction::InsertElement
, ArgVec
);
2581 LLVMContextImpl
*pImpl
= Val
->getContext().pImpl
;
2582 return pImpl
->ExprConstants
.getOrCreate(Val
->getType(), Key
);
2585 Constant
*ConstantExpr::getShuffleVector(Constant
*V1
, Constant
*V2
,
2587 Type
*OnlyIfReducedTy
) {
2588 assert(ShuffleVectorInst::isValidOperands(V1
, V2
, Mask
) &&
2589 "Invalid shuffle vector constant expr operands!");
2591 if (Constant
*FC
= ConstantFoldShuffleVectorInstruction(V1
, V2
, Mask
))
2592 return FC
; // Fold a few common cases.
2594 unsigned NElts
= Mask
.size();
2595 auto V1VTy
= cast
<VectorType
>(V1
->getType());
2596 Type
*EltTy
= V1VTy
->getElementType();
2597 bool TypeIsScalable
= isa
<ScalableVectorType
>(V1VTy
);
2598 Type
*ShufTy
= VectorType::get(EltTy
, NElts
, TypeIsScalable
);
2600 if (OnlyIfReducedTy
== ShufTy
)
2603 // Look up the constant in the table first to ensure uniqueness
2604 Constant
*ArgVec
[] = {V1
, V2
};
2605 ConstantExprKeyType
Key(Instruction::ShuffleVector
, ArgVec
, 0, 0, None
, Mask
);
2607 LLVMContextImpl
*pImpl
= ShufTy
->getContext().pImpl
;
2608 return pImpl
->ExprConstants
.getOrCreate(ShufTy
, Key
);
2611 Constant
*ConstantExpr::getInsertValue(Constant
*Agg
, Constant
*Val
,
2612 ArrayRef
<unsigned> Idxs
,
2613 Type
*OnlyIfReducedTy
) {
2614 assert(Agg
->getType()->isFirstClassType() &&
2615 "Non-first-class type for constant insertvalue expression");
2617 assert(ExtractValueInst::getIndexedType(Agg
->getType(),
2618 Idxs
) == Val
->getType() &&
2619 "insertvalue indices invalid!");
2620 Type
*ReqTy
= Val
->getType();
2622 if (Constant
*FC
= ConstantFoldInsertValueInstruction(Agg
, Val
, Idxs
))
2625 if (OnlyIfReducedTy
== ReqTy
)
2628 Constant
*ArgVec
[] = { Agg
, Val
};
2629 const ConstantExprKeyType
Key(Instruction::InsertValue
, ArgVec
, 0, 0, Idxs
);
2631 LLVMContextImpl
*pImpl
= Agg
->getContext().pImpl
;
2632 return pImpl
->ExprConstants
.getOrCreate(ReqTy
, Key
);
2635 Constant
*ConstantExpr::getExtractValue(Constant
*Agg
, ArrayRef
<unsigned> Idxs
,
2636 Type
*OnlyIfReducedTy
) {
2637 assert(Agg
->getType()->isFirstClassType() &&
2638 "Tried to create extractelement operation on non-first-class type!");
2640 Type
*ReqTy
= ExtractValueInst::getIndexedType(Agg
->getType(), Idxs
);
2642 assert(ReqTy
&& "extractvalue indices invalid!");
2644 assert(Agg
->getType()->isFirstClassType() &&
2645 "Non-first-class type for constant extractvalue expression");
2646 if (Constant
*FC
= ConstantFoldExtractValueInstruction(Agg
, Idxs
))
2649 if (OnlyIfReducedTy
== ReqTy
)
2652 Constant
*ArgVec
[] = { Agg
};
2653 const ConstantExprKeyType
Key(Instruction::ExtractValue
, ArgVec
, 0, 0, Idxs
);
2655 LLVMContextImpl
*pImpl
= Agg
->getContext().pImpl
;
2656 return pImpl
->ExprConstants
.getOrCreate(ReqTy
, Key
);
2659 Constant
*ConstantExpr::getNeg(Constant
*C
, bool HasNUW
, bool HasNSW
) {
2660 assert(C
->getType()->isIntOrIntVectorTy() &&
2661 "Cannot NEG a nonintegral value!");
2662 return getSub(ConstantFP::getZeroValueForNegation(C
->getType()),
2666 Constant
*ConstantExpr::getFNeg(Constant
*C
) {
2667 assert(C
->getType()->isFPOrFPVectorTy() &&
2668 "Cannot FNEG a non-floating-point value!");
2669 return get(Instruction::FNeg
, C
);
2672 Constant
*ConstantExpr::getNot(Constant
*C
) {
2673 assert(C
->getType()->isIntOrIntVectorTy() &&
2674 "Cannot NOT a nonintegral value!");
2675 return get(Instruction::Xor
, C
, Constant::getAllOnesValue(C
->getType()));
2678 Constant
*ConstantExpr::getAdd(Constant
*C1
, Constant
*C2
,
2679 bool HasNUW
, bool HasNSW
) {
2680 unsigned Flags
= (HasNUW
? OverflowingBinaryOperator::NoUnsignedWrap
: 0) |
2681 (HasNSW
? OverflowingBinaryOperator::NoSignedWrap
: 0);
2682 return get(Instruction::Add
, C1
, C2
, Flags
);
2685 Constant
*ConstantExpr::getFAdd(Constant
*C1
, Constant
*C2
) {
2686 return get(Instruction::FAdd
, C1
, C2
);
2689 Constant
*ConstantExpr::getSub(Constant
*C1
, Constant
*C2
,
2690 bool HasNUW
, bool HasNSW
) {
2691 unsigned Flags
= (HasNUW
? OverflowingBinaryOperator::NoUnsignedWrap
: 0) |
2692 (HasNSW
? OverflowingBinaryOperator::NoSignedWrap
: 0);
2693 return get(Instruction::Sub
, C1
, C2
, Flags
);
2696 Constant
*ConstantExpr::getFSub(Constant
*C1
, Constant
*C2
) {
2697 return get(Instruction::FSub
, C1
, C2
);
2700 Constant
*ConstantExpr::getMul(Constant
*C1
, Constant
*C2
,
2701 bool HasNUW
, bool HasNSW
) {
2702 unsigned Flags
= (HasNUW
? OverflowingBinaryOperator::NoUnsignedWrap
: 0) |
2703 (HasNSW
? OverflowingBinaryOperator::NoSignedWrap
: 0);
2704 return get(Instruction::Mul
, C1
, C2
, Flags
);
2707 Constant
*ConstantExpr::getFMul(Constant
*C1
, Constant
*C2
) {
2708 return get(Instruction::FMul
, C1
, C2
);
2711 Constant
*ConstantExpr::getUDiv(Constant
*C1
, Constant
*C2
, bool isExact
) {
2712 return get(Instruction::UDiv
, C1
, C2
,
2713 isExact
? PossiblyExactOperator::IsExact
: 0);
2716 Constant
*ConstantExpr::getSDiv(Constant
*C1
, Constant
*C2
, bool isExact
) {
2717 return get(Instruction::SDiv
, C1
, C2
,
2718 isExact
? PossiblyExactOperator::IsExact
: 0);
2721 Constant
*ConstantExpr::getFDiv(Constant
*C1
, Constant
*C2
) {
2722 return get(Instruction::FDiv
, C1
, C2
);
2725 Constant
*ConstantExpr::getURem(Constant
*C1
, Constant
*C2
) {
2726 return get(Instruction::URem
, C1
, C2
);
2729 Constant
*ConstantExpr::getSRem(Constant
*C1
, Constant
*C2
) {
2730 return get(Instruction::SRem
, C1
, C2
);
2733 Constant
*ConstantExpr::getFRem(Constant
*C1
, Constant
*C2
) {
2734 return get(Instruction::FRem
, C1
, C2
);
2737 Constant
*ConstantExpr::getAnd(Constant
*C1
, Constant
*C2
) {
2738 return get(Instruction::And
, C1
, C2
);
2741 Constant
*ConstantExpr::getOr(Constant
*C1
, Constant
*C2
) {
2742 return get(Instruction::Or
, C1
, C2
);
2745 Constant
*ConstantExpr::getXor(Constant
*C1
, Constant
*C2
) {
2746 return get(Instruction::Xor
, C1
, C2
);
2749 Constant
*ConstantExpr::getUMin(Constant
*C1
, Constant
*C2
) {
2750 Constant
*Cmp
= ConstantExpr::getICmp(CmpInst::ICMP_ULT
, C1
, C2
);
2751 return getSelect(Cmp
, C1
, C2
);
2754 Constant
*ConstantExpr::getShl(Constant
*C1
, Constant
*C2
,
2755 bool HasNUW
, bool HasNSW
) {
2756 unsigned Flags
= (HasNUW
? OverflowingBinaryOperator::NoUnsignedWrap
: 0) |
2757 (HasNSW
? OverflowingBinaryOperator::NoSignedWrap
: 0);
2758 return get(Instruction::Shl
, C1
, C2
, Flags
);
2761 Constant
*ConstantExpr::getLShr(Constant
*C1
, Constant
*C2
, bool isExact
) {
2762 return get(Instruction::LShr
, C1
, C2
,
2763 isExact
? PossiblyExactOperator::IsExact
: 0);
2766 Constant
*ConstantExpr::getAShr(Constant
*C1
, Constant
*C2
, bool isExact
) {
2767 return get(Instruction::AShr
, C1
, C2
,
2768 isExact
? PossiblyExactOperator::IsExact
: 0);
2771 Constant
*ConstantExpr::getExactLogBase2(Constant
*C
) {
2772 Type
*Ty
= C
->getType();
2774 if (match(C
, m_APInt(IVal
)) && IVal
->isPowerOf2())
2775 return ConstantInt::get(Ty
, IVal
->logBase2());
2777 // FIXME: We can extract pow of 2 of splat constant for scalable vectors.
2778 auto *VecTy
= dyn_cast
<FixedVectorType
>(Ty
);
2782 SmallVector
<Constant
*, 4> Elts
;
2783 for (unsigned I
= 0, E
= VecTy
->getNumElements(); I
!= E
; ++I
) {
2784 Constant
*Elt
= C
->getAggregateElement(I
);
2787 // Note that log2(iN undef) is *NOT* iN undef, because log2(iN undef) u< N.
2788 if (isa
<UndefValue
>(Elt
)) {
2789 Elts
.push_back(Constant::getNullValue(Ty
->getScalarType()));
2792 if (!match(Elt
, m_APInt(IVal
)) || !IVal
->isPowerOf2())
2794 Elts
.push_back(ConstantInt::get(Ty
->getScalarType(), IVal
->logBase2()));
2797 return ConstantVector::get(Elts
);
2800 Constant
*ConstantExpr::getBinOpIdentity(unsigned Opcode
, Type
*Ty
,
2801 bool AllowRHSConstant
) {
2802 assert(Instruction::isBinaryOp(Opcode
) && "Only binops allowed");
2804 // Commutative opcodes: it does not matter if AllowRHSConstant is set.
2805 if (Instruction::isCommutative(Opcode
)) {
2807 case Instruction::Add
: // X + 0 = X
2808 case Instruction::Or
: // X | 0 = X
2809 case Instruction::Xor
: // X ^ 0 = X
2810 return Constant::getNullValue(Ty
);
2811 case Instruction::Mul
: // X * 1 = X
2812 return ConstantInt::get(Ty
, 1);
2813 case Instruction::And
: // X & -1 = X
2814 return Constant::getAllOnesValue(Ty
);
2815 case Instruction::FAdd
: // X + -0.0 = X
2816 // TODO: If the fadd has 'nsz', should we return +0.0?
2817 return ConstantFP::getNegativeZero(Ty
);
2818 case Instruction::FMul
: // X * 1.0 = X
2819 return ConstantFP::get(Ty
, 1.0);
2821 llvm_unreachable("Every commutative binop has an identity constant");
2825 // Non-commutative opcodes: AllowRHSConstant must be set.
2826 if (!AllowRHSConstant
)
2830 case Instruction::Sub
: // X - 0 = X
2831 case Instruction::Shl
: // X << 0 = X
2832 case Instruction::LShr
: // X >>u 0 = X
2833 case Instruction::AShr
: // X >> 0 = X
2834 case Instruction::FSub
: // X - 0.0 = X
2835 return Constant::getNullValue(Ty
);
2836 case Instruction::SDiv
: // X / 1 = X
2837 case Instruction::UDiv
: // X /u 1 = X
2838 return ConstantInt::get(Ty
, 1);
2839 case Instruction::FDiv
: // X / 1.0 = X
2840 return ConstantFP::get(Ty
, 1.0);
2846 Constant
*ConstantExpr::getBinOpAbsorber(unsigned Opcode
, Type
*Ty
) {
2849 // Doesn't have an absorber.
2852 case Instruction::Or
:
2853 return Constant::getAllOnesValue(Ty
);
2855 case Instruction::And
:
2856 case Instruction::Mul
:
2857 return Constant::getNullValue(Ty
);
2861 /// Remove the constant from the constant table.
2862 void ConstantExpr::destroyConstantImpl() {
2863 getType()->getContext().pImpl
->ExprConstants
.remove(this);
2866 const char *ConstantExpr::getOpcodeName() const {
2867 return Instruction::getOpcodeName(getOpcode());
2870 GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2871 Type
*SrcElementTy
, Constant
*C
, ArrayRef
<Constant
*> IdxList
, Type
*DestTy
)
2872 : ConstantExpr(DestTy
, Instruction::GetElementPtr
,
2873 OperandTraits
<GetElementPtrConstantExpr
>::op_end(this) -
2874 (IdxList
.size() + 1),
2875 IdxList
.size() + 1),
2876 SrcElementTy(SrcElementTy
),
2877 ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy
, IdxList
)) {
2879 Use
*OperandList
= getOperandList();
2880 for (unsigned i
= 0, E
= IdxList
.size(); i
!= E
; ++i
)
2881 OperandList
[i
+1] = IdxList
[i
];
2884 Type
*GetElementPtrConstantExpr::getSourceElementType() const {
2885 return SrcElementTy
;
2888 Type
*GetElementPtrConstantExpr::getResultElementType() const {
2889 return ResElementTy
;
2892 //===----------------------------------------------------------------------===//
2893 // ConstantData* implementations
2895 Type
*ConstantDataSequential::getElementType() const {
2896 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(getType()))
2897 return ATy
->getElementType();
2898 return cast
<VectorType
>(getType())->getElementType();
2901 StringRef
ConstantDataSequential::getRawDataValues() const {
2902 return StringRef(DataElements
, getNumElements()*getElementByteSize());
2905 bool ConstantDataSequential::isElementTypeCompatible(Type
*Ty
) {
2906 if (Ty
->isHalfTy() || Ty
->isBFloatTy() || Ty
->isFloatTy() || Ty
->isDoubleTy())
2908 if (auto *IT
= dyn_cast
<IntegerType
>(Ty
)) {
2909 switch (IT
->getBitWidth()) {
2921 unsigned ConstantDataSequential::getNumElements() const {
2922 if (ArrayType
*AT
= dyn_cast
<ArrayType
>(getType()))
2923 return AT
->getNumElements();
2924 return cast
<FixedVectorType
>(getType())->getNumElements();
2928 uint64_t ConstantDataSequential::getElementByteSize() const {
2929 return getElementType()->getPrimitiveSizeInBits()/8;
2932 /// Return the start of the specified element.
2933 const char *ConstantDataSequential::getElementPointer(unsigned Elt
) const {
2934 assert(Elt
< getNumElements() && "Invalid Elt");
2935 return DataElements
+Elt
*getElementByteSize();
2939 /// Return true if the array is empty or all zeros.
2940 static bool isAllZeros(StringRef Arr
) {
2947 /// This is the underlying implementation of all of the
2948 /// ConstantDataSequential::get methods. They all thunk down to here, providing
2949 /// the correct element type. We take the bytes in as a StringRef because
2950 /// we *want* an underlying "char*" to avoid TBAA type punning violations.
2951 Constant
*ConstantDataSequential::getImpl(StringRef Elements
, Type
*Ty
) {
2953 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(Ty
))
2954 assert(isElementTypeCompatible(ATy
->getElementType()));
2956 assert(isElementTypeCompatible(cast
<VectorType
>(Ty
)->getElementType()));
2958 // If the elements are all zero or there are no elements, return a CAZ, which
2959 // is more dense and canonical.
2960 if (isAllZeros(Elements
))
2961 return ConstantAggregateZero::get(Ty
);
2963 // Do a lookup to see if we have already formed one of these.
2966 .pImpl
->CDSConstants
.insert(std::make_pair(Elements
, nullptr))
2969 // The bucket can point to a linked list of different CDS's that have the same
2970 // body but different types. For example, 0,0,0,1 could be a 4 element array
2971 // of i8, or a 1-element array of i32. They'll both end up in the same
2972 /// StringMap bucket, linked up by their Next pointers. Walk the list.
2973 std::unique_ptr
<ConstantDataSequential
> *Entry
= &Slot
.second
;
2974 for (; *Entry
; Entry
= &(*Entry
)->Next
)
2975 if ((*Entry
)->getType() == Ty
)
2976 return Entry
->get();
2978 // Okay, we didn't get a hit. Create a node of the right class, link it in,
2980 if (isa
<ArrayType
>(Ty
)) {
2981 // Use reset because std::make_unique can't access the constructor.
2982 Entry
->reset(new ConstantDataArray(Ty
, Slot
.first().data()));
2983 return Entry
->get();
2986 assert(isa
<VectorType
>(Ty
));
2987 // Use reset because std::make_unique can't access the constructor.
2988 Entry
->reset(new ConstantDataVector(Ty
, Slot
.first().data()));
2989 return Entry
->get();
2992 void ConstantDataSequential::destroyConstantImpl() {
2993 // Remove the constant from the StringMap.
2994 StringMap
<std::unique_ptr
<ConstantDataSequential
>> &CDSConstants
=
2995 getType()->getContext().pImpl
->CDSConstants
;
2997 auto Slot
= CDSConstants
.find(getRawDataValues());
2999 assert(Slot
!= CDSConstants
.end() && "CDS not found in uniquing table");
3001 std::unique_ptr
<ConstantDataSequential
> *Entry
= &Slot
->getValue();
3003 // Remove the entry from the hash table.
3004 if (!(*Entry
)->Next
) {
3005 // If there is only one value in the bucket (common case) it must be this
3006 // entry, and removing the entry should remove the bucket completely.
3007 assert(Entry
->get() == this && "Hash mismatch in ConstantDataSequential");
3008 getContext().pImpl
->CDSConstants
.erase(Slot
);
3012 // Otherwise, there are multiple entries linked off the bucket, unlink the
3013 // node we care about but keep the bucket around.
3015 std::unique_ptr
<ConstantDataSequential
> &Node
= *Entry
;
3016 assert(Node
&& "Didn't find entry in its uniquing hash table!");
3017 // If we found our entry, unlink it from the list and we're done.
3018 if (Node
.get() == this) {
3019 Node
= std::move(Node
->Next
);
3023 Entry
= &Node
->Next
;
3027 /// getFP() constructors - Return a constant of array type with a float
3028 /// element type taken from argument `ElementType', and count taken from
3029 /// argument `Elts'. The amount of bits of the contained type must match the
3030 /// number of bits of the type contained in the passed in ArrayRef.
3031 /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
3032 /// that this can return a ConstantAggregateZero object.
3033 Constant
*ConstantDataArray::getFP(Type
*ElementType
, ArrayRef
<uint16_t> Elts
) {
3034 assert((ElementType
->isHalfTy() || ElementType
->isBFloatTy()) &&
3035 "Element type is not a 16-bit float type");
3036 Type
*Ty
= ArrayType::get(ElementType
, Elts
.size());
3037 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
3038 return getImpl(StringRef(Data
, Elts
.size() * 2), Ty
);
3040 Constant
*ConstantDataArray::getFP(Type
*ElementType
, ArrayRef
<uint32_t> Elts
) {
3041 assert(ElementType
->isFloatTy() && "Element type is not a 32-bit float type");
3042 Type
*Ty
= ArrayType::get(ElementType
, Elts
.size());
3043 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
3044 return getImpl(StringRef(Data
, Elts
.size() * 4), Ty
);
3046 Constant
*ConstantDataArray::getFP(Type
*ElementType
, ArrayRef
<uint64_t> Elts
) {
3047 assert(ElementType
->isDoubleTy() &&
3048 "Element type is not a 64-bit float type");
3049 Type
*Ty
= ArrayType::get(ElementType
, Elts
.size());
3050 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
3051 return getImpl(StringRef(Data
, Elts
.size() * 8), Ty
);
3054 Constant
*ConstantDataArray::getString(LLVMContext
&Context
,
3055 StringRef Str
, bool AddNull
) {
3057 const uint8_t *Data
= Str
.bytes_begin();
3058 return get(Context
, makeArrayRef(Data
, Str
.size()));
3061 SmallVector
<uint8_t, 64> ElementVals
;
3062 ElementVals
.append(Str
.begin(), Str
.end());
3063 ElementVals
.push_back(0);
3064 return get(Context
, ElementVals
);
3067 /// get() constructors - Return a constant with vector type with an element
3068 /// count and element type matching the ArrayRef passed in. Note that this
3069 /// can return a ConstantAggregateZero object.
3070 Constant
*ConstantDataVector::get(LLVMContext
&Context
, ArrayRef
<uint8_t> Elts
){
3071 auto *Ty
= FixedVectorType::get(Type::getInt8Ty(Context
), Elts
.size());
3072 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
3073 return getImpl(StringRef(Data
, Elts
.size() * 1), Ty
);
3075 Constant
*ConstantDataVector::get(LLVMContext
&Context
, ArrayRef
<uint16_t> Elts
){
3076 auto *Ty
= FixedVectorType::get(Type::getInt16Ty(Context
), Elts
.size());
3077 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
3078 return getImpl(StringRef(Data
, Elts
.size() * 2), Ty
);
3080 Constant
*ConstantDataVector::get(LLVMContext
&Context
, ArrayRef
<uint32_t> Elts
){
3081 auto *Ty
= FixedVectorType::get(Type::getInt32Ty(Context
), Elts
.size());
3082 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
3083 return getImpl(StringRef(Data
, Elts
.size() * 4), Ty
);
3085 Constant
*ConstantDataVector::get(LLVMContext
&Context
, ArrayRef
<uint64_t> Elts
){
3086 auto *Ty
= FixedVectorType::get(Type::getInt64Ty(Context
), Elts
.size());
3087 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
3088 return getImpl(StringRef(Data
, Elts
.size() * 8), Ty
);
3090 Constant
*ConstantDataVector::get(LLVMContext
&Context
, ArrayRef
<float> Elts
) {
3091 auto *Ty
= FixedVectorType::get(Type::getFloatTy(Context
), Elts
.size());
3092 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
3093 return getImpl(StringRef(Data
, Elts
.size() * 4), Ty
);
3095 Constant
*ConstantDataVector::get(LLVMContext
&Context
, ArrayRef
<double> Elts
) {
3096 auto *Ty
= FixedVectorType::get(Type::getDoubleTy(Context
), Elts
.size());
3097 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
3098 return getImpl(StringRef(Data
, Elts
.size() * 8), Ty
);
3101 /// getFP() constructors - Return a constant of vector type with a float
3102 /// element type taken from argument `ElementType', and count taken from
3103 /// argument `Elts'. The amount of bits of the contained type must match the
3104 /// number of bits of the type contained in the passed in ArrayRef.
3105 /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
3106 /// that this can return a ConstantAggregateZero object.
3107 Constant
*ConstantDataVector::getFP(Type
*ElementType
,
3108 ArrayRef
<uint16_t> Elts
) {
3109 assert((ElementType
->isHalfTy() || ElementType
->isBFloatTy()) &&
3110 "Element type is not a 16-bit float type");
3111 auto *Ty
= FixedVectorType::get(ElementType
, Elts
.size());
3112 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
3113 return getImpl(StringRef(Data
, Elts
.size() * 2), Ty
);
3115 Constant
*ConstantDataVector::getFP(Type
*ElementType
,
3116 ArrayRef
<uint32_t> Elts
) {
3117 assert(ElementType
->isFloatTy() && "Element type is not a 32-bit float type");
3118 auto *Ty
= FixedVectorType::get(ElementType
, Elts
.size());
3119 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
3120 return getImpl(StringRef(Data
, Elts
.size() * 4), Ty
);
3122 Constant
*ConstantDataVector::getFP(Type
*ElementType
,
3123 ArrayRef
<uint64_t> Elts
) {
3124 assert(ElementType
->isDoubleTy() &&
3125 "Element type is not a 64-bit float type");
3126 auto *Ty
= FixedVectorType::get(ElementType
, Elts
.size());
3127 const char *Data
= reinterpret_cast<const char *>(Elts
.data());
3128 return getImpl(StringRef(Data
, Elts
.size() * 8), Ty
);
3131 Constant
*ConstantDataVector::getSplat(unsigned NumElts
, Constant
*V
) {
3132 assert(isElementTypeCompatible(V
->getType()) &&
3133 "Element type not compatible with ConstantData");
3134 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(V
)) {
3135 if (CI
->getType()->isIntegerTy(8)) {
3136 SmallVector
<uint8_t, 16> Elts(NumElts
, CI
->getZExtValue());
3137 return get(V
->getContext(), Elts
);
3139 if (CI
->getType()->isIntegerTy(16)) {
3140 SmallVector
<uint16_t, 16> Elts(NumElts
, CI
->getZExtValue());
3141 return get(V
->getContext(), Elts
);
3143 if (CI
->getType()->isIntegerTy(32)) {
3144 SmallVector
<uint32_t, 16> Elts(NumElts
, CI
->getZExtValue());
3145 return get(V
->getContext(), Elts
);
3147 assert(CI
->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
3148 SmallVector
<uint64_t, 16> Elts(NumElts
, CI
->getZExtValue());
3149 return get(V
->getContext(), Elts
);
3152 if (ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(V
)) {
3153 if (CFP
->getType()->isHalfTy()) {
3154 SmallVector
<uint16_t, 16> Elts(
3155 NumElts
, CFP
->getValueAPF().bitcastToAPInt().getLimitedValue());
3156 return getFP(V
->getType(), Elts
);
3158 if (CFP
->getType()->isBFloatTy()) {
3159 SmallVector
<uint16_t, 16> Elts(
3160 NumElts
, CFP
->getValueAPF().bitcastToAPInt().getLimitedValue());
3161 return getFP(V
->getType(), Elts
);
3163 if (CFP
->getType()->isFloatTy()) {
3164 SmallVector
<uint32_t, 16> Elts(
3165 NumElts
, CFP
->getValueAPF().bitcastToAPInt().getLimitedValue());
3166 return getFP(V
->getType(), Elts
);
3168 if (CFP
->getType()->isDoubleTy()) {
3169 SmallVector
<uint64_t, 16> Elts(
3170 NumElts
, CFP
->getValueAPF().bitcastToAPInt().getLimitedValue());
3171 return getFP(V
->getType(), Elts
);
3174 return ConstantVector::getSplat(ElementCount::getFixed(NumElts
), V
);
3178 uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt
) const {
3179 assert(isa
<IntegerType
>(getElementType()) &&
3180 "Accessor can only be used when element is an integer");
3181 const char *EltPtr
= getElementPointer(Elt
);
3183 // The data is stored in host byte order, make sure to cast back to the right
3184 // type to load with the right endianness.
3185 switch (getElementType()->getIntegerBitWidth()) {
3186 default: llvm_unreachable("Invalid bitwidth for CDS");
3188 return *reinterpret_cast<const uint8_t *>(EltPtr
);
3190 return *reinterpret_cast<const uint16_t *>(EltPtr
);
3192 return *reinterpret_cast<const uint32_t *>(EltPtr
);
3194 return *reinterpret_cast<const uint64_t *>(EltPtr
);
3198 APInt
ConstantDataSequential::getElementAsAPInt(unsigned Elt
) const {
3199 assert(isa
<IntegerType
>(getElementType()) &&
3200 "Accessor can only be used when element is an integer");
3201 const char *EltPtr
= getElementPointer(Elt
);
3203 // The data is stored in host byte order, make sure to cast back to the right
3204 // type to load with the right endianness.
3205 switch (getElementType()->getIntegerBitWidth()) {
3206 default: llvm_unreachable("Invalid bitwidth for CDS");
3208 auto EltVal
= *reinterpret_cast<const uint8_t *>(EltPtr
);
3209 return APInt(8, EltVal
);
3212 auto EltVal
= *reinterpret_cast<const uint16_t *>(EltPtr
);
3213 return APInt(16, EltVal
);
3216 auto EltVal
= *reinterpret_cast<const uint32_t *>(EltPtr
);
3217 return APInt(32, EltVal
);
3220 auto EltVal
= *reinterpret_cast<const uint64_t *>(EltPtr
);
3221 return APInt(64, EltVal
);
3226 APFloat
ConstantDataSequential::getElementAsAPFloat(unsigned Elt
) const {
3227 const char *EltPtr
= getElementPointer(Elt
);
3229 switch (getElementType()->getTypeID()) {
3231 llvm_unreachable("Accessor can only be used when element is float/double!");
3232 case Type::HalfTyID
: {
3233 auto EltVal
= *reinterpret_cast<const uint16_t *>(EltPtr
);
3234 return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal
));
3236 case Type::BFloatTyID
: {
3237 auto EltVal
= *reinterpret_cast<const uint16_t *>(EltPtr
);
3238 return APFloat(APFloat::BFloat(), APInt(16, EltVal
));
3240 case Type::FloatTyID
: {
3241 auto EltVal
= *reinterpret_cast<const uint32_t *>(EltPtr
);
3242 return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal
));
3244 case Type::DoubleTyID
: {
3245 auto EltVal
= *reinterpret_cast<const uint64_t *>(EltPtr
);
3246 return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal
));
3251 float ConstantDataSequential::getElementAsFloat(unsigned Elt
) const {
3252 assert(getElementType()->isFloatTy() &&
3253 "Accessor can only be used when element is a 'float'");
3254 return *reinterpret_cast<const float *>(getElementPointer(Elt
));
3257 double ConstantDataSequential::getElementAsDouble(unsigned Elt
) const {
3258 assert(getElementType()->isDoubleTy() &&
3259 "Accessor can only be used when element is a 'float'");
3260 return *reinterpret_cast<const double *>(getElementPointer(Elt
));
3263 Constant
*ConstantDataSequential::getElementAsConstant(unsigned Elt
) const {
3264 if (getElementType()->isHalfTy() || getElementType()->isBFloatTy() ||
3265 getElementType()->isFloatTy() || getElementType()->isDoubleTy())
3266 return ConstantFP::get(getContext(), getElementAsAPFloat(Elt
));
3268 return ConstantInt::get(getElementType(), getElementAsInteger(Elt
));
3271 bool ConstantDataSequential::isString(unsigned CharSize
) const {
3272 return isa
<ArrayType
>(getType()) && getElementType()->isIntegerTy(CharSize
);
3275 bool ConstantDataSequential::isCString() const {
3279 StringRef Str
= getAsString();
3281 // The last value must be nul.
3282 if (Str
.back() != 0) return false;
3284 // Other elements must be non-nul.
3285 return Str
.drop_back().find(0) == StringRef::npos
;
3288 bool ConstantDataVector::isSplatData() const {
3289 const char *Base
= getRawDataValues().data();
3291 // Compare elements 1+ to the 0'th element.
3292 unsigned EltSize
= getElementByteSize();
3293 for (unsigned i
= 1, e
= getNumElements(); i
!= e
; ++i
)
3294 if (memcmp(Base
, Base
+i
*EltSize
, EltSize
))
3300 bool ConstantDataVector::isSplat() const {
3303 IsSplat
= isSplatData();
3308 Constant
*ConstantDataVector::getSplatValue() const {
3309 // If they're all the same, return the 0th one as a representative.
3310 return isSplat() ? getElementAsConstant(0) : nullptr;
3313 //===----------------------------------------------------------------------===//
3314 // handleOperandChange implementations
3316 /// Update this constant array to change uses of
3317 /// 'From' to be uses of 'To'. This must update the uniquing data structures
3320 /// Note that we intentionally replace all uses of From with To here. Consider
3321 /// a large array that uses 'From' 1000 times. By handling this case all here,
3322 /// ConstantArray::handleOperandChange is only invoked once, and that
3323 /// single invocation handles all 1000 uses. Handling them one at a time would
3324 /// work, but would be really slow because it would have to unique each updated
3327 void Constant::handleOperandChange(Value
*From
, Value
*To
) {
3328 Value
*Replacement
= nullptr;
3329 switch (getValueID()) {
3331 llvm_unreachable("Not a constant!");
3332 #define HANDLE_CONSTANT(Name) \
3333 case Value::Name##Val: \
3334 Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
3336 #include "llvm/IR/Value.def"
3339 // If handleOperandChangeImpl returned nullptr, then it handled
3340 // replacing itself and we don't want to delete or replace anything else here.
3344 // I do need to replace this with an existing value.
3345 assert(Replacement
!= this && "I didn't contain From!");
3347 // Everyone using this now uses the replacement.
3348 replaceAllUsesWith(Replacement
);
3350 // Delete the old constant!
3354 Value
*ConstantArray::handleOperandChangeImpl(Value
*From
, Value
*To
) {
3355 assert(isa
<Constant
>(To
) && "Cannot make Constant refer to non-constant!");
3356 Constant
*ToC
= cast
<Constant
>(To
);
3358 SmallVector
<Constant
*, 8> Values
;
3359 Values
.reserve(getNumOperands()); // Build replacement array.
3361 // Fill values with the modified operands of the constant array. Also,
3362 // compute whether this turns into an all-zeros array.
3363 unsigned NumUpdated
= 0;
3365 // Keep track of whether all the values in the array are "ToC".
3366 bool AllSame
= true;
3367 Use
*OperandList
= getOperandList();
3368 unsigned OperandNo
= 0;
3369 for (Use
*O
= OperandList
, *E
= OperandList
+getNumOperands(); O
!= E
; ++O
) {
3370 Constant
*Val
= cast
<Constant
>(O
->get());
3372 OperandNo
= (O
- OperandList
);
3376 Values
.push_back(Val
);
3377 AllSame
&= Val
== ToC
;
3380 if (AllSame
&& ToC
->isNullValue())
3381 return ConstantAggregateZero::get(getType());
3383 if (AllSame
&& isa
<UndefValue
>(ToC
))
3384 return UndefValue::get(getType());
3386 // Check for any other type of constant-folding.
3387 if (Constant
*C
= getImpl(getType(), Values
))
3390 // Update to the new value.
3391 return getContext().pImpl
->ArrayConstants
.replaceOperandsInPlace(
3392 Values
, this, From
, ToC
, NumUpdated
, OperandNo
);
3395 Value
*ConstantStruct::handleOperandChangeImpl(Value
*From
, Value
*To
) {
3396 assert(isa
<Constant
>(To
) && "Cannot make Constant refer to non-constant!");
3397 Constant
*ToC
= cast
<Constant
>(To
);
3399 Use
*OperandList
= getOperandList();
3401 SmallVector
<Constant
*, 8> Values
;
3402 Values
.reserve(getNumOperands()); // Build replacement struct.
3404 // Fill values with the modified operands of the constant struct. Also,
3405 // compute whether this turns into an all-zeros struct.
3406 unsigned NumUpdated
= 0;
3407 bool AllSame
= true;
3408 unsigned OperandNo
= 0;
3409 for (Use
*O
= OperandList
, *E
= OperandList
+ getNumOperands(); O
!= E
; ++O
) {
3410 Constant
*Val
= cast
<Constant
>(O
->get());
3412 OperandNo
= (O
- OperandList
);
3416 Values
.push_back(Val
);
3417 AllSame
&= Val
== ToC
;
3420 if (AllSame
&& ToC
->isNullValue())
3421 return ConstantAggregateZero::get(getType());
3423 if (AllSame
&& isa
<UndefValue
>(ToC
))
3424 return UndefValue::get(getType());
3426 // Update to the new value.
3427 return getContext().pImpl
->StructConstants
.replaceOperandsInPlace(
3428 Values
, this, From
, ToC
, NumUpdated
, OperandNo
);
3431 Value
*ConstantVector::handleOperandChangeImpl(Value
*From
, Value
*To
) {
3432 assert(isa
<Constant
>(To
) && "Cannot make Constant refer to non-constant!");
3433 Constant
*ToC
= cast
<Constant
>(To
);
3435 SmallVector
<Constant
*, 8> Values
;
3436 Values
.reserve(getNumOperands()); // Build replacement array...
3437 unsigned NumUpdated
= 0;
3438 unsigned OperandNo
= 0;
3439 for (unsigned i
= 0, e
= getNumOperands(); i
!= e
; ++i
) {
3440 Constant
*Val
= getOperand(i
);
3446 Values
.push_back(Val
);
3449 if (Constant
*C
= getImpl(Values
))
3452 // Update to the new value.
3453 return getContext().pImpl
->VectorConstants
.replaceOperandsInPlace(
3454 Values
, this, From
, ToC
, NumUpdated
, OperandNo
);
3457 Value
*ConstantExpr::handleOperandChangeImpl(Value
*From
, Value
*ToV
) {
3458 assert(isa
<Constant
>(ToV
) && "Cannot make Constant refer to non-constant!");
3459 Constant
*To
= cast
<Constant
>(ToV
);
3461 SmallVector
<Constant
*, 8> NewOps
;
3462 unsigned NumUpdated
= 0;
3463 unsigned OperandNo
= 0;
3464 for (unsigned i
= 0, e
= getNumOperands(); i
!= e
; ++i
) {
3465 Constant
*Op
= getOperand(i
);
3471 NewOps
.push_back(Op
);
3473 assert(NumUpdated
&& "I didn't contain From!");
3475 if (Constant
*C
= getWithOperands(NewOps
, getType(), true))
3478 // Update to the new value.
3479 return getContext().pImpl
->ExprConstants
.replaceOperandsInPlace(
3480 NewOps
, this, From
, To
, NumUpdated
, OperandNo
);
3483 Instruction
*ConstantExpr::getAsInstruction() const {
3484 SmallVector
<Value
*, 4> ValueOperands(operands());
3485 ArrayRef
<Value
*> Ops(ValueOperands
);
3487 switch (getOpcode()) {
3488 case Instruction::Trunc
:
3489 case Instruction::ZExt
:
3490 case Instruction::SExt
:
3491 case Instruction::FPTrunc
:
3492 case Instruction::FPExt
:
3493 case Instruction::UIToFP
:
3494 case Instruction::SIToFP
:
3495 case Instruction::FPToUI
:
3496 case Instruction::FPToSI
:
3497 case Instruction::PtrToInt
:
3498 case Instruction::IntToPtr
:
3499 case Instruction::BitCast
:
3500 case Instruction::AddrSpaceCast
:
3501 return CastInst::Create((Instruction::CastOps
)getOpcode(),
3503 case Instruction::Select
:
3504 return SelectInst::Create(Ops
[0], Ops
[1], Ops
[2]);
3505 case Instruction::InsertElement
:
3506 return InsertElementInst::Create(Ops
[0], Ops
[1], Ops
[2]);
3507 case Instruction::ExtractElement
:
3508 return ExtractElementInst::Create(Ops
[0], Ops
[1]);
3509 case Instruction::InsertValue
:
3510 return InsertValueInst::Create(Ops
[0], Ops
[1], getIndices());
3511 case Instruction::ExtractValue
:
3512 return ExtractValueInst::Create(Ops
[0], getIndices());
3513 case Instruction::ShuffleVector
:
3514 return new ShuffleVectorInst(Ops
[0], Ops
[1], getShuffleMask());
3516 case Instruction::GetElementPtr
: {
3517 const auto *GO
= cast
<GEPOperator
>(this);
3518 if (GO
->isInBounds())
3519 return GetElementPtrInst::CreateInBounds(GO
->getSourceElementType(),
3520 Ops
[0], Ops
.slice(1));
3521 return GetElementPtrInst::Create(GO
->getSourceElementType(), Ops
[0],
3524 case Instruction::ICmp
:
3525 case Instruction::FCmp
:
3526 return CmpInst::Create((Instruction::OtherOps
)getOpcode(),
3527 (CmpInst::Predicate
)getPredicate(), Ops
[0], Ops
[1]);
3528 case Instruction::FNeg
:
3529 return UnaryOperator::Create((Instruction::UnaryOps
)getOpcode(), Ops
[0]);
3531 assert(getNumOperands() == 2 && "Must be binary operator?");
3532 BinaryOperator
*BO
=
3533 BinaryOperator::Create((Instruction::BinaryOps
)getOpcode(),
3535 if (isa
<OverflowingBinaryOperator
>(BO
)) {
3536 BO
->setHasNoUnsignedWrap(SubclassOptionalData
&
3537 OverflowingBinaryOperator::NoUnsignedWrap
);
3538 BO
->setHasNoSignedWrap(SubclassOptionalData
&
3539 OverflowingBinaryOperator::NoSignedWrap
);
3541 if (isa
<PossiblyExactOperator
>(BO
))
3542 BO
->setIsExact(SubclassOptionalData
& PossiblyExactOperator::IsExact
);