summaryrefslogtreecommitdiff
path: root/gcc/type-escaper.c
blob: 466f13a77cc6e43018e0faba1359918467fcd569 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple-expr.h"
#include "predict.h"
#include "alloc-pool.h"
#include "tree-pass.h"
#include "cgraph.h"
#include "diagnostic.h"
#include "fold-const.h"
#include "gimple-fold.h"
#include "symbol-summary.h"
#include "tree-vrp.h"
#include "ipa-prop.h"
#include "tree-pretty-print.h"
#include "tree-inline.h"
#include "ipa-fnsummary.h"
#include "ipa-utils.h"
#include "tree-ssa-ccp.h"
#include "stringpool.h"
#include "attribs.h"
#include "tree-ssa-alias.h"
#include "tree-ssanames.h"
#include "gimple.h"
#include "cfg.h" // needed for gimple-iterator.h
#include "gimple-iterator.h"
#include "gimple-ssa.h"
#include <stdbool.h>
#include "types-inlines.h"

#include "type-escaper.hpp"
#include "type-stringifier.hpp"

bool
TypeEscaper::is_memoized(const_tree t)
{
  const bool in_set = calc.find(t) != calc.end();
  if (!in_set) return false;

  const bool will_not_escape = !_reason.is_escaping;
  if (will_not_escape) return true;

  const bool already_escaping = in_set && calc[t].is_escaping;
  if (already_escaping) return true;

  return false;
}

static inline void
assert_type_is_in_universe(const_tree type, ptrset_t &types)
{
#ifdef SANITY_CHECKS
  gcc_assert(types.in_universe(type));
#endif
}

ptrset_t
TypeEscaper::get_sets()
{
  place_escaping_types_in_set();
  return _ptrset;
}

void
TypeEscaper::place_escaping_types_in_set()
{
  TypeStringifier stringifier;
  for (auto i = calc.cbegin(), e = calc.cend(); i != e; ++i)
  {
    const_tree type = i->first;
    // We should have seen it before
    assert_type_is_in_universe(type, _ptrset);

    // We should only track interesting types
    // Types which are not in points_to_record are the ones
    // that are pointed to by records.
    // I think it is possible to prune them ahead of time...
    if (!_ptrset.in_points_to_record(type)) continue;

    const Reason reason = i->second;
    std::string name = stringifier.stringify(type);
    reason.is_escaping ? _ptrset.escaping.insert(type) : _ptrset.non_escaping.insert(type);
  }
}

void
TypeEscaper::update(const_tree t, Reason r)
{
  gcc_assert(t);
  _reason = r;
  walk(t);
}

void
TypeEscaper::_update(const_tree t)
{
  gcc_assert(t);
  // assert type is in universe
  const bool already_in_typemap = calc.find(t) != calc.end();
  already_in_typemap ? calc[t] |= _reason : calc[t] = _reason;
}

void
TypeEscaper::_walk_ARRAY_TYPE_pre(const_tree t)
{
  _update(t);  
}

void
TypeEscaper::_walk_POINTER_TYPE_pre(const_tree t)
{
  _update(t);
}

void
TypeEscaper::_walk_REFERENCE_TYPE_pre(const_tree t)
{
  _update(t);
}

void
TypeEscaper::_walk_RECORD_TYPE_pre(const_tree t)
{
  _update(t);
}

void
TypeEscaper::_walk_UNION_TYPE_pre(const_tree t)
{
  _inside_union++;
  bool is_escaping = _inside_union > 0;
  _update(t);
  // After us... so that we can see what is our previous value
  _reason.type_is_in_union |= is_escaping;
  _reason.is_escaping |= is_escaping;
}

void
TypeEscaper::_walk_UNION_TYPE_post(const_tree t)
{
  _inside_union--;
  Reason prev = calc[t];
  _update(t);
  _reason = prev;
}

void
TypeEscaper::_walk_FUNCTION_TYPE_pre(const_tree t)
{
  _update(t);
}

void
TypeEscaper::_walk_METHOD_TYPE_pre(const_tree t)
{
  _update(t);
}

void
TypeEscaper::print_reasons()
{
  TypeStringifier stringifier;
  for (auto i = calc.cbegin(), e = calc.cend(); i != e; ++i)
  {
    const_tree t = i->first;
    std::string name = stringifier.stringify(t);
    const bool in_universe = _ptrset.in_universe(t);
    if (!in_universe) continue;

    Reason r = i->second;
    const bool is_escaping = r.is_escaping;
    if (!is_escaping) continue;

    log("%s reason: ", name.c_str());
    r.print();
  }
}