blob: ed37fc8825de8406037574692756e9639a245f31 [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"
Alina Sbirlea8b83d682018-08-22 20:10:21 +000027#include "llvm/Analysis/MemorySSA.h"
28#include "llvm/Analysis/MemorySSAUpdater.h"
Fiona Glaserb417d462016-01-29 22:35:36 +000029#include "llvm/Analysis/ScalarEvolution.h"
30#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
31#include "llvm/Analysis/TargetTransformInfo.h"
Chijun Sima21a8b602018-08-03 05:08:17 +000032#include "llvm/IR/DomTreeUpdater.h"
Fiona Glaserb417d462016-01-29 22:35:36 +000033#include "llvm/IR/Dominators.h"
Justin Bognerab6a5132016-05-03 21:47:32 +000034#include "llvm/Transforms/Scalar.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000035#include "llvm/Transforms/Scalar/LoopPassManager.h"
David Blaikiea373d182018-03-28 17:44:36 +000036#include "llvm/Transforms/Utils.h"
Alina Sbirleadfd14ad2018-06-20 22:01:04 +000037#include "llvm/Transforms/Utils/BasicBlockUtils.h"
38#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth31088a92016-02-19 10:45:18 +000039#include "llvm/Transforms/Utils/LoopUtils.h"
Fiona Glaserb417d462016-01-29 22:35:36 +000040using namespace llvm;
41
42#define DEBUG_TYPE "loop-simplifycfg"
43
David Greene6a9c242018-06-19 09:43:36 +000044static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI,
Alina Sbirlea8b83d682018-08-22 20:10:21 +000045 ScalarEvolution &SE, MemorySSAUpdater *MSSAU) {
Fiona Glaserb417d462016-01-29 22:35:36 +000046 bool Changed = false;
Chijun Sima21a8b602018-08-03 05:08:17 +000047 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
Fiona Glaserb417d462016-01-29 22:35:36 +000048 // Copy blocks into a temporary array to avoid iterator invalidation issues
49 // as we remove them.
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000050 SmallVector<WeakTrackingVH, 16> Blocks(L.blocks());
Fiona Glaserb417d462016-01-29 22:35:36 +000051
52 for (auto &Block : Blocks) {
53 // Attempt to merge blocks in the trivial case. Don't modify blocks which
54 // belong to other loops.
Fiona Glaser36e82302016-01-29 23:12:52 +000055 BasicBlock *Succ = cast_or_null<BasicBlock>(Block);
Fiona Glaserb417d462016-01-29 22:35:36 +000056 if (!Succ)
57 continue;
58
59 BasicBlock *Pred = Succ->getSinglePredecessor();
Justin Bognerab6a5132016-05-03 21:47:32 +000060 if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L)
Fiona Glaserb417d462016-01-29 22:35:36 +000061 continue;
62
Alina Sbirleadfd14ad2018-06-20 22:01:04 +000063 // Merge Succ into Pred and delete it.
Alina Sbirlea8b83d682018-08-22 20:10:21 +000064 MergeBlockIntoPredecessor(Succ, &DTU, &LI, MSSAU);
David Greene6a9c242018-06-19 09:43:36 +000065
Alina Sbirleabf9fe792018-08-09 17:53:26 +000066 SE.forgetTopmostLoop(&L);
67
Fiona Glaserb417d462016-01-29 22:35:36 +000068 Changed = true;
69 }
70
71 return Changed;
72}
73
Chandler Carruth410eaeb2017-01-11 06:23:21 +000074PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM,
75 LoopStandardAnalysisResults &AR,
76 LPMUpdater &) {
Alina Sbirlea8b83d682018-08-22 20:10:21 +000077 Optional<MemorySSAUpdater> MSSAU;
78 if (EnableMSSALoopDependency && AR.MSSA)
79 MSSAU = MemorySSAUpdater(AR.MSSA);
80 if (!simplifyLoopCFG(L, AR.DT, AR.LI, AR.SE,
81 MSSAU.hasValue() ? MSSAU.getPointer() : nullptr))
Justin Bognerab6a5132016-05-03 21:47:32 +000082 return PreservedAnalyses::all();
Chandler Carruthca68a3e2017-01-15 06:32:49 +000083
Justin Bognerab6a5132016-05-03 21:47:32 +000084 return getLoopPassPreservedAnalyses();
85}
86
87namespace {
88class LoopSimplifyCFGLegacyPass : public LoopPass {
89public:
90 static char ID; // Pass ID, replacement for typeid
91 LoopSimplifyCFGLegacyPass() : LoopPass(ID) {
92 initializeLoopSimplifyCFGLegacyPassPass(*PassRegistry::getPassRegistry());
93 }
94
95 bool runOnLoop(Loop *L, LPPassManager &) override {
96 if (skipLoop(L))
97 return false;
98
99 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
100 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
David Greene6a9c242018-06-19 09:43:36 +0000101 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Alina Sbirlea8b83d682018-08-22 20:10:21 +0000102 Optional<MemorySSAUpdater> MSSAU;
103 if (EnableMSSALoopDependency) {
104 MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
105 MSSAU = MemorySSAUpdater(MSSA);
106 if (VerifyMemorySSA)
107 MSSA->verifyMemorySSA();
108 }
109 return simplifyLoopCFG(*L, DT, LI, SE,
110 MSSAU.hasValue() ? MSSAU.getPointer() : nullptr);
Justin Bognerab6a5132016-05-03 21:47:32 +0000111 }
112
113 void getAnalysisUsage(AnalysisUsage &AU) const override {
Alina Sbirlea8b83d682018-08-22 20:10:21 +0000114 if (EnableMSSALoopDependency) {
115 AU.addRequired<MemorySSAWrapperPass>();
116 AU.addPreserved<MemorySSAWrapperPass>();
117 }
Chandler Carruth49c22192016-05-12 22:19:39 +0000118 AU.addPreserved<DependenceAnalysisWrapperPass>();
Justin Bognerab6a5132016-05-03 21:47:32 +0000119 getLoopAnalysisUsage(AU);
120 }
121};
122}
123
124char LoopSimplifyCFGLegacyPass::ID = 0;
125INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
126 "Simplify loop CFG", false, false)
127INITIALIZE_PASS_DEPENDENCY(LoopPass)
Alina Sbirlea8b83d682018-08-22 20:10:21 +0000128INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
Justin Bognerab6a5132016-05-03 21:47:32 +0000129INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
130 "Simplify loop CFG", false, false)
131
132Pass *llvm::createLoopSimplifyCFGPass() {
133 return new LoopSimplifyCFGLegacyPass();
Fiona Glaserb417d462016-01-29 22:35:36 +0000134}