LLVM API Documentation

Functions
ConstantFolding.cpp File Reference
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Config/config.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Operator.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include <cerrno>
#include <cmath>
#include <fenv.h>
Include dependency graph for ConstantFolding.cpp:

Go to the source code of this file.

Functions

static Constant * FoldBitCast (Constant *C, Type *DestTy, const DataLayout &TD)
static bool IsConstantOffsetFromGlobal (Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &TD)
static bool ReadDataFromGlobal (Constant *C, uint64_t ByteOffset, unsigned char *CurPtr, unsigned BytesLeft, const DataLayout &TD)
static Constant * FoldReinterpretLoadFromConstPtr (Constant *C, const DataLayout &TD)
static Constant * ConstantFoldLoadThroughBitcast (ConstantExpr *CE, const DataLayout *DL)
static Constant * ConstantFoldLoadInst (const LoadInst *LI, const DataLayout *TD)
static Constant * SymbolicallyEvaluateBinop (unsigned Opc, Constant *Op0, Constant *Op1, const DataLayout *DL)
static Constant * CastGEPIndices (ArrayRef< Constant * > Ops, Type *ResultTy, const DataLayout *TD, const TargetLibraryInfo *TLI)
static Constant * StripPtrCastKeepAS (Constant *Ptr)
 Strip the pointer casts, but preserve the address space information.
static Constant * SymbolicallyEvaluateGEP (ArrayRef< Constant * > Ops, Type *ResultTy, const DataLayout *TD, const TargetLibraryInfo *TLI)
static Constant * ConstantFoldConstantExpressionImpl (const ConstantExpr *CE, const DataLayout *TD, const TargetLibraryInfo *TLI, SmallPtrSetImpl< ConstantExpr * > &FoldedOps)
static Constant * GetConstantFoldFPValue (double V, Type *Ty)
static Constant * ConstantFoldFP (double(*NativeFP)(double), double V, Type *Ty)
static Constant * ConstantFoldBinaryFP (double(*NativeFP)(double, double), double V, double W, Type *Ty)
static Constant * ConstantFoldConvertToInt (const APFloat &Val, bool roundTowardZero, Type *Ty)
static double getValueAsDouble (ConstantFP *Op)
static Constant * ConstantFoldScalarCall (StringRef Name, unsigned IntrinsicID, Type *Ty, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI)
static Constant * ConstantFoldVectorCall (StringRef Name, unsigned IntrinsicID, VectorType *VTy, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI)

Function Documentation

static Constant* CastGEPIndices ( ArrayRef< Constant * >  Ops,
Type *  ResultTy,
const DataLayout *  TD,
const TargetLibraryInfo *  TLI 
) [static]
static Constant* ConstantFoldBinaryFP ( double(*)(double, double)  NativeFP,
double  V,
double  W,
Type *  Ty 
) [static]

Definition at line 1357 of file ConstantFolding.cpp.

References GetConstantFoldFPValue().

Referenced by ConstantFoldScalarCall().

static Constant* ConstantFoldConvertToInt ( const APFloat &  Val,
bool  roundTowardZero,
Type *  Ty 
) [static]

ConstantFoldConvertToInt - Attempt to an SSE floating point to integer conversion of a constant floating point. If roundTowardZero is false, the default IEEE rounding is used (toward nearest, ties to even). This matches the behavior of the non-truncating SSE instructions in the default rounding mode. The desired integer type Ty is used to select how many bits are available for the result. Returns null if the conversion cannot be performed, otherwise returns the Constant value resulting from the conversion.

Definition at line 1377 of file ConstantFolding.cpp.

References llvm::APFloat::convertToInteger(), llvm::ConstantInt::get(), llvm::Type::getIntegerBitWidth(), llvm::APFloat::opInexact, llvm::APFloat::opOK, llvm::APFloat::rmNearestTiesToEven, llvm::APFloat::rmTowardZero, and llvm::sys::fs::status().

Referenced by ConstantFoldScalarCall().

static Constant* ConstantFoldFP ( double(*)(double)  NativeFP,
double  V,
Type *  Ty 
) [static]

Definition at line 1345 of file ConstantFolding.cpp.

References GetConstantFoldFPValue().

Referenced by ConstantFoldScalarCall().

static Constant* ConstantFoldLoadInst ( const LoadInst *  LI,
const DataLayout *  TD 
) [static]
static Constant* ConstantFoldLoadThroughBitcast ( ConstantExpr *  CE,
const DataLayout *  DL 
) [static]
static Constant* ConstantFoldScalarCall ( StringRef  Name,
unsigned  IntrinsicID,
Type *  Ty,
ArrayRef< Constant * >  Operands,
const TargetLibraryInfo *  TLI 
) [static]

We only fold functions with finite arguments. Folding NaN and inf is likely to be aborted with an exception anyway, and some host libms have known errors raising exceptions.

Currently APFloat versions of these functions do not exist, so we use the host native double versions. Float versions are not called directly but for all these it is true (float)(f((double)arg)) == f(arg). Long double not supported yet.

Definition at line 1411 of file ConstantFolding.cpp.

