2016-01-29 23:35:36 +01: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-03 23:47:32 +02:00
|
|
|
#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
|
2016-01-29 23:35:36 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2016-12-19 09:22:17 +01:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2017-01-11 10:43:56 +01:00
|
|
|
#include "llvm/Analysis/BasicAliasAnalysis.h"
|
2016-01-29 23:35:36 +01: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-04 23:23:21 +02:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2016-01-29 23:35:36 +01:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2016-05-03 23:47:32 +02:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2017-01-11 10:43:56 +01:00
|
|
|
#include "llvm/Transforms/Scalar/LoopPassManager.h"
|
2018-03-28 19:44:36 +02: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 11:45:18 +01:00
|
|
|
#include "llvm/Transforms/Utils/LoopUtils.h"
|
2016-01-29 23:35:36 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "loop-simplifycfg"
|
|
|
|
|
2016-05-03 23:47:32 +02:00
|
|
|
static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI) {
|
2016-01-29 23:35:36 +01:00
|
|
|
bool Changed = false;
|
|
|
|
// Copy blocks into a temporary array to avoid iterator invalidation issues
|
|
|
|
// as we remove them.
|
2017-05-01 19:07:49 +02:00
|
|
|
SmallVector<WeakTrackingVH, 16> Blocks(L.blocks());
|
2016-01-29 23:35:36 +01: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 00:12:52 +01:00
|
|
|
BasicBlock *Succ = cast_or_null<BasicBlock>(Block);
|
2016-01-29 23:35:36 +01:00
|
|
|
if (!Succ)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BasicBlock *Pred = Succ->getSinglePredecessor();
|
2016-05-03 23:47:32 +02:00
|
|
|
if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L)
|
2016-01-29 23:35:36 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Pred is going to disappear, so we need to update the loop info.
|
2016-05-03 23:47:32 +02:00
|
|
|
if (L.getHeader() == Pred)
|
|
|
|
L.moveToHeader(Succ);
|
|
|
|
LI.removeBlock(Pred);
|
|
|
|
MergeBasicBlockIntoOnlyPred(Succ, &DT);
|
2016-01-29 23:35:36 +01:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-01-11 07:23:21 +01:00
|
|
|
PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM,
|
|
|
|
LoopStandardAnalysisResults &AR,
|
|
|
|
LPMUpdater &) {
|
|
|
|
if (!simplifyLoopCFG(L, AR.DT, AR.LI))
|
2016-05-03 23:47:32 +02:00
|
|
|
return PreservedAnalyses::all();
|
2017-01-15 07:32:49 +01:00
|
|
|
|
2016-05-03 23:47:32 +02: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();
|
|
|
|
return simplifyLoopCFG(*L, DT, LI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2016-05-13 00:19:39 +02:00
|
|
|
AU.addPreserved<DependenceAnalysisWrapperPass>();
|
2016-05-03 23:47:32 +02: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-29 23:35:36 +01:00
|
|
|
|
2016-05-03 23:47:32 +02:00
|
|
|
Pass *llvm::createLoopSimplifyCFGPass() {
|
|
|
|
return new LoopSimplifyCFGLegacyPass();
|
2016-01-29 23:35:36 +01:00
|
|
|
}
|