forked from illumos/gcc
/
tree-vrp.c
4179 lines (3485 loc) · 122 KB
/
tree-vrp.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
/* Support routines for Value Range Propagation (VRP).
Copyright (C) 2005 Free Software Foundation, Inc.
Contributed by Diego Novillo <dnovillo@redhat.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 2, 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 COPYING. If not, write to
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "ggc.h"
#include "flags.h"
#include "tree.h"
#include "basic-block.h"
#include "tree-flow.h"
#include "tree-pass.h"
#include "tree-dump.h"
#include "timevar.h"
#include "diagnostic.h"
#include "cfgloop.h"
#include "tree-scalar-evolution.h"
#include "tree-ssa-propagate.h"
#include "tree-chrec.h"
/* Set of SSA names found during the dominator traversal of a
sub-graph in find_assert_locations. */
static sbitmap found_in_subgraph;
/* Loop structure of the program. Used to analyze scalar evolutions
inside adjust_range_with_scev. */
static struct loops *cfg_loops;
/* Local functions. */
static int compare_values (tree val1, tree val2);
/* Location information for ASSERT_EXPRs. Each instance of this
structure describes an ASSERT_EXPR for an SSA name. Since a single
SSA name may have more than one assertion associated with it, these
locations are kept in a linked list attached to the corresponding
SSA name. */
struct assert_locus_d
{
/* Basic block where the assertion would be inserted. */
basic_block bb;
/* Some assertions need to be inserted on an edge (e.g., assertions
generated by COND_EXPRs). In those cases, BB will be NULL. */
edge e;
/* Pointer to the statement that generated this assertion. */
block_stmt_iterator si;
/* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
enum tree_code comp_code;
/* Value being compared against. */
tree val;
/* Next node in the linked list. */
struct assert_locus_d *next;
};
typedef struct assert_locus_d *assert_locus_t;
/* If bit I is present, it means that SSA name N_i has a list of
assertions that should be inserted in the IL. */
static bitmap need_assert_for;
/* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
holds a list of ASSERT_LOCUS_T nodes that describe where
ASSERT_EXPRs for SSA name N_I should be inserted. */
static assert_locus_t *asserts_for;
/* Set of blocks visited in find_assert_locations. Used to avoid
visiting the same block more than once. */
static sbitmap blocks_visited;
/* Value range array. After propagation, VR_VALUE[I] holds the range
of values that SSA name N_I may take. */
static value_range_t **vr_value;
/* Return true if ARG is marked with the nonnull attribute in the
current function signature. */
static bool
nonnull_arg_p (tree arg)
{
tree t, attrs, fntype;
unsigned HOST_WIDE_INT arg_num;
gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
fntype = TREE_TYPE (current_function_decl);
attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (fntype));
/* If "nonnull" wasn't specified, we know nothing about the argument. */
if (attrs == NULL_TREE)
return false;
/* If "nonnull" applies to all the arguments, then ARG is non-null. */
if (TREE_VALUE (attrs) == NULL_TREE)
return true;
/* Get the position number for ARG in the function signature. */
for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
t;
t = TREE_CHAIN (t), arg_num++)
{
if (t == arg)
break;
}
gcc_assert (t == arg);
/* Now see if ARG_NUM is mentioned in the nonnull list. */
for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
{
if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
return true;
}
return false;
}
/* Set value range VR to {T, MIN, MAX, EQUIV}. */
static void
set_value_range (value_range_t *vr, enum value_range_type t, tree min,
tree max, bitmap equiv)
{
#if defined ENABLE_CHECKING
/* Check the validity of the range. */
if (t == VR_RANGE || t == VR_ANTI_RANGE)
{
int cmp;
gcc_assert (min && max);
if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
gcc_assert (min != TYPE_MIN_VALUE (TREE_TYPE (min))
|| max != TYPE_MAX_VALUE (TREE_TYPE (max)));
cmp = compare_values (min, max);
gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
}
if (t == VR_UNDEFINED || t == VR_VARYING)
gcc_assert (min == NULL_TREE && max == NULL_TREE);
if (t == VR_UNDEFINED || t == VR_VARYING)
gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
#endif
vr->type = t;
vr->min = min;
vr->max = max;
/* Since updating the equivalence set involves deep copying the
bitmaps, only do it if absolutely necessary. */
if (vr->equiv == NULL)
vr->equiv = BITMAP_ALLOC (NULL);
if (equiv != vr->equiv)
{
if (equiv && !bitmap_empty_p (equiv))
bitmap_copy (vr->equiv, equiv);
else
bitmap_clear (vr->equiv);
}
}
/* Copy value range FROM into value range TO. */
static inline void
copy_value_range (value_range_t *to, value_range_t *from)
{
set_value_range (to, from->type, from->min, from->max, from->equiv);
}
/* Set value range VR to a non-NULL range of type TYPE. */
static inline void
set_value_range_to_nonnull (value_range_t *vr, tree type)
{
tree zero = build_int_cst (type, 0);
set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
}
/* Set value range VR to a NULL range of type TYPE. */
static inline void
set_value_range_to_null (value_range_t *vr, tree type)
{
tree zero = build_int_cst (type, 0);
set_value_range (vr, VR_RANGE, zero, zero, vr->equiv);
}
/* Set value range VR to VR_VARYING. */
static inline void
set_value_range_to_varying (value_range_t *vr)
{
vr->type = VR_VARYING;
vr->min = vr->max = NULL_TREE;
if (vr->equiv)
bitmap_clear (vr->equiv);
}
/* Set value range VR to VR_UNDEFINED. */
static inline void
set_value_range_to_undefined (value_range_t *vr)
{
vr->type = VR_UNDEFINED;
vr->min = vr->max = NULL_TREE;
if (vr->equiv)
bitmap_clear (vr->equiv);
}
/* Return value range information for VAR. Create an empty range
if none existed. */
static value_range_t *
get_value_range (tree var)
{
value_range_t *vr;
tree sym;
unsigned ver = SSA_NAME_VERSION (var);
vr = vr_value[ver];
if (vr)
return vr;
/* Create a default value range. */
vr_value[ver] = vr = xmalloc (sizeof (*vr));
memset (vr, 0, sizeof (*vr));
/* Allocate an equivalence set. */
vr->equiv = BITMAP_ALLOC (NULL);
/* If VAR is a default definition, the variable can take any value
in VAR's type. */
sym = SSA_NAME_VAR (var);
if (var == default_def (sym))
{
/* Try to use the "nonnull" attribute to create ~[0, 0]
anti-ranges for pointers. Note that this is only valid with
default definitions of PARM_DECLs. */
if (TREE_CODE (sym) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (sym))
&& nonnull_arg_p (sym))
set_value_range_to_nonnull (vr, TREE_TYPE (sym));
else
set_value_range_to_varying (vr);
}
return vr;
}
/* Update the value range and equivalence set for variable VAR to
NEW_VR. Return true if NEW_VR is different from VAR's previous
value.
NOTE: This function assumes that NEW_VR is a temporary value range
object created for the sole purpose of updating VAR's range. The
storage used by the equivalence set from NEW_VR will be freed by
this function. Do not call update_value_range when NEW_VR
is the range object associated with another SSA name. */
static inline bool
update_value_range (tree var, value_range_t *new_vr)
{
value_range_t *old_vr;
bool is_new;
/* Update the value range, if necessary. */
old_vr = get_value_range (var);
is_new = old_vr->type != new_vr->type
|| old_vr->min != new_vr->min
|| old_vr->max != new_vr->max
|| (old_vr->equiv == NULL && new_vr->equiv)
|| (old_vr->equiv && new_vr->equiv == NULL)
|| (!bitmap_equal_p (old_vr->equiv, new_vr->equiv));
if (is_new)
set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
new_vr->equiv);
BITMAP_FREE (new_vr->equiv);
new_vr->equiv = NULL;
return is_new;
}
/* Add VAR and VAR's equivalence set to EQUIV. */
static void
add_equivalence (bitmap equiv, tree var)
{
unsigned ver = SSA_NAME_VERSION (var);
value_range_t *vr = vr_value[ver];
bitmap_set_bit (equiv, ver);
if (vr && vr->equiv)
bitmap_ior_into (equiv, vr->equiv);
}
/* Return true if VR is ~[0, 0]. */
static inline bool
range_is_nonnull (value_range_t *vr)
{
return vr->type == VR_ANTI_RANGE
&& integer_zerop (vr->min)
&& integer_zerop (vr->max);
}
/* Return true if VR is [0, 0]. */
static inline bool
range_is_null (value_range_t *vr)
{
return vr->type == VR_RANGE
&& integer_zerop (vr->min)
&& integer_zerop (vr->max);
}
/* Return true if value range VR involves at least one symbol. */
static inline bool
symbolic_range_p (value_range_t *vr)
{
return (!is_gimple_min_invariant (vr->min)
|| !is_gimple_min_invariant (vr->max));
}
/* Like tree_expr_nonzero_p, but this function uses value ranges
obtained so far. */
static bool
vrp_expr_computes_nonzero (tree expr)
{
if (tree_expr_nonzero_p (expr))
return true;
/* If we have an expression of the form &X->a, then the expression
is nonnull if X is nonnull. */
if (TREE_CODE (expr) == ADDR_EXPR)
{
tree base = get_base_address (TREE_OPERAND (expr, 0));
if (base != NULL_TREE
&& TREE_CODE (base) == INDIRECT_REF
&& TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
{
value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
if (range_is_nonnull (vr))
return true;
}
}
return false;
}
/* Compare two values VAL1 and VAL2. Return
-2 if VAL1 and VAL2 cannot be compared at compile-time,
-1 if VAL1 < VAL2,
0 if VAL1 == VAL2,
+1 if VAL1 > VAL2, and
+2 if VAL1 != VAL2
This is similar to tree_int_cst_compare but supports pointer values
and values that cannot be compared at compile time. */
static int
compare_values (tree val1, tree val2)
{
if (val1 == val2)
return 0;
/* Below we rely on the fact that VAL1 and VAL2 are both pointers or
both integers. */
gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
== POINTER_TYPE_P (TREE_TYPE (val2)));
/* Do some limited symbolic comparisons. */
if (!POINTER_TYPE_P (TREE_TYPE (val1)))
{
/* We can determine some comparisons against +INF and -INF even
if the other value is an expression. */
if (val1 == TYPE_MAX_VALUE (TREE_TYPE (val1))
&& TREE_CODE (val2) == MINUS_EXPR)
{
/* +INF > NAME - CST. */
return 1;
}
else if (val1 == TYPE_MIN_VALUE (TREE_TYPE (val1))
&& TREE_CODE (val2) == PLUS_EXPR)
{
/* -INF < NAME + CST. */
return -1;
}
else if (TREE_CODE (val1) == MINUS_EXPR
&& val2 == TYPE_MAX_VALUE (TREE_TYPE (val2)))
{
/* NAME - CST < +INF. */
return -1;
}
else if (TREE_CODE (val1) == PLUS_EXPR
&& val2 == TYPE_MIN_VALUE (TREE_TYPE (val2)))
{
/* NAME + CST > -INF. */
return 1;
}
}
if ((TREE_CODE (val1) == SSA_NAME
|| TREE_CODE (val1) == PLUS_EXPR
|| TREE_CODE (val1) == MINUS_EXPR)
&& (TREE_CODE (val2) == SSA_NAME
|| TREE_CODE (val2) == PLUS_EXPR
|| TREE_CODE (val2) == MINUS_EXPR))
{
tree n1, c1, n2, c2;
/* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
same name, return -2. */
if (TREE_CODE (val1) == SSA_NAME)
{
n1 = val1;
c1 = NULL_TREE;
}
else
{
n1 = TREE_OPERAND (val1, 0);
c1 = TREE_OPERAND (val1, 1);
}
if (TREE_CODE (val2) == SSA_NAME)
{
n2 = val2;
c2 = NULL_TREE;
}
else
{
n2 = TREE_OPERAND (val2, 0);
c2 = TREE_OPERAND (val2, 1);
}
/* Both values must use the same name. */
if (n1 != n2)
return -2;
if (TREE_CODE (val1) == SSA_NAME)
{
if (TREE_CODE (val2) == SSA_NAME)
/* NAME == NAME */
return 0;
else if (TREE_CODE (val2) == PLUS_EXPR)
/* NAME < NAME + CST */
return -1;
else if (TREE_CODE (val2) == MINUS_EXPR)
/* NAME > NAME - CST */
return 1;
}
else if (TREE_CODE (val1) == PLUS_EXPR)
{
if (TREE_CODE (val2) == SSA_NAME)
/* NAME + CST > NAME */
return 1;
else if (TREE_CODE (val2) == PLUS_EXPR)
/* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
return compare_values (c1, c2);
else if (TREE_CODE (val2) == MINUS_EXPR)
/* NAME + CST1 > NAME - CST2 */
return 1;
}
else if (TREE_CODE (val1) == MINUS_EXPR)
{
if (TREE_CODE (val2) == SSA_NAME)
/* NAME - CST < NAME */
return -1;
else if (TREE_CODE (val2) == PLUS_EXPR)
/* NAME - CST1 < NAME + CST2 */
return -1;
else if (TREE_CODE (val2) == MINUS_EXPR)
/* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
C1 and C2 are swapped in the call to compare_values. */
return compare_values (c2, c1);
}
gcc_unreachable ();
}
/* We cannot compare non-constants. */
if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
return -2;
/* We cannot compare overflowed values. */
if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
return -2;
if (!POINTER_TYPE_P (TREE_TYPE (val1)))
return tree_int_cst_compare (val1, val2);
else
{
tree t;
/* First see if VAL1 and VAL2 are not the same. */
if (val1 == val2 || operand_equal_p (val1, val2, 0))
return 0;
/* If VAL1 is a lower address than VAL2, return -1. */
t = fold_binary (LT_EXPR, boolean_type_node, val1, val2);
if (t == boolean_true_node)
return -1;
/* If VAL1 is a higher address than VAL2, return +1. */
t = fold_binary (GT_EXPR, boolean_type_node, val1, val2);
if (t == boolean_true_node)
return 1;
/* If VAL1 is different than VAL2, return +2. */
t = fold_binary (NE_EXPR, boolean_type_node, val1, val2);
if (t == boolean_true_node)
return 2;
return -2;
}
}
/* Return 1 if VAL is inside value range VR (VR->MIN <= VAL <= VR->MAX),
0 if VAL is not inside VR,
-2 if we cannot tell either way.
FIXME, the current semantics of this functions are a bit quirky
when taken in the context of VRP. In here we do not care
about VR's type. If VR is the anti-range ~[3, 5] the call
value_inside_range (4, VR) will return 1.
This is counter-intuitive in a strict sense, but the callers
currently expect this. They are calling the function
merely to determine whether VR->MIN <= VAL <= VR->MAX. The
callers are applying the VR_RANGE/VR_ANTI_RANGE semantics
themselves.
This also applies to value_ranges_intersect_p and
range_includes_zero_p. The semantics of VR_RANGE and
VR_ANTI_RANGE should be encoded here, but that also means
adapting the users of these functions to the new semantics. */
static inline int
value_inside_range (tree val, value_range_t *vr)
{
int cmp1, cmp2;
cmp1 = compare_values (val, vr->min);
if (cmp1 == -2 || cmp1 == 2)
return -2;
cmp2 = compare_values (val, vr->max);
if (cmp2 == -2 || cmp2 == 2)
return -2;
return (cmp1 == 0 || cmp1 == 1) && (cmp2 == -1 || cmp2 == 0);
}
/* Return true if value ranges VR0 and VR1 have a non-empty
intersection. */
static inline bool
value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
{
return (value_inside_range (vr1->min, vr0) == 1
|| value_inside_range (vr1->max, vr0) == 1
|| value_inside_range (vr0->min, vr1) == 1
|| value_inside_range (vr0->max, vr1) == 1);
}
/* Return true if VR includes the value zero, false otherwise. FIXME,
currently this will return false for an anti-range like ~[-4, 3].
This will be wrong when the semantics of value_inside_range are
modified (currently the users of this function expect these
semantics). */
static inline bool
range_includes_zero_p (value_range_t *vr)
{
tree zero;
gcc_assert (vr->type != VR_UNDEFINED
&& vr->type != VR_VARYING
&& !symbolic_range_p (vr));
zero = build_int_cst (TREE_TYPE (vr->min), 0);
return (value_inside_range (zero, vr) == 1);
}
/* When extracting ranges from X_i = ASSERT_EXPR <Y_j, pred>, we will
initially consider X_i and Y_j equivalent, so the equivalence set
of Y_j is added to the equivalence set of X_i. However, it is
possible to have a chain of ASSERT_EXPRs whose predicates are
actually incompatible. This is usually the result of nesting of
contradictory if-then-else statements. For instance, in PR 24670:
count_4 has range [-INF, 63]
if (count_4 != 0)
{
count_19 = ASSERT_EXPR <count_4, count_4 != 0>
if (count_19 > 63)
{
count_18 = ASSERT_EXPR <count_19, count_19 > 63>
if (count_18 <= 63)
...
}
}
Notice that 'if (count_19 > 63)' is trivially false and will be
folded out at the end. However, during propagation, the flowgraph
is not cleaned up and so, VRP will evaluate predicates more
predicates than necessary, so it must support these
inconsistencies. The problem here is that because of the chaining
of ASSERT_EXPRs, the equivalency set for count_18 includes count_4.
Since count_4 has an incompatible range, we ICE when evaluating the
ranges in the equivalency set. So, we need to remove count_4 from
it. */
static void
fix_equivalence_set (value_range_t *vr_p)
{
bitmap_iterator bi;
unsigned i;
bitmap e = vr_p->equiv;
bitmap to_remove = BITMAP_ALLOC (NULL);
/* Only detect inconsistencies on numeric ranges. */
if (vr_p->type == VR_VARYING
|| vr_p->type == VR_UNDEFINED
|| symbolic_range_p (vr_p))
return;
EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
{
value_range_t *equiv_vr = vr_value[i];
if (equiv_vr->type == VR_VARYING
|| equiv_vr->type == VR_UNDEFINED
|| symbolic_range_p (equiv_vr))
continue;
if (equiv_vr->type == VR_RANGE
&& vr_p->type == VR_RANGE
&& !value_ranges_intersect_p (vr_p, equiv_vr))
bitmap_set_bit (to_remove, i);
else if ((equiv_vr->type == VR_RANGE && vr_p->type == VR_ANTI_RANGE)
|| (equiv_vr->type == VR_ANTI_RANGE && vr_p->type == VR_RANGE))
{
/* A range and an anti-range have an empty intersection if
their end points are the same. FIXME,
value_ranges_intersect_p should handle this
automatically. */
if (compare_values (equiv_vr->min, vr_p->min) == 0
&& compare_values (equiv_vr->max, vr_p->max) == 0)
bitmap_set_bit (to_remove, i);
}
}
bitmap_and_compl_into (vr_p->equiv, to_remove);
BITMAP_FREE (to_remove);
}
/* Extract value range information from an ASSERT_EXPR EXPR and store
it in *VR_P. */
static void
extract_range_from_assert (value_range_t *vr_p, tree expr)
{
tree var, cond, limit, min, max, type;
value_range_t *var_vr, *limit_vr;
enum tree_code cond_code;
var = ASSERT_EXPR_VAR (expr);
cond = ASSERT_EXPR_COND (expr);
gcc_assert (COMPARISON_CLASS_P (cond));
/* Find VAR in the ASSERT_EXPR conditional. */
if (var == TREE_OPERAND (cond, 0))
{
/* If the predicate is of the form VAR COMP LIMIT, then we just
take LIMIT from the RHS and use the same comparison code. */
limit = TREE_OPERAND (cond, 1);
cond_code = TREE_CODE (cond);
}
else
{
/* If the predicate is of the form LIMIT COMP VAR, then we need
to flip around the comparison code to create the proper range
for VAR. */
limit = TREE_OPERAND (cond, 0);
cond_code = swap_tree_comparison (TREE_CODE (cond));
}
type = TREE_TYPE (limit);
gcc_assert (limit != var);
/* For pointer arithmetic, we only keep track of pointer equality
and inequality. */
if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
{
set_value_range_to_varying (vr_p);
return;
}
/* If LIMIT is another SSA name and LIMIT has a range of its own,
try to use LIMIT's range to avoid creating symbolic ranges
unnecessarily. */
limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
/* LIMIT's range is only interesting if it has any useful information. */
if (limit_vr
&& (limit_vr->type == VR_UNDEFINED
|| limit_vr->type == VR_VARYING
|| symbolic_range_p (limit_vr)))
limit_vr = NULL;
/* Special handling for integral types with super-types. Some FEs
construct integral types derived from other types and restrict
the range of values these new types may take.
It may happen that LIMIT is actually smaller than TYPE's minimum
value. For instance, the Ada FE is generating code like this
during bootstrap:
D.1480_32 = nam_30 - 300000361;
if (D.1480_32 <= 1) goto <L112>; else goto <L52>;
<L112>:;
D.1480_94 = ASSERT_EXPR <D.1480_32, D.1480_32 <= 1>;
All the names are of type types__name_id___XDLU_300000000__399999999
which has min == 300000000 and max == 399999999. This means that
the ASSERT_EXPR would try to create the range [3000000, 1] which
is invalid.
The fact that the type specifies MIN and MAX values does not
automatically mean that every variable of that type will always
be within that range, so the predicate may well be true at run
time. If we had symbolic -INF and +INF values, we could
represent this range, but we currently represent -INF and +INF
using the type's min and max values.
So, the only sensible thing we can do for now is set the
resulting range to VR_VARYING. TODO, would having symbolic -INF
and +INF values be worth the trouble? */
if (TREE_CODE (limit) != SSA_NAME
&& INTEGRAL_TYPE_P (type)
&& TREE_TYPE (type))
{
if (cond_code == LE_EXPR || cond_code == LT_EXPR)
{
tree type_min = TYPE_MIN_VALUE (type);
int cmp = compare_values (limit, type_min);
/* For < or <= comparisons, if LIMIT is smaller than
TYPE_MIN, set the range to VR_VARYING. */
if (cmp == -1 || cmp == 0)
{
set_value_range_to_varying (vr_p);
return;
}
}
else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
{
tree type_max = TYPE_MIN_VALUE (type);
int cmp = compare_values (limit, type_max);
/* For > or >= comparisons, if LIMIT is bigger than
TYPE_MAX, set the range to VR_VARYING. */
if (cmp == 1 || cmp == 0)
{
set_value_range_to_varying (vr_p);
return;
}
}
}
/* Initially, the new range has the same set of equivalences of
VAR's range. This will be revised before returning the final
value. Since assertions may be chained via mutually exclusive
predicates, we will need to trim the set of equivalences before
we are done. */
gcc_assert (vr_p->equiv == NULL);
vr_p->equiv = BITMAP_ALLOC (NULL);
add_equivalence (vr_p->equiv, var);
/* Extract a new range based on the asserted comparison for VAR and
LIMIT's value range. Notice that if LIMIT has an anti-range, we
will only use it for equality comparisons (EQ_EXPR). For any
other kind of assertion, we cannot derive a range from LIMIT's
anti-range that can be used to describe the new range. For
instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
no single range for x_2 that could describe LE_EXPR, so we might
as well build the range [b_4, +INF] for it. */
if (cond_code == EQ_EXPR)
{
enum value_range_type range_type;
if (limit_vr)
{
range_type = limit_vr->type;
min = limit_vr->min;
max = limit_vr->max;
}
else
{
range_type = VR_RANGE;
min = limit;
max = limit;
}
set_value_range (vr_p, range_type, min, max, vr_p->equiv);
/* When asserting the equality VAR == LIMIT and LIMIT is another
SSA name, the new range will also inherit the equivalence set
from LIMIT. */
if (TREE_CODE (limit) == SSA_NAME)
add_equivalence (vr_p->equiv, limit);
}
else if (cond_code == NE_EXPR)
{
/* As described above, when LIMIT's range is an anti-range and
this assertion is an inequality (NE_EXPR), then we cannot
derive anything from the anti-range. For instance, if
LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
not imply that VAR's range is [0, 0]. So, in the case of
anti-ranges, we just assert the inequality using LIMIT and
not its anti-range.
If LIMIT_VR is a range, we can only use it to build a new
anti-range if LIMIT_VR is a single-valued range. For
instance, if LIMIT_VR is [0, 1], the predicate
VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
Rather, it means that for value 0 VAR should be ~[0, 0]
and for value 1, VAR should be ~[1, 1]. We cannot
represent these ranges.
The only situation in which we can build a valid
anti-range is when LIMIT_VR is a single-valued range
(i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
if (limit_vr
&& limit_vr->type == VR_RANGE
&& compare_values (limit_vr->min, limit_vr->max) == 0)
{
min = limit_vr->min;
max = limit_vr->max;
}
else
{
/* In any other case, we cannot use LIMIT's range to build a
valid anti-range. */
min = max = limit;
}
/* If MIN and MAX cover the whole range for their type, then
just use the original LIMIT. */
if (INTEGRAL_TYPE_P (type)
&& min == TYPE_MIN_VALUE (type)
&& max == TYPE_MAX_VALUE (type))
min = max = limit;
set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
}
else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
{
min = TYPE_MIN_VALUE (type);
if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
max = limit;
else
{
/* If LIMIT_VR is of the form [N1, N2], we need to build the
range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
LT_EXPR. */
max = limit_vr->max;
}
/* For LT_EXPR, we create the range [MIN, MAX - 1]. */
if (cond_code == LT_EXPR)
{
tree one = build_int_cst (type, 1);
max = fold_build2 (MINUS_EXPR, type, max, one);
}
set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
}
else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
{
max = TYPE_MAX_VALUE (type);
if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
min = limit;
else
{
/* If LIMIT_VR is of the form [N1, N2], we need to build the
range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
GT_EXPR. */
min = limit_vr->min;
}
/* For GT_EXPR, we create the range [MIN + 1, MAX]. */
if (cond_code == GT_EXPR)
{
tree one = build_int_cst (type, 1);
min = fold_build2 (PLUS_EXPR, type, min, one);
}
set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
}
else
gcc_unreachable ();
/* If VAR already had a known range, it may happen that the new
range we have computed and VAR's range are not compatible. For
instance,
if (p_5 == NULL)
p_6 = ASSERT_EXPR <p_5, p_5 == NULL>;
x_7 = p_6->fld;
p_8 = ASSERT_EXPR <p_6, p_6 != NULL>;
While the above comes from a faulty program, it will cause an ICE
later because p_8 and p_6 will have incompatible ranges and at
the same time will be considered equivalent. A similar situation
would arise from
if (i_5 > 10)
i_6 = ASSERT_EXPR <i_5, i_5 > 10>;
if (i_5 < 5)
i_7 = ASSERT_EXPR <i_6, i_6 < 5>;
Again i_6 and i_7 will have incompatible ranges. It would be
pointless to try and do anything with i_7's range because
anything dominated by 'if (i_5 < 5)' will be optimized away.
Note, due to the wa in which simulation proceeds, the statement
i_7 = ASSERT_EXPR <...> we would never be visited because the
conditional 'if (i_5 < 5)' always evaluates to false. However,
this extra check does not hurt and may protect against future
changes to VRP that may get into a situation similar to the
NULL pointer dereference example.
Note that these compatibility tests are only needed when dealing
with ranges or a mix of range and anti-range. If VAR_VR and VR_P
are both anti-ranges, they will always be compatible, because two
anti-ranges will always have a non-empty intersection. */
var_vr = get_value_range (var);
/* We may need to make adjustments when VR_P and VAR_VR are numeric
ranges or anti-ranges. */
if (vr_p->type == VR_VARYING