LLVM  5.0.0
Macros | Enumerations | Functions | Variables
LoopUnswitch.cpp File Reference
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/CodeMetrics.h"
#include "llvm/Analysis/DivergenceAnalysis.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
#include <algorithm>
#include <map>
#include <set>
Include dependency graph for LoopUnswitch.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "loop-unswitch"
 

Enumerations

enum  OperatorChain { OC_OpChainNone, OC_OpChainOr, OC_OpChainAnd, OC_OpChainMixed }
 Operator chain lattice. More...
 

Functions

 STATISTIC (NumBranches,"Number of branches unswitched")
 
 STATISTIC (NumSwitches,"Number of switches unswitched")
 
 STATISTIC (NumGuards,"Number of guards unswitched")
 
 STATISTIC (NumSelects,"Number of selects unswitched")
 
 STATISTIC (NumTrivial,"Number of unswitches that are trivial")
 
 STATISTIC (NumSimplify,"Number of simplifications of unswitched code")
 
 STATISTIC (TotalInsts,"Total number of instructions analyzed")
 
 INITIALIZE_PASS_BEGIN (LoopUnswitch,"loop-unswitch","Unswitch loops", false, false) INITIALIZE_PASS_END(LoopUnswitch
 
static Value * FindLIVLoopCondition (Value *Cond, Loop *L, bool &Changed, OperatorChain &ParentChain, DenseMap< Value *, Value * > &Cache)
 Cond is a condition that occurs in L. More...
 
static std::pair< Value
*, OperatorChain > 
FindLIVLoopCondition (Value *Cond, Loop *L, bool &Changed)
 Cond is a condition that occurs in L. More...
 
static bool isTrivialLoopExitBlockHelper (Loop *L, BasicBlock *BB, BasicBlock *&ExitBB, std::set< BasicBlock * > &Visited)
 Check to see if all paths from BB exit the loop with no side effects (including infinite loops). More...
 
static BasicBlock * isTrivialLoopExitBlock (Loop *L, BasicBlock *BB)
 Return true if the specified block unconditionally leads to an exit from the specified loop, and has no side-effects in the process. More...
 
static Loop * CloneLoop (Loop *L, Loop *PL, ValueToValueMapTy &VM, LoopInfo *LI, LPPassManager *LPM)
 Recursively clone the specified loop and all of its children, mapping the blocks with the specified map. More...
 
static void RemoveFromWorklist (Instruction *I, std::vector< Instruction * > &Worklist)
 Remove all instances of I from the worklist vector specified. More...
 
static void ReplaceUsesOfWith (Instruction *I, Value *V, std::vector< Instruction * > &Worklist, Loop *L, LPPassManager *LPM)
 When we find that I really equals V, remove I from the program, replacing all uses with V and update the worklist. More...
 

Variables

static cl::opt< unsigned > Threshold ("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
 
loop unswitch
 
loop Unswitch loops
 
loop Unswitch false
 

Macro Definition Documentation

#define DEBUG_TYPE   "loop-unswitch"

Definition at line 66 of file LoopUnswitch.cpp.

Enumeration Type Documentation

Operator chain lattice.

Enumerator
OC_OpChainNone 

There is no operator.

OC_OpChainOr 

There are only ORs.

OC_OpChainAnd 

There are only ANDs.

OC_OpChainMixed 

There are ANDs and ORs.

Definition at line 384 of file LoopUnswitch.cpp.

Function Documentation

static Loop* CloneLoop ( Loop *  L,
Loop *  PL,
ValueToValueMapTy &  VM,
LoopInfo *  LI,
LPPassManager *  LPM 
)
static
static Value* FindLIVLoopCondition ( Value *  Cond,
Loop *  L,
bool &  Changed,
OperatorChain &  ParentChain,
DenseMap< Value *, Value * > &  Cache 
)
static

Cond is a condition that occurs in L.

If it is invariant in the loop, or has an invariant piece, return the invariant. Otherwise, return null. NOTE: FindLIVLoopCondition will not return a partial LIV by walking up a mixed operator chain, as we can not reliably find a value which will simplify the operator chain. If the chain is AND-only or OR-only, we can use 0 or ~0 to simplify the chain.

NOTE: In case a partial LIV and a mixed operator chain, we may be able to simplify the condition itself to a loop variant condition, but at the cost of creating an entirely new loop.

Definition at line 402 of file LoopUnswitch.cpp.

References llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::Value::getType(), if(), llvm::Type::isVectorTy(), L, llvm::Loop::makeLoopInvariant(), OC_OpChainAnd, OC_OpChainMixed, OC_OpChainNone, and OC_OpChainOr.

Referenced by FindLIVLoopCondition().

static std::pair<Value *, OperatorChain> FindLIVLoopCondition ( Value *  Cond,
Loop *  L,
bool &  Changed 
)
static

Cond is a condition that occurs in L.

If it is invariant in the loop, or has an invariant piece, return the invariant along with the operator chain type. Otherwise, return null.

Definition at line 484 of file LoopUnswitch.cpp.

References assert(), FindLIVLoopCondition(), OC_OpChainMixed, and OC_OpChainNone.

INITIALIZE_PASS_BEGIN ( LoopUnswitch  ,
"loop-unswitch"  ,
"Unswitch loops"  ,
false  ,
false   
)
static BasicBlock* isTrivialLoopExitBlock ( Loop *  L,
BasicBlock *  BB 
)
static

Return true if the specified block unconditionally leads to an exit from the specified loop, and has no side-effects in the process.

If so, return the block that is exited to, otherwise return null.

Definition at line 793 of file LoopUnswitch.cpp.

References llvm::LoopBase< N, M >::getHeader(), and isTrivialLoopExitBlockHelper().

static bool isTrivialLoopExitBlockHelper ( Loop *  L,
BasicBlock *  BB,
BasicBlock *&  ExitBB,
std::set< BasicBlock * > &  Visited 
)
static

Check to see if all paths from BB exit the loop with no side effects (including infinite loops).

If true, we return true and set ExitBB to the block we exit through.

Definition at line 758 of file LoopUnswitch.cpp.

References llvm::LoopBase< N, M >::contains(), E, I, llvm::succ_begin(), and llvm::succ_end().

Referenced by isTrivialLoopExitBlock().

static void RemoveFromWorklist ( Instruction *  I,
std::vector< Instruction * > &  Worklist 
)
static

Remove all instances of I from the worklist vector specified.

Definition at line 1259 of file LoopUnswitch.cpp.

References I, and llvm::sys::fs::remove().

Referenced by ReplaceUsesOfWith().

static void ReplaceUsesOfWith ( Instruction *  I,
Value *  V,
std::vector< Instruction * > &  Worklist,
Loop *  L,
LPPassManager *  LPM 
)
static
STATISTIC ( NumBranches  ,
"Number of branches unswitched"   
)
STATISTIC ( NumSwitches  ,
"Number of switches unswitched"   
)
STATISTIC ( NumGuards  ,
"Number of guards unswitched"   
)
STATISTIC ( NumSelects  ,
"Number of selects unswitched"   
)
STATISTIC ( NumTrivial  ,
"Number of unswitches that are trivial"   
)
STATISTIC ( NumSimplify  ,
"Number of simplifications of unswitched code"   
)
STATISTIC ( TotalInsts  ,
"Total number of instructions analyzed"   
)

Variable Documentation

loop Unswitch false

Definition at line 376 of file LoopUnswitch.cpp.

loop Unswitch loops

Definition at line 376 of file LoopUnswitch.cpp.

cl::opt<unsigned> Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
static
loop unswitch

Definition at line 376 of file LoopUnswitch.cpp.