forked from riscvarchive/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvr-values.c
4361 lines (3836 loc) · 130 KB
/
vr-values.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-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 "insn-codes.h"
#include "tree.h"
#include "gimple.h"
#include "ssa.h"
#include "optabs-tree.h"
#include "gimple-pretty-print.h"
#include "diagnostic-core.h"
#include "flags.h"
#include "fold-const.h"
#include "calls.h"
#include "cfganal.h"
#include "gimple-fold.h"
#include "gimple-iterator.h"
#include "tree-cfg.h"
#include "tree-ssa-loop-niter.h"
#include "tree-ssa-loop.h"
#include "intl.h"
#include "cfgloop.h"
#include "tree-scalar-evolution.h"
#include "tree-ssa-propagate.h"
#include "tree-chrec.h"
#include "omp-general.h"
#include "case-cfn-macros.h"
#include "alloc-pool.h"
#include "attribs.h"
#include "range.h"
#include "vr-values.h"
#include "cfghooks.h"
#include "range-op.h"
/* Set value range VR to a non-negative range of type TYPE. */
static inline void
set_value_range_to_nonnegative (value_range_equiv *vr, tree type)
{
tree zero = build_int_cst (type, 0);
vr->update (zero, vrp_val_max (type));
}
/* Set value range VR to a range of a truthvalue of type TYPE. */
static inline void
set_value_range_to_truthvalue (value_range_equiv *vr, tree type)
{
if (TYPE_PRECISION (type) == 1)
vr->set_varying (type);
else
vr->update (build_int_cst (type, 0), build_int_cst (type, 1));
}
/* Return the lattice entry for VAR or NULL if it doesn't exist or cannot
be initialized. */
value_range_equiv *
vr_values::get_lattice_entry (const_tree var)
{
value_range_equiv *vr;
tree sym;
unsigned ver = SSA_NAME_VERSION (var);
/* If we query the entry for a new SSA name avoid reallocating the lattice
since we should get here at most from the substitute-and-fold stage which
will never try to change values. */
if (ver >= num_vr_values)
return NULL;
vr = vr_value[ver];
if (vr)
return vr;
/* Create a default value range. */
vr_value[ver] = vr = vrp_value_range_pool.allocate ();
/* After propagation finished return varying. */
if (values_propagated)
{
vr->set_varying (TREE_TYPE (var));
return vr;
}
vr->set_undefined ();
/* If VAR is a default definition of a parameter, the variable can
take any value in VAR's type. */
if (SSA_NAME_IS_DEFAULT_DEF (var))
{
sym = SSA_NAME_VAR (var);
if (TREE_CODE (sym) == PARM_DECL)
{
/* 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 (POINTER_TYPE_P (TREE_TYPE (sym))
&& (nonnull_arg_p (sym)
|| get_ptr_nonnull (var)))
{
vr->set_nonzero (TREE_TYPE (sym));
vr->equiv_clear ();
}
else if (INTEGRAL_TYPE_P (TREE_TYPE (sym)))
{
get_range_info (var, *vr);
if (vr->undefined_p ())
vr->set_varying (TREE_TYPE (sym));
}
else
vr->set_varying (TREE_TYPE (sym));
}
else if (TREE_CODE (sym) == RESULT_DECL
&& DECL_BY_REFERENCE (sym))
{
vr->set_nonzero (TREE_TYPE (sym));
vr->equiv_clear ();
}
}
return vr;
}
/* Return value range information for VAR.
If we have no values ranges recorded (ie, VRP is not running), then
return NULL. Otherwise create an empty range if none existed for VAR. */
const value_range_equiv *
vr_values::get_value_range (const_tree var)
{
/* If we have no recorded ranges, then return NULL. */
if (!vr_value)
return NULL;
value_range_equiv *vr = get_lattice_entry (var);
/* Reallocate the lattice if needed. */
if (!vr)
{
unsigned int old_sz = num_vr_values;
num_vr_values = num_ssa_names + num_ssa_names / 10;
vr_value = XRESIZEVEC (value_range_equiv *, vr_value, num_vr_values);
for ( ; old_sz < num_vr_values; old_sz++)
vr_value [old_sz] = NULL;
/* Now that the lattice has been resized, we should never fail. */
vr = get_lattice_entry (var);
gcc_assert (vr);
}
return vr;
}
/* Set the lattice entry for DEF to VARYING. */
void
vr_values::set_def_to_varying (const_tree def)
{
value_range_equiv *vr = get_lattice_entry (def);
if (vr)
vr->set_varying (TREE_TYPE (def));
}
/* Set value-ranges of all SSA names defined by STMT to varying. */
void
vr_values::set_defs_to_varying (gimple *stmt)
{
ssa_op_iter i;
tree def;
FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
set_def_to_varying (def);
}
/* 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. */
bool
vr_values::update_value_range (const_tree var, value_range_equiv *new_vr)
{
value_range_equiv *old_vr;
bool is_new;
/* If there is a value-range on the SSA name from earlier analysis
factor that in. */
if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
{
value_range_equiv nr;
get_range_info (var, nr);
if (!nr.undefined_p ())
new_vr->intersect (&nr);
}
/* Update the value range, if necessary. If we cannot allocate a lattice
entry for VAR keep it at VARYING. This happens when DOM feeds us stmts
with SSA names allocated after setting up the lattice. */
old_vr = get_lattice_entry (var);
if (!old_vr)
return false;
is_new = !old_vr->equal_p (*new_vr, /*ignore_equivs=*/false);
if (is_new)
{
/* Do not allow transitions up the lattice. The following
is slightly more awkward than just new_vr->type < old_vr->type
because VR_RANGE and VR_ANTI_RANGE need to be considered
the same. We may not have is_new when transitioning to
UNDEFINED. If old_vr->type is VARYING, we shouldn't be
called, if we are anyway, keep it VARYING. */
if (old_vr->varying_p ())
{
new_vr->set_varying (TREE_TYPE (var));
is_new = false;
}
else if (new_vr->undefined_p ())
{
old_vr->set_varying (TREE_TYPE (var));
new_vr->set_varying (TREE_TYPE (var));
return true;
}
else
old_vr->set (new_vr->min (), new_vr->max (), new_vr->equiv (),
new_vr->kind ());
}
new_vr->equiv_clear ();
return is_new;
}
/* Return true if value range VR involves exactly one symbol SYM. */
static bool
symbolic_range_based_on_p (value_range *vr, const_tree sym)
{
bool neg, min_has_symbol, max_has_symbol;
tree inv;
if (is_gimple_min_invariant (vr->min ()))
min_has_symbol = false;
else if (get_single_symbol (vr->min (), &neg, &inv) == sym)
min_has_symbol = true;
else
return false;
if (is_gimple_min_invariant (vr->max ()))
max_has_symbol = false;
else if (get_single_symbol (vr->max (), &neg, &inv) == sym)
max_has_symbol = true;
else
return false;
return (min_has_symbol || max_has_symbol);
}
/* Return true if the result of assignment STMT is know to be non-zero. */
static bool
gimple_assign_nonzero_p (gimple *stmt)
{
enum tree_code code = gimple_assign_rhs_code (stmt);
bool strict_overflow_p;
switch (get_gimple_rhs_class (code))
{
case GIMPLE_UNARY_RHS:
return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
gimple_expr_type (stmt),
gimple_assign_rhs1 (stmt),
&strict_overflow_p);
case GIMPLE_BINARY_RHS:
return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
gimple_expr_type (stmt),
gimple_assign_rhs1 (stmt),
gimple_assign_rhs2 (stmt),
&strict_overflow_p);
case GIMPLE_TERNARY_RHS:
return false;
case GIMPLE_SINGLE_RHS:
return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
&strict_overflow_p);
case GIMPLE_INVALID_RHS:
gcc_unreachable ();
default:
gcc_unreachable ();
}
}
/* Return true if STMT is known to compute a non-zero value. */
static bool
gimple_stmt_nonzero_p (gimple *stmt)
{
switch (gimple_code (stmt))
{
case GIMPLE_ASSIGN:
return gimple_assign_nonzero_p (stmt);
case GIMPLE_CALL:
{
gcall *call_stmt = as_a<gcall *> (stmt);
return (gimple_call_nonnull_result_p (call_stmt)
|| gimple_call_nonnull_arg (call_stmt));
}
default:
gcc_unreachable ();
}
}
/* Like tree_expr_nonzero_p, but this function uses value ranges
obtained so far. */
bool
vr_values::vrp_stmt_computes_nonzero (gimple *stmt)
{
if (gimple_stmt_nonzero_p (stmt))
return true;
/* If we have an expression of the form &X->a, then the expression
is nonnull if X is nonnull. */
if (is_gimple_assign (stmt)
&& gimple_assign_rhs_code (stmt) == ADDR_EXPR)
{
tree expr = gimple_assign_rhs1 (stmt);
poly_int64 bitsize, bitpos;
tree offset;
machine_mode mode;
int unsignedp, reversep, volatilep;
tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
&bitpos, &offset, &mode, &unsignedp,
&reversep, &volatilep);
if (base != NULL_TREE
&& TREE_CODE (base) == MEM_REF
&& TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
{
poly_offset_int off = 0;
bool off_cst = false;
if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
{
off = mem_ref_offset (base);
if (offset)
off += poly_offset_int::from (wi::to_poly_wide (offset),
SIGNED);
off <<= LOG2_BITS_PER_UNIT;
off += bitpos;
off_cst = true;
}
/* If &X->a is equal to X and X is ~[0, 0], the result is too.
For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
allow going from non-NULL pointer to NULL. */
if ((off_cst && known_eq (off, 0))
|| (flag_delete_null_pointer_checks
&& !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))))
{
const value_range_equiv *vr
= get_value_range (TREE_OPERAND (base, 0));
if (!range_includes_zero_p (vr))
return true;
}
/* If MEM_REF has a "positive" offset, consider it non-NULL
always, for -fdelete-null-pointer-checks also "negative"
ones. Punt for unknown offsets (e.g. variable ones). */
if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
&& off_cst
&& known_ne (off, 0)
&& (flag_delete_null_pointer_checks || known_gt (off, 0)))
return true;
}
}
return false;
}
/* Returns true if EXPR is a valid value (as expected by compare_values) --
a gimple invariant, or SSA_NAME +- CST. */
static bool
valid_value_p (tree expr)
{
if (TREE_CODE (expr) == SSA_NAME)
return true;
if (TREE_CODE (expr) == PLUS_EXPR
|| TREE_CODE (expr) == MINUS_EXPR)
return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
&& TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
return is_gimple_min_invariant (expr);
}
/* If OP has a value range with a single constant value return that,
otherwise return NULL_TREE. This returns OP itself if OP is a
constant. */
tree
vr_values::op_with_constant_singleton_value_range (tree op)
{
if (is_gimple_min_invariant (op))
return op;
if (TREE_CODE (op) != SSA_NAME)
return NULL_TREE;
tree t;
if (get_value_range (op)->singleton_p (&t))
return t;
return NULL;
}
/* Return true if op is in a boolean [0, 1] value-range. */
bool
vr_values::op_with_boolean_value_range_p (tree op)
{
const value_range_equiv *vr;
if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
return true;
if (integer_zerop (op)
|| integer_onep (op))
return true;
if (TREE_CODE (op) != SSA_NAME)
return false;
vr = get_value_range (op);
return (vr->kind () == VR_RANGE
&& integer_zerop (vr->min ())
&& integer_onep (vr->max ()));
}
/* Extract value range information for VAR when (OP COND_CODE LIMIT) is
true and store it in *VR_P. */
void
vr_values::extract_range_for_var_from_comparison_expr (tree var,
enum tree_code cond_code,
tree op, tree limit,
value_range_equiv *vr_p)
{
tree min, max, type;
const value_range_equiv *limit_vr;
type = TREE_TYPE (var);
/* For pointer arithmetic, we only keep track of pointer equality
and inequality. If we arrive here with unfolded conditions like
_1 > _1 do not derive anything. */
if ((POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
|| limit == var)
{
vr_p->set_varying (type);
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->undefined_p ()
|| limit_vr->varying_p ()
|| (limit_vr->symbolic_p ()
&& ! (limit_vr->kind () == VR_RANGE
&& (limit_vr->min () == limit_vr->max ()
|| operand_equal_p (limit_vr->min (),
limit_vr->max (), 0)))))
limit_vr = NULL;
/* 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_add (var, get_value_range (var), &vrp_equiv_obstack);
/* 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.
One special case we handle is extracting a range from a
range test encoded as (unsigned)var + CST <= limit. */
if (TREE_CODE (op) == NOP_EXPR
|| TREE_CODE (op) == PLUS_EXPR)
{
if (TREE_CODE (op) == PLUS_EXPR)
{
min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)),
TREE_OPERAND (op, 1));
max = int_const_binop (PLUS_EXPR, limit, min);
op = TREE_OPERAND (op, 0);
}
else
{
min = build_int_cst (TREE_TYPE (var), 0);
max = limit;
}
/* Make sure to not set TREE_OVERFLOW on the final type
conversion. We are willingly interpreting large positive
unsigned values as negative signed values here. */
min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
/* We can transform a max, min range to an anti-range or
vice-versa. Use set_and_canonicalize which does this for
us. */
if (cond_code == LE_EXPR)
vr_p->set (min, max, vr_p->equiv ());
else if (cond_code == GT_EXPR)
vr_p->set (min, max, vr_p->equiv (), VR_ANTI_RANGE);
else
gcc_unreachable ();
}
else if (cond_code == EQ_EXPR)
{
enum value_range_kind range_kind;
if (limit_vr)
{
range_kind = limit_vr->kind ();
min = limit_vr->min ();
max = limit_vr->max ();
}
else
{
range_kind = VR_RANGE;
min = limit;
max = limit;
}
vr_p->update (min, max, range_kind);
/* 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)
vr_p->equiv_add (limit, get_value_range (limit), &vrp_equiv_obstack);
}
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->kind () == 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)
&& vrp_val_is_min (min)
&& vrp_val_is_max (max))
min = max = limit;
vr_p->set (min, max, vr_p->equiv (), VR_ANTI_RANGE);
}
else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
{
min = TYPE_MIN_VALUE (type);
if (limit_vr == NULL || limit_vr->kind () == 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 ();
}
/* If the maximum value forces us to be out of bounds, simply punt.
It would be pointless to try and do anything more since this
all should be optimized away above us. */
if (cond_code == LT_EXPR
&& compare_values (max, min) == 0)
vr_p->set_varying (TREE_TYPE (min));
else
{
/* For LT_EXPR, we create the range [MIN, MAX - 1]. */
if (cond_code == LT_EXPR)
{
if (TYPE_PRECISION (TREE_TYPE (max)) == 1
&& !TYPE_UNSIGNED (TREE_TYPE (max)))
max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
build_int_cst (TREE_TYPE (max), -1));
else
max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
build_int_cst (TREE_TYPE (max), 1));
/* Signal to compare_values_warnv this expr doesn't overflow. */
if (EXPR_P (max))
TREE_NO_WARNING (max) = 1;
}
vr_p->update (min, max);
}
}
else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
{
max = TYPE_MAX_VALUE (type);
if (limit_vr == NULL || limit_vr->kind () == 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 ();
}
/* If the minimum value forces us to be out of bounds, simply punt.
It would be pointless to try and do anything more since this
all should be optimized away above us. */
if (cond_code == GT_EXPR
&& compare_values (min, max) == 0)
vr_p->set_varying (TREE_TYPE (min));
else
{
/* For GT_EXPR, we create the range [MIN + 1, MAX]. */
if (cond_code == GT_EXPR)
{
if (TYPE_PRECISION (TREE_TYPE (min)) == 1
&& !TYPE_UNSIGNED (TREE_TYPE (min)))
min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
build_int_cst (TREE_TYPE (min), -1));
else
min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
build_int_cst (TREE_TYPE (min), 1));
/* Signal to compare_values_warnv this expr doesn't overflow. */
if (EXPR_P (min))
TREE_NO_WARNING (min) = 1;
}
vr_p->update (min, max);
}
}
else
gcc_unreachable ();
/* Finally intersect the new range with what we already know about var. */
vr_p->intersect (get_value_range (var));
}
/* Extract value range information from an ASSERT_EXPR EXPR and store
it in *VR_P. */
void
vr_values::extract_range_from_assert (value_range_equiv *vr_p, tree expr)
{
tree var = ASSERT_EXPR_VAR (expr);
tree cond = ASSERT_EXPR_COND (expr);
tree limit, op;
enum tree_code cond_code;
gcc_assert (COMPARISON_CLASS_P (cond));
/* Find VAR in the ASSERT_EXPR conditional. */
if (var == TREE_OPERAND (cond, 0)
|| TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
|| TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
{
/* If the predicate is of the form VAR COMP LIMIT, then we just
take LIMIT from the RHS and use the same comparison code. */
cond_code = TREE_CODE (cond);
limit = TREE_OPERAND (cond, 1);
op = TREE_OPERAND (cond, 0);
}
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. */
cond_code = swap_tree_comparison (TREE_CODE (cond));
limit = TREE_OPERAND (cond, 0);
op = TREE_OPERAND (cond, 1);
}
extract_range_for_var_from_comparison_expr (var, cond_code, op,
limit, vr_p);
}
/* Extract range information from SSA name VAR and store it in VR. If
VAR has an interesting range, use it. Otherwise, create the
range [VAR, VAR] and return it. This is useful in situations where
we may have conditionals testing values of VARYING names. For
instance,
x_3 = y_5;
if (x_3 > y_5)
...
Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
always false. */
void
vr_values::extract_range_from_ssa_name (value_range_equiv *vr, tree var)
{
const value_range_equiv *var_vr = get_value_range (var);
if (!var_vr->varying_p ())
vr->deep_copy (var_vr);
else
vr->set (var);
if (!vr->undefined_p ())
vr->equiv_add (var, get_value_range (var), &vrp_equiv_obstack);
}
/* Extract range information from a binary expression OP0 CODE OP1 based on
the ranges of each of its operands with resulting type EXPR_TYPE.
The resulting range is stored in *VR. */
void
vr_values::extract_range_from_binary_expr (value_range_equiv *vr,
enum tree_code code,
tree expr_type, tree op0, tree op1)
{
/* Get value ranges for each operand. For constant operands, create
a new value range with the operand to simplify processing. */
value_range vr0, vr1;
if (TREE_CODE (op0) == SSA_NAME)
vr0 = *(get_value_range (op0));
else if (is_gimple_min_invariant (op0))
vr0.set (op0);
else
vr0.set_varying (TREE_TYPE (op0));
if (TREE_CODE (op1) == SSA_NAME)
vr1 = *(get_value_range (op1));
else if (is_gimple_min_invariant (op1))
vr1.set (op1);
else
vr1.set_varying (TREE_TYPE (op1));
/* If one argument is varying, we can sometimes still deduce a
range for the output: any + [3, +INF] is in [MIN+3, +INF]. */
if (INTEGRAL_TYPE_P (TREE_TYPE (op0))
&& TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
{
if (vr0.varying_p () && !vr1.varying_p ())
vr0 = value_range (vrp_val_min (expr_type), vrp_val_max (expr_type));
else if (vr1.varying_p () && !vr0.varying_p ())
vr1 = value_range (vrp_val_min (expr_type), vrp_val_max (expr_type));
}
range_fold_binary_expr (vr, code, expr_type, &vr0, &vr1);
/* Set value_range for n in following sequence:
def = __builtin_memchr (arg, 0, sz)
n = def - arg
Here the range for n can be set to [0, PTRDIFF_MAX - 1]. */
if (vr->varying_p ()
&& code == POINTER_DIFF_EXPR
&& TREE_CODE (op0) == SSA_NAME
&& TREE_CODE (op1) == SSA_NAME)
{
tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
gcall *call_stmt = NULL;
if (TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
&& TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
&& TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
&& TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
&& (call_stmt = dyn_cast<gcall *>(SSA_NAME_DEF_STMT (op0)))
&& gimple_call_builtin_p (call_stmt, BUILT_IN_MEMCHR)
&& operand_equal_p (op0, gimple_call_lhs (call_stmt), 0)
&& operand_equal_p (op1, gimple_call_arg (call_stmt, 0), 0)
&& integer_zerop (gimple_call_arg (call_stmt, 1)))
{
tree max = vrp_val_max (ptrdiff_type_node);
wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
tree range_min = build_zero_cst (expr_type);
tree range_max = wide_int_to_tree (expr_type, wmax - 1);
vr->set (range_min, range_max);
return;
}
}
/* Try harder for PLUS and MINUS if the range of one operand is symbolic
and based on the other operand, for example if it was deduced from a
symbolic comparison. When a bound of the range of the first operand
is invariant, we set the corresponding bound of the new range to INF
in order to avoid recursing on the range of the second operand. */
if (vr->varying_p ()
&& (code == PLUS_EXPR || code == MINUS_EXPR)
&& TREE_CODE (op1) == SSA_NAME
&& vr0.kind () == VR_RANGE
&& symbolic_range_based_on_p (&vr0, op1))
{
const bool minus_p = (code == MINUS_EXPR);
value_range n_vr1;
/* Try with VR0 and [-INF, OP1]. */
if (is_gimple_min_invariant (minus_p ? vr0.max () : vr0.min ()))
n_vr1.set (vrp_val_min (expr_type), op1);
/* Try with VR0 and [OP1, +INF]. */
else if (is_gimple_min_invariant (minus_p ? vr0.min () : vr0.max ()))
n_vr1.set (op1, vrp_val_max (expr_type));
/* Try with VR0 and [OP1, OP1]. */
else
n_vr1.set (op1, op1);
range_fold_binary_expr (vr, code, expr_type, &vr0, &n_vr1);
}
if (vr->varying_p ()
&& (code == PLUS_EXPR || code == MINUS_EXPR)
&& TREE_CODE (op0) == SSA_NAME
&& vr1.kind () == VR_RANGE
&& symbolic_range_based_on_p (&vr1, op0))
{
const bool minus_p = (code == MINUS_EXPR);
value_range n_vr0;
/* Try with [-INF, OP0] and VR1. */
if (is_gimple_min_invariant (minus_p ? vr1.max () : vr1.min ()))
n_vr0.set (vrp_val_min (expr_type), op0);
/* Try with [OP0, +INF] and VR1. */
else if (is_gimple_min_invariant (minus_p ? vr1.min (): vr1.max ()))
n_vr0.set (op0, vrp_val_max (expr_type));
/* Try with [OP0, OP0] and VR1. */
else
n_vr0.set (op0);
range_fold_binary_expr (vr, code, expr_type, &n_vr0, &vr1);
}
/* If we didn't derive a range for MINUS_EXPR, and
op1's range is ~[op0,op0] or vice-versa, then we
can derive a non-null range. This happens often for
pointer subtraction. */
if (vr->varying_p ()
&& (code == MINUS_EXPR || code == POINTER_DIFF_EXPR)
&& TREE_CODE (op0) == SSA_NAME
&& ((vr0.kind () == VR_ANTI_RANGE
&& vr0.min () == op1
&& vr0.min () == vr0.max ())
|| (vr1.kind () == VR_ANTI_RANGE
&& vr1.min () == op0
&& vr1.min () == vr1.max ())))
{
vr->set_nonzero (expr_type);
vr->equiv_clear ();
}
}
/* Extract range information from a unary expression CODE OP0 based on
the range of its operand with resulting type TYPE.
The resulting range is stored in *VR. */
void
vr_values::extract_range_from_unary_expr (value_range_equiv *vr,
enum tree_code code,
tree type, tree op0)
{
value_range vr0;
/* Get value ranges for the operand. For constant operands, create
a new value range with the operand to simplify processing. */
if (TREE_CODE (op0) == SSA_NAME)
vr0 = *(get_value_range (op0));
else if (is_gimple_min_invariant (op0))
vr0.set (op0);
else
vr0.set_varying (type);
range_fold_unary_expr (vr, code, type, &vr0, TREE_TYPE (op0));
}
/* Extract range information from a conditional expression STMT based on
the ranges of each of its operands and the expression code. */
void
vr_values::extract_range_from_cond_expr (value_range_equiv *vr, gassign *stmt)
{
/* Get value ranges for each operand. For constant operands, create
a new value range with the operand to simplify processing. */
tree op0 = gimple_assign_rhs2 (stmt);
value_range_equiv tem0;
const value_range_equiv *vr0 = &tem0;
if (TREE_CODE (op0) == SSA_NAME)
vr0 = get_value_range (op0);
else if (is_gimple_min_invariant (op0))
tem0.set (op0);
else
tem0.set_varying (TREE_TYPE (op0));
tree op1 = gimple_assign_rhs3 (stmt);
value_range_equiv tem1;
const value_range_equiv *vr1 = &tem1;
if (TREE_CODE (op1) == SSA_NAME)
vr1 = get_value_range (op1);
else if (is_gimple_min_invariant (op1))
tem1.set (op1);
else
tem1.set_varying (TREE_TYPE (op1));
/* The resulting value range is the union of the operand ranges */
vr->deep_copy (vr0);
vr->union_ (vr1);
}
/* Extract range information from a comparison expression EXPR based
on the range of its operand and the expression code. */
void
vr_values::extract_range_from_comparison (value_range_equiv *vr,
enum tree_code code,
tree type, tree op0, tree op1)
{
bool sop;
tree val;
val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
NULL);
if (val)
{
/* Since this expression was found on the RHS of an assignment,
its type may be different from _Bool. Convert VAL to EXPR's
type. */
val = fold_convert (type, val);
if (is_gimple_min_invariant (val))
vr->set (val);
else
vr->update (val, val);
}
else
/* The result of a comparison is always true or false. */
set_value_range_to_truthvalue (vr, type);
}
/* Helper function for simplify_internal_call_using_ranges and
extract_range_basic. Return true if OP0 SUBCODE OP1 for