summaryrefslogtreecommitdiff
path: root/gcc/ipa-hello-world.c
blob: 8289728f9bc4a21e6448c23250e14c17f468fe09 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "options.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-cfg.h"
#include "gimple-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 "gimple.h"
#include "cfg.h" // needed for gimple-iterator.h
#include "gimple-iterator.h"

#include "ipa-str-reorg-utils.h"

// First we need to collect all types
void
log (const char * const format, ...)
{
  if (!dump_file) return;

  va_list args;
  va_start(args, format);
  vfprintf(dump_file, format, args);
  va_end(args);
}


struct escaping_info_s { const_tree type; bool is_escaping; };
typedef struct escaping_info_s escaping_info;
typedef hash_map<const_tree, escaping_info> type_map;
static bool filter_type (type_map &escape_map, const_tree type);

void update_escape_info (const_tree type, type_map &escape_map, bool is_escaping);

void
update_escape_info_pointer (const_tree pointer_type, type_map &escape_map, bool is_escaping)
{
  gcc_assert(pointer_type);
  enum tree_code tree_code_pointer_type = TREE_CODE(pointer_type);
  bool is_pointer_type = POINTER_TYPE == tree_code_pointer_type;
  gcc_assert(is_pointer_type);

  escaping_info *info = escape_map.get(pointer_type);
  if (!info) return;

  info->is_escaping |= is_escaping;
  tree tree_type_pointer_type = TREE_TYPE(pointer_type);
  gcc_assert(tree_type_pointer_type);
  update_escape_info (tree_type_pointer_type, escape_map, info->is_escaping);
}

void
update_escape_info_array (const_tree array_type, type_map &escape_map, bool is_escaping)
{
  gcc_assert(array_type);
  enum tree_code tree_code_array_type = TREE_CODE(array_type);
  bool is_array_type = ARRAY_TYPE == tree_code_array_type;
  gcc_assert(is_array_type);

  escaping_info *info = escape_map.get(array_type);
  if (!info) return;

  info->is_escaping |= is_escaping;
  tree tree_type_array_type = TREE_TYPE(array_type);
  gcc_assert(tree_type_array_type);
  update_escape_info (tree_type_array_type, escape_map, info->is_escaping);
}

