LLVM  mainline
Defines | Functions | Variables
LoopSimplify.cpp File Reference
#include "llvm/Transforms/Scalar.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/DependenceAnalysis.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
Include dependency graph for LoopSimplify.cpp:

Go to the source code of this file.

Defines

#define DEBUG_TYPE   "loop-simplify"

Functions

 STATISTIC (NumInserted,"Number of pre-header or exit blocks inserted")
 STATISTIC (NumNested,"Number of nested loops split out")
static void placeSplitBlockCarefully (BasicBlock *NewBB, SmallVectorImpl< BasicBlock * > &SplitPreds, Loop *L)
static BasicBlockrewriteLoopExitBlock (Loop *L, BasicBlock *Exit, AliasAnalysis *AA, DominatorTree *DT, LoopInfo *LI, Pass *PP)
 Ensure that the loop preheader dominates all exit blocks.
static void addBlockAndPredsToSet (BasicBlock *InputBB, BasicBlock *StopBlock, std::set< BasicBlock * > &Blocks)
 Add the specified block, and all of its predecessors, to the specified set, if it's not already in there.
static PHINodefindPHIToPartitionLoops (Loop *L, AliasAnalysis *AA, DominatorTree *DT, AssumptionCache *AC)
 The first part of loop-nestification is to find a PHI node that tells us how to partition the loops.
static LoopseparateNestedLoop (Loop *L, BasicBlock *Preheader, AliasAnalysis *AA, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, Pass *PP, AssumptionCache *AC)
 If this loop has multiple backedges, try to pull one of them out into a nested loop.
static BasicBlockinsertUniqueBackedgeBlock (Loop *L, BasicBlock *Preheader, AliasAnalysis *AA, DominatorTree *DT, LoopInfo *LI)
 This method is called when the specified loop has more than one backedge in it.
