LCOV - code coverage report
Current view: top level - lib/IR - Constants.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 1106 1257 88.0 %
Date: 2018-10-20 13:21:21 Functions: 206 224 92.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- Constants.cpp - Implement Constant nodes --------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements the Constant* classes.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/IR/Constants.h"
      15             : #include "ConstantFold.h"
      16             : #include "LLVMContextImpl.h"
      17             : #include "llvm/ADT/STLExtras.h"
      18             : #include "llvm/ADT/SmallVector.h"
      19             : #include "llvm/ADT/StringMap.h"
      20             : #include "llvm/IR/DerivedTypes.h"
      21             : #include "llvm/IR/GetElementPtrTypeIterator.h"
      22             : #include "llvm/IR/GlobalValue.h"
      23             : #include "llvm/IR/Instructions.h"
      24             : #include "llvm/IR/Module.h"
      25             : #include "llvm/IR/Operator.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             : 
      35             : //===----------------------------------------------------------------------===//
      36             : //                              Constant Class
      37             : //===----------------------------------------------------------------------===//
      38             : 
      39        3166 : bool Constant::isNegativeZeroValue() const {
      40             :   // Floating point values have an explicit -0.0 value.
      41             :   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
      42         520 :     return CFP->isZero() && CFP->isNegative();
      43             : 
      44             :   // Equivalent for a vector of -0.0's.
      45             :   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
      46          83 :     if (CV->getElementType()->isFloatingPointTy() && CV->isSplat())
      47          41 :       if (CV->getElementAsAPFloat(0).isNegZero())
      48             :         return true;
      49             : 
      50             :   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
      51           2 :     if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
      52           0 :       if (SplatCFP && SplatCFP->isZero() && SplatCFP->isNegative())
      53             :         return true;
      54             : 
      55             :   // We've already handled true FP case; any other FP vectors can't represent -0.0.
      56        2605 :   if (getType()->isFPOrFPVectorTy())
      57             :     return false;
      58             : 
      59             :   // Otherwise, just use +0.0.
      60        2591 :   return isNullValue();
      61             : }
      62             : 
      63             : // Return true iff this constant is positive zero (floating point), negative
      64             : // zero (floating point), or a null value.
      65     2000507 : bool Constant::isZeroValue() const {
      66             :   // Floating point values have an explicit -0.0 value.
      67             :   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
      68         471 :     return CFP->isZero();
      69             : 
      70             :   // Equivalent for a vector of -0.0's.
      71             :   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
      72         996 :     if (CV->getElementType()->isFloatingPointTy() && CV->isSplat())
      73         168 :       if (CV->getElementAsAPFloat(0).isZero())
      74             :         return true;
      75             : 
      76             :   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
      77         121 :     if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
      78           0 :       if (SplatCFP && SplatCFP->isZero())
      79             :         return true;
      80             : 
      81             :   // Otherwise, just use +0.0.
      82     2000005 :   return isNullValue();
      83             : }
      84             : 
      85    63356240 : bool Constant::isNullValue() const {
      86             :   // 0 is null.
      87             :   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
      88             :     return CI->isZero();
      89             : 
      90             :   // +0.0 is null.
      91             :   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
      92       47864 :     return CFP->isZero() && !CFP->isNegative();
      93             : 
      94             :   // constant zero is zero for aggregates, cpnull is null for pointers, none for
      95             :   // tokens.
      96    43841018 :   return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this) ||
      97             :          isa<ConstantTokenNone>(this);
      98             : }
      99             : 
     100     1776053 : bool Constant::isAllOnesValue() const {
     101             :   // Check for -1 integers
     102             :   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
     103             :     return CI->isMinusOne();
     104             : 
     105             :   // Check for FP which are bitcasted from -1 integers
     106             :   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
     107         160 :     return CFP->getValueAPF().bitcastToAPInt().isAllOnesValue();
     108             : 
     109             :   // Check for constant vectors which are splats of -1 values.
     110             :   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
     111         412 :     if (Constant *Splat = CV->getSplatValue())
     112          53 :       return Splat->isAllOnesValue();
     113             : 
     114             :   // Check for constant vectors which are splats of -1 values.
     115             :   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
     116        7668 :     if (CV->isSplat()) {
     117        4710 :       if (CV->getElementType()->isFloatingPointTy())
     118        1416 :         return CV->getElementAsAPFloat(0).bitcastToAPInt().isAllOnesValue();
     119        8004 :       return CV->getElementAsAPInt(0).isAllOnesValue();
     120             :     }
     121             :   }
     122             : 
     123             :   return false;
     124             : }
     125             : 
     126        1315 : bool Constant::isOneValue() const {
     127             :   // Check for 1 integers
     128             :   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
     129             :     return CI->isOne();
     130             : 
     131             :   // Check for FP which are bitcasted from 1 integers
     132             :   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
     133           0 :     return CFP->getValueAPF().bitcastToAPInt().isOneValue();
     134             : 
     135             :   // Check for constant vectors which are splats of 1 values.
     136             :   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
     137           0 :     if (Constant *Splat = CV->getSplatValue())
     138           0 :       return Splat->isOneValue();
     139             : 
     140             :   // Check for constant vectors which are splats of 1 values.
     141             :   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
     142           0 :     if (CV->isSplat()) {
     143           0 :       if (CV->getElementType()->isFloatingPointTy())
     144           0 :         return CV->getElementAsAPFloat(0).bitcastToAPInt().isOneValue();
     145           0 :       return CV->getElementAsAPInt(0).isOneValue();
     146             :     }
     147             :   }
     148             : 
     149             :   return false;
     150             : }
     151             : 
     152        4728 : bool Constant::isMinSignedValue() const {
     153             :   // Check for INT_MIN integers
     154             :   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
     155        4624 :     return CI->isMinValue(/*isSigned=*/true);
     156             : 
     157             :   // Check for FP which are bitcasted from INT_MIN integers
     158             :   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
     159           0 :     return CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
     160             : 
     161             :   // Check for constant vectors which are splats of INT_MIN values.
     162             :   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
     163           0 :     if (Constant *Splat = CV->getSplatValue())
     164           0 :       return Splat->isMinSignedValue();
     165             : 
     166             :   // Check for constant vectors which are splats of INT_MIN values.
     167             :   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
     168         104 :     if (CV->isSplat()) {
     169          14 :       if (CV->getElementType()->isFloatingPointTy())
     170           0 :         return CV->getElementAsAPFloat(0).bitcastToAPInt().isMinSignedValue();
     171          14 :       return CV->getElementAsAPInt(0).isMinSignedValue();
     172             :     }
     173             :   }
     174             : 
     175             :   return false;
     176             : }
     177             : 
     178        1543 : bool Constant::isNotMinSignedValue() const {
     179             :   // Check for INT_MIN integers
     180             :   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
     181        1532 :     return !CI->isMinValue(/*isSigned=*/true);
     182             : 
     183             :   // Check for FP which are bitcasted from INT_MIN integers
     184             :   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
     185           0 :     return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
     186             : 
     187             :   // Check for constant vectors which are splats of INT_MIN values.
     188             :   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
     189           3 :     if (Constant *Splat = CV->getSplatValue())
     190           1 :       return Splat->isNotMinSignedValue();
     191             : 
     192             :   // Check for constant vectors which are splats of INT_MIN values.
     193             :   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
     194           8 :     if (CV->isSplat()) {
     195           6 :       if (CV->getElementType()->isFloatingPointTy())
     196           0 :         return !CV->getElementAsAPFloat(0).bitcastToAPInt().isMinSignedValue();
     197           6 :       return !CV->getElementAsAPInt(0).isMinSignedValue();
     198             :     }
     199             :   }
     200             : 
     201             :   // It *may* contain INT_MIN, we can't tell.
     202             :   return false;
     203             : }
     204             : 
     205         296 : bool Constant::isFiniteNonZeroFP() const {
     206             :   if (auto *CFP = dyn_cast<ConstantFP>(this))
     207         253 :     return CFP->getValueAPF().isFiniteNonZero();
     208          86 :   if (!getType()->isVectorTy())
     209             :     return false;
     210         141 :   for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
     211         112 :     auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
     212         110 :     if (!CFP || !CFP->getValueAPF().isFiniteNonZero())
     213             :       return false;
     214             :   }
     215             :   return true;
     216             : }
     217             : 
     218          52 : bool Constant::isNormalFP() const {
     219             :   if (auto *CFP = dyn_cast<ConstantFP>(this))
     220          32 :     return CFP->getValueAPF().isNormal();
     221          40 :   if (!getType()->isVectorTy())
     222             :     return false;
     223          72 :   for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
     224          53 :     auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
     225          52 :     if (!CFP || !CFP->getValueAPF().isNormal())
     226           1 :       return false;
     227             :   }
     228             :   return true;
     229             : }
     230             : 
     231          98 : bool Constant::hasExactInverseFP() const {
     232             :   if (auto *CFP = dyn_cast<ConstantFP>(this))
     233          58 :     return CFP->getValueAPF().getExactInverse(nullptr);
     234          80 :   if (!getType()->isVectorTy())
     235             :     return false;
     236          71 :   for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
     237          64 :     auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
     238          59 :     if (!CFP || !CFP->getValueAPF().getExactInverse(nullptr))
     239          33 :       return false;
     240             :   }
     241             :   return true;
     242             : }
     243             : 
     244          98 : bool Constant::isNaN() const {
     245             :   if (auto *CFP = dyn_cast<ConstantFP>(this))
     246          92 :     return CFP->isNaN();
     247          12 :   if (!getType()->isVectorTy())
     248             :     return false;
     249          19 :   for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
     250          14 :     auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
     251          13 :     if (!CFP || !CFP->isNaN())
     252             :       return false;
     253             :   }
     254             :   return true;
     255             : }
     256             : 
     257         202 : bool Constant::containsUndefElement() const {
     258         404 :   if (!getType()->isVectorTy())
     259             :     return false;
     260         735 :   for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i)
     261         614 :     if (isa<UndefValue>(getAggregateElement(i)))
     262             :       return true;
     263             : 
     264             :   return false;
     265             : }
     266             : 
     267             : /// Constructor to create a '0' constant of arbitrary type.
     268     4939468 : Constant *Constant::getNullValue(Type *Ty) {
     269     4939468 :   switch (Ty->getTypeID()) {
     270     4737710 :   case Type::IntegerTyID:
     271     4737710 :     return ConstantInt::get(Ty, 0);
     272         588 :   case Type::HalfTyID:
     273         588 :     return ConstantFP::get(Ty->getContext(),
     274        1176 :                            APFloat::getZero(APFloat::IEEEhalf()));
     275        3065 :   case Type::FloatTyID:
     276        3065 :     return ConstantFP::get(Ty->getContext(),
     277        6130 :                            APFloat::getZero(APFloat::IEEEsingle()));
     278        3211 :   case Type::DoubleTyID:
     279        3211 :     return ConstantFP::get(Ty->getContext(),
     280        6422 :                            APFloat::getZero(APFloat::IEEEdouble()));
     281         619 :   case Type::X86_FP80TyID:
     282         619 :     return ConstantFP::get(Ty->getContext(),
     283        1238 :                            APFloat::getZero(APFloat::x87DoubleExtended()));
     284          58 :   case Type::FP128TyID:
     285          58 :     return ConstantFP::get(Ty->getContext(),
     286         116 :                            APFloat::getZero(APFloat::IEEEquad()));
     287             :   case Type::PPC_FP128TyID:
     288          15 :     return ConstantFP::get(Ty->getContext(),
     289          30 :                            APFloat(APFloat::PPCDoubleDouble(),
     290          15 :                                    APInt::getNullValue(128)));
     291             :   case Type::PointerTyID:
     292       97435 :     return ConstantPointerNull::get(cast<PointerType>(Ty));
     293       96220 :   case Type::StructTyID:
     294             :   case Type::ArrayTyID:
     295             :   case Type::VectorTyID:
     296       96220 :     return ConstantAggregateZero::get(Ty);
     297         547 :   case Type::TokenTyID:
     298         547 :     return ConstantTokenNone::get(Ty->getContext());
     299           0 :   default:
     300             :     // Function, Label, or Opaque type?
     301           0 :     llvm_unreachable("Cannot create a null constant of that type!");
     302             :   }
     303             : }
     304             : 
     305       14043 : Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
     306             :   Type *ScalarTy = Ty->getScalarType();
     307             : 
     308             :   // Create the base integer constant.
     309       14043 :   Constant *C = ConstantInt::get(Ty->getContext(), V);
     310             : 
     311             :   // Convert an integer to a pointer, if necessary.
     312             :   if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
     313           1 :     C = ConstantExpr::getIntToPtr(C, PTy);
     314             : 
     315             :   // Broadcast a scalar to a vector, if necessary.
     316             :   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
     317           1 :     C = ConstantVector::getSplat(VTy->getNumElements(), C);
     318             : 
     319       14043 :   return C;
     320             : }
     321             : 
     322      668319 : Constant *Constant::getAllOnesValue(Type *Ty) {
     323             :   if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
     324      661492 :     return ConstantInt::get(Ty->getContext(),
     325     1322984 :                             APInt::getAllOnesValue(ITy->getBitWidth()));
     326             : 
     327             :   if (Ty->isFloatingPointTy()) {
     328             :     APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(),
     329          12 :                                           !Ty->isPPC_FP128Ty());
     330          12 :     return ConstantFP::get(Ty->getContext(), FL);
     331             :   }
     332             : 
     333             :   VectorType *VTy = cast<VectorType>(Ty);
     334        6815 :   return ConstantVector::getSplat(VTy->getNumElements(),
     335        6815 :                                   getAllOnesValue(VTy->getElementType()));
     336             : }
     337             : 
     338     3657269 : Constant *Constant::getAggregateElement(unsigned Elt) const {
     339             :   if (const ConstantAggregate *CC = dyn_cast<ConstantAggregate>(this))
     340     1674659 :     return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr;
     341             : 
     342             :   if (const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(this))
     343      354896 :     return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr;
     344             : 
     345             :   if (const UndefValue *UV = dyn_cast<UndefValue>(this))
     346        5013 :     return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
     347             : 
     348             :   if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this))
     349     1622567 :     return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
     350             :                                        : nullptr;
     351             :   return nullptr;
     352             : }
     353             : 
     354        5332 : Constant *Constant::getAggregateElement(Constant *Elt) const {
     355             :   assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer");
     356             :   if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt))
     357        5332 :     return getAggregateElement(CI->getZExtValue());
     358             :   return nullptr;
     359             : }
     360             : 
     361      319141 : void Constant::destroyConstant() {
     362             :   /// First call destroyConstantImpl on the subclass.  This gives the subclass
     363             :   /// a chance to remove the constant from any maps/pools it's contained in.
     364      638282 :   switch (getValueID()) {
     365           0 :   default:
     366           0 :     llvm_unreachable("Not a constant!");
     367             : #define HANDLE_CONSTANT(Name)                                                  \
     368             :   case Value::Name##Val:                                                       \
     369             :     cast<Name>(this)->destroyConstantImpl();                                   \
     370             :     break;
     371             : #include "llvm/IR/Value.def"
     372             :   }
     373             : 
     374             :   // When a Constant is destroyed, there may be lingering
     375             :   // references to the constant by other constants in the constant pool.  These
     376             :   // constants are implicitly dependent on the module that is being deleted,
     377             :   // but they don't know that.  Because we only find out when the CPV is
     378             :   // deleted, we must now notify all of our users (that should only be
     379             :   // Constants) that they are, in fact, invalid now and should be deleted.
     380             :   //
     381      319141 :   while (!use_empty()) {
     382             :     Value *V = user_back();
     383             : #ifndef NDEBUG // Only in -g mode...
     384             :     if (!isa<Constant>(V)) {
     385             :       dbgs() << "While deleting: " << *this
     386             :              << "\n\nUse still stuck around after Def is destroyed: " << *V
     387             :              << "\n\n";
     388             :     }
     389             : #endif
     390             :     assert(isa<Constant>(V) && "References remain to Constant being destroyed");
     391           0 :     cast<Constant>(V)->destroyConstant();
     392             : 
     393             :     // The constant should remove itself from our use list...
     394             :     assert((use_empty() || user_back() != V) && "Constant not removed!");
     395             :   }
     396             : 
     397             :   // Value has no outstanding references it is safe to delete it now...
     398      319141 :   delete this;
     399      319141 : }
     400             : 
     401     2475945 : static bool canTrapImpl(const Constant *C,
     402             :                         SmallPtrSetImpl<const ConstantExpr *> &NonTrappingOps) {
     403             :   assert(C->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
     404             :   // The only thing that could possibly trap are constant exprs.
     405             :   const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
     406             :   if (!CE)
     407             :     return false;
     408             : 
     409             :   // ConstantExpr traps if any operands can trap.
     410     2210173 :   for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
     411             :     if (ConstantExpr *Op = dyn_cast<ConstantExpr>(CE->getOperand(i))) {
     412        2746 :       if (NonTrappingOps.insert(Op).second && canTrapImpl(Op, NonTrappingOps))
     413          14 :         return true;
     414             :     }
     415             :   }
     416             : 
     417             :   // Otherwise, only specific operations can trap.
     418             :   switch (CE->getOpcode()) {
     419             :   default:
     420             :     return false;
     421             :   case Instruction::UDiv:
     422             :   case Instruction::SDiv:
     423             :   case Instruction::URem:
     424             :   case Instruction::SRem:
     425             :     // Div and rem can trap if the RHS is not known to be non-zero.
     426          24 :     if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
     427          24 :       return true;
     428             :     return false;
     429             :   }
     430             : }
     431             : 
     432     2473573 : bool Constant::canTrap() const {
     433             :   SmallPtrSet<const ConstantExpr *, 4> NonTrappingOps;
     434     2473573 :   return canTrapImpl(this, NonTrappingOps);
     435             : }
     436             : 
     437             : /// Check if C contains a GlobalValue for which Predicate is true.
     438             : static bool
     439       61651 : ConstHasGlobalValuePredicate(const Constant *C,
     440             :                              bool (*Predicate)(const GlobalValue *)) {
     441             :   SmallPtrSet<const Constant *, 8> Visited;
     442             :   SmallVector<const Constant *, 8> WorkList;
     443       61651 :   WorkList.push_back(C);
     444       61651 :   Visited.insert(C);
     445             : 
     446      126369 :   while (!WorkList.empty()) {
     447             :     const Constant *WorkItem = WorkList.pop_back_val();
     448             :     if (const auto *GV = dyn_cast<GlobalValue>(WorkItem))
     449        1432 :       if (Predicate(GV))
     450             :         return true;
     451      133238 :     for (const Value *Op : WorkItem->operands()) {
     452        3802 :       const Constant *ConstOp = dyn_cast<Constant>(Op);
     453        3802 :       if (!ConstOp)
     454           1 :         continue;
     455        3801 :       if (Visited.insert(ConstOp).second)
     456        3079 :         WorkList.push_back(ConstOp);
     457             :     }
     458             :   }
     459             :   return false;
     460             : }
     461             : 
     462       58050 : bool Constant::isThreadDependent() const {
     463             :   auto DLLImportPredicate = [](const GlobalValue *GV) {
     464             :     return GV->isThreadLocal();
     465             :   };
     466       58050 :   return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
     467             : }
     468             : 
     469        3601 : bool Constant::isDLLImportDependent() const {
     470             :   auto DLLImportPredicate = [](const GlobalValue *GV) {
     471             :     return GV->hasDLLImportStorageClass();
     472             :   };
     473        3601 :   return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
     474             : }
     475             : 
     476      146137 : bool Constant::isConstantUsed() const {
     477      146256 :   for (const User *U : users()) {
     478             :     const Constant *UC = dyn_cast<Constant>(U);
     479             :     if (!UC || isa<GlobalValue>(UC))
     480             :       return true;
     481             : 
     482       72736 :     if (UC->isConstantUsed())
     483             :       return true;
     484             :   }
     485             :   return false;
     486             : }
     487             : 
     488      915422 : bool Constant::needsRelocation() const {
     489             :   if (isa<GlobalValue>(this))
     490             :     return true; // Global reference.
     491             : 
     492             :   if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
     493         219 :     return BA->getFunction()->needsRelocation();
     494             : 
     495             :   // While raw uses of blockaddress need to be relocated, differences between
     496             :   // two of them don't when they are for labels in the same function.  This is a
     497             :   // common idiom when creating a table for the indirect goto extension, so we
     498             :   // handle it efficiently here.
     499             :   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
     500      149372 :     if (CE->getOpcode() == Instruction::Sub) {
     501             :       ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
     502             :       ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
     503          99 :       if (LHS && RHS && LHS->getOpcode() == Instruction::PtrToInt &&
     504          99 :           RHS->getOpcode() == Instruction::PtrToInt &&
     505           6 :           isa<BlockAddress>(LHS->getOperand(0)) &&
     506         105 :           isa<BlockAddress>(RHS->getOperand(0)) &&
     507             :           cast<BlockAddress>(LHS->getOperand(0))->getFunction() ==
     508             :               cast<BlockAddress>(RHS->getOperand(0))->getFunction())
     509             :         return false;
     510             :     }
     511             : 
     512             :   bool Result = false;
     513     1450441 :   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
     514     1320846 :     Result |= cast<Constant>(getOperand(i))->needsRelocation();
     515             : 
     516             :   return Result;
     517             : }
     518             : 
     519             : /// If the specified constantexpr is dead, remove it. This involves recursively
     520             : /// eliminating any dead users of the constantexpr.
     521     4340292 : static bool removeDeadUsersOfConstant(const Constant *C) {
     522             :   if (isa<GlobalValue>(C)) return false; // Cannot remove this
     523             : 
     524     3852193 :   while (!C->use_empty()) {
     525             :     const Constant *User = dyn_cast<Constant>(C->user_back());
     526             :     if (!User) return false; // Non-constant usage;
     527     1607281 :     if (!removeDeadUsersOfConstant(User))
     528             :       return false; // Constant wasn't dead
     529             :   }
     530             : 
     531      316329 :   const_cast<Constant*>(C)->destroyConstant();
     532      316329 :   return true;
     533             : }
     534             : 
     535             : 
     536     2688400 : void Constant::removeDeadConstantUsers() const {
     537             :   Value::const_user_iterator I = user_begin(), E = user_end();
     538             :   Value::const_user_iterator LastNonDeadUser = E;
     539     7870331 :   while (I != E) {
     540             :     const Constant *User = dyn_cast<Constant>(*I);
     541             :     if (!User) {
     542             :       LastNonDeadUser = I;
     543             :       ++I;
     544     2545403 :       continue;
     545             :     }
     546             : 
     547     2733011 :     if (!removeDeadUsersOfConstant(User)) {
     548             :       // If the constant wasn't dead, remember that this was the last live use
     549             :       // and move on to the next constant.
     550             :       LastNonDeadUser = I;
     551             :       ++I;
     552     2458891 :       continue;
     553             :     }
     554             : 
     555             :     // If the constant was dead, then the iterator is invalidated.
     556      274120 :     if (LastNonDeadUser == E) {
     557             :       I = user_begin();
     558      147504 :       if (I == E) break;
     559             :     } else {
     560             :       I = LastNonDeadUser;
     561             :       ++I;
     562             :     }
     563             :   }
     564     2688400 : }
     565             : 
     566             : 
     567             : 
     568             : //===----------------------------------------------------------------------===//
     569             : //                                ConstantInt
     570             : //===----------------------------------------------------------------------===//
     571             : 
     572     1011159 : ConstantInt::ConstantInt(IntegerType *Ty, const APInt &V)
     573     1011159 :     : ConstantData(Ty, ConstantIntVal), Val(V) {
     574             :   assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
     575     1011159 : }
     576             : 
     577      435898 : ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
     578      435898 :   LLVMContextImpl *pImpl = Context.pImpl;
     579      435898 :   if (!pImpl->TheTrueVal)
     580       10862 :     pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
     581      435898 :   return pImpl->TheTrueVal;
     582             : }
     583             : 
     584      266037 : ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
     585      266037 :   LLVMContextImpl *pImpl = Context.pImpl;
     586      266037 :   if (!pImpl->TheFalseVal)
     587        8483 :     pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
     588      266037 :   return pImpl->TheFalseVal;
     589             : }
     590             : 
     591       33681 : Constant *ConstantInt::getTrue(Type *Ty) {
     592             :   assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
     593       33681 :   ConstantInt *TrueC = ConstantInt::getTrue(Ty->getContext());
     594             :   if (auto *VTy = dyn_cast<VectorType>(Ty))
     595          48 :     return ConstantVector::getSplat(VTy->getNumElements(), TrueC);
     596             :   return TrueC;
     597             : }
     598             : 
     599        8260 : Constant *ConstantInt::getFalse(Type *Ty) {
     600             :   assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
     601        8260 :   ConstantInt *FalseC = ConstantInt::getFalse(Ty->getContext());
     602             :   if (auto *VTy = dyn_cast<VectorType>(Ty))
     603          59 :     return ConstantVector::getSplat(VTy->getNumElements(), FalseC);
     604             :   return FalseC;
     605             : }
     606             : 
     607             : // Get a ConstantInt from an APInt.
     608    93898313 : ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
     609             :   // get an existing value or the insertion position
     610    93898313 :   LLVMContextImpl *pImpl = Context.pImpl;
     611    93898313 :   std::unique_ptr<ConstantInt> &Slot = pImpl->IntConstants[V];
     612    93898312 :   if (!Slot) {
     613             :     // Get the corresponding integer type for the bit width of the value.
     614     1011159 :     IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
     615     1011159 :     Slot.reset(new ConstantInt(ITy, V));
     616             :   }
     617             :   assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth()));
     618    93898312 :   return Slot.get();
     619             : }
     620             : 
     621     9554027 : Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
     622     9555627 :   Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
     623             : 
     624             :   // For vectors, broadcast the value.
     625             :   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
     626        1600 :     return ConstantVector::getSplat(VTy->getNumElements(), C);
     627             : 
     628             :   return C;
     629             : }
     630             : 
     631    21168976 : ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) {
     632    42337952 :   return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
     633             : }
     634             : 
     635        1510 : ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) {
     636        1510 :   return get(Ty, V, true);
     637             : }
     638             : 
     639        8448 : Constant *ConstantInt::getSigned(Type *Ty, int64_t V) {
     640        8448 :   return get(Ty, V, true);
     641             : }
     642             : 
     643    35716128 : Constant *ConstantInt::get(Type *Ty, const APInt& V) {
     644    35716128 :   ConstantInt *C = get(Ty->getContext(), V);
     645             :   assert(C->getType() == Ty->getScalarType() &&
     646             :          "ConstantInt type doesn't match the type implied by its value!");
     647             : 
     648             :   // For vectors, broadcast the value.
     649             :   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
     650         636 :     return ConstantVector::getSplat(VTy->getNumElements(), C);
     651             : 
     652             :   return C;
     653             : }
     654             : 
     655         297 : ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) {
     656         594 :   return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
     657             : }
     658             : 
     659             : /// Remove the constant from the constant table.
     660           0 : void ConstantInt::destroyConstantImpl() {
     661           0 :   llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
     662             : }
     663             : 
     664             : //===----------------------------------------------------------------------===//
     665             : //                                ConstantFP
     666             : //===----------------------------------------------------------------------===//
     667             : 
     668        9375 : static const fltSemantics *TypeToFloatSemantics(Type *Ty) {
     669        9375 :   if (Ty->isHalfTy())
     670         425 :     return &APFloat::IEEEhalf();
     671        8950 :   if (Ty->isFloatTy())
     672        5225 :     return &APFloat::IEEEsingle();
     673        3725 :   if (Ty->isDoubleTy())
     674        3543 :     return &APFloat::IEEEdouble();
     675         182 :   if (Ty->isX86_FP80Ty())
     676         119 :     return &APFloat::x87DoubleExtended();
     677          63 :   else if (Ty->isFP128Ty())
     678          53 :     return &APFloat::IEEEquad();
     679             : 
     680             :   assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
     681          10 :   return &APFloat::PPCDoubleDouble();
     682             : }
     683             : 
     684        1096 : Constant *ConstantFP::get(Type *Ty, double V) {
     685        1096 :   LLVMContext &Context = Ty->getContext();
     686             : 
     687        1096 :   APFloat FV(V);
     688             :   bool ignored;
     689        1096 :   FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
     690             :              APFloat::rmNearestTiesToEven, &ignored);
     691        1096 :   Constant *C = get(Context, FV);
     692             : 
     693             :   // For vectors, broadcast the value.
     694             :   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
     695         112 :     return ConstantVector::getSplat(VTy->getNumElements(), C);
     696             : 
     697             :   return C;
     698             : }
     699             : 
     700           0 : Constant *ConstantFP::get(Type *Ty, const APFloat &V) {
     701           0 :   ConstantFP *C = get(Ty->getContext(), V);
     702             :   assert(C->getType() == Ty->getScalarType() &&
     703             :          "ConstantFP type doesn't match the type implied by its value!");
     704             : 
     705             :   // For vectors, broadcast the value.
     706             :   if (auto *VTy = dyn_cast<VectorType>(Ty))
     707           0 :     return ConstantVector::getSplat(VTy->getNumElements(), C);
     708             : 
     709             :   return C;
     710             : }
     711             : 
     712           0 : Constant *ConstantFP::get(Type *Ty, StringRef Str) {
     713           0 :   LLVMContext &Context = Ty->getContext();
     714             : 
     715           0 :   APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
     716           0 :   Constant *C = get(Context, FV);
     717             : 
     718             :   // For vectors, broadcast the value.
     719             :   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
     720           0 :     return ConstantVector::getSplat(VTy->getNumElements(), C);
     721             : 
     722             :   return C;
     723             : }
     724             : 
     725         543 : Constant *ConstantFP::getNaN(Type *Ty, bool Negative, unsigned Type) {
     726         543 :   const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
     727         543 :   APFloat NaN = APFloat::getNaN(Semantics, Negative, Type);
     728         543 :   Constant *C = get(Ty->getContext(), NaN);
     729             : 
     730             :   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
     731           6 :     return ConstantVector::getSplat(VTy->getNumElements(), C);
     732             : 
     733             :   return C;
     734             : }
     735             : 
     736        7485 : Constant *ConstantFP::getNegativeZero(Type *Ty) {
     737        7485 :   const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
     738             :   APFloat NegZero = APFloat::getZero(Semantics, /*Negative=*/true);
     739        7485 :   Constant *C = get(Ty->getContext(), NegZero);
     740             : 
     741             :   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
     742        3486 :     return ConstantVector::getSplat(VTy->getNumElements(), C);
     743             : 
     744             :   return C;
     745             : }
     746             : 
     747             : 
     748      504151 : Constant *ConstantFP::getZeroValueForNegation(Type *Ty) {
     749             :   if (Ty->isFPOrFPVectorTy())
     750        7387 :     return getNegativeZero(Ty);
     751             : 
     752      496764 :   return Constant::getNullValue(Ty);
     753             : }
     754             : 
     755             : 
     756             : // ConstantFP accessors.
     757      187756 : ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
     758      187756 :   LLVMContextImpl* pImpl = Context.pImpl;
     759             : 
     760      187756 :   std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];
     761             : 
     762      187756 :   if (!Slot) {
     763             :     Type *Ty;
     764       31467 :     if (&V.getSemantics() == &APFloat::IEEEhalf())
     765        1130 :       Ty = Type::getHalfTy(Context);
     766       30337 :     else if (&V.getSemantics() == &APFloat::IEEEsingle())
     767       15537 :       Ty = Type::getFloatTy(Context);
     768       14800 :     else if (&V.getSemantics() == &APFloat::IEEEdouble())
     769       13397 :       Ty = Type::getDoubleTy(Context);
     770        1403 :     else if (&V.getSemantics() == &APFloat::x87DoubleExtended())
     771         942 :       Ty = Type::getX86_FP80Ty(Context);
     772         461 :     else if (&V.getSemantics() == &APFloat::IEEEquad())
     773         380 :       Ty = Type::getFP128Ty(Context);
     774             :     else {
     775             :       assert(&V.getSemantics() == &APFloat::PPCDoubleDouble() &&
     776             :              "Unknown FP format");
     777          81 :       Ty = Type::getPPC_FP128Ty(Context);
     778             :     }
     779       31467 :     Slot.reset(new ConstantFP(Ty, V));
     780             :   }
     781             : 
     782      187756 :   return Slot.get();
     783             : }
     784             : 
     785         251 : Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
     786         251 :   const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
     787         753 :   Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
     788             : 
     789             :   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
     790           6 :     return ConstantVector::getSplat(VTy->getNumElements(), C);
     791             : 
     792             :   return C;
     793             : }
     794             : 
     795       31467 : ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
     796             :     : ConstantData(Ty, ConstantFPVal), Val(V) {
     797             :   assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
     798             :          "FP type Mismatch");
     799       31467 : }
     800             : 
     801        5333 : bool ConstantFP::isExactlyValue(const APFloat &V) const {
     802        5333 :   return Val.bitwiseIsEqual(V);
     803             : }
     804             : 
     805             : /// Remove the constant from the constant table.
     806           0 : void ConstantFP::destroyConstantImpl() {
     807           0 :   llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
     808             : }
     809             : 
     810             : //===----------------------------------------------------------------------===//
     811             : //                   ConstantAggregateZero Implementation
     812             : //===----------------------------------------------------------------------===//
     813             : 
     814      353882 : Constant *ConstantAggregateZero::getSequentialElement() const {
     815      707764 :   return Constant::getNullValue(getType()->getSequentialElementType());
     816             : }
     817             : 
     818        1034 : Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
     819        2068 :   return Constant::getNullValue(getType()->getStructElementType(Elt));
     820             : }
     821             : 
     822           0 : Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
     823           0 :   if (isa<SequentialType>(getType()))
     824           0 :     return getSequentialElement();
     825           0 :   return getStructElement(cast<ConstantInt>(C)->getZExtValue());
     826             : }
     827             : 
     828      354916 : Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
     829      354916 :   if (isa<SequentialType>(getType()))
     830      353882 :     return getSequentialElement();
     831        1034 :   return getStructElement(Idx);
     832             : }
     833             : 
     834      354934 : unsigned ConstantAggregateZero::getNumElements() const {
     835      354934 :   Type *Ty = getType();
     836             :   if (auto *AT = dyn_cast<ArrayType>(Ty))
     837      166085 :     return AT->getNumElements();
     838             :   if (auto *VT = dyn_cast<VectorType>(Ty))
     839      187814 :     return VT->getNumElements();
     840        1035 :   return Ty->getStructNumElements();
     841             : }
     842             : 
     843             : //===----------------------------------------------------------------------===//
     844             : //                         UndefValue Implementation
     845             : //===----------------------------------------------------------------------===//
     846             : 
     847        2480 : UndefValue *UndefValue::getSequentialElement() const {
     848        4960 :   return UndefValue::get(getType()->getSequentialElementType());
     849             : }
     850             : 
     851        2527 : UndefValue *UndefValue::getStructElement(unsigned Elt) const {
     852        5054 :   return UndefValue::get(getType()->getStructElementType(Elt));
     853             : }
     854             : 
     855           0 : UndefValue *UndefValue::getElementValue(Constant *C) const {
     856           0 :   if (isa<SequentialType>(getType()))
     857           0 :     return getSequentialElement();
     858           0 :   return getStructElement(cast<ConstantInt>(C)->getZExtValue());
     859             : }
     860             : 
     861        5007 : UndefValue *UndefValue::getElementValue(unsigned Idx) const {
     862        5007 :   if (isa<SequentialType>(getType()))
     863        2480 :     return getSequentialElement();
     864        2527 :   return getStructElement(Idx);
     865             : }
     866             : 
     867        5013 : unsigned UndefValue::getNumElements() const {
     868        5013 :   Type *Ty = getType();
     869             :   if (auto *ST = dyn_cast<SequentialType>(Ty))
     870        2483 :     return ST->getNumElements();
     871        2530 :   return Ty->getStructNumElements();
     872             : }
     873             : 
     874             : //===----------------------------------------------------------------------===//
     875             : //                            ConstantXXX Classes
     876             : //===----------------------------------------------------------------------===//
     877             : 
     878             : template <typename ItTy, typename EltTy>
     879             : static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
     880       27366 :   for (; Start != End; ++Start)
     881       26816 :     if (*Start != Elt)
     882             :       return false;
     883             :   return true;
     884             : }
     885             : 
     886             : template <typename SequentialTy, typename ElementTy>
     887      318075 : static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {
     888             :   assert(!V.empty() && "Cannot get empty int sequence.");
     889             : 
     890             :   SmallVector<ElementTy, 16> Elts;
     891     1832462 :   for (Constant *C : V)
     892             :     if (auto *CI = dyn_cast<ConstantInt>(C))
     893     1514387 :       Elts.push_back(CI->getZExtValue());
     894             :     else
     895             :       return nullptr;
     896      213226 :   return SequentialTy::get(V[0]->getContext(), Elts);
     897             : }
     898      203674 : 
     899             : template <typename SequentialTy, typename ElementTy>
     900             : static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {
     901             :   assert(!V.empty() && "Cannot get empty FP sequence.");
     902      539941 : 
     903             :   SmallVector<ElementTy, 16> Elts;
     904      336267 :   for (Constant *C : V)
     905             :     if (auto *CFP = dyn_cast<ConstantFP>(C))
     906             :       Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
     907      108194 :     else
     908             :       return nullptr;
     909       69025 :   return SequentialTy::getFP(V[0]->getContext(), Elts);
     910             : }
     911             : 
     912             : template <typename SequenceTy>
     913      718744 : static Constant *getSequenceIfElementsMatch(Constant *C,
     914             :                                             ArrayRef<Constant *> V) {
     915      649719 :   // We speculatively build the elements here even if it turns out that there is
     916             :   // a constantexpr or something else weird, since it is so uncommon for that to
     917             :   // happen.
     918       53085 :   if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
     919             :     if (CI->getType()->isIntegerTy(8))
     920        9577 :       return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
     921             :     else if (CI->getType()->isIntegerTy(16))
     922             :       return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
     923             :     else if (CI->getType()->isIntegerTy(32))
     924      124925 :       return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
     925             :     else if (CI->getType()->isIntegerTy(64))
     926      115348 :       return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
     927             :   } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
     928             :     if (CFP->getType()->isHalfTy())
     929        8420 :       return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
     930             :     else if (CFP->getType()->isFloatTy())
     931       20298 :       return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
     932             :     else if (CFP->getType()->isDoubleTy())
     933             :       return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
     934             :   }
     935      355675 : 
     936             :   return nullptr;
     937      335377 : }
     938             : 
     939             : ConstantAggregate::ConstantAggregate(CompositeType *T, ValueTy VT,
     940       12789 :                                      ArrayRef<Constant *> V)
     941             :     : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(),
     942        1681 :                V.size()) {
     943             :   std::copy(V.begin(), V.end(), op_begin());
     944             : 
     945             :   // Check that types match, unless this is an opaque struct.
     946        6085 :   if (auto *ST = dyn_cast<StructType>(T))
     947             :     if (ST->isOpaque())
     948        4404 :       return;
     949             :   for (unsigned I = 0, E = V.size(); I != E; ++I)
     950             :     assert(V[I]->getType() == T->getTypeAtIndex(I) &&
     951        3346 :            "Initializer for composite element doesn't match!");
     952             : }
     953       12841 : 
     954             : ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
     955             :     : ConstantAggregate(T, ConstantArrayVal, V) {
     956             :   assert(V.size() == T->getNumElements() &&
     957       79773 :          "Invalid initializer for constant array");
     958             : }
     959       66932 : 
     960             : Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
     961             :   if (Constant *C = getImpl(Ty, V))
     962       25560 :     return C;
     963             :   return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
     964         148 : }
     965             : 
     966             : Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
     967             :   // Empty arrays are canonicalized to ConstantAggregateZero.
     968         654 :   if (V.empty())
     969             :     return ConstantAggregateZero::get(Ty);
     970         506 : 
     971             :   for (unsigned i = 0, e = V.size(); i != e; ++i) {
     972             :     assert(V[i]->getType() == Ty->getElementType() &&
     973         296 :            "Wrong type in array element initializer");
     974             :   }
     975         831 : 
     976             :   // If this is an all-zero array, return a ConstantAggregateZero object.  If
     977             :   // all undef, return an UndefValue, if "all simple", then return a
     978             :   // ConstantDataArray.
     979        6665 :   Constant *C = V[0];
     980             :   if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
     981        5834 :     return UndefValue::get(Ty);
     982             : 
     983             :   if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
     984        1536 :     return ConstantAggregateZero::get(Ty);
     985             : 
     986             :   // Check to see if all of the elements are ConstantFP or ConstantInt and if
     987             :   // the element type is compatible with ConstantDataVector.  If so, use it.
     988       12732 :   if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
     989             :     return getSequenceIfElementsMatch<ConstantDataArray>(C, V);
     990             : 
     991             :   // Otherwise, we really do want to create a ConstantArray.
     992       63364 :   return nullptr;
     993             : }
     994      101264 : 
     995             : StructType *ConstantStruct::getTypeForElements(LLVMContext &Context,
     996             :                                                ArrayRef<Constant*> V,
     997       10949 :                                                bool Packed) {
     998             :   unsigned VecSize = V.size();
     999        5317 :   SmallVector<Type*, 16> EltTypes(VecSize);
    1000             :   for (unsigned i = 0; i != VecSize; ++i)
    1001             :     EltTypes[i] = V[i]->getType();
    1002             : 
    1003       20174 :   return StructType::get(Context, EltTypes, Packed);
    1004             : }
    1005       29714 : 
    1006             : 
    1007             : StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
    1008        4683 :                                                bool Packed) {
    1009             :   assert(!V.empty() &&
    1010        6321 :          "ConstantStruct::getTypeForElements cannot be called on empty list");
    1011             :   return getTypeForElements(V[0]->getContext(), V, Packed);
    1012             : }
    1013             : 
    1014       38450 : ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
    1015             :     : ConstantAggregate(T, ConstantStructVal, V) {
    1016       64258 :   assert((T->isOpaque() || V.size() == T->getNumElements()) &&
    1017             :          "Invalid initializer for constant struct");
    1018             : }
    1019        5241 : 
    1020             : // ConstantStruct accessors.
    1021         664 : Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
    1022             :   assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
    1023             :          "Incorrect # elements specified to ConstantStruct::get");
    1024             : 
    1025        2186 :   // Create a ConstantAggregateZero value if all elements are zeros.
    1026             :   bool isZero = true;
    1027        3044 :   bool isUndef = false;
    1028             : 
    1029             :   if (!V.empty()) {
    1030         595 :     isUndef = isa<UndefValue>(V[0]);
    1031             :     isZero = V[0]->isNullValue();
    1032         329 :     if (isUndef || isZero) {
    1033             :       for (unsigned i = 0, e = V.size(); i != e; ++i) {
    1034             :         if (!V[i]->isNullValue())
    1035             :           isZero = false;
    1036        1664 :         if (!isa<UndefValue>(V[i]))
    1037             :           isUndef = false;
    1038        2670 :       }
    1039             :     }
    1040             :   }
    1041         329 :   if (isZero)
    1042             :     return ConstantAggregateZero::get(ST);
    1043          90 :   if (isUndef)
    1044             :     return UndefValue::get(ST);
    1045             : 
    1046             :   return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
    1047         848 : }
    1048             : 
    1049        1516 : ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
    1050             :     : ConstantAggregate(T, ConstantVectorVal, V) {
    1051             :   assert(V.size() == T->getNumElements() &&
    1052          90 :          "Invalid initializer for constant vector");
    1053             : }
    1054          11 : 
    1055             : // ConstantVector accessors.
    1056             : Constant *ConstantVector::get(ArrayRef<Constant*> V) {
    1057             :   if (Constant *C = getImpl(V))
    1058          42 :     return C;
    1059             :   VectorType *Ty = VectorType::get(V.front()->getType(), V.size());
    1060          62 :   return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
    1061             : }
    1062             : 
    1063          11 : Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
    1064             :   assert(!V.empty() && "Vectors can't be empty");
    1065             :   VectorType *T = VectorType::get(V.front()->getType(), V.size());
    1066             : 
    1067      336860 :   // If this is an all-undef or all-zero vector, return a
    1068             :   // ConstantAggregateZero or UndefValue.
    1069             :   Constant *C = V[0];
    1070             :   bool isZero = C->isNullValue();
    1071             :   bool isUndef = isa<UndefValue>(C);
    1072             : 
    1073      318075 :   if (isZero || isUndef) {
    1074       21129 :     for (unsigned i = 1, e = V.size(); i != e; ++i)
    1075      296946 :       if (V[i] != C) {
    1076        9725 :         isZero = isUndef = false;
    1077      287221 :         break;
    1078       81866 :       }
    1079      205355 :   }
    1080      205355 : 
    1081             :   if (isZero)
    1082       25464 :     return ConstantAggregateZero::get(T);
    1083         675 :   if (isUndef)
    1084       12057 :     return UndefValue::get(T);
    1085        6411 : 
    1086        5646 :   // Check to see if all of the elements are ConstantFP or ConstantInt and if
    1087        5646 :   // the element type is compatible with ConstantDataVector.  If so, use it.
    1088             :   if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
    1089             :     return getSequenceIfElementsMatch<ConstantDataVector>(C, V);
    1090             : 
    1091             :   // Otherwise, the element type isn't compatible with ConstantDataVector, or
    1092      320780 :   // the operand list contains a ConstantExpr or something else strange.
    1093             :   return nullptr;
    1094             : }
    1095             : 
    1096             : Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
    1097             :   // If this splat is compatible with ConstantDataVector, use it instead of
    1098      302574 :   // ConstantVector.
    1099       20298 :   if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) &&
    1100      282276 :       ConstantDataSequential::isElementTypeCompatible(V->getType()))
    1101        9577 :     return ConstantDataVector::getSplat(NumElts, V);
    1102      272699 : 
    1103       69025 :   SmallVector<Constant*, 32> Elts(NumElts, V);
    1104      203674 :   return get(Elts);
    1105      203674 : }
    1106             : 
    1107       24604 : ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) {
    1108         664 :   LLVMContextImpl *pImpl = Context.pImpl;
    1109       11638 :   if (!pImpl->TheNoneToken)
    1110        6321 :     pImpl->TheNoneToken.reset(new ConstantTokenNone(Context));
    1111        5317 :   return pImpl->TheNoneToken.get();
    1112        5317 : }
    1113             : 
    1114             : /// Remove the constant from the constant table.
    1115             : void ConstantTokenNone::destroyConstantImpl() {
    1116             :   llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
    1117       16080 : }
    1118             : 
    1119             : // Utility function for determining if a ConstantExpr is a CastOp or not. This
    1120             : // can't be inline because we don't want to #include Instruction.h into
    1121             : // Constant.h
    1122             : bool ConstantExpr::isCast() const {
    1123       15501 :   return Instruction::isCast(getOpcode());
    1124         831 : }
    1125       14670 : 
    1126         148 : bool ConstantExpr::isCompare() const {
    1127       14522 :   return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
    1128       12841 : }
    1129        1681 : 
    1130        1681 : bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
    1131             :   if (getOpcode() != Instruction::GetElementPtr) return false;
    1132         860 : 
    1133          11 :   gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
    1134         419 :   User::const_op_iterator OI = std::next(this->op_begin());
    1135          90 : 
    1136         329 :   // The remaining indices may be compile-time known integers within the bounds
    1137         329 :   // of the corresponding notional static array types.
    1138             :   for (; GEPI != E; ++GEPI, ++OI) {
    1139             :     if (isa<UndefValue>(*OI))
    1140             :       continue;
    1141             :     auto *CI = dyn_cast<ConstantInt>(*OI);
    1142             :     if (!CI || (GEPI.isBoundedSequential() &&
    1143      168925 :                 (CI->getValue().getActiveBits() > 64 ||
    1144             :                  CI->getZExtValue() >= GEPI.getSequentialNumElements())))
    1145             :       return false;
    1146             :   }
    1147             : 
    1148             :   // All the indices checked out.
    1149             :   return true;
    1150             : }
    1151             : 
    1152             : bool ConstantExpr::hasIndices() const {
    1153             :   return getOpcode() == Instruction::ExtractValue ||
    1154             :          getOpcode() == Instruction::InsertValue;
    1155             : }
    1156             : 
    1157             : ArrayRef<unsigned> ConstantExpr::getIndices() const {
    1158       49888 :   if (const ExtractValueConstantExpr *EVCE =
    1159       49888 :         dyn_cast<ExtractValueConstantExpr>(this))
    1160             :     return EVCE->Indices;
    1161             : 
    1162       49888 :   return cast<InsertValueConstantExpr>(this)->Indices;
    1163             : }
    1164       69498 : 
    1165       69498 : unsigned ConstantExpr::getPredicate() const {
    1166             :   return cast<CompareConstantExpr>(this)->predicate;
    1167      105652 : }
    1168             : 
    1169             : Constant *
    1170       71029 : ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
    1171             :   assert(Op->getType() == getOperand(OpNo)->getType() &&
    1172       71029 :          "Replacing operand with value of different type!");
    1173         324 :   if (getOperand(OpNo) == Op)
    1174             :     return const_cast<ConstantExpr*>(this);
    1175             : 
    1176             :   SmallVector<Constant*, 8> NewOps;
    1177             :   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
    1178             :     NewOps.push_back(i == OpNo ? Op : getOperand(i));
    1179             : 
    1180             :   return getWithOperands(NewOps);
    1181             : }
    1182             : 
    1183       70705 : Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
    1184       70715 :                                         bool OnlyIfReduced, Type *SrcTy) const {
    1185           7 :   assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
    1186             : 
    1187       81770 :   // If no operands changed return self.
    1188         543 :   if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin()))
    1189             :     return const_cast<ConstantExpr*>(this);
    1190             : 
    1191             :   Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;
    1192       70155 :   switch (getOpcode()) {
    1193       16080 :   case Instruction::Trunc:
    1194             :   case Instruction::ZExt:
    1195             :   case Instruction::SExt:
    1196             :   case Instruction::FPTrunc:
    1197             :   case Instruction::FPExt:
    1198             :   case Instruction::UIToFP:
    1199       34888 :   case Instruction::SIToFP:
    1200             :   case Instruction::FPToUI:
    1201             :   case Instruction::FPToSI:
    1202       34888 :   case Instruction::PtrToInt:
    1203       34888 :   case Instruction::IntToPtr:
    1204      114078 :   case Instruction::BitCast:
    1205      237570 :   case Instruction::AddrSpaceCast:
    1206             :     return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced);
    1207       69776 :   case Instruction::Select:
    1208             :     return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2], OnlyIfReducedTy);
    1209             :   case Instruction::InsertElement:
    1210             :     return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2],
    1211       23030 :                                           OnlyIfReducedTy);
    1212             :   case Instruction::ExtractElement:
    1213             :     return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);
    1214             :   case Instruction::InsertValue:
    1215       23030 :     return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(),
    1216             :                                         OnlyIfReducedTy);
    1217             :   case Instruction::ExtractValue:
    1218      103841 :     return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy);
    1219      103841 :   case Instruction::ShuffleVector:
    1220             :     return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2],
    1221             :                                           OnlyIfReducedTy);
    1222      103841 :   case Instruction::GetElementPtr: {
    1223             :     auto *GEPO = cast<GEPOperator>(this);
    1224             :     assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
    1225      111748 :     return ConstantExpr::getGetElementPtr(
    1226             :         SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),
    1227             :         GEPO->isInBounds(), GEPO->getInRangeIndex(), OnlyIfReducedTy);
    1228             :   }
    1229             :   case Instruction::ICmp:
    1230             :   case Instruction::FCmp:
    1231             :     return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1],
    1232             :                                     OnlyIfReducedTy);
    1233      111748 :   default:
    1234      111713 :     assert(getNumOperands() == 2 && "Must be binary operator?");
    1235      111713 :     return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData,
    1236      111713 :                              OnlyIfReducedTy);
    1237       52797 :   }
    1238       82922 : }
    1239             : 
    1240       82922 : 
    1241             : //===----------------------------------------------------------------------===//
    1242             : //                      isValueValidForType implementations
    1243             : 
    1244             : bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
    1245      111713 :   unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay
    1246        2385 :   if (Ty->isIntegerTy(1))
    1247      109363 :     return Val == 0 || Val == 1;
    1248         830 :   return isUIntN(NumBits, Val);
    1249             : }
    1250      217066 : 
    1251             : bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
    1252             :   unsigned NumBits = Ty->getIntegerBitWidth();
    1253       15196 :   if (Ty->isIntegerTy(1))
    1254       15196 :     return Val == 0 || Val == 1 || Val == -1;
    1255             :   return isIntN(NumBits, Val);
    1256             : }
    1257       15196 : 
    1258             : bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
    1259             :   // convert modifies in place, so make a copy.
    1260      331182 :   APFloat Val2 = APFloat(Val);
    1261      331182 :   bool losesInfo;
    1262             :   switch (Ty->getTypeID()) {
    1263      131673 :   default:
    1264      263346 :     return false;         // These can't be represented as floating point!
    1265             : 
    1266             :   // FIXME rounding mode needs to be more flexible
    1267      331182 :   case Type::HalfTyID: {
    1268             :     if (&Val2.getSemantics() == &APFloat::IEEEhalf())
    1269      331182 :       return true;
    1270             :     Val2.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &losesInfo);
    1271             :     return !losesInfo;
    1272             :   }
    1273      331182 :   case Type::FloatTyID: {
    1274      331182 :     if (&Val2.getSemantics() == &APFloat::IEEEsingle())
    1275             :       return true;
    1276             :     Val2.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
    1277      331182 :     return !losesInfo;
    1278      106162 :   }
    1279      199320 :   case Type::DoubleTyID: {
    1280             :     if (&Val2.getSemantics() == &APFloat::IEEEhalf() ||
    1281             :         &Val2.getSemantics() == &APFloat::IEEEsingle() ||
    1282             :         &Val2.getSemantics() == &APFloat::IEEEdouble())
    1283             :       return true;
    1284             :     Val2.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
    1285      331182 :     return !losesInfo;
    1286        6182 :   }
    1287      325000 :   case Type::X86_FP80TyID:
    1288         320 :     return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
    1289             :            &Val2.getSemantics() == &APFloat::IEEEsingle() ||
    1290             :            &Val2.getSemantics() == &APFloat::IEEEdouble() ||
    1291             :            &Val2.getSemantics() == &APFloat::x87DoubleExtended();
    1292      324680 :   case Type::FP128TyID:
    1293      320780 :     return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
    1294             :            &Val2.getSemantics() == &APFloat::IEEEsingle() ||
    1295             :            &Val2.getSemantics() == &APFloat::IEEEdouble() ||
    1296             :            &Val2.getSemantics() == &APFloat::IEEEquad();
    1297             :   case Type::PPC_FP128TyID:
    1298             :     return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
    1299             :            &Val2.getSemantics() == &APFloat::IEEEsingle() ||
    1300       13330 :            &Val2.getSemantics() == &APFloat::IEEEdouble() ||
    1301             :            &Val2.getSemantics() == &APFloat::PPCDoubleDouble();
    1302             :   }
    1303       39988 : }
    1304       13328 : 
    1305       12644 : 
    1306             : //===----------------------------------------------------------------------===//
    1307         686 : //                      Factory Function Implementation
    1308         686 : 
    1309             : ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
    1310             :   assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
    1311        2755 :          "Cannot create an aggregate zero of non-aggregate type!");
    1312        2755 : 
    1313        2755 :   std::unique_ptr<ConstantAggregateZero> &Entry =
    1314         304 :       Ty->getContext().pImpl->CAZConstants[Ty];
    1315        2755 :   if (!Entry)
    1316             :     Entry.reset(new ConstantAggregateZero(Ty));
    1317             : 
    1318             :   return Entry.get();
    1319           0 : }
    1320           0 : 
    1321             : /// Remove the constant from the constant table.
    1322             : void ConstantAggregateZero::destroyConstantImpl() {
    1323             :   getContext().pImpl->CAZConstants.erase(getType());
    1324             : }
    1325             : 
    1326     1901915 : /// Remove the constant from the constant table.
    1327     1901915 : void ConstantArray::destroyConstantImpl() {
    1328             :   getType()->getContext().pImpl->ArrayConstants.remove(this);
    1329             : }
    1330    48149349 : 
    1331    48149349 : 
    1332             : //---- ConstantStruct::get() implementation...
    1333             : //
    1334        1281 : 
    1335        1281 : /// Remove the constant from the constant table.
    1336             : void ConstantStruct::destroyConstantImpl() {
    1337        1122 :   getType()->getContext().pImpl->StructConstants.remove(this);
    1338             : }
    1339             : 
    1340             : /// Remove the constant from the constant table.
    1341             : void ConstantVector::destroyConstantImpl() {
    1342        4031 :   getType()->getContext().pImpl->VectorConstants.remove(this);
    1343        2909 : }
    1344             : 
    1345             : Constant *Constant::getSplatValue() const {
    1346        1043 :   assert(this->getType()->isVectorTy() && "Only valid for vectors!");
    1347        1043 :   if (isa<ConstantAggregateZero>(this))
    1348             :     return getNullValue(this->getType()->getVectorElementType());
    1349             :   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
    1350             :     return CV->getSplatValue();
    1351             :   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
    1352             :     return CV->getSplatValue();
    1353             :   return nullptr;
    1354             : }
    1355             : 
    1356    20585498 : Constant *ConstantVector::getSplatValue() const {
    1357    20585498 :   // Check out first element.
    1358    20585498 :   Constant *Elt = getOperand(0);
    1359             :   // Then make sure all remaining elements point to the same value.
    1360             :   for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
    1361           7 :     if (getOperand(I) != Elt)
    1362             :       return nullptr;
    1363             :   return Elt;
    1364           6 : }
    1365             : 
    1366           1 : const APInt &Constant::getUniqueInteger() const {
    1367             :   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
    1368             :     return CI->getValue();
    1369        6082 :   assert(this->getSplatValue() && "Doesn't contain a unique integer!");
    1370        6082 :   const Constant *C = this->getAggregateElement(0U);
    1371             :   assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");
    1372             :   return cast<ConstantInt>(C)->getValue();
    1373             : }
    1374           0 : 
    1375             : //---- ConstantPointerNull::get() implementation.
    1376             : //
    1377           0 : 
    1378           0 : ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
    1379             :   std::unique_ptr<ConstantPointerNull> &Entry =
    1380             :       Ty->getContext().pImpl->CPNConstants[Ty];
    1381           0 :   if (!Entry)
    1382           0 :     Entry.reset(new ConstantPointerNull(Ty));
    1383             : 
    1384           0 :   return Entry.get();
    1385             : }
    1386             : 
    1387        7619 : /// Remove the constant from the constant table.
    1388             : void ConstantPointerNull::destroyConstantImpl() {
    1389             :   getContext().pImpl->CPNConstants.erase(getType());
    1390             : }
    1391             : 
    1392       15221 : UndefValue *UndefValue::get(Type *Ty) {
    1393         300 :   std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
    1394             :   if (!Entry)
    1395        7319 :     Entry.reset(new UndefValue(Ty));
    1396        7319 : 
    1397        6874 :   return Entry.get();
    1398             : }
    1399             : 
    1400             : /// Remove the constant from the constant table.
    1401             : void UndefValue::destroyConstantImpl() {
    1402             :   // Free the constant and any dangling references to it.
    1403             :   getContext().pImpl->UVConstants.erase(getType());
    1404             : }
    1405             : 
    1406             : BlockAddress *BlockAddress::get(BasicBlock *BB) {
    1407             :   assert(BB->getParent() && "Block must have a parent");
    1408             :   return get(BB->getParent(), BB);
    1409             : }
    1410        6874 : 
    1411           2 : BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
    1412           2 :   BlockAddress *&BA =
    1413           0 :     F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
    1414           0 :   if (!BA)
    1415           0 :     BA = new BlockAddress(F, BB);
    1416          33 : 
    1417          33 :   assert(BA->getFunction() == F && "Basic block moved between functions");
    1418           0 :   return BA;
    1419           0 : }
    1420           0 : 
    1421           2 : BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
    1422           2 : : Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
    1423           0 :            &Op<0>(), 2) {
    1424           0 :   setOperand(0, F);
    1425           0 :   setOperand(1, BB);
    1426             :   BB->AdjustBlockAddressRefCount(1);
    1427             : }
    1428             : 
    1429         630 : BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
    1430         279 :   if (!BB->hasAddressTaken())
    1431             :     return nullptr;
    1432             : 
    1433          10 :   const Function *F = BB->getParent();
    1434             :   assert(F && "Block must have a parent");
    1435          10 :   BlockAddress *BA =
    1436          10 :       F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB));
    1437          83 :   assert(BA && "Refcount and block address map disagree!");
    1438             :   return BA;
    1439          83 : }
    1440          83 : 
    1441             : /// Remove the constant from the constant table.
    1442             : void BlockAddress::destroyConstantImpl() {
    1443             :   getFunction()->getType()->getContext().pImpl
    1444             :     ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
    1445             :   getBasicBlock()->AdjustBlockAddressRefCount(-1);
    1446             : }
    1447             : 
    1448          61 : Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {
    1449             :   // This could be replacing either the Basic Block or the Function.  In either
    1450          61 :   // case, we have to remove the map entry.
    1451           0 :   Function *NewF = getFunction();
    1452             :   BasicBlock *NewBB = getBasicBlock();
    1453             : 
    1454             :   if (From == NewF)
    1455       36132 :     NewF = cast<Function>(To->stripPointerCasts());
    1456             :   else {
    1457       36132 :     assert(From == NewBB && "From does not match any operand");
    1458           0 :     NewBB = cast<BasicBlock>(To);
    1459             :   }
    1460             : 
    1461             :   // See if the 'new' entry already exists, if not, just update this in place
    1462       58767 :   // and return early.
    1463             :   BlockAddress *&NewBA =
    1464             :     getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
    1465             :   if (NewBA)
    1466       58767 :     return NewBA;
    1467             : 
    1468             :   getBasicBlock()->AdjustBlockAddressRefCount(-1);
    1469             : 
    1470             :   // Remove the old entry, this can't cause the map to rehash (just a
    1471        3410 :   // tombstone will get added).
    1472        3410 :   getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
    1473             :                                                           getBasicBlock()));
    1474        2222 :   NewBA = this;
    1475        2222 :   setOperand(0, NewF);
    1476             :   setOperand(1, NewBB);
    1477       38773 :   getBasicBlock()->AdjustBlockAddressRefCount(1);
    1478       38773 : 
    1479             :   // If we just want to keep the existing value, then return null.
    1480       38773 :   // Callers know that this means we shouldn't delete this value.
    1481       38773 :   return nullptr;
    1482             : }
    1483       16155 : 
    1484       32310 : //---- ConstantExpr::get() implementations.
    1485       16155 : //
    1486       16155 : 
    1487             : /// This is a utility function to handle folding of casts and lookup of the
    1488           0 : /// cast in the ExprConstants map. It is used by the various get* methods below.
    1489           0 : static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
    1490             :                                bool OnlyIfReduced = false) {
    1491         147 :   assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
    1492         294 :   // Fold a few common cases
    1493         147 :   if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
    1494         294 :     return FC;
    1495         147 : 
    1496         214 :   if (OnlyIfReduced)
    1497         428 :     return nullptr;
    1498         214 : 
    1499         428 :   LLVMContextImpl *pImpl = Ty->getContext().pImpl;
    1500         214 : 
    1501          68 :   // Look up the constant in the table first to ensure uniqueness.
    1502         136 :   ConstantExprKeyType Key(opc, C);
    1503          68 : 
    1504         136 :   return pImpl->ExprConstants.getOrCreate(Ty, Key);
    1505          68 : }
    1506             : 
    1507             : Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
    1508             :                                 bool OnlyIfReduced) {
    1509             :   Instruction::CastOps opc = Instruction::CastOps(oc);
    1510             :   assert(Instruction::isCast(opc) && "opcode out of range");
    1511             :   assert(C && Ty && "Null arguments to getCast");
    1512             :   assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
    1513      128351 : 
    1514             :   switch (opc) {
    1515             :   default:
    1516             :     llvm_unreachable("Invalid cast opcode");
    1517             :   case Instruction::Trunc:
    1518      128351 :     return getTrunc(C, Ty, OnlyIfReduced);
    1519      128351 :   case Instruction::ZExt:
    1520       22241 :     return getZExt(C, Ty, OnlyIfReduced);
    1521             :   case Instruction::SExt:
    1522      128351 :     return getSExt(C, Ty, OnlyIfReduced);
    1523             :   case Instruction::FPTrunc:
    1524             :     return getFPTrunc(C, Ty, OnlyIfReduced);
    1525             :   case Instruction::FPExt:
    1526           0 :     return getFPExtend(C, Ty, OnlyIfReduced);
    1527           0 :   case Instruction::UIToFP:
    1528           0 :     return getUIToFP(C, Ty, OnlyIfReduced);
    1529             :   case Instruction::SIToFP:
    1530             :     return getSIToFP(C, Ty, OnlyIfReduced);
    1531       10385 :   case Instruction::FPToUI:
    1532       10385 :     return getFPToUI(C, Ty, OnlyIfReduced);
    1533       10385 :   case Instruction::FPToSI:
    1534             :     return getFPToSI(C, Ty, OnlyIfReduced);
    1535             :   case Instruction::PtrToInt:
    1536             :     return getPtrToInt(C, Ty, OnlyIfReduced);
    1537             :   case Instruction::IntToPtr:
    1538             :     return getIntToPtr(C, Ty, OnlyIfReduced);
    1539             :   case Instruction::BitCast:
    1540       32263 :     return getBitCast(C, Ty, OnlyIfReduced);
    1541       32263 :   case Instruction::AddrSpaceCast:
    1542       32263 :     return getAddrSpaceCast(C, Ty, OnlyIfReduced);
    1543             :   }
    1544             : }
    1545         113 : 
    1546         113 : Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) {
    1547         113 :   if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
    1548             :     return getBitCast(C, Ty);
    1549     1697977 :   return getZExt(C, Ty);
    1550             : }
    1551     1697977 : 
    1552       44440 : Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) {
    1553             :   if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
    1554     1658731 :     return getBitCast(C, Ty);
    1555             :   return getSExt(C, Ty);
    1556       10924 : }
    1557             : 
    1558             : Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
    1559             :   if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
    1560       11463 :     return getBitCast(C, Ty);
    1561             :   return getTrunc(C, Ty);
    1562       11463 : }
    1563             : 
    1564       26731 : Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
    1565       22166 :   assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
    1566             :   assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
    1567             :           "Invalid cast");
    1568             : 
    1569             :   if (Ty->isIntOrIntVectorTy())
    1570    22481408 :     return getPtrToInt(S, Ty);
    1571             : 
    1572    22481047 :   unsigned SrcAS = S->getType()->getPointerAddressSpace();
    1573             :   if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
    1574         361 :     return getAddrSpaceCast(S, Ty);
    1575             : 
    1576         361 :   return getBitCast(S, Ty);
    1577             : }
    1578             : 
    1579             : Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
    1580             :                                                          Type *Ty) {
    1581             :   assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
    1582      732168 :   assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
    1583             : 
    1584      732168 :   if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
    1585      732168 :     return getAddrSpaceCast(S, Ty);
    1586       35338 : 
    1587             :   return getBitCast(S, Ty);
    1588      732168 : }
    1589             : 
    1590             : Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty, bool isSigned) {
    1591             :   assert(C->getType()->isIntOrIntVectorTy() &&
    1592           0 :          Ty->isIntOrIntVectorTy() && "Invalid cast");
    1593           0 :   unsigned SrcBits = C->getType()->getScalarSizeInBits();
    1594           0 :   unsigned DstBits = Ty->getScalarSizeInBits();
    1595             :   Instruction::CastOps opcode =
    1596     3701178 :     (SrcBits == DstBits ? Instruction::BitCast :
    1597     3701178 :      (SrcBits > DstBits ? Instruction::Trunc :
    1598     3701178 :       (isSigned ? Instruction::SExt : Instruction::ZExt)));
    1599       78568 :   return getCast(opcode, C, Ty);
    1600             : }
    1601     3701178 : 
    1602             : Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) {
    1603             :   assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
    1604             :          "Invalid cast");
    1605           0 :   unsigned SrcBits = C->getType()->getScalarSizeInBits();
    1606             :   unsigned DstBits = Ty->getScalarSizeInBits();
    1607           0 :   if (SrcBits == DstBits)
    1608           0 :     return C; // Avoid a useless cast
    1609             :   Instruction::CastOps opcode =
    1610         269 :     (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
    1611             :   return getCast(opcode, C, Ty);
    1612         269 : }
    1613             : 
    1614             : Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
    1615        1448 : #ifndef NDEBUG
    1616             :   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
    1617        1448 :   bool toVec = Ty->getTypeID() == Type::VectorTyID;
    1618        1448 : #endif
    1619         991 :   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
    1620             :   assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
    1621             :   assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
    1622        1448 :   assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
    1623             :          "SrcTy must be larger than DestTy for Trunc!");
    1624             : 
    1625         991 :   return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced);
    1626         991 : }
    1627             : 
    1628             : Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
    1629             : #ifndef NDEBUG
    1630             :   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
    1631         991 :   bool toVec = Ty->getTypeID() == Type::VectorTyID;
    1632             : #endif
    1633          62 :   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
    1634          62 :   assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
    1635             :   assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
    1636             :   assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
    1637           2 :          "SrcTy must be smaller than DestTy for SExt!");
    1638             : 
    1639             :   return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced);
    1640           2 : }
    1641             : 
    1642           2 : Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
    1643             : #ifndef NDEBUG
    1644             :   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
    1645             :   bool toVec = Ty->getTypeID() == Type::VectorTyID;
    1646         907 : #endif
    1647         907 :   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
    1648        1814 :   assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
    1649             :   assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
    1650         907 :   assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
    1651             :          "SrcTy must be smaller than DestTy for ZExt!");
    1652          20 : 
    1653             :   return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced);
    1654             : }
    1655             : 
    1656             : Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
    1657             : #ifndef NDEBUG
    1658          20 :   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
    1659             :   bool toVec = Ty->getTypeID() == Type::VectorTyID;
    1660             : #endif
    1661             :   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
    1662             :   assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
    1663             :          C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
    1664             :          "This is an illegal floating point truncation!");
    1665             :   return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced);
    1666             : }
    1667             : 
    1668          20 : Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced) {
    1669          20 : #ifndef NDEBUG
    1670             :   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
    1671             :   bool toVec = Ty->getTypeID() == Type::VectorTyID;
    1672             : #endif
    1673             :   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
    1674             :   assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
    1675             :          C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
    1676           9 :          "This is an illegal floating point extension!");
    1677             :   return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced);
    1678           9 : }
    1679             : 
    1680             : Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
    1681             : #ifndef NDEBUG
    1682             :   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
    1683             :   bool toVec = Ty->getTypeID() == Type::VectorTyID;
    1684             : #endif
    1685           9 :   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
    1686             :   assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
    1687             :          "This is an illegal uint to floating point cast!");
    1688             :   return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced);
    1689             : }
    1690             : 
    1691             : Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
    1692             : #ifndef NDEBUG
    1693     4750386 :   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
    1694             :   bool toVec = Ty->getTypeID() == Type::VectorTyID;
    1695             : #endif
    1696             :   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
    1697     4750386 :   assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
    1698             :          "This is an illegal sint to floating point cast!");
    1699             :   return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced);
    1700     2274803 : }
    1701             : 
    1702             : Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced) {
    1703     2269493 : #ifndef NDEBUG
    1704             :   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
    1705             :   bool toVec = Ty->getTypeID() == Type::VectorTyID;
    1706             : #endif
    1707             :   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
    1708     2269493 :   assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
    1709             :          "This is an illegal floating point to uint cast!");
    1710             :   return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced);
    1711     2107139 : }
    1712             : 
    1713             : Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) {
    1714             : #ifndef NDEBUG
    1715             :   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
    1716             :   bool toVec = Ty->getTypeID() == Type::VectorTyID;
    1717             : #endif
    1718     2107139 :   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
    1719           0 :   assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
    1720           0 :          "This is an illegal floating point to sint cast!");
    1721      494631 :   return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced);
    1722      494631 : }
    1723       68467 : 
    1724       68467 : Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
    1725      549452 :                                     bool OnlyIfReduced) {
    1726      549452 :   assert(C->getType()->isPtrOrPtrVectorTy() &&
    1727        1013 :          "PtrToInt source must be pointer or pointer vector");
    1728        1013 :   assert(DstTy->isIntOrIntVectorTy() &&
    1729         869 :          "PtrToInt destination must be integer or integer vector");
    1730         869 :   assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
    1731          76 :   if (isa<VectorType>(C->getType()))
    1732          76 :     assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
    1733       10094 :            "Invalid cast between a different number of vector elements");
    1734       10094 :   return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);
    1735          12 : }
    1736          12 : 
    1737          59 : Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
    1738          59 :                                     bool OnlyIfReduced) {
    1739       13443 :   assert(C->getType()->isIntOrIntVectorTy() &&
    1740       13443 :          "IntToPtr source must be integer or integer vector");
    1741       10070 :   assert(DstTy->isPtrOrPtrVectorTy() &&
    1742       10070 :          "IntToPtr destination must be a pointer or pointer vector");
    1743      957859 :   assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
    1744      957859 :   if (isa<VectorType>(C->getType()))
    1745        1094 :     assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
    1746        1094 :            "Invalid cast between a different number of vector elements");
    1747             :   return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced);
    1748             : }
    1749             : 
    1750         143 : Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
    1751         143 :                                    bool OnlyIfReduced) {
    1752           0 :   assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
    1753         143 :          "Invalid constantexpr bitcast!");
    1754             : 
    1755             :   // It is common to ask for a bitcast of a value to its own type, handle this
    1756       14074 :   // speedily.
    1757       14074 :   if (C->getType() == DstTy) return C;
    1758       13766 : 
    1759         308 :   return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced);
    1760             : }
    1761             : 
    1762          71 : Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
    1763          71 :                                          bool OnlyIfReduced) {
    1764           4 :   assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
    1765          67 :          "Invalid constantexpr addrspacecast!");
    1766             : 
    1767             :   // Canonicalize addrspacecasts between different pointer types by first
    1768      277329 :   // bitcasting the pointer type and then converting the address space.
    1769             :   PointerType *SrcScalarTy = cast<PointerType>(C->getType()->getScalarType());
    1770             :   PointerType *DstScalarTy = cast<PointerType>(DstTy->getScalarType());
    1771             :   Type *DstElemTy = DstScalarTy->getElementType();
    1772             :   if (SrcScalarTy->getElementType() != DstElemTy) {
    1773      277329 :     Type *MidTy = PointerType::get(DstElemTy, SrcScalarTy->getAddressSpace());
    1774        4063 :     if (VectorType *VT = dyn_cast<VectorType>(DstTy)) {
    1775             :       // Handle vectors of pointers.
    1776      273266 :       MidTy = VectorType::get(MidTy, VT->getNumElements());
    1777      273266 :     }
    1778         535 :     C = getBitCast(C, MidTy);
    1779             :   }
    1780      272731 :   return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);
    1781             : }
    1782             : 
    1783       14399 : Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
    1784             :                             unsigned Flags, Type *OnlyIfReducedTy) {
    1785             :   // Check the operands for consistency first.
    1786             :   assert(Instruction::isBinaryOp(Opcode) &&
    1787             :          "Invalid opcode in binary constant expression");
    1788       28798 :   assert(C1->getType() == C2->getType() &&
    1789          38 :          "Operand types in binary constant expression should match");
    1790             : 
    1791       14361 : #ifndef NDEBUG
    1792             :   switch (Opcode) {
    1793             :   case Instruction::Add:
    1794      581998 :   case Instruction::Sub:
    1795             :   case Instruction::Mul:
    1796             :     assert(C1->getType() == C2->getType() && "Op types should be identical!");
    1797      581998 :     assert(C1->getType()->isIntOrIntVectorTy() &&
    1798      581998 :            "Tried to create an integer operation on a non-integer type!");
    1799             :     break;
    1800      581998 :   case Instruction::FAdd:
    1801      570371 :   case Instruction::FSub:
    1802      545734 :   case Instruction::FMul:
    1803      581998 :     assert(C1->getType() == C2->getType() && "Op types should be identical!");
    1804             :     assert(C1->getType()->isFPOrFPVectorTy() &&
    1805             :            "Tried to create a floating-point operation on a "
    1806           0 :            "non-floating-point type!");
    1807             :     break;
    1808             :   case Instruction::UDiv:
    1809           0 :   case Instruction::SDiv:
    1810           0 :     assert(C1->getType() == C2->getType() && "Op types should be identical!");
    1811           0 :     assert(C1->getType()->isIntOrIntVectorTy() &&
    1812             :            "Tried to create an arithmetic operation on a non-arithmetic type!");
    1813             :     break;
    1814           0 :   case Instruction::FDiv:
    1815           0 :     assert(C1->getType() == C2->getType() && "Op types should be identical!");
    1816             :     assert(C1->getType()->isFPOrFPVectorTy() &&
    1817             :            "Tried to create an arithmetic operation on a non-arithmetic type!");
    1818      535767 :     break;
    1819             :   case Instruction::URem:
    1820             :   case Instruction::SRem:
    1821             :     assert(C1->getType() == C2->getType() && "Op types should be identical!");
    1822             :     assert(C1->getType()->isIntOrIntVectorTy() &&
    1823             :            "Tried to create an arithmetic operation on a non-arithmetic type!");
    1824             :     break;
    1825             :   case Instruction::FRem:
    1826             :     assert(C1->getType() == C2->getType() && "Op types should be identical!");
    1827             :     assert(C1->getType()->isFPOrFPVectorTy() &&
    1828             :            "Tried to create an arithmetic operation on a non-arithmetic type!");
    1829      535767 :     break;
    1830             :   case Instruction::And:
    1831             :   case Instruction::Or:
    1832      656401 :   case Instruction::Xor:
    1833             :     assert(C1->getType() == C2->getType() && "Op types should be identical!");
    1834             :     assert(C1->getType()->isIntOrIntVectorTy() &&
    1835             :            "Tried to create a logical operation on a non-integral type!");
    1836             :     break;
    1837             :   case Instruction::Shl:
    1838             :   case Instruction::LShr:
    1839             :   case Instruction::AShr:
    1840             :     assert(C1->getType() == C2->getType() && "Op types should be identical!");
    1841             :     assert(C1->getType()->isIntOrIntVectorTy() &&
    1842             :            "Tried to create a shift operation on a non-integer type!");
    1843      656401 :     break;
    1844             :   default:
    1845             :     break;
    1846      262639 :   }
    1847             : #endif
    1848             : 
    1849             :   if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
    1850             :     return FC;          // Fold a few common cases.
    1851             : 
    1852             :   if (OnlyIfReducedTy == C1->getType())
    1853             :     return nullptr;
    1854             : 
    1855             :   Constant *ArgVec[] = { C1, C2 };
    1856             :   ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
    1857      262639 : 
    1858             :   LLVMContextImpl *pImpl = C1->getContext().pImpl;
    1859             :   return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
    1860        3089 : }
    1861             : 
    1862             : Constant *ConstantExpr::getSizeOf(Type* Ty) {
    1863             :   // sizeof is implemented as: (i64) gep (Ty*)null, 1
    1864             :   // Note that a non-inbounds gep is used, as null isn't within any object.
    1865             :   Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
    1866             :   Constant *GEP = getGetElementPtr(
    1867             :       Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
    1868             :   return getPtrToInt(GEP,
    1869        3089 :                      Type::getInt64Ty(Ty->getContext()));
    1870             : }
    1871             : 
    1872         996 : Constant *ConstantExpr::getAlignOf(Type* Ty) {
    1873             :   // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
    1874             :   // Note that a non-inbounds gep is used, as null isn't within any object.
    1875             :   Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);
    1876             :   Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
    1877             :   Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
    1878             :   Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
    1879             :   Constant *Indices[2] = { Zero, One };
    1880             :   Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);
    1881         996 :   return getPtrToInt(GEP,
    1882             :                      Type::getInt64Ty(Ty->getContext()));
    1883             : }
    1884          85 : 
    1885             : Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) {
    1886             :   return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
    1887             :                                            FieldNo));
    1888             : }
    1889             : 
    1890             : Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) {
    1891             :   // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
    1892          85 :   // Note that a non-inbounds gep is used, as null isn't within any object.
    1893             :   Constant *GEPIdx[] = {
    1894             :     ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
    1895       10106 :     FieldNo
    1896             :   };
    1897             :   Constant *GEP = getGetElementPtr(
    1898             :       Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
    1899             :   return getPtrToInt(GEP,
    1900             :                      Type::getInt64Ty(Ty->getContext()));
    1901             : }
    1902             : 
    1903       10106 : Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1,
    1904             :                                    Constant *C2, bool OnlyIfReduced) {
    1905             :   assert(C1->getType() == C2->getType() && "Op types should be identical!");
    1906          34 : 
    1907             :   switch (Predicate) {
    1908             :   default: llvm_unreachable("Invalid CmpInst predicate");
    1909             :   case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
    1910             :   case CmpInst::FCMP_OGE:   case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
    1911             :   case CmpInst::FCMP_ONE:   case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
    1912             :   case CmpInst::FCMP_UEQ:   case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
    1913             :   case CmpInst::FCMP_ULT:   case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
    1914          34 :   case CmpInst::FCMP_TRUE:
    1915             :     return getFCmp(Predicate, C1, C2, OnlyIfReduced);
    1916             : 
    1917          86 :   case CmpInst::ICMP_EQ:  case CmpInst::ICMP_NE:  case CmpInst::ICMP_UGT:
    1918             :   case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
    1919             :   case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
    1920             :   case CmpInst::ICMP_SLE:
    1921             :     return getICmp(Predicate, C1, C2, OnlyIfReduced);
    1922             :   }
    1923             : }
    1924             : 
    1925          86 : Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2,
    1926             :                                   Type *OnlyIfReducedTy) {
    1927             :   assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
    1928       22202 : 
    1929             :   if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
    1930             :     return SC;        // Fold common cases
    1931             : 
    1932             :   if (OnlyIfReducedTy == V1->getType())
    1933             :     return nullptr;
    1934             : 
    1935             :   Constant *ArgVec[] = { C, V1, V2 };
    1936             :   ConstantExprKeyType Key(Instruction::Select, ArgVec);
    1937             : 
    1938       22202 :   LLVMContextImpl *pImpl = C->getContext().pImpl;
    1939             :   return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
    1940             : }
    1941       39690 : 
    1942             : Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
    1943             :                                          ArrayRef<Value *> Idxs, bool InBounds,
    1944             :                                          Optional<unsigned> InRangeIndex,
    1945             :                                          Type *OnlyIfReducedTy) {
    1946             :   if (!Ty)
    1947             :     Ty = cast<PointerType>(C->getType()->getScalarType())->getElementType();
    1948             :   else
    1949             :     assert(
    1950             :         Ty ==
    1951       39690 :         cast<PointerType>(C->getType()->getScalarType())->getContainedType(0u));
    1952             : 
    1953             :   if (Constant *FC =
    1954     3280051 :           ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs))
    1955             :     return FC;          // Fold a few common cases.
    1956             : 
    1957             :   // Get the result type of the getelementptr!
    1958             :   Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs);
    1959             :   assert(DestTy && "GEP indices invalid!");
    1960             :   unsigned AS = C->getType()->getPointerAddressSpace();
    1961     3280051 :   Type *ReqTy = DestTy->getPointerTo(AS);
    1962             : 
    1963     3217068 :   unsigned NumVecElts = 0;
    1964             :   if (C->getType()->isVectorTy())
    1965             :     NumVecElts = C->getType()->getVectorNumElements();
    1966        2223 :   else for (auto Idx : Idxs)
    1967             :     if (Idx->getType()->isVectorTy())
    1968             :       NumVecElts = Idx->getType()->getVectorNumElements();
    1969             : 
    1970             :   if (NumVecElts)
    1971             :     ReqTy = VectorType::get(ReqTy, NumVecElts);
    1972             : 
    1973        2223 :   if (OnlyIfReducedTy == ReqTy)
    1974             :     return nullptr;
    1975        2223 : 
    1976        2223 :   // Look up the constant in the table first to ensure uniqueness
    1977         367 :   std::vector<Constant*> ArgVec;
    1978             :   ArgVec.reserve(1 + Idxs.size());
    1979             :   ArgVec.push_back(C);
    1980           0 :   for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
    1981             :     assert((!Idxs[i]->getType()->isVectorTy() ||
    1982         367 :             Idxs[i]->getType()->getVectorNumElements() == NumVecElts) &&
    1983             :            "getelementptr index type missmatch");
    1984        2223 : 
    1985             :     Constant *Idx = cast<Constant>(Idxs[i]);
    1986             :     if (NumVecElts && !Idxs[i]->getType()->isVectorTy())
    1987     1024098 :       Idx = ConstantVector::getSplat(NumVecElts, Idx);
    1988             :     ArgVec.push_back(Idx);
    1989             :   }
    1990             : 
    1991             :   unsigned SubClassOptionalData = InBounds ? GEPOperator::IsInBounds : 0;
    1992             :   if (InRangeIndex && *InRangeIndex < 63)
    1993             :     SubClassOptionalData |= (*InRangeIndex + 1) << 1;
    1994             :   const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
    1995             :                                 SubClassOptionalData, None, Ty);
    1996             : 
    1997             :   LLVMContextImpl *pImpl = C->getContext().pImpl;
    1998             :   return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
    1999             : }
    2000             : 
    2001             : Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS,
    2002             :                                 Constant *RHS, bool OnlyIfReduced) {
    2003             :   assert(LHS->getType() == RHS->getType());
    2004             :   assert(CmpInst::isIntPredicate((CmpInst::Predicate)pred) &&
    2005             :          "Invalid ICmp Predicate");
    2006             : 
    2007             :   if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
    2008             :     return FC;          // Fold a few common cases...
    2009             : 
    2010             :   if (OnlyIfReduced)
    2011             :     return nullptr;
    2012             : 
    2013             :   // Look up the constant in the table first to ensure uniqueness
    2014             :   Constant *ArgVec[] = { LHS, RHS };
    2015             :   // Get the key type with both the opcode and predicate
    2016             :   const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
    2017             : 
    2018             :   Type *ResultTy = Type::getInt1Ty(LHS->getContext());
    2019             :   if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
    2020             :     ResultTy = VectorType::get(ResultTy, VT->getNumElements());
    2021             : 
    2022             :   LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
    2023             :   return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
    2024             : }
    2025             : 
    2026             : Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS,
    2027             :                                 Constant *RHS, bool OnlyIfReduced) {
    2028             :   assert(LHS->getType() == RHS->getType());
    2029             :   assert(CmpInst::isFPPredicate((CmpInst::Predicate)pred) &&
    2030             :          "Invalid FCmp Predicate");
    2031             : 
    2032             :   if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
    2033             :     return FC;          // Fold a few common cases...
    2034             : 
    2035             :   if (OnlyIfReduced)
    2036             :     return nullptr;
    2037             : 
    2038             :   // Look up the constant in the table first to ensure uniqueness
    2039             :   Constant *ArgVec[] = { LHS, RHS };
    2040             :   // Get the key type with both the opcode and predicate
    2041             :   const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
    2042             : 
    2043             :   Type *ResultTy = Type::getInt1Ty(LHS->getContext());
    2044             :   if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
    2045             :     ResultTy = VectorType::get(ResultTy, VT->getNumElements());
    2046             : 
    2047             :   LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
    2048             :   return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
    2049             : }
    2050             : 
    2051             : Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
    2052             :                                           Type *OnlyIfReducedTy) {
    2053     1024098 :   assert(Val->getType()->isVectorTy() &&
    2054             :          "Tried to create extractelement operation on non-vector type!");
    2055             :   assert(Idx->getType()->isIntegerTy() &&
    2056       12160 :          "Extractelement index must be an integer type!");
    2057             : 
    2058             :   if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
    2059       12118 :     return FC;          // Fold a few common cases.
    2060             : 
    2061             :   Type *ReqTy = Val->getType()->getVectorElementType();
    2062       12118 :   if (OnlyIfReducedTy == ReqTy)
    2063       12118 :     return nullptr;
    2064             : 
    2065             :   // Look up the constant in the table first to ensure uniqueness
    2066         797 :   Constant *ArgVec[] = { Val, Idx };
    2067             :   const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
    2068             : 
    2069         797 :   LLVMContextImpl *pImpl = Val->getContext().pImpl;
    2070         797 :   return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
    2071             : }
    2072         797 : 
    2073         797 : Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
    2074             :                                          Constant *Idx, Type *OnlyIfReducedTy) {
    2075             :   assert(Val->getType()->isVectorTy() &&
    2076          38 :          "Tried to create insertelement operation on non-vector type!");
    2077             :   assert(Elt->getType() == Val->getType()->getVectorElementType() &&
    2078             :          "Insertelement types must match!");
    2079          38 :   assert(Idx->getType()->isIntegerTy() &&
    2080          38 :          "Insertelement index must be i32 type!");
    2081          38 : 
    2082          38 :   if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
    2083          38 :     return FC;          // Fold a few common cases.
    2084             : 
    2085          38 :   if (OnlyIfReducedTy == Val->getType())
    2086          38 :     return nullptr;
    2087             : 
    2088             :   // Look up the constant in the table first to ensure uniqueness
    2089           0 :   Constant *ArgVec[] = { Val, Elt, Idx };
    2090           0 :   const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
    2091           0 : 
    2092             :   LLVMContextImpl *pImpl = Val->getContext().pImpl;
    2093             :   return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
    2094           0 : }
    2095             : 
    2096             : Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
    2097             :                                          Constant *Mask, Type *OnlyIfReducedTy) {
    2098           0 :   assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
    2099             :          "Invalid shuffle vector constant expr operands!");
    2100           0 : 
    2101           0 :   if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
    2102             :     return FC;          // Fold a few common cases.
    2103           0 : 
    2104           0 :   unsigned NElts = Mask->getType()->getVectorNumElements();
    2105             :   Type *EltTy = V1->getType()->getVectorElementType();
    2106             :   Type *ShufTy = VectorType::get(EltTy, NElts);
    2107       92640 : 
    2108             :   if (OnlyIfReducedTy == ShufTy)
    2109             :     return nullptr;
    2110             : 
    2111       92640 :   // Look up the constant in the table first to ensure uniqueness
    2112           0 :   Constant *ArgVec[] = { V1, V2, Mask };
    2113         488 :   const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec);
    2114             : 
    2115             :   LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
    2116             :   return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
    2117             : }
    2118             : 
    2119         488 : Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
    2120             :                                        ArrayRef<unsigned> Idxs,
    2121       92152 :                                        Type *OnlyIfReducedTy) {
    2122             :   assert(Agg->getType()->isFirstClassType() &&
    2123             :          "Non-first-class type for constant insertvalue expression");
    2124             : 
    2125       92152 :   assert(ExtractValueInst::getIndexedType(Agg->getType(),
    2126             :                                           Idxs) == Val->getType() &&
    2127             :          "insertvalue indices invalid!");
    2128             :   Type *ReqTy = Val->getType();
    2129        1214 : 
    2130             :   if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
    2131             :     return FC;
    2132             : 
    2133        1214 :   if (OnlyIfReducedTy == ReqTy)
    2134             :     return nullptr;
    2135             : 
    2136         107 :   Constant *ArgVec[] = { Agg, Val };
    2137             :   const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
    2138             : 
    2139         107 :   LLVMContextImpl *pImpl = Agg->getContext().pImpl;
    2140             :   return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
    2141             : }
    2142         107 : 
    2143         107 : Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
    2144             :                                         Type *OnlyIfReducedTy) {
    2145             :   assert(Agg->getType()->isFirstClassType() &&
    2146    18118848 :          "Tried to create extractelement operation on non-first-class type!");
    2147             : 
    2148             :   Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
    2149             :   (void)ReqTy;
    2150    18118848 :   assert(ReqTy && "extractvalue indices invalid!");
    2151      181350 : 
    2152             :   assert(Agg->getType()->isFirstClassType() &&
    2153             :          "Non-first-class type for constant extractvalue expression");
    2154             :   if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs))
    2155             :     return FC;
    2156             : 
    2157    18118848 :   if (OnlyIfReducedTy == ReqTy)
    2158    18118848 :     return nullptr;
    2159             : 
    2160             :   Constant *ArgVec[] = { Agg };
    2161             :   const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
    2162    17853107 : 
    2163             :   LLVMContextImpl *pImpl = Agg->getContext().pImpl;
    2164    17853107 :   return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
    2165    17853107 : }
    2166             : 
    2167             : Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
    2168    35706214 :   assert(C->getType()->isIntOrIntVectorTy() &&
    2169             :          "Cannot NEG a nonintegral value!");
    2170    53624090 :   return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
    2171    71542004 :                 C, HasNUW, HasNSW);
    2172             : }
    2173             : 
    2174    17853107 : Constant *ConstantExpr::getFNeg(Constant *C) {
    2175          51 :   assert(C->getType()->isFPOrFPVectorTy() &&
    2176             :          "Cannot FNEG a non-floating-point value!");
    2177    17853107 :   return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
    2178             : }
    2179             : 
    2180             : Constant *ConstantExpr::getNot(Constant *C) {
    2181             :   assert(C->getType()->isIntOrIntVectorTy() &&
    2182    17852986 :          "Cannot NOT a nonintegral value!");
    2183    17852986 :   return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
    2184    53623798 : }
    2185             : 
    2186             : Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
    2187             :                                bool HasNUW, bool HasNSW) {
    2188             :   unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
    2189    35770812 :                    (HasNSW ? OverflowingBinaryOperator::NoSignedWrap   : 0);
    2190    35770812 :   return get(Instruction::Add, C1, C2, Flags);
    2191          11 : }
    2192    35770812 : 
    2193             : Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
    2194             :   return get(Instruction::FAdd, C1, C2);
    2195    17852986 : }
    2196    17852986 : 
    2197       56118 : Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
    2198             :                                bool HasNUW, bool HasNSW) {
    2199             :   unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
    2200             :                    (HasNSW ? OverflowingBinaryOperator::NoSignedWrap   : 0);
    2201    17852986 :   return get(Instruction::Sub, C1, C2, Flags);
    2202    17852986 : }
    2203             : 
    2204             : Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
    2205      105626 :   return get(Instruction::FSub, C1, C2);
    2206             : }
    2207             : 
    2208             : Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
    2209             :                                bool HasNUW, bool HasNSW) {
    2210             :   unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
    2211      105626 :                    (HasNSW ? OverflowingBinaryOperator::NoSignedWrap   : 0);
    2212             :   return get(Instruction::Mul, C1, C2, Flags);
    2213             : }
    2214        2859 : 
    2215             : Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
    2216             :   return get(Instruction::FMul, C1, C2);
    2217             : }
    2218        2851 : 
    2219             : Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
    2220             :   return get(Instruction::UDiv, C1, C2,
    2221             :              isExact ? PossiblyExactOperator::IsExact : 0);
    2222        2851 : }
    2223        2851 : 
    2224           0 : Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
    2225             :   return get(Instruction::SDiv, C1, C2,
    2226        2851 :              isExact ? PossiblyExactOperator::IsExact : 0);
    2227        2851 : }
    2228             : 
    2229             : Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
    2230         495 :   return get(Instruction::FDiv, C1, C2);
    2231             : }
    2232             : 
    2233             : Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
    2234             :   return get(Instruction::URem, C1, C2);
    2235             : }
    2236         495 : 
    2237             : Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
    2238             :   return get(Instruction::SRem, C1, C2);
    2239           4 : }
    2240             : 
    2241             : Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
    2242             :   return get(Instruction::FRem, C1, C2);
    2243           4 : }
    2244             : 
    2245             : Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
    2246             :   return get(Instruction::And, C1, C2);
    2247           4 : }
    2248           4 : 
    2249           0 : Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
    2250             :   return get(Instruction::Or, C1, C2);
    2251           4 : }
    2252           4 : 
    2253             : Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
    2254             :   return get(Instruction::Xor, C1, C2);
    2255      363373 : }
    2256             : 
    2257             : Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
    2258             :                                bool HasNUW, bool HasNSW) {
    2259             :   unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
    2260             :                    (HasNSW ? OverflowingBinaryOperator::NoSignedWrap   : 0);
    2261             :   return get(Instruction::Shl, C1, C2, Flags);
    2262      363373 : }
    2263             : 
    2264             : Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
    2265         101 :   return get(Instruction::LShr, C1, C2,
    2266         101 :              isExact ? PossiblyExactOperator::IsExact : 0);
    2267             : }
    2268             : 
    2269             : Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
    2270         101 :   return get(Instruction::AShr, C1, C2,
    2271             :              isExact ? PossiblyExactOperator::IsExact : 0);
    2272             : }
    2273         101 : 
    2274         101 : Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty,
    2275             :                                          bool AllowRHSConstant) {
    2276             :   assert(Instruction::isBinaryOp(Opcode) && "Only binops allowed");
    2277      200949 : 
    2278             :   // Commutative opcodes: it does not matter if AllowRHSConstant is set.
    2279             :   if (Instruction::isCommutative(Opcode)) {
    2280             :     switch (Opcode) {
    2281             :       case Instruction::Add: // X + 0 = X
    2282             :       case Instruction::Or:  // X | 0 = X
    2283             :       case Instruction::Xor: // X ^ 0 = X
    2284             :         return Constant::getNullValue(Ty);
    2285             :       case Instruction::Mul: // X * 1 = X
    2286      200949 :         return ConstantInt::get(Ty, 1);
    2287             :       case Instruction::And: // X & -1 = X
    2288             :         return Constant::getAllOnesValue(Ty);
    2289           0 :       case Instruction::FAdd: // X + -0.0 = X
    2290             :         // TODO: If the fadd has 'nsz', should we return +0.0?
    2291             :         return ConstantFP::getNegativeZero(Ty);
    2292             :       case Instruction::FMul: // X * 1.0 = X
    2293           0 :         return ConstantFP::get(Ty, 1.0);
    2294             :       default:
    2295             :         llvm_unreachable("Every commutative binop has an identity constant");
    2296           0 :     }
    2297           0 :   }
    2298             : 
    2299             :   // Non-commutative opcodes: AllowRHSConstant must be set.
    2300        3417 :   if (!AllowRHSConstant)
    2301             :     return nullptr;
    2302             : 
    2303             :   switch (Opcode) {
    2304             :     case Instruction::Sub:  // X - 0 = X
    2305        3417 :     case Instruction::Shl:  // X << 0 = X
    2306             :     case Instruction::LShr: // X >>u 0 = X
    2307             :     case Instruction::AShr: // X >> 0 = X
    2308           0 :     case Instruction::FSub: // X - 0.0 = X
    2309           0 :       return Constant::getNullValue(Ty);
    2310           0 :     case Instruction::SDiv: // X / 1 = X
    2311             :     case Instruction::UDiv: // X /u 1 = X
    2312           0 :       return ConstantInt::get(Ty, 1);
    2313             :     case Instruction::FDiv: // X / 1.0 = X
    2314             :       return ConstantFP::get(Ty, 1.0);
    2315             :     default:
    2316           0 :       return nullptr;
    2317             :   }
    2318             : }
    2319           0 : 
    2320           0 : Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
    2321             :   switch (Opcode) {
    2322             :   default:
    2323          95 :     // Doesn't have an absorber.
    2324             :     return nullptr;
    2325             : 
    2326             :   case Instruction::Or:
    2327             :     return Constant::getAllOnesValue(Ty);
    2328             : 
    2329             :   case Instruction::And:
    2330             :   case Instruction::Mul:
    2331             :     return Constant::getNullValue(Ty);
    2332          95 :   }
    2333             : }
    2334          95 : 
    2335             : /// Remove the constant from the constant table.
    2336             : void ConstantExpr::destroyConstantImpl() {
    2337           1 :   getType()->getContext().pImpl->ExprConstants.remove(this);
    2338             : }
    2339             : 
    2340           1 : const char *ConstantExpr::getOpcodeName() const {
    2341             :   return Instruction::getOpcodeName(getOpcode());
    2342             : }
    2343           1 : 
    2344           1 : GetElementPtrConstantExpr::GetElementPtrConstantExpr(
    2345             :     Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy)
    2346             :     : ConstantExpr(DestTy, Instruction::GetElementPtr,
    2347      167624 :                    OperandTraits<GetElementPtrConstantExpr>::op_end(this) -
    2348             :                        (IdxList.size() + 1),
    2349             :                    IdxList.size() + 1),
    2350             :       SrcElementTy(SrcElementTy),
    2351             :       ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)) {
    2352      167624 :   Op<0>() = C;
    2353             :   Use *OperandList = getOperandList();
    2354             :   for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
    2355             :     OperandList[i+1] = IdxList[i];
    2356             : }
    2357             : 
    2358      167624 : Type *GetElementPtrConstantExpr::getSourceElementType() const {
    2359             :   return SrcElementTy;
    2360             : }
    2361           5 : 
    2362             : Type *GetElementPtrConstantExpr::getResultElementType() const {
    2363             :   return ResElementTy;
    2364           5 : }
    2365             : 
    2366             : //===----------------------------------------------------------------------===//
    2367           5 : //                       ConstantData* implementations
    2368           5 : 
    2369             : Type *ConstantDataSequential::getElementType() const {
    2370             :   return getType()->getElementType();
    2371      493019 : }
    2372             : 
    2373             : StringRef ConstantDataSequential::getRawDataValues() const {
    2374      493019 :   return StringRef(DataElements, getNumElements()*getElementByteSize());
    2375      493019 : }
    2376             : 
    2377             : bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
    2378          65 :   if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) return true;
    2379             :   if (auto *IT = dyn_cast<IntegerType>(Ty)) {
    2380             :     switch (IT->getBitWidth()) {
    2381          65 :     case 8:
    2382             :     case 16:
    2383             :     case 32:
    2384       97195 :     case 64:
    2385             :       return true;
    2386             :     default: break;
    2387       97195 :     }
    2388             :   }
    2389             :   return false;
    2390       46436 : }
    2391             : 
    2392       46436 : unsigned ConstantDataSequential::getNumElements() const {
    2393       46436 :   if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
    2394       46436 :     return AT->getNumElements();
    2395             :   return getType()->getVectorNumElements();
    2396             : }
    2397           1 : 
    2398           1 : 
    2399             : uint64_t ConstantDataSequential::getElementByteSize() const {
    2400             :   return getElementType()->getPrimitiveSizeInBits()/8;
    2401      583121 : }
    2402             : 
    2403      583121 : /// Return the start of the specified element.
    2404      583121 : const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
    2405      583121 :   assert(Elt < getNumElements() && "Invalid Elt");
    2406             :   return DataElements+Elt*getElementByteSize();
    2407             : }
    2408          70 : 
    2409          70 : 
    2410             : /// Return true if the array is empty or all zeros.
    2411             : static bool isAllZeros(StringRef Arr) {
    2412        9254 :   for (char I : Arr)
    2413             :     if (I != 0)
    2414        9254 :       return false;
    2415        9254 :   return true;
    2416        9254 : }
    2417             : 
    2418             : /// This is the underlying implementation of all of the
    2419          12 : /// ConstantDataSequential::get methods.  They all thunk down to here, providing
    2420          12 : /// the correct element type.  We take the bytes in as a StringRef because
    2421             : /// we *want* an underlying "char*" to avoid TBAA type punning violations.
    2422             : Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
    2423       22194 :   assert(isElementTypeCompatible(Ty->getSequentialElementType()));
    2424       44387 :   // If the elements are all zero or there are no elements, return a CAZ, which
    2425       22194 :   // is more dense and canonical.
    2426             :   if (isAllZeros(Elements))
    2427             :     return ConstantAggregateZero::get(Ty);
    2428       21212 : 
    2429       42396 :   // Do a lookup to see if we have already formed one of these.
    2430       21212 :   auto &Slot =
    2431             :       *Ty->getContext()
    2432             :            .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr))
    2433          42 :            .first;
    2434          42 : 
    2435             :   // The bucket can point to a linked list of different CDS's that have the same
    2436             :   // body but different types.  For example, 0,0,0,1 could be a 4 element array
    2437           2 :   // of i8, or a 1-element array of i32.  They'll both end up in the same
    2438           2 :   /// StringMap bucket, linked up by their Next pointers.  Walk the list.
    2439             :   ConstantDataSequential **Entry = &Slot.second;
    2440             :   for (ConstantDataSequential *Node = *Entry; Node;
    2441          70 :        Entry = &Node->Next, Node = *Entry)
    2442          70 :     if (Node->getType() == Ty)
    2443             :       return Node;
    2444             : 
    2445           1 :   // Okay, we didn't get a hit.  Create a node of the right class, link it in,
    2446           1 :   // and return it.
    2447             :   if (isa<ArrayType>(Ty))
    2448             :     return *Entry = new ConstantDataArray(Ty, Slot.first().data());
    2449        2175 : 
    2450        2175 :   assert(isa<VectorType>(Ty));
    2451             :   return *Entry = new ConstantDataVector(Ty, Slot.first().data());
    2452             : }
    2453        3293 : 
    2454        3293 : void ConstantDataSequential::destroyConstantImpl() {
    2455             :   // Remove the constant from the StringMap.
    2456             :   StringMap<ConstantDataSequential*> &CDSConstants =
    2457         133 :     getType()->getContext().pImpl->CDSConstants;
    2458         133 : 
    2459             :   StringMap<ConstantDataSequential*>::iterator Slot =
    2460             :     CDSConstants.find(getRawDataValues());
    2461        8393 : 
    2462             :   assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
    2463        8393 : 
    2464        8393 :   ConstantDataSequential **Entry = &Slot->getValue();
    2465        8393 : 
    2466             :   // Remove the entry from the hash table.
    2467             :   if (!(*Entry)->Next) {
    2468        1182 :     // If there is only one value in the bucket (common case) it must be this
    2469        2363 :     // entry, and removing the entry should remove the bucket completely.
    2470        1182 :     assert((*Entry) == this && "Hash mismatch in ConstantDataSequential");
    2471             :     getContext().pImpl->CDSConstants.erase(Slot);
    2472             :   } else {
    2473         607 :     // Otherwise, there are multiple entries linked off the bucket, unlink the
    2474         690 :     // node we care about but keep the bucket around.
    2475         607 :     for (ConstantDataSequential *Node = *Entry; ;
    2476             :          Entry = &Node->Next, Node = *Entry) {
    2477             :       assert(Node && "Didn't find entry in its uniquing hash table!");
    2478      667684 :       // If we found our entry, unlink it from the list and we're done.
    2479             :       if (Node == this) {
    2480             :         *Entry = Node->Next;
    2481             :         break;
    2482             :       }
    2483             :     }
    2484      643440 :   }
    2485      611540 : 
    2486             :   // If we were part of a list, make sure that we don't delete the list that is
    2487             :   // still owned by the uniquing map.
    2488      611540 :   Next = nullptr;
    2489       10086 : }
    2490       10086 : 
    2491       21671 : /// getFP() constructors - Return a constant with array type with an element
    2492       21671 : /// count and element type of float with precision matching the number of
    2493          51 : /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
    2494             : /// double for 64bits) Note that this can return a ConstantAggregateZero
    2495          51 : /// object.
    2496          92 : Constant *ConstantDataArray::getFP(LLVMContext &Context,
    2497          92 :                                    ArrayRef<uint16_t> Elts) {
    2498           0 :   Type *Ty = ArrayType::get(Type::getHalfTy(Context), Elts.size());
    2499           0 :   const char *Data = reinterpret_cast<const char *>(Elts.data());
    2500             :   return getImpl(StringRef(Data, Elts.size() * 2), Ty);
    2501             : }
    2502             : Constant *ConstantDataArray::getFP(LLVMContext &Context,
    2503             :                                    ArrayRef<uint32_t> Elts) {
    2504       24244 :   Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
    2505             :   const char *Data = reinterpret_cast<const char *>(Elts.data());
    2506             :   return getImpl(StringRef(Data, Elts.size() * 4), Ty);
    2507             : }
    2508          81 : Constant *ConstantDataArray::getFP(LLVMContext &Context,
    2509             :                                    ArrayRef<uint64_t> Elts) {
    2510             :   Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
    2511             :   const char *Data = reinterpret_cast<const char *>(Elts.data());
    2512             :   return getImpl(StringRef(Data, Elts.size() * 8), Ty);
    2513          81 : }
    2514          16 : 
    2515             : Constant *ConstantDataArray::getString(LLVMContext &Context,
    2516          16 :                                        StringRef Str, bool AddNull) {
    2517           4 :   if (!AddNull) {
    2518           4 :     const uint8_t *Data = reinterpret_cast<const uint8_t *>(Str.data());
    2519             :     return get(Context, makeArrayRef(Data, Str.size()));
    2520             :   }
    2521             : 
    2522             :   SmallVector<uint8_t, 64> ElementVals;
    2523             :   ElementVals.append(Str.begin(), Str.end());
    2524      584264 :   ElementVals.push_back(0);
    2525      584264 :   return get(Context, ElementVals);
    2526             : }
    2527             : 
    2528             : /// get() constructors - Return a constant with vector type with an element
    2529             : /// count and element type matching the ArrayRef passed in.  Note that this
    2530        1565 : /// can return a ConstantAggregateZero object.
    2531        1565 : Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
    2532             :   Type *Ty = VectorType::get(Type::getInt8Ty(Context), Elts.size());
    2533        7093 :   const char *Data = reinterpret_cast<const char *>(Elts.data());
    2534             :   return getImpl(StringRef(Data, Elts.size() * 1), Ty);
    2535        7093 : }
    2536             : Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
    2537             :   Type *Ty = VectorType::get(Type::getInt16Ty(Context), Elts.size());
    2538             :   const char *Data = reinterpret_cast<const char *>(Elts.data());
    2539             :   return getImpl(StringRef(Data, Elts.size() * 2), Ty);
    2540      275473 : }
    2541      275473 : Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
    2542      275473 :   Type *Ty = VectorType::get(Type::getInt32Ty(Context), Elts.size());
    2543             :   const char *Data = reinterpret_cast<const char *>(Elts.data());
    2544      108837 :   return getImpl(StringRef(Data, Elts.size() * 4), Ty);
    2545      108837 : }
    2546             : Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
    2547             :   Type *Ty = VectorType::get(Type::getInt64Ty(Context), Elts.size());
    2548      550810 :   const char *Data = reinterpret_cast<const char *>(Elts.data());
    2549      550810 :   return getImpl(StringRef(Data, Elts.size() * 8), Ty);
    2550             : }
    2551             : Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
    2552             :   Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
    2553             :   const char *Data = reinterpret_cast<const char *>(Elts.data());
    2554             :   return getImpl(StringRef(Data, Elts.size() * 4), Ty);
    2555      550810 : }
    2556             : Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
    2557      550810 :   Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
    2558     1669716 :   const char *Data = reinterpret_cast<const char *>(Elts.data());
    2559     2237812 :   return getImpl(StringRef(Data, Elts.size() * 8), Ty);
    2560      550810 : }
    2561             : 
    2562    87174408 : /// getFP() constructors - Return a constant with vector type with an element
    2563    87174408 : /// count and element type of float with the precision matching the number of
    2564             : /// bits in the ArrayRef passed in.  (i.e. half for 16bits, float for 32bits,
    2565             : /// double for 64bits) Note that this can return a ConstantAggregateZero
    2566    16629503 : /// object.
    2567    16629503 : Constant *ConstantDataVector::getFP(LLVMContext &Context,
    2568             :                                     ArrayRef<uint16_t> Elts) {
    2569             :   Type *Ty = VectorType::get(Type::getHalfTy(Context), Elts.size());
    2570             :   const char *Data = reinterpret_cast<const char *>(Elts.data());
    2571             :   return getImpl(StringRef(Data, Elts.size() * 2), Ty);
    2572             : }
    2573    57716953 : Constant *ConstantDataVector::getFP(LLVMContext &Context,
    2574    57716953 :                                     ArrayRef<uint32_t> Elts) {
    2575             :   Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
    2576             :   const char *Data = reinterpret_cast<const char *>(Elts.data());
    2577     2106037 :   return getImpl(StringRef(Data, Elts.size() * 4), Ty);
    2578     2106037 : }
    2579             : Constant *ConstantDataVector::getFP(LLVMContext &Context,
    2580             :                                     ArrayRef<uint64_t> Elts) {
    2581      408163 :   Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
    2582      408163 :   const char *Data = reinterpret_cast<const char *>(Elts.data());
    2583             :   return getImpl(StringRef(Data, Elts.size() * 8), Ty);
    2584             : }
    2585      330724 : 
    2586             : Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
    2587             :   assert(isElementTypeCompatible(V->getType()) &&
    2588             :          "Element type not compatible with ConstantData");
    2589      330724 :   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
    2590             :     if (CI->getType()->isIntegerTy(8)) {
    2591             :       SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
    2592             :       return get(V->getContext(), Elts);
    2593             :     }
    2594             :     if (CI->getType()->isIntegerTy(16)) {
    2595             :       SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
    2596     5756601 :       return get(V->getContext(), Elts);
    2597             :     }
    2598      655356 :     if (CI->getType()->isIntegerTy(32)) {
    2599     5101245 :       SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
    2600             :       return get(V->getContext(), Elts);
    2601             :     }
    2602             :     assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
    2603    23059407 :     SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
    2604    23059407 :     return get(V->getContext(), Elts);
    2605             :   }
    2606             : 
    2607             :   if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
    2608    19258092 :     if (CFP->getType()->isHalfTy()) {
    2609             :       SmallVector<uint16_t, 16> Elts(
    2610    19258092 :           NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
    2611             :       return getFP(V->getContext(), Elts);
    2612             :     }
    2613             :     if (CFP->getType()->isFloatTy()) {
    2614             :       SmallVector<uint32_t, 16> Elts(
    2615             :           NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
    2616     1263150 :       return getFP(V->getContext(), Elts);
    2617     1246745 :     }
    2618             :     if (CFP->getType()->isDoubleTy()) {
    2619             :       SmallVector<uint64_t, 16> Elts(
    2620             :           NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
    2621             :       return getFP(V->getContext(), Elts);
    2622             :     }
    2623             :   }
    2624             :   return ConstantVector::getSplat(NumElts, V);
    2625             : }
    2626      996562 : 
    2627             : 
    2628             : uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
    2629             :   assert(isa<IntegerType>(getElementType()) &&
    2630      996562 :          "Accessor can only be used when element is an integer");
    2631       16405 :   const char *EltPtr = getElementPointer(Elt);
    2632             : 
    2633             :   // The data is stored in host byte order, make sure to cast back to the right
    2634             :   // type to load with the right endianness.
    2635      980157 :   switch (getElementType()->getIntegerBitWidth()) {
    2636      980157 :   default: llvm_unreachable("Invalid bitwidth for CDS");
    2637             :   case 8:
    2638             :     return *reinterpret_cast<const uint8_t *>(EltPtr);
    2639             :   case 16:
    2640             :     return *reinterpret_cast<const uint16_t *>(EltPtr);
    2641             :   case 32:
    2642             :     return *reinterpret_cast<const uint32_t *>(EltPtr);
    2643      980157 :   case 64:
    2644      991861 :     return *reinterpret_cast<const uint64_t *>(EltPtr);
    2645       11704 :   }
    2646      722132 : }
    2647      710428 : 
    2648             : APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const {
    2649             :   assert(isa<IntegerType>(getElementType()) &&
    2650             :          "Accessor can only be used when element is an integer");
    2651      269729 :   const char *EltPtr = getElementPointer(Elt);
    2652      223751 : 
    2653             :   // The data is stored in host byte order, make sure to cast back to the right
    2654             :   // type to load with the right endianness.
    2655       45978 :   switch (getElementType()->getIntegerBitWidth()) {
    2656             :   default: llvm_unreachable("Invalid bitwidth for CDS");
    2657             :   case 8: {
    2658           0 :     auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr);
    2659             :     return APInt(8, EltVal);
    2660             :   }
    2661           0 :   case 16: {
    2662             :     auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
    2663             :     return APInt(16, EltVal);
    2664           0 :   }
    2665             :   case 32: {
    2666             :     auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
    2667             :     return APInt(32, EltVal);
    2668             :   }
    2669             :   case 64: {
    2670             :     auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
    2671           0 :     return APInt(64, EltVal);
    2672             :   }
    2673             :   }
    2674             : }
    2675           0 : 
    2676             : APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
    2677             :   const char *EltPtr = getElementPointer(Elt);
    2678             : 
    2679             :   switch (getElementType()->getTypeID()) {
    2680           0 :   default:
    2681           0 :     llvm_unreachable("Accessor can only be used when element is float/double!");
    2682             :   case Type::HalfTyID: {
    2683           0 :     auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
    2684           0 :     return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal));
    2685           0 :   }
    2686             :   case Type::FloatTyID: {
    2687             :     auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
    2688             :     return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal));
    2689             :   }
    2690             :   case Type::DoubleTyID: {
    2691             :     auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
    2692           0 :     return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal));
    2693           0 :   }
    2694             :   }
    2695             : }
    2696             : 
    2697             : float ConstantDataSequential::getElementAsFloat(unsigned Elt) const {
    2698             :   assert(getElementType()->isFloatTy() &&
    2699             :          "Accessor can only be used when element is a 'float'");
    2700          19 :   return *reinterpret_cast<const float *>(getElementPointer(Elt));
    2701             : }
    2702          19 : 
    2703          19 : double ConstantDataSequential::getElementAsDouble(unsigned Elt) const {
    2704          38 :   assert(getElementType()->isDoubleTy() &&
    2705             :          "Accessor can only be used when element is a 'float'");
    2706         102 :   return *reinterpret_cast<const double *>(getElementPointer(Elt));
    2707             : }
    2708         102 : 
    2709         102 : Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
    2710         204 :   if (getElementType()->isHalfTy() || getElementType()->isFloatTy() ||
    2711             :       getElementType()->isDoubleTy())
    2712         337 :     return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));
    2713             : 
    2714         337 :   return ConstantInt::get(getElementType(), getElementAsInteger(Elt));
    2715         337 : }
    2716         674 : 
    2717             : bool ConstantDataSequential::isString(unsigned CharSize) const {
    2718             :   return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize);
    2719      759786 : }
    2720             : 
    2721      759786 : bool ConstantDataSequential::isCString() const {
    2722             :   if (!isString())
    2723      713224 :     return false;
    2724             : 
    2725             :   StringRef Str = getAsString();
    2726             : 
    2727       46562 :   // The last value must be nul.
    2728       46562 :   if (Str.back() != 0) return false;
    2729             : 
    2730             :   // Other elements must be non-nul.
    2731             :   return Str.drop_back().find(0) == StringRef::npos;
    2732             : }
    2733             : 
    2734             : bool ConstantDataVector::isSplat() const {
    2735       14183 :   const char *Base = getRawDataValues().data();
    2736       14183 : 
    2737       14183 :   // Compare elements 1+ to the 0'th element.
    2738       14183 :   unsigned EltSize = getElementByteSize();
    2739             :   for (unsigned i = 1, e = getNumElements(); i != e; ++i)
    2740        9616 :     if (memcmp(Base, Base+i*EltSize, EltSize))
    2741        9616 :       return false;
    2742        9616 : 
    2743       19232 :   return true;
    2744             : }
    2745       65118 : 
    2746       65118 : Constant *ConstantDataVector::getSplatValue() const {
    2747       65118 :   // If they're all the same, return the 0th one as a representative.
    2748      130236 :   return isSplat() ? getElementAsConstant(0) : nullptr;
    2749             : }
    2750      110185 : 
    2751      110185 : //===----------------------------------------------------------------------===//
    2752      110185 : //                handleOperandChange implementations
    2753      220370 : 
    2754             : /// Update this constant array to change uses of
    2755           0 : /// 'From' to be uses of 'To'.  This must update the uniquing data structures
    2756           0 : /// etc.
    2757           0 : ///
    2758           0 : /// Note that we intentionally replace all uses of From with To here.  Consider
    2759             : /// a large array that uses 'From' 1000 times.  By handling this case all here,
    2760           0 : /// ConstantArray::handleOperandChange is only invoked once, and that
    2761           0 : /// single invocation handles all 1000 uses.  Handling them one at a time would
    2762           0 : /// work, but would be really slow because it would have to unique each updated
    2763           0 : /// array instance.
    2764             : ///
    2765             : void Constant::handleOperandChange(Value *From, Value *To) {
    2766             :   Value *Replacement = nullptr;
    2767             :   switch (getValueID()) {
    2768             :   default:
    2769             :     llvm_unreachable("Not a constant!");
    2770             : #define HANDLE_CONSTANT(Name)                                                  \
    2771         730 :   case Value::Name##Val:                                                       \
    2772             :     Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To);         \
    2773         730 :     break;
    2774         730 : #include "llvm/IR/Value.def"
    2775        1460 :   }
    2776             : 
    2777        7152 :   // If handleOperandChangeImpl returned nullptr, then it handled
    2778             :   // replacing itself and we don't want to delete or replace anything else here.
    2779        7152 :   if (!Replacement)
    2780        7152 :     return;
    2781       14304 : 
    2782             :   // I do need to replace this with an existing value.
    2783        6353 :   assert(Replacement != this && "I didn't contain From!");
    2784             : 
    2785        6353 :   // Everyone using this now uses the replacement.
    2786        6353 :   replaceAllUsesWith(Replacement);
    2787       12706 : 
    2788             :   // Delete the old constant!
    2789             :   destroyConstant();
    2790       12651 : }
    2791             : 
    2792             : Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
    2793             :   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
    2794        8983 :   Constant *ToC = cast<Constant>(To);
    2795        1331 : 
    2796        1331 :   SmallVector<Constant*, 8> Values;
    2797             :   Values.reserve(getNumOperands());  // Build replacement array.
    2798        7652 : 
    2799        1152 :   // Fill values with the modified operands of the constant array.  Also,
    2800        1152 :   // compute whether this turns into an all-zeros array.
    2801             :   unsigned NumUpdated = 0;
    2802        6500 : 
    2803        4564 :   // Keep track of whether all the values in the array are "ToC".
    2804        4564 :   bool AllSame = true;
    2805             :   Use *OperandList = getOperandList();
    2806             :   unsigned OperandNo = 0;
    2807        1936 :   for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
    2808        1936 :     Constant *Val = cast<Constant>(O->get());
    2809             :     if (Val == From) {
    2810             :       OperandNo = (O - OperandList);
    2811             :       Val = ToC;
    2812        7336 :       ++NumUpdated;
    2813             :     }
    2814         254 :     Values.push_back(Val);
    2815         127 :     AllSame &= Val == ToC;
    2816             :   }
    2817        3541 : 
    2818             :   if (AllSame && ToC->isNullValue())
    2819        3766 :     return ConstantAggregateZero::get(getType());
    2820        1883 : 
    2821             :   if (AllSame && isa<UndefValue>(ToC))
    2822        1658 :     return UndefValue::get(getType());
    2823             : 
    2824        3316 :   // Check for any other type of constant-folding.
    2825        1658 :   if (Constant *C = getImpl(getType(), Values))
    2826             :     return C;
    2827             : 
    2828           0 :   // Update to the new value.
    2829             :   return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(
    2830             :       Values, this, From, ToC, NumUpdated, OperandNo);
    2831             : }
    2832    19048572 : 
    2833             : Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
    2834             :   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
    2835    19048572 :   Constant *ToC = cast<Constant>(To);
    2836             : 
    2837             :   Use *OperandList = getOperandList();
    2838             : 
    2839    19048572 :   SmallVector<Constant*, 8> Values;
    2840           0 :   Values.reserve(getNumOperands());  // Build replacement struct.
    2841    14029558 : 
    2842    14029558 :   // Fill values with the modified operands of the constant struct.  Also,
    2843      220638 :   // compute whether this turns into an all-zeros struct.
    2844      220638 :   unsigned NumUpdated = 0;
    2845     3049489 :   bool AllSame = true;
    2846     3049489 :   unsigned OperandNo = 0;
    2847     1748887 :   for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
    2848     1748887 :     Constant *Val = cast<Constant>(O->get());
    2849             :     if (Val == From) {
    2850             :       OperandNo = (O - OperandList);
    2851             :       Val = ToC;
    2852      142126 :       ++NumUpdated;
    2853             :     }
    2854             :     Values.push_back(Val);
    2855      142126 :     AllSame &= Val == ToC;
    2856             :   }
    2857             : 
    2858             :   if (AllSame && ToC->isNullValue())
    2859      142126 :     return ConstantAggregateZero::get(getType());
    2860           0 : 
    2861       51595 :   if (AllSame && isa<UndefValue>(ToC))
    2862       51595 :     return UndefValue::get(getType());
    2863       51595 : 
    2864             :   // Update to the new value.
    2865       19305 :   return getContext().pImpl->StructConstants.replaceOperandsInPlace(
    2866       19305 :       Values, this, From, ToC, NumUpdated, OperandNo);
    2867       19305 : }
    2868             : 
    2869       45799 : Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
    2870       45799 :   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
    2871       45799 :   Constant *ToC = cast<Constant>(To);
    2872             : 
    2873       25427 :   SmallVector<Constant*, 8> Values;
    2874       25427 :   Values.reserve(getNumOperands());  // Build replacement array...
    2875             :   unsigned NumUpdated = 0;
    2876             :   unsigned OperandNo = 0;
    2877             :   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
    2878             :     Constant *Val = getOperand(i);
    2879             :     if (Val == From) {
    2880       67194 :       OperandNo = i;
    2881       67194 :       ++NumUpdated;
    2882             :       Val = ToC;
    2883       67194 :     }
    2884           0 :     Values.push_back(Val);
    2885           0 :   }
    2886        3542 : 
    2887        3542 :   if (Constant *C = getImpl(Values))
    2888        7084 :     return C;
    2889             : 
    2890       41290 :   // Update to the new value.
    2891       41290 :   return getContext().pImpl->VectorConstants.replaceOperandsInPlace(
    2892       82580 :       Values, this, From, ToC, NumUpdated, OperandNo);
    2893             : }
    2894       22362 : 
    2895       22362 : Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
    2896       44724 :   assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
    2897             :   Constant *To = cast<Constant>(ToV);
    2898             : 
    2899             :   SmallVector<Constant*, 8> NewOps;
    2900             :   unsigned NumUpdated = 0;
    2901          85 :   unsigned OperandNo = 0;
    2902             :   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
    2903             :     Constant *Op = getOperand(i);
    2904          85 :     if (Op == From) {
    2905             :       OperandNo = i;
    2906             :       ++NumUpdated;
    2907         115 :       Op = To;
    2908             :     }
    2909             :     NewOps.push_back(Op);
    2910         115 :   }
    2911             :   assert(NumUpdated && "I didn't contain From!");
    2912             : 
    2913     3781485 :   if (Constant *C = getWithOperands(NewOps, getType(), true))
    2914     7527911 :     return C;
    2915     3746426 : 
    2916      105222 :   // Update to the new value.
    2917             :   return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
    2918     3728874 :       NewOps, this, From, To, NumUpdated, OperandNo);
    2919             : }
    2920             : 
    2921      250112 : Instruction *ConstantExpr::getAsInstruction() {
    2922      250112 :   SmallVector<Value *, 4> ValueOperands(op_begin(), op_end());
    2923             :   ArrayRef<Value*> Ops(ValueOperands);
    2924             : 
    2925        2105 :   switch (getOpcode()) {
    2926        2105 :   case Instruction::Trunc:
    2927             :   case Instruction::ZExt:
    2928             :   case Instruction::SExt:
    2929        2105 :   case Instruction::FPTrunc:
    2930             :   case Instruction::FPExt:
    2931             :   case Instruction::UIToFP:
    2932        2105 :   case Instruction::SIToFP:
    2933             :   case Instruction::FPToUI:
    2934             :   case Instruction::FPToSI:
    2935        1306 :   case Instruction::PtrToInt:
    2936             :   case Instruction::IntToPtr:
    2937             :   case Instruction::BitCast:
    2938     1666686 :   case Instruction::AddrSpaceCast:
    2939     1666686 :     return CastInst::Create((Instruction::CastOps)getOpcode(),
    2940             :                             Ops[0], getType());
    2941             :   case Instruction::Select:
    2942     1666686 :     return SelectInst::Create(Ops[0], Ops[1], Ops[2]);
    2943     3632120 :   case Instruction::InsertElement:
    2944     1997202 :     return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]);
    2945             :   case Instruction::ExtractElement:
    2946             :     return ExtractElementInst::Create(Ops[0], Ops[1]);
    2947             :   case Instruction::InsertValue:
    2948             :     return InsertValueInst::Create(Ops[0], Ops[1], getIndices());
    2949             :   case Instruction::ExtractValue:
    2950     1658766 :     return ExtractValueInst::Create(Ops[0], getIndices());
    2951             :   case Instruction::ShuffleVector:
    2952     1658766 :     return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]);
    2953             : 
    2954             :   case Instruction::GetElementPtr: {
    2955             :     const auto *GO = cast<GEPOperator>(this);
    2956             :     if (GO->isInBounds())
    2957             :       return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
    2958             :                                                Ops[0], Ops.slice(1));
    2959             :     return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
    2960             :                                      Ops.slice(1));
    2961             :   }
    2962             :   case Instruction::ICmp:
    2963             :   case Instruction::FCmp:
    2964             :     return CmpInst::Create((Instruction::OtherOps)getOpcode(),
    2965             :                            (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]);
    2966             : 
    2967             :   default:
    2968             :     assert(getNumOperands() == 2 && "Must be binary operator?");
    2969        8606 :     BinaryOperator *BO =
    2970             :       BinaryOperator::Create((Instruction::BinaryOps)getOpcode(),
    2971       17212 :                              Ops[0], Ops[1]);
    2972           0 :     if (isa<OverflowingBinaryOperator>(BO)) {
    2973           0 :       BO->setHasNoUnsignedWrap(SubclassOptionalData &
    2974             :                                OverflowingBinaryOperator::NoUnsignedWrap);
    2975             :       BO->setHasNoSignedWrap(SubclassOptionalData &
    2976             :                              OverflowingBinaryOperator::NoSignedWrap);
    2977             :     }
    2978             :     if (isa<PossiblyExactOperator>(BO))
    2979             :       BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);
    2980             :     return BO;
    2981             :   }
    2982             : }

Generated by: LCOV version 1.13