summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/inlining/inliner.cc
blob: 85bbc8845576fc23d106669ec936d5e8dfcb2477 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 ** \file inlining/inliner.cc
 ** \brief Implementation of inlining::Inliner.
 */

#include <boost/graph/transitive_closure.hpp>

#include <ranges>
#include <callgraph/libcallgraph.hh>
#include <inlining/inliner.hh>
#include <parse/libparse.hh>
#include <parse/tweast.hh>

namespace inlining
{
  using namespace ast;

  Inliner::Inliner(const ast::Ast& tree)
    : super_type()
    , rec_funs_()
  {
    // Compute the transitive closure of the call graph to compute the
    // set of recursive functions.
    const callgraph::CallGraph* graph = callgraph::callgraph_compute(tree);
    callgraph::CallGraph closure;
    boost::transitive_closure(*graph, closure);

    // Compute the parent graph to get nested functions.
    const callgraph::ParentGraph* parentGraph =
      callgraph::parentgraph_compute(const_cast<ast::Ast&>(tree));

    // Re-attach properties to the vertices.
    for (auto [i, i_end] = boost::vertices(closure); i != i_end; ++i)
      closure[*i] = (*graph)[*i];
    // Detect recursive functions.
    for (auto [i, i_end] = boost::vertices(closure); i != i_end; ++i)
      {
        for (auto [j, j_end] = boost::adjacent_vertices(*i, closure);
             j != j_end; ++j)
          {
            // Check all parent functions to detect cases like these :
            // function a() =
            // (
            //     let
            //         function b() = a()
            //     in
            //     end
            // )
            if (parentGraph->hfundec_deep_get(closure[*i], closure[*j]))
              rec_funs_.insert(closure[*j]);
          }
      }
    delete graph;
    delete parentGraph;
  }

  const misc::set<const ast::FunctionDec*>& Inliner::rec_funs_get() const
  {
    return rec_funs_;
  }

  // FIXME: Some code was deleted here.

} // namespace inlining