summaryrefslogtreecommitdiff
path: root/gcc/ipa-str-reorg-instance-interleave.c
blob: d1d40ce9f3adb909868ce977e721b2917c7050c6 (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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
/* 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"
#include "stringpool.h"
#include "stor-layout.h"
#include "diagnostic-core.h"
#include "ssa.h"
#include "tree-ssanames.h"
#include "cfghooks.h"
#include "function.h"

static void wrangle_ssa_type( tree, Info_t*);
static bool print_internals (gimple *, void *);
static void str_reorg_instance_interleave_qual_part ( Info *);
static void str_reorg_instance_interleave_type_part ( Info *);
static void create_new_types ( Info_t *);
static void create_a_new_type ( Info_t *, tree);
static unsigned int reorg_perf_qual ( Info *);
static tree find_coresponding_field ( tree, tree);

// These are local to this file by design
#define REORG_SP_PTR_PREFIX "_reorg_SP_ptr_type_"
#define REORG_SP_PREFIX "_reorg_base_type_"
#define REORG_SP_BASE_PREFIX "_reorg_base_var_"
  
int
str_reorg_instance_interleave_qual ( Info *info)
{
  // this is the qualification code for instance interleaving
  //
  str_reorg_instance_interleave_qual_part ( info);

  // this modifiies the qualified types.
  //
  str_reorg_instance_interleave_type_part ( info);
  return 0;
}

int
str_reorg_instance_interleave_trans ( Info *info)
{
  if ( info->show_all_reorg_cands )
  {
    fprintf ( info->reorg_dump_file, "Start of str_reorg_instance_interleave_trans:\n");
    print_program ( info->reorg_dump_file, 4);
  }
  
  struct cgraph_node *node;
  FOR_EACH_FUNCTION_WITH_GIMPLE_BODY ( node)  {
    struct function *func = DECL_STRUCT_FUNCTION ( node->decl);
    // Ulgy GCC idiom with global pointer to current function.
    push_cfun ( func);
    if ( info->show_transforms )
      {
        fprintf( info->reorg_dump_file, "Function \"%s\":\n",
		 //IDENTIFIER_POINTER( DECL_NAME( func)));
		 //IDENTIFIER_POINTER( DECL_NAME( func->decl)));
		 lang_hooks.decl_printable_name ( node->decl, 2));
      }

    basic_block bb;
    FOR_EACH_BB_FN ( bb, func)
      {

	if( info->show_transforms )
	  {
	    fprintf( info->reorg_dump_file, "  Transforming BB%i:\n",
		     bb->index);
	  }

	gimple_stmt_iterator outer_gsi;
	gimple_stmt_iterator next_gsi;
	for ( outer_gsi = gsi_start_bb ( bb); !gsi_end_p ( outer_gsi); outer_gsi = next_gsi )
	  {
	    next_gsi = outer_gsi;
	    gsi_next ( &next_gsi);
	    // Every statement that uses a reorg type needs to
	    // be examined. Some are harmless and are skipped
	    // whereas others are transformed. However, anything
	    // else is an error.
	    gimple *stmt = gsi_stmt ( outer_gsi);
	    ReorgType_t *ri = contains_a_reorgtype( stmt, info);
	    if ( ri == NULL )
	      {
		DEBUG_L("No Transfrom on: ");
		DEBUG_F( print_gimple_stmt, stderr, stmt, 4, TDF_SLIM);
	      }
	    else
	      {
		DEBUG_F( print_reorg_with_msg, stderr, ri, 0,
			 "reorg from str_reorg_instance_interleave_trans");
		
		enum ReorgTransformation trans = 
		  reorg_recognize ( stmt, node, info);
		// print out trans and stmt if dumping
		if ( info->show_transforms )
		  {
		    print_gimple_stmt( info->reorg_dump_file, stmt, 0);
		  }
	    
		switch ( trans)
		  { 
		  case ReorgT_StrAssign:
		    DEBUG_L("ReorgT_StrAssign\n");
		    // TBD
		    /*
	      tree lhs = gimple_assign_lhs( stmt);
	      tree rhs = gimple_assign_rhs( stmt);
	      ReorgOpTrans lope = recognize_op( lhs, info);
	      ReorgOpTrans rope = recognize_op( rhs, info);
	      for each field in ri {
	        // lhs: ReorgT_Array & rhs ReorgT_Struct, ReorgT_Deref, ReorgT_Array
		// lhs: ReorgT_Struct & rhs ReorgT_Deref, ReorgT_Array
		// lhs ReorgT_Deref & rhs ReorgT_Struct, ReorgT_Array, ReorgT_Deref
		A is new ssa
		// Gimple for loading this element
		// Question? What if the element is large? Answer is it's OK.
		switch( rope) {
		// Not implemented in single pool
		//case ReorgT_Array:
		case ReorgT_Struct:
		  generate A <- rhs.field  
		  break;
		case ReorgT_Deref:
		  B,C is new SSA
		  // Note simplification with type_name( rhs)
		  generate B <- concat( REORG_SP_PREFIX, type_name( rhs))
		    and insert before stmt
		  generate C <- B->"f"
		    and insert before stmt
		  generate A <- C[rhs]
		    and insert before stmt
		  break
		default:
		  internal_error(
		    "Reached operand default in RHS enum ReorgOpTrans");
		}
		// Gimple for storing this element
		switch( lope)
		// Not implemented in single pool
		//case ReorgT_Array:
		case ReorgT_Deref:
		  B,C is new SSA
		  // Note simplification with type_name( lhs)
		  generate B <- concat( REORG_SP_PREFIX, type_name( lhs))
		    and insert before stmt
		  generate C <- B->"f"
		    and insert before stmt
		  // lhs here is a simplification
		  generate A <- C[lhs]
		    and insert before stmt
		  break;
		case ReorgT_Struct:
		  generate lhs.field <- A
		  break;
		default:
		  internal_error(
		    "Reached operand default in LHS enum ReorgOpTrans");
		}
	     }
		    */
		    break;
		  case ReorgT_ElemAssign:
		    {
		      //break;
		      gimple_stmt_iterator gsi = gsi_for_stmt( stmt);
		      
		      DEBUG_L("ReorgT_ElemAssign: ");
		      DEBUG_F( print_gimple_stmt, stderr, stmt, 0);
		      DEBUG("\n");
		      INDENT(2);
		      // Needed for helloworld
		      tree lhs = gimple_assign_lhs( stmt);
		      tree rhs = gimple_assign_rhs1( stmt);

		      bool ro_on_left = tree_contains_a_reorgtype_p ( lhs, info);
		      
		      tree ro_side = ro_on_left ? lhs : rhs;
		      tree nonro_side = ro_on_left ? rhs : lhs;
		      
		      switch ( recognize_op ( ro_side, info) )  // "a->f"
			{
			case ReorgOpT_Indirect:
			  {
			    tree orig_field = TREE_OPERAND( ro_side, 1);
			    tree field_type = TREE_TYPE( orig_field);
			    tree base = ri->instance_interleave.base;
			    
			    tree base_field =
			      find_coresponding_field ( base, orig_field);
			    
			    tree base_field_type = TREE_TYPE( base_field);

			    tree field_val_temp =
			      make_temp_ssa_name( field_type, NULL, "field_val_temp");

			    tree inner_op = TREE_OPERAND( ro_side, 0);
			    inner_op = TREE_OPERAND( inner_op, 0);
			    DEBUG_L("inner_op: ");
			    DEBUG_F( print_generic_expr, stderr, inner_op, (dump_flags_t)0);
			    DEBUG("\n");
			    
			    // For either case generate common code:
			    
			    // field_array = _base.f
			    tree field_arry_addr =
			      make_temp_ssa_name( base_field_type, NULL, "field_arry_addr");

			    tree rhs_faa = build3 ( COMPONENT_REF,
						    // ???
						    //base_field_type, 
						    ptr_type_node, // This seems bogus
						    base,
						    //base_field,
						    // This almost certainly is bogus
						    // If this "works" the the types
						    // of fields are messed up.
						    orig_field,
						    NULL_TREE);

		            // Use this to access the array of element.
			    gimple *get_field_arry_addr =
			      gimple_build_assign( field_arry_addr, rhs_faa);
			    SSA_NAME_DEF_STMT ( field_arry_addr) = get_field_arry_addr;

			    // index = a
			    tree index =
			      make_temp_ssa_name( ri->pointer_rep, NULL, "index");
			    gimple *get_index =
			      gimple_build_assign( index, inner_op);
			    SSA_NAME_DEF_STMT ( index) = get_index;

			    gimple *temp_set;
			    gimple *final_set;

			    // offset = index * size_of_field
			    tree size_of_field = TYPE_SIZE_UNIT ( base_field_type);
			    tree offset = make_temp_ssa_name( sizetype, NULL, "offset"); // TBD sizetype ???

			    gimple *get_offset = gimple_build_assign ( offset, MULT_EXPR, index, size_of_field);
			    SSA_NAME_DEF_STMT ( offset) = get_offset;

			    // field_addr = field_array + offset
			    // bug fix here (TBD) type must be *double not double
			    tree field_addr =
			      make_temp_ssa_name( base_field_type, NULL, "field_addr");

			    gimple *get_field_addr = 
			      gimple_build_assign ( field_addr, PLUS_EXPR, field_arry_addr, offset);
			    SSA_NAME_DEF_STMT ( field_addr) = get_field_addr;

			    if ( ro_on_left )
			      {
				// With:    a->f = rhs
				// Generate:
				
				//           temp = rhs
				temp_set = gimple_build_assign( field_val_temp, rhs);
				SSA_NAME_DEF_STMT ( field_val_temp) = temp_set;
				
				////           field_array[index] = temp
				//tree elem_to_set =
				//  build4 ( ARRAY_REF, field_type, field_arry_addr, index,
				//	   NULL_TREE, NULL_TREE);
				//final_set =
				//  gimple_build_assign( elem_to_set, field_val_temp);

				//                 *field_addr = temp
				tree lhs_ref = build2 ( MEM_REF, field_type, field_addr,
							build_int_cst (ptr_type_node, 0));
				
				final_set =
				  gimple_build_assign( lhs_ref, field_val_temp);
			      }
			    else
			      {
				// With:    lhs = a->f
				// Generate:
				
				//          temp = *field_addr
				// INDIRECT_REF is not supported fot GIMPLE... sigh...
				// tree rhs_ref = build1 ( INDIRECT_REF, field_type, field_addr);

				// This doesn't work
				//tree rhs_ref = build2 ( MEM_REF,
				//			base_field_type, base_field, offset);
				
				// Niether does this it dies on the offset
				//tree rhs_ref = build2 ( MEM_REF,
				//			field_type, base_field, offset);

				// This idiom seems to work (WTF?!)
				tree rhs_ref = build2 ( MEM_REF, field_type, field_addr,
							build_int_cst (ptr_type_node, 0));
				

				// If these will actually print then things are likely sane
				//DEBUG_L("rhs_ref: ");
				//DEBUG_F(print_generic_expr, stderr, rhs_ref, (dump_flags_t)0);
				//DEBUG("\n");
				
				tree op0 = TREE_OPERAND ( rhs_ref, 0);
				tree op1 = TREE_OPERAND ( rhs_ref, 1);
				tree op1type = TYPE_MAIN_VARIANT (TREE_TYPE (op1));
				tree op1type_type = TREE_TYPE ( op1type);
				DEBUG_L("op0: ");
				DEBUG_F(print_generic_expr, stderr, op0, (dump_flags_t)0);
				DEBUG("\n")
				DEBUG_L("op1: ");
				DEBUG_F(print_generic_expr, stderr, op1, (dump_flags_t)0);
				DEBUG("\n")
				DEBUG_L("op1type: ");
				DEBUG_F(print_generic_expr, stderr, op1type, (dump_flags_t)0);
				DEBUG("\n");
				DEBUG_L("op1type_type: %p\n", op1type_type);
				DEBUG_L("op1type_type: ");
				DEBUG_F(print_generic_expr, stderr, op1type_type, (dump_flags_t)0);
				DEBUG("\n")
				
				temp_set =
				  gimple_build_assign( field_val_temp, rhs_ref);
				SSA_NAME_DEF_STMT ( field_val_temp) = temp_set;
				
				//          lhs = temp
				final_set = gimple_build_assign( lhs, field_val_temp);
				SSA_NAME_DEF_STMT ( lhs) = final_set;
			      }
			    
			    DEBUG_L("get_field_arry_addr: ");
			    DEBUG_F( print_gimple_stmt, stderr, get_field_arry_addr, 0);
			    DEBUG("\n");
			    
			    DEBUG_L("get_index: ");
			    DEBUG_F( print_gimple_stmt, stderr, get_index, 0);
			    DEBUG("\n");

			    DEBUG_L("get_offset: ");
			    DEBUG_F( print_gimple_stmt, stderr, get_offset, 0);
			    DEBUG("\n");

			    DEBUG_L("get_field_addr: ");
			    DEBUG_F( print_gimple_stmt, stderr, get_field_addr, 0);
			    DEBUG("\n");
			    
			    DEBUG_L("temp_set: ");
			    DEBUG_F( print_gimple_stmt, stderr, temp_set, 0);
			    DEBUG("\n");
			    
			    DEBUG_L("final_set: ");
			    DEBUG_F( print_gimple_stmt, stderr, final_set, 0);
			    DEBUG("\n");

			    gsi_insert_before( &gsi, get_field_arry_addr, GSI_SAME_STMT);
			    gsi_insert_before( &gsi, get_index, GSI_SAME_STMT);
			    gsi_insert_before( &gsi, get_offset, GSI_SAME_STMT);
			    gsi_insert_before( &gsi, get_field_addr, GSI_SAME_STMT);
			    gsi_insert_before( &gsi, temp_set, GSI_SAME_STMT); // << malformed???
			    gsi_insert_before( &gsi, final_set, GSI_SAME_STMT);
			    
			      
			    //delete stmt
			    gsi_remove ( &gsi, true);
			  } // end ReorgOpT_Indirect case
			  break;
			case ReorgOpT_AryDir:  // "x[i].f"
			  // Not implemented in single pool
			  internal_error ( "ReorgOpT_AryDir not possible");
			default:
			  internal_error (
					  "Reached operand default for ReorgOpT_Indirect");
			  
			} // end recognize_op ( rhs, info) switch

		      INDENT(-2);
		    } // end ReorgT_ElemAssign case
		    break;
		  case ReorgT_If_Null:
		  case ReorgT_If_NotNull:
		    DEBUG_L("ReorgT_If_(Not)Null\n");
		    /*
	      gimple_cond_set_rhs( stmt, 
	        TYPE_MIN_VALUE( pointer_sized_int_node));
		    */
		    break;
		  case ReorgT_IfPtrEQ:
		  case ReorgT_IfPtrNE:
		  case ReorgT_IfPtrLT:
		  case ReorgT_IfPtrGT:
		  case ReorgT_IfPtrLE:
		  case ReorgT_IfPtrGE:
		    DEBUG_L("ReorgT_IfPtr*\n");
		    // Not needed for single pool.
		    break;
		  case ReorgT_PtrPlusInt:   // "a = b + i"
		    {
		      DEBUG_L("ReorgT_PtrPlusInt: ");
		      DEBUG_F( print_gimple_stmt, stderr, stmt, 0);
		      // Needed for hellowotrld
		      
		      // Does the type of stmt need to be adjusted? I assume so.
		      // The ReorgType contains the type of the pointer
		      // if so that should probably be used. Note, the variables
		      // should all be of the correct type (but maybe that's
		      // not reflected here. Punting and assigning the types to
		      // the type of pointer_sized_int_node is probably not correct
		      // even though that's the representation.
		      
		      tree type = ri->pointer_rep;
		      tree tmp =  make_temp_ssa_name( type, NULL, "PtrPlusInt");
		      tree str_siz =
			build_int_cst ( type, int_cst_value ( TYPE_SIZE_UNIT (ri->gcc_type)));
		      tree rhs2 = gimple_assign_rhs2( stmt);
		      gimple *adjust_stmt =
			gimple_build_assign ( tmp, TRUNC_DIV_EXPR, rhs2, str_siz);
		      SSA_NAME_DEF_STMT ( tmp) = adjust_stmt;
		      // Note, gimple_set_op is used in limited places so using it
		      // to modify existed code might be problematic.
		      gimple_set_op( stmt, 2, tmp);
		      gimple_stmt_iterator gsi = gsi_for_stmt( stmt);
		      gsi_insert_before( &gsi, adjust_stmt, GSI_SAME_STMT);
		      
		      DEBUG_L("");
		      DEBUG_F( print_gimple_stmt, stderr, adjust_stmt, 0);
		      DEBUG_L("");
		      DEBUG_F( print_gimple_stmt, stderr, stmt, 0);
		      
		    }
		    break;
		  case ReorgT_Ptr2Zero:   //  "a = 0"
		    DEBUG_L("ReorgT_Ptr2Zero\n");
		    /*
		    // Note, this is way too simple... just saying.
		    gimple_set_op( stmt, 1, 
		    TYPE_MIN_VALUE( pointer_sized_int_node));
		    */
		    break;
		  case ReorgT_PtrDiff:    //  "i = a - b"
		    DEBUG_L("ReorgT_PtrDiff\n");
		    // Do nothing in the single pool case.
		    break;
		  case ReorgT_Adr2Ptr:    //  "a = &x[i]"
		    DEBUG_L("ReorgT_Adr2Ptr\n");
		    /*
	      tree *add_stmt = 
	        gimple_build_assign( 
        	  gimple_assign_lhs( stmt);, 
		  PLUS_EXPR, 
		  gimple_assign_rhs1( stmt), 
		  gimple_assign_rhs2( stmt), 
		  NULL_TREE, NULL_TREE);
	      gimple_stmt_iterator gsi = gsi_for_stmt( stmt);
	      gsi_insert_before( gsi, add_stmt, GSI_SAME_STMT);
	      // delete stmt
	      gsi_remove( gsi, true);
		    */
		    break;
		  case ReorgT_PtrNull:     //  "x = a == 0"
		  case ReorgT_PtrNotNull:  //  "x = a != 0"
		    DEBUG_L("ReorgT_Ptr(Not)Null\n");
		    /*
	      gimple_set_op( stmt, 2, 
	      		     TYPE_MIN_VALUE( pointer_sized_int_node));
		    */
		    break;
		  case ReorgT_PtrEQ:       //  "i = a == b"
		  case ReorgT_PtrNE:       //  "i = a != b"
		  case ReorgT_PtrLT:       //  "i = a < b"
		  case ReorgT_PtrLE:       //  "i = a <= b"
		  case ReorgT_PtrGT:       //  "i = a > b"
		  case ReorgT_PtrGE:       //  "i = a >= b"
		    DEBUG_L("ReorgT_Ptr*\n");
		    // Not needed for single pool.
		    break;
		  case ReorgT_Malloc:
		    #if 1
		    {
		      DEBUG_L("Transform ReorgT_Malloc\n");
		      INDENT(2);
		      // Note, unlike other simpler transformations,
		      // this must build new basic blocks to add new
		      // gimple to and use a phi for the final result.
		      // See appendix on malloc transformation for
		      // each comment starting with "FROM."
		      ReorgType_t *ri = contains_a_reorgtype( stmt, info);
		      // FROM len = val/size
		      tree arg = gimple_call_arg( stmt, 0);
		      // TBD: len is new SSA
		      tree val = gimple_call_lhs( stmt);
		      //DEBUG_L("val is: ");
		      //DEBUG_F( print_generic_expr, stderr, val, (dump_flags_t)-1);
		      //DEBUG(", tree code type: %s\n", code_str(TREE_CODE(TREE_TYPE(val))));
		      //gcc_assert( TREE_CODE( TREE_TYPE(val)) == INDIRECT_REF);
		      gcc_assert( TREE_CODE( TREE_TYPE(val)) == POINTER_TYPE);
		      tree size = TYPE_SIZE_UNIT( TREE_TYPE( TREE_TYPE( val)));
		      // FROM len = val/size (insert before stmt) <== maybe arg/size
		      //tree len = make_temp_ssa_name( sizetype, NULL, "fail_val");
		      // The above segfaulted ??? note, it's not an idiom seen in gcc
		      tree int_ptrsize_type = signed_type_for ( ptr_type_node);
		      DEBUG_L("int_ptrsize_type = %p\n", int_ptrsize_type);
		      tree len = make_temp_ssa_name ( int_ptrsize_type, NULL, "malloc_len");
		      gimple_stmt_iterator gsi = gsi_for_stmt( stmt);
		      //gimple *glen = 
		      //  gimple_build_assign ( len, TRUNC_DIV_EXPR, val, size); 
		      gimple *glen = 
			gimple_build_assign ( len, TRUNC_DIV_EXPR, arg, size);
		      SSA_NAME_DEF_STMT ( len) = glen;
		      
		      gsi_insert_before( &gsi, glen, GSI_SAME_STMT);
		      // Note in other places in this doc this would
		      // be "insert glen before stmt" instead of this but
		      // here we need to create new basic blocks.
		      // FROM edge = split this block after stmt
		      edge new_edge = split_block ( bb, stmt);
		      // FROM before_bb = edge->src // same as this bb
		      basic_block before_bb = new_edge->src; // 
		      // FROM after_bb = edge->dest
		      basic_block after_bb = new_edge->dest;
		      // FROM delete edge
		      remove_edge ( new_edge);
		      // FROM prev_bb = before_bb
		      basic_block prev_bb = before_bb;
		      // FROM prev_ok_field is new label
		      tree prev_ok_field_L =
			create_artificial_label ( UNKNOWN_LOCATION);
		      // FROM after_label is new label
		      tree after_label_L =
			create_artificial_label ( UNKNOWN_LOCATION);
		      // FROM add goto for prev_ok_field to end of before_bb
		      gimple *goto_pof = gimple_build_goto ( prev_ok_field_L);
		      gsi_insert_before ( &gsi, goto_pof, GSI_SAME_STMT); // TBD insert after???
		      // FROM failure_bb = create_empty_block(prev_bb)
		      basic_block failure_bb = create_empty_bb ( prev_bb);
		      // FROM make_edge( failure_bb, after_bb, EDGE_FALLTHRU);
		      // TBD set edge probability and flags
		      edge failure_edge = make_edge ( failure_bb,
						      after_bb, EDGE_FALLTHRU);
		      // FROM bad_field is new label
		      tree bad_field_L =
			create_artificial_label ( UNKNOWN_LOCATION);
		      // FROM delete stmt
		      gsi_remove ( &gsi, true);

		      tree return_type = TREE_TYPE ( arg);
		      tree fail_val = 
			make_temp_ssa_name ( return_type, NULL, "malloc_fail_val");
		      tree field;
		      tree reorg_type = ri->gcc_type; // is this useful here?
		      tree reorg_pointer_type = ri->pointer_rep;
		      //tree base = ri->reorg_ver_type; //nopers
		      tree base = ri->instance_interleave.base;

		      // loop setup trickery for gimple idioms
		      //
		      // FROM prev_order = failure_bb
		      basic_block prev_order = failure_bb;
		      // FROM prev_bb = before_bb
		      prev_bb = before_bb;
		      
		      // Generate all the real allocation code
		      tree fndecl_malloc = builtin_decl_explicit( BUILT_IN_MALLOC);
		      // This, after the following loop, will hold the start of the
		      // field related code.
		      tree new_ok_field_L;
		      
		      // FROM (for fields) {
		      for( field = TYPE_FIELDS( reorg_type); 
			   field; 
			   field = DECL_CHAIN( field))
			{
			  // FROM res is new SSA
			  // Note, alternative code would substitute ptr_type_node
			  // for null_pointer_node.
			  // This is probably a duplicate of the def of res below.
			  //tree res = 
			  //  make_temp_ssa_name( null_pointer_node, NULL, "res");
			  // FROM new_bb = create_empty_block(prev_order);
			  basic_block new_bb = create_empty_bb ( prev_order);
			  // FROM gsi = gsi_start_bb( new_bb)
			  gimple_stmt_iterator gsi = gsi_start_bb ( new_bb);
			  // Note, switching the order of edge creation and
			  // setting dominator seems to make no difference
			  // TBD set edge probability and flags
			  make_edge ( prev_bb, new_bb, EDGE_TRUE_VALUE);
			  // TBD what happens if I punt on this????
			  //set_immediate_dominator ( CDI_DOMINATORS, new_bb, prev_bb);

			  // FROM make_edge( new_bb, failure_bb, EDGE_FALSE_VALUE);
			  // TBD set edge probability and flags
			  make_edge ( new_bb, failure_bb, EDGE_FALSE_VALUE);
			  // FROM new_ok_field is new label
			  new_ok_field_L =
			    create_artificial_label ( UNKNOWN_LOCATION);
			  // FROM gsi_insert_after( &gsi, "if( res NE NULL ) 
			  //    	            goto new_ok_field; 
			  //		    goto bad_field")
			  tree res = 
			    make_temp_ssa_name ( ptr_type_node, NULL, "res");
			  gimple *gcond =
			    gimple_build_cond ( NE_EXPR, res, null_pointer_node,
						new_ok_field_L, bad_field_L);
			  // Moved label her from end
			  gimple *gprev_ok_field = gimple_build_label ( prev_ok_field_L);
			  gsi_insert_after ( &gsi, gprev_ok_field, GSI_NEW_STMT);
			  //gsi_insert_after ( &gsi, gcond, GSI_SAME_STMT);		
			  gsi_insert_after ( &gsi, gcond, GSI_SAME_STMT);		
			  // FROM gsi_insert_after( &gsi, "base.field = res")
			  tree lhs_ass =
			    build3( COMPONENT_REF, ptr_type_node, base, field, NULL_TREE);

			  DEBUG_L("base: %p\n", base);
			  DEBUG_A("  base: ");
			  DEBUG_F(print_generic_expr, stderr, base, (dump_flags_t)0);
			  DEBUG("\n");
			  
			  DEBUG_L("field: %p\n", field);
			  DEBUG_A("  : ");
			  DEBUG_F(print_generic_expr, stderr, field, (dump_flags_t)0);
			  DEBUG("\n");

			  tree field_type = TREE_TYPE( field);
			  DEBUG_L("field_type: %p\n", field_type);
			  DEBUG_A("  : ");
			  DEBUG_F(print_generic_expr, stderr, field_type, (dump_flags_t)0);
			  DEBUG("\n");
			  
			  DEBUG_L("lhs_ass: %p\n", lhs_ass);
			  DEBUG_A("  lhs_ass: ");
			  DEBUG_F(print_generic_expr, stderr, lhs_ass, (dump_flags_t)0);
			  DEBUG("\n");
			  
			  gimple *gset_field = gimple_build_assign( lhs_ass, res);
			  
			  gsi_insert_after( &gsi, gset_field, GSI_SAME_STMT);
			  
			  // FROM gsi_insert_after( &gsi, "res = malloc( mem_size)")
			  // The alternative to sizetype are long_integer_type_node
			  // and integer_type_node.
			  tree mem_size = 
			    make_temp_ssa_name( sizetype, NULL, "malloc_mem_size");
			  gcall *malloc_call = gimple_build_call( fndecl_malloc, 1, mem_size);
			  gimple_call_set_lhs( malloc_call, res);
			  gsi_insert_after( &gsi, malloc_call, GSI_SAME_STMT);
			  // FROM gsi_insert_after( &gsi, "mem_size = len * field_size")
			  //gimple *gsize = 
			  //  gimple_build_assign ( mem_size, MULT_EXPR, TYPE_SIZE(field), 
			  //			len, NULL_TREE, NULL_TREE);
			  gimple *gsize = 
			    gimple_build_assign ( mem_size,
						  MULT_EXPR,
						  TYPE_SIZE ( TREE_TYPE ( field)), 
						  len);
			  SSA_NAME_DEF_STMT ( mem_size) = gsize;
			  
			  gsi_insert_after( &gsi, gsize, GSI_SAME_STMT);
			  // Moved label to top
			  //// FROM gsi_insert_after( &gsi, prev_ok_field)
			  //gimple *gprev_ok_field = gimple_build_label ( prev_ok_field_L);
			  //gsi_insert_after ( &gsi, gprev_ok_field, GSI_SAME_STMT);
			  // FROM prev_bb = new_bb
			  prev_bb = new_bb;
			  // FROM prev_order = new_bb
			  prev_order = new_bb;
			  // FROM prev_ok_field = new_ok_field
			  prev_ok_field_L = new_ok_field_L;
			}
		      
		      // create basic block for success
		      //
		      // FROM success_bb = create_empty_block(prev_bb_order);
		      basic_block success_bb = create_empty_bb ( prev_bb);
		      // FROM set imm dom success_bb as prev_bb
		      // TBD let's punt of the dominators for a bit
		      //set_immediate_dominator ( CDI_DOMINATORS, success_bb, prev_bb);
		      // FROM make_edge( prev_bb, success_bb, EDGE_TRUE_VALUE);
		      // TBD set edge probability and flags
		      make_edge ( prev_bb, success_bb, EDGE_TRUE_VALUE);
		      // FROM make_edge( success_bb, after_bb, EDGE_TRUE_VALUE);
		      edge success_edge = make_edge ( success_bb, after_bb, EDGE_TRUE_VALUE);
		      
		      // code in success_bb
		      //
		      // FROM success_val is new SSA
		      tree success_val = 
			make_temp_ssa_name( reorg_pointer_type, NULL, "malloc_success_val");
		      // FROM gsi = gsi_start_bb( failure_bb)
		      // Reuse the same gsi??? Or create a new one???
		      //gimple_stmt_iterator gsi = gsi_start_bb ( failure_bb);
		      gsi = gsi_start_bb ( success_bb);  // used to be failure_bb
		      // FROM gsi_insert_after( &gsi, "goto after_label")
		      //gimple *goto_al = gimple_build_goto( after_label_L);
		      // Emit the label first and then everything else after it
		      gimple *gnew_ok_field = gimple_build_label ( new_ok_field_L);
		      gsi_insert_after ( &gsi, gnew_ok_field, GSI_NEW_STMT);
		      gimple *goto_al_succ = gimple_build_goto( after_label_L);
		      gsi_insert_after( &gsi, goto_al_succ, GSI_SAME_STMT);
		      // FROM gsi_insert_after( &gsi, "success_val = 0")
		      gimple *set_succ =
			gimple_build_assign ( success_val,
					      build_int_cst ( reorg_pointer_type, 0));
		      SSA_NAME_DEF_STMT ( success_val) = set_succ;
		      
		      gsi_insert_after( &gsi, set_succ, GSI_SAME_STMT);
		      // FROM gsi_insert_after( &gsi, new_ok_field )
		      //gimple *gnew_ok_field = gimple_build_label ( new_ok_field_L);
		      //gsi_insert_after ( &gsi, gnew_ok_field, GSI_SAME_STMT);
		      
		      // add code to after_bb
		      //
		      // FROM gsi = gsi_start_bb( after_bb)
		      // Reuse gsi
		      //gimple_stmt_iterator gsi = gsi_start_bb( after_bb);
		      gsi = gsi_start_bb( after_bb);

		      // put the label first
		      // FROM gsi_insert_after( &gsi, after_label)
		      gimple *gafter_label = gimple_build_label( after_label_L);
		      gsi_insert_after( &gsi, gafter_label, GSI_NEW_STMT);

		      // FROM gsi_insert_after( &gsi, "lhs = "phi(success_val, fail_val)
		      gphi *der_phi = create_phi_node( val, after_bb); // was lhs?? instead of val
		      add_phi_arg( der_phi, success_val, success_edge, UNKNOWN_LOCATION);
		      add_phi_arg( der_phi, fail_val, failure_edge, UNKNOWN_LOCATION);
		      gsi_insert_after( &gsi, der_phi, GSI_SAME_STMT);

		      //// FROM gsi_insert_after( &gsi, after_label)
		      //gimple *gafter_label = gimple_build_label( after_label_L);
		      //gsi_insert_after( &gsi, gafter_label, GSI_SAME_STMT);


		      // Try failure_bb code here
		      #if 1
		      // if defed on here... moved this
		      // code in failure_bb
		      //
		      // FROM fail_val is new SSA
		      //tree return_type = TREE_TYPE ( arg);
		      //tree fail_val = 
		      //  make_temp_ssa_name ( return_type, NULL, "fail_val");
		      // FROM gsi = gsi_start_bb ( failure_bb)
		      gsi = gsi_start_bb ( failure_bb);

		      // Put the label first
		      // FROM gsi_insert_after( &gsi, bad_field )
		      gimple *gbad_field = gimple_build_label( bad_field_L);
		      gsi_insert_after( &gsi, gbad_field, GSI_NEW_STMT);
		      
		      // FROM gsi_insert_after ( &gsi, "goto after_label")
		      gimple *goto_al = gimple_build_goto ( after_label_L);
		      gsi_insert_after ( &gsi, goto_al, GSI_SAME_STMT);
		      // (per field) {
		      //tree field; // defined above
		      //tree reorg_type = ri->gcc_type; // is this useful here?
		      //tree reorg_pointer_type = ri->pointer_rep;
		      tree fndecl_free = builtin_decl_explicit( BUILT_IN_FREE);
		      //tree base = ri->reorg_ver_type;
		      for( field = TYPE_FIELDS( reorg_type); 
			   field; 
			   field = DECL_CHAIN( field)) {
			// FROM gsi_insert_after( &gsi, "base.field = 0")
			tree lhs_ass =
			  build3( COMPONENT_REF, ptr_type_node, base, field, NULL_TREE);
			gimple *gzero = gimple_build_assign( lhs_ass, null_pointer_node);
			
			gsi_insert_after( &gsi, gzero, GSI_SAME_STMT);
			
			// FROM gsi_insert_after( &gsi, "free(field)")
			// TBD -- I'm 
			tree to_free = 
			  make_temp_ssa_name( reorg_pointer_type, NULL, "malloc_to_free");
			gcall *free_call = gimple_build_call( fndecl_free, 1, to_free);
			gsi_insert_after( &gsi, free_call, GSI_SAME_STMT);
			tree rhs_ass =
			  build3( COMPONENT_REF, ptr_type_node, base, field, NULL_TREE);
			gimple *gaddr2free = gimple_build_assign( to_free, rhs_ass);
			SSA_NAME_DEF_STMT ( to_free) = gaddr2free;
			
			gsi_insert_after( &gsi, gaddr2free, GSI_SAME_STMT);
		      }
		      // FROM gsi_insert_after( &gsi, "fail_val = minint")
		      gimple *gretnull =
			gimple_build_assign ( fail_val,
					      //build_int_cst ( size_type_node,
					      TYPE_MIN_VALUE ( TREE_TYPE ( fail_val))); // );
		      SSA_NAME_DEF_STMT ( fail_val) = gretnull;
		      
		      gsi_insert_after( &gsi, gretnull, GSI_SAME_STMT);
		      
		      //// FROM gsi_insert_after( &gsi, bad_field )
		      //gimple *gbad_field = gimple_build_label( bad_field_L);
		      //gsi_insert_after( &gsi, gbad_field, GSI_SAME_STMT);
		      #endif

		      //DEBUG_L("End of malloc:\n");
		      //DEBUG_F( print_program, stderr, 4);
		    }
                    #endif
		    INDENT(-2);
		    break;
		  case ReorgT_Calloc:
		    DEBUG_L("ReorgT_Calloc\n");
		    /*
		    // This used to be almost a clone of the old version of
		    // the malloc code above and needs to transformed just like
		    // what was done above to malloc.
		    tree arg = gimple_call_arg( stmt, 0);
		    len is new SSA
		      tree val = gimple_call_lhs( stmt);
		    gcc_assert( TREE_CODE( TREE_TYPE(val)) == INDIRECT_REF);
		    tree size = TYPE_SIZE_UNIT( TREE_TYPE( TREE_TYPE( val)));
		    gimple *glen = 
		      gimple_build_assign( 
					  len, 
					  TRUNC_DIV_EXPR, 
					  val, 
					  size,
					  NULL_TREE, NULL_TREE);
		    //insert glen before stmt
		    gimple_stmt_iterator stmt_gsi = gsi_for_stmt ( stmt);
		    gsi_link_before( stmt_gsi, glen, GSI_SAME_STMT);
		    tree lfial = create_artificial_label( UNKNOWN_LOCATION);
		    gimple *gfail = gimple_build_label( lfail);
		    tree lnotfial = create_artificial_label( UNKNOWN_LOCATION);
		    gimple *gnotfail = gimple_build_label( lnotfail);
		    tree base = ri->reorg_ver_type;
		    for (each element of base) // TBD <==
		      {
			// call malloc
			tree lok = create_artificial_label( UNKNOWN_LOCATION);
			gimple *glok = gimple_build_label( lok);
			tree *fndecl = builtin_decl_explicit( BUILT_IN_MALLOC);
			mem_size is new SSA
			  gimple *gsize = 
			  gimple_build_assign( 
					      mem_size, 
					      MULT_EXPR, 
					      TYPE_SIZE(element), 
					      len,
					      NULL_TREE, NULL_TREE);
			insert gsize before stmt
			  gcall *call = gimple_build_call( fndecl, 1, mem_size);
			mres is new SSA
			  gimple_call_set_lhs( call, mres)
			  insert call before stmt
			  // Set element to return value of malloc.
			  // Note, the devil is in the details here.
			  gen concat( REORG_SP_PREFIX,
				      type_name( lhs)  ).element <- mres
			  and insert before stmt
			  // gen test of return
			  gimple *gcond =
			  gimple_build_cond( EQ_EXPR, mres,
					     null_pointer_node, lfail, lok);
			insert gcond before stmt
			  insert glok before stmt
			  // call memset
			  fndecl = builtin_decl_explicit( BUILT_IN_MEMSET);
			call =
			  gimple_build_call( fndecl, 3, mres, int_node_zero, mem_size);
			insert call before stmt
			  }
	      
		    // fake return value of zero
		    gimple *gretzero =
		      gimple_build_assign( lhs,
					   build_int_cst(
							 TYPE_MIN_VALUE( TREE_TYPE(lhs)), 0));
		    insert gretzero before stmt
		      gimple *ggoto = gimple_build_goto( lnotfail);
		    insert ggoto before stmt
		      insert glab1 before stmt
		      for each element of base {
			  tree fndecl = builtin_decl_explicit( BUILT_IN_FREE);
			  gcall *call = gimple_build_call( fndecl, 1, element);
			  insert call before stmt
			    set element to null
			    }
		    // fake return value of null
		    gimple *gretnull =
		      gimple_build_assign( lhs,
					   build_int_cst(
							 TYPE_MIN_VALUE( TREE_TYPE(lhs))));
		    insert gretnull before stmt
		      insert gnotfail before stmt
		      delete stmt
		      */
		    break;
		  case ReorgT_Realloc:
		    DEBUG_L("ReorgT_Realloc\n");
		    /*
		// This used to be closely related to the old version of
		// the malloc code above and needs to transformed just like
		// what was done above to malloc.
		tree arg = gimple_call_arg( stmt, 0);
		len is new SSA
		tree val = gimple_call_lhs( stmt);
		gcc_assert( TREE_CODE( TREE_TYPE(val)) == INDIRECT_REF);
		tree size = TYPE_SIZE_UNIT( TREE_TYPE( TREE_TYPE( val)));
		gimple *glen = 
	        gimple_build_assign( 
		  len, 
		  TRUNC_DIV_EXPR, 
		  val, 
		  size,
		  NULL_TREE, NULL_TREE);
		insert glen before stmt
		tree lfial = create_artificial_label( UNKNOWN_LOCATION);
		gimple *gfail = gimple_build_label( lfail);
		tree lnotfial = create_artificial_label( UNKNOWN_LOCATION);
		gimple *gnotfail = gimple_build_label( lnotfail);
		for each field of base {
	      	  // call malloc
		  tree lok = create_artificial_label( UNKNOWN_LOCATION);
		  gimple *gok = gimple_build_label( lok);
		  tree fndecl = builtin_decl_explicit( BUILT_IN_REALLOC);
		  // but first compute how much to malloc
		  mem_size, var, ptr are new SSA
		  gimple *gsize = 
	          gimple_build_assign( 
        	    mem_size, 
		    MULT_EXPR, 
		    TYPE_SIZE(field), 
		    len,
		    NULL_TREE, NULL_TREE);
		  insert gsize before stmt
		  generate ptr = base.field & insert before stmt
		  gcall *call
		    = gimple_build_call( fndecl, 3, ptr,
		    		         len, TYPE_SIZE( field));
		  gimple_call_set_lhs( call, var);
		  insert call before stmt
		  // gen test of return
		  gimple *gcond =
		  gimple_build_cond( EQ_EXPR, var,
		  null_pointer_node, lfail, lok);
		  insert gcond before stmt
		  insert gok before stmt
		  generate base.field = var & insert before stmt
		}
		// fake return value of starting address (an index of zero)
		gimple *gretzero =
	          gimple_build_assign( lhs, //
		    build_int_cst(
		    TYPE_MIN_VALUE( TREE_TYPE(lhs)), 0));
		insert gretzero before stmt
		gimple *ggoto = gimple_build_goto( lnotfail);
		insert ggoto before stmt
		insert glab1 before stmt
		for each element of base {
	      	tree fndecl = builtin_decl_explicit( BUILT_IN_FREE);
	        gcall *call = gimple_build_call( fndecl, 1, element);
		insert call before stmt
		set element to null
	      }
	      // fake return value of null (minimum value under this scheme)
	      gimple *gretnull =
	        gimple_build_assign( lhs,
				     build_int_cst(
				       TYPE_MIN_VALUE( TREE_TYPE(lhs))));
	      insert gretnull before stmt
	      insert gnotfail before stmt
	      delete stmt
		    */
		    break;
		  case ReorgT_Free:
		    DEBUG_L("ReorgT_Free\n");
		    // We won't free the base because it a global.
		    /*
		  for each element of base {
		    tree fndecl = builtin_decl_explicit( BUILT_IN_FREE);
		    gcall *call = gimple_build_call( fndecl, 1, element);
		    insert call before stmt
		  }
		  delete stmt
		    */
		    break;
		  case ReorgT_UserFunc:
		    // Needed for helloworld.
		    // TBD The type must be adjusted (maybe.)
		    
		    // Note, what proably needed is an extra
		    // mini-pass to adjust all "dangling" SSA temps.
		    // I mean dangling in the sense that they are
		    // pointers to a reorg type on the left hand side
		    // of an assign and they haven't been modified to
		    // use the correct reorg pointer represenatation,
		    // even though the right hand side has been.
		    
		    DEBUG_L("ReorgT_UserFunc\n");
		    break;
		  case ReorgT_Return:
		    // TBD The type must be adjusted (maybe.)
		    DEBUG_L("ReorgT_Return\n");
		    break;
		  default:
		    internal_error( "Invalid transformation");
		  }
	      }
	  }
      }
    pop_cfun ();
  }

  // TBD A mini-pass to fixup dangling SSA temps. We need to modidy
  // function parameter too before we do that.
  // is run here.
  int mini_pass; // Passes 0 and two are just for testing
  for ( mini_pass = 0; mini_pass < 3; mini_pass++ )
    {
      bool emit_header = true;
      FOR_EACH_FUNCTION_WITH_GIMPLE_BODY ( node)  {
	struct function *func = DECL_STRUCT_FUNCTION ( node->decl);
	// Ulgy GCC idiom with global pointer to current function.
	push_cfun ( func);
	basic_block bb;
	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);
		if ( gimple_code ( stmt) == GIMPLE_ASSIGN )
		  {
		    ReorgType_t *ri = contains_a_reorgtype( stmt, info);
		    if ( ri != NULL )
		      {
			tree lhs = gimple_assign_lhs( stmt);
			tree rhs1 = gimple_assign_rhs1( stmt);
			tree rhs2 = gimple_assign_rhs2( stmt);
			tree rhs3 = gimple_assign_rhs3( stmt);
			
			bool lhs_reorg = tree_contains_a_reorgtype_p ( lhs, info);
			bool rhs1_reorg = tree_contains_a_reorgtype_p ( rhs1, info);
			bool rhs2_reorg = tree_contains_a_reorgtype_p ( rhs2, info);
			bool rhs3_reorg = tree_contains_a_reorgtype_p ( rhs3, info);

			bool lhs_ssa = lhs_reorg && TREE_CODE(lhs) == SSA_NAME;
			bool rhs1_ssa = rhs1_reorg && TREE_CODE(rhs1) == SSA_NAME;
			bool rhs2_ssa = rhs2_reorg && TREE_CODE(rhs2) == SSA_NAME;
			bool rhs3_ssa = rhs3_reorg && TREE_CODE(rhs3) == SSA_NAME;

			if ( mini_pass == 0 || mini_pass == 2 )
			  {
			    if ( lhs_reorg || rhs1_reorg || rhs2_reorg|| rhs3_reorg)
			      {
				if ( emit_header )
				  {
				    emit_header = false;
				    fprintf( stderr, "SANITY CHECKING FAILURE:\n");
				  }
				print_gimple_stmt ( stderr, stmt, 0);
			      }
			    if ( lhs_reorg )
			      {
				fprintf( stderr, "  LHS%s: ", lhs_ssa ? "*" : "");
				print_generic_expr ( stderr, TREE_TYPE ( lhs), (dump_flags_t)0);
				fprintf( stderr, "  ");
			      }
			    if ( rhs1_reorg )
			      {
				fprintf( stderr, "%s  RHS1%s: ", lhs_reorg ? ",  " : "",
					 rhs1_ssa ? "*" : "");
				print_generic_expr ( stderr, TREE_TYPE ( rhs1), (dump_flags_t)0);
			      }
			    if ( rhs2_reorg )
			      {
				fprintf( stderr, "%s  RHS2%s: ",
					 lhs_reorg || rhs1_reorg ? ",  " : "",
					 rhs2_ssa ? "*" : "");
				print_generic_expr ( stderr, TREE_TYPE ( rhs2), (dump_flags_t)0);
			      }
			    if ( rhs3_reorg )
			      {
				fprintf( stderr, "%s  RHS3%s: ",
					 lhs_reorg || rhs1_reorg || rhs2_reorg ? ",  " : "",
					 rhs3_ssa ? "*" : "");
				print_generic_expr ( stderr, TREE_TYPE ( rhs3), (dump_flags_t)0);
			      }
			    if ( lhs_reorg || rhs1_reorg || rhs2_reorg || rhs3_reorg )
			      {
				fprintf( stderr, "\n");
			      }
			    // etc.
			  }
			if ( mini_pass == 1 )
			  {
			    if ( lhs_ssa )
			      {
				tree lhs_type = TREE_TYPE ( lhs);
				tree bottom_type = base_type_of ( lhs_type);
				DEBUG_L("lhs: ");
				DEBUG_F(print_generic_expr, stderr, lhs, (dump_flags_t)0);
				DEBUG("\n");
				DEBUG_L("bottom_type: ");
				DEBUG_F(print_generic_expr, stderr, bottom_type, (dump_flags_t)0);
				DEBUG("\n");

				ReorgType_t *ri = get_reorgtype_info ( bottom_type, info);
				tree prev_type = lhs_type;
				tree type = TREE_TYPE ( prev_type);
				DEBUG_L( "prev_type: %p, type: %p\n", prev_type, type);
				int levels;
				for ( levels = 0; TREE_CODE ( type) == POINTER_TYPE; levels++ )
				  {
				    prev_type = type;
				    type = TREE_TYPE ( prev_type);
				    DEBUG_L( "prev_type: %p, type: %p\n", prev_type, type);
				  }

				#if 0
				// Modify type of ssa temp (dicey!)
				// This changes every instance of * reorg_type to the
				// new pointre rep in one fell swoop.
				// I sweat just thinking how crazy this is....
				TREE_TYPE ( prev_type) = ri->pointer_rep;
				#endif
				// TBD might use build_pointer_type to build new type for *(N)reorg_type
				// to *(N-1)ri->pointer_rep
				// Fakes this for levels == 1
				if ( levels == 0)
				  {
				    DEBUG_L( "LEVELS  ZERO\n");
				  }
				else
				  {
				    if ( levels == 1 )
				      {
					TREE_TYPE ( lhs) = ri->pointer_rep;
				      }
				    else
				      {
					gcc_assert(0);
				      }
				  }
			      }
			    if ( rhs1_ssa )
			      {
				wrangle_ssa_type( rhs1, info);
			      }
			    if ( rhs2_ssa )
			      {
				wrangle_ssa_type( rhs2, info);
			      }
			    if ( rhs3_ssa )
			      {
				wrangle_ssa_type( rhs3, info);
			      }
			  }
		      }
		  }
	      }
	  }
	pop_cfun ();
      }
    }

  fprintf ( stderr, "INTERNALS PRINT\n");
  apply_to_all_gimple ( print_internals, (void *)info);

  if ( info->show_all_reorg_cands )
    {
      fprintf ( info->reorg_dump_file, "End of str_reorg_instance_interleave_trans:\n");
      print_program ( info->reorg_dump_file, 4);
    }
}

