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