aboutsummaryrefslogtreecommitdiff
path: root/gcc/match.pd
blob: 641148652b2f43fa99768fe50333b38f0daa0181 (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
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
/* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
   This file is consumed by genmatch which produces gimple-match.c
   and generic-match.c from it.

   Copyright (C) 2014-2015 Free Software Foundation, Inc.
   Contributed by Richard Biener <rguenther@suse.de>
   and Prathamesh Kulkarni  <bilbotheelffriend@gmail.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/>.  */


/* Generic tree predicates we inherit.  */
(define_predicates
   integer_onep integer_zerop integer_all_onesp integer_minus_onep
   integer_each_onep integer_truep
   real_zerop real_onep real_minus_onep
   CONSTANT_CLASS_P
   tree_expr_nonnegative_p)

/* Operator lists.  */
(define_operator_list tcc_comparison
  lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
(define_operator_list inverted_tcc_comparison
  ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
(define_operator_list inverted_tcc_comparison_with_nans
  unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
(define_operator_list swapped_tcc_comparison
  gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
(define_operator_list simple_comparison         lt   le   eq ne ge   gt)
(define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)

(define_operator_list LOG BUILT_IN_LOGF BUILT_IN_LOG BUILT_IN_LOGL)
(define_operator_list EXP BUILT_IN_EXPF BUILT_IN_EXP BUILT_IN_EXPL)
(define_operator_list LOG2 BUILT_IN_LOG2F BUILT_IN_LOG2 BUILT_IN_LOG2L)
(define_operator_list EXP2 BUILT_IN_EXP2F BUILT_IN_EXP2 BUILT_IN_EXP2L)
(define_operator_list LOG10 BUILT_IN_LOG10F BUILT_IN_LOG10 BUILT_IN_LOG10L)
(define_operator_list EXP10 BUILT_IN_EXP10F BUILT_IN_EXP10 BUILT_IN_EXP10L)
(define_operator_list POW BUILT_IN_POWF BUILT_IN_POW BUILT_IN_POWL)
(define_operator_list POW10 BUILT_IN_POW10F BUILT_IN_POW10 BUILT_IN_POW10L)
(define_operator_list SQRT BUILT_IN_SQRTF BUILT_IN_SQRT BUILT_IN_SQRTL)
(define_operator_list CBRT BUILT_IN_CBRTF BUILT_IN_CBRT BUILT_IN_CBRTL)


/* Simplifications of operations with one constant operand and
   simplifications to constants or single values.  */

(for op (plus pointer_plus minus bit_ior bit_xor)
  (simplify
    (op @0 integer_zerop)
    (non_lvalue @0)))

/* 0 +p index -> (type)index */
(simplify
 (pointer_plus integer_zerop @1)
 (non_lvalue (convert @1)))

/* See if ARG1 is zero and X + ARG1 reduces to X.
   Likewise if the operands are reversed.  */
(simplify
 (plus:c @0 real_zerop@1)
 (if (fold_real_zero_addition_p (type, @1, 0))
  (non_lvalue @0)))

/* See if ARG1 is zero and X - ARG1 reduces to X.  */
(simplify
 (minus @0 real_zerop@1)
 (if (fold_real_zero_addition_p (type, @1, 1))
  (non_lvalue @0)))

/* Simplify x - x.
   This is unsafe for certain floats even in non-IEEE formats.
   In IEEE, it is unsafe because it does wrong for NaNs.
   Also note that operand_equal_p is always false if an operand
   is volatile.  */
(simplify
 (minus @0 @0)
 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
  { build_zero_cst (type); }))

(simplify
 (mult @0 integer_zerop@1)
 @1)

/* Maybe fold x * 0 to 0.  The expressions aren't the same
   when x is NaN, since x * 0 is also NaN.  Nor are they the
   same in modes with signed zeros, since multiplying a
   negative value by 0 gives -0, not +0.  */
(simplify
 (mult @0 real_zerop@1)
 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
  @1))

/* In IEEE floating point, x*1 is not equivalent to x for snans.
   Likewise for complex arithmetic with signed zeros.  */
(simplify
 (mult @0 real_onep)
 (if (!HONOR_SNANS (type)
      && (!HONOR_SIGNED_ZEROS (type)
          || !COMPLEX_FLOAT_TYPE_P (type)))
  (non_lvalue @0)))

/* Transform x * -1.0 into -x.  */
(simplify
 (mult @0 real_minus_onep)
  (if (!HONOR_SNANS (type)
       && (!HONOR_SIGNED_ZEROS (type)
           || !COMPLEX_FLOAT_TYPE_P (type)))
   (negate @0)))

/* Make sure to preserve divisions by zero.  This is the reason why
   we don't simplify x / x to 1 or 0 / x to 0.  */
(for op (mult trunc_div ceil_div floor_div round_div exact_div)
  (simplify
    (op @0 integer_onep)
    (non_lvalue @0)))

/* X / -1 is -X.  */
(for div (trunc_div ceil_div floor_div round_div exact_div)
 (simplify
   (div @0 integer_minus_onep@1)
   (if (!TYPE_UNSIGNED (type))
    (negate @0))))

/* For unsigned integral types, FLOOR_DIV_EXPR is the same as
   TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
(simplify
 (floor_div @0 @1)
 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
      && TYPE_UNSIGNED (type))
  (trunc_div @0 @1)))

/* Combine two successive divisions.  Note that combining ceil_div
   and floor_div is trickier and combining round_div even more so.  */
(for div (trunc_div exact_div)
 (simplify
  (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
  (with {
    bool overflow_p;
    wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
   }
   (if (!overflow_p)
    (div @0 { wide_int_to_tree (type, mul); }))
   (if (overflow_p
        && (TYPE_UNSIGNED (type)
	    || mul != wi::min_value (TYPE_PRECISION (type), SIGNED)))
    { build_zero_cst (type); }))))

/* Optimize A / A to 1.0 if we don't care about
   NaNs or Infinities.  */
(simplify
 (rdiv @0 @0)
 (if (FLOAT_TYPE_P (type)
      && ! HONOR_NANS (type)
      && ! HONOR_INFINITIES (type))
  { build_one_cst (type); }))

/* Optimize -A / A to -1.0 if we don't care about
   NaNs or Infinities.  */
(simplify
 (rdiv:c @0 (negate @0))
 (if (FLOAT_TYPE_P (type)
      && ! HONOR_NANS (type)
      && ! HONOR_INFINITIES (type))
  { build_minus_one_cst (type); }))

/* In IEEE floating point, x/1 is not equivalent to x for snans.  */
(simplify
 (rdiv @0 real_onep)
 (if (!HONOR_SNANS (type))
  (non_lvalue @0)))

/* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
(simplify
 (rdiv @0 real_minus_onep)
 (if (!HONOR_SNANS (type))
  (negate @0)))

/* If ARG1 is a constant, we can convert this to a multiply by the
   reciprocal.  This does not have the same rounding properties,
   so only do this if -freciprocal-math.  We can actually
   always safely do it if ARG1 is a power of two, but it's hard to
   tell if it is or not in a portable manner.  */
(for cst (REAL_CST COMPLEX_CST VECTOR_CST)
 (simplify
  (rdiv @0 cst@1)
  (if (optimize)
   (if (flag_reciprocal_math
	&& !real_zerop (@1))
    (with
     { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
     (if (tem)
      (mult @0 { tem; } ))))
   (if (cst != COMPLEX_CST)
    (with { tree inverse = exact_inverse (type, @1); }
     (if (inverse)
      (mult @0 { inverse; } )))))))

/* Same applies to modulo operations, but fold is inconsistent here
   and simplifies 0 % x to 0, only preserving literal 0 % 0.  */
(for mod (ceil_mod floor_mod round_mod trunc_mod)
 /* 0 % X is always zero.  */
 (simplify
  (mod integer_zerop@0 @1)
  /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
  (if (!integer_zerop (@1))
   @0))
 /* X % 1 is always zero.  */
 (simplify
  (mod @0 integer_onep)
  { build_zero_cst (type); })
 /* X % -1 is zero.  */
 (simplify
  (mod @0 integer_minus_onep@1)
  (if (!TYPE_UNSIGNED (type))
   { build_zero_cst (type); }))
 /* (X % Y) % Y is just X % Y.  */
 (simplify
  (mod (mod@2 @0 @1) @1)
  @2))

/* X % -C is the same as X % C.  */
(simplify
 (trunc_mod @0 INTEGER_CST@1)
  (if (TYPE_SIGN (type) == SIGNED
       && !TREE_OVERFLOW (@1)
       && wi::neg_p (@1)
       && !TYPE_OVERFLOW_TRAPS (type)
       /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
       && !sign_bit_p (@1, @1))
   (trunc_mod @0 (negate @1))))

/* X % -Y is the same as X % Y.  */
(simplify
 (trunc_mod @0 (convert? (negate @1)))
 (if (!TYPE_UNSIGNED (type)
      && !TYPE_OVERFLOW_TRAPS (type)
      && tree_nop_conversion_p (type, TREE_TYPE (@1)))
  (trunc_mod @0 (convert @1))))

/* X - (X / Y) * Y is the same as X % Y.  */
(simplify
 (minus (convert1? @0) (convert2? (mult (trunc_div @0 @1) @1)))
 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
  (trunc_mod (convert @0) (convert @1))))

/* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
   i.e. "X % C" into "X & (C - 1)", if X and C are positive.
   Also optimize A % (C << N)  where C is a power of 2,
   to A & ((C << N) - 1).  */
(match (power_of_two_cand @1)
 INTEGER_CST@1)
(match (power_of_two_cand @1)
 (lshift INTEGER_CST@1 @2))
(for mod (trunc_mod floor_mod)
 (simplify
  (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
  (if ((TYPE_UNSIGNED (type)
	|| tree_expr_nonnegative_p (@0))
	&& tree_nop_conversion_p (type, TREE_TYPE (@3))
	&& integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
   (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))

/* X % Y is smaller than Y.  */
(for cmp (lt ge)
 (simplify
  (cmp (trunc_mod @0 @1) @1)
  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
   { constant_boolean_node (cmp == LT_EXPR, type); })))
(for cmp (gt le)
 (simplify
  (cmp @1 (trunc_mod @0 @1))
  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
   { constant_boolean_node (cmp == GT_EXPR, type); })))

/* x | ~0 -> ~0  */
(simplify
  (bit_ior @0 integer_all_onesp@1)
  @1)

/* x & 0 -> 0  */
(simplify
  (bit_and @0 integer_zerop@1)
  @1)

/* ~x | x -> -1 */
/* ~x ^ x -> -1 */
/* ~x + x -> -1 */
(for op (bit_ior bit_xor plus)
 (simplify
  (op:c (convert? @0) (convert? (bit_not @0)))
  (convert { build_all_ones_cst (TREE_TYPE (@0)); })))

/* x ^ x -> 0 */
(simplify
  (bit_xor @0 @0)
  { build_zero_cst (type); })

/* Canonicalize X ^ ~0 to ~X.  */
(simplify
  (bit_xor @0 integer_all_onesp@1)
  (bit_not @0))

/* x & ~0 -> x  */
(simplify
 (bit_and @0 integer_all_onesp)
  (non_lvalue @0))

/* x & x -> x,  x | x -> x  */
(for bitop (bit_and bit_ior)
 (simplify
  (bitop @0 @0)
  (non_lvalue @0)))

/* x + (x & 1) -> (x + 1) & ~1 */
(simplify
 (plus:c @0 (bit_and@2 @0 integer_onep@1))
 (if (single_use (@2))
  (bit_and (plus @0 @1) (bit_not @1))))

/* x & ~(x & y) -> x & ~y */
/* x | ~(x | y) -> x | ~y  */
(for bitop (bit_and bit_ior)
 (simplify
  (bitop:c @0 (bit_not (bitop:c@2 @0 @1)))
   (if (single_use (@2))
    (bitop @0 (bit_not @1)))))

/* (x | y) & ~x -> y & ~x */
/* (x & y) | ~x -> y | ~x */
(for bitop (bit_and bit_ior)
     rbitop (bit_ior bit_and)
 (simplify
  (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
  (bitop @1 @2)))

/* (x & y) ^ (x | y) -> x ^ y */
(simplify
 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
 (bit_xor @0 @1))

/* (x ^ y) ^ (x | y) -> x & y */
(simplify
 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
 (bit_and @0 @1))

/* (x & y) + (x ^ y) -> x | y */
/* (x & y) | (x ^ y) -> x | y */
/* (x & y) ^ (x ^ y) -> x | y */
(for op (plus bit_ior bit_xor)
 (simplify
  (op:c (bit_and @0 @1) (bit_xor @0 @1))
  (bit_ior @0 @1)))

/* (x & y) + (x | y) -> x + y */
(simplify
 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
 (plus @0 @1))

/* (x + y) - (x | y) -> x & y */
(simplify
 (minus (plus @0 @1) (bit_ior @0 @1))
 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
      && !TYPE_SATURATING (type))
  (bit_and @0 @1)))

/* (x + y) - (x & y) -> x | y */
(simplify
 (minus (plus @0 @1) (bit_and @0 @1))
 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
      && !TYPE_SATURATING (type))
  (bit_ior @0 @1)))

/* (x | y) - (x ^ y) -> x & y */
(simplify
 (minus (bit_ior @0 @1) (bit_xor @0 @1))
 (bit_and @0 @1))

/* (x | y) - (x & y) -> x ^ y */
(simplify
 (minus (bit_ior @0 @1) (bit_and @0 @1))
 (bit_xor @0 @1))

/* (x | y) & ~(x & y) -> x ^ y */
(simplify
 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
 (bit_xor @0 @1))

/* (x | y) & (~x ^ y) -> x & y */
(simplify
 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
 (bit_and @0 @1))

/* ~x & ~y -> ~(x | y)
   ~x | ~y -> ~(x & y) */
(for op (bit_and bit_ior)
     rop (bit_ior bit_and)
 (simplify
  (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
   (bit_not (rop (convert @0) (convert @1))))))

/* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
   with a constant, and the two constants have no bits in common,
   we should treat this as a BIT_IOR_EXPR since this may produce more
   simplifications.  */
(simplify
 (bit_xor (convert1? (bit_and@4 @0 INTEGER_CST@1))
          (convert2? (bit_and@5 @2 INTEGER_CST@3)))
 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
      && tree_nop_conversion_p (type, TREE_TYPE (@2))
      && wi::bit_and (@1, @3) == 0)
  (bit_ior (convert @4) (convert @5))))

/* (X | Y) ^ X -> Y & ~ X*/
(simplify
 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
  (convert (bit_and @1 (bit_not @0)))))