static void
wrangle_ssa_type( tree side, Info_t *info )
{
  tree side_type = TREE_TYPE ( side);
  tree bottom_type = base_type_of ( side_type);
  DEBUG_L("side: ");
  DEBUG_F(print_generic_expr, stderr, side, (dump_flags_t)0);
  DEBUG("\n");
  DEBUG_L("bottom_type: ");
  DEBUG_F(print_generic_expr, stderr, bottom_type, (dump_flags_t)0);
  DEBUG("\n");

  ReorgType_t *ri = get_reorgtype_info ( bottom_type, info);
  tree prev_type = side_type;
  tree type = TREE_TYPE ( prev_type);
  DEBUG_L( "prev_type: %p, type: %p\n", prev_type, type);
  int levels;
  for ( levels = 0; TREE_CODE ( type) == POINTER_TYPE; levels++ )
    {
      prev_type = type;
      type = TREE_TYPE ( prev_type);
      DEBUG_L( "prev_type: %p, type: %p\n", prev_type, type);
    }

#if 0
  // Modify type of ssa temp (dicey!)
  // This changes every instance of * reorg_type to the
  // new pointre rep in one fell swoop.
  // I sweat just thinking how crazy this is....
  TREE_TYPE ( prev_type) = ri->pointer_rep;
#endif
  // TBD might use build_pointer_type to build new type for *(N)reorg_type
  // to *(N-1)ri->pointer_rep
  // Fakes this for levels == 1
  if ( levels == 0)
    {
      DEBUG_L( "LEVELS  ZERO\n");
    }
  else
    {
      if ( levels == 1 )
	{
	  TREE_TYPE ( side) = ri->pointer_rep;
	}
      else
	{
	  gcc_assert(0);
	}
    }
}


