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

Generated by: LCOV version 1.13