29 #define DEBUG_TYPE "loop-delete"
31 STATISTIC(NumDeleted,
"Number of loops deleted");
61 bool AllEntriesInvariant =
true;
62 bool AllOutgoingValuesSame =
true;
63 while (
PHINode *
P = dyn_cast<PHINode>(BI)) {
64 Value *incoming =
P->getIncomingValueForBlock(ExitingBlocks[0]);
70 AllOutgoingValuesSame =
72 return incoming ==
P->getIncomingValueForBlock(BB);
75 if (!AllOutgoingValuesSame)
80 AllEntriesInvariant =
false;
90 if (!AllEntriesInvariant || !AllOutgoingValuesSame)
106 using namespace PatternMatch;
111 assert(Preheader &&
"Needs preheader!");
113 if (Preheader == &Preheader->getParent()->getEntryBlock())
120 if (!
match(Pred->getTerminator(),
125 if (Taken == Preheader)
129 "Preheader should have predecessors at this point!");
157 <<
"Deletion requires Loop with preheader and dedicated exits.\n");
163 DEBUG(
dbgs() <<
"Loop contains subloops.\n");
171 DEBUG(
dbgs() <<
"Loop is proven to never execute, delete it!");
174 while (
PHINode *
P = dyn_cast<PHINode>(BI)) {
175 for (
unsigned i = 0; i <
P->getNumIncomingValues(); i++)
194 DEBUG(
dbgs() <<
"Deletion requires single exit block\n");
198 bool Changed =
false;
199 if (!
isLoopDead(L, SE, ExitingBlocks, ExitBlock, Changed, Preheader)) {
200 DEBUG(
dbgs() <<
"Loop is not invariant, cannot delete.\n");
207 if (isa<SCEVCouldNotCompute>(S)) {
208 DEBUG(
dbgs() <<
"Could not compute SCEV MaxBackedgeTakenCount.\n");
212 DEBUG(
dbgs() <<
"Loop is invariant, delete it!");
223 assert(Preheader &&
"Preheader should exist!");
241 assert(ExitBlock &&
"Should have a unique exit block!");
252 Preheader->getTerminator()->replaceUsesOfWith(L->
getHeader(), ExitBlock);
257 while (
PHINode *
P = dyn_cast<PHINode>(BI)) {
262 P->setIncomingBlock(PredIndex, Preheader);
270 for (
unsigned i = 0, e =
P->getNumIncomingValues() - 1; i != e; ++i)
271 P->removeIncomingValue(e-i,
false);
273 assert((
P->getNumIncomingValues() == 1 &&
274 P->getIncomingBlock(PredIndex) == Preheader) &&
275 "Should have exactly one value and that's from the preheader!");
286 ChildNodes.
insert(ChildNodes.
begin(), DT[*LI]->begin(), DT[*LI]->end());
296 (*LI)->dropAllReferences();
305 (*LI)->eraseFromParent();
323 DEBUG(
dbgs() <<
"Analyzing Loop for deletion: ");
332 class LoopDeletionLegacyPass :
public LoopPass {
350 "Delete dead loops",
false,
false)
360 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
361 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
362 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
364 DEBUG(
dbgs() <<
"Analyzing Loop for deletion: ");
Pass interface - Implemented by all 'passes'.
BasicBlock * getUniqueExitBlock() const
If getUniqueExitBlocks would return exactly one block, return that block.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
STATISTIC(NumFunctions,"Total number of functions")
This header provides classes for managing a pipeline of passes over loops in LLVM IR...
INITIALIZE_PASS_BEGIN(LoopDeletionLegacyPass,"loop-deletion","Delete dead loops", false, false) INITIALIZE_PASS_END(LoopDeletionLegacyPass
This is the interface for a simple mod/ref and alias analysis over globals.
void initializeLoopDeletionLegacyPassPass(PassRegistry &)
The main scalar evolution driver.
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
BlockT * getHeader() const
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
iterator begin()
Instruction iterator methods.
bool match(Val *V, const Pattern &P)
#define INITIALIZE_PASS_DEPENDENCY(depName)
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
static bool isLoopDead(Loop *L, ScalarEvolution &SE, SmallVectorImpl< BasicBlock * > &ExitingBlocks, BasicBlock *ExitBlock, bool &Changed, BasicBlock *Preheader)
Determines if a loop is dead.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
static bool deleteLoopIfDead(Loop *L, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI, LPMUpdater *Updater=nullptr)
Remove a loop if it is dead.
Base class for the actual dominator tree node.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
void forgetLoopDispositions(const Loop *L)
Called when the client has changed the disposition of values in this loop.
iterator_range< block_iterator > blocks() const
static void deleteDeadLoop(Loop *L, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI, LPMUpdater *Updater=nullptr)
This function deletes dead loops.
A set of analyses that are preserved following a run of a transformation pass.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
LLVM Basic Block Representation.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
brc_match< Cond_t > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
Represent the analysis usage information of a pass.
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
const SCEV * getMaxBackedgeTakenCount(const Loop *L)
When successful, this returns a SCEVConstant that is greater than or equal to (i.e.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
void markAsRemoved(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
bool pred_empty(const BasicBlock *BB)
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
static bool isLoopNeverExecuted(Loop *L)
This function returns true if there is no viable path from the entry block to the header of L...
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is the shared class of boolean and integer constants.
bool makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt=nullptr) const
If the given value is an instruction inside of the loop and it can be hoisted, do so to make it trivi...
bool isLCSSAForm(DominatorTree &DT) const
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...
pred_range predecessors(BasicBlock *BB)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Pass * createLoopDeletionPass()
std::vector< BlockT * >::const_iterator block_iterator
iterator insert(iterator I, T &&Elt)
block_iterator block_end() const
void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
This class represents an analyzed expression in the program.
Represents a single loop in the control flow graph.
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
block_iterator block_begin() const
A container for analyses that lazily runs them and caches their results.
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
void markLoopAsDeleted(Loop &L)
Loop passes should use this method to indicate they have deleted a loop from the nest.