|
LLVM
5.0.0
|
#include "InstCombineInternal.h"#include "llvm-c/Initialization.h"#include "llvm/ADT/SmallPtrSet.h"#include "llvm/ADT/Statistic.h"#include "llvm/ADT/StringSwitch.h"#include "llvm/Analysis/AliasAnalysis.h"#include "llvm/Analysis/AssumptionCache.h"#include "llvm/Analysis/BasicAliasAnalysis.h"#include "llvm/Analysis/CFG.h"#include "llvm/Analysis/ConstantFolding.h"#include "llvm/Analysis/EHPersonalities.h"#include "llvm/Analysis/GlobalsModRef.h"#include "llvm/Analysis/InstructionSimplify.h"#include "llvm/Analysis/LoopInfo.h"#include "llvm/Analysis/MemoryBuiltins.h"#include "llvm/Analysis/TargetLibraryInfo.h"#include "llvm/Analysis/ValueTracking.h"#include "llvm/IR/CFG.h"#include "llvm/IR/DataLayout.h"#include "llvm/IR/Dominators.h"#include "llvm/IR/GetElementPtrTypeIterator.h"#include "llvm/IR/IntrinsicInst.h"#include "llvm/IR/PatternMatch.h"#include "llvm/IR/ValueHandle.h"#include "llvm/Support/CommandLine.h"#include "llvm/Support/Debug.h"#include "llvm/Support/KnownBits.h"#include "llvm/Support/raw_ostream.h"#include "llvm/Transforms/InstCombine/InstCombine.h"#include "llvm/Transforms/Scalar.h"#include "llvm/Transforms/Utils/Local.h"#include <algorithm>#include <climits>Go to the source code of this file.
Macros | |
| #define | DEBUG_TYPE "instcombine" |
Functions | |
| 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") | |
| static bool | MaintainNoSignedWrap (BinaryOperator &I, Value *B, Value *C) |
| static void | ClearSubclassDataAfterReassociation (BinaryOperator &I) |
| Conservatively clears subclassOptionalData after a reassociation or commutation. More... | |
| static bool | simplifyAssocCastAssoc (BinaryOperator *BinOp1) |
| 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)) More... | |
| 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)". More... | |
| 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)". More... | |
| 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). More... | |
| static Instruction::BinaryOps | getBinOpsForFactorization (Instruction::BinaryOps TopLevelOpcode, BinaryOperator *Op, Value *&LHS, Value *&RHS) |
| This function factors binary ops which can be combined using distributive laws. More... | |
| static Value * | foldOperationIntoSelectOperand (Instruction &I, Value *SO, InstCombiner::BuilderTy &Builder) |
| static Value * | foldOperationIntoPhiValue (BinaryOperator *I, Value *InV, InstCombiner::BuilderTy &Builder) |
| static bool | shouldMergeGEPs (GEPOperator &GEP, GEPOperator &Src) |
| static Value * | CreateBinOpAsGiven (BinaryOperator &Inst, Value *LHS, Value *RHS, InstCombiner::BuilderTy &B) |
| Creates node of binary operation with the same attributes as the specified one but with other operands. More... | |
| static bool | isNeverEqualToUnescapedAlloc (Value *V, const TargetLibraryInfo *TLI, Instruction *AI) |
| static bool | isAllocSiteRemovable (Instruction *AI, SmallVectorImpl< WeakTrackingVH > &Users, const TargetLibraryInfo *TLI) |
| static Instruction * | tryToMoveFreeBeforeNullTest (CallInst &FI) |
| Move the call to free before a NULL test. More... | |
| static bool | isCatchAll (EHPersonality Personality, Constant *TypeInfo) |
| Return 'true' if the given typeinfo will match anything. More... | |
| static bool | shorter_filter (const Value *LHS, const Value *RHS) |
| static bool | TryToSinkInstruction (Instruction *I, BasicBlock *DestBlock) |
| Try to move the specified instruction from its current block into the beginning of DestBlock, which can only happen if it's safe to move the instruction past all of the instructions between it and the end of its block. More... | |
| static bool | AddReachableCodeToWorklist (BasicBlock *BB, const DataLayout &DL, SmallPtrSetImpl< BasicBlock * > &Visited, InstCombineWorklist &ICWorklist, const TargetLibraryInfo *TLI) |
| Walk the function in depth-first order, adding all reachable code to the worklist. More... | |
| static bool | prepareICWorklistFromFunction (Function &F, const DataLayout &DL, TargetLibraryInfo *TLI, InstCombineWorklist &ICWorklist) |
| Populate the IC worklist from a function, and prune any dead basic blocks discovered in the process. More... | |
| static bool | combineInstructionsOverFunction (Function &F, InstCombineWorklist &Worklist, AliasAnalysis *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, DominatorTree &DT, bool ExpensiveCombines=true, LoopInfo *LI=nullptr) |
| INITIALIZE_PASS_BEGIN (InstructionCombiningPass,"instcombine","Combine redundant instructions", false, false) INITIALIZE_PASS_END(InstructionCombiningPass | |
| void | LLVMInitializeInstCombine (LLVMPassRegistryRef R) |
Variables | |
| static cl::opt< bool > | EnableExpensiveCombines ("expensive-combines", cl::desc("Enable expensive instruction combines")) |
| static cl::opt< unsigned > | MaxArraySize ("instcombine-maxarray-size", cl::init(1024), cl::desc("Maximum array size considered when doing a combine")) |
| instcombine | |
| Combine redundant | instructions |
| Combine redundant | false |
| #define DEBUG_TYPE "instcombine" |
Definition at line 72 of file InstructionCombining.cpp.
|
static |
Walk the function in depth-first order, adding all reachable code to the worklist.
This has a couple of tricks to make the code faster and more powerful. In particular, we constant fold and DCE instructions as we go, to avoid adding them to the worklist (this significantly speeds up instcombine on code where many instructions are dead or constant). Additionally, if we find a branch whose condition is a known constant, we only visit the reachable successors.
Definition at line 3031 of file InstructionCombining.cpp.
References llvm::InstCombineWorklist::AddInitialGroup(), llvm::BasicBlock::begin(), C, llvm::ConstantFoldConstant(), llvm::ConstantFoldInstruction(), llvm::dbgs(), DEBUG, E, llvm::SmallVectorBase::empty(), llvm::BasicBlock::end(), llvm::Instruction::eraseFromParent(), llvm::SelectInst::getCondition(), llvm::BranchInst::getCondition(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::BranchInst::isConditional(), llvm::isInstructionTriviallyDead(), llvm::User::operands(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::Value::replaceAllUsesWith(), llvm::TerminatorInst::successors(), and llvm::Value::use_empty().
Referenced by prepareICWorklistFromFunction().
|
static |
Conservatively clears subclassOptionalData after a reassociation or commutation.
We preserve fast-math flags when applicable as they can be preserved.
Definition at line 162 of file InstructionCombining.cpp.
References llvm::Value::clearSubclassOptionalData(), llvm::dyn_cast(), llvm::Instruction::getFastMathFlags(), I, and llvm::Instruction::setFastMathFlags().
|
static |
Builder - This is an IRBuilder that automatically inserts new instructions into the worklist when they are created.
Definition at line 3166 of file InstructionCombining.cpp.
References llvm::InstCombineWorklist::Add(), llvm::dbgs(), DEBUG, llvm::DL, EnableExpensiveCombines, llvm::Function::getContext(), llvm::Module::getDataLayout(), llvm::Value::getName(), llvm::GlobalValue::getParent(), llvm::LowerDbgDeclare(), llvm::PatternMatch::match(), MaxArraySize, llvm::InstCombiner::MaxArraySizeForCombine, llvm::Function::optForMinSize(), prepareICWorklistFromFunction(), and llvm::AssumptionCache::registerAssumption().
Referenced by llvm::InstCombinePass::run(), and llvm::InstructionCombiningPass::runOnFunction().
|
static |
Creates node of binary operation with the same attributes as the specified one but with other operands.
Definition at line 1366 of file InstructionCombining.cpp.
References llvm::IRBuilder< T, Inserter >::CreateBinOp(), and llvm::BinaryOperator::getOpcode().
|
static |
Definition at line 868 of file InstructionCombining.cpp.
References C, llvm::Instruction::copyFastMathFlags(), llvm::IRBuilder< T, Inserter >::CreateBinOp(), llvm::dyn_cast(), llvm::ConstantExpr::get(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), and std::swap().
|
static |
Definition at line 788 of file InstructionCombining.cpp.
References assert(), llvm::Instruction::copyFastMathFlags(), llvm::IRBuilder< T, Inserter >::CreateBinOp(), llvm::IRBuilder< T, Inserter >::CreateCast(), llvm::dyn_cast(), llvm::ConstantExpr::get(), llvm::Value::getName(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Value::getType(), I, llvm::Instruction::isBinaryOp(), and std::swap().
|
static |
This function factors binary ops which can be combined using distributive laws.
This function tries to transform 'Op' based TopLevelOpcode to enable factorization e.g for ADD(SHL(X , 2), MUL(X, 5)), When this function called with TopLevelOpcode == Instruction::Add and Op = SHL(X, 2), transforms SHL(X, 2) to MUL(X, 4) i.e. returns Instruction::Mul with LHS set to 'X' and RHS to 4.
Definition at line 473 of file InstructionCombining.cpp.
References llvm::MCID::Add, assert(), llvm::ConstantInt::get(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::ConstantExpr::getShl(), and llvm::Value::getType().
|
static |
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).
Definition at line 459 of file InstructionCombining.cpp.
References llvm::ConstantExpr::getBinOpIdentity(), and llvm::Value::getType().
| INITIALIZE_PASS_BEGIN | ( | InstructionCombiningPass | , |
| "instcombine" | , | ||
| "Combine redundant instructions" | , | ||
| false | , | ||
| false | |||
| ) |
|
static |
Definition at line 2005 of file InstructionCombining.cpp.
References llvm::MCID::Call, llvm::SmallVectorImpl< T >::emplace_back(), llvm::SmallVectorBase::empty(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::StoreInst::getPointerOperand(), llvm::MemIntrinsic::getRawDest(), I, llvm::ICmpInst::isEquality(), llvm::isFreeCall(), isNeverEqualToUnescapedAlloc(), llvm::StoreInst::isVolatile(), llvm::MemIntrinsic::isVolatile(), LLVM_FALLTHROUGH, llvm_unreachable(), MI, llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::SPII::Store, and llvm::Value::users().
Referenced by llvm::InstCombiner::visitAllocSite().
|
static |
Return 'true' if the given typeinfo will match anything.
Definition at line 2486 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_Win64SEH, llvm::MSVC_X86SEH, llvm::Rust, and llvm::Unknown.
Referenced by llvm::InstCombiner::visitLandingPadInst().
|
static |
Definition at line 1991 of file InstructionCombining.cpp.
References llvm::isAllocLikeFn().
Referenced by isAllocSiteRemovable().
|
static |
Return whether "X LOp (Y ROp Z)" is always equal to "(X LOp Y) ROp (X LOp Z)".
Definition at line 389 of file InstructionCombining.cpp.
References llvm::MCID::Add.
Referenced by RightDistributesOverLeft().
|
static |
Definition at line 136 of file InstructionCombining.cpp.
References llvm::MCID::Add, llvm::dyn_cast(), llvm::BinaryOperator::getOpcode(), llvm::OverflowingBinaryOperator::hasNoSignedWrap(), I, llvm::PatternMatch::m_APInt(), llvm::PatternMatch::match(), llvm::APInt::sadd_ov(), and llvm::APInt::ssub_ov().
|
static |
Populate the IC worklist from a function, and prune any dead basic blocks discovered in the process.
This also does basic constant propagation and other forward fixing to make the combiner itself run much faster.
Definition at line 3138 of file InstructionCombining.cpp.
References AddReachableCodeToWorklist(), llvm::Function::front(), and llvm::removeAllNonTerminatorAndEHPadInstructions().
Referenced by combineInstructionsOverFunction().
|
static |
Return whether "(X LOp Y) ROp Z" is always equal to "(X ROp Z) LOp (Y ROp Z)".
Definition at line 428 of file InstructionCombining.cpp.
References llvm::Instruction::isCommutative(), and LeftDistributesOverRight().
Definition at line 2512 of file InstructionCombining.cpp.
References llvm::Value::getType().
Referenced by llvm::InstCombiner::visitLandingPadInst().
|
static |
Definition at line 1110 of file InstructionCombining.cpp.
References llvm::GEPOperator::hasAllZeroIndices(), and llvm::Value::hasOneUse().
Referenced by llvm::InstCombiner::visitGetElementPtrInst().
|
static |
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 178 of file InstructionCombining.cpp.
References llvm::dyn_cast(), llvm::ConstantExpr::get(), llvm::ConstantExpr::getCast(), llvm::BinaryOperator::getOpcode(), llvm::CastInst::getOpcode(), llvm::User::getOperand(), llvm::Value::getType(), llvm::Instruction::isBitwiseLogicOp(), llvm::PatternMatch::m_Constant(), llvm::PatternMatch::match(), and llvm::User::setOperand().
| 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" | |||
| ) |
|
static |
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.:
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 2157 of file InstructionCombining.cpp.
References assert(), llvm::CallInst::getArgOperand(), llvm::Instruction::getParent(), llvm::BasicBlock::getSinglePredecessor(), llvm::BasicBlock::getTerminator(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::PatternMatch::m_Br(), llvm::PatternMatch::m_ICmp(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_UnconditionalBr(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::match(), llvm::Instruction::moveBefore(), and llvm::BasicBlock::size().
Referenced by llvm::InstCombiner::visitFree().
|
static |
Try to move the specified instruction from its current block into the beginning of DestBlock, which can only happen if it's safe to move the instruction past all of the instructions between it and the end of its block.
Definition at line 2832 of file InstructionCombining.cpp.
References assert(), E, llvm::BasicBlock::end(), llvm::Function::getEntryBlock(), llvm::BasicBlock::getFirstInsertionPt(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::Instruction::getParent(), llvm::BasicBlock::getParent(), llvm::BasicBlock::getTerminator(), llvm::Value::hasOneUse(), I, llvm::Instruction::isEHPad(), llvm::Instruction::mayHaveSideEffects(), llvm::Instruction::mayReadFromMemory(), and llvm::Instruction::moveBefore().
Referenced by llvm::InstCombiner::run().
|
static |
Referenced by combineInstructionsOverFunction().
| Combine redundant false |
Definition at line 3270 of file InstructionCombining.cpp.
| instcombine |
Definition at line 3270 of file InstructionCombining.cpp.
| Combine redundant instructions |
Definition at line 3270 of file InstructionCombining.cpp.
|
static |
Referenced by combineInstructionsOverFunction().
1.8.6