1198090Srdivacky//===-- ConstantFolding.cpp - Fold instructions into constants ------------===// 2193323Sed// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6193323Sed// 7193323Sed//===----------------------------------------------------------------------===// 8193323Sed// 9198090Srdivacky// This file defines routines for folding instructions into constants. 10193323Sed// 11249423Sdim// Also, to supplement the basic IR ConstantExpr simplifications, 12198090Srdivacky// this file defines some additional folding routines that can make use of 13249423Sdim// DataLayout information. These functions cannot go in IR due to library 14198090Srdivacky// dependency issues. 15198090Srdivacky// 16193323Sed//===----------------------------------------------------------------------===// 17193323Sed 18193323Sed#include "llvm/Analysis/ConstantFolding.h" 19314564Sdim#include "llvm/ADT/APFloat.h" 20314564Sdim#include "llvm/ADT/APInt.h" 21314564Sdim#include "llvm/ADT/ArrayRef.h" 22314564Sdim#include "llvm/ADT/DenseMap.h" 23309124Sdim#include "llvm/ADT/STLExtras.h" 24321369Sdim#include "llvm/ADT/SmallVector.h" 25314564Sdim#include "llvm/ADT/StringRef.h" 26288943Sdim#include "llvm/Analysis/TargetLibraryInfo.h" 27249423Sdim#include "llvm/Analysis/ValueTracking.h" 28353358Sdim#include "llvm/Analysis/VectorUtils.h" 29276479Sdim#include "llvm/Config/config.h" 30314564Sdim#include "llvm/IR/Constant.h" 31249423Sdim#include "llvm/IR/Constants.h" 32249423Sdim#include "llvm/IR/DataLayout.h" 33249423Sdim#include "llvm/IR/DerivedTypes.h" 34249423Sdim#include "llvm/IR/Function.h" 35314564Sdim#include "llvm/IR/GlobalValue.h" 36249423Sdim#include "llvm/IR/GlobalVariable.h" 37314564Sdim#include "llvm/IR/InstrTypes.h" 38314564Sdim#include "llvm/IR/Instruction.h" 39249423Sdim#include "llvm/IR/Instructions.h" 40360784Sdim#include "llvm/IR/Intrinsics.h" 41360784Sdim#include "llvm/IR/IntrinsicsX86.h" 42249423Sdim#include "llvm/IR/Operator.h" 43314564Sdim#include "llvm/IR/Type.h" 44314564Sdim#include "llvm/IR/Value.h" 45314564Sdim#include "llvm/Support/Casting.h" 46198090Srdivacky#include "llvm/Support/ErrorHandling.h" 47321369Sdim#include "llvm/Support/KnownBits.h" 48193323Sed#include "llvm/Support/MathExtras.h" 49309124Sdim#include <cassert> 50193323Sed#include <cerrno> 51309124Sdim#include <cfenv> 52193323Sed#include <cmath> 53314564Sdim#include <cstddef> 54314564Sdim#include <cstdint> 55276479Sdim 56193323Sedusing namespace llvm; 57193323Sed 58309124Sdimnamespace { 59309124Sdim 60193323Sed//===----------------------------------------------------------------------===// 61193323Sed// Constant Folding internal helper functions 62193323Sed//===----------------------------------------------------------------------===// 63193323Sed 64314564Sdimstatic Constant *foldConstVectorToAPInt(APInt &Result, Type *DestTy, 65314564Sdim Constant *C, Type *SrcEltTy, 66314564Sdim unsigned NumSrcElts, 67314564Sdim const DataLayout &DL) { 68314564Sdim // Now that we know that the input value is a vector of integers, just shift 69314564Sdim // and insert them into our result. 70314564Sdim unsigned BitShift = DL.getTypeSizeInBits(SrcEltTy); 71314564Sdim for (unsigned i = 0; i != NumSrcElts; ++i) { 72314564Sdim Constant *Element; 73314564Sdim if (DL.isLittleEndian()) 74314564Sdim Element = C->getAggregateElement(NumSrcElts - i - 1); 75314564Sdim else 76314564Sdim Element = C->getAggregateElement(i); 77314564Sdim 78314564Sdim if (Element && isa<UndefValue>(Element)) { 79314564Sdim Result <<= BitShift; 80314564Sdim continue; 81314564Sdim } 82314564Sdim 83314564Sdim auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element); 84314564Sdim if (!ElementCI) 85314564Sdim return ConstantExpr::getBitCast(C, DestTy); 86314564Sdim 87314564Sdim Result <<= BitShift; 88314564Sdim Result |= ElementCI->getValue().zextOrSelf(Result.getBitWidth()); 89314564Sdim } 90314564Sdim 91314564Sdim return nullptr; 92314564Sdim} 93314564Sdim 94280031Sdim/// Constant fold bitcast, symbolically evaluating it with DataLayout. 95280031Sdim/// This always returns a non-null constant, but it may be a 96198892Srdivacky/// ConstantExpr if unfoldable. 97309124SdimConstant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { 98360784Sdim assert(CastInst::castIsValid(Instruction::BitCast, C, DestTy) && 99360784Sdim "Invalid constantexpr bitcast!"); 100360784Sdim 101226633Sdim // Catch the obvious splat cases. 102226633Sdim if (C->isNullValue() && !DestTy->isX86_MMXTy()) 103226633Sdim return Constant::getNullValue(DestTy); 104280031Sdim if (C->isAllOnesValue() && !DestTy->isX86_MMXTy() && 105280031Sdim !DestTy->isPtrOrPtrVectorTy()) // Don't get ones for ptr types! 106226633Sdim return Constant::getAllOnesValue(DestTy); 107226633Sdim 108314564Sdim if (auto *VTy = dyn_cast<VectorType>(C->getType())) { 109314564Sdim // Handle a vector->scalar integer/fp cast. 110314564Sdim if (isa<IntegerType>(DestTy) || DestTy->isFloatingPointTy()) { 111314564Sdim unsigned NumSrcElts = VTy->getNumElements(); 112314564Sdim Type *SrcEltTy = VTy->getElementType(); 113234353Sdim 114314564Sdim // If the vector is a vector of floating point, convert it to vector of int 115314564Sdim // to simplify things. 116314564Sdim if (SrcEltTy->isFloatingPointTy()) { 117314564Sdim unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits(); 118314564Sdim Type *SrcIVTy = 119314564Sdim VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElts); 120314564Sdim // Ask IR to do the conversion now that #elts line up. 121314564Sdim C = ConstantExpr::getBitCast(C, SrcIVTy); 122314564Sdim } 123243830Sdim 124314564Sdim APInt Result(DL.getTypeSizeInBits(DestTy), 0); 125314564Sdim if (Constant *CE = foldConstVectorToAPInt(Result, DestTy, C, 126314564Sdim SrcEltTy, NumSrcElts, DL)) 127314564Sdim return CE; 128243830Sdim 129314564Sdim if (isa<IntegerType>(DestTy)) 130314564Sdim return ConstantInt::get(DestTy, Result); 131309124Sdim 132314564Sdim APFloat FP(DestTy->getFltSemantics(), Result); 133314564Sdim return ConstantFP::get(DestTy->getContext(), FP); 134234353Sdim } 135234353Sdim } 136243830Sdim 137226633Sdim // The code below only handles casts to vectors currently. 138309124Sdim auto *DestVTy = dyn_cast<VectorType>(DestTy); 139276479Sdim if (!DestVTy) 140198892Srdivacky return ConstantExpr::getBitCast(C, DestTy); 141243830Sdim 142198892Srdivacky // If this is a scalar -> vector cast, convert the input into a <1 x scalar> 143198892Srdivacky // vector so the code below can handle it uniformly. 144198892Srdivacky if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) { 145198892Srdivacky Constant *Ops = C; // don't take the address of C! 146288943Sdim return FoldBitCast(ConstantVector::get(Ops), DestTy, DL); 147198892Srdivacky } 148243830Sdim 149198892Srdivacky // If this is a bitcast from constant vector -> vector, fold it. 150234353Sdim if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C)) 151198892Srdivacky return ConstantExpr::getBitCast(C, DestTy); 152243830Sdim 153249423Sdim // If the element types match, IR can fold it. 154198892Srdivacky unsigned NumDstElt = DestVTy->getNumElements(); 155234353Sdim unsigned NumSrcElt = C->getType()->getVectorNumElements(); 156198892Srdivacky if (NumDstElt == NumSrcElt) 157198892Srdivacky return ConstantExpr::getBitCast(C, DestTy); 158243830Sdim 159234353Sdim Type *SrcEltTy = C->getType()->getVectorElementType(); 160226633Sdim Type *DstEltTy = DestVTy->getElementType(); 161243830Sdim 162243830Sdim // Otherwise, we're changing the number of elements in a vector, which 163198892Srdivacky // requires endianness information to do the right thing. For example, 164198892Srdivacky // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>) 165198892Srdivacky // folds to (little endian): 166198892Srdivacky // <4 x i32> <i32 0, i32 0, i32 1, i32 0> 167198892Srdivacky // and to (big endian): 168198892Srdivacky // <4 x i32> <i32 0, i32 0, i32 0, i32 1> 169243830Sdim 170198892Srdivacky // First thing is first. We only want to think about integer here, so if 171198892Srdivacky // we have something in FP form, recast it as integer. 172203954Srdivacky if (DstEltTy->isFloatingPointTy()) { 173198892Srdivacky // Fold to an vector of integers with same size as our FP type. 174198892Srdivacky unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits(); 175226633Sdim Type *DestIVTy = 176198892Srdivacky VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt); 177198892Srdivacky // Recursively handle this integer conversion, if possible. 178288943Sdim C = FoldBitCast(C, DestIVTy, DL); 179243830Sdim 180249423Sdim // Finally, IR can handle this now that #elts line up. 181198892Srdivacky return ConstantExpr::getBitCast(C, DestTy); 182198892Srdivacky } 183243830Sdim 184198892Srdivacky // Okay, we know the destination is integer, if the input is FP, convert 185198892Srdivacky // it to integer first. 186203954Srdivacky if (SrcEltTy->isFloatingPointTy()) { 187198892Srdivacky unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits(); 188226633Sdim Type *SrcIVTy = 189198892Srdivacky VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElt); 190249423Sdim // Ask IR to do the conversion now that #elts line up. 191198892Srdivacky C = ConstantExpr::getBitCast(C, SrcIVTy); 192249423Sdim // If IR wasn't able to fold it, bail out. 193234353Sdim if (!isa<ConstantVector>(C) && // FIXME: Remove ConstantVector. 194234353Sdim !isa<ConstantDataVector>(C)) 195198892Srdivacky return C; 196198892Srdivacky } 197243830Sdim 198198892Srdivacky // Now we know that the input and output vectors are both integer vectors 199198892Srdivacky // of the same size, and that their #elements is not the same. Do the 200198892Srdivacky // conversion here, which depends on whether the input or output has 201198892Srdivacky // more elements. 202288943Sdim bool isLittleEndian = DL.isLittleEndian(); 203243830Sdim 204198892Srdivacky SmallVector<Constant*, 32> Result; 205198892Srdivacky if (NumDstElt < NumSrcElt) { 206198892Srdivacky // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>) 207198892Srdivacky Constant *Zero = Constant::getNullValue(DstEltTy); 208198892Srdivacky unsigned Ratio = NumSrcElt/NumDstElt; 209198892Srdivacky unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits(); 210198892Srdivacky unsigned SrcElt = 0; 211198892Srdivacky for (unsigned i = 0; i != NumDstElt; ++i) { 212198892Srdivacky // Build each element of the result. 213198892Srdivacky Constant *Elt = Zero; 214198892Srdivacky unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1); 215198892Srdivacky for (unsigned j = 0; j != Ratio; ++j) { 216314564Sdim Constant *Src = C->getAggregateElement(SrcElt++); 217314564Sdim if (Src && isa<UndefValue>(Src)) 218314564Sdim Src = Constant::getNullValue(C->getType()->getVectorElementType()); 219314564Sdim else 220314564Sdim Src = dyn_cast_or_null<ConstantInt>(Src); 221198892Srdivacky if (!Src) // Reject constantexpr elements. 222198892Srdivacky return ConstantExpr::getBitCast(C, DestTy); 223243830Sdim 224198892Srdivacky // Zero extend the element to the right size. 225198892Srdivacky Src = ConstantExpr::getZExt(Src, Elt->getType()); 226243830Sdim 227198892Srdivacky // Shift it to the right place, depending on endianness. 228243830Sdim Src = ConstantExpr::getShl(Src, 229198892Srdivacky ConstantInt::get(Src->getType(), ShiftAmt)); 230198892Srdivacky ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize; 231243830Sdim 232198892Srdivacky // Mix it in. 233198892Srdivacky Elt = ConstantExpr::getOr(Elt, Src); 234198892Srdivacky } 235198892Srdivacky Result.push_back(Elt); 236198892Srdivacky } 237234353Sdim return ConstantVector::get(Result); 238234353Sdim } 239243830Sdim 240234353Sdim // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>) 241234353Sdim unsigned Ratio = NumDstElt/NumSrcElt; 242288943Sdim unsigned DstBitSize = DL.getTypeSizeInBits(DstEltTy); 243243830Sdim 244234353Sdim // Loop over each source value, expanding into multiple results. 245234353Sdim for (unsigned i = 0; i != NumSrcElt; ++i) { 246314564Sdim auto *Element = C->getAggregateElement(i); 247314564Sdim 248314564Sdim if (!Element) // Reject constantexpr elements. 249234353Sdim return ConstantExpr::getBitCast(C, DestTy); 250243830Sdim 251314564Sdim if (isa<UndefValue>(Element)) { 252314564Sdim // Correctly Propagate undef values. 253314564Sdim Result.append(Ratio, UndefValue::get(DstEltTy)); 254314564Sdim continue; 255314564Sdim } 256314564Sdim 257314564Sdim auto *Src = dyn_cast<ConstantInt>(Element); 258314564Sdim if (!Src) 259314564Sdim return ConstantExpr::getBitCast(C, DestTy); 260314564Sdim 261234353Sdim unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1); 262234353Sdim for (unsigned j = 0; j != Ratio; ++j) { 263234353Sdim // Shift the piece of the value into the right place, depending on 264234353Sdim // endianness. 265243830Sdim Constant *Elt = ConstantExpr::getLShr(Src, 266234353Sdim ConstantInt::get(Src->getType(), ShiftAmt)); 267234353Sdim ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize; 268243830Sdim 269280031Sdim // Truncate the element to an integer with the same pointer size and 270280031Sdim // convert the element back to a pointer using a inttoptr. 271280031Sdim if (DstEltTy->isPointerTy()) { 272280031Sdim IntegerType *DstIntTy = Type::getIntNTy(C->getContext(), DstBitSize); 273280031Sdim Constant *CE = ConstantExpr::getTrunc(Elt, DstIntTy); 274280031Sdim Result.push_back(ConstantExpr::getIntToPtr(CE, DstEltTy)); 275280031Sdim continue; 276280031Sdim } 277280031Sdim 278234353Sdim // Truncate and remember this piece. 279234353Sdim Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy)); 280198892Srdivacky } 281198892Srdivacky } 282243830Sdim 283218893Sdim return ConstantVector::get(Result); 284198892Srdivacky} 285198892Srdivacky 286309124Sdim} // end anonymous namespace 287198892Srdivacky 288280031Sdim/// If this constant is a constant offset from a global, return the global and 289280031Sdim/// the constant. Because of constantexprs, this function is recursive. 290309124Sdimbool llvm::IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, 291309124Sdim APInt &Offset, const DataLayout &DL) { 292193323Sed // Trivial case, constant is the global. 293193323Sed if ((GV = dyn_cast<GlobalValue>(C))) { 294341825Sdim unsigned BitWidth = DL.getIndexTypeSizeInBits(GV->getType()); 295261991Sdim Offset = APInt(BitWidth, 0); 296193323Sed return true; 297193323Sed } 298243830Sdim 299193323Sed // Otherwise, if this isn't a constant expr, bail out. 300309124Sdim auto *CE = dyn_cast<ConstantExpr>(C); 301193323Sed if (!CE) return false; 302243830Sdim 303193323Sed // Look through ptr->int and ptr->ptr casts. 304193323Sed if (CE->getOpcode() == Instruction::PtrToInt || 305296417Sdim CE->getOpcode() == Instruction::BitCast) 306288943Sdim return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, DL); 307243830Sdim 308243830Sdim // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) 309309124Sdim auto *GEP = dyn_cast<GEPOperator>(CE); 310261991Sdim if (!GEP) 311261991Sdim return false; 312243830Sdim 313341825Sdim unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType()); 314261991Sdim APInt TmpOffset(BitWidth, 0); 315243830Sdim 316261991Sdim // If the base isn't a global+constant, we aren't either. 317288943Sdim if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, TmpOffset, DL)) 318261991Sdim return false; 319261991Sdim 320261991Sdim // Otherwise, add any offset that our operands provide. 321288943Sdim if (!GEP->accumulateConstantOffset(DL, TmpOffset)) 322261991Sdim return false; 323261991Sdim 324261991Sdim Offset = TmpOffset; 325261991Sdim return true; 326193323Sed} 327193323Sed 328341825SdimConstant *llvm::ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, 329341825Sdim const DataLayout &DL) { 330341825Sdim do { 331341825Sdim Type *SrcTy = C->getType(); 332341825Sdim 333341825Sdim // If the type sizes are the same and a cast is legal, just directly 334341825Sdim // cast the constant. 335341825Sdim if (DL.getTypeSizeInBits(DestTy) == DL.getTypeSizeInBits(SrcTy)) { 336341825Sdim Instruction::CastOps Cast = Instruction::BitCast; 337341825Sdim // If we are going from a pointer to int or vice versa, we spell the cast 338341825Sdim // differently. 339341825Sdim if (SrcTy->isIntegerTy() && DestTy->isPointerTy()) 340341825Sdim Cast = Instruction::IntToPtr; 341341825Sdim else if (SrcTy->isPointerTy() && DestTy->isIntegerTy()) 342341825Sdim Cast = Instruction::PtrToInt; 343341825Sdim 344341825Sdim if (CastInst::castIsValid(Cast, C, DestTy)) 345341825Sdim return ConstantExpr::getCast(Cast, C, DestTy); 346341825Sdim } 347341825Sdim 348341825Sdim // If this isn't an aggregate type, there is nothing we can do to drill down 349341825Sdim // and find a bitcastable constant. 350341825Sdim if (!SrcTy->isAggregateType()) 351341825Sdim return nullptr; 352341825Sdim 353341825Sdim // We're simulating a load through a pointer that was bitcast to point to 354341825Sdim // a different type, so we can try to walk down through the initial 355344779Sdim // elements of an aggregate to see if some part of the aggregate is 356341825Sdim // castable to implement the "load" semantic model. 357344779Sdim if (SrcTy->isStructTy()) { 358344779Sdim // Struct types might have leading zero-length elements like [0 x i32], 359344779Sdim // which are certainly not what we are looking for, so skip them. 360344779Sdim unsigned Elem = 0; 361344779Sdim Constant *ElemC; 362344779Sdim do { 363344779Sdim ElemC = C->getAggregateElement(Elem++); 364344779Sdim } while (ElemC && DL.getTypeSizeInBits(ElemC->getType()) == 0); 365344779Sdim C = ElemC; 366344779Sdim } else { 367344779Sdim C = C->getAggregateElement(0u); 368344779Sdim } 369341825Sdim } while (C); 370341825Sdim 371341825Sdim return nullptr; 372341825Sdim} 373341825Sdim 374309124Sdimnamespace { 375309124Sdim 376280031Sdim/// Recursive helper to read bits out of global. C is the constant being copied 377280031Sdim/// out of. ByteOffset is an offset into C. CurPtr is the pointer to copy 378280031Sdim/// results into and BytesLeft is the number of bytes left in 379288943Sdim/// the CurPtr buffer. DL is the DataLayout. 380309124Sdimbool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr, 381309124Sdim unsigned BytesLeft, const DataLayout &DL) { 382288943Sdim assert(ByteOffset <= DL.getTypeAllocSize(C->getType()) && 383198396Srdivacky "Out of range access"); 384243830Sdim 385198892Srdivacky // If this element is zero or undefined, we can just return since *CurPtr is 386198892Srdivacky // zero initialized. 387198396Srdivacky if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) 388198396Srdivacky return true; 389243830Sdim 390309124Sdim if (auto *CI = dyn_cast<ConstantInt>(C)) { 391198396Srdivacky if (CI->getBitWidth() > 64 || 392198396Srdivacky (CI->getBitWidth() & 7) != 0) 393198396Srdivacky return false; 394243830Sdim 395198396Srdivacky uint64_t Val = CI->getZExtValue(); 396198396Srdivacky unsigned IntBytes = unsigned(CI->getBitWidth()/8); 397243830Sdim 398198396Srdivacky for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) { 399243830Sdim int n = ByteOffset; 400288943Sdim if (!DL.isLittleEndian()) 401243830Sdim n = IntBytes - n - 1; 402243830Sdim CurPtr[i] = (unsigned char)(Val >> (n * 8)); 403198396Srdivacky ++ByteOffset; 404198396Srdivacky } 405198396Srdivacky return true; 406198396Srdivacky } 407243830Sdim 408309124Sdim if (auto *CFP = dyn_cast<ConstantFP>(C)) { 409198396Srdivacky if (CFP->getType()->isDoubleTy()) { 410288943Sdim C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), DL); 411288943Sdim return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL); 412198396Srdivacky } 413198396Srdivacky if (CFP->getType()->isFloatTy()){ 414288943Sdim C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), DL); 415288943Sdim return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL); 416198396Srdivacky } 417249423Sdim if (CFP->getType()->isHalfTy()){ 418288943Sdim C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), DL); 419288943Sdim return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL); 420249423Sdim } 421198892Srdivacky return false; 422198396Srdivacky } 423243830Sdim 424309124Sdim if (auto *CS = dyn_cast<ConstantStruct>(C)) { 425288943Sdim const StructLayout *SL = DL.getStructLayout(CS->getType()); 426198396Srdivacky unsigned Index = SL->getElementContainingOffset(ByteOffset); 427198396Srdivacky uint64_t CurEltOffset = SL->getElementOffset(Index); 428198396Srdivacky ByteOffset -= CurEltOffset; 429243830Sdim 430314564Sdim while (true) { 431198396Srdivacky // If the element access is to the element itself and not to tail padding, 432198396Srdivacky // read the bytes from the element. 433288943Sdim uint64_t EltSize = DL.getTypeAllocSize(CS->getOperand(Index)->getType()); 434198396Srdivacky 435198396Srdivacky if (ByteOffset < EltSize && 436198396Srdivacky !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr, 437288943Sdim BytesLeft, DL)) 438198396Srdivacky return false; 439243830Sdim 440198396Srdivacky ++Index; 441243830Sdim 442198396Srdivacky // Check to see if we read from the last struct element, if so we're done. 443198396Srdivacky if (Index == CS->getType()->getNumElements()) 444198396Srdivacky return true; 445198396Srdivacky 446198396Srdivacky // If we read all of the bytes we needed from this element we're done. 447198396Srdivacky uint64_t NextEltOffset = SL->getElementOffset(Index); 448198396Srdivacky 449261991Sdim if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset) 450198396Srdivacky return true; 451198396Srdivacky 452198396Srdivacky // Move to the next element of the struct. 453261991Sdim CurPtr += NextEltOffset - CurEltOffset - ByteOffset; 454261991Sdim BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset; 455198396Srdivacky ByteOffset = 0; 456198396Srdivacky CurEltOffset = NextEltOffset; 457198396Srdivacky } 458198396Srdivacky // not reached. 459198396Srdivacky } 460198396Srdivacky 461234353Sdim if (isa<ConstantArray>(C) || isa<ConstantVector>(C) || 462234353Sdim isa<ConstantDataSequential>(C)) { 463261991Sdim Type *EltTy = C->getType()->getSequentialElementType(); 464288943Sdim uint64_t EltSize = DL.getTypeAllocSize(EltTy); 465198396Srdivacky uint64_t Index = ByteOffset / EltSize; 466198396Srdivacky uint64_t Offset = ByteOffset - Index * EltSize; 467234353Sdim uint64_t NumElts; 468309124Sdim if (auto *AT = dyn_cast<ArrayType>(C->getType())) 469234353Sdim NumElts = AT->getNumElements(); 470234353Sdim else 471261991Sdim NumElts = C->getType()->getVectorNumElements(); 472239462Sdim 473234353Sdim for (; Index != NumElts; ++Index) { 474234353Sdim if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr, 475288943Sdim BytesLeft, DL)) 476198396Srdivacky return false; 477239462Sdim 478239462Sdim uint64_t BytesWritten = EltSize - Offset; 479239462Sdim assert(BytesWritten <= EltSize && "Not indexing into this element?"); 480239462Sdim if (BytesWritten >= BytesLeft) 481198396Srdivacky return true; 482239462Sdim 483198396Srdivacky Offset = 0; 484239462Sdim BytesLeft -= BytesWritten; 485239462Sdim CurPtr += BytesWritten; 486198396Srdivacky } 487198396Srdivacky return true; 488198396Srdivacky } 489243830Sdim 490309124Sdim if (auto *CE = dyn_cast<ConstantExpr>(C)) { 491218893Sdim if (CE->getOpcode() == Instruction::IntToPtr && 492288943Sdim CE->getOperand(0)->getType() == DL.getIntPtrType(CE->getType())) { 493243830Sdim return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr, 494288943Sdim BytesLeft, DL); 495261991Sdim } 496218893Sdim } 497218893Sdim 498198396Srdivacky // Otherwise, unknown initializer type. 499198396Srdivacky return false; 500198396Srdivacky} 501198396Srdivacky 502309124SdimConstant *FoldReinterpretLoadFromConstPtr(Constant *C, Type *LoadTy, 503309124Sdim const DataLayout &DL) { 504309124Sdim auto *PTy = cast<PointerType>(C->getType()); 505309124Sdim auto *IntType = dyn_cast<IntegerType>(LoadTy); 506243830Sdim 507198396Srdivacky // If this isn't an integer load we can't fold it directly. 508198396Srdivacky if (!IntType) { 509261991Sdim unsigned AS = PTy->getAddressSpace(); 510261991Sdim 511198396Srdivacky // If this is a float/double load, we can try folding it as an int32/64 load 512198396Srdivacky // and then bitcast the result. This can be useful for union cases. Note 513198396Srdivacky // that address spaces don't matter here since we're not going to result in 514198396Srdivacky // an actual new load. 515226633Sdim Type *MapTy; 516249423Sdim if (LoadTy->isHalfTy()) 517309124Sdim MapTy = Type::getInt16Ty(C->getContext()); 518249423Sdim else if (LoadTy->isFloatTy()) 519309124Sdim MapTy = Type::getInt32Ty(C->getContext()); 520198396Srdivacky else if (LoadTy->isDoubleTy()) 521309124Sdim MapTy = Type::getInt64Ty(C->getContext()); 522204642Srdivacky else if (LoadTy->isVectorTy()) { 523309124Sdim MapTy = PointerType::getIntNTy(C->getContext(), 524353358Sdim DL.getTypeSizeInBits(LoadTy)); 525198396Srdivacky } else 526276479Sdim return nullptr; 527198396Srdivacky 528309124Sdim C = FoldBitCast(C, MapTy->getPointerTo(AS), DL); 529360784Sdim if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, MapTy, DL)) { 530360784Sdim if (Res->isNullValue() && !LoadTy->isX86_MMXTy()) 531360784Sdim // Materializing a zero can be done trivially without a bitcast 532360784Sdim return Constant::getNullValue(LoadTy); 533360784Sdim Type *CastTy = LoadTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(LoadTy) : LoadTy; 534360784Sdim Res = FoldBitCast(Res, CastTy, DL); 535360784Sdim if (LoadTy->isPtrOrPtrVectorTy()) { 536360784Sdim // For vector of pointer, we needed to first convert to a vector of integer, then do vector inttoptr 537360784Sdim if (Res->isNullValue() && !LoadTy->isX86_MMXTy()) 538360784Sdim return Constant::getNullValue(LoadTy); 539360784Sdim if (DL.isNonIntegralPointerType(LoadTy->getScalarType())) 540360784Sdim // Be careful not to replace a load of an addrspace value with an inttoptr here 541360784Sdim return nullptr; 542360784Sdim Res = ConstantExpr::getCast(Instruction::IntToPtr, Res, LoadTy); 543360784Sdim } 544360784Sdim return Res; 545360784Sdim } 546276479Sdim return nullptr; 547198396Srdivacky } 548243830Sdim 549198396Srdivacky unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8; 550261991Sdim if (BytesLoaded > 32 || BytesLoaded == 0) 551276479Sdim return nullptr; 552243830Sdim 553198396Srdivacky GlobalValue *GVal; 554309124Sdim APInt OffsetAI; 555309124Sdim if (!IsConstantOffsetFromGlobal(C, GVal, OffsetAI, DL)) 556276479Sdim return nullptr; 557243830Sdim 558309124Sdim auto *GV = dyn_cast<GlobalVariable>(GVal); 559198892Srdivacky if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() || 560198396Srdivacky !GV->getInitializer()->getType()->isSized()) 561276479Sdim return nullptr; 562198396Srdivacky 563309124Sdim int64_t Offset = OffsetAI.getSExtValue(); 564309124Sdim int64_t InitializerSize = DL.getTypeAllocSize(GV->getInitializer()->getType()); 565243830Sdim 566198396Srdivacky // If we're not accessing anything in this constant, the result is undefined. 567360784Sdim if (Offset <= -1 * static_cast<int64_t>(BytesLoaded)) 568198396Srdivacky return UndefValue::get(IntType); 569243830Sdim 570309124Sdim // If we're not accessing anything in this constant, the result is undefined. 571309124Sdim if (Offset >= InitializerSize) 572309124Sdim return UndefValue::get(IntType); 573309124Sdim 574198396Srdivacky unsigned char RawBytes[32] = {0}; 575309124Sdim unsigned char *CurPtr = RawBytes; 576309124Sdim unsigned BytesLeft = BytesLoaded; 577309124Sdim 578309124Sdim // If we're loading off the beginning of the global, some bytes may be valid. 579309124Sdim if (Offset < 0) { 580309124Sdim CurPtr += -Offset; 581309124Sdim BytesLeft += Offset; 582309124Sdim Offset = 0; 583309124Sdim } 584309124Sdim 585309124Sdim if (!ReadDataFromGlobal(GV->getInitializer(), Offset, CurPtr, BytesLeft, DL)) 586276479Sdim return nullptr; 587198396Srdivacky 588243830Sdim APInt ResultVal = APInt(IntType->getBitWidth(), 0); 589288943Sdim if (DL.isLittleEndian()) { 590243830Sdim ResultVal = RawBytes[BytesLoaded - 1]; 591243830Sdim for (unsigned i = 1; i != BytesLoaded; ++i) { 592243830Sdim ResultVal <<= 8; 593261991Sdim ResultVal |= RawBytes[BytesLoaded - 1 - i]; 594243830Sdim } 595243830Sdim } else { 596243830Sdim ResultVal = RawBytes[0]; 597243830Sdim for (unsigned i = 1; i != BytesLoaded; ++i) { 598243830Sdim ResultVal <<= 8; 599243830Sdim ResultVal |= RawBytes[i]; 600243830Sdim } 601198396Srdivacky } 602198396Srdivacky 603198396Srdivacky return ConstantInt::get(IntType->getContext(), ResultVal); 604198396Srdivacky} 605198396Srdivacky 606341825SdimConstant *ConstantFoldLoadThroughBitcastExpr(ConstantExpr *CE, Type *DestTy, 607341825Sdim const DataLayout &DL) { 608309124Sdim auto *SrcPtr = CE->getOperand(0); 609309124Sdim auto *SrcPtrTy = dyn_cast<PointerType>(SrcPtr->getType()); 610309124Sdim if (!SrcPtrTy) 611276479Sdim return nullptr; 612309124Sdim Type *SrcTy = SrcPtrTy->getPointerElementType(); 613276479Sdim 614309124Sdim Constant *C = ConstantFoldLoadFromConstPtr(SrcPtr, SrcTy, DL); 615276479Sdim if (!C) 616276479Sdim return nullptr; 617276479Sdim 618341825Sdim return llvm::ConstantFoldLoadThroughBitcast(C, DestTy, DL); 619276479Sdim} 620276479Sdim 621309124Sdim} // end anonymous namespace 622309124Sdim 623309124SdimConstant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, 624288943Sdim const DataLayout &DL) { 625198396Srdivacky // First, try the easy cases: 626309124Sdim if (auto *GV = dyn_cast<GlobalVariable>(C)) 627198396Srdivacky if (GV->isConstant() && GV->hasDefinitiveInitializer()) 628198396Srdivacky return GV->getInitializer(); 629198396Srdivacky 630296417Sdim if (auto *GA = dyn_cast<GlobalAlias>(C)) 631309124Sdim if (GA->getAliasee() && !GA->isInterposable()) 632309124Sdim return ConstantFoldLoadFromConstPtr(GA->getAliasee(), Ty, DL); 633296417Sdim 634198396Srdivacky // If the loaded value isn't a constant expr, we can't handle it. 635309124Sdim auto *CE = dyn_cast<ConstantExpr>(C); 636261991Sdim if (!CE) 637276479Sdim return nullptr; 638243830Sdim 639198396Srdivacky if (CE->getOpcode() == Instruction::GetElementPtr) { 640309124Sdim if (auto *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) { 641261991Sdim if (GV->isConstant() && GV->hasDefinitiveInitializer()) { 642243830Sdim if (Constant *V = 643198396Srdivacky ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) 644198396Srdivacky return V; 645261991Sdim } 646261991Sdim } 647198396Srdivacky } 648243830Sdim 649276479Sdim if (CE->getOpcode() == Instruction::BitCast) 650341825Sdim if (Constant *LoadedC = ConstantFoldLoadThroughBitcastExpr(CE, Ty, DL)) 651276479Sdim return LoadedC; 652276479Sdim 653198396Srdivacky // Instead of loading constant c string, use corresponding integer value 654198396Srdivacky // directly if string length is small enough. 655234353Sdim StringRef Str; 656288943Sdim if (getConstantStringInfo(CE, Str) && !Str.empty()) { 657309124Sdim size_t StrLen = Str.size(); 658198396Srdivacky unsigned NumBits = Ty->getPrimitiveSizeInBits(); 659210299Sed // Replace load with immediate integer if the result is an integer or fp 660210299Sed // value. 661210299Sed if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 && 662210299Sed (isa<IntegerType>(Ty) || Ty->isFloatingPointTy())) { 663198396Srdivacky APInt StrVal(NumBits, 0); 664198396Srdivacky APInt SingleChar(NumBits, 0); 665288943Sdim if (DL.isLittleEndian()) { 666309124Sdim for (unsigned char C : reverse(Str.bytes())) { 667309124Sdim SingleChar = static_cast<uint64_t>(C); 668198396Srdivacky StrVal = (StrVal << 8) | SingleChar; 669198396Srdivacky } 670198396Srdivacky } else { 671309124Sdim for (unsigned char C : Str.bytes()) { 672309124Sdim SingleChar = static_cast<uint64_t>(C); 673198396Srdivacky StrVal = (StrVal << 8) | SingleChar; 674198396Srdivacky } 675198396Srdivacky // Append NULL at the end. 676198396Srdivacky SingleChar = 0; 677198396Srdivacky StrVal = (StrVal << 8) | SingleChar; 678198396Srdivacky } 679243830Sdim 680210299Sed Constant *Res = ConstantInt::get(CE->getContext(), StrVal); 681210299Sed if (Ty->isFloatingPointTy()) 682210299Sed Res = ConstantExpr::getBitCast(Res, Ty); 683210299Sed return Res; 684198396Srdivacky } 685198396Srdivacky } 686243830Sdim 687198396Srdivacky // If this load comes from anywhere in a constant global, and if the global 688198396Srdivacky // is all undef or zero, we know what it loads. 689309124Sdim if (auto *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, DL))) { 690198396Srdivacky if (GV->isConstant() && GV->hasDefinitiveInitializer()) { 691198396Srdivacky if (GV->getInitializer()->isNullValue()) 692309124Sdim return Constant::getNullValue(Ty); 693198396Srdivacky if (isa<UndefValue>(GV->getInitializer())) 694309124Sdim return UndefValue::get(Ty); 695198396Srdivacky } 696198396Srdivacky } 697243830Sdim 698243830Sdim // Try hard to fold loads from bitcasted strange and non-type-safe things. 699309124Sdim return FoldReinterpretLoadFromConstPtr(CE, Ty, DL); 700198396Srdivacky} 701198396Srdivacky 702309124Sdimnamespace { 703309124Sdim 704309124SdimConstant *ConstantFoldLoadInst(const LoadInst *LI, const DataLayout &DL) { 705276479Sdim if (LI->isVolatile()) return nullptr; 706243830Sdim 707309124Sdim if (auto *C = dyn_cast<Constant>(LI->getOperand(0))) 708309124Sdim return ConstantFoldLoadFromConstPtr(C, LI->getType(), DL); 709198396Srdivacky 710276479Sdim return nullptr; 711198396Srdivacky} 712198396Srdivacky 713280031Sdim/// One of Op0/Op1 is a constant expression. 714193323Sed/// Attempt to symbolically evaluate the result of a binary operator merging 715249423Sdim/// these together. If target data info is available, it is provided as DL, 716249423Sdim/// otherwise DL is null. 717309124SdimConstant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1, 718309124Sdim const DataLayout &DL) { 719193323Sed // SROA 720243830Sdim 721193323Sed // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl. 722193323Sed // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute 723193323Sed // bits. 724243830Sdim 725288943Sdim if (Opc == Instruction::And) { 726321369Sdim KnownBits Known0 = computeKnownBits(Op0, DL); 727321369Sdim KnownBits Known1 = computeKnownBits(Op1, DL); 728321369Sdim if ((Known1.One | Known0.Zero).isAllOnesValue()) { 729249423Sdim // All the bits of Op0 that the 'and' could be masking are already zero. 730249423Sdim return Op0; 731249423Sdim } 732321369Sdim if ((Known0.One | Known1.Zero).isAllOnesValue()) { 733249423Sdim // All the bits of Op1 that the 'and' could be masking are already zero. 734249423Sdim return Op1; 735249423Sdim } 736249423Sdim 737321369Sdim Known0.Zero |= Known1.Zero; 738321369Sdim Known0.One &= Known1.One; 739321369Sdim if (Known0.isConstant()) 740321369Sdim return ConstantInt::get(Op0->getType(), Known0.getConstant()); 741249423Sdim } 742249423Sdim 743193323Sed // If the constant expr is something like &A[123] - &A[4].f, fold this into a 744193323Sed // constant. This happens frequently when iterating over a global array. 745288943Sdim if (Opc == Instruction::Sub) { 746193323Sed GlobalValue *GV1, *GV2; 747261991Sdim APInt Offs1, Offs2; 748243830Sdim 749288943Sdim if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, DL)) 750288943Sdim if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, DL) && GV1 == GV2) { 751288943Sdim unsigned OpSize = DL.getTypeSizeInBits(Op0->getType()); 752261991Sdim 753193323Sed // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow. 754249423Sdim // PtrToInt may change the bitwidth so we have convert to the right size 755249423Sdim // first. 756249423Sdim return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) - 757249423Sdim Offs2.zextOrTrunc(OpSize)); 758193323Sed } 759193323Sed } 760243830Sdim 761276479Sdim return nullptr; 762193323Sed} 763193323Sed 764280031Sdim/// If array indices are not pointer-sized integers, explicitly cast them so 765280031Sdim/// that they aren't implicitly casted by the getelementptr. 766309124SdimConstant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops, 767314564Sdim Type *ResultTy, Optional<unsigned> InRangeIndex, 768314564Sdim const DataLayout &DL, const TargetLibraryInfo *TLI) { 769360784Sdim Type *IntIdxTy = DL.getIndexType(ResultTy); 770360784Sdim Type *IntIdxScalarTy = IntIdxTy->getScalarType(); 771203954Srdivacky 772203954Srdivacky bool Any = false; 773203954Srdivacky SmallVector<Constant*, 32> NewIdxs; 774226633Sdim for (unsigned i = 1, e = Ops.size(); i != e; ++i) { 775203954Srdivacky if ((i == 1 || 776314564Sdim !isa<StructType>(GetElementPtrInst::getIndexedType( 777314564Sdim SrcElemTy, Ops.slice(1, i - 1)))) && 778360784Sdim Ops[i]->getType()->getScalarType() != IntIdxScalarTy) { 779203954Srdivacky Any = true; 780314564Sdim Type *NewType = Ops[i]->getType()->isVectorTy() 781360784Sdim ? IntIdxTy 782360784Sdim : IntIdxScalarTy; 783203954Srdivacky NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i], 784203954Srdivacky true, 785314564Sdim NewType, 786203954Srdivacky true), 787314564Sdim Ops[i], NewType)); 788203954Srdivacky } else 789203954Srdivacky NewIdxs.push_back(Ops[i]); 790203954Srdivacky } 791203954Srdivacky 792261991Sdim if (!Any) 793276479Sdim return nullptr; 794261991Sdim 795314564Sdim Constant *C = ConstantExpr::getGetElementPtr( 796314564Sdim SrcElemTy, Ops[0], NewIdxs, /*InBounds=*/false, InRangeIndex); 797314564Sdim if (Constant *Folded = ConstantFoldConstant(C, DL, TLI)) 798314564Sdim C = Folded; 799261991Sdim 800203954Srdivacky return C; 801203954Srdivacky} 802203954Srdivacky 803239462Sdim/// Strip the pointer casts, but preserve the address space information. 804360784SdimConstant *StripPtrCastKeepAS(Constant *Ptr, Type *&ElemTy) { 805239462Sdim assert(Ptr->getType()->isPointerTy() && "Not a pointer type"); 806309124Sdim auto *OldPtrTy = cast<PointerType>(Ptr->getType()); 807360784Sdim Ptr = cast<Constant>(Ptr->stripPointerCasts()); 808309124Sdim auto *NewPtrTy = cast<PointerType>(Ptr->getType()); 809239462Sdim 810309124Sdim ElemTy = NewPtrTy->getPointerElementType(); 811309124Sdim 812239462Sdim // Preserve the address space number of the pointer. 813239462Sdim if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) { 814309124Sdim NewPtrTy = ElemTy->getPointerTo(OldPtrTy->getAddressSpace()); 815261991Sdim Ptr = ConstantExpr::getPointerCast(Ptr, NewPtrTy); 816239462Sdim } 817239462Sdim return Ptr; 818239462Sdim} 819239462Sdim 820280031Sdim/// If we can symbolically evaluate the GEP constant expression, do so. 821309124SdimConstant *SymbolicallyEvaluateGEP(const GEPOperator *GEP, 822309124Sdim ArrayRef<Constant *> Ops, 823309124Sdim const DataLayout &DL, 824309124Sdim const TargetLibraryInfo *TLI) { 825314564Sdim const GEPOperator *InnermostGEP = GEP; 826314564Sdim bool InBounds = GEP->isInBounds(); 827314564Sdim 828309124Sdim Type *SrcElemTy = GEP->getSourceElementType(); 829309124Sdim Type *ResElemTy = GEP->getResultElementType(); 830309124Sdim Type *ResTy = GEP->getType(); 831309124Sdim if (!SrcElemTy->isSized()) 832309124Sdim return nullptr; 833309124Sdim 834314564Sdim if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy, 835314564Sdim GEP->getInRangeIndex(), DL, TLI)) 836309124Sdim return C; 837309124Sdim 838193323Sed Constant *Ptr = Ops[0]; 839309124Sdim if (!Ptr->getType()->isPointerTy()) 840276479Sdim return nullptr; 841243830Sdim 842360784Sdim Type *IntIdxTy = DL.getIndexType(Ptr->getType()); 843198090Srdivacky 844193323Sed // If this is a constant expr gep that is effectively computing an 845193323Sed // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12' 846226633Sdim for (unsigned i = 1, e = Ops.size(); i != e; ++i) 847341825Sdim if (!isa<ConstantInt>(Ops[i])) { 848243830Sdim 849341825Sdim // If this is "gep i8* Ptr, (sub 0, V)", fold this as: 850341825Sdim // "inttoptr (sub (ptrtoint Ptr), V)" 851341825Sdim if (Ops.size() == 2 && ResElemTy->isIntegerTy(8)) { 852341825Sdim auto *CE = dyn_cast<ConstantExpr>(Ops[1]); 853360784Sdim assert((!CE || CE->getType() == IntIdxTy) && 854341825Sdim "CastGEPIndices didn't canonicalize index types!"); 855341825Sdim if (CE && CE->getOpcode() == Instruction::Sub && 856341825Sdim CE->getOperand(0)->isNullValue()) { 857341825Sdim Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType()); 858341825Sdim Res = ConstantExpr::getSub(Res, CE->getOperand(1)); 859341825Sdim Res = ConstantExpr::getIntToPtr(Res, ResTy); 860341825Sdim if (auto *FoldedRes = ConstantFoldConstant(Res, DL, TLI)) 861341825Sdim Res = FoldedRes; 862341825Sdim return Res; 863341825Sdim } 864218893Sdim } 865341825Sdim return nullptr; 866218893Sdim } 867239462Sdim 868360784Sdim unsigned BitWidth = DL.getTypeSizeInBits(IntIdxTy); 869226633Sdim APInt Offset = 870288943Sdim APInt(BitWidth, 871309124Sdim DL.getIndexedOffsetInType( 872309124Sdim SrcElemTy, 873288943Sdim makeArrayRef((Value * const *)Ops.data() + 1, Ops.size() - 1))); 874309124Sdim Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy); 875205218Srdivacky 876205218Srdivacky // If this is a GEP of a GEP, fold it all into a single GEP. 877309124Sdim while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) { 878314564Sdim InnermostGEP = GEP; 879314564Sdim InBounds &= GEP->isInBounds(); 880314564Sdim 881261991Sdim SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end()); 882205218Srdivacky 883205218Srdivacky // Do not try the incorporate the sub-GEP if some index is not a number. 884205218Srdivacky bool AllConstantInt = true; 885309124Sdim for (Value *NestedOp : NestedOps) 886309124Sdim if (!isa<ConstantInt>(NestedOp)) { 887205218Srdivacky AllConstantInt = false; 888205218Srdivacky break; 889205218Srdivacky } 890205218Srdivacky if (!AllConstantInt) 891205218Srdivacky break; 892205218Srdivacky 893205218Srdivacky Ptr = cast<Constant>(GEP->getOperand(0)); 894309124Sdim SrcElemTy = GEP->getSourceElementType(); 895309124Sdim Offset += APInt(BitWidth, DL.getIndexedOffsetInType(SrcElemTy, NestedOps)); 896309124Sdim Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy); 897205218Srdivacky } 898205218Srdivacky 899198090Srdivacky // If the base value for this address is a literal integer value, fold the 900198090Srdivacky // getelementptr to the resulting integer value casted to the pointer type. 901205407Srdivacky APInt BasePtr(BitWidth, 0); 902309124Sdim if (auto *CE = dyn_cast<ConstantExpr>(Ptr)) { 903261991Sdim if (CE->getOpcode() == Instruction::IntToPtr) { 904309124Sdim if (auto *Base = dyn_cast<ConstantInt>(CE->getOperand(0))) 905218893Sdim BasePtr = Base->getValue().zextOrTrunc(BitWidth); 906261991Sdim } 907261991Sdim } 908261991Sdim 909314564Sdim auto *PTy = cast<PointerType>(Ptr->getType()); 910314564Sdim if ((Ptr->isNullValue() || BasePtr != 0) && 911314564Sdim !DL.isNonIntegralPointerType(PTy)) { 912261991Sdim Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr); 913309124Sdim return ConstantExpr::getIntToPtr(C, ResTy); 914198090Srdivacky } 915198090Srdivacky 916198090Srdivacky // Otherwise form a regular getelementptr. Recompute the indices so that 917198090Srdivacky // we eliminate over-indexing of the notional static type array bounds. 918198090Srdivacky // This makes it easy to determine if the getelementptr is "inbounds". 919198090Srdivacky // Also, this helps GlobalOpt do SROA on GlobalVariables. 920314564Sdim Type *Ty = PTy; 921261991Sdim SmallVector<Constant *, 32> NewIdxs; 922261991Sdim 923198090Srdivacky do { 924309124Sdim if (!Ty->isStructTy()) { 925309124Sdim if (Ty->isPointerTy()) { 926200581Srdivacky // The only pointer indexing we'll do is on the first index of the GEP. 927200581Srdivacky if (!NewIdxs.empty()) 928200581Srdivacky break; 929243830Sdim 930309124Sdim Ty = SrcElemTy; 931309124Sdim 932200581Srdivacky // Only handle pointers to sized types, not pointers to functions. 933309124Sdim if (!Ty->isSized()) 934276479Sdim return nullptr; 935309124Sdim } else if (auto *ATy = dyn_cast<SequentialType>(Ty)) { 936309124Sdim Ty = ATy->getElementType(); 937309124Sdim } else { 938309124Sdim // We've reached some non-indexable type. 939309124Sdim break; 940200581Srdivacky } 941243830Sdim 942198090Srdivacky // Determine which element of the array the offset points into. 943309124Sdim APInt ElemSize(BitWidth, DL.getTypeAllocSize(Ty)); 944309124Sdim if (ElemSize == 0) { 945218893Sdim // The element size is 0. This may be [0 x Ty]*, so just use a zero 946218893Sdim // index for this level and proceed to the next level to see if it can 947218893Sdim // accommodate the offset. 948360784Sdim NewIdxs.push_back(ConstantInt::get(IntIdxTy, 0)); 949309124Sdim } else { 950218893Sdim // The element size is non-zero divide the offset by the element 951218893Sdim // size (rounding down), to compute the index at this level. 952309124Sdim bool Overflow; 953309124Sdim APInt NewIdx = Offset.sdiv_ov(ElemSize, Overflow); 954309124Sdim if (Overflow) 955309124Sdim break; 956218893Sdim Offset -= NewIdx * ElemSize; 957360784Sdim NewIdxs.push_back(ConstantInt::get(IntIdxTy, NewIdx)); 958218893Sdim } 959309124Sdim } else { 960309124Sdim auto *STy = cast<StructType>(Ty); 961234982Sdim // If we end up with an offset that isn't valid for this struct type, we 962234982Sdim // can't re-form this GEP in a regular form, so bail out. The pointer 963234982Sdim // operand likely went through casts that are necessary to make the GEP 964234982Sdim // sensible. 965288943Sdim const StructLayout &SL = *DL.getStructLayout(STy); 966309124Sdim if (Offset.isNegative() || Offset.uge(SL.getSizeInBytes())) 967234982Sdim break; 968234982Sdim 969198090Srdivacky // Determine which field of the struct the offset points into. The 970234982Sdim // getZExtValue is fine as we've already ensured that the offset is 971234982Sdim // within the range representable by the StructLayout API. 972198090Srdivacky unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue()); 973199481Srdivacky NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 974199481Srdivacky ElIdx)); 975198090Srdivacky Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx)); 976198090Srdivacky Ty = STy->getTypeAtIndex(ElIdx); 977198090Srdivacky } 978309124Sdim } while (Ty != ResElemTy); 979198090Srdivacky 980198090Srdivacky // If we haven't used up the entire offset by descending the static 981198090Srdivacky // type, then the offset is pointing into the middle of an indivisible 982198090Srdivacky // member, so we can't simplify it. 983198090Srdivacky if (Offset != 0) 984276479Sdim return nullptr; 985198090Srdivacky 986314564Sdim // Preserve the inrange index from the innermost GEP if possible. We must 987314564Sdim // have calculated the same indices up to and including the inrange index. 988314564Sdim Optional<unsigned> InRangeIndex; 989314564Sdim if (Optional<unsigned> LastIRIndex = InnermostGEP->getInRangeIndex()) 990314564Sdim if (SrcElemTy == InnermostGEP->getSourceElementType() && 991314564Sdim NewIdxs.size() > *LastIRIndex) { 992314564Sdim InRangeIndex = LastIRIndex; 993314564Sdim for (unsigned I = 0; I <= *LastIRIndex; ++I) 994344779Sdim if (NewIdxs[I] != InnermostGEP->getOperand(I + 1)) 995344779Sdim return nullptr; 996314564Sdim } 997314564Sdim 998198090Srdivacky // Create a GEP. 999314564Sdim Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ptr, NewIdxs, 1000314564Sdim InBounds, InRangeIndex); 1001261991Sdim assert(C->getType()->getPointerElementType() == Ty && 1002198090Srdivacky "Computed GetElementPtr has unexpected type!"); 1003198090Srdivacky 1004198090Srdivacky // If we ended up indexing a member with a type that doesn't match 1005198090Srdivacky // the type of what the original indices indexed, add a cast. 1006309124Sdim if (Ty != ResElemTy) 1007309124Sdim C = FoldBitCast(C, ResTy, DL); 1008198090Srdivacky 1009198090Srdivacky return C; 1010193323Sed} 1011193323Sed 1012309124Sdim/// Attempt to constant fold an instruction with the 1013309124Sdim/// specified opcode and operands. If successful, the constant result is 1014309124Sdim/// returned, if not, null is returned. Note that this function can fail when 1015309124Sdim/// attempting to fold instructions like loads and stores, which have no 1016309124Sdim/// constant expression form. 1017314564SdimConstant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode, 1018309124Sdim ArrayRef<Constant *> Ops, 1019309124Sdim const DataLayout &DL, 1020309124Sdim const TargetLibraryInfo *TLI) { 1021314564Sdim Type *DestTy = InstOrCE->getType(); 1022314564Sdim 1023353358Sdim if (Instruction::isUnaryOp(Opcode)) 1024353358Sdim return ConstantFoldUnaryOpOperand(Opcode, Ops[0], DL); 1025353358Sdim 1026309124Sdim if (Instruction::isBinaryOp(Opcode)) 1027309124Sdim return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL); 1028193323Sed 1029309124Sdim if (Instruction::isCast(Opcode)) 1030309124Sdim return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL); 1031193323Sed 1032309124Sdim if (auto *GEP = dyn_cast<GEPOperator>(InstOrCE)) { 1033309124Sdim if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI)) 1034309124Sdim return C; 1035309124Sdim 1036314564Sdim return ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), Ops[0], 1037314564Sdim Ops.slice(1), GEP->isInBounds(), 1038314564Sdim GEP->getInRangeIndex()); 1039309124Sdim } 1040309124Sdim 1041314564Sdim if (auto *CE = dyn_cast<ConstantExpr>(InstOrCE)) 1042314564Sdim return CE->getWithOperands(Ops); 1043314564Sdim 1044309124Sdim switch (Opcode) { 1045309124Sdim default: return nullptr; 1046309124Sdim case Instruction::ICmp: 1047309124Sdim case Instruction::FCmp: llvm_unreachable("Invalid for compares"); 1048309124Sdim case Instruction::Call: 1049321369Sdim if (auto *F = dyn_cast<Function>(Ops.back())) { 1050353358Sdim const auto *Call = cast<CallBase>(InstOrCE); 1051353358Sdim if (canConstantFoldCallTo(Call, F)) 1052353358Sdim return ConstantFoldCall(Call, F, Ops.slice(0, Ops.size() - 1), TLI); 1053321369Sdim } 1054309124Sdim return nullptr; 1055309124Sdim case Instruction::Select: 1056309124Sdim return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); 1057309124Sdim case Instruction::ExtractElement: 1058309124Sdim return ConstantExpr::getExtractElement(Ops[0], Ops[1]); 1059353358Sdim case Instruction::ExtractValue: 1060353358Sdim return ConstantExpr::getExtractValue( 1061360784Sdim Ops[0], cast<ExtractValueInst>(InstOrCE)->getIndices()); 1062309124Sdim case Instruction::InsertElement: 1063309124Sdim return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); 1064309124Sdim case Instruction::ShuffleVector: 1065309124Sdim return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); 1066309124Sdim } 1067309124Sdim} 1068309124Sdim 1069309124Sdim} // end anonymous namespace 1070309124Sdim 1071193323Sed//===----------------------------------------------------------------------===// 1072193323Sed// Constant Folding public APIs 1073193323Sed//===----------------------------------------------------------------------===// 1074193323Sed 1075314564Sdimnamespace { 1076314564Sdim 1077314564SdimConstant * 1078314564SdimConstantFoldConstantImpl(const Constant *C, const DataLayout &DL, 1079314564Sdim const TargetLibraryInfo *TLI, 1080314564Sdim SmallDenseMap<Constant *, Constant *> &FoldedOps) { 1081314564Sdim if (!isa<ConstantVector>(C) && !isa<ConstantExpr>(C)) 1082314564Sdim return nullptr; 1083314564Sdim 1084314564Sdim SmallVector<Constant *, 8> Ops; 1085314564Sdim for (const Use &NewU : C->operands()) { 1086314564Sdim auto *NewC = cast<Constant>(&NewU); 1087314564Sdim // Recursively fold the ConstantExpr's operands. If we have already folded 1088314564Sdim // a ConstantExpr, we don't have to process it again. 1089314564Sdim if (isa<ConstantVector>(NewC) || isa<ConstantExpr>(NewC)) { 1090314564Sdim auto It = FoldedOps.find(NewC); 1091314564Sdim if (It == FoldedOps.end()) { 1092314564Sdim if (auto *FoldedC = 1093314564Sdim ConstantFoldConstantImpl(NewC, DL, TLI, FoldedOps)) { 1094321369Sdim FoldedOps.insert({NewC, FoldedC}); 1095314564Sdim NewC = FoldedC; 1096314564Sdim } else { 1097314564Sdim FoldedOps.insert({NewC, NewC}); 1098314564Sdim } 1099314564Sdim } else { 1100314564Sdim NewC = It->second; 1101314564Sdim } 1102314564Sdim } 1103314564Sdim Ops.push_back(NewC); 1104314564Sdim } 1105314564Sdim 1106314564Sdim if (auto *CE = dyn_cast<ConstantExpr>(C)) { 1107314564Sdim if (CE->isCompare()) 1108314564Sdim return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1], 1109314564Sdim DL, TLI); 1110314564Sdim 1111314564Sdim return ConstantFoldInstOperandsImpl(CE, CE->getOpcode(), Ops, DL, TLI); 1112314564Sdim } 1113314564Sdim 1114314564Sdim assert(isa<ConstantVector>(C)); 1115314564Sdim return ConstantVector::get(Ops); 1116314564Sdim} 1117314564Sdim 1118314564Sdim} // end anonymous namespace 1119314564Sdim 1120288943SdimConstant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL, 1121234353Sdim const TargetLibraryInfo *TLI) { 1122218893Sdim // Handle PHI nodes quickly here... 1123309124Sdim if (auto *PN = dyn_cast<PHINode>(I)) { 1124276479Sdim Constant *CommonValue = nullptr; 1125193323Sed 1126314564Sdim SmallDenseMap<Constant *, Constant *> FoldedOps; 1127288943Sdim for (Value *Incoming : PN->incoming_values()) { 1128218893Sdim // If the incoming value is undef then skip it. Note that while we could 1129218893Sdim // skip the value if it is equal to the phi node itself we choose not to 1130218893Sdim // because that would break the rule that constant folding only applies if 1131218893Sdim // all operands are constants. 1132218893Sdim if (isa<UndefValue>(Incoming)) 1133218893Sdim continue; 1134239462Sdim // If the incoming value is not a constant, then give up. 1135309124Sdim auto *C = dyn_cast<Constant>(Incoming); 1136239462Sdim if (!C) 1137276479Sdim return nullptr; 1138239462Sdim // Fold the PHI's operands. 1139314564Sdim if (auto *FoldedC = ConstantFoldConstantImpl(C, DL, TLI, FoldedOps)) 1140314564Sdim C = FoldedC; 1141239462Sdim // If the incoming value is a different constant to 1142239462Sdim // the one we saw previously, then give up. 1143239462Sdim if (CommonValue && C != CommonValue) 1144276479Sdim return nullptr; 1145218893Sdim CommonValue = C; 1146218893Sdim } 1147193323Sed 1148218893Sdim // If we reach here, all incoming values are the same constant or undef. 1149218893Sdim return CommonValue ? CommonValue : UndefValue::get(PN->getType()); 1150193323Sed } 1151193323Sed 1152193323Sed // Scan the operand list, checking to see if they are all constants, if so, 1153309124Sdim // hand off to ConstantFoldInstOperandsImpl. 1154309124Sdim if (!all_of(I->operands(), [](Use &U) { return isa<Constant>(U); })) 1155309124Sdim return nullptr; 1156193323Sed 1157314564Sdim SmallDenseMap<Constant *, Constant *> FoldedOps; 1158309124Sdim SmallVector<Constant *, 8> Ops; 1159309124Sdim for (const Use &OpU : I->operands()) { 1160309124Sdim auto *Op = cast<Constant>(&OpU); 1161239462Sdim // Fold the Instruction's operands. 1162314564Sdim if (auto *FoldedOp = ConstantFoldConstantImpl(Op, DL, TLI, FoldedOps)) 1163314564Sdim Op = FoldedOp; 1164239462Sdim 1165239462Sdim Ops.push_back(Op); 1166239462Sdim } 1167239462Sdim 1168309124Sdim if (const auto *CI = dyn_cast<CmpInst>(I)) 1169199481Srdivacky return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1], 1170288943Sdim DL, TLI); 1171243830Sdim 1172309124Sdim if (const auto *LI = dyn_cast<LoadInst>(I)) 1173288943Sdim return ConstantFoldLoadInst(LI, DL); 1174218893Sdim 1175309124Sdim if (auto *IVI = dyn_cast<InsertValueInst>(I)) { 1176218893Sdim return ConstantExpr::getInsertValue( 1177218893Sdim cast<Constant>(IVI->getAggregateOperand()), 1178218893Sdim cast<Constant>(IVI->getInsertedValueOperand()), 1179224145Sdim IVI->getIndices()); 1180261991Sdim } 1181218893Sdim 1182309124Sdim if (auto *EVI = dyn_cast<ExtractValueInst>(I)) { 1183218893Sdim return ConstantExpr::getExtractValue( 1184218893Sdim cast<Constant>(EVI->getAggregateOperand()), 1185224145Sdim EVI->getIndices()); 1186261991Sdim } 1187218893Sdim 1188309124Sdim return ConstantFoldInstOperands(I, Ops, DL, TLI); 1189193323Sed} 1190193323Sed 1191314564SdimConstant *llvm::ConstantFoldConstant(const Constant *C, const DataLayout &DL, 1192314564Sdim const TargetLibraryInfo *TLI) { 1193314564Sdim SmallDenseMap<Constant *, Constant *> FoldedOps; 1194314564Sdim return ConstantFoldConstantImpl(C, DL, TLI, FoldedOps); 1195193323Sed} 1196193323Sed 1197309124SdimConstant *llvm::ConstantFoldInstOperands(Instruction *I, 1198309124Sdim ArrayRef<Constant *> Ops, 1199309124Sdim const DataLayout &DL, 1200309124Sdim const TargetLibraryInfo *TLI) { 1201314564Sdim return ConstantFoldInstOperandsImpl(I, I->getOpcode(), Ops, DL, TLI); 1202309124Sdim} 1203309124Sdim 1204193323SedConstant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate, 1205243830Sdim Constant *Ops0, Constant *Ops1, 1206288943Sdim const DataLayout &DL, 1207234353Sdim const TargetLibraryInfo *TLI) { 1208193323Sed // fold: icmp (inttoptr x), null -> icmp x, 0 1209321369Sdim // fold: icmp null, (inttoptr x) -> icmp 0, x 1210193323Sed // fold: icmp (ptrtoint x), 0 -> icmp x, null 1211321369Sdim // fold: icmp 0, (ptrtoint x) -> icmp null, x 1212193323Sed // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y 1213193323Sed // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y 1214193323Sed // 1215288943Sdim // FIXME: The following comment is out of data and the DataLayout is here now. 1216288943Sdim // ConstantExpr::getCompare cannot do this, because it doesn't have DL 1217193323Sed // around to know if bit truncation is happening. 1218309124Sdim if (auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) { 1219288943Sdim if (Ops1->isNullValue()) { 1220193323Sed if (CE0->getOpcode() == Instruction::IntToPtr) { 1221288943Sdim Type *IntPtrTy = DL.getIntPtrType(CE0->getType()); 1222193323Sed // Convert the integer value to the right size to ensure we get the 1223193323Sed // proper extension or truncation. 1224193323Sed Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0), 1225193323Sed IntPtrTy, false); 1226199481Srdivacky Constant *Null = Constant::getNullValue(C->getType()); 1227288943Sdim return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI); 1228193323Sed } 1229243830Sdim 1230193323Sed // Only do this transformation if the int is intptrty in size, otherwise 1231193323Sed // there is a truncation or extension that we aren't modeling. 1232261991Sdim if (CE0->getOpcode() == Instruction::PtrToInt) { 1233288943Sdim Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType()); 1234261991Sdim if (CE0->getType() == IntPtrTy) { 1235261991Sdim Constant *C = CE0->getOperand(0); 1236261991Sdim Constant *Null = Constant::getNullValue(C->getType()); 1237288943Sdim return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI); 1238261991Sdim } 1239193323Sed } 1240193323Sed } 1241243830Sdim 1242309124Sdim if (auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) { 1243288943Sdim if (CE0->getOpcode() == CE1->getOpcode()) { 1244261991Sdim if (CE0->getOpcode() == Instruction::IntToPtr) { 1245288943Sdim Type *IntPtrTy = DL.getIntPtrType(CE0->getType()); 1246193323Sed 1247193323Sed // Convert the integer value to the right size to ensure we get the 1248193323Sed // proper extension or truncation. 1249193323Sed Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0), 1250193323Sed IntPtrTy, false); 1251193323Sed Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0), 1252193323Sed IntPtrTy, false); 1253288943Sdim return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI); 1254193323Sed } 1255193323Sed 1256193323Sed // Only do this transformation if the int is intptrty in size, otherwise 1257193323Sed // there is a truncation or extension that we aren't modeling. 1258261991Sdim if (CE0->getOpcode() == Instruction::PtrToInt) { 1259288943Sdim Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType()); 1260261991Sdim if (CE0->getType() == IntPtrTy && 1261261991Sdim CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) { 1262288943Sdim return ConstantFoldCompareInstOperands( 1263288943Sdim Predicate, CE0->getOperand(0), CE1->getOperand(0), DL, TLI); 1264261991Sdim } 1265261991Sdim } 1266193323Sed } 1267193323Sed } 1268243830Sdim 1269202375Srdivacky // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0) 1270202375Srdivacky // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0) 1271202375Srdivacky if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) && 1272202375Srdivacky CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) { 1273288943Sdim Constant *LHS = ConstantFoldCompareInstOperands( 1274288943Sdim Predicate, CE0->getOperand(0), Ops1, DL, TLI); 1275288943Sdim Constant *RHS = ConstantFoldCompareInstOperands( 1276288943Sdim Predicate, CE0->getOperand(1), Ops1, DL, TLI); 1277243830Sdim unsigned OpC = 1278202375Srdivacky Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or; 1279309124Sdim return ConstantFoldBinaryOpOperands(OpC, LHS, RHS, DL); 1280202375Srdivacky } 1281321369Sdim } else if (isa<ConstantExpr>(Ops1)) { 1282321369Sdim // If RHS is a constant expression, but the left side isn't, swap the 1283321369Sdim // operands and try again. 1284321369Sdim Predicate = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)Predicate); 1285321369Sdim return ConstantFoldCompareInstOperands(Predicate, Ops1, Ops0, DL, TLI); 1286193323Sed } 1287243830Sdim 1288199481Srdivacky return ConstantExpr::getCompare(Predicate, Ops0, Ops1); 1289193323Sed} 1290193323Sed 1291353358SdimConstant *llvm::ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, 1292353358Sdim const DataLayout &DL) { 1293353358Sdim assert(Instruction::isUnaryOp(Opcode)); 1294353358Sdim 1295353358Sdim return ConstantExpr::get(Opcode, Op); 1296353358Sdim} 1297353358Sdim 1298309124SdimConstant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, 1299309124Sdim Constant *RHS, 1300309124Sdim const DataLayout &DL) { 1301309124Sdim assert(Instruction::isBinaryOp(Opcode)); 1302309124Sdim if (isa<ConstantExpr>(LHS) || isa<ConstantExpr>(RHS)) 1303309124Sdim if (Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL)) 1304309124Sdim return C; 1305193323Sed 1306309124Sdim return ConstantExpr::get(Opcode, LHS, RHS); 1307309124Sdim} 1308309124Sdim 1309309124SdimConstant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C, 1310309124Sdim Type *DestTy, const DataLayout &DL) { 1311309124Sdim assert(Instruction::isCast(Opcode)); 1312309124Sdim switch (Opcode) { 1313309124Sdim default: 1314309124Sdim llvm_unreachable("Missing case"); 1315309124Sdim case Instruction::PtrToInt: 1316309124Sdim // If the input is a inttoptr, eliminate the pair. This requires knowing 1317309124Sdim // the width of a pointer, so it can't be done in ConstantExpr::getCast. 1318309124Sdim if (auto *CE = dyn_cast<ConstantExpr>(C)) { 1319309124Sdim if (CE->getOpcode() == Instruction::IntToPtr) { 1320309124Sdim Constant *Input = CE->getOperand(0); 1321309124Sdim unsigned InWidth = Input->getType()->getScalarSizeInBits(); 1322309124Sdim unsigned PtrWidth = DL.getPointerTypeSizeInBits(CE->getType()); 1323309124Sdim if (PtrWidth < InWidth) { 1324309124Sdim Constant *Mask = 1325309124Sdim ConstantInt::get(CE->getContext(), 1326309124Sdim APInt::getLowBitsSet(InWidth, PtrWidth)); 1327309124Sdim Input = ConstantExpr::getAnd(Input, Mask); 1328309124Sdim } 1329309124Sdim // Do a zext or trunc to get to the dest size. 1330309124Sdim return ConstantExpr::getIntegerCast(Input, DestTy, false); 1331309124Sdim } 1332309124Sdim } 1333309124Sdim return ConstantExpr::getCast(Opcode, C, DestTy); 1334309124Sdim case Instruction::IntToPtr: 1335309124Sdim // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if 1336309124Sdim // the int size is >= the ptr size and the address spaces are the same. 1337309124Sdim // This requires knowing the width of a pointer, so it can't be done in 1338309124Sdim // ConstantExpr::getCast. 1339309124Sdim if (auto *CE = dyn_cast<ConstantExpr>(C)) { 1340309124Sdim if (CE->getOpcode() == Instruction::PtrToInt) { 1341309124Sdim Constant *SrcPtr = CE->getOperand(0); 1342309124Sdim unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType()); 1343309124Sdim unsigned MidIntSize = CE->getType()->getScalarSizeInBits(); 1344309124Sdim 1345309124Sdim if (MidIntSize >= SrcPtrSize) { 1346309124Sdim unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace(); 1347309124Sdim if (SrcAS == DestTy->getPointerAddressSpace()) 1348309124Sdim return FoldBitCast(CE->getOperand(0), DestTy, DL); 1349309124Sdim } 1350309124Sdim } 1351309124Sdim } 1352309124Sdim 1353309124Sdim return ConstantExpr::getCast(Opcode, C, DestTy); 1354309124Sdim case Instruction::Trunc: 1355309124Sdim case Instruction::ZExt: 1356309124Sdim case Instruction::SExt: 1357309124Sdim case Instruction::FPTrunc: 1358309124Sdim case Instruction::FPExt: 1359309124Sdim case Instruction::UIToFP: 1360309124Sdim case Instruction::SIToFP: 1361309124Sdim case Instruction::FPToUI: 1362309124Sdim case Instruction::FPToSI: 1363309124Sdim case Instruction::AddrSpaceCast: 1364309124Sdim return ConstantExpr::getCast(Opcode, C, DestTy); 1365309124Sdim case Instruction::BitCast: 1366309124Sdim return FoldBitCast(C, DestTy, DL); 1367309124Sdim } 1368309124Sdim} 1369309124Sdim 1370243830SdimConstant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C, 1371193323Sed ConstantExpr *CE) { 1372234353Sdim if (!CE->getOperand(1)->isNullValue()) 1373276479Sdim return nullptr; // Do not allow stepping over the value! 1374234353Sdim 1375193323Sed // Loop over all of the operands, tracking down which value we are 1376234353Sdim // addressing. 1377234353Sdim for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) { 1378234353Sdim C = C->getAggregateElement(CE->getOperand(i)); 1379276479Sdim if (!C) 1380276479Sdim return nullptr; 1381234353Sdim } 1382193323Sed return C; 1383193323Sed} 1384193323Sed 1385309124SdimConstant * 1386309124Sdimllvm::ConstantFoldLoadThroughGEPIndices(Constant *C, 1387309124Sdim ArrayRef<Constant *> Indices) { 1388234353Sdim // Loop over all of the operands, tracking down which value we are 1389234353Sdim // addressing. 1390309124Sdim for (Constant *Index : Indices) { 1391309124Sdim C = C->getAggregateElement(Index); 1392276479Sdim if (!C) 1393276479Sdim return nullptr; 1394234353Sdim } 1395234353Sdim return C; 1396234353Sdim} 1397193323Sed 1398193323Sed//===----------------------------------------------------------------------===// 1399193323Sed// Constant Folding for Calls 1400193323Sed// 1401193323Sed 1402353358Sdimbool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) { 1403353358Sdim if (Call->isNoBuiltin() || Call->isStrictFP()) 1404321369Sdim return false; 1405193323Sed switch (F->getIntrinsicID()) { 1406249423Sdim case Intrinsic::fabs: 1407280031Sdim case Intrinsic::minnum: 1408280031Sdim case Intrinsic::maxnum: 1409344779Sdim case Intrinsic::minimum: 1410344779Sdim case Intrinsic::maximum: 1411249423Sdim case Intrinsic::log: 1412249423Sdim case Intrinsic::log2: 1413249423Sdim case Intrinsic::log10: 1414249423Sdim case Intrinsic::exp: 1415249423Sdim case Intrinsic::exp2: 1416249423Sdim case Intrinsic::floor: 1417276479Sdim case Intrinsic::ceil: 1418193323Sed case Intrinsic::sqrt: 1419288943Sdim case Intrinsic::sin: 1420288943Sdim case Intrinsic::cos: 1421296417Sdim case Intrinsic::trunc: 1422296417Sdim case Intrinsic::rint: 1423296417Sdim case Intrinsic::nearbyint: 1424234353Sdim case Intrinsic::pow: 1425193323Sed case Intrinsic::powi: 1426193323Sed case Intrinsic::bswap: 1427193323Sed case Intrinsic::ctpop: 1428193323Sed case Intrinsic::ctlz: 1429193323Sed case Intrinsic::cttz: 1430344779Sdim case Intrinsic::fshl: 1431344779Sdim case Intrinsic::fshr: 1432276479Sdim case Intrinsic::fma: 1433276479Sdim case Intrinsic::fmuladd: 1434276479Sdim case Intrinsic::copysign: 1435341825Sdim case Intrinsic::launder_invariant_group: 1436341825Sdim case Intrinsic::strip_invariant_group: 1437276479Sdim case Intrinsic::round: 1438309124Sdim case Intrinsic::masked_load: 1439221345Sdim case Intrinsic::sadd_with_overflow: 1440198090Srdivacky case Intrinsic::uadd_with_overflow: 1441221345Sdim case Intrinsic::ssub_with_overflow: 1442198090Srdivacky case Intrinsic::usub_with_overflow: 1443218893Sdim case Intrinsic::smul_with_overflow: 1444221345Sdim case Intrinsic::umul_with_overflow: 1445344779Sdim case Intrinsic::sadd_sat: 1446344779Sdim case Intrinsic::uadd_sat: 1447344779Sdim case Intrinsic::ssub_sat: 1448344779Sdim case Intrinsic::usub_sat: 1449353358Sdim case Intrinsic::smul_fix: 1450353358Sdim case Intrinsic::smul_fix_sat: 1451205407Srdivacky case Intrinsic::convert_from_fp16: 1452205407Srdivacky case Intrinsic::convert_to_fp16: 1453309124Sdim case Intrinsic::bitreverse: 1454218893Sdim case Intrinsic::x86_sse_cvtss2si: 1455218893Sdim case Intrinsic::x86_sse_cvtss2si64: 1456218893Sdim case Intrinsic::x86_sse_cvttss2si: 1457218893Sdim case Intrinsic::x86_sse_cvttss2si64: 1458218893Sdim case Intrinsic::x86_sse2_cvtsd2si: 1459218893Sdim case Intrinsic::x86_sse2_cvtsd2si64: 1460218893Sdim case Intrinsic::x86_sse2_cvttsd2si: 1461218893Sdim case Intrinsic::x86_sse2_cvttsd2si64: 1462344779Sdim case Intrinsic::x86_avx512_vcvtss2si32: 1463344779Sdim case Intrinsic::x86_avx512_vcvtss2si64: 1464344779Sdim case Intrinsic::x86_avx512_cvttss2si: 1465344779Sdim case Intrinsic::x86_avx512_cvttss2si64: 1466344779Sdim case Intrinsic::x86_avx512_vcvtsd2si32: 1467344779Sdim case Intrinsic::x86_avx512_vcvtsd2si64: 1468344779Sdim case Intrinsic::x86_avx512_cvttsd2si: 1469344779Sdim case Intrinsic::x86_avx512_cvttsd2si64: 1470344779Sdim case Intrinsic::x86_avx512_vcvtss2usi32: 1471344779Sdim case Intrinsic::x86_avx512_vcvtss2usi64: 1472344779Sdim case Intrinsic::x86_avx512_cvttss2usi: 1473344779Sdim case Intrinsic::x86_avx512_cvttss2usi64: 1474344779Sdim case Intrinsic::x86_avx512_vcvtsd2usi32: 1475344779Sdim case Intrinsic::x86_avx512_vcvtsd2usi64: 1476344779Sdim case Intrinsic::x86_avx512_cvttsd2usi: 1477344779Sdim case Intrinsic::x86_avx512_cvttsd2usi64: 1478344779Sdim case Intrinsic::is_constant: 1479193323Sed return true; 1480198090Srdivacky default: 1481198090Srdivacky return false; 1482321369Sdim case Intrinsic::not_intrinsic: break; 1483193323Sed } 1484193323Sed 1485261991Sdim if (!F->hasName()) 1486261991Sdim return false; 1487243830Sdim 1488193323Sed // In these cases, the check of the length is required. We don't want to 1489193323Sed // return true for a name like "cos\0blah" which strcmp would return equal to 1490193323Sed // "cos", but has length 8. 1491360784Sdim StringRef Name = F->getName(); 1492198090Srdivacky switch (Name[0]) { 1493296417Sdim default: 1494296417Sdim return false; 1495193323Sed case 'a': 1496360784Sdim return Name == "acos" || Name == "acosf" || 1497360784Sdim Name == "asin" || Name == "asinf" || 1498360784Sdim Name == "atan" || Name == "atanf" || 1499360784Sdim Name == "atan2" || Name == "atan2f"; 1500193323Sed case 'c': 1501360784Sdim return Name == "ceil" || Name == "ceilf" || 1502360784Sdim Name == "cos" || Name == "cosf" || 1503360784Sdim Name == "cosh" || Name == "coshf"; 1504193323Sed case 'e': 1505360784Sdim return Name == "exp" || Name == "expf" || 1506360784Sdim Name == "exp2" || Name == "exp2f"; 1507193323Sed case 'f': 1508360784Sdim return Name == "fabs" || Name == "fabsf" || 1509360784Sdim Name == "floor" || Name == "floorf" || 1510360784Sdim Name == "fmod" || Name == "fmodf"; 1511193323Sed case 'l': 1512360784Sdim return Name == "log" || Name == "logf" || 1513360784Sdim Name == "log2" || Name == "log2f" || 1514360784Sdim Name == "log10" || Name == "log10f"; 1515360784Sdim case 'n': 1516360784Sdim return Name == "nearbyint" || Name == "nearbyintf"; 1517193323Sed case 'p': 1518296417Sdim return Name == "pow" || Name == "powf"; 1519314564Sdim case 'r': 1520360784Sdim return Name == "rint" || Name == "rintf" || 1521360784Sdim Name == "round" || Name == "roundf"; 1522193323Sed case 's': 1523360784Sdim return Name == "sin" || Name == "sinf" || 1524360784Sdim Name == "sinh" || Name == "sinhf" || 1525360784Sdim Name == "sqrt" || Name == "sqrtf"; 1526193323Sed case 't': 1527360784Sdim return Name == "tan" || Name == "tanf" || 1528360784Sdim Name == "tanh" || Name == "tanhf" || 1529360784Sdim Name == "trunc" || Name == "truncf"; 1530321369Sdim case '_': 1531321369Sdim // Check for various function names that get used for the math functions 1532321369Sdim // when the header files are preprocessed with the macro 1533321369Sdim // __FINITE_MATH_ONLY__ enabled. 1534321369Sdim // The '12' here is the length of the shortest name that can match. 1535321369Sdim // We need to check the size before looking at Name[1] and Name[2] 1536321369Sdim // so we may as well check a limit that will eliminate mismatches. 1537321369Sdim if (Name.size() < 12 || Name[1] != '_') 1538321369Sdim return false; 1539321369Sdim switch (Name[2]) { 1540321369Sdim default: 1541321369Sdim return false; 1542321369Sdim case 'a': 1543321369Sdim return Name == "__acos_finite" || Name == "__acosf_finite" || 1544321369Sdim Name == "__asin_finite" || Name == "__asinf_finite" || 1545321369Sdim Name == "__atan2_finite" || Name == "__atan2f_finite"; 1546321369Sdim case 'c': 1547321369Sdim return Name == "__cosh_finite" || Name == "__coshf_finite"; 1548321369Sdim case 'e': 1549321369Sdim return Name == "__exp_finite" || Name == "__expf_finite" || 1550321369Sdim Name == "__exp2_finite" || Name == "__exp2f_finite"; 1551321369Sdim case 'l': 1552321369Sdim return Name == "__log_finite" || Name == "__logf_finite" || 1553321369Sdim Name == "__log10_finite" || Name == "__log10f_finite"; 1554321369Sdim case 'p': 1555321369Sdim return Name == "__pow_finite" || Name == "__powf_finite"; 1556321369Sdim case 's': 1557321369Sdim return Name == "__sinh_finite" || Name == "__sinhf_finite"; 1558321369Sdim } 1559193323Sed } 1560193323Sed} 1561193323Sed 1562309124Sdimnamespace { 1563309124Sdim 1564309124SdimConstant *GetConstantFoldFPValue(double V, Type *Ty) { 1565353358Sdim if (Ty->isHalfTy() || Ty->isFloatTy()) { 1566249423Sdim APFloat APF(V); 1567249423Sdim bool unused; 1568353358Sdim APF.convert(Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &unused); 1569249423Sdim return ConstantFP::get(Ty->getContext(), APF); 1570249423Sdim } 1571198090Srdivacky if (Ty->isDoubleTy()) 1572199481Srdivacky return ConstantFP::get(Ty->getContext(), APFloat(V)); 1573249423Sdim llvm_unreachable("Can only constant fold half/float/double"); 1574193323Sed} 1575193323Sed 1576280031Sdim/// Clear the floating-point exception state. 1577309124Sdiminline void llvm_fenv_clearexcept() { 1578276479Sdim#if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT 1579276479Sdim feclearexcept(FE_ALL_EXCEPT); 1580276479Sdim#endif 1581276479Sdim errno = 0; 1582276479Sdim} 1583276479Sdim 1584280031Sdim/// Test if a floating-point exception was raised. 1585309124Sdiminline bool llvm_fenv_testexcept() { 1586276479Sdim int errno_val = errno; 1587276479Sdim if (errno_val == ERANGE || errno_val == EDOM) 1588276479Sdim return true; 1589276479Sdim#if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT && HAVE_DECL_FE_INEXACT 1590276479Sdim if (fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT)) 1591276479Sdim return true; 1592276479Sdim#endif 1593276479Sdim return false; 1594276479Sdim} 1595276479Sdim 1596309124SdimConstant *ConstantFoldFP(double (*NativeFP)(double), double V, Type *Ty) { 1597276479Sdim llvm_fenv_clearexcept(); 1598276479Sdim V = NativeFP(V); 1599276479Sdim if (llvm_fenv_testexcept()) { 1600276479Sdim llvm_fenv_clearexcept(); 1601276479Sdim return nullptr; 1602276479Sdim } 1603276479Sdim 1604276479Sdim return GetConstantFoldFPValue(V, Ty); 1605276479Sdim} 1606276479Sdim 1607309124SdimConstant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), double V, 1608309124Sdim double W, Type *Ty) { 1609276479Sdim llvm_fenv_clearexcept(); 1610193323Sed V = NativeFP(V, W); 1611276479Sdim if (llvm_fenv_testexcept()) { 1612276479Sdim llvm_fenv_clearexcept(); 1613276479Sdim return nullptr; 1614193323Sed } 1615243830Sdim 1616276479Sdim return GetConstantFoldFPValue(V, Ty); 1617193323Sed} 1618193323Sed 1619280031Sdim/// Attempt to fold an SSE floating point to integer conversion of a constant 1620280031Sdim/// floating point. If roundTowardZero is false, the default IEEE rounding is 1621280031Sdim/// used (toward nearest, ties to even). This matches the behavior of the 1622280031Sdim/// non-truncating SSE instructions in the default rounding mode. The desired 1623280031Sdim/// integer type Ty is used to select how many bits are available for the 1624280031Sdim/// result. Returns null if the conversion cannot be performed, otherwise 1625280031Sdim/// returns the Constant value resulting from the conversion. 1626309124SdimConstant *ConstantFoldSSEConvertToInt(const APFloat &Val, bool roundTowardZero, 1627344779Sdim Type *Ty, bool IsSigned) { 1628218893Sdim // All of these conversion intrinsics form an integer of at most 64bits. 1629261991Sdim unsigned ResultWidth = Ty->getIntegerBitWidth(); 1630218893Sdim assert(ResultWidth <= 64 && 1631218893Sdim "Can only constant fold conversions to 64 and 32 bit ints"); 1632218893Sdim 1633218893Sdim uint64_t UIntVal; 1634218893Sdim bool isExact = false; 1635218893Sdim APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero 1636218893Sdim : APFloat::rmNearestTiesToEven; 1637321369Sdim APFloat::opStatus status = 1638321369Sdim Val.convertToInteger(makeMutableArrayRef(UIntVal), ResultWidth, 1639344779Sdim IsSigned, mode, &isExact); 1640309124Sdim if (status != APFloat::opOK && 1641309124Sdim (!roundTowardZero || status != APFloat::opInexact)) 1642276479Sdim return nullptr; 1643344779Sdim return ConstantInt::get(Ty, UIntVal, IsSigned); 1644218893Sdim} 1645218893Sdim 1646309124Sdimdouble getValueAsDouble(ConstantFP *Op) { 1647276479Sdim Type *Ty = Op->getType(); 1648198090Srdivacky 1649276479Sdim if (Ty->isFloatTy()) 1650276479Sdim return Op->getValueAPF().convertToFloat(); 1651276479Sdim 1652276479Sdim if (Ty->isDoubleTy()) 1653276479Sdim return Op->getValueAPF().convertToDouble(); 1654276479Sdim 1655276479Sdim bool unused; 1656276479Sdim APFloat APF = Op->getValueAPF(); 1657314564Sdim APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &unused); 1658276479Sdim return APF.convertToDouble(); 1659276479Sdim} 1660276479Sdim 1661344779Sdimstatic bool isManifestConstant(const Constant *c) { 1662344779Sdim if (isa<ConstantData>(c)) { 1663344779Sdim return true; 1664344779Sdim } else if (isa<ConstantAggregate>(c) || isa<ConstantExpr>(c)) { 1665344779Sdim for (const Value *subc : c->operand_values()) { 1666344779Sdim if (!isManifestConstant(cast<Constant>(subc))) 1667344779Sdim return false; 1668344779Sdim } 1669344779Sdim return true; 1670344779Sdim } 1671344779Sdim return false; 1672344779Sdim} 1673344779Sdim 1674344779Sdimstatic bool getConstIntOrUndef(Value *Op, const APInt *&C) { 1675344779Sdim if (auto *CI = dyn_cast<ConstantInt>(Op)) { 1676344779Sdim C = &CI->getValue(); 1677344779Sdim return true; 1678344779Sdim } 1679344779Sdim if (isa<UndefValue>(Op)) { 1680344779Sdim C = nullptr; 1681344779Sdim return true; 1682344779Sdim } 1683344779Sdim return false; 1684344779Sdim} 1685344779Sdim 1686353358Sdimstatic Constant *ConstantFoldScalarCall1(StringRef Name, 1687353358Sdim Intrinsic::ID IntrinsicID, 1688353358Sdim Type *Ty, 1689353358Sdim ArrayRef<Constant *> Operands, 1690353358Sdim const TargetLibraryInfo *TLI, 1691353358Sdim const CallBase *Call) { 1692353358Sdim assert(Operands.size() == 1 && "Wrong number of operands."); 1693353358Sdim 1694353358Sdim if (IntrinsicID == Intrinsic::is_constant) { 1695353358Sdim // We know we have a "Constant" argument. But we want to only 1696353358Sdim // return true for manifest constants, not those that depend on 1697353358Sdim // constants with unknowable values, e.g. GlobalValue or BlockAddress. 1698353358Sdim if (isManifestConstant(Operands[0])) 1699353358Sdim return ConstantInt::getTrue(Ty->getContext()); 1700353358Sdim return nullptr; 1701353358Sdim } 1702353358Sdim if (isa<UndefValue>(Operands[0])) { 1703353358Sdim // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN. 1704353358Sdim // ctpop() is between 0 and bitwidth, pick 0 for undef. 1705353358Sdim if (IntrinsicID == Intrinsic::cos || 1706353358Sdim IntrinsicID == Intrinsic::ctpop) 1707353358Sdim return Constant::getNullValue(Ty); 1708353358Sdim if (IntrinsicID == Intrinsic::bswap || 1709353358Sdim IntrinsicID == Intrinsic::bitreverse || 1710353358Sdim IntrinsicID == Intrinsic::launder_invariant_group || 1711353358Sdim IntrinsicID == Intrinsic::strip_invariant_group) 1712353358Sdim return Operands[0]; 1713353358Sdim } 1714353358Sdim 1715353358Sdim if (isa<ConstantPointerNull>(Operands[0])) { 1716353358Sdim // launder(null) == null == strip(null) iff in addrspace 0 1717353358Sdim if (IntrinsicID == Intrinsic::launder_invariant_group || 1718353358Sdim IntrinsicID == Intrinsic::strip_invariant_group) { 1719353358Sdim // If instruction is not yet put in a basic block (e.g. when cloning 1720353358Sdim // a function during inlining), Call's caller may not be available. 1721353358Sdim // So check Call's BB first before querying Call->getCaller. 1722353358Sdim const Function *Caller = 1723353358Sdim Call->getParent() ? Call->getCaller() : nullptr; 1724353358Sdim if (Caller && 1725353358Sdim !NullPointerIsDefined( 1726353358Sdim Caller, Operands[0]->getType()->getPointerAddressSpace())) { 1727321369Sdim return Operands[0]; 1728341825Sdim } 1729353358Sdim return nullptr; 1730341825Sdim } 1731353358Sdim } 1732341825Sdim 1733353358Sdim if (auto *Op = dyn_cast<ConstantFP>(Operands[0])) { 1734353358Sdim if (IntrinsicID == Intrinsic::convert_to_fp16) { 1735353358Sdim APFloat Val(Op->getValueAPF()); 1736205407Srdivacky 1737353358Sdim bool lost = false; 1738353358Sdim Val.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &lost); 1739205407Srdivacky 1740353358Sdim return ConstantInt::get(Ty->getContext(), Val.bitcastToAPInt()); 1741353358Sdim } 1742205407Srdivacky 1743353358Sdim if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy()) 1744353358Sdim return nullptr; 1745218893Sdim 1746360784Sdim // Use internal versions of these intrinsics. 1747360784Sdim APFloat U = Op->getValueAPF(); 1748360784Sdim 1749360784Sdim if (IntrinsicID == Intrinsic::nearbyint || IntrinsicID == Intrinsic::rint) { 1750360784Sdim U.roundToIntegral(APFloat::rmNearestTiesToEven); 1751360784Sdim return ConstantFP::get(Ty->getContext(), U); 1752353358Sdim } 1753276479Sdim 1754360784Sdim if (IntrinsicID == Intrinsic::round) { 1755360784Sdim U.roundToIntegral(APFloat::rmNearestTiesToAway); 1756360784Sdim return ConstantFP::get(Ty->getContext(), U); 1757353358Sdim } 1758296417Sdim 1759353358Sdim if (IntrinsicID == Intrinsic::ceil) { 1760360784Sdim U.roundToIntegral(APFloat::rmTowardPositive); 1761360784Sdim return ConstantFP::get(Ty->getContext(), U); 1762353358Sdim } 1763296417Sdim 1764360784Sdim if (IntrinsicID == Intrinsic::floor) { 1765360784Sdim U.roundToIntegral(APFloat::rmTowardNegative); 1766360784Sdim return ConstantFP::get(Ty->getContext(), U); 1767353358Sdim } 1768296417Sdim 1769360784Sdim if (IntrinsicID == Intrinsic::trunc) { 1770360784Sdim U.roundToIntegral(APFloat::rmTowardZero); 1771360784Sdim return ConstantFP::get(Ty->getContext(), U); 1772353358Sdim } 1773296417Sdim 1774360784Sdim if (IntrinsicID == Intrinsic::fabs) { 1775360784Sdim U.clearSign(); 1776360784Sdim return ConstantFP::get(Ty->getContext(), U); 1777353358Sdim } 1778296417Sdim 1779353358Sdim /// We only fold functions with finite arguments. Folding NaN and inf is 1780353358Sdim /// likely to be aborted with an exception anyway, and some host libms 1781353358Sdim /// have known errors raising exceptions. 1782353358Sdim if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity()) 1783353358Sdim return nullptr; 1784218893Sdim 1785353358Sdim /// Currently APFloat versions of these functions do not exist, so we use 1786353358Sdim /// the host native double versions. Float versions are not called 1787353358Sdim /// directly but for all these it is true (float)(f((double)arg)) == 1788353358Sdim /// f(arg). Long double not supported yet. 1789353358Sdim double V = getValueAsDouble(Op); 1790249423Sdim 1791353358Sdim switch (IntrinsicID) { 1792353358Sdim default: break; 1793360784Sdim case Intrinsic::log: 1794360784Sdim return ConstantFoldFP(log, V, Ty); 1795353358Sdim case Intrinsic::log2: 1796360784Sdim // TODO: What about hosts that lack a C99 library? 1797353358Sdim return ConstantFoldFP(Log2, V, Ty); 1798353358Sdim case Intrinsic::log10: 1799360784Sdim // TODO: What about hosts that lack a C99 library? 1800353358Sdim return ConstantFoldFP(log10, V, Ty); 1801353358Sdim case Intrinsic::exp: 1802353358Sdim return ConstantFoldFP(exp, V, Ty); 1803353358Sdim case Intrinsic::exp2: 1804360784Sdim // Fold exp2(x) as pow(2, x), in case the host lacks a C99 library. 1805360784Sdim return ConstantFoldBinaryFP(pow, 2.0, V, Ty); 1806353358Sdim case Intrinsic::sin: 1807353358Sdim return ConstantFoldFP(sin, V, Ty); 1808353358Sdim case Intrinsic::cos: 1809353358Sdim return ConstantFoldFP(cos, V, Ty); 1810353358Sdim case Intrinsic::sqrt: 1811353358Sdim return ConstantFoldFP(sqrt, V, Ty); 1812353358Sdim } 1813249423Sdim 1814353358Sdim if (!TLI) 1815353358Sdim return nullptr; 1816276479Sdim 1817360784Sdim LibFunc Func = NotLibFunc; 1818360784Sdim TLI->getLibFunc(Name, Func); 1819360784Sdim switch (Func) { 1820360784Sdim default: 1821360784Sdim break; 1822360784Sdim case LibFunc_acos: 1823360784Sdim case LibFunc_acosf: 1824360784Sdim case LibFunc_acos_finite: 1825360784Sdim case LibFunc_acosf_finite: 1826360784Sdim if (TLI->has(Func)) 1827353358Sdim return ConstantFoldFP(acos, V, Ty); 1828360784Sdim break; 1829360784Sdim case LibFunc_asin: 1830360784Sdim case LibFunc_asinf: 1831360784Sdim case LibFunc_asin_finite: 1832360784Sdim case LibFunc_asinf_finite: 1833360784Sdim if (TLI->has(Func)) 1834353358Sdim return ConstantFoldFP(asin, V, Ty); 1835360784Sdim break; 1836360784Sdim case LibFunc_atan: 1837360784Sdim case LibFunc_atanf: 1838360784Sdim if (TLI->has(Func)) 1839353358Sdim return ConstantFoldFP(atan, V, Ty); 1840353358Sdim break; 1841360784Sdim case LibFunc_ceil: 1842360784Sdim case LibFunc_ceilf: 1843360784Sdim if (TLI->has(Func)) { 1844360784Sdim U.roundToIntegral(APFloat::rmTowardPositive); 1845360784Sdim return ConstantFP::get(Ty->getContext(), U); 1846360784Sdim } 1847360784Sdim break; 1848360784Sdim case LibFunc_cos: 1849360784Sdim case LibFunc_cosf: 1850360784Sdim if (TLI->has(Func)) 1851353358Sdim return ConstantFoldFP(cos, V, Ty); 1852360784Sdim break; 1853360784Sdim case LibFunc_cosh: 1854360784Sdim case LibFunc_coshf: 1855360784Sdim case LibFunc_cosh_finite: 1856360784Sdim case LibFunc_coshf_finite: 1857360784Sdim if (TLI->has(Func)) 1858353358Sdim return ConstantFoldFP(cosh, V, Ty); 1859353358Sdim break; 1860360784Sdim case LibFunc_exp: 1861360784Sdim case LibFunc_expf: 1862360784Sdim case LibFunc_exp_finite: 1863360784Sdim case LibFunc_expf_finite: 1864360784Sdim if (TLI->has(Func)) 1865353358Sdim return ConstantFoldFP(exp, V, Ty); 1866360784Sdim break; 1867360784Sdim case LibFunc_exp2: 1868360784Sdim case LibFunc_exp2f: 1869360784Sdim case LibFunc_exp2_finite: 1870360784Sdim case LibFunc_exp2f_finite: 1871360784Sdim if (TLI->has(Func)) 1872360784Sdim // Fold exp2(x) as pow(2, x), in case the host lacks a C99 library. 1873353358Sdim return ConstantFoldBinaryFP(pow, 2.0, V, Ty); 1874353358Sdim break; 1875360784Sdim case LibFunc_fabs: 1876360784Sdim case LibFunc_fabsf: 1877360784Sdim if (TLI->has(Func)) { 1878360784Sdim U.clearSign(); 1879360784Sdim return ConstantFP::get(Ty->getContext(), U); 1880360784Sdim } 1881353358Sdim break; 1882360784Sdim case LibFunc_floor: 1883360784Sdim case LibFunc_floorf: 1884360784Sdim if (TLI->has(Func)) { 1885360784Sdim U.roundToIntegral(APFloat::rmTowardNegative); 1886360784Sdim return ConstantFP::get(Ty->getContext(), U); 1887360784Sdim } 1888360784Sdim break; 1889360784Sdim case LibFunc_log: 1890360784Sdim case LibFunc_logf: 1891360784Sdim case LibFunc_log_finite: 1892360784Sdim case LibFunc_logf_finite: 1893360784Sdim if (V > 0.0 && TLI->has(Func)) 1894353358Sdim return ConstantFoldFP(log, V, Ty); 1895360784Sdim break; 1896360784Sdim case LibFunc_log2: 1897360784Sdim case LibFunc_log2f: 1898360784Sdim case LibFunc_log2_finite: 1899360784Sdim case LibFunc_log2f_finite: 1900360784Sdim if (V > 0.0 && TLI->has(Func)) 1901360784Sdim // TODO: What about hosts that lack a C99 library? 1902360784Sdim return ConstantFoldFP(Log2, V, Ty); 1903360784Sdim break; 1904360784Sdim case LibFunc_log10: 1905360784Sdim case LibFunc_log10f: 1906360784Sdim case LibFunc_log10_finite: 1907360784Sdim case LibFunc_log10f_finite: 1908360784Sdim if (V > 0.0 && TLI->has(Func)) 1909360784Sdim // TODO: What about hosts that lack a C99 library? 1910353358Sdim return ConstantFoldFP(log10, V, Ty); 1911353358Sdim break; 1912360784Sdim case LibFunc_nearbyint: 1913360784Sdim case LibFunc_nearbyintf: 1914360784Sdim case LibFunc_rint: 1915360784Sdim case LibFunc_rintf: 1916360784Sdim if (TLI->has(Func)) { 1917360784Sdim U.roundToIntegral(APFloat::rmNearestTiesToEven); 1918360784Sdim return ConstantFP::get(Ty->getContext(), U); 1919360784Sdim } 1920353358Sdim break; 1921360784Sdim case LibFunc_round: 1922360784Sdim case LibFunc_roundf: 1923360784Sdim if (TLI->has(Func)) { 1924360784Sdim U.roundToIntegral(APFloat::rmNearestTiesToAway); 1925360784Sdim return ConstantFP::get(Ty->getContext(), U); 1926360784Sdim } 1927360784Sdim break; 1928360784Sdim case LibFunc_sin: 1929360784Sdim case LibFunc_sinf: 1930360784Sdim if (TLI->has(Func)) 1931353358Sdim return ConstantFoldFP(sin, V, Ty); 1932360784Sdim break; 1933360784Sdim case LibFunc_sinh: 1934360784Sdim case LibFunc_sinhf: 1935360784Sdim case LibFunc_sinh_finite: 1936360784Sdim case LibFunc_sinhf_finite: 1937360784Sdim if (TLI->has(Func)) 1938353358Sdim return ConstantFoldFP(sinh, V, Ty); 1939360784Sdim break; 1940360784Sdim case LibFunc_sqrt: 1941360784Sdim case LibFunc_sqrtf: 1942360784Sdim if (V >= 0.0 && TLI->has(Func)) 1943353358Sdim return ConstantFoldFP(sqrt, V, Ty); 1944353358Sdim break; 1945360784Sdim case LibFunc_tan: 1946360784Sdim case LibFunc_tanf: 1947360784Sdim if (TLI->has(Func)) 1948353358Sdim return ConstantFoldFP(tan, V, Ty); 1949360784Sdim break; 1950360784Sdim case LibFunc_tanh: 1951360784Sdim case LibFunc_tanhf: 1952360784Sdim if (TLI->has(Func)) 1953353358Sdim return ConstantFoldFP(tanh, V, Ty); 1954353358Sdim break; 1955360784Sdim case LibFunc_trunc: 1956360784Sdim case LibFunc_truncf: 1957360784Sdim if (TLI->has(Func)) { 1958360784Sdim U.roundToIntegral(APFloat::rmTowardZero); 1959360784Sdim return ConstantFP::get(Ty->getContext(), U); 1960360784Sdim } 1961353358Sdim break; 1962198090Srdivacky } 1963353358Sdim return nullptr; 1964353358Sdim } 1965218893Sdim 1966353358Sdim if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) { 1967353358Sdim switch (IntrinsicID) { 1968353358Sdim case Intrinsic::bswap: 1969353358Sdim return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap()); 1970353358Sdim case Intrinsic::ctpop: 1971353358Sdim return ConstantInt::get(Ty, Op->getValue().countPopulation()); 1972353358Sdim case Intrinsic::bitreverse: 1973353358Sdim return ConstantInt::get(Ty->getContext(), Op->getValue().reverseBits()); 1974353358Sdim case Intrinsic::convert_from_fp16: { 1975353358Sdim APFloat Val(APFloat::IEEEhalf(), Op->getValue()); 1976205407Srdivacky 1977353358Sdim bool lost = false; 1978353358Sdim APFloat::opStatus status = Val.convert( 1979353358Sdim Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &lost); 1980205407Srdivacky 1981353358Sdim // Conversion is always precise. 1982353358Sdim (void)status; 1983353358Sdim assert(status == APFloat::opOK && !lost && 1984353358Sdim "Precision lost during fp16 constfolding"); 1985205407Srdivacky 1986353358Sdim return ConstantFP::get(Ty->getContext(), Val); 1987193323Sed } 1988353358Sdim default: 1989353358Sdim return nullptr; 1990353358Sdim } 1991353358Sdim } 1992218893Sdim 1993353358Sdim // Support ConstantVector in case we have an Undef in the top. 1994353358Sdim if (isa<ConstantVector>(Operands[0]) || 1995353358Sdim isa<ConstantDataVector>(Operands[0])) { 1996353358Sdim auto *Op = cast<Constant>(Operands[0]); 1997353358Sdim switch (IntrinsicID) { 1998353358Sdim default: break; 1999353358Sdim case Intrinsic::x86_sse_cvtss2si: 2000353358Sdim case Intrinsic::x86_sse_cvtss2si64: 2001353358Sdim case Intrinsic::x86_sse2_cvtsd2si: 2002353358Sdim case Intrinsic::x86_sse2_cvtsd2si64: 2003353358Sdim if (ConstantFP *FPOp = 2004353358Sdim dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) 2005353358Sdim return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), 2006353358Sdim /*roundTowardZero=*/false, Ty, 2007353358Sdim /*IsSigned*/true); 2008353358Sdim break; 2009353358Sdim case Intrinsic::x86_sse_cvttss2si: 2010353358Sdim case Intrinsic::x86_sse_cvttss2si64: 2011353358Sdim case Intrinsic::x86_sse2_cvttsd2si: 2012353358Sdim case Intrinsic::x86_sse2_cvttsd2si64: 2013353358Sdim if (ConstantFP *FPOp = 2014353358Sdim dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) 2015353358Sdim return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), 2016353358Sdim /*roundTowardZero=*/true, Ty, 2017353358Sdim /*IsSigned*/true); 2018353358Sdim break; 2019218893Sdim } 2020198090Srdivacky } 2021218893Sdim 2022353358Sdim return nullptr; 2023353358Sdim} 2024249423Sdim 2025353358Sdimstatic Constant *ConstantFoldScalarCall2(StringRef Name, 2026353358Sdim Intrinsic::ID IntrinsicID, 2027353358Sdim Type *Ty, 2028353358Sdim ArrayRef<Constant *> Operands, 2029353358Sdim const TargetLibraryInfo *TLI, 2030353358Sdim const CallBase *Call) { 2031353358Sdim assert(Operands.size() == 2 && "Wrong number of operands."); 2032234353Sdim 2033353358Sdim if (auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) { 2034353358Sdim if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy()) 2035353358Sdim return nullptr; 2036353358Sdim double Op1V = getValueAsDouble(Op1); 2037280031Sdim 2038353358Sdim if (auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) { 2039353358Sdim if (Op2->getType() != Op1->getType()) 2040353358Sdim return nullptr; 2041280031Sdim 2042353358Sdim double Op2V = getValueAsDouble(Op2); 2043353358Sdim if (IntrinsicID == Intrinsic::pow) { 2044353358Sdim return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty); 2045353358Sdim } 2046353358Sdim if (IntrinsicID == Intrinsic::copysign) { 2047353358Sdim APFloat V1 = Op1->getValueAPF(); 2048353358Sdim const APFloat &V2 = Op2->getValueAPF(); 2049353358Sdim V1.copySign(V2); 2050353358Sdim return ConstantFP::get(Ty->getContext(), V1); 2051353358Sdim } 2052280031Sdim 2053353358Sdim if (IntrinsicID == Intrinsic::minnum) { 2054353358Sdim const APFloat &C1 = Op1->getValueAPF(); 2055353358Sdim const APFloat &C2 = Op2->getValueAPF(); 2056353358Sdim return ConstantFP::get(Ty->getContext(), minnum(C1, C2)); 2057353358Sdim } 2058344779Sdim 2059353358Sdim if (IntrinsicID == Intrinsic::maxnum) { 2060353358Sdim const APFloat &C1 = Op1->getValueAPF(); 2061353358Sdim const APFloat &C2 = Op2->getValueAPF(); 2062353358Sdim return ConstantFP::get(Ty->getContext(), maxnum(C1, C2)); 2063198090Srdivacky } 2064243830Sdim 2065353358Sdim if (IntrinsicID == Intrinsic::minimum) { 2066353358Sdim const APFloat &C1 = Op1->getValueAPF(); 2067353358Sdim const APFloat &C2 = Op2->getValueAPF(); 2068353358Sdim return ConstantFP::get(Ty->getContext(), minimum(C1, C2)); 2069193323Sed } 2070243830Sdim 2071353358Sdim if (IntrinsicID == Intrinsic::maximum) { 2072353358Sdim const APFloat &C1 = Op1->getValueAPF(); 2073353358Sdim const APFloat &C2 = Op2->getValueAPF(); 2074353358Sdim return ConstantFP::get(Ty->getContext(), maximum(C1, C2)); 2075344779Sdim } 2076344779Sdim 2077353358Sdim if (!TLI) 2078353358Sdim return nullptr; 2079360784Sdim 2080360784Sdim LibFunc Func = NotLibFunc; 2081360784Sdim TLI->getLibFunc(Name, Func); 2082360784Sdim switch (Func) { 2083360784Sdim default: 2084360784Sdim break; 2085360784Sdim case LibFunc_pow: 2086360784Sdim case LibFunc_powf: 2087360784Sdim case LibFunc_pow_finite: 2088360784Sdim case LibFunc_powf_finite: 2089360784Sdim if (TLI->has(Func)) 2090360784Sdim return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty); 2091360784Sdim break; 2092360784Sdim case LibFunc_fmod: 2093360784Sdim case LibFunc_fmodf: 2094360784Sdim if (TLI->has(Func)) { 2095360784Sdim APFloat V = Op1->getValueAPF(); 2096360784Sdim if (APFloat::opStatus::opOK == V.mod(Op2->getValueAPF())) 2097360784Sdim return ConstantFP::get(Ty->getContext(), V); 2098360784Sdim } 2099360784Sdim break; 2100360784Sdim case LibFunc_atan2: 2101360784Sdim case LibFunc_atan2f: 2102360784Sdim case LibFunc_atan2_finite: 2103360784Sdim case LibFunc_atan2f_finite: 2104360784Sdim if (TLI->has(Func)) 2105360784Sdim return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty); 2106360784Sdim break; 2107360784Sdim } 2108353358Sdim } else if (auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) { 2109353358Sdim if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy()) 2110353358Sdim return ConstantFP::get(Ty->getContext(), 2111353358Sdim APFloat((float)std::pow((float)Op1V, 2112353358Sdim (int)Op2C->getZExtValue()))); 2113353358Sdim if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy()) 2114353358Sdim return ConstantFP::get(Ty->getContext(), 2115353358Sdim APFloat((float)std::pow((float)Op1V, 2116353358Sdim (int)Op2C->getZExtValue()))); 2117353358Sdim if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy()) 2118353358Sdim return ConstantFP::get(Ty->getContext(), 2119353358Sdim APFloat((double)std::pow((double)Op1V, 2120353358Sdim (int)Op2C->getZExtValue()))); 2121193323Sed } 2122353358Sdim return nullptr; 2123353358Sdim } 2124344779Sdim 2125353358Sdim if (Operands[0]->getType()->isIntegerTy() && 2126353358Sdim Operands[1]->getType()->isIntegerTy()) { 2127353358Sdim const APInt *C0, *C1; 2128353358Sdim if (!getConstIntOrUndef(Operands[0], C0) || 2129353358Sdim !getConstIntOrUndef(Operands[1], C1)) 2130353358Sdim return nullptr; 2131353358Sdim 2132353358Sdim switch (IntrinsicID) { 2133353358Sdim default: break; 2134360784Sdim case Intrinsic::usub_with_overflow: 2135360784Sdim case Intrinsic::ssub_with_overflow: 2136360784Sdim case Intrinsic::uadd_with_overflow: 2137360784Sdim case Intrinsic::sadd_with_overflow: 2138360784Sdim // X - undef -> { undef, false } 2139360784Sdim // undef - X -> { undef, false } 2140360784Sdim // X + undef -> { undef, false } 2141360784Sdim // undef + x -> { undef, false } 2142360784Sdim if (!C0 || !C1) { 2143360784Sdim return ConstantStruct::get( 2144360784Sdim cast<StructType>(Ty), 2145360784Sdim {UndefValue::get(Ty->getStructElementType(0)), 2146360784Sdim Constant::getNullValue(Ty->getStructElementType(1))}); 2147360784Sdim } 2148360784Sdim LLVM_FALLTHROUGH; 2149353358Sdim case Intrinsic::smul_with_overflow: 2150360784Sdim case Intrinsic::umul_with_overflow: { 2151360784Sdim // undef * X -> { 0, false } 2152360784Sdim // X * undef -> { 0, false } 2153353358Sdim if (!C0 || !C1) 2154353358Sdim return Constant::getNullValue(Ty); 2155353358Sdim 2156353358Sdim APInt Res; 2157353358Sdim bool Overflow; 2158344779Sdim switch (IntrinsicID) { 2159353358Sdim default: llvm_unreachable("Invalid case"); 2160353358Sdim case Intrinsic::sadd_with_overflow: 2161353358Sdim Res = C0->sadd_ov(*C1, Overflow); 2162344779Sdim break; 2163353358Sdim case Intrinsic::uadd_with_overflow: 2164353358Sdim Res = C0->uadd_ov(*C1, Overflow); 2165344779Sdim break; 2166353358Sdim case Intrinsic::ssub_with_overflow: 2167353358Sdim Res = C0->ssub_ov(*C1, Overflow); 2168344779Sdim break; 2169353358Sdim case Intrinsic::usub_with_overflow: 2170353358Sdim Res = C0->usub_ov(*C1, Overflow); 2171344779Sdim break; 2172353358Sdim case Intrinsic::smul_with_overflow: 2173353358Sdim Res = C0->smul_ov(*C1, Overflow); 2174353358Sdim break; 2175353358Sdim case Intrinsic::umul_with_overflow: 2176353358Sdim Res = C0->umul_ov(*C1, Overflow); 2177353358Sdim break; 2178344779Sdim } 2179353358Sdim Constant *Ops[] = { 2180353358Sdim ConstantInt::get(Ty->getContext(), Res), 2181353358Sdim ConstantInt::get(Type::getInt1Ty(Ty->getContext()), Overflow) 2182353358Sdim }; 2183353358Sdim return ConstantStruct::get(cast<StructType>(Ty), Ops); 2184344779Sdim } 2185353358Sdim case Intrinsic::uadd_sat: 2186353358Sdim case Intrinsic::sadd_sat: 2187353358Sdim if (!C0 && !C1) 2188353358Sdim return UndefValue::get(Ty); 2189353358Sdim if (!C0 || !C1) 2190353358Sdim return Constant::getAllOnesValue(Ty); 2191353358Sdim if (IntrinsicID == Intrinsic::uadd_sat) 2192353358Sdim return ConstantInt::get(Ty, C0->uadd_sat(*C1)); 2193353358Sdim else 2194353358Sdim return ConstantInt::get(Ty, C0->sadd_sat(*C1)); 2195353358Sdim case Intrinsic::usub_sat: 2196353358Sdim case Intrinsic::ssub_sat: 2197353358Sdim if (!C0 && !C1) 2198353358Sdim return UndefValue::get(Ty); 2199353358Sdim if (!C0 || !C1) 2200353358Sdim return Constant::getNullValue(Ty); 2201353358Sdim if (IntrinsicID == Intrinsic::usub_sat) 2202353358Sdim return ConstantInt::get(Ty, C0->usub_sat(*C1)); 2203353358Sdim else 2204353358Sdim return ConstantInt::get(Ty, C0->ssub_sat(*C1)); 2205353358Sdim case Intrinsic::cttz: 2206353358Sdim case Intrinsic::ctlz: 2207353358Sdim assert(C1 && "Must be constant int"); 2208353358Sdim 2209353358Sdim // cttz(0, 1) and ctlz(0, 1) are undef. 2210353358Sdim if (C1->isOneValue() && (!C0 || C0->isNullValue())) 2211353358Sdim return UndefValue::get(Ty); 2212353358Sdim if (!C0) 2213353358Sdim return Constant::getNullValue(Ty); 2214353358Sdim if (IntrinsicID == Intrinsic::cttz) 2215353358Sdim return ConstantInt::get(Ty, C0->countTrailingZeros()); 2216353358Sdim else 2217353358Sdim return ConstantInt::get(Ty, C0->countLeadingZeros()); 2218353358Sdim } 2219353358Sdim 2220276479Sdim return nullptr; 2221193323Sed } 2222276479Sdim 2223353358Sdim // Support ConstantVector in case we have an Undef in the top. 2224353358Sdim if ((isa<ConstantVector>(Operands[0]) || 2225353358Sdim isa<ConstantDataVector>(Operands[0])) && 2226353358Sdim // Check for default rounding mode. 2227353358Sdim // FIXME: Support other rounding modes? 2228353358Sdim isa<ConstantInt>(Operands[1]) && 2229353358Sdim cast<ConstantInt>(Operands[1])->getValue() == 4) { 2230353358Sdim auto *Op = cast<Constant>(Operands[0]); 2231353358Sdim switch (IntrinsicID) { 2232353358Sdim default: break; 2233353358Sdim case Intrinsic::x86_avx512_vcvtss2si32: 2234353358Sdim case Intrinsic::x86_avx512_vcvtss2si64: 2235353358Sdim case Intrinsic::x86_avx512_vcvtsd2si32: 2236353358Sdim case Intrinsic::x86_avx512_vcvtsd2si64: 2237353358Sdim if (ConstantFP *FPOp = 2238353358Sdim dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) 2239353358Sdim return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), 2240353358Sdim /*roundTowardZero=*/false, Ty, 2241353358Sdim /*IsSigned*/true); 2242353358Sdim break; 2243353358Sdim case Intrinsic::x86_avx512_vcvtss2usi32: 2244353358Sdim case Intrinsic::x86_avx512_vcvtss2usi64: 2245353358Sdim case Intrinsic::x86_avx512_vcvtsd2usi32: 2246353358Sdim case Intrinsic::x86_avx512_vcvtsd2usi64: 2247353358Sdim if (ConstantFP *FPOp = 2248353358Sdim dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) 2249353358Sdim return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), 2250353358Sdim /*roundTowardZero=*/false, Ty, 2251353358Sdim /*IsSigned*/false); 2252353358Sdim break; 2253353358Sdim case Intrinsic::x86_avx512_cvttss2si: 2254353358Sdim case Intrinsic::x86_avx512_cvttss2si64: 2255353358Sdim case Intrinsic::x86_avx512_cvttsd2si: 2256353358Sdim case Intrinsic::x86_avx512_cvttsd2si64: 2257353358Sdim if (ConstantFP *FPOp = 2258353358Sdim dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) 2259353358Sdim return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), 2260353358Sdim /*roundTowardZero=*/true, Ty, 2261353358Sdim /*IsSigned*/true); 2262353358Sdim break; 2263353358Sdim case Intrinsic::x86_avx512_cvttss2usi: 2264353358Sdim case Intrinsic::x86_avx512_cvttss2usi64: 2265353358Sdim case Intrinsic::x86_avx512_cvttsd2usi: 2266353358Sdim case Intrinsic::x86_avx512_cvttsd2usi64: 2267353358Sdim if (ConstantFP *FPOp = 2268353358Sdim dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) 2269353358Sdim return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), 2270353358Sdim /*roundTowardZero=*/true, Ty, 2271353358Sdim /*IsSigned*/false); 2272353358Sdim break; 2273353358Sdim } 2274353358Sdim } 2275353358Sdim return nullptr; 2276353358Sdim} 2277276479Sdim 2278353358Sdimstatic Constant *ConstantFoldScalarCall3(StringRef Name, 2279353358Sdim Intrinsic::ID IntrinsicID, 2280353358Sdim Type *Ty, 2281353358Sdim ArrayRef<Constant *> Operands, 2282353358Sdim const TargetLibraryInfo *TLI, 2283353358Sdim const CallBase *Call) { 2284353358Sdim assert(Operands.size() == 3 && "Wrong number of operands."); 2285353358Sdim 2286309124Sdim if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) { 2287309124Sdim if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) { 2288309124Sdim if (const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) { 2289276479Sdim switch (IntrinsicID) { 2290276479Sdim default: break; 2291276479Sdim case Intrinsic::fma: 2292276479Sdim case Intrinsic::fmuladd: { 2293276479Sdim APFloat V = Op1->getValueAPF(); 2294360784Sdim V.fusedMultiplyAdd(Op2->getValueAPF(), Op3->getValueAPF(), 2295360784Sdim APFloat::rmNearestTiesToEven); 2296360784Sdim return ConstantFP::get(Ty->getContext(), V); 2297276479Sdim } 2298276479Sdim } 2299276479Sdim } 2300276479Sdim } 2301276479Sdim } 2302276479Sdim 2303353358Sdim if (const auto *Op1 = dyn_cast<ConstantInt>(Operands[0])) { 2304353358Sdim if (const auto *Op2 = dyn_cast<ConstantInt>(Operands[1])) { 2305353358Sdim if (const auto *Op3 = dyn_cast<ConstantInt>(Operands[2])) { 2306353358Sdim switch (IntrinsicID) { 2307353358Sdim default: break; 2308353358Sdim case Intrinsic::smul_fix: 2309353358Sdim case Intrinsic::smul_fix_sat: { 2310353358Sdim // This code performs rounding towards negative infinity in case the 2311353358Sdim // result cannot be represented exactly for the given scale. Targets 2312353358Sdim // that do care about rounding should use a target hook for specifying 2313353358Sdim // how rounding should be done, and provide their own folding to be 2314353358Sdim // consistent with rounding. This is the same approach as used by 2315353358Sdim // DAGTypeLegalizer::ExpandIntRes_MULFIX. 2316353358Sdim APInt Lhs = Op1->getValue(); 2317353358Sdim APInt Rhs = Op2->getValue(); 2318353358Sdim unsigned Scale = Op3->getValue().getZExtValue(); 2319353358Sdim unsigned Width = Lhs.getBitWidth(); 2320353358Sdim assert(Scale < Width && "Illegal scale."); 2321353358Sdim unsigned ExtendedWidth = Width * 2; 2322353358Sdim APInt Product = (Lhs.sextOrSelf(ExtendedWidth) * 2323353358Sdim Rhs.sextOrSelf(ExtendedWidth)).ashr(Scale); 2324353358Sdim if (IntrinsicID == Intrinsic::smul_fix_sat) { 2325353358Sdim APInt MaxValue = 2326353358Sdim APInt::getSignedMaxValue(Width).sextOrSelf(ExtendedWidth); 2327353358Sdim APInt MinValue = 2328353358Sdim APInt::getSignedMinValue(Width).sextOrSelf(ExtendedWidth); 2329353358Sdim Product = APIntOps::smin(Product, MaxValue); 2330353358Sdim Product = APIntOps::smax(Product, MinValue); 2331353358Sdim } 2332353358Sdim return ConstantInt::get(Ty->getContext(), 2333353358Sdim Product.sextOrTrunc(Width)); 2334353358Sdim } 2335353358Sdim } 2336353358Sdim } 2337353358Sdim } 2338353358Sdim } 2339353358Sdim 2340344779Sdim if (IntrinsicID == Intrinsic::fshl || IntrinsicID == Intrinsic::fshr) { 2341344779Sdim const APInt *C0, *C1, *C2; 2342344779Sdim if (!getConstIntOrUndef(Operands[0], C0) || 2343344779Sdim !getConstIntOrUndef(Operands[1], C1) || 2344344779Sdim !getConstIntOrUndef(Operands[2], C2)) 2345344779Sdim return nullptr; 2346344779Sdim 2347344779Sdim bool IsRight = IntrinsicID == Intrinsic::fshr; 2348344779Sdim if (!C2) 2349344779Sdim return Operands[IsRight ? 1 : 0]; 2350344779Sdim if (!C0 && !C1) 2351344779Sdim return UndefValue::get(Ty); 2352344779Sdim 2353344779Sdim // The shift amount is interpreted as modulo the bitwidth. If the shift 2354344779Sdim // amount is effectively 0, avoid UB due to oversized inverse shift below. 2355344779Sdim unsigned BitWidth = C2->getBitWidth(); 2356344779Sdim unsigned ShAmt = C2->urem(BitWidth); 2357344779Sdim if (!ShAmt) 2358344779Sdim return Operands[IsRight ? 1 : 0]; 2359344779Sdim 2360344779Sdim // (C0 << ShlAmt) | (C1 >> LshrAmt) 2361344779Sdim unsigned LshrAmt = IsRight ? ShAmt : BitWidth - ShAmt; 2362344779Sdim unsigned ShlAmt = !IsRight ? ShAmt : BitWidth - ShAmt; 2363344779Sdim if (!C0) 2364344779Sdim return ConstantInt::get(Ty, C1->lshr(LshrAmt)); 2365344779Sdim if (!C1) 2366344779Sdim return ConstantInt::get(Ty, C0->shl(ShlAmt)); 2367344779Sdim return ConstantInt::get(Ty, C0->shl(ShlAmt) | C1->lshr(LshrAmt)); 2368344779Sdim } 2369344779Sdim 2370276479Sdim return nullptr; 2371193323Sed} 2372276479Sdim 2373353358Sdimstatic Constant *ConstantFoldScalarCall(StringRef Name, 2374353358Sdim Intrinsic::ID IntrinsicID, 2375353358Sdim Type *Ty, 2376353358Sdim ArrayRef<Constant *> Operands, 2377353358Sdim const TargetLibraryInfo *TLI, 2378353358Sdim const CallBase *Call) { 2379353358Sdim if (Operands.size() == 1) 2380353358Sdim return ConstantFoldScalarCall1(Name, IntrinsicID, Ty, Operands, TLI, Call); 2381353358Sdim 2382353358Sdim if (Operands.size() == 2) 2383353358Sdim return ConstantFoldScalarCall2(Name, IntrinsicID, Ty, Operands, TLI, Call); 2384353358Sdim 2385353358Sdim if (Operands.size() == 3) 2386353358Sdim return ConstantFoldScalarCall3(Name, IntrinsicID, Ty, Operands, TLI, Call); 2387353358Sdim 2388353358Sdim return nullptr; 2389353358Sdim} 2390353358Sdim 2391353358Sdimstatic Constant *ConstantFoldVectorCall(StringRef Name, 2392353358Sdim Intrinsic::ID IntrinsicID, 2393353358Sdim VectorType *VTy, 2394353358Sdim ArrayRef<Constant *> Operands, 2395353358Sdim const DataLayout &DL, 2396353358Sdim const TargetLibraryInfo *TLI, 2397353358Sdim const CallBase *Call) { 2398276479Sdim SmallVector<Constant *, 4> Result(VTy->getNumElements()); 2399276479Sdim SmallVector<Constant *, 4> Lane(Operands.size()); 2400276479Sdim Type *Ty = VTy->getElementType(); 2401276479Sdim 2402309124Sdim if (IntrinsicID == Intrinsic::masked_load) { 2403309124Sdim auto *SrcPtr = Operands[0]; 2404309124Sdim auto *Mask = Operands[2]; 2405309124Sdim auto *Passthru = Operands[3]; 2406309124Sdim 2407309124Sdim Constant *VecData = ConstantFoldLoadFromConstPtr(SrcPtr, VTy, DL); 2408309124Sdim 2409309124Sdim SmallVector<Constant *, 32> NewElements; 2410309124Sdim for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { 2411309124Sdim auto *MaskElt = Mask->getAggregateElement(I); 2412309124Sdim if (!MaskElt) 2413309124Sdim break; 2414309124Sdim auto *PassthruElt = Passthru->getAggregateElement(I); 2415309124Sdim auto *VecElt = VecData ? VecData->getAggregateElement(I) : nullptr; 2416309124Sdim if (isa<UndefValue>(MaskElt)) { 2417309124Sdim if (PassthruElt) 2418309124Sdim NewElements.push_back(PassthruElt); 2419309124Sdim else if (VecElt) 2420309124Sdim NewElements.push_back(VecElt); 2421309124Sdim else 2422309124Sdim return nullptr; 2423309124Sdim } 2424309124Sdim if (MaskElt->isNullValue()) { 2425309124Sdim if (!PassthruElt) 2426309124Sdim return nullptr; 2427309124Sdim NewElements.push_back(PassthruElt); 2428309124Sdim } else if (MaskElt->isOneValue()) { 2429309124Sdim if (!VecElt) 2430309124Sdim return nullptr; 2431309124Sdim NewElements.push_back(VecElt); 2432309124Sdim } else { 2433309124Sdim return nullptr; 2434309124Sdim } 2435309124Sdim } 2436309124Sdim if (NewElements.size() != VTy->getNumElements()) 2437309124Sdim return nullptr; 2438309124Sdim return ConstantVector::get(NewElements); 2439309124Sdim } 2440309124Sdim 2441276479Sdim for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { 2442276479Sdim // Gather a column of constants. 2443276479Sdim for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) { 2444353358Sdim // Some intrinsics use a scalar type for certain arguments. 2445353358Sdim if (hasVectorInstrinsicScalarOpd(IntrinsicID, J)) { 2446321369Sdim Lane[J] = Operands[J]; 2447321369Sdim continue; 2448321369Sdim } 2449321369Sdim 2450276479Sdim Constant *Agg = Operands[J]->getAggregateElement(I); 2451276479Sdim if (!Agg) 2452276479Sdim return nullptr; 2453276479Sdim 2454276479Sdim Lane[J] = Agg; 2455276479Sdim } 2456276479Sdim 2457276479Sdim // Use the regular scalar folding to simplify this column. 2458353358Sdim Constant *Folded = 2459353358Sdim ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI, Call); 2460276479Sdim if (!Folded) 2461276479Sdim return nullptr; 2462276479Sdim Result[I] = Folded; 2463276479Sdim } 2464276479Sdim 2465276479Sdim return ConstantVector::get(Result); 2466276479Sdim} 2467276479Sdim 2468309124Sdim} // end anonymous namespace 2469309124Sdim 2470353358SdimConstant *llvm::ConstantFoldCall(const CallBase *Call, Function *F, 2471353358Sdim ArrayRef<Constant *> Operands, 2472353358Sdim const TargetLibraryInfo *TLI) { 2473353358Sdim if (Call->isNoBuiltin() || Call->isStrictFP()) 2474321369Sdim return nullptr; 2475276479Sdim if (!F->hasName()) 2476276479Sdim return nullptr; 2477276479Sdim StringRef Name = F->getName(); 2478276479Sdim 2479276479Sdim Type *Ty = F->getReturnType(); 2480276479Sdim 2481309124Sdim if (auto *VTy = dyn_cast<VectorType>(Ty)) 2482309124Sdim return ConstantFoldVectorCall(Name, F->getIntrinsicID(), VTy, Operands, 2483353358Sdim F->getParent()->getDataLayout(), TLI, Call); 2484276479Sdim 2485353358Sdim return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI, 2486353358Sdim Call); 2487276479Sdim} 2488314564Sdim 2489353358Sdimbool llvm::isMathLibCallNoop(const CallBase *Call, 2490353358Sdim const TargetLibraryInfo *TLI) { 2491314564Sdim // FIXME: Refactor this code; this duplicates logic in LibCallsShrinkWrap 2492314564Sdim // (and to some extent ConstantFoldScalarCall). 2493353358Sdim if (Call->isNoBuiltin() || Call->isStrictFP()) 2494321369Sdim return false; 2495353358Sdim Function *F = Call->getCalledFunction(); 2496314564Sdim if (!F) 2497314564Sdim return false; 2498314564Sdim 2499321369Sdim LibFunc Func; 2500314564Sdim if (!TLI || !TLI->getLibFunc(*F, Func)) 2501314564Sdim return false; 2502314564Sdim 2503353358Sdim if (Call->getNumArgOperands() == 1) { 2504353358Sdim if (ConstantFP *OpC = dyn_cast<ConstantFP>(Call->getArgOperand(0))) { 2505314564Sdim const APFloat &Op = OpC->getValueAPF(); 2506314564Sdim switch (Func) { 2507321369Sdim case LibFunc_logl: 2508321369Sdim case LibFunc_log: 2509321369Sdim case LibFunc_logf: 2510321369Sdim case LibFunc_log2l: 2511321369Sdim case LibFunc_log2: 2512321369Sdim case LibFunc_log2f: 2513321369Sdim case LibFunc_log10l: 2514321369Sdim case LibFunc_log10: 2515321369Sdim case LibFunc_log10f: 2516314564Sdim return Op.isNaN() || (!Op.isZero() && !Op.isNegative()); 2517314564Sdim 2518321369Sdim case LibFunc_expl: 2519321369Sdim case LibFunc_exp: 2520321369Sdim case LibFunc_expf: 2521314564Sdim // FIXME: These boundaries are slightly conservative. 2522314564Sdim if (OpC->getType()->isDoubleTy()) 2523314564Sdim return Op.compare(APFloat(-745.0)) != APFloat::cmpLessThan && 2524314564Sdim Op.compare(APFloat(709.0)) != APFloat::cmpGreaterThan; 2525314564Sdim if (OpC->getType()->isFloatTy()) 2526314564Sdim return Op.compare(APFloat(-103.0f)) != APFloat::cmpLessThan && 2527314564Sdim Op.compare(APFloat(88.0f)) != APFloat::cmpGreaterThan; 2528314564Sdim break; 2529314564Sdim 2530321369Sdim case LibFunc_exp2l: 2531321369Sdim case LibFunc_exp2: 2532321369Sdim case LibFunc_exp2f: 2533314564Sdim // FIXME: These boundaries are slightly conservative. 2534314564Sdim if (OpC->getType()->isDoubleTy()) 2535314564Sdim return Op.compare(APFloat(-1074.0)) != APFloat::cmpLessThan && 2536314564Sdim Op.compare(APFloat(1023.0)) != APFloat::cmpGreaterThan; 2537314564Sdim if (OpC->getType()->isFloatTy()) 2538314564Sdim return Op.compare(APFloat(-149.0f)) != APFloat::cmpLessThan && 2539314564Sdim Op.compare(APFloat(127.0f)) != APFloat::cmpGreaterThan; 2540314564Sdim break; 2541314564Sdim 2542321369Sdim case LibFunc_sinl: 2543321369Sdim case LibFunc_sin: 2544321369Sdim case LibFunc_sinf: 2545321369Sdim case LibFunc_cosl: 2546321369Sdim case LibFunc_cos: 2547321369Sdim case LibFunc_cosf: 2548314564Sdim return !Op.isInfinity(); 2549314564Sdim 2550321369Sdim case LibFunc_tanl: 2551321369Sdim case LibFunc_tan: 2552321369Sdim case LibFunc_tanf: { 2553314564Sdim // FIXME: Stop using the host math library. 2554314564Sdim // FIXME: The computation isn't done in the right precision. 2555314564Sdim Type *Ty = OpC->getType(); 2556314564Sdim if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) { 2557314564Sdim double OpV = getValueAsDouble(OpC); 2558314564Sdim return ConstantFoldFP(tan, OpV, Ty) != nullptr; 2559314564Sdim } 2560314564Sdim break; 2561314564Sdim } 2562314564Sdim 2563321369Sdim case LibFunc_asinl: 2564321369Sdim case LibFunc_asin: 2565321369Sdim case LibFunc_asinf: 2566321369Sdim case LibFunc_acosl: 2567321369Sdim case LibFunc_acos: 2568321369Sdim case LibFunc_acosf: 2569314564Sdim return Op.compare(APFloat(Op.getSemantics(), "-1")) != 2570314564Sdim APFloat::cmpLessThan && 2571314564Sdim Op.compare(APFloat(Op.getSemantics(), "1")) != 2572314564Sdim APFloat::cmpGreaterThan; 2573314564Sdim 2574321369Sdim case LibFunc_sinh: 2575321369Sdim case LibFunc_cosh: 2576321369Sdim case LibFunc_sinhf: 2577321369Sdim case LibFunc_coshf: 2578321369Sdim case LibFunc_sinhl: 2579321369Sdim case LibFunc_coshl: 2580314564Sdim // FIXME: These boundaries are slightly conservative. 2581314564Sdim if (OpC->getType()->isDoubleTy()) 2582314564Sdim return Op.compare(APFloat(-710.0)) != APFloat::cmpLessThan && 2583314564Sdim Op.compare(APFloat(710.0)) != APFloat::cmpGreaterThan; 2584314564Sdim if (OpC->getType()->isFloatTy()) 2585314564Sdim return Op.compare(APFloat(-89.0f)) != APFloat::cmpLessThan && 2586314564Sdim Op.compare(APFloat(89.0f)) != APFloat::cmpGreaterThan; 2587314564Sdim break; 2588314564Sdim 2589321369Sdim case LibFunc_sqrtl: 2590321369Sdim case LibFunc_sqrt: 2591321369Sdim case LibFunc_sqrtf: 2592314564Sdim return Op.isNaN() || Op.isZero() || !Op.isNegative(); 2593314564Sdim 2594314564Sdim // FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p, 2595314564Sdim // maybe others? 2596314564Sdim default: 2597314564Sdim break; 2598314564Sdim } 2599314564Sdim } 2600314564Sdim } 2601314564Sdim 2602353358Sdim if (Call->getNumArgOperands() == 2) { 2603353358Sdim ConstantFP *Op0C = dyn_cast<ConstantFP>(Call->getArgOperand(0)); 2604353358Sdim ConstantFP *Op1C = dyn_cast<ConstantFP>(Call->getArgOperand(1)); 2605314564Sdim if (Op0C && Op1C) { 2606314564Sdim const APFloat &Op0 = Op0C->getValueAPF(); 2607314564Sdim const APFloat &Op1 = Op1C->getValueAPF(); 2608314564Sdim 2609314564Sdim switch (Func) { 2610321369Sdim case LibFunc_powl: 2611321369Sdim case LibFunc_pow: 2612321369Sdim case LibFunc_powf: { 2613314564Sdim // FIXME: Stop using the host math library. 2614314564Sdim // FIXME: The computation isn't done in the right precision. 2615314564Sdim Type *Ty = Op0C->getType(); 2616314564Sdim if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) { 2617314564Sdim if (Ty == Op1C->getType()) { 2618314564Sdim double Op0V = getValueAsDouble(Op0C); 2619314564Sdim double Op1V = getValueAsDouble(Op1C); 2620314564Sdim return ConstantFoldBinaryFP(pow, Op0V, Op1V, Ty) != nullptr; 2621314564Sdim } 2622314564Sdim } 2623314564Sdim break; 2624314564Sdim } 2625314564Sdim 2626321369Sdim case LibFunc_fmodl: 2627321369Sdim case LibFunc_fmod: 2628321369Sdim case LibFunc_fmodf: 2629314564Sdim return Op0.isNaN() || Op1.isNaN() || 2630314564Sdim (!Op0.isInfinity() && !Op1.isZero()); 2631314564Sdim 2632314564Sdim default: 2633314564Sdim break; 2634314564Sdim } 2635314564Sdim } 2636314564Sdim } 2637314564Sdim 2638314564Sdim return false; 2639314564Sdim} 2640