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

source code of llvm/lib/IR/Constants.cpp