LLVM  9.0.0svn
InstCombineCasts.cpp File Reference
#include "InstCombineInternal.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/KnownBits.h"
Include dependency graph for InstCombineCasts.cpp:

Go to the source code of this file.

## Macros

#define DEBUG_TYPE   "instcombine"

## Functions

static ValuedecomposeSimpleLinearExpr (Value *Val, unsigned &Scale, uint64_t &Offset)
Analyze 'Val', seeing if it is a simple linear expression. More...

static bool canAlwaysEvaluateInType (Value *V, Type *Ty)
Constants and extensions/truncates from the destination type are always free to be evaluated in that type. More...

static bool canNotEvaluateInType (Value *V, Type *Ty)
Filter out values that we can not evaluate in the destination type for free. More...

static bool canEvaluateTruncated (Value *V, Type *Ty, InstCombiner &IC, Instruction *CxtI)
Return true if we can evaluate the specified expression tree as type Ty instead of its larger type, and arrive with the same value. More...

static InstructionfoldVecTruncToExtElt (TruncInst &Trunc, InstCombiner &IC)
Given a vector that is bitcast to an integer, optionally logically right-shifted, and truncated, convert it to an extractelement. More...

static InstructionshrinkSplatShuffle (TruncInst &Trunc, InstCombiner::BuilderTy &Builder)
Try to narrow the width of a splat shuffle. More...

static InstructionshrinkInsertElt (CastInst &Trunc, InstCombiner::BuilderTy &Builder)
Try to narrow the width of an insert element. More...

static bool canEvaluateZExtd (Value *V, Type *Ty, unsigned &BitsToClear, InstCombiner &IC, Instruction *CxtI)
Determine if the specified value can be computed in the specified wider type and produce the same low bits. More...

static bool canEvaluateSExtd (Value *V, Type *Ty)
Return true if we can take the specified value and return it as type Ty without inserting any new casts and without changing the value of the common low bits. More...

static bool fitsInFPType (ConstantFP *CFP, const fltSemantics &Sem)
Return a Constant* for the specified floating-point constant if it fits in the specified FP type without changing its value. More...

static TypeshrinkFPConstant (ConstantFP *CFP)

static TypeshrinkFPConstantVector (Value *V)

static TypegetMinimumFPType (Value *V)
Find the minimum FP type we can safely truncate to. More...

static InstructionoptimizeVectorResize (Value *InVal, VectorType *DestTy, InstCombiner &IC)
This input value (which is known to have vector type) is being zero extended or truncated to the specified vector type. More...

static bool isMultipleOfTypeSize (unsigned Value, Type *Ty)

static unsigned getTypeSizeIndex (unsigned Value, Type *Ty)

static bool collectInsertionElements (Value *V, unsigned Shift, SmallVectorImpl< Value *> &Elements, Type *VecEltTy, bool isBigEndian)
V is a value which is inserted into a vector of VecEltTy. More...

static ValueoptimizeIntegerToVectorInsertions (BitCastInst &CI, InstCombiner &IC)
If the input is an 'or' instruction, we may be doing shifts and ors to assemble the elements of the vector manually. More...

static InstructioncanonicalizeBitCastExtElt (BitCastInst &BitCast, InstCombiner &IC)
Canonicalize scalar bitcasts of extracted elements into a bitcast of the vector followed by extract element. More...

static InstructionfoldBitCastBitwiseLogic (BitCastInst &BitCast, InstCombiner::BuilderTy &Builder)
Change the type of a bitwise logic operation if we can eliminate a bitcast. More...

static InstructionfoldBitCastSelect (BitCastInst &BitCast, InstCombiner::BuilderTy &Builder)
Change the type of a select if we can eliminate a bitcast. More...

static bool hasStoreUsersOnly (CastInst &CI)
Check if all users of CI are StoreInsts. More...

## ◆ DEBUG_TYPE

 #define DEBUG_TYPE   "instcombine"

Definition at line 24 of file InstCombineCasts.cpp.

## ◆ canAlwaysEvaluateInType()

 static bool canAlwaysEvaluateInType ( Value * V, Type * Ty )
static

Constants and extensions/truncates from the destination type are always free to be evaluated in that type.

This is a helper for canEvaluate*.

Definition at line 305 of file InstCombineCasts.cpp.

Referenced by canEvaluateSExtd(), canEvaluateTruncated(), and canEvaluateZExtd().

## ◆ canEvaluateSExtd()

 static bool canEvaluateSExtd ( Value * V, Type * Ty )
static

Return true if we can take the specified value and return it as type Ty without inserting any new casts and without changing the value of the common low bits.

This is used by code that tries to promote integer operations to a wider types will allow us to eliminate the extension.

