2009-06-02 13:52:33 -04:00
|
|
|
//===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
|
|
|
|
|
//
|
2019-08-20 16:50:12 -04:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-06-02 13:52:33 -04:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// This file implements Loop Rotation Pass.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2016-07-23 16:41:05 -04:00
|
|
|
#include "llvm/Transforms/Scalar/LoopRotation.h"
|
2021-02-16 15:13:02 -05:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2017-01-14 10:37:50 -05:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2021-07-29 16:15:26 -04:00
|
|
|
#include "llvm/Analysis/LazyBlockFrequencyInfo.h"
|
2022-07-03 10:10:23 -04:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2013-04-08 14:41:23 -04:00
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
2019-01-19 05:01:25 -05:00
|
|
|
#include "llvm/Analysis/MemorySSA.h"
|
|
|
|
|
#include "llvm/Analysis/MemorySSAUpdater.h"
|
2009-06-02 13:52:33 -04:00
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
2013-04-08 14:41:23 -04:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2020-01-17 15:45:01 -05:00
|
|
|
#include "llvm/InitializePasses.h"
|
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2016-07-23 16:41:05 -04:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2018-07-28 06:51:19 -04:00
|
|
|
#include "llvm/Transforms/Utils/LoopRotationUtils.h"
|
2016-07-23 16:41:05 -04:00
|
|
|
#include "llvm/Transforms/Utils/LoopUtils.h"
|
2023-02-11 07:38:04 -05:00
|
|
|
#include <optional>
|
2009-06-02 13:52:33 -04:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
2014-11-24 04:08:18 -05:00
|
|
|
#define DEBUG_TYPE "loop-rotate"
|
|
|
|
|
|
2016-07-23 16:41:05 -04:00
|
|
|
static cl::opt<unsigned> DefaultRotationThreshold(
|
|
|
|
|
"rotation-max-header-size", cl::init(16), cl::Hidden,
|
|
|
|
|
cl::desc("The default maximum header size for automatic loop rotation"));
|
2009-06-02 13:52:33 -04:00
|
|
|
|
2021-02-16 15:13:02 -05:00
|
|
|
static cl::opt<bool> PrepareForLTOOption(
|
|
|
|
|
"rotation-prepare-for-lto", cl::init(false), cl::Hidden,
|
|
|
|
|
cl::desc("Run loop-rotation in the prepare-for-lto stage. This option "
|
|
|
|
|
"should be used for testing only."));
|
|
|
|
|
|
|
|
|
|
LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication, bool PrepareForLTO)
|
|
|
|
|
: EnableHeaderDuplication(EnableHeaderDuplication),
|
|
|
|
|
PrepareForLTO(PrepareForLTO) {}
|
2016-07-23 16:41:05 -04:00
|
|
|
|
2023-09-02 17:17:18 -04:00
|
|
|
void LoopRotatePass::printPipeline(
|
|
|
|
|
raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
|
|
|
|
|
static_cast<PassInfoMixin<LoopRotatePass> *>(this)->printPipeline(
|
|
|
|
|
OS, MapClassName2PassName);
|
|
|
|
|
OS << "<";
|
|
|
|
|
if (!EnableHeaderDuplication)
|
|
|
|
|
OS << "no-";
|
|
|
|
|
OS << "header-duplication;";
|
|
|
|
|
|
|
|
|
|
if (!PrepareForLTO)
|
|
|
|
|
OS << "no-";
|
|
|
|
|
OS << "prepare-for-lto";
|
|
|
|
|
OS << ">";
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-14 10:37:50 -05:00
|
|
|
PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
|
|
|
|
|
LoopStandardAnalysisResults &AR,
|
|
|
|
|
LPMUpdater &) {
|
2021-02-16 15:13:02 -05:00
|
|
|
// Vectorization requires loop-rotation. Use default threshold for loops the
|
|
|
|
|
// user explicitly marked for vectorization, even when header duplication is
|
|
|
|
|
// disabled.
|
2024-07-27 19:34:35 -04:00
|
|
|
int Threshold =
|
|
|
|
|
(EnableHeaderDuplication && !L.getHeader()->getParent()->hasMinSize()) ||
|
|
|
|
|
hasVectorizeTransformation(&L) == TM_ForcedByUser
|
|
|
|
|
? DefaultRotationThreshold
|
|
|
|
|
: 0;
|
|
|
|
|
const DataLayout &DL = L.getHeader()->getDataLayout();
|
2017-05-02 14:30:13 -04:00
|
|
|
const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
|
2016-07-23 16:41:05 -04:00
|
|
|
|
2023-02-11 07:38:04 -05:00
|
|
|
std::optional<MemorySSAUpdater> MSSAU;
|
2019-01-19 05:01:25 -05:00
|
|
|
if (AR.MSSA)
|
|
|
|
|
MSSAU = MemorySSAUpdater(AR.MSSA);
|
2023-02-11 07:38:04 -05:00
|
|
|
bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,
|
|
|
|
|
MSSAU ? &*MSSAU : nullptr, SQ, false, Threshold,
|
|
|
|
|
false, PrepareForLTO || PrepareForLTOOption);
|
2018-07-28 06:51:19 -04:00
|
|
|
|
2016-07-23 16:41:05 -04:00
|
|
|
if (!Changed)
|
|
|
|
|
return PreservedAnalyses::all();
|
2017-04-16 12:01:22 -04:00
|
|
|
|
2019-01-19 05:01:25 -05:00
|
|
|
if (AR.MSSA && VerifyMemorySSA)
|
|
|
|
|
AR.MSSA->verifyMemorySSA();
|
|
|
|
|
|
2019-08-20 16:50:12 -04:00
|
|
|
auto PA = getLoopPassPreservedAnalyses();
|
2019-10-23 13:51:42 -04:00
|
|
|
if (AR.MSSA)
|
2019-08-20 16:50:12 -04:00
|
|
|
PA.preserve<MemorySSAAnalysis>();
|
|
|
|
|
return PA;
|
2016-07-23 16:41:05 -04:00
|
|
|
}
|