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