Coverage Report

Created: 2019-02-20 07:29

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/Transform/ScopInliner.cpp
Line
Count
Source (jump to first uncovered line)
1
//===---- ScopInliner.cpp - Polyhedral based inliner ----------------------===//
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
// Take a SCC and:
10
// 1. If it has more than one component, bail out (contains cycles)
11
// 2. If it has just one component, and if the function is entirely a scop,
12
//    inline it.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "polly/LinkAllPasses.h"
17
#include "polly/RegisterPasses.h"
18
#include "polly/ScopDetection.h"
19
#include "llvm/Analysis/CallGraphSCCPass.h"
20
#include "llvm/IR/LLVMContext.h"
21
#include "llvm/IR/PassManager.h"
22
#include "llvm/Passes/PassBuilder.h"
23
#include "llvm/Transforms/IPO/AlwaysInliner.h"
24
25
#define DEBUG_TYPE "polly-scop-inliner"
26
27
using namespace polly;
28
extern bool polly::PollyAllowFullFunction;
29
30
namespace {
31
class ScopInliner : public CallGraphSCCPass {
32
  using llvm::Pass::doInitialization;
33
34
public:
35
  static char ID;
36
37
2
  ScopInliner() : CallGraphSCCPass(ID) {}
38
39
2
  bool doInitialization(CallGraph &CG) override {
40
2
    if (!polly::PollyAllowFullFunction) {
41
0
      report_fatal_error(
42
0
          "Aborting from ScopInliner because it only makes sense to run with "
43
0
          "-polly-allow-full-function. "
44
0
          "The heurtistic for ScopInliner checks that the full function is a "
45
0
          "Scop, which happens if and only if polly-allow-full-function is "
46
0
          " enabled. "
47
0
          " If not, the entry block is not included in the Scop");
48
0
    }
49
2
    return true;
50
2
  }
51
52
6
  bool runOnSCC(CallGraphSCC &SCC) override {
53
6
    // We do not try to inline non-trivial SCCs because this would lead to
54
6
    // "infinite" inlining if we are not careful.
55
6
    if (SCC.size() > 1)
56
0
      return false;
57
6
    assert(SCC.size() == 1 && "found empty SCC");
58
6
    Function *F = (*SCC.begin())->getFunction();
59
6
60
6
    // If the function is a nullptr, or the function is a declaration.
61
6
    if (!F)
62
3
      return false;
63
3
    if (F->isDeclaration()) {
64
1
      LLVM_DEBUG(dbgs() << "Skipping " << F->getName()
65
1
                        << "because it is a declaration.\n");
66
1
      return false;
67
1
    }
68
2
69
2
    PassBuilder PB;
70
2
    FunctionAnalysisManager FAM;
71
2
    FAM.registerPass([] { return ScopAnalysis(); });
72
2
    PB.registerFunctionAnalyses(FAM);
73
2
74
2
    RegionInfo &RI = FAM.getResult<RegionInfoAnalysis>(*F);
75
2
    ScopDetection &SD = FAM.getResult<ScopAnalysis>(*F);
76
2
77
2
    const bool HasScopAsTopLevelRegion =
78
2
        SD.ValidRegions.count(RI.getTopLevelRegion()) > 0;
79
2
80
2
    if (HasScopAsTopLevelRegion) {
81
2
      LLVM_DEBUG(dbgs() << "Skipping " << F->getName()
82
2
                        << " has scop as top level region");
83
2
      F->addFnAttr(llvm::Attribute::AlwaysInline);
84
2
85
2
      ModuleAnalysisManager MAM;
86
2
      PB.registerModuleAnalyses(MAM);
87
2
      ModulePassManager MPM;
88
2
      MPM.addPass(AlwaysInlinerPass());
89
2
      Module *M = F->getParent();
90
2
      assert(M && "Function has illegal module");
91
2
      MPM.run(*M, MAM);
92
2
    } else {
93
0
      LLVM_DEBUG(dbgs() << F->getName()
94
0
                        << " does NOT have scop as top level region\n");
95
0
    }
96
2
97
2
    return false;
98
2
  };
99
100
2
  void getAnalysisUsage(AnalysisUsage &AU) const override {
101
2
    CallGraphSCCPass::getAnalysisUsage(AU);
102
2
  }
103
};
104
} // namespace
105
char ScopInliner::ID;
106
107
0
Pass *polly::createScopInlinerPass() {
108
0
  ScopInliner *pass = new ScopInliner();
109
0
  return pass;
110
0
}
111
112
46.4k
INITIALIZE_PASS_BEGIN(
113
46.4k
    ScopInliner, "polly-scop-inliner",
114
46.4k
    "inline functions based on how much of the function is a scop.", false,
115
46.4k
    false)
116
46.4k
INITIALIZE_PASS_END(
117
    ScopInliner, "polly-scop-inliner",
118
    "inline functions based on how much of the function is a scop.", false,
119
    false)