blob: fd22128f7fe6b849c6083623081a934df2e5a3d5 [file] [log] [blame]
Devang Patelf42389f2007-04-07 01:25:15 +00001//===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patelf42389f2007-04-07 01:25:15 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Loop Rotation Pass.
11//
12//===----------------------------------------------------------------------===//
13
Justin Bognerd0d23412016-05-03 22:02:31 +000014#include "llvm/Transforms/Scalar/LoopRotation.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/Statistic.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000016#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Analysis/LoopPass.h"
Alina Sbirleaad4d0182018-10-24 22:46:45 +000018#include "llvm/Analysis/MemorySSA.h"
19#include "llvm/Analysis/MemorySSAUpdater.h"
Devang Patelfac4d1f2007-07-11 23:47:28 +000020#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruthbb9caa92013-01-21 13:04:33 +000021#include "llvm/Analysis/TargetTransformInfo.h"
Devang Patelf42389f2007-04-07 01:25:15 +000022#include "llvm/Support/Debug.h"
Justin Bognerd0d23412016-05-03 22:02:31 +000023#include "llvm/Transforms/Scalar.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000024#include "llvm/Transforms/Scalar/LoopPassManager.h"
David Greenb0aa36f2018-03-29 08:48:15 +000025#include "llvm/Transforms/Utils/LoopRotationUtils.h"
Chandler Carruth31088a92016-02-19 10:45:18 +000026#include "llvm/Transforms/Utils/LoopUtils.h"
Devang Patelf42389f2007-04-07 01:25:15 +000027using namespace llvm;
28
Chandler Carruth964daaa2014-04-22 02:55:47 +000029#define DEBUG_TYPE "loop-rotate"
30
Sebastian Popdfb66a12016-06-14 14:44:05 +000031static cl::opt<unsigned> DefaultRotationThreshold(
32 "rotation-max-header-size", cl::init(16), cl::Hidden,
33 cl::desc("The default maximum header size for automatic loop rotation"));
Devang Patelf42389f2007-04-07 01:25:15 +000034
Chandler Carruthe3f50642016-12-22 06:59:15 +000035LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication)
36 : EnableHeaderDuplication(EnableHeaderDuplication) {}
Justin Bognerd0d23412016-05-03 22:02:31 +000037
Chandler Carruth410eaeb2017-01-11 06:23:21 +000038PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
39 LoopStandardAnalysisResults &AR,
40 LPMUpdater &) {
Chandler Carruthe3f50642016-12-22 06:59:15 +000041 int Threshold = EnableHeaderDuplication ? DefaultRotationThreshold : 0;
Daniel Berlin62aee142017-04-26 13:52:18 +000042 const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
Daniel Berlin98a1de82017-04-28 22:05:55 +000043 const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
Justin Bognerd0d23412016-05-03 22:02:31 +000044
Alina Sbirleaad4d0182018-10-24 22:46:45 +000045 Optional<MemorySSAUpdater> MSSAU;
46 if (AR.MSSA)
47 MSSAU = MemorySSAUpdater(AR.MSSA);
48 bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,
49 MSSAU.hasValue() ? MSSAU.getPointer() : nullptr,
50 SQ, false, Threshold, false);
David Greenb0aa36f2018-03-29 08:48:15 +000051
Justin Bognerd0d23412016-05-03 22:02:31 +000052 if (!Changed)
53 return PreservedAnalyses::all();
Chandler Carruthca68a3e2017-01-15 06:32:49 +000054
Alina Sbirleaad4d0182018-10-24 22:46:45 +000055 if (AR.MSSA && VerifyMemorySSA)
56 AR.MSSA->verifyMemorySSA();
57
Justin Bognerd0d23412016-05-03 22:02:31 +000058 return getLoopPassPreservedAnalyses();
59}
60
Justin Bogner6291b582015-12-14 23:22:48 +000061namespace {
62
Justin Bognerd0d23412016-05-03 22:02:31 +000063class LoopRotateLegacyPass : public LoopPass {
Justin Bogner6291b582015-12-14 23:22:48 +000064 unsigned MaxHeaderSize;
65
66public:
67 static char ID; // Pass ID, replacement for typeid
Justin Bognerd0d23412016-05-03 22:02:31 +000068 LoopRotateLegacyPass(int SpecifiedMaxHeaderSize = -1) : LoopPass(ID) {
69 initializeLoopRotateLegacyPassPass(*PassRegistry::getPassRegistry());
Justin Bogner6291b582015-12-14 23:22:48 +000070 if (SpecifiedMaxHeaderSize == -1)
71 MaxHeaderSize = DefaultRotationThreshold;
72 else
73 MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize);
74 }
75
76 // LCSSA form makes instruction renaming easier.
77 void getAnalysisUsage(AnalysisUsage &AU) const override {
Daniel Jasperaec2fa32016-12-19 08:22:17 +000078 AU.addRequired<AssumptionCacheTracker>();
Justin Bogner6291b582015-12-14 23:22:48 +000079 AU.addRequired<TargetTransformInfoWrapperPass>();
Alina Sbirleaad4d0182018-10-24 22:46:45 +000080 if (EnableMSSALoopDependency) {
81 AU.addRequired<MemorySSAWrapperPass>();
82 AU.addPreserved<MemorySSAWrapperPass>();
83 }
Chandler Carruth31088a92016-02-19 10:45:18 +000084 getLoopAnalysisUsage(AU);
Justin Bogner6291b582015-12-14 23:22:48 +000085 }
86
87 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +000088 if (skipLoop(L))
Justin Bogner6291b582015-12-14 23:22:48 +000089 return false;
90 Function &F = *L->getHeader()->getParent();
91
92 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
93 const auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
Daniel Jasperaec2fa32016-12-19 08:22:17 +000094 auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Justin Bogner6291b582015-12-14 23:22:48 +000095 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
96 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
97 auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
98 auto *SE = SEWP ? &SEWP->getSE() : nullptr;
Daniel Berlin98a1de82017-04-28 22:05:55 +000099 const SimplifyQuery SQ = getBestSimplifyQuery(*this, F);
Alina Sbirleaad4d0182018-10-24 22:46:45 +0000100 Optional<MemorySSAUpdater> MSSAU;
101 if (EnableMSSALoopDependency) {
102 MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
103 MSSAU = MemorySSAUpdater(MSSA);
104 }
105 return LoopRotation(L, LI, TTI, AC, DT, SE,
106 MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ,
107 false, MaxHeaderSize, false);
Justin Bogner6291b582015-12-14 23:22:48 +0000108 }
109};
110}
111
Justin Bognerd0d23412016-05-03 22:02:31 +0000112char LoopRotateLegacyPass::ID = 0;
113INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops",
114 false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000115INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth31088a92016-02-19 10:45:18 +0000116INITIALIZE_PASS_DEPENDENCY(LoopPass)
117INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
Alina Sbirleaad4d0182018-10-24 22:46:45 +0000118INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
Sebastian Popdfb66a12016-06-14 14:44:05 +0000119INITIALIZE_PASS_END(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false,
120 false)
Justin Bogner6291b582015-12-14 23:22:48 +0000121
122Pass *llvm::createLoopRotatePass(int MaxHeaderSize) {
Justin Bognerd0d23412016-05-03 22:02:31 +0000123 return new LoopRotateLegacyPass(MaxHeaderSize);
Justin Bogner6291b582015-12-14 23:22:48 +0000124}