void
update_escape_info_record (const_tree record_type, type_map &escape_map, bool is_escaping)
{
  gcc_assert(record_type);
  enum tree_code tree_code_record_type = TREE_CODE(record_type);
  bool is_record_type = RECORD_TYPE == tree_code_record_type;
  gcc_assert(is_record_type);

  escaping_info *info = escape_map.get(record_type);
  // we are collecting records, therefore, we **must** have
  // it in the escaping info
  gcc_assert(info);
  info->is_escaping |= is_escaping;


  for (tree field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
  {
    gcc_assert(field);
    tree tree_type_field = TREE_TYPE(field);
    gcc_assert(tree_type_field);
    update_escape_info (tree_type_field, escape_map, info->is_escaping);
  }
}	

void
update_escape_info (const_tree type, type_map &escape_map, bool is_escaping)
{
  // INFO: This is an optimization.
  if (!is_escaping) return;

  gcc_assert(type);
  enum tree_code tree_code_type = TREE_CODE(type);
  switch (tree_code_type)
  {
    case ARRAY_TYPE:
      update_escape_info_array(type, escape_map, is_escaping);
    break;
    case POINTER_TYPE:
      update_escape_info_pointer(type, escape_map, is_escaping);
    break;
    case RECORD_TYPE:
      update_escape_info_record(type, escape_map, is_escaping);
    break;
    default:
    break;
  }
}

static bool
filter_pointer (type_map &escape_map, const_tree pointer)
{
  enum tree_code code = TREE_CODE (pointer);
  gcc_assert(POINTER_TYPE == code);
  const_tree base_type = TREE_TYPE(pointer);
  return filter_type (escape_map, base_type);
}

static bool
filter_array (type_map &escape_map, const_tree array)
{
  enum tree_code code = TREE_CODE (array);
  gcc_assert(ARRAY_TYPE == code);
  const_tree base_type = TREE_TYPE(array);
  return filter_type (escape_map, base_type);
}

static bool
filter_record (type_map &escape_map, const_tree record)
{
  log("filtering record %s\n", get_type_name(record));
  gcc_assert(record);
  enum tree_code code = TREE_CODE(record);
  gcc_assert(RECORD_TYPE == code);

  for (tree field = TYPE_FIELDS (record); field; field = DECL_CHAIN (field))
  {
    tree field_type = TREE_TYPE(field);
    gcc_assert(field_type);
    filter_type(escape_map, field_type);
  }

  return false;
}

static bool
filter_type (type_map &escape_map, const_tree type)
{
  log("inside filter_type\n");

  if (!type) return true;

  bool retval = true;
  enum tree_code code = TREE_CODE(type);
  switch (code)
  {
    case ARRAY_TYPE: retval = filter_array(escape_map, type); break;
    case POINTER_TYPE: retval = filter_pointer(escape_map, type); break;
    case RECORD_TYPE: retval = filter_record(escape_map, type); break;
    default: break;
  }

  if (retval) return retval;

  escaping_info info = { type, false };
  escape_map.put(type, info);
  return retval;
}

static bool
filter_var_decl (type_map &escape_map, const_tree var_decl)
{
  log("filtering var_decl\n");
  gcc_assert(var_decl);
  enum tree_code code = TREE_CODE(var_decl);
  gcc_assert(code == VAR_DECL);
  tree type = TREE_TYPE (var_decl);
  gcc_assert(type);
  return filter_type(escape_map, type);
}

static bool
filter_parm_declarations (const_tree parm_decl, type_map &escape_map)
{
  gcc_assert(parm_decl);
  enum tree_code code = TREE_CODE(parm_decl);
  gcc_assert(PARM_DECL == code);
  tree type = TREE_TYPE (parm_decl);
  gcc_assert(type);
  return filter_type(escape_map, type);
}

static void
collect_global(type_map &escape_map, varpool_node *vnode)
{
  log("collect_global\n");
  gcc_assert(vnode);
  
  struct ipa_ref *ref = NULL;
  for (int i = 0; vnode->iterate_referring (i, ref); i++)
  {
    log("inside vnode loop\n");
    tree decl = vnode->decl;
    gcc_assert(decl);
    enum tree_code code = TREE_CODE(decl);
    gcc_assert(VAR_DECL == code);
    bool filter_out = filter_var_decl(escape_map, decl);
    if (filter_out) continue;

    tree type = TREE_TYPE(decl);
    gcc_assert(type);
    escaping_info info = { type, false };
    escape_map.put (type, info);
  }
}

static void
collect_globals(type_map &escape_map)
{
  varpool_node *vnode;
  FOR_EACH_VARIABLE (vnode)
  {
    collect_global(escape_map, vnode);
  }
}

static void
collect_parm_declarations (cgraph_node *cnode, type_map &escape_map)
{
  gcc_assert(cnode);
  log("does function %s have parameters?\n", cnode->name());
  for (tree parm = DECL_ARGUMENTS (cnode->decl); parm; parm = DECL_CHAIN (parm))
  {
    tree type = TREE_TYPE(parm);
    log("about to enter parameter type %s\n", get_type_name(type));
    bool filter_out = filter_parm_declarations (parm, escape_map);
    if (filter_out) continue;

    log("putting parameter type %s\n", get_type_name(type));
    gcc_assert(type);
    escaping_info info = { type, false };
    escape_map.put(type, info);
  }
}

static void
collect_local_declarations (cgraph_node *cnode, type_map &escape_map)
{
  gcc_assert(cnode);
  int i = 0;
  function *func = DECL_STRUCT_FUNCTION (cnode->decl);
  cnode->get_untransformed_body ();
  tree var_decl = NULL;
  FOR_EACH_LOCAL_DECL (func, i, var_decl)
  {
    gcc_assert(var_decl);
    bool filter_out = filter_var_decl(escape_map, var_decl);
    if (filter_out) continue;

    tree tree_type = TREE_TYPE(var_decl);
    gcc_assert(tree_type);
    escaping_info info = { tree_type, false };
    escape_map.put(tree_type, info);
  }
}

static void
collect_pointer_plus (tree lhs, tree rhs1, tree rhs2, type_map &escape_map)
{
  const_tree lhs_type = lhs ? TREE_TYPE(lhs) : NULL;
  bool is_lhs_boring = lhs_type ? filter_type(escape_map, lhs_type) : true;
  const_tree rhs1_type = rhs1 ? TREE_TYPE(rhs1) : NULL;
  bool is_rhs1_boring = rhs1_type ? filter_type(escape_map, rhs1_type) : true;
  const_tree rhs2_type = rhs2 ? TREE_TYPE(rhs2): NULL;
  bool is_rhs2_boring = rhs2_type ? filter_type(escape_map, rhs2_type) : true;

  if (!is_lhs_boring)
  {
    escaping_info info = { lhs_type , false };
    escape_map.put(lhs_type, info);
  }
  if (!is_rhs1_boring)
  {
    escaping_info info = { rhs1_type, false };
    escape_map.put(rhs1_type, info);
  }
  if (!is_rhs2_boring)
  {
    escaping_info info = {rhs2_type, false};
    escape_map.put(rhs2_type, info);
  }
}

static void
collect_assign_rhs (gimple *stmt, type_map &escape_map)
{
  enum tree_code code = gimple_expr_code (stmt);
  switch (code)
  {
    case POINTER_PLUS_EXPR:
    case POINTER_DIFF_EXPR:
    case COMPONENT_REF:
    {
       tree rhs2 = gimple_assign_rhs2(stmt);
       tree rhs1 = gimple_assign_rhs1(stmt);
       tree lhs = gimple_assign_lhs (stmt);
       collect_pointer_plus (lhs, rhs1, rhs2, escape_map);
       break;
    }
    default:
    break;
  }
}

static void
collect_assign (gimple *stmt, type_map &escape_map)
{
  gcc_assert(stmt);
  collect_assign_rhs (stmt, escape_map);
}

static void
collect_stmt (gimple *stmt, type_map &escape_map)
{
  gcc_assert (stmt);
  const enum gimple_code code = gimple_code (stmt);
  switch (code)
  {
    case GIMPLE_ASSIGN:
      collect_assign(stmt, escape_map);
    break;
    default:
    break;
  }

  return;
}

static void
collect_basic_block (basic_block bb, type_map &escape_map)
{
  gcc_assert(bb);
  for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next(&gsi))
  {
    gimple *stmt = gsi_stmt (gsi);
    collect_stmt (stmt, escape_map);
  }
}

