LLVM
8.0.1
|
#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/CFG.h"
#include "llvm/Analysis/CodeMetrics.h"
#include "llvm/Analysis/GuardUtils.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/LoopAnalysisManager.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/LoopIterator.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/MemorySSA.h"
#include "llvm/Analysis/MemorySSAUpdater.h"
#include "llvm/Analysis/Utils/Local.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Use.h"
#include "llvm/IR/Value.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/GenericDomTree.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
#include "llvm/Transforms/Utils/ValueMapper.h"
#include <algorithm>
#include <cassert>
#include <iterator>
#include <numeric>
#include <utility>
Go to the source code of this file.
Macros | |
#define | DEBUG_TYPE "simple-loop-unswitch" |
===- SimpleLoopUnswitch.cpp - Hoist loop-invariant control flow ------—===// More... | |
Functions | |
STATISTIC (NumBranches, "Number of branches unswitched") | |
STATISTIC (NumSwitches, "Number of switches unswitched") | |
STATISTIC (NumGuards, "Number of guards turned into branches for unswitching") | |
STATISTIC (NumTrivial, "Number of unswitches that are trivial") | |
STATISTIC (NumCostMultiplierSkipped, "Number of unswitch candidates that had their cost multiplier skipped") | |
static TinyPtrVector< Value * > | collectHomogenousInstGraphLoopInvariants (Loop &L, Instruction &Root, LoopInfo &LI) |
Collect all of the loop invariant input values transitively used by the homogeneous instruction graph from a given root. More... | |
static void | replaceLoopInvariantUses (Loop &L, Value *Invariant, Constant &Replacement) |
static bool | areLoopExitPHIsLoopInvariant (Loop &L, BasicBlock &ExitingBB, BasicBlock &ExitBB) |
Check that all the LCSSA PHI nodes in the loop exit block have trivial incoming values along this edge. More... | |
static void | buildPartialUnswitchConditionalBranch (BasicBlock &BB, ArrayRef< Value *> Invariants, bool Direction, BasicBlock &UnswitchedSucc, BasicBlock &NormalSucc) |
Insert code to test a set of loop invariant values, and conditionally branch on them. More... | |
static void | rewritePHINodesForUnswitchedExitBlock (BasicBlock &UnswitchedBB, BasicBlock &OldExitingBB, BasicBlock &OldPH) |
Rewrite the PHI nodes in an unswitched loop exit basic block. More... | |
static void | rewritePHINodesForExitAndUnswitchedBlocks (BasicBlock &ExitBB, BasicBlock &UnswitchedBB, BasicBlock &OldExitingBB, BasicBlock &OldPH, bool FullUnswitch) |
Rewrite the PHI nodes in the loop exit basic block and the split off unswitched block. More... | |
static void | hoistLoopToNewParent (Loop &L, BasicBlock &Preheader, DominatorTree &DT, LoopInfo &LI) |
Hoist the current loop up to the innermost loop containing a remaining exit. More... | |
static bool | unswitchTrivialBranch (Loop &L, BranchInst &BI, DominatorTree &DT, LoopInfo &LI, ScalarEvolution *SE, MemorySSAUpdater *MSSAU) |
Unswitch a trivial branch if the condition is loop invariant. More... | |
static bool | unswitchTrivialSwitch (Loop &L, SwitchInst &SI, DominatorTree &DT, LoopInfo &LI, ScalarEvolution *SE, MemorySSAUpdater *MSSAU) |
Unswitch a trivial switch if the condition is loop invariant. More... | |
static bool | unswitchAllTrivialConditions (Loop &L, DominatorTree &DT, LoopInfo &LI, ScalarEvolution *SE, MemorySSAUpdater *MSSAU) |
This routine scans the loop to find a branch or switch which occurs before any side effects occur. More... | |
static BasicBlock * | buildClonedLoopBlocks (Loop &L, BasicBlock *LoopPH, BasicBlock *SplitBB, ArrayRef< BasicBlock *> ExitBlocks, BasicBlock *ParentBB, BasicBlock *UnswitchedSuccBB, BasicBlock *ContinueSuccBB, const SmallDenseMap< BasicBlock *, BasicBlock *, 16 > &DominatingSucc, ValueToValueMapTy &VMap, SmallVectorImpl< DominatorTree::UpdateType > &DTUpdates, AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU) |
Build the cloned blocks for an unswitched copy of the given loop. More... | |
static Loop * | cloneLoopNest (Loop &OrigRootL, Loop *RootParentL, const ValueToValueMapTy &VMap, LoopInfo &LI) |
Recursively clone the specified loop and all of its children. More... | |
static void | buildClonedLoops (Loop &OrigL, ArrayRef< BasicBlock *> ExitBlocks, const ValueToValueMapTy &VMap, LoopInfo &LI, SmallVectorImpl< Loop *> &NonChildClonedLoops) |
Build the cloned loops of an original loop from unswitching. More... | |
static void | deleteDeadClonedBlocks (Loop &L, ArrayRef< BasicBlock *> ExitBlocks, ArrayRef< std::unique_ptr< ValueToValueMapTy >> VMaps, DominatorTree &DT, MemorySSAUpdater *MSSAU) |
static void | deleteDeadBlocksFromLoop (Loop &L, SmallVectorImpl< BasicBlock *> &ExitBlocks, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU) |
static SmallPtrSet< const BasicBlock *, 16 > | recomputeLoopBlockSet (Loop &L, LoopInfo &LI) |
Recompute the set of blocks in a loop after unswitching. More... | |
static bool | rebuildLoopAfterUnswitch (Loop &L, ArrayRef< BasicBlock *> ExitBlocks, LoopInfo &LI, SmallVectorImpl< Loop *> &HoistedLoops) |
Rebuild a loop after unswitching removes some subset of blocks and edges. More... | |
template<typename CallableT > | |
void | visitDomSubTree (DominatorTree &DT, BasicBlock *BB, CallableT Callable) |
Helper to visit a dominator subtree, invoking a callable on each node. More... | |
static void | unswitchNontrivialInvariants (Loop &L, Instruction &TI, ArrayRef< Value *> Invariants, SmallVectorImpl< BasicBlock *> &ExitBlocks, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, function_ref< void(bool, ArrayRef< Loop *>)> UnswitchCB, ScalarEvolution *SE, MemorySSAUpdater *MSSAU) |
static int | computeDomSubtreeCost (DomTreeNode &N, const SmallDenseMap< BasicBlock *, int, 4 > &BBCostMap, SmallDenseMap< DomTreeNode *, int, 4 > &DTCostMap) |
Recursively compute the cost of a dominator subtree based on the per-block cost map provided. More... | |
static BranchInst * | turnGuardIntoBranch (IntrinsicInst *GI, Loop &L, SmallVectorImpl< BasicBlock *> &ExitBlocks, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU) |
Turns a llvm.experimental.guard intrinsic into implicit control flow branch, making the following replacement: More... | |
static int | calculateUnswitchCostMultiplier (Instruction &TI, Loop &L, LoopInfo &LI, DominatorTree &DT, ArrayRef< std::pair< Instruction *, TinyPtrVector< Value *>>> UnswitchCandidates) |
Cost multiplier is a way to limit potentially exponential behavior of loop-unswitch. More... | |
static bool | unswitchBestCondition (Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, TargetTransformInfo &TTI, function_ref< void(bool, ArrayRef< Loop *>)> UnswitchCB, ScalarEvolution *SE, MemorySSAUpdater *MSSAU) |
static bool | unswitchLoop (Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, TargetTransformInfo &TTI, bool NonTrivial, function_ref< void(bool, ArrayRef< Loop *>)> UnswitchCB, ScalarEvolution *SE, MemorySSAUpdater *MSSAU) |
Unswitch control flow predicated on loop invariant conditions. More... | |
INITIALIZE_PASS_BEGIN (SimpleLoopUnswitchLegacyPass, "simple-loop-unswitch", "Simple unswitch loops", false, false) INITIALIZE_PASS_END(SimpleLoopUnswitchLegacyPass | |
Variables | |
static cl::opt< bool > | EnableNonTrivialUnswitch ("enable-nontrivial-unswitch", cl::init(false), cl::Hidden, cl::desc("Forcibly enables non-trivial loop unswitching rather than " "following the configuration passed into the pass.")) |
static cl::opt< int > | UnswitchThreshold ("unswitch-threshold", cl::init(50), cl::Hidden, cl::desc("The cost threshold for unswitching a loop.")) |
static cl::opt< bool > | EnableUnswitchCostMultiplier ("enable-unswitch-cost-multiplier", cl::init(true), cl::Hidden, cl::desc("Enable unswitch cost multiplier that prohibits exponential " "explosion in nontrivial unswitch.")) |
static cl::opt< int > | UnswitchSiblingsToplevelDiv ("unswitch-siblings-toplevel-div", cl::init(2), cl::Hidden, cl::desc("Toplevel siblings divisor for cost multiplier.")) |
static cl::opt< int > | UnswitchNumInitialUnscaledCandidates ("unswitch-num-initial-unscaled-candidates", cl::init(8), cl::Hidden, cl::desc("Number of unswitch candidates that are ignored when calculating " "cost multiplier.")) |
static cl::opt< bool > | UnswitchGuards ("simple-loop-unswitch-guards", cl::init(true), cl::Hidden, cl::desc("If enabled, simple loop unswitching will also consider " "llvm.experimental.guard intrinsics as unswitch candidates.")) |
simple loop | unswitch |
simple loop Simple unswitch | loops |
simple loop Simple unswitch | false |
#define DEBUG_TYPE "simple-loop-unswitch" |
===- SimpleLoopUnswitch.cpp - Hoist loop-invariant control flow ------—===//
Definition at line 59 of file SimpleLoopUnswitch.cpp.
|
static |
Check that all the LCSSA PHI nodes in the loop exit block have trivial incoming values along this edge.
Definition at line 160 of file SimpleLoopUnswitch.cpp.
References llvm::dyn_cast(), I, llvm::Loop::isLoopInvariant(), and llvm_unreachable.
Referenced by unswitchTrivialBranch(), and unswitchTrivialSwitch().
|
static |
Build the cloned blocks for an unswitched copy of the given loop.
The cloned blocks are inserted before the loop preheader (LoopPH
) and after the split block (SplitBB
) that will be used to select between the cloned and original loop.
This routine handles cloning all of the necessary loop blocks and exit blocks including rewriting their instructions and the relevant PHI nodes. Any loop blocks or exit blocks which are dominated by a different successor than the one for this clone of the loop blocks can be trivially skipped. We use the DominatingSucc
map to determine whether a block satisfies that property with a simple map lookup.
It also correctly creates the unconditional branch in the cloned unswitched parent block to only point at the unswitched successor.
This does not handle most of the necessary updates to LoopInfo
. Only exit block splitting is correctly reflected in LoopInfo
, essentially all of the cloned blocks (and their loops) are left without full LoopInfo
updates. This also doesn't fully update DominatorTree
. It adds the cloned blocks to them but doesn't create the cloned DominatorTree
structure and instead the caller must recompute an accurate DT. It does correctly update the AssumptionCache
provided in AC
.
Definition at line 942 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::Intrinsic::assume, llvm::LoopBase< BlockT, LoopT >::blocks(), llvm::SmallPtrSetImplBase::clear(), llvm::CloneBasicBlock(), llvm::PHINode::Create(), llvm::BranchInst::Create(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::LoopBase< BlockT, LoopT >::getNumBlocks(), llvm::Value::getType(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::ValueMap< KeyT, ValueT, Config >::lookup(), llvm::make_range(), llvm::BasicBlock::moveBefore(), llvm::SmallVectorTemplateBase< T >::push_back(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::AssumptionCache::registerAssumption(), llvm::RemapInstruction(), llvm::Value::replaceAllUsesWith(), llvm::SmallVectorImpl< T >::reserve(), llvm::RF_IgnoreMissingLocals, llvm::RF_NoModuleLevelChanges, llvm::ArrayRef< T >::size(), llvm::SplitBlock(), llvm::successors(), llvm::Value::takeName(), and llvm::zip_first().
Referenced by unswitchNontrivialInvariants().
|
static |
Build the cloned loops of an original loop from unswitching.
Because unswitching simplifies the CFG of the loop, this isn't a trivial operation. We need to re-verify that there even is a loop (as the backedge may not have been cloned), and even if there are remaining backedges the backedge set may be different. However, we know that each child loop is undisturbed, we only need to find where to place each child loop within either any parent loop or within a cloned version of the original loop.
Because child loops may end up cloned outside of any cloned version of the original loop, multiple cloned sibling loops may be created. All of them are returned so that the newly introduced loop nest roots can be identified.
Definition at line 1173 of file SimpleLoopUnswitch.cpp.
References llvm::LoopBase< BlockT, LoopT >::addBasicBlockToLoop(), llvm::LoopInfoBase< BlockT, LoopT >::addTopLevelLoop(), llvm::LoopInfoBase< BlockT, LoopT >::AllocateLoop(), assert(), llvm::LoopBase< BlockT, LoopT >::blocks(), cloneLoopNest(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::ValueMap< KeyT, ValueT, Config >::count(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::count(), llvm::SmallPtrSetImpl< PtrType >::count(), llvm::SmallVectorBase::empty(), llvm::SmallPtrSetImplBase::empty(), llvm::SmallPtrSetImpl< PtrType >::erase(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::LoopBase< BlockT, LoopT >::getLoopDepth(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::LoopBase< BlockT, LoopT >::getLoopPreheader(), llvm::LoopBase< BlockT, LoopT >::getParentLoop(), llvm::SetVector< T, Vector, Set >::insert(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::insert(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::ValueMap< KeyT, ValueT, Config >::lookup(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::lookup(), llvm::makeArrayRef(), llvm::AArch64CC::PL, llvm::SmallVectorImpl< T >::pop_back_val(), llvm::predecessors(), llvm::SmallVectorTemplateBase< T >::push_back(), llvm::SmallVectorImpl< T >::reserve(), llvm::LoopBase< BlockT, LoopT >::reserveBlocks(), second, llvm::SmallPtrSetImplBase::size(), llvm::ArrayRef< T >::size(), and llvm::sort().
Referenced by unswitchNontrivialInvariants().
|
static |
Insert code to test a set of loop invariant values, and conditionally branch on them.
Definition at line 178 of file SimpleLoopUnswitch.cpp.
References llvm::ArrayRef< T >::begin(), llvm::IRBuilder< T, Inserter >::CreateAnd(), llvm::IRBuilder< T, Inserter >::CreateCondBr(), llvm::IRBuilder< T, Inserter >::CreateOr(), llvm::ArrayRef< T >::end(), llvm::ArrayRef< T >::front(), and llvm::make_range().
Referenced by unswitchNontrivialInvariants(), and unswitchTrivialBranch().
|
static |
Cost multiplier is a way to limit potentially exponential behavior of loop-unswitch.
Cost is multipied in proportion of 2^number of unswitch candidates available. Also accounting for the number of "sibling" loops with the idea to account for previous unswitches that already happened on this cluster of loops. There was an attempt to keep this formula simple, just enough to limit the worst case behavior. Even if it is not that simple now it is still not an attempt to provide a detailed heuristic size prediction.
TODO: Make a proper accounting of "explosion" effect for all kinds of unswitch candidates, making adequate predictions instead of wild guesses. That requires knowing not just the number of "remaining" candidates but also costs of unswitching for each of these candidates.
Definition at line 2421 of file SimpleLoopUnswitch.cpp.
References llvm::LoopInfoBase< BlockT, LoopT >::begin(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::count_if(), llvm::dbgs(), llvm::DominatorTree::dominates(), llvm::LoopInfoBase< BlockT, LoopT >::end(), llvm::LoopBase< BlockT, LoopT >::getLoopLatch(), llvm::Instruction::getParent(), llvm::LoopBase< BlockT, LoopT >::getParentLoop(), llvm::isGuard(), LLVM_DEBUG, llvm::Log2_32(), llvm::max(), llvm::successors(), UnswitchNumInitialUnscaledCandidates, UnswitchSiblingsToplevelDiv, and UnswitchThreshold.
Referenced by unswitchBestCondition().
|
static |
Recursively clone the specified loop and all of its children.
The target parent loop for the clone should be provided, or can be null if the clone is a top-level loop. While cloning, all the blocks are mapped with the provided value map. The entire original loop must be present in the value map. The cloned loop is returned.
Definition at line 1114 of file SimpleLoopUnswitch.cpp.
References llvm::LoopBase< BlockT, LoopT >::addChildLoop(), llvm::LoopInfoBase< BlockT, LoopT >::addTopLevelLoop(), llvm::LoopInfoBase< BlockT, LoopT >::AllocateLoop(), assert(), llvm::LoopBase< BlockT, LoopT >::blocks(), llvm::LoopInfoBase< BlockT, LoopT >::changeLoopFor(), llvm::SmallVectorBase::empty(), llvm::LoopBase< BlockT, LoopT >::empty(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::LoopBase< BlockT, LoopT >::getNumBlocks(), llvm::ValueMap< KeyT, ValueT, Config >::lookup(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T >::push_back(), and llvm::reverse().
Referenced by buildClonedLoops().
|
static |
Collect all of the loop invariant input values transitively used by the homogeneous instruction graph from a given root.
This essentially walks from a root recursively through loop variant operands which have the exact same opcode and finds all inputs which are loop invariant. For some operations these can be re-associated and unswitched out of the loop entirely.
Definition at line 104 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::dyn_cast(), llvm::SmallVectorBase::empty(), llvm::Instruction::getOpcode(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::Loop::isLoopInvariant(), llvm::User::operand_values(), llvm::SmallVectorImpl< T >::pop_back_val(), and llvm::SmallVectorTemplateBase< T >::push_back().
Referenced by unswitchBestCondition(), and unswitchTrivialBranch().
|
static |
Recursively compute the cost of a dominator subtree based on the per-block cost map provided.
The recursive computation is memozied into the provided DT-indexed cost map to allow querying it for most nodes in the domtree without it becoming quadratic.
Definition at line 2297 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::DomTreeNodeBase< NodeT >::begin(), llvm::DomTreeNodeBase< NodeT >::end(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::DomTreeNodeBase< NodeT >::getBlock(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::insert(), N, and second.
Referenced by unswitchBestCondition().
|
static |
Definition at line 1452 of file SimpleLoopUnswitch.cpp.
References llvm::all_of(), llvm::SmallVectorImpl< T >::append(), assert(), llvm::SmallVectorTemplateCommon< T, typename >::begin(), llvm::LoopBase< BlockT, LoopT >::blocks(), llvm::LoopInfoBase< BlockT, LoopT >::changeLoopFor(), llvm::SmallPtrSetImpl< PtrType >::count(), llvm::LoopInfoBase< BlockT, LoopT >::destroy(), llvm::BasicBlock::dropAllReferences(), llvm::SmallVectorTemplateCommon< T, typename >::end(), llvm::erase_if(), llvm::BasicBlock::eraseFromParent(), llvm::DominatorTreeBase< NodeT, IsPostDom >::getNode(), llvm::LoopBase< BlockT, LoopT >::getParentLoop(), llvm::LoopBase< BlockT, LoopT >::getSubLoopsVector(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::DominatorTree::isReachableFromEntry(), llvm::MemorySSAUpdater::removeBlocks(), and llvm::successors().
Referenced by unswitchNontrivialInvariants().
|
static |
Definition at line 1423 of file SimpleLoopUnswitch.cpp.
References llvm::SmallVectorTemplateCommon< T >::begin(), llvm::LoopBase< BlockT, LoopT >::blocks(), llvm::SmallVectorTemplateCommon< T >::end(), llvm::DominatorTree::isReachableFromEntry(), llvm::SmallVectorTemplateBase< T >::push_back(), llvm::MemorySSAUpdater::removeBlocks(), and llvm::successors().
Referenced by unswitchNontrivialInvariants().
|
static |
Hoist the current loop up to the innermost loop containing a remaining exit.
Because we've removed an exit from the loop, we may have changed the set of loops reachable and need to move the current loop up the loop nest or even to an entirely separate nest.
Definition at line 270 of file SimpleLoopUnswitch.cpp.
References llvm::LoopBase< BlockT, LoopT >::addChildLoop(), llvm::LoopInfoBase< BlockT, LoopT >::addTopLevelLoop(), assert(), llvm::LoopBase< BlockT, LoopT >::blocks(), llvm::LoopInfoBase< BlockT, LoopT >::changeLoopFor(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::erase_if(), llvm::formDedicatedExitBlocks(), llvm::formLCSSA(), llvm::LoopBase< BlockT, LoopT >::getExitBlocks(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::LoopBase< BlockT, LoopT >::getParentLoop(), and llvm::LoopBase< BlockT, LoopT >::removeChildLoop().
Referenced by unswitchTrivialBranch(), and unswitchTrivialSwitch().
INITIALIZE_PASS_BEGIN | ( | SimpleLoopUnswitchLegacyPass | , |
"simple-loop-unswitch" | , | ||
"Simple unswitch loops" | , | ||
false | , | ||
false | |||
) |
Referenced by llvm::SimpleLoopUnswitchPass::run().
|
static |
Rebuild a loop after unswitching removes some subset of blocks and edges.
The removal may have removed some child loops entirely but cannot have disturbed any remaining child loops. However, they may need to be hoisted to the parent loop (or to be top-level loops). The original loop may be completely removed.
The sibling loops resulting from this update are returned. If the original loop remains a valid loop, it will be the first entry in this list with all of the newly sibling loops following it.
Returns true if the loop remains a loop after unswitching, and false if it is no longer a loop after unswitching (and should not continue to be referenced).
Definition at line 1647 of file SimpleLoopUnswitch.cpp.
References llvm::LoopBase< BlockT, LoopT >::addChildLoop(), llvm::LoopInfoBase< BlockT, LoopT >::addTopLevelLoop(), assert(), llvm::SmallVectorTemplateCommon< T >::begin(), llvm::LoopBase< BlockT, LoopT >::blocks(), llvm::LoopInfoBase< BlockT, LoopT >::changeLoopFor(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::LoopInfoBase< BlockT, LoopT >::destroy(), llvm::SmallVectorBase::empty(), llvm::SmallVectorTemplateCommon< T >::end(), llvm::SmallPtrSetImpl< PtrType >::erase(), llvm::erase_if(), llvm::find(), llvm::LoopBase< BlockT, LoopT >::getBlocksSet(), llvm::LoopBase< BlockT, LoopT >::getBlocksVector(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopDepth(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::LoopBase< BlockT, LoopT >::getLoopPreheader(), llvm::LoopBase< BlockT, LoopT >::getParentLoop(), llvm::LoopBase< BlockT, LoopT >::getSubLoopsVector(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::make_range(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::predecessors(), llvm::SmallVectorTemplateBase< T >::push_back(), recomputeLoopBlockSet(), llvm::LoopBase< BlockT, LoopT >::removeChildLoop(), llvm::LoopInfoBase< BlockT, LoopT >::removeLoop(), llvm::SmallVectorImpl< T >::reserve(), and llvm::ArrayRef< T >::size().
Referenced by unswitchNontrivialInvariants().
|
static |
Recompute the set of blocks in a loop after unswitching.
This walks from the original headers predecessors to rebuild the loop. We take advantage of the fact that new blocks can't have been added, and so we filter by the original loop's blocks. This also handles potentially unreachable code that we don't want to explore but might be found examining the predecessors of the header.
If the original loop is no longer a loop, this will return an empty set. If it remains a loop, all the blocks within it will be added to the set (including those blocks in inner loops).
Definition at line 1536 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::SmallPtrSetImpl< PtrType >::count(), llvm::SmallVectorBase::empty(), llvm::SmallPtrSetImplBase::empty(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::LoopBase< BlockT, LoopT >::getLoopPreheader(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::predecessors(), and llvm::SmallVectorTemplateBase< T >::push_back().
Referenced by rebuildLoopAfterUnswitch().
Definition at line 142 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::dyn_cast(), llvm::Value::use_begin(), and llvm::Value::use_end().
Referenced by unswitchTrivialBranch().
|
static |
Rewrite the PHI nodes in the loop exit basic block and the split off unswitched block.
Because the exit block remains an exit from the loop, this rewrites the LCSSA PHI nodes in it to remove the unswitched edge and introduces PHI nodes into the unswitched basic block to select between the value in the old preheader and the loop exit.
Definition at line 225 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::BasicBlock::begin(), llvm::PHINode::Create(), llvm::BasicBlock::phis(), and llvm::Value::replaceAllUsesWith().
Referenced by unswitchTrivialBranch(), and unswitchTrivialSwitch().
|
static |
Rewrite the PHI nodes in an unswitched loop exit basic block.
Requires that the loop exit and unswitched basic block are the same, and that the exiting block was a unique predecessor of that block. Rewrites the PHI nodes in that block such that what were LCSSA PHI nodes become trivial PHI nodes from the old preheader that now contains the unswitched terminator.
Definition at line 203 of file SimpleLoopUnswitch.cpp.
References assert(), and llvm::BasicBlock::phis().
Referenced by unswitchTrivialBranch(), and unswitchTrivialSwitch().
STATISTIC | ( | NumBranches | , |
"Number of branches unswitched" | |||
) |
STATISTIC | ( | NumSwitches | , |
"Number of switches unswitched" | |||
) |
STATISTIC | ( | NumTrivial | , |
"Number of unswitches that are trivial" | |||
) |
STATISTIC | ( | NumCostMultiplierSkipped | , |
"Number of unswitch candidates that had their cost multiplier skipped" | |||
) |
|
static |
Turns a llvm.experimental.guard intrinsic into implicit control flow branch, making the following replacement:
–code before guard– call void (i1, ...) .experimental.guard(i1 cond) [ "deopt"() ] –code after guard–
into
–code before guard– br i1 cond, label guarded, label deopt
guarded: –code after guard–
deopt: call void (i1, ...) .experimental.guard(i1 false) [ "deopt"() ] unreachable
It also makes all relevant DT and LI updates, so that all structures are in valid state after this transform.
Definition at line 2345 of file SimpleLoopUnswitch.cpp.
References llvm::LoopBase< BlockT, LoopT >::addBasicBlockToLoop(), llvm::DominatorTreeBase< NodeT, IsPostDom >::applyUpdates(), llvm::dbgs(), llvm::MemorySSA::End, llvm::CallBase::getArgOperand(), llvm::Value::getContext(), llvm::ConstantInt::getFalse(), llvm::MemorySSA::getMemoryAccess(), llvm::MemorySSAUpdater::getMemorySSA(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), llvm::DominatorTreeBase< BasicBlock, false >::Insert, llvm::SmallPtrSetImpl< PtrType >::insert(), LLVM_DEBUG, llvm::MemorySSAUpdater::moveAllAfterSpliceBlocks(), llvm::Instruction::moveBefore(), llvm::MemorySSAUpdater::moveToPlace(), llvm::SmallVectorTemplateBase< T >::push_back(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::CallBase::setArgOperand(), llvm::Value::setName(), llvm::SplitBlockAndInsertIfThen(), llvm::successors(), llvm::BranchInst::swapSuccessors(), llvm::VerifyMemorySSA, and llvm::MemorySSA::verifyMemorySSA().
Referenced by unswitchBestCondition().
|
static |
This routine scans the loop to find a branch or switch which occurs before any side effects occur.
These can potentially be unswitched without duplicating the loop. If a branch or switch is successfully unswitched the scanning continues to see if subsequent branches or switches have become trivial. Once all trivial candidates have been unswitched, this routine returns.
The return value indicates whether anything was unswitched (and therefore changed).
If SE
is not null, it will be updated based on the potential loop SCEVs invalidated by this.
Definition at line 828 of file SimpleLoopUnswitch.cpp.
References llvm::any_of(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::dyn_cast(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::BasicBlock::getTerminator(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::Instruction::mayHaveSideEffects(), SI, unswitchTrivialBranch(), and unswitchTrivialSwitch().
Referenced by unswitchLoop().
|
static |
Definition at line 2494 of file SimpleLoopUnswitch.cpp.
References llvm::all_of(), assert(), llvm::LoopBase< BlockT, LoopT >::blocks(), calculateUnswitchCostMultiplier(), llvm::CodeMetrics::collectEphemeralValues(), collectHomogenousInstGraphLoopInvariants(), computeDomSubtreeCost(), llvm::dbgs(), llvm::DominatorTree::dominates(), llvm::dyn_cast(), llvm::TinyPtrVector< EltTy >::empty(), EnableUnswitchCostMultiplier, llvm::Intrinsic::experimental_guard, llvm::BranchInst::getCondition(), llvm::Module::getFunction(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::Intrinsic::getName(), llvm::Instruction::getOpcode(), getOpcode(), llvm::Instruction::getParent(), llvm::BasicBlock::getParent(), llvm::GlobalValue::getParent(), llvm::LoopBase< BlockT, LoopT >::getUniqueExitBlocks(), llvm::TargetTransformInfo::getUserCost(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::isGuard(), llvm::Loop::isLoopInvariant(), LLVM_DEBUG, llvm::LoopBlocksRPO::perform(), llvm::predecessors(), llvm::SmallVectorTemplateBase< T >::push_back(), SI, llvm::SmallPtrSetImplBase::size(), llvm::ArrayRef< T >::size(), llvm::successors(), turnGuardIntoBranch(), UnswitchGuards, unswitchNontrivialInvariants(), and UnswitchThreshold.
Referenced by unswitchLoop().
|
static |
Unswitch control flow predicated on loop invariant conditions.
This first hoists all branches or switches which are trivial (IE, do not require duplicating any part of the loop) out of the loop body. It then looks at other loop invariant control flows and tries to unswitch those as well by cloning the loop if the result is small enough.
The DT
, LI
, AC
, TTI
parameters are required analyses that are also updated based on the unswitch. The MSSA
analysis is also updated if valid (i.e. its use is enabled).
If either NonTrivial
is true or the flag EnableNonTrivialUnswitch
is true, we will attempt to do non-trivial unswitching as well as trivial unswitching.
The UnswitchCB
callback provided will be run after unswitching is complete, with the first parameter set to true
if the provided loop remains a loop, and a list of new sibling loops created.
If SE
is non-null, we will update that analysis based on the unswitching done.
Definition at line 2770 of file SimpleLoopUnswitch.cpp.
References assert(), EnableNonTrivialUnswitch, llvm::Loop::isLoopSimplifyForm(), llvm::Loop::isRecursivelyLCSSAForm(), unswitchAllTrivialConditions(), and unswitchBestCondition().
Referenced by llvm::SimpleLoopUnswitchPass::run().
|
static |
Definition at line 1875 of file SimpleLoopUnswitch.cpp.
References llvm::all_of(), llvm::MemorySSAUpdater::applyInsertUpdates(), llvm::DominatorTreeBase< NodeT, IsPostDom >::applyUpdates(), assert(), llvm::SmallVectorTemplateCommon< T >::back(), llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::begin(), llvm::BasicBlock::begin(), buildClonedLoopBlocks(), buildClonedLoops(), buildPartialUnswitchConditionalBranch(), llvm::SwitchInst::cases(), llvm::Instruction::clone(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::count(), llvm::BranchInst::Create(), llvm::DominatorTreeBase< BasicBlock, false >::Delete, deleteDeadBlocksFromLoop(), deleteDeadClonedBlocks(), llvm::DominatorTree::dominates(), llvm::dyn_cast(), llvm::SmallVectorImpl< T >::emplace_back(), llvm::Instruction::eraseFromParent(), llvm::CallingConv::Fast, llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::ScalarEvolution::forgetLoop(), llvm::ScalarEvolution::forgetTopmostLoop(), llvm::formDedicatedExitBlocks(), llvm::formLCSSA(), llvm::BranchInst::getCondition(), llvm::Value::getContext(), llvm::SwitchInst::getDefaultDest(), llvm::ConstantInt::getFalse(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::LoopBase< BlockT, LoopT >::getLoopPreheader(), llvm::MemorySSAUpdater::getMemorySSA(), getOpcode(), llvm::Instruction::getParent(), llvm::LoopBase< BlockT, LoopT >::getParentLoop(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::ConstantInt::getTrue(), llvm::cfg::Insert, llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert(), llvm::DominatorTreeBase< BasicBlock, false >::Insert, llvm::BranchInst::isConditional(), llvm::makeArrayRef(), llvm::LoopBlocksRPO::perform(), llvm::predecessors(), llvm::SmallVectorTemplateBase< T >::push_back(), rebuildLoopAfterUnswitch(), llvm::MemorySSAUpdater::removeDuplicatePhiEdgesBetween(), llvm::MemorySSAUpdater::removeEdge(), llvm::SmallVectorImpl< T >::reserve(), second, llvm::BranchInst::setSuccessor(), SI, llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::size(), llvm::ArrayRef< T >::size(), llvm::iplist_impl< IntrusiveListT, TraitsT >::splice(), llvm::SplitEdge(), llvm::MemorySSAUpdater::updateExitBlocksForClonedLoop(), llvm::MemorySSAUpdater::updateForClonedLoop(), llvm::DominatorTreeBase< NodeT, IsPostDom >::verify(), llvm::VerifyMemorySSA, llvm::MemorySSA::verifyMemorySSA(), and visitDomSubTree().
Referenced by unswitchBestCondition().
|
static |
Unswitch a trivial branch if the condition is loop invariant.
This routine should only be called when loop code leading to the branch has been validated as trivial (no side effects). This routine checks if the condition is invariant and one of the successors is a loop exit. This allows us to unswitch without duplicating the loop, making it trivial.
If this routine fails to unswitch the branch it returns false.
If the branch can be unswitched, this routine splits the preheader and hoists the branch above that split. Preserves loop simplified form (splitting the exit block as necessary). It simplifies the branch within the loop to an unconditional branch but doesn't remove it entirely. Further cleanup can be done with some simplify-cfg like pass.
If SE
is not null, it will be updated based on the potential loop SCEVs invalidated by this.
Definition at line 353 of file SimpleLoopUnswitch.cpp.
References llvm::MemorySSAUpdater::applyInsertUpdates(), areLoopExitPHIsLoopInvariant(), assert(), buildPartialUnswitchConditionalBranch(), llvm::Instruction::clone(), collectHomogenousInstGraphLoopInvariants(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::BranchInst::Create(), llvm::dbgs(), llvm::DominatorTreeBase< NodeT, IsPostDom >::deleteEdge(), llvm::TinyPtrVector< EltTy >::empty(), llvm::BasicBlock::end(), llvm::Instruction::eraseFromParent(), llvm::ScalarEvolution::forgetLoop(), llvm::ScalarEvolution::forgetTopmostLoop(), llvm::BranchInst::getCondition(), llvm::Value::getContext(), llvm::ConstantInt::getFalse(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::BasicBlock::getInstList(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::LoopBase< BlockT, LoopT >::getLoopPreheader(), llvm::MemorySSAUpdater::getMemorySSA(), getOpcode(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::ConstantInt::getTrue(), hoistLoopToNewParent(), llvm::cfg::Insert, llvm::DominatorTreeBase< NodeT, IsPostDom >::insertEdge(), llvm::BranchInst::isConditional(), llvm::Loop::isLoopInvariant(), LLVM_DEBUG, llvm::SmallVectorTemplateBase< T >::push_back(), llvm::TinyPtrVector< EltTy >::push_back(), llvm::MemorySSAUpdater::removeEdge(), replaceLoopInvariantUses(), rewritePHINodesForExitAndUnswitchedBlocks(), rewritePHINodesForUnswitchedExitBlock(), llvm::BranchInst::setSuccessor(), llvm::iplist_impl< IntrusiveListT, TraitsT >::splice(), llvm::SplitBlock(), llvm::SplitEdge(), llvm::VerifyMemorySSA, and llvm::MemorySSA::verifyMemorySSA().
Referenced by unswitchAllTrivialConditions().
|
static |
Unswitch a trivial switch if the condition is loop invariant.
This routine should only be called when loop code leading to the switch has been validated as trivial (no side effects). This routine checks if the condition is invariant and that at least one of the successors is a loop exit. This allows us to unswitch without duplicating the loop, making it trivial.
If this routine fails to unswitch the switch it returns false.
If the switch can be unswitched, this routine splits the preheader and copies the switch above that split. If the default case is one of the exiting cases, it copies the non-exiting cases and points them at the new preheader. If the default case is not exiting, it copies the exiting cases and points the default at the preheader. It preserves loop simplified form (splitting the exit blocks as necessary). It simplifies the switch within the loop by removing now-dead cases. If the default case is one of those unswitched, it replaces its destination with a new basic block containing only unreachable. Such basic blocks, while technically loop exits, are not considered for unswitching so this is a stable transform and the same switch will not be revisited. If after unswitching there is only a single in-loop successor, the switch is further simplified to an unconditional branch. Still more cleanup can be done with some simplify-cfg like pass.
If SE
is not null, it will be updated based on the potential loop SCEVs invalidated by this.
Definition at line 573 of file SimpleLoopUnswitch.cpp.
References llvm::all_of(), llvm::MemorySSAUpdater::applyUpdates(), llvm::DominatorTreeBase< NodeT, IsPostDom >::applyUpdates(), areLoopExitPHIsLoopInvariant(), assert(), llvm::SwitchInst::case_begin(), llvm::SwitchInst::case_end(), llvm::SwitchInst::cases(), llvm::LoopBase< BlockT, LoopT >::contains(), llvm::BranchInst::Create(), llvm::SwitchInst::Create(), llvm::dbgs(), llvm::DominatorTreeBase< NodeT, IsPostDom >::Delete, llvm::SmallVectorBase::empty(), llvm::Instruction::eraseFromParent(), llvm::CallingConv::Fast, llvm::ScalarEvolution::forgetLoop(), llvm::ScalarEvolution::forgetTopmostLoop(), llvm::BasicBlock::front(), llvm::SwitchInst::getCondition(), llvm::SwitchInst::getDefaultDest(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::LoopBase< BlockT, LoopT >::getLoopPreheader(), llvm::MemorySSAUpdater::getMemorySSA(), llvm::SwitchInst::getNumCases(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), hoistLoopToNewParent(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::insert(), llvm::DominatorTreeBase< NodeT, IsPostDom >::Insert, llvm::Loop::isLoopInvariant(), LLVM_DEBUG, llvm::pred_empty(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::SmallVectorTemplateBase< T >::push_back(), llvm::SwitchInst::removeCase(), llvm::BasicBlock::removePredecessor(), llvm::SmallVectorImpl< T >::reserve(), llvm::reverse(), rewritePHINodesForExitAndUnswitchedBlocks(), rewritePHINodesForUnswitchedExitBlock(), llvm::SwitchInst::setDefaultDest(), SI, llvm::SmallVectorBase::size(), llvm::SplitBlock(), llvm::SplitEdge(), llvm::DominatorTreeBase< NodeT, IsPostDom >::verify(), llvm::VerifyMemorySSA, and llvm::MemorySSA::verifyMemorySSA().
Referenced by unswitchAllTrivialConditions().
void visitDomSubTree | ( | DominatorTree & | DT, |
BasicBlock * | BB, | ||
CallableT | Callable | ||
) |
Helper to visit a dominator subtree, invoking a callable on each node.
Returning false at any point will stop walking past that node of the tree.
Definition at line 1852 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::SmallVectorBase::empty(), llvm::DomTreeNodeBase< NodeT >::getBlock(), llvm::SmallPtrSetImpl< PtrType >::insert(), N, llvm::SmallVectorImpl< T >::pop_back_val(), and llvm::SmallVectorTemplateBase< T >::push_back().
Referenced by unswitchNontrivialInvariants().
|
static |
Referenced by unswitchLoop().
|
static |
Referenced by unswitchBestCondition().
simple loop Simple unswitch false |
Definition at line 2954 of file SimpleLoopUnswitch.cpp.
simple loop Simple unswitch loops |
Definition at line 2954 of file SimpleLoopUnswitch.cpp.
simple loop unswitch |
Definition at line 2954 of file SimpleLoopUnswitch.cpp.
|
static |
Referenced by unswitchBestCondition().
|
static |
Referenced by calculateUnswitchCostMultiplier().
|
static |
Referenced by calculateUnswitchCostMultiplier().
|
static |
Referenced by calculateUnswitchCostMultiplier(), and unswitchBestCondition().