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"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2018-06-05 05:23:21 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.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"
|
[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-06-19 17:43:36 +08:00
|
|
|
static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI,
|
|
|
|
ScalarEvolution &SE) {
|
2016-01-30 06:35:36 +08:00
|
|
|
bool Changed = false;
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Pred is going to disappear, so we need to update the loop info.
|
2016-05-04 05:47:32 +08:00
|
|
|
if (L.getHeader() == Pred)
|
|
|
|
L.moveToHeader(Succ);
|
|
|
|
LI.removeBlock(Pred);
|
|
|
|
MergeBasicBlockIntoOnlyPred(Succ, &DT);
|
2018-06-19 17:43:36 +08:00
|
|
|
|
|
|
|
SE.forgetLoop(&L);
|
2016-01-30 06:35:36 +08:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-01-11 14:23:21 +08:00
|
|
|
PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM,
|
|
|
|
LoopStandardAnalysisResults &AR,
|
|
|
|
LPMUpdater &) {
|
2018-06-19 17:43:36 +08:00
|
|
|
if (!simplifyLoopCFG(L, AR.DT, AR.LI, AR.SE))
|
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();
|
|
|
|
return simplifyLoopCFG(*L, DT, LI, SE);
|
2016-05-04 05:47:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
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)
|
|
|
|
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
|
|
|
}
|