[lldb] Simplify DumpValueObjectOptions::PointerDepth (NFC) (#117504)
[llvm-project.git] / polly / lib / Analysis / PruneUnprofitable.cpp
blobf8469c03fe55bdaa6e457e8f115be9dc3611da75
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.
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"
22 using namespace llvm;
23 using namespace polly;
25 #include "polly/Support/PollyDebug.h"
26 #define DEBUG_TYPE "polly-prune-unprofitable"
28 namespace {
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();
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;
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;
67 ScopsProcessed++;
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);
79 return false;
82 class PruneUnprofitableWrapperPass final : public ScopPass {
83 public:
84 static char ID;
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>();
93 AU.setPreservesAll();
96 bool runOnScop(Scop &S) override { return runPruneUnprofitable(S); }
98 } // namespace
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);
116 if (!Changed)
117 return PreservedAnalyses::all();
119 PreservedAnalyses PA;
120 PA.preserveSet<AllAnalysesOn<Module>>();
121 PA.preserveSet<AllAnalysesOn<Function>>();
122 PA.preserveSet<AllAnalysesOn<Loop>>();
123 return PA;