62 #define DEBUG_TYPE "licm"
64 STATISTIC(NumSunk ,
"Number of instructions sunk out of loop");
65 STATISTIC(NumHoisted ,
"Number of instructions hoisted out of loop");
66 STATISTIC(NumMovedLoads,
"Number of load insts hoisted or sunk");
67 STATISTIC(NumMovedCalls,
"Number of call insts hoisted or sunk");
68 STATISTIC(NumPromoted ,
"Number of memory locations promoted to registers");
72 cl::desc(
"Disable memory promotion in LICM pass"));
129 bool doFinalization()
override {
130 assert(LoopToAliasSetMap.empty() &&
"Didn't free loop alias sets");
154 void deleteAnalysisValue(
Value *V,
Loop *L)
override;
157 void deleteAnalysisLoop(
Loop *L)
override;
179 if (skipOptnoneFunction(L))
185 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
186 AA = &getAnalysis<AliasAnalysis>();
187 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
189 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
191 assert(L->
isLCSSAForm(*DT) &&
"Loop is not in LCSSA form.");
196 LoopItr != LoopItrE; ++LoopItr) {
197 Loop *InnerL = *LoopItr;
199 assert(InnerAST &&
"Where is my AST?");
202 CurAST->add(*InnerAST);
207 LoopToAliasSetMap.erase(InnerL);
222 if (LI->getLoopFor(BB) == L)
242 CurAST, &SafetyInfo);
245 CurLoop, CurAST, &SafetyInfo);
258 PIC, LI, DT, CurLoop,
259 CurAST, &SafetyInfo);
269 getAnalysisIfAvailable<ScalarEvolution>());
275 assert(L->
isLCSSAForm(*DT) &&
"Loop not left in LCSSA form after LICM!");
277 "Parent loop not left in LCSSA form after LICM!");
286 LoopToAliasSetMap[L] = CurAST;
302 assert(N !=
nullptr && AA !=
nullptr && LI !=
nullptr &&
303 DT !=
nullptr && CurLoop !=
nullptr && CurAST !=
nullptr &&
304 SafetyInfo !=
nullptr &&
"Unexpected input to sinkRegion");
307 bool Changed =
false;
311 if (!CurLoop->
contains(BB))
return Changed;
314 const std::vector<DomTreeNode*> &Children = N->
getChildren();
315 for (
unsigned i = 0, e = Children.size(); i != e; ++i)
317 sinkRegion(Children[i], AA, LI, DT, TLI, CurLoop, CurAST, SafetyInfo);
320 if (
inSubLoop(BB,CurLoop,LI))
return Changed;
328 DEBUG(
dbgs() <<
"LICM deleting dead inst: " << I <<
'\n');
344 Changed |=
sink(I, LI, DT, CurLoop, CurAST);
359 assert(N !=
nullptr && AA !=
nullptr && LI !=
nullptr &&
360 DT !=
nullptr && CurLoop !=
nullptr && CurAST !=
nullptr &&
361 SafetyInfo !=
nullptr &&
"Unexpected input to hoistRegion");
363 bool Changed =
false;
367 if (!CurLoop->
contains(BB))
return Changed;
378 DEBUG(
dbgs() <<
"LICM folding inst: " << I <<
" --> " << *C <<
'\n');
397 const std::vector<DomTreeNode*> &Children = N->
getChildren();
398 for (
unsigned i = 0, e = Children.size(); i != e; ++i)
400 hoistRegion(Children[i], AA, LI, DT, TLI, CurLoop, CurAST, SafetyInfo);
408 assert(CurLoop !=
nullptr &&
"CurLoop cant be null");
434 if (
LoadInst *LI = dyn_cast<LoadInst>(&I)) {
435 if (!LI->isUnordered())
447 if (LI->getType()->isSized())
451 LI->getAAMetadata(AAInfo);
454 }
else if (
CallInst *CI = dyn_cast<CallInst>(&I)) {
456 if (isa<DbgInfoIntrinsic>(I))
466 bool FoundMod =
false;
475 if (!FoundMod)
return true;
485 if (!isa<BinaryOperator>(I) && !isa<CastInst>(
I) && !isa<SelectInst>(I) &&
486 !isa<GetElementPtrInst>(
I) && !isa<CmpInst>(I) &&
487 !isa<InsertElementInst>(
I) && !isa<ExtractElementInst>(I) &&
488 !isa<ShuffleVectorInst>(
I) && !isa<ExtractValueInst>(I) &&
489 !isa<InsertValueInst>(
I))
519 if (
const PHINode *PN = dyn_cast<PHINode>(UI)) {
533 for (
unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
534 if (PN->getIncomingValue(i) == &
I)
535 if (CurLoop->
contains(PN->getIncomingBlock(i)))
564 if (
Instruction *OInst = dyn_cast<Instruction>(*OI))
566 if (!OLoop->contains(&PN)) {
569 OInst->getName() +
".lcssa", ExitBlock.
begin());
584 DEBUG(
dbgs() <<
"LICM sinking instruction: " << I <<
"\n");
585 bool Changed =
false;
586 if (isa<LoadInst>(I)) ++NumMovedLoads;
587 else if (isa<CallInst>(I)) ++NumMovedCalls;
606 auto *
User = cast<Instruction>(*UI);
617 Use &U = UI.getUse();
625 assert(ExitBlockSet.count(ExitBlock) &&
626 "The LCSSA PHI is not in an exit block!");
629 auto It = SunkCopies.find(ExitBlock);
630 if (It != SunkCopies.
end())
633 New = SunkCopies[ExitBlock] =
654 if (isa<LoadInst>(I)) ++NumMovedLoads;
655 else if (isa<CallInst>(I)) ++NumMovedCalls;
702 for (
unsigned i = 0, e = ExitBlocks.
size(); i != e; ++i)
708 if (ExitBlocks.
empty())
729 if (
Loop *L = LI.getLoopFor(
I->getParent()))
734 I->getType(), PredCache.size(BB),
735 I->getName() +
".lcssa", BB->
begin());
752 LoopExitBlocks(LEB), LoopInsertPts(LIP), PredCache(PIC), AST(ast),
753 LI(li),
DL(dl), Alignment(alignment), AATags(AATags) {}
758 if (
LoadInst *LI = dyn_cast<LoadInst>(I))
759 Ptr = LI->getOperand(0);
762 return PointerMustAliases.count(Ptr);
765 void doExtraRewritesBeforeFinalDeletion()
const override {
770 for (
unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) {
772 Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
773 LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock);
774 Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock);
783 void replaceLoadWithValue(
LoadInst *LI,
Value *V)
const override {
785 AST.copyValue(LI, V);
787 void instructionDeleted(
Instruction *I)
const override {
806 assert(LI !=
nullptr && DT !=
nullptr &&
807 CurLoop !=
nullptr && CurAST !=
nullptr &&
808 SafetyInfo !=
nullptr &&
809 "Unexpected Input to promoteLoopAccessesToScalars");
811 bool Changed =
false;
819 assert(!AS.
empty() &&
820 "Must alias set should have at least one pointer element in it!");
838 bool GuaranteedToExecute =
false;
845 unsigned Alignment = 1;
853 Value *ASIV = ASI->getValue();
854 PointerMustAliases.
insert(ASIV);
859 if (SomePtr->getType() != ASIV->
getType())
870 if (
const LoadInst *load = dyn_cast<LoadInst>(UI)) {
871 assert(!load->isVolatile() &&
"AST broken");
872 if (!load->isSimple())
874 }
else if (
const StoreInst *store = dyn_cast<StoreInst>(UI)) {
879 assert(!store->isVolatile() &&
"AST broken");
880 if (!store->isSimple())
887 if (!HasDedicatedExits || !Preheader)
898 unsigned InstAlignment = store->getAlignment();
899 if ((InstAlignment > Alignment || InstAlignment == 0) && Alignment != 0)
901 GuaranteedToExecute =
true;
902 Alignment = InstAlignment;
905 if (!GuaranteedToExecute)
907 CurLoop, SafetyInfo);
913 if (LoopUses.
empty()) {
925 if (!GuaranteedToExecute)
929 DEBUG(
dbgs() <<
"LICM: Promoting value stored to in loop: " <<*SomePtr<<
'\n');
941 if (ExitBlocks.
empty()) {
944 for (
unsigned i = 0, e = ExitBlocks.
size(); i != e; ++i)
945 InsertPts[i] = ExitBlocks[i]->getFirstInsertionPt();
951 LoopPromoter Promoter(SomePtr, LoopUses, SSA,
952 PointerMustAliases, ExitBlocks,
953 InsertPts, PIC, *CurAST, *LI, DL, Alignment, AATags);
958 new LoadInst(SomePtr, SomePtr->getName()+
".promoted",
967 Promoter.run(LoopUses);
988 void LICM::deleteAnalysisValue(
Value *V,
Loop *L) {
998 void LICM::deleteAnalysisLoop(
Loop *L) {
1004 LoopToAliasSetMap.erase(L);
1022 assert(CurLoop->
contains(BB) &&
"Only valid if BB is IN the loop");
Pass interface - Implemented by all 'passes'.
static bool isGuaranteedToExecute(const Instruction &Inst, const DominatorTree *DT, const Loop *CurLoop, const LICMSafetyInfo *SafetyInfo)
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
void push_back(const T &Elt)
void computeLICMSafetyInfo(LICMSafetyInfo *, Loop *)
Computes safety information for a loop checks loop body & header for the possiblity of may throw exce...
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Helper class for SSA formation on a set of values defined in multiple blocks.
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool isForwardingAliasSet() const
isForwardingAliasSet - Return true if this alias set should be ignored as part of the AliasSetTracker...
bool sinkRegion(DomTreeNode *, AliasAnalysis *, LoopInfo *, DominatorTree *, TargetLibraryInfo *, Loop *, AliasSetTracker *, LICMSafetyInfo *)
Walk the specified region of the CFG (defined by all blocks dominated by the specified block...
STATISTIC(NumFunctions,"Total number of functions")
Define an iterator for alias sets... this is just a forward iterator.
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value...
ScalarEvolution - This class is the main scalar evolution driver.
ModRefBehavior
ModRefBehavior - Summary of how a function affects memory in the program.
bool hoistRegion(DomTreeNode *, AliasAnalysis *, LoopInfo *, DominatorTree *, TargetLibraryInfo *, Loop *, AliasSetTracker *, LICMSafetyInfo *)
Walk the specified region of the CFG (defined by all blocks dominated by the specified block...
CallInst - This class represents a function call, abstracting a target machine's calling convention...
static bool canSinkOrHoistInst(Instruction &I, AliasAnalysis *AA, DominatorTree *DT, TargetLibraryInfo *TLI, Loop *CurLoop, AliasSetTracker *CurAST, LICMSafetyInfo *SafetyInfo)
canSinkOrHoistInst - Return true if the hoister and sinker can handle this instruction.
Captures loop safety information.
static cl::opt< bool > DisablePromotion("disable-licm-promotion", cl::Hidden, cl::desc("Disable memory promotion in LICM pass"))
LoopT * getParentLoop() const
bool hasLoopInvariantOperands(const Instruction *I) const
hasLoopInvariantOperands - Return true if all the operands of the specified instruction are loop inva...
LoadInst - an instruction for reading from memory.
LoopT * getLoopFor(const BlockT *BB) const
getLoopFor - Return the inner most loop that BB lives in.
BlockT * getHeader() const
static bool isTriviallyReplacablePHI(const PHINode &PN, const Instruction &I)
Returns true if a PHINode is a trivially replaceable with an Instruction.
virtual bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
pointsToConstantMemory - If the specified memory location is known to be constant, return true.
StringRef getName() const
Return a constant reference to the value's name.
AliasSet & getAliasSetForPointer(Value *P, uint64_t Size, const AAMDNodes &AAInfo, bool *New=nullptr)
getAliasSetForPointer - Return the alias set that the specified pointer lives in. ...
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
iterator begin()
Instruction iterator methods.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
static Value * getPointerOperand(Instruction &Inst)
static Instruction * CloneInstructionInExitBlock(const Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI)
const_iterator end() const
A Use represents the edge between a Value definition and its users.
virtual bool doFinalization(Module &)
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
void setName(const Twine &Name)
Change the name of the value.
bool isLoopInvariant(const Value *V) const
isLoopInvariant - Return true if the specified value is loop invariant
Instruction * clone() const
clone() - Create a copy of 'this' instruction that is identical in all ways except the following: ...
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
getExitBlocks - Return all of the successor blocks of this loop.
bool promoteLoopAccessesToScalars(AliasSet &, SmallVectorImpl< BasicBlock * > &, SmallVectorImpl< Instruction * > &, PredIteratorCache &, LoopInfo *, DominatorTree *, Loop *, AliasSetTracker *, LICMSafetyInfo *)
Try to promote memory values to scalars by sinking stores out of the loop and moving loads to before ...
const_iterator begin() const
user_iterator_impl< User > user_iterator
static bool pointerInvalidatedByLoop(Value *V, uint64_t Size, const AAMDNodes &AAInfo, AliasSetTracker *CurAST)
Return true if the body of this loop may store into the memory location pointed to by V...
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
PredIteratorCache - This class is an extremely trivial cache for predecessor iterator queries...
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Base class for the actual dominator tree node.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
uint64_t getTypeStoreSize(Type *Ty)
getTypeStoreSize - Return the DataLayout store size for the given type, if known, or a conservative v...
AnalysisUsage & addPreservedID(const void *ID)
StoreInst - an instruction for storing to memory.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
static bool hoist(Instruction &I, BasicBlock *Preheader)
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.
bool onlyReadsMemory(ImmutableCallSite CS)
onlyReadsMemory - If the specified call is known to only read from non-volatile memory (or not access...
unsigned getNumIncomingValues() const
getNumIncomingValues - Return the number of incoming edges
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
void setDebugLoc(DebugLoc Loc)
setDebugLoc - Set the debug location information for this instruction.
BlockT * getLoopPreheader() const
getLoopPreheader - If there is a preheader for this loop, return it.
void setAAMetadata(const AAMDNodes &N)
setAAMetadata - Sets the metadata on this instruction from the AAMDNodes structure.
LLVM Basic Block Representation.
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
Represent the analysis usage information of a pass.
BasicBlock * getIncomingBlock(unsigned i) const
getIncomingBlock - Return incoming basic block number i.
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
const InstListType & getInstList() const
Return the underlying instruction list container.
iterator insert(iterator where, NodeTy *New)
Value * getOperand(unsigned i) const
const std::vector< DomTreeNodeBase< NodeT > * > & getChildren() const
void setAlignment(unsigned Align)
void initializeLICMPass(PassRegistry &)
#define INITIALIZE_AG_DEPENDENCY(depName)
bool hasDedicatedExits() const
hasDedicatedExits - Return true if no exit block for the loop has a predecessor that is outside the l...
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
void getUniqueExitBlocks(SmallVectorImpl< BasicBlock * > &ExitBlocks) const
getUniqueExitBlocks - Return all unique successor blocks of this loop.
virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS)
getModRefBehavior - Return the behavior when calling the given call site.
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
AnalysisUsage & addRequiredID(const void *ID)
void copyValue(Value *From, Value *To)
copyValue - This method should be used whenever a preexisting value in the program is copied or clone...
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
bool isLCSSAForm(DominatorTree &DT) const
isLCSSAForm - Return true if the Loop is in LCSSA form
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
Provides information about what library functions are available for the current target.
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
static bool sink(Instruction &I, const LoopInfo *LI, const DominatorTree *DT, const Loop *CurLoop, AliasSetTracker *CurAST)
When an instruction is found to only be used outside of the loop, this function moves it to the exit ...
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
iterator_range< user_iterator > users()
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
std::vector< BlockT * >::const_iterator block_iterator
bool formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE=nullptr)
Put a loop nest into LCSSA form.
block_iterator block_end() const
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
void getAAMetadata(AAMDNodes &N, bool Merge=false) const
getAAMetadata - Fills the AAMDNodes structure with AA metadata from this instruction.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
DoesNotAccessMemory - This function does not perform any non-local loads or stores to memory...
Machine Loop Invariant Code Motion
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...
user_iterator user_begin()
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
isInstructionTriviallyDead - Return true if the result produced by the instruction is not used...
LLVM Value Representation.
void setAlignment(unsigned Align)
static bool isSafeToExecuteUnconditionally(const Instruction &Inst, const DominatorTree *DT, const TargetLibraryInfo *TLI, const Loop *CurLoop, const LICMSafetyInfo *SafetyInfo, const Instruction *CtxI=nullptr)
Only sink or hoist an instruction if it is not a trapping instruction, or if the instruction is known...
static bool isNotUsedInLoop(const Instruction &I, const Loop *CurLoop)
Return true if the only users of this instruction are outside of the loop.
void moveBefore(Instruction *MovePos)
moveBefore - Unlink this instruction from its current basic block and insert it into the basic block ...
block_iterator block_begin() const
The legacy pass manager's analysis pass to compute loop information.
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
isSafeToSpeculativelyExecute - Return true if the instruction does not have any effects besides calcu...
Legacy analysis pass which computes a DominatorTree.
std::vector< LoopT * >::const_iterator iterator
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
op_range incoming_values()
const BasicBlock * getParent() const
void deleteValue(Value *PtrVal)
deleteValue method - This method is used to remove a pointer value from the AliasSetTracker entirely...
bool empty() const
empty - Check if the string is empty.