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 #define DEBUG_TYPE "polly-prune-unprofitable"
29 STATISTIC(ScopsProcessed
,
30 "Number of SCoPs considered for unprofitability pruning");
31 STATISTIC(ScopsPruned
, "Number of pruned SCoPs because it they cannot be "
32 "optimized in a significant way");
33 STATISTIC(ScopsSurvived
, "Number of SCoPs after pruning");
35 STATISTIC(NumPrunedLoops
, "Number of pruned loops");
36 STATISTIC(NumPrunedBoxedLoops
, "Number of pruned boxed loops");
37 STATISTIC(NumPrunedAffineLoops
, "Number of pruned affine loops");
39 STATISTIC(NumLoopsInScop
, "Number of loops in scops after pruning");
40 STATISTIC(NumBoxedLoops
, "Number of boxed loops in SCoPs after pruning");
41 STATISTIC(NumAffineLoops
, "Number of affine loops in SCoPs after pruning");
43 static void updateStatistics(Scop
&S
, bool Pruned
) {
44 Scop::ScopStatistics ScopStats
= S
.getStatistics();
47 NumPrunedLoops
+= ScopStats
.NumAffineLoops
+ ScopStats
.NumBoxedLoops
;
48 NumPrunedBoxedLoops
+= ScopStats
.NumBoxedLoops
;
49 NumPrunedAffineLoops
+= ScopStats
.NumAffineLoops
;
52 NumLoopsInScop
+= ScopStats
.NumAffineLoops
+ ScopStats
.NumBoxedLoops
;
53 NumBoxedLoops
+= ScopStats
.NumBoxedLoops
;
54 NumAffineLoops
+= ScopStats
.NumAffineLoops
;
58 static bool runPruneUnprofitable(Scop
&S
) {
59 if (PollyProcessUnprofitable
) {
61 dbgs() << "NOTE: -polly-process-unprofitable active, won't prune "
68 if (!S
.isProfitable(true)) {
70 dbgs() << "SCoP pruned because it probably cannot be optimized in "
71 "a significant way\n");
72 S
.invalidate(PROFITABLE
, DebugLoc());
73 updateStatistics(S
, true);
75 updateStatistics(S
, false);
81 class PruneUnprofitableWrapperPass final
: public ScopPass
{
85 explicit PruneUnprofitableWrapperPass() : ScopPass(ID
) {}
86 PruneUnprofitableWrapperPass(const PruneUnprofitableWrapperPass
&) = delete;
87 PruneUnprofitableWrapperPass
&
88 operator=(const PruneUnprofitableWrapperPass
&) = delete;
90 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
91 AU
.addRequired
<ScopInfoRegionPass
>();
95 bool runOnScop(Scop
&S
) override
{ return runPruneUnprofitable(S
); }
99 char PruneUnprofitableWrapperPass::ID
;
101 Pass
*polly::createPruneUnprofitableWrapperPass() {
102 return new PruneUnprofitableWrapperPass();
105 INITIALIZE_PASS_BEGIN(PruneUnprofitableWrapperPass
, "polly-prune-unprofitable",
106 "Polly - Prune unprofitable SCoPs", false, false)
107 INITIALIZE_PASS_END(PruneUnprofitableWrapperPass
, "polly-prune-unprofitable",
108 "Polly - Prune unprofitable SCoPs", false, false)
110 llvm::PreservedAnalyses
111 PruneUnprofitablePass::run(Scop
&S
, ScopAnalysisManager
&SAM
,
112 ScopStandardAnalysisResults
&SAR
, SPMUpdater
&U
) {
113 bool Changed
= runPruneUnprofitable(S
);
116 return PreservedAnalyses::all();
118 PreservedAnalyses PA
;
119 PA
.preserveSet
<AllAnalysesOn
<Module
>>();
120 PA
.preserveSet
<AllAnalysesOn
<Function
>>();
121 PA
.preserveSet
<AllAnalysesOn
<Loop
>>();