static void
collect_function_body (cgraph_node *cnode, type_map &escape_map)
{
  gcc_assert (cnode);
  cnode->get_untransformed_body();
  basic_block bb = NULL;
  tree decl = cnode->decl;
  gcc_assert(decl);
  function *func = DECL_STRUCT_FUNCTION (decl);
  gcc_assert(func);
  push_cfun(func);
  FOR_EACH_BB_FN (bb, func)
  {
    gcc_assert(bb);
    collect_basic_block (bb, escape_map);
  }
  pop_cfun();
}

static void
collect_types(type_map &escape_map)
{
  collect_globals(escape_map);

  cgraph_node *cnode = NULL;
  FOR_EACH_DEFINED_FUNCTION (cnode)
  {
    collect_function_body (cnode, escape_map);
    collect_local_declarations (cnode, escape_map);
    collect_parm_declarations (cnode, escape_map);
  }
}

bool
_print_types( const_tree const &type, escaping_info *info, __attribute((unused)) void*)
{
  log("collected,%s\n", get_type_name(type));
  bool is_escaping = info->is_escaping;
  log("type %s is escaping %s\n", get_type_name(type), is_escaping ? "true" : "false");
  return true;
}

static void
print_types(type_map &escape_map)
{
  log("printing_types\n");
  escape_map.traverse<void*, _print_types> (NULL);
}

