LLVM API Documentation

ConstantFolding.cpp
Go to the documentation of this file.
00001 //===-- ConstantFolding.cpp - Fold instructions into constants ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines routines for folding instructions into constants.
00011 //
00012 // Also, to supplement the basic IR ConstantExpr simplifications,
00013 // this file defines some additional folding routines that can make use of
00014 // DataLayout information. These functions cannot go in IR due to library
00015 // dependency issues.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "llvm/Analysis/ConstantFolding.h"
00020 #include "llvm/ADT/SmallPtrSet.h"
00021 #include "llvm/ADT/SmallVector.h"
00022 #include "llvm/ADT/StringMap.h"
00023 #include "llvm/Analysis/ValueTracking.h"
00024 #include "llvm/Config/config.h"
00025 #include "llvm/IR/Constants.h"
00026 #include "llvm/IR/DataLayout.h"
00027 #include "llvm/IR/DerivedTypes.h"
00028 #include "llvm/IR/Function.h"
00029 #include "llvm/IR/GetElementPtrTypeIterator.h"
00030 #include "llvm/IR/GlobalVariable.h"
00031 #include "llvm/IR/Instructions.h"
00032 #include "llvm/IR/Intrinsics.h"
00033 #include "llvm/IR/Operator.h"
00034 #include "llvm/Support/ErrorHandling.h"
00035 #include "llvm/Support/MathExtras.h"
00036 #include "llvm/Target/TargetLibraryInfo.h"
00037 #include <cerrno>
00038 #include <cmath>
00039 
00040 #ifdef HAVE_FENV_H
00041 #include <fenv.h>
00042 #endif
00043 
00044 using namespace llvm;
00045 
00046 //===----------------------------------------------------------------------===//
00047 // Constant Folding internal helper functions
00048 //===----------------------------------------------------------------------===//
00049 
00050 /// Constant fold bitcast, symbolically evaluating it with DataLayout.
00051 /// This always returns a non-null constant, but it may be a
00052 /// ConstantExpr if unfoldable.
00053 static Constant *FoldBitCast(Constant *C, Type *DestTy,
00054                              const DataLayout &TD) {
00055   // Catch the obvious splat cases.
00056   if (C->isNullValue() && !DestTy->isX86_MMXTy())
00057     return Constant::getNullValue(DestTy);
00058   if (C->isAllOnesValue() && !DestTy->isX86_MMXTy() &&
00059       !DestTy->isPtrOrPtrVectorTy()) // Don't get ones for ptr types!
00060     return Constant::getAllOnesValue(DestTy);
00061 
00062   // Handle a vector->integer cast.
00063   if (IntegerType *IT = dyn_cast<IntegerType>(DestTy)) {
00064     VectorType *VTy = dyn_cast<VectorType>(C->getType());
00065     if (!VTy)
00066       return ConstantExpr::getBitCast(C, DestTy);
00067 
00068     unsigned NumSrcElts = VTy->getNumElements();
00069     Type *SrcEltTy = VTy->getElementType();
00070 
00071     // If the vector is a vector of floating point, convert it to vector of int
00072     // to simplify things.
00073     if (SrcEltTy->isFloatingPointTy()) {
00074       unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
00075       Type *SrcIVTy =
00076         VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElts);
00077       // Ask IR to do the conversion now that #elts line up.
00078       C = ConstantExpr::getBitCast(C, SrcIVTy);
00079     }
00080 
00081     ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
00082     if (!CDV)
00083       return ConstantExpr::getBitCast(C, DestTy);
00084 
00085     // Now that we know that the input value is a vector of integers, just shift
00086     // and insert them into our result.
00087     unsigned BitShift = TD.getTypeAllocSizeInBits(SrcEltTy);
00088     APInt Result(IT->getBitWidth(), 0);
00089     for (unsigned i = 0; i != NumSrcElts; ++i) {
00090       Result <<= BitShift;
00091       if (TD.isLittleEndian())
00092         Result |= CDV->getElementAsInteger(NumSrcElts-i-1);
00093       else
00094         Result |= CDV->getElementAsInteger(i);
00095     }
00096 
00097     return ConstantInt::get(IT, Result);
00098   }
00099 
00100   // The code below only handles casts to vectors currently.
00101   VectorType *DestVTy = dyn_cast<VectorType>(DestTy);
00102   if (!DestVTy)
00103     return ConstantExpr::getBitCast(C, DestTy);
00104 
00105   // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
00106   // vector so the code below can handle it uniformly.
00107   if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) {
00108     Constant *Ops = C; // don't take the address of C!
00109     return FoldBitCast(ConstantVector::get(Ops), DestTy, TD);
00110   }
00111 
00112   // If this is a bitcast from constant vector -> vector, fold it.
00113   if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C))
00114     return ConstantExpr::getBitCast(C, DestTy);
00115 
00116   // If the element types match, IR can fold it.
00117   unsigned NumDstElt = DestVTy->getNumElements();
00118   unsigned NumSrcElt = C->getType()->getVectorNumElements();
00119   if (NumDstElt == NumSrcElt)
00120     return ConstantExpr::getBitCast(C, DestTy);
00121 
00122   Type *SrcEltTy = C->getType()->getVectorElementType();
00123   Type *DstEltTy = DestVTy->getElementType();
00124 
00125   // Otherwise, we're changing the number of elements in a vector, which
00126   // requires endianness information to do the right thing.  For example,
00127   //    bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
00128   // folds to (little endian):
00129   //    <4 x i32> <i32 0, i32 0, i32 1, i32 0>
00130   // and to (big endian):
00131   //    <4 x i32> <i32 0, i32 0, i32 0, i32 1>
00132 
00133   // First thing is first.  We only want to think about integer here, so if
00134   // we have something in FP form, recast it as integer.
00135   if (DstEltTy->isFloatingPointTy()) {
00136     // Fold to an vector of integers with same size as our FP type.
00137     unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits();
00138     Type *DestIVTy =
00139       VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt);
00140     // Recursively handle this integer conversion, if possible.
00141     C = FoldBitCast(C, DestIVTy, TD);
00142 
00143     // Finally, IR can handle this now that #elts line up.
00144     return ConstantExpr::getBitCast(C, DestTy);
00145   }
00146 
00147   // Okay, we know the destination is integer, if the input is FP, convert
00148   // it to integer first.
00149   if (SrcEltTy->isFloatingPointTy()) {
00150     unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
00151     Type *SrcIVTy =
00152       VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElt);
00153     // Ask IR to do the conversion now that #elts line up.
00154     C = ConstantExpr::getBitCast(C, SrcIVTy);
00155     // If IR wasn't able to fold it, bail out.
00156     if (!isa<ConstantVector>(C) &&  // FIXME: Remove ConstantVector.
00157         !isa<ConstantDataVector>(C))
00158       return C;
00159   }
00160 
00161   // Now we know that the input and output vectors are both integer vectors
00162   // of the same size, and that their #elements is not the same.  Do the
00163   // conversion here, which depends on whether the input or output has
00164   // more elements.
00165   bool isLittleEndian = TD.isLittleEndian();
00166 
00167   SmallVector<Constant*, 32> Result;
00168   if (NumDstElt < NumSrcElt) {
00169     // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
00170     Constant *Zero = Constant::getNullValue(DstEltTy);
00171     unsigned Ratio = NumSrcElt/NumDstElt;
00172     unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
00173     unsigned SrcElt = 0;
00174     for (unsigned i = 0; i != NumDstElt; ++i) {
00175       // Build each element of the result.
00176       Constant *Elt = Zero;
00177       unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
00178       for (unsigned j = 0; j != Ratio; ++j) {
00179         Constant *Src =dyn_cast<ConstantInt>(C->getAggregateElement(SrcElt++));
00180         if (!Src)  // Reject constantexpr elements.
00181           return ConstantExpr::getBitCast(C, DestTy);
00182 
00183         // Zero extend the element to the right size.
00184         Src = ConstantExpr::getZExt(Src, Elt->getType());
00185 
00186         // Shift it to the right place, depending on endianness.
00187         Src = ConstantExpr::getShl(Src,
00188                                    ConstantInt::get(Src->getType(), ShiftAmt));
00189         ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
00190 
00191         // Mix it in.
00192         Elt = ConstantExpr::getOr(Elt, Src);
00193       }
00194       Result.push_back(Elt);
00195     }
00196     return ConstantVector::get(Result);
00197   }
00198 
00199   // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
00200   unsigned Ratio = NumDstElt/NumSrcElt;
00201   unsigned DstBitSize = TD.getTypeSizeInBits(DstEltTy);
00202 
00203   // Loop over each source value, expanding into multiple results.
00204   for (unsigned i = 0; i != NumSrcElt; ++i) {
00205     Constant *Src = dyn_cast<ConstantInt>(C->getAggregateElement(i));
00206     if (!Src)  // Reject constantexpr elements.
00207       return ConstantExpr::getBitCast(C, DestTy);
00208 
00209     unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
00210     for (unsigned j = 0; j != Ratio; ++j) {
00211       // Shift the piece of the value into the right place, depending on
00212       // endianness.
00213       Constant *Elt = ConstantExpr::getLShr(Src,
00214                                   ConstantInt::get(Src->getType(), ShiftAmt));
00215       ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
00216 
00217       // Truncate the element to an integer with the same pointer size and
00218       // convert the element back to a pointer using a inttoptr.
00219       if (DstEltTy->isPointerTy()) {
00220         IntegerType *DstIntTy = Type::getIntNTy(C->getContext(), DstBitSize);
00221         Constant *CE = ConstantExpr::getTrunc(Elt, DstIntTy);
00222         Result.push_back(ConstantExpr::getIntToPtr(CE, DstEltTy));
00223         continue;
00224       }
00225 
00226       // Truncate and remember this piece.
00227       Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy));
00228     }
00229   }
00230 
00231   return ConstantVector::get(Result);
00232 }
00233 
00234 
00235 /// If this constant is a constant offset from a global, return the global and
00236 /// the constant. Because of constantexprs, this function is recursive.
00237 static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
00238                                        APInt &Offset, const DataLayout &TD) {
00239   // Trivial case, constant is the global.
00240   if ((GV = dyn_cast<GlobalValue>(C))) {
00241     unsigned BitWidth = TD.getPointerTypeSizeInBits(GV->getType());
00242     Offset = APInt(BitWidth, 0);
00243     return true;
00244   }
00245 
00246   // Otherwise, if this isn't a constant expr, bail out.
00247   ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
00248   if (!CE) return false;
00249 
00250   // Look through ptr->int and ptr->ptr casts.
00251   if (CE->getOpcode() == Instruction::PtrToInt ||
00252       CE->getOpcode() == Instruction::BitCast ||
00253       CE->getOpcode() == Instruction::AddrSpaceCast)
00254     return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD);
00255 
00256   // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
00257   GEPOperator *GEP = dyn_cast<GEPOperator>(CE);
00258   if (!GEP)
00259     return false;
00260 
00261   unsigned BitWidth = TD.getPointerTypeSizeInBits(GEP->getType());
00262   APInt TmpOffset(BitWidth, 0);
00263 
00264   // If the base isn't a global+constant, we aren't either.
00265   if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, TmpOffset, TD))
00266     return false;
00267 
00268   // Otherwise, add any offset that our operands provide.
00269   if (!GEP->accumulateConstantOffset(TD, TmpOffset))
00270     return false;
00271 
00272   Offset = TmpOffset;
00273   return true;
00274 }
00275 
00276 /// Recursive helper to read bits out of global. C is the constant being copied
00277 /// out of. ByteOffset is an offset into C. CurPtr is the pointer to copy
00278 /// results into and BytesLeft is the number of bytes left in
00279 /// the CurPtr buffer. TD is the target data.
00280 static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset,
00281                                unsigned char *CurPtr, unsigned BytesLeft,
00282                                const DataLayout &TD) {
00283   assert(ByteOffset <= TD.getTypeAllocSize(C->getType()) &&
00284          "Out of range access");
00285 
00286   // If this element is zero or undefined, we can just return since *CurPtr is
00287   // zero initialized.
00288   if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
00289     return true;
00290 
00291   if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
00292     if (CI->getBitWidth() > 64 ||
00293         (CI->getBitWidth() & 7) != 0)
00294       return false;
00295 
00296     uint64_t Val = CI->getZExtValue();
00297     unsigned IntBytes = unsigned(CI->getBitWidth()/8);
00298 
00299     for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
00300       int n = ByteOffset;
00301       if (!TD.isLittleEndian())
00302         n = IntBytes - n - 1;
00303       CurPtr[i] = (unsigned char)(Val >> (n * 8));
00304       ++ByteOffset;
00305     }
00306     return true;
00307   }
00308 
00309   if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
00310     if (CFP->getType()->isDoubleTy()) {
00311       C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), TD);
00312       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
00313     }
00314     if (CFP->getType()->isFloatTy()){
00315       C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), TD);
00316       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
00317     }
00318     if (CFP->getType()->isHalfTy()){
00319       C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), TD);
00320       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
00321     }
00322     return false;
00323   }
00324 
00325   if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
00326     const StructLayout *SL = TD.getStructLayout(CS->getType());
00327     unsigned Index = SL->getElementContainingOffset(ByteOffset);
00328     uint64_t CurEltOffset = SL->getElementOffset(Index);
00329     ByteOffset -= CurEltOffset;
00330 
00331     while (1) {
00332       // If the element access is to the element itself and not to tail padding,
00333       // read the bytes from the element.
00334       uint64_t EltSize = TD.getTypeAllocSize(CS->getOperand(Index)->getType());
00335 
00336       if (ByteOffset < EltSize &&
00337           !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
00338                               BytesLeft, TD))
00339         return false;
00340 
00341       ++Index;
00342 
00343       // Check to see if we read from the last struct element, if so we're done.
00344       if (Index == CS->getType()->getNumElements())
00345         return true;
00346 
00347       // If we read all of the bytes we needed from this element we're done.
00348       uint64_t NextEltOffset = SL->getElementOffset(Index);
00349 
00350       if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
00351         return true;
00352 
00353       // Move to the next element of the struct.
00354       CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
00355       BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
00356       ByteOffset = 0;
00357       CurEltOffset = NextEltOffset;
00358     }
00359     // not reached.
00360   }
00361 
00362   if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
00363       isa<ConstantDataSequential>(C)) {
00364     Type *EltTy = C->getType()->getSequentialElementType();
00365     uint64_t EltSize = TD.getTypeAllocSize(EltTy);
00366     uint64_t Index = ByteOffset / EltSize;
00367     uint64_t Offset = ByteOffset - Index * EltSize;
00368     uint64_t NumElts;
00369     if (ArrayType *AT = dyn_cast<ArrayType>(C->getType()))
00370       NumElts = AT->getNumElements();
00371     else
00372       NumElts = C->getType()->getVectorNumElements();
00373 
00374     for (; Index != NumElts; ++Index) {
00375       if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
00376                               BytesLeft, TD))
00377         return false;
00378 
00379       uint64_t BytesWritten = EltSize - Offset;
00380       assert(BytesWritten <= EltSize && "Not indexing into this element?");
00381       if (BytesWritten >= BytesLeft)
00382         return true;
00383 
00384       Offset = 0;
00385       BytesLeft -= BytesWritten;
00386       CurPtr += BytesWritten;
00387     }
00388     return true;
00389   }
00390 
00391   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
00392     if (CE->getOpcode() == Instruction::IntToPtr &&
00393         CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getType())) {
00394       return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
00395                                 BytesLeft, TD);
00396     }
00397   }
00398 
00399   // Otherwise, unknown initializer type.
00400   return false;
00401 }
00402 
00403 static Constant *FoldReinterpretLoadFromConstPtr(Constant *C,
00404                                                  const DataLayout &TD) {
00405   PointerType *PTy = cast<PointerType>(C->getType());
00406   Type *LoadTy = PTy->getElementType();
00407   IntegerType *IntType = dyn_cast<IntegerType>(LoadTy);
00408 
00409   // If this isn't an integer load we can't fold it directly.
00410   if (!IntType) {
00411     unsigned AS = PTy->getAddressSpace();
00412 
00413     // If this is a float/double load, we can try folding it as an int32/64 load
00414     // and then bitcast the result.  This can be useful for union cases.  Note
00415     // that address spaces don't matter here since we're not going to result in
00416     // an actual new load.
00417     Type *MapTy;
00418     if (LoadTy->isHalfTy())
00419       MapTy = Type::getInt16PtrTy(C->getContext(), AS);
00420     else if (LoadTy->isFloatTy())
00421       MapTy = Type::getInt32PtrTy(C->getContext(), AS);
00422     else if (LoadTy->isDoubleTy())
00423       MapTy = Type::getInt64PtrTy(C->getContext(), AS);
00424     else if (LoadTy->isVectorTy()) {
00425       MapTy = PointerType::getIntNPtrTy(C->getContext(),
00426                                         TD.getTypeAllocSizeInBits(LoadTy),
00427                                         AS);
00428     } else
00429       return nullptr;
00430 
00431     C = FoldBitCast(C, MapTy, TD);
00432     if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, TD))
00433       return FoldBitCast(Res, LoadTy, TD);
00434     return nullptr;
00435   }
00436 
00437   unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
00438   if (BytesLoaded > 32 || BytesLoaded == 0)
00439     return nullptr;
00440 
00441   GlobalValue *GVal;
00442   APInt Offset;
00443   if (!IsConstantOffsetFromGlobal(C, GVal, Offset, TD))
00444     return nullptr;
00445 
00446   GlobalVariable *GV = dyn_cast<GlobalVariable>(GVal);
00447   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
00448       !GV->getInitializer()->getType()->isSized())
00449     return nullptr;
00450 
00451   // If we're loading off the beginning of the global, some bytes may be valid,
00452   // but we don't try to handle this.
00453   if (Offset.isNegative())
00454     return nullptr;
00455 
00456   // If we're not accessing anything in this constant, the result is undefined.
00457   if (Offset.getZExtValue() >=
00458       TD.getTypeAllocSize(GV->getInitializer()->getType()))
00459     return UndefValue::get(IntType);
00460 
00461   unsigned char RawBytes[32] = {0};
00462   if (!ReadDataFromGlobal(GV->getInitializer(), Offset.getZExtValue(), RawBytes,
00463                           BytesLoaded, TD))
00464     return nullptr;
00465 
00466   APInt ResultVal = APInt(IntType->getBitWidth(), 0);
00467   if (TD.isLittleEndian()) {
00468     ResultVal = RawBytes[BytesLoaded - 1];
00469     for (unsigned i = 1; i != BytesLoaded; ++i) {
00470       ResultVal <<= 8;
00471       ResultVal |= RawBytes[BytesLoaded - 1 - i];
00472     }
00473   } else {
00474     ResultVal = RawBytes[0];
00475     for (unsigned i = 1; i != BytesLoaded; ++i) {
00476       ResultVal <<= 8;
00477       ResultVal |= RawBytes[i];
00478     }
00479   }
00480 
00481   return ConstantInt::get(IntType->getContext(), ResultVal);
00482 }
00483 
00484 static Constant *ConstantFoldLoadThroughBitcast(ConstantExpr *CE,
00485                                                 const DataLayout *DL) {
00486   if (!DL)
00487     return nullptr;
00488   auto *DestPtrTy = dyn_cast<PointerType>(CE->getType());
00489   if (!DestPtrTy)
00490     return nullptr;
00491   Type *DestTy = DestPtrTy->getElementType();
00492 
00493   Constant *C = ConstantFoldLoadFromConstPtr(CE->getOperand(0), DL);
00494   if (!C)
00495     return nullptr;
00496 
00497   do {
00498     Type *SrcTy = C->getType();
00499 
00500     // If the type sizes are the same and a cast is legal, just directly
00501     // cast the constant.
00502     if (DL->getTypeSizeInBits(DestTy) == DL->getTypeSizeInBits(SrcTy)) {
00503       Instruction::CastOps Cast = Instruction::BitCast;
00504       // If we are going from a pointer to int or vice versa, we spell the cast
00505       // differently.
00506       if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
00507         Cast = Instruction::IntToPtr;
00508       else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
00509         Cast = Instruction::PtrToInt;
00510 
00511       if (CastInst::castIsValid(Cast, C, DestTy))
00512         return ConstantExpr::getCast(Cast, C, DestTy);
00513     }
00514 
00515     // If this isn't an aggregate type, there is nothing we can do to drill down
00516     // and find a bitcastable constant.
00517     if (!SrcTy->isAggregateType())
00518       return nullptr;
00519 
00520     // We're simulating a load through a pointer that was bitcast to point to
00521     // a different type, so we can try to walk down through the initial
00522     // elements of an aggregate to see if some part of th e aggregate is
00523     // castable to implement the "load" semantic model.
00524     C = C->getAggregateElement(0u);
00525   } while (C);
00526 
00527   return nullptr;
00528 }
00529 
00530 /// Return the value that a load from C would produce if it is constant and
00531 /// determinable. If this is not determinable, return null.
00532 Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C,
00533                                              const DataLayout *TD) {
00534   // First, try the easy cases:
00535   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
00536     if (GV->isConstant() && GV->hasDefinitiveInitializer())
00537       return GV->getInitializer();
00538 
00539   // If the loaded value isn't a constant expr, we can't handle it.
00540   ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
00541   if (!CE)
00542     return nullptr;
00543 
00544   if (CE->getOpcode() == Instruction::GetElementPtr) {
00545     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
00546       if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
00547         if (Constant *V =
00548              ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
00549           return V;
00550       }
00551     }
00552   }
00553 
00554   if (CE->getOpcode() == Instruction::BitCast)
00555     if (Constant *LoadedC = ConstantFoldLoadThroughBitcast(CE, TD))
00556       return LoadedC;
00557 
00558   // Instead of loading constant c string, use corresponding integer value
00559   // directly if string length is small enough.
00560   StringRef Str;
00561   if (TD && getConstantStringInfo(CE, Str) && !Str.empty()) {
00562     unsigned StrLen = Str.size();
00563     Type *Ty = cast<PointerType>(CE->getType())->getElementType();
00564     unsigned NumBits = Ty->getPrimitiveSizeInBits();
00565     // Replace load with immediate integer if the result is an integer or fp
00566     // value.
00567     if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 &&
00568         (isa<IntegerType>(Ty) || Ty->isFloatingPointTy())) {
00569       APInt StrVal(NumBits, 0);
00570       APInt SingleChar(NumBits, 0);
00571       if (TD->isLittleEndian()) {
00572         for (signed i = StrLen-1; i >= 0; i--) {
00573           SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
00574           StrVal = (StrVal << 8) | SingleChar;
00575         }
00576       } else {
00577         for (unsigned i = 0; i < StrLen; i++) {
00578           SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
00579           StrVal = (StrVal << 8) | SingleChar;
00580         }
00581         // Append NULL at the end.
00582         SingleChar = 0;
00583         StrVal = (StrVal << 8) | SingleChar;
00584       }
00585 
00586       Constant *Res = ConstantInt::get(CE->getContext(), StrVal);
00587       if (Ty->isFloatingPointTy())
00588         Res = ConstantExpr::getBitCast(Res, Ty);
00589       return Res;
00590     }
00591   }
00592 
00593   // If this load comes from anywhere in a constant global, and if the global
00594   // is all undef or zero, we know what it loads.
00595   if (GlobalVariable *GV =
00596         dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, TD))) {
00597     if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
00598       Type *ResTy = cast<PointerType>(C->getType())->getElementType();
00599       if (GV->getInitializer()->isNullValue())
00600         return Constant::getNullValue(ResTy);
00601       if (isa<UndefValue>(GV->getInitializer()))
00602         return UndefValue::get(ResTy);
00603     }
00604   }
00605 
00606   // Try hard to fold loads from bitcasted strange and non-type-safe things.
00607   if (TD)
00608     return FoldReinterpretLoadFromConstPtr(CE, *TD);
00609   return nullptr;
00610 }
00611 
00612 static Constant *ConstantFoldLoadInst(const LoadInst *LI, const DataLayout *TD){
00613   if (LI->isVolatile()) return nullptr;
00614 
00615   if (Constant *C = dyn_cast<Constant>(LI->getOperand(0)))
00616     return ConstantFoldLoadFromConstPtr(C, TD);
00617 
00618   return nullptr;
00619 }
00620 
00621 /// One of Op0/Op1 is a constant expression.
00622 /// Attempt to symbolically evaluate the result of a binary operator merging
00623 /// these together.  If target data info is available, it is provided as DL,
00624 /// otherwise DL is null.
00625 static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
00626                                            Constant *Op1, const DataLayout *DL){
00627   // SROA
00628 
00629   // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
00630   // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
00631   // bits.
00632 
00633 
00634   if (Opc == Instruction::And && DL) {
00635     unsigned BitWidth = DL->getTypeSizeInBits(Op0->getType()->getScalarType());
00636     APInt KnownZero0(BitWidth, 0), KnownOne0(BitWidth, 0);
00637     APInt KnownZero1(BitWidth, 0), KnownOne1(BitWidth, 0);
00638     computeKnownBits(Op0, KnownZero0, KnownOne0, DL);
00639     computeKnownBits(Op1, KnownZero1, KnownOne1, DL);
00640     if ((KnownOne1 | KnownZero0).isAllOnesValue()) {
00641       // All the bits of Op0 that the 'and' could be masking are already zero.
00642       return Op0;
00643     }
00644     if ((KnownOne0 | KnownZero1).isAllOnesValue()) {
00645       // All the bits of Op1 that the 'and' could be masking are already zero.
00646       return Op1;
00647     }
00648 
00649     APInt KnownZero = KnownZero0 | KnownZero1;
00650     APInt KnownOne = KnownOne0 & KnownOne1;
00651     if ((KnownZero | KnownOne).isAllOnesValue()) {
00652       return ConstantInt::get(Op0->getType(), KnownOne);
00653     }
00654   }
00655 
00656   // If the constant expr is something like &A[123] - &A[4].f, fold this into a
00657   // constant.  This happens frequently when iterating over a global array.
00658   if (Opc == Instruction::Sub && DL) {
00659     GlobalValue *GV1, *GV2;
00660     APInt Offs1, Offs2;
00661 
00662     if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *DL))
00663       if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *DL) &&
00664           GV1 == GV2) {
00665         unsigned OpSize = DL->getTypeSizeInBits(Op0->getType());
00666 
00667         // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
00668         // PtrToInt may change the bitwidth so we have convert to the right size
00669         // first.
00670         return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) -
00671                                                 Offs2.zextOrTrunc(OpSize));
00672       }
00673   }
00674 
00675   return nullptr;
00676 }
00677 
00678 /// If array indices are not pointer-sized integers, explicitly cast them so
00679 /// that they aren't implicitly casted by the getelementptr.
00680 static Constant *CastGEPIndices(ArrayRef<Constant *> Ops,
00681                                 Type *ResultTy, const DataLayout *TD,
00682                                 const TargetLibraryInfo *TLI) {
00683   if (!TD)
00684     return nullptr;
00685 
00686   Type *IntPtrTy = TD->getIntPtrType(ResultTy);
00687 
00688   bool Any = false;
00689   SmallVector<Constant*, 32> NewIdxs;
00690   for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
00691     if ((i == 1 ||
00692          !isa<StructType>(GetElementPtrInst::getIndexedType(
00693                             Ops[0]->getType(),
00694                             Ops.slice(1, i - 1)))) &&
00695         Ops[i]->getType() != IntPtrTy) {
00696       Any = true;
00697       NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i],
00698                                                                       true,
00699                                                                       IntPtrTy,
00700                                                                       true),
00701                                               Ops[i], IntPtrTy));
00702     } else
00703       NewIdxs.push_back(Ops[i]);
00704   }
00705 
00706   if (!Any)
00707     return nullptr;
00708 
00709   Constant *C = ConstantExpr::getGetElementPtr(Ops[0], NewIdxs);
00710   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
00711     if (Constant *Folded = ConstantFoldConstantExpression(CE, TD, TLI))
00712       C = Folded;
00713   }
00714 
00715   return C;
00716 }
00717 
00718 /// Strip the pointer casts, but preserve the address space information.
00719 static Constant* StripPtrCastKeepAS(Constant* Ptr) {
00720   assert(Ptr->getType()->isPointerTy() && "Not a pointer type");
00721   PointerType *OldPtrTy = cast<PointerType>(Ptr->getType());
00722   Ptr = Ptr->stripPointerCasts();
00723   PointerType *NewPtrTy = cast<PointerType>(Ptr->getType());
00724 
00725   // Preserve the address space number of the pointer.
00726   if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) {
00727     NewPtrTy = NewPtrTy->getElementType()->getPointerTo(
00728       OldPtrTy->getAddressSpace());
00729     Ptr = ConstantExpr::getPointerCast(Ptr, NewPtrTy);
00730   }
00731   return Ptr;
00732 }
00733 
00734 /// If we can symbolically evaluate the GEP constant expression, do so.
00735 static Constant *SymbolicallyEvaluateGEP(ArrayRef<Constant *> Ops,
00736                                          Type *ResultTy, const DataLayout *TD,
00737                                          const TargetLibraryInfo *TLI) {
00738   Constant *Ptr = Ops[0];
00739   if (!TD || !Ptr->getType()->getPointerElementType()->isSized() ||
00740       !Ptr->getType()->isPointerTy())
00741     return nullptr;
00742 
00743   Type *IntPtrTy = TD->getIntPtrType(Ptr->getType());
00744   Type *ResultElementTy = ResultTy->getPointerElementType();
00745 
00746   // If this is a constant expr gep that is effectively computing an
00747   // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
00748   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
00749     if (!isa<ConstantInt>(Ops[i])) {
00750 
00751       // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
00752       // "inttoptr (sub (ptrtoint Ptr), V)"
00753       if (Ops.size() == 2 && ResultElementTy->isIntegerTy(8)) {
00754         ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[1]);
00755         assert((!CE || CE->getType() == IntPtrTy) &&
00756                "CastGEPIndices didn't canonicalize index types!");
00757         if (CE && CE->getOpcode() == Instruction::Sub &&
00758             CE->getOperand(0)->isNullValue()) {
00759           Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType());
00760           Res = ConstantExpr::getSub(Res, CE->getOperand(1));
00761           Res = ConstantExpr::getIntToPtr(Res, ResultTy);
00762           if (ConstantExpr *ResCE = dyn_cast<ConstantExpr>(Res))
00763             Res = ConstantFoldConstantExpression(ResCE, TD, TLI);
00764           return Res;
00765         }
00766       }
00767       return nullptr;
00768     }
00769 
00770   unsigned BitWidth = TD->getTypeSizeInBits(IntPtrTy);
00771   APInt Offset =
00772     APInt(BitWidth, TD->getIndexedOffset(Ptr->getType(),
00773                                          makeArrayRef((Value *const*)
00774                                                         Ops.data() + 1,
00775                                                       Ops.size() - 1)));
00776   Ptr = StripPtrCastKeepAS(Ptr);
00777 
00778   // If this is a GEP of a GEP, fold it all into a single GEP.
00779   while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
00780     SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end());
00781 
00782     // Do not try the incorporate the sub-GEP if some index is not a number.
00783     bool AllConstantInt = true;
00784     for (unsigned i = 0, e = NestedOps.size(); i != e; ++i)
00785       if (!isa<ConstantInt>(NestedOps[i])) {
00786         AllConstantInt = false;
00787         break;
00788       }
00789     if (!AllConstantInt)
00790       break;
00791 
00792     Ptr = cast<Constant>(GEP->getOperand(0));
00793     Offset += APInt(BitWidth,
00794                     TD->getIndexedOffset(Ptr->getType(), NestedOps));
00795     Ptr = StripPtrCastKeepAS(Ptr);
00796   }
00797 
00798   // If the base value for this address is a literal integer value, fold the
00799   // getelementptr to the resulting integer value casted to the pointer type.
00800   APInt BasePtr(BitWidth, 0);
00801   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
00802     if (CE->getOpcode() == Instruction::IntToPtr) {
00803       if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
00804         BasePtr = Base->getValue().zextOrTrunc(BitWidth);
00805     }
00806   }
00807 
00808   if (Ptr->isNullValue() || BasePtr != 0) {
00809     Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr);
00810     return ConstantExpr::getIntToPtr(C, ResultTy);
00811   }
00812 
00813   // Otherwise form a regular getelementptr. Recompute the indices so that
00814   // we eliminate over-indexing of the notional static type array bounds.
00815   // This makes it easy to determine if the getelementptr is "inbounds".
00816   // Also, this helps GlobalOpt do SROA on GlobalVariables.
00817   Type *Ty = Ptr->getType();
00818   assert(Ty->isPointerTy() && "Forming regular GEP of non-pointer type");
00819   SmallVector<Constant *, 32> NewIdxs;
00820 
00821   do {
00822     if (SequentialType *ATy = dyn_cast<SequentialType>(Ty)) {
00823       if (ATy->isPointerTy()) {
00824         // The only pointer indexing we'll do is on the first index of the GEP.
00825         if (!NewIdxs.empty())
00826           break;
00827 
00828         // Only handle pointers to sized types, not pointers to functions.
00829         if (!ATy->getElementType()->isSized())
00830           return nullptr;
00831       }
00832 
00833       // Determine which element of the array the offset points into.
00834       APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType()));
00835       if (ElemSize == 0)
00836         // The element size is 0. This may be [0 x Ty]*, so just use a zero
00837         // index for this level and proceed to the next level to see if it can
00838         // accommodate the offset.
00839         NewIdxs.push_back(ConstantInt::get(IntPtrTy, 0));
00840       else {
00841         // The element size is non-zero divide the offset by the element
00842         // size (rounding down), to compute the index at this level.
00843         APInt NewIdx = Offset.udiv(ElemSize);
00844         Offset -= NewIdx * ElemSize;
00845         NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx));
00846       }
00847       Ty = ATy->getElementType();
00848     } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
00849       // If we end up with an offset that isn't valid for this struct type, we
00850       // can't re-form this GEP in a regular form, so bail out. The pointer
00851       // operand likely went through casts that are necessary to make the GEP
00852       // sensible.
00853       const StructLayout &SL = *TD->getStructLayout(STy);
00854       if (Offset.uge(SL.getSizeInBytes()))
00855         break;
00856 
00857       // Determine which field of the struct the offset points into. The
00858       // getZExtValue is fine as we've already ensured that the offset is
00859       // within the range representable by the StructLayout API.
00860       unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
00861       NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
00862                                          ElIdx));
00863       Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
00864       Ty = STy->getTypeAtIndex(ElIdx);
00865     } else {
00866       // We've reached some non-indexable type.
00867       break;
00868     }
00869   } while (Ty != ResultElementTy);
00870 
00871   // If we haven't used up the entire offset by descending the static
00872   // type, then the offset is pointing into the middle of an indivisible
00873   // member, so we can't simplify it.
00874   if (Offset != 0)
00875     return nullptr;
00876 
00877   // Create a GEP.
00878   Constant *C = ConstantExpr::getGetElementPtr(Ptr, NewIdxs);
00879   assert(C->getType()->getPointerElementType() == Ty &&
00880          "Computed GetElementPtr has unexpected type!");
00881 
00882   // If we ended up indexing a member with a type that doesn't match
00883   // the type of what the original indices indexed, add a cast.
00884   if (Ty != ResultElementTy)
00885     C = FoldBitCast(C, ResultTy, *TD);
00886 
00887   return C;
00888 }
00889 
00890 
00891 
00892 //===----------------------------------------------------------------------===//
00893 // Constant Folding public APIs
00894 //===----------------------------------------------------------------------===//
00895 
00896 /// Try to constant fold the specified instruction.
00897 /// If successful, the constant result is returned, if not, null is returned.
00898 /// Note that this fails if not all of the operands are constant.  Otherwise,
00899 /// this function can only fail when attempting to fold instructions like loads
00900 /// and stores, which have no constant expression form.
00901 Constant *llvm::ConstantFoldInstruction(Instruction *I,
00902                                         const DataLayout *TD,
00903                                         const TargetLibraryInfo *TLI) {
00904   // Handle PHI nodes quickly here...
00905   if (PHINode *PN = dyn_cast<PHINode>(I)) {
00906     Constant *CommonValue = nullptr;
00907 
00908     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00909       Value *Incoming = PN->getIncomingValue(i);
00910       // If the incoming value is undef then skip it.  Note that while we could
00911       // skip the value if it is equal to the phi node itself we choose not to
00912       // because that would break the rule that constant folding only applies if
00913       // all operands are constants.
00914       if (isa<UndefValue>(Incoming))
00915         continue;
00916       // If the incoming value is not a constant, then give up.
00917       Constant *C = dyn_cast<Constant>(Incoming);
00918       if (!C)
00919         return nullptr;
00920       // Fold the PHI's operands.
00921       if (ConstantExpr *NewC = dyn_cast<ConstantExpr>(C))
00922         C = ConstantFoldConstantExpression(NewC, TD, TLI);
00923       // If the incoming value is a different constant to
00924       // the one we saw previously, then give up.
00925       if (CommonValue && C != CommonValue)
00926         return nullptr;
00927       CommonValue = C;
00928     }
00929 
00930 
00931     // If we reach here, all incoming values are the same constant or undef.
00932     return CommonValue ? CommonValue : UndefValue::get(PN->getType());
00933   }
00934 
00935   // Scan the operand list, checking to see if they are all constants, if so,
00936   // hand off to ConstantFoldInstOperands.
00937   SmallVector<Constant*, 8> Ops;
00938   for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
00939     Constant *Op = dyn_cast<Constant>(*i);
00940     if (!Op)
00941       return nullptr;  // All operands not constant!
00942 
00943     // Fold the Instruction's operands.
00944     if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(Op))
00945       Op = ConstantFoldConstantExpression(NewCE, TD, TLI);
00946 
00947     Ops.push_back(Op);
00948   }
00949 
00950   if (const CmpInst *CI = dyn_cast<CmpInst>(I))
00951     return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
00952                                            TD, TLI);
00953 
00954   if (const LoadInst *LI = dyn_cast<LoadInst>(I))
00955     return ConstantFoldLoadInst(LI, TD);
00956 
00957   if (InsertValueInst *IVI = dyn_cast<InsertValueInst>(I)) {
00958     return ConstantExpr::getInsertValue(
00959                                 cast<Constant>(IVI->getAggregateOperand()),
00960                                 cast<Constant>(IVI->getInsertedValueOperand()),
00961                                 IVI->getIndices());
00962   }
00963 
00964   if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I)) {
00965     return ConstantExpr::getExtractValue(
00966                                     cast<Constant>(EVI->getAggregateOperand()),
00967                                     EVI->getIndices());
00968   }
00969 
00970   return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Ops, TD, TLI);
00971 }
00972 
00973 static Constant *
00974 ConstantFoldConstantExpressionImpl(const ConstantExpr *CE, const DataLayout *TD,
00975                                    const TargetLibraryInfo *TLI,
00976                                    SmallPtrSetImpl<ConstantExpr *> &FoldedOps) {
00977   SmallVector<Constant *, 8> Ops;
00978   for (User::const_op_iterator i = CE->op_begin(), e = CE->op_end(); i != e;
00979        ++i) {
00980     Constant *NewC = cast<Constant>(*i);
00981     // Recursively fold the ConstantExpr's operands. If we have already folded
00982     // a ConstantExpr, we don't have to process it again.
00983     if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(NewC)) {
00984       if (FoldedOps.insert(NewCE))
00985         NewC = ConstantFoldConstantExpressionImpl(NewCE, TD, TLI, FoldedOps);
00986     }
00987     Ops.push_back(NewC);
00988   }
00989 
00990   if (CE->isCompare())
00991     return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
00992                                            TD, TLI);
00993   return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(), Ops, TD, TLI);
00994 }
00995 
00996 /// Attempt to fold the constant expression
00997 /// using the specified DataLayout.  If successful, the constant result is
00998 /// result is returned, if not, null is returned.
00999 Constant *llvm::ConstantFoldConstantExpression(const ConstantExpr *CE,
01000                                                const DataLayout *TD,
01001                                                const TargetLibraryInfo *TLI) {
01002   SmallPtrSet<ConstantExpr *, 4> FoldedOps;
01003   return ConstantFoldConstantExpressionImpl(CE, TD, TLI, FoldedOps);
01004 }
01005 
01006 /// Attempt to constant fold an instruction with the
01007 /// specified opcode and operands.  If successful, the constant result is
01008 /// returned, if not, null is returned.  Note that this function can fail when
01009 /// attempting to fold instructions like loads and stores, which have no
01010 /// constant expression form.
01011 ///
01012 /// TODO: This function neither utilizes nor preserves nsw/nuw/inbounds/etc
01013 /// information, due to only being passed an opcode and operands. Constant
01014 /// folding using this function strips this information.
01015 ///
01016 Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
01017                                          ArrayRef<Constant *> Ops,
01018                                          const DataLayout *TD,
01019                                          const TargetLibraryInfo *TLI) {
01020   // Handle easy binops first.
01021   if (Instruction::isBinaryOp(Opcode)) {
01022     if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1])) {
01023       if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD))
01024         return C;
01025     }
01026 
01027     return ConstantExpr::get(Opcode, Ops[0], Ops[1]);
01028   }
01029 
01030   switch (Opcode) {
01031   default: return nullptr;
01032   case Instruction::ICmp:
01033   case Instruction::FCmp: llvm_unreachable("Invalid for compares");
01034   case Instruction::Call:
01035     if (Function *F = dyn_cast<Function>(Ops.back()))
01036       if (canConstantFoldCallTo(F))
01037         return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI);
01038     return nullptr;
01039   case Instruction::PtrToInt:
01040     // If the input is a inttoptr, eliminate the pair.  This requires knowing
01041     // the width of a pointer, so it can't be done in ConstantExpr::getCast.
01042     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
01043       if (TD && CE->getOpcode() == Instruction::IntToPtr) {
01044         Constant *Input = CE->getOperand(0);
01045         unsigned InWidth = Input->getType()->getScalarSizeInBits();
01046         unsigned PtrWidth = TD->getPointerTypeSizeInBits(CE->getType());
01047         if (PtrWidth < InWidth) {
01048           Constant *Mask =
01049             ConstantInt::get(CE->getContext(),
01050                              APInt::getLowBitsSet(InWidth, PtrWidth));
01051           Input = ConstantExpr::getAnd(Input, Mask);
01052         }
01053         // Do a zext or trunc to get to the dest size.
01054         return ConstantExpr::getIntegerCast(Input, DestTy, false);
01055       }
01056     }
01057     return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
01058   case Instruction::IntToPtr:
01059     // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
01060     // the int size is >= the ptr size and the address spaces are the same.
01061     // This requires knowing the width of a pointer, so it can't be done in
01062     // ConstantExpr::getCast.
01063     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
01064       if (TD && CE->getOpcode() == Instruction::PtrToInt) {
01065         Constant *SrcPtr = CE->getOperand(0);
01066         unsigned SrcPtrSize = TD->getPointerTypeSizeInBits(SrcPtr->getType());
01067         unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
01068 
01069         if (MidIntSize >= SrcPtrSize) {
01070           unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace();
01071           if (SrcAS == DestTy->getPointerAddressSpace())
01072             return FoldBitCast(CE->getOperand(0), DestTy, *TD);
01073         }
01074       }
01075     }
01076 
01077     return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
01078   case Instruction::Trunc:
01079   case Instruction::ZExt:
01080   case Instruction::SExt:
01081   case Instruction::FPTrunc:
01082   case Instruction::FPExt:
01083   case Instruction::UIToFP:
01084   case Instruction::SIToFP:
01085   case Instruction::FPToUI:
01086   case Instruction::FPToSI:
01087   case Instruction::AddrSpaceCast:
01088       return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
01089   case Instruction::BitCast:
01090     if (TD)
01091       return FoldBitCast(Ops[0], DestTy, *TD);
01092     return ConstantExpr::getBitCast(Ops[0], DestTy);
01093   case Instruction::Select:
01094     return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
01095   case Instruction::ExtractElement:
01096     return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
01097   case Instruction::InsertElement:
01098     return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
01099   case Instruction::ShuffleVector:
01100     return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
01101   case Instruction::GetElementPtr:
01102     if (Constant *C = CastGEPIndices(Ops, DestTy, TD, TLI))
01103       return C;
01104     if (Constant *C = SymbolicallyEvaluateGEP(Ops, DestTy, TD, TLI))
01105       return C;
01106 
01107     return ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1));
01108   }
01109 }
01110 
01111 /// Attempt to constant fold a compare
01112 /// instruction (icmp/fcmp) with the specified operands.  If it fails, it
01113 /// returns a constant expression of the specified operands.
01114 Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
01115                                                 Constant *Ops0, Constant *Ops1,
01116                                                 const DataLayout *TD,
01117                                                 const TargetLibraryInfo *TLI) {
01118   // fold: icmp (inttoptr x), null         -> icmp x, 0
01119   // fold: icmp (ptrtoint x), 0            -> icmp x, null
01120   // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
01121   // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
01122   //
01123   // ConstantExpr::getCompare cannot do this, because it doesn't have TD
01124   // around to know if bit truncation is happening.
01125   if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
01126     if (TD && Ops1->isNullValue()) {
01127       if (CE0->getOpcode() == Instruction::IntToPtr) {
01128         Type *IntPtrTy = TD->getIntPtrType(CE0->getType());
01129         // Convert the integer value to the right size to ensure we get the
01130         // proper extension or truncation.
01131         Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
01132                                                    IntPtrTy, false);
01133         Constant *Null = Constant::getNullValue(C->getType());
01134         return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI);
01135       }
01136 
01137       // Only do this transformation if the int is intptrty in size, otherwise
01138       // there is a truncation or extension that we aren't modeling.
01139       if (CE0->getOpcode() == Instruction::PtrToInt) {
01140         Type *IntPtrTy = TD->getIntPtrType(CE0->getOperand(0)->getType());
01141         if (CE0->getType() == IntPtrTy) {
01142           Constant *C = CE0->getOperand(0);
01143           Constant *Null = Constant::getNullValue(C->getType());
01144           return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI);
01145         }
01146       }
01147     }
01148 
01149     if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
01150       if (TD && CE0->getOpcode() == CE1->getOpcode()) {
01151         if (CE0->getOpcode() == Instruction::IntToPtr) {
01152           Type *IntPtrTy = TD->getIntPtrType(CE0->getType());
01153 
01154           // Convert the integer value to the right size to ensure we get the
01155           // proper extension or truncation.
01156           Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
01157                                                       IntPtrTy, false);
01158           Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
01159                                                       IntPtrTy, false);
01160           return ConstantFoldCompareInstOperands(Predicate, C0, C1, TD, TLI);
01161         }
01162 
01163         // Only do this transformation if the int is intptrty in size, otherwise
01164         // there is a truncation or extension that we aren't modeling.
01165         if (CE0->getOpcode() == Instruction::PtrToInt) {
01166           Type *IntPtrTy = TD->getIntPtrType(CE0->getOperand(0)->getType());
01167           if (CE0->getType() == IntPtrTy &&
01168               CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
01169             return ConstantFoldCompareInstOperands(Predicate,
01170                                                    CE0->getOperand(0),
01171                                                    CE1->getOperand(0),
01172                                                    TD,
01173                                                    TLI);
01174           }
01175         }
01176       }
01177     }
01178 
01179     // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0)
01180     // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0)
01181     if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) &&
01182         CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) {
01183       Constant *LHS =
01184         ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0), Ops1,
01185                                         TD, TLI);
01186       Constant *RHS =
01187         ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(1), Ops1,
01188                                         TD, TLI);
01189       unsigned OpC =
01190         Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
01191       Constant *Ops[] = { LHS, RHS };
01192       return ConstantFoldInstOperands(OpC, LHS->getType(), Ops, TD, TLI);
01193     }
01194   }
01195 
01196   return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
01197 }
01198 
01199 
01200 /// Given a constant and a getelementptr constantexpr, return the constant value
01201 /// being addressed by the constant expression, or null if something is funny
01202 /// and we can't decide.
01203 Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
01204                                                        ConstantExpr *CE) {
01205   if (!CE->getOperand(1)->isNullValue())
01206     return nullptr;  // Do not allow stepping over the value!
01207 
01208   // Loop over all of the operands, tracking down which value we are
01209   // addressing.
01210   for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) {
01211     C = C->getAggregateElement(CE->getOperand(i));
01212     if (!C)
01213       return nullptr;
01214   }
01215   return C;
01216 }
01217 
01218 /// Given a constant and getelementptr indices (with an *implied* zero pointer
01219 /// index that is not in the list), return the constant value being addressed by
01220 /// a virtual load, or null if something is funny and we can't decide.
01221 Constant *llvm::ConstantFoldLoadThroughGEPIndices(Constant *C,
01222                                                   ArrayRef<Constant*> Indices) {
01223   // Loop over all of the operands, tracking down which value we are
01224   // addressing.
01225   for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
01226     C = C->getAggregateElement(Indices[i]);
01227     if (!C)
01228       return nullptr;
01229   }
01230   return C;
01231 }
01232 
01233 
01234 //===----------------------------------------------------------------------===//
01235 //  Constant Folding for Calls
01236 //
01237 
01238 /// Return true if it's even possible to fold a call to the specified function.
01239 bool llvm::canConstantFoldCallTo(const Function *F) {
01240   switch (F->getIntrinsicID()) {
01241   case Intrinsic::fabs:
01242   case Intrinsic::minnum:
01243   case Intrinsic::maxnum:
01244   case Intrinsic::log:
01245   case Intrinsic::log2:
01246   case Intrinsic::log10:
01247   case Intrinsic::exp:
01248   case Intrinsic::exp2:
01249   case Intrinsic::floor:
01250   case Intrinsic::ceil:
01251   case Intrinsic::sqrt:
01252   case Intrinsic::pow:
01253   case Intrinsic::powi:
01254   case Intrinsic::bswap:
01255   case Intrinsic::ctpop:
01256   case Intrinsic::ctlz:
01257   case Intrinsic::cttz:
01258   case Intrinsic::fma:
01259   case Intrinsic::fmuladd:
01260   case Intrinsic::copysign:
01261   case Intrinsic::round:
01262   case Intrinsic::sadd_with_overflow:
01263   case Intrinsic::uadd_with_overflow:
01264   case Intrinsic::ssub_with_overflow:
01265   case Intrinsic::usub_with_overflow:
01266   case Intrinsic::smul_with_overflow:
01267   case Intrinsic::umul_with_overflow:
01268   case Intrinsic::convert_from_fp16:
01269   case Intrinsic::convert_to_fp16:
01270   case Intrinsic::x86_sse_cvtss2si:
01271   case Intrinsic::x86_sse_cvtss2si64:
01272   case Intrinsic::x86_sse_cvttss2si:
01273   case Intrinsic::x86_sse_cvttss2si64:
01274   case Intrinsic::x86_sse2_cvtsd2si:
01275   case Intrinsic::x86_sse2_cvtsd2si64:
01276   case Intrinsic::x86_sse2_cvttsd2si:
01277   case Intrinsic::x86_sse2_cvttsd2si64:
01278     return true;
01279   default:
01280     return false;
01281   case 0: break;
01282   }
01283 
01284   if (!F->hasName())
01285     return false;
01286   StringRef Name = F->getName();
01287 
01288   // In these cases, the check of the length is required.  We don't want to
01289   // return true for a name like "cos\0blah" which strcmp would return equal to
01290   // "cos", but has length 8.
01291   switch (Name[0]) {
01292   default: return false;
01293   case 'a':
01294     return Name == "acos" || Name == "asin" || Name == "atan" || Name =="atan2";
01295   case 'c':
01296     return Name == "cos" || Name == "ceil" || Name == "cosf" || Name == "cosh";
01297   case 'e':
01298     return Name == "exp" || Name == "exp2";
01299   case 'f':
01300     return Name == "fabs" || Name == "fmod" || Name == "floor";
01301   case 'l':
01302     return Name == "log" || Name == "log10";
01303   case 'p':
01304     return Name == "pow";
01305   case 's':
01306     return Name == "sin" || Name == "sinh" || Name == "sqrt" ||
01307       Name == "sinf" || Name == "sqrtf";
01308   case 't':
01309     return Name == "tan" || Name == "tanh";
01310   }
01311 }
01312 
01313 static Constant *GetConstantFoldFPValue(double V, Type *Ty) {
01314   if (Ty->isHalfTy()) {
01315     APFloat APF(V);
01316     bool unused;
01317     APF.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &unused);
01318     return ConstantFP::get(Ty->getContext(), APF);
01319   }
01320   if (Ty->isFloatTy())
01321     return ConstantFP::get(Ty->getContext(), APFloat((float)V));
01322   if (Ty->isDoubleTy())
01323     return ConstantFP::get(Ty->getContext(), APFloat(V));
01324   llvm_unreachable("Can only constant fold half/float/double");
01325 
01326 }
01327 
01328 namespace {
01329 /// Clear the floating-point exception state.
01330 static inline void llvm_fenv_clearexcept() {
01331 #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT
01332   feclearexcept(FE_ALL_EXCEPT);
01333 #endif
01334   errno = 0;
01335 }
01336 
01337 /// Test if a floating-point exception was raised.
01338 static inline bool llvm_fenv_testexcept() {
01339   int errno_val = errno;
01340   if (errno_val == ERANGE || errno_val == EDOM)
01341     return true;
01342 #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT && HAVE_DECL_FE_INEXACT
01343   if (fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT))
01344     return true;
01345 #endif
01346   return false;
01347 }
01348 } // End namespace
01349 
01350 static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
01351                                 Type *Ty) {
01352   llvm_fenv_clearexcept();
01353   V = NativeFP(V);
01354   if (llvm_fenv_testexcept()) {
01355     llvm_fenv_clearexcept();
01356     return nullptr;
01357   }
01358 
01359   return GetConstantFoldFPValue(V, Ty);
01360 }
01361 
01362 static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
01363                                       double V, double W, Type *Ty) {
01364   llvm_fenv_clearexcept();
01365   V = NativeFP(V, W);
01366   if (llvm_fenv_testexcept()) {
01367     llvm_fenv_clearexcept();
01368     return nullptr;
01369   }
01370 
01371   return GetConstantFoldFPValue(V, Ty);
01372 }
01373 
01374 /// Attempt to fold an SSE floating point to integer conversion of a constant
01375 /// floating point. If roundTowardZero is false, the default IEEE rounding is
01376 /// used (toward nearest, ties to even). This matches the behavior of the
01377 /// non-truncating SSE instructions in the default rounding mode. The desired
01378 /// integer type Ty is used to select how many bits are available for the
01379 /// result. Returns null if the conversion cannot be performed, otherwise
01380 /// returns the Constant value resulting from the conversion.
01381 static Constant *ConstantFoldConvertToInt(const APFloat &Val,
01382                                           bool roundTowardZero, Type *Ty) {
01383   // All of these conversion intrinsics form an integer of at most 64bits.
01384   unsigned ResultWidth = Ty->getIntegerBitWidth();
01385   assert(ResultWidth <= 64 &&
01386          "Can only constant fold conversions to 64 and 32 bit ints");
01387 
01388   uint64_t UIntVal;
01389   bool isExact = false;
01390   APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero
01391                                               : APFloat::rmNearestTiesToEven;
01392   APFloat::opStatus status = Val.convertToInteger(&UIntVal, ResultWidth,
01393                                                   /*isSigned=*/true, mode,
01394                                                   &isExact);
01395   if (status != APFloat::opOK && status != APFloat::opInexact)
01396     return nullptr;
01397   return ConstantInt::get(Ty, UIntVal, /*isSigned=*/true);
01398 }
01399 
01400 static double getValueAsDouble(ConstantFP *Op) {
01401   Type *Ty = Op->getType();
01402 
01403   if (Ty->isFloatTy())
01404     return Op->getValueAPF().convertToFloat();
01405 
01406   if (Ty->isDoubleTy())
01407     return Op->getValueAPF().convertToDouble();
01408 
01409   bool unused;
01410   APFloat APF = Op->getValueAPF();
01411   APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &unused);
01412   return APF.convertToDouble();
01413 }
01414 
01415 static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID,
01416                                         Type *Ty, ArrayRef<Constant *> Operands,
01417                                         const TargetLibraryInfo *TLI) {
01418   if (Operands.size() == 1) {
01419     if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
01420       if (IntrinsicID == Intrinsic::convert_to_fp16) {
01421         APFloat Val(Op->getValueAPF());
01422 
01423         bool lost = false;
01424         Val.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &lost);
01425 
01426         return ConstantInt::get(Ty->getContext(), Val.bitcastToAPInt());
01427       }
01428 
01429       if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
01430         return nullptr;
01431 
01432       if (IntrinsicID == Intrinsic::round) {
01433         APFloat V = Op->getValueAPF();
01434         V.roundToIntegral(APFloat::rmNearestTiesToAway);
01435         return ConstantFP::get(Ty->getContext(), V);
01436       }
01437 
01438       /// We only fold functions with finite arguments. Folding NaN and inf is
01439       /// likely to be aborted with an exception anyway, and some host libms
01440       /// have known errors raising exceptions.
01441       if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity())
01442         return nullptr;
01443 
01444       /// Currently APFloat versions of these functions do not exist, so we use
01445       /// the host native double versions.  Float versions are not called
01446       /// directly but for all these it is true (float)(f((double)arg)) ==
01447       /// f(arg).  Long double not supported yet.
01448       double V = getValueAsDouble(Op);
01449 
01450       switch (IntrinsicID) {
01451         default: break;
01452         case Intrinsic::fabs:
01453           return ConstantFoldFP(fabs, V, Ty);
01454 #if HAVE_LOG2
01455         case Intrinsic::log2:
01456           return ConstantFoldFP(log2, V, Ty);
01457 #endif
01458 #if HAVE_LOG
01459         case Intrinsic::log:
01460           return ConstantFoldFP(log, V, Ty);
01461 #endif
01462 #if HAVE_LOG10
01463         case Intrinsic::log10:
01464           return ConstantFoldFP(log10, V, Ty);
01465 #endif
01466 #if HAVE_EXP
01467         case Intrinsic::exp:
01468           return ConstantFoldFP(exp, V, Ty);
01469 #endif
01470 #if HAVE_EXP2
01471         case Intrinsic::exp2:
01472           return ConstantFoldFP(exp2, V, Ty);
01473 #endif
01474         case Intrinsic::floor:
01475           return ConstantFoldFP(floor, V, Ty);
01476         case Intrinsic::ceil:
01477           return ConstantFoldFP(ceil, V, Ty);
01478       }
01479 
01480       if (!TLI)
01481         return nullptr;
01482 
01483       switch (Name[0]) {
01484       case 'a':
01485         if (Name == "acos" && TLI->has(LibFunc::acos))
01486           return ConstantFoldFP(acos, V, Ty);
01487         else if (Name == "asin" && TLI->has(LibFunc::asin))
01488           return ConstantFoldFP(asin, V, Ty);
01489         else if (Name == "atan" && TLI->has(LibFunc::atan))
01490           return ConstantFoldFP(atan, V, Ty);
01491         break;
01492       case 'c':
01493         if (Name == "ceil" && TLI->has(LibFunc::ceil))
01494           return ConstantFoldFP(ceil, V, Ty);
01495         else if (Name == "cos" && TLI->has(LibFunc::cos))
01496           return ConstantFoldFP(cos, V, Ty);
01497         else if (Name == "cosh" && TLI->has(LibFunc::cosh))
01498           return ConstantFoldFP(cosh, V, Ty);
01499         else if (Name == "cosf" && TLI->has(LibFunc::cosf))
01500           return ConstantFoldFP(cos, V, Ty);
01501         break;
01502       case 'e':
01503         if (Name == "exp" && TLI->has(LibFunc::exp))
01504           return ConstantFoldFP(exp, V, Ty);
01505 
01506         if (Name == "exp2" && TLI->has(LibFunc::exp2)) {
01507           // Constant fold exp2(x) as pow(2,x) in case the host doesn't have a
01508           // C99 library.
01509           return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
01510         }
01511         break;
01512       case 'f':
01513         if (Name == "fabs" && TLI->has(LibFunc::fabs))
01514           return ConstantFoldFP(fabs, V, Ty);
01515         else if (Name == "floor" && TLI->has(LibFunc::floor))
01516           return ConstantFoldFP(floor, V, Ty);
01517         break;
01518       case 'l':
01519         if (Name == "log" && V > 0 && TLI->has(LibFunc::log))
01520           return ConstantFoldFP(log, V, Ty);
01521         else if (Name == "log10" && V > 0 && TLI->has(LibFunc::log10))
01522           return ConstantFoldFP(log10, V, Ty);
01523         else if (IntrinsicID == Intrinsic::sqrt &&
01524                  (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())) {
01525           if (V >= -0.0)
01526             return ConstantFoldFP(sqrt, V, Ty);
01527           else {
01528             // Unlike the sqrt definitions in C/C++, POSIX, and IEEE-754 - which
01529             // all guarantee or favor returning NaN - the square root of a
01530             // negative number is not defined for the LLVM sqrt intrinsic.
01531             // This is because the intrinsic should only be emitted in place of
01532             // libm's sqrt function when using "no-nans-fp-math".
01533             return UndefValue::get(Ty);
01534           }
01535         }
01536         break;
01537       case 's':
01538         if (Name == "sin" && TLI->has(LibFunc::sin))
01539           return ConstantFoldFP(sin, V, Ty);
01540         else if (Name == "sinh" && TLI->has(LibFunc::sinh))
01541           return ConstantFoldFP(sinh, V, Ty);
01542         else if (Name == "sqrt" && V >= 0 && TLI->has(LibFunc::sqrt))
01543           return ConstantFoldFP(sqrt, V, Ty);
01544         else if (Name == "sqrtf" && V >= 0 && TLI->has(LibFunc::sqrtf))
01545           return ConstantFoldFP(sqrt, V, Ty);
01546         else if (Name == "sinf" && TLI->has(LibFunc::sinf))
01547           return ConstantFoldFP(sin, V, Ty);
01548         break;
01549       case 't':
01550         if (Name == "tan" && TLI->has(LibFunc::tan))
01551           return ConstantFoldFP(tan, V, Ty);
01552         else if (Name == "tanh" && TLI->has(LibFunc::tanh))
01553           return ConstantFoldFP(tanh, V, Ty);
01554         break;
01555       default:
01556         break;
01557       }
01558       return nullptr;
01559     }
01560 
01561     if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
01562       switch (IntrinsicID) {
01563       case Intrinsic::bswap:
01564         return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap());
01565       case Intrinsic::ctpop:
01566         return ConstantInt::get(Ty, Op->getValue().countPopulation());
01567       case Intrinsic::convert_from_fp16: {
01568         APFloat Val(APFloat::IEEEhalf, Op->getValue());
01569 
01570         bool lost = false;
01571         APFloat::opStatus status =
01572           Val.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &lost);
01573 
01574         // Conversion is always precise.
01575         (void)status;
01576         assert(status == APFloat::opOK && !lost &&
01577                "Precision lost during fp16 constfolding");
01578 
01579         return ConstantFP::get(Ty->getContext(), Val);
01580       }
01581       default:
01582         return nullptr;
01583       }
01584     }
01585 
01586     // Support ConstantVector in case we have an Undef in the top.
01587     if (isa<ConstantVector>(Operands[0]) ||
01588         isa<ConstantDataVector>(Operands[0])) {
01589       Constant *Op = cast<Constant>(Operands[0]);
01590       switch (IntrinsicID) {
01591       default: break;
01592       case Intrinsic::x86_sse_cvtss2si:
01593       case Intrinsic::x86_sse_cvtss2si64:
01594       case Intrinsic::x86_sse2_cvtsd2si:
01595       case Intrinsic::x86_sse2_cvtsd2si64:
01596         if (ConstantFP *FPOp =
01597               dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
01598           return ConstantFoldConvertToInt(FPOp->getValueAPF(),
01599                                           /*roundTowardZero=*/false, Ty);
01600       case Intrinsic::x86_sse_cvttss2si:
01601       case Intrinsic::x86_sse_cvttss2si64:
01602       case Intrinsic::x86_sse2_cvttsd2si:
01603       case Intrinsic::x86_sse2_cvttsd2si64:
01604         if (ConstantFP *FPOp =
01605               dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
01606           return ConstantFoldConvertToInt(FPOp->getValueAPF(),
01607                                           /*roundTowardZero=*/true, Ty);
01608       }
01609     }
01610 
01611     if (isa<UndefValue>(Operands[0])) {
01612       if (IntrinsicID == Intrinsic::bswap)
01613         return Operands[0];
01614       return nullptr;
01615     }
01616 
01617     return nullptr;
01618   }
01619 
01620   if (Operands.size() == 2) {
01621     if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
01622       if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
01623         return nullptr;
01624       double Op1V = getValueAsDouble(Op1);
01625 
01626       if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
01627         if (Op2->getType() != Op1->getType())
01628           return nullptr;
01629 
01630         double Op2V = getValueAsDouble(Op2);
01631         if (IntrinsicID == Intrinsic::pow) {
01632           return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
01633         }
01634         if (IntrinsicID == Intrinsic::copysign) {
01635           APFloat V1 = Op1->getValueAPF();
01636           APFloat V2 = Op2->getValueAPF();
01637           V1.copySign(V2);
01638           return ConstantFP::get(Ty->getContext(), V1);
01639         }
01640 
01641         if (IntrinsicID == Intrinsic::minnum) {
01642           const APFloat &C1 = Op1->getValueAPF();
01643           const APFloat &C2 = Op2->getValueAPF();
01644           return ConstantFP::get(Ty->getContext(), minnum(C1, C2));
01645         }
01646 
01647         if (IntrinsicID == Intrinsic::maxnum) {
01648           const APFloat &C1 = Op1->getValueAPF();
01649           const APFloat &C2 = Op2->getValueAPF();
01650           return ConstantFP::get(Ty->getContext(), maxnum(C1, C2));
01651         }
01652 
01653         if (!TLI)
01654           return nullptr;
01655         if (Name == "pow" && TLI->has(LibFunc::pow))
01656           return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
01657         if (Name == "fmod" && TLI->has(LibFunc::fmod))
01658           return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
01659         if (Name == "atan2" && TLI->has(LibFunc::atan2))
01660           return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
01661       } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
01662         if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy())
01663           return ConstantFP::get(Ty->getContext(),
01664                                  APFloat((float)std::pow((float)Op1V,
01665                                                  (int)Op2C->getZExtValue())));
01666         if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy())
01667           return ConstantFP::get(Ty->getContext(),
01668                                  APFloat((float)std::pow((float)Op1V,
01669                                                  (int)Op2C->getZExtValue())));
01670         if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy())
01671           return ConstantFP::get(Ty->getContext(),
01672                                  APFloat((double)std::pow((double)Op1V,
01673                                                    (int)Op2C->getZExtValue())));
01674       }
01675       return nullptr;
01676     }
01677 
01678     if (ConstantInt *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
01679       if (ConstantInt *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
01680         switch (IntrinsicID) {
01681         default: break;
01682         case Intrinsic::sadd_with_overflow:
01683         case Intrinsic::uadd_with_overflow:
01684         case Intrinsic::ssub_with_overflow:
01685         case Intrinsic::usub_with_overflow:
01686         case Intrinsic::smul_with_overflow:
01687         case Intrinsic::umul_with_overflow: {
01688           APInt Res;
01689           bool Overflow;
01690           switch (IntrinsicID) {
01691           default: llvm_unreachable("Invalid case");
01692           case Intrinsic::sadd_with_overflow:
01693             Res = Op1->getValue().sadd_ov(Op2->getValue(), Overflow);
01694             break;
01695           case Intrinsic::uadd_with_overflow:
01696             Res = Op1->getValue().uadd_ov(Op2->getValue(), Overflow);
01697             break;
01698           case Intrinsic::ssub_with_overflow:
01699             Res = Op1->getValue().ssub_ov(Op2->getValue(), Overflow);
01700             break;
01701           case Intrinsic::usub_with_overflow:
01702             Res = Op1->getValue().usub_ov(Op2->getValue(), Overflow);
01703             break;
01704           case Intrinsic::smul_with_overflow:
01705             Res = Op1->getValue().smul_ov(Op2->getValue(), Overflow);
01706             break;
01707           case Intrinsic::umul_with_overflow:
01708             Res = Op1->getValue().umul_ov(Op2->getValue(), Overflow);
01709             break;
01710           }
01711           Constant *Ops[] = {
01712             ConstantInt::get(Ty->getContext(), Res),
01713             ConstantInt::get(Type::getInt1Ty(Ty->getContext()), Overflow)
01714           };
01715           return ConstantStruct::get(cast<StructType>(Ty), Ops);
01716         }
01717         case Intrinsic::cttz:
01718           if (Op2->isOne() && Op1->isZero()) // cttz(0, 1) is undef.
01719             return UndefValue::get(Ty);
01720           return ConstantInt::get(Ty, Op1->getValue().countTrailingZeros());
01721         case Intrinsic::ctlz:
01722           if (Op2->isOne() && Op1->isZero()) // ctlz(0, 1) is undef.
01723             return UndefValue::get(Ty);
01724           return ConstantInt::get(Ty, Op1->getValue().countLeadingZeros());
01725         }
01726       }
01727 
01728       return nullptr;
01729     }
01730     return nullptr;
01731   }
01732 
01733   if (Operands.size() != 3)
01734     return nullptr;
01735 
01736   if (const ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
01737     if (const ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
01738       if (const ConstantFP *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
01739         switch (IntrinsicID) {
01740         default: break;
01741         case Intrinsic::fma:
01742         case Intrinsic::fmuladd: {
01743           APFloat V = Op1->getValueAPF();
01744           APFloat::opStatus s = V.fusedMultiplyAdd(Op2->getValueAPF(),
01745                                                    Op3->getValueAPF(),
01746                                                    APFloat::rmNearestTiesToEven);
01747           if (s != APFloat::opInvalidOp)
01748             return ConstantFP::get(Ty->getContext(), V);
01749 
01750           return nullptr;
01751         }
01752         }
01753       }
01754     }
01755   }
01756 
01757   return nullptr;
01758 }
01759 
01760 static Constant *ConstantFoldVectorCall(StringRef Name, unsigned IntrinsicID,
01761                                         VectorType *VTy,
01762                                         ArrayRef<Constant *> Operands,
01763                                         const TargetLibraryInfo *TLI) {
01764   SmallVector<Constant *, 4> Result(VTy->getNumElements());
01765   SmallVector<Constant *, 4> Lane(Operands.size());
01766   Type *Ty = VTy->getElementType();
01767 
01768   for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
01769     // Gather a column of constants.
01770     for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) {
01771       Constant *Agg = Operands[J]->getAggregateElement(I);
01772       if (!Agg)
01773         return nullptr;
01774 
01775       Lane[J] = Agg;
01776     }
01777 
01778     // Use the regular scalar folding to simplify this column.
01779     Constant *Folded = ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI);
01780     if (!Folded)
01781       return nullptr;
01782     Result[I] = Folded;
01783   }
01784 
01785   return ConstantVector::get(Result);
01786 }
01787 
01788 /// Attempt to constant fold a call to the specified function
01789 /// with the specified arguments, returning null if unsuccessful.
01790 Constant *
01791 llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
01792                        const TargetLibraryInfo *TLI) {
01793   if (!F->hasName())
01794     return nullptr;
01795   StringRef Name = F->getName();
01796 
01797   Type *Ty = F->getReturnType();
01798 
01799   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
01800     return ConstantFoldVectorCall(Name, F->getIntrinsicID(), VTy, Operands, TLI);
01801 
01802   return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI);
01803 }