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