static bool
print_internals (gimple *stmt, void *data)
{
  Info_t *info = (Info_t*)data;

  print_gimple_stmt ( stderr, stmt, TDF_SLIM);

  if ( gimple_code ( stmt) == GIMPLE_ASSIGN )
    {
      tree lhs = gimple_assign_lhs( stmt);
      tree rhs1 = gimple_assign_rhs1( stmt);
      tree rhs2 = gimple_assign_rhs2( stmt);
      tree rhs3 = gimple_assign_rhs3( stmt);
      gcc_assert ( lhs);
      gcc_assert ( rhs1);
      
      bool lhs_reorg = tree_contains_a_reorgtype_p ( lhs, info);
      bool rhs1_reorg = tree_contains_a_reorgtype_p ( rhs1, info);
      bool rhs2_reorg = tree_contains_a_reorgtype_p ( rhs2, info);
      bool rhs3_reorg = tree_contains_a_reorgtype_p ( rhs3, info);
      
      bool lhs_ssa = lhs_reorg && TREE_CODE(lhs) == SSA_NAME;
      bool rhs1_ssa = rhs1_reorg && TREE_CODE(rhs1) == SSA_NAME;
      bool rhs2_ssa = rhs2_reorg && TREE_CODE(rhs2) == SSA_NAME;
      bool rhs3_ssa = rhs3_reorg && TREE_CODE(rhs3) == SSA_NAME;

      fprintf( stderr, "  LHS%s: ", lhs_ssa ? "*" : "");
      print_generic_expr ( stderr, TREE_TYPE ( lhs), (dump_flags_t)0);

      fprintf( stderr, ",  RHS1%s: ", rhs1_ssa ? "*" : "");
      print_generic_expr ( stderr, TREE_TYPE ( rhs1), (dump_flags_t)0);

      if ( rhs2 )
	{
	  fprintf( stderr, ",  RHS2%s: ", rhs2_ssa ? "*" : "");
	  print_generic_expr ( stderr, TREE_TYPE ( rhs2), (dump_flags_t)0);
	}

      if ( rhs3 )
	{
	  fprintf( stderr, ",  RHS3%s: ", rhs3_ssa ? "*" : "");
	  print_generic_expr ( stderr, TREE_TYPE ( rhs3), (dump_flags_t)0);
	}
      fprintf ( stderr, "\n");
    }
  return false;
}


