forked from riscvarchive/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree-ssa-loop-niter.c
5007 lines (4181 loc) · 145 KB
/
tree-ssa-loop-niter.c
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
/* Functions to determine/estimate number of iterations of a loop.
Copyright (C) 2004-2020 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
GCC is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "rtl.h"
#include "tree.h"
#include "gimple.h"
#include "tree-pass.h"
#include "ssa.h"
#include "gimple-pretty-print.h"
#include "diagnostic-core.h"
#include "stor-layout.h"
#include "fold-const.h"
#include "calls.h"
#include "intl.h"
#include "gimplify.h"
#include "gimple-iterator.h"
#include "tree-cfg.h"
#include "tree-ssa-loop-ivopts.h"
#include "tree-ssa-loop-niter.h"
#include "tree-ssa-loop.h"
#include "cfgloop.h"
#include "tree-chrec.h"
#include "tree-scalar-evolution.h"
#include "tree-dfa.h"
/* The maximum number of dominator BBs we search for conditions
of loop header copies we use for simplifying a conditional
expression. */
#define MAX_DOMINATORS_TO_WALK 8
/*
Analysis of number of iterations of an affine exit test.
*/
/* Bounds on some value, BELOW <= X <= UP. */
struct bounds
{
mpz_t below, up;
};
static bool number_of_iterations_popcount (loop_p loop, edge exit,
enum tree_code code,
class tree_niter_desc *niter);
/* Splits expression EXPR to a variable part VAR and constant OFFSET. */
static void
split_to_var_and_offset (tree expr, tree *var, mpz_t offset)
{
tree type = TREE_TYPE (expr);
tree op0, op1;
bool negate = false;
*var = expr;
mpz_set_ui (offset, 0);
switch (TREE_CODE (expr))
{
case MINUS_EXPR:
negate = true;
/* Fallthru. */
case PLUS_EXPR:
case POINTER_PLUS_EXPR:
op0 = TREE_OPERAND (expr, 0);
op1 = TREE_OPERAND (expr, 1);
if (TREE_CODE (op1) != INTEGER_CST)
break;
*var = op0;
/* Always sign extend the offset. */
wi::to_mpz (wi::to_wide (op1), offset, SIGNED);
if (negate)
mpz_neg (offset, offset);
break;
case INTEGER_CST:
*var = build_int_cst_type (type, 0);
wi::to_mpz (wi::to_wide (expr), offset, TYPE_SIGN (type));
break;
default:
break;
}
}
/* From condition C0 CMP C1 derives information regarding the value range
of VAR, which is of TYPE. Results are stored in to BELOW and UP. */
static void
refine_value_range_using_guard (tree type, tree var,
tree c0, enum tree_code cmp, tree c1,
mpz_t below, mpz_t up)
{
tree varc0, varc1, ctype;
mpz_t offc0, offc1;
mpz_t mint, maxt, minc1, maxc1;
wide_int minv, maxv;
bool no_wrap = nowrap_type_p (type);
bool c0_ok, c1_ok;
signop sgn = TYPE_SIGN (type);
switch (cmp)
{
case LT_EXPR:
case LE_EXPR:
case GT_EXPR:
case GE_EXPR:
STRIP_SIGN_NOPS (c0);
STRIP_SIGN_NOPS (c1);
ctype = TREE_TYPE (c0);
if (!useless_type_conversion_p (ctype, type))
return;
break;
case EQ_EXPR:
/* We could derive quite precise information from EQ_EXPR, however,
such a guard is unlikely to appear, so we do not bother with
handling it. */
return;
case NE_EXPR:
/* NE_EXPR comparisons do not contain much of useful information,
except for cases of comparing with bounds. */
if (TREE_CODE (c1) != INTEGER_CST
|| !INTEGRAL_TYPE_P (type))
return;
/* Ensure that the condition speaks about an expression in the same
type as X and Y. */
ctype = TREE_TYPE (c0);
if (TYPE_PRECISION (ctype) != TYPE_PRECISION (type))
return;
c0 = fold_convert (type, c0);
c1 = fold_convert (type, c1);
if (operand_equal_p (var, c0, 0))
{
mpz_t valc1;
/* Case of comparing VAR with its below/up bounds. */
mpz_init (valc1);
wi::to_mpz (wi::to_wide (c1), valc1, TYPE_SIGN (type));
if (mpz_cmp (valc1, below) == 0)
cmp = GT_EXPR;
if (mpz_cmp (valc1, up) == 0)
cmp = LT_EXPR;
mpz_clear (valc1);
}
else
{
/* Case of comparing with the bounds of the type. */
wide_int min = wi::min_value (type);
wide_int max = wi::max_value (type);
if (wi::to_wide (c1) == min)
cmp = GT_EXPR;
if (wi::to_wide (c1) == max)
cmp = LT_EXPR;
}
/* Quick return if no useful information. */
if (cmp == NE_EXPR)
return;
break;
default:
return;
}
mpz_init (offc0);
mpz_init (offc1);
split_to_var_and_offset (expand_simple_operations (c0), &varc0, offc0);
split_to_var_and_offset (expand_simple_operations (c1), &varc1, offc1);
/* We are only interested in comparisons of expressions based on VAR. */
if (operand_equal_p (var, varc1, 0))
{
std::swap (varc0, varc1);
mpz_swap (offc0, offc1);
cmp = swap_tree_comparison (cmp);
}
else if (!operand_equal_p (var, varc0, 0))
{
mpz_clear (offc0);
mpz_clear (offc1);
return;
}
mpz_init (mint);
mpz_init (maxt);
get_type_static_bounds (type, mint, maxt);
mpz_init (minc1);
mpz_init (maxc1);
/* Setup range information for varc1. */
if (integer_zerop (varc1))
{
wi::to_mpz (0, minc1, TYPE_SIGN (type));
wi::to_mpz (0, maxc1, TYPE_SIGN (type));
}
else if (TREE_CODE (varc1) == SSA_NAME
&& INTEGRAL_TYPE_P (type)
&& get_range_info (varc1, &minv, &maxv) == VR_RANGE)
{
gcc_assert (wi::le_p (minv, maxv, sgn));
wi::to_mpz (minv, minc1, sgn);
wi::to_mpz (maxv, maxc1, sgn);
}
else
{
mpz_set (minc1, mint);
mpz_set (maxc1, maxt);
}
/* Compute valid range information for varc1 + offc1. Note nothing
useful can be derived if it overflows or underflows. Overflow or
underflow could happen when:
offc1 > 0 && varc1 + offc1 > MAX_VAL (type)
offc1 < 0 && varc1 + offc1 < MIN_VAL (type). */
mpz_add (minc1, minc1, offc1);
mpz_add (maxc1, maxc1, offc1);
c1_ok = (no_wrap
|| mpz_sgn (offc1) == 0
|| (mpz_sgn (offc1) < 0 && mpz_cmp (minc1, mint) >= 0)
|| (mpz_sgn (offc1) > 0 && mpz_cmp (maxc1, maxt) <= 0));
if (!c1_ok)
goto end;
if (mpz_cmp (minc1, mint) < 0)
mpz_set (minc1, mint);
if (mpz_cmp (maxc1, maxt) > 0)
mpz_set (maxc1, maxt);
if (cmp == LT_EXPR)
{
cmp = LE_EXPR;
mpz_sub_ui (maxc1, maxc1, 1);
}
if (cmp == GT_EXPR)
{
cmp = GE_EXPR;
mpz_add_ui (minc1, minc1, 1);
}
/* Compute range information for varc0. If there is no overflow,
the condition implied that
(varc0) cmp (varc1 + offc1 - offc0)
We can possibly improve the upper bound of varc0 if cmp is LE_EXPR,
or the below bound if cmp is GE_EXPR.
To prove there is no overflow/underflow, we need to check below
four cases:
1) cmp == LE_EXPR && offc0 > 0
(varc0 + offc0) doesn't overflow
&& (varc1 + offc1 - offc0) doesn't underflow
2) cmp == LE_EXPR && offc0 < 0
(varc0 + offc0) doesn't underflow
&& (varc1 + offc1 - offc0) doesn't overfloe
In this case, (varc0 + offc0) will never underflow if we can
prove (varc1 + offc1 - offc0) doesn't overflow.
3) cmp == GE_EXPR && offc0 < 0
(varc0 + offc0) doesn't underflow
&& (varc1 + offc1 - offc0) doesn't overflow
4) cmp == GE_EXPR && offc0 > 0
(varc0 + offc0) doesn't overflow
&& (varc1 + offc1 - offc0) doesn't underflow
In this case, (varc0 + offc0) will never overflow if we can
prove (varc1 + offc1 - offc0) doesn't underflow.
Note we only handle case 2 and 4 in below code. */
mpz_sub (minc1, minc1, offc0);
mpz_sub (maxc1, maxc1, offc0);
c0_ok = (no_wrap
|| mpz_sgn (offc0) == 0
|| (cmp == LE_EXPR
&& mpz_sgn (offc0) < 0 && mpz_cmp (maxc1, maxt) <= 0)
|| (cmp == GE_EXPR
&& mpz_sgn (offc0) > 0 && mpz_cmp (minc1, mint) >= 0));
if (!c0_ok)
goto end;
if (cmp == LE_EXPR)
{
if (mpz_cmp (up, maxc1) > 0)
mpz_set (up, maxc1);
}
else
{
if (mpz_cmp (below, minc1) < 0)
mpz_set (below, minc1);
}
end:
mpz_clear (mint);
mpz_clear (maxt);
mpz_clear (minc1);
mpz_clear (maxc1);
mpz_clear (offc0);
mpz_clear (offc1);
}
/* Stores estimate on the minimum/maximum value of the expression VAR + OFF
in TYPE to MIN and MAX. */
static void
determine_value_range (class loop *loop, tree type, tree var, mpz_t off,
mpz_t min, mpz_t max)
{
int cnt = 0;
mpz_t minm, maxm;
basic_block bb;
wide_int minv, maxv;
enum value_range_kind rtype = VR_VARYING;
/* If the expression is a constant, we know its value exactly. */
if (integer_zerop (var))
{
mpz_set (min, off);
mpz_set (max, off);
return;
}
get_type_static_bounds (type, min, max);
/* See if we have some range info from VRP. */
if (TREE_CODE (var) == SSA_NAME && INTEGRAL_TYPE_P (type))
{
edge e = loop_preheader_edge (loop);
signop sgn = TYPE_SIGN (type);
gphi_iterator gsi;
/* Either for VAR itself... */
rtype = get_range_info (var, &minv, &maxv);
/* Or for PHI results in loop->header where VAR is used as
PHI argument from the loop preheader edge. */
for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi))
{
gphi *phi = gsi.phi ();
wide_int minc, maxc;
if (PHI_ARG_DEF_FROM_EDGE (phi, e) == var
&& (get_range_info (gimple_phi_result (phi), &minc, &maxc)
== VR_RANGE))
{
if (rtype != VR_RANGE)
{
rtype = VR_RANGE;
minv = minc;
maxv = maxc;
}
else
{
minv = wi::max (minv, minc, sgn);
maxv = wi::min (maxv, maxc, sgn);
/* If the PHI result range are inconsistent with
the VAR range, give up on looking at the PHI
results. This can happen if VR_UNDEFINED is
involved. */
if (wi::gt_p (minv, maxv, sgn))
{
rtype = get_range_info (var, &minv, &maxv);
break;
}
}
}
}
mpz_init (minm);
mpz_init (maxm);
if (rtype != VR_RANGE)
{
mpz_set (minm, min);
mpz_set (maxm, max);
}
else
{
gcc_assert (wi::le_p (minv, maxv, sgn));
wi::to_mpz (minv, minm, sgn);
wi::to_mpz (maxv, maxm, sgn);
}
/* Now walk the dominators of the loop header and use the entry
guards to refine the estimates. */
for (bb = loop->header;
bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) && cnt < MAX_DOMINATORS_TO_WALK;
bb = get_immediate_dominator (CDI_DOMINATORS, bb))
{
edge e;
tree c0, c1;
gimple *cond;
enum tree_code cmp;
if (!single_pred_p (bb))
continue;
e = single_pred_edge (bb);
if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
continue;
cond = last_stmt (e->src);
c0 = gimple_cond_lhs (cond);
cmp = gimple_cond_code (cond);
c1 = gimple_cond_rhs (cond);
if (e->flags & EDGE_FALSE_VALUE)
cmp = invert_tree_comparison (cmp, false);
refine_value_range_using_guard (type, var, c0, cmp, c1, minm, maxm);
++cnt;
}
mpz_add (minm, minm, off);
mpz_add (maxm, maxm, off);
/* If the computation may not wrap or off is zero, then this
is always fine. If off is negative and minv + off isn't
smaller than type's minimum, or off is positive and
maxv + off isn't bigger than type's maximum, use the more
precise range too. */
if (nowrap_type_p (type)
|| mpz_sgn (off) == 0
|| (mpz_sgn (off) < 0 && mpz_cmp (minm, min) >= 0)
|| (mpz_sgn (off) > 0 && mpz_cmp (maxm, max) <= 0))
{
mpz_set (min, minm);
mpz_set (max, maxm);
mpz_clear (minm);
mpz_clear (maxm);
return;
}
mpz_clear (minm);
mpz_clear (maxm);
}
/* If the computation may wrap, we know nothing about the value, except for
the range of the type. */
if (!nowrap_type_p (type))
return;
/* Since the addition of OFF does not wrap, if OFF is positive, then we may
add it to MIN, otherwise to MAX. */
if (mpz_sgn (off) < 0)
mpz_add (max, max, off);
else
mpz_add (min, min, off);
}
/* Stores the bounds on the difference of the values of the expressions
(var + X) and (var + Y), computed in TYPE, to BNDS. */
static void
bound_difference_of_offsetted_base (tree type, mpz_t x, mpz_t y,
bounds *bnds)
{
int rel = mpz_cmp (x, y);
bool may_wrap = !nowrap_type_p (type);
mpz_t m;
/* If X == Y, then the expressions are always equal.
If X > Y, there are the following possibilities:
a) neither of var + X and var + Y overflow or underflow, or both of
them do. Then their difference is X - Y.
b) var + X overflows, and var + Y does not. Then the values of the
expressions are var + X - M and var + Y, where M is the range of
the type, and their difference is X - Y - M.
c) var + Y underflows and var + X does not. Their difference again
is M - X + Y.
Therefore, if the arithmetics in type does not overflow, then the
bounds are (X - Y, X - Y), otherwise they are (X - Y - M, X - Y)
Similarly, if X < Y, the bounds are either (X - Y, X - Y) or
(X - Y, X - Y + M). */
if (rel == 0)
{
mpz_set_ui (bnds->below, 0);
mpz_set_ui (bnds->up, 0);
return;
}
mpz_init (m);
wi::to_mpz (wi::minus_one (TYPE_PRECISION (type)), m, UNSIGNED);
mpz_add_ui (m, m, 1);
mpz_sub (bnds->up, x, y);
mpz_set (bnds->below, bnds->up);
if (may_wrap)
{
if (rel > 0)
mpz_sub (bnds->below, bnds->below, m);
else
mpz_add (bnds->up, bnds->up, m);
}
mpz_clear (m);
}
/* From condition C0 CMP C1 derives information regarding the
difference of values of VARX + OFFX and VARY + OFFY, computed in TYPE,
and stores it to BNDS. */
static void
refine_bounds_using_guard (tree type, tree varx, mpz_t offx,
tree vary, mpz_t offy,
tree c0, enum tree_code cmp, tree c1,
bounds *bnds)
{
tree varc0, varc1, ctype;
mpz_t offc0, offc1, loffx, loffy, bnd;
bool lbound = false;
bool no_wrap = nowrap_type_p (type);
bool x_ok, y_ok;
switch (cmp)
{
case LT_EXPR:
case LE_EXPR:
case GT_EXPR:
case GE_EXPR:
STRIP_SIGN_NOPS (c0);
STRIP_SIGN_NOPS (c1);
ctype = TREE_TYPE (c0);
if (!useless_type_conversion_p (ctype, type))
return;
break;
case EQ_EXPR:
/* We could derive quite precise information from EQ_EXPR, however, such
a guard is unlikely to appear, so we do not bother with handling
it. */
return;
case NE_EXPR:
/* NE_EXPR comparisons do not contain much of useful information, except for
special case of comparing with the bounds of the type. */
if (TREE_CODE (c1) != INTEGER_CST
|| !INTEGRAL_TYPE_P (type))
return;
/* Ensure that the condition speaks about an expression in the same type
as X and Y. */
ctype = TREE_TYPE (c0);
if (TYPE_PRECISION (ctype) != TYPE_PRECISION (type))
return;
c0 = fold_convert (type, c0);
c1 = fold_convert (type, c1);
if (TYPE_MIN_VALUE (type)
&& operand_equal_p (c1, TYPE_MIN_VALUE (type), 0))
{
cmp = GT_EXPR;
break;
}
if (TYPE_MAX_VALUE (type)
&& operand_equal_p (c1, TYPE_MAX_VALUE (type), 0))
{
cmp = LT_EXPR;
break;
}
return;
default:
return;
}
mpz_init (offc0);
mpz_init (offc1);
split_to_var_and_offset (expand_simple_operations (c0), &varc0, offc0);
split_to_var_and_offset (expand_simple_operations (c1), &varc1, offc1);
/* We are only interested in comparisons of expressions based on VARX and
VARY. TODO -- we might also be able to derive some bounds from
expressions containing just one of the variables. */
if (operand_equal_p (varx, varc1, 0))
{
std::swap (varc0, varc1);
mpz_swap (offc0, offc1);
cmp = swap_tree_comparison (cmp);
}
if (!operand_equal_p (varx, varc0, 0)
|| !operand_equal_p (vary, varc1, 0))
goto end;
mpz_init_set (loffx, offx);
mpz_init_set (loffy, offy);
if (cmp == GT_EXPR || cmp == GE_EXPR)
{
std::swap (varx, vary);
mpz_swap (offc0, offc1);
mpz_swap (loffx, loffy);
cmp = swap_tree_comparison (cmp);
lbound = true;
}
/* If there is no overflow, the condition implies that
(VARX + OFFX) cmp (VARY + OFFY) + (OFFX - OFFY + OFFC1 - OFFC0).
The overflows and underflows may complicate things a bit; each
overflow decreases the appropriate offset by M, and underflow
increases it by M. The above inequality would not necessarily be
true if
-- VARX + OFFX underflows and VARX + OFFC0 does not, or
VARX + OFFC0 overflows, but VARX + OFFX does not.
This may only happen if OFFX < OFFC0.
-- VARY + OFFY overflows and VARY + OFFC1 does not, or
VARY + OFFC1 underflows and VARY + OFFY does not.
This may only happen if OFFY > OFFC1. */
if (no_wrap)
{
x_ok = true;
y_ok = true;
}
else
{
x_ok = (integer_zerop (varx)
|| mpz_cmp (loffx, offc0) >= 0);
y_ok = (integer_zerop (vary)
|| mpz_cmp (loffy, offc1) <= 0);
}
if (x_ok && y_ok)
{
mpz_init (bnd);
mpz_sub (bnd, loffx, loffy);
mpz_add (bnd, bnd, offc1);
mpz_sub (bnd, bnd, offc0);
if (cmp == LT_EXPR)
mpz_sub_ui (bnd, bnd, 1);
if (lbound)
{
mpz_neg (bnd, bnd);
if (mpz_cmp (bnds->below, bnd) < 0)
mpz_set (bnds->below, bnd);
}
else
{
if (mpz_cmp (bnd, bnds->up) < 0)
mpz_set (bnds->up, bnd);
}
mpz_clear (bnd);
}
mpz_clear (loffx);
mpz_clear (loffy);
end:
mpz_clear (offc0);
mpz_clear (offc1);
}
/* Stores the bounds on the value of the expression X - Y in LOOP to BNDS.
The subtraction is considered to be performed in arbitrary precision,
without overflows.
We do not attempt to be too clever regarding the value ranges of X and
Y; most of the time, they are just integers or ssa names offsetted by
integer. However, we try to use the information contained in the
comparisons before the loop (usually created by loop header copying). */
static void
bound_difference (class loop *loop, tree x, tree y, bounds *bnds)
{
tree type = TREE_TYPE (x);
tree varx, vary;
mpz_t offx, offy;
mpz_t minx, maxx, miny, maxy;
int cnt = 0;
edge e;
basic_block bb;
tree c0, c1;
gimple *cond;
enum tree_code cmp;
/* Get rid of unnecessary casts, but preserve the value of
the expressions. */
STRIP_SIGN_NOPS (x);
STRIP_SIGN_NOPS (y);
mpz_init (bnds->below);
mpz_init (bnds->up);
mpz_init (offx);
mpz_init (offy);
split_to_var_and_offset (x, &varx, offx);
split_to_var_and_offset (y, &vary, offy);
if (!integer_zerop (varx)
&& operand_equal_p (varx, vary, 0))
{
/* Special case VARX == VARY -- we just need to compare the
offsets. The matters are a bit more complicated in the
case addition of offsets may wrap. */
bound_difference_of_offsetted_base (type, offx, offy, bnds);
}
else
{
/* Otherwise, use the value ranges to determine the initial
estimates on below and up. */
mpz_init (minx);
mpz_init (maxx);
mpz_init (miny);
mpz_init (maxy);
determine_value_range (loop, type, varx, offx, minx, maxx);
determine_value_range (loop, type, vary, offy, miny, maxy);
mpz_sub (bnds->below, minx, maxy);
mpz_sub (bnds->up, maxx, miny);
mpz_clear (minx);
mpz_clear (maxx);
mpz_clear (miny);
mpz_clear (maxy);
}
/* If both X and Y are constants, we cannot get any more precise. */
if (integer_zerop (varx) && integer_zerop (vary))
goto end;
/* Now walk the dominators of the loop header and use the entry
guards to refine the estimates. */
for (bb = loop->header;
bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) && cnt < MAX_DOMINATORS_TO_WALK;
bb = get_immediate_dominator (CDI_DOMINATORS, bb))
{
if (!single_pred_p (bb))
continue;
e = single_pred_edge (bb);
if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
continue;
cond = last_stmt (e->src);
c0 = gimple_cond_lhs (cond);
cmp = gimple_cond_code (cond);
c1 = gimple_cond_rhs (cond);
if (e->flags & EDGE_FALSE_VALUE)
cmp = invert_tree_comparison (cmp, false);
refine_bounds_using_guard (type, varx, offx, vary, offy,
c0, cmp, c1, bnds);
++cnt;
}
end:
mpz_clear (offx);
mpz_clear (offy);
}
/* Update the bounds in BNDS that restrict the value of X to the bounds
that restrict the value of X + DELTA. X can be obtained as a
difference of two values in TYPE. */
static void
bounds_add (bounds *bnds, const widest_int &delta, tree type)
{
mpz_t mdelta, max;
mpz_init (mdelta);
wi::to_mpz (delta, mdelta, SIGNED);
mpz_init (max);
wi::to_mpz (wi::minus_one (TYPE_PRECISION (type)), max, UNSIGNED);
mpz_add (bnds->up, bnds->up, mdelta);
mpz_add (bnds->below, bnds->below, mdelta);
if (mpz_cmp (bnds->up, max) > 0)
mpz_set (bnds->up, max);
mpz_neg (max, max);
if (mpz_cmp (bnds->below, max) < 0)
mpz_set (bnds->below, max);
mpz_clear (mdelta);
mpz_clear (max);
}
/* Update the bounds in BNDS that restrict the value of X to the bounds
that restrict the value of -X. */
static void
bounds_negate (bounds *bnds)
{
mpz_t tmp;
mpz_init_set (tmp, bnds->up);
mpz_neg (bnds->up, bnds->below);
mpz_neg (bnds->below, tmp);
mpz_clear (tmp);
}
/* Returns inverse of X modulo 2^s, where MASK = 2^s-1. */
static tree
inverse (tree x, tree mask)
{
tree type = TREE_TYPE (x);
tree rslt;
unsigned ctr = tree_floor_log2 (mask);
if (TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT)
{
unsigned HOST_WIDE_INT ix;
unsigned HOST_WIDE_INT imask;
unsigned HOST_WIDE_INT irslt = 1;
gcc_assert (cst_and_fits_in_hwi (x));
gcc_assert (cst_and_fits_in_hwi (mask));
ix = int_cst_value (x);
imask = int_cst_value (mask);
for (; ctr; ctr--)
{
irslt *= ix;
ix *= ix;
}
irslt &= imask;
rslt = build_int_cst_type (type, irslt);
}
else
{
rslt = build_int_cst (type, 1);
for (; ctr; ctr--)
{
rslt = int_const_binop (MULT_EXPR, rslt, x);
x = int_const_binop (MULT_EXPR, x, x);
}
rslt = int_const_binop (BIT_AND_EXPR, rslt, mask);
}
return rslt;
}
/* Derives the upper bound BND on the number of executions of loop with exit
condition S * i <> C. If NO_OVERFLOW is true, then the control variable of
the loop does not overflow. EXIT_MUST_BE_TAKEN is true if we are guaranteed
that the loop ends through this exit, i.e., the induction variable ever
reaches the value of C.
The value C is equal to final - base, where final and base are the final and
initial value of the actual induction variable in the analysed loop. BNDS
bounds the value of this difference when computed in signed type with
unbounded range, while the computation of C is performed in an unsigned
type with the range matching the range of the type of the induction variable.
In particular, BNDS.up contains an upper bound on C in the following cases:
-- if the iv must reach its final value without overflow, i.e., if
NO_OVERFLOW && EXIT_MUST_BE_TAKEN is true, or
-- if final >= base, which we know to hold when BNDS.below >= 0. */
static void
number_of_iterations_ne_max (mpz_t bnd, bool no_overflow, tree c, tree s,
bounds *bnds, bool exit_must_be_taken)
{
widest_int max;
mpz_t d;
tree type = TREE_TYPE (c);
bool bnds_u_valid = ((no_overflow && exit_must_be_taken)
|| mpz_sgn (bnds->below) >= 0);
if (integer_onep (s)
|| (TREE_CODE (c) == INTEGER_CST
&& TREE_CODE (s) == INTEGER_CST
&& wi::mod_trunc (wi::to_wide (c), wi::to_wide (s),
TYPE_SIGN (type)) == 0)
|| (TYPE_OVERFLOW_UNDEFINED (type)
&& multiple_of_p (type, c, s)))
{
/* If C is an exact multiple of S, then its value will be reached before
the induction variable overflows (unless the loop is exited in some
other way before). Note that the actual induction variable in the
loop (which ranges from base to final instead of from 0 to C) may
overflow, in which case BNDS.up will not be giving a correct upper
bound on C; thus, BNDS_U_VALID had to be computed in advance. */
no_overflow = true;
exit_must_be_taken = true;
}
/* If the induction variable can overflow, the number of iterations is at
most the period of the control variable (or infinite, but in that case
the whole # of iterations analysis will fail). */
if (!no_overflow)
{
max = wi::mask <widest_int> (TYPE_PRECISION (type)
- wi::ctz (wi::to_wide (s)), false);
wi::to_mpz (max, bnd, UNSIGNED);
return;
}
/* Now we know that the induction variable does not overflow, so the loop
iterates at most (range of type / S) times. */
wi::to_mpz (wi::minus_one (TYPE_PRECISION (type)), bnd, UNSIGNED);
/* If the induction variable is guaranteed to reach the value of C before
overflow, ... */
if (exit_must_be_taken)
{
/* ... then we can strengthen this to C / S, and possibly we can use
the upper bound on C given by BNDS. */
if (TREE_CODE (c) == INTEGER_CST)
wi::to_mpz (wi::to_wide (c), bnd, UNSIGNED);
else if (bnds_u_valid)
mpz_set (bnd, bnds->up);
}
mpz_init (d);
wi::to_mpz (wi::to_wide (s), d, UNSIGNED);
mpz_fdiv_q (bnd, bnd, d);
mpz_clear (d);
}
/* Determines number of iterations of loop whose ending condition
is IV <> FINAL. TYPE is the type of the iv. The number of
iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if
we know that the exit must be taken eventually, i.e., that the IV
ever reaches the value FINAL (we derived this earlier, and possibly set
NITER->assumptions to make sure this is the case). BNDS contains the
bounds on the difference FINAL - IV->base. */
static bool
number_of_iterations_ne (class loop *loop, tree type, affine_iv *iv,
tree final, class tree_niter_desc *niter,
bool exit_must_be_taken, bounds *bnds)
{
tree niter_type = unsigned_type_for (type);
tree s, c, d, bits, assumption, tmp, bound;
mpz_t max;
niter->control = *iv;
niter->bound = final;
niter->cmp = NE_EXPR;
/* Rearrange the terms so that we get inequality S * i <> C, with S
positive. Also cast everything to the unsigned type. If IV does
not overflow, BNDS bounds the value of C. Also, this is the
case if the computation |FINAL - IV->base| does not overflow, i.e.,
if BNDS->below in the result is nonnegative. */
if (tree_int_cst_sign_bit (iv->step))
{
s = fold_convert (niter_type,
fold_build1 (NEGATE_EXPR, type, iv->step));
c = fold_build2 (MINUS_EXPR, niter_type,
fold_convert (niter_type, iv->base),
fold_convert (niter_type, final));
bounds_negate (bnds);
}
else
{
s = fold_convert (niter_type, iv->step);
c = fold_build2 (MINUS_EXPR, niter_type,
fold_convert (niter_type, final),
fold_convert (niter_type, iv->base));
}
mpz_init (max);