blob: 35c05e84fd68d85b73f7f9c4291920f7ef677b15 [file] [log] [blame]
Fiona Glaserb417d462016-01-29 22:35:36 +00001//===--------- LoopSimplifyCFG.cpp - Loop CFG Simplification Pass ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Loop SimplifyCFG Pass. This pass is responsible for
11// basic loop CFG cleanup, primarily to assist other loop passes. If you
12// encounter a noncanonical CFG construct that causes another loop pass to
13// perform suboptimally, this is the place to fix it up.
14//
15//===----------------------------------------------------------------------===//
16
Justin Bognerab6a5132016-05-03 21:47:32 +000017#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Fiona Glaserb417d462016-01-29 22:35:36 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/Analysis/AliasAnalysis.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000021#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000022#include "llvm/Analysis/BasicAliasAnalysis.h"
Fiona Glaserb417d462016-01-29 22:35:36 +000023#include "llvm/Analysis/DependenceAnalysis.h"
24#include "llvm/Analysis/GlobalsModRef.h"
25#include "llvm/Analysis/LoopInfo.h"
26#include "llvm/Analysis/LoopPass.h"
27#include "llvm/Analysis/ScalarEvolution.h"
28#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
29#include "llvm/Analysis/TargetTransformInfo.h"
30#include "llvm/IR/Dominators.h"
Justin Bognerab6a5132016-05-03 21:47:32 +000031#include "llvm/Transforms/Scalar.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000032#include "llvm/Transforms/Scalar/LoopPassManager.h"
Fiona Glaserb417d462016-01-29 22:35:36 +000033#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth31088a92016-02-19 10:45:18 +000034#include "llvm/Transforms/Utils/LoopUtils.h"
Fiona Glaserb417d462016-01-29 22:35:36 +000035using namespace llvm;
36
37#define DEBUG_TYPE "loop-simplifycfg"
38
Justin Bognerab6a5132016-05-03 21:47:32 +000039static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI) {
Fiona Glaserb417d462016-01-29 22:35:36 +000040 bool Changed = false;
41 // Copy blocks into a temporary array to avoid iterator invalidation issues
42 // as we remove them.
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000043 SmallVector<WeakTrackingVH, 16> Blocks(L.blocks());
Fiona Glaserb417d462016-01-29 22:35:36 +000044
45 for (auto &Block : Blocks) {
46 // Attempt to merge blocks in the trivial case. Don't modify blocks which
47 // belong to other loops.
Fiona Glaser36e82302016-01-29 23:12:52 +000048 BasicBlock *Succ = cast_or_null<BasicBlock>(Block);
Fiona Glaserb417d462016-01-29 22:35:36 +000049 if (!Succ)
50 continue;
51
52 BasicBlock *Pred = Succ->getSinglePredecessor();
Justin Bognerab6a5132016-05-03 21:47:32 +000053 if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L)
Fiona Glaserb417d462016-01-29 22:35:36 +000054 continue;
55
56 // Pred is going to disappear, so we need to update the loop info.
Justin Bognerab6a5132016-05-03 21:47:32 +000057 if (L.getHeader() == Pred)
58 L.moveToHeader(Succ);
59 LI.removeBlock(Pred);
60 MergeBasicBlockIntoOnlyPred(Succ, &DT);
Fiona Glaserb417d462016-01-29 22:35:36 +000061 Changed = true;
62 }
63
64 return Changed;
65}
66
Chandler Carruth410eaeb2017-01-11 06:23:21 +000067PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM,
68 LoopStandardAnalysisResults &AR,
69 LPMUpdater &) {
70 if (!simplifyLoopCFG(L, AR.DT, AR.LI))
Justin Bognerab6a5132016-05-03 21:47:32 +000071 return PreservedAnalyses::all();
Chandler Carruthca68a3e2017-01-15 06:32:49 +000072
Justin Bognerab6a5132016-05-03 21:47:32 +000073 return getLoopPassPreservedAnalyses();
74}
75
76namespace {
77class LoopSimplifyCFGLegacyPass : public LoopPass {
78public:
79 static char ID; // Pass ID, replacement for typeid
80 LoopSimplifyCFGLegacyPass() : LoopPass(ID) {
81 initializeLoopSimplifyCFGLegacyPassPass(*PassRegistry::getPassRegistry());
82 }
83
84 bool runOnLoop(Loop *L, LPPassManager &) override {
85 if (skipLoop(L))
86 return false;
87
88 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
89 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
90 return simplifyLoopCFG(*L, DT, LI);
91 }
92
93 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth49c22192016-05-12 22:19:39 +000094 AU.addPreserved<DependenceAnalysisWrapperPass>();
Justin Bognerab6a5132016-05-03 21:47:32 +000095 getLoopAnalysisUsage(AU);
96 }
97};
98}
99
100char LoopSimplifyCFGLegacyPass::ID = 0;
101INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
102 "Simplify loop CFG", false, false)
103INITIALIZE_PASS_DEPENDENCY(LoopPass)
104INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
105 "Simplify loop CFG", false, false)
106
107Pass *llvm::createLoopSimplifyCFGPass() {
108 return new LoopSimplifyCFGLegacyPass();
Fiona Glaserb417d462016-01-29 22:35:36 +0000109}