From 529ecd19df1ff7bcbf81a1151c3f7006dc268611 Mon Sep 17 00:00:00 2001 From: Ta-Wei Tu Date: Tue, 20 Oct 2020 10:41:38 -0700 Subject: [PATCH] [NPM] port -unify-loop-exits to NPM Reviewed By: aeubanks Differential Revision: https://reviews.llvm.org/D89774 --- llvm/include/llvm/InitializePasses.h | 2 +- .../include/llvm/Transforms/Utils/UnifyLoopExits.h | 22 ++++++++++ llvm/lib/Passes/PassBuilder.cpp | 1 + llvm/lib/Passes/PassRegistry.def | 1 + llvm/lib/Transforms/Utils/UnifyLoopExits.cpp | 49 ++++++++++++++++------ llvm/lib/Transforms/Utils/Utils.cpp | 2 +- llvm/test/Transforms/UnifyLoopExits/basic.ll | 1 + llvm/test/Transforms/UnifyLoopExits/nested.ll | 1 + llvm/test/Transforms/UnifyLoopExits/restore-ssa.ll | 1 + llvm/test/Transforms/UnifyLoopExits/switch.ll | 1 + 10 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 llvm/include/llvm/Transforms/Utils/UnifyLoopExits.h diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h index 624fee4..920c691 100644 --- a/llvm/include/llvm/InitializePasses.h +++ b/llvm/include/llvm/InitializePasses.h @@ -429,7 +429,7 @@ void initializeTwoAddressInstructionPassPass(PassRegistry&); void initializeTypeBasedAAWrapperPassPass(PassRegistry&); void initializeTypePromotionPass(PassRegistry&); void initializeUnifyFunctionExitNodesLegacyPassPass(PassRegistry &); -void initializeUnifyLoopExitsPass(PassRegistry &); +void initializeUnifyLoopExitsLegacyPassPass(PassRegistry &); void initializeUnpackMachineBundlesPass(PassRegistry&); void initializeUnreachableBlockElimLegacyPassPass(PassRegistry&); void initializeUnreachableMachineBlockElimPass(PassRegistry&); diff --git a/llvm/include/llvm/Transforms/Utils/UnifyLoopExits.h b/llvm/include/llvm/Transforms/Utils/UnifyLoopExits.h new file mode 100644 index 0000000..0b219cd --- /dev/null +++ b/llvm/include/llvm/Transforms/Utils/UnifyLoopExits.h @@ -0,0 +1,22 @@ +//===- UnifyLoopExits.h - Redirect exiting edges to one block -*- C++ -*-===// +// +// 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_UTILS_UNIFYLOOPEXITS_H +#define LLVM_TRANSFORMS_UTILS_UNIFYLOOPEXITS_H + +#include "llvm/IR/PassManager.h" + +namespace llvm { + +class UnifyLoopExitsPass : public PassInfoMixin { +public: + PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); +}; +} // namespace llvm + +#endif // LLVM_TRANSFORMS_UTILS_UNIFYLOOPEXITS_H diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index 0a3687a..6db7f3c 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -213,6 +213,7 @@ #include "llvm/Transforms/Utils/StripNonLineTableDebugInfo.h" #include "llvm/Transforms/Utils/SymbolRewriter.h" #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" +#include "llvm/Transforms/Utils/UnifyLoopExits.h" #include "llvm/Transforms/Vectorize/LoadStoreVectorizer.h" #include "llvm/Transforms/Vectorize/LoopVectorize.h" #include "llvm/Transforms/Vectorize/SLPVectorizer.h" diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def index 4b14b15..5a0b377 100644 --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -286,6 +286,7 @@ FUNCTION_PASS("spec-phis", SpeculateAroundPHIsPass()) FUNCTION_PASS("sroa", SROA()) FUNCTION_PASS("strip-gc-relocates", StripGCRelocates()) FUNCTION_PASS("tailcallelim", TailCallElimPass()) +FUNCTION_PASS("unify-loop-exits", UnifyLoopExitsPass()) FUNCTION_PASS("vector-combine", VectorCombinePass()) FUNCTION_PASS("verify", VerifierPass()) FUNCTION_PASS("verify", DominatorTreeVerifierPass()) diff --git a/llvm/lib/Transforms/Utils/UnifyLoopExits.cpp b/llvm/lib/Transforms/Utils/UnifyLoopExits.cpp index 7017ee7..c4868a6 100644 --- a/llvm/lib/Transforms/Utils/UnifyLoopExits.cpp +++ b/llvm/lib/Transforms/Utils/UnifyLoopExits.cpp @@ -16,10 +16,12 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Transforms/Utils/UnifyLoopExits.h" #include "llvm/ADT/MapVector.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/IR/Dominators.h" #include "llvm/InitializePasses.h" +#include "llvm/Transforms/Scalar/LoopPassManager.h" #include "llvm/Transforms/Utils.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" @@ -28,10 +30,10 @@ using namespace llvm; namespace { -struct UnifyLoopExits : public FunctionPass { +struct UnifyLoopExitsLegacyPass : public FunctionPass { static char ID; - UnifyLoopExits() : FunctionPass(ID) { - initializeUnifyLoopExitsPass(*PassRegistry::getPassRegistry()); + UnifyLoopExitsLegacyPass() : FunctionPass(ID) { + initializeUnifyLoopExitsLegacyPassPass(*PassRegistry::getPassRegistry()); } void getAnalysisUsage(AnalysisUsage &AU) const override { @@ -47,17 +49,19 @@ struct UnifyLoopExits : public FunctionPass { }; } // namespace -char UnifyLoopExits::ID = 0; +char UnifyLoopExitsLegacyPass::ID = 0; -FunctionPass *llvm::createUnifyLoopExitsPass() { return new UnifyLoopExits(); } +FunctionPass *llvm::createUnifyLoopExitsPass() { + return new UnifyLoopExitsLegacyPass(); +} -INITIALIZE_PASS_BEGIN(UnifyLoopExits, "unify-loop-exits", +INITIALIZE_PASS_BEGIN(UnifyLoopExitsLegacyPass, "unify-loop-exits", "Fixup each natural loop to have a single exit block", false /* Only looks at CFG */, false /* Analysis Pass */) INITIALIZE_PASS_DEPENDENCY(LowerSwitchLegacyPass) INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) -INITIALIZE_PASS_END(UnifyLoopExits, "unify-loop-exits", +INITIALIZE_PASS_END(UnifyLoopExitsLegacyPass, "unify-loop-exits", "Fixup each natural loop to have a single exit block", false /* Only looks at CFG */, false /* Analysis Pass */) @@ -204,11 +208,7 @@ static bool unifyLoopExits(DominatorTree &DT, LoopInfo &LI, Loop *L) { return true; } -bool UnifyLoopExits::runOnFunction(Function &F) { - LLVM_DEBUG(dbgs() << "===== Unifying loop exits in function " << F.getName() - << "\n"); - auto &LI = getAnalysis().getLoopInfo(); - auto &DT = getAnalysis().getDomTree(); +static bool runImpl(LoopInfo &LI, DominatorTree &DT) { bool Changed = false; auto Loops = LI.getLoopsInPreorder(); @@ -219,3 +219,28 @@ bool UnifyLoopExits::runOnFunction(Function &F) { } return Changed; } + +bool UnifyLoopExitsLegacyPass::runOnFunction(Function &F) { + LLVM_DEBUG(dbgs() << "===== Unifying loop exits in function " << F.getName() + << "\n"); + auto &LI = getAnalysis().getLoopInfo(); + auto &DT = getAnalysis().getDomTree(); + + return runImpl(LI, DT); +} + +namespace llvm { + +PreservedAnalyses UnifyLoopExitsPass::run(Function &F, + FunctionAnalysisManager &AM) { + auto &LI = AM.getResult(F); + auto &DT = AM.getResult(F); + + if (!runImpl(LI, DT)) + return PreservedAnalyses::all(); + PreservedAnalyses PA; + PA.preserve(); + PA.preserve(); + return PA; +} +} // namespace llvm diff --git a/llvm/lib/Transforms/Utils/Utils.cpp b/llvm/lib/Transforms/Utils/Utils.cpp index aadf41c..73c0532 100644 --- a/llvm/lib/Transforms/Utils/Utils.cpp +++ b/llvm/lib/Transforms/Utils/Utils.cpp @@ -44,7 +44,7 @@ void llvm::initializeTransformUtils(PassRegistry &Registry) { initializePredicateInfoPrinterLegacyPassPass(Registry); initializeInjectTLIMappingsLegacyPass(Registry); initializeFixIrreduciblePass(Registry); - initializeUnifyLoopExitsPass(Registry); + initializeUnifyLoopExitsLegacyPassPass(Registry); initializeUniqueInternalLinkageNamesLegacyPassPass(Registry); } diff --git a/llvm/test/Transforms/UnifyLoopExits/basic.ll b/llvm/test/Transforms/UnifyLoopExits/basic.ll index 596abfa..c69996e 100644 --- a/llvm/test/Transforms/UnifyLoopExits/basic.ll +++ b/llvm/test/Transforms/UnifyLoopExits/basic.ll @@ -1,5 +1,6 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -unify-loop-exits -enable-new-pm=0 -S | FileCheck %s +; RUN: opt < %s -passes='lowerswitch,unify-loop-exits' -S | FileCheck %s define void @loop_1(i1 %PredEntry, i1 %PredB, i1 %PredC, i1 %PredD) { ; CHECK-LABEL: @loop_1( diff --git a/llvm/test/Transforms/UnifyLoopExits/nested.ll b/llvm/test/Transforms/UnifyLoopExits/nested.ll index beb258c..6609376 100644 --- a/llvm/test/Transforms/UnifyLoopExits/nested.ll +++ b/llvm/test/Transforms/UnifyLoopExits/nested.ll @@ -1,5 +1,6 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -unify-loop-exits -enable-new-pm=0 -S | FileCheck %s +; RUN: opt < %s -passes='lowerswitch,unify-loop-exits' -S | FileCheck %s define void @nested(i1 %PredB3, i1 %PredB4, i1 %PredA4, i1 %PredA3, i32 %X, i32 %Y, i32 %Z) { ; CHECK-LABEL: @nested( diff --git a/llvm/test/Transforms/UnifyLoopExits/restore-ssa.ll b/llvm/test/Transforms/UnifyLoopExits/restore-ssa.ll index cc975d6..d06437e 100644 --- a/llvm/test/Transforms/UnifyLoopExits/restore-ssa.ll +++ b/llvm/test/Transforms/UnifyLoopExits/restore-ssa.ll @@ -1,5 +1,6 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -unify-loop-exits -enable-new-pm=0 -S | FileCheck %s +; RUN: opt < %s -passes='lowerswitch,unify-loop-exits' -S | FileCheck %s ; Loop consists of A and B: ; - A is the header diff --git a/llvm/test/Transforms/UnifyLoopExits/switch.ll b/llvm/test/Transforms/UnifyLoopExits/switch.ll index 8d21165..1898d76 100644 --- a/llvm/test/Transforms/UnifyLoopExits/switch.ll +++ b/llvm/test/Transforms/UnifyLoopExits/switch.ll @@ -1,5 +1,6 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -unify-loop-exits -enable-new-pm=0 -S | FileCheck %s +; RUN: opt < %s -passes='lowerswitch,unify-loop-exits' -S | FileCheck %s define void @loop_1(i32 %Value, i1 %PredEntry, i1 %PredD) { ; CHECK-LABEL: @loop_1( -- 2.7.4