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 =
60 L.getHeader()->getContext().getLibraryOptions().get<ScalarOptions>();
61 // Vectorization requires loop-rotation. Use default threshold for loops the
62 // user explicitly marked for vectorization, even when header duplication is
63 // disabled.
64 int Threshold = EnableHeaderDuplication &&
65 (!L.getHeader()->getParent()->hasMinSize() ||
66 Opts.enable_loop_header_duplication_at_minsize ||
67 hasVectorizeTransformation(L: &L) == TM_ForcedByUser)
68 ? Opts.rotation_max_header_size
69 : 0;
70 const DataLayout &DL = L.getHeader()->getDataLayout();
71 const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
72
73 std::optional<MemorySSAUpdater> MSSAU;
74 if (AR.MSSA)
75 MSSAU = MemorySSAUpdater(AR.MSSA);
76 bool Changed = LoopRotation(
77 L: &L, LI: &AR.LI, TTI: &AR.TTI, AC: &AR.AC, DT: &AR.DT, SE: &AR.SE, MSSAU: MSSAU ? &*MSSAU : nullptr,
78 SQ, RotationOnly: false, Threshold, IsUtilMode: false,
79 PrepareForLTO: PrepareForLTO || Opts.rotation_prepare_for_lto, CheckExitCount);
80
81 if (!Changed)
82 return PreservedAnalyses::all();
83
84 if (AR.MSSA && VerifyMemorySSA)
85 AR.MSSA->verifyMemorySSA();
86
87 auto PA = getLoopPassPreservedAnalyses();
88 if (AR.MSSA)
89 PA.preserve<MemorySSAAnalysis>();
90 return PA;
91}
92