2016-01-30 06:35:36 +08:00
|
|
|
//===--------- LoopSimplifyCFG.cpp - Loop CFG Simplification Pass ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Loop SimplifyCFG Pass. This pass is responsible for
|
|
|
|
// basic loop CFG cleanup, primarily to assist other loop passes. If you
|
|
|
|
// encounter a noncanonical CFG construct that causes another loop pass to
|
|
|
|
// perform suboptimally, this is the place to fix it up.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-05-04 05:47:32 +08:00
|
|
|
#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
|
2016-01-30 06:35:36 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2016-12-19 16:22:17 +08:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2017-01-11 17:43:56 +08:00
|
|
|
#include "llvm/Analysis/BasicAliasAnalysis.h"
|
2016-01-30 06:35:36 +08:00
|
|
|
#include "llvm/Analysis/DependenceAnalysis.h"
|
|
|
|
#include "llvm/Analysis/GlobalsModRef.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
2018-08-23 04:10:21 +08:00
|
|
|
#include "llvm/Analysis/MemorySSA.h"
|
|
|
|
#include "llvm/Analysis/MemorySSAUpdater.h"
|
2016-01-30 06:35:36 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2018-08-03 13:08:17 +08:00
|
|
|
#include "llvm/IR/DomTreeUpdater.h"
|
2016-01-30 06:35:36 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2016-05-04 05:47:32 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2017-01-11 17:43:56 +08:00
|
|
|
#include "llvm/Transforms/Scalar/LoopPassManager.h"
|
2018-03-29 01:44:36 +08:00
|
|
|
#include "llvm/Transforms/Utils.h"
|
Generalize MergeBlockIntoPredecessor. Replace uses of MergeBasicBlockIntoOnlyPred.
Summary:
Two utils methods have essentially the same functionality. This is an attempt to merge them into one.
1. lib/Transforms/Utils/Local.cpp : MergeBasicBlockIntoOnlyPred
2. lib/Transforms/Utils/BasicBlockUtils.cpp : MergeBlockIntoPredecessor
Prior to the patch:
1. MergeBasicBlockIntoOnlyPred
Updates either DomTree or DeferredDominance
Moves all instructions from Pred to BB, deletes Pred
Asserts BB has single predecessor
If address was taken, replace the block address with constant 1 (?)
2. MergeBlockIntoPredecessor
Updates DomTree, LoopInfo and MemoryDependenceResults
Moves all instruction from BB to Pred, deletes BB
Returns if doesn't have a single predecessor
Returns if BB's address was taken
After the patch:
Method 2. MergeBlockIntoPredecessor is attempting to become the new default:
Updates DomTree or DeferredDominance, and LoopInfo and MemoryDependenceResults
Moves all instruction from BB to Pred, deletes BB
Returns if doesn't have a single predecessor
Returns if BB's address was taken
Uses of MergeBasicBlockIntoOnlyPred that need to be replaced:
1. lib/Transforms/Scalar/LoopSimplifyCFG.cpp
Updated in this patch. No challenges.
2. lib/CodeGen/CodeGenPrepare.cpp
Updated in this patch.
i. eliminateFallThrough is straightforward, but I added using a temporary array to avoid the iterator invalidation.
ii. eliminateMostlyEmptyBlock(s) methods also now use a temporary array for blocks
Some interesting aspects:
- Since Pred is not deleted (BB is), the entry block does not need updating.
- The entry block was being updated with the deleted block in eliminateMostlyEmptyBlock. Added assert to make obvious that BB=SinglePred.
- isMergingEmptyBlockProfitable assumes BB is the one to be deleted.
- eliminateMostlyEmptyBlock(BB) does not delete BB on one path, it deletes its unique predecessor instead.
- adding some test owner as subscribers for the interesting tests modified:
test/CodeGen/X86/avx-cmp.ll
test/CodeGen/AMDGPU/nested-loop-conditions.ll
test/CodeGen/AMDGPU/si-annotate-cf.ll
test/CodeGen/X86/hoist-spill.ll
test/CodeGen/X86/2006-11-17-IllegalMove.ll
3. lib/Transforms/Scalar/JumpThreading.cpp
Not covered in this patch. It is the only use case using the DeferredDominance.
I would defer to Brian Rzycki to make this replacement.
Reviewers: chandlerc, spatel, davide, brzycki, bkramer, javed.absar
Subscribers: qcolombet, sanjoy, nemanjai, nhaehnle, jlebar, tpr, kbarton, RKSimon, wmi, arsenm, llvm-commits
Differential Revision: https://reviews.llvm.org/D48202
llvm-svn: 335183
2018-06-21 06:01:04 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
[LPM] Factor all of the loop analysis usage updates into a common helper
routine.
We were getting this wrong in small ways and generally being very
inconsistent about it across loop passes. Instead, let's have a common
place where we do this. One minor downside is that this will require
some analyses like SCEV in more places than they are strictly needed.
However, this seems benign as these analyses are complete no-ops, and
without this consistency we can in many cases end up with the legacy
pass manager scheduling deciding to split up a loop pass pipeline in
order to run the function analysis half-way through. It is very, very
annoying to fix these without just being very pedantic across the board.
The only loop passes I've not updated here are ones that use
AU.setPreservesAll() such as IVUsers (an analysis) and the pass printer.
They seemed less relevant.
With this patch, almost all of the problems in PR24804 around loop pass
pipelines are fixed. The one remaining issue is that we run simplify-cfg
and instcombine in the middle of the loop pass pipeline. We've recently
added some loop variants of these passes that would seem substantially
cleaner to use, but this at least gets us much closer to the previous
state. Notably, the seven loop pass managers is down to three.
I've not updated the loop passes using LoopAccessAnalysis because that
analysis hasn't been fully wired into LoopSimplify/LCSSA, and it isn't
clear that those transforms want to support those forms anyways. They
all run late anyways, so this is harmless. Similarly, LSR is left alone
because it already carefully manages its forms and doesn't need to get
fused into a single loop pass manager with a bunch of other loop passes.
LoopReroll didn't use loop simplified form previously, and I've updated
the test case to match the trivially different output.
Finally, I've also factored all the pass initialization for the passes
that use this technique as well, so that should be done regularly and
reliably.
Thanks to James for the help reviewing and thinking about this stuff,
and Ben for help thinking about it as well!
Differential Revision: http://reviews.llvm.org/D17435
llvm-svn: 261316
2016-02-19 18:45:18 +08:00
|
|
|
#include "llvm/Transforms/Utils/LoopUtils.h"
|
2016-01-30 06:35:36 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "loop-simplifycfg"
|
|
|
|
|
2018-11-20 13:43:32 +08:00
|
|
|
STATISTIC(NumTerminatorsFolded,
|
|
|
|
"Number of terminators folded to unconditional branches");
|
|
|
|
|
|
|
|
/// If \p BB is a switch or a conditional branch, but only one of its successors
|
|
|
|
/// can be reached from this block in runtime, return this successor. Otherwise,
|
|
|
|
/// return nullptr.
|
|
|
|
static BasicBlock *getOnlyLiveSuccessor(BasicBlock *BB) {
|
|
|
|
Instruction *TI = BB->getTerminator();
|
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
|
|
|
|
if (BI->isUnconditional())
|
|
|
|
return nullptr;
|
|
|
|
if (BI->getSuccessor(0) == BI->getSuccessor(1))
|
|
|
|
return BI->getSuccessor(0);
|
|
|
|
ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
|
|
|
|
if (!Cond)
|
|
|
|
return nullptr;
|
|
|
|
return Cond->isZero() ? BI->getSuccessor(1) : BI->getSuccessor(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
|
|
|
|
auto *CI = dyn_cast<ConstantInt>(SI->getCondition());
|
|
|
|
if (!CI)
|
|
|
|
return nullptr;
|
|
|
|
for (auto Case : SI->cases())
|
|
|
|
if (Case.getCaseValue() == CI)
|
|
|
|
return Case.getCaseSuccessor();
|
|
|
|
return SI->getDefaultDest();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper class that can turn branches and switches with constant conditions
|
|
|
|
/// into unconditional branches.
|
|
|
|
class ConstantTerminatorFoldingImpl {
|
|
|
|
private:
|
|
|
|
Loop &L;
|
|
|
|
LoopInfo &LI;
|
|
|
|
DominatorTree &DT;
|
|
|
|
|
|
|
|
// Whether or not the current loop will still exist after terminator constant
|
|
|
|
// folding will be done. In theory, there are two ways how it can happen:
|
|
|
|
// 1. Loop's latch(es) become unreachable from loop header;
|
|
|
|
// 2. Loop's header becomes unreachable from method entry.
|
|
|
|
// In practice, the second situation is impossible because we only modify the
|
|
|
|
// current loop and its preheader and do not affect preheader's reachibility
|
|
|
|
// from any other block. So this variable set to true means that loop's latch
|
|
|
|
// has become unreachable from loop header.
|
|
|
|
bool DeleteCurrentLoop = false;
|
|
|
|
|
|
|
|
// The blocks of the original loop that will still be reachable from entry
|
|
|
|
// after the constant folding.
|
|
|
|
SmallPtrSet<BasicBlock *, 8> LiveLoopBlocks;
|
|
|
|
// The blocks of the original loop that will become unreachable from entry
|
|
|
|
// after the constant folding.
|
|
|
|
SmallPtrSet<BasicBlock *, 8> DeadLoopBlocks;
|
|
|
|
// The exits of the original loop that will still be reachable from entry
|
|
|
|
// after the constant folding.
|
|
|
|
SmallPtrSet<BasicBlock *, 8> LiveExitBlocks;
|
|
|
|
// The exits of the original loop that will become unreachable from entry
|
|
|
|
// after the constant folding.
|
2018-11-22 20:33:41 +08:00
|
|
|
SmallVector<BasicBlock *, 8> DeadExitBlocks;
|
2018-11-20 13:43:32 +08:00
|
|
|
// The blocks that will still be a part of the current loop after folding.
|
|
|
|
SmallPtrSet<BasicBlock *, 8> BlocksInLoopAfterFolding;
|
|
|
|
// The blocks that have terminators with constant condition that can be
|
|
|
|
// folded. Note: fold candidates should be in L but not in any of its
|
|
|
|
// subloops to avoid complex LI updates.
|
|
|
|
SmallVector<BasicBlock *, 8> FoldCandidates;
|
|
|
|
|
|
|
|
void dump() const {
|
|
|
|
dbgs() << "Constant terminator folding for loop " << L << "\n";
|
|
|
|
dbgs() << "After terminator constant-folding, the loop will";
|
|
|
|
if (!DeleteCurrentLoop)
|
|
|
|
dbgs() << " not";
|
|
|
|
dbgs() << " be destroyed\n";
|
2018-11-22 20:33:41 +08:00
|
|
|
auto PrintOutVector = [&](const char *Message,
|
|
|
|
const SmallVectorImpl<BasicBlock *> &S) {
|
|
|
|
dbgs() << Message << "\n";
|
|
|
|
for (const BasicBlock *BB : S)
|
|
|
|
dbgs() << "\t" << BB->getName() << "\n";
|
|
|
|
};
|
2018-11-20 13:43:32 +08:00
|
|
|
auto PrintOutSet = [&](const char *Message,
|
|
|
|
const SmallPtrSetImpl<BasicBlock *> &S) {
|
|
|
|
dbgs() << Message << "\n";
|
|
|
|
for (const BasicBlock *BB : S)
|
|
|
|
dbgs() << "\t" << BB->getName() << "\n";
|
|
|
|
};
|
2018-11-22 20:33:41 +08:00
|
|
|
PrintOutVector("Blocks in which we can constant-fold terminator:",
|
|
|
|
FoldCandidates);
|
2018-11-20 13:43:32 +08:00
|
|
|
PrintOutSet("Live blocks from the original loop:", LiveLoopBlocks);
|
|
|
|
PrintOutSet("Dead blocks from the original loop:", DeadLoopBlocks);
|
|
|
|
PrintOutSet("Live exit blocks:", LiveExitBlocks);
|
2018-11-22 20:33:41 +08:00
|
|
|
PrintOutVector("Dead exit blocks:", DeadExitBlocks);
|
2018-11-20 13:43:32 +08:00
|
|
|
if (!DeleteCurrentLoop)
|
|
|
|
PrintOutSet("The following blocks will still be part of the loop:",
|
|
|
|
BlocksInLoopAfterFolding);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fill all information about status of blocks and exits of the current loop
|
|
|
|
/// if constant folding of all branches will be done.
|
|
|
|
void analyze() {
|
|
|
|
LoopBlocksDFS DFS(&L);
|
|
|
|
DFS.perform(&LI);
|
|
|
|
assert(DFS.isComplete() && "DFS is expected to be finished");
|
|
|
|
|
|
|
|
// Collect live and dead loop blocks and exits.
|
|
|
|
LiveLoopBlocks.insert(L.getHeader());
|
|
|
|
for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) {
|
|
|
|
BasicBlock *BB = *I;
|
|
|
|
|
|
|
|
// If a loop block wasn't marked as live so far, then it's dead.
|
|
|
|
if (!LiveLoopBlocks.count(BB)) {
|
|
|
|
DeadLoopBlocks.insert(BB);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB);
|
|
|
|
|
|
|
|
// If a block has only one live successor, it's a candidate on constant
|
|
|
|
// folding. Only handle blocks from current loop: branches in child loops
|
|
|
|
// are skipped because if they can be folded, they should be folded during
|
|
|
|
// the processing of child loops.
|
|
|
|
if (TheOnlySucc && LI.getLoopFor(BB) == &L)
|
|
|
|
FoldCandidates.push_back(BB);
|
|
|
|
|
|
|
|
// Handle successors.
|
|
|
|
for (BasicBlock *Succ : successors(BB))
|
2018-11-22 18:48:30 +08:00
|
|
|
if (!TheOnlySucc || TheOnlySucc == Succ) {
|
|
|
|
if (L.contains(Succ))
|
|
|
|
LiveLoopBlocks.insert(Succ);
|
|
|
|
else
|
|
|
|
LiveExitBlocks.insert(Succ);
|
|
|
|
}
|
2018-11-20 13:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check: amount of dead and live loop blocks should match the total
|
|
|
|
// number of blocks in loop.
|
|
|
|
assert(L.getNumBlocks() == LiveLoopBlocks.size() + DeadLoopBlocks.size() &&
|
|
|
|
"Malformed block sets?");
|
|
|
|
|
|
|
|
// Now, all exit blocks that are not marked as live are dead.
|
2018-11-22 18:48:30 +08:00
|
|
|
SmallVector<BasicBlock *, 8> ExitBlocks;
|
|
|
|
L.getExitBlocks(ExitBlocks);
|
2018-11-20 13:43:32 +08:00
|
|
|
for (auto *ExitBlock : ExitBlocks)
|
|
|
|
if (!LiveExitBlocks.count(ExitBlock))
|
2018-11-22 20:33:41 +08:00
|
|
|
DeadExitBlocks.push_back(ExitBlock);
|
2018-11-20 13:43:32 +08:00
|
|
|
|
|
|
|
// Whether or not the edge From->To will still be present in graph after the
|
|
|
|
// folding.
|
|
|
|
auto IsEdgeLive = [&](BasicBlock *From, BasicBlock *To) {
|
|
|
|
if (!LiveLoopBlocks.count(From))
|
|
|
|
return false;
|
|
|
|
BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(From);
|
|
|
|
return !TheOnlySucc || TheOnlySucc == To;
|
|
|
|
};
|
|
|
|
|
|
|
|
// The loop will not be destroyed if its latch is live.
|
|
|
|
DeleteCurrentLoop = !IsEdgeLive(L.getLoopLatch(), L.getHeader());
|
|
|
|
|
|
|
|
// If we are going to delete the current loop completely, no extra analysis
|
|
|
|
// is needed.
|
|
|
|
if (DeleteCurrentLoop)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Otherwise, we should check which blocks will still be a part of the
|
|
|
|
// current loop after the transform.
|
|
|
|
BlocksInLoopAfterFolding.insert(L.getLoopLatch());
|
|
|
|
// If the loop is live, then we should compute what blocks are still in
|
|
|
|
// loop after all branch folding has been done. A block is in loop if
|
|
|
|
// it has a live edge to another block that is in the loop; by definition,
|
|
|
|
// latch is in the loop.
|
|
|
|
auto BlockIsInLoop = [&](BasicBlock *BB) {
|
|
|
|
return any_of(successors(BB), [&](BasicBlock *Succ) {
|
|
|
|
return BlocksInLoopAfterFolding.count(Succ) && IsEdgeLive(BB, Succ);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
for (auto I = DFS.beginPostorder(), E = DFS.endPostorder(); I != E; ++I) {
|
|
|
|
BasicBlock *BB = *I;
|
|
|
|
if (BlockIsInLoop(BB))
|
|
|
|
BlocksInLoopAfterFolding.insert(BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check: header must be in loop.
|
|
|
|
assert(BlocksInLoopAfterFolding.count(L.getHeader()) &&
|
|
|
|
"Header not in loop?");
|
2018-11-22 20:43:27 +08:00
|
|
|
assert(BlocksInLoopAfterFolding.size() <= LiveLoopBlocks.size() &&
|
|
|
|
"All blocks that stay in loop should be live!");
|
2018-11-20 13:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constant-fold terminators of blocks acculumated in FoldCandidates into the
|
|
|
|
/// unconditional branches.
|
|
|
|
void foldTerminators() {
|
|
|
|
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
|
|
|
|
|
|
|
|
for (BasicBlock *BB : FoldCandidates) {
|
|
|
|
assert(LI.getLoopFor(BB) == &L && "Should be a loop block!");
|
|
|
|
BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB);
|
|
|
|
assert(TheOnlySucc && "Should have one live successor!");
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Replacing terminator of " << BB->getName()
|
|
|
|
<< " with an unconditional branch to the block "
|
|
|
|
<< TheOnlySucc->getName() << "\n");
|
|
|
|
|
|
|
|
SmallPtrSet<BasicBlock *, 2> DeadSuccessors;
|
|
|
|
// Remove all BB's successors except for the live one.
|
|
|
|
for (auto *Succ : successors(BB))
|
|
|
|
if (Succ != TheOnlySucc) {
|
|
|
|
DeadSuccessors.insert(Succ);
|
2018-11-23 15:56:47 +08:00
|
|
|
// If our successor lies in a different loop, we don't want to remove
|
|
|
|
// the one-input Phi because it is a LCSSA Phi.
|
|
|
|
bool PreserveLCSSAPhi = !L.contains(Succ);
|
|
|
|
Succ->removePredecessor(BB, PreserveLCSSAPhi);
|
2018-11-20 13:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IRBuilder<> Builder(BB->getContext());
|
|
|
|
Instruction *Term = BB->getTerminator();
|
|
|
|
Builder.SetInsertPoint(Term);
|
|
|
|
Builder.CreateBr(TheOnlySucc);
|
|
|
|
Term->eraseFromParent();
|
|
|
|
|
|
|
|
for (auto *DeadSucc : DeadSuccessors)
|
|
|
|
DTU.deleteEdge(BB, DeadSucc);
|
|
|
|
|
|
|
|
++NumTerminatorsFolded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI, DominatorTree &DT)
|
|
|
|
: L(L), LI(LI), DT(DT) {}
|
|
|
|
bool run() {
|
|
|
|
assert(L.getLoopLatch() && "Should be single latch!");
|
|
|
|
|
|
|
|
// Collect all available information about status of blocks after constant
|
|
|
|
// folding.
|
|
|
|
analyze();
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "In function " << L.getHeader()->getParent()->getName()
|
|
|
|
<< ": ");
|
|
|
|
|
|
|
|
// Nothing to constant-fold.
|
|
|
|
if (FoldCandidates.empty()) {
|
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "No constant terminator folding candidates found in loop "
|
|
|
|
<< L.getHeader()->getName() << "\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Support deletion of the current loop.
|
|
|
|
if (DeleteCurrentLoop) {
|
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs()
|
|
|
|
<< "Give up constant terminator folding in loop "
|
|
|
|
<< L.getHeader()->getName()
|
|
|
|
<< ": we don't currently support deletion of the current loop.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Support deletion of dead loop blocks.
|
|
|
|
if (!DeadLoopBlocks.empty()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Give up constant terminator folding in loop "
|
|
|
|
<< L.getHeader()->getName()
|
|
|
|
<< ": we don't currently"
|
|
|
|
" support deletion of dead in-loop blocks.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Support dead loop exits.
|
|
|
|
if (!DeadExitBlocks.empty()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Give up constant terminator folding in loop "
|
|
|
|
<< L.getHeader()->getName()
|
|
|
|
<< ": we don't currently support dead loop exits.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Support blocks that are not dead, but also not in loop after the
|
|
|
|
// folding.
|
|
|
|
if (BlocksInLoopAfterFolding.size() != L.getNumBlocks()) {
|
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "Give up constant terminator folding in loop "
|
|
|
|
<< L.getHeader()->getName()
|
|
|
|
<< ": we don't currently"
|
|
|
|
" support blocks that are not dead, but will stop "
|
|
|
|
"being a part of the loop after constant-folding.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump analysis results.
|
|
|
|
LLVM_DEBUG(dump());
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Constant-folding " << FoldCandidates.size()
|
|
|
|
<< " terminators in loop " << L.getHeader()->getName()
|
|
|
|
<< "\n");
|
|
|
|
|
|
|
|
// Make the actual transforms.
|
|
|
|
foldTerminators();
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Make sure that we have preserved all data structures after the transform.
|
|
|
|
DT.verify();
|
|
|
|
assert(DT.isReachableFromEntry(L.getHeader()));
|
|
|
|
LI.verify(DT);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Turn branches and switches with known constant conditions into unconditional
|
|
|
|
/// branches.
|
|
|
|
static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI) {
|
|
|
|
// To keep things simple, only process loops with single latch. We
|
|
|
|
// canonicalize most loops to this form. We can support multi-latch if needed.
|
|
|
|
if (!L.getLoopLatch())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ConstantTerminatorFoldingImpl BranchFolder(L, LI, DT);
|
|
|
|
return BranchFolder.run();
|
|
|
|
}
|
|
|
|
|
2018-11-01 17:42:50 +08:00
|
|
|
static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree &DT,
|
|
|
|
LoopInfo &LI, MemorySSAUpdater *MSSAU) {
|
2016-01-30 06:35:36 +08:00
|
|
|
bool Changed = false;
|
2018-08-03 13:08:17 +08:00
|
|
|
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
|
2016-01-30 06:35:36 +08:00
|
|
|
// Copy blocks into a temporary array to avoid iterator invalidation issues
|
|
|
|
// as we remove them.
|
2017-05-02 01:07:49 +08:00
|
|
|
SmallVector<WeakTrackingVH, 16> Blocks(L.blocks());
|
2016-01-30 06:35:36 +08:00
|
|
|
|
|
|
|
for (auto &Block : Blocks) {
|
|
|
|
// Attempt to merge blocks in the trivial case. Don't modify blocks which
|
|
|
|
// belong to other loops.
|
2016-01-30 07:12:52 +08:00
|
|
|
BasicBlock *Succ = cast_or_null<BasicBlock>(Block);
|
2016-01-30 06:35:36 +08:00
|
|
|
if (!Succ)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BasicBlock *Pred = Succ->getSinglePredecessor();
|
2016-05-04 05:47:32 +08:00
|
|
|
if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L)
|
2016-01-30 06:35:36 +08:00
|
|
|
continue;
|
|
|
|
|
Generalize MergeBlockIntoPredecessor. Replace uses of MergeBasicBlockIntoOnlyPred.
Summary:
Two utils methods have essentially the same functionality. This is an attempt to merge them into one.
1. lib/Transforms/Utils/Local.cpp : MergeBasicBlockIntoOnlyPred
2. lib/Transforms/Utils/BasicBlockUtils.cpp : MergeBlockIntoPredecessor
Prior to the patch:
1. MergeBasicBlockIntoOnlyPred
Updates either DomTree or DeferredDominance
Moves all instructions from Pred to BB, deletes Pred
Asserts BB has single predecessor
If address was taken, replace the block address with constant 1 (?)
2. MergeBlockIntoPredecessor
Updates DomTree, LoopInfo and MemoryDependenceResults
Moves all instruction from BB to Pred, deletes BB
Returns if doesn't have a single predecessor
Returns if BB's address was taken
After the patch:
Method 2. MergeBlockIntoPredecessor is attempting to become the new default:
Updates DomTree or DeferredDominance, and LoopInfo and MemoryDependenceResults
Moves all instruction from BB to Pred, deletes BB
Returns if doesn't have a single predecessor
Returns if BB's address was taken
Uses of MergeBasicBlockIntoOnlyPred that need to be replaced:
1. lib/Transforms/Scalar/LoopSimplifyCFG.cpp
Updated in this patch. No challenges.
2. lib/CodeGen/CodeGenPrepare.cpp
Updated in this patch.
i. eliminateFallThrough is straightforward, but I added using a temporary array to avoid the iterator invalidation.
ii. eliminateMostlyEmptyBlock(s) methods also now use a temporary array for blocks
Some interesting aspects:
- Since Pred is not deleted (BB is), the entry block does not need updating.
- The entry block was being updated with the deleted block in eliminateMostlyEmptyBlock. Added assert to make obvious that BB=SinglePred.
- isMergingEmptyBlockProfitable assumes BB is the one to be deleted.
- eliminateMostlyEmptyBlock(BB) does not delete BB on one path, it deletes its unique predecessor instead.
- adding some test owner as subscribers for the interesting tests modified:
test/CodeGen/X86/avx-cmp.ll
test/CodeGen/AMDGPU/nested-loop-conditions.ll
test/CodeGen/AMDGPU/si-annotate-cf.ll
test/CodeGen/X86/hoist-spill.ll
test/CodeGen/X86/2006-11-17-IllegalMove.ll
3. lib/Transforms/Scalar/JumpThreading.cpp
Not covered in this patch. It is the only use case using the DeferredDominance.
I would defer to Brian Rzycki to make this replacement.
Reviewers: chandlerc, spatel, davide, brzycki, bkramer, javed.absar
Subscribers: qcolombet, sanjoy, nemanjai, nhaehnle, jlebar, tpr, kbarton, RKSimon, wmi, arsenm, llvm-commits
Differential Revision: https://reviews.llvm.org/D48202
llvm-svn: 335183
2018-06-21 06:01:04 +08:00
|
|
|
// Merge Succ into Pred and delete it.
|
2018-08-23 04:10:21 +08:00
|
|
|
MergeBlockIntoPredecessor(Succ, &DTU, &LI, MSSAU);
|
2018-06-19 17:43:36 +08:00
|
|
|
|
2016-01-30 06:35:36 +08:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2018-11-01 17:42:50 +08:00
|
|
|
static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI,
|
|
|
|
ScalarEvolution &SE, MemorySSAUpdater *MSSAU) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
2018-11-20 13:43:32 +08:00
|
|
|
// Constant-fold terminators with known constant conditions.
|
|
|
|
Changed |= constantFoldTerminators(L, DT, LI);
|
|
|
|
|
2018-11-01 17:42:50 +08:00
|
|
|
// Eliminate unconditional branches by merging blocks into their predecessors.
|
|
|
|
Changed |= mergeBlocksIntoPredecessors(L, DT, LI, MSSAU);
|
|
|
|
|
|
|
|
if (Changed)
|
|
|
|
SE.forgetTopmostLoop(&L);
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-01-11 14:23:21 +08:00
|
|
|
PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM,
|
|
|
|
LoopStandardAnalysisResults &AR,
|
|
|
|
LPMUpdater &) {
|
2018-08-23 04:10:21 +08:00
|
|
|
Optional<MemorySSAUpdater> MSSAU;
|
|
|
|
if (EnableMSSALoopDependency && AR.MSSA)
|
|
|
|
MSSAU = MemorySSAUpdater(AR.MSSA);
|
|
|
|
if (!simplifyLoopCFG(L, AR.DT, AR.LI, AR.SE,
|
|
|
|
MSSAU.hasValue() ? MSSAU.getPointer() : nullptr))
|
2016-05-04 05:47:32 +08:00
|
|
|
return PreservedAnalyses::all();
|
2017-01-15 14:32:49 +08:00
|
|
|
|
2016-05-04 05:47:32 +08:00
|
|
|
return getLoopPassPreservedAnalyses();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class LoopSimplifyCFGLegacyPass : public LoopPass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass ID, replacement for typeid
|
|
|
|
LoopSimplifyCFGLegacyPass() : LoopPass(ID) {
|
|
|
|
initializeLoopSimplifyCFGLegacyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnLoop(Loop *L, LPPassManager &) override {
|
|
|
|
if (skipLoop(L))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
|
|
|
LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
2018-06-19 17:43:36 +08:00
|
|
|
ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
|
2018-08-23 04:10:21 +08:00
|
|
|
Optional<MemorySSAUpdater> MSSAU;
|
|
|
|
if (EnableMSSALoopDependency) {
|
|
|
|
MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
|
|
|
|
MSSAU = MemorySSAUpdater(MSSA);
|
|
|
|
if (VerifyMemorySSA)
|
|
|
|
MSSA->verifyMemorySSA();
|
|
|
|
}
|
|
|
|
return simplifyLoopCFG(*L, DT, LI, SE,
|
|
|
|
MSSAU.hasValue() ? MSSAU.getPointer() : nullptr);
|
2016-05-04 05:47:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2018-08-23 04:10:21 +08:00
|
|
|
if (EnableMSSALoopDependency) {
|
|
|
|
AU.addRequired<MemorySSAWrapperPass>();
|
|
|
|
AU.addPreserved<MemorySSAWrapperPass>();
|
|
|
|
}
|
2016-05-13 06:19:39 +08:00
|
|
|
AU.addPreserved<DependenceAnalysisWrapperPass>();
|
2016-05-04 05:47:32 +08:00
|
|
|
getLoopAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char LoopSimplifyCFGLegacyPass::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
|
|
|
|
"Simplify loop CFG", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopPass)
|
2018-08-23 04:10:21 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
|
2016-05-04 05:47:32 +08:00
|
|
|
INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
|
|
|
|
"Simplify loop CFG", false, false)
|
2016-01-30 06:35:36 +08:00
|
|
|
|
2016-05-04 05:47:32 +08:00
|
|
|
Pass *llvm::createLoopSimplifyCFGPass() {
|
|
|
|
return new LoopSimplifyCFGLegacyPass();
|
2016-01-30 06:35:36 +08:00
|
|
|
}
|