This function works on both vectors and scalars.

Definition at line 1312 of file InstCombineCasts.cpp.

Referenced by llvm::InstCombiner::visitSExt().

## ◆ canEvaluateTruncated()

 static bool canEvaluateTruncated ( Value * V, Type * Ty, InstCombiner & IC, Instruction * CxtI )
static

Return true if we can evaluate the specified expression tree as type Ty instead of its larger type, and arrive with the same value.

This is used by code that tries to eliminate truncates.

Ty will always be a type smaller than V. We should return true if trunc(V) can be computed by computing V in the smaller type. If V is an instruction, then trunc(inst(x,y)) can be computed as inst(trunc(x),trunc(y)), which only makes sense if x and y can be efficiently truncated.

This function works on both vectors and scalars.

Definition at line 341 of file InstCombineCasts.cpp.

Referenced by llvm::InstCombiner::visitTrunc().

## ◆ canEvaluateZExtd()

 static bool canEvaluateZExtd ( Value * V, Type * Ty, unsigned & BitsToClear, InstCombiner & IC, Instruction * CxtI )
static

Determine if the specified value can be computed in the specified wider type and produce the same low bits.

If not, return false.

If this function returns true, it can also return a non-zero number of bits (in BitsToClear) which indicates that the value it computes is correct for the zero extend, but that the additional BitsToClear bits need to be zero'd out. For example, to promote something like:

B = trunc i64 A to i32 C = lshr i32 B, 8 E = zext i32 C to i64

CanEvaluateZExtd for the 'lshr' will return true, and BitsToClear will be set to 8 to indicate that the promoted value needs to have bits 24-31 cleared in addition to bits 32-63. Since an 'and' will be generated to clear the top bits anyway, doing this has no extra cost.

This function works on both vectors and scalars.

Definition at line 982 of file InstCombineCasts.cpp.

Referenced by llvm::InstCombiner::visitZExt().

## ◆ canNotEvaluateInType()

 static bool canNotEvaluateInType ( Value * V, Type * Ty )
static

Filter out values that we can not evaluate in the destination type for free.

This is a helper for canEvaluate*.

Definition at line 318 of file InstCombineCasts.cpp.

References assert(), and llvm::Value::hasOneUse().

Referenced by canEvaluateSExtd(), canEvaluateTruncated(), and canEvaluateZExtd().

## ◆ canonicalizeBitCastExtElt()

 static Instruction* canonicalizeBitCastExtElt ( BitCastInst & BitCast, InstCombiner & IC )
static

Canonicalize scalar bitcasts of extracted elements into a bitcast of the vector followed by extract element.

The backend tends to handle bitcasts of vectors better than bitcasts of scalars because vector registers are usually not type-specific like scalar integer or scalar floating-point.

Definition at line 2029 of file InstCombineCasts.cpp.

Referenced by llvm::InstCombiner::visitBitCast().

## ◆ collectInsertionElements()

 static bool collectInsertionElements ( Value * V, unsigned Shift, SmallVectorImpl< Value *> & Elements, Type * VecEltTy, bool isBigEndian )
static

V is a value which is inserted into a vector of VecEltTy.

Look through the value to see if we can decompose it into insertions into the vector. See the example in the comment for OptimizeIntegerToVectorInsertions for the pattern this handles. The type of V is always a non-zero multiple of VecEltTy's size. Shift is the number of bits between the lsb of V and the lsb of the vector.

This returns false if the pattern can't be matched or true if it can, filling in Elements with the elements found here.

Definition at line 1891 of file InstCombineCasts.cpp.

Referenced by optimizeIntegerToVectorInsertions().

## ◆ decomposeSimpleLinearExpr()

 static Value* decomposeSimpleLinearExpr ( Value * Val, unsigned & Scale, uint64_t & Offset )
static

## ◆ fitsInFPType()

 static bool fitsInFPType ( ConstantFP * CFP, const fltSemantics & Sem )
static

Return a Constant* for the specified floating-point constant if it fits in the specified FP type without changing its value.

Definition at line 1451 of file InstCombineCasts.cpp.

Referenced by shrinkFPConstant().

## ◆ foldBitCastBitwiseLogic()

 static Instruction* foldBitCastBitwiseLogic ( BitCastInst & BitCast, InstCombiner::BuilderTy & Builder )
static

Change the type of a bitwise logic operation if we can eliminate a bitcast.

Definition at line 2050 of file InstCombineCasts.cpp.

Referenced by llvm::InstCombiner::visitBitCast().

## ◆ foldBitCastSelect()

 static Instruction* foldBitCastSelect ( BitCastInst & BitCast, InstCombiner::BuilderTy & Builder )
static

Change the type of a select if we can eliminate a bitcast.

