1 //===- PruneUnprofitable.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Mark a SCoP as unfeasible if not deemed profitable to optimize.
11 //===----------------------------------------------------------------------===//
13 #include "polly/PruneUnprofitable.h"
14 #include "polly/ScopDetection.h"
15 #include "polly/ScopInfo.h"
16 #include "polly/ScopPass.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/IR/DebugLoc.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
23 using namespace polly
;
25 #include "polly/Support/PollyDebug.h"
26 #define DEBUG_TYPE "polly-prune-unprofitable"
30 STATISTIC(ScopsProcessed
,
31 "Number of SCoPs considered for unprofitability pruning");
32 STATISTIC(ScopsPruned
, "Number of pruned SCoPs because it they cannot be "
33 "optimized in a significant way");
34 STATISTIC(ScopsSurvived
, "Number of SCoPs after pruning");
36 STATISTIC(NumPrunedLoops
, "Number of pruned loops");
37 STATISTIC(NumPrunedBoxedLoops
, "Number of pruned boxed loops");
38 STATISTIC(NumPrunedAffineLoops
, "Number of pruned affine loops");
40 STATISTIC(NumLoopsInScop
, "Number of loops in scops after pruning");
41 STATISTIC(NumBoxedLoops
, "Number of boxed loops in SCoPs after pruning");
42 STATISTIC(NumAffineLoops
, "Number of affine loops in SCoPs after pruning");
44 static void updateStatistics(Scop
&S
, bool Pruned
) {
45 Scop::ScopStatistics ScopStats
= S
.getStatistics();
48 NumPrunedLoops
+= ScopStats
.NumAffineLoops
+ ScopStats
.NumBoxedLoops
;
49 NumPrunedBoxedLoops
+= ScopStats
.NumBoxedLoops
;
50 NumPrunedAffineLoops
+= ScopStats
.NumAffineLoops
;
53 NumLoopsInScop
+= ScopStats
.NumAffineLoops
+ ScopStats
.NumBoxedLoops
;
54 NumBoxedLoops
+= ScopStats
.NumBoxedLoops
;
55 NumAffineLoops
+= ScopStats
.NumAffineLoops
;
59 static bool runPruneUnprofitable(Scop
&S
) {
60 if (PollyProcessUnprofitable
) {
62 dbgs() << "NOTE: -polly-process-unprofitable active, won't prune "
69 if (!S
.isProfitable(true)) {
71 dbgs() << "SCoP pruned because it probably cannot be optimized in "
72 "a significant way\n");
73 S
.invalidate(PROFITABLE
, DebugLoc());
74 updateStatistics(S
, true);
76 updateStatistics(S
, false);
82 class PruneUnprofitableWrapperPass final
: public ScopPass
{
86 explicit PruneUnprofitableWrapperPass() : ScopPass(ID
) {}
87 PruneUnprofitableWrapperPass(const PruneUnprofitableWrapperPass
&) = delete;
88 PruneUnprofitableWrapperPass
&
89 operator=(const PruneUnprofitableWrapperPass
&) = delete;
91 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
92 AU
.addRequired
<ScopInfoRegionPass
>();
96 bool runOnScop(Scop
&S
) override
{ return runPruneUnprofitable(S
); }
100 char PruneUnprofitableWrapperPass::ID
;
102 Pass
*polly::createPruneUnprofitableWrapperPass() {
103 return new PruneUnprofitableWrapperPass();
106 INITIALIZE_PASS_BEGIN(PruneUnprofitableWrapperPass
, "polly-prune-unprofitable",
107 "Polly - Prune unprofitable SCoPs", false, false)
108 INITIALIZE_PASS_END(PruneUnprofitableWrapperPass
, "polly-prune-unprofitable",
109 "Polly - Prune unprofitable SCoPs", false, false)
111 llvm::PreservedAnalyses
112 PruneUnprofitablePass::run(Scop
&S
, ScopAnalysisManager
&SAM
,
113 ScopStandardAnalysisResults
&SAR
, SPMUpdater
&U
) {
114 bool Changed
= runPruneUnprofitable(S
);
117 return PreservedAnalyses::all();
119 PreservedAnalyses PA
;
120 PA
.preserveSet
<AllAnalysesOn
<Module
>>();
121 PA
.preserveSet
<AllAnalysesOn
<Function
>>();
122 PA
.preserveSet
<AllAnalysesOn
<Loop
>>();