/* Convert ~X ^ ~Y to X ^ Y.  */
(simplify
 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
      && tree_nop_conversion_p (type, TREE_TYPE (@1)))
  (bit_xor (convert @0) (convert @1))))

/* Convert ~X ^ C to X ^ ~C.  */
(simplify
 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
  (bit_xor (convert @0) (bit_not @1))))

/* Fold (X & Y) ^ Y as ~X & Y.  */
(simplify
 (bit_xor:c (bit_and:c @0 @1) @1)
 (bit_and (bit_not @0) @1))


(simplify
 (abs (abs@1 @0))
 @1)
(simplify
 (abs (negate @0))
 (abs @0))
(simplify
 (abs tree_expr_nonnegative_p@0)
 @0)

/* A - B -> A + (-B) if B is easily negatable.  This just covers
   very few cases of "easily negatable", effectively inlining negate_expr_p.  */
(simplify
 (minus @0 INTEGER_CST@1)
 (if ((INTEGRAL_TYPE_P (type)
       && TYPE_OVERFLOW_WRAPS (type))
      || (!TYPE_OVERFLOW_SANITIZED (type)
	  && may_negate_without_overflow_p (@1)))
  (plus @0 (negate @1))))
(simplify
 (minus @0 REAL_CST@1)
 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
  (plus @0 (negate @1))))
(simplify
 (minus @0 VECTOR_CST@1)
 (if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
  (plus @0 (negate @1))))


/* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
   when profitable.
   For bitwise binary operations apply operand conversions to the
   binary operation result instead of to the operands.  This allows
   to combine successive conversions and bitwise binary operations.
   We combine the above two cases by using a conditional convert.  */
(for bitop (bit_and bit_ior bit_xor)
 (simplify
  (bitop (convert @0) (convert? @1))
  (if (((TREE_CODE (@1) == INTEGER_CST
	 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
	 && int_fits_type_p (@1, TREE_TYPE (@0)))
	|| types_match (@0, @1))
       /* ???  This transform conflicts with fold-const.c doing
	  Convert (T)(x & c) into (T)x & (T)c, if c is an integer
	  constants (if x has signed type, the sign bit cannot be set
	  in c).  This folds extension into the BIT_AND_EXPR.
	  Restrict it to GIMPLE to avoid endless recursions.  */
       && (bitop != BIT_AND_EXPR || GIMPLE)
       && (/* That's a good idea if the conversion widens the operand, thus
	      after hoisting the conversion the operation will be narrower.  */
	   TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
	   /* It's also a good idea if the conversion is to a non-integer
	      mode.  */
	   || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
	   /* Or if the precision of TO is not the same as the precision
	      of its mode.  */
	   || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
   (convert (bitop @0 (convert @1))))))

(for bitop (bit_and bit_ior)
     rbitop (bit_ior bit_and)
  /* (x | y) & x -> x */
  /* (x & y) | x -> x */
 (simplify
  (bitop:c (rbitop:c @0 @1) @0)
  @0)
 /* (~x | y) & x -> x & y */
 /* (~x & y) | x -> x | y */
 (simplify
  (bitop:c (rbitop:c (bit_not @0) @1) @0)
  (bitop @0 @1)))

/* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
(for bitop (bit_and bit_ior bit_xor)
 (simplify
  (bitop (bit_and:c @0 @1) (bit_and @2 @1))
  (bit_and (bitop @0 @2) @1)))

/* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
(simplify
  (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
  (bit_ior (bit_and @0 @2) (bit_and @1 @2)))

/* Combine successive equal operations with constants.  */
(for bitop (bit_and bit_ior bit_xor)
 (simplify
  (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
  (bitop @0 (bitop @1 @2))))

/* Try simple folding for X op !X, and X op X with the help
   of the truth_valued_p and logical_inverted_value predicates.  */
(match truth_valued_p
 @0
 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
(for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
 (match truth_valued_p
  (op @0 @1)))
(match truth_valued_p
  (truth_not @0))

(match (logical_inverted_value @0)
 (bit_not truth_valued_p@0))
(match (logical_inverted_value @0)
 (eq @0 integer_zerop))
(match (logical_inverted_value @0)
 (ne truth_valued_p@0 integer_truep))
(match (logical_inverted_value @0)
 (bit_xor truth_valued_p@0 integer_truep))

/* X & !X -> 0.  */
(simplify
 (bit_and:c @0 (logical_inverted_value @0))
 { build_zero_cst (type); })
/* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
(for op (bit_ior bit_xor)
 (simplify
  (op:c truth_valued_p@0 (logical_inverted_value @0))
  { constant_boolean_node (true, type); }))

/* If arg1 and arg2 are booleans (or any single bit type)
   then try to simplify:

   (~X & Y) -> X < Y
   (X & ~Y) -> Y < X
   (~X | Y) -> X <= Y
   (X | ~Y) -> Y <= X

   But only do this if our result feeds into a comparison as
   this transformation is not always a win, particularly on
   targets with and-not instructions.
   -> simplify_bitwise_binary_boolean */
(simplify
  (ne (bit_and:c (bit_not @0) @1) integer_zerop)
  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
       && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
   (lt @0 @1)))
(simplify
  (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
       && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
   (le @0 @1)))

/* ~~x -> x */
(simplify
  (bit_not (bit_not @0))
  @0)

/* Convert ~ (-A) to A - 1.  */
(simplify
 (bit_not (convert? (negate @0)))
 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
  (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))

/* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
(simplify
 (bit_not (convert? (minus @0 integer_each_onep)))
 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
  (convert (negate @0))))
(simplify
 (bit_not (convert? (plus @0 integer_all_onesp)))
 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
  (convert (negate @0))))

/* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
(simplify
 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
  (convert (bit_xor @0 (bit_not @1)))))
(simplify
 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
  (convert (bit_xor @0 @1))))

/* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
(simplify
  (bit_ior:c (bit_and:c@3 @0 (bit_not @2)) (bit_and:c@4 @1 @2))
  (if (single_use (@3) && single_use (@4))
   (bit_xor (bit_and (bit_xor @0 @1) @2) @0)))


/* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
(simplify
  (pointer_plus (pointer_plus@2 @0 @1) @3)
  (if (single_use (@2)
       || (TREE_CODE (@1) == INTEGER_CST && TREE_CODE (@3) == INTEGER_CST))
   (pointer_plus @0 (plus @1 @3))))

/* Pattern match
     tem1 = (long) ptr1;
     tem2 = (long) ptr2;
     tem3 = tem2 - tem1;
     tem4 = (unsigned long) tem3;
     tem5 = ptr1 + tem4;
   and produce
     tem5 = ptr2;  */
(simplify
  (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
  /* Conditionally look through a sign-changing conversion.  */
  (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
       && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
	    || (GENERIC && type == TREE_TYPE (@1))))
   @1))

/* Pattern match
     tem = (sizetype) ptr;
     tem = tem & algn;
     tem = -tem;
     ... = ptr p+ tem;
   and produce the simpler and easier to analyze with respect to alignment
     ... = ptr & ~algn;  */
(simplify
  (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
  (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
   (bit_and @0 { algn; })))

/* Try folding difference of addresses.  */
(simplify
 (minus (convert ADDR_EXPR@0) (convert @1))
 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
  (with { HOST_WIDE_INT diff; }
   (if (ptr_difference_const (@0, @1, &diff))
    { build_int_cst_type (type, diff); }))))
(simplify
 (minus (convert @0) (convert ADDR_EXPR@1))
 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
  (with { HOST_WIDE_INT diff; }
   (if (ptr_difference_const (@0, @1, &diff))
    { build_int_cst_type (type, diff); }))))

/* If arg0 is derived from the address of an object or function, we may
   be able to fold this expression using the object or function's
   alignment.  */
(simplify
 (bit_and (convert? @0) INTEGER_CST@1)
 (if (POINTER_TYPE_P (TREE_TYPE (@0))
      && tree_nop_conversion_p (type, TREE_TYPE (@0)))
  (with
   {
     unsigned int align;
     unsigned HOST_WIDE_INT bitpos;
     get_pointer_alignment_1 (@0, &align, &bitpos);
   }
   (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
    { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))


/* We can't reassociate at all for saturating types.  */
(if (!TYPE_SATURATING (type))

 /* Contract negates.  */
 /* A + (-B) -> A - B */
 (simplify
  (plus:c (convert1? @0) (convert2? (negate @1)))
  /* Apply STRIP_NOPS on @0 and the negate.  */
  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
       && tree_nop_conversion_p (type, TREE_TYPE (@1))
       && !TYPE_OVERFLOW_SANITIZED (type))
   (minus (convert @0) (convert @1))))
 /* A - (-B) -> A + B */
 (simplify
  (minus (convert1? @0) (convert2? (negate @1)))
  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
       && tree_nop_conversion_p (type, TREE_TYPE (@1))
       && !TYPE_OVERFLOW_SANITIZED (type))
   (plus (convert @0) (convert @1))))
 /* -(-A) -> A */
 (simplify
  (negate (convert? (negate @1)))
  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
       && !TYPE_OVERFLOW_SANITIZED (type))
   (convert @1)))

 /* We can't reassociate floating-point unless -fassociative-math
    or fixed-point plus or minus because of saturation to +-Inf.  */
 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
      && !FIXED_POINT_TYPE_P (type))

  /* Match patterns that allow contracting a plus-minus pair
     irrespective of overflow issues.  */
  /* (A +- B) - A       ->  +- B */
  /* (A +- B) -+ B      ->  A */
  /* A - (A +- B)       -> -+ B */
  /* A +- (B -+ A)      ->  +- B */
  (simplify
    (minus (plus:c @0 @1) @0)
    @1)
  (simplify
    (minus (minus @0 @1) @0)
    (negate @1))
  (simplify
    (plus:c (minus @0 @1) @1)
    @0)
  (simplify
   (minus @0 (plus:c @0 @1))
   (negate @1))
  (simplify
   (minus @0 (minus @0 @1))
   @1)

  /* (A +- CST) +- CST -> A + CST  */
  (for outer_op (plus minus)
   (for inner_op (plus minus)
    (simplify
     (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
     /* If the constant operation overflows we cannot do the transform
	as we would introduce undefined overflow, for example
	with (a - 1) + INT_MIN.  */
     (with { tree cst = fold_binary (outer_op == inner_op
				     ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
      (if (cst && !TREE_OVERFLOW (cst))
       (inner_op @0 { cst; } ))))))

  /* (CST - A) +- CST -> CST - A  */
  (for outer_op (plus minus)
   (simplify
    (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
    (with { tree cst = fold_binary (outer_op, type, @1, @2); }
     (if (cst && !TREE_OVERFLOW (cst))
      (minus { cst; } @0)))))

  /* ~A + A -> -1 */
  (simplify
   (plus:c (bit_not @0) @0)
   (if (!TYPE_OVERFLOW_TRAPS (type))
    { build_all_ones_cst (type); }))

  /* ~A + 1 -> -A */
  (simplify
   (plus (convert? (bit_not @0)) integer_each_onep)
   (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
    (negate (convert @0))))

  /* -A - 1 -> ~A */
  (simplify
   (minus (convert? (negate @0)) integer_each_onep)
   (if (!TYPE_OVERFLOW_TRAPS (type)
	&& tree_nop_conversion_p (type, TREE_TYPE (@0)))
    (bit_not (convert @0))))

  /* -1 - A -> ~A */
  (simplify
   (minus integer_all_onesp @0)
   (bit_not @0))

  /* (T)(P + A) - (T)P -> (T) A */
  (for add (plus pointer_plus)
   (simplify
    (minus (convert (add @0 @1))
     (convert @0))
    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
	 /* For integer types, if A has a smaller type
	    than T the result depends on the possible
	    overflow in P + A.
	    E.g. T=size_t, A=(unsigned)429497295, P>0.
	    However, if an overflow in P + A would cause
	    undefined behavior, we can assume that there
	    is no overflow.  */
	 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
	     && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
	 /* For pointer types, if the conversion of A to the
	    final type requires a sign- or zero-extension,
	    then we have to punt - it is not defined which
	    one is correct.  */
	 || (POINTER_TYPE_P (TREE_TYPE (@0))
	     && TREE_CODE (@1) == INTEGER_CST
	     && tree_int_cst_sign_bit (@1) == 0))
     (convert @1))))))


/* Simplifications of MIN_EXPR and MAX_EXPR.  */

(for minmax (min max)
 (simplify
  (minmax @0 @0)
  @0))
(simplify
 (min @0 @1)
 (if (INTEGRAL_TYPE_P (type)
      && TYPE_MIN_VALUE (type)
      && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
  @1))
(simplify
 (max @0 @1)
 (if (INTEGRAL_TYPE_P (type)
      && TYPE_MAX_VALUE (type)
      && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
  @1))


/* Simplifications of shift and rotates.  */

(for rotate (lrotate rrotate)
 (simplify
  (rotate integer_all_onesp@0 @1)
  @0))

/* Optimize -1 >> x for arithmetic right shifts.  */
(simplify
 (rshift integer_all_onesp@0 @1)
 (if (!TYPE_UNSIGNED (type)
      && tree_expr_nonnegative_p (@1))
  @0))

(for shiftrotate (lrotate rrotate lshift rshift)
 (simplify
  (shiftrotate @0 integer_zerop)
  (non_lvalue @0))
 (simplify
  (shiftrotate integer_zerop@0 @1)
  @0)
 /* Prefer vector1 << scalar to vector1 << vector2
    if vector2 is uniform.  */
 (for vec (VECTOR_CST CONSTRUCTOR)
  (simplify
   (shiftrotate @0 vec@1)
   (with { tree tem = uniform_vector_p (@1); }
    (if (tem)
     (shiftrotate @0 { tem; }))))))

/* Rewrite an LROTATE_EXPR by a constant into an
   RROTATE_EXPR by a new constant.  */
(simplify
 (lrotate @0 INTEGER_CST@1)
 (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
			    build_int_cst (TREE_TYPE (@1),
					   element_precision (type)), @1); }))

/* ((1 << A) & 1) != 0 -> A == 0
   ((1 << A) & 1) == 0 -> A != 0 */