Definition at line 2096 of file InstCombineCasts.cpp.

Referenced by llvm::InstCombiner::visitBitCast().

## ◆ foldVecTruncToExtElt()

 static Instruction* foldVecTruncToExtElt ( TruncInst & Trunc, InstCombiner & IC )
static

Given a vector that is bitcast to an integer, optionally logically right-shifted, and truncated, convert it to an extractelement.

Example (big endian): trunc (lshr (bitcast <4 x i32> X to i128), 32) to i32 —> extractelement <4 x i32> X, 1

Definition at line 456 of file InstCombineCasts.cpp.

Referenced by llvm::InstCombiner::visitTrunc().

## ◆ getMinimumFPType()

 static Type* getMinimumFPType ( Value * V )
static

Find the minimum FP type we can safely truncate to.

Definition at line 1506 of file InstCombineCasts.cpp.

References llvm::Value::getType(), shrinkFPConstant(), shrinkFPConstantVector(), and T.

Referenced by llvm::InstCombiner::visitFPTrunc().

## ◆ getTypeSizeIndex()

 static unsigned getTypeSizeIndex ( unsigned Value, Type * Ty )
static

Definition at line 1877 of file InstCombineCasts.cpp.

References llvm::Type::getPrimitiveSizeInBits().

Referenced by collectInsertionElements().

## ◆ hasStoreUsersOnly()

 static bool hasStoreUsersOnly ( CastInst & CI )
static

Check if all users of CI are StoreInsts.

Definition at line 2140 of file InstCombineCasts.cpp.

## ◆ isMultipleOfTypeSize()

 static bool isMultipleOfTypeSize ( unsigned Value, Type * Ty )
static

Definition at line 1873 of file InstCombineCasts.cpp.

References llvm::Type::getPrimitiveSizeInBits().

Referenced by collectInsertionElements().

## ◆ optimizeIntegerToVectorInsertions()

 static Value* optimizeIntegerToVectorInsertions ( BitCastInst & CI, InstCombiner & IC )
static

If the input is an 'or' instruction, we may be doing shifts and ors to assemble the elements of the vector manually.

Try to rip the code out and replace it with insertelements. This is to optimize code like this:

tmp37 = bitcast float inc to i32 tmp38 = zext i32 tmp37 to i64 tmp31 = bitcast float inc5 to i32 tmp32 = zext i32 tmp31 to i64 tmp33 = shl i64 tmp32, 32 ins35 = or i64 tmp33, tmp38 tmp43 = bitcast i64 ins35 to <2 x float>

Into two insertelements that do "buildvector{%inc, %inc5}".

Definition at line 2000 of file InstCombineCasts.cpp.

Referenced by llvm::InstCombiner::visitBitCast().

## ◆ optimizeVectorResize()

 static Instruction* optimizeVectorResize ( Value * InVal, VectorType * DestTy, InstCombiner & IC )
static

This input value (which is known to have vector type) is being zero extended or truncated to the specified vector type.

Try to replace it with a shuffle (and vector/vector bitcast) if possible.

The source and destination vector types may have different element types.

Definition at line 1821 of file InstCombineCasts.cpp.

Referenced by llvm::InstCombiner::visitBitCast().

## ◆ shrinkFPConstant()

 static Type* shrinkFPConstant ( ConstantFP * CFP )
static

Definition at line 1458 of file InstCombineCasts.cpp.

Referenced by getMinimumFPType(), and shrinkFPConstantVector().

## ◆ shrinkFPConstantVector()

 static Type* shrinkFPConstantVector ( Value * V )
static

Definition at line 1478 of file InstCombineCasts.cpp.

Referenced by getMinimumFPType().

## ◆ shrinkInsertElt()

 static Instruction* shrinkInsertElt ( CastInst & Trunc, InstCombiner::BuilderTy & Builder )
static

Try to narrow the width of an insert element.

This could be generalized for any vector constant, but we limit the transform to insertion into undef to avoid potential backend problems from unsupported insertion widths. This could also be extended to handle the case of inserting a scalar constant into a vector variable.

Definition at line 657 of file InstCombineCasts.cpp.

Referenced by llvm::InstCombiner::visitFPTrunc(), and llvm::InstCombiner::visitTrunc().

## ◆ shrinkSplatShuffle()

 static Instruction* shrinkSplatShuffle ( TruncInst & Trunc, InstCombiner::BuilderTy & Builder )
static

Try to narrow the width of a splat shuffle.

This could be generalized to any shuffle with a constant operand, but we limit the transform to avoid creating a shuffle type that targets may not be able to lower effectively.

Definition at line 637 of file InstCombineCasts.cpp.

Referenced by llvm::InstCombiner::visitTrunc().