LLVM
8.0.1
|
#include "llvm/Transforms/Scalar/LICM.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/AliasSetTracker.h"
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/Analysis/CaptureTracking.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/GuardUtils.h"
#include "llvm/Analysis/Loads.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/LoopIterator.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/MemorySSA.h"
#include "llvm/Analysis/MemorySSAUpdater.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.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/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/LoopPassManager.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.
Macros | |
#define | DEBUG_TYPE "licm" |
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 (NumPromoted, "Number of memory locations promoted to registers") | |
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. More... | |
static bool | isNotUsedOrFreeInLoop (const Instruction &I, const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo, TargetTransformInfo *TTI, bool &FreeInLoop) |
Return true if the only users of this instruction are outside of the loop. More... | |
static void | hoist (Instruction &I, const DominatorTree *DT, const Loop *CurLoop, BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU, 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. More... | |
static bool | sink (Instruction &I, LoopInfo *LI, DominatorTree *DT, const Loop *CurLoop, ICFLoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU, OptimizationRemarkEmitter *ORE, bool FreeInLoop) |
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. More... | |
static bool | isSafeToExecuteUnconditionally (Instruction &Inst, const DominatorTree *DT, const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE, const Instruction *CtxI) |
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. More... | |
static bool | pointerInvalidatedByLoop (MemoryLocation MemLoc, AliasSetTracker *CurAST, Loop *CurLoop, AliasAnalysis *AA) |
static bool | pointerInvalidatedByLoopWithMSSA (MemorySSA *MSSA, MemoryUse *MU, Loop *CurLoop) |
static Instruction * | CloneInstructionInExitBlock (Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI, const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU) |
static void | eraseInstruction (Instruction &I, ICFLoopSafetyInfo &SafetyInfo, AliasSetTracker *AST, MemorySSAUpdater *MSSAU) |
static void | moveInstructionBefore (Instruction &I, Instruction &Dest, ICFLoopSafetyInfo &SafetyInfo) |
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. More... | |
static bool | isFreeInLoop (const Instruction &I, const Loop *CurLoop, const TargetTransformInfo *TTI) |
Return true if the instruction is free in the loop. More... | |
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. More... | |
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< 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)")) |
static cl::opt< int > | LICMN2Theshold ("licm-n2-threshold", cl::Hidden, cl::init(0), cl::desc("How many instruction to cross product using AA")) |
static cl::opt< bool > | EnableLicmCap ("enable-licm-cap", cl::init(false), cl::Hidden, cl::desc("Enable imprecision in LICM (uses MemorySSA cap) in " "pathological cases, in exchange for faster compile")) |
licm | |
Loop Invariant Code | Motion |
Loop Invariant Code | false |
#define DEBUG_TYPE "licm" |
Definition at line 79 of file LICM.cpp.
Referenced by llvm::canSinkOrHoistInst(), hoist(), isSafeToExecuteUnconditionally(), llvm::promoteLoopAccessesToScalars(), and sink().
|
static |
Definition at line 1337 of file LICM.cpp.
References llvm::BasicBlock::canSplitPredecessors(), E, llvm::LoopSafetyInfo::getBlockColors(), llvm::BasicBlock::getFirstNonPHI(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), llvm::Instruction::isEHPad(), llvm::pred_begin(), and llvm::pred_end().
Referenced by sink().
|
static |
Definition at line 1227 of file LICM.cpp.
References llvm::PHINode::addIncoming(), assert(), llvm::MemorySSA::Beginning, llvm::Instruction::clone(), llvm::CallInst::Create(), llvm::PHINode::Create(), llvm::MemorySSAUpdater::createMemoryAccessInBB(), llvm::SmallVectorImpl< T >::emplace_back(), llvm::StringRef::empty(), llvm::TinyPtrVector< EltTy >::front(), llvm::BasicBlock::front(), llvm::LoopSafetyInfo::getBlockColors(), llvm::BasicBlock::getFirstInsertionPt(), llvm::PHINode::getIncomingBlock(), llvm::BasicBlock::getInstList(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::MemorySSA::getMemoryAccess(), llvm::MemorySSAUpdater::getMemorySSA(), llvm::Value::getName(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getParent(), llvm::OperandBundleUse::getTagID(), llvm::iplist_impl< IntrusiveListT, TraitsT >::insert(), llvm::MemorySSAUpdater::insertDef(), llvm::MemorySSAUpdater::insertUse(), llvm::Instruction::isEHPad(), llvm::LLVMContext::OB_funclet, llvm::User::op_begin(), llvm::User::op_end(), llvm::Value::setName(), and llvm::TinyPtrVector< EltTy >::size().
Referenced by sinkThroughTriviallyReplaceablePHI().
|
static |
Definition at line 1302 of file LICM.cpp.
References llvm::AliasSetTracker::deleteValue(), llvm::Instruction::eraseFromParent(), llvm::ICFLoopSafetyInfo::removeInstruction(), and llvm::MemorySSAUpdater::removeMemoryAccess().
Referenced by llvm::slpvectorizer::BoUpSLP::getORE(), llvm::hoistRegion(), operator<<(), llvm::slpvectorizer::BoUpSLP::optimizeGatherSequence(), llvm::promoteLoopAccessesToScalars(), sink(), llvm::sinkRegion(), and llvm::slpvectorizer::BoUpSLP::vectorizeTree().
|
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 1529 of file LICM.cpp.
References llvm::dbgs(), DEBUG_TYPE, llvm::Instruction::dropUnknownNonDebugMetadata(), llvm::OptimizationRemarkEmitter::emit(), llvm::MemorySSA::End, llvm::BasicBlock::getFirstNonPHI(), llvm::MemorySSA::getMemoryAccess(), llvm::MemorySSAUpdater::getMemorySSA(), llvm::Value::getName(), llvm::BasicBlock::getTerminator(), llvm::Instruction::hasMetadataOtherThanDebugLoc(), llvm::ICFLoopSafetyInfo::isGuaranteedToExecute(), LLVM_DEBUG, moveInstructionBefore(), llvm::MemorySSAUpdater::moveToPlace(), and llvm::Instruction::setDebugLoc().
Referenced by llvm::hoistRegion().
Referenced by llvm::LICMPass::run().
|
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 2112 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 1167 of file LICM.cpp.
References llvm::LoopBase< BlockT, LoopT >::contains(), GEP, llvm::Instruction::getParent(), llvm::TargetTransformInfo::getUserCost(), and llvm::TargetTransformInfo::TCC_Free.
Referenced by isNotUsedOrFreeInLoop().
|
static |
Definition at line 896 of file LICM.cpp.
References llvm::dyn_cast(), llvm::PointerType::get(), llvm::CallBase::getArgOperand(), llvm::MemorySSA::getBlockAccesses(), llvm::MemorySSA::getBlockDefs(), llvm::LoopBase< BlockT, LoopT >::getBlocks(), llvm::Value::getContext(), llvm::Module::getDataLayout(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::Type::getInt8Ty(), llvm::IntrinsicInst::getIntrinsicID(), llvm::MemorySSAUpdater::getMemorySSA(), llvm::Instruction::getModule(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::LoadInst::getPointerAddressSpace(), llvm::Value::getType(), llvm::DataLayout::getTypeSizeInBits(), I, llvm::Intrinsic::invariant_start, MaxNumUsesTraversed, llvm::DominatorTreeBase< NodeT, IsPostDom >::properlyDominates(), llvm::Value::use_empty(), and llvm::Value::users().
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 1195 of file LICM.cpp.
References llvm::LoopBase< BlockT, LoopT >::contains(), llvm::LoopSafetyInfo::getBlockColors(), llvm::BasicBlock::getParent(), llvm::BasicBlock::getTerminator(), isFreeInLoop(), and llvm::Value::users().
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 1582 of file LICM.cpp.
References llvm::PHINode::addIncoming(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::AliasSetTracker::copyValue(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::count(), llvm::PHINode::Create(), DEBUG_TYPE, llvm::AliasSetTracker::deleteValue(), llvm::dyn_cast(), llvm::OptimizationRemarkEmitter::emit(), llvm::BasicBlock::front(), llvm::PredIteratorCache::get(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::getPointerOperand(), I, llvm::isAllocLikeFn(), llvm::LoopSafetyInfo::isGuaranteedToExecute(), llvm::Loop::isLoopInvariant(), llvm::isSafeToSpeculativelyExecute(), llvm::PointerMayBeCaptured(), llvm::ICFLoopSafetyInfo::removeInstruction(), llvm::Instruction::setAAMetadata(), llvm::StoreInst::setAlignment(), llvm::Instruction::setDebugLoc(), llvm::StoreInst::setOrdering(), llvm::SmallVectorBase::size(), llvm::PredIteratorCache::size(), SSA, and llvm::Unordered.
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 1158 of file LICM.cpp.
References llvm::PHINode::incoming_values().
Referenced by sink(), and sinkThroughTriviallyReplaceablePHI().
|
static |
Definition at line 1312 of file LICM.cpp.
References llvm::Instruction::getParent(), llvm::ICFLoopSafetyInfo::insertInstructionTo(), llvm::Instruction::moveBefore(), and llvm::ICFLoopSafetyInfo::removeInstruction().
Referenced by hoist(), and llvm::hoistRegion().
|
static |
Definition at line 2050 of file LICM.cpp.
References llvm::LoopBase< BlockT, LoopT >::begin(), llvm::dbgs(), llvm::LoopBase< BlockT, LoopT >::end(), llvm::AliasSetTracker::getAliasSetFor(), llvm::LoopBase< BlockT, LoopT >::getBlocks(), llvm::AAResults::getModRefInfo(), I, llvm::AliasSet::isMod(), llvm::isModSet(), LICMN2Theshold, LLVM_DEBUG, N, and llvm::MemoryLocation::Ptr.
Referenced by llvm::canSinkOrHoistInst().
|
static |
Definition at line 2097 of file LICM.cpp.
References llvm::LoopBase< BlockT, LoopT >::contains(), EnableLicmCap, llvm::MemoryAccess::getBlock(), llvm::MemorySSAWalker::getClobberingMemoryAccess(), llvm::MemoryUseOrDef::getDefiningAccess(), llvm::MemorySSA::getSkipSelfWalker(), llvm::MemorySSA::isLiveOnEntryDef(), and llvm::Sched::Source.
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 1427 of file LICM.cpp.
References assert(), llvm::SmallVectorTemplateCommon< T >::begin(), canSplitPredecessors(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::SmallPtrSetImpl< PtrType >::count(), llvm::dbgs(), DEBUG_TYPE, llvm::OptimizationRemarkEmitter::emit(), llvm::SmallPtrSetImplBase::empty(), llvm::SmallVectorTemplateCommon< T >::end(), eraseInstruction(), llvm::UndefValue::get(), llvm::PHINode::getIncomingBlock(), llvm::Instruction::getParent(), llvm::Value::getType(), llvm::LoopBase< BlockT, LoopT >::getUniqueExitBlocks(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::DominatorTree::isReachableFromEntry(), isTriviallyReplaceablePHI(), LLVM_DEBUG, llvm::Value::replaceAllUsesWith(), sinkThroughTriviallyReplaceablePHI(), splitPredecessorsOfLoopExit(), llvm::Value::user_begin(), llvm::Value::user_end(), and Users.
Referenced by llvm::sinkRegion().
|
static |
Definition at line 1319 of file LICM.cpp.
References assert(), CloneInstructionInExitBlock(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::Instruction::getParent(), and isTriviallyReplaceablePHI().
Referenced by sink().
|
static |
Definition at line 1355 of file LICM.cpp.
References assert(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::begin(), llvm::SmallVectorTemplateCommon< T >::begin(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::LoopSafetyInfo::copyColors(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::empty(), llvm::SmallVectorTemplateCommon< T >::end(), llvm::PHINode::getBasicBlockIndex(), llvm::LoopSafetyInfo::getBlockColors(), llvm::Instruction::getParent(), llvm::LoopBase< BlockT, LoopT >::getUniqueExitBlocks(), llvm::pred_begin(), llvm::pred_end(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::remove(), and llvm::SplitBlockPredecessors().
Referenced by sink().
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 | ( | NumPromoted | , |
"Number of memory locations promoted to registers" | |||
) |
|
static |
Referenced by llvm::hoistRegion(), and llvm::sinkRegion().
|
static |
Memory promotion is enabled by default.
Referenced by llvm::createLICMPass().
|
static |
Referenced by llvm::canSinkOrHoistInst(), and pointerInvalidatedByLoopWithMSSA().
|
static |
Referenced by pointerInvalidatedByLoop().
|
static |
Referenced by isLoadInvariantInLoop().