LLVM: lib/Transforms/InstCombine/InstructionCombining.cpp File Reference (original) (raw)

Go to the source code of this file.

Functions
STATISTIC (NumWorklistIterations, "Number of instruction combining iterations performed")
STATISTIC (NumOneIteration, "Number of functions with one iteration")
STATISTIC (NumTwoIterations, "Number of functions with two iterations")
STATISTIC (NumThreeIterations, "Number of functions with three iterations")
STATISTIC (NumFourOrMoreIterations, "Number of functions with four or more iterations")
STATISTIC (NumCombined, "Number of insts combined")
STATISTIC (NumConstProp, "Number of constant folds")
STATISTIC (NumDeadInst, "Number of dead inst eliminated")
STATISTIC (NumSunkInst, "Number of instructions sunk")
STATISTIC (NumExpand, "Number of expansions")
STATISTIC (NumFactor, "Number of factorizations")
STATISTIC (NumReassoc, "Number of reassociations")
DEBUG_COUNTER (VisitCounter, "instcombine-visit", "Controls which instructions are visited")
static bool maintainNoSignedWrap (BinaryOperator &I, Value *B, Value *C)
static bool hasNoUnsignedWrap (BinaryOperator &I)
static bool hasNoSignedWrap (BinaryOperator &I)
static void ClearSubclassDataAfterReassociation (BinaryOperator &I)
Conservatively clears subclassOptionalData after a reassociation or commutation.
static bool simplifyAssocCastAssoc (BinaryOperator *BinOp1, InstCombinerImpl &IC)
Combine constant operands of associative operations either before or after a cast to eliminate one of the associative operations: (op (cast (op X, C2)), C1) --> (cast (op X, op (C1, C2))) (op (cast (op X, C2)), C1) --> (op (cast X), op (C1, C2))
static bool leftDistributesOverRight (Instruction::BinaryOps LOp, Instruction::BinaryOps ROp)
Return whether "X LOp (Y ROp Z)" is always equal to "(X LOp Y) ROp (X LOp Z)".
static bool rightDistributesOverLeft (Instruction::BinaryOps LOp, Instruction::BinaryOps ROp)
Return whether "(X LOp Y) ROp Z" is always equal to "(X ROp Z) LOp (Y ROp Z)".
static Value * getIdentityValue (Instruction::BinaryOps Opcode, Value *V)
This function returns identity value for given opcode, which can be used to factor patterns like (X * 2) + X ==> (X * 2) + (X * 1) ==> X * (2 + 1).
static Instruction::BinaryOps getBinOpsForFactorization (Instruction::BinaryOps TopOpcode, BinaryOperator *Op, Value *&LHS, Value *&RHS, BinaryOperator *OtherOp)
This function predicates factorization using distributive laws.
static Value * tryFactorization (BinaryOperator &I, const SimplifyQuery &SQ, InstCombiner::BuilderTy &Builder, Instruction::BinaryOps InnerOpcode, Value *A, Value *B, Value *C, Value *D)
This tries to simplify binary operations by factorizing out common terms (e.
static std::optional< std::pair< Value *, Value * > > matchSymmetricPhiNodesPair (PHINode *LHS, PHINode *RHS)
static Value * simplifyOperationIntoSelectOperand (Instruction &I, SelectInst *SI, bool IsTrueArm)
static Value * foldOperationIntoSelectOperand (Instruction &I, SelectInst *SI, Value *NewOp, InstCombiner &IC)
static Value * simplifyInstructionWithPHI (Instruction &I, PHINode *PN, Value *InValue, BasicBlock *InBB, const DataLayout &DL, const SimplifyQuery SQ)
static bool shouldMergeGEPs (GEPOperator &GEP, GEPOperator &Src)
static GEPNoWrapFlags getMergedGEPNoWrapFlags (GEPOperator &GEP1, GEPOperator &GEP2)
Determine nowrap flags for (gep (gep p, x), y) to (gep p, (x + y)) transform.
static Instruction * foldSelectGEP (GetElementPtrInst &GEP, InstCombiner::BuilderTy &Builder)
Thread a GEP operation with constant indices through the constant true/false arms of a select.
static Instruction * canonicalizeGEPOfConstGEPI8 (GetElementPtrInst &GEP, GEPOperator *Src, InstCombinerImpl &IC)
static bool shouldCanonicalizeGEPToPtrAdd (GetElementPtrInst &GEP)
Return true if we should canonicalize the gep to an i8 ptradd.
static Instruction * foldGEPOfPhi (GetElementPtrInst &GEP, PHINode *PN, IRBuilderBase &Builder)
static bool isNeverEqualToUnescapedAlloc (Value *V, const TargetLibraryInfo &TLI, Instruction *AI)
static bool isRemovableWrite (CallBase &CB, Value *UsedV, const TargetLibraryInfo &TLI)
Given a call CB which uses an address UsedV, return true if we can prove the call's only possible effect is storing to V.
static bool isAllocSiteRemovable (Instruction *AI, SmallVectorImpl< WeakTrackingVH > &Users, const TargetLibraryInfo &TLI)
static Instruction * tryToMoveFreeBeforeNullTest (CallInst &FI, const DataLayout &DL)
Move the call to free before a NULL test.
static Value * simplifySwitchOnSelectUsingRanges (SwitchInst &SI, SelectInst *Select, bool IsTrueArm)
static bool isCatchAll (EHPersonality Personality, Constant *TypeInfo)
Return 'true' if the given typeinfo will match anything.
static bool shorter_filter (const Value *LHS, const Value *RHS)
static bool isUsedWithinShuffleVector (Value *V)
static bool SoleWriteToDeadLocal (Instruction *I, TargetLibraryInfo &TLI)
Check for case where the call writes to an otherwise dead alloca.
static bool combineInstructionsOverFunction (Function &F, InstructionWorklist &Worklist, AliasAnalysis *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, TargetTransformInfo &TTI, DominatorTree &DT, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI, const InstCombineOptions &Opts)
INITIALIZE_PASS_BEGIN (InstructionCombiningPass, "instcombine", "Combine redundant instructions", false, false) INITIALIZE_PASS_END(InstructionCombiningPass
Variables
static cl::opt< bool > EnableCodeSinking ("instcombine-code-sinking", cl::desc("Enable code sinking"), cl::init(true))
static cl::opt< unsigned > MaxSinkNumUsers ("instcombine-max-sink-users", cl::init(32), cl::desc("Maximum number of undroppable users for instruction sinking"))
static cl::opt< unsigned > MaxArraySize ("instcombine-maxarray-size", cl::init(1024), cl::desc("Maximum array size considered when doing a combine"))
static cl::opt< unsigned > ShouldLowerDbgDeclare ("instcombine-lower-dbg-declare", cl::Hidden, cl::init(true))
instcombine
Combine redundant instructions
Combine redundant false

DEBUG_TYPE

#define DEBUG_TYPE "instcombine"

canonicalizeGEPOfConstGEPI8()

Definition at line 2412 of file InstructionCombining.cpp.

References llvm::sampleprof::Base, llvm::InstCombiner::Builder, llvm::GetElementPtrInst::Create(), llvm::IRBuilderBase::CreatePtrAdd(), DL, GEP, llvm::APInt::getBitWidth(), llvm::InstCombiner::getDataLayout(), llvm::IRBuilderBase::getInt(), llvm::APInt::isZero(), llvm::PatternMatch::m_AddLike(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_PtrAdd(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().

Referenced by llvm::InstCombinerImpl::visitGEPOfGEP().

ClearSubclassDataAfterReassociation()

static void ClearSubclassDataAfterReassociation ( BinaryOperator & I) static

combineInstructionsOverFunction()

static bool combineInstructionsOverFunction ( Function & F, InstructionWorklist & Worklist, AliasAnalysis * AA, AssumptionCache & AC, TargetLibraryInfo & TLI, TargetTransformInfo & TTI, DominatorTree & DT, OptimizationRemarkEmitter & ORE, BlockFrequencyInfo * BFI, BranchProbabilityInfo * BPI, ProfileSummaryInfo * PSI, const InstCombineOptions & Opts ) static

Builder - This is an IRBuilder that automatically inserts new instructions into the worklist when they are created.

Definition at line 5508 of file InstructionCombining.cpp.

References llvm::InstructionWorklist::add(), llvm::dbgs(), DL, F, I, LLVM_DEBUG, llvm::LowerDbgDeclare(), MaxArraySize, llvm::InstCombiner::MaxArraySizeForCombine, llvm::InstCombineOptions::MaxIterations, llvm::InstCombinerImpl::prepareWorklist(), llvm::AssumptionCache::registerAssumption(), llvm::report_fatal_error(), llvm::InstCombinerImpl::run(), ShouldLowerDbgDeclare, and llvm::InstCombineOptions::VerifyFixpoint.

Referenced by llvm::InstCombinePass::run(), and llvm::InstructionCombiningPass::runOnFunction().

DEBUG_COUNTER()

DEBUG_COUNTER ( VisitCounter ,
"instcombine-visit" ,
"Controls which instructions are visited"
)

foldGEPOfPhi()

Definition at line 2792 of file InstructionCombining.cpp.

References llvm::PHINode::addIncoming(), assert(), llvm::IRBuilderBase::CreatePHI(), F, GEP, llvm::PHINode::getIncomingBlock(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::GetElementPtrInst::getTypeAtIndex(), llvm::Value::hasOneUse(), I, llvm::Type::isStructTy(), llvm::User::op_begin(), llvm::User::op_end(), llvm::User::operands(), and llvm::IRBuilderBase::SetInsertPoint().

Referenced by llvm::InstCombinerImpl::visitGetElementPtrInst().

foldOperationIntoSelectOperand()

foldSelectGEP()

getBinOpsForFactorization()

This function predicates factorization using distributive laws.

By default, it just returns the 'Op' inputs. But for special-cases like 'add(shl(X, 5), ...)', this function will have TopOpcode == Instruction::Add and Op = shl(X, 5). The 'shl' is treated as the more general 'mul X, 32' to allow more factorization opportunities.

Definition at line 646 of file InstructionCombining.cpp.

References assert(), llvm::CallingConv::C, llvm::ConstantFoldBinaryInstruction(), llvm::BinaryOperator::getOpcode(), llvm::Instruction::isBitwiseLogicOp(), LHS, llvm::PatternMatch::m_ImmConstant(), llvm::PatternMatch::m_LShr(), llvm::PatternMatch::m_NonNegative(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and RHS.

Referenced by llvm::InstCombinerImpl::tryFactorizationFolds().

getIdentityValue()

getMergedGEPNoWrapFlags()

hasNoSignedWrap()

hasNoUnsignedWrap()

INITIALIZE_PASS_BEGIN()

isAllocSiteRemovable()

Definition at line 3207 of file InstructionCombining.cpp.

References assert(), llvm::SmallVectorBase< Size_T >::empty(), llvm::getAllocationFamily(), llvm::getFreedOperand(), llvm::TargetLibraryInfo::getLibFunc(), llvm::User::getOperand(), llvm::getReallocatedOperand(), llvm::TargetLibraryInfo::has(), I, II, llvm::ICmpInst::isEquality(), isNeverEqualToUnescapedAlloc(), llvm::APInt::isPowerOf2(), isRemovableWrite(), llvm_unreachable, llvm::PatternMatch::m_APInt(), llvm::PatternMatch::match(), MI, llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), Size, llvm::Value::users(), and Users.

Referenced by llvm::InstCombinerImpl::visitAllocSite().

isCatchAll()

Return 'true' if the given typeinfo will match anything.

Definition at line 4193 of file InstructionCombining.cpp.

References llvm::CoreCLR, llvm::GNU_Ada, llvm::GNU_C, llvm::GNU_C_SjLj, llvm::GNU_CXX, llvm::GNU_CXX_SjLj, llvm::GNU_ObjC, llvm::Constant::isNullValue(), llvm_unreachable, llvm::MSVC_CXX, llvm::MSVC_TableSEH, llvm::MSVC_X86SEH, llvm::Rust, llvm::Unknown, llvm::Wasm_CXX, llvm::XL_CXX, and llvm::ZOS_CXX.

Referenced by llvm::InstCombinerImpl::visitLandingPadInst().

isNeverEqualToUnescapedAlloc()

isRemovableWrite()

isUsedWithinShuffleVector()

static bool isUsedWithinShuffleVector ( Value * V) static

leftDistributesOverRight()

Return whether "X LOp (Y ROp Z)" is always equal to "(X LOp Y) ROp (X LOp Z)".

Definition at line 597 of file InstructionCombining.cpp.

maintainNoSignedWrap()

matchSymmetricPhiNodesPair()

static std::optional< std::pair< Value *, Value * > > matchSymmetricPhiNodesPair ( PHINode * LHS, PHINode * RHS ) static

rightDistributesOverLeft()

shorter_filter()

shouldCanonicalizeGEPToPtrAdd()

Return true if we should canonicalize the gep to an i8 ptradd.

Definition at line 2763 of file InstructionCombining.cpp.

References llvm::any_of(), GEP, llvm::Type::isIntegerTy(), llvm::Type::isScalableTy(), llvm::PatternMatch::m_CombineOr(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_Mul(), llvm::PatternMatch::m_OneUse(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().

Referenced by llvm::InstCombinerImpl::visitGetElementPtrInst().

shouldMergeGEPs()

simplifyAssocCastAssoc()

Combine constant operands of associative operations either before or after a cast to eliminate one of the associative operations: (op (cast (op X, C2)), C1) --> (cast (op X, op (C1, C2))) (op (cast (op X, C2)), C1) --> (op (cast X), op (C1, C2))

Definition at line 343 of file InstructionCombining.cpp.

References llvm::ConstantFoldBinaryOpOperands(), llvm::ConstantFoldCastOperand(), DL, llvm::Instruction::dropPoisonGeneratingFlags(), llvm::InstCombiner::getDataLayout(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::Value::getType(), llvm::Instruction::isBitwiseLogicOp(), llvm::PatternMatch::m_Constant(), llvm::PatternMatch::match(), and llvm::InstCombiner::replaceOperand().

Referenced by llvm::InstCombinerImpl::SimplifyAssociativeOrCommutative().

simplifyInstructionWithPHI()

Definition at line 1730 of file InstructionCombining.cpp.

References DL, llvm::ConstantInt::getBool(), llvm::ICmpInst::getCmpPredicate(), llvm::BranchInst::getCondition(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::SimplifyQuery::getWithInstruction(), I, llvm::BranchInst::isConditional(), llvm::isImpliedCondition(), llvm::PatternMatch::m_ConstantExpr(), llvm::PatternMatch::match(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), and llvm::simplifyInstructionWithOperands().

Referenced by llvm::InstCombinerImpl::foldOpIntoPhi().

simplifyOperationIntoSelectOperand()

Definition at line 1656 of file InstructionCombining.cpp.

References I, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::isGuaranteedNotToBeUndefOrPoison(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_SpecificICmp(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), and llvm::simplifyInstructionWithOperands().

Referenced by llvm::InstCombinerImpl::FoldOpIntoSelect().

simplifySwitchOnSelectUsingRanges()

SoleWriteToDeadLocal()

STATISTIC() [1/12]

STATISTIC ( NumCombined ,
"Number of insts combined"
)

STATISTIC() [2/12]

STATISTIC ( NumConstProp ,
"Number of constant folds"
)

STATISTIC() [3/12]

STATISTIC ( NumDeadInst ,
"Number of dead inst eliminated"
)

STATISTIC() [4/12]

STATISTIC ( NumExpand ,
"Number of expansions"
)

STATISTIC() [5/12]

STATISTIC ( NumFactor ,
"Number of factorizations"
)

STATISTIC() [6/12]

STATISTIC ( NumFourOrMoreIterations ,
"Number of functions with four or more iterations"
)

STATISTIC() [7/12]

STATISTIC ( NumOneIteration ,
"Number of functions with one iteration"
)

STATISTIC() [8/12]

STATISTIC ( NumReassoc ,
"Number of reassociations"
)

STATISTIC() [9/12]

STATISTIC() [10/12]

STATISTIC ( NumThreeIterations ,
"Number of functions with three iterations"
)

STATISTIC() [11/12]

STATISTIC ( NumTwoIterations ,
"Number of functions with two iterations"
)

STATISTIC() [12/12]

STATISTIC ( NumWorklistIterations ,
"Number of instruction combining iterations performed"
)

tryFactorization()

This tries to simplify binary operations by factorizing out common terms (e.

g. "(A*B)+(A*C)" -> "A*(B+C)").

Definition at line 674 of file InstructionCombining.cpp.

References A, assert(), B, llvm::CallingConv::C, llvm::IRBuilderBase::CreateBinOp(), D, llvm::Value::getName(), llvm::SimplifyQuery::getWithInstruction(), llvm::Value::hasOneUse(), I, llvm::Instruction::isCommutative(), llvm::APInt::isMinSignedValue(), leftDistributesOverRight(), LHS, llvm::PatternMatch::m_APInt(), llvm::PatternMatch::match(), RHS, rightDistributesOverLeft(), llvm::simplifyBinOp(), std::swap(), and llvm::Value::takeName().

Referenced by llvm::InstCombinerImpl::tryFactorizationFolds().

tryToMoveFreeBeforeNullTest()

Move the call to free before a NULL test.

Check if this free is accessed after its argument has been test against NULL (property 0). If yes, it is legal to move this call in its predecessor block.

The move is performed only if the block containing the call to free will be removed, i.e.:

  1. it has only one predecessor P, and P has two successors
  2. it contains the call, noops, and an unconditional branch
  3. its successor is the same as its predecessor's successor

The profitability is out-of concern here and this function should be called only if the caller knows this transformation would be profitable (e.g., for code size).

Definition at line 3463 of file InstructionCombining.cpp.

References assert(), DL, llvm::CallBase::getArgOperand(), llvm::CallBase::getAttributes(), llvm::Value::getContext(), llvm::Attribute::getDereferenceableBytes(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), llvm::BasicBlock::getSinglePredecessor(), llvm::BasicBlock::getTerminator(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::BasicBlock::instructionsWithoutDebug(), llvm::Attribute::isValid(), llvm::PatternMatch::m_Br(), llvm::PatternMatch::m_CombineOr(), llvm::PatternMatch::m_ICmp(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_UnconditionalBr(), llvm::PatternMatch::m_Zero(), llvm::make_early_inc_range(), llvm::PatternMatch::match(), llvm::CallBase::setAttributes(), and llvm::BasicBlock::size().

Referenced by llvm::InstCombinerImpl::visitFree().

EnableCodeSinking

cl::opt< bool > EnableCodeSinking("instcombine-code-sinking", cl::desc("Enable code sinking"), cl::init(true)) ( "instcombine-code-sinking" , cl::desc("Enable code sinking") , cl::init(true) ) static

false

instructions

MaxArraySize

cl::opt< unsigned > MaxArraySize("instcombine-maxarray-size", cl::init(1024), cl::desc("Maximum array size considered when doing a combine")) ( "instcombine-maxarray-size" , cl::init(1024) , cl::desc("Maximum array size considered when doing a combine") ) static

MaxSinkNumUsers

cl::opt< unsigned > MaxSinkNumUsers("instcombine-max-sink-users", cl::init(32), cl::desc("Maximum number of undroppable users for instruction sinking")) ( "instcombine-max-sink-users" , cl::init(32) , cl::desc("Maximum number of undroppable users for instruction sinking") ) static

ShouldLowerDbgDeclare

cl::opt< unsigned > ShouldLowerDbgDeclare("instcombine-lower-dbg-declare", cl::Hidden, cl::init(true)) ( "instcombine-lower-dbg-declare" , cl::Hidden , cl::init(true) ) static