LLVM API Documentation

Functions
ConstantFold.cpp File Reference
#include "ConstantFold.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Operator.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MathExtras.h"
#include <limits>
Include dependency graph for ConstantFold.cpp:

Go to the source code of this file.

Functions

static ConstantBitCastConstantVector (Constant *CV, VectorType *DstTy)
static unsigned foldConstantCastPair (unsigned opc, ConstantExpr *Op, Type *DstTy)
 Determine if it is valid to fold a cast of a cast.
static ConstantFoldBitCast (Constant *V, Type *DestTy)
static ConstantExtractConstantBytes (Constant *C, unsigned ByteStart, unsigned ByteSize)
static ConstantgetFoldedSizeOf (Type *Ty, Type *DestTy, bool Folded)
static ConstantgetFoldedAlignOf (Type *Ty, Type *DestTy, bool Folded)
static ConstantgetFoldedOffsetOf (Type *Ty, Constant *FieldNo, Type *DestTy, bool Folded)
static bool isMaybeZeroSizedType (Type *Ty)
static int IdxCompare (Constant *C1, Constant *C2, Type *ElTy)
static FCmpInst::Predicate evaluateFCmpRelation (Constant *V1, Constant *V2)
static ICmpInst::Predicate areGlobalsPotentiallyEqual (const GlobalValue *GV1, const GlobalValue *GV2)
static ICmpInst::Predicate evaluateICmpRelation (Constant *V1, Constant *V2, bool isSigned)
template<typename IndexTy >
static bool isInBoundsIndices (ArrayRef< IndexTy > Idxs)
static bool isIndexInRangeOfSequentialType (const SequentialType *STy, const ConstantInt *CI)
 Test whether a given ConstantInt is in-range for a SequentialType.
template<typename IndexTy >
static ConstantConstantFoldGetElementPtrImpl (Constant *C, bool inBounds, ArrayRef< IndexTy > Idxs)

Function Documentation

static ICmpInst::Predicate areGlobalsPotentiallyEqual ( const GlobalValue GV1,
const GlobalValue GV2 
) [static]
static Constant* BitCastConstantVector ( Constant CV,
VectorType DstTy 
) [static]
template<typename IndexTy >
static Constant* ConstantFoldGetElementPtrImpl ( Constant C,
bool  inBounds,
ArrayRef< IndexTy >  Idxs 
) [static]
static FCmpInst::Predicate evaluateFCmpRelation ( Constant V1,
Constant V2 
) [static]

evaluateFCmpRelation - This function determines if there is anything we can decide about the two constants provided. This doesn't need to handle simple things like ConstantFP comparisons, but should instead handle ConstantExprs. If we can determine that the two constants have a particular relation to each other, we should return the corresponding FCmpInst predicate, otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in ConstantFoldCompareInstruction.

To simplify this code we canonicalize the relation so that the first operand is always the most "complex" of the two. We consider ConstantFP to be the simplest, and ConstantExprs to be the most complex.

Definition at line 1288 of file ConstantFold.cpp.

References llvm::CmpInst::BAD_FCMP_PREDICATE, llvm::dyn_cast(), llvm::CmpInst::FCMP_OEQ, llvm::CmpInst::FCMP_OGT, llvm::CmpInst::FCMP_OLT, llvm::FPExt, llvm::ConstantExpr::getFCmp(), llvm::ConstantExpr::getOpcode(), llvm::CmpInst::getSwappedPredicate(), llvm::Value::getType(), llvm::ConstantInt::isZero(), and llvm::SIToFP.

Referenced by llvm::ConstantFoldCompareInstruction().

static ICmpInst::Predicate evaluateICmpRelation ( Constant V1,
Constant V2,
bool  isSigned 
) [static]

evaluateICmpRelation - This function determines if there is anything we can decide about the two constants provided. This doesn't need to handle simple things like integer comparisons, but should instead handle ConstantExprs and GlobalValues. If we can determine that the two constants have a particular relation to each other, we should return the corresponding ICmp predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.

