aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib
diff options
context:
space:
mode:
authorRuobing Han <hanruobing@gatech.edu>2022-08-01 17:22:45 +0000
committerRuobing Han <hanruobing@gatech.edu>2022-08-08 18:12:04 +0000
commitf756f06cc471b91d81d7f7f15d9df76ed24de730 (patch)
tree522dc01ac406696b3a5afed41b16dbb7b8437ca2 /llvm/lib
parent6c52f82d77a1b8d9d9f8b585c73f94e58191b5a9 (diff)
downloadllvm-f756f06cc471b91d81d7f7f15d9df76ed24de730.zip
llvm-f756f06cc471b91d81d7f7f15d9df76ed24de730.tar.gz
llvm-f756f06cc471b91d81d7f7f15d9df76ed24de730.tar.bz2
[SimpleLoopUnswitch] Skip non-trivial unswitching of cold loops
With profile data, non-trivial LoopUnswitch will only apply on non-cold loops, as unswitching cold loops may not gain much benefit but significantly increase the code size. Reviewed By: aeubanks, asbirlea Differential Revision: https://reviews.llvm.org/D129599
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Passes/PassBuilder.cpp6
-rw-r--r--llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp26
2 files changed, 24 insertions, 8 deletions
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index af18479..e77e582 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -1399,8 +1399,10 @@ Error PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
return Err;
// Add the nested pass manager with the appropriate adaptor.
bool UseMemorySSA = (Name == "loop-mssa");
- bool UseBFI = llvm::any_of(
- InnerPipeline, [](auto Pipeline) { return Pipeline.Name == "licm"; });
+ bool UseBFI = llvm::any_of(InnerPipeline, [](auto Pipeline) {
+ return Pipeline.Name.contains("licm") ||
+ Pipeline.Name.contains("simple-loop-unswitch");
+ });
bool UseBPI = llvm::any_of(InnerPipeline, [](auto Pipeline) {
return Pipeline.Name == "loop-predication";
});
diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
index 0535608..01c0bf7 100644
--- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
+++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
@@ -16,6 +16,7 @@
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Analysis/AssumptionCache.h"
+#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/CFG.h"
#include "llvm/Analysis/CodeMetrics.h"
#include "llvm/Analysis/GuardUtils.h"
@@ -26,6 +27,7 @@
#include "llvm/Analysis/MemorySSA.h"
#include "llvm/Analysis/MemorySSAUpdater.h"
#include "llvm/Analysis/MustExecute.h"
+#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/ValueTracking.h"
@@ -3044,6 +3046,7 @@ unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
bool NonTrivial,
function_ref<void(bool, bool, ArrayRef<Loop *>)> UnswitchCB,
ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
+ ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI,
function_ref<void(Loop &, StringRef)> DestroyLoopCB) {
assert(L.isRecursivelyLCSSAForm(DT, LI) &&
"Loops must be in LCSSA form before unswitching.");
@@ -3080,6 +3083,14 @@ unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
if (L.getHeader()->getParent()->hasOptSize())
return false;
+ // Skip cold loops, as unswitching them brings little benefit
+ // but increases the code size
+ if (PSI && PSI->hasProfileSummary() && BFI &&
+ PSI->isColdBlock(L.getHeader(), BFI)) {
+ LLVM_DEBUG(dbgs() << " Skip cold loop: " << L << "\n");
+ return false;
+ }
+
// Skip non-trivial unswitching for loops that cannot be cloned.
if (!L.isSafeToClone())
return false;
@@ -3105,7 +3116,11 @@ PreservedAnalyses SimpleLoopUnswitchPass::run(Loop &L, LoopAnalysisManager &AM,
LPMUpdater &U) {
Function &F = *L.getHeader()->getParent();
(void)F;
-
+ ProfileSummaryInfo *PSI = nullptr;
+ if (auto OuterProxy =
+ AM.getResult<FunctionAnalysisManagerLoopProxy>(L, AR)
+ .getCachedResult<ModuleAnalysisManagerFunctionProxy>(F))
+ PSI = OuterProxy->getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
LLVM_DEBUG(dbgs() << "Unswitching loop in " << F.getName() << ": " << L
<< "\n");
@@ -3152,7 +3167,7 @@ PreservedAnalyses SimpleLoopUnswitchPass::run(Loop &L, LoopAnalysisManager &AM,
}
if (!unswitchLoop(L, AR.DT, AR.LI, AR.AC, AR.AA, AR.TTI, Trivial, NonTrivial,
UnswitchCB, &AR.SE, MSSAU ? MSSAU.getPointer() : nullptr,
- DestroyLoopCB))
+ PSI, AR.BFI, DestroyLoopCB))
return PreservedAnalyses::all();
if (AR.MSSA && VerifyMemorySSA)
@@ -3214,7 +3229,6 @@ bool SimpleLoopUnswitchLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) {
LLVM_DEBUG(dbgs() << "Unswitching loop in " << F.getName() << ": " << *L
<< "\n");
-
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
@@ -3251,9 +3265,9 @@ bool SimpleLoopUnswitchLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) {
if (VerifyMemorySSA)
MSSA->verifyMemorySSA();
-
- bool Changed = unswitchLoop(*L, DT, LI, AC, AA, TTI, true, NonTrivial,
- UnswitchCB, SE, &MSSAU, DestroyLoopCB);
+ bool Changed =
+ unswitchLoop(*L, DT, LI, AC, AA, TTI, true, NonTrivial, UnswitchCB, SE,
+ &MSSAU, nullptr, nullptr, DestroyLoopCB);
if (VerifyMemorySSA)
MSSA->verifyMemorySSA();