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