blob: f9d7840499815aa7b93c6c8430d45badb040ab73 [file] [log] [blame]
Devang Patelf42389f2007-04-07 01:25:15 +00001//===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Devang Patelf42389f2007-04-07 01:25:15 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements Loop Rotation Pass.
10//
11//===----------------------------------------------------------------------===//
12
Justin Bognerd0d23412016-05-03 22:02:31 +000013#include "llvm/Transforms/Scalar/LoopRotation.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/ADT/Statistic.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000015#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Analysis/LoopPass.h"
Alina Sbirleaad4d0182018-10-24 22:46:45 +000017#include "llvm/Analysis/MemorySSA.h"
18#include "llvm/Analysis/MemorySSAUpdater.h"
Devang Patelfac4d1f2007-07-11 23:47:28 +000019#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruthbb9caa92013-01-21 13:04:33 +000020#include "llvm/Analysis/TargetTransformInfo.h"
Devang Patelf42389f2007-04-07 01:25:15 +000021#include "llvm/Support/Debug.h"
Justin Bognerd0d23412016-05-03 22:02:31 +000022#include "llvm/Transforms/Scalar.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000023#include "llvm/Transforms/Scalar/LoopPassManager.h"
David Greenb0aa36f2018-03-29 08:48:15 +000024#include "llvm/Transforms/Utils/LoopRotationUtils.h"
Chandler Carruth31088a92016-02-19 10:45:18 +000025#include "llvm/Transforms/Utils/LoopUtils.h"
Devang Patelf42389f2007-04-07 01:25:15 +000026using namespace llvm;
27
Chandler Carruth964daaa2014-04-22 02:55:47 +000028#define DEBUG_TYPE "loop-rotate"
29
Sebastian Popdfb66a12016-06-14 14:44:05 +000030static cl::opt<unsigned> DefaultRotationThreshold(
31 "rotation-max-header-size", cl::init(16), cl::Hidden,
32 cl::desc("The default maximum header size for automatic loop rotation"));
Devang Patelf42389f2007-04-07 01:25:15 +000033
Chandler Carruthe3f50642016-12-22 06:59:15 +000034LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication)
35 : EnableHeaderDuplication(EnableHeaderDuplication) {}
Justin Bognerd0d23412016-05-03 22:02:31 +000036
Chandler Carruth410eaeb2017-01-11 06:23:21 +000037PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
38 LoopStandardAnalysisResults &AR,
39 LPMUpdater &) {
Chandler Carruthe3f50642016-12-22 06:59:15 +000040 int Threshold = EnableHeaderDuplication ? DefaultRotationThreshold : 0;
Daniel Berlin62aee142017-04-26 13:52:18 +000041 const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
Daniel Berlin98a1de82017-04-28 22:05:55 +000042 const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
Justin Bognerd0d23412016-05-03 22:02:31 +000043
Alina Sbirleaad4d0182018-10-24 22:46:45 +000044 Optional<MemorySSAUpdater> MSSAU;
45 if (AR.MSSA)
46 MSSAU = MemorySSAUpdater(AR.MSSA);
47 bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,
48 MSSAU.hasValue() ? MSSAU.getPointer() : nullptr,
49 SQ, false, Threshold, false);
David Greenb0aa36f2018-03-29 08:48:15 +000050
Justin Bognerd0d23412016-05-03 22:02:31 +000051 if (!Changed)
52 return PreservedAnalyses::all();
Chandler Carruthca68a3e2017-01-15 06:32:49 +000053
Alina Sbirleaad4d0182018-10-24 22:46:45 +000054 if (AR.MSSA && VerifyMemorySSA)
55 AR.MSSA->verifyMemorySSA();
56
Justin Bognerd0d23412016-05-03 22:02:31 +000057 return getLoopPassPreservedAnalyses();
58}
59
Justin Bogner6291b582015-12-14 23:22:48 +000060namespace {
61
Justin Bognerd0d23412016-05-03 22:02:31 +000062class LoopRotateLegacyPass : public LoopPass {
Justin Bogner6291b582015-12-14 23:22:48 +000063 unsigned MaxHeaderSize;
64
65public:
66 static char ID; // Pass ID, replacement for typeid
Justin Bognerd0d23412016-05-03 22:02:31 +000067 LoopRotateLegacyPass(int SpecifiedMaxHeaderSize = -1) : LoopPass(ID) {
68 initializeLoopRotateLegacyPassPass(*PassRegistry::getPassRegistry());
Justin Bogner6291b582015-12-14 23:22:48 +000069 if (SpecifiedMaxHeaderSize == -1)
70 MaxHeaderSize = DefaultRotationThreshold;
71 else
72 MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize);
73 }
74
75 // LCSSA form makes instruction renaming easier.
76 void getAnalysisUsage(AnalysisUsage &AU) const override {
Daniel Jasperaec2fa32016-12-19 08:22:17 +000077 AU.addRequired<AssumptionCacheTracker>();
Justin Bogner6291b582015-12-14 23:22:48 +000078 AU.addRequired<TargetTransformInfoWrapperPass>();
Alina Sbirleaad4d0182018-10-24 22:46:45 +000079 if (EnableMSSALoopDependency) {
80 AU.addRequired<MemorySSAWrapperPass>();
81 AU.addPreserved<MemorySSAWrapperPass>();
82 }
Chandler Carruth31088a92016-02-19 10:45:18 +000083 getLoopAnalysisUsage(AU);
Justin Bogner6291b582015-12-14 23:22:48 +000084 }
85
86 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +000087 if (skipLoop(L))
Justin Bogner6291b582015-12-14 23:22:48 +000088 return false;
89 Function &F = *L->getHeader()->getParent();
90
91 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
92 const auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
Daniel Jasperaec2fa32016-12-19 08:22:17 +000093 auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Justin Bogner6291b582015-12-14 23:22:48 +000094 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
95 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
96 auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
97 auto *SE = SEWP ? &SEWP->getSE() : nullptr;
Daniel Berlin98a1de82017-04-28 22:05:55 +000098 const SimplifyQuery SQ = getBestSimplifyQuery(*this, F);
Alina Sbirleaad4d0182018-10-24 22:46:45 +000099 Optional<MemorySSAUpdater> MSSAU;
100 if (EnableMSSALoopDependency) {
101 MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
102 MSSAU = MemorySSAUpdater(MSSA);
103 }
104 return LoopRotation(L, LI, TTI, AC, DT, SE,
105 MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ,
106 false, MaxHeaderSize, false);
Justin Bogner6291b582015-12-14 23:22:48 +0000107 }
108};
109}
110
Justin Bognerd0d23412016-05-03 22:02:31 +0000111char LoopRotateLegacyPass::ID = 0;
112INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops",
113 false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000114INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth31088a92016-02-19 10:45:18 +0000115INITIALIZE_PASS_DEPENDENCY(LoopPass)
116INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
Alina Sbirleaad4d0182018-10-24 22:46:45 +0000117INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
Sebastian Popdfb66a12016-06-14 14:44:05 +0000118INITIALIZE_PASS_END(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false,
119 false)
Justin Bogner6291b582015-12-14 23:22:48 +0000120
121Pass *llvm::createLoopRotatePass(int MaxHeaderSize) {
Justin Bognerd0d23412016-05-03 22:02:31 +0000122 return new LoopRotateLegacyPass(MaxHeaderSize);
Justin Bogner6291b582015-12-14 23:22:48 +0000123}