LLVM  5.0.0
Macros | Functions | Variables
LoopDeletion.cpp File Reference
#include "llvm/Transforms/Scalar/LoopDeletion.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/LoopPassManager.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
Include dependency graph for LoopDeletion.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "loop-delete"
 

Functions

 STATISTIC (NumDeleted,"Number of loops deleted")
 
static void deleteDeadLoop (Loop *L, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI, LPMUpdater *Updater=nullptr)
 This function deletes dead loops. More...
 
static bool isLoopDead (Loop *L, ScalarEvolution &SE, SmallVectorImpl< BasicBlock * > &ExitingBlocks, BasicBlock *ExitBlock, bool &Changed, BasicBlock *Preheader)
 Determines if a loop is dead. More...
 
static bool isLoopNeverExecuted (Loop *L)
 This function returns true if there is no viable path from the entry block to the header of L. More...
 
static bool deleteLoopIfDead (Loop *L, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI, LPMUpdater *Updater=nullptr)
 Remove a loop if it is dead. More...
 
 INITIALIZE_PASS_BEGIN (LoopDeletionLegacyPass,"loop-deletion","Delete dead loops", false, false) INITIALIZE_PASS_END(LoopDeletionLegacyPass
 

Variables

loop deletion
 
loop Delete dead loops
 
loop Delete dead false
 

Macro Definition Documentation

#define DEBUG_TYPE   "loop-delete"

Definition at line 29 of file LoopDeletion.cpp.

Function Documentation

static void deleteDeadLoop ( Loop *  L,
DominatorTree &  DT,
ScalarEvolution &  SE,
LoopInfo &  LI,
LPMUpdater *  Updater = nullptr 
)
static

This function deletes dead loops.

The caller of this function needs to guarantee that the loop is infact dead. Here we handle two kinds of dead loop. The first kind (isLoopDead) is where only invariant values from within the loop are used outside of it. The second kind (isLoopNeverExecuted) is where the loop is provably never executed. We can always remove never executed loops since they will not cause any difference to program behaviour.

This also updates the relevant analysis information in DT, SE, and LI. It also updates the loop PM if an updater struct is provided.

Definition at line 219 of file LoopDeletion.cpp.

References assert(), llvm::SmallVectorTemplateCommon< T >::begin(), llvm::LoopBase< N, M >::block_begin(), llvm::LoopBase< N, M >::block_end(), llvm::DominatorTreeBase< N, IsPostDom >::changeImmediateDominator(), llvm::DominatorTreeBase< N, IsPostDom >::eraseNode(), llvm::ScalarEvolution::forgetLoop(), llvm::LoopBase< N, M >::getHeader(), llvm::LoopBase< N, M >::getLoopPreheader(), llvm::Loop::getUniqueExitBlock(), llvm::Loop::hasDedicatedExits(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::SmallVectorImpl< T >::insert(), llvm::Loop::isLCSSAForm(), llvm::AArch64CC::LE, llvm::LoopInfo::markAsRemoved(), llvm::LPMUpdater::markLoopAsDeleted(), P, and llvm::LoopInfoBase< N, M >::removeBlock().

Referenced by deleteLoopIfDead().

static bool deleteLoopIfDead ( Loop *  L,
DominatorTree &  DT,
ScalarEvolution &  SE,
LoopInfo &  LI,
LPMUpdater *  Updater = nullptr 
)
static

Remove a loop if it is dead.

A loop is considered dead if it does not impact the observable behavior of the program other than finite running time. This never removes a loop that might be infinite (unless it is never executed), as doing so could change the halting/non-halting nature of a program.

This entire process relies pretty heavily on LoopSimplify form and LCSSA in order to make various safety checks work.

Returns
true if any changes were made. This may mutate the loop even if it is unable to delete it due to hoisting trivially loop invariant instructions out of the loop.

Definition at line 147 of file LoopDeletion.cpp.

References assert(), llvm::LoopBase< N, M >::begin(), llvm::BasicBlock::begin(), llvm::dbgs(), DEBUG, deleteDeadLoop(), llvm::LoopBase< N, M >::end(), llvm::UndefValue::get(), llvm::LoopBase< N, M >::getExitingBlocks(), llvm::LoopBase< N, M >::getLoopPreheader(), llvm::ScalarEvolution::getMaxBackedgeTakenCount(), llvm::Loop::getUniqueExitBlock(), llvm::Loop::hasDedicatedExits(), llvm::Loop::isLCSSAForm(), isLoopDead(), isLoopNeverExecuted(), and P.

Referenced by llvm::LoopDeletionPass::run().

INITIALIZE_PASS_BEGIN ( LoopDeletionLegacyPass  ,
"loop-deletion"  ,
"Delete dead loops"  ,
false  ,
false   
)
static bool isLoopDead ( Loop *  L,
ScalarEvolution &  SE,
SmallVectorImpl< BasicBlock * > &  ExitingBlocks,
BasicBlock *  ExitBlock,
bool &  Changed,
BasicBlock *  Preheader 
)
static

Determines if a loop is dead.

This assumes that we've already checked for unique exit and exiting blocks, and that the code is in LCSSA form.

Definition at line 51 of file LoopDeletion.cpp.

References llvm::all_of(), llvm::any_of(), llvm::BasicBlock::begin(), llvm::LoopBase< N, M >::blocks(), llvm::ScalarEvolution::forgetLoopDispositions(), llvm::BasicBlock::getTerminator(), I, llvm::makeArrayRef(), llvm::Loop::makeLoopInvariant(), llvm::Instruction::mayHaveSideEffects(), and P.

Referenced by deleteLoopIfDead().

static bool isLoopNeverExecuted ( Loop *  L)
static

This function returns true if there is no viable path from the entry block to the header of L.

Right now, it only does a local search to save compile time.

Definition at line 105 of file LoopDeletion.cpp.

References assert(), llvm::LoopBase< N, M >::getLoopPreheader(), llvm::ConstantInt::getZExtValue(), llvm::PatternMatch::m_Br(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::match(), llvm::pred_empty(), llvm::predecessors(), and std::swap().

Referenced by deleteLoopIfDead().

STATISTIC ( NumDeleted  ,
"Number of loops deleted"   
)

Variable Documentation

loop deletion

Definition at line 352 of file LoopDeletion.cpp.

loop Delete dead false

Definition at line 352 of file LoopDeletion.cpp.

loop Delete dead loops

Definition at line 352 of file LoopDeletion.cpp.