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