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