LLVM 19.0.0git
Functions
ConstantFold.cpp File Reference
#include "llvm/IR/ConstantFold.h"
#include "llvm/ADT/APSInt.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/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/ErrorHandling.h"

Go to the source code of this file.

Functions

static unsigned foldConstantCastPair (unsigned opc, ConstantExpr *Op, Type *DstTy)
 This function determines which opcode to use to fold two constant cast expressions together.
 
static ConstantFoldBitCast (Constant *V, Type *DestTy)
 
static ConstantExtractConstantBytes (Constant *C, unsigned ByteStart, unsigned ByteSize)
 V is an integer constant which only has a subset of its bytes used.
 
static ConstantfoldMaybeUndesirableCast (unsigned opc, Constant *V, Type *DestTy)
 
static ICmpInst::Predicate areGlobalsPotentiallyEqual (const GlobalValue *GV1, const GlobalValue *GV2)
 
static ICmpInst::Predicate evaluateICmpRelation (Constant *V1, Constant *V2)
 This function determines if there is anything we can decide about the two constants provided.
 
template<typename IndexTy >
static bool isInBoundsIndices (ArrayRef< IndexTy > Idxs)
 Test whether the given sequence of normalized indices is "inbounds".
 
static bool isIndexInRangeOfArrayType (uint64_t NumElements, const ConstantInt *CI)
 Test whether a given ConstantInt is in-range for a SequentialType.
 
static ConstantfoldGEPOfGEP (GEPOperator *GEP, Type *PointeeTy, bool InBounds, ArrayRef< Value * > Idxs)
 

Function Documentation

◆ areGlobalsPotentiallyEqual()

static ICmpInst::Predicate areGlobalsPotentiallyEqual ( const GlobalValue GV1,
const GlobalValue GV2 
)
static

Definition at line 1069 of file ConstantFold.cpp.

References llvm::Type::isEmptyTy(), and llvm::Type::isSized().

Referenced by evaluateICmpRelation().

◆ evaluateICmpRelation()

static ICmpInst::Predicate evaluateICmpRelation ( Constant V1,
Constant V2 
)
static

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.

Definition at line 1099 of file ConstantFold.cpp.

References areGlobalsPotentiallyEqual(), assert(), evaluateICmpRelation(), llvm::User::getOperand(), llvm::Value::getType(), llvm::GEPOperator::hasAllZeroIndices(), llvm::GEPOperator::isInBounds(), llvm::Type::isPointerTy(), and llvm::NullPointerIsDefined().

Referenced by llvm::ConstantFoldCompareInstruction(), and evaluateICmpRelation().

◆ ExtractConstantBytes()

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

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 132 of file ConstantFold.cpp.

References assert(), llvm::CallingConv::C, ExtractConstantBytes(), llvm::IntegerType::get(), llvm::Constant::getNullValue(), llvm::ConstantInt::getValue(), llvm::APInt::getZExtValue(), llvm::APInt::lshrInPlace(), llvm::APInt::uge(), and llvm::APInt::ule().

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

◆ FoldBitCast()

static Constant * FoldBitCast ( Constant V,
Type DestTy 
)
static

◆ foldConstantCastPair()

static unsigned foldConstantCastPair ( unsigned  opc,
ConstantExpr Op,
Type DstTy 
)
static

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. Determine if it is valid to fold a cast of a cast

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

Definition at line 45 of file ConstantFold.cpp.

References assert(), llvm::Type::getContext(), llvm::Type::getInt64Ty(), llvm::CastInst::isEliminableCastPair(), and llvm::Type::isFirstClassType().

Referenced by llvm::ConstantFoldCastInstruction().

◆ foldGEPOfGEP()

static Constant * foldGEPOfGEP ( GEPOperator GEP,
Type PointeeTy,
bool  InBounds,
ArrayRef< Value * >  Idxs 
)
static

◆ foldMaybeUndesirableCast()

static Constant * foldMaybeUndesirableCast ( unsigned  opc,
Constant V,
Type DestTy 
)
static

◆ isInBoundsIndices()

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

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

Definition at line 1421 of file ConstantFold.cpp.

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

Referenced by llvm::ConstantFoldGetElementPtr().

◆ isIndexInRangeOfArrayType()

static bool isIndexInRangeOfArrayType ( uint64_t  NumElements,
const ConstantInt CI 
)
static

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

Definition at line 1447 of file ConstantFold.cpp.

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

Referenced by llvm::ConstantFoldGetElementPtr().