summaryrefslogtreecommitdiff
path: root/gcc/config/tilepro/gen-mul-tables.cc
blob: 5845d8a7c9d9dc301aad9aa84db19ece7f7e89f0 (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
/* Multiply table generator for tile.
   Copyright (C) 2011-2015 Free Software Foundation, Inc.
   Contributed by Walter Lee (walt@tilera.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/>.  */

/* This program creates a table used to compile multiply by constant
   efficiently.

   This program should be compiled by a c++ compiler.  If it's
   compiled with with -DTILEPRO, it generates the multiply table for
   TILEPro; otherwise it generates the multiply table for TILE-Gx.
   Running the program produces the table in stdout.

   The program works by generating every possible combination of up to
   MAX_INSTRUCTIONS linear operators (such as add, sub, s2a, left
   shift) and computing the multiplier computed by those instructions.
   For example,

   s2a r2,r1,r1
   s2a r3,r2,r2

   multiplies r1 by 25.

   There are usually multiple instruction sequences to multiply by a
   given constant. This program keeps only the cheapest one.
   "Cheapest" is defined first by the minimum theoretical schedule
   length, and if those are equal then by the number of instructions,
   and if those are equal then by which instructions we "prefer"
   (e.g. because we think one might take infinitesimally less power
   than another, or simply because we arbitrarily pick one to be more
   canonical).

   Once this program has determined the best instruction sequence for
   each multiplier, it emits them in a table sorted by the multiplier
   so the user can binary-search it to look for a match.  The table is
   pruned based on various criteria to keep its sizes reasonable.  */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define __STDC_LIMIT_MACROS
#include <stdint.h>

#include <map>

#ifdef TILEPRO

/* The string representing the architecture.  */
#define ARCH "tilepro"

/* The type for the multiplication.  */
typedef int MUL_TYPE;

#else

/* The string representing the architecture.  */
#define ARCH "tilegx"

/* The type for the multiplication.  */
typedef long long MUL_TYPE;

#endif

/* Longest instruction sequence this will produce. With the current
   stupid algorithm runtime grows very quickly with this number.  */
#define MAX_INSTRUCTIONS 4

/* Maximum number of subexpressions in the expression DAG being
   generated.  This is the same as the number of instructions, except
   that zero and the original register we'd like to multiply by a
   constant are also thrown into the mix.  */
#define MAX_SUBEXPRS (2 + MAX_INSTRUCTIONS)

#define MIN(x, y)  ((x) <= (y) ? (x) : (y))
#define MAX(x, y)  ((x) >= (y) ? (x) : (y))

/* For this program a unary op is one which has only one nonconstant
   operand.  So shift left by 5 is considered unary.  */
typedef MUL_TYPE (*unary_op_func) (MUL_TYPE);
typedef MUL_TYPE (*binary_op_func) (MUL_TYPE, MUL_TYPE);

/* This describes an operation like 'add two registers' or 'left-shift
   by 7'.

   We call something a 'unary' operator if it only takes in one
   register as input, even though it might have an implicit second
   constant operand.  Currently this is used for left-shift by
   constant.  */
class Operator
{
public:
  /* Construct for a binary operator.  */
  Operator (const char *pattern, const char *name, binary_op_func func,
	    int cost)
    : m_pattern (pattern), m_name (name), m_top_index (-1),
      m_unary_func (0), m_binary_func (func), m_cost (cost),
      m_rhs_if_unary (0)
  {
  }

  /* Construct for a unary operator.  */
  Operator (const char *pattern, const char *name, unary_op_func func,
	    int rhs_if_unary, int cost)
    : m_pattern (pattern), m_name (name), m_top_index (-1),
      m_unary_func (func), m_binary_func (0), m_cost (cost),
      m_rhs_if_unary (rhs_if_unary)
  {
  }

  bool is_unary () const
  {
    return m_binary_func == NULL;
  }

  /* Name of the pattern for this operation, e.g. CODE_FOR_addsi3.  */
  const char *m_pattern;

  /* Name of the opcode for this operation, e.g. add.  */
  const char *m_name;

  /* We don't have enough bits in our output representation to store
     the original insn_code value, so we store a compressed form
     instead.  These values are decoded back into insn_code via the
     machine-generated multiply_insn_seq_decode_opcode lookup
     table.  */
  int m_top_index;

  /* Unary operator to apply, or NULL if this is a binary operator.  */
  unary_op_func m_unary_func;

  /* Binary operator to apply, or NULL if this is a unary operator.  */
  binary_op_func m_binary_func;

  /* Function of how expensive we consider this operator. Higher is
     worse.  */
  int m_cost;

  /* the RHS value to write into the C file if unary; used for shift
     count.  */
  int m_rhs_if_unary;
};


/* An entry in an expression DAG.  */
class Expr
{
public:
  Expr () : m_op (NULL), m_lhs (0), m_rhs (0), m_produced_val (0),
    m_critical_path_length (0)
  {
  }

  /* The operator being applied to the operands.  */
  const Operator *m_op;

  /* The index of the left-hand operand in the array of subexpressions
     already computed.  */
  int m_lhs;

  /* For binary ops ,this is the index of the left-hand operand in the
     array of subexpressions already computed. For unary ops, it is
     specific to the op (e.g. it might hold a constant shift
     count).  */
  int m_rhs;

  /* The multiplier produced by this expression tree. For example, for
     the tree ((x << 5) + x), the value would be 33.  */
  MUL_TYPE m_produced_val;

  /* How far is this expression from the root, i.e. how many cycles
     minimum will it take to compute this?  */
  int m_critical_path_length;
};


/* Make function pointers for the various linear operators we can
   apply to compute a multiplicative value.  */

static MUL_TYPE
add (MUL_TYPE n1, MUL_TYPE n2)
{
  return n1 + n2;
}

static MUL_TYPE
sub (MUL_TYPE n1, MUL_TYPE n2)
{
  return n1 - n2;
}

static MUL_TYPE
s1a (MUL_TYPE n1, MUL_TYPE n2)
{
  return n1 * 2 + n2;
}

static MUL_TYPE
s2a (MUL_TYPE n1, MUL_TYPE n2)
{
  return n1 * 4 + n2;
}

static MUL_TYPE
s3a (MUL_TYPE n1, MUL_TYPE n2)
{
  return n1 * 8 + n2;
}

#define SHIFT(count)                            \
static MUL_TYPE                                 \
shift##count(MUL_TYPE n)                        \
{                                               \
  return n << (count);                          \
}

SHIFT (1);
SHIFT (2);
SHIFT (3);
SHIFT (4);
SHIFT (5);
SHIFT (6);
SHIFT (7);
SHIFT (8);
SHIFT (9);
SHIFT (10);
SHIFT (11);
SHIFT (12);
SHIFT (13);
SHIFT (14);
SHIFT (15);
SHIFT (16);
SHIFT (17);
SHIFT (18);
SHIFT (19);
SHIFT (20);
SHIFT (21);
SHIFT (22);
SHIFT (23);
SHIFT (24);
SHIFT (25);
SHIFT (26);
SHIFT (27);
SHIFT (28);
SHIFT (29);
SHIFT (30);
SHIFT (31);
#ifndef TILEPRO
SHIFT (32);
SHIFT (33);
SHIFT (34);
SHIFT (35);
SHIFT (36);
SHIFT (37);
SHIFT (38);
SHIFT (39);
SHIFT (40);
SHIFT (41);
SHIFT (42);
SHIFT (43);
SHIFT (44);
SHIFT (45);
SHIFT (46);
SHIFT (47);
SHIFT (48);
SHIFT (49);
SHIFT (50);
SHIFT (51);
SHIFT (52);
SHIFT (53);
SHIFT (54);
SHIFT (55);
SHIFT (56);
SHIFT (57);
SHIFT (58);
SHIFT (59);
SHIFT (60);
SHIFT (61);
SHIFT (62);
SHIFT (63);
#endif

#ifdef TILEPRO
static Operator ops[] = {
  Operator ("CODE_FOR_addsi3", "add", add, 1040),
  Operator ("CODE_FOR_subsi3", "sub", sub, 1041),
  Operator ("CODE_FOR_insn_s1a", "s1a", s1a, 1042),
  Operator ("CODE_FOR_insn_s2a", "s2a", s2a, 1043),
  Operator ("CODE_FOR_insn_s3a", "s3a", s3a, 1044),
  /* Note: shl by 1 is not necessary, since adding a value to itself
     produces the same result. But the architecture team thinks
     left-shift may use slightly less power, so we might as well
     prefer it.  */
  Operator ("CODE_FOR_ashlsi3", "shli", shift1, 1, 1001),
  Operator ("CODE_FOR_ashlsi3", "shli", shift2, 2, 1002),
  Operator ("CODE_FOR_ashlsi3", "shli", shift3, 3, 1003),
  Operator ("CODE_FOR_ashlsi3", "shli", shift4, 4, 1004),
  Operator ("CODE_FOR_ashlsi3", "shli", shift5, 5, 1005),
  Operator ("CODE_FOR_ashlsi3", "shli", shift6, 6, 1006),
  Operator ("CODE_FOR_ashlsi3", "shli", shift7, 7, 1007),
  Operator ("CODE_FOR_ashlsi3", "shli", shift8, 8, 1008),
  Operator ("CODE_FOR_ashlsi3", "shli", shift9, 9, 1009),
  Operator ("CODE_FOR_ashlsi3", "shli", shift10, 10, 1010),
  Operator ("CODE_FOR_ashlsi3", "shli", shift11, 11, 1011),
  Operator ("CODE_FOR_ashlsi3", "shli", shift12, 12, 1012),
  Operator ("CODE_FOR_ashlsi3", "shli", shift13, 13, 1013),
  Operator ("CODE_FOR_ashlsi3", "shli", shift14, 14, 1014),
  Operator ("CODE_FOR_ashlsi3", "shli", shift15, 15, 1015),
  Operator ("CODE_FOR_ashlsi3", "shli", shift16, 16, 1016),
  Operator ("CODE_FOR_ashlsi3", "shli", shift17, 17, 1017),
  Operator ("CODE_FOR_ashlsi3", "shli", shift18, 18, 1018),
  Operator ("CODE_FOR_ashlsi3", "shli", shift19, 19, 1019),
  Operator ("CODE_FOR_ashlsi3", "shli", shift20, 20, 1020),
  Operator ("CODE_FOR_ashlsi3", "shli", shift21, 21, 1021),
  Operator ("CODE_FOR_ashlsi3", "shli", shift22, 22, 1022),
  Operator ("CODE_FOR_ashlsi3", "shli", shift23, 23, 1023),
  Operator ("CODE_FOR_ashlsi3", "shli", shift24, 24, 1024),
  Operator ("CODE_FOR_ashlsi3", "shli", shift25, 25, 1025),
  Operator ("CODE_FOR_ashlsi3", "shli", shift26, 26, 1026),
  Operator ("CODE_FOR_ashlsi3", "shli", shift27, 27, 1027),
  Operator ("CODE_FOR_ashlsi3", "shli", shift28, 28, 1028),
  Operator ("CODE_FOR_ashlsi3", "shli", shift29, 29, 1029),
  Operator ("CODE_FOR_ashlsi3", "shli", shift30, 30, 1030),
  Operator ("CODE_FOR_ashlsi3", "shli", shift31, 31, 1031)
};
#else
static Operator ops[] = {
  Operator ("CODE_FOR_adddi3", "add", add, 1070),
  Operator ("CODE_FOR_subdi3", "sub", sub, 1071),
  Operator ("CODE_FOR_insn_shl1add", "shl1add", s1a, 1072),
  Operator ("CODE_FOR_insn_shl2add", "shl2add", s2a, 1073),
  Operator ("CODE_FOR_insn_shl3add", "shl3add", s3a, 1074),
  // Note: shl by 1 is not necessary, since adding a value to itself
  // produces the same result. But the architecture team thinks left-shift
  // may use slightly less power, so we might as well prefer it.
  Operator ("CODE_FOR_ashldi3", "shli", shift1, 1, 1001),
  Operator ("CODE_FOR_ashldi3", "shli", shift2, 2, 1002),
  Operator ("CODE_FOR_ashldi3", "shli", shift3, 3, 1003),
  Operator ("CODE_FOR_ashldi3", "shli", shift4, 4, 1004),
  Operator ("CODE_FOR_ashldi3", "shli", shift5, 5, 1005),
  Operator ("CODE_FOR_ashldi3", "shli", shift6, 6, 1006),
  Operator ("CODE_FOR_ashldi3", "shli", shift7, 7, 1007),
  Operator ("CODE_FOR_ashldi3", "shli", shift8, 8, 1008),
  Operator ("CODE_FOR_ashldi3", "shli", shift9, 9, 1009),
  Operator ("CODE_FOR_ashldi3", "shli", shift10, 10, 1010),
  Operator ("CODE_FOR_ashldi3", "shli", shift11, 11, 1011),
  Operator ("CODE_FOR_ashldi3", "shli", shift12, 12, 1012),
  Operator ("CODE_FOR_ashldi3", "shli", shift13, 13, 1013),
  Operator ("CODE_FOR_ashldi3", "shli", shift14, 14, 1014),
  Operator ("CODE_FOR_ashldi3", "shli", shift15, 15, 1015),
  Operator ("CODE_FOR_ashldi3", "shli", shift16, 16, 1016),
  Operator ("CODE_FOR_ashldi3", "shli", shift17, 17, 1017),
  Operator ("CODE_FOR_ashldi3", "shli", shift18, 18, 1018),
  Operator ("CODE_FOR_ashldi3", "shli", shift19, 19, 1019),
  Operator ("CODE_FOR_ashldi3", "shli", shift20, 20, 1020),
  Operator ("CODE_FOR_ashldi3", "shli", shift21, 21, 1021),
  Operator ("CODE_FOR_ashldi3", "shli", shift22, 22, 1022),
  Operator ("CODE_FOR_ashldi3", "shli", shift23, 23, 1023),
  Operator ("CODE_FOR_ashldi3", "shli", shift24, 24, 1024),
  Operator ("CODE_FOR_ashldi3", "shli", shift25, 25, 1025),
  Operator ("CODE_FOR_ashldi3", "shli", shift26, 26, 1026),
  Operator ("CODE_FOR_ashldi3", "shli", shift27, 27, 1027),
  Operator ("CODE_FOR_ashldi3", "shli", shift28, 28, 1028),
  Operator ("CODE_FOR_ashldi3", "shli", shift29, 29, 1029),
  Operator ("CODE_FOR_ashldi3", "shli", shift30, 30, 1030),
  Operator ("CODE_FOR_ashldi3", "shli", shift31, 31, 1031),
  Operator ("CODE_FOR_ashldi3", "shli", shift32, 32, 1032),
  Operator ("CODE_FOR_ashldi3", "shli", shift33, 33, 1033),
  Operator ("CODE_FOR_ashldi3", "shli", shift34, 34, 1034),
  Operator ("CODE_FOR_ashldi3", "shli", shift35, 35, 1035),
  Operator ("CODE_FOR_ashldi3", "shli", shift36, 36, 1036),
  Operator ("CODE_FOR_ashldi3", "shli", shift37, 37, 1037),
  Operator ("CODE_FOR_ashldi3", "shli", shift38, 38, 1038),
  Operator ("CODE_FOR_ashldi3", "shli", shift39, 39, 1039),
  Operator ("CODE_FOR_ashldi3", "shli", shift40, 40, 1040),
  Operator ("CODE_FOR_ashldi3", "shli", shift41, 41, 1041),
  Operator ("CODE_FOR_ashldi3", "shli", shift42, 42, 1042),
  Operator ("CODE_FOR_ashldi3", "shli", shift43, 43, 1043),
  Operator ("CODE_FOR_ashldi3", "shli", shift44, 44, 1044),
  Operator ("CODE_FOR_ashldi3", "shli", shift45, 45, 1045),
  Operator ("CODE_FOR_ashldi3", "shli", shift46, 46, 1046),
  Operator ("CODE_FOR_ashldi3", "shli", shift47, 47, 1047),
  Operator ("CODE_FOR_ashldi3", "shli", shift48, 48, 1048),
  Operator ("CODE_FOR_ashldi3", "shli", shift49, 49, 1049),
  Operator ("CODE_FOR_ashldi3", "shli", shift50, 50, 1050),
  Operator ("CODE_FOR_ashldi3", "shli", shift51, 51, 1051),
  Operator ("CODE_FOR_ashldi3", "shli", shift52, 52, 1052),
  Operator ("CODE_FOR_ashldi3", "shli", shift53, 53, 1053),
  Operator ("CODE_FOR_ashldi3", "shli", shift54, 54, 1054),
  Operator ("CODE_FOR_ashldi3", "shli", shift55, 55, 1055),
  Operator ("CODE_FOR_ashldi3", "shli", shift56, 56, 1056),
  Operator ("CODE_FOR_ashldi3", "shli", shift57, 57, 1057),
  Operator ("CODE_FOR_ashldi3", "shli", shift58, 58, 1058),
  Operator ("CODE_FOR_ashldi3", "shli", shift59, 59, 1059),
  Operator ("CODE_FOR_ashldi3", "shli", shift60, 60, 1060),
  Operator ("CODE_FOR_ashldi3", "shli", shift61, 61, 1061),
  Operator ("CODE_FOR_ashldi3", "shli", shift62, 62, 1062),
  Operator ("CODE_FOR_ashldi3", "shli", shift63, 63, 1063)
};
#endif

/* An ExpressionTree is an expression DAG.  */
class ExpressionTree
{
public:
  ExpressionTree () : m_num_vals (2)
  {
    m_exprs[0].m_produced_val = 0;
    m_exprs[1].m_produced_val = 1;
  }

  void copy_technique_from (ExpressionTree * other)
  {
    /* TODO: write this; other can compute the same products with less
       cost.  We update this ExpressionTree object because some int is
       already mapped to it.  */
  }

  int m_num_vals;
  Expr m_exprs[MAX_SUBEXPRS];

  int cost () const
  {
    int cost = 0;
    for (int j = 2; j < m_num_vals; j++)
        cost += m_exprs[j].m_op->m_cost;
      return cost + m_exprs[m_num_vals - 1].m_critical_path_length * 1000000;
  }
};


typedef std::map<MUL_TYPE, ExpressionTree *> ExpressionTreeMap;


static void
find_sequences (ExpressionTree &s, ExpressionTreeMap &best_solution)
{
  /* Don't look more if we can't add any new values to the expression
     tree.  */
  const int num_vals = s.m_num_vals;
  if (num_vals == MAX_SUBEXPRS)
    return;

  /* Grow array to make room for new values added as we loop.  */
  s.m_num_vals = num_vals + 1;

  const Operator *const prev_op = s.m_exprs[num_vals - 1].m_op;
  const int prev_top_index = (prev_op != NULL) ? prev_op->m_top_index : -1;

  for (size_t f = 0; f < sizeof ops / sizeof ops[0]; f++)
    {
      const Operator *const op = &ops[f];

      for (int i = 0; i < num_vals; i++)
	{
	  /* Only allow zero as the first operand to sub, otherwise
	     it is useless.  */
	  if (i == 0 && op->m_binary_func != sub)
	    continue;

	  /* Unary ops don't actually use the second operand, so as a
	     big hack we trick it into only looping once in the inner
	     loop.  */
	  const int j_end = op->is_unary () ? 2 : num_vals;

	  /* We never allow zero as the second operand, as it is
	     always useless.  */
	  for (int j = 1; j < j_end; j++)
	    {
	      /* Does this expression use the immediately previous
		 expression?  */
	      const bool uses_prev_value =
		(i == num_vals - 1
		 || (!op->is_unary () && j == num_vals - 1));

	      if (!uses_prev_value)
		{
		  /* For search efficiency, prune redundant
		     instruction orderings.

		     This op does not take the immediately preceding
		     value as input, which means we could have done it
		     in the previous slot. If its opcode is less than
		     the previous instruction's, we'll declare this
		     instruction order non-canonical and not pursue
		     this branch of the search.  */
		  if (op->m_top_index < prev_top_index)
		    continue;
		}

	      MUL_TYPE n;
	      if (op->is_unary ())
		{
		  n = op->m_unary_func (s.m_exprs[i].m_produced_val);
		}
	      else
		{
		  n = op->m_binary_func (s.m_exprs[i].m_produced_val,
					 s.m_exprs[j].m_produced_val);
		}

	      bool duplicate = false;
	      for (int k = 0; k < num_vals; k++)
		if (n == s.m_exprs[j].m_produced_val)
		  {
		    duplicate = true;
		    break;
		  }

	      if (duplicate)
		continue;

	      /* See if we found the best solution for n.  */
	      Expr *e = &s.m_exprs[num_vals];
	      e->m_op = op;
	      e->m_lhs = i;
	      e->m_rhs = op->is_unary () ? op->m_rhs_if_unary : j;
	      e->m_produced_val = n;
	      e->m_critical_path_length =
		1 + MAX (s.m_exprs[i].m_critical_path_length,
			 s.m_exprs[j].m_critical_path_length);

	      ExpressionTreeMap::iterator best (best_solution.find (n));
	      if (best == best_solution.end ()
		  || (*best).second->cost () > s.cost ())
		best_solution[n] = new ExpressionTree (s);

	      /* Recurse and find more.  */
	      find_sequences (s, best_solution);
	    }
	}
    }

  /* Restore old size.  */
  s.m_num_vals = num_vals;
}


/* For each insn_code used by an operator, choose a compact number so
   it takes less space in the output data structure. This prints out a
   lookup table used to map the compactified number back to an
   insn_code.  */
static void
create_insn_code_compression_table ()
{
  int next_index = 1;

  /* Entry 0 must hold CODE_FOR_nothing to mean "end of array".  */
  printf ("const enum insn_code %s_multiply_insn_seq_decode_opcode[] = {\n"
	  "  CODE_FOR_nothing /* must be first */ ", ARCH);

  for (size_t i = 0; i < sizeof ops / sizeof ops[0]; i++)
    {
      Operator *op = &ops[i];
      int index = -1;

      /* See if some previous Operator was using the same insn_code.
	 If so, reuse its table entry.  */
      for (size_t j = 0; j < i; j++)
	{
	  Operator *old = &ops[j];
	  if (strcmp (old->m_pattern, op->m_pattern) == 0)
	    {
	      index = old->m_top_index;
	      break;
	    }
	}

      if (index == -1)
	{
	  /* We need to make a new entry in the table.  */
	  printf (",\n  %s", op->m_pattern);
	  index = next_index++;
	}

      op->m_top_index = index;
    }

  printf ("\n};\n\n");
}


/* These are constants we've seen in code, that we want to keep table
   entries for.  */
static int multiply_constants_used[] = {
  -11796480,
  -27439,
  -25148,
  -22820,
  -21709,
  -20995,
  -20284,
  -20239,
  -19595,
  -19447,
  -19183,
  -19165,
  -18730,
  -17828,
  -17799,
  -17237,
  -17036,
  -16549,
  -16423,
  -16294,
  -16244,
  -16069,
  -15137,
  -15083,
  -15038,
  -14924,
  -14905,
  -14752,
  -14731,
  -14529,
  -14273,
  -14090,
  -14084,
  -14043,
  -13850,
  -13802,
  -13631,
  -13455,
  -13275,
  -12879,
  -12700,
  -12534,
  -12399,
  -12131,
  -12112,
  -12054,
  -12019,
  -11759,
  -11585,
  -11467,
  -11395,
  -11295,
  -11248,
  -11148,
  -11116,
  -11086,
  -11059,
  -11018,
  -10811,
  -10538,
  -10258,
  -10217,
  -10033,
  -9766,
  -9754,
  -9534,
  -9527,
  -9467,
  -9262,
  -9232,
  -9222,
  -9198,
  -9191,
  -9113,
  -8825,
  -8756,
  -8697,
  -8693,
  -8565,
  -8342,
  -8208,
  -8200,
  -8170,
  -8102,
  -7770,
  -7678,
  -7562,
  -7376,
  -7373,
  -7221,
  -7121,
  -6835,
  -6810,
  -6626,
  -6581,
  -6461,
  -6278,
  -6263,
  -6163,
  -6029,
  -5816,
  -5540,
  -5461,
  -5384,
  -5329,
  -4985,
  -4926,
  -4815,
  -4788,
  -4758,
  -4433,
  -4229,
  -4209,
  -4176,
  -4104,
  -4095,
  -4078,
  -3941,
  -3818,
  -3600,
  -3474,
  -3314,
  -3264,
  -3196,
  -3072,
  -2912,
  -2836,
  -2773,
  -2269,
  -2184,
  -2100,
  -1730,
  -1512,
  -1500,
  -1396,
  -1344,
  -1312,
  -1297,
  -1059,
  -1058,
  1027,
  1049,
  1059,
  1100,
  1104,
  1108,
  1136,
  1200,
  1204,
  1242,
  1292,
  1304,
  1312,
  1320,
  1336,
  1344,
  1348,
  1360,
  1364,
  1395,
  1448,
  1460,
  1461,
  1472,
  1488,
  1500,
  1512,
  1568,
  1576,
  1649,
  1664,
  1684,
  1696,
  1744,
  1812,
  1822,
  1884,
  1963,
  1978,
  2000,
  2012,
  2014,
  2037,
  2039,
  2100,
  2139,
  2144,
  2184,
  2237,
  2260,
  2320,
  2408,
  2446,
  2447,
  2499,
  2531,
  2578,
  2592,
  2611,
  2633,
  2704,
  2730,
  2773,
  2880,
  2896,
  2998,
  3000,
  3001,
  3021,
  3079,
  3112,
  3150,
  3179,
  3192,
  3240,
  3264,
  3271,
  3283,
  3328,
  3363,
  3367,
  3453,
  3529,
  3570,
  3580,
  3600,
  3624,
  3707,
  3783,
  3826,
  3897,
  3941,
  3962,
  3989,
  4000,
  4025,
  4073,
  4093,
  4099,
  4108,
  4184,
  4209,
  4369,
  4376,
  4416,
  4433,
  4434,
  4482,
  4582,
  4712,
  4717,
  4813,
  4815,
  4864,
  5000,
  5027,
  5040,
  5091,
  5195,
  5243,
  5260,
  5285,
  5329,
  5331,
  5350,
  5361,
  5387,
  5461,
  5492,
  5529,
  5573,
  5793,
  5819,
  5915,
  5946,
  5992,
  6000,
  6164,
  6205,
  6262,
  6263,
  6269,
  6270,
  6387,
  6400,
  6406,
  6476,
  6541,
  6565,
  6568,
  6626,
  6656,
  6732,
  6810,
  6817,
  6859,
  7040,
  7053,
  7141,
  7169,
  7221,
  7223,
  7274,
  7282,
  7350,
  7369,
  7373,
  7442,
  7447,
  7471,
  7518,
  7542,
  7566,
  7587,
  7663,
  7678,
  7682,
  7748,
  7752,
  7791,
  8000,
  8026,
  8048,
  8170,
  8203,
  8204,
  8290,
  8368,
  8520,
  8640,
  8666,
  8672,
  8697,
  8716,
  8728,
  8756,
  8820,
  8875,
  8918,
  8956,
  9058,
  9154,
  9175,
  9191,
  9217,
  9262,
  9321,
  9373,
  9434,
  9465,
  9514,
  9534,
  9633,
  9746,
  9810,
  9850,
  9947,
  9973,
  10000,
  10009,
  10033,
  10055,
  10217,
  10248,
  10298,
  10310,
  10323,
  10368,
  10438,
  10456,
  10486,
  10538,
  10664,
  10695,
  10700,
  10703,
  10832,
  10887,
  10935,
  10958,
  11018,
  11059,
  11061,
  11086,
  11116,
  11148,
  11190,
  11249,
  11314,
  11332,
  11363,
  11409,
  11415,
  11443,
  11467,
  11512,
  11522,
  11529,
  11585,
  11759,
  11768,
  11795,
  11893,
  11997,
  12131,
  12299,
  12536,
  12543,
  12893,
  12945,
  12998,
  13109,
  13213,
  13685,
  13930,
  14023,
  14024,
  14271,
  14564,
  14647,
  15326,
  15850,
  15855,
  15929,
  16000,
  16154,
  16496,
  16807,
  16819,
  16984,
  17203,
  17223,
  17333,
  17760,
  17799,
  17837,
  18029,
  18068,
  18336,
  18515,
  19595,
  20017,
  20131,
  20862,
  20995,
  21709,
  22554,
  25000,
  25172,
  25600,
  25733,
  27439,
  38470,
  46802,
  50000,
  11796480,
  16843009,
  23592960,
};


const int num_mult_constants_used =
  (int)(sizeof multiply_constants_used
	/ sizeof multiply_constants_used[0]);


#define XSIZE (sizeof multiply_constants_used / \
	       sizeof multiply_constants_used[0] + 32) / 32