static void
str_reorg_instance_interleave_qual_part ( Info *info)
{
  // TBD save the return value so we can bypass further
  // instance interleaving if none of it is profitable.
  reorg_perf_qual ( info);
}

static void
str_reorg_instance_interleave_type_part ( Info *info)
{
  create_new_types ( info);
}

static unsigned int
reorg_perf_qual ( Info *info)
{
  // TBD use design in doc but mark ReorgTypes
  // (do_instance_interleave) that qualify instead of deleting them
  // unless both dead field elimination and field reorderig are not
  // viable (use do_dead_field_elim and do_field_reorder in
  // Reorg_type_t.)

  // For the mean time assume if a ReorgType made it here then it's qualified.
  for ( int i = 0; i < info->reorg_type->size (); i++ )
    {
      (*(info->reorg_type))[i].do_instance_interleave = true;
    }
}

// create_new_types has to crawl "all" the
// types, create new types and transform
// other types that must be changed.
// A type will change when it's a
// a pointer to a ReorgType or it contains
// an interior pointer to one. 
static void
create_new_types ( Info_t *info)
{
  std::map < tree, BoolPair_t>::iterator tmi; // TBD what is 
  for( tmi = info->struct_types->begin ();
       tmi != info->struct_types->end ();
       tmi++ ) {
    if ( !tmi->second.processed ) create_a_new_type ( info, tmi->first);
  }    
}

