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
22using namespace llvm;
23using namespace polly;
24
25#include "polly/Support/PollyDebug.h"
26#define DEBUG_TYPE "polly-prune-unprofitable"
27
28namespace {
29
30STATISTIC(ScopsProcessed,
31 "Number of SCoPs considered for unprofitability pruning");
32STATISTIC(ScopsPruned, "Number of pruned SCoPs because it they cannot be "
33 "optimized in a significant way");
34STATISTIC(ScopsSurvived, "Number of SCoPs after pruning");
35
36STATISTIC(NumPrunedLoops, "Number of pruned loops");
37STATISTIC(NumPrunedBoxedLoops, "Number of pruned boxed loops");
38STATISTIC(NumPrunedAffineLoops, "Number of pruned affine loops");
39
40STATISTIC(NumLoopsInScop, "Number of loops in scops after pruning");
41STATISTIC(NumBoxedLoops, "Number of boxed loops in SCoPs after pruning");
42STATISTIC(NumAffineLoops, "Number of affine loops in SCoPs after pruning");
43
44static 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
59static 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(ScalarsAreUnprofitable: true)) {
70 POLLY_DEBUG(
71 dbgs() << "SCoP pruned because it probably cannot be optimized in "
72 "a significant way\n");
73 S.invalidate(Kind: PROFITABLE, Loc: DebugLoc());
74 updateStatistics(S, Pruned: true);
75 } else {
76 updateStatistics(S, Pruned: false);
77 }
78
79 return false;
80}
81
82class PruneUnprofitableWrapperPass final : public ScopPass {
83public:
84 static char ID;
85
86 explicit PruneUnprofitableWrapperPass() : ScopPass(ID) {}
87 PruneUnprofitableWrapperPass(const PruneUnprofitableWrapperPass &) = delete;
88 PruneUnprofitableWrapperPass &
89 operator=(const PruneUnprofitableWrapperPass &) = delete;
90
91 void getAnalysisUsage(AnalysisUsage &AU) const override {
92 AU.addRequired<ScopInfoRegionPass>();
93 AU.setPreservesAll();
94 }
95
96 bool runOnScop(Scop &S) override { return runPruneUnprofitable(S); }
97};
98} // namespace
99
100char PruneUnprofitableWrapperPass::ID;
101
102Pass *polly::createPruneUnprofitableWrapperPass() {
103 return new PruneUnprofitableWrapperPass();
104}
105
106INITIALIZE_PASS_BEGIN(PruneUnprofitableWrapperPass, "polly-prune-unprofitable",
107 "Polly - Prune unprofitable SCoPs", false, false)
108INITIALIZE_PASS_END(PruneUnprofitableWrapperPass, "polly-prune-unprofitable",
109 "Polly - Prune unprofitable SCoPs", false, false)
110
111llvm::PreservedAnalyses
112PruneUnprofitablePass::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

source code of polly/lib/Analysis/PruneUnprofitable.cpp