unsigned multiply_constants_avail[XSIZE];
#undef XSIZE


/* bsearch helper function.  */
static int
compare_constants (const void *key, const void *t)
{
  return (*(int*)key) - *((int*)t);
}


static int *
find_mult_constants_used (int multiplier)
{
  return (int *) bsearch (&multiplier, multiply_constants_used,
			  num_mult_constants_used,
			  sizeof multiply_constants_used[0],
			  compare_constants);
}


int num_ops (ExpressionTree *s)
{
  int n = 0;
  for (int i = 0; i < s->m_num_vals; i++)
    {
      Expr *e = &s->m_exprs[i];
      if (e->m_op != NULL)
	n++;
    }
  return n;
}


#ifdef TILEPRO
bool
tilepro_emit (int multiplier, int num_ops)
{
  int abs_multiplier = (multiplier >= 0) ? multiplier : -multiplier;

  /* Keep constants in range [-1024, 1024].  */
  if (abs_multiplier <= 1024)
    return true;

  /* Keep constants near powers of two.  */
  int prev_pow2 = 1 << (31 - __builtin_clz (abs_multiplier));
  int next_pow2 = prev_pow2 << 1;

  if ((abs_multiplier - prev_pow2 <= 10)
      || (next_pow2 - abs_multiplier <= 10))
    return true;

  /* Keep constants near powers of ten.  */
  {
    long long j = 1;
    long long prev_pow10;
    long long next_pow10;

    while (((j * 10) < abs_multiplier)
	   && (j < (j * 10)))
      j = j * 10;

    prev_pow10 = j;
    next_pow10 = j * 10;

    if ((abs_multiplier - prev_pow10 <= 10)
	|| (next_pow10 - abs_multiplier <= 10))
      return true;
  }

  /* Keep short sequences that have two or fewer ops.  */
  if (num_ops <= 2)
    return true;

  /* Keep constants that are mostly 0's or mostly 1's.  */
  if (__builtin_popcount (multiplier) <= 2 ||
      __builtin_popcount (multiplier) >= 30)
    return true;

  /* Keep constants seen in actual code.  */
  if ((find_mult_constants_used (multiplier)))
    return true;

  return false;
}
#else
bool
tilegx_emit (long long multiplier, int num_ops)
{
  long long abs_multiplier = (multiplier >= 0) ? multiplier : - multiplier;

  /* Keep constants in range [-1024, 1024].  */
  if (abs_multiplier <= 1024)
    return true;

  /* Otherwise exclude sequences with four ops.  */
  if (num_ops > 3)
    return false;

  /* Keep constants near powers of two.  */
  {
    unsigned long long prev_pow2 =
      1LL << (63 - __builtin_clzll (abs_multiplier));
    unsigned long long next_pow2 = prev_pow2 << 1;

    /* handle overflow case. */
    if (next_pow2 == 0)
      {
	if (prev_pow2 - abs_multiplier <= 10)
	  return true;
      }
    else if ((abs_multiplier - prev_pow2 <= 10)
	     || (next_pow2 - abs_multiplier <= 10))
      return true;
  }

  /* Keep constants near powers of ten.  */
  {
    long long j = 1;
    long long prev_pow10;
    long long next_pow10;

    while (((j * 10) < abs_multiplier)
	   && (j < (INTMAX_MAX / 10)))
      j = j * 10;

    prev_pow10 = j;
    next_pow10 = (j > (INTMAX_MAX / 10)) ? 0 : j * 10;

    if ((abs_multiplier - prev_pow10 <= 100)
	|| (next_pow10
	    && (next_pow10 - abs_multiplier <= 100)))
      return true;
  }

  if (num_ops <= 2)
    return true;

  /* Keep constants that are mostly 0's or mostly 1's.  */
  if (__builtin_popcountll (multiplier) <= 2 ||
      __builtin_popcountll (multiplier) >= 62)
    return true;

  /* Keep constants seen in actual code.  */
  if (find_mult_constants_used (multiplier))
    return true;

  return false;
}
#endif


