LCOV - code coverage report
Current view: top level - lib/Analysis - ConstantFolding.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 997 1069 93.3 %
Date: 2017-09-14 15:23:50 Functions: 32 33 97.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- ConstantFolding.cpp - Fold instructions into constants ------------===//
       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 defines routines for folding instructions into constants.
      11             : //
      12             : // Also, to supplement the basic IR ConstantExpr simplifications,
      13             : // this file defines some additional folding routines that can make use of
      14             : // DataLayout information. These functions cannot go in IR due to library
      15             : // dependency issues.
      16             : //
      17             : //===----------------------------------------------------------------------===//
      18             : 
      19             : #include "llvm/Analysis/ConstantFolding.h"
      20             : #include "llvm/ADT/APFloat.h"
      21             : #include "llvm/ADT/APInt.h"
      22             : #include "llvm/ADT/ArrayRef.h"
      23             : #include "llvm/ADT/DenseMap.h"
      24             : #include "llvm/ADT/STLExtras.h"
      25             : #include "llvm/ADT/SmallVector.h"
      26             : #include "llvm/ADT/StringRef.h"
      27             : #include "llvm/Analysis/TargetLibraryInfo.h"
      28             : #include "llvm/Analysis/ValueTracking.h"
      29             : #include "llvm/Config/config.h"
      30             : #include "llvm/IR/Constant.h"
      31             : #include "llvm/IR/Constants.h"
      32             : #include "llvm/IR/DataLayout.h"
      33             : #include "llvm/IR/DerivedTypes.h"
      34             : #include "llvm/IR/Function.h"
      35             : #include "llvm/IR/GlobalValue.h"
      36             : #include "llvm/IR/GlobalVariable.h"
      37             : #include "llvm/IR/InstrTypes.h"
      38             : #include "llvm/IR/Instruction.h"
      39             : #include "llvm/IR/Instructions.h"
      40             : #include "llvm/IR/Operator.h"
      41             : #include "llvm/IR/Type.h"
      42             : #include "llvm/IR/Value.h"
      43             : #include "llvm/Support/Casting.h"
      44             : #include "llvm/Support/ErrorHandling.h"
      45             : #include "llvm/Support/KnownBits.h"
      46             : #include "llvm/Support/MathExtras.h"
      47             : #include <cassert>
      48             : #include <cerrno>
      49             : #include <cfenv>
      50             : #include <cmath>
      51             : #include <cstddef>
      52             : #include <cstdint>
      53             : 
      54             : using namespace llvm;
      55             : 
      56             : namespace {
      57             : 
      58             : //===----------------------------------------------------------------------===//
      59             : // Constant Folding internal helper functions
      60             : //===----------------------------------------------------------------------===//
      61             : 
      62        3772 : static Constant *foldConstVectorToAPInt(APInt &Result, Type *DestTy,
      63             :                                         Constant *C, Type *SrcEltTy,
      64             :                                         unsigned NumSrcElts,
      65             :                                         const DataLayout &DL) {
      66             :   // Now that we know that the input value is a vector of integers, just shift
      67             :   // and insert them into our result.
      68        3772 :   unsigned BitShift = DL.getTypeSizeInBits(SrcEltTy);
      69       28994 :   for (unsigned i = 0; i != NumSrcElts; ++i) {
      70             :     Constant *Element;
      71       25223 :     if (DL.isLittleEndian())
      72       24001 :       Element = C->getAggregateElement(NumSrcElts - i - 1);
      73             :     else
      74        1222 :       Element = C->getAggregateElement(i);
      75             : 
      76       51235 :     if (Element && isa<UndefValue>(Element)) {
      77         789 :       Result <<= BitShift;
      78         789 :       continue;
      79             :     }
      80             : 
      81       48867 :     auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
      82             :     if (!ElementCI)
      83           1 :       return ConstantExpr::getBitCast(C, DestTy);
      84             : 
      85       24433 :     Result <<= BitShift;
      86       73299 :     Result |= ElementCI->getValue().zextOrSelf(Result.getBitWidth());
      87             :   }
      88             : 
      89             :   return nullptr;
      90             : }
      91             : 
      92             : /// Constant fold bitcast, symbolically evaluating it with DataLayout.
      93             : /// This always returns a non-null constant, but it may be a
      94             : /// ConstantExpr if unfoldable.
      95     1295735 : Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
      96             :   // Catch the obvious splat cases.
      97     1296318 :   if (C->isNullValue() && !DestTy->isX86_MMXTy())
      98         576 :     return Constant::getNullValue(DestTy);
      99     1298681 :   if (C->isAllOnesValue() && !DestTy->isX86_MMXTy() &&
     100        3522 :       !DestTy->isPtrOrPtrVectorTy()) // Don't get ones for ptr types!
     101        1759 :     return Constant::getAllOnesValue(DestTy);
     102             : 
     103     1297379 :   if (auto *VTy = dyn_cast<VectorType>(C->getType())) {
     104             :     // Handle a vector->scalar integer/fp cast.
     105        7958 :     if (isa<IntegerType>(DestTy) || DestTy->isFloatingPointTy()) {
     106        3772 :       unsigned NumSrcElts = VTy->getNumElements();
     107        3772 :       Type *SrcEltTy = VTy->getElementType();
     108             : 
     109             :       // If the vector is a vector of floating point, convert it to vector of int
     110             :       // to simplify things.
     111             :       if (SrcEltTy->isFloatingPointTy()) {
     112         302 :         unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
     113             :         Type *SrcIVTy =
     114         302 :           VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElts);
     115             :         // Ask IR to do the conversion now that #elts line up.
     116         302 :         C = ConstantExpr::getBitCast(C, SrcIVTy);
     117             :       }
     118             : 
     119       11316 :       APInt Result(DL.getTypeSizeInBits(DestTy), 0);
     120        3772 :       if (Constant *CE = foldConstVectorToAPInt(Result, DestTy, C,
     121        3772 :                                                 SrcEltTy, NumSrcElts, DL))
     122             :         return CE;
     123             : 
     124        7542 :       if (isa<IntegerType>(DestTy))
     125        3759 :         return ConstantInt::get(DestTy, Result);
     126             : 
     127          24 :       APFloat FP(DestTy->getFltSemantics(), Result);
     128          12 :       return ConstantFP::get(DestTy->getContext(), FP);
     129             :     }
     130             :   }
     131             : 
     132             :   // The code below only handles casts to vectors currently.
     133     1289846 :   auto *DestVTy = dyn_cast<VectorType>(DestTy);
     134             :   if (!DestVTy)
     135     1289410 :     return ConstantExpr::getBitCast(C, DestTy);
     136             : 
     137             :   // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
     138             :   // vector so the code below can handle it uniformly.
     139         654 :   if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) {
     140          18 :     Constant *Ops = C; // don't take the address of C!
     141          36 :     return FoldBitCast(ConstantVector::get(Ops), DestTy, DL);
     142             :   }
     143             : 
     144             :   // If this is a bitcast from constant vector -> vector, fold it.
     145         540 :   if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C))
     146           6 :     return ConstantExpr::getBitCast(C, DestTy);
     147             : 
     148             :   // If the element types match, IR can fold it.
     149         194 :   unsigned NumDstElt = DestVTy->getNumElements();
     150         388 :   unsigned NumSrcElt = C->getType()->getVectorNumElements();
     151         194 :   if (NumDstElt == NumSrcElt)
     152          33 :     return ConstantExpr::getBitCast(C, DestTy);
     153             : 
     154         322 :   Type *SrcEltTy = C->getType()->getVectorElementType();
     155         161 :   Type *DstEltTy = DestVTy->getElementType();
     156             : 
     157             :   // Otherwise, we're changing the number of elements in a vector, which
     158             :   // requires endianness information to do the right thing.  For example,
     159             :   //    bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
     160             :   // folds to (little endian):
     161             :   //    <4 x i32> <i32 0, i32 0, i32 1, i32 0>
     162             :   // and to (big endian):
     163             :   //    <4 x i32> <i32 0, i32 0, i32 0, i32 1>
     164             : 
     165             :   // First thing is first.  We only want to think about integer here, so if
     166             :   // we have something in FP form, recast it as integer.
     167         147 :   if (DstEltTy->isFloatingPointTy()) {
     168             :     // Fold to an vector of integers with same size as our FP type.
     169          14 :     unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits();
     170             :     Type *DestIVTy =
     171          14 :       VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt);
     172             :     // Recursively handle this integer conversion, if possible.
     173          14 :     C = FoldBitCast(C, DestIVTy, DL);
     174             : 
     175             :     // Finally, IR can handle this now that #elts line up.
     176          14 :     return ConstantExpr::getBitCast(C, DestTy);
     177             :   }
     178             : 
     179             :   // Okay, we know the destination is integer, if the input is FP, convert
     180             :   // it to integer first.
     181             :   if (SrcEltTy->isFloatingPointTy()) {
     182           5 :     unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
     183             :     Type *SrcIVTy =
     184           5 :       VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElt);
     185             :     // Ask IR to do the conversion now that #elts line up.
     186           5 :     C = ConstantExpr::getBitCast(C, SrcIVTy);
     187             :     // If IR wasn't able to fold it, bail out.
     188          14 :     if (!isa<ConstantVector>(C) &&  // FIXME: Remove ConstantVector.
     189           4 :         !isa<ConstantDataVector>(C))
     190             :       return C;
     191             :   }
     192             : 
     193             :   // Now we know that the input and output vectors are both integer vectors
     194             :   // of the same size, and that their #elements is not the same.  Do the
     195             :   // conversion here, which depends on whether the input or output has
     196             :   // more elements.
     197         294 :   bool isLittleEndian = DL.isLittleEndian();
     198             : 
     199         147 :   SmallVector<Constant*, 32> Result;
     200         147 :   if (NumDstElt < NumSrcElt) {
     201             :     // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
     202          43 :     Constant *Zero = Constant::getNullValue(DstEltTy);
     203          43 :     unsigned Ratio = NumSrcElt/NumDstElt;
     204          43 :     unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
     205          43 :     unsigned SrcElt = 0;
     206         220 :     for (unsigned i = 0; i != NumDstElt; ++i) {
     207             :       // Build each element of the result.
     208         177 :       Constant *Elt = Zero;
     209         177 :       unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
     210        1045 :       for (unsigned j = 0; j != Ratio; ++j) {
     211         434 :         Constant *Src = C->getAggregateElement(SrcElt++);
     212         868 :         if (Src && isa<UndefValue>(Src))
     213         116 :           Src = Constant::getNullValue(C->getType()->getVectorElementType());
     214             :         else
     215         376 :           Src = dyn_cast_or_null<ConstantInt>(Src);
     216         434 :         if (!Src)  // Reject constantexpr elements.
     217           0 :           return ConstantExpr::getBitCast(C, DestTy);
     218             : 
     219             :         // Zero extend the element to the right size.
     220         434 :         Src = ConstantExpr::getZExt(Src, Elt->getType());
     221             : 
     222             :         // Shift it to the right place, depending on endianness.
     223         434 :         Src = ConstantExpr::getShl(Src,
     224             :                                    ConstantInt::get(Src->getType(), ShiftAmt));
     225         434 :         ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
     226             : 
     227             :         // Mix it in.
     228         434 :         Elt = ConstantExpr::getOr(Elt, Src);
     229             :       }
     230         177 :       Result.push_back(Elt);
     231             :     }
     232          43 :     return ConstantVector::get(Result);
     233             :   }
     234             : 
     235             :   // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
     236         104 :   unsigned Ratio = NumDstElt/NumSrcElt;
     237         104 :   unsigned DstBitSize = DL.getTypeSizeInBits(DstEltTy);
     238             : 
     239             :   // Loop over each source value, expanding into multiple results.
     240         418 :   for (unsigned i = 0; i != NumSrcElt; ++i) {
     241         316 :     auto *Element = C->getAggregateElement(i);
     242             : 
     243         316 :     if (!Element) // Reject constantexpr elements.
     244           0 :       return ConstantExpr::getBitCast(C, DestTy);
     245             : 
     246         677 :     if (isa<UndefValue>(Element)) {
     247             :       // Correctly Propagate undef values.
     248          45 :       Result.append(Ratio, UndefValue::get(DstEltTy));
     249          45 :       continue;
     250             :     }
     251             : 
     252         542 :     auto *Src = dyn_cast<ConstantInt>(Element);
     253             :     if (!Src)
     254           2 :       return ConstantExpr::getBitCast(C, DestTy);
     255             : 
     256         269 :     unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
     257        2237 :     for (unsigned j = 0; j != Ratio; ++j) {
     258             :       // Shift the piece of the value into the right place, depending on
     259             :       // endianness.
     260             :       Constant *Elt = ConstantExpr::getLShr(Src,
     261        1968 :                                   ConstantInt::get(Src->getType(), ShiftAmt));
     262         984 :       ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
     263             : 
     264             :       // Truncate the element to an integer with the same pointer size and
     265             :       // convert the element back to a pointer using a inttoptr.
     266         984 :       if (DstEltTy->isPointerTy()) {
     267           6 :         IntegerType *DstIntTy = Type::getIntNTy(C->getContext(), DstBitSize);
     268           6 :         Constant *CE = ConstantExpr::getTrunc(Elt, DstIntTy);
     269           6 :         Result.push_back(ConstantExpr::getIntToPtr(CE, DstEltTy));
     270           6 :         continue;
     271             :       }
     272             : 
     273             :       // Truncate and remember this piece.
     274         978 :       Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy));
     275             :     }
     276             :   }
     277             : 
     278         102 :   return ConstantVector::get(Result);
     279             : }
     280             : 
     281             : } // end anonymous namespace
     282             : 
     283             : /// If this constant is a constant offset from a global, return the global and
     284             : /// the constant. Because of constantexprs, this function is recursive.
     285    14000589 : bool llvm::IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
     286             :                                       APInt &Offset, const DataLayout &DL) {
     287             :   // Trivial case, constant is the global.
     288    14000589 :   if ((GV = dyn_cast<GlobalValue>(C))) {
     289    13975770 :     unsigned BitWidth = DL.getPointerTypeSizeInBits(GV->getType());
     290    20963655 :     Offset = APInt(BitWidth, 0);
     291     6987885 :     return true;
     292             :   }
     293             : 
     294             :   // Otherwise, if this isn't a constant expr, bail out.
     295     7011834 :   auto *CE = dyn_cast<ConstantExpr>(C);
     296             :   if (!CE) return false;
     297             : 
     298             :   // Look through ptr->int and ptr->ptr casts.
     299    14023374 :   if (CE->getOpcode() == Instruction::PtrToInt ||
     300     7011540 :       CE->getOpcode() == Instruction::BitCast)
     301      505343 :     return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, DL);
     302             : 
     303             :   // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
     304     6506369 :   auto *GEP = dyn_cast<GEPOperator>(CE);
     305             :   if (!GEP)
     306             :     return false;
     307             : 
     308     6506369 :   unsigned BitWidth = DL.getPointerTypeSizeInBits(GEP->getType());
     309     6506369 :   APInt TmpOffset(BitWidth, 0);
     310             : 
     311             :   // If the base isn't a global+constant, we aren't either.
     312     6506369 :   if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, TmpOffset, DL))
     313             :     return false;
     314             : 
     315             :   // Otherwise, add any offset that our operands provide.
     316     6505515 :   if (!GEP->accumulateConstantOffset(DL, TmpOffset))
     317             :     return false;
     318             : 
     319     6505515 :   Offset = TmpOffset;
     320     6505515 :   return true;
     321             : }
     322             : 
     323             : namespace {
     324             : 
     325             : /// Recursive helper to read bits out of global. C is the constant being copied
     326             : /// out of. ByteOffset is an offset into C. CurPtr is the pointer to copy
     327             : /// results into and BytesLeft is the number of bytes left in
     328             : /// the CurPtr buffer. DL is the DataLayout.
     329         824 : bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr,
     330             :                         unsigned BytesLeft, const DataLayout &DL) {
     331             :   assert(ByteOffset <= DL.getTypeAllocSize(C->getType()) &&
     332             :          "Out of range access");
     333             : 
     334             :   // If this element is zero or undefined, we can just return since *CurPtr is
     335             :   // zero initialized.
     336        2592 :   if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
     337             :     return true;
     338             : 
     339        1497 :   if (auto *CI = dyn_cast<ConstantInt>(C)) {
     340        1276 :     if (CI->getBitWidth() > 64 ||
     341         638 :         (CI->getBitWidth() & 7) != 0)
     342             :       return false;
     343             : 
     344         638 :     uint64_t Val = CI->getZExtValue();
     345         638 :     unsigned IntBytes = unsigned(CI->getBitWidth()/8);
     346             : 
     347        1667 :     for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
     348        1029 :       int n = ByteOffset;
     349        1029 :       if (!DL.isLittleEndian())
     350         108 :         n = IntBytes - n - 1;
     351        1029 :       CurPtr[i] = (unsigned char)(Val >> (n * 8));
     352        1029 :       ++ByteOffset;
     353             :     }
     354             :     return true;
     355             :   }
     356             : 
     357         255 :   if (auto *CFP = dyn_cast<ConstantFP>(C)) {
     358          68 :     if (CFP->getType()->isDoubleTy()) {
     359           2 :       C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), DL);
     360           2 :       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
     361             :     }
     362          64 :     if (CFP->getType()->isFloatTy()){
     363          32 :       C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), DL);
     364          32 :       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
     365             :     }
     366           0 :     if (CFP->getType()->isHalfTy()){
     367           0 :       C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), DL);
     368           0 :       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
     369             :     }
     370             :     return false;
     371             :   }
     372             : 
     373         265 :   if (auto *CS = dyn_cast<ConstantStruct>(C)) {
     374          78 :     const StructLayout *SL = DL.getStructLayout(CS->getType());
     375          78 :     unsigned Index = SL->getElementContainingOffset(ByteOffset);
     376          78 :     uint64_t CurEltOffset = SL->getElementOffset(Index);
     377          78 :     ByteOffset -= CurEltOffset;
     378             : 
     379             :     while (true) {
     380             :       // If the element access is to the element itself and not to tail padding,
     381             :       // read the bytes from the element.
     382         282 :       uint64_t EltSize = DL.getTypeAllocSize(CS->getOperand(Index)->getType());
     383             : 
     384         282 :       if (ByteOffset < EltSize &&
     385         282 :           !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
     386             :                               BytesLeft, DL))
     387             :         return false;
     388             : 
     389         141 :       ++Index;
     390             : 
     391             :       // Check to see if we read from the last struct element, if so we're done.
     392         141 :       if (Index == CS->getType()->getNumElements())
     393             :         return true;
     394             : 
     395             :       // If we read all of the bytes we needed from this element we're done.
     396         117 :       uint64_t NextEltOffset = SL->getElementOffset(Index);
     397             : 
     398         117 :       if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
     399             :         return true;
     400             : 
     401             :       // Move to the next element of the struct.
     402          63 :       CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
     403          63 :       BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
     404          63 :       ByteOffset = 0;
     405          63 :       CurEltOffset = NextEltOffset;
     406          63 :     }
     407             :     // not reached.
     408             :   }
     409             : 
     410         327 :   if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
     411         109 :       isa<ConstantDataSequential>(C)) {
     412         206 :     Type *EltTy = C->getType()->getSequentialElementType();
     413         103 :     uint64_t EltSize = DL.getTypeAllocSize(EltTy);
     414         103 :     uint64_t Index = ByteOffset / EltSize;
     415         103 :     uint64_t Offset = ByteOffset - Index * EltSize;
     416             :     uint64_t NumElts;
     417         206 :     if (auto *AT = dyn_cast<ArrayType>(C->getType()))
     418         103 :       NumElts = AT->getNumElements();
     419             :     else
     420           0 :       NumElts = C->getType()->getVectorNumElements();
     421             : 
     422         905 :     for (; Index != NumElts; ++Index) {
     423         502 :       if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
     424             :                               BytesLeft, DL))
     425             :         return false;
     426             : 
     427         502 :       uint64_t BytesWritten = EltSize - Offset;
     428             :       assert(BytesWritten <= EltSize && "Not indexing into this element?");
     429         502 :       if (BytesWritten >= BytesLeft)
     430             :         return true;
     431             : 
     432         401 :       Offset = 0;
     433         401 :       BytesLeft -= BytesWritten;
     434         401 :       CurPtr += BytesWritten;
     435             :     }
     436             :     return true;
     437             :   }
     438             : 
     439          12 :   if (auto *CE = dyn_cast<ConstantExpr>(C)) {
     440          12 :     if (CE->getOpcode() == Instruction::IntToPtr &&
     441           6 :         CE->getOperand(0)->getType() == DL.getIntPtrType(CE->getType())) {
     442           6 :       return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
     443           6 :                                 BytesLeft, DL);
     444             :     }
     445             :   }
     446             : 
     447             :   // Otherwise, unknown initializer type.
     448             :   return false;
     449             : }
     450             : 
     451     7507221 : Constant *FoldReinterpretLoadFromConstPtr(Constant *C, Type *LoadTy,
     452             :                                           const DataLayout &DL) {
     453    15014442 :   auto *PTy = cast<PointerType>(C->getType());
     454     6988536 :   auto *IntType = dyn_cast<IntegerType>(LoadTy);
     455             : 
     456             :   // If this isn't an integer load we can't fold it directly.
     457             :   if (!IntType) {
     458      518685 :     unsigned AS = PTy->getAddressSpace();
     459             : 
     460             :     // If this is a float/double load, we can try folding it as an int32/64 load
     461             :     // and then bitcast the result.  This can be useful for union cases.  Note
     462             :     // that address spaces don't matter here since we're not going to result in
     463             :     // an actual new load.
     464             :     Type *MapTy;
     465      518685 :     if (LoadTy->isHalfTy())
     466           0 :       MapTy = Type::getInt16Ty(C->getContext());
     467      518685 :     else if (LoadTy->isFloatTy())
     468         188 :       MapTy = Type::getInt32Ty(C->getContext());
     469      518497 :     else if (LoadTy->isDoubleTy())
     470          59 :       MapTy = Type::getInt64Ty(C->getContext());
     471      518438 :     else if (LoadTy->isVectorTy()) {
     472      495733 :       MapTy = PointerType::getIntNTy(C->getContext(),
     473      495733 :                                      DL.getTypeAllocSizeInBits(LoadTy));
     474             :     } else
     475             :       return nullptr;
     476             : 
     477      495980 :     C = FoldBitCast(C, MapTy->getPointerTo(AS), DL);
     478      495980 :     if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, MapTy, DL))
     479          33 :       return FoldBitCast(Res, LoadTy, DL);
     480             :     return nullptr;
     481             :   }
     482             : 
     483     6988536 :   unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
     484     6988536 :   if (BytesLoaded > 32 || BytesLoaded == 0)
     485             :     return nullptr;
     486             : 
     487             :   GlobalValue *GVal;
     488     6988536 :   APInt OffsetAI;
     489     6988536 :   if (!IsConstantOffsetFromGlobal(C, GVal, OffsetAI, DL))
     490             :     return nullptr;
     491             : 
     492    13975184 :   auto *GV = dyn_cast<GlobalVariable>(GVal);
     493     6987777 :   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
     494         188 :       !GV->getInitializer()->getType()->isSized())
     495             :     return nullptr;
     496             : 
     497         188 :   int64_t Offset = OffsetAI.getSExtValue();
     498         188 :   int64_t InitializerSize = DL.getTypeAllocSize(GV->getInitializer()->getType());
     499             : 
     500             :   // If we're not accessing anything in this constant, the result is undefined.
     501         188 :   if (Offset + BytesLoaded <= 0)
     502           3 :     return UndefValue::get(IntType);
     503             : 
     504             :   // If we're not accessing anything in this constant, the result is undefined.
     505         185 :   if (Offset >= InitializerSize)
     506           4 :     return UndefValue::get(IntType);
     507             : 
     508         181 :   unsigned char RawBytes[32] = {0};
     509         181 :   unsigned char *CurPtr = RawBytes;
     510         181 :   unsigned BytesLeft = BytesLoaded;
     511             : 
     512             :   // If we're loading off the beginning of the global, some bytes may be valid.
     513         181 :   if (Offset < 0) {
     514           3 :     CurPtr += -Offset;
     515           3 :     BytesLeft += Offset;
     516           3 :     Offset = 0;
     517             :   }
     518             : 
     519         362 :   if (!ReadDataFromGlobal(GV->getInitializer(), Offset, CurPtr, BytesLeft, DL))
     520             :     return nullptr;
     521             : 
     522         362 :   APInt ResultVal = APInt(IntType->getBitWidth(), 0);
     523         181 :   if (DL.isLittleEndian()) {
     524         149 :     ResultVal = RawBytes[BytesLoaded - 1];
     525         965 :     for (unsigned i = 1; i != BytesLoaded; ++i) {
     526         816 :       ResultVal <<= 8;
     527        1632 :       ResultVal |= RawBytes[BytesLoaded - 1 - i];
     528             :     }
     529             :   } else {
     530          32 :     ResultVal = RawBytes[0];
     531         112 :     for (unsigned i = 1; i != BytesLoaded; ++i) {
     532          80 :       ResultVal <<= 8;
     533         160 :       ResultVal |= RawBytes[i];
     534             :     }
     535             :   }
     536             : 
     537         181 :   return ConstantInt::get(IntType->getContext(), ResultVal);
     538             : }
     539             : 
     540      506661 : Constant *ConstantFoldLoadThroughBitcast(ConstantExpr *CE, Type *DestTy,
     541             :                                          const DataLayout &DL) {
     542      506661 :   auto *SrcPtr = CE->getOperand(0);
     543     1013322 :   auto *SrcPtrTy = dyn_cast<PointerType>(SrcPtr->getType());
     544             :   if (!SrcPtrTy)
     545             :     return nullptr;
     546     1013322 :   Type *SrcTy = SrcPtrTy->getPointerElementType();
     547             : 
     548      506661 :   Constant *C = ConstantFoldLoadFromConstPtr(SrcPtr, SrcTy, DL);
     549      506661 :   if (!C)
     550             :     return nullptr;
     551             : 
     552             :   do {
     553         349 :     Type *SrcTy = C->getType();
     554             : 
     555             :     // If the type sizes are the same and a cast is legal, just directly
     556             :     // cast the constant.
     557         349 :     if (DL.getTypeSizeInBits(DestTy) == DL.getTypeSizeInBits(SrcTy)) {
     558         122 :       Instruction::CastOps Cast = Instruction::BitCast;
     559             :       // If we are going from a pointer to int or vice versa, we spell the cast
     560             :       // differently.
     561         134 :       if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
     562             :         Cast = Instruction::IntToPtr;
     563         167 :       else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
     564             :         Cast = Instruction::PtrToInt;
     565             : 
     566         122 :       if (CastInst::castIsValid(Cast, C, DestTy))
     567          59 :         return ConstantExpr::getCast(Cast, C, DestTy);
     568             :     }
     569             : 
     570             :     // If this isn't an aggregate type, there is nothing we can do to drill down
     571             :     // and find a bitcastable constant.
     572             :     if (!SrcTy->isAggregateType())
     573             :       return nullptr;
     574             : 
     575             :     // We're simulating a load through a pointer that was bitcast to point to
     576             :     // a different type, so we can try to walk down through the initial
     577             :     // elements of an aggregate to see if some part of th e aggregate is
     578             :     // castable to implement the "load" semantic model.
     579         115 :     C = C->getAggregateElement(0u);
     580         115 :   } while (C);
     581             : 
     582             :   return nullptr;
     583             : }
     584             : 
     585             : } // end anonymous namespace
     586             : 
     587     7616452 : Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
     588             :                                              const DataLayout &DL) {
     589             :   // First, try the easy cases:
     590      601352 :   if (auto *GV = dyn_cast<GlobalVariable>(C))
     591      601352 :     if (GV->isConstant() && GV->hasDefinitiveInitializer())
     592         199 :       return GV->getInitializer();
     593             : 
     594           8 :   if (auto *GA = dyn_cast<GlobalAlias>(C))
     595          16 :     if (GA->getAliasee() && !GA->isInterposable())
     596          16 :       return ConstantFoldLoadFromConstPtr(GA->getAliasee(), Ty, DL);
     597             : 
     598             :   // If the loaded value isn't a constant expr, we can't handle it.
     599     7011888 :   auto *CE = dyn_cast<ConstantExpr>(C);
     600             :   if (!CE)
     601             :     return nullptr;
     602             : 
     603     7011888 :   if (CE->getOpcode() == Instruction::GetElementPtr) {
     604    13009223 :     if (auto *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
     605     6504120 :       if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
     606         552 :         if (Constant *V =
     607         552 :              ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
     608             :           return V;
     609             :       }
     610             :     }
     611             :   }
     612             : 
     613     7011340 :   if (CE->getOpcode() == Instruction::BitCast)
     614      506661 :     if (Constant *LoadedC = ConstantFoldLoadThroughBitcast(CE, Ty, DL))
     615             :       return LoadedC;
     616             : 
     617             :   // Instead of loading constant c string, use corresponding integer value
     618             :   // directly if string length is small enough.
     619     7011281 :   StringRef Str;
     620     7011370 :   if (getConstantStringInfo(CE, Str) && !Str.empty()) {
     621          86 :     size_t StrLen = Str.size();
     622          86 :     unsigned NumBits = Ty->getPrimitiveSizeInBits();
     623             :     // Replace load with immediate integer if the result is an integer or fp
     624             :     // value.
     625         121 :     if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 &&
     626          36 :         (isa<IntegerType>(Ty) || Ty->isFloatingPointTy())) {
     627          70 :       APInt StrVal(NumBits, 0);
     628          70 :       APInt SingleChar(NumBits, 0);
     629          35 :       if (DL.isLittleEndian()) {
     630         542 :         for (unsigned char C : reverse(Str.bytes())) {
     631         205 :           SingleChar = static_cast<uint64_t>(C);
     632        1025 :           StrVal = (StrVal << 8) | SingleChar;
     633             :         }
     634             :       } else {
     635           8 :         for (unsigned char C : Str.bytes()) {
     636           6 :           SingleChar = static_cast<uint64_t>(C);
     637          30 :           StrVal = (StrVal << 8) | SingleChar;
     638             :         }
     639             :         // Append NULL at the end.
     640           2 :         SingleChar = 0;
     641          10 :         StrVal = (StrVal << 8) | SingleChar;
     642             :       }
     643             : 
     644          35 :       Constant *Res = ConstantInt::get(CE->getContext(), StrVal);
     645          35 :       if (Ty->isFloatingPointTy())
     646           1 :         Res = ConstantExpr::getBitCast(Res, Ty);
     647          35 :       return Res;
     648             :     }
     649             :   }
     650             : 
     651             :   // If this load comes from anywhere in a constant global, and if the global
     652             :   // is all undef or zero, we know what it loads.
     653    14021466 :   if (auto *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, DL))) {
     654     7010220 :     if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
     655         201 :       if (GV->getInitializer()->isNullValue())
     656           4 :         return Constant::getNullValue(Ty);
     657         394 :       if (isa<UndefValue>(GV->getInitializer()))
     658           1 :         return UndefValue::get(Ty);
     659             :     }
     660             :   }
     661             : 
     662             :   // Try hard to fold loads from bitcasted strange and non-type-safe things.
     663     7011241 :   return FoldReinterpretLoadFromConstPtr(CE, Ty, DL);
     664             : }
     665             : 
     666             : namespace {
     667             : 
     668     6789240 : Constant *ConstantFoldLoadInst(const LoadInst *LI, const DataLayout &DL) {
     669     6789240 :   if (LI->isVolatile()) return nullptr;
     670             : 
     671    20100918 :   if (auto *C = dyn_cast<Constant>(LI->getOperand(0)))
     672     6700306 :     return ConstantFoldLoadFromConstPtr(C, LI->getType(), DL);
     673             : 
     674             :   return nullptr;
     675             : }
     676             : 
     677             : /// One of Op0/Op1 is a constant expression.
     678             : /// Attempt to symbolically evaluate the result of a binary operator merging
     679             : /// these together.  If target data info is available, it is provided as DL,
     680             : /// otherwise DL is null.
     681        9828 : Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1,
     682             :                                     const DataLayout &DL) {
     683             :   // SROA
     684             : 
     685             :   // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
     686             :   // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
     687             :   // bits.
     688             : 
     689        9828 :   if (Opc == Instruction::And) {
     690        8123 :     KnownBits Known0 = computeKnownBits(Op0, DL);
     691        8123 :     KnownBits Known1 = computeKnownBits(Op1, DL);
     692       22780 :     if ((Known1.One | Known0.Zero).isAllOnesValue()) {
     693             :       // All the bits of Op0 that the 'and' could be masking are already zero.
     694         989 :       return Op0;
     695             :     }
     696       22775 :     if ((Known0.One | Known1.Zero).isAllOnesValue()) {
     697             :       // All the bits of Op1 that the 'and' could be masking are already zero.
     698             :       return Op1;
     699             :     }
     700             : 
     701        4555 :     Known0.Zero |= Known1.Zero;
     702        4555 :     Known0.One &= Known1.One;
     703        4555 :     if (Known0.isConstant())
     704         988 :       return ConstantInt::get(Op0->getType(), Known0.getConstant());
     705             :   }
     706             : 
     707             :   // If the constant expr is something like &A[123] - &A[4].f, fold this into a
     708             :   // constant.  This happens frequently when iterating over a global array.
     709        8839 :   if (Opc == Instruction::Sub) {
     710             :     GlobalValue *GV1, *GV2;
     711         266 :     APInt Offs1, Offs2;
     712             : 
     713          70 :     if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, DL))
     714          34 :       if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, DL) && GV1 == GV2) {
     715           7 :         unsigned OpSize = DL.getTypeSizeInBits(Op0->getType());
     716             : 
     717             :         // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
     718             :         // PtrToInt may change the bitwidth so we have convert to the right size
     719             :         // first.
     720          21 :         return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) -
     721          21 :                                                 Offs2.zextOrTrunc(OpSize));
     722             :       }
     723             :   }
     724             : 
     725             :   return nullptr;
     726             : }
     727             : 
     728             : /// If array indices are not pointer-sized integers, explicitly cast them so
     729             : /// that they aren't implicitly casted by the getelementptr.
     730     9843209 : Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops,
     731             :                          Type *ResultTy, Optional<unsigned> InRangeIndex,
     732             :                          const DataLayout &DL, const TargetLibraryInfo *TLI) {
     733     9843209 :   Type *IntPtrTy = DL.getIntPtrType(ResultTy);
     734     9843209 :   Type *IntPtrScalarTy = IntPtrTy->getScalarType();
     735             : 
     736     9843209 :   bool Any = false;
     737    19686418 :   SmallVector<Constant*, 32> NewIdxs;
     738    29549910 :   for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
     739     9863492 :     if ((i == 1 ||
     740    19726984 :          !isa<StructType>(GetElementPtrInst::getIndexedType(
     741    39366348 :              SrcElemTy, Ops.slice(1, i - 1)))) &&
     742    58978941 :         Ops[i]->getType()->getScalarType() != IntPtrScalarTy) {
     743      394394 :       Any = true;
     744      788788 :       Type *NewType = Ops[i]->getType()->isVectorTy()
     745      394394 :                           ? IntPtrTy
     746      394394 :                           : IntPtrTy->getScalarType();
     747      394394 :       NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i],
     748             :                                                                       true,
     749             :                                                                       NewType,
     750             :                                                                       true),
     751      394394 :                                               Ops[i], NewType));
     752             :     } else
     753    38624614 :       NewIdxs.push_back(Ops[i]);
     754             :   }
     755             : 
     756     9843209 :   if (!Any)
     757             :     return nullptr;
     758             : 
     759      989850 :   Constant *C = ConstantExpr::getGetElementPtr(
     760      395940 :       SrcElemTy, Ops[0], NewIdxs, /*InBounds=*/false, InRangeIndex);
     761      197970 :   if (Constant *Folded = ConstantFoldConstant(C, DL, TLI))
     762      197967 :     C = Folded;
     763             : 
     764             :   return C;
     765             : }
     766             : 
     767             : /// Strip the pointer casts, but preserve the address space information.
     768     9646123 : Constant* StripPtrCastKeepAS(Constant* Ptr, Type *&ElemTy) {
     769             :   assert(Ptr->getType()->isPointerTy() && "Not a pointer type");
     770    19292246 :   auto *OldPtrTy = cast<PointerType>(Ptr->getType());
     771     9646123 :   Ptr = Ptr->stripPointerCasts();
     772    19292246 :   auto *NewPtrTy = cast<PointerType>(Ptr->getType());
     773             : 
     774    19292246 :   ElemTy = NewPtrTy->getPointerElementType();
     775             : 
     776             :   // Preserve the address space number of the pointer.
     777    19292246 :   if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) {
     778          18 :     NewPtrTy = ElemTy->getPointerTo(OldPtrTy->getAddressSpace());
     779           9 :     Ptr = ConstantExpr::getPointerCast(Ptr, NewPtrTy);
     780             :   }
     781     9646123 :   return Ptr;
     782             : }
     783             : 
     784             : /// If we can symbolically evaluate the GEP constant expression, do so.
     785     9843209 : Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
     786             :                                   ArrayRef<Constant *> Ops,
     787             :                                   const DataLayout &DL,
     788             :                                   const TargetLibraryInfo *TLI) {
     789     9843209 :   const GEPOperator *InnermostGEP = GEP;
     790     9843209 :   bool InBounds = GEP->isInBounds();
     791             : 
     792     9843209 :   Type *SrcElemTy = GEP->getSourceElementType();
     793     9843209 :   Type *ResElemTy = GEP->getResultElementType();
     794     9843209 :   Type *ResTy = GEP->getType();
     795     9843209 :   if (!SrcElemTy->isSized())
     796             :     return nullptr;
     797             : 
     798    19686418 :   if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy,
     799    29529627 :                                    GEP->getInRangeIndex(), DL, TLI))
     800             :     return C;
     801             : 
     802     9645239 :   Constant *Ptr = Ops[0];
     803    19290478 :   if (!Ptr->getType()->isPointerTy())
     804             :     return nullptr;
     805             : 
     806     9645236 :   Type *IntPtrTy = DL.getIntPtrType(Ptr->getType());
     807             : 
     808             :   // If this is a constant expr gep that is effectively computing an
     809             :   // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
     810    28955272 :   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
     811    57930234 :     if (!isa<ConstantInt>(Ops[i])) {
     812             : 
     813             :       // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
     814             :       // "inttoptr (sub (ptrtoint Ptr), V)"
     815          42 :       if (Ops.size() == 2 && ResElemTy->isIntegerTy(8)) {
     816          34 :         auto *CE = dyn_cast<ConstantExpr>(Ops[1]);
     817             :         assert((!CE || CE->getType() == IntPtrTy) &&
     818             :                "CastGEPIndices didn't canonicalize index types!");
     819          33 :         if (CE && CE->getOpcode() == Instruction::Sub &&
     820          16 :             CE->getOperand(0)->isNullValue()) {
     821          11 :           Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType());
     822          11 :           Res = ConstantExpr::getSub(Res, CE->getOperand(1));
     823          11 :           Res = ConstantExpr::getIntToPtr(Res, ResTy);
     824          11 :           if (auto *FoldedRes = ConstantFoldConstant(Res, DL, TLI))
     825          11 :             Res = FoldedRes;
     826             :           return Res;
     827             :         }
     828             :       }
     829             :       return nullptr;
     830             :     }
     831             : 
     832     9645194 :   unsigned BitWidth = DL.getTypeSizeInBits(IntPtrTy);
     833             :   APInt Offset =
     834             :       APInt(BitWidth,
     835    28935582 :             DL.getIndexedOffsetInType(
     836             :                 SrcElemTy,
     837    38580776 :                 makeArrayRef((Value * const *)Ops.data() + 1, Ops.size() - 1)));
     838     9645194 :   Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
     839             : 
     840             :   // If this is a GEP of a GEP, fold it all into a single GEP.
     841         929 :   while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
     842         929 :     InnermostGEP = GEP;
     843         929 :     InBounds &= GEP->isInBounds();
     844             : 
     845        4645 :     SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end());
     846             : 
     847             :     // Do not try the incorporate the sub-GEP if some index is not a number.
     848         929 :     bool AllConstantInt = true;
     849        4665 :     for (Value *NestedOp : NestedOps)
     850        3756 :       if (!isa<ConstantInt>(NestedOp)) {
     851             :         AllConstantInt = false;
     852             :         break;
     853             :       }
     854         929 :     if (!AllConstantInt)
     855             :       break;
     856             : 
     857        2787 :     Ptr = cast<Constant>(GEP->getOperand(0));
     858         929 :     SrcElemTy = GEP->getSourceElementType();
     859        2787 :     Offset += APInt(BitWidth, DL.getIndexedOffsetInType(SrcElemTy, NestedOps));
     860         929 :     Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
     861             :   }
     862             : 
     863             :   // If the base value for this address is a literal integer value, fold the
     864             :   // getelementptr to the resulting integer value casted to the pointer type.
     865    19290388 :   APInt BasePtr(BitWidth, 0);
     866        2912 :   if (auto *CE = dyn_cast<ConstantExpr>(Ptr)) {
     867        2912 :     if (CE->getOpcode() == Instruction::IntToPtr) {
     868        2986 :       if (auto *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
     869         249 :         BasePtr = Base->getValue().zextOrTrunc(BitWidth);
     870             :     }
     871             :   }
     872             : 
     873    19290388 :   auto *PTy = cast<PointerType>(Ptr->getType());
     874    19290471 :   if ((Ptr->isNullValue() || BasePtr != 0) &&
     875        1252 :       !DL.isNonIntegralPointerType(PTy)) {
     876        5000 :     Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr);
     877        1250 :     return ConstantExpr::getIntToPtr(C, ResTy);
     878             :   }
     879             : 
     880             :   // Otherwise form a regular getelementptr. Recompute the indices so that
     881             :   // we eliminate over-indexing of the notional static type array bounds.
     882             :   // This makes it easy to determine if the getelementptr is "inbounds".
     883             :   // Also, this helps GlobalOpt do SROA on GlobalVariables.
     884     9643944 :   Type *Ty = PTy;
     885             :   SmallVector<Constant *, 32> NewIdxs;
     886             : 
     887             :   do {
     888    19309649 :     if (!Ty->isStructTy()) {
     889    19263960 :       if (Ty->isPointerTy()) {
     890             :         // The only pointer indexing we'll do is on the first index of the GEP.
     891     9643975 :         if (!NewIdxs.empty())
     892             :           break;
     893             : 
     894     9643944 :         Ty = SrcElemTy;
     895             : 
     896             :         // Only handle pointers to sized types, not pointers to functions.
     897     9643944 :         if (!Ty->isSized())
     898           1 :           return nullptr;
     899     9619039 :       } else if (auto *ATy = dyn_cast<SequentialType>(Ty)) {
     900     9619039 :         Ty = ATy->getElementType();
     901             :       } else {
     902             :         // We've reached some non-indexable type.
     903             :         break;
     904             :       }
     905             : 
     906             :       // Determine which element of the array the offset points into.
     907    57788946 :       APInt ElemSize(BitWidth, DL.getTypeAllocSize(Ty));
     908    19262982 :       if (ElemSize == 0) {
     909             :         // The element size is 0. This may be [0 x Ty]*, so just use a zero
     910             :         // index for this level and proceed to the next level to see if it can
     911             :         // accommodate the offset.
     912          50 :         NewIdxs.push_back(ConstantInt::get(IntPtrTy, 0));
     913             :       } else {
     914             :         // The element size is non-zero divide the offset by the element
     915             :         // size (rounding down), to compute the index at this level.
     916             :         bool Overflow;
     917    38525864 :         APInt NewIdx = Offset.sdiv_ov(ElemSize, Overflow);
     918    19262932 :         if (Overflow)
     919             :           break;
     920    38525864 :         Offset -= NewIdx * ElemSize;
     921    19262932 :         NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx));
     922             :       }
     923             :     } else {
     924       45689 :       auto *STy = cast<StructType>(Ty);
     925             :       // If we end up with an offset that isn't valid for this struct type, we
     926             :       // can't re-form this GEP in a regular form, so bail out. The pointer
     927             :       // operand likely went through casts that are necessary to make the GEP
     928             :       // sensible.
     929       45689 :       const StructLayout &SL = *DL.getStructLayout(STy);
     930      137066 :       if (Offset.isNegative() || Offset.uge(SL.getSizeInBytes()))
     931             :         break;
     932             : 
     933             :       // Determine which field of the struct the offset points into. The
     934             :       // getZExtValue is fine as we've already ensured that the offset is
     935             :       // within the range representable by the StructLayout API.
     936       45684 :       unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
     937       45684 :       NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
     938             :                                          ElIdx));
     939      137052 :       Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
     940       45684 :       Ty = STy->getTypeAtIndex(ElIdx);
     941             :     }
     942    19308666 :   } while (Ty != ResElemTy);
     943             : 
     944             :   // If we haven't used up the entire offset by descending the static
     945             :   // type, then the offset is pointing into the middle of an indivisible
     946             :   // member, so we can't simplify it.
     947     9643943 :   if (Offset != 0)
     948             :     return nullptr;
     949             : 
     950             :   // Preserve the inrange index from the innermost GEP if possible. We must
     951             :   // have calculated the same indices up to and including the inrange index.
     952     9643931 :   Optional<unsigned> InRangeIndex;
     953    19287862 :   if (Optional<unsigned> LastIRIndex = InnermostGEP->getInRangeIndex())
     954       38456 :     if (SrcElemTy == InnermostGEP->getSourceElementType() &&
     955       38456 :         NewIdxs.size() > *LastIRIndex) {
     956             :       InRangeIndex = LastIRIndex;
     957       57671 :       for (unsigned I = 0; I <= *LastIRIndex; ++I)
     958      115344 :         if (NewIdxs[I] != InnermostGEP->getOperand(I + 1)) {
     959             :           InRangeIndex = None;
     960             :           break;
     961             :         }
     962             :     }
     963             : 
     964             :   // Create a GEP.
     965    38575724 :   Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ptr, NewIdxs,
     966     9643931 :                                                InBounds, InRangeIndex);
     967             :   assert(C->getType()->getPointerElementType() == Ty &&
     968             :          "Computed GetElementPtr has unexpected type!");
     969             : 
     970             :   // If we ended up indexing a member with a type that doesn't match
     971             :   // the type of what the original indices indexed, add a cast.
     972     9643931 :   if (Ty != ResElemTy)
     973         970 :     C = FoldBitCast(C, ResTy, DL);
     974             : 
     975     9643931 :   return C;
     976             : }
     977             : 
     978             : /// Attempt to constant fold an instruction with the
     979             : /// specified opcode and operands.  If successful, the constant result is
     980             : /// returned, if not, null is returned.  Note that this function can fail when
     981             : /// attempting to fold instructions like loads and stores, which have no
     982             : /// constant expression form.
     983             : ///
     984             : /// TODO: This function neither utilizes nor preserves nsw/nuw/inbounds/inrange
     985             : /// etc information, due to only being passed an opcode and operands. Constant
     986             : /// folding using this function strips this information.
     987             : ///
     988    13724528 : Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode,
     989             :                                        ArrayRef<Constant *> Ops,
     990             :                                        const DataLayout &DL,
     991             :                                        const TargetLibraryInfo *TLI) {
     992    13724528 :   Type *DestTy = InstOrCE->getType();
     993             : 
     994             :   // Handle easy binops first.
     995    13724528 :   if (Instruction::isBinaryOp(Opcode))
     996       26651 :     return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL);
     997             : 
     998    13697877 :   if (Instruction::isCast(Opcode))
     999      782973 :     return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL);
    1000             : 
    1001     9843209 :   if (auto *GEP = dyn_cast<GEPOperator>(InstOrCE)) {
    1002     9843209 :     if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI))
    1003             :       return C;
    1004             : 
    1005          94 :     return ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), Ops[0],
    1006          47 :                                           Ops.slice(1), GEP->isInBounds(),
    1007         141 :                                           GEP->getInRangeIndex());
    1008             :   }
    1009             : 
    1010         315 :   if (auto *CE = dyn_cast<ConstantExpr>(InstOrCE))
    1011         315 :     return CE->getWithOperands(Ops);
    1012             : 
    1013     3071380 :   switch (Opcode) {
    1014             :   default: return nullptr;
    1015           0 :   case Instruction::ICmp:
    1016           0 :   case Instruction::FCmp: llvm_unreachable("Invalid for compares");
    1017       59284 :   case Instruction::Call:
    1018      118484 :     if (auto *F = dyn_cast<Function>(Ops.back())) {
    1019      118400 :       ImmutableCallSite CS(cast<CallInst>(InstOrCE));
    1020       59200 :       if (canConstantFoldCallTo(CS, F))
    1021        1206 :         return ConstantFoldCall(CS, F, Ops.slice(0, Ops.size() - 1), TLI);
    1022             :     }
    1023             :     return nullptr;
    1024         723 :   case Instruction::Select:
    1025         723 :     return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
    1026          45 :   case Instruction::ExtractElement:
    1027          45 :     return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
    1028         598 :   case Instruction::InsertElement:
    1029         598 :     return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
    1030          20 :   case Instruction::ShuffleVector:
    1031          20 :     return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
    1032             :   }
    1033             : }
    1034             : 
    1035             : } // end anonymous namespace
    1036             : 
    1037             : //===----------------------------------------------------------------------===//
    1038             : // Constant Folding public APIs
    1039             : //===----------------------------------------------------------------------===//
    1040             : 
    1041             : namespace {
    1042             : 
    1043             : Constant *
    1044    12594201 : ConstantFoldConstantImpl(const Constant *C, const DataLayout &DL,
    1045             :                          const TargetLibraryInfo *TLI,
    1046             :                          SmallDenseMap<Constant *, Constant *> &FoldedOps) {
    1047    37777905 :   if (!isa<ConstantVector>(C) && !isa<ConstantExpr>(C))
    1048             :     return nullptr;
    1049             : 
    1050    10639380 :   SmallVector<Constant *, 8> Ops;
    1051    51664145 :   for (const Use &NewU : C->operands()) {
    1052    30385385 :     auto *NewC = cast<Constant>(&NewU);
    1053             :     // Recursively fold the ConstantExpr's operands. If we have already folded
    1054             :     // a ConstantExpr, we don't have to process it again.
    1055    91156136 :     if (isa<ConstantVector>(NewC) || isa<ConstantExpr>(NewC)) {
    1056       78437 :       auto It = FoldedOps.find(NewC);
    1057      156874 :       if (It == FoldedOps.end()) {
    1058       73134 :         if (auto *FoldedC =
    1059       73134 :                 ConstantFoldConstantImpl(NewC, DL, TLI, FoldedOps)) {
    1060      146268 :           FoldedOps.insert({NewC, FoldedC});
    1061       73134 :           NewC = FoldedC;
    1062             :         } else {
    1063           0 :           FoldedOps.insert({NewC, NewC});
    1064             :         }
    1065             :       } else {
    1066        5303 :         NewC = It->second;
    1067             :       }
    1068             :     }
    1069    30385385 :     Ops.push_back(NewC);
    1070             :   }
    1071             : 
    1072    21274062 :   if (auto *CE = dyn_cast<ConstantExpr>(C)) {
    1073    10634682 :     if (CE->isCompare())
    1074        3810 :       return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
    1075        1905 :                                              DL, TLI);
    1076             : 
    1077    21265554 :     return ConstantFoldInstOperandsImpl(CE, CE->getOpcode(), Ops, DL, TLI);
    1078             :   }
    1079             : 
    1080             :   assert(isa<ConstantVector>(C));
    1081        4698 :   return ConstantVector::get(Ops);
    1082             : }
    1083             : 
    1084             : } // end anonymous namespace
    1085             : 
    1086    14128828 : Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
    1087             :                                         const TargetLibraryInfo *TLI) {
    1088             :   // Handle PHI nodes quickly here...
    1089      131274 :   if (auto *PN = dyn_cast<PHINode>(I)) {
    1090      131274 :     Constant *CommonValue = nullptr;
    1091             : 
    1092      262548 :     SmallDenseMap<Constant *, Constant *> FoldedOps;
    1093      251509 :     for (Value *Incoming : PN->incoming_values()) {
    1094             :       // If the incoming value is undef then skip it.  Note that while we could
    1095             :       // skip the value if it is equal to the phi node itself we choose not to
    1096             :       // because that would break the rule that constant folding only applies if
    1097             :       // all operands are constants.
    1098      502772 :       if (isa<UndefValue>(Incoming))
    1099        1417 :         continue;
    1100             :       // If the incoming value is not a constant, then give up.
    1101      401351 :       auto *C = dyn_cast<Constant>(Incoming);
    1102             :       if (!C)
    1103             :         return nullptr;
    1104             :       // Fold the PHI's operands.
    1105      151382 :       if (auto *FoldedC = ConstantFoldConstantImpl(C, DL, TLI, FoldedOps))
    1106       26083 :         C = FoldedC;
    1107             :       // If the incoming value is a different constant to
    1108             :       // the one we saw previously, then give up.
    1109      151382 :       if (CommonValue && C != CommonValue)
    1110             :         return nullptr;
    1111             :       CommonValue = C;
    1112             :     }
    1113             : 
    1114             :     // If we reach here, all incoming values are the same constant or undef.
    1115         123 :     return CommonValue ? CommonValue : UndefValue::get(PN->getType());
    1116             :   }
    1117             : 
    1118             :   // Scan the operand list, checking to see if they are all constants, if so,
    1119             :   // hand off to ConstantFoldInstOperandsImpl.
    1120    52415242 :   if (!all_of(I->operands(), [](Use &U) { return isa<Constant>(U); }))
    1121             :     return nullptr;
    1122             : 
    1123     8544806 :   SmallDenseMap<Constant *, Constant *> FoldedOps;
    1124    17089612 :   SmallVector<Constant *, 8> Ops;
    1125    25518503 :   for (const Use &OpU : I->operands()) {
    1126     8428891 :     auto *Op = cast<Constant>(&OpU);
    1127             :     // Fold the Instruction's operands.
    1128     8428891 :     if (auto *FoldedOp = ConstantFoldConstantImpl(Op, DL, TLI, FoldedOps))
    1129     6920032 :       Op = FoldedOp;
    1130             : 
    1131     8428891 :     Ops.push_back(Op);
    1132             :   }
    1133             : 
    1134         473 :   if (const auto *CI = dyn_cast<CmpInst>(I))
    1135        1419 :     return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
    1136         473 :                                            DL, TLI);
    1137             : 
    1138     6789240 :   if (const auto *LI = dyn_cast<LoadInst>(I))
    1139     6789240 :     return ConstantFoldLoadInst(LI, DL);
    1140             : 
    1141          32 :   if (auto *IVI = dyn_cast<InsertValueInst>(I)) {
    1142         160 :     return ConstantExpr::getInsertValue(
    1143             :                                 cast<Constant>(IVI->getAggregateOperand()),
    1144             :                                 cast<Constant>(IVI->getInsertedValueOperand()),
    1145          32 :                                 IVI->getIndices());
    1146             :   }
    1147             : 
    1148          51 :   if (auto *EVI = dyn_cast<ExtractValueInst>(I)) {
    1149         153 :     return ConstantExpr::getExtractValue(
    1150             :                                     cast<Constant>(EVI->getAggregateOperand()),
    1151          51 :                                     EVI->getIndices());
    1152             :   }
    1153             : 
    1154     1755010 :   return ConstantFoldInstOperands(I, Ops, DL, TLI);
    1155             : }
    1156             : 
    1157     3940794 : Constant *llvm::ConstantFoldConstant(const Constant *C, const DataLayout &DL,
    1158             :                                      const TargetLibraryInfo *TLI) {
    1159     7881588 :   SmallDenseMap<Constant *, Constant *> FoldedOps;
    1160     7881588 :   return ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
    1161             : }
    1162             : 
    1163     3091751 : Constant *llvm::ConstantFoldInstOperands(Instruction *I,
    1164             :                                          ArrayRef<Constant *> Ops,
    1165             :                                          const DataLayout &DL,
    1166             :                                          const TargetLibraryInfo *TLI) {
    1167     3091751 :   return ConstantFoldInstOperandsImpl(I, I->getOpcode(), Ops, DL, TLI);
    1168             : }
    1169             : 
    1170       57498 : Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
    1171             :                                                 Constant *Ops0, Constant *Ops1,
    1172             :                                                 const DataLayout &DL,
    1173             :                                                 const TargetLibraryInfo *TLI) {
    1174             :   // fold: icmp (inttoptr x), null         -> icmp x, 0
    1175             :   // fold: icmp null, (inttoptr x)         -> icmp 0, x
    1176             :   // fold: icmp (ptrtoint x), 0            -> icmp x, null
    1177             :   // fold: icmp 0, (ptrtoint x)            -> icmp null, x
    1178             :   // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
    1179             :   // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
    1180             :   //
    1181             :   // FIXME: The following comment is out of data and the DataLayout is here now.
    1182             :   // ConstantExpr::getCompare cannot do this, because it doesn't have DL
    1183             :   // around to know if bit truncation is happening.
    1184        2884 :   if (auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
    1185        2884 :     if (Ops1->isNullValue()) {
    1186        2311 :       if (CE0->getOpcode() == Instruction::IntToPtr) {
    1187          13 :         Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
    1188             :         // Convert the integer value to the right size to ensure we get the
    1189             :         // proper extension or truncation.
    1190          13 :         Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
    1191          13 :                                                    IntPtrTy, false);
    1192          13 :         Constant *Null = Constant::getNullValue(C->getType());
    1193          13 :         return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
    1194             :       }
    1195             : 
    1196             :       // Only do this transformation if the int is intptrty in size, otherwise
    1197             :       // there is a truncation or extension that we aren't modeling.
    1198        2298 :       if (CE0->getOpcode() == Instruction::PtrToInt) {
    1199          59 :         Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
    1200          59 :         if (CE0->getType() == IntPtrTy) {
    1201          18 :           Constant *C = CE0->getOperand(0);
    1202          18 :           Constant *Null = Constant::getNullValue(C->getType());
    1203          18 :           return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
    1204             :         }
    1205             :       }
    1206             :     }
    1207             : 
    1208        3239 :     if (auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
    1209         772 :       if (CE0->getOpcode() == CE1->getOpcode()) {
    1210          81 :         if (CE0->getOpcode() == Instruction::IntToPtr) {
    1211          10 :           Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
    1212             : 
    1213             :           // Convert the integer value to the right size to ensure we get the
    1214             :           // proper extension or truncation.
    1215          10 :           Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
    1216          10 :                                                       IntPtrTy, false);
    1217          10 :           Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
    1218          10 :                                                       IntPtrTy, false);
    1219          10 :           return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI);
    1220             :         }
    1221             : 
    1222             :         // Only do this transformation if the int is intptrty in size, otherwise
    1223             :         // there is a truncation or extension that we aren't modeling.
    1224          71 :         if (CE0->getOpcode() == Instruction::PtrToInt) {
    1225           9 :           Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
    1226          18 :           if (CE0->getType() == IntPtrTy &&
    1227          18 :               CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
    1228             :             return ConstantFoldCompareInstOperands(
    1229             :                 Predicate, CE0->getOperand(0), CE1->getOperand(0), DL, TLI);
    1230             :           }
    1231             :         }
    1232             :       }
    1233             :     }
    1234             : 
    1235             :     // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0)
    1236             :     // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0)
    1237        4929 :     if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) &&
    1238        4949 :         CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) {
    1239          40 :       Constant *LHS = ConstantFoldCompareInstOperands(
    1240          20 :           Predicate, CE0->getOperand(0), Ops1, DL, TLI);
    1241          40 :       Constant *RHS = ConstantFoldCompareInstOperands(
    1242          20 :           Predicate, CE0->getOperand(1), Ops1, DL, TLI);
    1243          20 :       unsigned OpC =
    1244          20 :         Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
    1245          20 :       return ConstantFoldBinaryOpOperands(OpC, LHS, RHS, DL);
    1246             :     }
    1247      109380 :   } else if (isa<ConstantExpr>(Ops1)) {
    1248             :     // If RHS is a constant expression, but the left side isn't, swap the
    1249             :     // operands and try again.
    1250          26 :     Predicate = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)Predicate);
    1251          26 :     return ConstantFoldCompareInstOperands(Predicate, Ops1, Ops0, DL, TLI);
    1252             :   }
    1253             : 
    1254       57478 :   return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
    1255             : }
    1256             : 
    1257      147542 : Constant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
    1258             :                                              Constant *RHS,
    1259             :                                              const DataLayout &DL) {
    1260             :   assert(Instruction::isBinaryOp(Opcode));
    1261      433881 :   if (isa<ConstantExpr>(LHS) || isa<ConstantExpr>(RHS))
    1262        9828 :     if (Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL))
    1263             :       return C;
    1264             : 
    1265      146546 :   return ConstantExpr::get(Opcode, LHS, RHS);
    1266             : }
    1267             : 
    1268      816680 : Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
    1269             :                                         Type *DestTy, const DataLayout &DL) {
    1270             :   assert(Instruction::isCast(Opcode));
    1271      816680 :   switch (Opcode) {
    1272           0 :   default:
    1273           0 :     llvm_unreachable("Missing case");
    1274        5840 :   case Instruction::PtrToInt:
    1275             :     // If the input is a inttoptr, eliminate the pair.  This requires knowing
    1276             :     // the width of a pointer, so it can't be done in ConstantExpr::getCast.
    1277        3966 :     if (auto *CE = dyn_cast<ConstantExpr>(C)) {
    1278        3966 :       if (CE->getOpcode() == Instruction::IntToPtr) {
    1279          67 :         Constant *Input = CE->getOperand(0);
    1280          67 :         unsigned InWidth = Input->getType()->getScalarSizeInBits();
    1281          67 :         unsigned PtrWidth = DL.getPointerTypeSizeInBits(CE->getType());
    1282          67 :         if (PtrWidth < InWidth) {
    1283             :           Constant *Mask =
    1284           2 :             ConstantInt::get(CE->getContext(),
    1285           6 :                              APInt::getLowBitsSet(InWidth, PtrWidth));
    1286           2 :           Input = ConstantExpr::getAnd(Input, Mask);
    1287             :         }
    1288             :         // Do a zext or trunc to get to the dest size.
    1289          67 :         return ConstantExpr::getIntegerCast(Input, DestTy, false);
    1290             :       }
    1291             :     }
    1292        5773 :     return ConstantExpr::getCast(Opcode, C, DestTy);
    1293        5429 :   case Instruction::IntToPtr:
    1294             :     // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
    1295             :     // the int size is >= the ptr size and the address spaces are the same.
    1296             :     // This requires knowing the width of a pointer, so it can't be done in
    1297             :     // ConstantExpr::getCast.
    1298        3937 :     if (auto *CE = dyn_cast<ConstantExpr>(C)) {
    1299        3937 :       if (CE->getOpcode() == Instruction::PtrToInt) {
    1300         947 :         Constant *SrcPtr = CE->getOperand(0);
    1301         947 :         unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType());
    1302         947 :         unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
    1303             : 
    1304         947 :         if (MidIntSize >= SrcPtrSize) {
    1305        1890 :           unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace();
    1306         945 :           if (SrcAS == DestTy->getPointerAddressSpace())
    1307         941 :             return FoldBitCast(CE->getOperand(0), DestTy, DL);
    1308             :         }
    1309             :       }
    1310             :     }
    1311             : 
    1312        4488 :     return ConstantExpr::getCast(Opcode, C, DestTy);
    1313        7666 :   case Instruction::Trunc:
    1314             :   case Instruction::ZExt:
    1315             :   case Instruction::SExt:
    1316             :   case Instruction::FPTrunc:
    1317             :   case Instruction::FPExt:
    1318             :   case Instruction::UIToFP:
    1319             :   case Instruction::SIToFP:
    1320             :   case Instruction::FPToUI:
    1321             :   case Instruction::FPToSI:
    1322             :   case Instruction::AddrSpaceCast:
    1323        7666 :       return ConstantExpr::getCast(Opcode, C, DestTy);
    1324      797745 :   case Instruction::BitCast:
    1325      797745 :     return FoldBitCast(C, DestTy, DL);
    1326             :   }
    1327             : }
    1328             : 
    1329         618 : Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
    1330             :                                                        ConstantExpr *CE) {
    1331         618 :   if (!CE->getOperand(1)->isNullValue())
    1332             :     return nullptr;  // Do not allow stepping over the value!
    1333             : 
    1334             :   // Loop over all of the operands, tracking down which value we are
    1335             :   // addressing.
    1336        1948 :   for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) {
    1337         721 :     C = C->getAggregateElement(CE->getOperand(i));
    1338         721 :     if (!C)
    1339             :       return nullptr;
    1340             :   }
    1341             :   return C;
    1342             : }
    1343             : 
    1344             : Constant *
    1345           0 : llvm::ConstantFoldLoadThroughGEPIndices(Constant *C,
    1346             :                                         ArrayRef<Constant *> Indices) {
    1347             :   // Loop over all of the operands, tracking down which value we are
    1348             :   // addressing.
    1349           0 :   for (Constant *Index : Indices) {
    1350           0 :     C = C->getAggregateElement(Index);
    1351           0 :     if (!C)
    1352             :       return nullptr;
    1353             :   }
    1354             :   return C;
    1355             : }
    1356             : 
    1357             : //===----------------------------------------------------------------------===//
    1358             : //  Constant Folding for Calls
    1359             : //
    1360             : 
    1361     4685640 : bool llvm::canConstantFoldCallTo(ImmutableCallSite CS, const Function *F) {
    1362     4685640 :   if (CS.isNoBuiltin() || CS.isStrictFP())
    1363             :     return false;
    1364     4638389 :   switch (F->getIntrinsicID()) {
    1365             :   case Intrinsic::fabs:
    1366             :   case Intrinsic::minnum:
    1367             :   case Intrinsic::maxnum:
    1368             :   case Intrinsic::log:
    1369             :   case Intrinsic::log2:
    1370             :   case Intrinsic::log10:
    1371             :   case Intrinsic::exp:
    1372             :   case Intrinsic::exp2:
    1373             :   case Intrinsic::floor:
    1374             :   case Intrinsic::ceil:
    1375             :   case Intrinsic::sqrt:
    1376             :   case Intrinsic::sin:
    1377             :   case Intrinsic::cos:
    1378             :   case Intrinsic::trunc:
    1379             :   case Intrinsic::rint:
    1380             :   case Intrinsic::nearbyint:
    1381             :   case Intrinsic::pow:
    1382             :   case Intrinsic::powi:
    1383             :   case Intrinsic::bswap:
    1384             :   case Intrinsic::ctpop:
    1385             :   case Intrinsic::ctlz:
    1386             :   case Intrinsic::cttz:
    1387             :   case Intrinsic::fma:
    1388             :   case Intrinsic::fmuladd:
    1389             :   case Intrinsic::copysign:
    1390             :   case Intrinsic::round:
    1391             :   case Intrinsic::masked_load:
    1392             :   case Intrinsic::sadd_with_overflow:
    1393             :   case Intrinsic::uadd_with_overflow:
    1394             :   case Intrinsic::ssub_with_overflow:
    1395             :   case Intrinsic::usub_with_overflow:
    1396             :   case Intrinsic::smul_with_overflow:
    1397             :   case Intrinsic::umul_with_overflow:
    1398             :   case Intrinsic::convert_from_fp16:
    1399             :   case Intrinsic::convert_to_fp16:
    1400             :   case Intrinsic::bitreverse:
    1401             :   case Intrinsic::x86_sse_cvtss2si:
    1402             :   case Intrinsic::x86_sse_cvtss2si64:
    1403             :   case Intrinsic::x86_sse_cvttss2si:
    1404             :   case Intrinsic::x86_sse_cvttss2si64:
    1405             :   case Intrinsic::x86_sse2_cvtsd2si:
    1406             :   case Intrinsic::x86_sse2_cvtsd2si64:
    1407             :   case Intrinsic::x86_sse2_cvttsd2si:
    1408             :   case Intrinsic::x86_sse2_cvttsd2si64:
    1409             :     return true;
    1410     1363209 :   default:
    1411     1363209 :     return false;
    1412             :   case Intrinsic::not_intrinsic: break;
    1413             :   }
    1414             : 
    1415     6505984 :   if (!F->hasName())
    1416             :     return false;
    1417     3252992 :   StringRef Name = F->getName();
    1418             : 
    1419             :   // In these cases, the check of the length is required.  We don't want to
    1420             :   // return true for a name like "cos\0blah" which strcmp would return equal to
    1421             :   // "cos", but has length 8.
    1422     6505984 :   switch (Name[0]) {
    1423             :   default:
    1424             :     return false;
    1425        1060 :   case 'a':
    1426        2950 :     return Name == "acos" || Name == "asin" || Name == "atan" ||
    1427        2323 :            Name == "atan2" || Name == "acosf" || Name == "asinf" ||
    1428        1467 :            Name == "atanf" || Name == "atan2f";
    1429        4080 :   case 'c':
    1430       12086 :     return Name == "ceil" || Name == "cos" || Name == "cosh" ||
    1431       11639 :            Name == "ceilf" || Name == "cosf" || Name == "coshf";
    1432       10962 :   case 'e':
    1433       43511 :     return Name == "exp" || Name == "exp2" || Name == "expf" || Name == "exp2f";
    1434        9653 :   case 'f':
    1435       28583 :     return Name == "fabs" || Name == "floor" || Name == "fmod" ||
    1436       28065 :            Name == "fabsf" || Name == "floorf" || Name == "fmodf";
    1437      420768 :   case 'l':
    1438     1262126 :     return Name == "log" || Name == "log10" || Name == "logf" ||
    1439      420672 :            Name == "log10f";
    1440        9088 :   case 'p':
    1441       18017 :     return Name == "pow" || Name == "powf";
    1442        1747 :   case 'r':
    1443        3436 :     return Name == "round" || Name == "roundf";
    1444       28663 :   case 's':
    1445       85745 :     return Name == "sin" || Name == "sinh" || Name == "sqrt" ||
    1446       85272 :            Name == "sinf" || Name == "sinhf" || Name == "sqrtf";
    1447        1357 :   case 't':
    1448        5037 :     return Name == "tan" || Name == "tanh" || Name == "tanf" || Name == "tanhf";
    1449     2744350 :   case '_':
    1450             : 
    1451             :     // Check for various function names that get used for the math functions
    1452             :     // when the header files are preprocessed with the macro
    1453             :     // __FINITE_MATH_ONLY__ enabled.
    1454             :     // The '12' here is the length of the shortest name that can match.
    1455             :     // We need to check the size before looking at Name[1] and Name[2]
    1456             :     // so we may as well check a limit that will eliminate mismatches.
    1457     5343477 :     if (Name.size() < 12 || Name[1] != '_')
    1458             :       return false;
    1459     2436716 :     switch (Name[2]) {
    1460             :     default:
    1461             :       return false;
    1462        2701 :     case 'a':
    1463        5401 :       return Name == "__acos_finite" || Name == "__acosf_finite" ||
    1464        5397 :              Name == "__asin_finite" || Name == "__asinf_finite" ||
    1465        5393 :              Name == "__atan2_finite" || Name == "__atan2f_finite";
    1466      152434 :     case 'c':
    1467      304867 :       return Name == "__cosh_finite" || Name == "__coshf_finite";
    1468        6792 :     case 'e':
    1469       13583 :       return Name == "__exp_finite" || Name == "__expf_finite" ||
    1470       13579 :              Name == "__exp2_finite" || Name == "__exp2f_finite";
    1471     1054033 :     case 'l':
    1472     2108065 :       return Name == "__log_finite" || Name == "__logf_finite" ||
    1473     2108061 :              Name == "__log10_finite" || Name == "__log10f_finite";
    1474           2 :     case 'p':
    1475           3 :       return Name == "__pow_finite" || Name == "__powf_finite";
    1476         585 :     case 's':
    1477        1169 :       return Name == "__sinh_finite" || Name == "__sinhf_finite";
    1478             :     }
    1479             :   }
    1480             : }
    1481             : 
    1482             : namespace {
    1483             : 
    1484         126 : Constant *GetConstantFoldFPValue(double V, Type *Ty) {
    1485         126 :   if (Ty->isHalfTy()) {
    1486           0 :     APFloat APF(V);
    1487             :     bool unused;
    1488           0 :     APF.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &unused);
    1489           0 :     return ConstantFP::get(Ty->getContext(), APF);
    1490             :   }
    1491         126 :   if (Ty->isFloatTy())
    1492         120 :     return ConstantFP::get(Ty->getContext(), APFloat((float)V));
    1493          66 :   if (Ty->isDoubleTy())
    1494         132 :     return ConstantFP::get(Ty->getContext(), APFloat(V));
    1495           0 :   llvm_unreachable("Can only constant fold half/float/double");
    1496             : }
    1497             : 
    1498             : /// Clear the floating-point exception state.
    1499             : inline void llvm_fenv_clearexcept() {
    1500             : #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT
    1501         128 :   feclearexcept(FE_ALL_EXCEPT);
    1502             : #endif
    1503         128 :   errno = 0;
    1504             : }
    1505             : 
    1506             : /// Test if a floating-point exception was raised.
    1507         127 : inline bool llvm_fenv_testexcept() {
    1508         127 :   int errno_val = errno;
    1509         127 :   if (errno_val == ERANGE || errno_val == EDOM)
    1510             :     return true;
    1511             : #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT && HAVE_DECL_FE_INEXACT
    1512         126 :   if (fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT))
    1513             :     return true;
    1514             : #endif
    1515         126 :   return false;
    1516             : }
    1517             : 
    1518          93 : Constant *ConstantFoldFP(double (*NativeFP)(double), double V, Type *Ty) {
    1519             :   llvm_fenv_clearexcept();
    1520          93 :   V = NativeFP(V);
    1521          93 :   if (llvm_fenv_testexcept()) {
    1522             :     llvm_fenv_clearexcept();
    1523           1 :     return nullptr;
    1524             :   }
    1525             : 
    1526          92 :   return GetConstantFoldFPValue(V, Ty);
    1527             : }
    1528             : 
    1529          34 : Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), double V,
    1530             :                                double W, Type *Ty) {
    1531             :   llvm_fenv_clearexcept();
    1532          34 :   V = NativeFP(V, W);
    1533          34 :   if (llvm_fenv_testexcept()) {
    1534             :     llvm_fenv_clearexcept();
    1535           0 :     return nullptr;
    1536             :   }
    1537             : 
    1538          34 :   return GetConstantFoldFPValue(V, Ty);
    1539             : }
    1540             : 
    1541             : /// Attempt to fold an SSE floating point to integer conversion of a constant
    1542             : /// floating point. If roundTowardZero is false, the default IEEE rounding is
    1543             : /// used (toward nearest, ties to even). This matches the behavior of the
    1544             : /// non-truncating SSE instructions in the default rounding mode. The desired
    1545             : /// integer type Ty is used to select how many bits are available for the
    1546             : /// result. Returns null if the conversion cannot be performed, otherwise
    1547             : /// returns the Constant value resulting from the conversion.
    1548          40 : Constant *ConstantFoldSSEConvertToInt(const APFloat &Val, bool roundTowardZero,
    1549             :                                       Type *Ty) {
    1550             :   // All of these conversion intrinsics form an integer of at most 64bits.
    1551          40 :   unsigned ResultWidth = Ty->getIntegerBitWidth();
    1552             :   assert(ResultWidth <= 64 &&
    1553             :          "Can only constant fold conversions to 64 and 32 bit ints");
    1554             : 
    1555             :   uint64_t UIntVal;
    1556          40 :   bool isExact = false;
    1557          40 :   APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero
    1558             :                                               : APFloat::rmNearestTiesToEven;
    1559             :   APFloat::opStatus status =
    1560          40 :       Val.convertToInteger(makeMutableArrayRef(UIntVal), ResultWidth,
    1561          40 :                            /*isSigned=*/true, mode, &isExact);
    1562          72 :   if (status != APFloat::opOK &&
    1563          32 :       (!roundTowardZero || status != APFloat::opInexact))
    1564             :     return nullptr;
    1565          12 :   return ConstantInt::get(Ty, UIntVal, /*isSigned=*/true);
    1566             : }
    1567             : 
    1568         377 : double getValueAsDouble(ConstantFP *Op) {
    1569         377 :   Type *Ty = Op->getType();
    1570             : 
    1571         377 :   if (Ty->isFloatTy())
    1572         238 :     return Op->getValueAPF().convertToFloat();
    1573             : 
    1574         139 :   if (Ty->isDoubleTy())
    1575         139 :     return Op->getValueAPF().convertToDouble();
    1576             : 
    1577             :   bool unused;
    1578           0 :   APFloat APF = Op->getValueAPF();
    1579           0 :   APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &unused);
    1580           0 :   return APF.convertToDouble();
    1581             : }
    1582             : 
    1583         755 : Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, Type *Ty,
    1584             :                                  ArrayRef<Constant *> Operands,
    1585             :                                  const TargetLibraryInfo *TLI) {
    1586         755 :   if (Operands.size() == 1) {
    1587         580 :     if (isa<UndefValue>(Operands[0])) {
    1588             :       // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN
    1589           5 :       if (IntrinsicID == Intrinsic::cos)
    1590           2 :         return Constant::getNullValue(Ty);
    1591           3 :       if (IntrinsicID == Intrinsic::bswap ||
    1592             :           IntrinsicID == Intrinsic::bitreverse)
    1593             :         return Operands[0];
    1594             :     }
    1595         490 :     if (auto *Op = dyn_cast<ConstantFP>(Operands[0])) {
    1596         205 :       if (IntrinsicID == Intrinsic::convert_to_fp16) {
    1597          15 :         APFloat Val(Op->getValueAPF());
    1598             : 
    1599           5 :         bool lost = false;
    1600           5 :         Val.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &lost);
    1601             : 
    1602          10 :         return ConstantInt::get(Ty->getContext(), Val.bitcastToAPInt());
    1603             :       }
    1604             : 
    1605         521 :       if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
    1606             :         return nullptr;
    1607             : 
    1608         200 :       if (IntrinsicID == Intrinsic::round) {
    1609          54 :         APFloat V = Op->getValueAPF();
    1610          18 :         V.roundToIntegral(APFloat::rmNearestTiesToAway);
    1611          18 :         return ConstantFP::get(Ty->getContext(), V);
    1612             :       }
    1613             : 
    1614         182 :       if (IntrinsicID == Intrinsic::floor) {
    1615          24 :         APFloat V = Op->getValueAPF();
    1616           8 :         V.roundToIntegral(APFloat::rmTowardNegative);
    1617           8 :         return ConstantFP::get(Ty->getContext(), V);
    1618             :       }
    1619             : 
    1620         174 :       if (IntrinsicID == Intrinsic::ceil) {
    1621          54 :         APFloat V = Op->getValueAPF();
    1622          18 :         V.roundToIntegral(APFloat::rmTowardPositive);
    1623          18 :         return ConstantFP::get(Ty->getContext(), V);
    1624             :       }
    1625             : 
    1626         156 :       if (IntrinsicID == Intrinsic::trunc) {
    1627          24 :         APFloat V = Op->getValueAPF();
    1628           8 :         V.roundToIntegral(APFloat::rmTowardZero);
    1629           8 :         return ConstantFP::get(Ty->getContext(), V);
    1630             :       }
    1631             : 
    1632         148 :       if (IntrinsicID == Intrinsic::rint) {
    1633           6 :         APFloat V = Op->getValueAPF();
    1634           2 :         V.roundToIntegral(APFloat::rmNearestTiesToEven);
    1635           2 :         return ConstantFP::get(Ty->getContext(), V);
    1636             :       }
    1637             : 
    1638         146 :       if (IntrinsicID == Intrinsic::nearbyint) {
    1639          24 :         APFloat V = Op->getValueAPF();
    1640           8 :         V.roundToIntegral(APFloat::rmNearestTiesToEven);
    1641           8 :         return ConstantFP::get(Ty->getContext(), V);
    1642             :       }
    1643             : 
    1644             :       /// We only fold functions with finite arguments. Folding NaN and inf is
    1645             :       /// likely to be aborted with an exception anyway, and some host libms
    1646             :       /// have known errors raising exceptions.
    1647         414 :       if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity())
    1648             :         return nullptr;
    1649             : 
    1650             :       /// Currently APFloat versions of these functions do not exist, so we use
    1651             :       /// the host native double versions.  Float versions are not called
    1652             :       /// directly but for all these it is true (float)(f((double)arg)) ==
    1653             :       /// f(arg).  Long double not supported yet.
    1654         135 :       double V = getValueAsDouble(Op);
    1655             : 
    1656         135 :       switch (IntrinsicID) {
    1657             :         default: break;
    1658          31 :         case Intrinsic::fabs:
    1659          31 :           return ConstantFoldFP(fabs, V, Ty);
    1660           0 :         case Intrinsic::log2:
    1661           0 :           return ConstantFoldFP(Log2, V, Ty);
    1662           0 :         case Intrinsic::log:
    1663           0 :           return ConstantFoldFP(log, V, Ty);
    1664           0 :         case Intrinsic::log10:
    1665           0 :           return ConstantFoldFP(log10, V, Ty);
    1666           0 :         case Intrinsic::exp:
    1667           0 :           return ConstantFoldFP(exp, V, Ty);
    1668           0 :         case Intrinsic::exp2:
    1669           0 :           return ConstantFoldFP(exp2, V, Ty);
    1670           1 :         case Intrinsic::sin:
    1671           1 :           return ConstantFoldFP(sin, V, Ty);
    1672           1 :         case Intrinsic::cos:
    1673           1 :           return ConstantFoldFP(cos, V, Ty);
    1674           0 :         case Intrinsic::sqrt:
    1675           0 :           return ConstantFoldFP(sqrt, V, Ty);
    1676             :       }
    1677             : 
    1678         102 :       if (!TLI)
    1679             :         return nullptr;
    1680             : 
    1681         204 :       char NameKeyChar = Name[0];
    1682         118 :       if (Name[0] == '_' && Name.size() > 2 && Name[1] == '_')
    1683          32 :         NameKeyChar = Name[2];
    1684             : 
    1685         102 :       switch (NameKeyChar) {
    1686          18 :       case 'a':
    1687          24 :         if ((Name == "acos" && TLI->has(LibFunc_acos)) ||
    1688          22 :             (Name == "acosf" && TLI->has(LibFunc_acosf)) ||
    1689          16 :             (Name == "__acos_finite" && TLI->has(LibFunc_acos_finite)) ||
    1690          15 :             (Name == "__acosf_finite" && TLI->has(LibFunc_acosf_finite)))
    1691           6 :           return ConstantFoldFP(acos, V, Ty);
    1692          16 :         else if ((Name == "asin" && TLI->has(LibFunc_asin)) ||
    1693          15 :                  (Name == "asinf" && TLI->has(LibFunc_asinf)) ||
    1694          12 :                  (Name == "__asin_finite" && TLI->has(LibFunc_asin_finite)) ||
    1695          11 :                  (Name == "__asinf_finite" && TLI->has(LibFunc_asinf_finite)))
    1696           4 :           return ConstantFoldFP(asin, V, Ty);
    1697          12 :         else if ((Name == "atan" && TLI->has(LibFunc_atan)) ||
    1698          11 :                  (Name == "atanf" && TLI->has(LibFunc_atanf)))
    1699           2 :           return ConstantFoldFP(atan, V, Ty);
    1700             :         break;
    1701          15 :       case 'c':
    1702          19 :         if ((Name == "ceil" && TLI->has(LibFunc_ceil)) ||
    1703          18 :             (Name == "ceilf" && TLI->has(LibFunc_ceilf)))
    1704           2 :           return ConstantFoldFP(ceil, V, Ty);
    1705          19 :         else if ((Name == "cos" && TLI->has(LibFunc_cos)) ||
    1706          15 :                  (Name == "cosf" && TLI->has(LibFunc_cosf)))
    1707           3 :           return ConstantFoldFP(cos, V, Ty);
    1708          14 :         else if ((Name == "cosh" && TLI->has(LibFunc_cosh)) ||
    1709          13 :                  (Name == "coshf" && TLI->has(LibFunc_coshf)) ||
    1710          10 :                  (Name == "__cosh_finite" && TLI->has(LibFunc_cosh_finite)) ||
    1711           9 :                  (Name == "__coshf_finite" && TLI->has(LibFunc_coshf_finite)))
    1712           4 :           return ConstantFoldFP(cosh, V, Ty);
    1713             :         break;
    1714          14 :       case 'e':
    1715          18 :         if ((Name == "exp" && TLI->has(LibFunc_exp)) ||
    1716          17 :             (Name == "expf" && TLI->has(LibFunc_expf)) ||
    1717          14 :             (Name == "__exp_finite" && TLI->has(LibFunc_exp_finite)) ||
    1718          13 :             (Name == "__expf_finite" && TLI->has(LibFunc_expf_finite)))
    1719           4 :           return ConstantFoldFP(exp, V, Ty);
    1720          18 :         if ((Name == "exp2" && TLI->has(LibFunc_exp2)) ||
    1721          12 :             (Name == "exp2f" && TLI->has(LibFunc_exp2f)) ||
    1722           9 :             (Name == "__exp2_finite" && TLI->has(LibFunc_exp2_finite)) ||
    1723           8 :             (Name == "__exp2f_finite" && TLI->has(LibFunc_exp2f_finite)))
    1724             :           // Constant fold exp2(x) as pow(2,x) in case the host doesn't have a
    1725             :           // C99 library.
    1726           5 :           return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
    1727             :         break;
    1728          10 :       case 'f':
    1729          18 :         if ((Name == "fabs" && TLI->has(LibFunc_fabs)) ||
    1730          11 :             (Name == "fabsf" && TLI->has(LibFunc_fabsf)))
    1731           4 :           return ConstantFoldFP(fabs, V, Ty);
    1732          10 :         else if ((Name == "floor" && TLI->has(LibFunc_floor)) ||
    1733           9 :                  (Name == "floorf" && TLI->has(LibFunc_floorf)))
    1734           2 :           return ConstantFoldFP(floor, V, Ty);
    1735             :         break;
    1736          14 :       case 'l':
    1737          22 :         if ((Name == "log" && V > 0 && TLI->has(LibFunc_log)) ||
    1738          15 :             (Name == "logf" && V > 0 && TLI->has(LibFunc_logf)) ||
    1739          12 :             (Name == "__log_finite" && V > 0 &&
    1740           1 :               TLI->has(LibFunc_log_finite)) ||
    1741          11 :             (Name == "__logf_finite" && V > 0 &&
    1742           1 :               TLI->has(LibFunc_logf_finite)))
    1743           6 :           return ConstantFoldFP(log, V, Ty);
    1744          12 :         else if ((Name == "log10" && V > 0 && TLI->has(LibFunc_log10)) ||
    1745          11 :                  (Name == "log10f" && V > 0 && TLI->has(LibFunc_log10f)) ||
    1746           8 :                  (Name == "__log10_finite" && V > 0 &&
    1747           1 :                    TLI->has(LibFunc_log10_finite)) ||
    1748           7 :                  (Name == "__log10f_finite" && V > 0 &&
    1749           1 :                    TLI->has(LibFunc_log10f_finite)))
    1750           4 :           return ConstantFoldFP(log10, V, Ty);
    1751             :         break;
    1752           4 :       case 'r':
    1753           8 :         if ((Name == "round" && TLI->has(LibFunc_round)) ||
    1754           7 :             (Name == "roundf" && TLI->has(LibFunc_roundf)))
    1755           2 :           return ConstantFoldFP(round, V, Ty);
    1756             :         break;
    1757          19 :       case 's':
    1758          27 :         if ((Name == "sin" && TLI->has(LibFunc_sin)) ||
    1759          20 :             (Name == "sinf" && TLI->has(LibFunc_sinf)))
    1760           4 :           return ConstantFoldFP(sin, V, Ty);
    1761          19 :         else if ((Name == "sinh" && TLI->has(LibFunc_sinh)) ||
    1762          18 :                  (Name == "sinhf" && TLI->has(LibFunc_sinhf)) ||
    1763          15 :                  (Name == "__sinh_finite" && TLI->has(LibFunc_sinh_finite)) ||
    1764          14 :                  (Name == "__sinhf_finite" && TLI->has(LibFunc_sinhf_finite)))
    1765           4 :           return ConstantFoldFP(sinh, V, Ty);
    1766          21 :         else if ((Name == "sqrt" && V >= 0 && TLI->has(LibFunc_sqrt)) ||
    1767          11 :                  (Name == "sqrtf" && V >= 0 && TLI->has(LibFunc_sqrtf)))
    1768           5 :           return ConstantFoldFP(sqrt, V, Ty);
    1769             :         break;
    1770           8 :       case 't':
    1771          12 :         if ((Name == "tan" && TLI->has(LibFunc_tan)) ||
    1772          11 :             (Name == "tanf" && TLI->has(LibFunc_tanf)))
    1773           2 :           return ConstantFoldFP(tan, V, Ty);
    1774          10 :         else if ((Name == "tanh" && TLI->has(LibFunc_tanh)) ||
    1775           9 :                  (Name == "tanhf" && TLI->has(LibFunc_tanhf)))
    1776           2 :           return ConstantFoldFP(tanh, V, Ty);
    1777             :         break;
    1778             :       default:
    1779             :         break;
    1780             :       }
    1781             :       return nullptr;
    1782             :     }
    1783             : 
    1784         113 :     if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) {
    1785          33 :       switch (IntrinsicID) {
    1786           4 :       case Intrinsic::bswap:
    1787           8 :         return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap());
    1788           4 :       case Intrinsic::ctpop:
    1789           8 :         return ConstantInt::get(Ty, Op->getValue().countPopulation());
    1790          15 :       case Intrinsic::bitreverse:
    1791          30 :         return ConstantInt::get(Ty->getContext(), Op->getValue().reverseBits());
    1792          10 :       case Intrinsic::convert_from_fp16: {
    1793          30 :         APFloat Val(APFloat::IEEEhalf(), Op->getValue());
    1794             : 
    1795          10 :         bool lost = false;
    1796          10 :         APFloat::opStatus status = Val.convert(
    1797          10 :             Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &lost);
    1798             : 
    1799             :         // Conversion is always precise.
    1800             :         (void)status;
    1801             :         assert(status == APFloat::opOK && !lost &&
    1802             :                "Precision lost during fp16 constfolding");
    1803             : 
    1804          10 :         return ConstantFP::get(Ty->getContext(), Val);
    1805             :       }
    1806             :       default:
    1807             :         return nullptr;
    1808             :       }
    1809             :     }
    1810             : 
    1811             :     // Support ConstantVector in case we have an Undef in the top.
    1812         101 :     if (isa<ConstantVector>(Operands[0]) ||
    1813           7 :         isa<ConstantDataVector>(Operands[0])) {
    1814          80 :       auto *Op = cast<Constant>(Operands[0]);
    1815             :       switch (IntrinsicID) {
    1816             :       default: break;
    1817          20 :       case Intrinsic::x86_sse_cvtss2si:
    1818             :       case Intrinsic::x86_sse_cvtss2si64:
    1819             :       case Intrinsic::x86_sse2_cvtsd2si:
    1820             :       case Intrinsic::x86_sse2_cvtsd2si64:
    1821             :         if (ConstantFP *FPOp =
    1822          40 :                 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
    1823          20 :           return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
    1824          20 :                                              /*roundTowardZero=*/false, Ty);
    1825             :         break;
    1826          20 :       case Intrinsic::x86_sse_cvttss2si:
    1827             :       case Intrinsic::x86_sse_cvttss2si64:
    1828             :       case Intrinsic::x86_sse2_cvttsd2si:
    1829             :       case Intrinsic::x86_sse2_cvttsd2si64:
    1830             :         if (ConstantFP *FPOp =
    1831          40 :                 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
    1832          20 :           return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
    1833          20 :                                              /*roundTowardZero=*/true, Ty);
    1834             :         break;
    1835             :       }
    1836             :     }
    1837             : 
    1838             :     return nullptr;
    1839             :   }
    1840             : 
    1841         465 :   if (Operands.size() == 2) {
    1842         551 :     if (auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
    1843         289 :       if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
    1844             :         return nullptr;
    1845         125 :       double Op1V = getValueAsDouble(Op1);
    1846             : 
    1847         240 :       if (auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
    1848         115 :         if (Op2->getType() != Op1->getType())
    1849             :           return nullptr;
    1850             : 
    1851         115 :         double Op2V = getValueAsDouble(Op2);
    1852         115 :         if (IntrinsicID == Intrinsic::pow) {
    1853           2 :           return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
    1854             :         }
    1855         113 :         if (IntrinsicID == Intrinsic::copysign) {
    1856          18 :           APFloat V1 = Op1->getValueAPF();
    1857           6 :           const APFloat &V2 = Op2->getValueAPF();
    1858           6 :           V1.copySign(V2);
    1859           6 :           return ConstantFP::get(Ty->getContext(), V1);
    1860             :         }
    1861             : 
    1862         107 :         if (IntrinsicID == Intrinsic::minnum) {
    1863          42 :           const APFloat &C1 = Op1->getValueAPF();
    1864          42 :           const APFloat &C2 = Op2->getValueAPF();
    1865          84 :           return ConstantFP::get(Ty->getContext(), minnum(C1, C2));
    1866             :         }
    1867             : 
    1868          65 :         if (IntrinsicID == Intrinsic::maxnum) {
    1869          33 :           const APFloat &C1 = Op1->getValueAPF();
    1870          33 :           const APFloat &C2 = Op2->getValueAPF();
    1871          66 :           return ConstantFP::get(Ty->getContext(), maxnum(C1, C2));
    1872             :         }
    1873             : 
    1874          32 :         if (!TLI)
    1875             :           return nullptr;
    1876          52 :         if ((Name == "pow" && TLI->has(LibFunc_pow)) ||
    1877          43 :             (Name == "powf" && TLI->has(LibFunc_powf)) ||
    1878          16 :             (Name == "__pow_finite" && TLI->has(LibFunc_pow_finite)) ||
    1879          15 :             (Name == "__powf_finite" && TLI->has(LibFunc_powf_finite)))
    1880          20 :           return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
    1881          16 :         if ((Name == "fmod" && TLI->has(LibFunc_fmod)) ||
    1882          15 :             (Name == "fmodf" && TLI->has(LibFunc_fmodf)))
    1883           2 :           return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
    1884          14 :         if ((Name == "atan2" && TLI->has(LibFunc_atan2)) ||
    1885          13 :             (Name == "atan2f" && TLI->has(LibFunc_atan2f)) ||
    1886          10 :             (Name == "__atan2_finite" && TLI->has(LibFunc_atan2_finite)) ||
    1887           9 :             (Name == "__atan2f_finite" && TLI->has(LibFunc_atan2f_finite)))
    1888           4 :           return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
    1889          16 :       } else if (auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
    1890          12 :         if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy())
    1891           0 :           return ConstantFP::get(Ty->getContext(),
    1892           0 :                                  APFloat((float)std::pow((float)Op1V,
    1893           0 :                                                  (int)Op2C->getZExtValue())));
    1894          12 :         if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy())
    1895           0 :           return ConstantFP::get(Ty->getContext(),
    1896           0 :                                  APFloat((float)std::pow((float)Op1V,
    1897           0 :                                                  (int)Op2C->getZExtValue())));
    1898          12 :         if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy())
    1899           6 :           return ConstantFP::get(Ty->getContext(),
    1900          18 :                                  APFloat((double)std::pow((double)Op1V,
    1901          12 :                                                    (int)Op2C->getZExtValue())));
    1902             :       }
    1903             :       return nullptr;
    1904             :     }
    1905             : 
    1906         592 :     if (auto *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
    1907         582 :       if (auto *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
    1908         291 :         switch (IntrinsicID) {
    1909             :         default: break;
    1910          32 :         case Intrinsic::sadd_with_overflow:
    1911             :         case Intrinsic::uadd_with_overflow:
    1912             :         case Intrinsic::ssub_with_overflow:
    1913             :         case Intrinsic::usub_with_overflow:
    1914             :         case Intrinsic::smul_with_overflow:
    1915             :         case Intrinsic::umul_with_overflow: {
    1916          64 :           APInt Res;
    1917             :           bool Overflow;
    1918          32 :           switch (IntrinsicID) {
    1919           0 :           default: llvm_unreachable("Invalid case");
    1920           9 :           case Intrinsic::sadd_with_overflow:
    1921          36 :             Res = Op1->getValue().sadd_ov(Op2->getValue(), Overflow);
    1922           9 :             break;
    1923          10 :           case Intrinsic::uadd_with_overflow:
    1924          40 :             Res = Op1->getValue().uadd_ov(Op2->getValue(), Overflow);
    1925          10 :             break;
    1926           7 :           case Intrinsic::ssub_with_overflow:
    1927          28 :             Res = Op1->getValue().ssub_ov(Op2->getValue(), Overflow);
    1928           7 :             break;
    1929           2 :           case Intrinsic::usub_with_overflow:
    1930           8 :             Res = Op1->getValue().usub_ov(Op2->getValue(), Overflow);
    1931           2 :             break;
    1932           1 :           case Intrinsic::smul_with_overflow:
    1933           4 :             Res = Op1->getValue().smul_ov(Op2->getValue(), Overflow);
    1934           1 :             break;
    1935           3 :           case Intrinsic::umul_with_overflow:
    1936          12 :             Res = Op1->getValue().umul_ov(Op2->getValue(), Overflow);
    1937           3 :             break;
    1938             :           }
    1939             :           Constant *Ops[] = {
    1940          32 :             ConstantInt::get(Ty->getContext(), Res),
    1941          32 :             ConstantInt::get(Type::getInt1Ty(Ty->getContext()), Overflow)
    1942          64 :           };
    1943          64 :           return ConstantStruct::get(cast<StructType>(Ty), Ops);
    1944             :         }
    1945         105 :         case Intrinsic::cttz:
    1946         203 :           if (Op2->isOne() && Op1->isZero()) // cttz(0, 1) is undef.
    1947          39 :             return UndefValue::get(Ty);
    1948          66 :           return ConstantInt::get(Ty, Op1->getValue().countTrailingZeros());
    1949         154 :         case Intrinsic::ctlz:
    1950         262 :           if (Op2->isOne() && Op1->isZero()) // ctlz(0, 1) is undef.
    1951          74 :             return UndefValue::get(Ty);
    1952          80 :           return ConstantInt::get(Ty, Op1->getValue().countLeadingZeros());
    1953             :         }
    1954             :       }
    1955             : 
    1956             :       return nullptr;
    1957             :     }
    1958             :     return nullptr;
    1959             :   }
    1960             : 
    1961          39 :   if (Operands.size() != 3)
    1962             :     return nullptr;
    1963             : 
    1964          50 :   if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
    1965          44 :     if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
    1966          44 :       if (const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
    1967          22 :         switch (IntrinsicID) {
    1968             :         default: break;
    1969          22 :         case Intrinsic::fma:
    1970             :         case Intrinsic::fmuladd: {
    1971          66 :           APFloat V = Op1->getValueAPF();
    1972          44 :           APFloat::opStatus s = V.fusedMultiplyAdd(Op2->getValueAPF(),
    1973             :                                                    Op3->getValueAPF(),
    1974          22 :                                                    APFloat::rmNearestTiesToEven);
    1975          22 :           if (s != APFloat::opInvalidOp)
    1976          22 :             return ConstantFP::get(Ty->getContext(), V);
    1977             : 
    1978             :           return nullptr;
    1979             :         }
    1980             :         }
    1981             :       }
    1982             :     }
    1983             :   }
    1984             : 
    1985             :   return nullptr;
    1986             : }
    1987             : 
    1988          21 : Constant *ConstantFoldVectorCall(StringRef Name, unsigned IntrinsicID,
    1989             :                                  VectorType *VTy, ArrayRef<Constant *> Operands,
    1990             :                                  const DataLayout &DL,
    1991             :                                  const TargetLibraryInfo *TLI) {
    1992          63 :   SmallVector<Constant *, 4> Result(VTy->getNumElements());
    1993          63 :   SmallVector<Constant *, 4> Lane(Operands.size());
    1994          21 :   Type *Ty = VTy->getElementType();
    1995             : 
    1996          21 :   if (IntrinsicID == Intrinsic::masked_load) {
    1997           2 :     auto *SrcPtr = Operands[0];
    1998           2 :     auto *Mask = Operands[2];
    1999           2 :     auto *Passthru = Operands[3];
    2000             : 
    2001           2 :     Constant *VecData = ConstantFoldLoadFromConstPtr(SrcPtr, VTy, DL);
    2002             : 
    2003           4 :     SmallVector<Constant *, 32> NewElements;
    2004          18 :     for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
    2005          16 :       auto *MaskElt = Mask->getAggregateElement(I);
    2006          16 :       if (!MaskElt)
    2007             :         break;
    2008          16 :       auto *PassthruElt = Passthru->getAggregateElement(I);
    2009          16 :       auto *VecElt = VecData ? VecData->getAggregateElement(I) : nullptr;
    2010          32 :       if (isa<UndefValue>(MaskElt)) {
    2011           0 :         if (PassthruElt)
    2012           0 :           NewElements.push_back(PassthruElt);
    2013           0 :         else if (VecElt)
    2014           0 :           NewElements.push_back(VecElt);
    2015             :         else
    2016           0 :           return nullptr;
    2017             :       }
    2018          16 :       if (MaskElt->isNullValue()) {
    2019           4 :         if (!PassthruElt)
    2020             :           return nullptr;
    2021           4 :         NewElements.push_back(PassthruElt);
    2022          12 :       } else if (MaskElt->isOneValue()) {
    2023          12 :         if (!VecElt)
    2024             :           return nullptr;
    2025          12 :         NewElements.push_back(VecElt);
    2026             :       } else {
    2027             :         return nullptr;
    2028             :       }
    2029             :     }
    2030           2 :     if (NewElements.size() != VTy->getNumElements())
    2031             :       return nullptr;
    2032           2 :     return ConstantVector::get(NewElements);
    2033             :   }
    2034             : 
    2035          63 :   for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
    2036             :     // Gather a column of constants.
    2037         128 :     for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) {
    2038             :       // These intrinsics use a scalar type for their second argument.
    2039         136 :       if (J == 1 &&
    2040          50 :           (IntrinsicID == Intrinsic::cttz || IntrinsicID == Intrinsic::ctlz ||
    2041             :            IntrinsicID == Intrinsic::powi)) {
    2042          60 :         Lane[J] = Operands[J];
    2043          20 :         continue;
    2044             :       }
    2045             : 
    2046         126 :       Constant *Agg = Operands[J]->getAggregateElement(I);
    2047          63 :       if (!Agg)
    2048             :         return nullptr;
    2049             : 
    2050         126 :       Lane[J] = Agg;
    2051             :     }
    2052             : 
    2053             :     // Use the regular scalar folding to simplify this column.
    2054          45 :     Constant *Folded = ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI);
    2055          45 :     if (!Folded)
    2056             :       return nullptr;
    2057          88 :     Result[I] = Folded;
    2058             :   }
    2059             : 
    2060          18 :   return ConstantVector::get(Result);
    2061             : }
    2062             : 
    2063             : } // end anonymous namespace
    2064             : 
    2065             : Constant *
    2066         731 : llvm::ConstantFoldCall(ImmutableCallSite CS, Function *F,
    2067             :                        ArrayRef<Constant *> Operands,
    2068             :                        const TargetLibraryInfo *TLI) {
    2069         731 :   if (CS.isNoBuiltin() || CS.isStrictFP())
    2070             :     return nullptr;
    2071        1462 :   if (!F->hasName())
    2072             :     return nullptr;
    2073         731 :   StringRef Name = F->getName();
    2074             : 
    2075         731 :   Type *Ty = F->getReturnType();
    2076             : 
    2077          21 :   if (auto *VTy = dyn_cast<VectorType>(Ty))
    2078          21 :     return ConstantFoldVectorCall(Name, F->getIntrinsicID(), VTy, Operands,
    2079          21 :                                   F->getParent()->getDataLayout(), TLI);
    2080             : 
    2081         710 :   return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI);
    2082             : }
    2083             : 
    2084     4384052 : bool llvm::isMathLibCallNoop(CallSite CS, const TargetLibraryInfo *TLI) {
    2085             :   // FIXME: Refactor this code; this duplicates logic in LibCallsShrinkWrap
    2086             :   // (and to some extent ConstantFoldScalarCall).
    2087     4384052 :   if (CS.isNoBuiltin() || CS.isStrictFP())
    2088             :     return false;
    2089     4319817 :   Function *F = CS.getCalledFunction();
    2090             :   if (!F)
    2091             :     return false;
    2092             : 
    2093             :   LibFunc Func;
    2094     8484563 :   if (!TLI || !TLI->getLibFunc(*F, Func))
    2095             :     return false;
    2096             : 
    2097      103072 :   if (CS.getNumArgOperands() == 1) {
    2098       95136 :     if (ConstantFP *OpC = dyn_cast<ConstantFP>(CS.getArgOperand(0))) {
    2099          13 :       const APFloat &Op = OpC->getValueAPF();
    2100          13 :       switch (Func) {
    2101           3 :       case LibFunc_logl:
    2102             :       case LibFunc_log:
    2103             :       case LibFunc_logf:
    2104             :       case LibFunc_log2l:
    2105             :       case LibFunc_log2:
    2106             :       case LibFunc_log2f:
    2107             :       case LibFunc_log10l:
    2108             :       case LibFunc_log10:
    2109             :       case LibFunc_log10f:
    2110           8 :         return Op.isNaN() || (!Op.isZero() && !Op.isNegative());
    2111             : 
    2112           2 :       case LibFunc_expl:
    2113             :       case LibFunc_exp:
    2114             :       case LibFunc_expf:
    2115             :         // FIXME: These boundaries are slightly conservative.
    2116           4 :         if (OpC->getType()->isDoubleTy())
    2117           6 :           return Op.compare(APFloat(-745.0)) != APFloat::cmpLessThan &&
    2118           5 :                  Op.compare(APFloat(709.0)) != APFloat::cmpGreaterThan;
    2119           0 :         if (OpC->getType()->isFloatTy())
    2120           0 :           return Op.compare(APFloat(-103.0f)) != APFloat::cmpLessThan &&
    2121           0 :                  Op.compare(APFloat(88.0f)) != APFloat::cmpGreaterThan;
    2122             :         break;
    2123             : 
    2124           0 :       case LibFunc_exp2l:
    2125             :       case LibFunc_exp2:
    2126             :       case LibFunc_exp2f:
    2127             :         // FIXME: These boundaries are slightly conservative.
    2128           0 :         if (OpC->getType()->isDoubleTy())
    2129           0 :           return Op.compare(APFloat(-1074.0)) != APFloat::cmpLessThan &&
    2130           0 :                  Op.compare(APFloat(1023.0)) != APFloat::cmpGreaterThan;
    2131           0 :         if (OpC->getType()->isFloatTy())
    2132           0 :           return Op.compare(APFloat(-149.0f)) != APFloat::cmpLessThan &&
    2133           0 :                  Op.compare(APFloat(127.0f)) != APFloat::cmpGreaterThan;
    2134             :         break;
    2135             : 
    2136           3 :       case LibFunc_sinl:
    2137             :       case LibFunc_sin:
    2138             :       case LibFunc_sinf:
    2139             :       case LibFunc_cosl:
    2140             :       case LibFunc_cos:
    2141             :       case LibFunc_cosf:
    2142           3 :         return !Op.isInfinity();
    2143             : 
    2144           0 :       case LibFunc_tanl:
    2145             :       case LibFunc_tan:
    2146             :       case LibFunc_tanf: {
    2147             :         // FIXME: Stop using the host math library.
    2148             :         // FIXME: The computation isn't done in the right precision.
    2149           0 :         Type *Ty = OpC->getType();
    2150           0 :         if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) {
    2151           0 :           double OpV = getValueAsDouble(OpC);
    2152           0 :           return ConstantFoldFP(tan, OpV, Ty) != nullptr;
    2153             :         }
    2154             :         break;
    2155             :       }
    2156             : 
    2157           3 :       case LibFunc_asinl:
    2158             :       case LibFunc_asin:
    2159             :       case LibFunc_asinf:
    2160             :       case LibFunc_acosl:
    2161             :       case LibFunc_acos:
    2162             :       case LibFunc_acosf:
    2163           6 :         return Op.compare(APFloat(Op.getSemantics(), "-1")) !=
    2164           9 :                    APFloat::cmpLessThan &&
    2165           8 :                Op.compare(APFloat(Op.getSemantics(), "1")) !=
    2166             :                    APFloat::cmpGreaterThan;
    2167             : 
    2168           0 :       case LibFunc_sinh:
    2169             :       case LibFunc_cosh:
    2170             :       case LibFunc_sinhf:
    2171             :       case LibFunc_coshf:
    2172             :       case LibFunc_sinhl:
    2173             :       case LibFunc_coshl:
    2174             :         // FIXME: These boundaries are slightly conservative.
    2175           0 :         if (OpC->getType()->isDoubleTy())
    2176           0 :           return Op.compare(APFloat(-710.0)) != APFloat::cmpLessThan &&
    2177           0 :                  Op.compare(APFloat(710.0)) != APFloat::cmpGreaterThan;
    2178           0 :         if (OpC->getType()->isFloatTy())
    2179           0 :           return Op.compare(APFloat(-89.0f)) != APFloat::cmpLessThan &&
    2180           0 :                  Op.compare(APFloat(89.0f)) != APFloat::cmpGreaterThan;
    2181             :         break;
    2182             : 
    2183           0 :       case LibFunc_sqrtl:
    2184             :       case LibFunc_sqrt:
    2185             :       case LibFunc_sqrtf:
    2186           0 :         return Op.isNaN() || Op.isZero() || !Op.isNegative();
    2187             : 
    2188             :       // FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p,
    2189             :       // maybe others?
    2190             :       default:
    2191             :         break;
    2192             :       }
    2193             :     }
    2194             :   }
    2195             : 
    2196      103061 :   if (CS.getNumArgOperands() == 2) {
    2197        4776 :     ConstantFP *Op0C = dyn_cast<ConstantFP>(CS.getArgOperand(0));
    2198        4776 :     ConstantFP *Op1C = dyn_cast<ConstantFP>(CS.getArgOperand(1));
    2199        2388 :     if (Op0C && Op1C) {
    2200           3 :       const APFloat &Op0 = Op0C->getValueAPF();
    2201           3 :       const APFloat &Op1 = Op1C->getValueAPF();
    2202             : 
    2203           3 :       switch (Func) {
    2204           1 :       case LibFunc_powl:
    2205             :       case LibFunc_pow:
    2206             :       case LibFunc_powf: {
    2207             :         // FIXME: Stop using the host math library.
    2208             :         // FIXME: The computation isn't done in the right precision.
    2209           1 :         Type *Ty = Op0C->getType();
    2210           1 :         if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) {
    2211           1 :           if (Ty == Op1C->getType()) {
    2212           1 :             double Op0V = getValueAsDouble(Op0C);
    2213           1 :             double Op1V = getValueAsDouble(Op1C);
    2214           1 :             return ConstantFoldBinaryFP(pow, Op0V, Op1V, Ty) != nullptr;
    2215             :           }
    2216             :         }
    2217             :         break;
    2218             :       }
    2219             : 
    2220           2 :       case LibFunc_fmodl:
    2221             :       case LibFunc_fmod:
    2222             :       case LibFunc_fmodf:
    2223           5 :         return Op0.isNaN() || Op1.isNaN() ||
    2224           1 :                (!Op0.isInfinity() && !Op1.isZero());
    2225             : 
    2226             :       default:
    2227             :         break;
    2228             :       }
    2229             :     }
    2230             :   }
    2231             : 
    2232             :   return false;
    2233             : }

Generated by: LCOV version 1.13