(for cmp (ne eq)
     icmp (eq ne)
 (simplify
  (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
  (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))

/* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
   (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
   if CST2 != 0.  */
(for cmp (ne eq)
 (simplify
  (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
  (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
   (if (cand < 0
	|| (!integer_zerop (@2)
	    && wi::ne_p (wi::lshift (@0, cand), @2)))
    { constant_boolean_node (cmp == NE_EXPR, type); })
   (if (!integer_zerop (@2)
	&& wi::eq_p (wi::lshift (@0, cand), @2))
    (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); })))))

/* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
        (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
   if the new mask might be further optimized.  */
(for shift (lshift rshift)
 (simplify
  (bit_and (convert?@4 (shift@5 (convert1?@3 @0) INTEGER_CST@1)) INTEGER_CST@2)
   (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
	&& TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
	&& tree_fits_uhwi_p (@1)
	&& tree_to_uhwi (@1) > 0
	&& tree_to_uhwi (@1) < TYPE_PRECISION (type))
    (with
     {
       unsigned int shiftc = tree_to_uhwi (@1);
       unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
       unsigned HOST_WIDE_INT newmask, zerobits = 0;
       tree shift_type = TREE_TYPE (@3);
       unsigned int prec;

       if (shift == LSHIFT_EXPR)
	 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
       else if (shift == RSHIFT_EXPR
		&& (TYPE_PRECISION (shift_type)
		    == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
	 {
	   prec = TYPE_PRECISION (TREE_TYPE (@3));
	   tree arg00 = @0;
	   /* See if more bits can be proven as zero because of
	      zero extension.  */
	   if (@3 != @0
	       && TYPE_UNSIGNED (TREE_TYPE (@0)))
	     {
	       tree inner_type = TREE_TYPE (@0);
	       if ((TYPE_PRECISION (inner_type)
		    == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
		   && TYPE_PRECISION (inner_type) < prec)
		 {
		   prec = TYPE_PRECISION (inner_type);
		   /* See if we can shorten the right shift.  */
		   if (shiftc < prec)
		     shift_type = inner_type;
		   /* Otherwise X >> C1 is all zeros, so we'll optimize
		      it into (X, 0) later on by making sure zerobits
		      is all ones.  */
		 }
	     }
	   zerobits = ~(unsigned HOST_WIDE_INT) 0;
	   if (shiftc < prec)
	     {
	       zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
	       zerobits <<= prec - shiftc;
	     }
	   /* For arithmetic shift if sign bit could be set, zerobits
	      can contain actually sign bits, so no transformation is
	      possible, unless MASK masks them all away.  In that
	      case the shift needs to be converted into logical shift.  */
	   if (!TYPE_UNSIGNED (TREE_TYPE (@3))
	       && prec == TYPE_PRECISION (TREE_TYPE (@3)))
	     {
	       if ((mask & zerobits) == 0)
		 shift_type = unsigned_type_for (TREE_TYPE (@3));
	       else
		 zerobits = 0;
	     }
	 }
     }
     /* ((X << 16) & 0xff00) is (X, 0).  */
     (if ((mask & zerobits) == mask)
      { build_int_cst (type, 0); })
     (with { newmask = mask | zerobits; }
      (if (newmask != mask && (newmask & (newmask + 1)) == 0)
       (with
	{
	  /* Only do the transformation if NEWMASK is some integer
	     mode's mask.  */
	  for (prec = BITS_PER_UNIT;
	       prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
	    if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
	      break;
	}
	(if (prec < HOST_BITS_PER_WIDE_INT
	     || newmask == ~(unsigned HOST_WIDE_INT) 0)
	 (with
	  { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
	  (if (!tree_int_cst_equal (newmaskt, @2))
	   (if (shift_type != TREE_TYPE (@3)
		&& single_use (@4) && single_use (@5))
	    (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; }))
	   (if (shift_type == TREE_TYPE (@3))
	    (bit_and @4 { newmaskt; }))))))))))))

/* Simplifications of conversions.  */

/* Basic strip-useless-type-conversions / strip_nops.  */
(for cvt (convert view_convert float fix_trunc)
 (simplify
  (cvt @0)
  (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
       || (GENERIC && type == TREE_TYPE (@0)))
   @0)))

/* Contract view-conversions.  */
(simplify
  (view_convert (view_convert @0))
  (view_convert @0))

/* For integral conversions with the same precision or pointer
   conversions use a NOP_EXPR instead.  */
(simplify
  (view_convert @0)
  (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
       && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
   (convert @0)))

/* Strip inner integral conversions that do not change precision or size.  */
(simplify
  (view_convert (convert@0 @1))
  (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
       && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
       && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
       && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
   (view_convert @1)))

/* Re-association barriers around constants and other re-association
   barriers can be removed.  */
(simplify
 (paren CONSTANT_CLASS_P@0)
 @0)
(simplify
 (paren (paren@1 @0))
 @1)

/* Handle cases of two conversions in a row.  */
(for ocvt (convert float fix_trunc)
 (for icvt (convert float)
  (simplify
   (ocvt (icvt@1 @0))
   (with
    {
      tree inside_type = TREE_TYPE (@0);
      tree inter_type = TREE_TYPE (@1);
      int inside_int = INTEGRAL_TYPE_P (inside_type);
      int inside_ptr = POINTER_TYPE_P (inside_type);
      int inside_float = FLOAT_TYPE_P (inside_type);
      int inside_vec = VECTOR_TYPE_P (inside_type);
      unsigned int inside_prec = TYPE_PRECISION (inside_type);
      int inside_unsignedp = TYPE_UNSIGNED (inside_type);
      int inter_int = INTEGRAL_TYPE_P (inter_type);
      int inter_ptr = POINTER_TYPE_P (inter_type);
      int inter_float = FLOAT_TYPE_P (inter_type);
      int inter_vec = VECTOR_TYPE_P (inter_type);
      unsigned int inter_prec = TYPE_PRECISION (inter_type);
      int inter_unsignedp = TYPE_UNSIGNED (inter_type);
      int final_int = INTEGRAL_TYPE_P (type);
      int final_ptr = POINTER_TYPE_P (type);
      int final_float = FLOAT_TYPE_P (type);
      int final_vec = VECTOR_TYPE_P (type);
      unsigned int final_prec = TYPE_PRECISION (type);
      int final_unsignedp = TYPE_UNSIGNED (type);
    }
   /* In addition to the cases of two conversions in a row
      handled below, if we are converting something to its own
      type via an object of identical or wider precision, neither
      conversion is needed.  */
   (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
	 || (GENERIC
	     && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
	&& (((inter_int || inter_ptr) && final_int)
	    || (inter_float && final_float))
	&& inter_prec >= final_prec)
    (ocvt @0))

   /* Likewise, if the intermediate and initial types are either both
      float or both integer, we don't need the middle conversion if the
      former is wider than the latter and doesn't change the signedness
      (for integers).  Avoid this if the final type is a pointer since
      then we sometimes need the middle conversion.  Likewise if the
      final type has a precision not equal to the size of its mode.  */
   (if (((inter_int && inside_int) || (inter_float && inside_float))
	&& (final_int || final_float)
	&& inter_prec >= inside_prec
	&& (inter_float || inter_unsignedp == inside_unsignedp)
	&& ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
	      && TYPE_MODE (type) == TYPE_MODE (inter_type)))
    (ocvt @0))

   /* If we have a sign-extension of a zero-extended value, we can
      replace that by a single zero-extension.  Likewise if the
      final conversion does not change precision we can drop the
      intermediate conversion.  */
   (if (inside_int && inter_int && final_int
	&& ((inside_prec < inter_prec && inter_prec < final_prec
	     && inside_unsignedp && !inter_unsignedp)
	    || final_prec == inter_prec))
    (ocvt @0))

   /* Two conversions in a row are not needed unless:
	- some conversion is floating-point (overstrict for now), or
	- some conversion is a vector (overstrict for now), or
	- the intermediate type is narrower than both initial and
	  final, or
	- the intermediate type and innermost type differ in signedness,
	  and the outermost type is wider than the intermediate, or
	- the initial type is a pointer type and the precisions of the
	  intermediate and final types differ, or
	- the final type is a pointer type and the precisions of the
	  initial and intermediate types differ.  */
   (if (! inside_float && ! inter_float && ! final_float
	&& ! inside_vec && ! inter_vec && ! final_vec
	&& (inter_prec >= inside_prec || inter_prec >= final_prec)
	&& ! (inside_int && inter_int
	      && inter_unsignedp != inside_unsignedp
	      && inter_prec < final_prec)
	&& ((inter_unsignedp && inter_prec > inside_prec)
	    == (final_unsignedp && final_prec > inter_prec))
	&& ! (inside_ptr && inter_prec != final_prec)
	&& ! (final_ptr && inside_prec != inter_prec)
	&& ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
	      && TYPE_MODE (type) == TYPE_MODE (inter_type)))
    (ocvt @0))

   /* A truncation to an unsigned type (a zero-extension) should be
      canonicalized as bitwise and of a mask.  */
   (if (final_int && inter_int && inside_int
	&& final_prec == inside_prec
	&& final_prec > inter_prec
	&& inter_unsignedp)
    (convert (bit_and @0 { wide_int_to_tree
	                     (inside_type,
			      wi::mask (inter_prec, false,
					TYPE_PRECISION (inside_type))); })))

   /* If we are converting an integer to a floating-point that can
      represent it exactly and back to an integer, we can skip the
      floating-point conversion.  */
   (if (GIMPLE /* PR66211 */
	&& inside_int && inter_float && final_int &&
	(unsigned) significand_size (TYPE_MODE (inter_type))
	>= inside_prec - !inside_unsignedp)
    (convert @0))))))

/* If we have a narrowing conversion to an integral type that is fed by a
   BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
   masks off bits outside the final type (and nothing else).  */
(simplify
  (convert (bit_and @0 INTEGER_CST@1))
  (if (INTEGRAL_TYPE_P (type)
       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
       && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
						    TYPE_PRECISION (type)), 0))
   (convert @0)))