static bool
is_variable_escaping(varpool_node *vnode)
{
  gcc_assert(vnode);
  return vnode->externally_visible;
}

static bool
is_function_escaping(cgraph_node *cnode)
{
  gcc_assert(cnode);
  return cnode->externally_visible;
}

void
calculate_escaping_parameters(cgraph_node *cnode, type_map &escape_map)
{
  gcc_assert(cnode);
  tree function = cnode->decl;
  gcc_assert(function);
  enum tree_code code = TREE_CODE (function);
  bool is_function_decl = FUNCTION_DECL == code;
  gcc_assert (is_function_decl);

  bool is_escaping = is_function_escaping(cnode);
  for (tree parm = DECL_ARGUMENTS (function); parm; parm = DECL_CHAIN(parm))
  {
    tree decl_name = DECL_NAME(parm);
    gcc_assert(decl_name);
    const char* identifier = IDENTIFIER_POINTER(decl_name);
    gcc_assert(identifier);
    tree tree_type = TREE_TYPE(parm);
    gcc_assert(tree_type);
   // void update_escaping_info (const_tree type, type_map &escape_map, bool is_escaping);
    update_escape_info(tree_type, escape_map, is_escaping);
    log("variable %s is escaping %s\n", identifier, is_escaping ? "true" : "false");
  }
}

void
is_return_type_escaping(cgraph_node *cnode, type_map &escape_map)
{
  gcc_assert(cnode);
  bool is_escaping = is_function_escaping(cnode);
  tree function = cnode->decl;
  gcc_assert(function);
  enum tree_code code = TREE_CODE(function);
  bool is_function = FUNCTION_DECL == code;
  gcc_assert(is_function);
 
  tree tree_type = TREE_TYPE(function);
  gcc_assert(tree_type);
  tree return_type = TREE_TYPE(tree_type);
  gcc_assert(return_type);
  log("return type %s\n", get_type_name(return_type));

  // void update_escaping_info (const_tree type, type_map &escape_map, bool is_escaping);
  update_escape_info(return_type, escape_map, is_escaping);
  //escaping_info *info = escape_map.get(return_type);
  // If there's no info it means it is not interesting...
  //if (!info) return;
  //info->is_escaping |= is_escaping;
}

void
is_any_function_escaping(type_map &escape_map)
{
  cgraph_node *cnode = NULL;
  FOR_EACH_FUNCTION (cnode)
  {
     cnode->get_untransformed_body();
     log("function name = %s\n", cnode->name());
     bool is_escaping = is_function_escaping(cnode);
     log("function %s is escaping %s\n", cnode->name(), is_escaping ? "true" : "false");
     calculate_escaping_parameters(cnode, escape_map);
     is_return_type_escaping(cnode, escape_map);
  }
}

void
is_any_variable_escaping(type_map &escape_map)
{
  varpool_node *vnode = NULL;
  // Global variables
  FOR_EACH_VARIABLE(vnode)
  {
    bool is_escaping = is_variable_escaping(vnode);
    log("variable %s is escaping %s\n", vnode->name(), is_escaping ? "true" : "false");
    tree decl = vnode->decl;
    gcc_assert(decl);
    tree type = TREE_TYPE(decl);
    gcc_assert(type);
   // void update_escaping_info (const_tree type, type_map &escape_map, bool is_escaping);
    update_escape_info (type, escape_map, is_escaping);
    //escaping_info *info = escape_map.get(type);
    //if (!info) return;
    //info->is_escaping |= is_escaping;
  }
}

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

