28 #define DEBUG_TYPE "loop-delete"
30 STATISTIC(NumDeleted,
"Number of loops deleted");
47 bool AllEntriesInvariant =
true;
48 bool AllOutgoingValuesSame =
true;
49 while (
PHINode *
P = dyn_cast<PHINode>(BI)) {
50 Value *incoming =
P->getIncomingValueForBlock(exitingBlocks[0]);
56 AllOutgoingValuesSame =
58 return incoming ==
P->getIncomingValueForBlock(BB);
61 if (!AllOutgoingValuesSame)
66 AllEntriesInvariant =
false;
76 if (!AllEntriesInvariant || !AllOutgoingValuesSame)
86 if (
I.mayHaveSideEffects())
129 if (exitBlocks.
size() != 1)
133 bool Changed =
false;
134 if (!isLoopDead(L, SE, exitingBlocks, exitBlocks, Changed, preheader))
140 if (isa<SCEVCouldNotCompute>(S))
164 while (
PHINode *
P = dyn_cast<PHINode>(BI)) {
165 int j =
P->getBasicBlockIndex(exitingBlock);
166 assert(j >= 0 &&
"Can't find exiting block in exit block's phi node!");
167 P->setIncomingBlock(j, preheader);
168 for (
unsigned i = 1;
i < exitingBlocks.
size(); ++
i)
169 P->removeIncomingValue(exitingBlocks[
i]);
180 ChildNodes.
insert(ChildNodes.
begin(), DT[*LI]->begin(), DT[*LI]->end());
190 (*LI)->dropAllReferences();
199 (*LI)->eraseFromParent();
229 class LoopDeletionLegacyPass :
public LoopPass {
247 "Delete dead loops",
false,
false)
258 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
259 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
260 LoopInfo &loopInfo = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
263 return Impl.
runImpl(L, DT, SE, loopInfo);
Pass interface - Implemented by all 'passes'.
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.
bool runImpl(Loop *L, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &loopInfo)
Remove dead loops, by which we mean loops that do not impact the observable behavior of the program o...
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 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
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
iterator begin()
Instruction iterator methods.
#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.
Base class for the actual dominator tree node.
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.
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Subclasses of this class are all able to terminate a basic block.
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.
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
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.
Represent the analysis usage information of a pass.
const SCEV * getMaxBackedgeTakenCount(const Loop *L)
Similar to getBackedgeTakenCount, except return the least SCEV value that is known never to be less t...
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.
static BasicBlock * preheader(DominatorTree *DT, Loop *L)
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void getUniqueExitBlocks(SmallVectorImpl< BasicBlock * > &ExitBlocks) const
Return all unique successor blocks of this loop.
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...
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...
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.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
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.