/* (X /[ex] A) * A -> X.  */
(simplify
  (mult (convert? (exact_div @0 @1)) @1)
  /* Look through a sign-changing conversion.  */
  (convert @0))

/* Canonicalization of binary operations.  */

/* Convert X + -C into X - C.  */
(simplify
 (plus @0 REAL_CST@1)
 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
  (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
   (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
    (minus @0 { tem; })))))

/* Convert x+x into x*2.0.  */
(simplify
 (plus @0 @0)
 (if (SCALAR_FLOAT_TYPE_P (type))
  (mult @0 { build_real (type, dconst2); })))

(simplify
 (minus integer_zerop @1)
 (negate @1))

/* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
   ARG0 is zero and X + ARG0 reduces to X, since that would mean
   (-ARG1 + ARG0) reduces to -ARG1.  */
(simplify
 (minus real_zerop@0 @1)
 (if (fold_real_zero_addition_p (type, @0, 0))
  (negate @1)))

/* Transform x * -1 into -x.  */
(simplify
 (mult @0 integer_minus_onep)
 (negate @0))

/* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
(simplify
 (complex (realpart @0) (imagpart @0))
 @0)
(simplify
 (realpart (complex @0 @1))
 @0)
(simplify
 (imagpart (complex @0 @1))
 @1)


/* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
(for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
 (simplify
  (bswap (bswap @0))
  @0)
 (simplify
  (bswap (bit_not (bswap @0)))
  (bit_not @0))
 (for bitop (bit_xor bit_ior bit_and)
  (simplify
   (bswap (bitop:c (bswap @0) @1))
   (bitop @0 (bswap @1)))))


/* Combine COND_EXPRs and VEC_COND_EXPRs.  */

/* Simplify constant conditions.
   Only optimize constant conditions when the selected branch
   has the same type as the COND_EXPR.  This avoids optimizing
   away "c ? x : throw", where the throw has a void type.
   Note that we cannot throw away the fold-const.c variant nor
   this one as we depend on doing this transform before possibly
   A ? B : B -> B triggers and the fold-const.c one can optimize
   0 ? A : B to B even if A has side-effects.  Something
   genmatch cannot handle.  */
(simplify
 (cond INTEGER_CST@0 @1 @2)
 (if (integer_zerop (@0)
      && (!VOID_TYPE_P (TREE_TYPE (@2))
	  || VOID_TYPE_P (type)))
  @2)
 (if (!integer_zerop (@0)
      && (!VOID_TYPE_P (TREE_TYPE (@1))
	  || VOID_TYPE_P (type)))
  @1))
(simplify
 (vec_cond VECTOR_CST@0 @1 @2)
 (if (integer_all_onesp (@0))
  @1)
 (if (integer_zerop (@0))
  @2))

