Movatterモバイル変換


[0]ホーム

URL:


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

Generated on Thu Jul 17 2025 12:27:05 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp