summaryrefslogtreecommitdiff
path: root/gcc/type-reconstructor.c
blob: 350d9bd49895f36ce3c13aea6bea5bcebf225339 (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
#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"

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)
{
  _working_stack.push(build_distinct_type_copy((tree)t));
}

void
TypeReconstructor::_walk_ARRAY_TYPE_post(const_tree t)
{
  const_tree inner_type = TREE_TYPE(t);
  gcc_assert(inner_type);
  const bool in_map = _reorg_map.find(inner_type) != _reorg_map.end();
  tree t_reorg = _working_stack.top();
  _working_stack.pop();

  if (!in_map) return;

  tree inner_reorg_type = _reorg_map[inner_type];
  TREE_TYPE(t_reorg) = inner_reorg_type;
  TYPE_NAME((tree)t_reorg) = get_new_identifier(t_reorg);
  _reorg_map[t] = t_reorg;
  log("working type %s\n", t_reorg ? "t" : "f");
}

void
TypeReconstructor::_walk_POINTER_TYPE_pre(const_tree t)
{
  // We really should only make a distinct type copy if 
  // it points to a pointer that will be rewritten...
  _working_stack.push(build_distinct_type_copy((tree)t));
}

void
TypeReconstructor::_walk_POINTER_TYPE_post(const_tree t)
{
  tree t_reorg = _working_stack.top();
  _working_stack.pop();

  const_tree inner_type = TREE_TYPE(t);
  const bool in_map = _reorg_map.find(inner_type) != _reorg_map.end();
  // We are pointing to an integer or something like that.
  if (!in_map) return;

  tree inner_type_reorg = _reorg_map[inner_type];
  TREE_TYPE((tree)t_reorg) = inner_type_reorg;
  TYPE_NAME((tree)t_reorg) = get_new_identifier(t_reorg);
  _reorg_map[t] = t_reorg;;
  log("working type %s\n", t_reorg ? "t" : "f");
}

void
TypeReconstructor::_walk_RECORD_TYPE_pre(const_tree t)
{
  const bool in_map = _records.find(t) != _records.end();
  // The modifying stack let's us know if the current record
  // type is being modified.
  _modifying.push(in_map);

  field_offsets_t empty;
  _record_stack.push(in_map ? _records[t] : empty);

  // We really should only make a distinct type copy if 
  // it points to a pointer that will be rewritten...
  _working_stack.push(build_distinct_type_copy((tree)t));

  type_vector_t vec;
  type_vector_t vec2;
  _field_decls.push(vec);
  _eliminated_fields.push(vec2);
}

void
TypeReconstructor::_walk_UNION_TYPE_pre(const_tree t)
{
  const bool in_map = _records.find(t) != _records.end();
  // The modifying stack let's us know if the current record
  // type is being modified.
  gcc_assert(!in_map);
  _modifying.push(in_map);

  field_offsets_t empty;
  _record_stack.push(in_map ? _records[t] : empty);

  // We really should only make a distinct type copy if 
  // it points to a pointer that will be rewritten...
  _working_stack.push(build_distinct_type_copy((tree)t));

  type_vector_t vec;
  type_vector_t vec2;
  _field_decls.push(vec);
  _eliminated_fields.push(vec2);
}

void
TypeReconstructor::_walk_UNION_TYPE_post(const_tree t)
{
  _walk_RECORD_TYPE_post(t);
}


void
TypeReconstructor::_walk_RECORD_TYPE_post(const_tree t)
{
  const bool already_changed = _reorg_map.find(t) != _reorg_map.end();
  type_vector_t fields_to_keep = _field_decls.top();
  for (auto i = fields_to_keep.cbegin(), e = fields_to_keep.cend(); i != e; ++i)
  {
    const_tree field = *i;
  }
  type_vector_t fields_to_eliminate = _eliminated_fields.top();
  _eliminated_fields.pop();
  _field_decls.pop();
  tree working_type = _working_stack.top();
  _working_stack.pop();
  _record_stack.pop();
  const bool should_be_modified = _modifying.top();
  _modifying.pop();

  // There are no fields to eliminate
  // if (fields_to_eliminate.empty()) return;
  // But, we can have a nested struct that will be rewritten
  // so, we should keep not exit early

  // There are no fields to keep
  // Can be an incomplete type

  // It is already changed, so just pop the stacks and that's it.
  if (already_changed) return;

  if (fields_to_keep.empty())
  {
    layout_type(working_type);
    TYPE_NAME(working_type) = get_new_identifier(working_type);
    _reorg_map[t] = working_type;
    return;
  }

  tree f = (tree) fields_to_keep[0];
  tree tf = TREE_TYPE(f);
  bool field_type_in_reorg_map = _reorg_map.find(tf) != _reorg_map.end();
  if (field_type_in_reorg_map)
  {
    TREE_TYPE(f) = _reorg_map[tf];
  }
  TYPE_FIELDS((tree)working_type) = f;
  unsigned s = fields_to_keep.size();

  for (unsigned i = 1; i < s; i++)
  {
    tree current = (tree) fields_to_keep[i];
    tree tf = TREE_TYPE(current);
    field_type_in_reorg_map = _reorg_map.find(tf) != _reorg_map.end();
    if (field_type_in_reorg_map)
    {
      TREE_TYPE(current) = _reorg_map[tf];
    }
    DECL_CHAIN(f) = current;
    f = current;
  }

  layout_type(working_type);
  TYPE_NAME(working_type) = get_new_identifier(working_type);
  log("working type %s\n", working_type ? "t" : "f");
  _reorg_map[t] = working_type;
}



void
TypeReconstructor::_walk_field_pre(const_tree t)
{
  unsigned f_offset = tree_to_uhwi(DECL_FIELD_OFFSET(t));
  unsigned f_offset_2 = tree_to_uhwi(DECL_FIELD_BIT_OFFSET(t));
  unsigned f_offset_3 = f_offset * 8 + f_offset_2;
  field_offsets_t field_offsets = _record_stack.top();
  const bool should_be_deleted = field_offsets.find(f_offset_3) != field_offsets.end();
  const bool should_be_modified = _modifying.top();
  if (should_be_modified && should_be_deleted) {
	  type_vector_t &vec_ = _eliminated_fields.top();
	  vec_.push_back(t);
	  return;
  }

  // This is a field declaration, that should be saved.
  type_vector_t &vec = _field_decls.top();
  vec.push_back(copy_node((tree)t));
}

void
TypeReconstructor::_walk_field_post(const_tree t)
{
}