1//===-- Constants.cpp - Implement Constant nodes --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Constant* classes.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/IR/Constants.h"
14#include "LLVMContextImpl.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/IR/BasicBlock.h"
19#include "llvm/IR/ConstantFold.h"
20#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/GetElementPtrTypeIterator.h"
23#include "llvm/IR/GlobalAlias.h"
24#include "llvm/IR/GlobalIFunc.h"
25#include "llvm/IR/GlobalValue.h"
26#include "llvm/IR/GlobalVariable.h"
27#include "llvm/IR/Instructions.h"
28#include "llvm/IR/Operator.h"
29#include "llvm/IR/PatternMatch.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/MathExtras.h"
32#include "llvm/Support/raw_ostream.h"
33#include <algorithm>
34
35using namespace llvm;
36using namespace PatternMatch;
37
38// As set of temporary options to help migrate how splats are represented.
39static cl::opt<bool> UseConstantIntForFixedLengthSplat(
40 "use-constant-int-for-fixed-length-splat", cl::init(Val: false), cl::Hidden,
41 cl::desc("Use ConstantInt's native fixed-length vector splat support."));
42static cl::opt<bool> UseConstantIntForScalableSplat(
43 "use-constant-int-for-scalable-splat", cl::init(Val: false), cl::Hidden,
44 cl::desc("Use ConstantInt's native scalable vector splat support."));
45
46//===----------------------------------------------------------------------===//
47// Constant Class
48//===----------------------------------------------------------------------===//
49
50bool Constant::isNegativeZeroValue() const {
51 // Floating point values have an explicit -0.0 value.
52 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this))
53 return CFP->isZero() && CFP->isNegative();
54
55 // Equivalent for a vector of -0.0's.
56 if (getType()->isVectorTy())
57 if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(Val: getSplatValue()))
58 return SplatCFP->isNegativeZeroValue();
59
60 // We've already handled true FP case; any other FP vectors can't represent -0.0.
61 if (getType()->isFPOrFPVectorTy())
62 return false;
63
64 // Otherwise, just use +0.0.
65 return isNullValue();
66}
67
68bool Constant::isAllOnesValue() const {
69 // Check for -1 integers
70 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this))
71 return CI->isMinusOne();
72
73 // Check for MaxValue bytes
74 if (const ConstantByte *CB = dyn_cast<ConstantByte>(Val: this))
75 return CB->isMinusOne();
76
77 // Check for FP which are bitcasted from -1 integers
78 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this))
79 return CFP->getValueAPF().bitcastToAPInt().isAllOnes();
80
81 // Check for constant splat vectors of 1 values.
82 if (getType()->isVectorTy())
83 if (const auto *SplatVal = getSplatValue())
84 return SplatVal->isAllOnesValue();
85
86 return false;
87}
88
89bool Constant::isOneValue() const {
90 // Check for 1 integers
91 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this))
92 return CI->isOne();
93
94 // Check for 1 bytes
95 if (const ConstantByte *CB = dyn_cast<ConstantByte>(Val: this))
96 return CB->isOne();
97
98 // Check for FP which are bitcasted from 1 integers
99 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this))
100 return CFP->getValueAPF().bitcastToAPInt().isOne();
101
102 // Check for constant splat vectors of 1 values.
103 if (getType()->isVectorTy())
104 if (const auto *SplatVal = getSplatValue())
105 return SplatVal->isOneValue();
106
107 return false;
108}
109
110bool Constant::isNotOneValue() const {
111 // Check for 1 integers
112 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this))
113 return !CI->isOneValue();
114
115 // Check for 1 bytes
116 if (const ConstantByte *CB = dyn_cast<ConstantByte>(Val: this))
117 return !CB->isOneValue();
118
119 // Check for FP which are bitcasted from 1 integers
120 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this))
121 return !CFP->getValueAPF().bitcastToAPInt().isOne();
122
123 // Check that vectors don't contain 1
124 if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) {
125 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
126 Constant *Elt = getAggregateElement(Elt: I);
127 if (!Elt || !Elt->isNotOneValue())
128 return false;
129 }
130 return true;
131 }
132
133 // Check for splats that don't contain 1
134 if (getType()->isVectorTy())
135 if (const auto *SplatVal = getSplatValue())
136 return SplatVal->isNotOneValue();
137
138 // It *may* contain 1, we can't tell.
139 return false;
140}
141
142bool Constant::isMinSignedValue() const {
143 // Check for INT_MIN integers
144 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this))
145 return CI->isMinValue(/*isSigned=*/IsSigned: true);
146
147 // Check for FP which are bitcasted from INT_MIN integers
148 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this))
149 return CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
150
151 // Check for splats of INT_MIN values.
152 if (getType()->isVectorTy())
153 if (const auto *SplatVal = getSplatValue())
154 return SplatVal->isMinSignedValue();
155
156 return false;
157}
158
159bool Constant::isMaxSignedValue() const {
160 // Check for INT_MAX integers
161 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this))
162 return CI->isMaxValue(/*isSigned=*/IsSigned: true);
163
164 // Check for FP which are bitcasted from INT_MAX integers
165 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this))
166 return CFP->getValueAPF().bitcastToAPInt().isMaxSignedValue();
167
168 // Check for splats of INT_MAX values.
169 if (getType()->isVectorTy())
170 if (const auto *SplatVal = getSplatValue())
171 return SplatVal->isMaxSignedValue();
172
173 return false;
174}
175
176bool Constant::isNotMinSignedValue() const {
177 // Check for INT_MIN integers
178 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this))
179 return !CI->isMinValue(/*isSigned=*/IsSigned: true);
180
181 // Check for FP which are bitcasted from INT_MIN integers
182 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this))
183 return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
184
185 // Check that vectors don't contain INT_MIN
186 if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) {
187 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
188 Constant *Elt = getAggregateElement(Elt: I);
189 if (!Elt || !Elt->isNotMinSignedValue())
190 return false;
191 }
192 return true;
193 }
194
195 // Check for splats that aren't INT_MIN
196 if (getType()->isVectorTy())
197 if (const auto *SplatVal = getSplatValue())
198 return SplatVal->isNotMinSignedValue();
199
200 // It *may* contain INT_MIN, we can't tell.
201 return false;
202}
203
204bool Constant::isFiniteNonZeroFP() const {
205 if (auto *CFP = dyn_cast<ConstantFP>(Val: this))
206 return CFP->getValueAPF().isFiniteNonZero();
207
208 if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) {
209 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
210 auto *CFP = dyn_cast_or_null<ConstantFP>(Val: getAggregateElement(Elt: I));
211 if (!CFP || !CFP->getValueAPF().isFiniteNonZero())
212 return false;
213 }
214 return true;
215 }
216
217 if (getType()->isVectorTy())
218 if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(Val: getSplatValue()))
219 return SplatCFP->isFiniteNonZeroFP();
220
221 // It *may* contain finite non-zero, we can't tell.
222 return false;
223}
224
225bool Constant::isNormalFP() const {
226 if (auto *CFP = dyn_cast<ConstantFP>(Val: this))
227 return CFP->getValueAPF().isNormal();
228
229 if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) {
230 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
231 auto *CFP = dyn_cast_or_null<ConstantFP>(Val: getAggregateElement(Elt: I));
232 if (!CFP || !CFP->getValueAPF().isNormal())
233 return false;
234 }
235 return true;
236 }
237
238 if (getType()->isVectorTy())
239 if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(Val: getSplatValue()))
240 return SplatCFP->isNormalFP();
241
242 // It *may* contain a normal fp value, we can't tell.
243 return false;
244}
245
246bool Constant::hasExactInverseFP() const {
247 if (auto *CFP = dyn_cast<ConstantFP>(Val: this))
248 return CFP->getValueAPF().getExactInverse(Inv: nullptr);
249
250 if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) {
251 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
252 auto *CFP = dyn_cast_or_null<ConstantFP>(Val: getAggregateElement(Elt: I));
253 if (!CFP || !CFP->getValueAPF().getExactInverse(Inv: nullptr))
254 return false;
255 }
256 return true;
257 }
258
259 if (getType()->isVectorTy())
260 if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(Val: getSplatValue()))
261 return SplatCFP->hasExactInverseFP();
262
263 // It *may* have an exact inverse fp value, we can't tell.
264 return false;
265}
266
267bool Constant::isNaN() const {
268 if (auto *CFP = dyn_cast<ConstantFP>(Val: this))
269 return CFP->isNaN();
270
271 if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) {
272 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
273 auto *CFP = dyn_cast_or_null<ConstantFP>(Val: getAggregateElement(Elt: I));
274 if (!CFP || !CFP->isNaN())
275 return false;
276 }
277 return true;
278 }
279
280 if (getType()->isVectorTy())
281 if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(Val: getSplatValue()))
282 return SplatCFP->isNaN();
283
284 // It *may* be NaN, we can't tell.
285 return false;
286}
287
288bool Constant::isElementWiseEqual(Value *Y) const {
289 // Are they fully identical?
290 if (this == Y)
291 return true;
292
293 // The input value must be a vector constant with the same type.
294 auto *VTy = dyn_cast<VectorType>(Val: getType());
295 if (!isa<Constant>(Val: Y) || !VTy || VTy != Y->getType())
296 return false;
297
298 // TODO: Compare pointer constants?
299 if (!(VTy->getElementType()->isIntegerTy() ||
300 VTy->getElementType()->isFloatingPointTy()))
301 return false;
302
303 // They may still be identical element-wise (if they have `undef`s).
304 // Bitcast to integer to allow exact bitwise comparison for all types.
305 Type *IntTy = VectorType::getInteger(VTy);
306 Constant *C0 = ConstantExpr::getBitCast(C: const_cast<Constant *>(this), Ty: IntTy);
307 Constant *C1 = ConstantExpr::getBitCast(C: cast<Constant>(Val: Y), Ty: IntTy);
308 Constant *CmpEq = ConstantFoldCompareInstruction(Predicate: ICmpInst::ICMP_EQ, C1: C0, C2: C1);
309 return CmpEq && (isa<PoisonValue>(Val: CmpEq) || match(V: CmpEq, P: m_One()));
310}
311
312static std::optional<unsigned> getNumWalkableElements(Type *Ty) {
313 if (auto *FVTy = dyn_cast<FixedVectorType>(Val: Ty))
314 return FVTy->getNumElements();
315 if (auto *STy = dyn_cast<StructType>(Val: Ty))
316 return STy->getNumElements();
317 if (auto *ATy = dyn_cast<ArrayType>(Val: Ty))
318 return ATy->getNumElements();
319 return std::nullopt;
320}
321
322static bool
323containsMatchingElement(const Constant *C,
324 function_ref<bool(const Constant *)> PredFn) {
325 // Simple pruning for large size array. UndefValue is fine as it is filtered
326 // out by PredFn already.
327 if (isa<ConstantData>(Val: C))
328 return false;
329
330 std::optional<unsigned> NumElts = getNumWalkableElements(Ty: C->getType());
331 if (!NumElts)
332 return false;
333
334 for (unsigned I = 0; I != *NumElts; ++I) {
335 Constant *Elt = C->getAggregateElement(Elt: I);
336 if (Elt && (PredFn(Elt) || containsMatchingElement(C: Elt, PredFn)))
337 return true;
338 }
339 return false;
340}
341
342static bool
343containsUndefinedElement(const Constant *C,
344 function_ref<bool(const Constant *)> HasFn) {
345 Type *Ty = C->getType();
346 if (!Ty->isVectorTy() && !Ty->isAggregateType())
347 return false;
348
349 if (HasFn(C))
350 return true;
351
352 return containsMatchingElement(C, PredFn: HasFn);
353}
354
355bool Constant::containsUndefOrPoisonElement() const {
356 return containsUndefinedElement(
357 C: this, HasFn: [&](const auto *C) { return isa<UndefValue>(C); });
358}
359
360bool Constant::containsPoisonElement() const {
361 return containsUndefinedElement(
362 C: this, HasFn: [&](const auto *C) { return isa<PoisonValue>(C); });
363}
364
365bool Constant::containsUndefElement() const {
366 return containsUndefinedElement(C: this, HasFn: [&](const auto *C) {
367 return isa<UndefValue>(C) && !isa<PoisonValue>(C);
368 });
369}
370
371bool Constant::containsConstantExpression() const {
372 if (isa<ConstantInt>(Val: this) || isa<ConstantFP>(Val: this))
373 return false;
374
375 return containsMatchingElement(C: this, PredFn: IsaPred<ConstantExpr>);
376}
377
378bool Constant::containsMatchingVectorElement(
379 function_ref<bool(Constant *)> PredFn) const {
380 auto *FVTy = dyn_cast<FixedVectorType>(Val: getType());
381 if (!FVTy)
382 return false;
383
384 unsigned NumElts = FVTy->getNumElements();
385 for (unsigned I = 0; I != NumElts; ++I) {
386 Constant *Elem = getAggregateElement(Elt: I);
387 if (Elem && PredFn(Elem))
388 return true;
389 }
390
391 return false;
392}
393
394/// Constructor to create a '0' constant of arbitrary type.
395Constant *Constant::getNullValue(Type *Ty) {
396 switch (Ty->getTypeID()) {
397 case Type::ByteTyID:
398 return ConstantByte::get(Ty, V: 0);
399 case Type::IntegerTyID:
400 return ConstantInt::get(Ty, V: 0);
401 case Type::HalfTyID:
402 case Type::BFloatTyID:
403 case Type::FloatTyID:
404 case Type::DoubleTyID:
405 case Type::X86_FP80TyID:
406 case Type::FP128TyID:
407 case Type::PPC_FP128TyID:
408 return ConstantFP::get(Context&: Ty->getContext(),
409 V: APFloat::getZero(Sem: Ty->getFltSemantics()));
410 case Type::PointerTyID:
411 return ConstantPointerNull::get(T: cast<PointerType>(Val: Ty));
412 case Type::FixedVectorTyID:
413 case Type::ScalableVectorTyID: {
414 Type *EltTy = cast<VectorType>(Val: Ty)->getElementType();
415 if (EltTy->isFloatingPointTy())
416 return ConstantFP::get(Ty, V: APFloat::getZero(Sem: EltTy->getFltSemantics()));
417 if (EltTy->isPointerTy())
418 return ConstantPointerNull::get(T: Ty);
419 return ConstantAggregateZero::get(Ty);
420 }
421 case Type::StructTyID:
422 case Type::ArrayTyID:
423 return ConstantAggregateZero::get(Ty);
424 case Type::TokenTyID:
425 return ConstantTokenNone::get(Context&: Ty->getContext());
426 case Type::TargetExtTyID:
427 return ConstantTargetNone::get(T: cast<TargetExtType>(Val: Ty));
428 default:
429 // Function, Label, or Opaque type?
430 llvm_unreachable("Cannot create a null constant of that type!");
431 }
432}
433
434Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
435 Type *ScalarTy = Ty->getScalarType();
436
437 // Create the base integer constant.
438 Constant *C = ConstantInt::get(Context&: Ty->getContext(), V);
439
440 // Convert an integer to a pointer, if necessary.
441 if (PointerType *PTy = dyn_cast<PointerType>(Val: ScalarTy))
442 C = ConstantExpr::getIntToPtr(C, Ty: PTy);
443
444 // Convert an integer to a byte, if necessary.
445 if (ByteType *BTy = dyn_cast<ByteType>(Val: ScalarTy))
446 C = ConstantExpr::getBitCast(C, Ty: BTy);
447
448 // Broadcast a scalar to a vector, if necessary.
449 if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty))
450 C = ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C);
451
452 return C;
453}
454
455Constant *Constant::getAllOnesValue(Type *Ty) {
456 if (IntegerType *ITy = dyn_cast<IntegerType>(Val: Ty))
457 return ConstantInt::get(Context&: Ty->getContext(),
458 V: APInt::getAllOnes(numBits: ITy->getBitWidth()));
459
460 if (Ty->isFloatingPointTy()) {
461 APFloat FL = APFloat::getAllOnesValue(Semantics: Ty->getFltSemantics());
462 return ConstantFP::get(Context&: Ty->getContext(), V: FL);
463 }
464
465 if (ByteType *BTy = dyn_cast<ByteType>(Val: Ty))
466 return ConstantByte::get(Context&: Ty->getContext(),
467 V: APInt::getAllOnes(numBits: BTy->getBitWidth()));
468
469 VectorType *VTy = cast<VectorType>(Val: Ty);
470 return ConstantVector::getSplat(EC: VTy->getElementCount(),
471 Elt: getAllOnesValue(Ty: VTy->getElementType()));
472}
473
474Constant *Constant::getAggregateElement(unsigned Elt) const {
475 assert((getType()->isAggregateType() || getType()->isVectorTy()) &&
476 "Must be an aggregate/vector constant");
477
478 if (const auto *CC = dyn_cast<ConstantAggregate>(Val: this))
479 return Elt < CC->getNumOperands() ? CC->getOperand(i_nocapture: Elt) : nullptr;
480
481 if (const auto *CAZ = dyn_cast<ConstantAggregateZero>(Val: this))
482 return Elt < CAZ->getElementCount().getKnownMinValue()
483 ? CAZ->getElementValue(Idx: Elt)
484 : nullptr;
485
486 if (const auto *CI = dyn_cast<ConstantInt>(Val: this))
487 return Elt < cast<VectorType>(Val: getType())
488 ->getElementCount()
489 .getKnownMinValue()
490 ? ConstantInt::get(Context&: getContext(), V: CI->getValue())
491 : nullptr;
492
493 if (const auto *CB = dyn_cast<ConstantByte>(Val: this))
494 return Elt < cast<VectorType>(Val: getType())
495 ->getElementCount()
496 .getKnownMinValue()
497 ? ConstantByte::get(Context&: getContext(), V: CB->getValue())
498 : nullptr;
499
500 if (const auto *CFP = dyn_cast<ConstantFP>(Val: this))
501 return Elt < cast<VectorType>(Val: getType())
502 ->getElementCount()
503 .getKnownMinValue()
504 ? ConstantFP::get(Context&: getContext(), V: CFP->getValue())
505 : nullptr;
506
507 if (isa<ConstantPointerNull>(Val: this)) {
508 auto *VT = cast<VectorType>(Val: getType());
509 return Elt < VT->getElementCount().getKnownMinValue()
510 ? ConstantPointerNull::get(T: VT->getElementType())
511 : nullptr;
512 }
513
514 // FIXME: getNumElements() will fail for non-fixed vector types.
515 if (isa<ScalableVectorType>(Val: getType()))
516 return nullptr;
517
518 if (const auto *PV = dyn_cast<PoisonValue>(Val: this))
519 return Elt < PV->getNumElements() ? PV->getElementValue(Idx: Elt) : nullptr;
520
521 if (const auto *UV = dyn_cast<UndefValue>(Val: this))
522 return Elt < UV->getNumElements() ? UV->getElementValue(Idx: Elt) : nullptr;
523
524 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Val: this))
525 return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(i: Elt)
526 : nullptr;
527
528 return nullptr;
529}
530
531Constant *Constant::getAggregateElement(Constant *Elt) const {
532 assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer");
533 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: Elt)) {
534 // Check if the constant fits into an uint64_t.
535 if (CI->getValue().getActiveBits() > 64)
536 return nullptr;
537 return getAggregateElement(Elt: CI->getZExtValue());
538 }
539 return nullptr;
540}
541
542void Constant::destroyConstant() {
543 /// First call destroyConstantImpl on the subclass. This gives the subclass
544 /// a chance to remove the constant from any maps/pools it's contained in.
545 switch (getValueID()) {
546 default:
547 llvm_unreachable("Not a constant!");
548#define HANDLE_CONSTANT(Name) \
549 case Value::Name##Val: \
550 cast<Name>(this)->destroyConstantImpl(); \
551 break;
552#include "llvm/IR/Value.def"
553 }
554
555 // When a Constant is destroyed, there may be lingering
556 // references to the constant by other constants in the constant pool. These
557 // constants are implicitly dependent on the module that is being deleted,
558 // but they don't know that. Because we only find out when the CPV is
559 // deleted, we must now notify all of our users (that should only be
560 // Constants) that they are, in fact, invalid now and should be deleted.
561 //
562 while (!use_empty()) {
563 Value *V = user_back();
564#ifndef NDEBUG // Only in -g mode...
565 if (!isa<Constant>(V)) {
566 dbgs() << "While deleting: " << *this
567 << "\n\nUse still stuck around after Def is destroyed: " << *V
568 << "\n\n";
569 }
570#endif
571 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
572 cast<Constant>(Val: V)->destroyConstant();
573
574 // The constant should remove itself from our use list...
575 assert((use_empty() || user_back() != V) && "Constant not removed!");
576 }
577
578 // Value has no outstanding references it is safe to delete it now...
579 deleteConstant(C: this);
580}
581
582void llvm::deleteConstant(Constant *C) {
583 switch (C->getValueID()) {
584 case Constant::ConstantIntVal:
585 delete static_cast<ConstantInt *>(C);
586 break;
587 case Constant::ConstantByteVal:
588 delete static_cast<ConstantByte *>(C);
589 break;
590 case Constant::ConstantFPVal:
591 delete static_cast<ConstantFP *>(C);
592 break;
593 case Constant::ConstantAggregateZeroVal:
594 delete static_cast<ConstantAggregateZero *>(C);
595 break;
596 case Constant::ConstantArrayVal:
597 delete static_cast<ConstantArray *>(C);
598 break;
599 case Constant::ConstantStructVal:
600 delete static_cast<ConstantStruct *>(C);
601 break;
602 case Constant::ConstantVectorVal:
603 delete static_cast<ConstantVector *>(C);
604 break;
605 case Constant::ConstantPointerNullVal:
606 delete static_cast<ConstantPointerNull *>(C);
607 break;
608 case Constant::ConstantDataArrayVal:
609 delete static_cast<ConstantDataArray *>(C);
610 break;
611 case Constant::ConstantDataVectorVal:
612 delete static_cast<ConstantDataVector *>(C);
613 break;
614 case Constant::ConstantTokenNoneVal:
615 delete static_cast<ConstantTokenNone *>(C);
616 break;
617 case Constant::BlockAddressVal:
618 delete static_cast<BlockAddress *>(C);
619 break;
620 case Constant::DSOLocalEquivalentVal:
621 delete static_cast<DSOLocalEquivalent *>(C);
622 break;
623 case Constant::NoCFIValueVal:
624 delete static_cast<NoCFIValue *>(C);
625 break;
626 case Constant::ConstantPtrAuthVal:
627 delete static_cast<ConstantPtrAuth *>(C);
628 break;
629 case Constant::UndefValueVal:
630 delete static_cast<UndefValue *>(C);
631 break;
632 case Constant::PoisonValueVal:
633 delete static_cast<PoisonValue *>(C);
634 break;
635 case Constant::ConstantExprVal:
636 if (isa<CastConstantExpr>(Val: C))
637 delete static_cast<CastConstantExpr *>(C);
638 else if (isa<BinaryConstantExpr>(Val: C))
639 delete static_cast<BinaryConstantExpr *>(C);
640 else if (isa<ExtractElementConstantExpr>(Val: C))
641 delete static_cast<ExtractElementConstantExpr *>(C);
642 else if (isa<InsertElementConstantExpr>(Val: C))
643 delete static_cast<InsertElementConstantExpr *>(C);
644 else if (isa<ShuffleVectorConstantExpr>(Val: C))
645 delete static_cast<ShuffleVectorConstantExpr *>(C);
646 else if (isa<GetElementPtrConstantExpr>(Val: C))
647 delete static_cast<GetElementPtrConstantExpr *>(C);
648 else
649 llvm_unreachable("Unexpected constant expr");
650 break;
651 default:
652 llvm_unreachable("Unexpected constant");
653 }
654}
655
656/// Check if C contains a GlobalValue for which Predicate is true.
657static bool
658ConstHasGlobalValuePredicate(const Constant *C,
659 bool (*Predicate)(const GlobalValue *)) {
660 SmallPtrSet<const Constant *, 8> Visited;
661 SmallVector<const Constant *, 8> WorkList;
662 WorkList.push_back(Elt: C);
663 Visited.insert(Ptr: C);
664
665 while (!WorkList.empty()) {
666 const Constant *WorkItem = WorkList.pop_back_val();
667 if (const auto *GV = dyn_cast<GlobalValue>(Val: WorkItem))
668 if (Predicate(GV))
669 return true;
670 for (const Value *Op : WorkItem->operands()) {
671 const Constant *ConstOp = dyn_cast<Constant>(Val: Op);
672 if (!ConstOp)
673 continue;
674 if (Visited.insert(Ptr: ConstOp).second)
675 WorkList.push_back(Elt: ConstOp);
676 }
677 }
678 return false;
679}
680
681bool Constant::isThreadDependent() const {
682 auto DLLImportPredicate = [](const GlobalValue *GV) {
683 return GV->isThreadLocal();
684 };
685 return ConstHasGlobalValuePredicate(C: this, Predicate: DLLImportPredicate);
686}
687
688bool Constant::isDLLImportDependent() const {
689 auto DLLImportPredicate = [](const GlobalValue *GV) {
690 return GV->hasDLLImportStorageClass();
691 };
692 return ConstHasGlobalValuePredicate(C: this, Predicate: DLLImportPredicate);
693}
694
695bool Constant::isConstantUsed() const {
696 for (const User *U : users()) {
697 const Constant *UC = dyn_cast<Constant>(Val: U);
698 if (!UC || isa<GlobalValue>(Val: UC))
699 return true;
700
701 if (UC->isConstantUsed())
702 return true;
703 }
704 return false;
705}
706
707bool Constant::needsDynamicRelocation() const {
708 return getRelocationInfo() == GlobalRelocation;
709}
710
711bool Constant::needsRelocation() const {
712 return getRelocationInfo() != NoRelocation;
713}
714
715Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
716 if (isa<GlobalValue>(Val: this))
717 return GlobalRelocation; // Global reference.
718
719 if (const BlockAddress *BA = dyn_cast<BlockAddress>(Val: this))
720 return BA->getFunction()->getRelocationInfo();
721
722 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Val: this)) {
723 if (CE->getOpcode() == Instruction::Sub) {
724 ConstantExpr *LHS = dyn_cast<ConstantExpr>(Val: CE->getOperand(i_nocapture: 0));
725 ConstantExpr *RHS = dyn_cast<ConstantExpr>(Val: CE->getOperand(i_nocapture: 1));
726 if (LHS && RHS &&
727 (LHS->getOpcode() == Instruction::PtrToInt ||
728 LHS->getOpcode() == Instruction::PtrToAddr) &&
729 (RHS->getOpcode() == Instruction::PtrToInt ||
730 RHS->getOpcode() == Instruction::PtrToAddr)) {
731 Constant *LHSOp0 = LHS->getOperand(i_nocapture: 0);
732 Constant *RHSOp0 = RHS->getOperand(i_nocapture: 0);
733
734 // While raw uses of blockaddress need to be relocated, differences
735 // between two of them don't when they are for labels in the same
736 // function. This is a common idiom when creating a table for the
737 // indirect goto extension, so we handle it efficiently here.
738 if (isa<BlockAddress>(Val: LHSOp0) && isa<BlockAddress>(Val: RHSOp0) &&
739 cast<BlockAddress>(Val: LHSOp0)->getFunction() ==
740 cast<BlockAddress>(Val: RHSOp0)->getFunction())
741 return NoRelocation;
742
743 // Relative pointers do not need to be dynamically relocated.
744 if (auto *RHSGV =
745 dyn_cast<GlobalValue>(Val: RHSOp0->stripInBoundsConstantOffsets())) {
746 auto *LHS = LHSOp0->stripInBoundsConstantOffsets();
747 if (auto *LHSGV = dyn_cast<GlobalValue>(Val: LHS)) {
748 if (LHSGV->isDSOLocal() && RHSGV->isDSOLocal())
749 return LocalRelocation;
750 } else if (isa<DSOLocalEquivalent>(Val: LHS)) {
751 if (RHSGV->isDSOLocal())
752 return LocalRelocation;
753 }
754 }
755 }
756 }
757 }
758
759 PossibleRelocationsTy Result = NoRelocation;
760 for (const Value *Op : operands())
761 Result = std::max(a: cast<Constant>(Val: Op)->getRelocationInfo(), b: Result);
762
763 return Result;
764}
765
766/// Return true if the specified constantexpr is dead. This involves
767/// recursively traversing users of the constantexpr.
768/// If RemoveDeadUsers is true, also remove dead users at the same time.
769static bool constantIsDead(const Constant *C, bool RemoveDeadUsers) {
770 if (isa<GlobalValue>(Val: C)) return false; // Cannot remove this
771
772 Value::const_user_iterator I = C->user_begin(), E = C->user_end();
773 while (I != E) {
774 const Constant *User = dyn_cast<Constant>(Val: *I);
775 if (!User) return false; // Non-constant usage;
776 if (!constantIsDead(C: User, RemoveDeadUsers))
777 return false; // Constant wasn't dead
778
779 // Just removed User, so the iterator was invalidated.
780 // Since we return immediately upon finding a live user, we can always
781 // restart from user_begin().
782 if (RemoveDeadUsers)
783 I = C->user_begin();
784 else
785 ++I;
786 }
787
788 if (RemoveDeadUsers) {
789 // If C is only used by metadata, it should not be preserved but should
790 // have its uses replaced.
791 ReplaceableUses::SalvageDebugInfo(C: *C);
792 const_cast<Constant *>(C)->destroyConstant();
793 }
794
795 return true;
796}
797
798void Constant::removeDeadConstantUsers() const {
799 Value::const_user_iterator I = user_begin(), E = user_end();
800 Value::const_user_iterator LastNonDeadUser = E;
801 while (I != E) {
802 const Constant *User = dyn_cast<Constant>(Val: *I);
803 if (!User) {
804 LastNonDeadUser = I;
805 ++I;
806 continue;
807 }
808
809 if (!constantIsDead(C: User, /* RemoveDeadUsers= */ true)) {
810 // If the constant wasn't dead, remember that this was the last live use
811 // and move on to the next constant.
812 LastNonDeadUser = I;
813 ++I;
814 continue;
815 }
816
817 // If the constant was dead, then the iterator is invalidated.
818 if (LastNonDeadUser == E)
819 I = user_begin();
820 else
821 I = std::next(x: LastNonDeadUser);
822 }
823}
824
825bool Constant::hasOneLiveUse() const { return hasNLiveUses(N: 1); }
826
827bool Constant::hasZeroLiveUses() const { return hasNLiveUses(N: 0); }
828
829bool Constant::hasNLiveUses(unsigned N) const {
830 unsigned NumUses = 0;
831 for (const Use &U : uses()) {
832 const Constant *User = dyn_cast<Constant>(Val: U.getUser());
833 if (!User || !constantIsDead(C: User, /* RemoveDeadUsers= */ false)) {
834 ++NumUses;
835
836 if (NumUses > N)
837 return false;
838 }
839 }
840 return NumUses == N;
841}
842
843Constant *Constant::replaceUndefsWith(Constant *C, Constant *Replacement) {
844 assert(C && Replacement && "Expected non-nullptr constant arguments");
845 Type *Ty = C->getType();
846 if (match(V: C, P: m_Undef())) {
847 assert(Ty == Replacement->getType() && "Expected matching types");
848 return Replacement;
849 }
850
851 // Don't know how to deal with this constant.
852 auto *VTy = dyn_cast<FixedVectorType>(Val: Ty);
853 if (!VTy)
854 return C;
855
856 unsigned NumElts = VTy->getNumElements();
857 SmallVector<Constant *, 32> NewC(NumElts);
858 for (unsigned i = 0; i != NumElts; ++i) {
859 Constant *EltC = C->getAggregateElement(Elt: i);
860 assert((!EltC || EltC->getType() == Replacement->getType()) &&
861 "Expected matching types");
862 NewC[i] = EltC && match(V: EltC, P: m_Undef()) ? Replacement : EltC;
863 }
864 return ConstantVector::get(V: NewC);
865}
866
867Constant *Constant::mergeUndefsWith(Constant *C, Constant *Other) {
868 assert(C && Other && "Expected non-nullptr constant arguments");
869 if (match(V: C, P: m_Undef()))
870 return C;
871
872 Type *Ty = C->getType();
873 if (match(V: Other, P: m_Undef()))
874 return UndefValue::get(T: Ty);
875
876 auto *VTy = dyn_cast<FixedVectorType>(Val: Ty);
877 if (!VTy)
878 return C;
879
880 Type *EltTy = VTy->getElementType();
881 unsigned NumElts = VTy->getNumElements();
882 assert(isa<FixedVectorType>(Other->getType()) &&
883 cast<FixedVectorType>(Other->getType())->getNumElements() == NumElts &&
884 "Type mismatch");
885
886 bool FoundExtraUndef = false;
887 SmallVector<Constant *, 32> NewC(NumElts);
888 for (unsigned I = 0; I != NumElts; ++I) {
889 NewC[I] = C->getAggregateElement(Elt: I);
890 Constant *OtherEltC = Other->getAggregateElement(Elt: I);
891 assert(NewC[I] && OtherEltC && "Unknown vector element");
892 if (!match(V: NewC[I], P: m_Undef()) && match(V: OtherEltC, P: m_Undef())) {
893 NewC[I] = UndefValue::get(T: EltTy);
894 FoundExtraUndef = true;
895 }
896 }
897 if (FoundExtraUndef)
898 return ConstantVector::get(V: NewC);
899 return C;
900}
901
902bool Constant::isManifestConstant() const {
903 if (isa<UndefValue>(Val: this))
904 return false;
905 if (isa<ConstantData>(Val: this))
906 return true;
907 if (isa<ConstantAggregate>(Val: this) || isa<ConstantExpr>(Val: this)) {
908 for (const Value *Op : operand_values())
909 if (!cast<Constant>(Val: Op)->isManifestConstant())
910 return false;
911 return true;
912 }
913 return false;
914}
915
916//===----------------------------------------------------------------------===//
917// ConstantInt
918//===----------------------------------------------------------------------===//
919
920ConstantInt::ConstantInt(Type *Ty, const APInt &V)
921 : ConstantData(Ty, ConstantIntVal), Val(V) {
922 assert(V.getBitWidth() ==
923 cast<IntegerType>(Ty->getScalarType())->getBitWidth() &&
924 "Invalid constant for type");
925 if (V.isZero())
926 SubclassOptionalData = IsNullValue;
927}
928
929ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
930 LLVMContextImpl *pImpl = Context.pImpl;
931 if (!pImpl->TheTrueVal)
932 pImpl->TheTrueVal = ConstantInt::get(Ty: Type::getInt1Ty(C&: Context), V: 1);
933 return pImpl->TheTrueVal;
934}
935
936ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
937 LLVMContextImpl *pImpl = Context.pImpl;
938 if (!pImpl->TheFalseVal)
939 pImpl->TheFalseVal = ConstantInt::get(Ty: Type::getInt1Ty(C&: Context), V: 0);
940 return pImpl->TheFalseVal;
941}
942
943ConstantInt *ConstantInt::getBool(LLVMContext &Context, bool V) {
944 return V ? getTrue(Context) : getFalse(Context);
945}
946
947Constant *ConstantInt::getTrue(Type *Ty) {
948 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
949 ConstantInt *TrueC = ConstantInt::getTrue(Context&: Ty->getContext());
950 if (auto *VTy = dyn_cast<VectorType>(Val: Ty))
951 return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: TrueC);
952 return TrueC;
953}
954
955Constant *ConstantInt::getFalse(Type *Ty) {
956 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
957 ConstantInt *FalseC = ConstantInt::getFalse(Context&: Ty->getContext());
958 if (auto *VTy = dyn_cast<VectorType>(Val: Ty))
959 return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: FalseC);
960 return FalseC;
961}
962
963Constant *ConstantInt::getBool(Type *Ty, bool V) {
964 return V ? getTrue(Ty) : getFalse(Ty);
965}
966
967// Get a ConstantInt from an APInt.
968ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
969 // get an existing value or the insertion position
970 LLVMContextImpl *pImpl = Context.pImpl;
971 std::unique_ptr<ConstantInt> &Slot =
972 V.isZero() ? pImpl->IntZeroConstants[V.getBitWidth()]
973 : V.isOne() ? pImpl->IntOneConstants[V.getBitWidth()]
974 : pImpl->IntConstants[V];
975 if (!Slot) {
976 // Get the corresponding integer type for the bit width of the value.
977 IntegerType *ITy = IntegerType::get(C&: Context, NumBits: V.getBitWidth());
978 Slot.reset(p: new ConstantInt(ITy, V));
979 }
980 assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth()));
981 return Slot.get();
982}
983
984// Get a ConstantInt vector with each lane set to the same APInt.
985ConstantInt *ConstantInt::get(LLVMContext &Context, ElementCount EC,
986 const APInt &V) {
987 // Get an existing value or the insertion position.
988 std::unique_ptr<ConstantInt> &Slot =
989 Context.pImpl->IntSplatConstants[std::make_pair(x&: EC, y: V)];
990 if (!Slot) {
991 IntegerType *ITy = IntegerType::get(C&: Context, NumBits: V.getBitWidth());
992 VectorType *VTy = VectorType::get(ElementType: ITy, EC);
993 Slot.reset(p: new ConstantInt(VTy, V));
994 }
995
996#ifndef NDEBUG
997 IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
998 VectorType *VTy = VectorType::get(ITy, EC);
999 assert(Slot->getType() == VTy);
1000#endif
1001 return Slot.get();
1002}
1003
1004Constant *ConstantInt::get(Type *Ty, uint64_t V, bool IsSigned,
1005 bool ImplicitTrunc) {
1006 Constant *C =
1007 get(Ty: cast<IntegerType>(Val: Ty->getScalarType()), V, IsSigned, ImplicitTrunc);
1008
1009 // For vectors, broadcast the value.
1010 if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty))
1011 return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C);
1012
1013 return C;
1014}
1015
1016ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool IsSigned,
1017 bool ImplicitTrunc) {
1018 return get(Context&: Ty->getContext(),
1019 V: APInt(Ty->getBitWidth(), V, IsSigned, ImplicitTrunc));
1020}
1021
1022Constant *ConstantInt::get(Type *Ty, const APInt& V) {
1023 ConstantInt *C = get(Context&: Ty->getContext(), V);
1024 assert(C->getType() == Ty->getScalarType() &&
1025 "ConstantInt type doesn't match the type implied by its value!");
1026
1027 // For vectors, broadcast the value.
1028 if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty))
1029 return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C);
1030
1031 return C;
1032}
1033
1034ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) {
1035 return get(Context&: Ty->getContext(), V: APInt(Ty->getBitWidth(), Str, radix));
1036}
1037
1038/// Remove the constant from the constant table.
1039void ConstantInt::destroyConstantImpl() {
1040 llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
1041}
1042
1043//===----------------------------------------------------------------------===//
1044// ConstantByte
1045//===----------------------------------------------------------------------===//
1046
1047ConstantByte::ConstantByte(Type *Ty, const APInt &V)
1048 : ConstantData(Ty, ConstantByteVal), Val(V) {
1049 assert(V.getBitWidth() ==
1050 cast<ByteType>(Ty->getScalarType())->getBitWidth() &&
1051 "Invalid constant for type");
1052 if (V.isZero())
1053 SubclassOptionalData = IsNullValue;
1054}
1055
1056// Get a ConstantByte from an APInt.
1057ConstantByte *ConstantByte::get(LLVMContext &Context, const APInt &V) {
1058 // get an existing value or the insertion position
1059 LLVMContextImpl *pImpl = Context.pImpl;
1060 std::unique_ptr<ConstantByte> &Slot =
1061 V.isZero() ? pImpl->ByteZeroConstants[V.getBitWidth()]
1062 : V.isOne() ? pImpl->ByteOneConstants[V.getBitWidth()]
1063 : pImpl->ByteConstants[V];
1064 if (!Slot) {
1065 // Get the corresponding byte type for the bit width of the value.
1066 ByteType *BTy = ByteType::get(C&: Context, NumBits: V.getBitWidth());
1067 Slot.reset(p: new ConstantByte(BTy, V));
1068 }
1069 assert(Slot->getType() == ByteType::get(Context, V.getBitWidth()));
1070 return Slot.get();
1071}
1072
1073// Get a ConstantByte vector with each lane set to the same APInt.
1074ConstantByte *ConstantByte::get(LLVMContext &Context, ElementCount EC,
1075 const APInt &V) {
1076 // Get an existing value or the insertion position.
1077 std::unique_ptr<ConstantByte> &Slot =
1078 Context.pImpl->ByteSplatConstants[std::make_pair(x&: EC, y: V)];
1079 if (!Slot) {
1080 ByteType *BTy = ByteType::get(C&: Context, NumBits: V.getBitWidth());
1081 VectorType *VTy = VectorType::get(ElementType: BTy, EC);
1082 Slot.reset(p: new ConstantByte(VTy, V));
1083 }
1084
1085#ifndef NDEBUG
1086 ByteType *BTy = ByteType::get(Context, V.getBitWidth());
1087 VectorType *VTy = VectorType::get(BTy, EC);
1088 assert(Slot->getType() == VTy);
1089#endif
1090 return Slot.get();
1091}
1092
1093Constant *ConstantByte::get(Type *Ty, uint64_t V, bool isSigned,
1094 bool ImplicitTrunc) {
1095 Constant *C =
1096 get(Ty: cast<ByteType>(Val: Ty->getScalarType()), V, isSigned, ImplicitTrunc);
1097
1098 // For vectors, broadcast the value.
1099 if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty))
1100 return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C);
1101
1102 return C;
1103}
1104
1105ConstantByte *ConstantByte::get(ByteType *Ty, uint64_t V, bool isSigned,
1106 bool ImplicitTrunc) {
1107 return get(Context&: Ty->getContext(),
1108 V: APInt(Ty->getBitWidth(), V, isSigned, ImplicitTrunc));
1109}
1110
1111Constant *ConstantByte::get(Type *Ty, const APInt &V) {
1112 ConstantByte *C = get(Context&: Ty->getContext(), V);
1113 assert(C->getType() == Ty->getScalarType() &&
1114 "ConstantByte type doesn't match the type implied by its value!");
1115
1116 // For vectors, broadcast the value.
1117 if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty))
1118 return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C);
1119
1120 return C;
1121}
1122
1123ConstantByte *ConstantByte::get(ByteType *Ty, StringRef Str, uint8_t radix) {
1124 return get(Context&: Ty->getContext(), V: APInt(Ty->getBitWidth(), Str, radix));
1125}
1126
1127/// Remove the constant from the constant table.
1128void ConstantByte::destroyConstantImpl() {
1129 llvm_unreachable("You can't ConstantByte->destroyConstantImpl()!");
1130}
1131
1132//===----------------------------------------------------------------------===//
1133// ConstantFP
1134//===----------------------------------------------------------------------===//
1135
1136ConstantFP *ConstantFP::get(Type *Ty, double V) {
1137 LLVMContext &Context = Ty->getContext();
1138
1139 APFloat FV(V);
1140 bool ignored;
1141 FV.convert(ToSemantics: Ty->getScalarType()->getFltSemantics(),
1142 RM: APFloat::rmNearestTiesToEven, losesInfo: &ignored);
1143
1144 if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty))
1145 return get(Context, EC: VTy->getElementCount(), V: FV);
1146
1147 return get(Context, V: FV);
1148}
1149
1150ConstantFP *ConstantFP::get(Type *Ty, const APFloat &V) {
1151 LLVMContext &Context = Ty->getContext();
1152 assert(Ty->getScalarType() ==
1153 Type::getFloatingPointTy(Context, V.getSemantics()) &&
1154 "ConstantFP type doesn't match the type implied by its value!");
1155
1156 if (auto *VTy = dyn_cast<VectorType>(Val: Ty))
1157 return get(Context, EC: VTy->getElementCount(), V);
1158
1159 return get(Context&: Ty->getContext(), V);
1160}
1161
1162ConstantFP *ConstantFP::get(Type *Ty, StringRef Str) {
1163 LLVMContext &Context = Ty->getContext();
1164 APFloat FV(Ty->getScalarType()->getFltSemantics(), Str);
1165
1166 if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty))
1167 return get(Context, EC: VTy->getElementCount(), V: FV);
1168
1169 return get(Context, V: FV);
1170}
1171
1172ConstantFP *ConstantFP::getInfinity(Type *Ty, bool Negative) {
1173 const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
1174 return get(Ty, V: APFloat::getInf(Sem: Semantics, Negative));
1175}
1176
1177ConstantFP *ConstantFP::getNaN(Type *Ty, bool Negative, uint64_t Payload) {
1178 const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
1179 APFloat NaN = APFloat::getNaN(Sem: Semantics, Negative, payload: Payload);
1180 return get(Ty, V: NaN);
1181}
1182
1183ConstantFP *ConstantFP::getQNaN(Type *Ty, bool Negative, APInt *Payload) {
1184 const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
1185 APFloat NaN = APFloat::getQNaN(Sem: Semantics, Negative, payload: Payload);
1186 return get(Ty, V: NaN);
1187}
1188
1189ConstantFP *ConstantFP::getSNaN(Type *Ty, bool Negative, APInt *Payload) {
1190 const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
1191 APFloat NaN = APFloat::getSNaN(Sem: Semantics, Negative, payload: Payload);
1192 return get(Ty, V: NaN);
1193}
1194
1195ConstantFP *ConstantFP::getZero(Type *Ty, bool Negative) {
1196 const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
1197 APFloat NegZero = APFloat::getZero(Sem: Semantics, Negative);
1198 return get(Ty, V: NegZero);
1199}
1200
1201// ConstantFP accessors.
1202ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
1203 LLVMContextImpl* pImpl = Context.pImpl;
1204
1205 std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];
1206
1207 if (!Slot) {
1208 Type *Ty = Type::getFloatingPointTy(C&: Context, S: V.getSemantics());
1209 Slot.reset(p: new ConstantFP(Ty, V));
1210 }
1211
1212 return Slot.get();
1213}
1214
1215// Get a ConstantFP vector with each lane set to the same APFloat.
1216ConstantFP *ConstantFP::get(LLVMContext &Context, ElementCount EC,
1217 const APFloat &V) {
1218 // Get an existing value or the insertion position.
1219 std::unique_ptr<ConstantFP> &Slot =
1220 Context.pImpl->FPSplatConstants[std::make_pair(x&: EC, y: V)];
1221 if (!Slot) {
1222 Type *EltTy = Type::getFloatingPointTy(C&: Context, S: V.getSemantics());
1223 VectorType *VTy = VectorType::get(ElementType: EltTy, EC);
1224 Slot.reset(p: new ConstantFP(VTy, V));
1225 }
1226
1227#ifndef NDEBUG
1228 Type *EltTy = Type::getFloatingPointTy(Context, V.getSemantics());
1229 VectorType *VTy = VectorType::get(EltTy, EC);
1230 assert(Slot->getType() == VTy);
1231#endif
1232 return Slot.get();
1233}
1234
1235ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
1236 : ConstantData(Ty, ConstantFPVal), Val(V) {
1237 assert(&V.getSemantics() == &Ty->getScalarType()->getFltSemantics() &&
1238 "FP type Mismatch");
1239 // ppc_fp128 determine isZero using high order double only
1240 // so check the bitwise value to make sure all bits are zero.
1241 if (V.bitcastToAPInt().isZero())
1242 SubclassOptionalData = IsNullValue;
1243}
1244
1245bool ConstantFP::isExactlyValue(const APFloat &V) const {
1246 return Val.bitwiseIsEqual(RHS: V);
1247}
1248
1249/// Remove the constant from the constant table.
1250void ConstantFP::destroyConstantImpl() {
1251 llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
1252}
1253
1254//===----------------------------------------------------------------------===//
1255// ConstantAggregateZero Implementation
1256//===----------------------------------------------------------------------===//
1257
1258Constant *ConstantAggregateZero::getSequentialElement() const {
1259 if (auto *AT = dyn_cast<ArrayType>(Val: getType()))
1260 return Constant::getNullValue(Ty: AT->getElementType());
1261 return Constant::getNullValue(Ty: cast<VectorType>(Val: getType())->getElementType());
1262}
1263
1264Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
1265 return Constant::getNullValue(Ty: getType()->getStructElementType(N: Elt));
1266}
1267
1268Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
1269 if (isa<ArrayType>(Val: getType()) || isa<VectorType>(Val: getType()))
1270 return getSequentialElement();
1271 return getStructElement(Elt: cast<ConstantInt>(Val: C)->getZExtValue());
1272}
1273
1274Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
1275 if (isa<ArrayType>(Val: getType()) || isa<VectorType>(Val: getType()))
1276 return getSequentialElement();
1277 return getStructElement(Elt: Idx);
1278}
1279
1280ElementCount ConstantAggregateZero::getElementCount() const {
1281 Type *Ty = getType();
1282 if (auto *AT = dyn_cast<ArrayType>(Val: Ty))
1283 return ElementCount::getFixed(MinVal: AT->getNumElements());
1284 if (auto *VT = dyn_cast<VectorType>(Val: Ty))
1285 return VT->getElementCount();
1286 return ElementCount::getFixed(MinVal: Ty->getStructNumElements());
1287}
1288
1289//===----------------------------------------------------------------------===//
1290// UndefValue Implementation
1291//===----------------------------------------------------------------------===//
1292
1293UndefValue *UndefValue::getSequentialElement() const {
1294 if (ArrayType *ATy = dyn_cast<ArrayType>(Val: getType()))
1295 return UndefValue::get(T: ATy->getElementType());
1296 return UndefValue::get(T: cast<VectorType>(Val: getType())->getElementType());
1297}
1298
1299UndefValue *UndefValue::getStructElement(unsigned Elt) const {
1300 return UndefValue::get(T: getType()->getStructElementType(N: Elt));
1301}
1302
1303UndefValue *UndefValue::getElementValue(Constant *C) const {
1304 if (isa<ArrayType>(Val: getType()) || isa<VectorType>(Val: getType()))
1305 return getSequentialElement();
1306 return getStructElement(Elt: cast<ConstantInt>(Val: C)->getZExtValue());
1307}
1308
1309UndefValue *UndefValue::getElementValue(unsigned Idx) const {
1310 if (isa<ArrayType>(Val: getType()) || isa<VectorType>(Val: getType()))
1311 return getSequentialElement();
1312 return getStructElement(Elt: Idx);
1313}
1314
1315unsigned UndefValue::getNumElements() const {
1316 Type *Ty = getType();
1317 if (auto *AT = dyn_cast<ArrayType>(Val: Ty))
1318 return AT->getNumElements();
1319 if (auto *VT = dyn_cast<VectorType>(Val: Ty))
1320 return cast<FixedVectorType>(Val: VT)->getNumElements();
1321 return Ty->getStructNumElements();
1322}
1323
1324//===----------------------------------------------------------------------===//
1325// PoisonValue Implementation
1326//===----------------------------------------------------------------------===//
1327
1328PoisonValue *PoisonValue::getSequentialElement() const {
1329 if (ArrayType *ATy = dyn_cast<ArrayType>(Val: getType()))
1330 return PoisonValue::get(T: ATy->getElementType());
1331 return PoisonValue::get(T: cast<VectorType>(Val: getType())->getElementType());
1332}
1333
1334PoisonValue *PoisonValue::getStructElement(unsigned Elt) const {
1335 return PoisonValue::get(T: getType()->getStructElementType(N: Elt));
1336}
1337
1338PoisonValue *PoisonValue::getElementValue(Constant *C) const {
1339 if (isa<ArrayType>(Val: getType()) || isa<VectorType>(Val: getType()))
1340 return getSequentialElement();
1341 return getStructElement(Elt: cast<ConstantInt>(Val: C)->getZExtValue());
1342}
1343
1344PoisonValue *PoisonValue::getElementValue(unsigned Idx) const {
1345 if (isa<ArrayType>(Val: getType()) || isa<VectorType>(Val: getType()))
1346 return getSequentialElement();
1347 return getStructElement(Elt: Idx);
1348}
1349
1350//===----------------------------------------------------------------------===//
1351// ConstantXXX Classes
1352//===----------------------------------------------------------------------===//
1353
1354template <typename ItTy, typename EltTy>
1355static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
1356 for (; Start != End; ++Start)
1357 if (*Start != Elt)
1358 return false;
1359 return true;
1360}
1361
1362template <typename SequentialTy, typename ElementTy>
1363static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {
1364 assert(!V.empty() && "Cannot get empty int sequence.");
1365
1366 SmallVector<ElementTy, 16> Elts;
1367 for (Constant *C : V)
1368 if (auto *CI = dyn_cast<ConstantInt>(Val: C))
1369 Elts.push_back(CI->getZExtValue());
1370 else
1371 return nullptr;
1372 return SequentialTy::get(V[0]->getContext(), Elts);
1373}
1374
1375template <typename SequentialTy, typename ElementTy>
1376static Constant *getByteSequenceIfElementsMatch(ArrayRef<Constant *> V) {
1377 assert(!V.empty() && "Cannot get empty byte sequence.");
1378
1379 SmallVector<ElementTy, 16> Elts;
1380 for (Constant *C : V)
1381 if (auto *CI = dyn_cast<ConstantByte>(Val: C))
1382 Elts.push_back(CI->getZExtValue());
1383 else
1384 return nullptr;
1385 return SequentialTy::getByte(V[0]->getType(), Elts);
1386}
1387
1388template <typename SequentialTy, typename ElementTy>
1389static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {
1390 assert(!V.empty() && "Cannot get empty FP sequence.");
1391
1392 SmallVector<ElementTy, 16> Elts;
1393 for (Constant *C : V)
1394 if (auto *CFP = dyn_cast<ConstantFP>(Val: C))
1395 Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
1396 else
1397 return nullptr;
1398 return SequentialTy::getFP(V[0]->getType(), Elts);
1399}
1400
1401template <typename SequenceTy>
1402static Constant *getSequenceIfElementsMatch(Constant *C,
1403 ArrayRef<Constant *> V) {
1404 // We speculatively build the elements here even if it turns out that there is
1405 // a constantexpr or something else weird, since it is so uncommon for that to
1406 // happen.
1407 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: C)) {
1408 if (CI->getType()->isIntegerTy(BitWidth: 8))
1409 return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
1410 else if (CI->getType()->isIntegerTy(BitWidth: 16))
1411 return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
1412 else if (CI->getType()->isIntegerTy(BitWidth: 32))
1413 return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
1414 else if (CI->getType()->isIntegerTy(BitWidth: 64))
1415 return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
1416 } else if (ConstantByte *CB = dyn_cast<ConstantByte>(Val: C)) {
1417 if (CB->getType()->isByteTy(BitWidth: 8))
1418 return getByteSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
1419 else if (CB->getType()->isByteTy(BitWidth: 16))
1420 return getByteSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
1421 else if (CB->getType()->isByteTy(BitWidth: 32))
1422 return getByteSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
1423 else if (CB->getType()->isByteTy(BitWidth: 64))
1424 return getByteSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
1425 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(Val: C)) {
1426 if (CFP->getType()->isHalfTy() || CFP->getType()->isBFloatTy())
1427 return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
1428 else if (CFP->getType()->isFloatTy())
1429 return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
1430 else if (CFP->getType()->isDoubleTy())
1431 return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
1432 }
1433
1434 return nullptr;
1435}
1436
1437ConstantAggregate::ConstantAggregate(Type *T, ValueTy VT,
1438 ArrayRef<Constant *> V,
1439 AllocInfo AllocInfo)
1440 : Constant(T, VT, AllocInfo) {
1441 llvm::copy(Range&: V, Out: op_begin());
1442
1443 // Check that types match, unless this is an opaque struct.
1444 if (auto *ST = dyn_cast<StructType>(Val: T)) {
1445 if (ST->isOpaque())
1446 return;
1447 for (unsigned I = 0, E = V.size(); I != E; ++I)
1448 assert(V[I]->getType() == ST->getTypeAtIndex(I) &&
1449 "Initializer for struct element doesn't match!");
1450 }
1451}
1452
1453ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V,
1454 AllocInfo AllocInfo)
1455 : ConstantAggregate(T, ConstantArrayVal, V, AllocInfo) {
1456 assert(V.size() == T->getNumElements() &&
1457 "Invalid initializer for constant array");
1458}
1459
1460Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
1461 if (Constant *C = getImpl(T: Ty, V))
1462 return C;
1463 return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
1464}
1465
1466Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
1467 // Empty arrays are canonicalized to ConstantAggregateZero.
1468 if (V.empty())
1469 return ConstantAggregateZero::get(Ty);
1470
1471 for (Constant *C : V) {
1472 assert(C->getType() == Ty->getElementType() &&
1473 "Wrong type in array element initializer");
1474 (void)C;
1475 }
1476
1477 // If this is an all-zero array, return a ConstantAggregateZero object. If
1478 // all undef, return an UndefValue, if "all simple", then return a
1479 // ConstantDataArray.
1480 Constant *C = V[0];
1481 if (isa<PoisonValue>(Val: C) && rangeOnlyContains(Start: V.begin(), End: V.end(), Elt: C))
1482 return PoisonValue::get(T: Ty);
1483
1484 if (isa<UndefValue>(Val: C) && rangeOnlyContains(Start: V.begin(), End: V.end(), Elt: C))
1485 return UndefValue::get(T: Ty);
1486
1487 if (C->isNullValue() && rangeOnlyContains(Start: V.begin(), End: V.end(), Elt: C))
1488 return ConstantAggregateZero::get(Ty);
1489
1490 // Check to see if all of the elements are ConstantFP or ConstantInt or
1491 // ConstantByte and if the element type is compatible with ConstantDataVector.
1492 // If so, use it.
1493 if (ConstantDataSequential::isElementTypeCompatible(Ty: C->getType()))
1494 return getSequenceIfElementsMatch<ConstantDataArray>(C, V);
1495
1496 // Otherwise, we really do want to create a ConstantArray.
1497 return nullptr;
1498}
1499
1500StructType *ConstantStruct::getTypeForElements(LLVMContext &Context,
1501 ArrayRef<Constant*> V,
1502 bool Packed) {
1503 unsigned VecSize = V.size();
1504 SmallVector<Type*, 16> EltTypes(VecSize);
1505 for (unsigned i = 0; i != VecSize; ++i)
1506 EltTypes[i] = V[i]->getType();
1507
1508 return StructType::get(Context, Elements: EltTypes, isPacked: Packed);
1509}
1510
1511
1512StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
1513 bool Packed) {
1514 assert(!V.empty() &&
1515 "ConstantStruct::getTypeForElements cannot be called on empty list");
1516 return getTypeForElements(Context&: V[0]->getContext(), V, Packed);
1517}
1518
1519ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V,
1520 AllocInfo AllocInfo)
1521 : ConstantAggregate(T, ConstantStructVal, V, AllocInfo) {
1522 assert((T->isOpaque() || V.size() == T->getNumElements()) &&
1523 "Invalid initializer for constant struct");
1524}
1525
1526// ConstantStruct accessors.
1527Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
1528 assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
1529 "Incorrect # elements specified to ConstantStruct::get");
1530
1531 // Create a ConstantAggregateZero value if all elements are zeros.
1532 bool isZero = true;
1533 bool isUndef = false;
1534 bool isPoison = false;
1535
1536 if (!V.empty()) {
1537 isUndef = isa<UndefValue>(Val: V[0]);
1538 isPoison = isa<PoisonValue>(Val: V[0]);
1539 isZero = V[0]->isNullValue();
1540 // PoisonValue inherits UndefValue, so its check is not necessary.
1541 if (isUndef || isZero) {
1542 for (Constant *C : V) {
1543 if (!C->isNullValue())
1544 isZero = false;
1545 if (!isa<PoisonValue>(Val: C))
1546 isPoison = false;
1547 if (isa<PoisonValue>(Val: C) || !isa<UndefValue>(Val: C))
1548 isUndef = false;
1549 }
1550 }
1551 }
1552 if (isZero)
1553 return ConstantAggregateZero::get(Ty: ST);
1554 if (isPoison)
1555 return PoisonValue::get(T: ST);
1556 if (isUndef)
1557 return UndefValue::get(T: ST);
1558
1559 return ST->getContext().pImpl->StructConstants.getOrCreate(Ty: ST, V);
1560}
1561
1562ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V,
1563 AllocInfo AllocInfo)
1564 : ConstantAggregate(T, ConstantVectorVal, V, AllocInfo) {
1565 assert(V.size() == cast<FixedVectorType>(T)->getNumElements() &&
1566 "Invalid initializer for constant vector");
1567}
1568
1569// ConstantVector accessors.
1570Constant *ConstantVector::get(ArrayRef<Constant*> V) {
1571 if (Constant *C = getImpl(V))
1572 return C;
1573 auto *Ty = FixedVectorType::get(ElementType: V.front()->getType(), NumElts: V.size());
1574 return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
1575}
1576
1577Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
1578 assert(!V.empty() && "Vectors can't be empty");
1579 auto *T = FixedVectorType::get(ElementType: V.front()->getType(), NumElts: V.size());
1580
1581 // If this is an all-undef or all-zero vector, return a
1582 // ConstantAggregateZero or UndefValue.
1583 Constant *C = V[0];
1584 bool isZero = C->isNullValue();
1585 bool isUndef = isa<UndefValue>(Val: C);
1586 bool isPoison = isa<PoisonValue>(Val: C);
1587 bool isSplatFP = isa<ConstantFP>(Val: C);
1588 bool isSplatInt = UseConstantIntForFixedLengthSplat && isa<ConstantInt>(Val: C);
1589 bool isSplatByte = isa<ConstantByte>(Val: C);
1590 bool isSplatPtrNull = isa<ConstantPointerNull>(Val: C);
1591
1592 if (isZero || isUndef || isSplatFP || isSplatInt || isSplatByte ||
1593 isSplatPtrNull) {
1594 for (unsigned i = 1, e = V.size(); i != e; ++i)
1595 if (V[i] != C) {
1596 isZero = isUndef = isPoison = isSplatFP = isSplatInt = isSplatByte =
1597 isSplatPtrNull = false;
1598 break;
1599 }
1600 }
1601
1602 if (isSplatPtrNull)
1603 return ConstantPointerNull::get(T);
1604 if (isZero)
1605 return ConstantAggregateZero::get(Ty: T);
1606 if (isPoison)
1607 return PoisonValue::get(T);
1608 if (isUndef)
1609 return UndefValue::get(T);
1610 if (isSplatFP)
1611 return ConstantFP::get(Context&: C->getContext(), EC: T->getElementCount(),
1612 V: cast<ConstantFP>(Val: C)->getValue());
1613 if (isSplatInt)
1614 return ConstantInt::get(Context&: C->getContext(), EC: T->getElementCount(),
1615 V: cast<ConstantInt>(Val: C)->getValue());
1616 if (isSplatByte)
1617 return ConstantByte::get(Context&: C->getContext(), EC: T->getElementCount(),
1618 V: cast<ConstantByte>(Val: C)->getValue());
1619
1620 // Check to see if all of the elements are ConstantFP or ConstantInt and if
1621 // the element type is compatible with ConstantDataVector. If so, use it.
1622 if (ConstantDataSequential::isElementTypeCompatible(Ty: C->getType()))
1623 return getSequenceIfElementsMatch<ConstantDataVector>(C, V);
1624
1625 // Otherwise, the element type isn't compatible with ConstantDataVector, or
1626 // the operand list contains a ConstantExpr or something else strange.
1627 return nullptr;
1628}
1629
1630Constant *ConstantVector::getSplat(ElementCount EC, Constant *V) {
1631 if (isa<ConstantPointerNull>(Val: V)) {
1632 VectorType *VTy = VectorType::get(ElementType: V->getType(), EC);
1633 return ConstantPointerNull::get(T: VTy);
1634 }
1635
1636 if (auto *CB = dyn_cast<ConstantByte>(Val: V))
1637 return ConstantByte::get(Context&: V->getContext(), EC, V: CB->getValue());
1638
1639 if (auto *CFP = dyn_cast<ConstantFP>(Val: V))
1640 return ConstantFP::get(Context&: V->getContext(), EC, V: CFP->getValue());
1641
1642 if (!EC.isScalable()) {
1643 // Maintain special handling of zero.
1644 if (!V->isNullValue()) {
1645 if (UseConstantIntForFixedLengthSplat && isa<ConstantInt>(Val: V))
1646 return ConstantInt::get(Context&: V->getContext(), EC,
1647 V: cast<ConstantInt>(Val: V)->getValue());
1648 }
1649
1650 // If this splat is compatible with ConstantDataVector, use it instead of
1651 // ConstantVector.
1652 if (isa<ConstantInt>(Val: V) &&
1653 ConstantDataSequential::isElementTypeCompatible(Ty: V->getType()))
1654 return ConstantDataVector::getSplat(NumElts: EC.getKnownMinValue(), Elt: V);
1655
1656 SmallVector<Constant *, 32> Elts(EC.getKnownMinValue(), V);
1657 return get(V: Elts);
1658 }
1659
1660 // Maintain special handling of zero.
1661 if (!V->isNullValue()) {
1662 if (UseConstantIntForScalableSplat && isa<ConstantInt>(Val: V))
1663 return ConstantInt::get(Context&: V->getContext(), EC,
1664 V: cast<ConstantInt>(Val: V)->getValue());
1665 }
1666
1667 Type *VTy = VectorType::get(ElementType: V->getType(), EC);
1668
1669 if (V->isNullValue())
1670 return ConstantAggregateZero::get(Ty: VTy);
1671 if (isa<PoisonValue>(Val: V))
1672 return PoisonValue::get(T: VTy);
1673 if (isa<UndefValue>(Val: V))
1674 return UndefValue::get(T: VTy);
1675
1676 Type *IdxTy = Type::getInt64Ty(C&: VTy->getContext());
1677
1678 // Move scalar into vector.
1679 Constant *PoisonV = PoisonValue::get(T: VTy);
1680 V = ConstantExpr::getInsertElement(Vec: PoisonV, Elt: V, Idx: ConstantInt::get(Ty: IdxTy, V: 0));
1681 // Build shuffle mask to perform the splat.
1682 SmallVector<int, 8> Zeros(EC.getKnownMinValue(), 0);
1683 // Splat.
1684 return ConstantExpr::getShuffleVector(V1: V, V2: PoisonV, Mask: Zeros);
1685}
1686
1687ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) {
1688 LLVMContextImpl *pImpl = Context.pImpl;
1689 if (!pImpl->TheNoneToken)
1690 pImpl->TheNoneToken.reset(p: new ConstantTokenNone(Context));
1691 return pImpl->TheNoneToken.get();
1692}
1693
1694/// Remove the constant from the constant table.
1695void ConstantTokenNone::destroyConstantImpl() {
1696 llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
1697}
1698
1699// Utility function for determining if a ConstantExpr is a CastOp or not. This
1700// can't be inline because we don't want to #include Instruction.h into
1701// Constant.h
1702bool ConstantExpr::isCast() const { return Instruction::isCast(Opcode: getOpcode()); }
1703
1704ArrayRef<int> ConstantExpr::getShuffleMask() const {
1705 return cast<ShuffleVectorConstantExpr>(Val: this)->ShuffleMask;
1706}
1707
1708Constant *ConstantExpr::getShuffleMaskForBitcode() const {
1709 return cast<ShuffleVectorConstantExpr>(Val: this)->ShuffleMaskForBitcode;
1710}
1711
1712Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
1713 bool OnlyIfReduced, Type *SrcTy) const {
1714 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
1715
1716 // If no operands changed return self.
1717 if (Ty == getType() && std::equal(first1: Ops.begin(), last1: Ops.end(), first2: op_begin()))
1718 return const_cast<ConstantExpr*>(this);
1719
1720 Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;
1721 switch (getOpcode()) {
1722 case Instruction::Trunc:
1723 case Instruction::ZExt:
1724 case Instruction::SExt:
1725 case Instruction::FPTrunc:
1726 case Instruction::FPExt:
1727 case Instruction::UIToFP:
1728 case Instruction::SIToFP:
1729 case Instruction::FPToUI:
1730 case Instruction::FPToSI:
1731 case Instruction::PtrToAddr:
1732 case Instruction::PtrToInt:
1733 case Instruction::IntToPtr:
1734 case Instruction::BitCast:
1735 case Instruction::AddrSpaceCast:
1736 return ConstantExpr::getCast(ops: getOpcode(), C: Ops[0], Ty, OnlyIfReduced);
1737 case Instruction::InsertElement:
1738 return ConstantExpr::getInsertElement(Vec: Ops[0], Elt: Ops[1], Idx: Ops[2],
1739 OnlyIfReducedTy);
1740 case Instruction::ExtractElement:
1741 return ConstantExpr::getExtractElement(Vec: Ops[0], Idx: Ops[1], OnlyIfReducedTy);
1742 case Instruction::ShuffleVector:
1743 return ConstantExpr::getShuffleVector(V1: Ops[0], V2: Ops[1], Mask: getShuffleMask(),
1744 OnlyIfReducedTy);
1745 case Instruction::GetElementPtr: {
1746 auto *GEPO = cast<GEPOperator>(Val: this);
1747 assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
1748 LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
1749 return ConstantExpr::getGetElementPtr(
1750 Ty: SrcTy ? SrcTy : GEPO->getSourceElementType(), C: Ops[0], IdxList: Ops.slice(N: 1),
1751 NW: GEPO->getNoWrapFlags(), InRange: GEPO->getInRange(), OnlyIfReducedTy);
1752 LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
1753 }
1754 default:
1755 assert(getNumOperands() == 2 && "Must be binary operator?");
1756 return ConstantExpr::get(Opcode: getOpcode(), C1: Ops[0], C2: Ops[1], Flags: SubclassOptionalData,
1757 OnlyIfReducedTy);
1758 }
1759}
1760
1761
1762//===----------------------------------------------------------------------===//
1763// isValueValidForType implementations
1764
1765bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
1766 unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay
1767 if (Ty->isIntegerTy(BitWidth: 1))
1768 return Val == 0 || Val == 1;
1769 return isUIntN(N: NumBits, x: Val);
1770}
1771
1772bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
1773 unsigned NumBits = Ty->getIntegerBitWidth();
1774 if (Ty->isIntegerTy(BitWidth: 1))
1775 return Val == 0 || Val == 1 || Val == -1;
1776 return isIntN(N: NumBits, x: Val);
1777}
1778
1779bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
1780 // convert modifies in place, so make a copy.
1781 APFloat Val2 = APFloat(Val);
1782 bool losesInfo;
1783 switch (Ty->getTypeID()) {
1784 default:
1785 return false; // These can't be represented as floating point!
1786
1787 // FIXME rounding mode needs to be more flexible
1788 case Type::HalfTyID: {
1789 if (&Val2.getSemantics() == &APFloat::IEEEhalf())
1790 return true;
1791 Val2.convert(ToSemantics: APFloat::IEEEhalf(), RM: APFloat::rmNearestTiesToEven, losesInfo: &losesInfo);
1792 return !losesInfo;
1793 }
1794 case Type::BFloatTyID: {
1795 if (&Val2.getSemantics() == &APFloat::BFloat())
1796 return true;
1797 Val2.convert(ToSemantics: APFloat::BFloat(), RM: APFloat::rmNearestTiesToEven, losesInfo: &losesInfo);
1798 return !losesInfo;
1799 }
1800 case Type::FloatTyID: {
1801 if (&Val2.getSemantics() == &APFloat::IEEEsingle())
1802 return true;
1803 Val2.convert(ToSemantics: APFloat::IEEEsingle(), RM: APFloat::rmNearestTiesToEven, losesInfo: &losesInfo);
1804 return !losesInfo;
1805 }
1806 case Type::DoubleTyID: {
1807 if (&Val2.getSemantics() == &APFloat::IEEEhalf() ||
1808 &Val2.getSemantics() == &APFloat::BFloat() ||
1809 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1810 &Val2.getSemantics() == &APFloat::IEEEdouble())
1811 return true;
1812 Val2.convert(ToSemantics: APFloat::IEEEdouble(), RM: APFloat::rmNearestTiesToEven, losesInfo: &losesInfo);
1813 return !losesInfo;
1814 }
1815 case Type::X86_FP80TyID:
1816 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1817 &Val2.getSemantics() == &APFloat::BFloat() ||
1818 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1819 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1820 &Val2.getSemantics() == &APFloat::x87DoubleExtended();
1821 case Type::FP128TyID:
1822 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1823 &Val2.getSemantics() == &APFloat::BFloat() ||
1824 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1825 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1826 &Val2.getSemantics() == &APFloat::IEEEquad();
1827 case Type::PPC_FP128TyID:
1828 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1829 &Val2.getSemantics() == &APFloat::BFloat() ||
1830 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1831 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1832 &Val2.getSemantics() == &APFloat::PPCDoubleDouble();
1833 }
1834}
1835
1836
1837//===----------------------------------------------------------------------===//
1838// Factory Function Implementation
1839
1840ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
1841 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
1842 "Cannot create an aggregate zero of non-aggregate type!");
1843
1844 std::unique_ptr<ConstantAggregateZero> &Entry =
1845 Ty->getContext().pImpl->CAZConstants[Ty];
1846 if (!Entry)
1847 Entry.reset(p: new ConstantAggregateZero(Ty));
1848
1849 return Entry.get();
1850}
1851
1852/// Remove the constant from the constant table.
1853void ConstantAggregateZero::destroyConstantImpl() {
1854 getContext().pImpl->CAZConstants.erase(Val: getType());
1855}
1856
1857/// Remove the constant from the constant table.
1858void ConstantArray::destroyConstantImpl() {
1859 getType()->getContext().pImpl->ArrayConstants.remove(CP: this);
1860}
1861
1862
1863//---- ConstantStruct::get() implementation...
1864//
1865
1866/// Remove the constant from the constant table.
1867void ConstantStruct::destroyConstantImpl() {
1868 getType()->getContext().pImpl->StructConstants.remove(CP: this);
1869}
1870
1871/// Remove the constant from the constant table.
1872void ConstantVector::destroyConstantImpl() {
1873 getType()->getContext().pImpl->VectorConstants.remove(CP: this);
1874}
1875
1876Constant *Constant::getSplatValue(bool AllowPoison) const {
1877 assert(this->getType()->isVectorTy() && "Only valid for vectors!");
1878 if (isa<PoisonValue>(Val: this))
1879 return PoisonValue::get(T: cast<VectorType>(Val: getType())->getElementType());
1880 if (isa<ConstantAggregateZero>(Val: this))
1881 return getNullValue(Ty: cast<VectorType>(Val: getType())->getElementType());
1882 if (auto *CI = dyn_cast<ConstantInt>(Val: this))
1883 return ConstantInt::get(Context&: getContext(), V: CI->getValue());
1884 if (auto *CB = dyn_cast<ConstantByte>(Val: this))
1885 return ConstantByte::get(Context&: getContext(), V: CB->getValue());
1886 if (auto *CFP = dyn_cast<ConstantFP>(Val: this))
1887 return ConstantFP::get(Context&: getContext(), V: CFP->getValue());
1888 if (auto *CPN = dyn_cast<ConstantPointerNull>(Val: this))
1889 return ConstantPointerNull::get(T: CPN->getPointerType());
1890 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(Val: this))
1891 return CV->getSplatValue();
1892 if (const ConstantVector *CV = dyn_cast<ConstantVector>(Val: this))
1893 return CV->getSplatValue(AllowPoison);
1894
1895 // Check if this is a constant expression splat of the form returned by
1896 // ConstantVector::getSplat()
1897 const auto *Shuf = dyn_cast<ConstantExpr>(Val: this);
1898 if (Shuf && Shuf->getOpcode() == Instruction::ShuffleVector &&
1899 isa<UndefValue>(Val: Shuf->getOperand(i_nocapture: 1))) {
1900
1901 const auto *IElt = dyn_cast<ConstantExpr>(Val: Shuf->getOperand(i_nocapture: 0));
1902 if (IElt && IElt->getOpcode() == Instruction::InsertElement &&
1903 isa<UndefValue>(Val: IElt->getOperand(i_nocapture: 0))) {
1904
1905 ArrayRef<int> Mask = Shuf->getShuffleMask();
1906 Constant *SplatVal = IElt->getOperand(i_nocapture: 1);
1907 ConstantInt *Index = dyn_cast<ConstantInt>(Val: IElt->getOperand(i_nocapture: 2));
1908
1909 if (Index && Index->getValue() == 0 && llvm::all_of(Range&: Mask, P: equal_to(Arg: 0)))
1910 return SplatVal;
1911 }
1912 }
1913
1914 return nullptr;
1915}
1916
1917Constant *ConstantVector::getSplatValue(bool AllowPoison) const {
1918 // Check out first element.
1919 Constant *Elt = getOperand(i_nocapture: 0);
1920 // Then make sure all remaining elements point to the same value.
1921 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1922 Constant *OpC = getOperand(i_nocapture: I);
1923 if (OpC == Elt)
1924 continue;
1925
1926 // Strict mode: any mismatch is not a splat.
1927 if (!AllowPoison)
1928 return nullptr;
1929
1930 // Allow poison mode: ignore poison elements.
1931 if (isa<PoisonValue>(Val: OpC))
1932 continue;
1933
1934 // If we do not have a defined element yet, use the current operand.
1935 if (isa<PoisonValue>(Val: Elt))
1936 Elt = OpC;
1937
1938 if (OpC != Elt)
1939 return nullptr;
1940 }
1941 return Elt;
1942}
1943
1944const APInt &Constant::getUniqueInteger() const {
1945 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this))
1946 return CI->getValue();
1947 if (const ConstantByte *CB = dyn_cast<ConstantByte>(Val: this))
1948 return CB->getValue();
1949 // Scalable vectors can use a ConstantExpr to build a splat.
1950 if (isa<ConstantExpr>(Val: this))
1951 return cast<ConstantInt>(Val: this->getSplatValue())->getValue();
1952 // For non-ConstantExpr we use getAggregateElement as a fast path to avoid
1953 // calling getSplatValue in release builds.
1954 assert(this->getSplatValue() && "Doesn't contain a unique integer!");
1955 const Constant *C = this->getAggregateElement(Elt: 0U);
1956 assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");
1957 return cast<ConstantInt>(Val: C)->getValue();
1958}
1959
1960ConstantRange Constant::toConstantRange() const {
1961 if (auto *CI = dyn_cast<ConstantInt>(Val: this))
1962 return ConstantRange(CI->getValue());
1963
1964 unsigned BitWidth = getType()->getScalarSizeInBits();
1965 if (!getType()->isVectorTy())
1966 return ConstantRange::getFull(BitWidth);
1967
1968 if (auto *CI = dyn_cast_or_null<ConstantInt>(
1969 Val: getSplatValue(/*AllowPoison=*/true)))
1970 return ConstantRange(CI->getValue());
1971
1972 if (auto *CB =
1973 dyn_cast_or_null<ConstantByte>(Val: getSplatValue(/*AllowPoison=*/true)))
1974 return ConstantRange(CB->getValue());
1975
1976 if (auto *CDV = dyn_cast<ConstantDataVector>(Val: this)) {
1977 ConstantRange CR = ConstantRange::getEmpty(BitWidth);
1978 for (unsigned I = 0, E = CDV->getNumElements(); I < E; ++I)
1979 CR = CR.unionWith(CR: CDV->getElementAsAPInt(i: I));
1980 return CR;
1981 }
1982
1983 if (auto *CV = dyn_cast<ConstantVector>(Val: this)) {
1984 ConstantRange CR = ConstantRange::getEmpty(BitWidth);
1985 for (unsigned I = 0, E = CV->getNumOperands(); I < E; ++I) {
1986 Constant *Elem = CV->getOperand(i_nocapture: I);
1987 if (!Elem)
1988 return ConstantRange::getFull(BitWidth);
1989 if (isa<PoisonValue>(Val: Elem))
1990 continue;
1991 auto *CI = dyn_cast<ConstantInt>(Val: Elem);
1992 auto *CB = dyn_cast<ConstantByte>(Val: Elem);
1993 if (!CI && !CB)
1994 return ConstantRange::getFull(BitWidth);
1995 CR = CR.unionWith(CR: CI ? CI->getValue() : CB->getValue());
1996 }
1997 return CR;
1998 }
1999
2000 return ConstantRange::getFull(BitWidth);
2001}
2002
2003//---- ConstantPointerNull::get() implementation.
2004//
2005
2006ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
2007 return get(T: static_cast<Type *>(Ty));
2008}
2009
2010ConstantPointerNull *ConstantPointerNull::get(Type *Ty) {
2011 assert(Ty->isPtrOrPtrVectorTy() && "invalid type for null pointer constant");
2012 std::unique_ptr<ConstantPointerNull> &Entry =
2013 Ty->getContext().pImpl->CPNConstants[Ty];
2014 if (!Entry)
2015 Entry.reset(p: new ConstantPointerNull(Ty));
2016
2017 assert(Entry->getType() == Ty);
2018 return Entry.get();
2019}
2020
2021/// Remove the constant from the constant table.
2022void ConstantPointerNull::destroyConstantImpl() {
2023 getContext().pImpl->CPNConstants.erase(Val: getType());
2024}
2025
2026//---- ConstantTargetNone::get() implementation.
2027//
2028
2029ConstantTargetNone *ConstantTargetNone::get(TargetExtType *Ty) {
2030 assert(Ty->hasProperty(TargetExtType::HasZeroInit) &&
2031 "Target extension type not allowed to have a zeroinitializer");
2032 std::unique_ptr<ConstantTargetNone> &Entry =
2033 Ty->getContext().pImpl->CTNConstants[Ty];
2034 if (!Entry)
2035 Entry.reset(p: new ConstantTargetNone(Ty));
2036
2037 return Entry.get();
2038}
2039
2040/// Remove the constant from the constant table.
2041void ConstantTargetNone::destroyConstantImpl() {
2042 getContext().pImpl->CTNConstants.erase(Val: getType());
2043}
2044
2045UndefValue *UndefValue::get(Type *Ty) {
2046 std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
2047 if (!Entry)
2048 Entry.reset(p: new UndefValue(Ty));
2049
2050 return Entry.get();
2051}
2052
2053/// Remove the constant from the constant table.
2054void UndefValue::destroyConstantImpl() {
2055 // Free the constant and any dangling references to it.
2056 if (getValueID() == UndefValueVal) {
2057 getContext().pImpl->UVConstants.erase(Val: getType());
2058 } else if (getValueID() == PoisonValueVal) {
2059 getContext().pImpl->PVConstants.erase(Val: getType());
2060 }
2061 llvm_unreachable("Not a undef or a poison!");
2062}
2063
2064PoisonValue *PoisonValue::get(Type *Ty) {
2065 std::unique_ptr<PoisonValue> &Entry = Ty->getContext().pImpl->PVConstants[Ty];
2066 if (!Entry)
2067 Entry.reset(p: new PoisonValue(Ty));
2068
2069 return Entry.get();
2070}
2071
2072/// Remove the constant from the constant table.
2073void PoisonValue::destroyConstantImpl() {
2074 // Free the constant and any dangling references to it.
2075 getContext().pImpl->PVConstants.erase(Val: getType());
2076}
2077
2078BlockAddress *BlockAddress::get(Type *Ty, BasicBlock *BB) {
2079 BlockAddress *&BA = BB->getContext().pImpl->BlockAddresses[BB];
2080 if (!BA)
2081 BA = new BlockAddress(Ty, BB);
2082 return BA;
2083}
2084
2085BlockAddress *BlockAddress::get(BasicBlock *BB) {
2086 assert(BB->getParent() && "Block must have a parent");
2087 return get(Ty: BB->getParent()->getType(), BB);
2088}
2089
2090BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
2091 assert(BB->getParent() == F && "Block not part of specified function");
2092 return get(Ty: BB->getParent()->getType(), BB);
2093}
2094
2095BlockAddress::BlockAddress(Type *Ty, BasicBlock *BB)
2096 : Constant(Ty, Value::BlockAddressVal, AllocMarker) {
2097 Block = BB;
2098 BB->setHasAddressTaken(true);
2099}
2100
2101BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
2102 if (!BB->hasAddressTaken())
2103 return nullptr;
2104
2105 BlockAddress *BA = BB->getContext().pImpl->BlockAddresses.lookup(Val: BB);
2106 assert(BA && "Refcount and block address map disagree!");
2107 return BA;
2108}
2109
2110/// Remove the constant from the constant table.
2111void BlockAddress::destroyConstantImpl() {
2112 getType()->getContext().pImpl->BlockAddresses.erase(Val: getBasicBlock());
2113 getBasicBlock()->setHasAddressTaken(false);
2114}
2115
2116Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {
2117 assert(From == getBasicBlock());
2118 BasicBlock *NewBB = cast<BasicBlock>(Val: To);
2119
2120 // See if the 'new' entry already exists, if not, just update this in place
2121 // and return early.
2122 if (BlockAddress *NewBA = getContext().pImpl->BlockAddresses.lookup(Val: NewBB))
2123 return NewBA;
2124
2125 getBasicBlock()->setHasAddressTaken(false);
2126
2127 // erase invalidates iterators/references, hence the duplicate NewBB lookup.
2128 getContext().pImpl->BlockAddresses.erase(Val: getBasicBlock());
2129 getContext().pImpl->BlockAddresses[NewBB] = this;
2130 Block = NewBB;
2131 getBasicBlock()->setHasAddressTaken(true);
2132
2133 // If we just want to keep the existing value, then return null.
2134 // Callers know that this means we shouldn't delete this value.
2135 return nullptr;
2136}
2137
2138DSOLocalEquivalent *DSOLocalEquivalent::get(GlobalValue *GV) {
2139 DSOLocalEquivalent *&Equiv = GV->getContext().pImpl->DSOLocalEquivalents[GV];
2140 if (!Equiv)
2141 Equiv = new DSOLocalEquivalent(GV);
2142
2143 assert(Equiv->getGlobalValue() == GV &&
2144 "DSOLocalFunction does not match the expected global value");
2145 return Equiv;
2146}
2147
2148DSOLocalEquivalent::DSOLocalEquivalent(GlobalValue *GV)
2149 : Constant(GV->getType(), Value::DSOLocalEquivalentVal, AllocMarker) {
2150 setOperand(i_nocapture: 0, Val_nocapture: GV);
2151}
2152
2153/// Remove the constant from the constant table.
2154void DSOLocalEquivalent::destroyConstantImpl() {
2155 const GlobalValue *GV = getGlobalValue();
2156 GV->getContext().pImpl->DSOLocalEquivalents.erase(Val: GV);
2157}
2158
2159Value *DSOLocalEquivalent::handleOperandChangeImpl(Value *From, Value *To) {
2160 assert(From == getGlobalValue() && "Changing value does not match operand.");
2161 assert(isa<Constant>(To) && "Can only replace the operands with a constant");
2162
2163 // If the argument is replaced with a null value, just replace this constant
2164 // with a null value.
2165 if (isa<ConstantPointerNull>(Val: To))
2166 return To;
2167
2168 // The replacement could be a bitcast to another GlobalValue. We can
2169 // replace it with a bitcast to the dso_local_equivalent of that GV.
2170 GlobalValue *GV = cast<GlobalValue>(Val: To->stripPointerCasts());
2171 if (DSOLocalEquivalent *NewEquiv =
2172 getContext().pImpl->DSOLocalEquivalents.lookup(Val: GV))
2173 return llvm::ConstantExpr::getBitCast(C: NewEquiv, Ty: getType());
2174
2175 // erase invalidates iterators/references, hence the duplicate GV lookup.
2176 getContext().pImpl->DSOLocalEquivalents.erase(Val: getGlobalValue());
2177 getContext().pImpl->DSOLocalEquivalents[GV] = this;
2178 setOperand(i_nocapture: 0, Val_nocapture: GV);
2179
2180 if (GV->getType() != getType()) {
2181 // It is ok to mutate the type here because this constant should always
2182 // reflect the type of the function it's holding.
2183 mutateType(Ty: GV->getType());
2184 }
2185 return nullptr;
2186}
2187
2188NoCFIValue *NoCFIValue::get(GlobalValue *GV) {
2189 NoCFIValue *&NC = GV->getContext().pImpl->NoCFIValues[GV];
2190 if (!NC)
2191 NC = new NoCFIValue(GV);
2192
2193 assert(NC->getGlobalValue() == GV &&
2194 "NoCFIValue does not match the expected global value");
2195 return NC;
2196}
2197
2198NoCFIValue::NoCFIValue(GlobalValue *GV)
2199 : Constant(GV->getType(), Value::NoCFIValueVal, AllocMarker) {
2200 setOperand(i_nocapture: 0, Val_nocapture: GV);
2201}
2202
2203/// Remove the constant from the constant table.
2204void NoCFIValue::destroyConstantImpl() {
2205 const GlobalValue *GV = getGlobalValue();
2206 GV->getContext().pImpl->NoCFIValues.erase(Val: GV);
2207}
2208
2209Value *NoCFIValue::handleOperandChangeImpl(Value *From, Value *To) {
2210 assert(From == getGlobalValue() && "Changing value does not match operand.");
2211
2212 GlobalValue *GV = dyn_cast<GlobalValue>(Val: To->stripPointerCasts());
2213 assert(GV && "Can only replace the operands with a global value");
2214
2215 if (NoCFIValue *NewNC = getContext().pImpl->NoCFIValues.lookup(Val: GV))
2216 return llvm::ConstantExpr::getBitCast(C: NewNC, Ty: getType());
2217
2218 // erase invalidates iterators/references, hence the duplicate GV lookup.
2219 getContext().pImpl->NoCFIValues.erase(Val: getGlobalValue());
2220 getContext().pImpl->NoCFIValues[GV] = this;
2221 setOperand(i_nocapture: 0, Val_nocapture: GV);
2222
2223 if (GV->getType() != getType())
2224 mutateType(Ty: GV->getType());
2225
2226 return nullptr;
2227}
2228
2229//---- ConstantPtrAuth::get() implementations.
2230//
2231
2232ConstantPtrAuth *ConstantPtrAuth::get(Constant *Ptr, ConstantInt *Key,
2233 ConstantInt *Disc, Constant *AddrDisc,
2234 Constant *DeactivationSymbol) {
2235 Constant *ArgVec[] = {Ptr, Key, Disc, AddrDisc, DeactivationSymbol};
2236 ConstantPtrAuthKeyType MapKey(ArgVec);
2237 LLVMContextImpl *pImpl = Ptr->getContext().pImpl;
2238 return pImpl->ConstantPtrAuths.getOrCreate(Ty: Ptr->getType(), V: MapKey);
2239}
2240
2241ConstantPtrAuth *ConstantPtrAuth::getWithSameSchema(Constant *Pointer) const {
2242 return get(Ptr: Pointer, Key: getKey(), Disc: getDiscriminator(), AddrDisc: getAddrDiscriminator(),
2243 DeactivationSymbol: getDeactivationSymbol());
2244}
2245
2246ConstantPtrAuth::ConstantPtrAuth(Constant *Ptr, ConstantInt *Key,
2247 ConstantInt *Disc, Constant *AddrDisc,
2248 Constant *DeactivationSymbol)
2249 : Constant(Ptr->getType(), Value::ConstantPtrAuthVal, AllocMarker) {
2250 assert(Ptr->getType()->isPointerTy());
2251 assert(Key->getBitWidth() == 32);
2252 assert(Disc->getBitWidth() == 64);
2253 assert(AddrDisc->getType()->isPointerTy());
2254 assert(DeactivationSymbol->getType()->isPointerTy());
2255 setOperand(i_nocapture: 0, Val_nocapture: Ptr);
2256 setOperand(i_nocapture: 1, Val_nocapture: Key);
2257 setOperand(i_nocapture: 2, Val_nocapture: Disc);
2258 setOperand(i_nocapture: 3, Val_nocapture: AddrDisc);
2259 setOperand(i_nocapture: 4, Val_nocapture: DeactivationSymbol);
2260}
2261
2262/// Remove the constant from the constant table.
2263void ConstantPtrAuth::destroyConstantImpl() {
2264 getType()->getContext().pImpl->ConstantPtrAuths.remove(CP: this);
2265}
2266
2267Value *ConstantPtrAuth::handleOperandChangeImpl(Value *From, Value *ToV) {
2268 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2269 Constant *To = cast<Constant>(Val: ToV);
2270
2271 SmallVector<Constant *, 4> Values;
2272 Values.reserve(N: getNumOperands());
2273
2274 unsigned NumUpdated = 0;
2275
2276 Use *OperandList = getOperandList();
2277 unsigned OperandNo = 0;
2278 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
2279 Constant *Val = cast<Constant>(Val: O->get());
2280 if (Val == From) {
2281 OperandNo = (O - OperandList);
2282 Val = To;
2283 ++NumUpdated;
2284 }
2285 Values.push_back(Elt: Val);
2286 }
2287
2288 return getContext().pImpl->ConstantPtrAuths.replaceOperandsInPlace(
2289 Operands: Values, CP: this, From, To, NumUpdated, OperandNo);
2290}
2291
2292bool ConstantPtrAuth::hasSpecialAddressDiscriminator(uint64_t Value) const {
2293 const auto *CastV = dyn_cast<ConstantExpr>(Val: getAddrDiscriminator());
2294 if (!CastV || CastV->getOpcode() != Instruction::IntToPtr)
2295 return false;
2296
2297 const auto *IntVal = dyn_cast<ConstantInt>(Val: CastV->getOperand(i_nocapture: 0));
2298 if (!IntVal)
2299 return false;
2300
2301 return IntVal->getValue() == Value;
2302}
2303
2304bool ConstantPtrAuth::isKnownCompatibleWith(const Value *Key,
2305 const Value *Discriminator,
2306 const DataLayout &DL) const {
2307 // This function may only be validly called to analyze a ptrauth operation
2308 // with no deactivation symbol, so if we have one it isn't compatible.
2309 if (!isa<ConstantPointerNull>(Val: getDeactivationSymbol()))
2310 return false;
2311
2312 // If the keys are different, there's no chance for this to be compatible.
2313 if (getKey() != Key)
2314 return false;
2315
2316 // We can have 3 kinds of discriminators:
2317 // - simple, integer-only: `i64 x, ptr null` vs. `i64 x`
2318 // - address-only: `i64 0, ptr p` vs. `ptr p`
2319 // - blended address/integer: `i64 x, ptr p` vs. `@llvm.ptrauth.blend(p, x)`
2320
2321 // If this constant has a simple discriminator (integer, no address), easy:
2322 // it's compatible iff the provided full discriminator is also a simple
2323 // discriminator, identical to our integer discriminator.
2324 if (!hasAddressDiscriminator())
2325 return getDiscriminator() == Discriminator;
2326
2327 // Otherwise, we can isolate address and integer discriminator components.
2328 const Value *AddrDiscriminator = nullptr;
2329
2330 // This constant may or may not have an integer discriminator (instead of 0).
2331 if (!getDiscriminator()->isNullValue()) {
2332 // If it does, there's an implicit blend. We need to have a matching blend
2333 // intrinsic in the provided full discriminator.
2334 if (!match(V: Discriminator,
2335 P: m_Intrinsic<Intrinsic::ptrauth_blend>(
2336 Ops: m_Value(V&: AddrDiscriminator), Ops: m_Specific(V: getDiscriminator()))))
2337 return false;
2338 } else {
2339 // Otherwise, interpret the provided full discriminator as address-only.
2340 AddrDiscriminator = Discriminator;
2341 }
2342
2343 // Either way, we can now focus on comparing the address discriminators.
2344
2345 // Discriminators are i64, so the provided addr disc may be a ptrtoint.
2346 if (auto *Cast = dyn_cast<PtrToIntOperator>(Val: AddrDiscriminator))
2347 AddrDiscriminator = Cast->getPointerOperand();
2348
2349 // Beyond that, we're only interested in compatible pointers.
2350 if (getAddrDiscriminator()->getType() != AddrDiscriminator->getType())
2351 return false;
2352
2353 // These are often the same constant GEP, making them trivially equivalent.
2354 if (getAddrDiscriminator() == AddrDiscriminator)
2355 return true;
2356
2357 // Finally, they may be equivalent base+offset expressions.
2358 APInt Off1(DL.getIndexTypeSizeInBits(Ty: getAddrDiscriminator()->getType()), 0);
2359 auto *Base1 = getAddrDiscriminator()->stripAndAccumulateConstantOffsets(
2360 DL, Offset&: Off1, /*AllowNonInbounds=*/true);
2361
2362 APInt Off2(DL.getIndexTypeSizeInBits(Ty: AddrDiscriminator->getType()), 0);
2363 auto *Base2 = AddrDiscriminator->stripAndAccumulateConstantOffsets(
2364 DL, Offset&: Off2, /*AllowNonInbounds=*/true);
2365
2366 return Base1 == Base2 && Off1 == Off2;
2367}
2368
2369//---- ConstantExpr::get() implementations.
2370//
2371
2372/// This is a utility function to handle folding of casts and lookup of the
2373/// cast in the ExprConstants map. It is used by the various get* methods below.
2374static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
2375 bool OnlyIfReduced = false) {
2376 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
2377 // Fold a few common cases
2378 if (Constant *FC = ConstantFoldCastInstruction(opcode: opc, V: C, DestTy: Ty))
2379 return FC;
2380
2381 if (OnlyIfReduced)
2382 return nullptr;
2383
2384 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
2385
2386 // Look up the constant in the table first to ensure uniqueness.
2387 ConstantExprKeyType Key(opc, C);
2388
2389 return pImpl->ExprConstants.getOrCreate(Ty, V: Key);
2390}
2391
2392Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
2393 bool OnlyIfReduced) {
2394 Instruction::CastOps opc = Instruction::CastOps(oc);
2395 assert(Instruction::isCast(opc) && "opcode out of range");
2396 assert(isSupportedCastOp(opc) &&
2397 "Cast opcode not supported as constant expression");
2398 assert(C && Ty && "Null arguments to getCast");
2399 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
2400
2401 switch (opc) {
2402 default:
2403 llvm_unreachable("Invalid cast opcode");
2404 case Instruction::Trunc:
2405 return getTrunc(C, Ty, OnlyIfReduced);
2406 case Instruction::PtrToAddr:
2407 return getPtrToAddr(C, Ty, OnlyIfReduced);
2408 case Instruction::PtrToInt:
2409 return getPtrToInt(C, Ty, OnlyIfReduced);
2410 case Instruction::IntToPtr:
2411 return getIntToPtr(C, Ty, OnlyIfReduced);
2412 case Instruction::BitCast:
2413 return getBitCast(C, Ty, OnlyIfReduced);
2414 case Instruction::AddrSpaceCast:
2415 return getAddrSpaceCast(C, Ty, OnlyIfReduced);
2416 }
2417}
2418
2419Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
2420 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2421 return getBitCast(C, Ty);
2422 return getTrunc(C, Ty);
2423}
2424
2425Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
2426 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2427 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2428 "Invalid cast");
2429
2430 if (Ty->isIntOrIntVectorTy())
2431 return getPtrToInt(C: S, Ty);
2432
2433 unsigned SrcAS = S->getType()->getPointerAddressSpace();
2434 if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
2435 return getAddrSpaceCast(C: S, Ty);
2436
2437 return getBitCast(C: S, Ty);
2438}
2439
2440Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
2441 Type *Ty) {
2442 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2443 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2444
2445 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2446 return getAddrSpaceCast(C: S, Ty);
2447
2448 return getBitCast(C: S, Ty);
2449}
2450
2451Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
2452#ifndef NDEBUG
2453 bool fromVec = isa<VectorType>(C->getType());
2454 bool toVec = isa<VectorType>(Ty);
2455#endif
2456 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2457 assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
2458 assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
2459 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
2460 "SrcTy must be larger than DestTy for Trunc!");
2461
2462 return getFoldedCast(opc: Instruction::Trunc, C, Ty, OnlyIfReduced);
2463}
2464
2465Constant *ConstantExpr::getPtrToAddr(Constant *C, Type *DstTy,
2466 bool OnlyIfReduced) {
2467 assert(C->getType()->isPtrOrPtrVectorTy() &&
2468 "PtrToAddr source must be pointer or pointer vector");
2469 assert(DstTy->isIntOrIntVectorTy() &&
2470 "PtrToAddr destination must be integer or integer vector");
2471 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
2472 if (isa<VectorType>(Val: C->getType()))
2473 assert(cast<VectorType>(C->getType())->getElementCount() ==
2474 cast<VectorType>(DstTy)->getElementCount() &&
2475 "Invalid cast between a different number of vector elements");
2476 return getFoldedCast(opc: Instruction::PtrToAddr, C, Ty: DstTy, OnlyIfReduced);
2477}
2478
2479Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
2480 bool OnlyIfReduced) {
2481 assert(C->getType()->isPtrOrPtrVectorTy() &&
2482 "PtrToInt source must be pointer or pointer vector");
2483 assert(DstTy->isIntOrIntVectorTy() &&
2484 "PtrToInt destination must be integer or integer vector");
2485 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
2486 if (isa<VectorType>(Val: C->getType()))
2487 assert(cast<VectorType>(C->getType())->getElementCount() ==
2488 cast<VectorType>(DstTy)->getElementCount() &&
2489 "Invalid cast between a different number of vector elements");
2490 return getFoldedCast(opc: Instruction::PtrToInt, C, Ty: DstTy, OnlyIfReduced);
2491}
2492
2493Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
2494 bool OnlyIfReduced) {
2495 assert(C->getType()->isIntOrIntVectorTy() &&
2496 "IntToPtr source must be integer or integer vector");
2497 assert(DstTy->isPtrOrPtrVectorTy() &&
2498 "IntToPtr destination must be a pointer or pointer vector");
2499 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
2500 if (isa<VectorType>(Val: C->getType()))
2501 assert(cast<VectorType>(C->getType())->getElementCount() ==
2502 cast<VectorType>(DstTy)->getElementCount() &&
2503 "Invalid cast between a different number of vector elements");
2504 return getFoldedCast(opc: Instruction::IntToPtr, C, Ty: DstTy, OnlyIfReduced);
2505}
2506
2507Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
2508 bool OnlyIfReduced) {
2509 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
2510 "Invalid constantexpr bitcast!");
2511
2512 // It is common to ask for a bitcast of a value to its own type, handle this
2513 // speedily.
2514 if (C->getType() == DstTy) return C;
2515
2516 return getFoldedCast(opc: Instruction::BitCast, C, Ty: DstTy, OnlyIfReduced);
2517}
2518
2519Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
2520 bool OnlyIfReduced) {
2521 assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
2522 "Invalid constantexpr addrspacecast!");
2523 return getFoldedCast(opc: Instruction::AddrSpaceCast, C, Ty: DstTy, OnlyIfReduced);
2524}
2525
2526Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
2527 unsigned Flags, Type *OnlyIfReducedTy) {
2528 // Check the operands for consistency first.
2529 assert(Instruction::isBinaryOp(Opcode) &&
2530 "Invalid opcode in binary constant expression");
2531 assert(isSupportedBinOp(Opcode) &&
2532 "Binop not supported as constant expression");
2533 assert(C1->getType() == C2->getType() &&
2534 "Operand types in binary constant expression should match");
2535
2536#ifndef NDEBUG
2537 switch (Opcode) {
2538 case Instruction::Add:
2539 case Instruction::Sub:
2540 case Instruction::Mul:
2541 assert(C1->getType()->isIntOrIntVectorTy() &&
2542 "Tried to create an integer operation on a non-integer type!");
2543 break;
2544 case Instruction::And:
2545 case Instruction::Or:
2546 case Instruction::Xor:
2547 assert(C1->getType()->isIntOrIntVectorTy() &&
2548 "Tried to create a logical operation on a non-integral type!");
2549 break;
2550 default:
2551 break;
2552 }
2553#endif
2554
2555 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, V1: C1, V2: C2))
2556 return FC;
2557
2558 if (OnlyIfReducedTy == C1->getType())
2559 return nullptr;
2560
2561 Constant *ArgVec[] = {C1, C2};
2562 ConstantExprKeyType Key(Opcode, ArgVec, Flags);
2563
2564 LLVMContextImpl *pImpl = C1->getContext().pImpl;
2565 return pImpl->ExprConstants.getOrCreate(Ty: C1->getType(), V: Key);
2566}
2567
2568bool ConstantExpr::isDesirableBinOp(unsigned Opcode) {
2569 switch (Opcode) {
2570 case Instruction::UDiv:
2571 case Instruction::SDiv:
2572 case Instruction::URem:
2573 case Instruction::SRem:
2574 case Instruction::FAdd:
2575 case Instruction::FSub:
2576 case Instruction::FMul:
2577 case Instruction::FDiv:
2578 case Instruction::FRem:
2579 case Instruction::And:
2580 case Instruction::Or:
2581 case Instruction::LShr:
2582 case Instruction::AShr:
2583 case Instruction::Shl:
2584 case Instruction::Mul:
2585 return false;
2586 case Instruction::Add:
2587 case Instruction::Sub:
2588 case Instruction::Xor:
2589 return true;
2590 default:
2591 llvm_unreachable("Argument must be binop opcode");
2592 }
2593}
2594
2595bool ConstantExpr::isSupportedBinOp(unsigned Opcode) {
2596 switch (Opcode) {
2597 case Instruction::UDiv:
2598 case Instruction::SDiv:
2599 case Instruction::URem:
2600 case Instruction::SRem:
2601 case Instruction::FAdd:
2602 case Instruction::FSub:
2603 case Instruction::FMul:
2604 case Instruction::FDiv:
2605 case Instruction::FRem:
2606 case Instruction::And:
2607 case Instruction::Or:
2608 case Instruction::LShr:
2609 case Instruction::AShr:
2610 case Instruction::Shl:
2611 case Instruction::Mul:
2612 return false;
2613 case Instruction::Add:
2614 case Instruction::Sub:
2615 case Instruction::Xor:
2616 return true;
2617 default:
2618 llvm_unreachable("Argument must be binop opcode");
2619 }
2620}
2621
2622bool ConstantExpr::isDesirableCastOp(unsigned Opcode) {
2623 switch (Opcode) {
2624 case Instruction::ZExt:
2625 case Instruction::SExt:
2626 case Instruction::FPTrunc:
2627 case Instruction::FPExt:
2628 case Instruction::UIToFP:
2629 case Instruction::SIToFP:
2630 case Instruction::FPToUI:
2631 case Instruction::FPToSI:
2632 return false;
2633 case Instruction::Trunc:
2634 case Instruction::PtrToAddr:
2635 case Instruction::PtrToInt:
2636 case Instruction::IntToPtr:
2637 case Instruction::BitCast:
2638 case Instruction::AddrSpaceCast:
2639 return true;
2640 default:
2641 llvm_unreachable("Argument must be cast opcode");
2642 }
2643}
2644
2645bool ConstantExpr::isSupportedCastOp(unsigned Opcode) {
2646 switch (Opcode) {
2647 case Instruction::ZExt:
2648 case Instruction::SExt:
2649 case Instruction::FPTrunc:
2650 case Instruction::FPExt:
2651 case Instruction::UIToFP:
2652 case Instruction::SIToFP:
2653 case Instruction::FPToUI:
2654 case Instruction::FPToSI:
2655 return false;
2656 case Instruction::Trunc:
2657 case Instruction::PtrToAddr:
2658 case Instruction::PtrToInt:
2659 case Instruction::IntToPtr:
2660 case Instruction::BitCast:
2661 case Instruction::AddrSpaceCast:
2662 return true;
2663 default:
2664 llvm_unreachable("Argument must be cast opcode");
2665 }
2666}
2667
2668Constant *ConstantExpr::getSizeOf(Type* Ty) {
2669 // sizeof is implemented as: (i64) gep (Ty*)null, 1
2670 // Note that a non-inbounds gep is used, as null isn't within any object.
2671 Constant *GEPIdx = ConstantInt::get(Ty: Type::getInt32Ty(C&: Ty->getContext()), V: 1);
2672 LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
2673 Constant *GEP = getGetElementPtr(
2674 Ty, C: Constant::getNullValue(Ty: PointerType::getUnqual(C&: Ty->getContext())),
2675 Idx: GEPIdx);
2676 LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
2677 return getPtrToInt(C: GEP,
2678 DstTy: Type::getInt64Ty(C&: Ty->getContext()));
2679}
2680
2681Constant *ConstantExpr::getAlignOf(Type* Ty) {
2682 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
2683 // Note that a non-inbounds gep is used, as null isn't within any object.
2684 Type *AligningTy = StructType::get(elt1: Type::getInt1Ty(C&: Ty->getContext()), elts: Ty);
2685 Constant *NullPtr =
2686 Constant::getNullValue(Ty: PointerType::getUnqual(C&: AligningTy->getContext()));
2687 Constant *Zero = ConstantInt::get(Ty: Type::getInt64Ty(C&: Ty->getContext()), V: 0);
2688 Constant *One = ConstantInt::get(Ty: Type::getInt32Ty(C&: Ty->getContext()), V: 1);
2689 Constant *Indices[2] = {Zero, One};
2690 LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
2691 Constant *GEP = getGetElementPtr(Ty: AligningTy, C: NullPtr, IdxList: Indices);
2692 LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
2693 return getPtrToInt(C: GEP, DstTy: Type::getInt64Ty(C&: Ty->getContext()));
2694}
2695
2696Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
2697 ArrayRef<Value *> Idxs,
2698 GEPNoWrapFlags NW,
2699 std::optional<ConstantRange> InRange,
2700 Type *OnlyIfReducedTy) {
2701 assert(Ty && "Must specify element type");
2702 assert(isSupportedGetElementPtr(Ty) && "Element type is unsupported!");
2703
2704 if (Constant *FC = ConstantFoldGetElementPtr(Ty, C, InRange, Idxs))
2705 return FC; // Fold a few common cases.
2706
2707 assert(GetElementPtrInst::getIndexedType(Ty, Idxs) && "GEP indices invalid!");
2708 ;
2709
2710 // Get the result type of the getelementptr!
2711 Type *ReqTy = GetElementPtrInst::getGEPReturnType(Ptr: C, IdxList: Idxs);
2712 if (OnlyIfReducedTy == ReqTy)
2713 return nullptr;
2714
2715 auto EltCount = ElementCount::getFixed(MinVal: 0);
2716 if (VectorType *VecTy = dyn_cast<VectorType>(Val: ReqTy))
2717 EltCount = VecTy->getElementCount();
2718
2719 // Look up the constant in the table first to ensure uniqueness
2720 std::vector<Constant*> ArgVec;
2721 ArgVec.reserve(n: 1 + Idxs.size());
2722 ArgVec.push_back(x: C);
2723 auto GTI = gep_type_begin(Op0: Ty, A: Idxs), GTE = gep_type_end(Ty, A: Idxs);
2724 for (; GTI != GTE; ++GTI) {
2725 auto *Idx = cast<Constant>(Val: GTI.getOperand());
2726 assert(
2727 (!isa<VectorType>(Idx->getType()) ||
2728 cast<VectorType>(Idx->getType())->getElementCount() == EltCount) &&
2729 "getelementptr index type missmatch");
2730
2731 if (GTI.isStruct() && Idx->getType()->isVectorTy()) {
2732 Idx = Idx->getSplatValue();
2733 } else if (GTI.isSequential() && EltCount.isNonZero() &&
2734 !Idx->getType()->isVectorTy()) {
2735 Idx = ConstantVector::getSplat(EC: EltCount, V: Idx);
2736 }
2737 ArgVec.push_back(x: Idx);
2738 }
2739
2740 const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, NW.getRaw(),
2741 {}, Ty, InRange);
2742
2743 LLVMContextImpl *pImpl = C->getContext().pImpl;
2744 return pImpl->ExprConstants.getOrCreate(Ty: ReqTy, V: Key);
2745}
2746
2747Constant *ConstantExpr::getGetElementPtr(const DataLayout &DL, Type *Ty,
2748 Constant *C, ArrayRef<Constant *> Idxs,
2749 GEPNoWrapFlags NW,
2750 std::optional<ConstantRange> InRange,
2751 Type *OnlyIfReducedTy) {
2752 // Handle already canonical GEP.
2753 if (Ty->isIntegerTy(BitWidth: 8) && Idxs[0]->getType() == DL.getIndexType(PtrTy: C->getType()))
2754 return getPtrAdd(Ptr: C, Offset: Idxs[0], NW, InRange, OnlyIfReduced: OnlyIfReducedTy);
2755
2756 // Some API require an ArrayRef of Value * instead of Constant *.
2757 ArrayRef<Value *> ValIdxs =
2758 ArrayRef((Value *const *)Idxs.data(), Idxs.size());
2759 assert(GetElementPtrInst::getIndexedType(Ty, Idxs) && "GEP indices invalid!");
2760
2761 if (!isSupportedGetElementPtr(SrcElemTy: Ty))
2762 return nullptr;
2763
2764 Type *RetTy = GetElementPtrInst::getGEPReturnType(Ptr: C, IdxList: ValIdxs);
2765 Type *IdxTy = DL.getIndexType(PtrTy: RetTy);
2766
2767 Constant *Offset = Constant::getNullValue(Ty: IdxTy);
2768 auto GTI = gep_type_begin(Op0: Ty, A: ValIdxs), GTE = gep_type_end(Ty, A: ValIdxs);
2769 for (; GTI != GTE; ++GTI) {
2770 auto *Idx = cast<Constant>(Val: GTI.getOperand());
2771 if (Idx->isNullValue())
2772 continue;
2773
2774 if (StructType *STy = GTI.getStructTypeOrNull()) {
2775 uint64_t OpValue = Idx->getUniqueInteger().getZExtValue();
2776 uint64_t Size = DL.getStructLayout(Ty: STy)->getElementOffset(Idx: OpValue);
2777 if (!Size)
2778 continue;
2779
2780 Offset = ConstantFoldBinaryInstruction(Opcode: Instruction::Add, V1: Offset,
2781 V2: ConstantInt::get(Ty: IdxTy, V: Size));
2782 if (!Offset)
2783 return nullptr;
2784
2785 continue;
2786 }
2787
2788 // Splat the index if needed.
2789 if (IdxTy->isVectorTy() && !Idx->getType()->isVectorTy())
2790 Idx = ConstantVector::getSplat(EC: cast<VectorType>(Val: IdxTy)->getElementCount(),
2791 V: Idx);
2792
2793 // Convert to correct type.
2794 if (Idx->getType() != IdxTy) {
2795 Idx = ConstantFoldCastInstruction(opcode: Idx->getType()->getScalarSizeInBits() <
2796 IdxTy->getScalarSizeInBits()
2797 ? Instruction::SExt
2798 : Instruction::Trunc,
2799 V: Idx, DestTy: IdxTy);
2800 if (!Idx)
2801 return nullptr;
2802 }
2803
2804 TypeSize TySize = GTI.getSequentialElementStride(DL);
2805 if (TySize.isScalable())
2806 return nullptr;
2807
2808 // Multiply by scale.
2809 if (TySize != TypeSize::getFixed(ExactSize: 1)) {
2810 Constant *Scale = ConstantInt::getSigned(Ty: IdxTy, V: TySize.getFixedValue(),
2811 /*ImplicitTrunc=*/true);
2812 Idx = ConstantFoldBinaryInstruction(Opcode: Instruction::Mul, V1: Idx, V2: Scale);
2813 if (!Idx)
2814 return nullptr;
2815 }
2816
2817 Offset = ConstantFoldBinaryInstruction(Opcode: Instruction::Add, V1: Offset, V2: Idx);
2818 if (!Offset)
2819 return nullptr;
2820 }
2821
2822 return getPtrAdd(Ptr: C, Offset, NW, InRange, OnlyIfReduced: OnlyIfReducedTy);
2823}
2824
2825Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
2826 Type *OnlyIfReducedTy) {
2827 assert(Val->getType()->isVectorTy() &&
2828 "Tried to create extractelement operation on non-vector type!");
2829 assert(Idx->getType()->isIntegerTy() &&
2830 "Extractelement index must be an integer type!");
2831
2832 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2833 return FC; // Fold a few common cases.
2834
2835 Type *ReqTy = cast<VectorType>(Val: Val->getType())->getElementType();
2836 if (OnlyIfReducedTy == ReqTy)
2837 return nullptr;
2838
2839 // Look up the constant in the table first to ensure uniqueness
2840 Constant *ArgVec[] = { Val, Idx };
2841 const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
2842
2843 LLVMContextImpl *pImpl = Val->getContext().pImpl;
2844 return pImpl->ExprConstants.getOrCreate(Ty: ReqTy, V: Key);
2845}
2846
2847Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2848 Constant *Idx, Type *OnlyIfReducedTy) {
2849 assert(Val->getType()->isVectorTy() &&
2850 "Tried to create insertelement operation on non-vector type!");
2851 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType() &&
2852 "Insertelement types must match!");
2853 assert(Idx->getType()->isIntegerTy() &&
2854 "Insertelement index must be i32 type!");
2855
2856 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2857 return FC; // Fold a few common cases.
2858
2859 if (OnlyIfReducedTy == Val->getType())
2860 return nullptr;
2861
2862 // Look up the constant in the table first to ensure uniqueness
2863 Constant *ArgVec[] = { Val, Elt, Idx };
2864 const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
2865
2866 LLVMContextImpl *pImpl = Val->getContext().pImpl;
2867 return pImpl->ExprConstants.getOrCreate(Ty: Val->getType(), V: Key);
2868}
2869
2870Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2871 ArrayRef<int> Mask,
2872 Type *OnlyIfReducedTy) {
2873 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2874 "Invalid shuffle vector constant expr operands!");
2875
2876 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2877 return FC; // Fold a few common cases.
2878
2879 unsigned NElts = Mask.size();
2880 auto V1VTy = cast<VectorType>(Val: V1->getType());
2881 Type *EltTy = V1VTy->getElementType();
2882 bool TypeIsScalable = isa<ScalableVectorType>(Val: V1VTy);
2883 Type *ShufTy = VectorType::get(ElementType: EltTy, NumElements: NElts, Scalable: TypeIsScalable);
2884
2885 if (OnlyIfReducedTy == ShufTy)
2886 return nullptr;
2887
2888 // Look up the constant in the table first to ensure uniqueness
2889 Constant *ArgVec[] = {V1, V2};
2890 ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec, 0, Mask);
2891
2892 LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
2893 return pImpl->ExprConstants.getOrCreate(Ty: ShufTy, V: Key);
2894}
2895
2896Constant *ConstantExpr::getNeg(Constant *C, bool HasNSW) {
2897 assert(C->getType()->isIntOrIntVectorTy() &&
2898 "Cannot NEG a nonintegral value!");
2899 return getSub(C1: ConstantInt::get(Ty: C->getType(), V: 0), C2: C, /*HasNUW=*/false, HasNSW);
2900}
2901
2902Constant *ConstantExpr::getNot(Constant *C) {
2903 assert(C->getType()->isIntOrIntVectorTy() &&
2904 "Cannot NOT a nonintegral value!");
2905 return get(Opcode: Instruction::Xor, C1: C, C2: Constant::getAllOnesValue(Ty: C->getType()));
2906}
2907
2908Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
2909 bool HasNUW, bool HasNSW) {
2910 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2911 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2912 return get(Opcode: Instruction::Add, C1, C2, Flags);
2913}
2914
2915Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
2916 bool HasNUW, bool HasNSW) {
2917 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2918 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2919 return get(Opcode: Instruction::Sub, C1, C2, Flags);
2920}
2921
2922Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
2923 return get(Opcode: Instruction::Xor, C1, C2);
2924}
2925
2926Constant *ConstantExpr::getExactLogBase2(Constant *C) {
2927 Type *Ty = C->getType();
2928 const APInt *IVal;
2929 if (match(V: C, P: m_APInt(Res&: IVal)) && IVal->isPowerOf2())
2930 return ConstantInt::get(Ty, V: IVal->logBase2());
2931
2932 // FIXME: We can extract pow of 2 of splat constant for scalable vectors.
2933 auto *VecTy = dyn_cast<FixedVectorType>(Val: Ty);
2934 if (!VecTy)
2935 return nullptr;
2936
2937 SmallVector<Constant *, 4> Elts;
2938 for (unsigned I = 0, E = VecTy->getNumElements(); I != E; ++I) {
2939 Constant *Elt = C->getAggregateElement(Elt: I);
2940 if (!Elt)
2941 return nullptr;
2942 // Note that log2(iN undef) is *NOT* iN undef, because log2(iN undef) u< N.
2943 if (isa<UndefValue>(Val: Elt)) {
2944 Elts.push_back(Elt: Constant::getNullValue(Ty: Ty->getScalarType()));
2945 continue;
2946 }
2947 if (!match(V: Elt, P: m_APInt(Res&: IVal)) || !IVal->isPowerOf2())
2948 return nullptr;
2949 Elts.push_back(Elt: ConstantInt::get(Ty: Ty->getScalarType(), V: IVal->logBase2()));
2950 }
2951
2952 return ConstantVector::get(V: Elts);
2953}
2954
2955Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty,
2956 bool AllowRHSConstant, bool NSZ) {
2957 assert(Instruction::isBinaryOp(Opcode) && "Only binops allowed");
2958
2959 // Commutative opcodes: it does not matter if AllowRHSConstant is set.
2960 if (Instruction::isCommutative(Opcode)) {
2961 switch (Opcode) {
2962 case Instruction::Add: // X + 0 = X
2963 case Instruction::Or: // X | 0 = X
2964 case Instruction::Xor: // X ^ 0 = X
2965 return Constant::getNullValue(Ty);
2966 case Instruction::Mul: // X * 1 = X
2967 return ConstantInt::get(Ty, V: 1);
2968 case Instruction::And: // X & -1 = X
2969 return Constant::getAllOnesValue(Ty);
2970 case Instruction::FAdd: // X + -0.0 = X
2971 return ConstantFP::getZero(Ty, Negative: !NSZ);
2972 case Instruction::FMul: // X * 1.0 = X
2973 return ConstantFP::get(Ty, V: 1.0);
2974 default:
2975 llvm_unreachable("Every commutative binop has an identity constant");
2976 }
2977 }
2978
2979 // Non-commutative opcodes: AllowRHSConstant must be set.
2980 if (!AllowRHSConstant)
2981 return nullptr;
2982
2983 switch (Opcode) {
2984 case Instruction::Sub: // X - 0 = X
2985 case Instruction::Shl: // X << 0 = X
2986 case Instruction::LShr: // X >>u 0 = X
2987 case Instruction::AShr: // X >> 0 = X
2988 case Instruction::FSub: // X - 0.0 = X
2989 return Constant::getNullValue(Ty);
2990 case Instruction::SDiv: // X / 1 = X
2991 case Instruction::UDiv: // X /u 1 = X
2992 return ConstantInt::get(Ty, V: 1);
2993 case Instruction::FDiv: // X / 1.0 = X
2994 return ConstantFP::get(Ty, V: 1.0);
2995 default:
2996 return nullptr;
2997 }
2998}
2999
3000Constant *ConstantExpr::getIntrinsicIdentity(Intrinsic::ID ID, Type *Ty) {
3001 switch (ID) {
3002 case Intrinsic::umax:
3003 return Constant::getNullValue(Ty);
3004 case Intrinsic::umin:
3005 return Constant::getAllOnesValue(Ty);
3006 case Intrinsic::smax:
3007 return Constant::getIntegerValue(
3008 Ty, V: APInt::getSignedMinValue(numBits: Ty->getScalarSizeInBits()));
3009 case Intrinsic::smin:
3010 return Constant::getIntegerValue(
3011 Ty, V: APInt::getSignedMaxValue(numBits: Ty->getScalarSizeInBits()));
3012 default:
3013 return nullptr;
3014 }
3015}
3016
3017Constant *ConstantExpr::getIdentity(Instruction *I, Type *Ty,
3018 bool AllowRHSConstant, bool NSZ) {
3019 if (I->isBinaryOp())
3020 return getBinOpIdentity(Opcode: I->getOpcode(), Ty, AllowRHSConstant, NSZ);
3021 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Val: I))
3022 return getIntrinsicIdentity(ID: II->getIntrinsicID(), Ty);
3023 return nullptr;
3024}
3025
3026Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty,
3027 bool AllowLHSConstant) {
3028 switch (Opcode) {
3029 default:
3030 break;
3031
3032 case Instruction::Or: // -1 | X = -1
3033 return Constant::getAllOnesValue(Ty);
3034
3035 case Instruction::And: // 0 & X = 0
3036 case Instruction::Mul: // 0 * X = 0
3037 return Constant::getNullValue(Ty);
3038 }
3039
3040 // AllowLHSConstant must be set.
3041 if (!AllowLHSConstant)
3042 return nullptr;
3043
3044 switch (Opcode) {
3045 default:
3046 return nullptr;
3047 case Instruction::Shl: // 0 << X = 0
3048 case Instruction::LShr: // 0 >>l X = 0
3049 case Instruction::AShr: // 0 >>a X = 0
3050 case Instruction::SDiv: // 0 /s X = 0
3051 case Instruction::UDiv: // 0 /u X = 0
3052 case Instruction::URem: // 0 %u X = 0
3053 case Instruction::SRem: // 0 %s X = 0
3054 return Constant::getNullValue(Ty);
3055 }
3056}
3057
3058/// Remove the constant from the constant table.
3059void ConstantExpr::destroyConstantImpl() {
3060 getType()->getContext().pImpl->ExprConstants.remove(CP: this);
3061}
3062
3063const char *ConstantExpr::getOpcodeName() const {
3064 return Instruction::getOpcodeName(Opcode: getOpcode());
3065}
3066
3067GetElementPtrConstantExpr::GetElementPtrConstantExpr(
3068 Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy,
3069 std::optional<ConstantRange> InRange, AllocInfo AllocInfo)
3070 : ConstantExpr(DestTy, Instruction::GetElementPtr, AllocInfo),
3071 SrcElementTy(SrcElementTy),
3072 ResElementTy(GetElementPtrInst::getIndexedType(Ty: SrcElementTy, IdxList)),
3073 InRange(std::move(InRange)) {
3074 Op<0>() = C;
3075 Use *OperandList = getOperandList();
3076 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
3077 OperandList[i+1] = IdxList[i];
3078}
3079
3080Type *GetElementPtrConstantExpr::getSourceElementType() const {
3081 return SrcElementTy;
3082}
3083
3084Type *GetElementPtrConstantExpr::getResultElementType() const {
3085 return ResElementTy;
3086}
3087
3088std::optional<ConstantRange> GetElementPtrConstantExpr::getInRange() const {
3089 return InRange;
3090}
3091
3092//===----------------------------------------------------------------------===//
3093// ConstantData* implementations
3094
3095Type *ConstantDataSequential::getElementType() const {
3096 if (ArrayType *ATy = dyn_cast<ArrayType>(Val: getType()))
3097 return ATy->getElementType();
3098 return cast<VectorType>(Val: getType())->getElementType();
3099}
3100
3101StringRef ConstantDataSequential::getRawDataValues() const {
3102 return StringRef(DataElements, getNumElements()*getElementByteSize());
3103}
3104
3105bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
3106 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() || Ty->isDoubleTy())
3107 return true;
3108 if (auto *IT = dyn_cast<IntegerType>(Val: Ty)) {
3109 switch (IT->getBitWidth()) {
3110 case 8:
3111 case 16:
3112 case 32:
3113 case 64:
3114 return true;
3115 default: break;
3116 }
3117 }
3118 if (auto *IT = dyn_cast<ByteType>(Val: Ty)) {
3119 switch (IT->getBitWidth()) {
3120 case 8:
3121 case 16:
3122 case 32:
3123 case 64:
3124 return true;
3125 default:
3126 break;
3127 }
3128 }
3129 return false;
3130}
3131
3132uint64_t ConstantDataSequential::getNumElements() const {
3133 if (ArrayType *AT = dyn_cast<ArrayType>(Val: getType()))
3134 return AT->getNumElements();
3135 return cast<FixedVectorType>(Val: getType())->getNumElements();
3136}
3137
3138uint64_t ConstantDataSequential::getElementByteSize() const {
3139 return getElementType()->getPrimitiveSizeInBits().getFixedValue() / 8;
3140}
3141
3142/// Return the start of the specified element.
3143const char *ConstantDataSequential::getElementPointer(uint64_t Elt) const {
3144 assert(Elt < getNumElements() && "Invalid Elt");
3145 return DataElements + Elt * getElementByteSize();
3146}
3147
3148/// Return true if the array is empty or all zeros.
3149static bool isAllZeros(StringRef Arr) {
3150 for (char I : Arr)
3151 if (I != 0)
3152 return false;
3153 return true;
3154}
3155
3156/// This is the underlying implementation of all of the
3157/// ConstantDataSequential::get methods. They all thunk down to here, providing
3158/// the correct element type. We take the bytes in as a StringRef because
3159/// we *want* an underlying "char*" to avoid TBAA type punning violations.
3160Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
3161#ifndef NDEBUG
3162 if (ArrayType *ATy = dyn_cast<ArrayType>(Ty))
3163 assert(isElementTypeCompatible(ATy->getElementType()));
3164 else
3165 assert(isElementTypeCompatible(cast<VectorType>(Ty)->getElementType()));
3166#endif
3167 // If the elements are all zero or there are no elements, return a CAZ, which
3168 // is more dense and canonical.
3169 if (isAllZeros(Arr: Elements))
3170 return ConstantAggregateZero::get(Ty);
3171
3172 // Do a lookup to see if we have already formed one of these.
3173 auto &Slot =
3174 *Ty->getContext().pImpl->CDSConstants.try_emplace(Key: Elements).first;
3175
3176 // The bucket can point to a linked list of different CDS's that have the same
3177 // body but different types. For example, 0,0,0,1 could be a 4 element array
3178 // of i8, or a 1-element array of i32. They'll both end up in the same
3179 /// StringMap bucket, linked up by their Next pointers. Walk the list.
3180 std::unique_ptr<ConstantDataSequential> *Entry = &Slot.second;
3181 for (; *Entry; Entry = &(*Entry)->Next)
3182 if ((*Entry)->getType() == Ty)
3183 return Entry->get();
3184
3185 // Okay, we didn't get a hit. Create a node of the right class, link it in,
3186 // and return it.
3187 if (isa<ArrayType>(Val: Ty)) {
3188 // Use reset because std::make_unique can't access the constructor.
3189 Entry->reset(p: new ConstantDataArray(Ty, Slot.first().data()));
3190 return Entry->get();
3191 }
3192
3193 assert(isa<VectorType>(Ty));
3194 // Use reset because std::make_unique can't access the constructor.
3195 Entry->reset(p: new ConstantDataVector(Ty, Slot.first().data()));
3196 return Entry->get();
3197}
3198
3199void ConstantDataSequential::destroyConstantImpl() {
3200 // Remove the constant from the StringMap.
3201 StringMap<std::unique_ptr<ConstantDataSequential>> &CDSConstants =
3202 getType()->getContext().pImpl->CDSConstants;
3203
3204 auto Slot = CDSConstants.find(Key: getRawDataValues());
3205
3206 assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
3207
3208 std::unique_ptr<ConstantDataSequential> *Entry = &Slot->getValue();
3209
3210 // Remove the entry from the hash table.
3211 if (!(*Entry)->Next) {
3212 // If there is only one value in the bucket (common case) it must be this
3213 // entry, and removing the entry should remove the bucket completely.
3214 assert(Entry->get() == this && "Hash mismatch in ConstantDataSequential");
3215 getContext().pImpl->CDSConstants.erase(I: Slot);
3216 return;
3217 }
3218
3219 // Otherwise, there are multiple entries linked off the bucket, unlink the
3220 // node we care about but keep the bucket around.
3221 while (true) {
3222 std::unique_ptr<ConstantDataSequential> &Node = *Entry;
3223 assert(Node && "Didn't find entry in its uniquing hash table!");
3224 // If we found our entry, unlink it from the list and we're done.
3225 if (Node.get() == this) {
3226 Node = std::move(Node->Next);
3227 return;
3228 }
3229
3230 Entry = &Node->Next;
3231 }
3232}
3233
3234/// getFP() constructors - Return a constant of array type with a float
3235/// element type taken from argument `ElementType', and count taken from
3236/// argument `Elts'. The amount of bits of the contained type must match the
3237/// number of bits of the type contained in the passed in ArrayRef.
3238/// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
3239/// that this can return a ConstantAggregateZero object.
3240Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint16_t> Elts) {
3241 assert((ElementType->isHalfTy() || ElementType->isBFloatTy()) &&
3242 "Element type is not a 16-bit float type");
3243 Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size());
3244 const char *Data = reinterpret_cast<const char *>(Elts.data());
3245 return getImpl(Elements: StringRef(Data, Elts.size() * 2), Ty);
3246}
3247Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint32_t> Elts) {
3248 assert(ElementType->isFloatTy() && "Element type is not a 32-bit float type");
3249 Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size());
3250 const char *Data = reinterpret_cast<const char *>(Elts.data());
3251 return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty);
3252}
3253Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint64_t> Elts) {
3254 assert(ElementType->isDoubleTy() &&
3255 "Element type is not a 64-bit float type");
3256 Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size());
3257 const char *Data = reinterpret_cast<const char *>(Elts.data());
3258 return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty);
3259}
3260
3261/// getByte() constructors - Return a constant of array type with a byte
3262/// element type taken from argument `ElementType', and count taken from
3263/// argument `Elts'. The amount of bits of the contained type must match the
3264/// number of bits of the type contained in the passed in ArrayRef.
3265/// Note that this can return a ConstantAggregateZero object.
3266Constant *ConstantDataArray::getByte(Type *ElementType,
3267 ArrayRef<uint8_t> Elts) {
3268 assert(ElementType->isByteTy(8) && "Element type is not a 8-bit byte type");
3269 Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size());
3270 const char *Data = reinterpret_cast<const char *>(Elts.data());
3271 return getImpl(Elements: StringRef(Data, Elts.size() * 1), Ty);
3272}
3273Constant *ConstantDataArray::getByte(Type *ElementType,
3274 ArrayRef<uint16_t> Elts) {
3275 assert(ElementType->isByteTy(16) && "Element type is not a 16-bit byte type");
3276 Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size());
3277 const char *Data = reinterpret_cast<const char *>(Elts.data());
3278 return getImpl(Elements: StringRef(Data, Elts.size() * 2), Ty);
3279}
3280Constant *ConstantDataArray::getByte(Type *ElementType,
3281 ArrayRef<uint32_t> Elts) {
3282 assert(ElementType->isByteTy(32) && "Element type is not a 32-bit byte type");
3283 Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size());
3284 const char *Data = reinterpret_cast<const char *>(Elts.data());
3285 return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty);
3286}
3287Constant *ConstantDataArray::getByte(Type *ElementType,
3288 ArrayRef<uint64_t> Elts) {
3289 assert(ElementType->isByteTy(64) && "Element type is not a 64-bit byte type");
3290 Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size());
3291 const char *Data = reinterpret_cast<const char *>(Elts.data());
3292 return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty);
3293}
3294
3295Constant *ConstantDataArray::getString(LLVMContext &Context, StringRef Str,
3296 bool AddNull, bool ByteString) {
3297 if (!AddNull) {
3298 const uint8_t *Data = Str.bytes_begin();
3299 return ByteString
3300 ? getByte(ElementType: Type::getByte8Ty(C&: Context), Elts: ArrayRef(Data, Str.size()))
3301 : get(Context, Elts: ArrayRef(Data, Str.size()));
3302 }
3303
3304 SmallVector<uint8_t, 64> ElementVals;
3305 ElementVals.append(in_start: Str.begin(), in_end: Str.end());
3306 ElementVals.push_back(Elt: 0);
3307 return ByteString ? getByte(ElementType: Type::getByte8Ty(C&: Context), Elts: ElementVals)
3308 : get(Context, Elts&: ElementVals);
3309}
3310
3311/// get() constructors - Return a constant with vector type with an element
3312/// count and element type matching the ArrayRef passed in. Note that this
3313/// can return a ConstantAggregateZero object.
3314Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
3315 auto *Ty = FixedVectorType::get(ElementType: Type::getInt8Ty(C&: Context), NumElts: Elts.size());
3316 const char *Data = reinterpret_cast<const char *>(Elts.data());
3317 return getImpl(Elements: StringRef(Data, Elts.size() * 1), Ty);
3318}
3319Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
3320 auto *Ty = FixedVectorType::get(ElementType: Type::getInt16Ty(C&: Context), NumElts: Elts.size());
3321 const char *Data = reinterpret_cast<const char *>(Elts.data());
3322 return getImpl(Elements: StringRef(Data, Elts.size() * 2), Ty);
3323}
3324Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
3325 auto *Ty = FixedVectorType::get(ElementType: Type::getInt32Ty(C&: Context), NumElts: Elts.size());
3326 const char *Data = reinterpret_cast<const char *>(Elts.data());
3327 return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty);
3328}
3329Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
3330 auto *Ty = FixedVectorType::get(ElementType: Type::getInt64Ty(C&: Context), NumElts: Elts.size());
3331 const char *Data = reinterpret_cast<const char *>(Elts.data());
3332 return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty);
3333}
3334Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
3335 auto *Ty = FixedVectorType::get(ElementType: Type::getFloatTy(C&: Context), NumElts: Elts.size());
3336 const char *Data = reinterpret_cast<const char *>(Elts.data());
3337 return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty);
3338}
3339Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
3340 auto *Ty = FixedVectorType::get(ElementType: Type::getDoubleTy(C&: Context), NumElts: Elts.size());
3341 const char *Data = reinterpret_cast<const char *>(Elts.data());
3342 return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty);
3343}
3344
3345/// getByte() constructors - Return a constant of vector type with a byte
3346/// element type taken from argument `ElementType', and count taken from
3347/// argument `Elts'. The amount of bits of the contained type must match the
3348/// number of bits of the type contained in the passed in ArrayRef.
3349/// Note that this can return a ConstantAggregateZero object.
3350Constant *ConstantDataVector::getByte(Type *ElementType,
3351 ArrayRef<uint8_t> Elts) {
3352 assert(ElementType->isByteTy(8) && "Element type is not a 8-bit byte");
3353 auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size());
3354 const char *Data = reinterpret_cast<const char *>(Elts.data());
3355 return getImpl(Elements: StringRef(Data, Elts.size() * 1), Ty);
3356}
3357Constant *ConstantDataVector::getByte(Type *ElementType,
3358 ArrayRef<uint16_t> Elts) {
3359 assert(ElementType->isByteTy(16) && "Element type is not a 16-bit byte");
3360 auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size());
3361 const char *Data = reinterpret_cast<const char *>(Elts.data());
3362 return getImpl(Elements: StringRef(Data, Elts.size() * 2), Ty);
3363}
3364Constant *ConstantDataVector::getByte(Type *ElementType,
3365 ArrayRef<uint32_t> Elts) {
3366 assert(ElementType->isByteTy(32) && "Element type is not a 32-bit byte");
3367 auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size());
3368 const char *Data = reinterpret_cast<const char *>(Elts.data());
3369 return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty);
3370}
3371Constant *ConstantDataVector::getByte(Type *ElementType,
3372 ArrayRef<uint64_t> Elts) {
3373 assert(ElementType->isByteTy(64) && "Element type is not a 64-bit byte");
3374 auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size());
3375 const char *Data = reinterpret_cast<const char *>(Elts.data());
3376 return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty);
3377}
3378
3379/// getFP() constructors - Return a constant of vector type with a float
3380/// element type taken from argument `ElementType', and count taken from
3381/// argument `Elts'. The amount of bits of the contained type must match the
3382/// number of bits of the type contained in the passed in ArrayRef.
3383/// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
3384/// that this can return a ConstantAggregateZero object.
3385Constant *ConstantDataVector::getFP(Type *ElementType,
3386 ArrayRef<uint16_t> Elts) {
3387 assert((ElementType->isHalfTy() || ElementType->isBFloatTy()) &&
3388 "Element type is not a 16-bit float type");
3389 auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size());
3390 const char *Data = reinterpret_cast<const char *>(Elts.data());
3391 return getImpl(Elements: StringRef(Data, Elts.size() * 2), Ty);
3392}
3393Constant *ConstantDataVector::getFP(Type *ElementType,
3394 ArrayRef<uint32_t> Elts) {
3395 assert(ElementType->isFloatTy() && "Element type is not a 32-bit float type");
3396 auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size());
3397 const char *Data = reinterpret_cast<const char *>(Elts.data());
3398 return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty);
3399}
3400Constant *ConstantDataVector::getFP(Type *ElementType,
3401 ArrayRef<uint64_t> Elts) {
3402 assert(ElementType->isDoubleTy() &&
3403 "Element type is not a 64-bit float type");
3404 auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size());
3405 const char *Data = reinterpret_cast<const char *>(Elts.data());
3406 return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty);
3407}
3408
3409Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
3410 assert(isElementTypeCompatible(V->getType()) &&
3411 "Element type not compatible with ConstantData");
3412 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: V)) {
3413 if (CI->getType()->isIntegerTy(BitWidth: 8)) {
3414 SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
3415 return get(Context&: V->getContext(), Elts);
3416 }
3417 if (CI->getType()->isIntegerTy(BitWidth: 16)) {
3418 SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
3419 return get(Context&: V->getContext(), Elts);
3420 }
3421 if (CI->getType()->isIntegerTy(BitWidth: 32)) {
3422 SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
3423 return get(Context&: V->getContext(), Elts);
3424 }
3425 assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
3426 SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
3427 return get(Context&: V->getContext(), Elts);
3428 }
3429
3430 if (ConstantByte *CB = dyn_cast<ConstantByte>(Val: V)) {
3431 if (CB->getType()->isByteTy(BitWidth: 8)) {
3432 SmallVector<uint8_t, 16> Elts(NumElts, CB->getZExtValue());
3433 return getByte(ElementType: V->getType(), Elts);
3434 }
3435 if (CB->getType()->isByteTy(BitWidth: 16)) {
3436 SmallVector<uint16_t, 16> Elts(NumElts, CB->getZExtValue());
3437 return getByte(ElementType: V->getType(), Elts);
3438 }
3439 if (CB->getType()->isByteTy(BitWidth: 32)) {
3440 SmallVector<uint32_t, 16> Elts(NumElts, CB->getZExtValue());
3441 return getByte(ElementType: V->getType(), Elts);
3442 }
3443 assert(CB->getType()->isByteTy(64) && "Unsupported ConstantData type");
3444 SmallVector<uint64_t, 16> Elts(NumElts, CB->getZExtValue());
3445 return getByte(ElementType: V->getType(), Elts);
3446 }
3447
3448 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Val: V)) {
3449 if (CFP->getType()->isHalfTy()) {
3450 SmallVector<uint16_t, 16> Elts(
3451 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
3452 return getFP(ElementType: V->getType(), Elts);
3453 }
3454 if (CFP->getType()->isBFloatTy()) {
3455 SmallVector<uint16_t, 16> Elts(
3456 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
3457 return getFP(ElementType: V->getType(), Elts);
3458 }
3459 if (CFP->getType()->isFloatTy()) {
3460 SmallVector<uint32_t, 16> Elts(
3461 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
3462 return getFP(ElementType: V->getType(), Elts);
3463 }
3464 if (CFP->getType()->isDoubleTy()) {
3465 SmallVector<uint64_t, 16> Elts(
3466 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
3467 return getFP(ElementType: V->getType(), Elts);
3468 }
3469 }
3470 return ConstantVector::getSplat(EC: ElementCount::getFixed(MinVal: NumElts), V);
3471}
3472
3473uint64_t ConstantDataSequential::getElementAsInteger(uint64_t Elt) const {
3474 assert(
3475 (isa<IntegerType>(getElementType()) || isa<ByteType>(getElementType())) &&
3476 "Accessor can only be used when element is an integer or byte");
3477 const char *EltPtr = getElementPointer(Elt);
3478
3479 // The data is stored in host byte order, make sure to cast back to the right
3480 // type to load with the right endianness.
3481 switch (getElementByteSize()) {
3482 default: llvm_unreachable("Invalid bitwidth for CDS");
3483 case 1:
3484 return *reinterpret_cast<const uint8_t *>(EltPtr);
3485 case 2:
3486 return *reinterpret_cast<const uint16_t *>(EltPtr);
3487 case 4:
3488 return *reinterpret_cast<const uint32_t *>(EltPtr);
3489 case 8:
3490 return *reinterpret_cast<const uint64_t *>(EltPtr);
3491 }
3492}
3493
3494APInt ConstantDataSequential::getElementAsAPInt(uint64_t Elt) const {
3495 assert(
3496 (isa<IntegerType>(getElementType()) || isa<ByteType>(getElementType())) &&
3497 "Accessor can only be used when element is an integer or byte");
3498 const char *EltPtr = getElementPointer(Elt);
3499
3500 // The data is stored in host byte order, make sure to cast back to the right
3501 // type to load with the right endianness.
3502 switch (getElementByteSize()) {
3503 default: llvm_unreachable("Invalid bitwidth for CDS");
3504 case 1: {
3505 auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr);
3506 return APInt(8, EltVal);
3507 }
3508 case 2: {
3509 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
3510 return APInt(16, EltVal);
3511 }
3512 case 4: {
3513 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
3514 return APInt(32, EltVal);
3515 }
3516 case 8: {
3517 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
3518 return APInt(64, EltVal);
3519 }
3520 }
3521}
3522
3523APFloat ConstantDataSequential::getElementAsAPFloat(uint64_t Elt) const {
3524 const char *EltPtr = getElementPointer(Elt);
3525
3526 switch (getElementType()->getTypeID()) {
3527 default:
3528 llvm_unreachable("Accessor can only be used when element is float/double!");
3529 case Type::HalfTyID: {
3530 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
3531 return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal));
3532 }
3533 case Type::BFloatTyID: {
3534 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
3535 return APFloat(APFloat::BFloat(), APInt(16, EltVal));
3536 }
3537 case Type::FloatTyID: {
3538 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
3539 return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal));
3540 }
3541 case Type::DoubleTyID: {
3542 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
3543 return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal));
3544 }
3545 }
3546}
3547
3548float ConstantDataSequential::getElementAsFloat(uint64_t Elt) const {
3549 assert(getElementType()->isFloatTy() &&
3550 "Accessor can only be used when element is a 'float'");
3551 return *reinterpret_cast<const float *>(getElementPointer(Elt));
3552}
3553
3554double ConstantDataSequential::getElementAsDouble(uint64_t Elt) const {
3555 assert(getElementType()->isDoubleTy() &&
3556 "Accessor can only be used when element is a 'float'");
3557 return *reinterpret_cast<const double *>(getElementPointer(Elt));
3558}
3559
3560Constant *ConstantDataSequential::getElementAsConstant(uint64_t Elt) const {
3561 if (getElementType()->isHalfTy() || getElementType()->isBFloatTy() ||
3562 getElementType()->isFloatTy() || getElementType()->isDoubleTy())
3563 return ConstantFP::get(Context&: getContext(), V: getElementAsAPFloat(Elt));
3564
3565 if (getElementType()->isByteTy())
3566 return ConstantByte::get(Ty: getElementType(), V: getElementAsInteger(Elt));
3567
3568 return ConstantInt::get(Ty: getElementType(), V: getElementAsInteger(Elt));
3569}
3570
3571bool ConstantDataSequential::isString(unsigned CharSize) const {
3572 return isa<ArrayType>(Val: getType()) &&
3573 (getElementType()->isIntegerTy(BitWidth: CharSize) ||
3574 getElementType()->isByteTy(BitWidth: CharSize));
3575}
3576
3577bool ConstantDataSequential::isCString() const {
3578 if (!isString())
3579 return false;
3580
3581 StringRef Str = getAsString();
3582
3583 // The last value must be nul.
3584 if (Str.back() != 0) return false;
3585
3586 // Other elements must be non-nul.
3587 return !Str.drop_back().contains(C: 0);
3588}
3589
3590bool ConstantDataVector::isSplatData() const {
3591 const char *Base = getRawDataValues().data();
3592
3593 // Compare elements 1+ to the 0'th element.
3594 unsigned EltSize = getElementByteSize();
3595 for (unsigned i = 1, e = getNumElements(); i != e; ++i)
3596 if (memcmp(s1: Base, s2: Base+i*EltSize, n: EltSize))
3597 return false;
3598
3599 return true;
3600}
3601
3602bool ConstantDataVector::isSplat() const {
3603 if (!IsSplatSet) {
3604 IsSplatSet = true;
3605 IsSplat = isSplatData();
3606 }
3607 return IsSplat;
3608}
3609
3610Constant *ConstantDataVector::getSplatValue() const {
3611 // If they're all the same, return the 0th one as a representative.
3612 return isSplat() ? getElementAsConstant(Elt: 0) : nullptr;
3613}
3614
3615//===----------------------------------------------------------------------===//
3616// handleOperandChange implementations
3617
3618/// Update this constant array to change uses of
3619/// 'From' to be uses of 'To'. This must update the uniquing data structures
3620/// etc.
3621///
3622/// Note that we intentionally replace all uses of From with To here. Consider
3623/// a large array that uses 'From' 1000 times. By handling this case all here,
3624/// ConstantArray::handleOperandChange is only invoked once, and that
3625/// single invocation handles all 1000 uses. Handling them one at a time would
3626/// work, but would be really slow because it would have to unique each updated
3627/// array instance.
3628///
3629void Constant::handleOperandChange(Value *From, Value *To) {
3630 Value *Replacement = nullptr;
3631 switch (getValueID()) {
3632 default:
3633 llvm_unreachable("Not a constant!");
3634#define HANDLE_CONSTANT(Name) \
3635 case Value::Name##Val: \
3636 Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
3637 break;
3638#include "llvm/IR/Value.def"
3639 }
3640
3641 // If handleOperandChangeImpl returned nullptr, then it handled
3642 // replacing itself and we don't want to delete or replace anything else here.
3643 if (!Replacement)
3644 return;
3645
3646 // I do need to replace this with an existing value.
3647 assert(Replacement != this && "I didn't contain From!");
3648
3649 // Everyone using this now uses the replacement.
3650 replaceAllUsesWith(V: Replacement);
3651
3652 // Delete the old constant!
3653 destroyConstant();
3654}
3655
3656Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
3657 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
3658 Constant *ToC = cast<Constant>(Val: To);
3659
3660 SmallVector<Constant*, 8> Values;
3661 Values.reserve(N: getNumOperands()); // Build replacement array.
3662
3663 // Fill values with the modified operands of the constant array. Also,
3664 // compute whether this turns into an all-zeros array.
3665 unsigned NumUpdated = 0;
3666
3667 // Keep track of whether all the values in the array are "ToC".
3668 bool AllSame = true;
3669 Use *OperandList = getOperandList();
3670 unsigned OperandNo = 0;
3671 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
3672 Constant *Val = cast<Constant>(Val: O->get());
3673 if (Val == From) {
3674 OperandNo = (O - OperandList);
3675 Val = ToC;
3676 ++NumUpdated;
3677 }
3678 Values.push_back(Elt: Val);
3679 AllSame &= Val == ToC;
3680 }
3681
3682 if (AllSame && ToC->isNullValue())
3683 return ConstantAggregateZero::get(Ty: getType());
3684
3685 if (AllSame && isa<UndefValue>(Val: ToC))
3686 return UndefValue::get(Ty: getType());
3687
3688 // Check for any other type of constant-folding.
3689 if (Constant *C = getImpl(Ty: getType(), V: Values))
3690 return C;
3691
3692 // Update to the new value.
3693 return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(
3694 Operands: Values, CP: this, From, To: ToC, NumUpdated, OperandNo);
3695}
3696
3697Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
3698 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
3699 Constant *ToC = cast<Constant>(Val: To);
3700
3701 Use *OperandList = getOperandList();
3702
3703 SmallVector<Constant*, 8> Values;
3704 Values.reserve(N: getNumOperands()); // Build replacement struct.
3705
3706 // Fill values with the modified operands of the constant struct. Also,
3707 // compute whether this turns into an all-zeros struct.
3708 unsigned NumUpdated = 0;
3709 bool AllSame = true;
3710 unsigned OperandNo = 0;
3711 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
3712 Constant *Val = cast<Constant>(Val: O->get());
3713 if (Val == From) {
3714 OperandNo = (O - OperandList);
3715 Val = ToC;
3716 ++NumUpdated;
3717 }
3718 Values.push_back(Elt: Val);
3719 AllSame &= Val == ToC;
3720 }
3721
3722 if (AllSame && ToC->isNullValue())
3723 return ConstantAggregateZero::get(Ty: getType());
3724
3725 if (AllSame && isa<UndefValue>(Val: ToC))
3726 return UndefValue::get(Ty: getType());
3727
3728 // Update to the new value.
3729 return getContext().pImpl->StructConstants.replaceOperandsInPlace(
3730 Operands: Values, CP: this, From, To: ToC, NumUpdated, OperandNo);
3731}
3732
3733Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
3734 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
3735 Constant *ToC = cast<Constant>(Val: To);
3736
3737 SmallVector<Constant*, 8> Values;
3738 Values.reserve(N: getNumOperands()); // Build replacement array...
3739 unsigned NumUpdated = 0;
3740 unsigned OperandNo = 0;
3741 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3742 Constant *Val = getOperand(i_nocapture: i);
3743 if (Val == From) {
3744 OperandNo = i;
3745 ++NumUpdated;
3746 Val = ToC;
3747 }
3748 Values.push_back(Elt: Val);
3749 }
3750
3751 if (Constant *C = getImpl(V: Values))
3752 return C;
3753
3754 // Update to the new value.
3755 return getContext().pImpl->VectorConstants.replaceOperandsInPlace(
3756 Operands: Values, CP: this, From, To: ToC, NumUpdated, OperandNo);
3757}
3758
3759Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
3760 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
3761 Constant *To = cast<Constant>(Val: ToV);
3762
3763 SmallVector<Constant*, 8> NewOps;
3764 unsigned NumUpdated = 0;
3765 unsigned OperandNo = 0;
3766 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3767 Constant *Op = getOperand(i_nocapture: i);
3768 if (Op == From) {
3769 OperandNo = i;
3770 ++NumUpdated;
3771 Op = To;
3772 }
3773 NewOps.push_back(Elt: Op);
3774 }
3775 assert(NumUpdated && "I didn't contain From!");
3776
3777 if (Constant *C = getWithOperands(Ops: NewOps, Ty: getType(), OnlyIfReduced: true))
3778 return C;
3779
3780 // Update to the new value.
3781 return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
3782 Operands: NewOps, CP: this, From, To, NumUpdated, OperandNo);
3783}
3784
3785Instruction *ConstantExpr::getAsInstruction() const {
3786 SmallVector<Value *, 4> ValueOperands(operands());
3787 ArrayRef<Value*> Ops(ValueOperands);
3788
3789 switch (getOpcode()) {
3790 case Instruction::Trunc:
3791 case Instruction::PtrToAddr:
3792 case Instruction::PtrToInt:
3793 case Instruction::IntToPtr:
3794 case Instruction::BitCast:
3795 case Instruction::AddrSpaceCast:
3796 return CastInst::Create((Instruction::CastOps)getOpcode(), S: Ops[0],
3797 Ty: getType(), Name: "");
3798 case Instruction::InsertElement:
3799 return InsertElementInst::Create(Vec: Ops[0], NewElt: Ops[1], Idx: Ops[2], NameStr: "");
3800 case Instruction::ExtractElement:
3801 return ExtractElementInst::Create(Vec: Ops[0], Idx: Ops[1], NameStr: "");
3802 case Instruction::ShuffleVector:
3803 return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "");
3804
3805 case Instruction::GetElementPtr: {
3806 const auto *GO = cast<GEPOperator>(Val: this);
3807 return GetElementPtrInst::Create(PointeeType: GO->getSourceElementType(), Ptr: Ops[0],
3808 IdxList: Ops.slice(N: 1), NW: GO->getNoWrapFlags(), NameStr: "");
3809 }
3810 default:
3811 assert(getNumOperands() == 2 && "Must be binary operator?");
3812 BinaryOperator *BO = BinaryOperator::Create(
3813 Op: (Instruction::BinaryOps)getOpcode(), S1: Ops[0], S2: Ops[1], Name: "");
3814 if (isa<OverflowingBinaryOperator>(Val: BO)) {
3815 BO->setHasNoUnsignedWrap(SubclassOptionalData &
3816 OverflowingBinaryOperator::NoUnsignedWrap);
3817 BO->setHasNoSignedWrap(SubclassOptionalData &
3818 OverflowingBinaryOperator::NoSignedWrap);
3819 }
3820 if (isa<PossiblyExactOperator>(Val: BO))
3821 BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);
3822 return BO;
3823 }
3824}
3825