|
LLVM
5.0.0
|
#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/LoopAnalysisManager.h"#include "llvm/Analysis/LoopInfo.h"#include "llvm/Analysis/LoopPass.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/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/LoopUtils.h"#include <algorithm>#include <cassert>#include <iterator>#include <utility>Go to the source code of this file.
Macros | |
| #define | DEBUG_TYPE "simple-loop-unswitch" |
Functions | |
| STATISTIC (NumBranches,"Number of branches unswitched") | |
| STATISTIC (NumSwitches,"Number of switches unswitched") | |
| STATISTIC (NumTrivial,"Number of unswitches that are trivial") | |
| static void | replaceLoopUsesWithConstant (Loop &L, Value &LIC, Constant &Replacement) |
| static void | updateLoopExitIDom (BasicBlock *LoopExitBB, Loop &L, DominatorTree &DT) |
| Update the dominator tree after removing one exiting predecessor of a loop exit block. More... | |
| static void | updateDTAfterUnswitch (BasicBlock *UnswitchedBB, BasicBlock *OldPH, DominatorTree &DT) |
| Update the dominator tree after unswitching a particular former exit block. More... | |
| 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 | 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) |
| Rewrite the PHI nodes in the loop exit basic block and the split off unswitched block. More... | |
| static bool | unswitchTrivialBranch (Loop &L, BranchInst &BI, DominatorTree &DT, LoopInfo &LI) |
| Unswitch a trivial branch if the condition is loop invariant. More... | |
| static bool | unswitchTrivialSwitch (Loop &L, SwitchInst &SI, DominatorTree &DT, LoopInfo &LI) |
| Unswitch a trivial switch if the condition is loop invariant. More... | |
| static bool | unswitchAllTrivialConditions (Loop &L, DominatorTree &DT, LoopInfo &LI) |
| This routine scans the loop to find a branch or switch which occurs before any side effects occur. More... | |
| static bool | unswitchLoop (Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC) |
| 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 | |
| simple loop | unswitch |
| simple loop Simple unswitch | loops |
| simple loop Simple unswitch | false |
| #define DEBUG_TYPE "simple-loop-unswitch" |
Definition at line 46 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 206 of file SimpleLoopUnswitch.cpp.
References llvm::dyn_cast(), I, llvm::Loop::isLoopInvariant(), and llvm_unreachable().
Referenced by unswitchTrivialBranch(), and unswitchTrivialSwitch().
| INITIALIZE_PASS_BEGIN | ( | SimpleLoopUnswitchLegacyPass | , |
| "simple-loop-unswitch" | , | ||
| "Simple unswitch loops" | , | ||
| false | , | ||
| false | |||
| ) |
Definition at line 54 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::LoopBase< N, M >::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 256 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::BasicBlock::begin(), llvm::PHINode::Create(), llvm::dyn_cast(), I, 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 229 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::dyn_cast(), and I.
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" | |||
| ) |
|
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).
Definition at line 623 of file SimpleLoopUnswitch.cpp.
References llvm::any_of(), assert(), llvm::LoopBase< N, M >::contains(), llvm::dyn_cast(), llvm::LoopBase< N, M >::getHeader(), llvm::BasicBlock::getTerminator(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::Instruction::mayHaveSideEffects(), SI, unswitchTrivialBranch(), and unswitchTrivialSwitch().
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.
Definition at line 717 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::Loop::isLCSSAForm(), llvm::Loop::isLoopSimplifyForm(), and unswitchAllTrivialConditions().
Referenced by llvm::SimpleLoopUnswitchPass::run().
|
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.
Definition at line 310 of file SimpleLoopUnswitch.cpp.
References areLoopExitPHIsLoopInvariant(), assert(), llvm::SmallVectorTemplateCommon< T >::begin(), llvm::LoopBase< N, M >::contains(), llvm::BranchInst::Create(), llvm::dbgs(), DEBUG, llvm::SmallVectorTemplateCommon< T >::end(), llvm::BasicBlock::end(), llvm::Instruction::eraseFromParent(), llvm::BranchInst::getCondition(), llvm::Value::getContext(), llvm::LoopBase< N, M >::getExitBlocks(), llvm::ConstantInt::getFalse(), llvm::LoopBase< N, M >::getHeader(), llvm::BasicBlock::getInstList(), llvm::LoopBase< N, M >::getLoopPreheader(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::ConstantInt::getTrue(), llvm::BasicBlock::getUniquePredecessor(), llvm::BranchInst::isConditional(), llvm::Loop::isLoopInvariant(), replaceLoopUsesWithConstant(), rewritePHINodesForExitAndUnswitchedBlocks(), rewritePHINodesForUnswitchedExitBlock(), llvm::BranchInst::setSuccessor(), llvm::iplist_impl< IntrusiveListT, TraitsT >::splice(), llvm::SplitBlock(), llvm::SplitEdge(), std::swap(), updateDTAfterUnswitch(), and updateLoopExitIDom().
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.
Definition at line 432 of file SimpleLoopUnswitch.cpp.
References llvm::DominatorTreeBase< N, IsPostDom >::addNewBlock(), llvm::all_of(), areLoopExitPHIsLoopInvariant(), llvm::SmallVectorTemplateCommon< T >::begin(), llvm::SwitchInst::case_begin(), llvm::SwitchInst::case_end(), llvm::SwitchInst::cases(), llvm::BasicBlock::Create(), llvm::BranchInst::Create(), llvm::SwitchInst::Create(), llvm::dbgs(), DEBUG, llvm::SmallPtrSetImplBase::empty(), llvm::SmallVectorTemplateCommon< T >::end(), llvm::Instruction::eraseFromParent(), llvm::BasicBlock::front(), llvm::SwitchInst::getCondition(), llvm::SwitchInst::getDefaultDest(), llvm::LoopBase< N, M >::getExitBlocks(), llvm::LoopBase< N, M >::getHeader(), llvm::LoopBase< N, M >::getLoopPreheader(), llvm::SwitchInst::getNumCases(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::insert(), llvm::Loop::isLoopInvariant(), llvm::pred_empty(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::SwitchInst::removeCase(), llvm::SmallVectorImpl< T >::reserve(), llvm::reverse(), rewritePHINodesForExitAndUnswitchedBlocks(), rewritePHINodesForUnswitchedExitBlock(), llvm::SwitchInst::setDefaultDest(), llvm::SmallPtrSetImplBase::size(), llvm::SmallVectorTemplateCommon< T >::size(), llvm::SplitBlock(), llvm::SplitEdge(), updateDTAfterUnswitch(), updateLoopExitIDom(), and llvm::DominatorTree::verifyDomTree().
Referenced by unswitchAllTrivialConditions().
|
static |
Update the dominator tree after unswitching a particular former exit block.
This handles the full update of the dominator tree after hoisting a block that previously was an exit block (or split off of an exit block) up to be reached from the new immediate dominator of the preheader.
The common case is simple – we just move the unswitched block to have an immediate dominator of the old preheader. But in complex cases, there may be other blocks reachable from the unswitched block that are immediately dominated by some node between the unswitched one and the old preheader. All of these also need to be hoisted in the dominator tree. We also want to minimize queries to the dominator tree because each step of this invalidates any DFS numbers that would make queries fast.
Definition at line 104 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::SmallVectorTemplateCommon< T >::begin(), llvm::DominatorTreeBase< N, IsPostDom >::changeImmediateDominator(), llvm::SmallPtrSetImplBase::clear(), llvm::SmallPtrSetImpl< PtrType >::count(), llvm::SmallVectorBase::empty(), llvm::SmallPtrSetImplBase::empty(), llvm::SmallVectorTemplateCommon< T >::end(), llvm::DomTreeNodeBase< NodeT >::getIDom(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::SmallVectorImpl< T >::insert(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::size(), llvm::SmallVectorTemplateCommon< T >::size(), and llvm::successors().
Referenced by unswitchTrivialBranch(), and unswitchTrivialSwitch().
|
static |
Update the dominator tree after removing one exiting predecessor of a loop exit block.
Definition at line 73 of file SimpleLoopUnswitch.cpp.
References assert(), llvm::DominatorTreeBase< N, IsPostDom >::changeImmediateDominator(), llvm::DominatorTreeBase< N, IsPostDom >::findNearestCommonDominator(), llvm::LoopBase< N, M >::getHeader(), llvm::pred_begin(), and llvm::pred_end().
Referenced by unswitchTrivialBranch(), and unswitchTrivialSwitch().
| simple loop Simple unswitch false |
Definition at line 803 of file SimpleLoopUnswitch.cpp.
| simple loop Simple unswitch loops |
Definition at line 803 of file SimpleLoopUnswitch.cpp.
| simple loop unswitch |
Definition at line 803 of file SimpleLoopUnswitch.cpp.
1.8.6