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 ReplaceableMetadataImpl::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 // The replacement is with another global value.
2134 if (const auto *ToObj = dyn_cast<GlobalValue>(Val: To)) {
2135 if (DSOLocalEquivalent *NewEquiv =
2136 getContext().pImpl->DSOLocalEquivalents.lookup(Val: ToObj))
2137 return llvm::ConstantExpr::getBitCast(C: NewEquiv, Ty: getType());
2138 }
2139
2140 // If the argument is replaced with a null value, just replace this constant
2141 // with a null value.
2142 if (isa<ConstantPointerNull>(Val: To))
2143 return To;
2144
2145 // The replacement could be a bitcast or an alias to another function. We can
2146 // replace it with a bitcast to the dso_local_equivalent of that function.
2147 auto *Func = cast<Function>(Val: To->stripPointerCastsAndAliases());
2148 if (DSOLocalEquivalent *NewEquiv =
2149 getContext().pImpl->DSOLocalEquivalents.lookup(Val: Func))
2150 return llvm::ConstantExpr::getBitCast(C: NewEquiv, Ty: getType());
2151
2152 // erase invalidates iterators/references, hence the duplicate Func lookup.
2153 getContext().pImpl->DSOLocalEquivalents.erase(Val: getGlobalValue());
2154 getContext().pImpl->DSOLocalEquivalents[Func] = this;
2155 setOperand(i_nocapture: 0, Val_nocapture: Func);
2156
2157 if (Func->getType() != getType()) {
2158 // It is ok to mutate the type here because this constant should always
2159 // reflect the type of the function it's holding.
2160 mutateType(Ty: Func->getType());
2161 }
2162 return nullptr;
2163}
2164
2165NoCFIValue *NoCFIValue::get(GlobalValue *GV) {
2166 NoCFIValue *&NC = GV->getContext().pImpl->NoCFIValues[GV];
2167 if (!NC)
2168 NC = new NoCFIValue(GV);
2169
2170 assert(NC->getGlobalValue() == GV &&
2171 "NoCFIValue does not match the expected global value");
2172 return NC;
2173}
2174
2175NoCFIValue::NoCFIValue(GlobalValue *GV)
2176 : Constant(GV->getType(), Value::NoCFIValueVal, AllocMarker) {
2177 setOperand(i_nocapture: 0, Val_nocapture: GV);
2178}
2179
2180/// Remove the constant from the constant table.
2181void NoCFIValue::destroyConstantImpl() {
2182 const GlobalValue *GV = getGlobalValue();
2183 GV->getContext().pImpl->NoCFIValues.erase(Val: GV);
2184}
2185
2186Value *NoCFIValue::handleOperandChangeImpl(Value *From, Value *To) {
2187 assert(From == getGlobalValue() && "Changing value does not match operand.");
2188
2189 GlobalValue *GV = dyn_cast<GlobalValue>(Val: To->stripPointerCasts());
2190 assert(GV && "Can only replace the operands with a global value");
2191
2192 if (NoCFIValue *NewNC = getContext().pImpl->NoCFIValues.lookup(Val: GV))
2193 return llvm::ConstantExpr::getBitCast(C: NewNC, Ty: getType());
2194
2195 // erase invalidates iterators/references, hence the duplicate GV lookup.
2196 getContext().pImpl->NoCFIValues.erase(Val: getGlobalValue());
2197 getContext().pImpl->NoCFIValues[GV] = this;
2198 setOperand(i_nocapture: 0, Val_nocapture: GV);
2199
2200 if (GV->getType() != getType())
2201 mutateType(Ty: GV->getType());
2202
2203 return nullptr;
2204}
2205
2206//---- ConstantPtrAuth::get() implementations.
2207//
2208
2209ConstantPtrAuth *ConstantPtrAuth::get(Constant *Ptr, ConstantInt *Key,
2210 ConstantInt *Disc, Constant *AddrDisc,
2211 Constant *DeactivationSymbol) {
2212 Constant *ArgVec[] = {Ptr, Key, Disc, AddrDisc, DeactivationSymbol};
2213 ConstantPtrAuthKeyType MapKey(ArgVec);
2214 LLVMContextImpl *pImpl = Ptr->getContext().pImpl;
2215 return pImpl->ConstantPtrAuths.getOrCreate(Ty: Ptr->getType(), V: MapKey);
2216}
2217
2218ConstantPtrAuth *ConstantPtrAuth::getWithSameSchema(Constant *Pointer) const {
2219 return get(Ptr: Pointer, Key: getKey(), Disc: getDiscriminator(), AddrDisc: getAddrDiscriminator(),
2220 DeactivationSymbol: getDeactivationSymbol());
2221}
2222
2223ConstantPtrAuth::ConstantPtrAuth(Constant *Ptr, ConstantInt *Key,
2224 ConstantInt *Disc, Constant *AddrDisc,
2225 Constant *DeactivationSymbol)
2226 : Constant(Ptr->getType(), Value::ConstantPtrAuthVal, AllocMarker) {
2227 assert(Ptr->getType()->isPointerTy());
2228 assert(Key->getBitWidth() == 32);
2229 assert(Disc->getBitWidth() == 64);
2230 assert(AddrDisc->getType()->isPointerTy());
2231 assert(DeactivationSymbol->getType()->isPointerTy());
2232 setOperand(i_nocapture: 0, Val_nocapture: Ptr);
2233 setOperand(i_nocapture: 1, Val_nocapture: Key);
2234 setOperand(i_nocapture: 2, Val_nocapture: Disc);
2235 setOperand(i_nocapture: 3, Val_nocapture: AddrDisc);
2236 setOperand(i_nocapture: 4, Val_nocapture: DeactivationSymbol);
2237}
2238
2239/// Remove the constant from the constant table.
2240void ConstantPtrAuth::destroyConstantImpl() {
2241 getType()->getContext().pImpl->ConstantPtrAuths.remove(CP: this);
2242}
2243
2244Value *ConstantPtrAuth::handleOperandChangeImpl(Value *From, Value *ToV) {
2245 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2246 Constant *To = cast<Constant>(Val: ToV);
2247
2248 SmallVector<Constant *, 4> Values;
2249 Values.reserve(N: getNumOperands());
2250
2251 unsigned NumUpdated = 0;
2252
2253 Use *OperandList = getOperandList();
2254 unsigned OperandNo = 0;
2255 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
2256 Constant *Val = cast<Constant>(Val: O->get());
2257 if (Val == From) {
2258 OperandNo = (O - OperandList);
2259 Val = To;
2260 ++NumUpdated;
2261 }
2262 Values.push_back(Elt: Val);
2263 }
2264
2265 return getContext().pImpl->ConstantPtrAuths.replaceOperandsInPlace(
2266 Operands: Values, CP: this, From, To, NumUpdated, OperandNo);
2267}
2268
2269bool ConstantPtrAuth::hasSpecialAddressDiscriminator(uint64_t Value) const {
2270 const auto *CastV = dyn_cast<ConstantExpr>(Val: getAddrDiscriminator());
2271 if (!CastV || CastV->getOpcode() != Instruction::IntToPtr)
2272 return false;
2273
2274 const auto *IntVal = dyn_cast<ConstantInt>(Val: CastV->getOperand(i_nocapture: 0));
2275 if (!IntVal)
2276 return false;
2277
2278 return IntVal->getValue() == Value;
2279}
2280
2281bool ConstantPtrAuth::isKnownCompatibleWith(const Value *Key,
2282 const Value *Discriminator,
2283 const DataLayout &DL) const {
2284 // This function may only be validly called to analyze a ptrauth operation
2285 // with no deactivation symbol, so if we have one it isn't compatible.
2286 if (!isa<ConstantPointerNull>(Val: getDeactivationSymbol()))
2287 return false;
2288
2289 // If the keys are different, there's no chance for this to be compatible.
2290 if (getKey() != Key)
2291 return false;
2292
2293 // We can have 3 kinds of discriminators:
2294 // - simple, integer-only: `i64 x, ptr null` vs. `i64 x`
2295 // - address-only: `i64 0, ptr p` vs. `ptr p`
2296 // - blended address/integer: `i64 x, ptr p` vs. `@llvm.ptrauth.blend(p, x)`
2297
2298 // If this constant has a simple discriminator (integer, no address), easy:
2299 // it's compatible iff the provided full discriminator is also a simple
2300 // discriminator, identical to our integer discriminator.
2301 if (!hasAddressDiscriminator())
2302 return getDiscriminator() == Discriminator;
2303
2304 // Otherwise, we can isolate address and integer discriminator components.
2305 const Value *AddrDiscriminator = nullptr;
2306
2307 // This constant may or may not have an integer discriminator (instead of 0).
2308 if (!getDiscriminator()->isNullValue()) {
2309 // If it does, there's an implicit blend. We need to have a matching blend
2310 // intrinsic in the provided full discriminator.
2311 if (!match(V: Discriminator,
2312 P: m_Intrinsic<Intrinsic::ptrauth_blend>(
2313 Op0: m_Value(V&: AddrDiscriminator), Op1: m_Specific(V: getDiscriminator()))))
2314 return false;
2315 } else {
2316 // Otherwise, interpret the provided full discriminator as address-only.
2317 AddrDiscriminator = Discriminator;
2318 }
2319
2320 // Either way, we can now focus on comparing the address discriminators.
2321
2322 // Discriminators are i64, so the provided addr disc may be a ptrtoint.
2323 if (auto *Cast = dyn_cast<PtrToIntOperator>(Val: AddrDiscriminator))
2324 AddrDiscriminator = Cast->getPointerOperand();
2325
2326 // Beyond that, we're only interested in compatible pointers.
2327 if (getAddrDiscriminator()->getType() != AddrDiscriminator->getType())
2328 return false;
2329
2330 // These are often the same constant GEP, making them trivially equivalent.
2331 if (getAddrDiscriminator() == AddrDiscriminator)
2332 return true;
2333
2334 // Finally, they may be equivalent base+offset expressions.
2335 APInt Off1(DL.getIndexTypeSizeInBits(Ty: getAddrDiscriminator()->getType()), 0);
2336 auto *Base1 = getAddrDiscriminator()->stripAndAccumulateConstantOffsets(
2337 DL, Offset&: Off1, /*AllowNonInbounds=*/true);
2338
2339 APInt Off2(DL.getIndexTypeSizeInBits(Ty: AddrDiscriminator->getType()), 0);
2340 auto *Base2 = AddrDiscriminator->stripAndAccumulateConstantOffsets(
2341 DL, Offset&: Off2, /*AllowNonInbounds=*/true);
2342
2343 return Base1 == Base2 && Off1 == Off2;
2344}
2345
2346//---- ConstantExpr::get() implementations.
2347//
2348
2349/// This is a utility function to handle folding of casts and lookup of the
2350/// cast in the ExprConstants map. It is used by the various get* methods below.
2351static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
2352 bool OnlyIfReduced = false) {
2353 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
2354 // Fold a few common cases
2355 if (Constant *FC = ConstantFoldCastInstruction(opcode: opc, V: C, DestTy: Ty))
2356 return FC;
2357
2358 if (OnlyIfReduced)
2359 return nullptr;
2360
2361 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
2362
2363 // Look up the constant in the table first to ensure uniqueness.
2364 ConstantExprKeyType Key(opc, C);
2365
2366 return pImpl->ExprConstants.getOrCreate(Ty, V: Key);
2367}
2368
2369Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
2370 bool OnlyIfReduced) {
2371 Instruction::CastOps opc = Instruction::CastOps(oc);
2372 assert(Instruction::isCast(opc) && "opcode out of range");
2373 assert(isSupportedCastOp(opc) &&
2374 "Cast opcode not supported as constant expression");
2375 assert(C && Ty && "Null arguments to getCast");
2376 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
2377
2378 switch (opc) {
2379 default:
2380 llvm_unreachable("Invalid cast opcode");
2381 case Instruction::Trunc:
2382 return getTrunc(C, Ty, OnlyIfReduced);
2383 case Instruction::PtrToAddr:
2384 return getPtrToAddr(C, Ty, OnlyIfReduced);
2385 case Instruction::PtrToInt:
2386 return getPtrToInt(C, Ty, OnlyIfReduced);
2387 case Instruction::IntToPtr:
2388 return getIntToPtr(C, Ty, OnlyIfReduced);
2389 case Instruction::BitCast:
2390 return getBitCast(C, Ty, OnlyIfReduced);
2391 case Instruction::AddrSpaceCast:
2392 return getAddrSpaceCast(C, Ty, OnlyIfReduced);
2393 }
2394}
2395
2396Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
2397 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2398 return getBitCast(C, Ty);
2399 return getTrunc(C, Ty);
2400}
2401
2402Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
2403 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2404 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2405 "Invalid cast");
2406
2407 if (Ty->isIntOrIntVectorTy())
2408 return getPtrToInt(C: S, Ty);
2409
2410 unsigned SrcAS = S->getType()->getPointerAddressSpace();
2411 if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
2412 return getAddrSpaceCast(C: S, Ty);
2413
2414 return getBitCast(C: S, Ty);
2415}
2416
2417Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
2418 Type *Ty) {
2419 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2420 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2421
2422 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2423 return getAddrSpaceCast(C: S, Ty);
2424
2425 return getBitCast(C: S, Ty);
2426}
2427
2428Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
2429#ifndef NDEBUG
2430 bool fromVec = isa<VectorType>(C->getType());
2431 bool toVec = isa<VectorType>(Ty);
2432#endif
2433 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2434 assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
2435 assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
2436 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
2437 "SrcTy must be larger than DestTy for Trunc!");
2438
2439 return getFoldedCast(opc: Instruction::Trunc, C, Ty, OnlyIfReduced);
2440}
2441
2442Constant *ConstantExpr::getPtrToAddr(Constant *C, Type *DstTy,
2443 bool OnlyIfReduced) {
2444 assert(C->getType()->isPtrOrPtrVectorTy() &&
2445 "PtrToAddr source must be pointer or pointer vector");
2446 assert(DstTy->isIntOrIntVectorTy() &&
2447 "PtrToAddr destination must be integer or integer vector");
2448 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
2449 if (isa<VectorType>(Val: C->getType()))
2450 assert(cast<VectorType>(C->getType())->getElementCount() ==
2451 cast<VectorType>(DstTy)->getElementCount() &&
2452 "Invalid cast between a different number of vector elements");
2453 return getFoldedCast(opc: Instruction::PtrToAddr, C, Ty: DstTy, OnlyIfReduced);
2454}
2455
2456Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
2457 bool OnlyIfReduced) {
2458 assert(C->getType()->isPtrOrPtrVectorTy() &&
2459 "PtrToInt source must be pointer or pointer vector");
2460 assert(DstTy->isIntOrIntVectorTy() &&
2461 "PtrToInt destination must be integer or integer vector");
2462 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
2463 if (isa<VectorType>(Val: C->getType()))
2464 assert(cast<VectorType>(C->getType())->getElementCount() ==
2465 cast<VectorType>(DstTy)->getElementCount() &&
2466 "Invalid cast between a different number of vector elements");
2467 return getFoldedCast(opc: Instruction::PtrToInt, C, Ty: DstTy, OnlyIfReduced);
2468}
2469
2470Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
2471 bool OnlyIfReduced) {
2472 assert(C->getType()->isIntOrIntVectorTy() &&
2473 "IntToPtr source must be integer or integer vector");
2474 assert(DstTy->isPtrOrPtrVectorTy() &&
2475 "IntToPtr destination must be a pointer or pointer vector");
2476 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
2477 if (isa<VectorType>(Val: C->getType()))
2478 assert(cast<VectorType>(C->getType())->getElementCount() ==
2479 cast<VectorType>(DstTy)->getElementCount() &&
2480 "Invalid cast between a different number of vector elements");
2481 return getFoldedCast(opc: Instruction::IntToPtr, C, Ty: DstTy, OnlyIfReduced);
2482}
2483
2484Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
2485 bool OnlyIfReduced) {
2486 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
2487 "Invalid constantexpr bitcast!");
2488
2489 // It is common to ask for a bitcast of a value to its own type, handle this
2490 // speedily.
2491 if (C->getType() == DstTy) return C;
2492
2493 return getFoldedCast(opc: Instruction::BitCast, C, Ty: DstTy, OnlyIfReduced);
2494}
2495
2496Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
2497 bool OnlyIfReduced) {
2498 assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
2499 "Invalid constantexpr addrspacecast!");
2500 return getFoldedCast(opc: Instruction::AddrSpaceCast, C, Ty: DstTy, OnlyIfReduced);
2501}
2502
2503Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
2504 unsigned Flags, Type *OnlyIfReducedTy) {
2505 // Check the operands for consistency first.
2506 assert(Instruction::isBinaryOp(Opcode) &&
2507 "Invalid opcode in binary constant expression");
2508 assert(isSupportedBinOp(Opcode) &&
2509 "Binop not supported as constant expression");
2510 assert(C1->getType() == C2->getType() &&
2511 "Operand types in binary constant expression should match");
2512
2513#ifndef NDEBUG
2514 switch (Opcode) {
2515 case Instruction::Add:
2516 case Instruction::Sub:
2517 case Instruction::Mul:
2518 assert(C1->getType()->isIntOrIntVectorTy() &&
2519 "Tried to create an integer operation on a non-integer type!");
2520 break;
2521 case Instruction::And:
2522 case Instruction::Or:
2523 case Instruction::Xor:
2524 assert(C1->getType()->isIntOrIntVectorTy() &&
2525 "Tried to create a logical operation on a non-integral type!");
2526 break;
2527 default:
2528 break;
2529 }
2530#endif
2531
2532 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, V1: C1, V2: C2))
2533 return FC;
2534
2535 if (OnlyIfReducedTy == C1->getType())
2536 return nullptr;
2537
2538 Constant *ArgVec[] = {C1, C2};
2539 ConstantExprKeyType Key(Opcode, ArgVec, Flags);
2540
2541 LLVMContextImpl *pImpl = C1->getContext().pImpl;
2542 return pImpl->ExprConstants.getOrCreate(Ty: C1->getType(), V: Key);
2543}
2544
2545bool ConstantExpr::isDesirableBinOp(unsigned Opcode) {
2546 switch (Opcode) {
2547 case Instruction::UDiv:
2548 case Instruction::SDiv:
2549 case Instruction::URem:
2550 case Instruction::SRem:
2551 case Instruction::FAdd:
2552 case Instruction::FSub:
2553 case Instruction::FMul:
2554 case Instruction::FDiv:
2555 case Instruction::FRem:
2556 case Instruction::And:
2557 case Instruction::Or:
2558 case Instruction::LShr:
2559 case Instruction::AShr:
2560 case Instruction::Shl:
2561 case Instruction::Mul:
2562 return false;
2563 case Instruction::Add:
2564 case Instruction::Sub:
2565 case Instruction::Xor:
2566 return true;
2567 default:
2568 llvm_unreachable("Argument must be binop opcode");
2569 }
2570}
2571
2572bool ConstantExpr::isSupportedBinOp(unsigned Opcode) {
2573 switch (Opcode) {
2574 case Instruction::UDiv:
2575 case Instruction::SDiv:
2576 case Instruction::URem:
2577 case Instruction::SRem:
2578 case Instruction::FAdd:
2579 case Instruction::FSub:
2580 case Instruction::FMul:
2581 case Instruction::FDiv:
2582 case Instruction::FRem:
2583 case Instruction::And:
2584 case Instruction::Or:
2585 case Instruction::LShr:
2586 case Instruction::AShr:
2587 case Instruction::Shl:
2588 case Instruction::Mul:
2589 return false;
2590 case Instruction::Add:
2591 case Instruction::Sub:
2592 case Instruction::Xor:
2593 return true;
2594 default:
2595 llvm_unreachable("Argument must be binop opcode");
2596 }
2597}
2598
2599bool ConstantExpr::isDesirableCastOp(unsigned Opcode) {
2600 switch (Opcode) {
2601 case Instruction::ZExt:
2602 case Instruction::SExt:
2603 case Instruction::FPTrunc:
2604 case Instruction::FPExt:
2605 case Instruction::UIToFP:
2606 case Instruction::SIToFP:
2607 case Instruction::FPToUI:
2608 case Instruction::FPToSI:
2609 return false;
2610 case Instruction::Trunc:
2611 case Instruction::PtrToAddr:
2612 case Instruction::PtrToInt:
2613 case Instruction::IntToPtr:
2614 case Instruction::BitCast:
2615 case Instruction::AddrSpaceCast:
2616 return true;
2617 default:
2618 llvm_unreachable("Argument must be cast opcode");
2619 }
2620}
2621
2622bool ConstantExpr::isSupportedCastOp(unsigned Opcode) {
2623 switch (Opcode) {
2624 case Instruction::ZExt:
2625 case Instruction::SExt:
2626 case Instruction::FPTrunc:
2627 case Instruction::FPExt:
2628 case Instruction::UIToFP:
2629 case Instruction::SIToFP:
2630 case Instruction::FPToUI:
2631 case Instruction::FPToSI:
2632 return false;
2633 case Instruction::Trunc:
2634 case Instruction::PtrToAddr:
2635 case Instruction::PtrToInt:
2636 case Instruction::IntToPtr:
2637 case Instruction::BitCast:
2638 case Instruction::AddrSpaceCast:
2639 return true;
2640 default:
2641 llvm_unreachable("Argument must be cast opcode");
2642 }
2643}
2644
2645Constant *ConstantExpr::getSizeOf(Type* Ty) {
2646 // sizeof is implemented as: (i64) gep (Ty*)null, 1
2647 // Note that a non-inbounds gep is used, as null isn't within any object.
2648 Constant *GEPIdx = ConstantInt::get(Ty: Type::getInt32Ty(C&: Ty->getContext()), V: 1);
2649 Constant *GEP = getGetElementPtr(
2650 Ty, C: Constant::getNullValue(Ty: PointerType::getUnqual(C&: Ty->getContext())),
2651 Idx: GEPIdx);
2652 return getPtrToInt(C: GEP,
2653 DstTy: Type::getInt64Ty(C&: Ty->getContext()));
2654}
2655
2656Constant *ConstantExpr::getAlignOf(Type* Ty) {
2657 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
2658 // Note that a non-inbounds gep is used, as null isn't within any object.
2659 Type *AligningTy = StructType::get(elt1: Type::getInt1Ty(C&: Ty->getContext()), elts: Ty);
2660 Constant *NullPtr =
2661 Constant::getNullValue(Ty: PointerType::getUnqual(C&: AligningTy->getContext()));
2662 Constant *Zero = ConstantInt::get(Ty: Type::getInt64Ty(C&: Ty->getContext()), V: 0);
2663 Constant *One = ConstantInt::get(Ty: Type::getInt32Ty(C&: Ty->getContext()), V: 1);
2664 Constant *Indices[2] = {Zero, One};
2665 Constant *GEP = getGetElementPtr(Ty: AligningTy, C: NullPtr, IdxList: Indices);
2666 return getPtrToInt(C: GEP, DstTy: Type::getInt64Ty(C&: Ty->getContext()));
2667}
2668
2669Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
2670 ArrayRef<Value *> Idxs,
2671 GEPNoWrapFlags NW,
2672 std::optional<ConstantRange> InRange,
2673 Type *OnlyIfReducedTy) {
2674 assert(Ty && "Must specify element type");
2675 assert(isSupportedGetElementPtr(Ty) && "Element type is unsupported!");
2676
2677 if (Constant *FC = ConstantFoldGetElementPtr(Ty, C, InRange, Idxs))
2678 return FC; // Fold a few common cases.
2679
2680 assert(GetElementPtrInst::getIndexedType(Ty, Idxs) && "GEP indices invalid!");
2681 ;
2682
2683 // Get the result type of the getelementptr!
2684 Type *ReqTy = GetElementPtrInst::getGEPReturnType(Ptr: C, IdxList: Idxs);
2685 if (OnlyIfReducedTy == ReqTy)
2686 return nullptr;
2687
2688 auto EltCount = ElementCount::getFixed(MinVal: 0);
2689 if (VectorType *VecTy = dyn_cast<VectorType>(Val: ReqTy))
2690 EltCount = VecTy->getElementCount();
2691
2692 // Look up the constant in the table first to ensure uniqueness
2693 std::vector<Constant*> ArgVec;
2694 ArgVec.reserve(n: 1 + Idxs.size());
2695 ArgVec.push_back(x: C);
2696 auto GTI = gep_type_begin(Op0: Ty, A: Idxs), GTE = gep_type_end(Ty, A: Idxs);
2697 for (; GTI != GTE; ++GTI) {
2698 auto *Idx = cast<Constant>(Val: GTI.getOperand());
2699 assert(
2700 (!isa<VectorType>(Idx->getType()) ||
2701 cast<VectorType>(Idx->getType())->getElementCount() == EltCount) &&
2702 "getelementptr index type missmatch");
2703
2704 if (GTI.isStruct() && Idx->getType()->isVectorTy()) {
2705 Idx = Idx->getSplatValue();
2706 } else if (GTI.isSequential() && EltCount.isNonZero() &&
2707 !Idx->getType()->isVectorTy()) {
2708 Idx = ConstantVector::getSplat(EC: EltCount, V: Idx);
2709 }
2710 ArgVec.push_back(x: Idx);
2711 }
2712
2713 const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, NW.getRaw(),
2714 {}, Ty, InRange);
2715
2716 LLVMContextImpl *pImpl = C->getContext().pImpl;
2717 return pImpl->ExprConstants.getOrCreate(Ty: ReqTy, V: Key);
2718}
2719
2720Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
2721 Type *OnlyIfReducedTy) {
2722 assert(Val->getType()->isVectorTy() &&
2723 "Tried to create extractelement operation on non-vector type!");
2724 assert(Idx->getType()->isIntegerTy() &&
2725 "Extractelement index must be an integer type!");
2726
2727 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2728 return FC; // Fold a few common cases.
2729
2730 Type *ReqTy = cast<VectorType>(Val: Val->getType())->getElementType();
2731 if (OnlyIfReducedTy == ReqTy)
2732 return nullptr;
2733
2734 // Look up the constant in the table first to ensure uniqueness
2735 Constant *ArgVec[] = { Val, Idx };
2736 const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
2737
2738 LLVMContextImpl *pImpl = Val->getContext().pImpl;
2739 return pImpl->ExprConstants.getOrCreate(Ty: ReqTy, V: Key);
2740}
2741
2742Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2743 Constant *Idx, Type *OnlyIfReducedTy) {
2744 assert(Val->getType()->isVectorTy() &&
2745 "Tried to create insertelement operation on non-vector type!");
2746 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType() &&
2747 "Insertelement types must match!");
2748 assert(Idx->getType()->isIntegerTy() &&
2749 "Insertelement index must be i32 type!");
2750
2751 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2752 return FC; // Fold a few common cases.
2753
2754 if (OnlyIfReducedTy == Val->getType())
2755 return nullptr;
2756
2757 // Look up the constant in the table first to ensure uniqueness
2758 Constant *ArgVec[] = { Val, Elt, Idx };
2759 const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
2760
2761 LLVMContextImpl *pImpl = Val->getContext().pImpl;
2762 return pImpl->ExprConstants.getOrCreate(Ty: Val->getType(), V: Key);
2763}
2764
2765Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2766 ArrayRef<int> Mask,
2767 Type *OnlyIfReducedTy) {
2768 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2769 "Invalid shuffle vector constant expr operands!");
2770
2771 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2772 return FC; // Fold a few common cases.
2773
2774 unsigned NElts = Mask.size();
2775 auto V1VTy = cast<VectorType>(Val: V1->getType());
2776 Type *EltTy = V1VTy->getElementType();
2777 bool TypeIsScalable = isa<ScalableVectorType>(Val: V1VTy);
2778 Type *ShufTy = VectorType::get(ElementType: EltTy, NumElements: NElts, Scalable: TypeIsScalable);
2779
2780 if (OnlyIfReducedTy == ShufTy)
2781 return nullptr;
2782
2783 // Look up the constant in the table first to ensure uniqueness
2784 Constant *ArgVec[] = {V1, V2};
2785 ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec, 0, Mask);
2786
2787 LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
2788 return pImpl->ExprConstants.getOrCreate(Ty: ShufTy, V: Key);
2789}
2790
2791Constant *ConstantExpr::getNeg(Constant *C, bool HasNSW) {
2792 assert(C->getType()->isIntOrIntVectorTy() &&
2793 "Cannot NEG a nonintegral value!");
2794 return getSub(C1: ConstantInt::get(Ty: C->getType(), V: 0), C2: C, /*HasNUW=*/false, HasNSW);
2795}
2796
2797Constant *ConstantExpr::getNot(Constant *C) {
2798 assert(C->getType()->isIntOrIntVectorTy() &&
2799 "Cannot NOT a nonintegral value!");
2800 return get(Opcode: Instruction::Xor, C1: C, C2: Constant::getAllOnesValue(Ty: C->getType()));
2801}
2802
2803Constant *ConstantExpr::getAdd(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::Add, C1, C2, Flags);
2808}
2809
2810Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
2811 bool HasNUW, bool HasNSW) {
2812 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2813 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2814 return get(Opcode: Instruction::Sub, C1, C2, Flags);
2815}
2816
2817Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
2818 return get(Opcode: Instruction::Xor, C1, C2);
2819}
2820
2821Constant *ConstantExpr::getExactLogBase2(Constant *C) {
2822 Type *Ty = C->getType();
2823 const APInt *IVal;
2824 if (match(V: C, P: m_APInt(Res&: IVal)) && IVal->isPowerOf2())
2825 return ConstantInt::get(Ty, V: IVal->logBase2());
2826
2827 // FIXME: We can extract pow of 2 of splat constant for scalable vectors.
2828 auto *VecTy = dyn_cast<FixedVectorType>(Val: Ty);
2829 if (!VecTy)
2830 return nullptr;
2831
2832 SmallVector<Constant *, 4> Elts;
2833 for (unsigned I = 0, E = VecTy->getNumElements(); I != E; ++I) {
2834 Constant *Elt = C->getAggregateElement(Elt: I);
2835 if (!Elt)
2836 return nullptr;
2837 // Note that log2(iN undef) is *NOT* iN undef, because log2(iN undef) u< N.
2838 if (isa<UndefValue>(Val: Elt)) {
2839 Elts.push_back(Elt: Constant::getNullValue(Ty: Ty->getScalarType()));
2840 continue;
2841 }
2842 if (!match(V: Elt, P: m_APInt(Res&: IVal)) || !IVal->isPowerOf2())
2843 return nullptr;
2844 Elts.push_back(Elt: ConstantInt::get(Ty: Ty->getScalarType(), V: IVal->logBase2()));
2845 }
2846
2847 return ConstantVector::get(V: Elts);
2848}
2849
2850Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty,
2851 bool AllowRHSConstant, bool NSZ) {
2852 assert(Instruction::isBinaryOp(Opcode) && "Only binops allowed");
2853
2854 // Commutative opcodes: it does not matter if AllowRHSConstant is set.
2855 if (Instruction::isCommutative(Opcode)) {
2856 switch (Opcode) {
2857 case Instruction::Add: // X + 0 = X
2858 case Instruction::Or: // X | 0 = X
2859 case Instruction::Xor: // X ^ 0 = X
2860 return Constant::getNullValue(Ty);
2861 case Instruction::Mul: // X * 1 = X
2862 return ConstantInt::get(Ty, V: 1);
2863 case Instruction::And: // X & -1 = X
2864 return Constant::getAllOnesValue(Ty);
2865 case Instruction::FAdd: // X + -0.0 = X
2866 return ConstantFP::getZero(Ty, Negative: !NSZ);
2867 case Instruction::FMul: // X * 1.0 = X
2868 return ConstantFP::get(Ty, V: 1.0);
2869 default:
2870 llvm_unreachable("Every commutative binop has an identity constant");
2871 }
2872 }
2873
2874 // Non-commutative opcodes: AllowRHSConstant must be set.
2875 if (!AllowRHSConstant)
2876 return nullptr;
2877
2878 switch (Opcode) {
2879 case Instruction::Sub: // X - 0 = X
2880 case Instruction::Shl: // X << 0 = X
2881 case Instruction::LShr: // X >>u 0 = X
2882 case Instruction::AShr: // X >> 0 = X
2883 case Instruction::FSub: // X - 0.0 = X
2884 return Constant::getNullValue(Ty);
2885 case Instruction::SDiv: // X / 1 = X
2886 case Instruction::UDiv: // X /u 1 = X
2887 return ConstantInt::get(Ty, V: 1);
2888 case Instruction::FDiv: // X / 1.0 = X
2889 return ConstantFP::get(Ty, V: 1.0);
2890 default:
2891 return nullptr;
2892 }
2893}
2894
2895Constant *ConstantExpr::getIntrinsicIdentity(Intrinsic::ID ID, Type *Ty) {
2896 switch (ID) {
2897 case Intrinsic::umax:
2898 return Constant::getNullValue(Ty);
2899 case Intrinsic::umin:
2900 return Constant::getAllOnesValue(Ty);
2901 case Intrinsic::smax:
2902 return Constant::getIntegerValue(
2903 Ty, V: APInt::getSignedMinValue(numBits: Ty->getIntegerBitWidth()));
2904 case Intrinsic::smin:
2905 return Constant::getIntegerValue(
2906 Ty, V: APInt::getSignedMaxValue(numBits: Ty->getIntegerBitWidth()));
2907 default:
2908 return nullptr;
2909 }
2910}
2911
2912Constant *ConstantExpr::getIdentity(Instruction *I, Type *Ty,
2913 bool AllowRHSConstant, bool NSZ) {
2914 if (I->isBinaryOp())
2915 return getBinOpIdentity(Opcode: I->getOpcode(), Ty, AllowRHSConstant, NSZ);
2916 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Val: I))
2917 return getIntrinsicIdentity(ID: II->getIntrinsicID(), Ty);
2918 return nullptr;
2919}
2920
2921Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty,
2922 bool AllowLHSConstant) {
2923 switch (Opcode) {
2924 default:
2925 break;
2926
2927 case Instruction::Or: // -1 | X = -1
2928 return Constant::getAllOnesValue(Ty);
2929
2930 case Instruction::And: // 0 & X = 0
2931 case Instruction::Mul: // 0 * X = 0
2932 return Constant::getNullValue(Ty);
2933 }
2934
2935 // AllowLHSConstant must be set.
2936 if (!AllowLHSConstant)
2937 return nullptr;
2938
2939 switch (Opcode) {
2940 default:
2941 return nullptr;
2942 case Instruction::Shl: // 0 << X = 0
2943 case Instruction::LShr: // 0 >>l X = 0
2944 case Instruction::AShr: // 0 >>a X = 0
2945 case Instruction::SDiv: // 0 /s X = 0
2946 case Instruction::UDiv: // 0 /u X = 0
2947 case Instruction::URem: // 0 %u X = 0
2948 case Instruction::SRem: // 0 %s X = 0
2949 return Constant::getNullValue(Ty);
2950 }
2951}
2952
2953/// Remove the constant from the constant table.
2954void ConstantExpr::destroyConstantImpl() {
2955 getType()->getContext().pImpl->ExprConstants.remove(CP: this);
2956}
2957
2958const char *ConstantExpr::getOpcodeName() const {
2959 return Instruction::getOpcodeName(Opcode: getOpcode());
2960}
2961
2962GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2963 Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy,
2964 std::optional<ConstantRange> InRange, AllocInfo AllocInfo)
2965 : ConstantExpr(DestTy, Instruction::GetElementPtr, AllocInfo),
2966 SrcElementTy(SrcElementTy),
2967 ResElementTy(GetElementPtrInst::getIndexedType(Ty: SrcElementTy, IdxList)),
2968 InRange(std::move(InRange)) {
2969 Op<0>() = C;
2970 Use *OperandList = getOperandList();
2971 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
2972 OperandList[i+1] = IdxList[i];
2973}
2974
2975Type *GetElementPtrConstantExpr::getSourceElementType() const {
2976 return SrcElementTy;
2977}
2978
2979Type *GetElementPtrConstantExpr::getResultElementType() const {
2980 return ResElementTy;
2981}
2982
2983std::optional<ConstantRange> GetElementPtrConstantExpr::getInRange() const {
2984 return InRange;
2985}
2986
2987//===----------------------------------------------------------------------===//
2988// ConstantData* implementations
2989
2990Type *ConstantDataSequential::getElementType() const {
2991 if (ArrayType *ATy = dyn_cast<ArrayType>(Val: getType()))
2992 return ATy->getElementType();
2993 return cast<VectorType>(Val: getType())->getElementType();
2994}
2995
2996StringRef ConstantDataSequential::getRawDataValues() const {
2997 return StringRef(DataElements, getNumElements()*getElementByteSize());
2998}
2999
3000bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
3001 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() || Ty->isDoubleTy())
3002 return true;
3003 if (auto *IT = dyn_cast<IntegerType>(Val: Ty)) {
3004 switch (IT->getBitWidth()) {
3005 case 8:
3006 case 16:
3007 case 32:
3008 case 64:
3009 return true;
3010 default: break;
3011 }
3012 }
3013 if (auto *IT = dyn_cast<ByteType>(Val: Ty)) {
3014 switch (IT->getBitWidth()) {
3015 case 8:
3016 case 16:
3017 case 32:
3018 case 64:
3019 return true;
3020 default:
3021 break;
3022 }
3023 }
3024 return false;
3025}
3026
3027uint64_t ConstantDataSequential::getNumElements() const {
3028 if (ArrayType *AT = dyn_cast<ArrayType>(Val: getType()))
3029 return AT->getNumElements();
3030 return cast<FixedVectorType>(Val: getType())->getNumElements();
3031}
3032
3033uint64_t ConstantDataSequential::getElementByteSize() const {
3034 return getElementType()->getPrimitiveSizeInBits().getFixedValue() / 8;
3035}
3036
3037/// Return the start of the specified element.
3038const char *ConstantDataSequential::getElementPointer(uint64_t Elt) const {
3039 assert(Elt < getNumElements() && "Invalid Elt");
3040 return DataElements + Elt * getElementByteSize();
3041}
3042
3043/// Return true if the array is empty or all zeros.
3044static bool isAllZeros(StringRef Arr) {
3045 for (char I : Arr)
3046 if (I != 0)
3047 return false;
3048 return true;
3049}
3050
3051/// This is the underlying implementation of all of the
3052/// ConstantDataSequential::get methods. They all thunk down to here, providing
3053/// the correct element type. We take the bytes in as a StringRef because
3054/// we *want* an underlying "char*" to avoid TBAA type punning violations.
3055Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
3056#ifndef NDEBUG
3057 if (ArrayType *ATy = dyn_cast<ArrayType>(Ty))
3058 assert(isElementTypeCompatible(ATy->getElementType()));
3059 else
3060 assert(isElementTypeCompatible(cast<VectorType>(Ty)->getElementType()));
3061#endif
3062 // If the elements are all zero or there are no elements, return a CAZ, which
3063 // is more dense and canonical.
3064 if (isAllZeros(Arr: Elements))
3065 return ConstantAggregateZero::get(Ty);
3066
3067 // Do a lookup to see if we have already formed one of these.
3068 auto &Slot =
3069 *Ty->getContext().pImpl->CDSConstants.try_emplace(Key: Elements).first;
3070
3071 // The bucket can point to a linked list of different CDS's that have the same
3072 // body but different types. For example, 0,0,0,1 could be a 4 element array
3073 // of i8, or a 1-element array of i32. They'll both end up in the same
3074 /// StringMap bucket, linked up by their Next pointers. Walk the list.
3075 std::unique_ptr<ConstantDataSequential> *Entry = &Slot.second;
3076 for (; *Entry; Entry = &(*Entry)->Next)
3077 if ((*Entry)->getType() == Ty)
3078 return Entry->get();
3079
3080 // Okay, we didn't get a hit. Create a node of the right class, link it in,
3081 // and return it.
3082 if (isa<ArrayType>(Val: Ty)) {
3083 // Use reset because std::make_unique can't access the constructor.
3084 Entry->reset(p: new ConstantDataArray(Ty, Slot.first().data()));
3085 return Entry->get();
3086 }
3087
3088 assert(isa<VectorType>(Ty));
3089 // Use reset because std::make_unique can't access the constructor.
3090 Entry->reset(p: new ConstantDataVector(Ty, Slot.first().data()));
3091 return Entry->get();
3092}
3093
3094void ConstantDataSequential::destroyConstantImpl() {
3095 // Remove the constant from the StringMap.
3096 StringMap<std::unique_ptr<ConstantDataSequential>> &CDSConstants =
3097 getType()->getContext().pImpl->CDSConstants;
3098
3099 auto Slot = CDSConstants.find(Key: getRawDataValues());
3100
3101 assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
3102
3103 std::unique_ptr<ConstantDataSequential> *Entry = &Slot->getValue();
3104
3105 // Remove the entry from the hash table.
3106 if (!(*Entry)->Next) {
3107 // If there is only one value in the bucket (common case) it must be this
3108 // entry, and removing the entry should remove the bucket completely.
3109 assert(Entry->get() == this && "Hash mismatch in ConstantDataSequential");
3110 getContext().pImpl->CDSConstants.erase(I: Slot);
3111 return;
3112 }
3113
3114 // Otherwise, there are multiple entries linked off the bucket, unlink the
3115 // node we care about but keep the bucket around.
3116 while (true) {
3117 std::unique_ptr<ConstantDataSequential> &Node = *Entry;
3118 assert(Node && "Didn't find entry in its uniquing hash table!");
3119 // If we found our entry, unlink it from the list and we're done.
3120 if (Node.get() == this) {
3121 Node = std::move(Node->Next);
3122 return;
3123 }
3124
3125 Entry = &Node->Next;
3126 }
3127}
3128
3129/// getFP() constructors - Return a constant of array type with a float
3130/// element type taken from argument `ElementType', and count taken from
3131/// argument `Elts'. The amount of bits of the contained type must match the
3132/// number of bits of the type contained in the passed in ArrayRef.
3133/// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
3134/// that this can return a ConstantAggregateZero object.
3135Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint16_t> Elts) {
3136 assert((ElementType->isHalfTy() || ElementType->isBFloatTy()) &&
3137 "Element type is not a 16-bit float type");
3138 Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size());
3139 const char *Data = reinterpret_cast<const char *>(Elts.data());
3140 return getImpl(Elements: StringRef(Data, Elts.size() * 2), Ty);
3141}
3142Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint32_t> Elts) {
3143 assert(ElementType->isFloatTy() && "Element type is not a 32-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() * 4), Ty);
3147}
3148Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint64_t> Elts) {
3149 assert(ElementType->isDoubleTy() &&
3150 "Element type is not a 64-bit float type");
3151 Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size());
3152 const char *Data = reinterpret_cast<const char *>(Elts.data());
3153 return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty);
3154}
3155
3156/// getByte() constructors - Return a constant of array type with a byte
3157/// element type taken from argument `ElementType', and count taken from
3158/// argument `Elts'. The amount of bits of the contained type must match the
3159/// number of bits of the type contained in the passed in ArrayRef.
3160/// Note that this can return a ConstantAggregateZero object.
3161Constant *ConstantDataArray::getByte(Type *ElementType,
3162 ArrayRef<uint8_t> Elts) {
3163 assert(ElementType->isByteTy(8) && "Element type is not a 8-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() * 1), Ty);
3167}
3168Constant *ConstantDataArray::getByte(Type *ElementType,
3169 ArrayRef<uint16_t> Elts) {
3170 assert(ElementType->isByteTy(16) && "Element type is not a 16-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() * 2), Ty);
3174}
3175Constant *ConstantDataArray::getByte(Type *ElementType,
3176 ArrayRef<uint32_t> Elts) {
3177 assert(ElementType->isByteTy(32) && "Element type is not a 32-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() * 4), Ty);
3181}
3182Constant *ConstantDataArray::getByte(Type *ElementType,
3183 ArrayRef<uint64_t> Elts) {
3184 assert(ElementType->isByteTy(64) && "Element type is not a 64-bit byte type");
3185 Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size());
3186 const char *Data = reinterpret_cast<const char *>(Elts.data());
3187 return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty);
3188}
3189
3190Constant *ConstantDataArray::getString(LLVMContext &Context, StringRef Str,
3191 bool AddNull, bool ByteString) {
3192 if (!AddNull) {
3193 const uint8_t *Data = Str.bytes_begin();
3194 return ByteString
3195 ? getByte(ElementType: Type::getByte8Ty(C&: Context), Elts: ArrayRef(Data, Str.size()))
3196 : get(Context, Elts: ArrayRef(Data, Str.size()));
3197 }
3198
3199 SmallVector<uint8_t, 64> ElementVals;
3200 ElementVals.append(in_start: Str.begin(), in_end: Str.end());
3201 ElementVals.push_back(Elt: 0);
3202 return ByteString ? getByte(ElementType: Type::getByte8Ty(C&: Context), Elts: ElementVals)
3203 : get(Context, Elts&: ElementVals);
3204}
3205
3206/// get() constructors - Return a constant with vector type with an element
3207/// count and element type matching the ArrayRef passed in. Note that this
3208/// can return a ConstantAggregateZero object.
3209Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
3210 auto *Ty = FixedVectorType::get(ElementType: Type::getInt8Ty(C&: Context), NumElts: Elts.size());
3211 const char *Data = reinterpret_cast<const char *>(Elts.data());
3212 return getImpl(Elements: StringRef(Data, Elts.size() * 1), Ty);
3213}
3214Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
3215 auto *Ty = FixedVectorType::get(ElementType: Type::getInt16Ty(C&: Context), NumElts: Elts.size());
3216 const char *Data = reinterpret_cast<const char *>(Elts.data());
3217 return getImpl(Elements: StringRef(Data, Elts.size() * 2), Ty);
3218}
3219Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
3220 auto *Ty = FixedVectorType::get(ElementType: Type::getInt32Ty(C&: Context), NumElts: Elts.size());
3221 const char *Data = reinterpret_cast<const char *>(Elts.data());
3222 return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty);
3223}
3224Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
3225 auto *Ty = FixedVectorType::get(ElementType: Type::getInt64Ty(C&: Context), NumElts: Elts.size());
3226 const char *Data = reinterpret_cast<const char *>(Elts.data());
3227 return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty);
3228}
3229Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
3230 auto *Ty = FixedVectorType::get(ElementType: Type::getFloatTy(C&: Context), NumElts: Elts.size());
3231 const char *Data = reinterpret_cast<const char *>(Elts.data());
3232 return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty);
3233}
3234Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
3235 auto *Ty = FixedVectorType::get(ElementType: Type::getDoubleTy(C&: Context), NumElts: Elts.size());
3236 const char *Data = reinterpret_cast<const char *>(Elts.data());
3237 return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty);
3238}
3239
3240/// getByte() constructors - Return a constant of vector type with a byte
3241/// element type taken from argument `ElementType', and count taken from
3242/// argument `Elts'. The amount of bits of the contained type must match the
3243/// number of bits of the type contained in the passed in ArrayRef.
3244/// Note that this can return a ConstantAggregateZero object.
3245Constant *ConstantDataVector::getByte(Type *ElementType,
3246 ArrayRef<uint8_t> Elts) {
3247 assert(ElementType->isByteTy(8) && "Element type is not a 8-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() * 1), Ty);
3251}
3252Constant *ConstantDataVector::getByte(Type *ElementType,
3253 ArrayRef<uint16_t> Elts) {
3254 assert(ElementType->isByteTy(16) && "Element type is not a 16-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() * 2), Ty);
3258}
3259Constant *ConstantDataVector::getByte(Type *ElementType,
3260 ArrayRef<uint32_t> Elts) {
3261 assert(ElementType->isByteTy(32) && "Element type is not a 32-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() * 4), Ty);
3265}
3266Constant *ConstantDataVector::getByte(Type *ElementType,
3267 ArrayRef<uint64_t> Elts) {
3268 assert(ElementType->isByteTy(64) && "Element type is not a 64-bit byte");
3269 auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size());
3270 const char *Data = reinterpret_cast<const char *>(Elts.data());
3271 return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty);
3272}
3273
3274/// getFP() constructors - Return a constant of vector type with a float
3275/// element type taken from argument `ElementType', and count taken from
3276/// argument `Elts'. The amount of bits of the contained type must match the
3277/// number of bits of the type contained in the passed in ArrayRef.
3278/// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
3279/// that this can return a ConstantAggregateZero object.
3280Constant *ConstantDataVector::getFP(Type *ElementType,
3281 ArrayRef<uint16_t> Elts) {
3282 assert((ElementType->isHalfTy() || ElementType->isBFloatTy()) &&
3283 "Element type is not a 16-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() * 2), Ty);
3287}
3288Constant *ConstantDataVector::getFP(Type *ElementType,
3289 ArrayRef<uint32_t> Elts) {
3290 assert(ElementType->isFloatTy() && "Element type is not a 32-bit float type");
3291 auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size());
3292 const char *Data = reinterpret_cast<const char *>(Elts.data());
3293 return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty);
3294}
3295Constant *ConstantDataVector::getFP(Type *ElementType,
3296 ArrayRef<uint64_t> Elts) {
3297 assert(ElementType->isDoubleTy() &&
3298 "Element type is not a 64-bit float type");
3299 auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size());
3300 const char *Data = reinterpret_cast<const char *>(Elts.data());
3301 return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty);
3302}
3303
3304Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
3305 assert(isElementTypeCompatible(V->getType()) &&
3306 "Element type not compatible with ConstantData");
3307 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: V)) {
3308 if (CI->getType()->isIntegerTy(BitWidth: 8)) {
3309 SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
3310 return get(Context&: V->getContext(), Elts);
3311 }
3312 if (CI->getType()->isIntegerTy(BitWidth: 16)) {
3313 SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
3314 return get(Context&: V->getContext(), Elts);
3315 }
3316 if (CI->getType()->isIntegerTy(BitWidth: 32)) {
3317 SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
3318 return get(Context&: V->getContext(), Elts);
3319 }
3320 assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
3321 SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
3322 return get(Context&: V->getContext(), Elts);
3323 }
3324
3325 if (ConstantByte *CB = dyn_cast<ConstantByte>(Val: V)) {
3326 if (CB->getType()->isByteTy(BitWidth: 8)) {
3327 SmallVector<uint8_t, 16> Elts(NumElts, CB->getZExtValue());
3328 return getByte(ElementType: V->getType(), Elts);
3329 }
3330 if (CB->getType()->isByteTy(BitWidth: 16)) {
3331 SmallVector<uint16_t, 16> Elts(NumElts, CB->getZExtValue());
3332 return getByte(ElementType: V->getType(), Elts);
3333 }
3334 if (CB->getType()->isByteTy(BitWidth: 32)) {
3335 SmallVector<uint32_t, 16> Elts(NumElts, CB->getZExtValue());
3336 return getByte(ElementType: V->getType(), Elts);
3337 }
3338 assert(CB->getType()->isByteTy(64) && "Unsupported ConstantData type");
3339 SmallVector<uint64_t, 16> Elts(NumElts, CB->getZExtValue());
3340 return getByte(ElementType: V->getType(), Elts);
3341 }
3342
3343 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Val: V)) {
3344 if (CFP->getType()->isHalfTy()) {
3345 SmallVector<uint16_t, 16> Elts(
3346 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
3347 return getFP(ElementType: V->getType(), Elts);
3348 }
3349 if (CFP->getType()->isBFloatTy()) {
3350 SmallVector<uint16_t, 16> Elts(
3351 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
3352 return getFP(ElementType: V->getType(), Elts);
3353 }
3354 if (CFP->getType()->isFloatTy()) {
3355 SmallVector<uint32_t, 16> Elts(
3356 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
3357 return getFP(ElementType: V->getType(), Elts);
3358 }
3359 if (CFP->getType()->isDoubleTy()) {
3360 SmallVector<uint64_t, 16> Elts(
3361 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
3362 return getFP(ElementType: V->getType(), Elts);
3363 }
3364 }
3365 return ConstantVector::getSplat(EC: ElementCount::getFixed(MinVal: NumElts), V);
3366}
3367
3368uint64_t ConstantDataSequential::getElementAsInteger(uint64_t Elt) const {
3369 assert(
3370 (isa<IntegerType>(getElementType()) || isa<ByteType>(getElementType())) &&
3371 "Accessor can only be used when element is an integer or byte");
3372 const char *EltPtr = getElementPointer(Elt);
3373
3374 // The data is stored in host byte order, make sure to cast back to the right
3375 // type to load with the right endianness.
3376 switch (getElementType()->getScalarSizeInBits()) {
3377 default: llvm_unreachable("Invalid bitwidth for CDS");
3378 case 8:
3379 return *reinterpret_cast<const uint8_t *>(EltPtr);
3380 case 16:
3381 return *reinterpret_cast<const uint16_t *>(EltPtr);
3382 case 32:
3383 return *reinterpret_cast<const uint32_t *>(EltPtr);
3384 case 64:
3385 return *reinterpret_cast<const uint64_t *>(EltPtr);
3386 }
3387}
3388
3389APInt ConstantDataSequential::getElementAsAPInt(uint64_t Elt) const {
3390 assert(
3391 (isa<IntegerType>(getElementType()) || isa<ByteType>(getElementType())) &&
3392 "Accessor can only be used when element is an integer or byte");
3393 const char *EltPtr = getElementPointer(Elt);
3394
3395 // The data is stored in host byte order, make sure to cast back to the right
3396 // type to load with the right endianness.
3397 switch (getElementType()->getScalarSizeInBits()) {
3398 default: llvm_unreachable("Invalid bitwidth for CDS");
3399 case 8: {
3400 auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr);
3401 return APInt(8, EltVal);
3402 }
3403 case 16: {
3404 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
3405 return APInt(16, EltVal);
3406 }
3407 case 32: {
3408 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
3409 return APInt(32, EltVal);
3410 }
3411 case 64: {
3412 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
3413 return APInt(64, EltVal);
3414 }
3415 }
3416}
3417
3418APFloat ConstantDataSequential::getElementAsAPFloat(uint64_t Elt) const {
3419 const char *EltPtr = getElementPointer(Elt);
3420
3421 switch (getElementType()->getTypeID()) {
3422 default:
3423 llvm_unreachable("Accessor can only be used when element is float/double!");
3424 case Type::HalfTyID: {
3425 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
3426 return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal));
3427 }
3428 case Type::BFloatTyID: {
3429 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
3430 return APFloat(APFloat::BFloat(), APInt(16, EltVal));
3431 }
3432 case Type::FloatTyID: {
3433 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
3434 return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal));
3435 }
3436 case Type::DoubleTyID: {
3437 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
3438 return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal));
3439 }
3440 }
3441}
3442
3443float ConstantDataSequential::getElementAsFloat(uint64_t Elt) const {
3444 assert(getElementType()->isFloatTy() &&
3445 "Accessor can only be used when element is a 'float'");
3446 return *reinterpret_cast<const float *>(getElementPointer(Elt));
3447}
3448
3449double ConstantDataSequential::getElementAsDouble(uint64_t Elt) const {
3450 assert(getElementType()->isDoubleTy() &&
3451 "Accessor can only be used when element is a 'float'");
3452 return *reinterpret_cast<const double *>(getElementPointer(Elt));
3453}
3454
3455Constant *ConstantDataSequential::getElementAsConstant(uint64_t Elt) const {
3456 if (getElementType()->isHalfTy() || getElementType()->isBFloatTy() ||
3457 getElementType()->isFloatTy() || getElementType()->isDoubleTy())
3458 return ConstantFP::get(Context&: getContext(), V: getElementAsAPFloat(Elt));
3459
3460 if (getElementType()->isByteTy())
3461 return ConstantByte::get(Ty: getElementType(), V: getElementAsInteger(Elt));
3462
3463 return ConstantInt::get(Ty: getElementType(), V: getElementAsInteger(Elt));
3464}
3465
3466bool ConstantDataSequential::isString(unsigned CharSize) const {
3467 return isa<ArrayType>(Val: getType()) &&
3468 (getElementType()->isIntegerTy(BitWidth: CharSize) ||
3469 getElementType()->isByteTy(BitWidth: CharSize));
3470}
3471
3472bool ConstantDataSequential::isCString() const {
3473 if (!isString())
3474 return false;
3475
3476 StringRef Str = getAsString();
3477
3478 // The last value must be nul.
3479 if (Str.back() != 0) return false;
3480
3481 // Other elements must be non-nul.
3482 return !Str.drop_back().contains(C: 0);
3483}
3484
3485bool ConstantDataVector::isSplatData() const {
3486 const char *Base = getRawDataValues().data();
3487
3488 // Compare elements 1+ to the 0'th element.
3489 unsigned EltSize = getElementByteSize();
3490 for (unsigned i = 1, e = getNumElements(); i != e; ++i)
3491 if (memcmp(s1: Base, s2: Base+i*EltSize, n: EltSize))
3492 return false;
3493
3494 return true;
3495}
3496
3497bool ConstantDataVector::isSplat() const {
3498 if (!IsSplatSet) {
3499 IsSplatSet = true;
3500 IsSplat = isSplatData();
3501 }
3502 return IsSplat;
3503}
3504
3505Constant *ConstantDataVector::getSplatValue() const {
3506 // If they're all the same, return the 0th one as a representative.
3507 return isSplat() ? getElementAsConstant(Elt: 0) : nullptr;
3508}
3509
3510//===----------------------------------------------------------------------===//
3511// handleOperandChange implementations
3512
3513/// Update this constant array to change uses of
3514/// 'From' to be uses of 'To'. This must update the uniquing data structures
3515/// etc.
3516///
3517/// Note that we intentionally replace all uses of From with To here. Consider
3518/// a large array that uses 'From' 1000 times. By handling this case all here,
3519/// ConstantArray::handleOperandChange is only invoked once, and that
3520/// single invocation handles all 1000 uses. Handling them one at a time would
3521/// work, but would be really slow because it would have to unique each updated
3522/// array instance.
3523///
3524void Constant::handleOperandChange(Value *From, Value *To) {
3525 Value *Replacement = nullptr;
3526 switch (getValueID()) {
3527 default:
3528 llvm_unreachable("Not a constant!");
3529#define HANDLE_CONSTANT(Name) \
3530 case Value::Name##Val: \
3531 Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
3532 break;
3533#include "llvm/IR/Value.def"
3534 }
3535
3536 // If handleOperandChangeImpl returned nullptr, then it handled
3537 // replacing itself and we don't want to delete or replace anything else here.
3538 if (!Replacement)
3539 return;
3540
3541 // I do need to replace this with an existing value.
3542 assert(Replacement != this && "I didn't contain From!");
3543
3544 // Everyone using this now uses the replacement.
3545 replaceAllUsesWith(V: Replacement);
3546
3547 // Delete the old constant!
3548 destroyConstant();
3549}
3550
3551Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
3552 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
3553 Constant *ToC = cast<Constant>(Val: To);
3554
3555 SmallVector<Constant*, 8> Values;
3556 Values.reserve(N: getNumOperands()); // Build replacement array.
3557
3558 // Fill values with the modified operands of the constant array. Also,
3559 // compute whether this turns into an all-zeros array.
3560 unsigned NumUpdated = 0;
3561
3562 // Keep track of whether all the values in the array are "ToC".
3563 bool AllSame = true;
3564 Use *OperandList = getOperandList();
3565 unsigned OperandNo = 0;
3566 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
3567 Constant *Val = cast<Constant>(Val: O->get());
3568 if (Val == From) {
3569 OperandNo = (O - OperandList);
3570 Val = ToC;
3571 ++NumUpdated;
3572 }
3573 Values.push_back(Elt: Val);
3574 AllSame &= Val == ToC;
3575 }
3576
3577 if (AllSame && ToC->isNullValue())
3578 return ConstantAggregateZero::get(Ty: getType());
3579
3580 if (AllSame && isa<UndefValue>(Val: ToC))
3581 return UndefValue::get(Ty: getType());
3582
3583 // Check for any other type of constant-folding.
3584 if (Constant *C = getImpl(Ty: getType(), V: Values))
3585 return C;
3586
3587 // Update to the new value.
3588 return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(
3589 Operands: Values, CP: this, From, To: ToC, NumUpdated, OperandNo);
3590}
3591
3592Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
3593 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
3594 Constant *ToC = cast<Constant>(Val: To);
3595
3596 Use *OperandList = getOperandList();
3597
3598 SmallVector<Constant*, 8> Values;
3599 Values.reserve(N: getNumOperands()); // Build replacement struct.
3600
3601 // Fill values with the modified operands of the constant struct. Also,
3602 // compute whether this turns into an all-zeros struct.
3603 unsigned NumUpdated = 0;
3604 bool AllSame = true;
3605 unsigned OperandNo = 0;
3606 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
3607 Constant *Val = cast<Constant>(Val: O->get());
3608 if (Val == From) {
3609 OperandNo = (O - OperandList);
3610 Val = ToC;
3611 ++NumUpdated;
3612 }
3613 Values.push_back(Elt: Val);
3614 AllSame &= Val == ToC;
3615 }
3616
3617 if (AllSame && ToC->isNullValue())
3618 return ConstantAggregateZero::get(Ty: getType());
3619
3620 if (AllSame && isa<UndefValue>(Val: ToC))
3621 return UndefValue::get(Ty: getType());
3622
3623 // Update to the new value.
3624 return getContext().pImpl->StructConstants.replaceOperandsInPlace(
3625 Operands: Values, CP: this, From, To: ToC, NumUpdated, OperandNo);
3626}
3627
3628Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
3629 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
3630 Constant *ToC = cast<Constant>(Val: To);
3631
3632 SmallVector<Constant*, 8> Values;
3633 Values.reserve(N: getNumOperands()); // Build replacement array...
3634 unsigned NumUpdated = 0;
3635 unsigned OperandNo = 0;
3636 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3637 Constant *Val = getOperand(i_nocapture: i);
3638 if (Val == From) {
3639 OperandNo = i;
3640 ++NumUpdated;
3641 Val = ToC;
3642 }
3643 Values.push_back(Elt: Val);
3644 }
3645
3646 if (Constant *C = getImpl(V: Values))
3647 return C;
3648
3649 // Update to the new value.
3650 return getContext().pImpl->VectorConstants.replaceOperandsInPlace(
3651 Operands: Values, CP: this, From, To: ToC, NumUpdated, OperandNo);
3652}
3653
3654Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
3655 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
3656 Constant *To = cast<Constant>(Val: ToV);
3657
3658 SmallVector<Constant*, 8> NewOps;
3659 unsigned NumUpdated = 0;
3660 unsigned OperandNo = 0;
3661 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3662 Constant *Op = getOperand(i_nocapture: i);
3663 if (Op == From) {
3664 OperandNo = i;
3665 ++NumUpdated;
3666 Op = To;
3667 }
3668 NewOps.push_back(Elt: Op);
3669 }
3670 assert(NumUpdated && "I didn't contain From!");
3671
3672 if (Constant *C = getWithOperands(Ops: NewOps, Ty: getType(), OnlyIfReduced: true))
3673 return C;
3674
3675 // Update to the new value.
3676 return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
3677 Operands: NewOps, CP: this, From, To, NumUpdated, OperandNo);
3678}
3679
3680Instruction *ConstantExpr::getAsInstruction() const {
3681 SmallVector<Value *, 4> ValueOperands(operands());
3682 ArrayRef<Value*> Ops(ValueOperands);
3683
3684 switch (getOpcode()) {
3685 case Instruction::Trunc:
3686 case Instruction::PtrToAddr:
3687 case Instruction::PtrToInt:
3688 case Instruction::IntToPtr:
3689 case Instruction::BitCast:
3690 case Instruction::AddrSpaceCast:
3691 return CastInst::Create((Instruction::CastOps)getOpcode(), S: Ops[0],
3692 Ty: getType(), Name: "");
3693 case Instruction::InsertElement:
3694 return InsertElementInst::Create(Vec: Ops[0], NewElt: Ops[1], Idx: Ops[2], NameStr: "");
3695 case Instruction::ExtractElement:
3696 return ExtractElementInst::Create(Vec: Ops[0], Idx: Ops[1], NameStr: "");
3697 case Instruction::ShuffleVector:
3698 return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "");
3699
3700 case Instruction::GetElementPtr: {
3701 const auto *GO = cast<GEPOperator>(Val: this);
3702 return GetElementPtrInst::Create(PointeeType: GO->getSourceElementType(), Ptr: Ops[0],
3703 IdxList: Ops.slice(N: 1), NW: GO->getNoWrapFlags(), NameStr: "");
3704 }
3705 default:
3706 assert(getNumOperands() == 2 && "Must be binary operator?");
3707 BinaryOperator *BO = BinaryOperator::Create(
3708 Op: (Instruction::BinaryOps)getOpcode(), S1: Ops[0], S2: Ops[1], Name: "");
3709 if (isa<OverflowingBinaryOperator>(Val: BO)) {
3710 BO->setHasNoUnsignedWrap(SubclassOptionalData &
3711 OverflowingBinaryOperator::NoUnsignedWrap);
3712 BO->setHasNoSignedWrap(SubclassOptionalData &
3713 OverflowingBinaryOperator::NoSignedWrap);
3714 }
3715 if (isa<PossiblyExactOperator>(Val: BO))
3716 BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);
3717 return BO;
3718 }
3719}
3720