static void
cast_to_void_in_assign(const_tree lhs, const_tree rhs, type_map &escape_map)
{
  gcc_assert(lhs);
  gcc_assert(rhs);
  tree tree_type_lhs = TREE_TYPE(lhs);
  gcc_assert(tree_type_lhs);
  tree tree_type_rhs = TREE_TYPE(rhs);
  gcc_assert(tree_type_rhs);
  const char* const type_name_lhs = get_type_name(tree_type_lhs);
  const char* const type_name_rhs = get_type_name(tree_type_rhs);
  enum tree_code tree_code_rhs = TREE_CODE(rhs);
  bool is_ssa_name = SSA_NAME == tree_code_rhs;
  bool is_var_decl = VAR_DECL == tree_code_rhs;
  bool is_var_decl_or_ssa_name = is_ssa_name || is_var_decl;
  log("lhs = %s, rhs = %s\n", type_name_lhs, type_name_rhs);
  if (!is_var_decl_or_ssa_name) return;

  bool is_casting_stmt = tree_type_lhs != tree_type_rhs;
  log("is casting stmt ? %s\n", is_casting_stmt ? "true" : "false");
  if (!is_casting_stmt) return;

  log("escaping lhs %s\n", type_name_lhs);
  update_escape_info(tree_type_lhs, escape_map, true);

  log("escaping rhs %s\n", type_name_rhs);
  update_escape_info(tree_type_rhs, escape_map, true);
}

static void
cast_to_void_in_assign (gimple *stmt, type_map &escape_map)
{
  enum gimple_code gcode = gimple_code (stmt);
  const char* const gcode_str  = gimple_code_name[gcode];
  gcc_assert(gcode_str);

  switch (gimple_assign_rhs_class (stmt))
  {
    case GIMPLE_SINGLE_RHS:
    case GIMPLE_UNARY_RHS:
    {
       tree lhs = gimple_assign_lhs(stmt);
       tree rhs = gimple_assign_rhs1(stmt);
       cast_to_void_in_assign(lhs, rhs, escape_map);
    }
    default:
    break;
  }
}

static void
cast_to_void_in_stmt (gimple *stmt, type_map &escape_map)
{
  gcc_assert (stmt);
  const enum gimple_code code = gimple_code (stmt);
  switch (code)
  {
    case GIMPLE_ASSIGN:
      cast_to_void_in_assign(stmt, escape_map);
    break;
    default:
    break;
  }

  return;
}

static void
cast_to_void_in_bb(basic_block bb, type_map &escape_map)
{
  gcc_assert(bb);
  for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next(&gsi))
  {
    gimple *stmt = gsi_stmt (gsi);
    cast_to_void_in_stmt (stmt, escape_map);
  }
}

static void
cast_to_void_in_function(cgraph_node *cnode, type_map &escape_map)
{
  gcc_assert (cnode);
  print_function (cnode);
  basic_block bb = NULL;
  tree decl = cnode->decl;
  gcc_assert(decl);
  function *func = DECL_STRUCT_FUNCTION (decl);
  gcc_assert(func);
  push_cfun(func);
  FOR_EACH_BB_FN (bb, func)
  {
    gcc_assert(bb);
    cast_to_void_in_bb(bb, escape_map);
  }
  pop_cfun();
}

static void
cast_to_void_in_program(type_map &escape_map)
{
  cgraph_node *cnode = NULL;
  FOR_EACH_FUNCTION (cnode)
  {
     gcc_assert(cnode);
     cnode->get_untransformed_body();
     cast_to_void_in_function(cnode, escape_map);
  }
}

static unsigned int
iphw_execute()
{
  type_map escape_map;
  collect_types(escape_map);
  is_any_variable_escaping(escape_map);
  is_any_function_escaping(escape_map);
  cast_to_void_in_program(escape_map);
  print_types(escape_map);
  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);
}