LLVM 17.0.0git
|
#include "llvm/Transforms/Scalar/LICM.h"
#include "llvm/ADT/PriorityWorklist.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/AliasSetTracker.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/CaptureTracking.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/GuardUtils.h"
#include "llvm/Analysis/LazyBlockFrequencyInfo.h"
#include "llvm/Analysis/Loads.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/LoopIterator.h"
#include "llvm/Analysis/LoopNestAnalysis.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/MemorySSA.h"
#include "llvm/Analysis/MemorySSAUpdater.h"
#include "llvm/Analysis/MustExecute.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/PredIteratorCache.h"
#include "llvm/InitializePasses.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
#include "llvm/Transforms/Utils/SSAUpdater.h"
#include <algorithm>
#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 "licm" |
Typedefs | |
using | PointersAndHasReadsOutsideSet = std::pair< SmallSetVector< Value *, 8 >, bool > |
Functions | |
STATISTIC (NumCreatedBlocks, "Number of blocks created") | |
STATISTIC (NumClonedBranches, "Number of branches cloned") | |
STATISTIC (NumSunk, "Number of instructions sunk out of loop") | |
STATISTIC (NumHoisted, "Number of instructions hoisted out of loop") | |
STATISTIC (NumMovedLoads, "Number of load insts hoisted or sunk") | |
STATISTIC (NumMovedCalls, "Number of call insts hoisted or sunk") | |
STATISTIC (NumPromotionCandidates, "Number of promotion candidates") | |
STATISTIC (NumLoadPromoted, "Number of load-only promotions") | |
STATISTIC (NumLoadStorePromoted, "Number of load and store promotions") | |
static bool | inSubLoop (BasicBlock *BB, Loop *CurLoop, LoopInfo *LI) |
Little predicate that returns true if the specified basic block is in a subloop of the current one, not the current one itself. | |
static bool | isNotUsedOrFreeInLoop (const Instruction &I, const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo, TargetTransformInfo *TTI, bool &FreeInLoop, bool LoopNestMode) |
Return true if the only users of this instruction are outside of the loop. | |
static void | hoist (Instruction &I, const DominatorTree *DT, const Loop *CurLoop, BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo, MemorySSAUpdater &MSSAU, ScalarEvolution *SE, OptimizationRemarkEmitter *ORE) |
When an instruction is found to only use loop invariant operands that is safe to hoist, this instruction is called to do the dirty work. | |
static bool | sink (Instruction &I, LoopInfo *LI, DominatorTree *DT, const Loop *CurLoop, ICFLoopSafetyInfo *SafetyInfo, MemorySSAUpdater &MSSAU, OptimizationRemarkEmitter *ORE) |
When an instruction is found to only be used outside of the loop, this function moves it to the exit blocks and patches up SSA form as needed. | |
static bool | isSafeToExecuteUnconditionally (Instruction &Inst, const DominatorTree *DT, const TargetLibraryInfo *TLI, const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE, const Instruction *CtxI, AssumptionCache *AC, bool AllowSpeculation) |
Only sink or hoist an instruction if it is not a trapping instruction, or if the instruction is known not to trap when moved to the preheader. | |
static bool | pointerInvalidatedByLoop (MemorySSA *MSSA, MemoryUse *MU, Loop *CurLoop, Instruction &I, SinkAndHoistLICMFlags &Flags) |
static bool | pointerInvalidatedByBlock (BasicBlock &BB, MemorySSA &MSSA, MemoryUse &MU) |
static Instruction * | cloneInstructionInExitBlock (Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater &MSSAU) |
static void | eraseInstruction (Instruction &I, ICFLoopSafetyInfo &SafetyInfo, MemorySSAUpdater &MSSAU) |
static void | moveInstructionBefore (Instruction &I, Instruction &Dest, ICFLoopSafetyInfo &SafetyInfo, MemorySSAUpdater &MSSAU, ScalarEvolution *SE) |
static void | foreachMemoryAccess (MemorySSA *MSSA, Loop *L, function_ref< void(Instruction *)> Fn) |
static SmallVector< PointersAndHasReadsOutsideSet, 0 > | collectPromotionCandidates (MemorySSA *MSSA, AliasAnalysis *AA, Loop *L) |
INITIALIZE_PASS_BEGIN (LegacyLICMPass, "licm", "Loop Invariant Code Motion", false, false) INITIALIZE_PASS_END(LegacyLICMPass | |
static bool | isLoadInvariantInLoop (LoadInst *LI, DominatorTree *DT, Loop *CurLoop) |
static bool | isTriviallyReplaceablePHI (const PHINode &PN, const Instruction &I) |
Returns true if a PHINode is a trivially replaceable with an Instruction. | |
static bool | isFreeInLoop (const Instruction &I, const Loop *CurLoop, const TargetTransformInfo *TTI) |
Return true if the instruction is free in the loop. | |
static Instruction * | sinkThroughTriviallyReplaceablePHI (PHINode *TPN, Instruction *I, LoopInfo *LI, SmallDenseMap< BasicBlock *, Instruction *, 32 > &SunkCopies, const LoopSafetyInfo *SafetyInfo, const Loop *CurLoop, MemorySSAUpdater &MSSAU) |
static bool | canSplitPredecessors (PHINode *PN, LoopSafetyInfo *SafetyInfo) |
static void | splitPredecessorsOfLoopExit (PHINode *PN, DominatorTree *DT, LoopInfo *LI, const Loop *CurLoop, LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU) |
Variables | |
static cl::opt< bool > | DisablePromotion ("disable-licm-promotion", cl::Hidden, cl::init(false), cl::desc("Disable memory promotion in LICM pass")) |
Memory promotion is enabled by default. | |
static cl::opt< bool > | ControlFlowHoisting ("licm-control-flow-hoisting", cl::Hidden, cl::init(false), cl::desc("Enable control flow (and PHI) hoisting in LICM")) |
static cl::opt< bool > | SingleThread ("licm-force-thread-model-single", cl::Hidden, cl::init(false), cl::desc("Force thread model single in LICM pass")) |
static cl::opt< uint32_t > | MaxNumUsesTraversed ("licm-max-num-uses-traversed", cl::Hidden, cl::init(8), cl::desc("Max num uses visited for identifying load " "invariance in loop using invariant start (default = 8)")) |
licm | |
Loop Invariant Code | Motion |
Loop Invariant Code | false |
using PointersAndHasReadsOutsideSet = std::pair<SmallSetVector<Value *, 8>, bool> |
|
static |
Definition at line 1514 of file LICM.cpp.
References llvm::BasicBlock::canSplitPredecessors(), llvm::LoopSafetyInfo::getBlockColors(), llvm::BasicBlock::getFirstNonPHI(), llvm::Instruction::getParent(), llvm::Instruction::isEHPad(), and llvm::predecessors().
Referenced by sink().
|
static |
Definition at line 1403 of file LICM.cpp.
References llvm::PHINode::addIncoming(), assert(), llvm::MemorySSA::Beginning, llvm::CallInst::Create(), llvm::PHINode::Create(), llvm::MemorySSAUpdater::createMemoryAccessInBB(), llvm::SmallVectorImpl< T >::emplace_back(), llvm::TinyPtrVector< EltTy >::front(), llvm::BasicBlock::front(), llvm::LoopSafetyInfo::getBlockColors(), llvm::BasicBlock::getFirstInsertionPt(), llvm::BasicBlock::getFirstNonPHI(), llvm::PHINode::getIncomingBlock(), llvm::MemorySSA::getMemoryAccess(), llvm::MemorySSAUpdater::getMemorySSA(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getParent(), llvm::OperandBundleUse::getTagID(), I, llvm::MemorySSAUpdater::insertDef(), llvm::MemorySSAUpdater::insertUse(), llvm::Instruction::isEHPad(), llvm::LLVMContext::OB_funclet, llvm::TinyPtrVector< EltTy >::size(), and llvm::LoopInfo::wouldBeOutOfLoopUseRequiringLCSSA().
Referenced by sinkThroughTriviallyReplaceablePHI().
|
static |
Definition at line 2269 of file LICM.cpp.
References llvm::AliasSetTracker::add(), llvm::SmallPtrSetImpl< PtrType >::contains(), llvm::erase_if(), foreachMemoryAccess(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::Loop::isLoopInvariant(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), and SI.
|
static |
Definition at line 1475 of file LICM.cpp.
References I, llvm::ICFLoopSafetyInfo::removeInstruction(), and llvm::MemorySSAUpdater::removeMemoryAccess().
Referenced by llvm::hoistRegion(), llvm::promoteLoopAccessesToScalars(), sink(), and llvm::sinkRegion().
|
static |
Definition at line 2257 of file LICM.cpp.
References llvm::LoopBase< BlockT, LoopT >::blocks(), and llvm::MemorySSA::getBlockAccesses().
Referenced by collectPromotionCandidates().
|
static |
When an instruction is found to only use loop invariant operands that is safe to hoist, this instruction is called to do the dirty work.
Definition at line 1708 of file LICM.cpp.
References llvm::dbgs(), DEBUG_TYPE, llvm::OptimizationRemarkEmitter::emit(), llvm::BasicBlock::getFirstNonPHI(), llvm::Value::getNameOrAsOperand(), llvm::BasicBlock::getTerminator(), I, llvm::ICFLoopSafetyInfo::isGuaranteedToExecute(), LLVM_DEBUG, and moveInstructionBefore().
INITIALIZE_PASS_BEGIN | ( | LegacyLICMPass | , |
"licm" | , | ||
"Loop Invariant Code Motion" | , | ||
false | , | ||
false | |||
) |
|
static |
Little predicate that returns true if the specified basic block is in a subloop of the current one, not the current one itself.
Definition at line 2389 of file LICM.cpp.
References assert(), llvm::LoopBase< BlockT, LoopT >::contains(), and llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor().
Referenced by llvm::hoistRegion(), and llvm::sinkRegion().
|
static |
Return true if the instruction is free in the loop.
Definition at line 1330 of file LICM.cpp.
References llvm::LoopBase< BlockT, LoopT >::contains(), GEP, llvm::TargetTransformInfo::getInstructionCost(), llvm::Instruction::getParent(), I, llvm::TargetTransformInfo::TCC_Free, and llvm::TargetTransformInfo::TCK_SizeAndLatency.
Referenced by isNotUsedOrFreeInLoop().
|
static |
Definition at line 1042 of file LICM.cpp.
References Addr, DL, llvm::PointerType::get(), llvm::CallBase::getArgOperand(), llvm::Value::getContext(), llvm::Module::getDataLayout(), llvm::details::FixedOrScalableQuantity< LeafTy, ValueTy >::getFixedValue(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::Type::getInt8Ty(), llvm::IntrinsicInst::getIntrinsicID(), llvm::Instruction::getModule(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::LoadInst::getPointerAddressSpace(), llvm::ConstantInt::getSExtValue(), llvm::Value::getType(), llvm::ConstantInt::isNegative(), llvm::details::FixedOrScalableQuantity< LeafTy, ValueTy >::isScalable(), MaxNumUsesTraversed, llvm::DominatorTreeBase< NodeT, IsPostDom >::properlyDominates(), and llvm::Value::use_empty().
Referenced by llvm::canSinkOrHoistInst().
|
static |
Return true if the only users of this instruction are outside of the loop.
If this is true, we can sink the instruction to the exit blocks of the loop.
We also return true if the instruction could be folded away in lowering. (e.g., a GEP can be folded into a load as an addressing mode in the loop).
Definition at line 1361 of file LICM.cpp.
References llvm::LoopBase< BlockT, LoopT >::contains(), llvm::LoopSafetyInfo::getBlockColors(), llvm::User::getNumOperands(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), llvm::Value::hasOneUser(), I, isFreeInLoop(), and llvm::Instruction::user_back().
Referenced by llvm::sinkRegion().
|
static |
Only sink or hoist an instruction if it is not a trapping instruction, or if the instruction is known not to trap when moved to the preheader.
or if it is a trapping instruction and is guaranteed to execute.
Definition at line 1753 of file LICM.cpp.
References DEBUG_TYPE, llvm::OptimizationRemarkEmitter::emit(), llvm::LoopSafetyInfo::isGuaranteedToExecute(), llvm::Loop::isLoopInvariant(), and llvm::isSafeToSpeculativelyExecute().
Referenced by llvm::hoistRegion(), and llvm::promoteLoopAccessesToScalars().
|
static |
Returns true if a PHINode is a trivially replaceable with an Instruction.
This is true when all incoming values are that instruction. This pattern occurs most often with LCSSA PHI nodes.
Definition at line 1321 of file LICM.cpp.
References I, and llvm::PHINode::incoming_values().
Referenced by sink(), and sinkThroughTriviallyReplaceablePHI().
|
static |
Definition at line 1482 of file LICM.cpp.
References llvm::MemorySSA::BeforeTerminator, llvm::ScalarEvolution::forgetValue(), llvm::MemorySSA::getMemoryAccess(), llvm::MemorySSAUpdater::getMemorySSA(), llvm::Instruction::getParent(), I, llvm::ICFLoopSafetyInfo::insertInstructionTo(), llvm::MemorySSAUpdater::moveToPlace(), and llvm::ICFLoopSafetyInfo::removeInstruction().
Referenced by hoist(), and llvm::hoistRegion().
|
static |
Definition at line 2377 of file LICM.cpp.
References llvm::MemoryAccess::getBlock(), llvm::MemorySSA::getBlockDefs(), and llvm::MemorySSA::locallyDominates().
Referenced by pointerInvalidatedByLoop().
|
static |
Definition at line 2331 of file LICM.cpp.
References llvm::LoopBase< BlockT, LoopT >::contains(), llvm::LoopBase< BlockT, LoopT >::getBlocks(), llvm::MemorySSAWalker::getClobberingMemoryAccess(), llvm::MemoryUseOrDef::getDefiningAccess(), llvm::MemorySSA::getSkipSelfWalker(), I, llvm::MemorySSA::isLiveOnEntryDef(), and pointerInvalidatedByBlock().
Referenced by llvm::canSinkOrHoistInst().
|
static |
When an instruction is found to only be used outside of the loop, this function moves it to the exit blocks and patches up SSA form as needed.
This method is guaranteed to remove the original instruction from its position, and may either delete it or move it to outside of the loop.
Definition at line 1603 of file LICM.cpp.
References assert(), llvm::SmallVectorTemplateCommon< T, typename >::begin(), canSplitPredecessors(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::SmallPtrSetImpl< PtrType >::count(), llvm::dbgs(), DEBUG_TYPE, llvm::OptimizationRemarkEmitter::emit(), llvm::SmallPtrSetImplBase::empty(), llvm::SmallVectorTemplateCommon< T, typename >::end(), eraseInstruction(), llvm::PoisonValue::get(), llvm::PHINode::getIncomingBlock(), llvm::Instruction::getParent(), llvm::LoopBase< BlockT, LoopT >::getUniqueExitBlocks(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::DominatorTree::isReachableFromEntry(), isTriviallyReplaceablePHI(), LLVM_DEBUG, llvm::Value::replaceAllUsesWith(), sinkThroughTriviallyReplaceablePHI(), splitPredecessorsOfLoopExit(), and Users.
|
static |
Definition at line 1496 of file LICM.cpp.
References assert(), cloneInstructionInExitBlock(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::Instruction::getParent(), I, and isTriviallyReplaceablePHI().
Referenced by sink().
|
static |
Definition at line 1531 of file LICM.cpp.
References assert(), llvm::SetVector< T, Vector, Set >::begin(), llvm::SmallVectorTemplateCommon< T, typename >::begin(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::LoopSafetyInfo::copyColors(), llvm::SmallPtrSetImpl< PtrType >::count(), llvm::SetVector< T, Vector, Set >::empty(), llvm::SmallVectorTemplateCommon< T, typename >::end(), llvm::PHINode::getBasicBlockIndex(), llvm::LoopSafetyInfo::getBlockColors(), llvm::Instruction::getParent(), llvm::LoopBase< BlockT, LoopT >::getUniqueExitBlocks(), llvm::pred_begin(), llvm::pred_end(), llvm::SetVector< T, Vector, Set >::remove(), and llvm::SplitBlockPredecessors().
Referenced by sink().
STATISTIC | ( | NumClonedBranches | , |
"Number of branches cloned" | |||
) |
STATISTIC | ( | NumCreatedBlocks | , |
"Number of blocks created" | |||
) |
STATISTIC | ( | NumHoisted | , |
"Number of instructions hoisted out of loop" | |||
) |
STATISTIC | ( | NumLoadPromoted | , |
"Number of load-only promotions" | |||
) |
STATISTIC | ( | NumLoadStorePromoted | , |
"Number of load and store promotions" | |||
) |
STATISTIC | ( | NumMovedCalls | , |
"Number of call insts hoisted or sunk" | |||
) |
STATISTIC | ( | NumMovedLoads | , |
"Number of load insts hoisted or sunk" | |||
) |
STATISTIC | ( | NumPromotionCandidates | , |
"Number of promotion candidates" | |||
) |
STATISTIC | ( | NumSunk | , |
"Number of instructions sunk out of loop" | |||
) |
|
static |
Referenced by llvm::hoistRegion().
|
static |
Memory promotion is enabled by default.
|
static |
Referenced by isLoadInvariantInLoop().