To simplify this code we canonicalize the relation so that the first operand is always the most "complex" of the two. We consider simple constants (like ConstantInt) to be the simplest, followed by GlobalValues, followed by ConstantExpr's (the most complex).

Definition at line 1361 of file ConstantFold.cpp.

References areGlobalsPotentiallyEqual(), llvm::CmpInst::BAD_ICMP_PREDICATE, llvm::dyn_cast(), llvm::FPExt, llvm::FPToSI, llvm::gep_type_begin(), llvm::ConstantExpr::getICmp(), llvm::generic_gep_type_iterator< ItTy >::getIndexedType(), llvm::Constant::getNullValue(), llvm::User::getNumOperands(), llvm::ConstantExpr::getOpcode(), llvm::User::getOperand(), llvm::CmpInst::getSwappedPredicate(), llvm::Value::getType(), llvm::GEPOperator::hasAllZeroIndices(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULE, llvm::CmpInst::ICMP_ULT, IdxCompare(), llvm::ConstantExpr::isGEPWithNoNotionalOverIndexing(), llvm::Type::isIntegerTy(), llvm::Constant::isNullValue(), llvm::Type::isPointerTy(), llvm::ConstantInt::isZero(), llvm::SExt, llvm::SIToFP, llvm::Trunc, and llvm::NVPTX::PTXLdStInstCode::V2.

Referenced by llvm::ConstantFoldCompareInstruction().

static Constant* ExtractConstantBytes ( Constant C,
unsigned  ByteStart,
unsigned  ByteSize 
) [static]

ExtractConstantBytes - V is an integer constant which only has a subset of its bytes used. The bytes used are indicated by ByteStart (which is the first byte used, counting from the least significant byte) and ByteSize, which is the number of bytes used.

This function analyzes the specified constant to see if the specified byte range can be returned as a simplified constant. If so, the constant is returned, otherwise null is returned.

Definition at line 197 of file ConstantFold.cpp.

References llvm::APIntOps::And(), llvm::CallingConv::C, llvm::WinEH::CE, llvm::dyn_cast(), llvm::IntegerType::get(), llvm::ConstantInt::get(), llvm::ConstantExpr::getAnd(), getBitWidth(), llvm::Value::getContext(), llvm::ConstantExpr::getLShr(), llvm::Constant::getNullValue(), llvm::ConstantExpr::getOpcode(), llvm::User::getOperand(), llvm::ConstantExpr::getOr(), llvm::ConstantExpr::getTrunc(), llvm::Value::getType(), llvm::ConstantInt::getZExtValue(), llvm::Type::isIntegerTy(), llvm::Constant::isNullValue(), llvm::LShr, llvm::APInt::lshr(), llvm::APIntOps::Or(), and llvm::APInt::trunc().

Referenced by llvm::ConstantFoldCastInstruction().

static Constant* FoldBitCast ( Constant V,
Type DestTy 
) [static]
static unsigned foldConstantCastPair ( unsigned  opc,
ConstantExpr Op,
Type DstTy 
) [static]

Determine if it is valid to fold a cast of a cast.

This function determines which opcode to use to fold two constant cast expressions together. It uses CastInst::isEliminableCastPair to determine the opcode. Consequently its just a wrapper around that function.

Parameters:
opcopcode of the second cast constant expression
Opthe first cast constant expression
DstTydestination type of the first cast

Definition at line 75 of file ConstantFold.cpp.

References llvm::Type::getContext(), llvm::Type::getInt64Ty(), llvm::ConstantExpr::getOpcode(), llvm::User::getOperand(), llvm::Value::getType(), llvm::Instruction::isCast(), llvm::ConstantExpr::isCast(), llvm::CastInst::isEliminableCastPair(), and llvm::Type::isFirstClassType().

Referenced by llvm::ConstantFoldCastInstruction().

static Constant* getFoldedAlignOf ( Type Ty,
Type DestTy,
bool  Folded 
) [static]

getFoldedAlignOf - Return a ConstantExpr with type DestTy for alignof on Ty, with any known factors factored out. If Folded is false, return null if no factoring was possible, to avoid endlessly bouncing an unfoldable expression back into the top-level folder.

Definition at line 394 of file ConstantFold.cpp.

References llvm::CallingConv::C, llvm::IntegerType::get(), llvm::ConstantInt::get(), llvm::PointerType::get(), llvm::ConstantExpr::getAlignOf(), llvm::ConstantExpr::getCast(), and llvm::CastInst::getCastOpcode().

Referenced by llvm::ConstantFoldCastInstruction().

static Constant* getFoldedOffsetOf ( Type Ty,
Constant FieldNo,
Type DestTy,
bool  Folded 
) [static]

getFoldedOffsetOf - Return a ConstantExpr with type DestTy for offsetof on Ty and FieldNo, with any known factors factored out. If Folded is false, return null if no factoring was possible, to avoid endlessly bouncing an unfoldable expression back into the top-level folder.

Definition at line 460 of file ConstantFold.cpp.

References llvm::CallingConv::C, llvm::ConstantExpr::getCast(), llvm::CastInst::getCastOpcode(), getFoldedSizeOf(), llvm::ConstantExpr::getNUWMul(), llvm::ConstantExpr::getOffsetOf(), and N.

Referenced by llvm::ConstantFoldCastInstruction().

static Constant* getFoldedSizeOf ( Type Ty,
Type DestTy,
bool  Folded 
) [static]

getFoldedSizeOf - Return a ConstantExpr with type DestTy for sizeof on Ty, with any known factors factored out. If Folded is false, return null if no factoring was possible, to avoid endlessly bouncing an unfoldable expression back into the top-level folder.

Definition at line 337 of file ConstantFold.cpp.

References llvm::CallingConv::C, llvm::IntegerType::get(), llvm::ConstantInt::get(), llvm::PointerType::get(), llvm::ConstantExpr::getCast(), llvm::CastInst::getCastOpcode(), llvm::Constant::getNullValue(), llvm::ConstantExpr::getNUWMul(), llvm::ConstantExpr::getSizeOf(), and N.

Referenced by llvm::ConstantFoldCastInstruction(), and getFoldedOffsetOf().

static int IdxCompare ( Constant C1,
Constant C2,
Type ElTy 
) [static]

IdxCompare - Compare the two constants as though they were getelementptr indices. This allows coersion of the types to be the same thing.

If the two constants are the "same" (after coersion), return 0. If the first is less than the second, return -1, if the second is less than the first, return 1. If the constants are not integral, return -2.

Definition at line 1245 of file ConstantFold.cpp.

References llvm::Value::getContext(), llvm::Type::getInt64Ty(), llvm::ConstantExpr::getSExt(), llvm::Value::getType(), llvm::Type::isIntegerTy(), and isMaybeZeroSizedType().

Referenced by evaluateICmpRelation().

template<typename IndexTy >
static bool isInBoundsIndices ( ArrayRef< IndexTy >  Idxs) [static]

isInBoundsIndices - Test whether the given sequence of *normalized* indices is "inbounds".

Definition at line 1915 of file ConstantFold.cpp.

References llvm::ArrayRef< T >::empty(), and llvm::ArrayRef< T >::size().

Referenced by ConstantFoldGetElementPtrImpl().

Test whether a given ConstantInt is in-range for a SequentialType.

Definition at line 1933 of file ConstantFold.cpp.

References llvm::APInt::getActiveBits(), llvm::ConstantInt::getSExtValue(), and llvm::ConstantInt::getValue().

Referenced by ConstantFoldGetElementPtrImpl().

static bool isMaybeZeroSizedType ( Type Ty) [static]

isZeroSizedType - This type is zero sized if its an array or structure of zero sized types. The only leaf zero sized type is an empty structure.

Definition at line 1223 of file ConstantFold.cpp.

Referenced by IdxCompare().