int
main ()
{
  ExpressionTreeMap best_solution;
  ExpressionTree s;

#ifdef TILEPRO
  printf ("/* Constant multiply table for TILEPro.\n");
#else
  printf ("/* Constant multiply table for TILE-Gx.\n");
#endif
  printf ("   Copyright (C) 2011-2015 Free Software Foundation, Inc.\n");
  printf ("   Contributed by Walter Lee (walt@tilera.com)\n");
  printf ("\n");
  printf ("   This file is part of GCC.\n");
  printf ("\n");
  printf ("   GCC is free software; you can redistribute it and/or modify it\n");
  printf ("   under the terms of the GNU General Public License as published\n");
  printf ("   by the Free Software Foundation; either version 3, or (at your\n");
  printf ("   option) any later version.\n");
  printf ("\n");
  printf ("   GCC is distributed in the hope that it will be useful, but WITHOUT\n");
  printf ("   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\n");
  printf ("   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public\n");
  printf ("   License for more details.\n");
  printf ("\n");
  printf ("   You should have received a copy of the GNU General Public License\n");
  printf ("   along with GCC; see the file COPYING3.  If not see\n");
  printf ("   <http://www.gnu.org/licenses/>.  */\n");
  printf ("\n");
  printf ("/* Note this file is auto-generated from gen-mul-tables.cc.\n");
  printf ("   Make any required changes there.  */\n");
  printf ("\n");
  printf ("#include \"config.h\"\n");
  printf ("#include \"system.h\"\n");
  printf ("#include \"coretypes.h\"\n");
  printf ("#include \"expr.h\"\n");
  printf ("#include \"insn-codes.h\"\n");
  printf ("#include \"optabs.h\"\n");
  printf ("#include \"%s-multiply.h\"\n\n", ARCH);
  create_insn_code_compression_table ();

  /* Try all combinations of operators and see what constants we
     produce.  For each possible constant, record the most efficient
     way to generate it.  */
  find_sequences (s, best_solution);

  printf ("const struct %s_multiply_insn_seq "
	  "%s_multiply_insn_seq_table[] = {\n",
	  ARCH, ARCH);

  const char *comma_separator = "";

  ExpressionTreeMap::iterator i (best_solution.begin ());
  for (; i != best_solution.end (); ++i)
    {
      ExpressionTree *s = (*i).second;
      const MUL_TYPE n = (*i).first;

      if (n == 0 || n == 1)
	{
	  /* Both of these require zero operations, so these entries
	     are bogus and should never be used.  */
	  continue;
	}

      /* Prune the list of entries to keep the table to a reasonable
	 size.  */
#ifdef TILEPRO
      if (!tilepro_emit (n, num_ops (s)))
	continue;
#else
      if (!tilegx_emit (n, num_ops (s)))
	continue;
#endif

      printf ("%s", comma_separator);

#ifdef TILEPRO
      const MUL_TYPE int_min = INT32_MIN;
#else
      const MUL_TYPE int_min = INT64_MIN;
#endif
      if (n == int_min)
	{
	  /* Handle C's woeful lack of unary negation. Without this,
	     printing out INT_MIN in decimal will yield an unsigned
	     int which could generate a compiler warning.  */
#ifdef TILEPRO
	  printf ("  {%d - 1 /* 0x%x */ ,\n   {", n + 1,
		  (unsigned) n);
#else
	  printf ("  {%lldll - 1 /* 0x%llx */ ,\n   {", n + 1,
		  (unsigned MUL_TYPE) n);
#endif
	}
      else
	{
#ifdef TILEPRO
	  printf ("  {%d /* 0x%x */ ,\n   {", n, (unsigned) n);
#else
	  printf ("  {%lldll /* 0x%llx */ ,\n   {", n, (unsigned MUL_TYPE) n);
#endif
	}

      bool first = true;
      for (int j = 0; j < s->m_num_vals; j++)
	{
	  Expr *e = &s->m_exprs[j];

	  const Operator *op = e->m_op;
	  if (op == NULL)
	    continue;

	  char buf[1024];
	  snprintf (buf, sizeof buf, "%s{%d, %d, %d}%s",
		    first ? "" : "    ",
		    op->m_top_index,
		    e->m_lhs, e->m_rhs, (j + 1) == s->m_num_vals ? "}" : ",");

	  char opnd0[10];
	  if (e->m_lhs)
	    snprintf (opnd0, sizeof opnd0, "r%d", e->m_lhs);
	  else
	    snprintf (opnd0, sizeof opnd0, "zero");

	  printf ("%s\t\t\t/* %s r%d, %s, %s%d */\n",
		  buf, op->m_name, j, opnd0,
		  op->is_unary () ? "" : "r", e->m_rhs);

	  first = false;
	}
      printf ("   }");
      comma_separator = ",\n";
    }

  printf ("\n};\n\n");
  printf ("const int %s_multiply_insn_seq_table_size =\n"
	  "  (int) (sizeof %s_multiply_insn_seq_table\n"
	  "         / sizeof %s_multiply_insn_seq_table[0]);\n",
	  ARCH, ARCH, ARCH);

  return EXIT_SUCCESS;
}