1198090Srdivacky//===-- ConstantFolding.cpp - Fold instructions into constants ------------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10198090Srdivacky// This file defines routines for folding instructions into constants. 11193323Sed// 12249423Sdim// Also, to supplement the basic IR ConstantExpr simplifications, 13198090Srdivacky// this file defines some additional folding routines that can make use of 14249423Sdim// DataLayout information. These functions cannot go in IR due to library 15198090Srdivacky// dependency issues. 16198090Srdivacky// 17193323Sed//===----------------------------------------------------------------------===// 18193323Sed 19193323Sed#include "llvm/Analysis/ConstantFolding.h" 20251662Sdim#include "llvm/ADT/SmallPtrSet.h" 21193323Sed#include "llvm/ADT/SmallVector.h" 22193323Sed#include "llvm/ADT/StringMap.h" 23249423Sdim#include "llvm/Analysis/ValueTracking.h" 24249423Sdim#include "llvm/IR/Constants.h" 25249423Sdim#include "llvm/IR/DataLayout.h" 26249423Sdim#include "llvm/IR/DerivedTypes.h" 27249423Sdim#include "llvm/IR/Function.h" 28249423Sdim#include "llvm/IR/GlobalVariable.h" 29249423Sdim#include "llvm/IR/Instructions.h" 30249423Sdim#include "llvm/IR/Intrinsics.h" 31249423Sdim#include "llvm/IR/Operator.h" 32198090Srdivacky#include "llvm/Support/ErrorHandling.h" 33249423Sdim#include "llvm/Support/FEnv.h" 34193323Sed#include "llvm/Support/GetElementPtrTypeIterator.h" 35193323Sed#include "llvm/Support/MathExtras.h" 36249423Sdim#include "llvm/Target/TargetLibraryInfo.h" 37193323Sed#include <cerrno> 38193323Sed#include <cmath> 39193323Sedusing namespace llvm; 40193323Sed 41193323Sed//===----------------------------------------------------------------------===// 42193323Sed// Constant Folding internal helper functions 43193323Sed//===----------------------------------------------------------------------===// 44193323Sed 45243830Sdim/// FoldBitCast - Constant fold bitcast, symbolically evaluating it with 46243830Sdim/// DataLayout. This always returns a non-null constant, but it may be a 47198892Srdivacky/// ConstantExpr if unfoldable. 48226633Sdimstatic Constant *FoldBitCast(Constant *C, Type *DestTy, 49243830Sdim const DataLayout &TD) { 50226633Sdim // Catch the obvious splat cases. 51226633Sdim if (C->isNullValue() && !DestTy->isX86_MMXTy()) 52226633Sdim return Constant::getNullValue(DestTy); 53226633Sdim if (C->isAllOnesValue() && !DestTy->isX86_MMXTy()) 54226633Sdim return Constant::getAllOnesValue(DestTy); 55226633Sdim 56234353Sdim // Handle a vector->integer cast. 57234353Sdim if (IntegerType *IT = dyn_cast<IntegerType>(DestTy)) { 58249423Sdim VectorType *VTy = dyn_cast<VectorType>(C->getType()); 59249423Sdim if (VTy == 0) 60234353Sdim return ConstantExpr::getBitCast(C, DestTy); 61234353Sdim 62249423Sdim unsigned NumSrcElts = VTy->getNumElements(); 63249423Sdim Type *SrcEltTy = VTy->getElementType(); 64243830Sdim 65234353Sdim // If the vector is a vector of floating point, convert it to vector of int 66234353Sdim // to simplify things. 67234353Sdim if (SrcEltTy->isFloatingPointTy()) { 68234353Sdim unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits(); 69234353Sdim Type *SrcIVTy = 70234353Sdim VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElts); 71249423Sdim // Ask IR to do the conversion now that #elts line up. 72234353Sdim C = ConstantExpr::getBitCast(C, SrcIVTy); 73234353Sdim } 74243830Sdim 75249423Sdim ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C); 76249423Sdim if (CDV == 0) 77249423Sdim return ConstantExpr::getBitCast(C, DestTy); 78249423Sdim 79234353Sdim // Now that we know that the input value is a vector of integers, just shift 80234353Sdim // and insert them into our result. 81234353Sdim unsigned BitShift = TD.getTypeAllocSizeInBits(SrcEltTy); 82234353Sdim APInt Result(IT->getBitWidth(), 0); 83234353Sdim for (unsigned i = 0; i != NumSrcElts; ++i) { 84234353Sdim Result <<= BitShift; 85234353Sdim if (TD.isLittleEndian()) 86234353Sdim Result |= CDV->getElementAsInteger(NumSrcElts-i-1); 87234353Sdim else 88234353Sdim Result |= CDV->getElementAsInteger(i); 89234353Sdim } 90243830Sdim 91234353Sdim return ConstantInt::get(IT, Result); 92234353Sdim } 93243830Sdim 94226633Sdim // The code below only handles casts to vectors currently. 95226633Sdim VectorType *DestVTy = dyn_cast<VectorType>(DestTy); 96198892Srdivacky if (DestVTy == 0) 97198892Srdivacky return ConstantExpr::getBitCast(C, DestTy); 98243830Sdim 99198892Srdivacky // If this is a scalar -> vector cast, convert the input into a <1 x scalar> 100198892Srdivacky // vector so the code below can handle it uniformly. 101198892Srdivacky if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) { 102198892Srdivacky Constant *Ops = C; // don't take the address of C! 103218893Sdim return FoldBitCast(ConstantVector::get(Ops), DestTy, TD); 104198892Srdivacky } 105243830Sdim 106198892Srdivacky // If this is a bitcast from constant vector -> vector, fold it. 107234353Sdim if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C)) 108198892Srdivacky return ConstantExpr::getBitCast(C, DestTy); 109243830Sdim 110249423Sdim // If the element types match, IR can fold it. 111198892Srdivacky unsigned NumDstElt = DestVTy->getNumElements(); 112234353Sdim unsigned NumSrcElt = C->getType()->getVectorNumElements(); 113198892Srdivacky if (NumDstElt == NumSrcElt) 114198892Srdivacky return ConstantExpr::getBitCast(C, DestTy); 115243830Sdim 116234353Sdim Type *SrcEltTy = C->getType()->getVectorElementType(); 117226633Sdim Type *DstEltTy = DestVTy->getElementType(); 118243830Sdim 119243830Sdim // Otherwise, we're changing the number of elements in a vector, which 120198892Srdivacky // requires endianness information to do the right thing. For example, 121198892Srdivacky // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>) 122198892Srdivacky // folds to (little endian): 123198892Srdivacky // <4 x i32> <i32 0, i32 0, i32 1, i32 0> 124198892Srdivacky // and to (big endian): 125198892Srdivacky // <4 x i32> <i32 0, i32 0, i32 0, i32 1> 126243830Sdim 127198892Srdivacky // First thing is first. We only want to think about integer here, so if 128198892Srdivacky // we have something in FP form, recast it as integer. 129203954Srdivacky if (DstEltTy->isFloatingPointTy()) { 130198892Srdivacky // Fold to an vector of integers with same size as our FP type. 131198892Srdivacky unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits(); 132226633Sdim Type *DestIVTy = 133198892Srdivacky VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt); 134198892Srdivacky // Recursively handle this integer conversion, if possible. 135198892Srdivacky C = FoldBitCast(C, DestIVTy, TD); 136243830Sdim 137249423Sdim // Finally, IR can handle this now that #elts line up. 138198892Srdivacky return ConstantExpr::getBitCast(C, DestTy); 139198892Srdivacky } 140243830Sdim 141198892Srdivacky // Okay, we know the destination is integer, if the input is FP, convert 142198892Srdivacky // it to integer first. 143203954Srdivacky if (SrcEltTy->isFloatingPointTy()) { 144198892Srdivacky unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits(); 145226633Sdim Type *SrcIVTy = 146198892Srdivacky VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElt); 147249423Sdim // Ask IR to do the conversion now that #elts line up. 148198892Srdivacky C = ConstantExpr::getBitCast(C, SrcIVTy); 149249423Sdim // If IR wasn't able to fold it, bail out. 150234353Sdim if (!isa<ConstantVector>(C) && // FIXME: Remove ConstantVector. 151234353Sdim !isa<ConstantDataVector>(C)) 152198892Srdivacky return C; 153198892Srdivacky } 154243830Sdim 155198892Srdivacky // Now we know that the input and output vectors are both integer vectors 156198892Srdivacky // of the same size, and that their #elements is not the same. Do the 157198892Srdivacky // conversion here, which depends on whether the input or output has 158198892Srdivacky // more elements. 159198892Srdivacky bool isLittleEndian = TD.isLittleEndian(); 160243830Sdim 161198892Srdivacky SmallVector<Constant*, 32> Result; 162198892Srdivacky if (NumDstElt < NumSrcElt) { 163198892Srdivacky // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>) 164198892Srdivacky Constant *Zero = Constant::getNullValue(DstEltTy); 165198892Srdivacky unsigned Ratio = NumSrcElt/NumDstElt; 166198892Srdivacky unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits(); 167198892Srdivacky unsigned SrcElt = 0; 168198892Srdivacky for (unsigned i = 0; i != NumDstElt; ++i) { 169198892Srdivacky // Build each element of the result. 170198892Srdivacky Constant *Elt = Zero; 171198892Srdivacky unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1); 172198892Srdivacky for (unsigned j = 0; j != Ratio; ++j) { 173234353Sdim Constant *Src =dyn_cast<ConstantInt>(C->getAggregateElement(SrcElt++)); 174198892Srdivacky if (!Src) // Reject constantexpr elements. 175198892Srdivacky return ConstantExpr::getBitCast(C, DestTy); 176243830Sdim 177198892Srdivacky // Zero extend the element to the right size. 178198892Srdivacky Src = ConstantExpr::getZExt(Src, Elt->getType()); 179243830Sdim 180198892Srdivacky // Shift it to the right place, depending on endianness. 181243830Sdim Src = ConstantExpr::getShl(Src, 182198892Srdivacky ConstantInt::get(Src->getType(), ShiftAmt)); 183198892Srdivacky ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize; 184243830Sdim 185198892Srdivacky // Mix it in. 186198892Srdivacky Elt = ConstantExpr::getOr(Elt, Src); 187198892Srdivacky } 188198892Srdivacky Result.push_back(Elt); 189198892Srdivacky } 190234353Sdim return ConstantVector::get(Result); 191234353Sdim } 192243830Sdim 193234353Sdim // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>) 194234353Sdim unsigned Ratio = NumDstElt/NumSrcElt; 195234353Sdim unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits(); 196243830Sdim 197234353Sdim // Loop over each source value, expanding into multiple results. 198234353Sdim for (unsigned i = 0; i != NumSrcElt; ++i) { 199234353Sdim Constant *Src = dyn_cast<ConstantInt>(C->getAggregateElement(i)); 200234353Sdim if (!Src) // Reject constantexpr elements. 201234353Sdim return ConstantExpr::getBitCast(C, DestTy); 202243830Sdim 203234353Sdim unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1); 204234353Sdim for (unsigned j = 0; j != Ratio; ++j) { 205234353Sdim // Shift the piece of the value into the right place, depending on 206234353Sdim // endianness. 207243830Sdim Constant *Elt = ConstantExpr::getLShr(Src, 208234353Sdim ConstantInt::get(Src->getType(), ShiftAmt)); 209234353Sdim ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize; 210243830Sdim 211234353Sdim // Truncate and remember this piece. 212234353Sdim Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy)); 213198892Srdivacky } 214198892Srdivacky } 215243830Sdim 216218893Sdim return ConstantVector::get(Result); 217198892Srdivacky} 218198892Srdivacky 219198892Srdivacky 220193323Sed/// IsConstantOffsetFromGlobal - If this constant is actually a constant offset 221193323Sed/// from a global, return the global and the constant. Because of 222193323Sed/// constantexprs, this function is recursive. 223193323Sedstatic bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, 224249423Sdim APInt &Offset, const DataLayout &TD) { 225193323Sed // Trivial case, constant is the global. 226193323Sed if ((GV = dyn_cast<GlobalValue>(C))) { 227263508Sdim unsigned BitWidth = TD.getPointerTypeSizeInBits(GV->getType()); 228263508Sdim Offset = APInt(BitWidth, 0); 229193323Sed return true; 230193323Sed } 231243830Sdim 232193323Sed // Otherwise, if this isn't a constant expr, bail out. 233193323Sed ConstantExpr *CE = dyn_cast<ConstantExpr>(C); 234193323Sed if (!CE) return false; 235243830Sdim 236193323Sed // Look through ptr->int and ptr->ptr casts. 237193323Sed if (CE->getOpcode() == Instruction::PtrToInt || 238193323Sed CE->getOpcode() == Instruction::BitCast) 239193323Sed return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD); 240243830Sdim 241243830Sdim // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) 242263508Sdim GEPOperator *GEP = dyn_cast<GEPOperator>(CE); 243263508Sdim if (!GEP) 244263508Sdim return false; 245243830Sdim 246263508Sdim unsigned BitWidth = TD.getPointerTypeSizeInBits(GEP->getType()); 247263508Sdim APInt TmpOffset(BitWidth, 0); 248243830Sdim 249263508Sdim // If the base isn't a global+constant, we aren't either. 250263508Sdim if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, TmpOffset, TD)) 251263508Sdim return false; 252263508Sdim 253263508Sdim // Otherwise, add any offset that our operands provide. 254263508Sdim if (!GEP->accumulateConstantOffset(TD, TmpOffset)) 255263508Sdim return false; 256263508Sdim 257263508Sdim Offset = TmpOffset; 258263508Sdim return true; 259193323Sed} 260193323Sed 261198396Srdivacky/// ReadDataFromGlobal - Recursive helper to read bits out of global. C is the 262198396Srdivacky/// constant being copied out of. ByteOffset is an offset into C. CurPtr is the 263198396Srdivacky/// pointer to copy results into and BytesLeft is the number of bytes left in 264198396Srdivacky/// the CurPtr buffer. TD is the target data. 265198396Srdivackystatic bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, 266198396Srdivacky unsigned char *CurPtr, unsigned BytesLeft, 267243830Sdim const DataLayout &TD) { 268198396Srdivacky assert(ByteOffset <= TD.getTypeAllocSize(C->getType()) && 269198396Srdivacky "Out of range access"); 270243830Sdim 271198892Srdivacky // If this element is zero or undefined, we can just return since *CurPtr is 272198892Srdivacky // zero initialized. 273198396Srdivacky if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) 274198396Srdivacky return true; 275243830Sdim 276198396Srdivacky if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { 277198396Srdivacky if (CI->getBitWidth() > 64 || 278198396Srdivacky (CI->getBitWidth() & 7) != 0) 279198396Srdivacky return false; 280243830Sdim 281198396Srdivacky uint64_t Val = CI->getZExtValue(); 282198396Srdivacky unsigned IntBytes = unsigned(CI->getBitWidth()/8); 283243830Sdim 284198396Srdivacky for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) { 285243830Sdim int n = ByteOffset; 286243830Sdim if (!TD.isLittleEndian()) 287243830Sdim n = IntBytes - n - 1; 288243830Sdim CurPtr[i] = (unsigned char)(Val >> (n * 8)); 289198396Srdivacky ++ByteOffset; 290198396Srdivacky } 291198396Srdivacky return true; 292198396Srdivacky } 293243830Sdim 294198396Srdivacky if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { 295198396Srdivacky if (CFP->getType()->isDoubleTy()) { 296198892Srdivacky C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), TD); 297198396Srdivacky return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD); 298198396Srdivacky } 299198396Srdivacky if (CFP->getType()->isFloatTy()){ 300198892Srdivacky C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), TD); 301198396Srdivacky return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD); 302198396Srdivacky } 303249423Sdim if (CFP->getType()->isHalfTy()){ 304249423Sdim C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), TD); 305249423Sdim return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD); 306249423Sdim } 307198892Srdivacky return false; 308198396Srdivacky } 309243830Sdim 310198396Srdivacky if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { 311198396Srdivacky const StructLayout *SL = TD.getStructLayout(CS->getType()); 312198396Srdivacky unsigned Index = SL->getElementContainingOffset(ByteOffset); 313198396Srdivacky uint64_t CurEltOffset = SL->getElementOffset(Index); 314198396Srdivacky ByteOffset -= CurEltOffset; 315243830Sdim 316198396Srdivacky while (1) { 317198396Srdivacky // If the element access is to the element itself and not to tail padding, 318198396Srdivacky // read the bytes from the element. 319198396Srdivacky uint64_t EltSize = TD.getTypeAllocSize(CS->getOperand(Index)->getType()); 320198396Srdivacky 321198396Srdivacky if (ByteOffset < EltSize && 322198396Srdivacky !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr, 323198396Srdivacky BytesLeft, TD)) 324198396Srdivacky return false; 325243830Sdim 326198396Srdivacky ++Index; 327243830Sdim 328198396Srdivacky // Check to see if we read from the last struct element, if so we're done. 329198396Srdivacky if (Index == CS->getType()->getNumElements()) 330198396Srdivacky return true; 331198396Srdivacky 332198396Srdivacky // If we read all of the bytes we needed from this element we're done. 333198396Srdivacky uint64_t NextEltOffset = SL->getElementOffset(Index); 334198396Srdivacky 335263508Sdim if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset) 336198396Srdivacky return true; 337198396Srdivacky 338198396Srdivacky // Move to the next element of the struct. 339263508Sdim CurPtr += NextEltOffset - CurEltOffset - ByteOffset; 340263508Sdim BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset; 341198396Srdivacky ByteOffset = 0; 342198396Srdivacky CurEltOffset = NextEltOffset; 343198396Srdivacky } 344198396Srdivacky // not reached. 345198396Srdivacky } 346198396Srdivacky 347234353Sdim if (isa<ConstantArray>(C) || isa<ConstantVector>(C) || 348234353Sdim isa<ConstantDataSequential>(C)) { 349263508Sdim Type *EltTy = C->getType()->getSequentialElementType(); 350234353Sdim uint64_t EltSize = TD.getTypeAllocSize(EltTy); 351198396Srdivacky uint64_t Index = ByteOffset / EltSize; 352198396Srdivacky uint64_t Offset = ByteOffset - Index * EltSize; 353234353Sdim uint64_t NumElts; 354234353Sdim if (ArrayType *AT = dyn_cast<ArrayType>(C->getType())) 355234353Sdim NumElts = AT->getNumElements(); 356234353Sdim else 357263508Sdim NumElts = C->getType()->getVectorNumElements(); 358239462Sdim 359234353Sdim for (; Index != NumElts; ++Index) { 360234353Sdim if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr, 361198396Srdivacky BytesLeft, TD)) 362198396Srdivacky return false; 363239462Sdim 364239462Sdim uint64_t BytesWritten = EltSize - Offset; 365239462Sdim assert(BytesWritten <= EltSize && "Not indexing into this element?"); 366239462Sdim if (BytesWritten >= BytesLeft) 367198396Srdivacky return true; 368239462Sdim 369198396Srdivacky Offset = 0; 370239462Sdim BytesLeft -= BytesWritten; 371239462Sdim CurPtr += BytesWritten; 372198396Srdivacky } 373198396Srdivacky return true; 374198396Srdivacky } 375243830Sdim 376218893Sdim if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 377218893Sdim if (CE->getOpcode() == Instruction::IntToPtr && 378263508Sdim CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getType())) { 379243830Sdim return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr, 380234353Sdim BytesLeft, TD); 381263508Sdim } 382218893Sdim } 383218893Sdim 384198396Srdivacky // Otherwise, unknown initializer type. 385198396Srdivacky return false; 386198396Srdivacky} 387198396Srdivacky 388198396Srdivackystatic Constant *FoldReinterpretLoadFromConstPtr(Constant *C, 389243830Sdim const DataLayout &TD) { 390263508Sdim PointerType *PTy = cast<PointerType>(C->getType()); 391263508Sdim Type *LoadTy = PTy->getElementType(); 392226633Sdim IntegerType *IntType = dyn_cast<IntegerType>(LoadTy); 393243830Sdim 394198396Srdivacky // If this isn't an integer load we can't fold it directly. 395198396Srdivacky if (!IntType) { 396263508Sdim unsigned AS = PTy->getAddressSpace(); 397263508Sdim 398198396Srdivacky // If this is a float/double load, we can try folding it as an int32/64 load 399198396Srdivacky // and then bitcast the result. This can be useful for union cases. Note 400198396Srdivacky // that address spaces don't matter here since we're not going to result in 401198396Srdivacky // an actual new load. 402226633Sdim Type *MapTy; 403249423Sdim if (LoadTy->isHalfTy()) 404263508Sdim MapTy = Type::getInt16PtrTy(C->getContext(), AS); 405249423Sdim else if (LoadTy->isFloatTy()) 406263508Sdim MapTy = Type::getInt32PtrTy(C->getContext(), AS); 407198396Srdivacky else if (LoadTy->isDoubleTy()) 408263508Sdim MapTy = Type::getInt64PtrTy(C->getContext(), AS); 409204642Srdivacky else if (LoadTy->isVectorTy()) { 410263508Sdim MapTy = PointerType::getIntNPtrTy(C->getContext(), 411263508Sdim TD.getTypeAllocSizeInBits(LoadTy), 412263508Sdim AS); 413198396Srdivacky } else 414198396Srdivacky return 0; 415198396Srdivacky 416198892Srdivacky C = FoldBitCast(C, MapTy, TD); 417198396Srdivacky if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, TD)) 418198892Srdivacky return FoldBitCast(Res, LoadTy, TD); 419198396Srdivacky return 0; 420198396Srdivacky } 421243830Sdim 422198396Srdivacky unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8; 423263508Sdim if (BytesLoaded > 32 || BytesLoaded == 0) 424263508Sdim return 0; 425243830Sdim 426198396Srdivacky GlobalValue *GVal; 427263508Sdim APInt Offset; 428198396Srdivacky if (!IsConstantOffsetFromGlobal(C, GVal, Offset, TD)) 429198396Srdivacky return 0; 430243830Sdim 431198396Srdivacky GlobalVariable *GV = dyn_cast<GlobalVariable>(GVal); 432198892Srdivacky if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() || 433198396Srdivacky !GV->getInitializer()->getType()->isSized()) 434198396Srdivacky return 0; 435198396Srdivacky 436198396Srdivacky // If we're loading off the beginning of the global, some bytes may be valid, 437198396Srdivacky // but we don't try to handle this. 438263508Sdim if (Offset.isNegative()) 439263508Sdim return 0; 440243830Sdim 441198396Srdivacky // If we're not accessing anything in this constant, the result is undefined. 442249423Sdim if (Offset.getZExtValue() >= 443249423Sdim TD.getTypeAllocSize(GV->getInitializer()->getType())) 444198396Srdivacky return UndefValue::get(IntType); 445243830Sdim 446198396Srdivacky unsigned char RawBytes[32] = {0}; 447249423Sdim if (!ReadDataFromGlobal(GV->getInitializer(), Offset.getZExtValue(), RawBytes, 448198396Srdivacky BytesLoaded, TD)) 449198396Srdivacky return 0; 450198396Srdivacky 451243830Sdim APInt ResultVal = APInt(IntType->getBitWidth(), 0); 452243830Sdim if (TD.isLittleEndian()) { 453243830Sdim ResultVal = RawBytes[BytesLoaded - 1]; 454243830Sdim for (unsigned i = 1; i != BytesLoaded; ++i) { 455243830Sdim ResultVal <<= 8; 456263508Sdim ResultVal |= RawBytes[BytesLoaded - 1 - i]; 457243830Sdim } 458243830Sdim } else { 459243830Sdim ResultVal = RawBytes[0]; 460243830Sdim for (unsigned i = 1; i != BytesLoaded; ++i) { 461243830Sdim ResultVal <<= 8; 462243830Sdim ResultVal |= RawBytes[i]; 463243830Sdim } 464198396Srdivacky } 465198396Srdivacky 466198396Srdivacky return ConstantInt::get(IntType->getContext(), ResultVal); 467198396Srdivacky} 468198396Srdivacky 469198396Srdivacky/// ConstantFoldLoadFromConstPtr - Return the value that a load from C would 470198396Srdivacky/// produce if it is constant and determinable. If this is not determinable, 471198396Srdivacky/// return null. 472198396SrdivackyConstant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, 473243830Sdim const DataLayout *TD) { 474198396Srdivacky // First, try the easy cases: 475198396Srdivacky if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 476198396Srdivacky if (GV->isConstant() && GV->hasDefinitiveInitializer()) 477198396Srdivacky return GV->getInitializer(); 478198396Srdivacky 479198396Srdivacky // If the loaded value isn't a constant expr, we can't handle it. 480198396Srdivacky ConstantExpr *CE = dyn_cast<ConstantExpr>(C); 481263508Sdim if (!CE) 482263508Sdim return 0; 483243830Sdim 484198396Srdivacky if (CE->getOpcode() == Instruction::GetElementPtr) { 485263508Sdim if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) { 486263508Sdim if (GV->isConstant() && GV->hasDefinitiveInitializer()) { 487243830Sdim if (Constant *V = 488198396Srdivacky ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) 489198396Srdivacky return V; 490263508Sdim } 491263508Sdim } 492198396Srdivacky } 493243830Sdim 494198396Srdivacky // Instead of loading constant c string, use corresponding integer value 495198396Srdivacky // directly if string length is small enough. 496234353Sdim StringRef Str; 497234353Sdim if (TD && getConstantStringInfo(CE, Str) && !Str.empty()) { 498234353Sdim unsigned StrLen = Str.size(); 499226633Sdim Type *Ty = cast<PointerType>(CE->getType())->getElementType(); 500198396Srdivacky unsigned NumBits = Ty->getPrimitiveSizeInBits(); 501210299Sed // Replace load with immediate integer if the result is an integer or fp 502210299Sed // value. 503210299Sed if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 && 504210299Sed (isa<IntegerType>(Ty) || Ty->isFloatingPointTy())) { 505198396Srdivacky APInt StrVal(NumBits, 0); 506198396Srdivacky APInt SingleChar(NumBits, 0); 507198396Srdivacky if (TD->isLittleEndian()) { 508198396Srdivacky for (signed i = StrLen-1; i >= 0; i--) { 509198396Srdivacky SingleChar = (uint64_t) Str[i] & UCHAR_MAX; 510198396Srdivacky StrVal = (StrVal << 8) | SingleChar; 511198396Srdivacky } 512198396Srdivacky } else { 513198396Srdivacky for (unsigned i = 0; i < StrLen; i++) { 514198396Srdivacky SingleChar = (uint64_t) Str[i] & UCHAR_MAX; 515198396Srdivacky StrVal = (StrVal << 8) | SingleChar; 516198396Srdivacky } 517198396Srdivacky // Append NULL at the end. 518198396Srdivacky SingleChar = 0; 519198396Srdivacky StrVal = (StrVal << 8) | SingleChar; 520198396Srdivacky } 521243830Sdim 522210299Sed Constant *Res = ConstantInt::get(CE->getContext(), StrVal); 523210299Sed if (Ty->isFloatingPointTy()) 524210299Sed Res = ConstantExpr::getBitCast(Res, Ty); 525210299Sed return Res; 526198396Srdivacky } 527198396Srdivacky } 528243830Sdim 529198396Srdivacky // If this load comes from anywhere in a constant global, and if the global 530198396Srdivacky // is all undef or zero, we know what it loads. 531218893Sdim if (GlobalVariable *GV = 532218893Sdim dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, TD))) { 533198396Srdivacky if (GV->isConstant() && GV->hasDefinitiveInitializer()) { 534226633Sdim Type *ResTy = cast<PointerType>(C->getType())->getElementType(); 535198396Srdivacky if (GV->getInitializer()->isNullValue()) 536198396Srdivacky return Constant::getNullValue(ResTy); 537198396Srdivacky if (isa<UndefValue>(GV->getInitializer())) 538198396Srdivacky return UndefValue::get(ResTy); 539198396Srdivacky } 540198396Srdivacky } 541243830Sdim 542243830Sdim // Try hard to fold loads from bitcasted strange and non-type-safe things. 543243830Sdim if (TD) 544198396Srdivacky return FoldReinterpretLoadFromConstPtr(CE, *TD); 545198396Srdivacky return 0; 546198396Srdivacky} 547198396Srdivacky 548243830Sdimstatic Constant *ConstantFoldLoadInst(const LoadInst *LI, const DataLayout *TD){ 549198396Srdivacky if (LI->isVolatile()) return 0; 550243830Sdim 551198396Srdivacky if (Constant *C = dyn_cast<Constant>(LI->getOperand(0))) 552198396Srdivacky return ConstantFoldLoadFromConstPtr(C, TD); 553198396Srdivacky 554198396Srdivacky return 0; 555198396Srdivacky} 556198396Srdivacky 557193323Sed/// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression. 558193323Sed/// Attempt to symbolically evaluate the result of a binary operator merging 559249423Sdim/// these together. If target data info is available, it is provided as DL, 560249423Sdim/// otherwise DL is null. 561193323Sedstatic Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, 562249423Sdim Constant *Op1, const DataLayout *DL){ 563193323Sed // SROA 564243830Sdim 565193323Sed // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl. 566193323Sed // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute 567193323Sed // bits. 568243830Sdim 569243830Sdim 570249423Sdim if (Opc == Instruction::And && DL) { 571251662Sdim unsigned BitWidth = DL->getTypeSizeInBits(Op0->getType()->getScalarType()); 572249423Sdim APInt KnownZero0(BitWidth, 0), KnownOne0(BitWidth, 0); 573249423Sdim APInt KnownZero1(BitWidth, 0), KnownOne1(BitWidth, 0); 574249423Sdim ComputeMaskedBits(Op0, KnownZero0, KnownOne0, DL); 575249423Sdim ComputeMaskedBits(Op1, KnownZero1, KnownOne1, DL); 576249423Sdim if ((KnownOne1 | KnownZero0).isAllOnesValue()) { 577249423Sdim // All the bits of Op0 that the 'and' could be masking are already zero. 578249423Sdim return Op0; 579249423Sdim } 580249423Sdim if ((KnownOne0 | KnownZero1).isAllOnesValue()) { 581249423Sdim // All the bits of Op1 that the 'and' could be masking are already zero. 582249423Sdim return Op1; 583249423Sdim } 584249423Sdim 585249423Sdim APInt KnownZero = KnownZero0 | KnownZero1; 586249423Sdim APInt KnownOne = KnownOne0 & KnownOne1; 587249423Sdim if ((KnownZero | KnownOne).isAllOnesValue()) { 588249423Sdim return ConstantInt::get(Op0->getType(), KnownOne); 589249423Sdim } 590249423Sdim } 591249423Sdim 592193323Sed // If the constant expr is something like &A[123] - &A[4].f, fold this into a 593193323Sed // constant. This happens frequently when iterating over a global array. 594249423Sdim if (Opc == Instruction::Sub && DL) { 595193323Sed GlobalValue *GV1, *GV2; 596263508Sdim APInt Offs1, Offs2; 597243830Sdim 598249423Sdim if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *DL)) 599249423Sdim if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *DL) && 600193323Sed GV1 == GV2) { 601263508Sdim unsigned OpSize = DL->getTypeSizeInBits(Op0->getType()); 602263508Sdim 603193323Sed // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow. 604249423Sdim // PtrToInt may change the bitwidth so we have convert to the right size 605249423Sdim // first. 606249423Sdim return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) - 607249423Sdim Offs2.zextOrTrunc(OpSize)); 608193323Sed } 609193323Sed } 610243830Sdim 611193323Sed return 0; 612193323Sed} 613193323Sed 614203954Srdivacky/// CastGEPIndices - If array indices are not pointer-sized integers, 615203954Srdivacky/// explicitly cast them so that they aren't implicitly casted by the 616203954Srdivacky/// getelementptr. 617226633Sdimstatic Constant *CastGEPIndices(ArrayRef<Constant *> Ops, 618243830Sdim Type *ResultTy, const DataLayout *TD, 619234353Sdim const TargetLibraryInfo *TLI) { 620263508Sdim if (!TD) 621263508Sdim return 0; 622203954Srdivacky 623263508Sdim Type *IntPtrTy = TD->getIntPtrType(ResultTy); 624263508Sdim 625203954Srdivacky bool Any = false; 626203954Srdivacky SmallVector<Constant*, 32> NewIdxs; 627226633Sdim for (unsigned i = 1, e = Ops.size(); i != e; ++i) { 628203954Srdivacky if ((i == 1 || 629263508Sdim !isa<StructType>(GetElementPtrInst::getIndexedType( 630263508Sdim Ops[0]->getType(), 631263508Sdim Ops.slice(1, i - 1)))) && 632203954Srdivacky Ops[i]->getType() != IntPtrTy) { 633203954Srdivacky Any = true; 634203954Srdivacky NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i], 635203954Srdivacky true, 636203954Srdivacky IntPtrTy, 637203954Srdivacky true), 638203954Srdivacky Ops[i], IntPtrTy)); 639203954Srdivacky } else 640203954Srdivacky NewIdxs.push_back(Ops[i]); 641203954Srdivacky } 642203954Srdivacky 643263508Sdim if (!Any) 644263508Sdim return 0; 645263508Sdim 646263508Sdim Constant *C = ConstantExpr::getGetElementPtr(Ops[0], NewIdxs); 647263508Sdim if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 648234353Sdim if (Constant *Folded = ConstantFoldConstantExpression(CE, TD, TLI)) 649203954Srdivacky C = Folded; 650263508Sdim } 651263508Sdim 652203954Srdivacky return C; 653203954Srdivacky} 654203954Srdivacky 655239462Sdim/// Strip the pointer casts, but preserve the address space information. 656239462Sdimstatic Constant* StripPtrCastKeepAS(Constant* Ptr) { 657239462Sdim assert(Ptr->getType()->isPointerTy() && "Not a pointer type"); 658239462Sdim PointerType *OldPtrTy = cast<PointerType>(Ptr->getType()); 659239462Sdim Ptr = cast<Constant>(Ptr->stripPointerCasts()); 660239462Sdim PointerType *NewPtrTy = cast<PointerType>(Ptr->getType()); 661239462Sdim 662239462Sdim // Preserve the address space number of the pointer. 663239462Sdim if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) { 664239462Sdim NewPtrTy = NewPtrTy->getElementType()->getPointerTo( 665239462Sdim OldPtrTy->getAddressSpace()); 666263508Sdim Ptr = ConstantExpr::getPointerCast(Ptr, NewPtrTy); 667239462Sdim } 668239462Sdim return Ptr; 669239462Sdim} 670239462Sdim 671193323Sed/// SymbolicallyEvaluateGEP - If we can symbolically evaluate the specified GEP 672193323Sed/// constant expression, do so. 673226633Sdimstatic Constant *SymbolicallyEvaluateGEP(ArrayRef<Constant *> Ops, 674243830Sdim Type *ResultTy, const DataLayout *TD, 675234353Sdim const TargetLibraryInfo *TLI) { 676193323Sed Constant *Ptr = Ops[0]; 677263508Sdim if (!TD || !Ptr->getType()->getPointerElementType()->isSized() || 678234353Sdim !Ptr->getType()->isPointerTy()) 679193323Sed return 0; 680243830Sdim 681263508Sdim Type *IntPtrTy = TD->getIntPtrType(Ptr->getType()); 682263508Sdim Type *ResultElementTy = ResultTy->getPointerElementType(); 683198090Srdivacky 684193323Sed // If this is a constant expr gep that is effectively computing an 685193323Sed // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12' 686226633Sdim for (unsigned i = 1, e = Ops.size(); i != e; ++i) 687218893Sdim if (!isa<ConstantInt>(Ops[i])) { 688243830Sdim 689218893Sdim // If this is "gep i8* Ptr, (sub 0, V)", fold this as: 690218893Sdim // "inttoptr (sub (ptrtoint Ptr), V)" 691263508Sdim if (Ops.size() == 2 && ResultElementTy->isIntegerTy(8)) { 692218893Sdim ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[1]); 693218893Sdim assert((CE == 0 || CE->getType() == IntPtrTy) && 694218893Sdim "CastGEPIndices didn't canonicalize index types!"); 695218893Sdim if (CE && CE->getOpcode() == Instruction::Sub && 696218893Sdim CE->getOperand(0)->isNullValue()) { 697218893Sdim Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType()); 698218893Sdim Res = ConstantExpr::getSub(Res, CE->getOperand(1)); 699218893Sdim Res = ConstantExpr::getIntToPtr(Res, ResultTy); 700218893Sdim if (ConstantExpr *ResCE = dyn_cast<ConstantExpr>(Res)) 701234353Sdim Res = ConstantFoldConstantExpression(ResCE, TD, TLI); 702218893Sdim return Res; 703218893Sdim } 704218893Sdim } 705198090Srdivacky return 0; 706218893Sdim } 707239462Sdim 708218893Sdim unsigned BitWidth = TD->getTypeSizeInBits(IntPtrTy); 709226633Sdim APInt Offset = 710226633Sdim APInt(BitWidth, TD->getIndexedOffset(Ptr->getType(), 711243830Sdim makeArrayRef((Value *const*) 712243830Sdim Ops.data() + 1, 713226633Sdim Ops.size() - 1))); 714239462Sdim Ptr = StripPtrCastKeepAS(Ptr); 715205218Srdivacky 716205218Srdivacky // If this is a GEP of a GEP, fold it all into a single GEP. 717205218Srdivacky while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) { 718263508Sdim SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end()); 719205218Srdivacky 720205218Srdivacky // Do not try the incorporate the sub-GEP if some index is not a number. 721205218Srdivacky bool AllConstantInt = true; 722205218Srdivacky for (unsigned i = 0, e = NestedOps.size(); i != e; ++i) 723205218Srdivacky if (!isa<ConstantInt>(NestedOps[i])) { 724205218Srdivacky AllConstantInt = false; 725205218Srdivacky break; 726205218Srdivacky } 727205218Srdivacky if (!AllConstantInt) 728205218Srdivacky break; 729205218Srdivacky 730205218Srdivacky Ptr = cast<Constant>(GEP->getOperand(0)); 731205218Srdivacky Offset += APInt(BitWidth, 732226633Sdim TD->getIndexedOffset(Ptr->getType(), NestedOps)); 733239462Sdim Ptr = StripPtrCastKeepAS(Ptr); 734205218Srdivacky } 735205218Srdivacky 736198090Srdivacky // If the base value for this address is a literal integer value, fold the 737198090Srdivacky // getelementptr to the resulting integer value casted to the pointer type. 738205407Srdivacky APInt BasePtr(BitWidth, 0); 739263508Sdim if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { 740263508Sdim if (CE->getOpcode() == Instruction::IntToPtr) { 741218893Sdim if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0))) 742218893Sdim BasePtr = Base->getValue().zextOrTrunc(BitWidth); 743263508Sdim } 744263508Sdim } 745263508Sdim 746205407Srdivacky if (Ptr->isNullValue() || BasePtr != 0) { 747263508Sdim Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr); 748198090Srdivacky return ConstantExpr::getIntToPtr(C, ResultTy); 749198090Srdivacky } 750198090Srdivacky 751198090Srdivacky // Otherwise form a regular getelementptr. Recompute the indices so that 752198090Srdivacky // we eliminate over-indexing of the notional static type array bounds. 753198090Srdivacky // This makes it easy to determine if the getelementptr is "inbounds". 754198090Srdivacky // Also, this helps GlobalOpt do SROA on GlobalVariables. 755226633Sdim Type *Ty = Ptr->getType(); 756234982Sdim assert(Ty->isPointerTy() && "Forming regular GEP of non-pointer type"); 757263508Sdim SmallVector<Constant *, 32> NewIdxs; 758263508Sdim 759198090Srdivacky do { 760226633Sdim if (SequentialType *ATy = dyn_cast<SequentialType>(Ty)) { 761204642Srdivacky if (ATy->isPointerTy()) { 762200581Srdivacky // The only pointer indexing we'll do is on the first index of the GEP. 763200581Srdivacky if (!NewIdxs.empty()) 764200581Srdivacky break; 765243830Sdim 766200581Srdivacky // Only handle pointers to sized types, not pointers to functions. 767200581Srdivacky if (!ATy->getElementType()->isSized()) 768200581Srdivacky return 0; 769200581Srdivacky } 770243830Sdim 771198090Srdivacky // Determine which element of the array the offset points into. 772198090Srdivacky APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType())); 773198090Srdivacky if (ElemSize == 0) 774218893Sdim // The element size is 0. This may be [0 x Ty]*, so just use a zero 775218893Sdim // index for this level and proceed to the next level to see if it can 776218893Sdim // accommodate the offset. 777218893Sdim NewIdxs.push_back(ConstantInt::get(IntPtrTy, 0)); 778218893Sdim else { 779218893Sdim // The element size is non-zero divide the offset by the element 780218893Sdim // size (rounding down), to compute the index at this level. 781218893Sdim APInt NewIdx = Offset.udiv(ElemSize); 782218893Sdim Offset -= NewIdx * ElemSize; 783218893Sdim NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx)); 784218893Sdim } 785198090Srdivacky Ty = ATy->getElementType(); 786226633Sdim } else if (StructType *STy = dyn_cast<StructType>(Ty)) { 787234982Sdim // If we end up with an offset that isn't valid for this struct type, we 788234982Sdim // can't re-form this GEP in a regular form, so bail out. The pointer 789234982Sdim // operand likely went through casts that are necessary to make the GEP 790234982Sdim // sensible. 791234982Sdim const StructLayout &SL = *TD->getStructLayout(STy); 792234982Sdim if (Offset.uge(SL.getSizeInBytes())) 793234982Sdim break; 794234982Sdim 795198090Srdivacky // Determine which field of the struct the offset points into. The 796234982Sdim // getZExtValue is fine as we've already ensured that the offset is 797234982Sdim // within the range representable by the StructLayout API. 798198090Srdivacky unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue()); 799199481Srdivacky NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 800199481Srdivacky ElIdx)); 801198090Srdivacky Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx)); 802198090Srdivacky Ty = STy->getTypeAtIndex(ElIdx); 803198090Srdivacky } else { 804198090Srdivacky // We've reached some non-indexable type. 805198090Srdivacky break; 806198090Srdivacky } 807263508Sdim } while (Ty != ResultElementTy); 808198090Srdivacky 809198090Srdivacky // If we haven't used up the entire offset by descending the static 810198090Srdivacky // type, then the offset is pointing into the middle of an indivisible 811198090Srdivacky // member, so we can't simplify it. 812198090Srdivacky if (Offset != 0) 813198090Srdivacky return 0; 814198090Srdivacky 815198090Srdivacky // Create a GEP. 816263508Sdim Constant *C = ConstantExpr::getGetElementPtr(Ptr, NewIdxs); 817263508Sdim assert(C->getType()->getPointerElementType() == Ty && 818198090Srdivacky "Computed GetElementPtr has unexpected type!"); 819198090Srdivacky 820198090Srdivacky // If we ended up indexing a member with a type that doesn't match 821198090Srdivacky // the type of what the original indices indexed, add a cast. 822263508Sdim if (Ty != ResultElementTy) 823198892Srdivacky C = FoldBitCast(C, ResultTy, *TD); 824198090Srdivacky 825198090Srdivacky return C; 826193323Sed} 827193323Sed 828193323Sed 829193323Sed 830193323Sed//===----------------------------------------------------------------------===// 831193323Sed// Constant Folding public APIs 832193323Sed//===----------------------------------------------------------------------===// 833193323Sed 834218893Sdim/// ConstantFoldInstruction - Try to constant fold the specified instruction. 835218893Sdim/// If successful, the constant result is returned, if not, null is returned. 836218893Sdim/// Note that this fails if not all of the operands are constant. Otherwise, 837218893Sdim/// this function can only fail when attempting to fold instructions like loads 838218893Sdim/// and stores, which have no constant expression form. 839234353SdimConstant *llvm::ConstantFoldInstruction(Instruction *I, 840243830Sdim const DataLayout *TD, 841234353Sdim const TargetLibraryInfo *TLI) { 842218893Sdim // Handle PHI nodes quickly here... 843193323Sed if (PHINode *PN = dyn_cast<PHINode>(I)) { 844218893Sdim Constant *CommonValue = 0; 845193323Sed 846218893Sdim for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 847218893Sdim Value *Incoming = PN->getIncomingValue(i); 848218893Sdim // If the incoming value is undef then skip it. Note that while we could 849218893Sdim // skip the value if it is equal to the phi node itself we choose not to 850218893Sdim // because that would break the rule that constant folding only applies if 851218893Sdim // all operands are constants. 852218893Sdim if (isa<UndefValue>(Incoming)) 853218893Sdim continue; 854239462Sdim // If the incoming value is not a constant, then give up. 855218893Sdim Constant *C = dyn_cast<Constant>(Incoming); 856239462Sdim if (!C) 857218893Sdim return 0; 858239462Sdim // Fold the PHI's operands. 859239462Sdim if (ConstantExpr *NewC = dyn_cast<ConstantExpr>(C)) 860239462Sdim C = ConstantFoldConstantExpression(NewC, TD, TLI); 861239462Sdim // If the incoming value is a different constant to 862239462Sdim // the one we saw previously, then give up. 863239462Sdim if (CommonValue && C != CommonValue) 864239462Sdim return 0; 865218893Sdim CommonValue = C; 866218893Sdim } 867193323Sed 868239462Sdim 869218893Sdim // If we reach here, all incoming values are the same constant or undef. 870218893Sdim return CommonValue ? CommonValue : UndefValue::get(PN->getType()); 871193323Sed } 872193323Sed 873193323Sed // Scan the operand list, checking to see if they are all constants, if so, 874193323Sed // hand off to ConstantFoldInstOperands. 875193323Sed SmallVector<Constant*, 8> Ops; 876239462Sdim for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) { 877239462Sdim Constant *Op = dyn_cast<Constant>(*i); 878239462Sdim if (!Op) 879193323Sed return 0; // All operands not constant! 880193323Sed 881239462Sdim // Fold the Instruction's operands. 882239462Sdim if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(Op)) 883239462Sdim Op = ConstantFoldConstantExpression(NewCE, TD, TLI); 884239462Sdim 885239462Sdim Ops.push_back(Op); 886239462Sdim } 887239462Sdim 888193323Sed if (const CmpInst *CI = dyn_cast<CmpInst>(I)) 889199481Srdivacky return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1], 890234353Sdim TD, TLI); 891243830Sdim 892198396Srdivacky if (const LoadInst *LI = dyn_cast<LoadInst>(I)) 893198396Srdivacky return ConstantFoldLoadInst(LI, TD); 894218893Sdim 895263508Sdim if (InsertValueInst *IVI = dyn_cast<InsertValueInst>(I)) { 896218893Sdim return ConstantExpr::getInsertValue( 897218893Sdim cast<Constant>(IVI->getAggregateOperand()), 898218893Sdim cast<Constant>(IVI->getInsertedValueOperand()), 899224145Sdim IVI->getIndices()); 900263508Sdim } 901218893Sdim 902263508Sdim if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I)) { 903218893Sdim return ConstantExpr::getExtractValue( 904218893Sdim cast<Constant>(EVI->getAggregateOperand()), 905224145Sdim EVI->getIndices()); 906263508Sdim } 907218893Sdim 908234353Sdim return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Ops, TD, TLI); 909193323Sed} 910193323Sed 911251662Sdimstatic Constant * 912251662SdimConstantFoldConstantExpressionImpl(const ConstantExpr *CE, const DataLayout *TD, 913251662Sdim const TargetLibraryInfo *TLI, 914251662Sdim SmallPtrSet<ConstantExpr *, 4> &FoldedOps) { 915251662Sdim SmallVector<Constant *, 8> Ops; 916251662Sdim for (User::const_op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; 917251662Sdim ++i) { 918199989Srdivacky Constant *NewC = cast<Constant>(*i); 919251662Sdim // Recursively fold the ConstantExpr's operands. If we have already folded 920251662Sdim // a ConstantExpr, we don't have to process it again. 921251662Sdim if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(NewC)) { 922251662Sdim if (FoldedOps.insert(NewCE)) 923251662Sdim NewC = ConstantFoldConstantExpressionImpl(NewCE, TD, TLI, FoldedOps); 924251662Sdim } 925199989Srdivacky Ops.push_back(NewC); 926199989Srdivacky } 927193323Sed 928193323Sed if (CE->isCompare()) 929199481Srdivacky return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1], 930234353Sdim TD, TLI); 931234353Sdim return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(), Ops, TD, TLI); 932193323Sed} 933193323Sed 934251662Sdim/// ConstantFoldConstantExpression - Attempt to fold the constant expression 935251662Sdim/// using the specified DataLayout. If successful, the constant result is 936251662Sdim/// result is returned, if not, null is returned. 937251662SdimConstant *llvm::ConstantFoldConstantExpression(const ConstantExpr *CE, 938251662Sdim const DataLayout *TD, 939251662Sdim const TargetLibraryInfo *TLI) { 940251662Sdim SmallPtrSet<ConstantExpr *, 4> FoldedOps; 941251662Sdim return ConstantFoldConstantExpressionImpl(CE, TD, TLI, FoldedOps); 942251662Sdim} 943251662Sdim 944193323Sed/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the 945193323Sed/// specified opcode and operands. If successful, the constant result is 946193323Sed/// returned, if not, null is returned. Note that this function can fail when 947193323Sed/// attempting to fold instructions like loads and stores, which have no 948193323Sed/// constant expression form. 949193323Sed/// 950199989Srdivacky/// TODO: This function neither utilizes nor preserves nsw/nuw/inbounds/etc 951199989Srdivacky/// information, due to only being passed an opcode and operands. Constant 952199989Srdivacky/// folding using this function strips this information. 953199989Srdivacky/// 954243830SdimConstant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy, 955226633Sdim ArrayRef<Constant *> Ops, 956243830Sdim const DataLayout *TD, 957243830Sdim const TargetLibraryInfo *TLI) { 958193323Sed // Handle easy binops first. 959193323Sed if (Instruction::isBinaryOp(Opcode)) { 960263508Sdim if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1])) { 961199481Srdivacky if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD)) 962193323Sed return C; 963263508Sdim } 964243830Sdim 965193323Sed return ConstantExpr::get(Opcode, Ops[0], Ops[1]); 966193323Sed } 967243830Sdim 968193323Sed switch (Opcode) { 969193323Sed default: return 0; 970202375Srdivacky case Instruction::ICmp: 971234353Sdim case Instruction::FCmp: llvm_unreachable("Invalid for compares"); 972193323Sed case Instruction::Call: 973226633Sdim if (Function *F = dyn_cast<Function>(Ops.back())) 974193323Sed if (canConstantFoldCallTo(F)) 975234353Sdim return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI); 976193323Sed return 0; 977193323Sed case Instruction::PtrToInt: 978193323Sed // If the input is a inttoptr, eliminate the pair. This requires knowing 979193323Sed // the width of a pointer, so it can't be done in ConstantExpr::getCast. 980193323Sed if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) { 981193323Sed if (TD && CE->getOpcode() == Instruction::IntToPtr) { 982193323Sed Constant *Input = CE->getOperand(0); 983194612Sed unsigned InWidth = Input->getType()->getScalarSizeInBits(); 984263508Sdim unsigned PtrWidth = TD->getPointerTypeSizeInBits(CE->getType()); 985263508Sdim if (PtrWidth < InWidth) { 986243830Sdim Constant *Mask = 987263508Sdim ConstantInt::get(CE->getContext(), 988263508Sdim APInt::getLowBitsSet(InWidth, PtrWidth)); 989193323Sed Input = ConstantExpr::getAnd(Input, Mask); 990193323Sed } 991193323Sed // Do a zext or trunc to get to the dest size. 992193323Sed return ConstantExpr::getIntegerCast(Input, DestTy, false); 993193323Sed } 994193323Sed } 995193323Sed return ConstantExpr::getCast(Opcode, Ops[0], DestTy); 996193323Sed case Instruction::IntToPtr: 997193323Sed // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if 998263508Sdim // the int size is >= the ptr size and the address spaces are the same. 999263508Sdim // This requires knowing the width of a pointer, so it can't be done in 1000263508Sdim // ConstantExpr::getCast. 1001263508Sdim if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) { 1002263508Sdim if (TD && CE->getOpcode() == Instruction::PtrToInt) { 1003263508Sdim Constant *SrcPtr = CE->getOperand(0); 1004263508Sdim unsigned SrcPtrSize = TD->getPointerTypeSizeInBits(SrcPtr->getType()); 1005263508Sdim unsigned MidIntSize = CE->getType()->getScalarSizeInBits(); 1006204642Srdivacky 1007263508Sdim if (MidIntSize >= SrcPtrSize) { 1008263508Sdim unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace(); 1009263508Sdim if (SrcAS == DestTy->getPointerAddressSpace()) 1010263508Sdim return FoldBitCast(CE->getOperand(0), DestTy, *TD); 1011263508Sdim } 1012263508Sdim } 1013263508Sdim } 1014263508Sdim 1015193323Sed return ConstantExpr::getCast(Opcode, Ops[0], DestTy); 1016193323Sed case Instruction::Trunc: 1017193323Sed case Instruction::ZExt: 1018193323Sed case Instruction::SExt: 1019193323Sed case Instruction::FPTrunc: 1020193323Sed case Instruction::FPExt: 1021193323Sed case Instruction::UIToFP: 1022193323Sed case Instruction::SIToFP: 1023193323Sed case Instruction::FPToUI: 1024193323Sed case Instruction::FPToSI: 1025263508Sdim case Instruction::AddrSpaceCast: 1026193323Sed return ConstantExpr::getCast(Opcode, Ops[0], DestTy); 1027193323Sed case Instruction::BitCast: 1028193323Sed if (TD) 1029198892Srdivacky return FoldBitCast(Ops[0], DestTy, *TD); 1030193323Sed return ConstantExpr::getBitCast(Ops[0], DestTy); 1031193323Sed case Instruction::Select: 1032193323Sed return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); 1033193323Sed case Instruction::ExtractElement: 1034193323Sed return ConstantExpr::getExtractElement(Ops[0], Ops[1]); 1035193323Sed case Instruction::InsertElement: 1036193323Sed return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); 1037193323Sed case Instruction::ShuffleVector: 1038193323Sed return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); 1039193323Sed case Instruction::GetElementPtr: 1040234353Sdim if (Constant *C = CastGEPIndices(Ops, DestTy, TD, TLI)) 1041203954Srdivacky return C; 1042234353Sdim if (Constant *C = SymbolicallyEvaluateGEP(Ops, DestTy, TD, TLI)) 1043193323Sed return C; 1044243830Sdim 1045226633Sdim return ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1)); 1046193323Sed } 1047193323Sed} 1048193323Sed 1049193323Sed/// ConstantFoldCompareInstOperands - Attempt to constant fold a compare 1050193323Sed/// instruction (icmp/fcmp) with the specified operands. If it fails, it 1051193323Sed/// returns a constant expression of the specified operands. 1052193323Sed/// 1053193323SedConstant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate, 1054243830Sdim Constant *Ops0, Constant *Ops1, 1055243830Sdim const DataLayout *TD, 1056234353Sdim const TargetLibraryInfo *TLI) { 1057193323Sed // fold: icmp (inttoptr x), null -> icmp x, 0 1058193323Sed // fold: icmp (ptrtoint x), 0 -> icmp x, null 1059193323Sed // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y 1060193323Sed // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y 1061193323Sed // 1062193323Sed // ConstantExpr::getCompare cannot do this, because it doesn't have TD 1063193323Sed // around to know if bit truncation is happening. 1064199481Srdivacky if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops0)) { 1065199481Srdivacky if (TD && Ops1->isNullValue()) { 1066193323Sed if (CE0->getOpcode() == Instruction::IntToPtr) { 1067263508Sdim Type *IntPtrTy = TD->getIntPtrType(CE0->getType()); 1068193323Sed // Convert the integer value to the right size to ensure we get the 1069193323Sed // proper extension or truncation. 1070193323Sed Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0), 1071193323Sed IntPtrTy, false); 1072199481Srdivacky Constant *Null = Constant::getNullValue(C->getType()); 1073234353Sdim return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI); 1074193323Sed } 1075243830Sdim 1076193323Sed // Only do this transformation if the int is intptrty in size, otherwise 1077193323Sed // there is a truncation or extension that we aren't modeling. 1078263508Sdim if (CE0->getOpcode() == Instruction::PtrToInt) { 1079263508Sdim Type *IntPtrTy = TD->getIntPtrType(CE0->getOperand(0)->getType()); 1080263508Sdim if (CE0->getType() == IntPtrTy) { 1081263508Sdim Constant *C = CE0->getOperand(0); 1082263508Sdim Constant *Null = Constant::getNullValue(C->getType()); 1083263508Sdim return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI); 1084263508Sdim } 1085193323Sed } 1086193323Sed } 1087243830Sdim 1088199481Srdivacky if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops1)) { 1089193323Sed if (TD && CE0->getOpcode() == CE1->getOpcode()) { 1090263508Sdim if (CE0->getOpcode() == Instruction::IntToPtr) { 1091263508Sdim Type *IntPtrTy = TD->getIntPtrType(CE0->getType()); 1092193323Sed 1093193323Sed // Convert the integer value to the right size to ensure we get the 1094193323Sed // proper extension or truncation. 1095193323Sed Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0), 1096193323Sed IntPtrTy, false); 1097193323Sed Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0), 1098193323Sed IntPtrTy, false); 1099234353Sdim return ConstantFoldCompareInstOperands(Predicate, C0, C1, TD, TLI); 1100193323Sed } 1101193323Sed 1102193323Sed // Only do this transformation if the int is intptrty in size, otherwise 1103193323Sed // there is a truncation or extension that we aren't modeling. 1104263508Sdim if (CE0->getOpcode() == Instruction::PtrToInt) { 1105263508Sdim Type *IntPtrTy = TD->getIntPtrType(CE0->getOperand(0)->getType()); 1106263508Sdim if (CE0->getType() == IntPtrTy && 1107263508Sdim CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) { 1108263508Sdim return ConstantFoldCompareInstOperands(Predicate, 1109263508Sdim CE0->getOperand(0), 1110263508Sdim CE1->getOperand(0), 1111263508Sdim TD, 1112263508Sdim TLI); 1113263508Sdim } 1114263508Sdim } 1115193323Sed } 1116193323Sed } 1117243830Sdim 1118202375Srdivacky // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0) 1119202375Srdivacky // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0) 1120202375Srdivacky if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) && 1121202375Srdivacky CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) { 1122243830Sdim Constant *LHS = 1123234353Sdim ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0), Ops1, 1124234353Sdim TD, TLI); 1125243830Sdim Constant *RHS = 1126234353Sdim ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(1), Ops1, 1127234353Sdim TD, TLI); 1128243830Sdim unsigned OpC = 1129202375Srdivacky Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or; 1130202375Srdivacky Constant *Ops[] = { LHS, RHS }; 1131234353Sdim return ConstantFoldInstOperands(OpC, LHS->getType(), Ops, TD, TLI); 1132202375Srdivacky } 1133193323Sed } 1134243830Sdim 1135199481Srdivacky return ConstantExpr::getCompare(Predicate, Ops0, Ops1); 1136193323Sed} 1137193323Sed 1138193323Sed 1139193323Sed/// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a 1140193323Sed/// getelementptr constantexpr, return the constant value being addressed by the 1141193323Sed/// constant expression, or null if something is funny and we can't decide. 1142243830SdimConstant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C, 1143193323Sed ConstantExpr *CE) { 1144234353Sdim if (!CE->getOperand(1)->isNullValue()) 1145193323Sed return 0; // Do not allow stepping over the value! 1146234353Sdim 1147193323Sed // Loop over all of the operands, tracking down which value we are 1148234353Sdim // addressing. 1149234353Sdim for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) { 1150234353Sdim C = C->getAggregateElement(CE->getOperand(i)); 1151263508Sdim if (C == 0) 1152263508Sdim return 0; 1153234353Sdim } 1154193323Sed return C; 1155193323Sed} 1156193323Sed 1157234353Sdim/// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr 1158234353Sdim/// indices (with an *implied* zero pointer index that is not in the list), 1159234353Sdim/// return the constant value being addressed by a virtual load, or null if 1160234353Sdim/// something is funny and we can't decide. 1161234353SdimConstant *llvm::ConstantFoldLoadThroughGEPIndices(Constant *C, 1162234353Sdim ArrayRef<Constant*> Indices) { 1163234353Sdim // Loop over all of the operands, tracking down which value we are 1164234353Sdim // addressing. 1165234353Sdim for (unsigned i = 0, e = Indices.size(); i != e; ++i) { 1166234353Sdim C = C->getAggregateElement(Indices[i]); 1167263508Sdim if (C == 0) 1168263508Sdim return 0; 1169234353Sdim } 1170234353Sdim return C; 1171234353Sdim} 1172193323Sed 1173234353Sdim 1174193323Sed//===----------------------------------------------------------------------===// 1175193323Sed// Constant Folding for Calls 1176193323Sed// 1177193323Sed 1178193323Sed/// canConstantFoldCallTo - Return true if its even possible to fold a call to 1179193323Sed/// the specified function. 1180263508Sdimbool llvm::canConstantFoldCallTo(const Function *F) { 1181193323Sed switch (F->getIntrinsicID()) { 1182249423Sdim case Intrinsic::fabs: 1183249423Sdim case Intrinsic::log: 1184249423Sdim case Intrinsic::log2: 1185249423Sdim case Intrinsic::log10: 1186249423Sdim case Intrinsic::exp: 1187249423Sdim case Intrinsic::exp2: 1188249423Sdim case Intrinsic::floor: 1189193323Sed case Intrinsic::sqrt: 1190234353Sdim case Intrinsic::pow: 1191193323Sed case Intrinsic::powi: 1192193323Sed case Intrinsic::bswap: 1193193323Sed case Intrinsic::ctpop: 1194193323Sed case Intrinsic::ctlz: 1195193323Sed case Intrinsic::cttz: 1196221345Sdim case Intrinsic::sadd_with_overflow: 1197198090Srdivacky case Intrinsic::uadd_with_overflow: 1198221345Sdim case Intrinsic::ssub_with_overflow: 1199198090Srdivacky case Intrinsic::usub_with_overflow: 1200218893Sdim case Intrinsic::smul_with_overflow: 1201221345Sdim case Intrinsic::umul_with_overflow: 1202205407Srdivacky case Intrinsic::convert_from_fp16: 1203205407Srdivacky case Intrinsic::convert_to_fp16: 1204218893Sdim case Intrinsic::x86_sse_cvtss2si: 1205218893Sdim case Intrinsic::x86_sse_cvtss2si64: 1206218893Sdim case Intrinsic::x86_sse_cvttss2si: 1207218893Sdim case Intrinsic::x86_sse_cvttss2si64: 1208218893Sdim case Intrinsic::x86_sse2_cvtsd2si: 1209218893Sdim case Intrinsic::x86_sse2_cvtsd2si64: 1210218893Sdim case Intrinsic::x86_sse2_cvttsd2si: 1211218893Sdim case Intrinsic::x86_sse2_cvttsd2si64: 1212193323Sed return true; 1213198090Srdivacky default: 1214198090Srdivacky return false; 1215198090Srdivacky case 0: break; 1216193323Sed } 1217193323Sed 1218263508Sdim if (!F->hasName()) 1219263508Sdim return false; 1220198090Srdivacky StringRef Name = F->getName(); 1221243830Sdim 1222193323Sed // In these cases, the check of the length is required. We don't want to 1223193323Sed // return true for a name like "cos\0blah" which strcmp would return equal to 1224193323Sed // "cos", but has length 8. 1225198090Srdivacky switch (Name[0]) { 1226193323Sed default: return false; 1227193323Sed case 'a': 1228249423Sdim return Name == "acos" || Name == "asin" || Name == "atan" || Name =="atan2"; 1229193323Sed case 'c': 1230198090Srdivacky return Name == "cos" || Name == "ceil" || Name == "cosf" || Name == "cosh"; 1231193323Sed case 'e': 1232223017Sdim return Name == "exp" || Name == "exp2"; 1233193323Sed case 'f': 1234198090Srdivacky return Name == "fabs" || Name == "fmod" || Name == "floor"; 1235193323Sed case 'l': 1236198090Srdivacky return Name == "log" || Name == "log10"; 1237193323Sed case 'p': 1238198090Srdivacky return Name == "pow"; 1239193323Sed case 's': 1240198090Srdivacky return Name == "sin" || Name == "sinh" || Name == "sqrt" || 1241198090Srdivacky Name == "sinf" || Name == "sqrtf"; 1242193323Sed case 't': 1243198090Srdivacky return Name == "tan" || Name == "tanh"; 1244193323Sed } 1245193323Sed} 1246193323Sed 1247243830Sdimstatic Constant *ConstantFoldFP(double (*NativeFP)(double), double V, 1248226633Sdim Type *Ty) { 1249218893Sdim sys::llvm_fenv_clearexcept(); 1250193323Sed V = NativeFP(V); 1251218893Sdim if (sys::llvm_fenv_testexcept()) { 1252218893Sdim sys::llvm_fenv_clearexcept(); 1253193323Sed return 0; 1254193323Sed } 1255243830Sdim 1256249423Sdim if (Ty->isHalfTy()) { 1257249423Sdim APFloat APF(V); 1258249423Sdim bool unused; 1259249423Sdim APF.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &unused); 1260249423Sdim return ConstantFP::get(Ty->getContext(), APF); 1261249423Sdim } 1262198090Srdivacky if (Ty->isFloatTy()) 1263199481Srdivacky return ConstantFP::get(Ty->getContext(), APFloat((float)V)); 1264198090Srdivacky if (Ty->isDoubleTy()) 1265199481Srdivacky return ConstantFP::get(Ty->getContext(), APFloat(V)); 1266249423Sdim llvm_unreachable("Can only constant fold half/float/double"); 1267193323Sed} 1268193323Sed 1269193323Sedstatic Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), 1270226633Sdim double V, double W, Type *Ty) { 1271218893Sdim sys::llvm_fenv_clearexcept(); 1272193323Sed V = NativeFP(V, W); 1273218893Sdim if (sys::llvm_fenv_testexcept()) { 1274218893Sdim sys::llvm_fenv_clearexcept(); 1275193323Sed return 0; 1276193323Sed } 1277243830Sdim 1278249423Sdim if (Ty->isHalfTy()) { 1279249423Sdim APFloat APF(V); 1280249423Sdim bool unused; 1281249423Sdim APF.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &unused); 1282249423Sdim return ConstantFP::get(Ty->getContext(), APF); 1283249423Sdim } 1284198090Srdivacky if (Ty->isFloatTy()) 1285199481Srdivacky return ConstantFP::get(Ty->getContext(), APFloat((float)V)); 1286198090Srdivacky if (Ty->isDoubleTy()) 1287199481Srdivacky return ConstantFP::get(Ty->getContext(), APFloat(V)); 1288249423Sdim llvm_unreachable("Can only constant fold half/float/double"); 1289193323Sed} 1290193323Sed 1291218893Sdim/// ConstantFoldConvertToInt - Attempt to an SSE floating point to integer 1292218893Sdim/// conversion of a constant floating point. If roundTowardZero is false, the 1293218893Sdim/// default IEEE rounding is used (toward nearest, ties to even). This matches 1294218893Sdim/// the behavior of the non-truncating SSE instructions in the default rounding 1295218893Sdim/// mode. The desired integer type Ty is used to select how many bits are 1296218893Sdim/// available for the result. Returns null if the conversion cannot be 1297218893Sdim/// performed, otherwise returns the Constant value resulting from the 1298218893Sdim/// conversion. 1299234353Sdimstatic Constant *ConstantFoldConvertToInt(const APFloat &Val, 1300234353Sdim bool roundTowardZero, Type *Ty) { 1301218893Sdim // All of these conversion intrinsics form an integer of at most 64bits. 1302263508Sdim unsigned ResultWidth = Ty->getIntegerBitWidth(); 1303218893Sdim assert(ResultWidth <= 64 && 1304218893Sdim "Can only constant fold conversions to 64 and 32 bit ints"); 1305218893Sdim 1306218893Sdim uint64_t UIntVal; 1307218893Sdim bool isExact = false; 1308218893Sdim APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero 1309218893Sdim : APFloat::rmNearestTiesToEven; 1310218893Sdim APFloat::opStatus status = Val.convertToInteger(&UIntVal, ResultWidth, 1311218893Sdim /*isSigned=*/true, mode, 1312218893Sdim &isExact); 1313218893Sdim if (status != APFloat::opOK && status != APFloat::opInexact) 1314218893Sdim return 0; 1315218893Sdim return ConstantInt::get(Ty, UIntVal, /*isSigned=*/true); 1316218893Sdim} 1317218893Sdim 1318193323Sed/// ConstantFoldCall - Attempt to constant fold a call to the specified function 1319193323Sed/// with the specified arguments, returning null if unsuccessful. 1320193323SedConstant * 1321234353Sdimllvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands, 1322234353Sdim const TargetLibraryInfo *TLI) { 1323263508Sdim if (!F->hasName()) 1324263508Sdim return 0; 1325198090Srdivacky StringRef Name = F->getName(); 1326198090Srdivacky 1327226633Sdim Type *Ty = F->getReturnType(); 1328226633Sdim if (Operands.size() == 1) { 1329193323Sed if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) { 1330218893Sdim if (F->getIntrinsicID() == Intrinsic::convert_to_fp16) { 1331205407Srdivacky APFloat Val(Op->getValueAPF()); 1332205407Srdivacky 1333205407Srdivacky bool lost = false; 1334205407Srdivacky Val.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &lost); 1335205407Srdivacky 1336205407Srdivacky return ConstantInt::get(F->getContext(), Val.bitcastToAPInt()); 1337205407Srdivacky } 1338234353Sdim if (!TLI) 1339234353Sdim return 0; 1340205407Srdivacky 1341249423Sdim if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy()) 1342193323Sed return 0; 1343218893Sdim 1344218893Sdim /// We only fold functions with finite arguments. Folding NaN and inf is 1345218893Sdim /// likely to be aborted with an exception anyway, and some host libms 1346218893Sdim /// have known errors raising exceptions. 1347218893Sdim if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity()) 1348218893Sdim return 0; 1349218893Sdim 1350193323Sed /// Currently APFloat versions of these functions do not exist, so we use 1351193323Sed /// the host native double versions. Float versions are not called 1352193323Sed /// directly but for all these it is true (float)(f((double)arg)) == 1353193323Sed /// f(arg). Long double not supported yet. 1354249423Sdim double V; 1355249423Sdim if (Ty->isFloatTy()) 1356249423Sdim V = Op->getValueAPF().convertToFloat(); 1357249423Sdim else if (Ty->isDoubleTy()) 1358249423Sdim V = Op->getValueAPF().convertToDouble(); 1359249423Sdim else { 1360249423Sdim bool unused; 1361249423Sdim APFloat APF = Op->getValueAPF(); 1362249423Sdim APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &unused); 1363249423Sdim V = APF.convertToDouble(); 1364249423Sdim } 1365249423Sdim 1366249423Sdim switch (F->getIntrinsicID()) { 1367249423Sdim default: break; 1368249423Sdim case Intrinsic::fabs: 1369249423Sdim return ConstantFoldFP(fabs, V, Ty); 1370249423Sdim#if HAVE_LOG2 1371249423Sdim case Intrinsic::log2: 1372249423Sdim return ConstantFoldFP(log2, V, Ty); 1373249423Sdim#endif 1374249423Sdim#if HAVE_LOG 1375249423Sdim case Intrinsic::log: 1376249423Sdim return ConstantFoldFP(log, V, Ty); 1377249423Sdim#endif 1378249423Sdim#if HAVE_LOG10 1379249423Sdim case Intrinsic::log10: 1380249423Sdim return ConstantFoldFP(log10, V, Ty); 1381249423Sdim#endif 1382249423Sdim#if HAVE_EXP 1383249423Sdim case Intrinsic::exp: 1384249423Sdim return ConstantFoldFP(exp, V, Ty); 1385249423Sdim#endif 1386249423Sdim#if HAVE_EXP2 1387249423Sdim case Intrinsic::exp2: 1388249423Sdim return ConstantFoldFP(exp2, V, Ty); 1389249423Sdim#endif 1390249423Sdim case Intrinsic::floor: 1391249423Sdim return ConstantFoldFP(floor, V, Ty); 1392249423Sdim } 1393249423Sdim 1394198090Srdivacky switch (Name[0]) { 1395193323Sed case 'a': 1396234353Sdim if (Name == "acos" && TLI->has(LibFunc::acos)) 1397199481Srdivacky return ConstantFoldFP(acos, V, Ty); 1398234353Sdim else if (Name == "asin" && TLI->has(LibFunc::asin)) 1399199481Srdivacky return ConstantFoldFP(asin, V, Ty); 1400234353Sdim else if (Name == "atan" && TLI->has(LibFunc::atan)) 1401199481Srdivacky return ConstantFoldFP(atan, V, Ty); 1402193323Sed break; 1403193323Sed case 'c': 1404234353Sdim if (Name == "ceil" && TLI->has(LibFunc::ceil)) 1405199481Srdivacky return ConstantFoldFP(ceil, V, Ty); 1406234353Sdim else if (Name == "cos" && TLI->has(LibFunc::cos)) 1407199481Srdivacky return ConstantFoldFP(cos, V, Ty); 1408234353Sdim else if (Name == "cosh" && TLI->has(LibFunc::cosh)) 1409199481Srdivacky return ConstantFoldFP(cosh, V, Ty); 1410234353Sdim else if (Name == "cosf" && TLI->has(LibFunc::cosf)) 1411199481Srdivacky return ConstantFoldFP(cos, V, Ty); 1412193323Sed break; 1413193323Sed case 'e': 1414234353Sdim if (Name == "exp" && TLI->has(LibFunc::exp)) 1415199481Srdivacky return ConstantFoldFP(exp, V, Ty); 1416243830Sdim 1417234353Sdim if (Name == "exp2" && TLI->has(LibFunc::exp2)) { 1418223017Sdim // Constant fold exp2(x) as pow(2,x) in case the host doesn't have a 1419223017Sdim // C99 library. 1420223017Sdim return ConstantFoldBinaryFP(pow, 2.0, V, Ty); 1421223017Sdim } 1422193323Sed break; 1423193323Sed case 'f': 1424234353Sdim if (Name == "fabs" && TLI->has(LibFunc::fabs)) 1425199481Srdivacky return ConstantFoldFP(fabs, V, Ty); 1426234353Sdim else if (Name == "floor" && TLI->has(LibFunc::floor)) 1427199481Srdivacky return ConstantFoldFP(floor, V, Ty); 1428193323Sed break; 1429193323Sed case 'l': 1430234353Sdim if (Name == "log" && V > 0 && TLI->has(LibFunc::log)) 1431199481Srdivacky return ConstantFoldFP(log, V, Ty); 1432234353Sdim else if (Name == "log10" && V > 0 && TLI->has(LibFunc::log10)) 1433199481Srdivacky return ConstantFoldFP(log10, V, Ty); 1434218893Sdim else if (F->getIntrinsicID() == Intrinsic::sqrt && 1435249423Sdim (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())) { 1436193323Sed if (V >= -0.0) 1437199481Srdivacky return ConstantFoldFP(sqrt, V, Ty); 1438193323Sed else // Undefined 1439193323Sed return Constant::getNullValue(Ty); 1440193323Sed } 1441193323Sed break; 1442193323Sed case 's': 1443234353Sdim if (Name == "sin" && TLI->has(LibFunc::sin)) 1444199481Srdivacky return ConstantFoldFP(sin, V, Ty); 1445234353Sdim else if (Name == "sinh" && TLI->has(LibFunc::sinh)) 1446199481Srdivacky return ConstantFoldFP(sinh, V, Ty); 1447234353Sdim else if (Name == "sqrt" && V >= 0 && TLI->has(LibFunc::sqrt)) 1448199481Srdivacky return ConstantFoldFP(sqrt, V, Ty); 1449234353Sdim else if (Name == "sqrtf" && V >= 0 && TLI->has(LibFunc::sqrtf)) 1450199481Srdivacky return ConstantFoldFP(sqrt, V, Ty); 1451234353Sdim else if (Name == "sinf" && TLI->has(LibFunc::sinf)) 1452199481Srdivacky return ConstantFoldFP(sin, V, Ty); 1453193323Sed break; 1454193323Sed case 't': 1455234353Sdim if (Name == "tan" && TLI->has(LibFunc::tan)) 1456199481Srdivacky return ConstantFoldFP(tan, V, Ty); 1457234353Sdim else if (Name == "tanh" && TLI->has(LibFunc::tanh)) 1458199481Srdivacky return ConstantFoldFP(tanh, V, Ty); 1459193323Sed break; 1460193323Sed default: 1461193323Sed break; 1462193323Sed } 1463198090Srdivacky return 0; 1464198090Srdivacky } 1465218893Sdim 1466198090Srdivacky if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) { 1467218893Sdim switch (F->getIntrinsicID()) { 1468218893Sdim case Intrinsic::bswap: 1469199481Srdivacky return ConstantInt::get(F->getContext(), Op->getValue().byteSwap()); 1470218893Sdim case Intrinsic::ctpop: 1471193323Sed return ConstantInt::get(Ty, Op->getValue().countPopulation()); 1472218893Sdim case Intrinsic::convert_from_fp16: { 1473249423Sdim APFloat Val(APFloat::IEEEhalf, Op->getValue()); 1474205407Srdivacky 1475205407Srdivacky bool lost = false; 1476205407Srdivacky APFloat::opStatus status = 1477205407Srdivacky Val.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &lost); 1478205407Srdivacky 1479205407Srdivacky // Conversion is always precise. 1480218893Sdim (void)status; 1481205407Srdivacky assert(status == APFloat::opOK && !lost && 1482205407Srdivacky "Precision lost during fp16 constfolding"); 1483205407Srdivacky 1484205407Srdivacky return ConstantFP::get(F->getContext(), Val); 1485205407Srdivacky } 1486218893Sdim default: 1487218893Sdim return 0; 1488218893Sdim } 1489193323Sed } 1490218893Sdim 1491234353Sdim // Support ConstantVector in case we have an Undef in the top. 1492243830Sdim if (isa<ConstantVector>(Operands[0]) || 1493234353Sdim isa<ConstantDataVector>(Operands[0])) { 1494234353Sdim Constant *Op = cast<Constant>(Operands[0]); 1495218893Sdim switch (F->getIntrinsicID()) { 1496218893Sdim default: break; 1497218893Sdim case Intrinsic::x86_sse_cvtss2si: 1498218893Sdim case Intrinsic::x86_sse_cvtss2si64: 1499218893Sdim case Intrinsic::x86_sse2_cvtsd2si: 1500218893Sdim case Intrinsic::x86_sse2_cvtsd2si64: 1501234353Sdim if (ConstantFP *FPOp = 1502234353Sdim dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) 1503234353Sdim return ConstantFoldConvertToInt(FPOp->getValueAPF(), 1504234353Sdim /*roundTowardZero=*/false, Ty); 1505218893Sdim case Intrinsic::x86_sse_cvttss2si: 1506218893Sdim case Intrinsic::x86_sse_cvttss2si64: 1507218893Sdim case Intrinsic::x86_sse2_cvttsd2si: 1508218893Sdim case Intrinsic::x86_sse2_cvttsd2si64: 1509234353Sdim if (ConstantFP *FPOp = 1510234353Sdim dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) 1511243830Sdim return ConstantFoldConvertToInt(FPOp->getValueAPF(), 1512234353Sdim /*roundTowardZero=*/true, Ty); 1513218893Sdim } 1514218893Sdim } 1515243830Sdim 1516204642Srdivacky if (isa<UndefValue>(Operands[0])) { 1517218893Sdim if (F->getIntrinsicID() == Intrinsic::bswap) 1518204642Srdivacky return Operands[0]; 1519204642Srdivacky return 0; 1520204642Srdivacky } 1521204642Srdivacky 1522198090Srdivacky return 0; 1523198090Srdivacky } 1524218893Sdim 1525226633Sdim if (Operands.size() == 2) { 1526193323Sed if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) { 1527249423Sdim if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy()) 1528193323Sed return 0; 1529249423Sdim double Op1V; 1530249423Sdim if (Ty->isFloatTy()) 1531249423Sdim Op1V = Op1->getValueAPF().convertToFloat(); 1532249423Sdim else if (Ty->isDoubleTy()) 1533249423Sdim Op1V = Op1->getValueAPF().convertToDouble(); 1534249423Sdim else { 1535249423Sdim bool unused; 1536249423Sdim APFloat APF = Op1->getValueAPF(); 1537249423Sdim APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &unused); 1538249423Sdim Op1V = APF.convertToDouble(); 1539249423Sdim } 1540249423Sdim 1541193323Sed if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) { 1542198090Srdivacky if (Op2->getType() != Op1->getType()) 1543198090Srdivacky return 0; 1544234353Sdim 1545249423Sdim double Op2V; 1546249423Sdim if (Ty->isFloatTy()) 1547249423Sdim Op2V = Op2->getValueAPF().convertToFloat(); 1548249423Sdim else if (Ty->isDoubleTy()) 1549249423Sdim Op2V = Op2->getValueAPF().convertToDouble(); 1550249423Sdim else { 1551249423Sdim bool unused; 1552249423Sdim APFloat APF = Op2->getValueAPF(); 1553249423Sdim APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &unused); 1554249423Sdim Op2V = APF.convertToDouble(); 1555249423Sdim } 1556193323Sed 1557234353Sdim if (F->getIntrinsicID() == Intrinsic::pow) { 1558199481Srdivacky return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty); 1559234353Sdim } 1560234353Sdim if (!TLI) 1561234353Sdim return 0; 1562234353Sdim if (Name == "pow" && TLI->has(LibFunc::pow)) 1563234353Sdim return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty); 1564234353Sdim if (Name == "fmod" && TLI->has(LibFunc::fmod)) 1565199481Srdivacky return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty); 1566234353Sdim if (Name == "atan2" && TLI->has(LibFunc::atan2)) 1567199481Srdivacky return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty); 1568193323Sed } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) { 1569249423Sdim if (F->getIntrinsicID() == Intrinsic::powi && Ty->isHalfTy()) 1570249423Sdim return ConstantFP::get(F->getContext(), 1571249423Sdim APFloat((float)std::pow((float)Op1V, 1572249423Sdim (int)Op2C->getZExtValue()))); 1573218893Sdim if (F->getIntrinsicID() == Intrinsic::powi && Ty->isFloatTy()) 1574199481Srdivacky return ConstantFP::get(F->getContext(), 1575199481Srdivacky APFloat((float)std::pow((float)Op1V, 1576193323Sed (int)Op2C->getZExtValue()))); 1577218893Sdim if (F->getIntrinsicID() == Intrinsic::powi && Ty->isDoubleTy()) 1578199481Srdivacky return ConstantFP::get(F->getContext(), 1579199481Srdivacky APFloat((double)std::pow((double)Op1V, 1580199481Srdivacky (int)Op2C->getZExtValue()))); 1581198090Srdivacky } 1582198090Srdivacky return 0; 1583198090Srdivacky } 1584243830Sdim 1585198090Srdivacky if (ConstantInt *Op1 = dyn_cast<ConstantInt>(Operands[0])) { 1586198090Srdivacky if (ConstantInt *Op2 = dyn_cast<ConstantInt>(Operands[1])) { 1587198090Srdivacky switch (F->getIntrinsicID()) { 1588198090Srdivacky default: break; 1589218893Sdim case Intrinsic::sadd_with_overflow: 1590218893Sdim case Intrinsic::uadd_with_overflow: 1591218893Sdim case Intrinsic::ssub_with_overflow: 1592218893Sdim case Intrinsic::usub_with_overflow: 1593221345Sdim case Intrinsic::smul_with_overflow: 1594221345Sdim case Intrinsic::umul_with_overflow: { 1595218893Sdim APInt Res; 1596218893Sdim bool Overflow; 1597218893Sdim switch (F->getIntrinsicID()) { 1598234353Sdim default: llvm_unreachable("Invalid case"); 1599218893Sdim case Intrinsic::sadd_with_overflow: 1600218893Sdim Res = Op1->getValue().sadd_ov(Op2->getValue(), Overflow); 1601218893Sdim break; 1602218893Sdim case Intrinsic::uadd_with_overflow: 1603218893Sdim Res = Op1->getValue().uadd_ov(Op2->getValue(), Overflow); 1604218893Sdim break; 1605218893Sdim case Intrinsic::ssub_with_overflow: 1606218893Sdim Res = Op1->getValue().ssub_ov(Op2->getValue(), Overflow); 1607218893Sdim break; 1608218893Sdim case Intrinsic::usub_with_overflow: 1609218893Sdim Res = Op1->getValue().usub_ov(Op2->getValue(), Overflow); 1610218893Sdim break; 1611218893Sdim case Intrinsic::smul_with_overflow: 1612218893Sdim Res = Op1->getValue().smul_ov(Op2->getValue(), Overflow); 1613218893Sdim break; 1614221345Sdim case Intrinsic::umul_with_overflow: 1615221345Sdim Res = Op1->getValue().umul_ov(Op2->getValue(), Overflow); 1616221345Sdim break; 1617218893Sdim } 1618198090Srdivacky Constant *Ops[] = { 1619218893Sdim ConstantInt::get(F->getContext(), Res), 1620218893Sdim ConstantInt::get(Type::getInt1Ty(F->getContext()), Overflow) 1621198090Srdivacky }; 1622224145Sdim return ConstantStruct::get(cast<StructType>(F->getReturnType()), Ops); 1623193323Sed } 1624234353Sdim case Intrinsic::cttz: 1625249423Sdim if (Op2->isOne() && Op1->isZero()) // cttz(0, 1) is undef. 1626249423Sdim return UndefValue::get(Ty); 1627234353Sdim return ConstantInt::get(Ty, Op1->getValue().countTrailingZeros()); 1628234353Sdim case Intrinsic::ctlz: 1629249423Sdim if (Op2->isOne() && Op1->isZero()) // ctlz(0, 1) is undef. 1630249423Sdim return UndefValue::get(Ty); 1631234353Sdim return ConstantInt::get(Ty, Op1->getValue().countLeadingZeros()); 1632198090Srdivacky } 1633193323Sed } 1634243830Sdim 1635198090Srdivacky return 0; 1636193323Sed } 1637198090Srdivacky return 0; 1638193323Sed } 1639193323Sed return 0; 1640193323Sed} 1641