summaryrefslogtreecommitdiff
path: root/gcc/ipa-hello-world.c
blob: 6d911082b5853a85f8b0a80818f86d6474486531 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#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-cfg.h"
#include "gimple.h"
#include "cfg.h" // needed for gimple-iterator.h
#include "gimple-iterator.h"

#include <map>
#include <set>

#include "ipa-type-escape-analysis.h"
#include "ipa-str-reorg-utils.h"


//TODO: place in header file
inline static void
print_function (cgraph_node *cnode)
{
  if (!dump_file)
    return;
  gcc_assert (cnode);
  cnode->get_untransformed_body ();
  dump_function_to_file (cnode->decl, dump_file, TDF_NONE);
}

typedef std::pair<const_tree /* record */, const_tree /* field */> fields;
//TODO: do not use pair.
//This names are unintelligible
typedef std::pair<unsigned /* reads */, unsigned /* writes */> accesses;
struct field_comparator
{
  bool operator()(const fields &left, const fields &right) const
  {
    const_tree left_record = left.first;
    gcc_assert(left_record);
    const_tree right_record = right.first;
    gcc_assert(right_record);
    // Make sure that we are only comparing valid types...
    const enum tree_code tree_code_left_record = TREE_CODE(left_record);
    const enum tree_code tree_code_right_record = TREE_CODE(right_record);
    const bool is_left_record_type = RECORD_TYPE == tree_code_left_record;
    const bool is_right_record_type = RECORD_TYPE == tree_code_right_record;
    log("left type is weird %s\n", get_type_name(left_record));
    gcc_assert(is_left_record_type);
    gcc_assert(is_right_record_type);
    const bool are_left_and_right_valid = is_left_record_type && is_right_record_type;
    gcc_assert(are_left_and_right_valid);

    // Handle typedefs:
    // Get the main variants of the main types
    const_tree main_variant_left = TYPE_MAIN_VARIANT(left_record);
    gcc_assert(main_variant_left);
    const_tree main_variant_right = TYPE_MAIN_VARIANT(right_record);
    gcc_assert(main_variant_right);
    // If they are not equal, we can do a comparison of the types here...
    const bool are_main_variants_equal = main_variant_left == main_variant_right;
    const bool left_type_less_than_right_type = main_variant_left < main_variant_right;
    if (!are_main_variants_equal) return left_type_less_than_right_type;

    // If they are equal, that means that we are comparing fields defined in the same record
    const_tree left_field = left.second;
    gcc_assert(left_field);
    const_tree right_field = right.second;
    gcc_assert(right_field);
    // Make sure that they are valid
    const enum tree_code tree_code_left_field = TREE_CODE(left_field);
    const enum tree_code tree_code_right_field = TREE_CODE(right_field);
    const bool is_left_field_field_decl = FIELD_DECL == tree_code_left_field;
    const bool is_right_field_field_decl = FIELD_DECL == tree_code_right_field;
    const bool are_left_and_right_field_decls = is_left_field_field_decl && is_right_field_field_decl;
    gcc_assert(are_left_and_right_field_decls);

    // Compare on the field offset.
    const_tree left_constant = byte_position(left_field);
    gcc_assert(left_constant);
    const_tree right_constant = byte_position(right_field);
    gcc_assert(right_constant);
    unsigned left_offset = tree_to_uhwi(left_constant);
    unsigned right_offset = tree_to_uhwi(right_constant);
    const bool left_offset_less_than_right_offset = left_offset < right_offset;
    return left_offset_less_than_right_offset;
  }
};

typedef std::map<fields, accesses, field_comparator> field_access_counter;
typedef std::set<const_tree> record_set;
enum access_code { READ_ACCESS, WRITE_ACCESS };

