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 "ScalarOptions.h"
15#include "llvm/Analysis/AssumptionCache.h"
16#include "llvm/Analysis/InstructionSimplify.h"
17#include "llvm/Analysis/LazyBlockFrequencyInfo.h"
18#include "llvm/Analysis/LoopInfo.h"
19#include "llvm/Analysis/LoopPass.h"
20#include "llvm/Analysis/MemorySSA.h"
21#include "llvm/Analysis/MemorySSAUpdater.h"
22#include "llvm/Analysis/ScalarEvolution.h"
23#include "llvm/Analysis/TargetTransformInfo.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
32LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication, bool PrepareForLTO,
33 bool CheckExitCount)
34 : EnableHeaderDuplication(EnableHeaderDuplication),
35 PrepareForLTO(PrepareForLTO), CheckExitCount(CheckExitCount) {}
36
37void LoopRotatePass::printPipeline(
38 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
39 static_cast<PassInfoMixin<LoopRotatePass> *>(this)->printPipeline(
40 OS, MapClassName2PassName);
41 OS << "<";
42 if (!EnableHeaderDuplication)
43 OS << "no-";
44 OS << "header-duplication;";
45
46 if (!PrepareForLTO)
47 OS << "no-";
48 OS << "prepare-for-lto;";
49
50 if (!CheckExitCount)
51 OS << "no-";
52 OS << "check-exit-count";
53 OS << ">";
54}
55
56PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
57 LoopStandardAnalysisResults &AR,
58 LPMUpdater &) {
59 const ScalarOptions &Opts = ScalarOptions::Global;
60 // Vectorization requires loop-rotation. Use default threshold for loops the
61 // user explicitly marked for vectorization, even when header duplication is
62 // disabled.
63 int Threshold = EnableHeaderDuplication &&
64 (!L.getHeader()->getParent()->hasMinSize() ||
65 Opts.enable_loop_header_duplication_at_minsize ||
66 hasVectorizeTransformation(L: &L) == TM_ForcedByUser)
67 ? Opts.rotation_max_header_size
68 : 0;
69 const DataLayout &DL = L.getHeader()->getDataLayout();
70 const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
71
72 std::optional<MemorySSAUpdater> MSSAU;
73 if (AR.MSSA)
74 MSSAU = MemorySSAUpdater(AR.MSSA);
75 bool Changed = LoopRotation(
76 L: &L, LI: &AR.LI, TTI: &AR.TTI, AC: &AR.AC, DT: &AR.DT, SE: &AR.SE, MSSAU: MSSAU ? &*MSSAU : nullptr,
77 SQ, RotationOnly: false, Threshold, IsUtilMode: false,
78 PrepareForLTO: PrepareForLTO || Opts.rotation_prepare_for_lto, CheckExitCount);
79
80 if (!Changed)
81 return PreservedAnalyses::all();
82
83 if (AR.MSSA && VerifyMemorySSA)
84 AR.MSSA->verifyMemorySSA();
85
86 auto PA = getLoopPassPreservedAnalyses();
87 if (AR.MSSA)
88 PA.preserve<MemorySSAAnalysis>();
89 return PA;
90}
91