|
LLVM
3.7.0
|
#include "llvm/Analysis/ConstantFolding.h"#include "llvm/ADT/SmallPtrSet.h"#include "llvm/ADT/SmallVector.h"#include "llvm/ADT/StringMap.h"#include "llvm/Analysis/TargetLibraryInfo.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 <cerrno>#include <cmath>#include <fenv.h>Go to the source code of this file.
Functions | |
| static Constant * | FoldBitCast (Constant *C, Type *DestTy, const DataLayout &DL) |
| Constant fold bitcast, symbolically evaluating it with DataLayout. More... | |
| static bool | IsConstantOffsetFromGlobal (Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL) |
| If this constant is a constant offset from a global, return the global and the constant. More... | |
| static bool | ReadDataFromGlobal (Constant *C, uint64_t ByteOffset, unsigned char *CurPtr, unsigned BytesLeft, const DataLayout &DL) |
| Recursive helper to read bits out of global. More... | |
| static Constant * | FoldReinterpretLoadFromConstPtr (Constant *C, const DataLayout &DL) |
| static Constant * | ConstantFoldLoadThroughBitcast (ConstantExpr *CE, const DataLayout &DL) |
| static Constant * | ConstantFoldLoadInst (const LoadInst *LI, const DataLayout &DL) |
| static Constant * | SymbolicallyEvaluateBinop (unsigned Opc, Constant *Op0, Constant *Op1, const DataLayout &DL) |
| One of Op0/Op1 is a constant expression. More... | |
| static Constant * | CastGEPIndices (Type *SrcTy, ArrayRef< Constant * > Ops, Type *ResultTy, const DataLayout &DL, const TargetLibraryInfo *TLI) |
| If array indices are not pointer-sized integers, explicitly cast them so that they aren't implicitly casted by the getelementptr. More... | |
| static Constant * | StripPtrCastKeepAS (Constant *Ptr) |
| Strip the pointer casts, but preserve the address space information. More... | |
| static Constant * | SymbolicallyEvaluateGEP (Type *SrcTy, ArrayRef< Constant * > Ops, Type *ResultTy, const DataLayout &DL, const TargetLibraryInfo *TLI) |
| If we can symbolically evaluate the GEP constant expression, do so. More... | |
| static Constant * | ConstantFoldConstantExpressionImpl (const ConstantExpr *CE, const DataLayout &DL, 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) |
| Attempt to fold an SSE floating point to integer conversion of a constant floating point. More... | |
| 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) |
|
static |
If array indices are not pointer-sized integers, explicitly cast them so that they aren't implicitly casted by the getelementptr.
Definition at line 674 of file ConstantFolding.cpp.
References llvm::SystemZICMP::Any, llvm::ConstantFoldConstantExpression(), llvm::ConstantExpr::getCast(), llvm::CastInst::getCastOpcode(), llvm::ConstantExpr::getGetElementPtr(), llvm::GetElementPtrInst::getIndexedType(), llvm::DataLayout::getIntPtrType(), llvm::Intrinsic::getType(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::ArrayRef< T >::size(), and llvm::ArrayRef< T >::slice().
Referenced by llvm::ConstantFoldInstOperands().
|
static |
Definition at line 1349 of file ConstantFolding.cpp.
References GetConstantFoldFPValue().
Referenced by ConstantFoldScalarCall().
|
static |
Definition at line 963 of file ConstantFolding.cpp.
References llvm::ConstantFoldCompareInstOperands(), llvm::ConstantFoldInstOperands(), llvm::ConstantExpr::getOpcode(), llvm::ConstantExpr::getPredicate(), llvm::Value::getType(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::ConstantExpr::isCompare(), llvm::User::op_begin(), llvm::User::op_end(), and llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back().
Referenced by llvm::ConstantFoldConstantExpression().
|
static |
Attempt to fold 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 1368 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().
Definition at line 1337 of file ConstantFolding.cpp.
References GetConstantFoldFPValue().
Referenced by ConstantFoldScalarCall().
|
static |
Definition at line 606 of file ConstantFolding.cpp.
References llvm::ConstantFoldLoadFromConstPtr(), llvm::User::getOperand(), and llvm::LoadInst::isVolatile().
Referenced by llvm::ConstantFoldInstruction().
|
static |
Definition at line 482 of file ConstantFolding.cpp.
References llvm::CastInst::castIsValid(), llvm::ConstantFoldLoadFromConstPtr(), llvm::dyn_cast(), llvm::Constant::getAggregateElement(), llvm::ConstantExpr::getCast(), llvm::User::getOperand(), llvm::Value::getType(), llvm::DataLayout::getTypeSizeInBits(), llvm::Type::isAggregateType(), llvm::Type::isIntegerTy(), and llvm::Type::isPointerTy().
Referenced by llvm::ConstantFoldLoadFromConstPtr().
|
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 1402 of file ConstantFolding.cpp.
References llvm::lltok::APFloat, ConstantFoldBinaryFP(), ConstantFoldConvertToInt(), ConstantFoldFP(), llvm::APFloat::convert(), llvm::APFloat::copySign(), llvm::APFloat::fusedMultiplyAdd(), llvm::ConstantInt::get(), llvm::ConstantFP::get(), llvm::ConstantStruct::get(), llvm::UndefValue::get(), llvm::Constant::getAggregateElement(), llvm::Type::getContext(), llvm::Type::getFltSemantics(), llvm::Type::getInt1Ty(), getValueAsDouble(), llvm::TargetLibraryInfo::has(), llvm::APFloat::IEEEhalf, llvm::Type::isDoubleTy(), llvm::Type::isFloatTy(), llvm::Type::isHalfTy(), llvm_unreachable, llvm::Log2(), llvm::maxnum(), llvm::minnum(), llvm::APFloat::opInvalidOp, llvm::APFloat::opOK, llvm::APFloat::rmNearestTiesToAway, llvm::APFloat::rmNearestTiesToEven, llvm::APFloat::roundToIntegral(), llvm::APInt::sadd_ov(), llvm::ArrayRef< T >::size(), llvm::APInt::smul_ov(), llvm::APInt::ssub_ov(), llvm::sys::fs::status(), llvm::APInt::uadd_ov(), llvm::APInt::umul_ov(), llvm::APInt::usub_ov(), and llvm::NVPTX::PTXLdStInstCode::V2.
Referenced by llvm::ConstantFoldCall(), and ConstantFoldVectorCall().
|
static |
Definition at line 1741 of file ConstantFolding.cpp.
References ConstantFoldScalarCall(), llvm::ConstantVector::get(), llvm::SequentialType::getElementType(), llvm::VectorType::getNumElements(), I, and llvm::ArrayRef< T >::size().
Referenced by llvm::ConstantFoldCall().
|
static |
Constant fold bitcast, symbolically evaluating it with DataLayout.
This always returns a non-null constant, but it may be a ConstantExpr if unfoldable.
Definition at line 53 of file ConstantFolding.cpp.
References llvm::dyn_cast(), llvm::IntegerType::get(), llvm::ConstantInt::get(), llvm::VectorType::get(), llvm::ConstantVector::get(), llvm::Constant::getAggregateElement(), llvm::Constant::getAllOnesValue(), llvm::ConstantExpr::getBitCast(), llvm::Value::getContext(), llvm::ConstantDataSequential::getElementAsInteger(), llvm::SequentialType::getElementType(), llvm::Type::getIntNTy(), llvm::ConstantExpr::getIntToPtr(), llvm::ConstantExpr::getLShr(), llvm::Constant::getNullValue(), llvm::VectorType::getNumElements(), llvm::ConstantExpr::getOr(), llvm::Type::getPrimitiveSizeInBits(), llvm::ConstantExpr::getShl(), llvm::ConstantExpr::getTrunc(), llvm::Value::getType(), llvm::DataLayout::getTypeAllocSizeInBits(), llvm::DataLayout::getTypeSizeInBits(), llvm::Type::getVectorElementType(), llvm::Type::getVectorNumElements(), llvm::ConstantExpr::getZExt(), llvm::Constant::isAllOnesValue(), llvm::Type::isFloatingPointTy(), llvm::DataLayout::isLittleEndian(), llvm::Constant::isNullValue(), llvm::Type::isPointerTy(), llvm::Type::isPtrOrPtrVectorTy(), llvm::Type::isX86_MMXTy(), IT(), and llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back().
Referenced by llvm::ConstantFoldInstOperands(), FoldReinterpretLoadFromConstPtr(), ReadDataFromGlobal(), and SymbolicallyEvaluateGEP().
|
static |
Definition at line 402 of file ConstantFolding.cpp.
References llvm::dyn_cast(), FoldBitCast(), llvm::ConstantInt::get(), llvm::UndefValue::get(), llvm::PointerType::getAddressSpace(), llvm::IntegerType::getBitWidth(), llvm::Type::getContext(), llvm::Value::getContext(), llvm::SequentialType::getElementType(), llvm::GlobalVariable::getInitializer(), llvm::Type::getInt16PtrTy(), llvm::Type::getInt32PtrTy(), llvm::Type::getInt64PtrTy(), llvm::Type::getIntNPtrTy(), llvm::Value::getType(), llvm::DataLayout::getTypeAllocSize(), llvm::DataLayout::getTypeAllocSizeInBits(), llvm::APInt::getZExtValue(), llvm::GlobalVariable::hasDefinitiveInitializer(), llvm::GlobalVariable::isConstant(), IsConstantOffsetFromGlobal(), llvm::DataLayout::isLittleEndian(), llvm::APInt::isNegative(), llvm::Type::isSized(), and ReadDataFromGlobal().
Referenced by llvm::ConstantFoldLoadFromConstPtr().
Definition at line 1300 of file ConstantFolding.cpp.
References llvm::lltok::APFloat, llvm::APFloat::convert(), llvm::ConstantFP::get(), llvm::Type::getContext(), llvm::APFloat::IEEEhalf, llvm::Type::isDoubleTy(), llvm::Type::isFloatTy(), llvm::Type::isHalfTy(), llvm_unreachable, and llvm::APFloat::rmNearestTiesToEven.
Referenced by ConstantFoldBinaryFP(), and ConstantFoldFP().
|
static |
Definition at line 1387 of file ConstantFolding.cpp.
References llvm::APFloat::convert(), llvm::APFloat::convertToDouble(), llvm::APFloat::convertToFloat(), llvm::Value::getType(), llvm::ConstantFP::getValueAPF(), llvm::APFloat::IEEEdouble, llvm::Type::isDoubleTy(), llvm::Type::isFloatTy(), and llvm::APFloat::rmNearestTiesToEven.
Referenced by ConstantFoldScalarCall().
|
static |
If this constant is a constant offset from a global, return the global and the constant.
Because of constantexprs, this function is recursive.
Definition at line 236 of file ConstantFolding.cpp.
References llvm::GEPOperator::accumulateConstantOffset(), llvm::dyn_cast(), GEP, llvm::ConstantExpr::getOpcode(), llvm::User::getOperand(), llvm::DataLayout::getPointerTypeSizeInBits(), llvm::GlobalValue::getType(), and llvm::Value::getType().
Referenced by FoldReinterpretLoadFromConstPtr(), and SymbolicallyEvaluateBinop().
|
static |
Recursive helper to read bits out of global.
C is the constant being copied out of. ByteOffset is an offset into C. CurPtr is the pointer to copy results into and BytesLeft is the number of bytes left in the CurPtr buffer. DL is the DataLayout.
Definition at line 279 of file ConstantFolding.cpp.
References llvm::CS, FoldBitCast(), llvm::Constant::getAggregateElement(), llvm::Value::getContext(), llvm::StructLayout::getElementContainingOffset(), llvm::StructLayout::getElementOffset(), llvm::Type::getInt16Ty(), llvm::Type::getInt32Ty(), llvm::Type::getInt64Ty(), llvm::DataLayout::getIntPtrType(), llvm::Type::getSequentialElementType(), llvm::DataLayout::getStructLayout(), llvm::Value::getType(), llvm::DataLayout::getTypeAllocSize(), llvm::Type::getVectorNumElements(), and llvm::DataLayout::isLittleEndian().
Referenced by FoldReinterpretLoadFromConstPtr().
Strip the pointer casts, but preserve the address space information.
Definition at line 711 of file ConstantFolding.cpp.
References llvm::PointerType::getAddressSpace(), llvm::SequentialType::getElementType(), llvm::ConstantExpr::getPointerCast(), llvm::Type::getPointerTo(), llvm::Value::getType(), llvm::Type::isPointerTy(), and llvm::Constant::stripPointerCasts().
Referenced by SymbolicallyEvaluateGEP().
|
static |
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 620 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 |
If we can symbolically evaluate the GEP constant expression, do so.
Definition at line 727 of file ConstantFolding.cpp.
References llvm::ConstantFoldConstantExpression(), llvm::ArrayRef< T >::data(), llvm::dyn_cast(), FoldBitCast(), GEP, llvm::ConstantInt::get(), llvm::Type::getContext(), llvm::StructLayout::getElementContainingOffset(), llvm::StructLayout::getElementOffset(), llvm::ConstantExpr::getGetElementPtr(), llvm::DataLayout::getIndexedOffset(), llvm::Type::getInt32Ty(), llvm::DataLayout::getIntPtrType(), llvm::ConstantExpr::getIntToPtr(), llvm::ConstantExpr::getOpcode(), llvm::User::getOperand(), llvm::Type::getPointerElementType(), llvm::ConstantExpr::getPtrToInt(), llvm::StructLayout::getSizeInBytes(), llvm::DataLayout::getStructLayout(), llvm::ConstantExpr::getSub(), llvm::Value::getType(), llvm::DataLayout::getTypeAllocSize(), llvm::DataLayout::getTypeSizeInBits(), llvm::APInt::getZExtValue(), llvm::Type::isIntegerTy(), llvm::Type::isPointerTy(), llvm::Type::isSized(), llvm::makeArrayRef(), llvm::ArrayRef< T >::size(), StripPtrCastKeepAS(), llvm::APInt::udiv(), llvm::APInt::uge(), and llvm::APInt::zextOrTrunc().
Referenced by llvm::ConstantFoldInstOperands().
1.8.6