static bool simplifyOneLoop (Loop *L, SmallVectorImpl< Loop * > &Worklist, AliasAnalysis *AA, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, Pass *PP, AssumptionCache *AC)
 Simplify one loop and queue further loops for simplification.
 INITIALIZE_PASS_BEGIN (LoopSimplify,"loop-simplify","Canonicalize natural loops", false, false) INITIALIZE_PASS_END(LoopSimplify

Variables

loop simplify
loop Canonicalize natural loops
loop Canonicalize natural false

Define Documentation

#define DEBUG_TYPE   "loop-simplify"

Definition at line 69 of file LoopSimplify.cpp.


Function Documentation

static void addBlockAndPredsToSet ( BasicBlock InputBB,
BasicBlock StopBlock,
std::set< BasicBlock * > &  Blocks 
) [static]

Add the specified block, and all of its predecessors, to the specified set, if it's not already in there.

Stop predecessor traversal when we reach StopBlock.

Definition at line 193 of file LoopSimplify.cpp.

References llvm::SmallVectorBase::empty(), I, llvm::SmallVectorImpl< T >::pop_back_val(), llvm::pred_begin(), llvm::pred_end(), and llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back().

Referenced by separateNestedLoop().

static PHINode* findPHIToPartitionLoops ( Loop L,
AliasAnalysis AA,
DominatorTree DT,
AssumptionCache AC 
) [static]
INITIALIZE_PASS_BEGIN ( LoopSimplify  ,
"loop-simplify ,
"Canonicalize natural loops ,
false  ,
false   
)
static BasicBlock* insertUniqueBackedgeBlock ( Loop L,
BasicBlock Preheader,
AliasAnalysis AA,
DominatorTree DT,
LoopInfo LI 
) [static]
static void placeSplitBlockCarefully ( BasicBlock NewBB,
SmallVectorImpl< BasicBlock * > &  SplitPreds,
Loop L 
) [static]
static BasicBlock* rewriteLoopExitBlock ( Loop L,
BasicBlock Exit,
AliasAnalysis AA,
DominatorTree DT,
LoopInfo LI,
Pass PP 
) [static]

Ensure that the loop preheader dominates all exit blocks.

This method is used to split exit blocks that have predecessors outside of the loop.

Definition at line 163 of file LoopSimplify.cpp.

References llvm::LoopBase< BlockT, LoopT >::contains(), llvm::dbgs(), DEBUG, llvm::SmallVectorBase::empty(), llvm::BasicBlock::getTerminator(), I, llvm::LCSSAID, llvm::Pass::mustPreserveAnalysisID(), P, llvm::pred_begin(), llvm::pred_end(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), and llvm::SplitBlockPredecessors().

Referenced by simplifyOneLoop().

static Loop* separateNestedLoop ( Loop L,
BasicBlock Preheader,
AliasAnalysis AA,
DominatorTree DT,
LoopInfo LI,
ScalarEvolution SE,
Pass PP,
AssumptionCache AC 
) [static]

If this loop has multiple backedges, try to pull one of them out into a nested loop.

This is important for code that looks like this:

Loop: ... br cond, Loop, Next ... br cond2, Loop, Out

To identify this common case, we look at the PHI nodes in the header of the loop. PHI nodes with unchanging values on one backedge correspond to values that change in the "outer" loop, but not in the "inner" loop.

If we are able to separate out a loop, return the new outer loop that was created.

Definition at line 255 of file LoopSimplify.cpp.

References addBlockAndPredsToSet(), llvm::LoopBase< BlockT, LoopT >::addBlockEntry(), llvm::LoopBase< BlockT, LoopT >::addChildLoop(), llvm::LoopBase< BlockT, LoopT >::block_begin(), llvm::LoopBase< BlockT, LoopT >::block_end(), llvm::LoopInfoBase< BlockT, LoopT >::changeLoopFor(), llvm::LoopInfoBase< BlockT, LoopT >::changeTopLevelLoop(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::dbgs(), DEBUG, llvm::DominatorTree::dominates(), findPHIToPartitionLoops(), llvm::ScalarEvolution::forgetLoop(), llvm::LoopBase< BlockT, LoopT >::getBlocks(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::LoopBase< BlockT, LoopT >::getParentLoop(), llvm::LoopBase< BlockT, LoopT >::getSubLoops(), I, llvm::BasicBlock::isLandingPad(), llvm::LCSSAID, llvm::LoopBase< BlockT, LoopT >::moveToHeader(), llvm::Pass::mustPreserveAnalysisID(), P, placeSplitBlockCarefully(), llvm::pred_begin(), llvm::pred_end(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::LoopBase< BlockT, LoopT >::removeBlockFromLoop(), llvm::LoopBase< BlockT, LoopT >::removeChildLoop(), and llvm::SplitBlockPredecessors().

Referenced by simplifyOneLoop().

static bool simplifyOneLoop ( Loop L,
SmallVectorImpl< Loop * > &  Worklist,
AliasAnalysis AA,
DominatorTree DT,
LoopInfo LI,
ScalarEvolution SE,
Pass PP,
AssumptionCache AC 
) [static]

Simplify one loop and queue further loops for simplification.

FIXME: Currently this accepts both lots of analyses that it uses and a raw Pass pointer. The Pass pointer is used by numerous utilities to update specific analyses. Rather than a pass it would be much cleaner and more explicit if they accepted the analysis directly and then updated it.

Definition at line 480 of file LoopSimplify.cpp.

References llvm::SmallVectorTemplateCommon< T >::begin(), llvm::BasicBlock::begin(), llvm::LoopBase< BlockT, LoopT >::block_begin(), llvm::LoopBase< BlockT, LoopT >::block_end(), llvm::DominatorTreeBase< NodeT >::changeImmediateDominator(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::dbgs(), DEBUG, llvm::AliasAnalysis::deleteValue(), llvm::DL, llvm::dyn_cast(), llvm::SmallVectorBase::empty(), llvm::SmallVectorTemplateCommon< T >::end(), llvm::Instruction::eraseFromParent(), llvm::BasicBlock::eraseFromParent(), llvm::DominatorTreeBase< NodeT >::eraseNode(), llvm::FoldBranchToCommonDest(), llvm::ScalarEvolution::forgetLoop(), llvm::ScalarEvolution::forgetLoopDispositions(), llvm::ScalarEvolution::forgetValue(), llvm::ConstantInt::get(), llvm::UndefValue::get(), llvm::DomTreeNodeBase< NodeT >::getChildren(), llvm::BranchInst::getCondition(), llvm::Module::getDataLayout(), llvm::LoopBase< BlockT, LoopT >::getExitBlocks(), llvm::LoopBase< BlockT, LoopT >::getExitingBlocks(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::DomTreeNodeBase< NodeT >::getIDom(), llvm::LoopBase< BlockT, LoopT >::getLoopLatch(), llvm::LoopBase< BlockT, LoopT >::getLoopPreheader(), llvm::BasicBlock::getModule(), llvm::Value::getName(), llvm::DominatorTreeBase< NodeT >::getNode(), llvm::LoopBase< BlockT, LoopT >::getNumBackEdges(), llvm::Instruction::getParent(), llvm::BasicBlock::getSinglePredecessor(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::InsertPreheaderForLoop(), insertUniqueBackedgeBlock(), llvm::BranchInst::isConditional(), llvm::Loop::makeLoopInvariant(), P, llvm::pred_begin(), llvm::pred_end(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::LoopInfoBase< BlockT, LoopT >::removeBlock(), llvm::BasicBlock::removePredecessor(), llvm::Value::replaceAllUsesWith(), rewriteLoopExitBlock(), separateNestedLoop(), llvm::SI, llvm::SimplifyInstruction(), llvm::SmallVectorTemplateCommon< T >::size(), llvm::succ_begin(), and llvm::succ_end().

Referenced by llvm::simplifyLoop().

STATISTIC ( NumInserted  ,
"Number of pre-header or exit blocks inserted"   
)
STATISTIC ( NumNested  ,
"Number of nested loops split out"   
)

Variable Documentation

loop Canonicalize natural false

Definition at line 789 of file LoopSimplify.cpp.

loop Canonicalize natural loops

Definition at line 789 of file LoopSimplify.cpp.

loop simplify

Definition at line 789 of file LoopSimplify.cpp.