1 //===- PruneUnprofitable.cpp ----------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Mark a SCoP as unfeasible if not deemed profitable to optimize.
10 //
11 //===----------------------------------------------------------------------===//
12
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"
21
22 using namespace llvm;
23 using namespace polly;
24
25 #include "polly/Support/PollyDebug.h"
26 #define DEBUG_TYPE "polly-prune-unprofitable"
27
28 namespace {
29
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");
35
36 STATISTIC(NumPrunedLoops, "Number of pruned loops");
37 STATISTIC(NumPrunedBoxedLoops, "Number of pruned boxed loops");
38 STATISTIC(NumPrunedAffineLoops, "Number of pruned affine loops");
39
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");
43
updateStatistics(Scop & S,bool Pruned)44 static void updateStatistics(Scop &S, bool Pruned) {
45 Scop::ScopStatistics ScopStats = S.getStatistics();
46 if (Pruned) {
47 ScopsPruned++;
48 NumPrunedLoops += ScopStats.NumAffineLoops + ScopStats.NumBoxedLoops;
49 NumPrunedBoxedLoops += ScopStats.NumBoxedLoops;
50 NumPrunedAffineLoops += ScopStats.NumAffineLoops;
51 } else {
52 ScopsSurvived++;
53 NumLoopsInScop += ScopStats.NumAffineLoops + ScopStats.NumBoxedLoops;
54 NumBoxedLoops += ScopStats.NumBoxedLoops;
55 NumAffineLoops += ScopStats.NumAffineLoops;
56 }
57 }
58
runPruneUnprofitable(Scop & S)59 static bool runPruneUnprofitable(Scop &S) {
60 if (PollyProcessUnprofitable) {
61 POLLY_DEBUG(
62 dbgs() << "NOTE: -polly-process-unprofitable active, won't prune "
63 "anything\n");
64 return false;
65 }
66
67 ScopsProcessed++;
68
69 if (!S.isProfitable(true)) {
70 POLLY_DEBUG(
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);
75 } else {
76 updateStatistics(S, false);
77 }
78
79 return false;
80 }
81
82 class PruneUnprofitableWrapperPass final : public ScopPass {
83 public:
84 static char ID;
85
PruneUnprofitableWrapperPass()86 explicit PruneUnprofitableWrapperPass() : ScopPass(ID) {}
87 PruneUnprofitableWrapperPass(const PruneUnprofitableWrapperPass &) = delete;
88 PruneUnprofitableWrapperPass &
89 operator=(const PruneUnprofitableWrapperPass &) = delete;
90
getAnalysisUsage(AnalysisUsage & AU) const91 void getAnalysisUsage(AnalysisUsage &AU) const override {
92 AU.addRequired<ScopInfoRegionPass>();
93 AU.setPreservesAll();
94 }
95
runOnScop(Scop & S)96 bool runOnScop(Scop &S) override { return runPruneUnprofitable(S); }
97 };
98 } // namespace
99
100 char PruneUnprofitableWrapperPass::ID;
101
createPruneUnprofitableWrapperPass()102 Pass *polly::createPruneUnprofitableWrapperPass() {
103 return new PruneUnprofitableWrapperPass();
104 }
105
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)
110
111 llvm::PreservedAnalyses
run(Scop & S,ScopAnalysisManager & SAM,ScopStandardAnalysisResults & SAR,SPMUpdater & U)112 PruneUnprofitablePass::run(Scop &S, ScopAnalysisManager &SAM,
113 ScopStandardAnalysisResults &SAR, SPMUpdater &U) {
114 bool Changed = runPruneUnprofitable(S);
115
116 if (!Changed)
117 return PreservedAnalyses::all();
118
119 PreservedAnalyses PA;
120 PA.preserveSet<AllAnalysesOn<Module>>();
121 PA.preserveSet<AllAnalysesOn<Function>>();
122 PA.preserveSet<AllAnalysesOn<Loop>>();
123 return PA;
124 }
125