blob: 2b83d3dc5f1b83a9e0140cd6182ecd41bd1b7685 [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"
David Blaikiea373d182018-03-28 17:44:36 +000033#include "llvm/Transforms/Utils.h"
Alina Sbirleadfd14ad2018-06-20 22:01:04 +000034#include "llvm/Transforms/Utils/BasicBlockUtils.h"
35#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth31088a92016-02-19 10:45:18 +000036#include "llvm/Transforms/Utils/LoopUtils.h"
Fiona Glaserb417d462016-01-29 22:35:36 +000037using namespace llvm;
38
39#define DEBUG_TYPE "loop-simplifycfg"
40
David Greene6a9c242018-06-19 09:43:36 +000041static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI,
42 ScalarEvolution &SE) {
Fiona Glaserb417d462016-01-29 22:35:36 +000043 bool Changed = false;
44 // Copy blocks into a temporary array to avoid iterator invalidation issues
45 // as we remove them.
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000046 SmallVector<WeakTrackingVH, 16> Blocks(L.blocks());
Fiona Glaserb417d462016-01-29 22:35:36 +000047
48 for (auto &Block : Blocks) {
49 // Attempt to merge blocks in the trivial case. Don't modify blocks which
50 // belong to other loops.
Fiona Glaser36e82302016-01-29 23:12:52 +000051 BasicBlock *Succ = cast_or_null<BasicBlock>(Block);
Fiona Glaserb417d462016-01-29 22:35:36 +000052 if (!Succ)
53 continue;
54
55 BasicBlock *Pred = Succ->getSinglePredecessor();
Justin Bognerab6a5132016-05-03 21:47:32 +000056 if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L)
Fiona Glaserb417d462016-01-29 22:35:36 +000057 continue;
58
Alina Sbirleadfd14ad2018-06-20 22:01:04 +000059 // Merge Succ into Pred and delete it.
60 MergeBlockIntoPredecessor(Succ, &DT, &LI);
David Greene6a9c242018-06-19 09:43:36 +000061
62 SE.forgetLoop(&L);
Fiona Glaserb417d462016-01-29 22:35:36 +000063 Changed = true;
64 }
65
66 return Changed;
67}
68
Chandler Carruth410eaeb2017-01-11 06:23:21 +000069PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM,
70 LoopStandardAnalysisResults &AR,
71 LPMUpdater &) {
David Greene6a9c242018-06-19 09:43:36 +000072 if (!simplifyLoopCFG(L, AR.DT, AR.LI, AR.SE))
Justin Bognerab6a5132016-05-03 21:47:32 +000073 return PreservedAnalyses::all();
Chandler Carruthca68a3e2017-01-15 06:32:49 +000074
Justin Bognerab6a5132016-05-03 21:47:32 +000075 return getLoopPassPreservedAnalyses();
76}
77
78namespace {
79class LoopSimplifyCFGLegacyPass : public LoopPass {
80public:
81 static char ID; // Pass ID, replacement for typeid
82 LoopSimplifyCFGLegacyPass() : LoopPass(ID) {
83 initializeLoopSimplifyCFGLegacyPassPass(*PassRegistry::getPassRegistry());
84 }
85
86 bool runOnLoop(Loop *L, LPPassManager &) override {
87 if (skipLoop(L))
88 return false;
89
90 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
91 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
David Greene6a9c242018-06-19 09:43:36 +000092 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
93 return simplifyLoopCFG(*L, DT, LI, SE);
Justin Bognerab6a5132016-05-03 21:47:32 +000094 }
95
96 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth49c22192016-05-12 22:19:39 +000097 AU.addPreserved<DependenceAnalysisWrapperPass>();
Justin Bognerab6a5132016-05-03 21:47:32 +000098 getLoopAnalysisUsage(AU);
99 }
100};
101}
102
103char LoopSimplifyCFGLegacyPass::ID = 0;
104INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
105 "Simplify loop CFG", false, false)
106INITIALIZE_PASS_DEPENDENCY(LoopPass)
107INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
108 "Simplify loop CFG", false, false)
109
110Pass *llvm::createLoopSimplifyCFGPass() {
111 return new LoopSimplifyCFGLegacyPass();
Fiona Glaserb417d462016-01-29 22:35:36 +0000112}