summaryrefslogtreecommitdiff
path: root/gcc/type-escaper.c
blob: ef81af37589cdb9932bcf835a9445f15f0249377 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#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_single_level(const_tree t, Reason r)
{
  gcc_assert(t);
  const bool already_in_typemap = calc.find(t) != calc.end();
  already_in_typemap ? calc[t] |= r : calc[t] = r;
}

void
TypeEscaper::_update(const_tree t)
{
  gcc_assert(t);
  // assert type is in universe
  const bool already_in_typemap = calc.find(t) != calc.end();
  // Do we have to invalidate all types which point to a volatile type?
  // Or do we have to invalidate all types pointed to by a volatile type?
  // Or do we only invalidate all types which are volatile.
  // This is only the third option.
  const bool is_volatile = TYPE_VOLATILE(t);
  Reason _is_volatile;
  _is_volatile.type_is_volatile = is_volatile;
  Reason _inner = _reason | _is_volatile;
  _inner.type_is_casted = _inside_indirect_field ? false : _inner.type_is_casted;
  already_in_typemap ? calc[t] |= _inner : calc[t] = _inner;
}

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

void
TypeEscaper::_walk_POINTER_TYPE_pre(const_tree t)
{
  _inside_indirect_field = _inside_field > 0 ? _inside_indirect_field + 1 : _inside_indirect_field;
  _update(t);
}

void
TypeEscaper::_walk_POINTER_TYPE_post(const_tree t)
{
  _inside_indirect_field = _inside_field > 0 ? _inside_indirect_field - 1 : _inside_indirect_field;
}

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;
}

void
TypeEscaper::_walk_field_pre(const_tree t)
{
  _inside_field++;
}

void
TypeEscaper::_walk_field_post(const_tree t)
{
  _inside_field--;
}

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)
{
}

void
TypeEscaper::_walk_FUNCTION_TYPE(const_tree t)
{
}

void
TypeEscaper::_walk_METHOD_TYPE(const_tree t)
{
}


void
TypeEscaper::_walk_METHOD_TYPE_pre(const_tree 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);
    Reason r = i->second;
    log("%s reason: ", name.c_str());
    r.print();
  }
}