diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h --- a/llvm/include/llvm/InitializePasses.h +++ b/llvm/include/llvm/InitializePasses.h @@ -245,7 +245,7 @@ void initializeLoopLoadEliminationPass(PassRegistry&); void initializeLoopPassPass(PassRegistry&); void initializeLoopPredicationLegacyPassPass(PassRegistry&); -void initializeLoopRerollPass(PassRegistry&); +void initializeLoopRerollLegacyPassPass(PassRegistry &); void initializeLoopRotateLegacyPassPass(PassRegistry&); void initializeLoopSimplifyCFGLegacyPassPass(PassRegistry&); void initializeLoopSimplifyPass(PassRegistry&); diff --git a/llvm/include/llvm/Transforms/Scalar/LoopReroll.h b/llvm/include/llvm/Transforms/Scalar/LoopReroll.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/Transforms/Scalar/LoopReroll.h @@ -0,0 +1,27 @@ +//===- LoopReroll.h - Loop rerolling pass ---------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_SCALAR_LOOPREROLL_H +#define LLVM_TRANSFORMS_SCALAR_LOOPREROLL_H + +#include "llvm/IR/PassManager.h" +#include "llvm/Transforms/Scalar/LoopPassManager.h" + +namespace llvm { + +class Function; + +class LoopRerollPass : public PassInfoMixin { +public: + PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, + LoopStandardAnalysisResults &AR, LPMUpdater &U); +}; + +} // end namespace llvm + +#endif // LLVM_TRANSFORMS_SCALAR_LOOPREROLL_H diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -152,6 +152,7 @@ #include "llvm/Transforms/Scalar/LoopLoadElimination.h" #include "llvm/Transforms/Scalar/LoopPassManager.h" #include "llvm/Transforms/Scalar/LoopPredication.h" +#include "llvm/Transforms/Scalar/LoopReroll.h" #include "llvm/Transforms/Scalar/LoopRotation.h" #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" #include "llvm/Transforms/Scalar/LoopSink.h" diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -364,6 +364,7 @@ LOOP_PASS("loop-predication", LoopPredicationPass()) LOOP_PASS("guard-widening", GuardWideningPass()) LOOP_PASS("simple-loop-unswitch", SimpleLoopUnswitchPass()) +LOOP_PASS("loop-reroll", LoopRerollPass()) #undef LOOP_PASS #ifndef LOOP_PASS_WITH_PARAMS diff --git a/llvm/lib/Transforms/Scalar/LoopRerollPass.cpp b/llvm/lib/Transforms/Scalar/LoopRerollPass.cpp --- a/llvm/lib/Transforms/Scalar/LoopRerollPass.cpp +++ b/llvm/lib/Transforms/Scalar/LoopRerollPass.cpp @@ -50,6 +50,7 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/Scalar/LoopReroll.h" #include "llvm/Transforms/Utils.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Local.h" @@ -161,12 +162,12 @@ IL_End }; - class LoopReroll : public LoopPass { + class LoopRerollLegacyPass : public LoopPass { public: static char ID; // Pass ID, replacement for typeid - LoopReroll() : LoopPass(ID) { - initializeLoopRerollPass(*PassRegistry::getPassRegistry()); + LoopRerollLegacyPass() : LoopPass(ID) { + initializeLoopRerollLegacyPassPass(*PassRegistry::getPassRegistry()); } bool runOnLoop(Loop *L, LPPassManager &LPM) override; @@ -175,6 +176,15 @@ AU.addRequired(); getLoopAnalysisUsage(AU); } + }; + + class LoopReroll { + public: + LoopReroll(AliasAnalysis *AA, LoopInfo *LI, ScalarEvolution *SE, + TargetLibraryInfo *TLI, DominatorTree *DT, bool PreserveLCSSA) + : AA(AA), LI(LI), SE(SE), TLI(TLI), DT(DT), + PreserveLCSSA(PreserveLCSSA) {} + bool runOnLoop(Loop *L); protected: AliasAnalysis *AA; @@ -484,16 +494,16 @@ } // end anonymous namespace -char LoopReroll::ID = 0; +char LoopRerollLegacyPass::ID = 0; -INITIALIZE_PASS_BEGIN(LoopReroll, "loop-reroll", "Reroll loops", false, false) +INITIALIZE_PASS_BEGIN(LoopRerollLegacyPass, "loop-reroll", "Reroll loops", + false, false) INITIALIZE_PASS_DEPENDENCY(LoopPass) INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) -INITIALIZE_PASS_END(LoopReroll, "loop-reroll", "Reroll loops", false, false) +INITIALIZE_PASS_END(LoopRerollLegacyPass, "loop-reroll", "Reroll loops", false, + false) -Pass *llvm::createLoopRerollPass() { - return new LoopReroll; -} +Pass *llvm::createLoopRerollPass() { return new LoopRerollLegacyPass; } // Returns true if the provided instruction is used outside the given loop. // This operates like Instruction::isUsedOutsideOfBlock, but considers PHIs in @@ -1644,18 +1654,7 @@ return true; } -bool LoopReroll::runOnLoop(Loop *L, LPPassManager &LPM) { - if (skipLoop(L)) - return false; - - AA = &getAnalysis().getAAResults(); - LI = &getAnalysis().getLoopInfo(); - SE = &getAnalysis().getSE(); - TLI = &getAnalysis().getTLI( - *L->getHeader()->getParent()); - DT = &getAnalysis().getDomTree(); - PreserveLCSSA = mustPreserveAnalysisID(LCSSAID); - +bool LoopReroll::runOnLoop(Loop *L) { BasicBlock *Header = L->getHeader(); LLVM_DEBUG(dbgs() << "LRR: F[" << Header->getParent()->getName() << "] Loop %" << Header->getName() << " (" << L->getNumBlocks() @@ -1704,3 +1703,26 @@ return Changed; } + +bool LoopRerollLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) { + if (skipLoop(L)) + return false; + + auto *AA = &getAnalysis().getAAResults(); + auto *LI = &getAnalysis().getLoopInfo(); + auto *SE = &getAnalysis().getSE(); + auto *TLI = &getAnalysis().getTLI( + *L->getHeader()->getParent()); + auto *DT = &getAnalysis().getDomTree(); + bool PreserveLCSSA = mustPreserveAnalysisID(LCSSAID); + + return LoopReroll(AA, LI, SE, TLI, DT, PreserveLCSSA).runOnLoop(L); +} + +PreservedAnalyses LoopRerollPass::run(Loop &L, LoopAnalysisManager &AM, + LoopStandardAnalysisResults &AR, + LPMUpdater &U) { + return LoopReroll(&AR.AA, &AR.LI, &AR.SE, &AR.TLI, &AR.DT, true).runOnLoop(&L) + ? PreservedAnalyses::none() + : PreservedAnalyses::all(); +} diff --git a/llvm/lib/Transforms/Scalar/Scalar.cpp b/llvm/lib/Transforms/Scalar/Scalar.cpp --- a/llvm/lib/Transforms/Scalar/Scalar.cpp +++ b/llvm/lib/Transforms/Scalar/Scalar.cpp @@ -71,7 +71,7 @@ initializeLoopPredicationLegacyPassPass(Registry); initializeLoopRotateLegacyPassPass(Registry); initializeLoopStrengthReducePass(Registry); - initializeLoopRerollPass(Registry); + initializeLoopRerollLegacyPassPass(Registry); initializeLoopUnrollPass(Registry); initializeLoopUnrollAndJamPass(Registry); initializeLoopUnswitchPass(Registry); diff --git a/llvm/test/Transforms/LoopReroll/basic.ll b/llvm/test/Transforms/LoopReroll/basic.ll --- a/llvm/test/Transforms/LoopReroll/basic.ll +++ b/llvm/test/Transforms/LoopReroll/basic.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -loop-reroll -S | FileCheck %s +; RUN: opt < %s -passes=loop-reroll -S | FileCheck %s target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu"