1//===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements Loop Rotation Pass.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Transforms/Scalar/LoopRotation.h"
14#include "llvm/Analysis/AssumptionCache.h"
15#include "llvm/Analysis/InstructionSimplify.h"
16#include "llvm/Analysis/LazyBlockFrequencyInfo.h"
17#include "llvm/Analysis/LoopInfo.h"
18#include "llvm/Analysis/LoopPass.h"
19#include "llvm/Analysis/MemorySSA.h"
20#include "llvm/Analysis/MemorySSAUpdater.h"
21#include "llvm/Analysis/ScalarEvolution.h"
22#include "llvm/Analysis/TargetTransformInfo.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Transforms/Scalar.h"
25#include "llvm/Transforms/Utils/LoopRotationUtils.h"
26#include "llvm/Transforms/Utils/LoopUtils.h"
27#include <optional>
28using namespace llvm;
29
30#define DEBUG_TYPE "loop-rotate"
31
32static cl::opt<unsigned> DefaultRotationThreshold(
33 "rotation-max-header-size", cl::init(Val: 16), cl::Hidden,
34 cl::desc("The default maximum header size for automatic loop rotation"));
35
36static cl::opt<bool> PrepareForLTOOption(
37 "rotation-prepare-for-lto", cl::init(Val: false), cl::Hidden,
38 cl::desc("Run loop-rotation in the prepare-for-lto stage. This option "
39 "should be used for testing only."));
40
41// Experimentally allow loop header duplication. This should allow for better
42// optimization at Oz, since loop-idiom recognition can then recognize things
43// like memcpy. If this ends up being useful for many targets, we should drop
44// this flag and make a code generation option that can be controlled
45// independent of the opt level and exposed through the frontend.
46static cl::opt<bool> EnableLoopHeaderDuplicationAtMinSize(
47 "enable-loop-header-duplication-at-minsize", cl::init(Val: false), cl::Hidden,
48 cl::desc("Enable loop header duplication even for minsize"));
49
50LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication, bool PrepareForLTO,
51 bool CheckExitCount)
52 : EnableHeaderDuplication(EnableHeaderDuplication),
53 PrepareForLTO(PrepareForLTO), CheckExitCount(CheckExitCount) {}
54
55void LoopRotatePass::printPipeline(
56 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
57 static_cast<PassInfoMixin<LoopRotatePass> *>(this)->printPipeline(
58 OS, MapClassName2PassName);
59 OS << "<";
60 if (!EnableHeaderDuplication)
61 OS << "no-";
62 OS << "header-duplication;";
63
64 if (!PrepareForLTO)
65 OS << "no-";
66 OS << "prepare-for-lto;";
67
68 if (!CheckExitCount)
69 OS << "no-";
70 OS << "check-exit-count";
71 OS << ">";
72}
73
74PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
75 LoopStandardAnalysisResults &AR,
76 LPMUpdater &) {
77 // Vectorization requires loop-rotation. Use default threshold for loops the
78 // user explicitly marked for vectorization, even when header duplication is
79 // disabled.
80 int Threshold = EnableHeaderDuplication &&
81 (!L.getHeader()->getParent()->hasMinSize() ||
82 EnableLoopHeaderDuplicationAtMinSize ||
83 hasVectorizeTransformation(L: &L) == TM_ForcedByUser)
84 ? DefaultRotationThreshold
85 : 0;
86 const DataLayout &DL = L.getHeader()->getDataLayout();
87 const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
88
89 std::optional<MemorySSAUpdater> MSSAU;
90 if (AR.MSSA)
91 MSSAU = MemorySSAUpdater(AR.MSSA);
92 bool Changed =
93 LoopRotation(L: &L, LI: &AR.LI, TTI: &AR.TTI, AC: &AR.AC, DT: &AR.DT, SE: &AR.SE,
94 MSSAU: MSSAU ? &*MSSAU : nullptr, SQ, RotationOnly: false, Threshold, IsUtilMode: false,
95 PrepareForLTO: PrepareForLTO || PrepareForLTOOption, CheckExitCount);
96
97 if (!Changed)
98 return PreservedAnalyses::all();
99
100 if (AR.MSSA && VerifyMemorySSA)
101 AR.MSSA->verifyMemorySSA();
102
103 auto PA = getLoopPassPreservedAnalyses();
104 if (AR.MSSA)
105 PA.preserve<MemorySSAAnalysis>();
106 return PA;
107}
108