static void
create_a_new_type ( Info_t *info, tree type)
{
  bool layout_changed = false;
  // skip if already processed  		   
  if ( ( *( info->struct_types))[type].processed ) return;

  // Implementation note: Check this for infinite recursion.
  // I don't think it's possible in a sane universe but
  // pointers to reorganized types can occur, so does that
  // an issue (not necessarily here.)
  // Also, is this even necessary? Singletons don't expand
  // and static arrays are not allowed "yet."
  tree field;
  tree new_fields = NULL;
  for ( field = TYPE_FIELDS ( type); // WHF, I speced reorg_type_prime here???
        field; 
        field = DECL_CHAIN ( field))
    {
      // make sure all the interior types are processed
      // before processing this type
      if ( TREE_CODE ( field) == RECORD_TYPE )
	{
	  create_a_new_type ( info, field);
	}
    }
	 
  ReorgType_t *ri = get_reorgtype_info ( type, info);
  if ( ri != NULL ) {
    // From the comment of build_variant_type_copy it might not what
    // is needed to here. Comment in tree:
    // "Create a new variant of TYPE, equivalent but distinct."
    // Now before we panic le'ts give it a try.
    //
    tree reorg_type_prime = 
      build_variant_type_copy ( type MEM_STAT_DECL);
    ri->reorg_ver_type = reorg_type_prime;
    
    /* Multi-pool only
    // Create pointer_rep
    // this will be a long and a pointer to the 
    // reorg_type_prime
    tree pointer_rep = 
    lang_hooks.types.make_type( RECORD_TYPE);
    
    tree index_name = get_identifier("index");
    tree index_field = build_decl( BUILTINS_LOCATION, 
    FIELD_DECL, 
    index_name, 
    long_integer_type_node);
    tree base_name = get_identifier("base");
    tree base_field = build_decl( BUILTINS_LOCATION, 
    FIELD_DECL, 
    base_name, 
    reorg_type_prime);
    insert_field_into_struct( pointer_rep, index_field);
    insert_field_into_struct( pointer_rep, base);
    
    reorg_type->pointer_rep = pointer_rep;
    */

    tree pointer_rep = make_node ( INTEGER_TYPE);
    TYPE_PRECISION ( pointer_rep) =
      TYPE_PRECISION ( pointer_sized_int_node);
    //DEBUG("Issue with gcc_ of reorg\n");
    //DEBUG_F(print_reorg, stderr, 2, ri);
    const char *gcc_name =
      identifier_to_locale ( IDENTIFIER_POINTER ( TYPE_NAME ( ri->gcc_type)));
    size_t len =
      strlen ( REORG_SP_PTR_PREFIX) + strlen ( gcc_name);
    char *name = ( char *)alloca(len + 1);
    strcpy ( name, REORG_SP_PTR_PREFIX);
    strcat ( name, gcc_name);
    TYPE_NAME ( pointer_rep) = get_identifier ( name);
    ri->pointer_rep = pointer_rep;
    DEBUG_L("pointer_rep = ");
    DEBUG_F( print_generic_expr, stderr, pointer_rep, (dump_flags_t)-1);
    DEBUG("\n");

    // TBD ! Some of the key bits from above seem to be missing below.
    // Specifically make_node for the base type, setting the base
    // in the ReorgType.
    //
    // Note, someplace (probably here) also has to declare a base type
    // variable (probably globally.) Doesn't this variable also belong
    // in the ReorgType?
    
    // Set name of reorg_type_prime
    // TBD shouldn't base_type_name be different from gcc_name above
    const char *base_type_name =
      identifier_to_locale ( IDENTIFIER_POINTER ( TYPE_NAME ( ri->gcc_type)));
    len = strlen ( REORG_SP_PREFIX) + strlen ( base_type_name);
    char *rec_name = ( char*)alloca ( len + 1);
    strcpy ( rec_name, REORG_SP_PREFIX);
    strcat ( rec_name, base_type_name);
    
    // Build the new pointer type fields
    TYPE_NAME ( reorg_type_prime) = get_identifier ( rec_name);
    tree field;
    tree new_fields = NULL;
    for ( field = TYPE_FIELDS ( reorg_type_prime); 
	 field; 
	 field = DECL_CHAIN ( field))
      {
	//DEBUG_F( print_generic_decl, stderr, field, TDF_DETAILS); // example
	tree tree_type = TREE_TYPE ( field);
	tree new_fld_type = build_pointer_type ( tree_type);
	tree new_decl =
	  build_decl ( DECL_SOURCE_LOCATION (field),
		       VAR_DECL, DECL_NAME (field), new_fld_type);
	// Missing a bunch of attributes (see tree-nested.c:899)
	// Let us seee what happens without them!
	DECL_CHAIN ( new_decl) = new_fields; // <- bug: need decl, not type
	new_fields = new_decl;
	//DEBUG( "built new pointer type field:");
	//DEBUG_F( print_generic_decl, stderr, new_decl, TDF_DETAILS);
	//DEBUG( "\n");
      }
    TYPE_FIELDS ( reorg_type_prime) = new_fields; // fix?
    
    // store reversed fields back into reorg_type_prime
    TYPE_FIELDS ( reorg_type_prime) = NULL;
    tree next_fld;
    for ( field = new_fields;
	  field; 
	  field = next_fld    )
      {
	next_fld = DECL_CHAIN ( field);
	DECL_CHAIN ( field) = TYPE_FIELDS ( reorg_type_prime);
	TYPE_FIELDS ( reorg_type_prime) = field;
      }
    // Fix-up the layout
    layout_type ( reorg_type_prime);

    // HERE
    // Create the base element for a reorg type. This is for the single
    // pool case only.
    tree base_var =
      build_decl ( UNKNOWN_LOCATION, VAR_DECL, NULL_TREE, ri->reorg_ver_type);
    
    const char *type_name =
      identifier_to_locale ( IDENTIFIER_POINTER ( TYPE_NAME ( ri->gcc_type)));
    size_t tlen = strlen ( REORG_SP_BASE_PREFIX) + strlen ( type_name);
    char *base_name = ( char*)alloca ( tlen + 1);
    strcpy ( base_name, REORG_SP_BASE_PREFIX);
    //DECL_NAME ( base_var) = get_identifier ( base_name);
    
    strcat ( base_name, type_name);
    
    DECL_NAME ( base_var) = get_identifier ( base_name); // wrong spot above???
    
    TREE_STATIC ( base_var) = 1;
    TREE_ADDRESSABLE  ( base_var) = 1;
    DECL_NONALIASED ( base_var) = 1;
    SET_DECL_ALIGN ( base_var, TYPE_ALIGN ( ri->reorg_ver_type));
    
    varpool_node::finalize_decl ( base_var);
    
    ri->instance_interleave.base = base_var;
  }

  // Mess with the original type too because it might
  // have base_type_fldinterior elements that are modified.
  for ( field = TYPE_FIELDS ( type); 
       field; 
       field = DECL_CHAIN ( field))
    {
      if ( TREE_CODE ( field) == RECORD_TYPE )
	{
	  layout_changed =
	    layout_changed || ( *( info->struct_types)) [ field].layout_changed;
	}
      else
	{
	  // process pointers to reorg types
	  if ( POINTER_TYPE_P ( field) )
	    {
	      tree field_type = TREE_TYPE ( field);
	      if ( is_reorg_type ( field_type, info) )
		{
		  // Change field type.
		  
		  // If multi-pool then set layout_changed to true.
		  
		  // The type pointed to changes for single-pool.
		  ReorgType_t *ri =
		    get_reorgtype_info ( field_type, info);
		  TREE_TYPE ( field) = ri->pointer_rep;
		}
	      tree base = base_type_of ( field);
	      if ( is_reorg_type ( base, info) )
		{
		  // strip off a layer of pointers
		  TREE_TYPE ( field) = TREE_TYPE ( TREE_TYPE( field));
		}
	    }
	}
    }

  // Mark the type as processed
  ( *( info->struct_types)) [ type] = { true, layout_changed};
}

static tree
find_coresponding_field ( tree base_decl, tree field)
{
  tree reorg_field;
  for ( reorg_field = TYPE_FIELDS ( TREE_TYPE ( base_decl)); 
	 reorg_field; 
	 reorg_field = DECL_CHAIN ( reorg_field))
      {
	const char *reorg_field_name =
	  lang_hooks.decl_printable_name ( reorg_field, 2);
	const char *field_name =
	  lang_hooks.decl_printable_name ( field, 2);
	//DEBUG_L("LOOK %s, %s\n", reorg_field_name, field_name);
	
	if ( strcmp ( reorg_field_name, field_name) == 0 )
	  {
	    gcc_assert ( TREE_TYPE( field) == TREE_TYPE( TREE_TYPE(reorg_field)));
	    return reorg_field;
	  }
      }
  internal_error ( "find_coresponding_field: found no field");
}