LCOV - code coverage report
Current view: top level - lib/IR - Constants.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 1004 1155 86.9 %
Date: 2018-05-20 00:06:23 Functions: 207 225 92.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13