summaryrefslogtreecommitdiff
path: root/gcc/ipa-structure-reorg.c
blob: 10e092b664d5242da1fc4fc3e9c07de8bb7d9e5c (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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/* Interprocedural scalar replacement of aggregates
   Copyright (C) 2019-2020 Free Software Foundation, Inc.

  Contributed by Gary Oblock <goblock@marvell.com>

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple.h"
#include "tree-pass.h"
#include "cgraph.h"
#include "gimple-iterator.h"
#include "pretty-print.h"
#include <vector>
#include <map>
#include <set>
#include "ipa-structure-reorg.h"
#include "dumpfile.h"
#include "tree-pretty-print.h"
#include "gimple-pretty-print.h"
#include "langhooks.h"
// TBD more includes go here

static void
setup_debug_flags (Info *);
static void
final_debug_info (Info *);
static unsigned int
reorg_analysis (Info_t *);
static void
reorg_analysis_debug (Info *, ReorgType *);
static bool
find_decls_and_types (Info_t *);
static void
disqualify_struct_in_struct_or_union (Info_t *);
static void
initial_reorg_debug (Info *info, ReorgType *reorg);
static void
disqualify_struct_in_struct_or_union_debug (Info *, ReorgType *);
static void
disq_str_in_str_or_union_helper (tree, std::set<tree> *, Info_t *);
static unsigned int
reorg_qualification (Info_t *);
// Name changed and moved to its own file
// static void reorg_transformation ( Info_t *);
// made extern
// static void delete_reorgtype ( ReorgType_t *, Info_t *);
// static void undelete_reorgtype ( ReorgType_t *, Info_t *);
// static void remove_deleted_types ( Info *, ReorgFn);
static tree base_type_of (tree);
static bool
is_reorg_alloc_trigger (gimple *);
static ReorgType_t *
find_struct_type_ptr_to_struct (tree, Info *);
static ReorgType_t *
get_reorgtype_info (tree type, Info_t *info);
static void
print_reorg_with_msg (FILE *, ReorgType_t *, int, const char *);
static void
dump_reorg (ReorgType_t *reorg);
static void
print_reorgs (FILE *, int, Info *);
static void
print_reorg (FILE *, int, ReorgType_t *);
//-- debugging only --
static const char *code_str (enum tree_code);

//---------------- Code Follows ----------------

static unsigned int
ipa_structure_reorg (void)
{
  std::vector<ReorgType_t> Reorg_Type;
  std::vector<ReorgType_t> Saved_Reorg_Type;
  std::vector<ProgDecl_t> Prog_Decl;
  std::map<tree, BoolPair_t> StructTypes; // TBD horrible type name

  // DEBUG( "Running ipa_structure_reorg\n");

  // TBD we must have run the IPA points-to analysis and
  // be running in a single LTRANS partition. Sanity check
  // these here.

  Info_t info = {&Reorg_Type, &Saved_Reorg_Type,
		 &Prog_Decl,  &StructTypes,
		 0,	   0.0,
		 false,       false,
		 false,       false,
		 false,       false};

  setup_debug_flags (&info);

  if (flag_ipa_dead_field_eliminate)
    {
      str_reorg_dead_field_eliminate (&info);
    }

  return true;

  if (reorg_qualification (&info))
    {
      // Each of these do their own performance qualification and
      // if they delete any types they must invoke restore_deleted_types
      // so the next subpass has all the types to consider.
      // Also, if they change the shape of a type the must create my
      // type and update the ReorgTypes to reflect this.

      if (flag_ipa_structure_reorg || flag_ipa_field_reorder)
	{
	  str_reorg_field_reorder (&info);
	}
      if (flag_ipa_structure_reorg || flag_ipa_instance_interleave)
	{
	  str_reorg_instance_interleave (&info);
	}
    }

  final_debug_info (&info);

  return true;
}

static void
setup_debug_flags (Info *info)
{
  // The normal way of doing this would be to
  // set the flags with dump_file && (dump_flags & TDF_XXX
  // where XXX is some level of dump control but
  // I think the code here would work better
  // (where each flag is set off the dump_flags.)

  if (dump_file)
    {
      info->show_all_reorg_cands = true;
      info->show_all_reorg_cands_in_detail = dump_flags & TDF_DETAILS;
      info->show_delete = dump_flags & TDF_DETAILS;
      info->show_new_BBs = dump_flags & TDF_DETAILS;
      info->show_transforms = dump_flags & TDF_DETAILS;
      info->show_bounds = dump_flags & TDF_DETAILS;
    }
}

static void
final_debug_info (Info *info)
{
  // TBD
}

static unsigned int
reorg_analysis (Info_t *info)
{
  struct cgraph_node *node;

  find_decls_and_types (info);

  // Skip computing numbOfGlobalArrays initially.

  // Skip computing numbOfLocalArrays initially.

  // Compute numbOfDynmAllocs per type in regtype
  FOR_EACH_FUNCTION (node)
    {
      basic_block bb;

      struct function *func = DECL_STRUCT_FUNCTION (node->decl);

      // This is a work around
      if (func == NULL)
	{
	  continue;
	}

      FOR_EACH_BB_FN (bb, func)
	{
	  gimple_stmt_iterator gsi;
	  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
	    {
	      gimple *stmt = gsi_stmt (gsi);
	      // DEBUG_F ( print_gimple_stmt, stderr, stmt, (dump_flags_t)-1);
	      // DEBUG ( "\n");
	      if (is_gimple_call (stmt))
		{
		  // next line has issues but the mechanism is sound
		  tree t = *gimple_call_lhs_ptr (stmt);
		  // DEBUG( "t %p\n", t);
		  // Calls to a function returning void are skipped.
		  if (t != NULL)
		    {
		      tree bt = base_type_of (t);
		      if (TREE_CODE (bt) != RECORD_TYPE)
			{
			  continue;
			}
		      // find if in reorgtypes and get the info (in one call)
		      ReorgType_t *ri = get_reorgtype_info (bt, info);
		      if (ri != NULL && is_reorg_alloc_trigger (stmt))
			{
			  ri->numbOfDynmAllocs++;
			}
		    }
		}
	    }
	}
    }

  // It's LOT clear to use an iterator here TBD
  for (int i = 0; i < info->reorg_type->size (); i++)
    {
      int n = (*(info->reorg_type))[i].numbOfGlobalArrays
	      + (*(info->reorg_type))[i].numbOfLocalArrays
	      + (*(info->reorg_type))[i].numbOfDynmAllocs;
      if (n > 1)
	{
	  (*(info->reorg_type))[i].multi_pool = true;
	}
      if (n == 0)
	{
	  delete_reorgtype (&(*(info->reorg_type))[i], info);
	}
    }

  remove_deleted_types (info, &reorg_analysis_debug);

  return !info->reorg_type->empty ();
}

void
reorg_analysis_debug (Info *info, ReorgType *reorg)
{
  if (info->show_delete)
    {
      print_reorg_with_msg (dump_file, reorg, 2, "Was not allocated");
    }
}

static bool
find_decls_and_types (Info_t *info)
{
  // Don't keep any structure types if they aren't
  // used in an array or have a pointer type (which
  // hopefully will have an associated allocation.)
  // Note, initially ignore the explicit statically
  // allocated arrays.
  //
  // This initializes them too of course.

  // Find all struct types for initial ReorgTypes
  // marking them all to initially be deleted.
  // This is done by walking all variables.
  std::set<tree> typeset;
  varpool_node *var;
  FOR_EACH_VARIABLE (var)
    {
      tree decl = var->decl;
      // DEBUG( "L# %d Consider var->decl\n", __LINE__);
      // DEBUG_F( print_generic_decl, stderr, decl, (dump_flags_t)-1);
      tree base = base_type_of (decl);
      // DEBUG( "\nBase\n");
      // DEBUG( "TREE_CODE = %s\n", code_str( TREE_CODE ( base)));
      // DEBUG_F( print_generic_expr, stderr, base, (dump_flags_t)-1);
      // DEBUG( "\n");

      if (TREE_CODE (base) == RECORD_TYPE)
	{
	  // skip if found before
	  if (typeset.find (base) != typeset.end ())
	    {
	      // DEBUG( "not found\n");
	      continue;
	    }
	  else
	    {
	      // DEBUG( "found\n")
	      ;
	    }
	  ReorgType_t rt
	    = {0, base, 0, 0, 0, 0.0, 0.0, false, true, NULL, NULL};
	  info->reorg_type->push_back (rt);
	  typeset.insert (base); // ???
	}
    }

  // NOTE, the scheme above leaves out local variables so
  // I'll repeat the for the local variable of functions.
  // TBD Am I doing something actually, after this, that
  // is actually done here or above... it seems likely.

  cgraph_node *node;
  FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
    {
      tree decl;
      unsigned i;

      node->get_untransformed_body ();

      struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
      // enable this to see error in test_1_8
      // DEBUG( "L# %d, function name = %s\n",
      //__LINE__, lang_hooks.decl_printable_name ( node->decl, 2));
      if (fn == NULL)
	{
	  // DEBUG( "  EMPTY\n");
	  continue;
	}

      FOR_EACH_LOCAL_DECL (fn, i, decl)
	{
	  tree base = base_type_of (decl);
	  // enable this to see error in test_1_8
	  // DEBUG( "L# %d Consider local var decl\n", __LINE__);
	  // DEBUG_F( print_generic_decl, stderr, decl, (dump_flags_t)-1);
	  // DEBUG( "\n");

	  if (TREE_CODE (base) == RECORD_TYPE)
	    {
	      if (typeset.find (base) != typeset.end ())
		{
		  continue;
		}

	      ReorgType_t rt
		= {0, base, 0, 0, 0, 0.0, 0.0, false, true, NULL, NULL};
	      info->reorg_type->push_back (rt);
	      typeset.insert (base); // ???
	    }
	}
    }

  // We need this later for creating new types
  for (std::set<tree>::iterator ti = typeset.begin (); ti != typeset.end ();
       ti++)
    {
      (*(info->struct_types))[*ti] = {false, false};
    }

  if (info->show_all_reorg_cands_in_detail)
    {
      fprintf (dump_file, "All possible candidates:\n");
      print_reorgs (dump_file, 2, info);
    }

  // Scan all declarations for pointers to ReorgTypes
  // and in a later version array of them. When found
  // clear the deletion mark.
  // Note, there is no mechanism for looking at global declarations
  // so use FOR_EACH_VARIABLE instead. I'm not 100% this is the thing
  // actuall do here... but...
  FOR_EACH_VARIABLE (var)
    {
      tree decl = var->decl;
      ReorgType_t *rtype = find_struct_type_ptr_to_struct (decl, info);
      if (rtype != NULL)
	{
	  undelete_reorgtype (rtype, info);
	}
    }

  FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
    {
      tree decl;
      unsigned i;

      // Only need to do this once in the program and it was done
      // above!
      // Node->get_untransformed_body ();

      struct function *fn = DECL_STRUCT_FUNCTION (node->decl);

      // DEBUG( "fn %p\n", fn);
      // DEBUG_F( print_generic_decl, stderr, node->decl, (dump_flags_t)-1);
      // DEBUG( "\n");
      // I don't know why this is coming up null.... but I'll
      // skip it for now.
      if (fn == NULL)
	{
	  continue;
	}

      FOR_EACH_LOCAL_DECL (fn, i, decl)
	{
	  // Does this work... see tree.c:6132
	  ReorgType_t *rtype = find_struct_type_ptr_to_struct (decl, info);
	  if (rtype != NULL)
	    {
	      undelete_reorgtype (rtype, info);
	    }
	}
    }

  if (info->show_all_reorg_cands)
    {
      fprintf (dump_file, "All preliminary ReorgTypes:\n");
      // print_reorgs ( dump_file, 2, info);
    }

  // Scan all types in ReorgTypes for structure fields
  // and if they are pointers to a type Q in ReorgTypes
  // then clear the deletion mark of Q. Note, at this
  // point in the execution ReorgTypes is all the structure
  // types.
  //
  // It would be a bit nuts to allocate memory and hang it
  // off of pointer in a structure, but it's still possible.
  // Note, if there are no pointers to a structure of a type
  // then it is impossible to dynamically allocate memory of
  // that type. This of course assumes sane programming
  // practices and if they violate those structure reorg has
  // every right to punt.
  for (std::vector<ReorgType_t>::iterator ri = info->reorg_type->begin ();
       ri != info->reorg_type->end (); ri++)
    {
      for (tree fld = TYPE_FIELDS (ri->gcc_type); fld; fld = DECL_CHAIN (fld))
	{
	  ReorgType_t *rtype = find_struct_type_ptr_to_struct (fld, info);
	  if (rtype != NULL)
	    {
	      undelete_reorgtype (rtype, info);
	    }
	}
    }

  remove_deleted_types (info, &initial_reorg_debug);

  // Disqualifying structures in interior to structures is optional
  // (see comment at end of type escape section) but if it's not
  // done it commits the optimization to do something a little too
  // involved for the initial version.
  disqualify_struct_in_struct_or_union (info);

  if (info->reorg_type->empty ())
    {
      if (info->show_all_reorg_cands_in_detail)
	{
	  fprintf (dump_file, "find_decls_and_types: Found no types\n");
	}
      return false;
    }

  // initialize ids of ReorgTypes
  int id = 0;
  for (std::vector<ReorgType_t>::iterator ri = info->reorg_type->begin ();
       ri != info->reorg_type->end (); ri++)
    {
      ri->id = id;
      id++;
    }

  // Scan all declarations. If their type is in ReorgTypes
  // add them to ProgDecl.
  // Note, there is no mechanism for looking at global declarations
  // so use FOR_EACH_VARIABLE instead. I'm not 100% this is the thing
  // actuall do here... but...
  FOR_EACH_VARIABLE (var)
    {
      tree decl = var->decl;
      tree type = base_type_of (decl);
      if (TREE_CODE (type) == RECORD_TYPE
	  && get_reorgtype_info (type, info) != NULL)
	{
	  ProgDecl_t decl_info;
	  decl_info.gcc_decl = decl;
	  info->prog_decl->push_back (decl_info);
	}
    }

  FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
    {
      // local declarations
      unsigned i;
      tree decl;
      struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
      FOR_EACH_LOCAL_DECL (fn, i, decl)
	{
	  tree type = base_type_of (decl);
	  if (TREE_CODE (type) == RECORD_TYPE
	      && get_reorgtype_info (type, info) != NULL)
	    {
	      ProgDecl_t decl_info;
	      decl_info.gcc_decl = decl;
	      info->prog_decl->push_back (decl_info);
	    }
	}

      // parameter declarations
      tree parm;
      for (parm = DECL_ARGUMENTS (fn->decl); parm; parm = DECL_CHAIN (parm))
	{
	  tree decl = TREE_TYPE (parm);
	  tree type = base_type_of (decl);
	  if (TREE_CODE (type) == RECORD_TYPE
	      && get_reorgtype_info (type, info) != NULL)
	    {
	      ProgDecl_t decl_info;
	      decl_info.gcc_decl = decl;
	      info->prog_decl->push_back (decl_info);
	    }
	}
    }

  if (info->show_all_reorg_cands_in_detail)
    {
      fprintf (dump_file, "find_decls_and_types: Found the following types:\n");
      print_reorgs (dump_file, 2, info);
    }

  return true;
}

void
disqualify_struct_in_struct_or_union (Info_t *info)
{
  varpool_node *var;
  std::set<tree> typeset;
  FOR_EACH_VARIABLE (var)
    {
      tree decl = var->decl;
      tree base = base_type_of (decl);
      if (TREE_CODE (base) == RECORD_TYPE || TREE_CODE (base) == UNION_TYPE)
	{
	  disq_str_in_str_or_union_helper (base, &typeset, info);
	  typeset.insert (base);
	}
    }

  remove_deleted_types (info, &disqualify_struct_in_struct_or_union_debug);
}

static void
initial_reorg_debug (Info *info, ReorgType *reorg)
{
  if (info->show_delete)
    {
      print_reorg_with_msg (dump_file, reorg, 2, "No Pointer to Structure");
    }
}

static void
disqualify_struct_in_struct_or_union_debug (Info *info, ReorgType *reorg)
{
  if (info->show_delete)
    {
      print_reorg_with_msg (dump_file, reorg, 2, "Interior Struct or Union");
    }
}

static void
disq_str_in_str_or_union_helper (tree type, std::set<tree> *typeset,
				 Info_t *info)
{
  if (typeset->find (type) != typeset->end ())
    return;
  tree fld;
  for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
    {
      if (TREE_CODE (fld) == RECORD_TYPE)
	{
	  ReorgType_t *rinfo = get_reorgtype_info (fld, info);
	  if (rinfo != NULL)
	    {
	      delete_reorgtype (rinfo, info);
	    }
	  else
	    {
	      disq_str_in_str_or_union_helper (fld, typeset, info);
	      typeset->insert (fld); // might be bug here
	    }
	}
      else
	{
	  if (TREE_CODE (fld) == UNION_TYPE)
	    {
	      disq_str_in_str_or_union_helper (fld, typeset, info);
	      typeset->insert (fld); // might be bug here
	    }
	}
    }
  return;
}

static unsigned int
reorg_qualification (Info_t *info)
{
  // TBD
  // This only does a generic legality qualification and each
  // subpass does its own performance qualification.
}

void
delete_reorgtype (ReorgType_t *rt, Info_t *info)
{
  if (!rt->delete_me)
    {
      info->num_deleted++;
      rt->delete_me = true;
    }
}

void
undelete_reorgtype (ReorgType_t *rt, Info_t *info)
{
  if (rt->delete_me)
    {
      info->num_deleted--;
      rt->delete_me = false;
    }
}

void
clear_deleted_types (Info *info)
{
  info->saved_reorg_type->clear ();
}

void
restore_deleted_types (Info *info)
{
  while (!info->saved_reorg_type->empty ())
    {
      info->reorg_type->push_back (info->saved_reorg_type->back ());
      info->saved_reorg_type->pop_back ();
    }
}

void
remove_deleted_types (Info *info, ReorgFn reorg_fn)
{
  if (info->show_delete)
    {
      fprintf (dump_file, "DELETING REORG TYPES:\n");
    }

  if (info->num_deleted > 0)
    {
      // Do delete here and run reorg_fn on each
      // deleted type
      int n = info->reorg_type->size ();
      int to = 0;
      for (int from = 0; from < n; from++)
	{
	  if (!(*(info->reorg_type))[from].delete_me)
	    {
	      // Save copy of removed entry
	      (*(info->reorg_type))[from].delete_me = false;
	      info->saved_reorg_type->push_back ((*(info->reorg_type))[from]);

	      // Delete by not copying deleted elements
	      if (from != to)
		{
		  if (reorg_fn != NULL)
		    {
		      (*reorg_fn) (info, &(*(info->reorg_type))[from]);
		    }
		  (*(info->reorg_type))[to] = (*(info->reorg_type))[from];
		}
	      to++;
	    }
	}
      info->reorg_type->resize (n - info->num_deleted);
      info->num_deleted = 0;
    }
}

static tree
base_type_of (tree type)
{
  for (; POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE
	 || TREE_CODE (type) == VAR_DECL;
       type = TREE_TYPE (type))
    ;
  return type;
}

// There are other allocations such as alloca or
// aligned allocs that I'm pretty sure are not
// a good fit for structure reorg optimization.
// Also, we do handle realloc but it doesn't
// create a new pool of memory so we ignore it here.
static bool
is_reorg_alloc_trigger (gimple *stmt)
{
  return gimple_call_builtin_p (stmt, BUILT_IN_MALLOC)
	 || gimple_call_builtin_p (stmt, BUILT_IN_CALLOC);
}

static ReorgType_t *
find_struct_type_ptr_to_struct (tree type, Info *info)
{
  if (!POINTER_TYPE_P (type))
    {
      return NULL;
    }
  for (; POINTER_TYPE_P (type); type = TREE_TYPE (type))
    ;

  if (TREE_CODE (type) == RECORD_TYPE)
    {
      return get_reorgtype_info (type, info);
    }
  return NULL;
}

// TBD Garbage just so it will compile
// What's dicey about this is it may sort of work but then I
// can see places where it wouldn't... The language has a say
// in what types are equal so maybe language hooks are involved???
bool
same_type_p (tree a, tree b)
{
  // DEBUG( "same_type_p:\n");
  // DEBUG( " a: TREE_CODE = %s, name = %p\n
  // ",code_str(TREE_CODE(a)),TYPE_NAME(a)); DEBUG_F( print_generic_expr, stderr,
  // a, (dump_flags_t)-1); DEBUG( "\n b TREE_CODE = %s, name = %p\n
  // ",code_str(TREE_CODE(b)),TYPE_NAME(b)); DEBUG_F( print_generic_expr, stderr,
  // b, (dump_flags_t)-1); DEBUG( "\n");
  gcc_assert (TREE_CODE (a) == RECORD_TYPE && TYPE_NAME (a) != 0);
  gcc_assert (TREE_CODE (b) == RECORD_TYPE && TYPE_NAME (b) != 0);
  return TYPE_NAME (a) == TYPE_NAME (b);
}

// May need to add secondary map container to
// look them up or even modify the container
// type of ReorgType
static ReorgType_t *
get_reorgtype_info (tree type, Info_t *info)
{
  // Note, I'm going to use the most stupid and slowest possible way
  // to do this. The advanage is it will be super easy and almost
  // certainly correct. It will also almost certainly need to be
  // improved but I get something out there now.
  for (std::vector<ReorgType_t>::iterator ri = info->reorg_type->begin ();
       ri != info->reorg_type->end (); ri++)
    {
      // TBD the internal docs lie and same_type_p doesn't exist
      // (at least it's not available here at LTO time)
      // so this is just a place holder until I can get an answer
      // from the gcc community. Note, this is a big issue.
      // Remember, the same_type_p here is my own temporary hack.
      if (same_type_p (ri->gcc_type, type))
	{
	  return &(*ri);
	}
    }
  return NULL;
}

static void
print_reorg_with_msg (FILE *file, ReorgType_t *reorg, int leading_space,
		      const char *msg)
{
  fprintf (file, "%*s%s:\n", leading_space, "", msg);
  print_reorg (file, leading_space + 2, reorg);
}

static void
dump_reorg (ReorgType_t *reorg)
{
  print_reorg (stderr, 0, reorg);
}

static void
print_reorgs (FILE *file, int leading_space, Info *info)
{
  for (int i = 0; i < info->reorg_type->size (); i++)
    {
      print_reorg (file, leading_space, &(*(info->reorg_type))[i]);
    }
}

static void
print_reorg (FILE *file, int leading_space, ReorgType_t *reorg)
{
  // TBD
  // note if reorg_perf & regular_perf are nonzero
  // synthesize and display absolute_effect & raw_effect

  // Note, the following is a stub.
  const char *text
    = identifier_to_locale (IDENTIFIER_POINTER (TYPE_NAME (reorg->gcc_type)));
  fprintf (file, "%*s{ type:%s, id:%d, ... }\n", leading_space, "", text,
	   reorg->id);
}

//-- debugging only --
static const char *
code_str (enum tree_code tc)
{
  switch (tc)
    {
    case POINTER_TYPE:
      return "POINTER_TYPE";
    case RECORD_TYPE:
      return "RECORD_TYPE";
    case UNION_TYPE:
      return "UNION_TYPE";
    case ARRAY_TYPE:
      return "ARRAY_TYPE";
    case REFERENCE_TYPE:
      return "REFERENCE_TYPE";
    case VAR_DECL:
      return "VAR_DECL";
    case TYPE_DECL:
      return "TYPE_DECL";
    case CONST_DECL:
      return "CONST_DECL";
    default:
      switch (TREE_CODE_CLASS (tc))
	{
	case tcc_type:
	  return "class type";
	case tcc_declaration:
	  return "class declaration";
	case tcc_reference:
	  return "class reference";
	default:
	  return "unknown class";
	}
    }
}

//---------------- Pass Control Follows ----------------

const pass_data pass_data_ipa_structure_reorg = {
  SIMPLE_IPA_PASS,   /* type */
  "structure-reorg", /* name */
  OPTGROUP_NONE,     /* optinfo_flags */
  // TV_IPA_SRA, /* tv_id */ // TBD
  TV_IPA_STRUCTURE_REORG, /* tv_id */
  0,			  /* properties_required */
  0,			  /* properties_provided */
  0,			  /* properties_destroyed */
  0,			  /* todo_flags_start */
  //( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */ // ???
  (TODO_update_ssa | TODO_update_address_taken | TODO_cleanup_cfg
   | TODO_remove_unused_locals),
  /* todo_flags_finish */ // ???
};

class pass_ipa_structure_reorg : public simple_ipa_opt_pass
{
public:
  pass_ipa_structure_reorg (gcc::context *ctxt)
    : simple_ipa_opt_pass (pass_data_ipa_structure_reorg, ctxt)
  {}

  /* opt_pass methods: */
  virtual bool gate (function *)
  {
    return ((flag_ipa_structure_reorg || flag_ipa_instance_interleave
	     || flag_ipa_field_reorder || flag_ipa_dead_field_eliminate));
  }

  virtual unsigned int execute (function *) { return ipa_structure_reorg (); }

}; // class ipa_structure_reorg

// ipa_opt_pass_d *
simple_ipa_opt_pass *
make_pass_ipa_structure_reorg (gcc::context *ctxt)
{
  return new pass_ipa_structure_reorg (ctxt);
}