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