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

Generated by: LCOV version 1.13