void
count_access_for_type_in_component_ref(const_tree component_ref, const record_set &non_escaping_records, field_access_counter &counter, const enum access_code access)
{
  gcc_assert(component_ref);
  enum tree_code tree_code_component_ref = TREE_CODE(component_ref);
  const bool is_component_ref = COMPONENT_REF == tree_code_component_ref;
  gcc_assert(is_component_ref);

  const_tree _struct = TREE_OPERAND(component_ref, 0);
  gcc_assert(_struct);
  const_tree tree_type_struct = TREE_TYPE(_struct);
  gcc_assert(tree_type_struct);
  enum tree_code tree_type_struct_code = TREE_CODE(tree_type_struct);
  const bool is_record_type = RECORD_TYPE == tree_type_struct_code;
  gcc_assert(is_record_type);

  //FIXME: Future proofing or makinng things more difficult to read?
  const bool in_set = 
#if __cplusplus > 201703L
          non_escaping_records.contains(tree_type_struct)
#else
	  non_escaping_records.find(tree_type_struct) != non_escaping_records.end()
#endif
	  ;

  log("%s is in non_escaping_records ? %s\n", get_type_name(tree_type_struct), in_set ? "true" : "false");
  log("access is %s\n", access == READ_ACCESS ? "read_access" : "write_access");
  if (!in_set) return;

  const_tree field = TREE_OPERAND(component_ref, 1);
  gcc_assert(field);
  enum tree_code tree_code_field = TREE_CODE(field);
  const bool is_field_decl = FIELD_DECL == tree_code_field;
  gcc_assert(is_field_decl);

  const std::pair<const_tree, const_tree> struct_field_pair = std::make_pair(tree_type_struct, field);
  accesses &access_counter = counter[struct_field_pair];

  switch (access)
  {
    case READ_ACCESS:
     //TODO: do not use pair.
     //This names are unintelligible
     access_counter.first++;
     log("%s.%s read %d\n", get_type_name(tree_type_struct), get_field_name(field), access_counter.first);
    break;
    case WRITE_ACCESS:
     //TODO: do not use pair.
     //This names are unintelligible
      access_counter.second++;
      log("%s.%s write %d\n", get_type_name(tree_type_struct), get_field_name(field), access_counter.second);
    break;
    default:
    gcc_unreachable();
    break;
  }
}

void
count_access_for_types_in_expr(const_tree expr, const record_set &non_escaping_records, field_access_counter &counter, const enum access_code access)
{
  gcc_assert(expr);
  enum tree_code tree_code_expr = TREE_CODE(expr);
  switch (tree_code_expr)
  {
    case COMPONENT_REF: count_access_for_type_in_component_ref(expr, non_escaping_records, counter, access); break;
    default: break;
  }
}

void
count_access_for_types_in_lhs(gimple *stmt, const record_set &non_escaping_records, field_access_counter &counter)
{
  gcc_assert(stmt);
  const enum gimple_code gimple_code_stmt = gimple_code(stmt);
  const bool is_assign = GIMPLE_ASSIGN == gimple_code_stmt;
  gcc_assert(is_assign);

  const_tree lhs = gimple_assign_lhs (stmt);
  //FIXME: I think if we do not access from GIMPLE_ASSIGN
  //this is no longer an invariant...
  gcc_assert(lhs);
  count_access_for_types_in_expr(lhs, non_escaping_records, counter, WRITE_ACCESS);
}

void
count_access_for_types_in_rhs(gimple *stmt, const record_set &non_escaping_records, field_access_counter &counter)
{
  gcc_assert(stmt);
  const enum gimple_code gimple_code_stmt = gimple_code(stmt);
  const bool is_assign = GIMPLE_ASSIGN == gimple_code_stmt;
  gcc_assert(is_assign);

  const enum gimple_rhs_class gimple_rhs_class_stmt = gimple_assign_rhs_class(stmt);
  switch (gimple_rhs_class_stmt)
  {
    case GIMPLE_TERNARY_RHS:
    {
      log("gimple_ternary_rhs\n");
    }
    /* fall through */
    case GIMPLE_BINARY_RHS:
    {
      log("gimple_binary_rhs\n");
    }
    /* fall through */
    case GIMPLE_SINGLE_RHS:
    case GIMPLE_UNARY_RHS:
    {
      const_tree rhs1 = gimple_assign_rhs1(stmt);
      gcc_assert(rhs1);
      count_access_for_types_in_expr(rhs1, non_escaping_records, counter, READ_ACCESS);
      log("gimple_single_rhs\n");
    }
    break;
    default:
      gcc_unreachable();
    break;
  }
}

void
count_access_for_types_in_assign(gimple *stmt, const record_set &non_escaping_records, field_access_counter &counter)
{
  gcc_assert(stmt);
  const enum gimple_code gimple_code_stmt = gimple_code(stmt);
  const bool is_assign = GIMPLE_ASSIGN == gimple_code_stmt;
  gcc_assert(is_assign);

  count_access_for_types_in_rhs(stmt, non_escaping_records, counter);
  count_access_for_types_in_lhs(stmt, non_escaping_records, counter);
}