References llvm::LibFunc::acos, llvm::lltok::APFloat, llvm::LibFunc::asin, llvm::LibFunc::atan, llvm::LibFunc::atan2, llvm::LibFunc::ceil, ConstantFoldBinaryFP(), ConstantFoldConvertToInt(), ConstantFoldFP(), llvm::APFloat::convert(), llvm::LibFunc::copysign, llvm::APFloat::copySign(), llvm::LibFunc::cos, llvm::LibFunc::cosf, llvm::LibFunc::cosh, llvm::LibFunc::exp, llvm::LibFunc::exp2, llvm::LibFunc::fabs, llvm::LibFunc::floor, llvm::LibFunc::fmod, llvm::APFloat::fusedMultiplyAdd(), llvm::ConstantInt::get(), llvm::ConstantFP::get(), llvm::ConstantStruct::get(), llvm::UndefValue::get(), llvm::Constant::getAggregateElement(), llvm::Type::getContext(), llvm::Type::getInt1Ty(), llvm::Constant::getNullValue(), getValueAsDouble(), llvm::TargetLibraryInfo::has(), llvm::APFloat::IEEEhalf, llvm::APFloat::IEEEsingle, llvm::Type::isDoubleTy(), llvm::Type::isFloatTy(), llvm::Type::isHalfTy(), llvm_unreachable, llvm::LibFunc::log, llvm::LibFunc::log10, llvm::LibFunc::log2, llvm::APFloat::opInvalidOp, llvm::APFloat::opOK, llvm::LibFunc::pow, llvm::APFloat::rmNearestTiesToAway, llvm::APFloat::rmNearestTiesToEven, llvm::LibFunc::round, llvm::APFloat::roundToIntegral(), llvm::APInt::sadd_ov(), llvm::LibFunc::sin, llvm::LibFunc::sinf, llvm::LibFunc::sinh, llvm::ArrayRef< T >::size(), llvm::APInt::smul_ov(), llvm::LibFunc::sqrt, llvm::LibFunc::sqrtf, llvm::APInt::ssub_ov(), llvm::sys::fs::status(), llvm::LibFunc::tan, llvm::LibFunc::tanh, llvm::APInt::uadd_ov(), llvm::APInt::umul_ov(), llvm::APInt::usub_ov(), and llvm::NVPTX::PTXLdStInstCode::V2.

Referenced by llvm::ConstantFoldCall(), and ConstantFoldVectorCall().

static Constant* ConstantFoldVectorCall ( StringRef  Name,
unsigned  IntrinsicID,
VectorType *  VTy,
ArrayRef< Constant * >  Operands,
const TargetLibraryInfo *  TLI 
) [static]
static Constant* FoldBitCast ( Constant *  C,
Type *  DestTy,
const DataLayout &  TD 
) [static]
static Constant* FoldReinterpretLoadFromConstPtr ( Constant *  C,
const DataLayout &  TD 
) [static]
static Constant* GetConstantFoldFPValue ( double  V,
Type *  Ty 
) [static]
static double getValueAsDouble ( ConstantFP *  Op) [static]
static bool IsConstantOffsetFromGlobal ( Constant *  C,
GlobalValue *&  GV,
APInt &  Offset,
const DataLayout &  TD 
) [static]

IsConstantOffsetFromGlobal - If this constant is actually a constant offset from a global, return the global and the constant. Because of constantexprs, this function is recursive.

Definition at line 228 of file ConstantFolding.cpp.

References llvm::GEPOperator::accumulateConstantOffset(), llvm::AddrSpaceCast, llvm::CallingConv::C, llvm::WinEH::CE, llvm::dyn_cast(), llvm::ConstantExpr::getOpcode(), llvm::User::getOperand(), llvm::DataLayout::getPointerTypeSizeInBits(), llvm::GlobalValue::getType(), and llvm::Value::getType().

Referenced by FoldReinterpretLoadFromConstPtr(), and SymbolicallyEvaluateBinop().

static bool ReadDataFromGlobal ( Constant *  C,
uint64_t  ByteOffset,
unsigned char *  CurPtr,
unsigned  BytesLeft,
const DataLayout &  TD 
) [static]
static Constant* StripPtrCastKeepAS ( Constant *  Ptr) [static]
static Constant* SymbolicallyEvaluateBinop ( unsigned  Opc,
Constant *  Op0,
Constant *  Op1,
const DataLayout *  DL 
) [static]

SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression. Attempt to symbolically evaluate the result of a binary operator merging these together. If target data info is available, it is provided as DL, otherwise DL is null.

Definition at line 617 of file ConstantFolding.cpp.

References llvm::APIntOps::And(), llvm::computeKnownBits(), llvm::ConstantInt::get(), llvm::Type::getScalarType(), llvm::Value::getType(), llvm::DataLayout::getTypeSizeInBits(), IsConstantOffsetFromGlobal(), and llvm::APInt::zextOrTrunc().

Referenced by llvm::ConstantFoldInstOperands().

static Constant* SymbolicallyEvaluateGEP ( ArrayRef< Constant * >  Ops,
Type *  ResultTy,
const DataLayout *  TD,
const TargetLibraryInfo *  TLI 
) [static]