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