void
count_access_for_types_in_stmt(gimple *stmt, const record_set &non_escaping_records, field_access_counter &counter)
{
   gcc_assert(stmt);
   const enum gimple_code gimple_code_stmt = gimple_code(stmt);
   switch(gimple_code_stmt)
   {
     case GIMPLE_ASSIGN:
       count_access_for_types_in_assign(stmt, non_escaping_records, counter);
     break;
     default:
     break;
   }
}

void
count_access_for_types_in_bb(basic_block bb, const record_set &non_escaping_records, field_access_counter &counter)
{
  gcc_assert(bb);
  for (gimple_stmt_iterator gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi))
  {
    gimple *stmt = gsi_stmt(gsi);
    count_access_for_types_in_stmt(stmt, non_escaping_records, counter);
  }
}

void
count_access_for_types_in_function(cgraph_node *cnode, const record_set &non_escaping_records, field_access_counter &counter)
{
  gcc_assert(cnode);
  tree decl = cnode->decl;
  gcc_assert(decl);
  function *func = DECL_STRUCT_FUNCTION (decl);
  gcc_assert(func);
  push_cfun(func);
  basic_block bb = NULL;
  FOR_EACH_BB_FN (bb, func)
  {
    gcc_assert(bb); 
    count_access_for_types_in_bb(bb, non_escaping_records, counter);
  }
  pop_cfun();
}

/* INFO:
 * Yes, I know we are returning a std::map.
 * Bad pattern? Maybe, but this will only be called once
 * and I rather pass by value because that allows
 * me to have a pure function and not worry about
 * garbage collection
 *
 * TODO: I'd like to change type_map for a std::map
 * TODO: I'd like to make this a template that can work
 * for std::map and std::set
 */
field_access_counter
count_access_for_types_in_linking_unit(const record_set &non_escaping_records)
{
  field_access_counter counter;
  cgraph_node *cnode = NULL;
  FOR_EACH_DEFINED_FUNCTION(cnode)
  {
    gcc_assert(cnode);
    print_function(cnode);
    count_access_for_types_in_function(cnode, non_escaping_records, counter);
  }
  return counter;
}

bool
_calculate_non_escaping_records(const_tree const &type, escaping_info *info, record_set *non_escaping_records)
{
  gcc_assert(info);
  gcc_assert(non_escaping_records);
  
  enum tree_code tree_code_type = TREE_CODE(type);
  const bool is_record_type = RECORD_TYPE == tree_code_type;
  if (!is_record_type) return true;

  non_escaping_records->insert(type);
  return true;
}

/*
 * Yes, again, we are passing a std::set
 * as a value. I don\t care too much since
 * this is only called once...
 */
static record_set
calculate_non_escaping_records(type_map &escaping_type_info)
{
  // We are going to have to traverse the type_map...
  // This is why I don't really like hash_set...
  record_set non_escaping_records;
  escaping_type_info.traverse<record_set*, _calculate_non_escaping_records>(&non_escaping_records);
  return non_escaping_records;
}

static unsigned int
iphw_execute()
{
  type_map escaping_types;
  calculate_escaping_types(escaping_types);
  const record_set non_escaping_records = calculate_non_escaping_records(escaping_types); 
  field_access_counter counter = count_access_for_types_in_linking_unit(non_escaping_records);
  return 0;
}

namespace {
const pass_data pass_data_ipa_hello_world =
{
  SIMPLE_IPA_PASS,
  "hello-world",
  OPTGROUP_NONE,
  TV_NONE,
  (PROP_cfg | PROP_ssa),
  0,
  0,
  0,
  0,
};

class pass_ipa_hello_world : public simple_ipa_opt_pass
{
public:
  pass_ipa_hello_world (gcc::context *ctx)
    : simple_ipa_opt_pass(pass_data_ipa_hello_world, ctx)
  {}

  virtual bool gate(function*) { return flag_ipa_hello_world; }
  virtual unsigned execute (function*) { return iphw_execute(); }
};
} // anon namespace

simple_ipa_opt_pass*
make_pass_ipa_hello_world (gcc::context *ctx)
{
  return new pass_ipa_hello_world (ctx);
}