|
LLVM 24.0.0git
|
#include "InstCombineInternal.h"#include "llvm/ADT/APFloat.h"#include "llvm/ADT/APInt.h"#include "llvm/ADT/STLExtras.h"#include "llvm/ADT/SmallVector.h"#include "llvm/Analysis/InstructionSimplify.h"#include "llvm/Analysis/ValueTracking.h"#include "llvm/IR/Constant.h"#include "llvm/IR/Constants.h"#include "llvm/IR/InstrTypes.h"#include "llvm/IR/Instruction.h"#include "llvm/IR/Instructions.h"#include "llvm/IR/Operator.h"#include "llvm/IR/PatternMatch.h"#include "llvm/IR/ProfDataUtils.h"#include "llvm/IR/Type.h"#include "llvm/IR/Value.h"#include "llvm/Support/AlignOf.h"#include "llvm/Support/Casting.h"#include "llvm/Support/KnownBits.h"#include "llvm/Transforms/InstCombine/InstCombiner.h"#include <cassert>#include <utility>Go to the source code of this file.
Namespaces | |
| namespace | llvm |
| This is an optimization pass for GlobalISel generic memory operations. | |
Macros | |
| #define | DEBUG_TYPE "instcombine" |
Functions | |
| static Value * | checkForNegativeOperand (BinaryOperator &I, InstCombiner::BuilderTy &Builder) |
| static Instruction * | foldNoWrapAdd (BinaryOperator &Add, InstCombiner::BuilderTy &Builder) |
| Wrapping flags may allow combining constants separated by an extend. | |
| template<bool FP, typename Mul2Rhs> | |
| static bool | matchesSquareSum (BinaryOperator &I, Mul2Rhs M2Rhs, Value *&A, Value *&B) |
| static bool | MatchMul (Value *E, Value *&Op, APInt &C) |
| static bool | MatchRem (Value *E, Value *&Op, APInt &C, bool &IsSigned) |
| static bool | MatchDiv (Value *E, Value *&Op, APInt &C, bool IsSigned) |
| static bool | MulWillOverflow (APInt &C0, APInt &C1, bool IsSigned) |
| static Instruction * | canonicalizeLowbitMask (BinaryOperator &I, InstCombiner::BuilderTy &Builder) |
| Fold (1 << NBits) - 1 Into: ~(-(1 << NBits)) Because a 'not' is better for bit-tracking analysis and other transforms than an 'add'. | |
| static Instruction * | foldToUnsignedSaturatedAdd (BinaryOperator &I) |
| static Instruction * | combineAddSubWithShlAddSub (InstCombiner::BuilderTy &Builder, const BinaryOperator &I) |
| static Instruction * | foldAddToAshr (BinaryOperator &Add) |
| Try to reduce signed division by power-of-2 to an arithmetic shift right. | |
| static Instruction * | factorizeMathWithShlOps (BinaryOperator &I, InstCombiner::BuilderTy &Builder) |
| This is a specialization of a more general transform from foldUsingDistributiveLaws. | |
| static Instruction * | foldBoxMultiply (BinaryOperator &I) |
| Reduce a sequence of masked half-width multiplies to a single multiply. | |
| static bool | checkDivCeilNUW (Value *X, Value *Y, const SimplifyQuery &SQ) |
| Return true if X + (Y-1) is provably non-wrapping in X's type. | |
| static Instruction * | factorizeLerp (BinaryOperator &I, InstCombiner::BuilderTy &Builder) |
| Eliminate an op from a linear interpolation (lerp) pattern. | |
| static Instruction * | factorizeFAddFSub (BinaryOperator &I, InstCombiner::BuilderTy &Builder) |
| Factor a common operand out of fadd/fsub of fmul/fdiv. | |
| static Instruction * | foldSubOfMinMax (BinaryOperator &I, InstCombiner::BuilderTy &Builder) |
| static Instruction * | foldSubNUWOfMaskAndZExt (BinaryOperator &I, InstCombiner::BuilderTy &Builder) |
| sub nuw (and X, LowMask), (zext Y) --> zext (sub nuw (trunc X), Y) sub nuw (zext Y), (and X, LowMask) --> zext (sub nuw Y, (trunc X)) where LowMask has exactly as many low bits set as Y's width, so the 'and' is zext(trunc X) and the wide nuw implies the narrow sub does not wrap. | |
| static Instruction * | foldFNegIntoConstant (Instruction &I, const DataLayout &DL) |
| This eliminates floating-point negation in either 'fneg(X)' or 'fsub(-0.0, X)' form by combining into a constant operand. | |
| #define DEBUG_TYPE "instcombine" |
Definition at line 40 of file InstCombineAddSub.cpp.
|
static |
Fold (1 << NBits) - 1 Into: ~(-(1 << NBits)) Because a 'not' is better for bit-tracking analysis and other transforms than an 'add'.
The new shl is always nsw, and is nuw if old and was.
Definition at line 1232 of file InstCombineAddSub.cpp.
References llvm::BinaryOperator::CreateNot(), llvm::dyn_cast(), llvm::Constant::getAllOnesValue(), llvm::Value::getType(), I, llvm::PatternMatch::m_Add(), llvm::MIPatternMatch::m_AllOnes(), llvm::PatternMatch::m_One(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().
Referenced by llvm::InstCombinerImpl::visitAdd().
|
static |
Return true if X + (Y-1) is provably non-wrapping in X's type.
Definition at line 1550 of file InstCombineAddSub.cpp.
References llvm::computeConstantRange(), llvm::ConstantRange::getUnsignedMax(), llvm::ConstantRange::getUnsignedMin(), llvm::APInt::isZero(), llvm::APInt::ugt(), X, and Y.
Referenced by llvm::InstCombinerImpl::foldDivCeil().
|
static |
Definition at line 757 of file InstCombineAddSub.cpp.
References C1, I, LHS, llvm::PatternMatch::m_Add(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_One(), llvm::PatternMatch::m_Or(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_Xor(), llvm::PatternMatch::match(), RHS, std::swap(), X, and Y.
Referenced by llvm::InstCombinerImpl::visitAdd().
|
static |
Definition at line 1276 of file InstCombineAddSub.cpp.
References A(), B(), I, llvm::PatternMatch::m_c_Add(), llvm::MIPatternMatch::m_Neg(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().
Referenced by llvm::InstCombinerImpl::visitAdd().
|
static |
Factor a common operand out of fadd/fsub of fmul/fdiv.
Definition at line 2073 of file InstCombineAddSub.cpp.
References assert(), C(), llvm::BinaryOperator::CreateFDivFMF(), llvm::BinaryOperator::CreateFMulFMF(), factorizeLerp(), llvm::Value::hasOneUse(), I, llvm::PatternMatch::m_APFloat(), llvm::PatternMatch::m_c_FMul(), llvm::PatternMatch::m_FDiv(), llvm::PatternMatch::m_FMul(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), X, and Y.
Referenced by llvm::InstCombinerImpl::visitFAdd(), and llvm::InstCombinerImpl::visitFSub().
|
static |
Eliminate an op from a linear interpolation (lerp) pattern.
Definition at line 2057 of file InstCombineAddSub.cpp.
References llvm::BinaryOperator::CreateFAddFMF(), I, llvm::PatternMatch::m_c_FAdd(), llvm::PatternMatch::m_c_FMul(), llvm::PatternMatch::m_Deferred(), llvm::PatternMatch::m_FPOne(), llvm::PatternMatch::m_FSub(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), X, and Y.
Referenced by factorizeFAddFSub().
|
static |
This is a specialization of a more general transform from foldUsingDistributiveLaws.
If that code can be made to work optimally for multi-use cases or propagating nsw/nuw, then we would not need this.
Definition at line 1474 of file InstCombineAddSub.cpp.
References assert(), llvm::dyn_cast(), I, llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), X, and Y.
Referenced by llvm::InstCombinerImpl::visitAdd(), and llvm::InstCombinerImpl::visitSub().
|
static |
Try to reduce signed division by power-of-2 to an arithmetic shift right.
Definition at line 1289 of file InstCombineAddSub.cpp.
References llvm::Add, llvm::APInt::exactLogBase2(), llvm::APInt::getSignedMinValue(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_UGT, llvm::APInt::isNegative(), llvm::APInt::isSignMask(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_ICmp(), llvm::PatternMatch::m_Power2(), llvm::PatternMatch::m_SDiv(), llvm::PatternMatch::m_SExt(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), llvm::SMin, and X.
Referenced by llvm::InstCombinerImpl::visitAdd().
|
static |
Reduce a sequence of masked half-width multiplies to a single multiply.
((XLow * YHigh) + (YLow * XHigh)) << HalfBits) + (XLow * YLow) --> X * Y
Definition at line 1510 of file InstCombineAddSub.cpp.
References llvm::BitWidth, llvm::APInt::getMaxValue(), I, llvm::PatternMatch::m_And(), llvm::PatternMatch::m_c_Add(), llvm::PatternMatch::m_c_Mul(), llvm::PatternMatchHelpers::m_CombineOr(), llvm::PatternMatch::m_LShr(), llvm::PatternMatch::m_Mul(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_SpecificInt(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), X, and Y.
Referenced by llvm::InstCombinerImpl::visitAdd().
|
static |
This eliminates floating-point negation in either 'fneg(X)' or 'fsub(-0.0, X)' form by combining into a constant operand.
Definition at line 3144 of file InstCombineAddSub.cpp.
References C(), llvm::ConstantFoldUnaryOpOperand(), llvm::Instruction::copyMetadata(), llvm::BinaryOperator::CreateFDivFMF(), llvm::BinaryOperator::CreateFMulFMF(), llvm::BinaryOperator::CreateFSubFMF(), DL, llvm::Instruction::getFastMathFlags(), I, llvm::FastMathFlags::intersectRewrite(), llvm::PatternMatch::m_Constant(), llvm::PatternMatch::m_FAdd(), llvm::PatternMatch::m_FDiv(), llvm::PatternMatch::m_FMul(), llvm::PatternMatch::m_FNegOrFSubNegZero(), llvm::PatternMatch::m_Instruction(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), llvm::FastMathFlags::noInfs(), llvm::FastMathFlags::noSignedZeros(), llvm::Instruction::setHasNoInfs(), llvm::Instruction::setHasNoSignedZeros(), llvm::FastMathFlags::setNoInfs(), llvm::FastMathFlags::unionValue(), and X.
Referenced by llvm::InstCombinerImpl::visitFNeg(), and llvm::InstCombinerImpl::visitFSub().
|
static |
Wrapping flags may allow combining constants separated by an extend.
Definition at line 814 of file InstCombineAddSub.cpp.
References llvm::Add, C1, llvm::APInt::getBitWidth(), llvm::Value::hasOneUse(), llvm::APInt::isZero(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_Constant(), llvm::PatternMatch::m_NSWAddLike(), llvm::PatternMatch::m_NUWAddLike(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_SExtLike(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_ZExt(), llvm::PatternMatch::match(), llvm::APInt::sext(), and X.
Referenced by llvm::InstCombinerImpl::visitAdd().
|
static |
sub nuw (and X, LowMask), (zext Y) --> zext (sub nuw (trunc X), Y) sub nuw (zext Y), (and X, LowMask) --> zext (sub nuw Y, (trunc X)) where LowMask has exactly as many low bits set as Y's width, so the 'and' is zext(trunc X) and the wide nuw implies the narrow sub does not wrap.
Definition at line 2421 of file InstCombineAddSub.cpp.
References I, llvm::PatternMatch::m_And(), llvm::PatternMatch::m_LowBitMask(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Sub(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_ZExt(), llvm::PatternMatch::match(), X, and Y.
Referenced by llvm::InstCombinerImpl::visitSub().
|
static |
Definition at line 2371 of file InstCombineAddSub.cpp.
References llvm::CallInst::Create(), llvm::dyn_cast(), F, llvm::getInverseMinMaxIntrinsic(), llvm::Intrinsic::getOrInsertDeclaration(), llvm::Value::hasOneUse(), I, llvm::PatternMatch::m_c_Add(), llvm::PatternMatch::m_NSWSub(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_UMin(), llvm::PatternMatch::m_Value(), llvm::MIPatternMatch::m_ZeroInt(), llvm::PatternMatch::match(), X, and Y.
Referenced by llvm::InstCombinerImpl::visitSub().
|
static |
Definition at line 1250 of file InstCombineAddSub.cpp.
References assert(), C(), llvm::CallInst::Create(), llvm::Intrinsic::getOrInsertDeclaration(), I, llvm::PatternMatch::m_Add(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_c_Add(), llvm::PatternMatch::m_c_UMin(), llvm::PatternMatch::m_Deferred(), llvm::MIPatternMatch::m_Not(), llvm::PatternMatch::m_UMin(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), X, and Y.
Referenced by llvm::InstCombinerImpl::visitAdd().
Definition at line 1129 of file InstCombineAddSub.cpp.
References C(), E(), llvm::APInt::getBitWidth(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_LShr(), llvm::PatternMatch::m_SDiv(), llvm::PatternMatch::m_UDiv(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().
Referenced by llvm::InstCombinerImpl::SimplifyAddWithRemainder().
|
static |
Definition at line 1032 of file InstCombineAddSub.cpp.
References A(), B(), FP, I, llvm::PatternMatch::m_BinOp(), llvm::PatternMatch::m_c_BinOp(), llvm::PatternMatchHelpers::m_CombineOr(), llvm::PatternMatch::m_Deferred(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().
Referenced by llvm::InstCombinerImpl::foldSquareSumFP(), and llvm::InstCombinerImpl::foldSquareSumInt().
Definition at line 1089 of file InstCombineAddSub.cpp.
References C(), E(), llvm::APInt::getBitWidth(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_Mul(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().
Referenced by llvm::InstCombinerImpl::SimplifyAddWithRemainder().
Definition at line 1107 of file InstCombineAddSub.cpp.
References C(), E(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_SRem(), llvm::PatternMatch::m_URem(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().
Referenced by llvm::InstCombinerImpl::SimplifyAddWithRemainder().
Definition at line 1150 of file InstCombineAddSub.cpp.
Referenced by llvm::InstCombinerImpl::SimplifyAddWithRemainder().