forked from riscvarchive/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.pd
6185 lines (5711 loc) · 208 KB
/
match.pd
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
/* 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-2020 Free Software Foundation, Inc.
Contributed by Richard Biener <[email protected]>
and Prathamesh Kulkarni <[email protected]>
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 integer_nonzerop
real_zerop real_onep real_minus_onep
zerop
initializer_each_zero_or_onep
CONSTANT_CLASS_P
tree_expr_nonnegative_p
tree_expr_nonzero_p
integer_valued_real_p
integer_pow2p
uniform_integer_cst_p
HONOR_NANS
uniform_vector_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)
#include "cfn-operators.pd"
/* Define operand lists for math rounding functions {,i,l,ll}FN,
where the versions prefixed with "i" return an int, those prefixed with
"l" return a long and those prefixed with "ll" return a long long.
Also define operand lists:
X<FN>F for all float functions, in the order i, l, ll
X<FN> for all double functions, in the same order
X<FN>L for all long double functions, in the same order. */
#define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
(define_operator_list X##FN##F BUILT_IN_I##FN##F \
BUILT_IN_L##FN##F \
BUILT_IN_LL##FN##F) \
(define_operator_list X##FN BUILT_IN_I##FN \
BUILT_IN_L##FN \
BUILT_IN_LL##FN) \
(define_operator_list X##FN##L BUILT_IN_I##FN##L \
BUILT_IN_L##FN##L \
BUILT_IN_LL##FN##L)
DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
/* Binary operations and their associated IFN_COND_* function. */
(define_operator_list UNCOND_BINARY
plus minus
mult trunc_div trunc_mod rdiv
min max
bit_and bit_ior bit_xor
lshift rshift)
(define_operator_list COND_BINARY
IFN_COND_ADD IFN_COND_SUB
IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
IFN_COND_MIN IFN_COND_MAX
IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
IFN_COND_SHL IFN_COND_SHR)
/* Same for ternary operations. */
(define_operator_list UNCOND_TERNARY
IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
(define_operator_list COND_TERNARY
IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
/* With nop_convert? combine convert? and view_convert? in one pattern
plus conditionalize on tree_nop_conversion_p conversions. */
(match (nop_convert @0)
(convert @0)
(if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
(match (nop_convert @0)
(view_convert @0)
(if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
&& known_eq (TYPE_VECTOR_SUBPARTS (type),
TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
&& tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
/* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
ABSU_EXPR returns unsigned absolute value of the operand and the operand
of the ABSU_EXPR will have the corresponding signed type. */
(simplify (abs (convert @0))
(if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
&& !TYPE_UNSIGNED (TREE_TYPE (@0))
&& element_precision (type) > element_precision (TREE_TYPE (@0)))
(with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
(convert (absu:utype @0)))))
/* 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)))
/* ptr - 0 -> (type)ptr */
(simplify
(pointer_diff @0 integer_zerop)
(convert @0))
/* 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)))
/* Even if the fold_real_zero_addition_p can't simplify X + 0.0
into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
if not -frounding-math. For sNaNs the first operation would raise
exceptions but turn the result into qNan, so the second operation
would not raise it. */
(for inner_op (plus minus)
(for outer_op (plus minus)
(simplify
(outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
(if (real_zerop (@1)
&& real_zerop (@2)
&& !HONOR_SIGN_DEPENDENT_ROUNDING (type))
(with { bool inner_plus = ((inner_op == PLUS_EXPR)
^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
bool outer_plus
= ((outer_op == PLUS_EXPR)
^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
(if (outer_plus && !inner_plus)
(outer_op @0 @2)
@3))))))
/* 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
(pointer_diff @@0 @0)
{ 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)))
/* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
(simplify
(mult SSA_NAME@1 SSA_NAME@2)
(if (INTEGRAL_TYPE_P (type)
&& get_nonzero_bits (@1) == 1
&& get_nonzero_bits (@2) == 1)
(bit_and @1 @2)))
/* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
unless the target has native support for the former but not the latter. */
(simplify
(mult @0 VECTOR_CST@1)
(if (initializer_each_zero_or_onep (@1)
&& !HONOR_SNANS (type)
&& !HONOR_SIGNED_ZEROS (type))
(with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
(if (itype
&& (!VECTOR_MODE_P (TYPE_MODE (type))
|| (VECTOR_MODE_P (TYPE_MODE (itype))
&& optab_handler (and_optab,
TYPE_MODE (itype)) != CODE_FOR_nothing)))
(view_convert (bit_and:itype (view_convert @0)
(ne @1 { build_zero_cst (type); })))))))
(for cmp (gt ge lt le)
outp (convert convert negate negate)
outn (negate negate convert convert)
/* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
/* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
/* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
/* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
(simplify
(cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
(if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
&& types_match (type, TREE_TYPE (@0)))
(switch
(if (types_match (type, float_type_node))
(BUILT_IN_COPYSIGNF @1 (outp @0)))
(if (types_match (type, double_type_node))
(BUILT_IN_COPYSIGN @1 (outp @0)))
(if (types_match (type, long_double_type_node))
(BUILT_IN_COPYSIGNL @1 (outp @0))))))
/* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
/* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
/* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
/* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
(simplify
(cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
(if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
&& types_match (type, TREE_TYPE (@0)))
(switch
(if (types_match (type, float_type_node))
(BUILT_IN_COPYSIGNF @1 (outn @0)))
(if (types_match (type, double_type_node))
(BUILT_IN_COPYSIGN @1 (outn @0)))
(if (types_match (type, long_double_type_node))
(BUILT_IN_COPYSIGNL @1 (outn @0)))))))
/* Transform X * copysign (1.0, X) into abs(X). */
(simplify
(mult:c @0 (COPYSIGN_ALL real_onep @0))
(if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
(abs @0)))
/* Transform X * copysign (1.0, -X) into -abs(X). */
(simplify
(mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
(if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
(negate (abs @0))))
/* Transform copysign (CST, X) into copysign (ABS(CST), X). */
(simplify
(COPYSIGN_ALL REAL_CST@0 @1)
(if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
(COPYSIGN_ALL (negate @0) @1)))
/* X * 1, X / 1 -> X. */
(for op (mult trunc_div ceil_div floor_div round_div exact_div)
(simplify
(op @0 integer_onep)
(non_lvalue @0)))
/* (A / (1 << B)) -> (A >> B).
Only for unsigned A. For signed A, this would not preserve rounding
toward zero.
For example: (-1 / ( 1 << B)) != -1 >> B.
Also also widening conversions, like:
(A / (unsigned long long) (1U << B)) -> (A >> B)
or
(A / (unsigned long long) (1 << B)) -> (A >> B).
If the left shift is signed, it can be done only if the upper bits
of A starting from shift's type sign bit are zero, as
(unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
so it is valid only if A >> 31 is zero. */
(simplify
(trunc_div @0 (convert? (lshift integer_onep@1 @2)))
(if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
&& (!VECTOR_TYPE_P (type)
|| target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
|| target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
&& (useless_type_conversion_p (type, TREE_TYPE (@1))
|| (element_precision (type) >= element_precision (TREE_TYPE (@1))
&& (TYPE_UNSIGNED (TREE_TYPE (@1))
|| (element_precision (type)
== element_precision (TREE_TYPE (@1)))
|| (INTEGRAL_TYPE_P (type)
&& (tree_nonzero_bits (@0)
& wi::mask (element_precision (TREE_TYPE (@1)) - 1,
true,
element_precision (type))) == 0)))))
(rshift @0 @2)))
/* Preserve explicit divisions by 0: the C++ front-end wants to detect
undefined behavior in constexpr evaluation, and assuming that the division
traps enables better optimizations than these anyway. */
(for div (trunc_div ceil_div floor_div round_div exact_div)
/* 0 / X is always zero. */
(simplify
(div 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 -X. */
(simplify
(div @0 integer_minus_onep@1)
(if (!TYPE_UNSIGNED (type))
(negate @0)))
/* X / X is one. */
(simplify
(div @0 @0)
/* But not for 0 / 0 so that we can get the proper warnings and errors.
And not for _Fract types where we can't build 1. */
(if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
{ build_one_cst (type); }))
/* X / abs (X) is X < 0 ? -1 : 1. */
(simplify
(div:C @0 (abs @0))
(if (INTEGRAL_TYPE_P (type)
&& TYPE_OVERFLOW_UNDEFINED (type))
(cond (lt @0 { build_zero_cst (type); })
{ build_minus_one_cst (type); } { build_one_cst (type); })))
/* X / -X is -1. */
(simplify
(div:C @0 (negate @0))
(if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
&& TYPE_OVERFLOW_UNDEFINED (type))
{ build_minus_one_cst (type); })))
/* 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@3 @0 INTEGER_CST@1) INTEGER_CST@2)
(with {
wi::overflow_type overflow;
wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
TYPE_SIGN (type), &overflow);
}
(if (div == EXACT_DIV_EXPR
|| optimize_successive_divisions_p (@2, @3))
(if (!overflow)
(div @0 { wide_int_to_tree (type, mul); })
(if (TYPE_UNSIGNED (type)
|| mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
{ build_zero_cst (type); }))))))
/* Combine successive multiplications. Similar to above, but handling
overflow is different. */
(simplify
(mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
(with {
wi::overflow_type overflow;
wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
TYPE_SIGN (type), &overflow);
}
/* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
otherwise undefined overflow implies that @0 must be zero. */
(if (!overflow || TYPE_OVERFLOW_WRAPS (type))
(mult @0 { wide_int_to_tree (type, mul); }))))
/* 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); }))
/* PR71078: x / abs(x) -> copysign (1.0, x) */
(simplify
(rdiv:C (convert? @0) (convert? (abs @0)))
(if (SCALAR_FLOAT_TYPE_P (type)
&& ! HONOR_NANS (type)
&& ! HONOR_INFINITIES (type))
(switch
(if (types_match (type, float_type_node))
(BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
(if (types_match (type, double_type_node))
(BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
(if (types_match (type, long_double_type_node))
(BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
/* 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 (flag_reciprocal_math)
/* Convert (A/B)/C to A/(B*C). */
(simplify
(rdiv (rdiv:s @0 @1) @2)
(rdiv @0 (mult @1 @2)))
/* Canonicalize x / (C1 * y) to (x * C2) / y. */
(simplify
(rdiv @0 (mult:s @1 REAL_CST@2))
(with
{ tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
(if (tem)
(rdiv (mult @0 { tem; } ) @1))))
/* Convert A/(B/C) to (A/B)*C */
(simplify
(rdiv @0 (rdiv:s @1 @2))
(mult (rdiv @0 @1) @2)))
/* Simplify x / (- y) to -x / y. */
(simplify
(rdiv @0 (negate @1))
(rdiv (negate @0) @1))
(if (flag_unsafe_math_optimizations)
/* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
Since C / x may underflow to zero, do this only for unsafe math. */
(for op (lt le gt ge)
neg_op (gt ge lt le)
(simplify
(op (rdiv REAL_CST@0 @1) real_zerop@2)
(if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
(switch
(if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
(op @1 @2))
/* For C < 0, use the inverted operator. */
(if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
(neg_op @1 @2)))))))
/* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
(for div (trunc_div ceil_div floor_div round_div exact_div)
(simplify
(div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
(if (integer_pow2p (@2)
&& tree_int_cst_sgn (@2) > 0
&& tree_nop_conversion_p (type, TREE_TYPE (@0))
&& wi::to_wide (@2) + wi::to_wide (@1) == 0)
(rshift (convert @0)
{ build_int_cst (integer_type_node,
wi::exact_log2 (wi::to_wide (@2))); }))))
/* 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; } ))))))))
(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 % X is zero. */
(simplify
(mod @0 @0)
/* But not for 0 % 0 so that we can get the proper warnings and errors. */
(if (!integer_zerop (@0))
{ build_zero_cst (type); }))
/* (X % Y) % Y is just X % Y. */
(simplify
(mod (mod@2 @0 @1) @1)
@2)
/* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
(simplify
(mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
(if (ANY_INTEGRAL_TYPE_P (type)
&& TYPE_OVERFLOW_UNDEFINED (type)
&& wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
TYPE_SIGN (type)))
{ build_zero_cst (type); }))
/* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
modulo and comparison, since it is simpler and equivalent. */
(for cmp (eq ne)
(simplify
(cmp (mod @0 integer_pow2p@2) integer_zerop@1)
(if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
(with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
(cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
/* 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 (wi::to_wide (@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 (INTEGRAL_TYPE_P (type)
&& !TYPE_UNSIGNED (type)
&& !TYPE_OVERFLOW_TRAPS (type)
&& tree_nop_conversion_p (type, TREE_TYPE (@1))
/* Avoid this transformation if X might be INT_MIN or
Y might be -1, because we would then change valid
INT_MIN % -(-1) into invalid INT_MIN % -1. */
&& (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
|| expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
(TREE_TYPE (@1))))))
(trunc_mod @0 (convert @1))))
/* X - (X / Y) * Y is the same as X % Y. */
(simplify
(minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
(if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
(convert (trunc_mod @0 @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); }))))))
/* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
(simplify
(trunc_div (mult @0 integer_pow2p@1) @1)
(if (TYPE_UNSIGNED (TREE_TYPE (@0)))
(bit_and @0 { wide_int_to_tree
(type, wi::mask (TYPE_PRECISION (type)
- wi::exact_log2 (wi::to_wide (@1)),
false, TYPE_PRECISION (type))); })))
/* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
(simplify
(mult (trunc_div @0 integer_pow2p@1) @1)
(if (TYPE_UNSIGNED (TREE_TYPE (@0)))
(bit_and @0 (negate @1))))
/* Simplify (t * 2) / 2) -> t. */
(for div (trunc_div ceil_div floor_div round_div exact_div)
(simplify
(div (mult:c @0 @1) @1)
(if (ANY_INTEGRAL_TYPE_P (type)
&& TYPE_OVERFLOW_UNDEFINED (type))
@0)))
(for op (negate abs)
/* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
(for coss (COS COSH)
(simplify
(coss (op @0))
(coss @0)))
/* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
(for pows (POW)
(simplify
(pows (op @0) REAL_CST@1)
(with { HOST_WIDE_INT n; }
(if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
(pows @0 @1)))))
/* Likewise for powi. */
(for pows (POWI)
(simplify
(pows (op @0) INTEGER_CST@1)
(if ((wi::to_wide (@1) & 1) == 0)
(pows @0 @1))))
/* Strip negate and abs from both operands of hypot. */
(for hypots (HYPOT)
(simplify
(hypots (op @0) @1)
(hypots @0 @1))
(simplify
(hypots @0 (op @1))
(hypots @0 @1)))
/* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
(for copysigns (COPYSIGN_ALL)
(simplify
(copysigns (op @0) @1)
(copysigns @0 @1))))
/* abs(x)*abs(x) -> x*x. Should be valid for all types. */
(simplify
(mult (abs@1 @0) @1)
(mult @0 @0))
/* Convert absu(x)*absu(x) -> x*x. */
(simplify
(mult (absu@1 @0) @1)
(mult (convert@2 @0) @2))
/* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
(for coss (COS COSH)
copysigns (COPYSIGN)
(simplify
(coss (copysigns @0 @1))
(coss @0)))
/* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
(for pows (POW)
copysigns (COPYSIGN)
(simplify
(pows (copysigns @0 @2) REAL_CST@1)
(with { HOST_WIDE_INT n; }
(if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
(pows @0 @1)))))
/* Likewise for powi. */
(for pows (POWI)
copysigns (COPYSIGN)
(simplify
(pows (copysigns @0 @2) INTEGER_CST@1)
(if ((wi::to_wide (@1) & 1) == 0)
(pows @0 @1))))
(for hypots (HYPOT)
copysigns (COPYSIGN)
/* hypot(copysign(x, y), z) -> hypot(x, z). */
(simplify
(hypots (copysigns @0 @1) @2)
(hypots @0 @2))
/* hypot(x, copysign(y, z)) -> hypot(x, y). */
(simplify
(hypots @0 (copysigns @1 @2))
(hypots @0 @1)))
/* copysign(x, CST) -> [-]abs (x). */
(for copysigns (COPYSIGN_ALL)
(simplify
(copysigns @0 REAL_CST@1)
(if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
(negate (abs @0))
(abs @0))))
/* copysign(copysign(x, y), z) -> copysign(x, z). */
(for copysigns (COPYSIGN_ALL)
(simplify
(copysigns (copysigns @0 @1) @2)
(copysigns @0 @2)))
/* copysign(x,y)*copysign(x,y) -> x*x. */
(for copysigns (COPYSIGN_ALL)
(simplify
(mult (copysigns@2 @0 @1) @2)
(mult @0 @0)))
/* ccos(-x) -> ccos(x). Similarly for ccosh. */
(for ccoss (CCOS CCOSH)
(simplify
(ccoss (negate @0))
(ccoss @0)))
/* cabs(-x) and cos(conj(x)) -> cabs(x). */
(for ops (conj negate)
(for cabss (CABS)
(simplify
(cabss (ops @0))
(cabss @0))))
/* Fold (a * (1 << b)) into (a << b) */
(simplify
(mult:c @0 (convert? (lshift integer_onep@1 @2)))
(if (! FLOAT_TYPE_P (type)
&& tree_nop_conversion_p (type, TREE_TYPE (@1)))
(lshift @0 @2)))
/* Fold (1 << (C - x)) where C = precision(type) - 1
into ((1 << C) >> x). */
(simplify
(lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
(if (INTEGRAL_TYPE_P (type)
&& wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
&& single_use (@1))
(if (TYPE_UNSIGNED (type))
(rshift (lshift @0 @2) @3)
(with
{ tree utype = unsigned_type_for (type); }
(convert (rshift (lshift (convert:utype @0) @2) @3))))))
/* Fold (C1/X)*C2 into (C1*C2)/X. */
(simplify
(mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
(if (flag_associative_math
&& single_use (@3))
(with
{ tree tem = const_binop (MULT_EXPR, type, @0, @2); }
(if (tem)
(rdiv { tem; } @1)))))
/* Simplify ~X & X as zero. */
(simplify
(bit_and:c (convert? @0) (convert? (bit_not @0)))
{ build_zero_cst (type); })
/* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
(simplify
(bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
(if (TYPE_UNSIGNED (type))
(bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
(for bitop (bit_and bit_ior)
cmp (eq ne)
/* PR35691: Transform
(x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
(x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
(simplify
(bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
(if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
&& INTEGRAL_TYPE_P (TREE_TYPE (@1))
&& TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
(cmp (bit_ior @0 (convert @1)) @2)))
/* Transform:
(x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
(x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
(simplify
(bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
(if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
&& INTEGRAL_TYPE_P (TREE_TYPE (@1))
&& TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
(cmp (bit_and @0 (convert @1)) @2))))
/* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
(simplify
(minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
(minus (bit_xor @0 @1) @1))
(simplify
(minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
(if (~wi::to_wide (@2) == wi::to_wide (@1))
(minus (bit_xor @0 @1) @1)))
/* Fold (A & B) - (A & ~B) into B - (A ^ B). */
(simplify
(minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
(minus @1 (bit_xor @0 @1)))
/* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
(for op (bit_ior bit_xor plus)
(simplify
(op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
(bit_xor @0 @1))
(simplify
(op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
(if (~wi::to_wide (@2) == wi::to_wide (@1))
(bit_xor @0 @1))))
/* PR53979: Transform ((a ^ b) | a) -> (a | b) */
(simplify
(bit_ior:c (bit_xor:c @0 @1) @0)
(bit_ior @0 @1))
/* (a & ~b) | (a ^ b) --> a ^ b */
(simplify
(bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
@2)
/* (a & ~b) ^ ~a --> ~(a & b) */
(simplify
(bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
(bit_not (bit_and @0 @1)))
/* (~a & b) ^ a --> (a | b) */
(simplify
(bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
(bit_ior @0 @1))
/* (a | b) & ~(a ^ b) --> a & b */
(simplify
(bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
(bit_and @0 @1))
/* a | ~(a ^ b) --> a | ~b */
(simplify
(bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
(bit_ior @0 (bit_not @1)))
/* (a | b) | (a &^ b) --> a | b */
(for op (bit_and bit_xor)
(simplify
(bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
@2))
/* (a & b) | ~(a ^ b) --> ~(a ^ b) */
(simplify
(bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
@2)
/* ~(~a & b) --> a | ~b */
(simplify
(bit_not (bit_and:cs (bit_not @0) @1))
(bit_ior @0 (bit_not @1)))
/* ~(~a | b) --> a & ~b */
(simplify
(bit_not (bit_ior:cs (bit_not @0) @1))
(bit_and @0 (bit_not @1)))
/* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
#if GIMPLE
(simplify
(bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
(if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
&& wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
(bit_xor @0 @1)))
#endif
/* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
((A & N) + B) & M -> (A + B) & M
Similarly if (N & M) == 0,
((A | N) + B) & M -> (A + B) & M
and for - instead of + (or unary - instead of +)
and/or ^ instead of |.
If B is constant and (B & M) == 0, fold into A & M. */
(for op (plus minus)
(for bitop (bit_and bit_ior bit_xor)
(simplify
(bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
(with
{ tree pmop[2];
tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
@3, @4, @1, ERROR_MARK, NULL_TREE,
NULL_TREE, pmop); }
(if (utype)
(convert (bit_and (op (convert:utype { pmop[0]; })
(convert:utype { pmop[1]; }))
(convert:utype @2))))))
(simplify
(bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
(with
{ tree pmop[2];
tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
NULL_TREE, NULL_TREE, @1, bitop, @3,
@4, pmop); }
(if (utype)
(convert (bit_and (op (convert:utype { pmop[0]; })
(convert:utype { pmop[1]; }))
(convert:utype @2)))))))
(simplify
(bit_and (op:s @0 @1) INTEGER_CST@2)
(with
{ tree pmop[2];
tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
NULL_TREE, NULL_TREE, @1, ERROR_MARK,
NULL_TREE, NULL_TREE, pmop); }
(if (utype)
(convert (bit_and (op (convert:utype { pmop[0]; })
(convert:utype { pmop[1]; }))
(convert:utype @2)))))))
(for bitop (bit_and bit_ior bit_xor)
(simplify
(bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
(with
{ tree pmop[2];
tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
bitop, @2, @3, NULL_TREE, ERROR_MARK,
NULL_TREE, NULL_TREE, pmop); }
(if (utype)
(convert (bit_and (negate (convert:utype { pmop[0]; }))
(convert:utype @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 -> x */
(simplify
(bit_ior @0 integer_zerop)
@0)
/* 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