(for cnd (cond vec_cond)
 /* A ? B : (A ? X : C) -> A ? B : C.  */
 (simplify
  (cnd @0 (cnd @0 @1 @2) @3)
  (cnd @0 @1 @3))
 (simplify
  (cnd @0 @1 (cnd @0 @2 @3))
  (cnd @0 @1 @3))

 /* A ? B : B -> B.  */
 (simplify
  (cnd @0 @1 @1)
  @1)

 /* !A ? B : C -> A ? C : B.  */
 (simplify
  (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
  (cnd @0 @2 @1)))

/* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons
   return all-1 or all-0 results.  */
/* ??? We could instead convert all instances of the vec_cond to negate,
   but that isn't necessarily a win on its own.  */
(simplify
 (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
 (if (VECTOR_TYPE_P (type)
      && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
      && (TYPE_MODE (TREE_TYPE (type))
          == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
  (minus @3 (view_convert @0))))

/* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C).  */
(simplify
 (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
 (if (VECTOR_TYPE_P (type)
      && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
      && (TYPE_MODE (TREE_TYPE (type))
          == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
  (plus @3 (view_convert @0))))


/* Simplifications of comparisons.  */

/* We can simplify a logical negation of a comparison to the
   inverted comparison.  As we cannot compute an expression
   operator using invert_tree_comparison we have to simulate
   that with expression code iteration.  */
(for cmp (tcc_comparison)
     icmp (inverted_tcc_comparison)
     ncmp (inverted_tcc_comparison_with_nans)
 /* Ideally we'd like to combine the following two patterns
    and handle some more cases by using
      (logical_inverted_value (cmp @0 @1))
    here but for that genmatch would need to "inline" that.
    For now implement what forward_propagate_comparison did.  */
 (simplify
  (bit_not (cmp @0 @1))
  (if (VECTOR_TYPE_P (type)
       || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
   /* Comparison inversion may be impossible for trapping math,
      invert_tree_comparison will tell us.  But we can't use
      a computed operator in the replacement tree thus we have
      to play the trick below.  */
   (with { enum tree_code ic = invert_tree_comparison
             (cmp, HONOR_NANS (@0)); }
    (if (ic == icmp)
     (icmp @0 @1))
    (if (ic == ncmp)
     (ncmp @0 @1)))))
 (simplify
  (bit_xor (cmp @0 @1) integer_truep)
  (with { enum tree_code ic = invert_tree_comparison
            (cmp, HONOR_NANS (@0)); }
   (if (ic == icmp)
    (icmp @0 @1))
   (if (ic == ncmp)
    (ncmp @0 @1)))))

/* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
   ??? The transformation is valid for the other operators if overflow
   is undefined for the type, but performing it here badly interacts
   with the transformation in fold_cond_expr_with_comparison which
   attempts to synthetize ABS_EXPR.  */
(for cmp (eq ne)
 (simplify
  (cmp (minus@2 @0 @1) integer_zerop)
  (if (single_use (@2))
   (cmp @0 @1))))

/* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
   signed arithmetic case.  That form is created by the compiler
   often enough for folding it to be of value.  One example is in
   computing loop trip counts after Operator Strength Reduction.  */
(for cmp (simple_comparison)
     scmp (swapped_simple_comparison)
 (simplify
  (cmp (mult @0 INTEGER_CST@1) integer_zerop@2)
  /* Handle unfolded multiplication by zero.  */
  (if (integer_zerop (@1))
   (cmp @1 @2))
  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
       && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
   /* If @1 is negative we swap the sense of the comparison.  */
   (if (tree_int_cst_sgn (@1) < 0)
    (scmp @0 @2))
   (cmp @0 @2))))
 
/* Simplify comparison of something with itself.  For IEEE
   floating-point, we can only do some of these simplifications.  */
(simplify
 (eq @0 @0)
 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
      || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
  { constant_boolean_node (true, type); }))
(for cmp (ge le)
 (simplify
  (cmp @0 @0)
  (eq @0 @0)))
(for cmp (ne gt lt)
 (simplify
  (cmp @0 @0)
  (if (cmp != NE_EXPR
       || ! FLOAT_TYPE_P (TREE_TYPE (@0))
       || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
   { constant_boolean_node (false, type); })))

/* Fold ~X op ~Y as Y op X.  */
(for cmp (simple_comparison)
 (simplify
  (cmp (bit_not @0) (bit_not @1))
  (cmp @1 @0)))

/* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
(for cmp (simple_comparison)
     scmp (swapped_simple_comparison)
 (simplify
  (cmp (bit_not @0) CONSTANT_CLASS_P@1)
  (if (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)
   (scmp @0 (bit_not @1)))))

(for cmp (simple_comparison)
 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
 (simplify
  (cmp (convert@2 @0) (convert? @1))
  (if (FLOAT_TYPE_P (TREE_TYPE (@0))
       && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
	   == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
       && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
	   == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
   (with
    {
      tree type1 = TREE_TYPE (@1);
      if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
        {
	  REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
	  if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
	      && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
	    type1 = float_type_node;
	  if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
	      && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
	    type1 = double_type_node;
        }
      tree newtype
        = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
	   ? TREE_TYPE (@0) : type1); 
    }
    (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
     (cmp (convert:newtype @0) (convert:newtype @1))))))
 
 (simplify
  (cmp @0 REAL_CST@1)
  /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
  /* a CMP (-0) -> a CMP 0  */
  (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
   (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
  /* x != NaN is always true, other ops are always false.  */
  (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
       && ! HONOR_SNANS (@1))
   { constant_boolean_node (cmp == NE_EXPR, type); })
  /* Fold comparisons against infinity.  */
  (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
       && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
   (with
    {
      REAL_VALUE_TYPE max;
      enum tree_code code = cmp;
      bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
      if (neg)
        code = swap_tree_comparison (code);
    }
    /* x > +Inf is always false, if with ignore sNANs.  */
    (if (code == GT_EXPR
    	 && ! HONOR_SNANS (@0))
     { constant_boolean_node (false, type); })
    (if (code == LE_EXPR)
     /* x <= +Inf is always true, if we don't case about NaNs.  */
     (if (! HONOR_NANS (@0))
      { constant_boolean_node (true, type); })
     /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
     (eq @0 @0))
    /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
    (if (code == EQ_EXPR || code == GE_EXPR)
     (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
      (if (neg)
       (lt @0 { build_real (TREE_TYPE (@0), max); }))
      (gt @0 { build_real (TREE_TYPE (@0), max); })))
    /* x < +Inf is always equal to x <= DBL_MAX.  */
    (if (code == LT_EXPR)
     (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
      (if (neg)
       (ge @0 { build_real (TREE_TYPE (@0), max); }))
      (le @0 { build_real (TREE_TYPE (@0), max); })))
    /* x != +Inf is always equal to !(x > DBL_MAX).  */
    (if (code == NE_EXPR)
     (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
      (if (! HONOR_NANS (@0))
       (if (neg)
        (ge @0 { build_real (TREE_TYPE (@0), max); }))
       (le @0 { build_real (TREE_TYPE (@0), max); }))
      (if (neg)
       (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
	        { build_one_cst (type); }))
      (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
       { build_one_cst (type); }))))))

 /* If this is a comparison of a real constant with a PLUS_EXPR
    or a MINUS_EXPR of a real constant, we can convert it into a
    comparison with a revised real constant as long as no overflow
    occurs when unsafe_math_optimizations are enabled.  */
 (if (flag_unsafe_math_optimizations)
  (for op (plus minus)
   (simplify
    (cmp (op @0 REAL_CST@1) REAL_CST@2)
    (with
     {
       tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
			       TREE_TYPE (@1), @2, @1);
     }
     (if (!TREE_OVERFLOW (tem))
      (cmp @0 { tem; }))))))

 /* Likewise, we can simplify a comparison of a real constant with
    a MINUS_EXPR whose first operand is also a real constant, i.e.
    (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
    floating-point types only if -fassociative-math is set.  */
 (if (flag_associative_math)
  (simplify
   (cmp (minus REAL_CST@0 @1) REAL_CST@2)
   (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
    (if (!TREE_OVERFLOW (tem))
     (cmp { tem; } @1)))))

 /* Fold comparisons against built-in math functions.  */
 (if (flag_unsafe_math_optimizations
      && ! flag_errno_math)
  (for sq (SQRT)
   (simplify
    (cmp (sq @0) REAL_CST@1)
    (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
     /* sqrt(x) < y is always false, if y is negative.  */
     (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
      { constant_boolean_node (false, type); })
     /* sqrt(x) > y is always true, if y is negative and we
	don't care about NaNs, i.e. negative values of x.  */
     (if (cmp == NE_EXPR || !HONOR_NANS (@0))
      { constant_boolean_node (true, type); })
     /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
     (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
    (if (cmp == GT_EXPR || cmp == GE_EXPR)
     (with
      {
       	REAL_VALUE_TYPE c2;
	REAL_ARITHMETIC (c2, MULT_EXPR, TREE_REAL_CST (@1), TREE_REAL_CST (@1));
	real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
      }
      (if (REAL_VALUE_ISINF (c2))
       /* sqrt(x) > y is x == +Inf, when y is very large.  */
       (if (HONOR_INFINITIES (@0))
        (eq @0 { build_real (TREE_TYPE (@0), c2); }))
       { constant_boolean_node (false, type); })
      /* sqrt(x) > c is the same as x > c*c.  */
      (cmp @0 { build_real (TREE_TYPE (@0), c2); })))
    (if (cmp == LT_EXPR || cmp == LE_EXPR)
     (with
      {
       	REAL_VALUE_TYPE c2;
	REAL_ARITHMETIC (c2, MULT_EXPR, TREE_REAL_CST (@1), TREE_REAL_CST (@1));
	real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
      }
      (if (REAL_VALUE_ISINF (c2))
       /* sqrt(x) < y is always true, when y is a very large
	  value and we don't care about NaNs or Infinities.  */
       (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
        { constant_boolean_node (true, type); })
       /* sqrt(x) < y is x != +Inf when y is very large and we
	  don't care about NaNs.  */
       (if (! HONOR_NANS (@0))
        (ne @0 { build_real (TREE_TYPE (@0), c2); }))
       /* sqrt(x) < y is x >= 0 when y is very large and we
	  don't care about Infinities.  */
       (if (! HONOR_INFINITIES (@0))
        (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
       /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
       (if (GENERIC)
        (truth_andif
	 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
	 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
      /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
      (if (! REAL_VALUE_ISINF (c2)
           && ! HONOR_NANS (@0))
       (cmp @0 { build_real (TREE_TYPE (@0), c2); }))
      /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
      (if (! REAL_VALUE_ISINF (c2)
           && GENERIC)
       (truth_andif
        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
	(cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))

/* Unordered tests if either argument is a NaN.  */
(simplify
 (bit_ior (unordered @0 @0) (unordered @1 @1))
 (if (types_match (@0, @1))
  (unordered @0 @1)))
(simplify
 (bit_and (ordered @0 @0) (ordered @1 @1))
 (if (types_match (@0, @1))
  (ordered @0 @1)))
(simplify
 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
 @2)
(simplify
 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
 @2)

/* -A CMP -B -> B CMP A.  */
(for cmp (tcc_comparison)
     scmp (swapped_tcc_comparison)
 (simplify
  (cmp (negate @0) (negate @1))
  (if (FLOAT_TYPE_P (TREE_TYPE (@0))
       || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
	   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
   (scmp @0 @1)))
 (simplify
  (cmp (negate @0) CONSTANT_CLASS_P@1)
  (if (FLOAT_TYPE_P (TREE_TYPE (@0))
       || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
	   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
   (with { tree tem = fold_unary (NEGATE_EXPR, TREE_TYPE (@0), @1); }
    (if (tem && !TREE_OVERFLOW (tem))
     (scmp @0 { tem; }))))))


/* Equality compare simplifications from fold_binary  */
(for cmp (eq ne)

 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
    Similarly for NE_EXPR.  */
 (simplify
  (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
  (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
       && wi::bit_and_not (@1, @2) != 0)
   { constant_boolean_node (cmp == NE_EXPR, type); }))

 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
 (simplify
  (cmp (bit_xor @0 @1) integer_zerop)
  (cmp @0 @1))

 /* (X ^ Y) == Y becomes X == 0.
    Likewise (X ^ Y) == X becomes Y == 0.  */
 (simplify
  (cmp:c (bit_xor:c @0 @1) @0)
  (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))

 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
 (simplify
  (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
  (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
   (cmp @0 (bit_xor @1 (convert @2))))))

/* Simplification of math builtins.  */

/* fold_builtin_logarithm */
(if (flag_unsafe_math_optimizations)
 /* Special case, optimize logN(expN(x)) = x.  */
 (for logs (LOG LOG2 LOG10)
      exps (EXP EXP2 EXP10)
  (simplify
   (logs (exps @0))
    @0))
 /* Optimize logN(func()) for various exponential functions.  We
    want to determine the value "x" and the power "exponent" in
    order to transform logN(x**exponent) into exponent*logN(x).  */
 (for logs (LOG LOG LOG LOG
            LOG2 LOG2 LOG2 LOG2
	    LOG10 LOG10 LOG10 LOG10)
      exps (EXP EXP2 EXP10 POW10)
  (simplify
   (logs (exps @0))
   (with {
     tree x;
     switch (exps)
       {
       CASE_FLT_FN (BUILT_IN_EXP):
         /* Prepare to do logN(exp(exponent) -> exponent*logN(e).  */
	 x = build_real (type, real_value_truncate (TYPE_MODE (type),
						    dconst_e ()));
         break;
       CASE_FLT_FN (BUILT_IN_EXP2):
         /* Prepare to do logN(exp2(exponent) -> exponent*logN(2).  */
         x = build_real (type, dconst2);
         break;
       CASE_FLT_FN (BUILT_IN_EXP10):
       CASE_FLT_FN (BUILT_IN_POW10):
	 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10).  */
	 {
	   REAL_VALUE_TYPE dconst10;
	   real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
	   x = build_real (type, dconst10);
	 }
         break;
       }
     }
    (mult (logs { x; }) @0))))
 (for logs (LOG LOG
            LOG2 LOG2
	    LOG10 LOG10)
      exps (SQRT CBRT)
  (simplify
   (logs (exps @0))
   (with {
     tree x;
     switch (exps)
       {
       CASE_FLT_FN (BUILT_IN_SQRT):
	 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x).  */
	 x = build_real (type, dconsthalf);
         break;
       CASE_FLT_FN (BUILT_IN_CBRT):
	 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x).  */
         x = build_real (type, real_value_truncate (TYPE_MODE (type),
						    dconst_third ()));
         break;
       }
     }
    (mult { x; } (logs @0)))))
 /* logN(pow(x,exponent) -> exponent*logN(x).  */
 (for logs (LOG LOG2 LOG10)
      pows (POW)
  (simplify
   (logs (pows @0 @1))
   (mult @1 (logs @0)))))

/* Narrowing of arithmetic and logical operations. 

   These are conceptually similar to the transformations performed for
   the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
   term we want to move all that code out of the front-ends into here.  */

/* If we have a narrowing conversion of an arithmetic operation where
   both operands are widening conversions from the same type as the outer
   narrowing conversion.  Then convert the innermost operands to a suitable
   unsigned type (to avoid introducing undefined behaviour), perform the
   operation and convert the result to the desired type.  */
(for op (plus minus)
  (simplify
    (convert (op@4 (convert@2 @0) (convert@3 @1)))
    (if (INTEGRAL_TYPE_P (type)
	 /* We check for type compatibility between @0 and @1 below,
	    so there's no need to check that @1/@3 are integral types.  */
	 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
	 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
	 /* The precision of the type of each operand must match the
	    precision of the mode of each operand, similarly for the
	    result.  */
	 && (TYPE_PRECISION (TREE_TYPE (@0))
	     == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
	 && (TYPE_PRECISION (TREE_TYPE (@1))
	     == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
	 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
	 /* The inner conversion must be a widening conversion.  */
	 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
	 && types_match (@0, @1)
	 && types_match (@0, type)
	 && single_use (@4))
      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
	(convert (op @0 @1)))
      (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
	(convert (op (convert:utype @0) (convert:utype @1)))))))

/* This is another case of narrowing, specifically when there's an outer
   BIT_AND_EXPR which masks off bits outside the type of the innermost
   operands.   Like the previous case we have to convert the operands
   to unsigned types to avoid introducing undefined behaviour for the
   arithmetic operation.  */
(for op (minus plus)
  (simplify
    (bit_and (op@5 (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
    (if (INTEGRAL_TYPE_P (type)
	 /* We check for type compatibility between @0 and @1 below,
	    so there's no need to check that @1/@3 are integral types.  */
	 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
	 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
	 /* The precision of the type of each operand must match the
	    precision of the mode of each operand, similarly for the
	    result.  */
	 && (TYPE_PRECISION (TREE_TYPE (@0))
	     == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
	 && (TYPE_PRECISION (TREE_TYPE (@1))
	     == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
	 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
	 /* The inner conversion must be a widening conversion.  */
	 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
	 && types_match (@0, @1)
	 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
	     <= TYPE_PRECISION (TREE_TYPE (@0)))
	 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
	     || tree_int_cst_sgn (@4) >= 0)
	 && single_use (@5))
      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
	(with { tree ntype = TREE_TYPE (@0); }
	  (convert (bit_and (op @0 @1) (convert:ntype @4)))))
      (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
	(convert (bit_and (op (convert:utype @0) (convert:utype @1))
			  (convert:utype @4)))))))