summaryrefslogtreecommitdiff
path: root/gcc/type-reconstructor.c
blob: df48c39267fbb9ad409df50e96a966694e25cced (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#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-reconstructor.hpp"
#include "type-stringifier.hpp"
#include "stor-layout.h"

void
TypeReconstructor::set_is_not_modified_yet(const_tree t)
{
  gcc_assert(t);
  const bool is_in_reorg_map = _reorg_map.find(t) != _reorg_map.end();
  modified_map[t] = false;

  const_tree tt = TREE_TYPE(t);
  if (!tt) return;

  const bool is_in_reorg_map_2 = _reorg_map.find(tt) != _reorg_map.end();
  log ("is in reorg_map_2 ? %s\n", is_in_reorg_map_2 ? "t" : "f");
  if (!is_in_reorg_map_2) return;

  tree type = _reorg_map[tt];
  const bool is_modified = strstr(TypeStringifier::get_type_identifier(type).c_str(), ".reorg");
  log("is modified %s\n", is_modified ? "t" : "f");
  if (!is_modified) return;

  mark_all_pointing_here_as_modified();

}

void
TypeReconstructor::mark_all_pointing_here_as_modified()
{
  for (auto i = modified_map.begin(), e = modified_map.end(); i != e; ++i)
  {
    const_tree type = i->first;
    i->second = true;
  }
}

bool
TypeReconstructor::get_is_modified(const_tree t)
{
  gcc_assert(t);
  const bool in_map = modified_map.find(t) != modified_map.end();
  gcc_assert(in_map);
  bool retval = modified_map[t];
  modified_map.erase(t);
  return retval;
}

bool
TypeReconstructor::is_memoized(const_tree t)
{
  const bool already_changed = _reorg_map.find(t) != _reorg_map.end();
  return already_changed;
}

static tree
get_new_identifier(const_tree type)
{
  const char* identifier = TypeStringifier::get_type_identifier(type).c_str();
  const bool is_new_type = strstr(identifier, "reorg");
  gcc_assert(!is_new_type);
  char *new_name;
  asprintf(&new_name, "%s.reorg", identifier);
  return get_identifier(new_name);
}

// Invariant for all _pre functions:
// _reorg_map[t] == NULL (due to memoization)
//
// Invariant for all _post functions:
// _reorg_map[TREE_TYPE(t)] != NULL
// unless TREE_TYPE(t) is not a type which points to a record
// a.k.a. no modifications
//
// To preserve invariant, we must include
// the following at the end of all _post functions:
// _reorg_map[t] = /* reorg type */
// 
// How information is passed?
// To further _post functions via stacks
//
// To previous _post functions via _reorg_map (and maybe others?)
//
//
void
TypeReconstructor::_walk_ARRAY_TYPE_pre(const_tree t)
{
  for_reference.push(t);
  set_is_not_modified_yet(t);

  tree copy = build_variant_type_copy((tree) t);
  in_progress.push(copy);

}

void
TypeReconstructor::_walk_ARRAY_TYPE_post(const_tree t)
{
  const_tree t2 = for_reference.top();
  gcc_assert(t2 == t);
  for_reference.pop();
  tree copy = in_progress.top();
  in_progress.pop();

  bool is_modified = get_is_modified(t);

  copy = is_modified ? build_distinct_type_copy(copy) : copy;
  TREE_TYPE(copy) = is_modified ? _reorg_map[TREE_TYPE(t)] : TREE_TYPE(copy);
  TYPE_NAME(copy) = is_modified ? get_new_identifier(copy) : TYPE_NAME(copy);
  // This is useful so that we go again through type layout
  TYPE_SIZE(copy) = is_modified ? NULL : TYPE_SIZE(copy);
  if (is_modified) layout_type(copy);

  _reorg_map[t] = is_modified ? copy : (tree)t;
}

void
TypeReconstructor::_walk_POINTER_TYPE_pre(const_tree t)
{
  for_reference.push(t);
  set_is_not_modified_yet(t);

  tree copy = build_variant_type_copy((tree) t);
  in_progress.push(copy);
}

void
TypeReconstructor::_walk_POINTER_TYPE_post(const_tree t)
{
  const_tree t2 = for_reference.top();
  gcc_assert(t2 == t);
  for_reference.pop();
  tree copy = in_progress.top();
  in_progress.pop();

  bool is_modified = get_is_modified(t);

  copy = is_modified ? build_variant_type_copy(copy) : copy;
  TREE_TYPE(copy) = is_modified ? _reorg_map[TREE_TYPE(t)] : TREE_TYPE(copy);
  TYPE_NAME(copy) = is_modified ? get_new_identifier(copy) : TYPE_NAME(copy);
  // This is useful so that we go again through type layout
  TYPE_SIZE(copy) = is_modified ? NULL : TYPE_SIZE(copy);
  if (is_modified) layout_type(copy);
  
  _reorg_map[t] = is_modified ? copy : (tree)t;
}

void
TypeReconstructor::_walk_RECORD_TYPE_pre(const_tree t)
{
  set_is_not_modified_yet(t);
  for_reference.push(t);
  // We don't know if we will modify this type t
  // So, let's make a copy. Just in case.
  tree copy = build_variant_type_copy((tree) t);
  in_progress.push(copy);
  field_list_stack.push( field_tuple_list_t() );
}

void
TypeReconstructor::_walk_RECORD_TYPE_post(const_tree t)
{
  const_tree t2 = for_reference.top();
  gcc_assert(t2 == t);
  for_reference.pop();

  tree copy = in_progress.top();
  in_progress.pop();
  field_tuple_list_t field_tuple_list = field_list_stack.top();
  field_list_stack.pop();

  // So, here all the work has been done to make sure
  // that the fields produced a field_tuple_list_t
  // with old fields and pointers to new fields.
  // There might be NULL values if new fields are eliminated.
  // So, now we want to do a couple of things.
  // First, is we need to change the TYPE_FIELDS
  // of the copy
  tree prev_field = NULL;
  for (auto i = field_tuple_list.cbegin(), e = field_tuple_list.cend(); i != e; ++i)
  {
    field_tuple_t field_tuple = *i;
    const_tree original_field = field_tuple.first;
    tree modified_field = field_tuple.second;
    if (!modified_field) {
      continue;
    }

    tree current_field = modified_field;
    if (!prev_field) {
      TYPE_FIELDS(copy) = current_field;
    } else {
      DECL_CHAIN(prev_field) = current_field;
    }
    prev_field = current_field;
  }


  bool is_modified = get_is_modified(t);
  // Ok, so now that we have fixed the TYPE_FIELDS of the copy...
  // We need to call layout_type
  copy = is_modified ? build_distinct_type_copy(copy) : copy;
  TYPE_NAME(copy) = is_modified ? get_new_identifier(copy) : TYPE_NAME(copy);
  // This is useful so that we go again through type layout
  TYPE_SIZE(copy) = is_modified ? NULL : TYPE_SIZE(copy);
  if (is_modified) layout_type(copy);
  _reorg_map[t] = is_modified ? copy : (tree)t;

  /*
  if (for_reference.empty()) return;
  gcc_assert(!in_progress.empty());

  // Now we need to find out
  // if we are modifying something in the parent as well?
  const_tree possible_field = for_reference.top();
  const enum tree_code code = TREE_CODE(possible_field);
  const bool is_field = FIELD_DECL == code;
  if (!is_field) return;

  tree field_in_progress = in_progress.top();
  TREE_TYPE(field_in_progress) = copy;
  */
}

void
TypeReconstructor::_walk_UNION_TYPE_pre(const_tree t)
{
}

void
TypeReconstructor::_walk_UNION_TYPE_post(const_tree t)
{
}

void
TypeReconstructor::_walk_field_pre(const_tree t)
{
  for_reference.push(t);
  // We don't know if we will rewrite the field
  // that we are working on. So proactively, let's make
  // a copy
  tree copy = copy_node((tree) t);
  tree type_copy = build_variant_type_copy((tree)(TREE_TYPE(t)));
  TREE_TYPE(copy) = type_copy;
  // To communicate this field to the other methods,
  // let's put it in the "in_progress" stack.
  in_progress.push(copy);
}

void
TypeReconstructor::_walk_field_post(const_tree t)
{
  const_tree t2 = for_reference.top();
  gcc_assert(t2 == t);
  for_reference.pop();

  // Let's get the copy we were working on.
  tree copy = in_progress.top();
  // Let's put the stack in the same position...
  in_progress.pop();

  // What record does this field belongs to?
  const_tree record = for_reference.top();

  // Is this a record that has a field that might be deleted?
  const bool is_record_interesting = _records.find(record) != _records.end();
  if (!is_record_interesting) {
    return;
  }

  field_offsets_t field_offsets = _records[record];
  // What's the field offset?
  unsigned f_byte_offset = tree_to_uhwi(DECL_FIELD_OFFSET(t));
  unsigned f_bit_offset = tree_to_uhwi(DECL_FIELD_BIT_OFFSET(t));
  unsigned f_offset = 8 * f_byte_offset + f_bit_offset;

  const bool can_field_be_deleted = field_offsets.find(f_offset) != field_offsets.end();
  if (can_field_be_deleted) mark_all_pointing_here_as_modified();
  const_tree original_type = TREE_TYPE(t);
  const bool type_memoized = is_memoized(original_type);
  // here we did a modification

  TREE_TYPE(copy) = type_memoized ? _reorg_map[TREE_TYPE(t)] : TREE_TYPE(copy);
  field_tuple_t tuple = std::make_pair(t, can_field_be_deleted ? NULL : copy);

  // Put the field into the vector
  field_tuple_list_t &field_tuple_list = field_list_stack.top();
  field_tuple_list.push_back(tuple);
  _reorg_fields[t] = copy;
}