OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.2.2/] [gcc/] [tree-vrp.c] - Blame information for rev 455

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 julius
/* Support routines for Value Range Propagation (VRP).
2
   Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
3
   Contributed by Diego Novillo <dnovillo@redhat.com>.
4
 
5
This file is part of GCC.
6
 
7
GCC is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 3, or (at your option)
10
any later version.
11
 
12
GCC is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with GCC; see the file COPYING3.  If not see
19
<http://www.gnu.org/licenses/>.  */
20
 
21
#include "config.h"
22
#include "system.h"
23
#include "coretypes.h"
24
#include "tm.h"
25
#include "ggc.h"
26
#include "flags.h"
27
#include "tree.h"
28
#include "basic-block.h"
29
#include "tree-flow.h"
30
#include "tree-pass.h"
31
#include "tree-dump.h"
32
#include "timevar.h"
33
#include "diagnostic.h"
34
#include "toplev.h"
35
#include "intl.h"
36
#include "cfgloop.h"
37
#include "tree-scalar-evolution.h"
38
#include "tree-ssa-propagate.h"
39
#include "tree-chrec.h"
40
 
41
/* Set of SSA names found during the dominator traversal of a
42
   sub-graph in find_assert_locations.  */
43
static sbitmap found_in_subgraph;
44
 
45
/* Local functions.  */
46
static int compare_values (tree val1, tree val2);
47
static int compare_values_warnv (tree val1, tree val2, bool *);
48
static tree vrp_evaluate_conditional_warnv (tree, bool, bool *);
49
 
50
/* Location information for ASSERT_EXPRs.  Each instance of this
51
   structure describes an ASSERT_EXPR for an SSA name.  Since a single
52
   SSA name may have more than one assertion associated with it, these
53
   locations are kept in a linked list attached to the corresponding
54
   SSA name.  */
55
struct assert_locus_d
56
{
57
  /* Basic block where the assertion would be inserted.  */
58
  basic_block bb;
59
 
60
  /* Some assertions need to be inserted on an edge (e.g., assertions
61
     generated by COND_EXPRs).  In those cases, BB will be NULL.  */
62
  edge e;
63
 
64
  /* Pointer to the statement that generated this assertion.  */
65
  block_stmt_iterator si;
66
 
67
  /* Predicate code for the ASSERT_EXPR.  Must be COMPARISON_CLASS_P.  */
68
  enum tree_code comp_code;
69
 
70
  /* Value being compared against.  */
71
  tree val;
72
 
73
  /* Next node in the linked list.  */
74
  struct assert_locus_d *next;
75
};
76
 
77
typedef struct assert_locus_d *assert_locus_t;
78
 
79
/* If bit I is present, it means that SSA name N_i has a list of
80
   assertions that should be inserted in the IL.  */
81
static bitmap need_assert_for;
82
 
83
/* Array of locations lists where to insert assertions.  ASSERTS_FOR[I]
84
   holds a list of ASSERT_LOCUS_T nodes that describe where
85
   ASSERT_EXPRs for SSA name N_I should be inserted.  */
86
static assert_locus_t *asserts_for;
87
 
88
/* Set of blocks visited in find_assert_locations.  Used to avoid
89
   visiting the same block more than once.  */
90
static sbitmap blocks_visited;
91
 
92
/* Value range array.  After propagation, VR_VALUE[I] holds the range
93
   of values that SSA name N_I may take.  */
94
static value_range_t **vr_value;
95
 
96
 
97
/* Return whether TYPE should use an overflow infinity distinct from
98
   TYPE_{MIN,MAX}_VALUE.  We use an overflow infinity value to
99
   represent a signed overflow during VRP computations.  An infinity
100
   is distinct from a half-range, which will go from some number to
101
   TYPE_{MIN,MAX}_VALUE.  */
102
 
103
static inline bool
104
needs_overflow_infinity (tree type)
105
{
106
  return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
107
}
108
 
109
/* Return whether TYPE can support our overflow infinity
110
   representation: we use the TREE_OVERFLOW flag, which only exists
111
   for constants.  If TYPE doesn't support this, we don't optimize
112
   cases which would require signed overflow--we drop them to
113
   VARYING.  */
114
 
115
static inline bool
116
supports_overflow_infinity (tree type)
117
{
118
#ifdef ENABLE_CHECKING
119
  gcc_assert (needs_overflow_infinity (type));
120
#endif
121
  return (TYPE_MIN_VALUE (type) != NULL_TREE
122
          && CONSTANT_CLASS_P (TYPE_MIN_VALUE (type))
123
          && TYPE_MAX_VALUE (type) != NULL_TREE
124
          && CONSTANT_CLASS_P (TYPE_MAX_VALUE (type)));
125
}
126
 
127
/* VAL is the maximum or minimum value of a type.  Return a
128
   corresponding overflow infinity.  */
129
 
130
static inline tree
131
make_overflow_infinity (tree val)
132
{
133
#ifdef ENABLE_CHECKING
134
  gcc_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
135
#endif
136
  val = copy_node (val);
137
  TREE_OVERFLOW (val) = 1;
138
  return val;
139
}
140
 
141
/* Return a negative overflow infinity for TYPE.  */
142
 
143
static inline tree
144
negative_overflow_infinity (tree type)
145
{
146
#ifdef ENABLE_CHECKING
147
  gcc_assert (supports_overflow_infinity (type));
148
#endif
149
  return make_overflow_infinity (TYPE_MIN_VALUE (type));
150
}
151
 
152
/* Return a positive overflow infinity for TYPE.  */
153
 
154
static inline tree
155
positive_overflow_infinity (tree type)
156
{
157
#ifdef ENABLE_CHECKING
158
  gcc_assert (supports_overflow_infinity (type));
159
#endif
160
  return make_overflow_infinity (TYPE_MAX_VALUE (type));
161
}
162
 
163
/* Return whether VAL is a negative overflow infinity.  */
164
 
165
static inline bool
166
is_negative_overflow_infinity (tree val)
167
{
168
  return (needs_overflow_infinity (TREE_TYPE (val))
169
          && CONSTANT_CLASS_P (val)
170
          && TREE_OVERFLOW (val)
171
          && operand_equal_p (val, TYPE_MIN_VALUE (TREE_TYPE (val)), 0));
172
}
173
 
174
/* Return whether VAL is a positive overflow infinity.  */
175
 
176
static inline bool
177
is_positive_overflow_infinity (tree val)
178
{
179
  return (needs_overflow_infinity (TREE_TYPE (val))
180
          && CONSTANT_CLASS_P (val)
181
          && TREE_OVERFLOW (val)
182
          && operand_equal_p (val, TYPE_MAX_VALUE (TREE_TYPE (val)), 0));
183
}
184
 
185
/* Return whether VAL is a positive or negative overflow infinity.  */
186
 
187
static inline bool
188
is_overflow_infinity (tree val)
189
{
190
  return (needs_overflow_infinity (TREE_TYPE (val))
191
          && CONSTANT_CLASS_P (val)
192
          && TREE_OVERFLOW (val)
193
          && (operand_equal_p (val, TYPE_MAX_VALUE (TREE_TYPE (val)), 0)
194
              || operand_equal_p (val, TYPE_MIN_VALUE (TREE_TYPE (val)), 0)));
195
}
196
 
197
/* If VAL is now an overflow infinity, return VAL.  Otherwise, return
198
   the same value with TREE_OVERFLOW clear.  This can be used to avoid
199
   confusing a regular value with an overflow value.  */
200
 
201
static inline tree
202
avoid_overflow_infinity (tree val)
203
{
204
  if (!is_overflow_infinity (val))
205
    return val;
206
 
207
  if (operand_equal_p (val, TYPE_MAX_VALUE (TREE_TYPE (val)), 0))
208
    return TYPE_MAX_VALUE (TREE_TYPE (val));
209
  else
210
    {
211
#ifdef ENABLE_CHECKING
212
      gcc_assert (operand_equal_p (val, TYPE_MIN_VALUE (TREE_TYPE (val)), 0));
213
#endif
214
      return TYPE_MIN_VALUE (TREE_TYPE (val));
215
    }
216
}
217
 
218
 
219
/* Return whether VAL is equal to the maximum value of its type.  This
220
   will be true for a positive overflow infinity.  We can't do a
221
   simple equality comparison with TYPE_MAX_VALUE because C typedefs
222
   and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
223
   to the integer constant with the same value in the type.  */
224
 
225
static inline bool
226
vrp_val_is_max (tree val)
227
{
228
  tree type_max = TYPE_MAX_VALUE (TREE_TYPE (val));
229
 
230
  return (val == type_max
231
          || (type_max != NULL_TREE
232
              && operand_equal_p (val, type_max, 0)));
233
}
234
 
235
/* Return whether VAL is equal to the minimum value of its type.  This
236
   will be true for a negative overflow infinity.  */
237
 
238
static inline bool
239
vrp_val_is_min (tree val)
240
{
241
  tree type_min = TYPE_MIN_VALUE (TREE_TYPE (val));
242
 
243
  return (val == type_min
244
          || (type_min != NULL_TREE
245
              && operand_equal_p (val, type_min, 0)));
246
}
247
 
248
 
249
/* Return true if ARG is marked with the nonnull attribute in the
250
   current function signature.  */
251
 
252
static bool
253
nonnull_arg_p (tree arg)
254
{
255
  tree t, attrs, fntype;
256
  unsigned HOST_WIDE_INT arg_num;
257
 
258
  gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
259
 
260
  /* The static chain decl is always non null.  */
261
  if (arg == cfun->static_chain_decl)
262
    return true;
263
 
264
  fntype = TREE_TYPE (current_function_decl);
265
  attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (fntype));
266
 
267
  /* If "nonnull" wasn't specified, we know nothing about the argument.  */
268
  if (attrs == NULL_TREE)
269
    return false;
270
 
271
  /* If "nonnull" applies to all the arguments, then ARG is non-null.  */
272
  if (TREE_VALUE (attrs) == NULL_TREE)
273
    return true;
274
 
275
  /* Get the position number for ARG in the function signature.  */
276
  for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
277
       t;
278
       t = TREE_CHAIN (t), arg_num++)
279
    {
280
      if (t == arg)
281
        break;
282
    }
283
 
284
  gcc_assert (t == arg);
285
 
286
  /* Now see if ARG_NUM is mentioned in the nonnull list.  */
287
  for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
288
    {
289
      if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
290
        return true;
291
    }
292
 
293
  return false;
294
}
295
 
296
 
297
/* Set value range VR to {T, MIN, MAX, EQUIV}.  */
298
 
299
static void
300
set_value_range (value_range_t *vr, enum value_range_type t, tree min,
301
                 tree max, bitmap equiv)
302
{
303
#if defined ENABLE_CHECKING
304
  /* Check the validity of the range.  */
305
  if (t == VR_RANGE || t == VR_ANTI_RANGE)
306
    {
307
      int cmp;
308
 
309
      gcc_assert (min && max);
310
 
311
      if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
312
        gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
313
 
314
      cmp = compare_values (min, max);
315
      gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
316
 
317
      if (needs_overflow_infinity (TREE_TYPE (min)))
318
        gcc_assert (!is_overflow_infinity (min)
319
                    || !is_overflow_infinity (max));
320
    }
321
 
322
  if (t == VR_UNDEFINED || t == VR_VARYING)
323
    gcc_assert (min == NULL_TREE && max == NULL_TREE);
324
 
325
  if (t == VR_UNDEFINED || t == VR_VARYING)
326
    gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
327
#endif
328
 
329
  vr->type = t;
330
  vr->min = min;
331
  vr->max = max;
332
 
333
  /* Since updating the equivalence set involves deep copying the
334
     bitmaps, only do it if absolutely necessary.  */
335
  if (vr->equiv == NULL)
336
    vr->equiv = BITMAP_ALLOC (NULL);
337
 
338
  if (equiv != vr->equiv)
339
    {
340
      if (equiv && !bitmap_empty_p (equiv))
341
        bitmap_copy (vr->equiv, equiv);
342
      else
343
        bitmap_clear (vr->equiv);
344
    }
345
}
346
 
347
 
348
/* Copy value range FROM into value range TO.  */
349
 
350
static inline void
351
copy_value_range (value_range_t *to, value_range_t *from)
352
{
353
  set_value_range (to, from->type, from->min, from->max, from->equiv);
354
}
355
 
356
 
357
/* Set value range VR to VR_VARYING.  */
358
 
359
static inline void
360
set_value_range_to_varying (value_range_t *vr)
361
{
362
  vr->type = VR_VARYING;
363
  vr->min = vr->max = NULL_TREE;
364
  if (vr->equiv)
365
    bitmap_clear (vr->equiv);
366
}
367
 
368
/* Set value range VR to a single value.  This function is only called
369
   with values we get from statements, and exists to clear the
370
   TREE_OVERFLOW flag so that we don't think we have an overflow
371
   infinity when we shouldn't.  */
372
 
373
static inline void
374
set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
375
{
376
  gcc_assert (is_gimple_min_invariant (val));
377
  val = avoid_overflow_infinity (val);
378
  set_value_range (vr, VR_RANGE, val, val, equiv);
379
}
380
 
381
/* Set value range VR to a non-negative range of type TYPE.
382
   OVERFLOW_INFINITY indicates whether to use a overflow infinity
383
   rather than TYPE_MAX_VALUE; this should be true if we determine
384
   that the range is nonnegative based on the assumption that signed
385
   overflow does not occur.  */
386
 
387
static inline void
388
set_value_range_to_nonnegative (value_range_t *vr, tree type,
389
                                bool overflow_infinity)
390
{
391
  tree zero;
392
 
393
  if (overflow_infinity && !supports_overflow_infinity (type))
394
    {
395
      set_value_range_to_varying (vr);
396
      return;
397
    }
398
 
399
  zero = build_int_cst (type, 0);
400
  set_value_range (vr, VR_RANGE, zero,
401
                   (overflow_infinity
402
                    ? positive_overflow_infinity (type)
403
                    : TYPE_MAX_VALUE (type)),
404
                   vr->equiv);
405
}
406
 
407
/* Set value range VR to a non-NULL range of type TYPE.  */
408
 
409
static inline void
410
set_value_range_to_nonnull (value_range_t *vr, tree type)
411
{
412
  tree zero = build_int_cst (type, 0);
413
  set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
414
}
415
 
416
 
417
/* Set value range VR to a NULL range of type TYPE.  */
418
 
419
static inline void
420
set_value_range_to_null (value_range_t *vr, tree type)
421
{
422
  set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
423
}
424
 
425
 
426
/* Set value range VR to VR_UNDEFINED.  */
427
 
428
static inline void
429
set_value_range_to_undefined (value_range_t *vr)
430
{
431
  vr->type = VR_UNDEFINED;
432
  vr->min = vr->max = NULL_TREE;
433
  if (vr->equiv)
434
    bitmap_clear (vr->equiv);
435
}
436
 
437
 
438
/* Return value range information for VAR.
439
 
440
   If we have no values ranges recorded (ie, VRP is not running), then
441
   return NULL.  Otherwise create an empty range if none existed for VAR.  */
442
 
443
static value_range_t *
444
get_value_range (tree var)
445
{
446
  value_range_t *vr;
447
  tree sym;
448
  unsigned ver = SSA_NAME_VERSION (var);
449
 
450
  /* If we have no recorded ranges, then return NULL.  */
451
  if (! vr_value)
452
    return NULL;
453
 
454
  vr = vr_value[ver];
455
  if (vr)
456
    return vr;
457
 
458
  /* Create a default value range.  */
459
  vr_value[ver] = vr = XNEW (value_range_t);
460
  memset (vr, 0, sizeof (*vr));
461
 
462
  /* Allocate an equivalence set.  */
463
  vr->equiv = BITMAP_ALLOC (NULL);
464
 
465
  /* If VAR is a default definition, the variable can take any value
466
     in VAR's type.  */
467
  sym = SSA_NAME_VAR (var);
468
  if (var == default_def (sym))
469
    {
470
      /* Try to use the "nonnull" attribute to create ~[0, 0]
471
         anti-ranges for pointers.  Note that this is only valid with
472
         default definitions of PARM_DECLs.  */
473
      if (TREE_CODE (sym) == PARM_DECL
474
          && POINTER_TYPE_P (TREE_TYPE (sym))
475
          && nonnull_arg_p (sym))
476
        set_value_range_to_nonnull (vr, TREE_TYPE (sym));
477
      else
478
        set_value_range_to_varying (vr);
479
    }
480
 
481
  return vr;
482
}
483
 
484
/* Return true, if VAL1 and VAL2 are equal values for VRP purposes.  */
485
 
486
static inline bool
487
vrp_operand_equal_p (tree val1, tree val2)
488
{
489
  if (val1 == val2)
490
    return true;
491
  if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
492
    return false;
493
  if (is_overflow_infinity (val1))
494
    return is_overflow_infinity (val2);
495
  return true;
496
}
497
 
498
/* Return true, if the bitmaps B1 and B2 are equal.  */
499
 
500
static inline bool
501
vrp_bitmap_equal_p (bitmap b1, bitmap b2)
502
{
503
  return (b1 == b2
504
          || (b1 && b2
505
              && bitmap_equal_p (b1, b2)));
506
}
507
 
508
/* Update the value range and equivalence set for variable VAR to
509
   NEW_VR.  Return true if NEW_VR is different from VAR's previous
510
   value.
511
 
512
   NOTE: This function assumes that NEW_VR is a temporary value range
513
   object created for the sole purpose of updating VAR's range.  The
514
   storage used by the equivalence set from NEW_VR will be freed by
515
   this function.  Do not call update_value_range when NEW_VR
516
   is the range object associated with another SSA name.  */
517
 
518
static inline bool
519
update_value_range (tree var, value_range_t *new_vr)
520
{
521
  value_range_t *old_vr;
522
  bool is_new;
523
 
524
  /* Update the value range, if necessary.  */
525
  old_vr = get_value_range (var);
526
  is_new = old_vr->type != new_vr->type
527
           || !vrp_operand_equal_p (old_vr->min, new_vr->min)
528
           || !vrp_operand_equal_p (old_vr->max, new_vr->max)
529
           || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
530
 
531
  if (is_new)
532
    set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
533
                     new_vr->equiv);
534
 
535
  BITMAP_FREE (new_vr->equiv);
536
  new_vr->equiv = NULL;
537
 
538
  return is_new;
539
}
540
 
541
 
542
/* Add VAR and VAR's equivalence set to EQUIV.  */
543
 
544
static void
545
add_equivalence (bitmap equiv, tree var)
546
{
547
  unsigned ver = SSA_NAME_VERSION (var);
548
  value_range_t *vr = vr_value[ver];
549
 
550
  bitmap_set_bit (equiv, ver);
551
  if (vr && vr->equiv)
552
    bitmap_ior_into (equiv, vr->equiv);
553
}
554
 
555
 
556
/* Return true if VR is ~[0, 0].  */
557
 
558
static inline bool
559
range_is_nonnull (value_range_t *vr)
560
{
561
  return vr->type == VR_ANTI_RANGE
562
         && integer_zerop (vr->min)
563
         && integer_zerop (vr->max);
564
}
565
 
566
 
567
/* Return true if VR is [0, 0].  */
568
 
569
static inline bool
570
range_is_null (value_range_t *vr)
571
{
572
  return vr->type == VR_RANGE
573
         && integer_zerop (vr->min)
574
         && integer_zerop (vr->max);
575
}
576
 
577
 
578
/* Return true if value range VR involves at least one symbol.  */
579
 
580
static inline bool
581
symbolic_range_p (value_range_t *vr)
582
{
583
  return (!is_gimple_min_invariant (vr->min)
584
          || !is_gimple_min_invariant (vr->max));
585
}
586
 
587
/* Return true if value range VR uses a overflow infinity.  */
588
 
589
static inline bool
590
overflow_infinity_range_p (value_range_t *vr)
591
{
592
  return (vr->type == VR_RANGE
593
          && (is_overflow_infinity (vr->min)
594
              || is_overflow_infinity (vr->max)));
595
}
596
 
597
/* Return false if we can not make a valid comparison based on VR;
598
   this will be the case if it uses an overflow infinity and overflow
599
   is not undefined (i.e., -fno-strict-overflow is in effect).
600
   Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
601
   uses an overflow infinity.  */
602
 
603
static bool
604
usable_range_p (value_range_t *vr, bool *strict_overflow_p)
605
{
606
  gcc_assert (vr->type == VR_RANGE);
607
  if (is_overflow_infinity (vr->min))
608
    {
609
      *strict_overflow_p = true;
610
      if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
611
        return false;
612
    }
613
  if (is_overflow_infinity (vr->max))
614
    {
615
      *strict_overflow_p = true;
616
      if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
617
        return false;
618
    }
619
  return true;
620
}
621
 
622
 
623
/* Like tree_expr_nonnegative_warnv_p, but this function uses value
624
   ranges obtained so far.  */
625
 
626
static bool
627
vrp_expr_computes_nonnegative (tree expr, bool *strict_overflow_p)
628
{
629
  return tree_expr_nonnegative_warnv_p (expr, strict_overflow_p);
630
}
631
 
632
/* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
633
   obtained so far.  */
634
 
635
static bool
636
vrp_expr_computes_nonzero (tree expr, bool *strict_overflow_p)
637
{
638
  if (tree_expr_nonzero_warnv_p (expr, strict_overflow_p))
639
    return true;
640
 
641
  /* If we have an expression of the form &X->a, then the expression
642
     is nonnull if X is nonnull.  */
643
  if (TREE_CODE (expr) == ADDR_EXPR)
644
    {
645
      tree base = get_base_address (TREE_OPERAND (expr, 0));
646
 
647
      if (base != NULL_TREE
648
          && TREE_CODE (base) == INDIRECT_REF
649
          && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
650
        {
651
          value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
652
          if (range_is_nonnull (vr))
653
            return true;
654
        }
655
    }
656
 
657
  return false;
658
}
659
 
660
/* Returns true if EXPR is a valid value (as expected by compare_values) --
661
   a gimple invariant, or SSA_NAME +- CST.  */
662
 
663
static bool
664
valid_value_p (tree expr)
665
{
666
  if (TREE_CODE (expr) == SSA_NAME)
667
    return true;
668
 
669
  if (TREE_CODE (expr) == PLUS_EXPR
670
      || TREE_CODE (expr) == MINUS_EXPR)
671
    return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
672
            && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
673
 
674
  return is_gimple_min_invariant (expr);
675
}
676
 
677
/* Compare two values VAL1 and VAL2.  Return
678
 
679
        -2 if VAL1 and VAL2 cannot be compared at compile-time,
680
        -1 if VAL1 < VAL2,
681
 
682
        +1 if VAL1 > VAL2, and
683
        +2 if VAL1 != VAL2
684
 
685
   This is similar to tree_int_cst_compare but supports pointer values
686
   and values that cannot be compared at compile time.
687
 
688
   If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
689
   true if the return value is only valid if we assume that signed
690
   overflow is undefined.  */
691
 
692
static int
693
compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
694
{
695
  if (val1 == val2)
696
    return 0;
697
 
698
  /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
699
     both integers.  */
700
  gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
701
              == POINTER_TYPE_P (TREE_TYPE (val2)));
702
 
703
  if ((TREE_CODE (val1) == SSA_NAME
704
       || TREE_CODE (val1) == PLUS_EXPR
705
       || TREE_CODE (val1) == MINUS_EXPR)
706
      && (TREE_CODE (val2) == SSA_NAME
707
          || TREE_CODE (val2) == PLUS_EXPR
708
          || TREE_CODE (val2) == MINUS_EXPR))
709
    {
710
      tree n1, c1, n2, c2;
711
      enum tree_code code1, code2;
712
 
713
      /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
714
         return -1 or +1 accordingly.  If VAL1 and VAL2 don't use the
715
         same name, return -2.  */
716
      if (TREE_CODE (val1) == SSA_NAME)
717
        {
718
          code1 = SSA_NAME;
719
          n1 = val1;
720
          c1 = NULL_TREE;
721
        }
722
      else
723
        {
724
          code1 = TREE_CODE (val1);
725
          n1 = TREE_OPERAND (val1, 0);
726
          c1 = TREE_OPERAND (val1, 1);
727
          if (tree_int_cst_sgn (c1) == -1)
728
            {
729
              if (is_negative_overflow_infinity (c1))
730
                return -2;
731
              c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
732
              if (!c1)
733
                return -2;
734
              code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
735
            }
736
        }
737
 
738
      if (TREE_CODE (val2) == SSA_NAME)
739
        {
740
          code2 = SSA_NAME;
741
          n2 = val2;
742
          c2 = NULL_TREE;
743
        }
744
      else
745
        {
746
          code2 = TREE_CODE (val2);
747
          n2 = TREE_OPERAND (val2, 0);
748
          c2 = TREE_OPERAND (val2, 1);
749
          if (tree_int_cst_sgn (c2) == -1)
750
            {
751
              if (is_negative_overflow_infinity (c2))
752
                return -2;
753
              c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
754
              if (!c2)
755
                return -2;
756
              code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
757
            }
758
        }
759
 
760
      /* Both values must use the same name.  */
761
      if (n1 != n2)
762
        return -2;
763
 
764
      if (code1 == SSA_NAME
765
          && code2 == SSA_NAME)
766
        /* NAME == NAME  */
767
        return 0;
768
 
769
      /* If overflow is defined we cannot simplify more.  */
770
      if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
771
        return -2;
772
 
773
      if (strict_overflow_p != NULL
774
          && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
775
          && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
776
        *strict_overflow_p = true;
777
 
778
      if (code1 == SSA_NAME)
779
        {
780
          if (code2 == PLUS_EXPR)
781
            /* NAME < NAME + CST  */
782
            return -1;
783
          else if (code2 == MINUS_EXPR)
784
            /* NAME > NAME - CST  */
785
            return 1;
786
        }
787
      else if (code1 == PLUS_EXPR)
788
        {
789
          if (code2 == SSA_NAME)
790
            /* NAME + CST > NAME  */
791
            return 1;
792
          else if (code2 == PLUS_EXPR)
793
            /* NAME + CST1 > NAME + CST2, if CST1 > CST2  */
794
            return compare_values_warnv (c1, c2, strict_overflow_p);
795
          else if (code2 == MINUS_EXPR)
796
            /* NAME + CST1 > NAME - CST2  */
797
            return 1;
798
        }
799
      else if (code1 == MINUS_EXPR)
800
        {
801
          if (code2 == SSA_NAME)
802
            /* NAME - CST < NAME  */
803
            return -1;
804
          else if (code2 == PLUS_EXPR)
805
            /* NAME - CST1 < NAME + CST2  */
806
            return -1;
807
          else if (code2 == MINUS_EXPR)
808
            /* NAME - CST1 > NAME - CST2, if CST1 < CST2.  Notice that
809
               C1 and C2 are swapped in the call to compare_values.  */
810
            return compare_values_warnv (c2, c1, strict_overflow_p);
811
        }
812
 
813
      gcc_unreachable ();
814
    }
815
 
816
  /* We cannot compare non-constants.  */
817
  if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
818
    return -2;
819
 
820
  if (!POINTER_TYPE_P (TREE_TYPE (val1)))
821
    {
822
      /* We cannot compare overflowed values, except for overflow
823
         infinities.  */
824
      if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
825
        {
826
          if (strict_overflow_p != NULL)
827
            *strict_overflow_p = true;
828
          if (is_negative_overflow_infinity (val1))
829
            return is_negative_overflow_infinity (val2) ? 0 : -1;
830
          else if (is_negative_overflow_infinity (val2))
831
            return 1;
832
          else if (is_positive_overflow_infinity (val1))
833
            return is_positive_overflow_infinity (val2) ? 0 : 1;
834
          else if (is_positive_overflow_infinity (val2))
835
            return -1;
836
          return -2;
837
        }
838
 
839
      return tree_int_cst_compare (val1, val2);
840
    }
841
  else
842
    {
843
      tree t;
844
 
845
      /* First see if VAL1 and VAL2 are not the same.  */
846
      if (val1 == val2 || operand_equal_p (val1, val2, 0))
847
        return 0;
848
 
849
      /* If VAL1 is a lower address than VAL2, return -1.  */
850
      t = fold_binary (LT_EXPR, boolean_type_node, val1, val2);
851
      if (t == boolean_true_node)
852
        return -1;
853
 
854
      /* If VAL1 is a higher address than VAL2, return +1.  */
855
      t = fold_binary (GT_EXPR, boolean_type_node, val1, val2);
856
      if (t == boolean_true_node)
857
        return 1;
858
 
859
      /* If VAL1 is different than VAL2, return +2.  */
860
      t = fold_binary (NE_EXPR, boolean_type_node, val1, val2);
861
      if (t == boolean_true_node)
862
        return 2;
863
 
864
      return -2;
865
    }
866
}
867
 
868
/* Compare values like compare_values_warnv, but treat comparisons of
869
   nonconstants which rely on undefined overflow as incomparable.  */
870
 
871
static int
872
compare_values (tree val1, tree val2)
873
{
874
  bool sop;
875
  int ret;
876
 
877
  sop = false;
878
  ret = compare_values_warnv (val1, val2, &sop);
879
  if (sop
880
      && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
881
    ret = -2;
882
  return ret;
883
}
884
 
885
 
886
/* Return 1 if VAL is inside value range VR (VR->MIN <= VAL <= VR->MAX),
887
 
888
         -2 if we cannot tell either way.
889
 
890
   FIXME, the current semantics of this functions are a bit quirky
891
          when taken in the context of VRP.  In here we do not care
892
          about VR's type.  If VR is the anti-range ~[3, 5] the call
893
          value_inside_range (4, VR) will return 1.
894
 
895
          This is counter-intuitive in a strict sense, but the callers
896
          currently expect this.  They are calling the function
897
          merely to determine whether VR->MIN <= VAL <= VR->MAX.  The
898
          callers are applying the VR_RANGE/VR_ANTI_RANGE semantics
899
          themselves.
900
 
901
          This also applies to value_ranges_intersect_p and
902
          range_includes_zero_p.  The semantics of VR_RANGE and
903
          VR_ANTI_RANGE should be encoded here, but that also means
904
          adapting the users of these functions to the new semantics.  */
905
 
906
static inline int
907
value_inside_range (tree val, value_range_t *vr)
908
{
909
  tree cmp1, cmp2;
910
 
911
  fold_defer_overflow_warnings ();
912
 
913
  cmp1 = fold_binary_to_constant (GE_EXPR, boolean_type_node, val, vr->min);
914
  if (!cmp1)
915
  {
916
    fold_undefer_and_ignore_overflow_warnings ();
917
    return -2;
918
  }
919
 
920
  cmp2 = fold_binary_to_constant (LE_EXPR, boolean_type_node, val, vr->max);
921
 
922
  fold_undefer_and_ignore_overflow_warnings ();
923
 
924
  if (!cmp2)
925
    return -2;
926
 
927
  return cmp1 == boolean_true_node && cmp2 == boolean_true_node;
928
}
929
 
930
 
931
/* Return true if value ranges VR0 and VR1 have a non-empty
932
   intersection.  */
933
 
934
static inline bool
935
value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
936
{
937
  return (value_inside_range (vr1->min, vr0) == 1
938
          || value_inside_range (vr1->max, vr0) == 1
939
          || value_inside_range (vr0->min, vr1) == 1
940
          || value_inside_range (vr0->max, vr1) == 1);
941
}
942
 
943
 
944
/* Return true if VR includes the value zero, false otherwise.  FIXME,
945
   currently this will return false for an anti-range like ~[-4, 3].
946
   This will be wrong when the semantics of value_inside_range are
947
   modified (currently the users of this function expect these
948
   semantics).  */
949
 
950
static inline bool
951
range_includes_zero_p (value_range_t *vr)
952
{
953
  tree zero;
954
 
955
  gcc_assert (vr->type != VR_UNDEFINED
956
              && vr->type != VR_VARYING
957
              && !symbolic_range_p (vr));
958
 
959
  zero = build_int_cst (TREE_TYPE (vr->min), 0);
960
  return (value_inside_range (zero, vr) == 1);
961
}
962
 
963
/* Return true if T, an SSA_NAME, is known to be nonnegative.  Return
964
   false otherwise or if no value range information is available.  */
965
 
966
bool
967
ssa_name_nonnegative_p (tree t)
968
{
969
  value_range_t *vr = get_value_range (t);
970
 
971
  if (!vr)
972
    return false;
973
 
974
  /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
975
     which would return a useful value should be encoded as a VR_RANGE.  */
976
  if (vr->type == VR_RANGE)
977
    {
978
      int result = compare_values (vr->min, integer_zero_node);
979
 
980
      return (result == 0 || result == 1);
981
    }
982
  return false;
983
}
984
 
985
/* Return true if T, an SSA_NAME, is known to be nonzero.  Return
986
   false otherwise or if no value range information is available.  */
987
 
988
bool
989
ssa_name_nonzero_p (tree t)
990
{
991
  value_range_t *vr = get_value_range (t);
992
 
993
  if (!vr)
994
    return false;
995
 
996
  /* A VR_RANGE which does not include zero is a nonzero value.  */
997
  if (vr->type == VR_RANGE && !symbolic_range_p (vr))
998
    return ! range_includes_zero_p (vr);
999
 
1000
  /* A VR_ANTI_RANGE which does include zero is a nonzero value.  */
1001
  if (vr->type == VR_ANTI_RANGE && !symbolic_range_p (vr))
1002
    return range_includes_zero_p (vr);
1003
 
1004
  return false;
1005
}
1006
 
1007
 
1008
/* Extract value range information from an ASSERT_EXPR EXPR and store
1009
   it in *VR_P.  */
1010
 
1011
static void
1012
extract_range_from_assert (value_range_t *vr_p, tree expr)
1013
{
1014
  tree var, cond, limit, min, max, type;
1015
  value_range_t *var_vr, *limit_vr;
1016
  enum tree_code cond_code;
1017
 
1018
  var = ASSERT_EXPR_VAR (expr);
1019
  cond = ASSERT_EXPR_COND (expr);
1020
 
1021
  gcc_assert (COMPARISON_CLASS_P (cond));
1022
 
1023
  /* Find VAR in the ASSERT_EXPR conditional.  */
1024
  if (var == TREE_OPERAND (cond, 0))
1025
    {
1026
      /* If the predicate is of the form VAR COMP LIMIT, then we just
1027
         take LIMIT from the RHS and use the same comparison code.  */
1028
      limit = TREE_OPERAND (cond, 1);
1029
      cond_code = TREE_CODE (cond);
1030
    }
1031
  else
1032
    {
1033
      /* If the predicate is of the form LIMIT COMP VAR, then we need
1034
         to flip around the comparison code to create the proper range
1035
         for VAR.  */
1036
      limit = TREE_OPERAND (cond, 0);
1037
      cond_code = swap_tree_comparison (TREE_CODE (cond));
1038
    }
1039
 
1040
  limit = avoid_overflow_infinity (limit);
1041
 
1042
  type = TREE_TYPE (limit);
1043
  gcc_assert (limit != var);
1044
 
1045
  /* For pointer arithmetic, we only keep track of pointer equality
1046
     and inequality.  */
1047
  if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1048
    {
1049
      set_value_range_to_varying (vr_p);
1050
      return;
1051
    }
1052
 
1053
  /* If LIMIT is another SSA name and LIMIT has a range of its own,
1054
     try to use LIMIT's range to avoid creating symbolic ranges
1055
     unnecessarily. */
1056
  limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1057
 
1058
  /* LIMIT's range is only interesting if it has any useful information.  */
1059
  if (limit_vr
1060
      && (limit_vr->type == VR_UNDEFINED
1061
          || limit_vr->type == VR_VARYING
1062
          || symbolic_range_p (limit_vr)))
1063
    limit_vr = NULL;
1064
 
1065
  /* Initially, the new range has the same set of equivalences of
1066
     VAR's range.  This will be revised before returning the final
1067
     value.  Since assertions may be chained via mutually exclusive
1068
     predicates, we will need to trim the set of equivalences before
1069
     we are done.  */
1070
  gcc_assert (vr_p->equiv == NULL);
1071
  vr_p->equiv = BITMAP_ALLOC (NULL);
1072
  add_equivalence (vr_p->equiv, var);
1073
 
1074
  /* Extract a new range based on the asserted comparison for VAR and
1075
     LIMIT's value range.  Notice that if LIMIT has an anti-range, we
1076
     will only use it for equality comparisons (EQ_EXPR).  For any
1077
     other kind of assertion, we cannot derive a range from LIMIT's
1078
     anti-range that can be used to describe the new range.  For
1079
     instance, ASSERT_EXPR <x_2, x_2 <= b_4>.  If b_4 is ~[2, 10],
1080
     then b_4 takes on the ranges [-INF, 1] and [11, +INF].  There is
1081
     no single range for x_2 that could describe LE_EXPR, so we might
1082
     as well build the range [b_4, +INF] for it.  */
1083
  if (cond_code == EQ_EXPR)
1084
    {
1085
      enum value_range_type range_type;
1086
 
1087
      if (limit_vr)
1088
        {
1089
          range_type = limit_vr->type;
1090
          min = limit_vr->min;
1091
          max = limit_vr->max;
1092
        }
1093
      else
1094
        {
1095
          range_type = VR_RANGE;
1096
          min = limit;
1097
          max = limit;
1098
        }
1099
 
1100
      set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1101
 
1102
      /* When asserting the equality VAR == LIMIT and LIMIT is another
1103
         SSA name, the new range will also inherit the equivalence set
1104
         from LIMIT.  */
1105
      if (TREE_CODE (limit) == SSA_NAME)
1106
        add_equivalence (vr_p->equiv, limit);
1107
    }
1108
  else if (cond_code == NE_EXPR)
1109
    {
1110
      /* As described above, when LIMIT's range is an anti-range and
1111
         this assertion is an inequality (NE_EXPR), then we cannot
1112
         derive anything from the anti-range.  For instance, if
1113
         LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1114
         not imply that VAR's range is [0, 0].  So, in the case of
1115
         anti-ranges, we just assert the inequality using LIMIT and
1116
         not its anti-range.
1117
 
1118
         If LIMIT_VR is a range, we can only use it to build a new
1119
         anti-range if LIMIT_VR is a single-valued range.  For
1120
         instance, if LIMIT_VR is [0, 1], the predicate
1121
         VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1122
         Rather, it means that for value 0 VAR should be ~[0, 0]
1123
         and for value 1, VAR should be ~[1, 1].  We cannot
1124
         represent these ranges.
1125
 
1126
         The only situation in which we can build a valid
1127
         anti-range is when LIMIT_VR is a single-valued range
1128
         (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX).  In that case,
1129
         build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX].  */
1130
      if (limit_vr
1131
          && limit_vr->type == VR_RANGE
1132
          && compare_values (limit_vr->min, limit_vr->max) == 0)
1133
        {
1134
          min = limit_vr->min;
1135
          max = limit_vr->max;
1136
        }
1137
      else
1138
        {
1139
          /* In any other case, we cannot use LIMIT's range to build a
1140
             valid anti-range.  */
1141
          min = max = limit;
1142
        }
1143
 
1144
      /* If MIN and MAX cover the whole range for their type, then
1145
         just use the original LIMIT.  */
1146
      if (INTEGRAL_TYPE_P (type)
1147
          && vrp_val_is_min (min)
1148
          && vrp_val_is_max (max))
1149
        min = max = limit;
1150
 
1151
      set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
1152
    }
1153
  else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1154
    {
1155
      min = TYPE_MIN_VALUE (type);
1156
 
1157
      if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1158
        max = limit;
1159
      else
1160
        {
1161
          /* If LIMIT_VR is of the form [N1, N2], we need to build the
1162
             range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1163
             LT_EXPR.  */
1164
          max = limit_vr->max;
1165
        }
1166
 
1167
      /* If the maximum value forces us to be out of bounds, simply punt.
1168
         It would be pointless to try and do anything more since this
1169
         all should be optimized away above us.  */
1170
      if ((cond_code == LT_EXPR
1171
           && compare_values (max, min) == 0)
1172
          || is_overflow_infinity (max))
1173
        set_value_range_to_varying (vr_p);
1174
      else
1175
        {
1176
          /* For LT_EXPR, we create the range [MIN, MAX - 1].  */
1177
          if (cond_code == LT_EXPR)
1178
            {
1179
              tree one = build_int_cst (type, 1);
1180
              max = fold_build2 (MINUS_EXPR, type, max, one);
1181
              if (EXPR_P (max))
1182
                TREE_NO_WARNING (max) = 1;
1183
            }
1184
 
1185
          set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1186
        }
1187
    }
1188
  else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1189
    {
1190
      max = TYPE_MAX_VALUE (type);
1191
 
1192
      if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1193
        min = limit;
1194
      else
1195
        {
1196
          /* If LIMIT_VR is of the form [N1, N2], we need to build the
1197
             range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1198
             GT_EXPR.  */
1199
          min = limit_vr->min;
1200
        }
1201
 
1202
      /* If the minimum value forces us to be out of bounds, simply punt.
1203
         It would be pointless to try and do anything more since this
1204
         all should be optimized away above us.  */
1205
      if ((cond_code == GT_EXPR
1206
           && compare_values (min, max) == 0)
1207
          || is_overflow_infinity (min))
1208
        set_value_range_to_varying (vr_p);
1209
      else
1210
        {
1211
          /* For GT_EXPR, we create the range [MIN + 1, MAX].  */
1212
          if (cond_code == GT_EXPR)
1213
            {
1214
              tree one = build_int_cst (type, 1);
1215
              min = fold_build2 (PLUS_EXPR, type, min, one);
1216
              if (EXPR_P (min))
1217
                TREE_NO_WARNING (min) = 1;
1218
            }
1219
 
1220
          set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1221
        }
1222
    }
1223
  else
1224
    gcc_unreachable ();
1225
 
1226
  /* If VAR already had a known range, it may happen that the new
1227
     range we have computed and VAR's range are not compatible.  For
1228
     instance,
1229
 
1230
        if (p_5 == NULL)
1231
          p_6 = ASSERT_EXPR <p_5, p_5 == NULL>;
1232
          x_7 = p_6->fld;
1233
          p_8 = ASSERT_EXPR <p_6, p_6 != NULL>;
1234
 
1235
     While the above comes from a faulty program, it will cause an ICE
1236
     later because p_8 and p_6 will have incompatible ranges and at
1237
     the same time will be considered equivalent.  A similar situation
1238
     would arise from
1239
 
1240
        if (i_5 > 10)
1241
          i_6 = ASSERT_EXPR <i_5, i_5 > 10>;
1242
          if (i_5 < 5)
1243
            i_7 = ASSERT_EXPR <i_6, i_6 < 5>;
1244
 
1245
     Again i_6 and i_7 will have incompatible ranges.  It would be
1246
     pointless to try and do anything with i_7's range because
1247
     anything dominated by 'if (i_5 < 5)' will be optimized away.
1248
     Note, due to the wa in which simulation proceeds, the statement
1249
     i_7 = ASSERT_EXPR <...> we would never be visited because the
1250
     conditional 'if (i_5 < 5)' always evaluates to false.  However,
1251
     this extra check does not hurt and may protect against future
1252
     changes to VRP that may get into a situation similar to the
1253
     NULL pointer dereference example.
1254
 
1255
     Note that these compatibility tests are only needed when dealing
1256
     with ranges or a mix of range and anti-range.  If VAR_VR and VR_P
1257
     are both anti-ranges, they will always be compatible, because two
1258
     anti-ranges will always have a non-empty intersection.  */
1259
 
1260
  var_vr = get_value_range (var);
1261
 
1262
  /* We may need to make adjustments when VR_P and VAR_VR are numeric
1263
     ranges or anti-ranges.  */
1264
  if (vr_p->type == VR_VARYING
1265
      || vr_p->type == VR_UNDEFINED
1266
      || var_vr->type == VR_VARYING
1267
      || var_vr->type == VR_UNDEFINED
1268
      || symbolic_range_p (vr_p)
1269
      || symbolic_range_p (var_vr))
1270
    return;
1271
 
1272
  if (var_vr->type == VR_RANGE && vr_p->type == VR_RANGE)
1273
    {
1274
      /* If the two ranges have a non-empty intersection, we can
1275
         refine the resulting range.  Since the assert expression
1276
         creates an equivalency and at the same time it asserts a
1277
         predicate, we can take the intersection of the two ranges to
1278
         get better precision.  */
1279
      if (value_ranges_intersect_p (var_vr, vr_p))
1280
        {
1281
          /* Use the larger of the two minimums.  */
1282
          if (compare_values (vr_p->min, var_vr->min) == -1)
1283
            min = var_vr->min;
1284
          else
1285
            min = vr_p->min;
1286
 
1287
          /* Use the smaller of the two maximums.  */
1288
          if (compare_values (vr_p->max, var_vr->max) == 1)
1289
            max = var_vr->max;
1290
          else
1291
            max = vr_p->max;
1292
 
1293
          set_value_range (vr_p, vr_p->type, min, max, vr_p->equiv);
1294
        }
1295
      else
1296
        {
1297
          /* The two ranges do not intersect, set the new range to
1298
             VARYING, because we will not be able to do anything
1299
             meaningful with it.  */
1300
          set_value_range_to_varying (vr_p);
1301
        }
1302
    }
1303
  else if ((var_vr->type == VR_RANGE && vr_p->type == VR_ANTI_RANGE)
1304
           || (var_vr->type == VR_ANTI_RANGE && vr_p->type == VR_RANGE))
1305
    {
1306
      /* A range and an anti-range will cancel each other only if
1307
         their ends are the same.  For instance, in the example above,
1308
         p_8's range ~[0, 0] and p_6's range [0, 0] are incompatible,
1309
         so VR_P should be set to VR_VARYING.  */
1310
      if (compare_values (var_vr->min, vr_p->min) == 0
1311
          && compare_values (var_vr->max, vr_p->max) == 0)
1312
        set_value_range_to_varying (vr_p);
1313
      else
1314
        {
1315
          tree min, max, anti_min, anti_max, real_min, real_max;
1316
 
1317
          /* We want to compute the logical AND of the two ranges;
1318
             there are three cases to consider.
1319
 
1320
 
1321
             1. The VR_ANTI_RANGE range is completely within the
1322
                VR_RANGE and the endpoints of the ranges are
1323
                different.  In that case the resulting range
1324
                should be whichever range is more precise.
1325
                Typically that will be the VR_RANGE.
1326
 
1327
             2. The VR_ANTI_RANGE is completely disjoint from
1328
                the VR_RANGE.  In this case the resulting range
1329
                should be the VR_RANGE.
1330
 
1331
             3. There is some overlap between the VR_ANTI_RANGE
1332
                and the VR_RANGE.
1333
 
1334
                3a. If the high limit of the VR_ANTI_RANGE resides
1335
                    within the VR_RANGE, then the result is a new
1336
                    VR_RANGE starting at the high limit of the
1337
                    the VR_ANTI_RANGE + 1 and extending to the
1338
                    high limit of the original VR_RANGE.
1339
 
1340
                3b. If the low limit of the VR_ANTI_RANGE resides
1341
                    within the VR_RANGE, then the result is a new
1342
                    VR_RANGE starting at the low limit of the original
1343
                    VR_RANGE and extending to the low limit of the
1344
                    VR_ANTI_RANGE - 1.  */
1345
          if (vr_p->type == VR_ANTI_RANGE)
1346
            {
1347
              anti_min = vr_p->min;
1348
              anti_max = vr_p->max;
1349
              real_min = var_vr->min;
1350
              real_max = var_vr->max;
1351
            }
1352
          else
1353
            {
1354
              anti_min = var_vr->min;
1355
              anti_max = var_vr->max;
1356
              real_min = vr_p->min;
1357
              real_max = vr_p->max;
1358
            }
1359
 
1360
 
1361
          /* Case 1, VR_ANTI_RANGE completely within VR_RANGE,
1362
             not including any endpoints.  */
1363
          if (compare_values (anti_max, real_max) == -1
1364
              && compare_values (anti_min, real_min) == 1)
1365
            {
1366
              set_value_range (vr_p, VR_RANGE, real_min,
1367
                               real_max, vr_p->equiv);
1368
            }
1369
          /* Case 2, VR_ANTI_RANGE completely disjoint from
1370
             VR_RANGE.  */
1371
          else if (compare_values (anti_min, real_max) == 1
1372
                   || compare_values (anti_max, real_min) == -1)
1373
            {
1374
              set_value_range (vr_p, VR_RANGE, real_min,
1375
                               real_max, vr_p->equiv);
1376
            }
1377
          /* Case 3a, the anti-range extends into the low
1378
             part of the real range.  Thus creating a new
1379
             low for the real range.  */
1380
          else if ((compare_values (anti_max, real_min) == 1
1381
                    || compare_values (anti_max, real_min) == 0)
1382
                   && compare_values (anti_max, real_max) == -1)
1383
            {
1384
              gcc_assert (!is_positive_overflow_infinity (anti_max));
1385
              if (needs_overflow_infinity (TREE_TYPE (anti_max))
1386
                  && vrp_val_is_max (anti_max))
1387
                {
1388
                  if (!supports_overflow_infinity (TREE_TYPE (var_vr->min)))
1389
                    {
1390
                      set_value_range_to_varying (vr_p);
1391
                      return;
1392
                    }
1393
                  min = positive_overflow_infinity (TREE_TYPE (var_vr->min));
1394
                }
1395
              else
1396
                min = fold_build2 (PLUS_EXPR, TREE_TYPE (var_vr->min),
1397
                                   anti_max,
1398
                                   build_int_cst (TREE_TYPE (var_vr->min), 1));
1399
              max = real_max;
1400
              set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1401
            }
1402
          /* Case 3b, the anti-range extends into the high
1403
             part of the real range.  Thus creating a new
1404
             higher for the real range.  */
1405
          else if (compare_values (anti_min, real_min) == 1
1406
                   && (compare_values (anti_min, real_max) == -1
1407
                       || compare_values (anti_min, real_max) == 0))
1408
            {
1409
              gcc_assert (!is_negative_overflow_infinity (anti_min));
1410
              if (needs_overflow_infinity (TREE_TYPE (anti_min))
1411
                  && vrp_val_is_min (anti_min))
1412
                {
1413
                  if (!supports_overflow_infinity (TREE_TYPE (var_vr->min)))
1414
                    {
1415
                      set_value_range_to_varying (vr_p);
1416
                      return;
1417
                    }
1418
                  max = negative_overflow_infinity (TREE_TYPE (var_vr->min));
1419
                }
1420
              else
1421
                max = fold_build2 (MINUS_EXPR, TREE_TYPE (var_vr->min),
1422
                                   anti_min,
1423
                                   build_int_cst (TREE_TYPE (var_vr->min), 1));
1424
              min = real_min;
1425
              set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1426
            }
1427
        }
1428
    }
1429
}
1430
 
1431
 
1432
/* Extract range information from SSA name VAR and store it in VR.  If
1433
   VAR has an interesting range, use it.  Otherwise, create the
1434
   range [VAR, VAR] and return it.  This is useful in situations where
1435
   we may have conditionals testing values of VARYING names.  For
1436
   instance,
1437
 
1438
        x_3 = y_5;
1439
        if (x_3 > y_5)
1440
          ...
1441
 
1442
    Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1443
    always false.  */
1444
 
1445
static void
1446
extract_range_from_ssa_name (value_range_t *vr, tree var)
1447
{
1448
  value_range_t *var_vr = get_value_range (var);
1449
 
1450
  if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1451
    copy_value_range (vr, var_vr);
1452
  else
1453
    set_value_range (vr, VR_RANGE, var, var, NULL);
1454
 
1455
  add_equivalence (vr->equiv, var);
1456
}
1457
 
1458
 
1459
/* Wrapper around int_const_binop.  If the operation overflows and we
1460
   are not using wrapping arithmetic, then adjust the result to be
1461
   -INF or +INF depending on CODE, VAL1 and VAL2.  This can return
1462
   NULL_TREE if we need to use an overflow infinity representation but
1463
   the type does not support it.  */
1464
 
1465
static tree
1466
vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1467
{
1468
  tree res;
1469
 
1470
  res = int_const_binop (code, val1, val2, 0);
1471
 
1472
  /* If we are not using wrapping arithmetic, operate symbolically
1473
     on -INF and +INF.  */
1474
  if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1475
    {
1476
      int checkz = compare_values (res, val1);
1477
      bool overflow = false;
1478
 
1479
      /* Ensure that res = val1 [+*] val2 >= val1
1480
         or that res = val1 - val2 <= val1.  */
1481
      if ((code == PLUS_EXPR
1482
           && !(checkz == 1 || checkz == 0))
1483
          || (code == MINUS_EXPR
1484
              && !(checkz == 0 || checkz == -1)))
1485
        {
1486
          overflow = true;
1487
        }
1488
      /* Checking for multiplication overflow is done by dividing the
1489
         output of the multiplication by the first input of the
1490
         multiplication.  If the result of that division operation is
1491
         not equal to the second input of the multiplication, then the
1492
         multiplication overflowed.  */
1493
      else if (code == MULT_EXPR && !integer_zerop (val1))
1494
        {
1495
          tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1496
                                      res,
1497
                                      val1, 0);
1498
          int check = compare_values (tmp, val2);
1499
 
1500
          if (check != 0)
1501
            overflow = true;
1502
        }
1503
 
1504
      if (overflow)
1505
        {
1506
          res = copy_node (res);
1507
          TREE_OVERFLOW (res) = 1;
1508
        }
1509
 
1510
    }
1511
  else if ((TREE_OVERFLOW (res)
1512
            && !TREE_OVERFLOW (val1)
1513
            && !TREE_OVERFLOW (val2))
1514
           || is_overflow_infinity (val1)
1515
           || is_overflow_infinity (val2))
1516
    {
1517
      /* If the operation overflowed but neither VAL1 nor VAL2 are
1518
         overflown, return -INF or +INF depending on the operation
1519
         and the combination of signs of the operands.  */
1520
      int sgn1 = tree_int_cst_sgn (val1);
1521
      int sgn2 = tree_int_cst_sgn (val2);
1522
 
1523
      if (needs_overflow_infinity (TREE_TYPE (res))
1524
          && !supports_overflow_infinity (TREE_TYPE (res)))
1525
        return NULL_TREE;
1526
 
1527
      /* We have to punt on adding infinities of different signs,
1528
         since we can't tell what the sign of the result should be.
1529
         Likewise for subtracting infinities of the same sign.  */
1530
      if (((code == PLUS_EXPR && sgn1 != sgn2)
1531
           || (code == MINUS_EXPR && sgn1 == sgn2))
1532
          && is_overflow_infinity (val1)
1533
          && is_overflow_infinity (val2))
1534
        return NULL_TREE;
1535
 
1536
      /* Don't try to handle division or shifting of infinities.  */
1537
      if ((code == TRUNC_DIV_EXPR
1538
           || code == FLOOR_DIV_EXPR
1539
           || code == CEIL_DIV_EXPR
1540
           || code == EXACT_DIV_EXPR
1541
           || code == ROUND_DIV_EXPR
1542
           || code == RSHIFT_EXPR)
1543
          && (is_overflow_infinity (val1)
1544
              || is_overflow_infinity (val2)))
1545
        return NULL_TREE;
1546
 
1547
      /* Notice that we only need to handle the restricted set of
1548
         operations handled by extract_range_from_binary_expr.
1549
         Among them, only multiplication, addition and subtraction
1550
         can yield overflow without overflown operands because we
1551
         are working with integral types only... except in the
1552
         case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1553
         for division too.  */
1554
 
1555
      /* For multiplication, the sign of the overflow is given
1556
         by the comparison of the signs of the operands.  */
1557
      if ((code == MULT_EXPR && sgn1 == sgn2)
1558
          /* For addition, the operands must be of the same sign
1559
             to yield an overflow.  Its sign is therefore that
1560
             of one of the operands, for example the first.  For
1561
             infinite operands X + -INF is negative, not positive.  */
1562
          || (code == PLUS_EXPR
1563
              && (sgn1 >= 0
1564
                  ? !is_negative_overflow_infinity (val2)
1565
                  : is_positive_overflow_infinity (val2)))
1566
          /* For subtraction, non-infinite operands must be of
1567
             different signs to yield an overflow.  Its sign is
1568
             therefore that of the first operand or the opposite of
1569
             that of the second operand.  A first operand of 0 counts
1570
             as positive here, for the corner case 0 - (-INF), which
1571
             overflows, but must yield +INF.  For infinite operands 0
1572
             - INF is negative, not positive.  */
1573
          || (code == MINUS_EXPR
1574
              && (sgn1 >= 0
1575
                  ? !is_positive_overflow_infinity (val2)
1576
                  : is_negative_overflow_infinity (val2)))
1577
          /* For division, the only case is -INF / -1 = +INF.  */
1578
          || code == TRUNC_DIV_EXPR
1579
          || code == FLOOR_DIV_EXPR
1580
          || code == CEIL_DIV_EXPR
1581
          || code == EXACT_DIV_EXPR
1582
          || code == ROUND_DIV_EXPR)
1583
        return (needs_overflow_infinity (TREE_TYPE (res))
1584
                ? positive_overflow_infinity (TREE_TYPE (res))
1585
                : TYPE_MAX_VALUE (TREE_TYPE (res)));
1586
      else
1587
        return (needs_overflow_infinity (TREE_TYPE (res))
1588
                ? negative_overflow_infinity (TREE_TYPE (res))
1589
                : TYPE_MIN_VALUE (TREE_TYPE (res)));
1590
    }
1591
 
1592
  return res;
1593
}
1594
 
1595
 
1596
/* Extract range information from a binary expression EXPR based on
1597
   the ranges of each of its operands and the expression code.  */
1598
 
1599
static void
1600
extract_range_from_binary_expr (value_range_t *vr, tree expr)
1601
{
1602
  enum tree_code code = TREE_CODE (expr);
1603
  enum value_range_type type;
1604
  tree op0, op1, min, max;
1605
  int cmp;
1606
  value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1607
  value_range_t vr1 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1608
 
1609
  /* Not all binary expressions can be applied to ranges in a
1610
     meaningful way.  Handle only arithmetic operations.  */
1611
  if (code != PLUS_EXPR
1612
      && code != MINUS_EXPR
1613
      && code != MULT_EXPR
1614
      && code != TRUNC_DIV_EXPR
1615
      && code != FLOOR_DIV_EXPR
1616
      && code != CEIL_DIV_EXPR
1617
      && code != EXACT_DIV_EXPR
1618
      && code != ROUND_DIV_EXPR
1619
      && code != MIN_EXPR
1620
      && code != MAX_EXPR
1621
      && code != BIT_AND_EXPR
1622
      && code != TRUTH_ANDIF_EXPR
1623
      && code != TRUTH_ORIF_EXPR
1624
      && code != TRUTH_AND_EXPR
1625
      && code != TRUTH_OR_EXPR)
1626
    {
1627
      set_value_range_to_varying (vr);
1628
      return;
1629
    }
1630
 
1631
  /* Get value ranges for each operand.  For constant operands, create
1632
     a new value range with the operand to simplify processing.  */
1633
  op0 = TREE_OPERAND (expr, 0);
1634
  if (TREE_CODE (op0) == SSA_NAME)
1635
    vr0 = *(get_value_range (op0));
1636
  else if (is_gimple_min_invariant (op0))
1637
    set_value_range_to_value (&vr0, op0, NULL);
1638
  else
1639
    set_value_range_to_varying (&vr0);
1640
 
1641
  op1 = TREE_OPERAND (expr, 1);
1642
  if (TREE_CODE (op1) == SSA_NAME)
1643
    vr1 = *(get_value_range (op1));
1644
  else if (is_gimple_min_invariant (op1))
1645
    set_value_range_to_value (&vr1, op1, NULL);
1646
  else
1647
    set_value_range_to_varying (&vr1);
1648
 
1649
  /* If either range is UNDEFINED, so is the result.  */
1650
  if (vr0.type == VR_UNDEFINED || vr1.type == VR_UNDEFINED)
1651
    {
1652
      set_value_range_to_undefined (vr);
1653
      return;
1654
    }
1655
 
1656
  /* The type of the resulting value range defaults to VR0.TYPE.  */
1657
  type = vr0.type;
1658
 
1659
  /* Refuse to operate on VARYING ranges, ranges of different kinds
1660
     and symbolic ranges.  As an exception, we allow BIT_AND_EXPR
1661
     because we may be able to derive a useful range even if one of
1662
     the operands is VR_VARYING or symbolic range.  TODO, we may be
1663
     able to derive anti-ranges in some cases.  */
1664
  if (code != BIT_AND_EXPR
1665
      && code != TRUTH_AND_EXPR
1666
      && code != TRUTH_OR_EXPR
1667
      && (vr0.type == VR_VARYING
1668
          || vr1.type == VR_VARYING
1669
          || vr0.type != vr1.type
1670
          || symbolic_range_p (&vr0)
1671
          || symbolic_range_p (&vr1)))
1672
    {
1673
      set_value_range_to_varying (vr);
1674
      return;
1675
    }
1676
 
1677
  /* Now evaluate the expression to determine the new range.  */
1678
  if (POINTER_TYPE_P (TREE_TYPE (expr))
1679
      || POINTER_TYPE_P (TREE_TYPE (op0))
1680
      || POINTER_TYPE_P (TREE_TYPE (op1)))
1681
    {
1682
      /* For pointer types, we are really only interested in asserting
1683
         whether the expression evaluates to non-NULL.  FIXME, we used
1684
         to gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR), but
1685
         ivopts is generating expressions with pointer multiplication
1686
         in them.  */
1687
      if (code == PLUS_EXPR)
1688
        {
1689
          if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
1690
            set_value_range_to_nonnull (vr, TREE_TYPE (expr));
1691
          else if (range_is_null (&vr0) && range_is_null (&vr1))
1692
            set_value_range_to_null (vr, TREE_TYPE (expr));
1693
          else
1694
            set_value_range_to_varying (vr);
1695
        }
1696
      else
1697
        {
1698
          /* Subtracting from a pointer, may yield 0, so just drop the
1699
             resulting range to varying.  */
1700
          set_value_range_to_varying (vr);
1701
        }
1702
 
1703
      return;
1704
    }
1705
 
1706
  /* For integer ranges, apply the operation to each end of the
1707
     range and see what we end up with.  */
1708
  if (code == TRUTH_ANDIF_EXPR
1709
      || code == TRUTH_ORIF_EXPR
1710
      || code == TRUTH_AND_EXPR
1711
      || code == TRUTH_OR_EXPR)
1712
    {
1713
      /* If one of the operands is zero, we know that the whole
1714
         expression evaluates zero.  */
1715
      if (code == TRUTH_AND_EXPR
1716
          && ((vr0.type == VR_RANGE
1717
               && integer_zerop (vr0.min)
1718
               && integer_zerop (vr0.max))
1719
              || (vr1.type == VR_RANGE
1720
                  && integer_zerop (vr1.min)
1721
                  && integer_zerop (vr1.max))))
1722
        {
1723
          type = VR_RANGE;
1724
          min = max = build_int_cst (TREE_TYPE (expr), 0);
1725
        }
1726
      /* If one of the operands is one, we know that the whole
1727
         expression evaluates one.  */
1728
      else if (code == TRUTH_OR_EXPR
1729
               && ((vr0.type == VR_RANGE
1730
                    && integer_onep (vr0.min)
1731
                    && integer_onep (vr0.max))
1732
                   || (vr1.type == VR_RANGE
1733
                       && integer_onep (vr1.min)
1734
                       && integer_onep (vr1.max))))
1735
        {
1736
          type = VR_RANGE;
1737
          min = max = build_int_cst (TREE_TYPE (expr), 1);
1738
        }
1739
      else if (vr0.type != VR_VARYING
1740
               && vr1.type != VR_VARYING
1741
               && vr0.type == vr1.type
1742
               && !symbolic_range_p (&vr0)
1743
               && !overflow_infinity_range_p (&vr0)
1744
               && !symbolic_range_p (&vr1)
1745
               && !overflow_infinity_range_p (&vr1))
1746
        {
1747
          /* Boolean expressions cannot be folded with int_const_binop.  */
1748
          min = fold_binary (code, TREE_TYPE (expr), vr0.min, vr1.min);
1749
          max = fold_binary (code, TREE_TYPE (expr), vr0.max, vr1.max);
1750
        }
1751
      else
1752
        {
1753
          set_value_range_to_varying (vr);
1754
          return;
1755
        }
1756
    }
1757
  else if (code == PLUS_EXPR
1758
           || code == MIN_EXPR
1759
           || code == MAX_EXPR)
1760
    {
1761
      /* If we have a PLUS_EXPR with two VR_ANTI_RANGEs, drop to
1762
         VR_VARYING.  It would take more effort to compute a precise
1763
         range for such a case.  For example, if we have op0 == 1 and
1764
         op1 == -1 with their ranges both being ~[0,0], we would have
1765
         op0 + op1 == 0, so we cannot claim that the sum is in ~[0,0].
1766
         Note that we are guaranteed to have vr0.type == vr1.type at
1767
         this point.  */
1768
      if (code == PLUS_EXPR && vr0.type == VR_ANTI_RANGE)
1769
        {
1770
          set_value_range_to_varying (vr);
1771
          return;
1772
        }
1773
 
1774
      /* For operations that make the resulting range directly
1775
         proportional to the original ranges, apply the operation to
1776
         the same end of each range.  */
1777
      min = vrp_int_const_binop (code, vr0.min, vr1.min);
1778
      max = vrp_int_const_binop (code, vr0.max, vr1.max);
1779
    }
1780
  else if (code == MULT_EXPR
1781
           || code == TRUNC_DIV_EXPR
1782
           || code == FLOOR_DIV_EXPR
1783
           || code == CEIL_DIV_EXPR
1784
           || code == EXACT_DIV_EXPR
1785
           || code == ROUND_DIV_EXPR)
1786
    {
1787
      tree val[4];
1788
      size_t i;
1789
      bool sop;
1790
 
1791
      /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
1792
         drop to VR_VARYING.  It would take more effort to compute a
1793
         precise range for such a case.  For example, if we have
1794
         op0 == 65536 and op1 == 65536 with their ranges both being
1795
         ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
1796
         we cannot claim that the product is in ~[0,0].  Note that we
1797
         are guaranteed to have vr0.type == vr1.type at this
1798
         point.  */
1799
      if (code == MULT_EXPR
1800
          && vr0.type == VR_ANTI_RANGE
1801
          && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
1802
        {
1803
          set_value_range_to_varying (vr);
1804
          return;
1805
        }
1806
 
1807
      /* Multiplications and divisions are a bit tricky to handle,
1808
         depending on the mix of signs we have in the two ranges, we
1809
         need to operate on different values to get the minimum and
1810
         maximum values for the new range.  One approach is to figure
1811
         out all the variations of range combinations and do the
1812
         operations.
1813
 
1814
         However, this involves several calls to compare_values and it
1815
         is pretty convoluted.  It's simpler to do the 4 operations
1816
         (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1817
         MAX1) and then figure the smallest and largest values to form
1818
         the new range.  */
1819
 
1820
      /* Divisions by zero result in a VARYING value.  */
1821
      if (code != MULT_EXPR
1822
          && (vr0.type == VR_ANTI_RANGE || range_includes_zero_p (&vr1)))
1823
        {
1824
          set_value_range_to_varying (vr);
1825
          return;
1826
        }
1827
 
1828
      /* Compute the 4 cross operations.  */
1829
      sop = false;
1830
      val[0] = vrp_int_const_binop (code, vr0.min, vr1.min);
1831
      if (val[0] == NULL_TREE)
1832
        sop = true;
1833
 
1834
      if (vr1.max == vr1.min)
1835
        val[1] = NULL_TREE;
1836
      else
1837
        {
1838
          val[1] = vrp_int_const_binop (code, vr0.min, vr1.max);
1839
          if (val[1] == NULL_TREE)
1840
            sop = true;
1841
        }
1842
 
1843
      if (vr0.max == vr0.min)
1844
        val[2] = NULL_TREE;
1845
      else
1846
        {
1847
          val[2] = vrp_int_const_binop (code, vr0.max, vr1.min);
1848
          if (val[2] == NULL_TREE)
1849
            sop = true;
1850
        }
1851
 
1852
      if (vr0.min == vr0.max || vr1.min == vr1.max)
1853
        val[3] = NULL_TREE;
1854
      else
1855
        {
1856
          val[3] = vrp_int_const_binop (code, vr0.max, vr1.max);
1857
          if (val[3] == NULL_TREE)
1858
            sop = true;
1859
        }
1860
 
1861
      if (sop)
1862
        {
1863
          set_value_range_to_varying (vr);
1864
          return;
1865
        }
1866
 
1867
      /* Set MIN to the minimum of VAL[i] and MAX to the maximum
1868
         of VAL[i].  */
1869
      min = val[0];
1870
      max = val[0];
1871
      for (i = 1; i < 4; i++)
1872
        {
1873
          if (!is_gimple_min_invariant (min)
1874
              || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
1875
              || !is_gimple_min_invariant (max)
1876
              || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
1877
            break;
1878
 
1879
          if (val[i])
1880
            {
1881
              if (!is_gimple_min_invariant (val[i])
1882
                  || (TREE_OVERFLOW (val[i])
1883
                      && !is_overflow_infinity (val[i])))
1884
                {
1885
                  /* If we found an overflowed value, set MIN and MAX
1886
                     to it so that we set the resulting range to
1887
                     VARYING.  */
1888
                  min = max = val[i];
1889
                  break;
1890
                }
1891
 
1892
              if (compare_values (val[i], min) == -1)
1893
                min = val[i];
1894
 
1895
              if (compare_values (val[i], max) == 1)
1896
                max = val[i];
1897
            }
1898
        }
1899
    }
1900
  else if (code == MINUS_EXPR)
1901
    {
1902
      /* If we have a MINUS_EXPR with two VR_ANTI_RANGEs, drop to
1903
         VR_VARYING.  It would take more effort to compute a precise
1904
         range for such a case.  For example, if we have op0 == 1 and
1905
         op1 == 1 with their ranges both being ~[0,0], we would have
1906
         op0 - op1 == 0, so we cannot claim that the difference is in
1907
         ~[0,0].  Note that we are guaranteed to have
1908
         vr0.type == vr1.type at this point.  */
1909
      if (vr0.type == VR_ANTI_RANGE)
1910
        {
1911
          set_value_range_to_varying (vr);
1912
          return;
1913
        }
1914
 
1915
      /* For MINUS_EXPR, apply the operation to the opposite ends of
1916
         each range.  */
1917
      min = vrp_int_const_binop (code, vr0.min, vr1.max);
1918
      max = vrp_int_const_binop (code, vr0.max, vr1.min);
1919
    }
1920
  else if (code == BIT_AND_EXPR)
1921
    {
1922
      if (vr0.type == VR_RANGE
1923
          && vr0.min == vr0.max
1924
          && TREE_CODE (vr0.max) == INTEGER_CST
1925
          && !TREE_OVERFLOW (vr0.max)
1926
          && tree_int_cst_sgn (vr0.max) >= 0)
1927
        {
1928
          min = build_int_cst (TREE_TYPE (expr), 0);
1929
          max = vr0.max;
1930
        }
1931
      else if (vr1.type == VR_RANGE
1932
               && vr1.min == vr1.max
1933
               && TREE_CODE (vr1.max) == INTEGER_CST
1934
               && !TREE_OVERFLOW (vr1.max)
1935
               && tree_int_cst_sgn (vr1.max) >= 0)
1936
        {
1937
          type = VR_RANGE;
1938
          min = build_int_cst (TREE_TYPE (expr), 0);
1939
          max = vr1.max;
1940
        }
1941
      else
1942
        {
1943
          set_value_range_to_varying (vr);
1944
          return;
1945
        }
1946
    }
1947
  else
1948
    gcc_unreachable ();
1949
 
1950
  /* If either MIN or MAX overflowed, then set the resulting range to
1951
     VARYING.  But we do accept an overflow infinity
1952
     representation.  */
1953
  if (min == NULL_TREE
1954
      || !is_gimple_min_invariant (min)
1955
      || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
1956
      || max == NULL_TREE
1957
      || !is_gimple_min_invariant (max)
1958
      || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
1959
    {
1960
      set_value_range_to_varying (vr);
1961
      return;
1962
    }
1963
 
1964
  /* We punt if:
1965
     1) [-INF, +INF]
1966
     2) [-INF, +-INF(OVF)]
1967
     3) [+-INF(OVF), +INF]
1968
     4) [+-INF(OVF), +-INF(OVF)]
1969
     We learn nothing when we have INF and INF(OVF) on both sides.
1970
     Note that we do accept [-INF, -INF] and [+INF, +INF] without
1971
     overflow.  */
1972
  if ((vrp_val_is_min (min) || is_overflow_infinity (min))
1973
      && (vrp_val_is_max (max) || is_overflow_infinity (max)))
1974
    {
1975
      set_value_range_to_varying (vr);
1976
      return;
1977
    }
1978
 
1979
  cmp = compare_values (min, max);
1980
  if (cmp == -2 || cmp == 1)
1981
    {
1982
      /* If the new range has its limits swapped around (MIN > MAX),
1983
         then the operation caused one of them to wrap around, mark
1984
         the new range VARYING.  */
1985
      set_value_range_to_varying (vr);
1986
    }
1987
  else
1988
    set_value_range (vr, type, min, max, NULL);
1989
}
1990
 
1991
 
1992
/* Extract range information from a unary expression EXPR based on
1993
   the range of its operand and the expression code.  */
1994
 
1995
static void
1996
extract_range_from_unary_expr (value_range_t *vr, tree expr)
1997
{
1998
  enum tree_code code = TREE_CODE (expr);
1999
  tree min, max, op0;
2000
  int cmp;
2001
  value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
2002
 
2003
  /* Refuse to operate on certain unary expressions for which we
2004
     cannot easily determine a resulting range.  */
2005
  if (code == FIX_TRUNC_EXPR
2006
      || code == FIX_CEIL_EXPR
2007
      || code == FIX_FLOOR_EXPR
2008
      || code == FIX_ROUND_EXPR
2009
      || code == FLOAT_EXPR
2010
      || code == BIT_NOT_EXPR
2011
      || code == NON_LVALUE_EXPR
2012
      || code == CONJ_EXPR)
2013
    {
2014
      set_value_range_to_varying (vr);
2015
      return;
2016
    }
2017
 
2018
  /* Get value ranges for the operand.  For constant operands, create
2019
     a new value range with the operand to simplify processing.  */
2020
  op0 = TREE_OPERAND (expr, 0);
2021
  if (TREE_CODE (op0) == SSA_NAME)
2022
    vr0 = *(get_value_range (op0));
2023
  else if (is_gimple_min_invariant (op0))
2024
    set_value_range_to_value (&vr0, op0, NULL);
2025
  else
2026
    set_value_range_to_varying (&vr0);
2027
 
2028
  /* If VR0 is UNDEFINED, so is the result.  */
2029
  if (vr0.type == VR_UNDEFINED)
2030
    {
2031
      set_value_range_to_undefined (vr);
2032
      return;
2033
    }
2034
 
2035
  /* Refuse to operate on symbolic ranges, or if neither operand is
2036
     a pointer or integral type.  */
2037
  if ((!INTEGRAL_TYPE_P (TREE_TYPE (op0))
2038
       && !POINTER_TYPE_P (TREE_TYPE (op0)))
2039
      || (vr0.type != VR_VARYING
2040
          && symbolic_range_p (&vr0)))
2041
    {
2042
      set_value_range_to_varying (vr);
2043
      return;
2044
    }
2045
 
2046
  /* If the expression involves pointers, we are only interested in
2047
     determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]).  */
2048
  if (POINTER_TYPE_P (TREE_TYPE (expr)) || POINTER_TYPE_P (TREE_TYPE (op0)))
2049
    {
2050
      bool sop;
2051
 
2052
      sop = false;
2053
      if (range_is_nonnull (&vr0)
2054
          || (tree_expr_nonzero_warnv_p (expr, &sop)
2055
              && !sop))
2056
        set_value_range_to_nonnull (vr, TREE_TYPE (expr));
2057
      else if (range_is_null (&vr0))
2058
        set_value_range_to_null (vr, TREE_TYPE (expr));
2059
      else
2060
        set_value_range_to_varying (vr);
2061
 
2062
      return;
2063
    }
2064
 
2065
  /* Handle unary expressions on integer ranges.  */
2066
  if (code == NOP_EXPR || code == CONVERT_EXPR)
2067
    {
2068
      tree inner_type = TREE_TYPE (op0);
2069
      tree outer_type = TREE_TYPE (expr);
2070
 
2071
      /* If VR0 represents a simple range, then try to convert
2072
         the min and max values for the range to the same type
2073
         as OUTER_TYPE.  If the results compare equal to VR0's
2074
         min and max values and the new min is still less than
2075
         or equal to the new max, then we can safely use the newly
2076
         computed range for EXPR.  This allows us to compute
2077
         accurate ranges through many casts.  */
2078
      if ((vr0.type == VR_RANGE
2079
           && !overflow_infinity_range_p (&vr0))
2080
          || (vr0.type == VR_VARYING
2081
              && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)))
2082
        {
2083
          tree new_min, new_max, orig_min, orig_max;
2084
 
2085
          /* Convert the input operand min/max to OUTER_TYPE.   If
2086
             the input has no range information, then use the min/max
2087
             for the input's type.  */
2088
          if (vr0.type == VR_RANGE)
2089
            {
2090
              orig_min = vr0.min;
2091
              orig_max = vr0.max;
2092
            }
2093
          else
2094
            {
2095
              orig_min = TYPE_MIN_VALUE (inner_type);
2096
              orig_max = TYPE_MAX_VALUE (inner_type);
2097
            }
2098
 
2099
          new_min = fold_convert (outer_type, orig_min);
2100
          new_max = fold_convert (outer_type, orig_max);
2101
 
2102
          /* Verify the new min/max values are gimple values and
2103
             that they compare equal to the original input's
2104
             min/max values.  */
2105
          if (is_gimple_val (new_min)
2106
              && is_gimple_val (new_max)
2107
              && tree_int_cst_equal (new_min, orig_min)
2108
              && tree_int_cst_equal (new_max, orig_max)
2109
              && (!is_overflow_infinity (new_min)
2110
                  || !is_overflow_infinity (new_max))
2111
              && compare_values (new_min, new_max) <= 0
2112
              && compare_values (new_min, new_max) >= -1)
2113
            {
2114
              set_value_range (vr, VR_RANGE, new_min, new_max, vr->equiv);
2115
              return;
2116
            }
2117
        }
2118
 
2119
      /* When converting types of different sizes, set the result to
2120
         VARYING.  Things like sign extensions and precision loss may
2121
         change the range.  For instance, if x_3 is of type 'long long
2122
         int' and 'y_5 = (unsigned short) x_3', if x_3 is ~[0, 0], it
2123
         is impossible to know at compile time whether y_5 will be
2124
         ~[0, 0].  */
2125
      if (TYPE_SIZE (inner_type) != TYPE_SIZE (outer_type)
2126
          || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
2127
        {
2128
          set_value_range_to_varying (vr);
2129
          return;
2130
        }
2131
    }
2132
 
2133
  /* Conversion of a VR_VARYING value to a wider type can result
2134
     in a usable range.  So wait until after we've handled conversions
2135
     before dropping the result to VR_VARYING if we had a source
2136
     operand that is VR_VARYING.  */
2137
  if (vr0.type == VR_VARYING)
2138
    {
2139
      set_value_range_to_varying (vr);
2140
      return;
2141
    }
2142
 
2143
  /* Apply the operation to each end of the range and see what we end
2144
     up with.  */
2145
  if (code == NEGATE_EXPR
2146
      && !TYPE_UNSIGNED (TREE_TYPE (expr)))
2147
    {
2148
      /* NEGATE_EXPR flips the range around.  We need to treat
2149
         TYPE_MIN_VALUE specially.  */
2150
      if (is_positive_overflow_infinity (vr0.max))
2151
        min = negative_overflow_infinity (TREE_TYPE (expr));
2152
      else if (is_negative_overflow_infinity (vr0.max))
2153
        min = positive_overflow_infinity (TREE_TYPE (expr));
2154
      else if (!vrp_val_is_min (vr0.max))
2155
        min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2156
      else if (needs_overflow_infinity (TREE_TYPE (expr)))
2157
        {
2158
          if (supports_overflow_infinity (TREE_TYPE (expr))
2159
              && !is_overflow_infinity (vr0.min)
2160
              && !vrp_val_is_min (vr0.min))
2161
            min = positive_overflow_infinity (TREE_TYPE (expr));
2162
          else
2163
            {
2164
              set_value_range_to_varying (vr);
2165
              return;
2166
            }
2167
        }
2168
      else
2169
        min = TYPE_MIN_VALUE (TREE_TYPE (expr));
2170
 
2171
      if (is_positive_overflow_infinity (vr0.min))
2172
        max = negative_overflow_infinity (TREE_TYPE (expr));
2173
      else if (is_negative_overflow_infinity (vr0.min))
2174
        max = positive_overflow_infinity (TREE_TYPE (expr));
2175
      else if (!vrp_val_is_min (vr0.min))
2176
        max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2177
      else if (needs_overflow_infinity (TREE_TYPE (expr)))
2178
        {
2179
          if (supports_overflow_infinity (TREE_TYPE (expr)))
2180
            max = positive_overflow_infinity (TREE_TYPE (expr));
2181
          else
2182
            {
2183
              set_value_range_to_varying (vr);
2184
              return;
2185
            }
2186
        }
2187
      else
2188
        max = TYPE_MIN_VALUE (TREE_TYPE (expr));
2189
    }
2190
  else if (code == NEGATE_EXPR
2191
           && TYPE_UNSIGNED (TREE_TYPE (expr)))
2192
    {
2193
      if (!range_includes_zero_p (&vr0))
2194
        {
2195
          max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2196
          min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2197
        }
2198
      else
2199
        {
2200
          if (range_is_null (&vr0))
2201
            set_value_range_to_null (vr, TREE_TYPE (expr));
2202
          else
2203
            set_value_range_to_varying (vr);
2204
          return;
2205
        }
2206
    }
2207
  else if (code == ABS_EXPR
2208
           && !TYPE_UNSIGNED (TREE_TYPE (expr)))
2209
    {
2210
      /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
2211
         useful range.  */
2212
      if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (expr))
2213
          && ((vr0.type == VR_RANGE
2214
               && vrp_val_is_min (vr0.min))
2215
              || (vr0.type == VR_ANTI_RANGE
2216
                  && !vrp_val_is_min (vr0.min)
2217
                  && !range_includes_zero_p (&vr0))))
2218
        {
2219
          set_value_range_to_varying (vr);
2220
          return;
2221
        }
2222
 
2223
      /* ABS_EXPR may flip the range around, if the original range
2224
         included negative values.  */
2225
      if (is_overflow_infinity (vr0.min))
2226
        min = positive_overflow_infinity (TREE_TYPE (expr));
2227
      else if (!vrp_val_is_min (vr0.min))
2228
        min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2229
      else if (!needs_overflow_infinity (TREE_TYPE (expr)))
2230
        min = TYPE_MAX_VALUE (TREE_TYPE (expr));
2231
      else if (supports_overflow_infinity (TREE_TYPE (expr)))
2232
        min = positive_overflow_infinity (TREE_TYPE (expr));
2233
      else
2234
        {
2235
          set_value_range_to_varying (vr);
2236
          return;
2237
        }
2238
 
2239
      if (is_overflow_infinity (vr0.max))
2240
        max = positive_overflow_infinity (TREE_TYPE (expr));
2241
      else if (!vrp_val_is_min (vr0.max))
2242
        max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2243
      else if (!needs_overflow_infinity (TREE_TYPE (expr)))
2244
        max = TYPE_MAX_VALUE (TREE_TYPE (expr));
2245
      else if (supports_overflow_infinity (TREE_TYPE (expr)))
2246
        max = positive_overflow_infinity (TREE_TYPE (expr));
2247
      else
2248
        {
2249
          set_value_range_to_varying (vr);
2250
          return;
2251
        }
2252
 
2253
      cmp = compare_values (min, max);
2254
 
2255
      /* If a VR_ANTI_RANGEs contains zero, then we have
2256
         ~[-INF, min(MIN, MAX)].  */
2257
      if (vr0.type == VR_ANTI_RANGE)
2258
        {
2259
          if (range_includes_zero_p (&vr0))
2260
            {
2261
              /* Take the lower of the two values.  */
2262
              if (cmp != 1)
2263
                max = min;
2264
 
2265
              /* Create ~[-INF, min (abs(MIN), abs(MAX))]
2266
                 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
2267
                 flag_wrapv is set and the original anti-range doesn't include
2268
                 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE.  */
2269
              if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
2270
                {
2271
                  tree type_min_value = TYPE_MIN_VALUE (TREE_TYPE (expr));
2272
 
2273
                  min = (vr0.min != type_min_value
2274
                         ? int_const_binop (PLUS_EXPR, type_min_value,
2275
                                            integer_one_node, 0)
2276
                         : type_min_value);
2277
                }
2278
              else
2279
                {
2280
                  if (overflow_infinity_range_p (&vr0))
2281
                    min = negative_overflow_infinity (TREE_TYPE (expr));
2282
                  else
2283
                    min = TYPE_MIN_VALUE (TREE_TYPE (expr));
2284
                }
2285
            }
2286
          else
2287
            {
2288
              /* All else has failed, so create the range [0, INF], even for
2289
                 flag_wrapv since TYPE_MIN_VALUE is in the original
2290
                 anti-range.  */
2291
              vr0.type = VR_RANGE;
2292
              min = build_int_cst (TREE_TYPE (expr), 0);
2293
              if (needs_overflow_infinity (TREE_TYPE (expr)))
2294
                {
2295
                  if (supports_overflow_infinity (TREE_TYPE (expr)))
2296
                    max = positive_overflow_infinity (TREE_TYPE (expr));
2297
                  else
2298
                    {
2299
                      set_value_range_to_varying (vr);
2300
                      return;
2301
                    }
2302
                }
2303
              else
2304
                max = TYPE_MAX_VALUE (TREE_TYPE (expr));
2305
            }
2306
        }
2307
 
2308
      /* If the range contains zero then we know that the minimum value in the
2309
         range will be zero.  */
2310
      else if (range_includes_zero_p (&vr0))
2311
        {
2312
          if (cmp == 1)
2313
            max = min;
2314
          min = build_int_cst (TREE_TYPE (expr), 0);
2315
        }
2316
      else
2317
        {
2318
          /* If the range was reversed, swap MIN and MAX.  */
2319
          if (cmp == 1)
2320
            {
2321
              tree t = min;
2322
              min = max;
2323
              max = t;
2324
            }
2325
        }
2326
    }
2327
  else
2328
    {
2329
      /* Otherwise, operate on each end of the range.  */
2330
      min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2331
      max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2332
 
2333
      if (needs_overflow_infinity (TREE_TYPE (expr)))
2334
        {
2335
          gcc_assert (code != NEGATE_EXPR && code != ABS_EXPR);
2336
 
2337
          /* If both sides have overflowed, we don't know
2338
             anything.  */
2339
          if ((is_overflow_infinity (vr0.min)
2340
               || TREE_OVERFLOW (min))
2341
              && (is_overflow_infinity (vr0.max)
2342
                  || TREE_OVERFLOW (max)))
2343
            {
2344
              set_value_range_to_varying (vr);
2345
              return;
2346
            }
2347
 
2348
          if (is_overflow_infinity (vr0.min))
2349
            min = vr0.min;
2350
          else if (TREE_OVERFLOW (min))
2351
            {
2352
              if (supports_overflow_infinity (TREE_TYPE (expr)))
2353
                min = (tree_int_cst_sgn (min) >= 0
2354
                       ? positive_overflow_infinity (TREE_TYPE (min))
2355
                       : negative_overflow_infinity (TREE_TYPE (min)));
2356
              else
2357
                {
2358
                  set_value_range_to_varying (vr);
2359
                  return;
2360
                }
2361
            }
2362
 
2363
          if (is_overflow_infinity (vr0.max))
2364
            max = vr0.max;
2365
          else if (TREE_OVERFLOW (max))
2366
            {
2367
              if (supports_overflow_infinity (TREE_TYPE (expr)))
2368
                max = (tree_int_cst_sgn (max) >= 0
2369
                       ? positive_overflow_infinity (TREE_TYPE (max))
2370
                       : negative_overflow_infinity (TREE_TYPE (max)));
2371
              else
2372
                {
2373
                  set_value_range_to_varying (vr);
2374
                  return;
2375
                }
2376
            }
2377
        }
2378
    }
2379
 
2380
  cmp = compare_values (min, max);
2381
  if (cmp == -2 || cmp == 1)
2382
    {
2383
      /* If the new range has its limits swapped around (MIN > MAX),
2384
         then the operation caused one of them to wrap around, mark
2385
         the new range VARYING.  */
2386
      set_value_range_to_varying (vr);
2387
    }
2388
  else
2389
    set_value_range (vr, vr0.type, min, max, NULL);
2390
}
2391
 
2392
 
2393
/* Extract range information from a comparison expression EXPR based
2394
   on the range of its operand and the expression code.  */
2395
 
2396
static void
2397
extract_range_from_comparison (value_range_t *vr, tree expr)
2398
{
2399
  bool sop = false;
2400
  tree val = vrp_evaluate_conditional_warnv (expr, false, &sop);
2401
 
2402
  /* A disadvantage of using a special infinity as an overflow
2403
     representation is that we lose the ability to record overflow
2404
     when we don't have an infinity.  So we have to ignore a result
2405
     which relies on overflow.  */
2406
 
2407
  if (val && !is_overflow_infinity (val) && !sop)
2408
    {
2409
      /* Since this expression was found on the RHS of an assignment,
2410
         its type may be different from _Bool.  Convert VAL to EXPR's
2411
         type.  */
2412
      val = fold_convert (TREE_TYPE (expr), val);
2413
      if (is_gimple_min_invariant (val))
2414
        set_value_range_to_value (vr, val, vr->equiv);
2415
      else
2416
        set_value_range (vr, VR_RANGE, val, val, vr->equiv);
2417
    }
2418
  else
2419
    set_value_range_to_varying (vr);
2420
}
2421
 
2422
 
2423
/* Try to compute a useful range out of expression EXPR and store it
2424
   in *VR.  */
2425
 
2426
static void
2427
extract_range_from_expr (value_range_t *vr, tree expr)
2428
{
2429
  enum tree_code code = TREE_CODE (expr);
2430
 
2431
  if (code == ASSERT_EXPR)
2432
    extract_range_from_assert (vr, expr);
2433
  else if (code == SSA_NAME)
2434
    extract_range_from_ssa_name (vr, expr);
2435
  else if (TREE_CODE_CLASS (code) == tcc_binary
2436
           || code == TRUTH_ANDIF_EXPR
2437
           || code == TRUTH_ORIF_EXPR
2438
           || code == TRUTH_AND_EXPR
2439
           || code == TRUTH_OR_EXPR
2440
           || code == TRUTH_XOR_EXPR)
2441
    extract_range_from_binary_expr (vr, expr);
2442
  else if (TREE_CODE_CLASS (code) == tcc_unary)
2443
    extract_range_from_unary_expr (vr, expr);
2444
  else if (TREE_CODE_CLASS (code) == tcc_comparison)
2445
    extract_range_from_comparison (vr, expr);
2446
  else if (is_gimple_min_invariant (expr))
2447
    set_value_range_to_value (vr, expr, NULL);
2448
  else
2449
    set_value_range_to_varying (vr);
2450
 
2451
  /* If we got a varying range from the tests above, try a final
2452
     time to derive a nonnegative or nonzero range.  This time
2453
     relying primarily on generic routines in fold in conjunction
2454
     with range data.  */
2455
  if (vr->type == VR_VARYING)
2456
    {
2457
      bool sop = false;
2458
 
2459
      if (INTEGRAL_TYPE_P (TREE_TYPE (expr))
2460
          && vrp_expr_computes_nonnegative (expr, &sop))
2461
        set_value_range_to_nonnegative (vr, TREE_TYPE (expr),
2462
                                        sop || is_overflow_infinity (expr));
2463
      else if (vrp_expr_computes_nonzero (expr, &sop)
2464
               && !sop)
2465
        set_value_range_to_nonnull (vr, TREE_TYPE (expr));
2466
    }
2467
}
2468
 
2469
/* Given a range VR, a LOOP and a variable VAR, determine whether it
2470
   would be profitable to adjust VR using scalar evolution information
2471
   for VAR.  If so, update VR with the new limits.  */
2472
 
2473
static void
2474
adjust_range_with_scev (value_range_t *vr, struct loop *loop, tree stmt,
2475
                        tree var)
2476
{
2477
  tree init, step, chrec, tmin, tmax, min, max, type;
2478
  enum ev_direction dir;
2479
 
2480
  /* TODO.  Don't adjust anti-ranges.  An anti-range may provide
2481
     better opportunities than a regular range, but I'm not sure.  */
2482
  if (vr->type == VR_ANTI_RANGE)
2483
    return;
2484
 
2485
  chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
2486
  if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
2487
    return;
2488
 
2489
  init = initial_condition_in_loop_num (chrec, loop->num);
2490
  step = evolution_part_in_loop_num (chrec, loop->num);
2491
 
2492
  /* If STEP is symbolic, we can't know whether INIT will be the
2493
     minimum or maximum value in the range.  Also, unless INIT is
2494
     a simple expression, compare_values and possibly other functions
2495
     in tree-vrp won't be able to handle it.  */
2496
  if (step == NULL_TREE
2497
      || !is_gimple_min_invariant (step)
2498
      || !valid_value_p (init))
2499
    return;
2500
 
2501
  dir = scev_direction (chrec);
2502
  if (/* Do not adjust ranges if we do not know whether the iv increases
2503
         or decreases,  ... */
2504
      dir == EV_DIR_UNKNOWN
2505
      /* ... or if it may wrap.  */
2506
      || scev_probably_wraps_p (init, step, stmt,
2507
                                current_loops->parray[CHREC_VARIABLE (chrec)],
2508
                                true))
2509
    return;
2510
 
2511
  /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
2512
     negative_overflow_infinity and positive_overflow_infinity,
2513
     because we have concluded that the loop probably does not
2514
     wrap.  */
2515
 
2516
  type = TREE_TYPE (var);
2517
  if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
2518
    tmin = lower_bound_in_type (type, type);
2519
  else
2520
    tmin = TYPE_MIN_VALUE (type);
2521
  if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
2522
    tmax = upper_bound_in_type (type, type);
2523
  else
2524
    tmax = TYPE_MAX_VALUE (type);
2525
 
2526
  if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
2527
    {
2528
      min = tmin;
2529
      max = tmax;
2530
 
2531
      /* For VARYING or UNDEFINED ranges, just about anything we get
2532
         from scalar evolutions should be better.  */
2533
 
2534
      if (dir == EV_DIR_DECREASES)
2535
        max = init;
2536
      else
2537
        min = init;
2538
 
2539
      /* If we would create an invalid range, then just assume we
2540
         know absolutely nothing.  This may be over-conservative,
2541
         but it's clearly safe, and should happen only in unreachable
2542
         parts of code, or for invalid programs.  */
2543
      if (compare_values (min, max) == 1)
2544
        return;
2545
 
2546
      set_value_range (vr, VR_RANGE, min, max, vr->equiv);
2547
    }
2548
  else if (vr->type == VR_RANGE)
2549
    {
2550
      min = vr->min;
2551
      max = vr->max;
2552
 
2553
      if (dir == EV_DIR_DECREASES)
2554
        {
2555
          /* INIT is the maximum value.  If INIT is lower than VR->MAX
2556
             but no smaller than VR->MIN, set VR->MAX to INIT.  */
2557
          if (compare_values (init, max) == -1)
2558
            {
2559
              max = init;
2560
 
2561
              /* If we just created an invalid range with the minimum
2562
                 greater than the maximum, we fail conservatively.
2563
                 This should happen only in unreachable
2564
                 parts of code, or for invalid programs.  */
2565
              if (compare_values (min, max) == 1)
2566
                return;
2567
            }
2568
 
2569
          /* According to the loop information, the variable does not
2570
             overflow.  If we think it does, probably because of an
2571
             overflow due to arithmetic on a different INF value,
2572
             reset now.  */
2573
          if (is_negative_overflow_infinity (min))
2574
            min = tmin;
2575
        }
2576
      else
2577
        {
2578
          /* If INIT is bigger than VR->MIN, set VR->MIN to INIT.  */
2579
          if (compare_values (init, min) == 1)
2580
            {
2581
              min = init;
2582
 
2583
              /* Again, avoid creating invalid range by failing.  */
2584
              if (compare_values (min, max) == 1)
2585
                return;
2586
            }
2587
 
2588
          if (is_positive_overflow_infinity (max))
2589
            max = tmax;
2590
        }
2591
 
2592
      set_value_range (vr, VR_RANGE, min, max, vr->equiv);
2593
    }
2594
}
2595
 
2596
/* Return true if VAR may overflow at STMT.  This checks any available
2597
   loop information to see if we can determine that VAR does not
2598
   overflow.  */
2599
 
2600
static bool
2601
vrp_var_may_overflow (tree var, tree stmt)
2602
{
2603
  struct loop *l;
2604
  tree chrec, init, step;
2605
 
2606
  if (current_loops == NULL)
2607
    return true;
2608
 
2609
  l = loop_containing_stmt (stmt);
2610
  if (l == NULL)
2611
    return true;
2612
 
2613
  chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
2614
  if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
2615
    return true;
2616
 
2617
  init = initial_condition_in_loop_num (chrec, l->num);
2618
  step = evolution_part_in_loop_num (chrec, l->num);
2619
 
2620
  if (step == NULL_TREE
2621
      || !is_gimple_min_invariant (step)
2622
      || !valid_value_p (init))
2623
    return true;
2624
 
2625
  /* If we get here, we know something useful about VAR based on the
2626
     loop information.  If it wraps, it may overflow.  */
2627
 
2628
  if (scev_probably_wraps_p (init, step, stmt,
2629
                             current_loops->parray[CHREC_VARIABLE (chrec)],
2630
                             true))
2631
    return true;
2632
 
2633
  if (dump_file && (dump_flags & TDF_DETAILS) != 0)
2634
    {
2635
      print_generic_expr (dump_file, var, 0);
2636
      fprintf (dump_file, ": loop information indicates does not overflow\n");
2637
    }
2638
 
2639
  return false;
2640
}
2641
 
2642
 
2643
/* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
2644
 
2645
   - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
2646
     all the values in the ranges.
2647
 
2648
   - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
2649
 
2650
   - Return NULL_TREE if it is not always possible to determine the
2651
     value of the comparison.
2652
 
2653
   Also set *STRICT_OVERFLOW_P to indicate whether a range with an
2654
   overflow infinity was used in the test.  */
2655
 
2656
 
2657
static tree
2658
compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
2659
                bool *strict_overflow_p)
2660
{
2661
  /* VARYING or UNDEFINED ranges cannot be compared.  */
2662
  if (vr0->type == VR_VARYING
2663
      || vr0->type == VR_UNDEFINED
2664
      || vr1->type == VR_VARYING
2665
      || vr1->type == VR_UNDEFINED)
2666
    return NULL_TREE;
2667
 
2668
  /* Anti-ranges need to be handled separately.  */
2669
  if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
2670
    {
2671
      /* If both are anti-ranges, then we cannot compute any
2672
         comparison.  */
2673
      if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
2674
        return NULL_TREE;
2675
 
2676
      /* These comparisons are never statically computable.  */
2677
      if (comp == GT_EXPR
2678
          || comp == GE_EXPR
2679
          || comp == LT_EXPR
2680
          || comp == LE_EXPR)
2681
        return NULL_TREE;
2682
 
2683
      /* Equality can be computed only between a range and an
2684
         anti-range.  ~[VAL1, VAL2] == [VAL1, VAL2] is always false.  */
2685
      if (vr0->type == VR_RANGE)
2686
        {
2687
          /* To simplify processing, make VR0 the anti-range.  */
2688
          value_range_t *tmp = vr0;
2689
          vr0 = vr1;
2690
          vr1 = tmp;
2691
        }
2692
 
2693
      gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
2694
 
2695
      if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
2696
          && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
2697
        return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
2698
 
2699
      return NULL_TREE;
2700
    }
2701
 
2702
  if (!usable_range_p (vr0, strict_overflow_p)
2703
      || !usable_range_p (vr1, strict_overflow_p))
2704
    return NULL_TREE;
2705
 
2706
  /* Simplify processing.  If COMP is GT_EXPR or GE_EXPR, switch the
2707
     operands around and change the comparison code.  */
2708
  if (comp == GT_EXPR || comp == GE_EXPR)
2709
    {
2710
      value_range_t *tmp;
2711
      comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
2712
      tmp = vr0;
2713
      vr0 = vr1;
2714
      vr1 = tmp;
2715
    }
2716
 
2717
  if (comp == EQ_EXPR)
2718
    {
2719
      /* Equality may only be computed if both ranges represent
2720
         exactly one value.  */
2721
      if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
2722
          && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
2723
        {
2724
          int cmp_min = compare_values_warnv (vr0->min, vr1->min,
2725
                                              strict_overflow_p);
2726
          int cmp_max = compare_values_warnv (vr0->max, vr1->max,
2727
                                              strict_overflow_p);
2728
          if (cmp_min == 0 && cmp_max == 0)
2729
            return boolean_true_node;
2730
          else if (cmp_min != -2 && cmp_max != -2)
2731
            return boolean_false_node;
2732
        }
2733
      /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1.  */
2734
      else if (compare_values_warnv (vr0->min, vr1->max,
2735
                                     strict_overflow_p) == 1
2736
               || compare_values_warnv (vr1->min, vr0->max,
2737
                                        strict_overflow_p) == 1)
2738
        return boolean_false_node;
2739
 
2740
      return NULL_TREE;
2741
    }
2742
  else if (comp == NE_EXPR)
2743
    {
2744
      int cmp1, cmp2;
2745
 
2746
      /* If VR0 is completely to the left or completely to the right
2747
         of VR1, they are always different.  Notice that we need to
2748
         make sure that both comparisons yield similar results to
2749
         avoid comparing values that cannot be compared at
2750
         compile-time.  */
2751
      cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
2752
      cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
2753
      if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
2754
        return boolean_true_node;
2755
 
2756
      /* If VR0 and VR1 represent a single value and are identical,
2757
         return false.  */
2758
      else if (compare_values_warnv (vr0->min, vr0->max,
2759
                                     strict_overflow_p) == 0
2760
               && compare_values_warnv (vr1->min, vr1->max,
2761
                                        strict_overflow_p) == 0
2762
               && compare_values_warnv (vr0->min, vr1->min,
2763
                                        strict_overflow_p) == 0
2764
               && compare_values_warnv (vr0->max, vr1->max,
2765
                                        strict_overflow_p) == 0)
2766
        return boolean_false_node;
2767
 
2768
      /* Otherwise, they may or may not be different.  */
2769
      else
2770
        return NULL_TREE;
2771
    }
2772
  else if (comp == LT_EXPR || comp == LE_EXPR)
2773
    {
2774
      int tst;
2775
 
2776
      /* If VR0 is to the left of VR1, return true.  */
2777
      tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
2778
      if ((comp == LT_EXPR && tst == -1)
2779
          || (comp == LE_EXPR && (tst == -1 || tst == 0)))
2780
        {
2781
          if (overflow_infinity_range_p (vr0)
2782
              || overflow_infinity_range_p (vr1))
2783
            *strict_overflow_p = true;
2784
          return boolean_true_node;
2785
        }
2786
 
2787
      /* If VR0 is to the right of VR1, return false.  */
2788
      tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
2789
      if ((comp == LT_EXPR && (tst == 0 || tst == 1))
2790
          || (comp == LE_EXPR && tst == 1))
2791
        {
2792
          if (overflow_infinity_range_p (vr0)
2793
              || overflow_infinity_range_p (vr1))
2794
            *strict_overflow_p = true;
2795
          return boolean_false_node;
2796
        }
2797
 
2798
      /* Otherwise, we don't know.  */
2799
      return NULL_TREE;
2800
    }
2801
 
2802
  gcc_unreachable ();
2803
}
2804
 
2805
 
2806
/* Given a value range VR, a value VAL and a comparison code COMP, return
2807
   BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
2808
   values in VR.  Return BOOLEAN_FALSE_NODE if the comparison
2809
   always returns false.  Return NULL_TREE if it is not always
2810
   possible to determine the value of the comparison.  Also set
2811
   *STRICT_OVERFLOW_P to indicate whether a range with an overflow
2812
   infinity was used in the test.  */
2813
 
2814
static tree
2815
compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
2816
                          bool *strict_overflow_p)
2817
{
2818
  if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
2819
    return NULL_TREE;
2820
 
2821
  /* Anti-ranges need to be handled separately.  */
2822
  if (vr->type == VR_ANTI_RANGE)
2823
    {
2824
      /* For anti-ranges, the only predicates that we can compute at
2825
         compile time are equality and inequality.  */
2826
      if (comp == GT_EXPR
2827
          || comp == GE_EXPR
2828
          || comp == LT_EXPR
2829
          || comp == LE_EXPR)
2830
        return NULL_TREE;
2831
 
2832
      /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2.  */
2833
      if (value_inside_range (val, vr) == 1)
2834
        return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
2835
 
2836
      return NULL_TREE;
2837
    }
2838
 
2839
  if (!usable_range_p (vr, strict_overflow_p))
2840
    return NULL_TREE;
2841
 
2842
  if (comp == EQ_EXPR)
2843
    {
2844
      /* EQ_EXPR may only be computed if VR represents exactly
2845
         one value.  */
2846
      if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
2847
        {
2848
          int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
2849
          if (cmp == 0)
2850
            return boolean_true_node;
2851
          else if (cmp == -1 || cmp == 1 || cmp == 2)
2852
            return boolean_false_node;
2853
        }
2854
      else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
2855
               || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
2856
        return boolean_false_node;
2857
 
2858
      return NULL_TREE;
2859
    }
2860
  else if (comp == NE_EXPR)
2861
    {
2862
      /* If VAL is not inside VR, then they are always different.  */
2863
      if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
2864
          || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
2865
        return boolean_true_node;
2866
 
2867
      /* If VR represents exactly one value equal to VAL, then return
2868
         false.  */
2869
      if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
2870
          && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
2871
        return boolean_false_node;
2872
 
2873
      /* Otherwise, they may or may not be different.  */
2874
      return NULL_TREE;
2875
    }
2876
  else if (comp == LT_EXPR || comp == LE_EXPR)
2877
    {
2878
      int tst;
2879
 
2880
      /* If VR is to the left of VAL, return true.  */
2881
      tst = compare_values_warnv (vr->max, val, strict_overflow_p);
2882
      if ((comp == LT_EXPR && tst == -1)
2883
          || (comp == LE_EXPR && (tst == -1 || tst == 0)))
2884
        {
2885
          if (overflow_infinity_range_p (vr))
2886
            *strict_overflow_p = true;
2887
          return boolean_true_node;
2888
        }
2889
 
2890
      /* If VR is to the right of VAL, return false.  */
2891
      tst = compare_values_warnv (vr->min, val, strict_overflow_p);
2892
      if ((comp == LT_EXPR && (tst == 0 || tst == 1))
2893
          || (comp == LE_EXPR && tst == 1))
2894
        {
2895
          if (overflow_infinity_range_p (vr))
2896
            *strict_overflow_p = true;
2897
          return boolean_false_node;
2898
        }
2899
 
2900
      /* Otherwise, we don't know.  */
2901
      return NULL_TREE;
2902
    }
2903
  else if (comp == GT_EXPR || comp == GE_EXPR)
2904
    {
2905
      int tst;
2906
 
2907
      /* If VR is to the right of VAL, return true.  */
2908
      tst = compare_values_warnv (vr->min, val, strict_overflow_p);
2909
      if ((comp == GT_EXPR && tst == 1)
2910
          || (comp == GE_EXPR && (tst == 0 || tst == 1)))
2911
        {
2912
          if (overflow_infinity_range_p (vr))
2913
            *strict_overflow_p = true;
2914
          return boolean_true_node;
2915
        }
2916
 
2917
      /* If VR is to the left of VAL, return false.  */
2918
      tst = compare_values_warnv (vr->max, val, strict_overflow_p);
2919
      if ((comp == GT_EXPR && (tst == -1 || tst == 0))
2920
          || (comp == GE_EXPR && tst == -1))
2921
        {
2922
          if (overflow_infinity_range_p (vr))
2923
            *strict_overflow_p = true;
2924
          return boolean_false_node;
2925
        }
2926
 
2927
      /* Otherwise, we don't know.  */
2928
      return NULL_TREE;
2929
    }
2930
 
2931
  gcc_unreachable ();
2932
}
2933
 
2934
 
2935
/* Debugging dumps.  */
2936
 
2937
void dump_value_range (FILE *, value_range_t *);
2938
void debug_value_range (value_range_t *);
2939
void dump_all_value_ranges (FILE *);
2940
void debug_all_value_ranges (void);
2941
void dump_vr_equiv (FILE *, bitmap);
2942
void debug_vr_equiv (bitmap);
2943
 
2944
 
2945
/* Dump value range VR to FILE.  */
2946
 
2947
void
2948
dump_value_range (FILE *file, value_range_t *vr)
2949
{
2950
  if (vr == NULL)
2951
    fprintf (file, "[]");
2952
  else if (vr->type == VR_UNDEFINED)
2953
    fprintf (file, "UNDEFINED");
2954
  else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
2955
    {
2956
      tree type = TREE_TYPE (vr->min);
2957
 
2958
      fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
2959
 
2960
      if (is_negative_overflow_infinity (vr->min))
2961
        fprintf (file, "-INF(OVF)");
2962
      else if (INTEGRAL_TYPE_P (type)
2963
               && !TYPE_UNSIGNED (type)
2964
               && vrp_val_is_min (vr->min))
2965
        fprintf (file, "-INF");
2966
      else
2967
        print_generic_expr (file, vr->min, 0);
2968
 
2969
      fprintf (file, ", ");
2970
 
2971
      if (is_positive_overflow_infinity (vr->max))
2972
        fprintf (file, "+INF(OVF)");
2973
      else if (INTEGRAL_TYPE_P (type)
2974
               && vrp_val_is_max (vr->max))
2975
        fprintf (file, "+INF");
2976
      else
2977
        print_generic_expr (file, vr->max, 0);
2978
 
2979
      fprintf (file, "]");
2980
 
2981
      if (vr->equiv)
2982
        {
2983
          bitmap_iterator bi;
2984
          unsigned i, c = 0;
2985
 
2986
          fprintf (file, "  EQUIVALENCES: { ");
2987
 
2988
          EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
2989
            {
2990
              print_generic_expr (file, ssa_name (i), 0);
2991
              fprintf (file, " ");
2992
              c++;
2993
            }
2994
 
2995
          fprintf (file, "} (%u elements)", c);
2996
        }
2997
    }
2998
  else if (vr->type == VR_VARYING)
2999
    fprintf (file, "VARYING");
3000
  else
3001
    fprintf (file, "INVALID RANGE");
3002
}
3003
 
3004
 
3005
/* Dump value range VR to stderr.  */
3006
 
3007
void
3008
debug_value_range (value_range_t *vr)
3009
{
3010
  dump_value_range (stderr, vr);
3011
  fprintf (stderr, "\n");
3012
}
3013
 
3014
 
3015
/* Dump value ranges of all SSA_NAMEs to FILE.  */
3016
 
3017
void
3018
dump_all_value_ranges (FILE *file)
3019
{
3020
  size_t i;
3021
 
3022
  for (i = 0; i < num_ssa_names; i++)
3023
    {
3024
      if (vr_value[i])
3025
        {
3026
          print_generic_expr (file, ssa_name (i), 0);
3027
          fprintf (file, ": ");
3028
          dump_value_range (file, vr_value[i]);
3029
          fprintf (file, "\n");
3030
        }
3031
    }
3032
 
3033
  fprintf (file, "\n");
3034
}
3035
 
3036
 
3037
/* Dump all value ranges to stderr.  */
3038
 
3039
void
3040
debug_all_value_ranges (void)
3041
{
3042
  dump_all_value_ranges (stderr);
3043
}
3044
 
3045
 
3046
/* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
3047
   create a new SSA name N and return the assertion assignment
3048
   'V = ASSERT_EXPR <V, V OP W>'.  */
3049
 
3050
static tree
3051
build_assert_expr_for (tree cond, tree v)
3052
{
3053
  tree n, assertion;
3054
 
3055
  gcc_assert (TREE_CODE (v) == SSA_NAME);
3056
  n = duplicate_ssa_name (v, NULL_TREE);
3057
 
3058
  if (COMPARISON_CLASS_P (cond))
3059
    {
3060
      tree a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
3061
      assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, a);
3062
    }
3063
  else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
3064
    {
3065
      /* Given !V, build the assignment N = false.  */
3066
      tree op0 = TREE_OPERAND (cond, 0);
3067
      gcc_assert (op0 == v);
3068
      assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, boolean_false_node);
3069
    }
3070
  else if (TREE_CODE (cond) == SSA_NAME)
3071
    {
3072
      /* Given V, build the assignment N = true.  */
3073
      gcc_assert (v == cond);
3074
      assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, boolean_true_node);
3075
    }
3076
  else
3077
    gcc_unreachable ();
3078
 
3079
  SSA_NAME_DEF_STMT (n) = assertion;
3080
 
3081
  /* The new ASSERT_EXPR, creates a new SSA name that replaces the
3082
     operand of the ASSERT_EXPR. Register the new name and the old one
3083
     in the replacement table so that we can fix the SSA web after
3084
     adding all the ASSERT_EXPRs.  */
3085
  register_new_name_mapping (n, v);
3086
 
3087
  return assertion;
3088
}
3089
 
3090
 
3091
/* Return false if EXPR is a predicate expression involving floating
3092
   point values.  */
3093
 
3094
static inline bool
3095
fp_predicate (tree expr)
3096
{
3097
  return (COMPARISON_CLASS_P (expr)
3098
          && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0))));
3099
}
3100
 
3101
 
3102
/* If the range of values taken by OP can be inferred after STMT executes,
3103
   return the comparison code (COMP_CODE_P) and value (VAL_P) that
3104
   describes the inferred range.  Return true if a range could be
3105
   inferred.  */
3106
 
3107
static bool
3108
infer_value_range (tree stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
3109
{
3110
  *val_p = NULL_TREE;
3111
  *comp_code_p = ERROR_MARK;
3112
 
3113
  /* Do not attempt to infer anything in names that flow through
3114
     abnormal edges.  */
3115
  if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
3116
    return false;
3117
 
3118
  /* Similarly, don't infer anything from statements that may throw
3119
     exceptions.  */
3120
  if (tree_could_throw_p (stmt))
3121
    return false;
3122
 
3123
  /* If STMT is the last statement of a basic block with no
3124
     successors, there is no point inferring anything about any of its
3125
     operands.  We would not be able to find a proper insertion point
3126
     for the assertion, anyway.  */
3127
  if (stmt_ends_bb_p (stmt) && EDGE_COUNT (bb_for_stmt (stmt)->succs) == 0)
3128
    return false;
3129
 
3130
  /* We can only assume that a pointer dereference will yield
3131
     non-NULL if -fdelete-null-pointer-checks is enabled.  */
3132
  if (flag_delete_null_pointer_checks && POINTER_TYPE_P (TREE_TYPE (op)))
3133
    {
3134
      bool is_store;
3135
      unsigned num_uses, num_derefs;
3136
 
3137
      count_uses_and_derefs (op, stmt, &num_uses, &num_derefs, &is_store);
3138
      if (num_derefs > 0)
3139
        {
3140
          *val_p = build_int_cst (TREE_TYPE (op), 0);
3141
          *comp_code_p = NE_EXPR;
3142
          return true;
3143
        }
3144
    }
3145
 
3146
  return false;
3147
}
3148
 
3149
 
3150
void dump_asserts_for (FILE *, tree);
3151
void debug_asserts_for (tree);
3152
void dump_all_asserts (FILE *);
3153
void debug_all_asserts (void);
3154
 
3155
/* Dump all the registered assertions for NAME to FILE.  */
3156
 
3157
void
3158
dump_asserts_for (FILE *file, tree name)
3159
{
3160
  assert_locus_t loc;
3161
 
3162
  fprintf (file, "Assertions to be inserted for ");
3163
  print_generic_expr (file, name, 0);
3164
  fprintf (file, "\n");
3165
 
3166
  loc = asserts_for[SSA_NAME_VERSION (name)];
3167
  while (loc)
3168
    {
3169
      fprintf (file, "\t");
3170
      print_generic_expr (file, bsi_stmt (loc->si), 0);
3171
      fprintf (file, "\n\tBB #%d", loc->bb->index);
3172
      if (loc->e)
3173
        {
3174
          fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
3175
                   loc->e->dest->index);
3176
          dump_edge_info (file, loc->e, 0);
3177
        }
3178
      fprintf (file, "\n\tPREDICATE: ");
3179
      print_generic_expr (file, name, 0);
3180
      fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
3181
      print_generic_expr (file, loc->val, 0);
3182
      fprintf (file, "\n\n");
3183
      loc = loc->next;
3184
    }
3185
 
3186
  fprintf (file, "\n");
3187
}
3188
 
3189
 
3190
/* Dump all the registered assertions for NAME to stderr.  */
3191
 
3192
void
3193
debug_asserts_for (tree name)
3194
{
3195
  dump_asserts_for (stderr, name);
3196
}
3197
 
3198
 
3199
/* Dump all the registered assertions for all the names to FILE.  */
3200
 
3201
void
3202
dump_all_asserts (FILE *file)
3203
{
3204
  unsigned i;
3205
  bitmap_iterator bi;
3206
 
3207
  fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
3208
  EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
3209
    dump_asserts_for (file, ssa_name (i));
3210
  fprintf (file, "\n");
3211
}
3212
 
3213
 
3214
/* Dump all the registered assertions for all the names to stderr.  */
3215
 
3216
void
3217
debug_all_asserts (void)
3218
{
3219
  dump_all_asserts (stderr);
3220
}
3221
 
3222
 
3223
/* If NAME doesn't have an ASSERT_EXPR registered for asserting
3224
   'NAME COMP_CODE VAL' at a location that dominates block BB or
3225
   E->DEST, then register this location as a possible insertion point
3226
   for ASSERT_EXPR <NAME, NAME COMP_CODE VAL>.
3227
 
3228
   BB, E and SI provide the exact insertion point for the new
3229
   ASSERT_EXPR.  If BB is NULL, then the ASSERT_EXPR is to be inserted
3230
   on edge E.  Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
3231
   BB.  If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
3232
   must not be NULL.  */
3233
 
3234
static void
3235
register_new_assert_for (tree name,
3236
                         enum tree_code comp_code,
3237
                         tree val,
3238
                         basic_block bb,
3239
                         edge e,
3240
                         block_stmt_iterator si)
3241
{
3242
  assert_locus_t n, loc, last_loc;
3243
  bool found;
3244
  basic_block dest_bb;
3245
 
3246
#if defined ENABLE_CHECKING
3247
  gcc_assert (bb == NULL || e == NULL);
3248
 
3249
  if (e == NULL)
3250
    gcc_assert (TREE_CODE (bsi_stmt (si)) != COND_EXPR
3251
                && TREE_CODE (bsi_stmt (si)) != SWITCH_EXPR);
3252
#endif
3253
 
3254
  /* The new assertion A will be inserted at BB or E.  We need to
3255
     determine if the new location is dominated by a previously
3256
     registered location for A.  If we are doing an edge insertion,
3257
     assume that A will be inserted at E->DEST.  Note that this is not
3258
     necessarily true.
3259
 
3260
     If E is a critical edge, it will be split.  But even if E is
3261
     split, the new block will dominate the same set of blocks that
3262
     E->DEST dominates.
3263
 
3264
     The reverse, however, is not true, blocks dominated by E->DEST
3265
     will not be dominated by the new block created to split E.  So,
3266
     if the insertion location is on a critical edge, we will not use
3267
     the new location to move another assertion previously registered
3268
     at a block dominated by E->DEST.  */
3269
  dest_bb = (bb) ? bb : e->dest;
3270
 
3271
  /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
3272
     VAL at a block dominating DEST_BB, then we don't need to insert a new
3273
     one.  Similarly, if the same assertion already exists at a block
3274
     dominated by DEST_BB and the new location is not on a critical
3275
     edge, then update the existing location for the assertion (i.e.,
3276
     move the assertion up in the dominance tree).
3277
 
3278
     Note, this is implemented as a simple linked list because there
3279
     should not be more than a handful of assertions registered per
3280
     name.  If this becomes a performance problem, a table hashed by
3281
     COMP_CODE and VAL could be implemented.  */
3282
  loc = asserts_for[SSA_NAME_VERSION (name)];
3283
  last_loc = loc;
3284
  found = false;
3285
  while (loc)
3286
    {
3287
      if (loc->comp_code == comp_code
3288
          && (loc->val == val
3289
              || operand_equal_p (loc->val, val, 0)))
3290
        {
3291
          /* If the assertion NAME COMP_CODE VAL has already been
3292
             registered at a basic block that dominates DEST_BB, then
3293
             we don't need to insert the same assertion again.  Note
3294
             that we don't check strict dominance here to avoid
3295
             replicating the same assertion inside the same basic
3296
             block more than once (e.g., when a pointer is
3297
             dereferenced several times inside a block).
3298
 
3299
             An exception to this rule are edge insertions.  If the
3300
             new assertion is to be inserted on edge E, then it will
3301
             dominate all the other insertions that we may want to
3302
             insert in DEST_BB.  So, if we are doing an edge
3303
             insertion, don't do this dominance check.  */
3304
          if (e == NULL
3305
              && dominated_by_p (CDI_DOMINATORS, dest_bb, loc->bb))
3306
            return;
3307
 
3308
          /* Otherwise, if E is not a critical edge and DEST_BB
3309
             dominates the existing location for the assertion, move
3310
             the assertion up in the dominance tree by updating its
3311
             location information.  */
3312
          if ((e == NULL || !EDGE_CRITICAL_P (e))
3313
              && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
3314
            {
3315
              loc->bb = dest_bb;
3316
              loc->e = e;
3317
              loc->si = si;
3318
              return;
3319
            }
3320
        }
3321
 
3322
      /* Update the last node of the list and move to the next one.  */
3323
      last_loc = loc;
3324
      loc = loc->next;
3325
    }
3326
 
3327
  /* If we didn't find an assertion already registered for
3328
     NAME COMP_CODE VAL, add a new one at the end of the list of
3329
     assertions associated with NAME.  */
3330
  n = XNEW (struct assert_locus_d);
3331
  n->bb = dest_bb;
3332
  n->e = e;
3333
  n->si = si;
3334
  n->comp_code = comp_code;
3335
  n->val = val;
3336
  n->next = NULL;
3337
 
3338
  if (last_loc)
3339
    last_loc->next = n;
3340
  else
3341
    asserts_for[SSA_NAME_VERSION (name)] = n;
3342
 
3343
  bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
3344
}
3345
 
3346
 
3347
/* Try to register an edge assertion for SSA name NAME on edge E for
3348
   the conditional jump pointed to by SI.  Return true if an assertion
3349
   for NAME could be registered.  */
3350
 
3351
static bool
3352
register_edge_assert_for (tree name, edge e, block_stmt_iterator si)
3353
{
3354
  tree val, stmt;
3355
  enum tree_code comp_code;
3356
 
3357
  stmt = bsi_stmt (si);
3358
 
3359
  /* Do not attempt to infer anything in names that flow through
3360
     abnormal edges.  */
3361
  if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
3362
    return false;
3363
 
3364
  /* If NAME was not found in the sub-graph reachable from E, then
3365
     there's nothing to do.  */
3366
  if (!TEST_BIT (found_in_subgraph, SSA_NAME_VERSION (name)))
3367
    return false;
3368
 
3369
  /* We found a use of NAME in the sub-graph rooted at E->DEST.
3370
     Register an assertion for NAME according to the value that NAME
3371
     takes on edge E.  */
3372
  if (TREE_CODE (stmt) == COND_EXPR)
3373
    {
3374
      /* If BB ends in a COND_EXPR then NAME then we should insert
3375
         the original predicate on EDGE_TRUE_VALUE and the
3376
         opposite predicate on EDGE_FALSE_VALUE.  */
3377
      tree cond = COND_EXPR_COND (stmt);
3378
      bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
3379
 
3380
      /* Predicates may be a single SSA name or NAME OP VAL.  */
3381
      if (cond == name)
3382
        {
3383
          /* If the predicate is a name, it must be NAME, in which
3384
             case we create the predicate NAME == true or
3385
             NAME == false accordingly.  */
3386
          comp_code = EQ_EXPR;
3387
          val = (is_else_edge) ? boolean_false_node : boolean_true_node;
3388
        }
3389
      else
3390
        {
3391
          /* Otherwise, we have a comparison of the form NAME COMP VAL
3392
             or VAL COMP NAME.  */
3393
          if (name == TREE_OPERAND (cond, 1))
3394
            {
3395
              /* If the predicate is of the form VAL COMP NAME, flip
3396
                 COMP around because we need to register NAME as the
3397
                 first operand in the predicate.  */
3398
              comp_code = swap_tree_comparison (TREE_CODE (cond));
3399
              val = TREE_OPERAND (cond, 0);
3400
            }
3401
          else
3402
            {
3403
              /* The comparison is of the form NAME COMP VAL, so the
3404
                 comparison code remains unchanged.  */
3405
              comp_code = TREE_CODE (cond);
3406
              val = TREE_OPERAND (cond, 1);
3407
            }
3408
 
3409
          /* If we are inserting the assertion on the ELSE edge, we
3410
             need to invert the sign comparison.  */
3411
          if (is_else_edge)
3412
            comp_code = invert_tree_comparison (comp_code, 0);
3413
 
3414
          /* Do not register always-false predicates.  FIXME, this
3415
             works around a limitation in fold() when dealing with
3416
             enumerations.  Given 'enum { N1, N2 } x;', fold will not
3417
             fold 'if (x > N2)' to 'if (0)'.  */
3418
          if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
3419
              && (INTEGRAL_TYPE_P (TREE_TYPE (val))
3420
                  || SCALAR_FLOAT_TYPE_P (TREE_TYPE (val))))
3421
            {
3422
              tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
3423
              tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
3424
 
3425
              if (comp_code == GT_EXPR && compare_values (val, max) == 0)
3426
                return false;
3427
 
3428
              if (comp_code == LT_EXPR && compare_values (val, min) == 0)
3429
                return false;
3430
            }
3431
        }
3432
    }
3433
  else
3434
    {
3435
      /* FIXME.  Handle SWITCH_EXPR.  */
3436
      gcc_unreachable ();
3437
    }
3438
 
3439
  register_new_assert_for (name, comp_code, val, NULL, e, si);
3440
  return true;
3441
}
3442
 
3443
 
3444
static bool find_assert_locations (basic_block bb);
3445
 
3446
/* Determine whether the outgoing edges of BB should receive an
3447
   ASSERT_EXPR for each of the operands of BB's last statement.  The
3448
   last statement of BB must be a COND_EXPR or a SWITCH_EXPR.
3449
 
3450
   If any of the sub-graphs rooted at BB have an interesting use of
3451
   the predicate operands, an assert location node is added to the
3452
   list of assertions for the corresponding operands.  */
3453
 
3454
static bool
3455
find_conditional_asserts (basic_block bb)
3456
{
3457
  bool need_assert;
3458
  block_stmt_iterator last_si;
3459
  tree op, last;
3460
  edge_iterator ei;
3461
  edge e;
3462
  ssa_op_iter iter;
3463
 
3464
  need_assert = false;
3465
  last_si = bsi_last (bb);
3466
  last = bsi_stmt (last_si);
3467
 
3468
  /* Look for uses of the operands in each of the sub-graphs
3469
     rooted at BB.  We need to check each of the outgoing edges
3470
     separately, so that we know what kind of ASSERT_EXPR to
3471
     insert.  */
3472
  FOR_EACH_EDGE (e, ei, bb->succs)
3473
    {
3474
      if (e->dest == bb)
3475
        continue;
3476
 
3477
      /* Remove the COND_EXPR operands from the FOUND_IN_SUBGRAPH bitmap.
3478
         Otherwise, when we finish traversing each of the sub-graphs, we
3479
         won't know whether the variables were found in the sub-graphs or
3480
         if they had been found in a block upstream from BB.
3481
 
3482
         This is actually a bad idea is some cases, particularly jump
3483
         threading.  Consider a CFG like the following:
3484
 
3485
 
3486
                   /|
3487
                  1 |
3488
                   \|
3489
                    2
3490
                   / \
3491
                  3   4
3492
 
3493
         Assume that one or more operands in the conditional at the
3494
         end of block 0 are used in a conditional in block 2, but not
3495
         anywhere in block 1.  In this case we will not insert any
3496
         assert statements in block 1, which may cause us to miss
3497
         opportunities to optimize, particularly for jump threading.  */
3498
      FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3499
        RESET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3500
 
3501
      /* Traverse the strictly dominated sub-graph rooted at E->DEST
3502
         to determine if any of the operands in the conditional
3503
         predicate are used.  */
3504
      if (e->dest != bb)
3505
        need_assert |= find_assert_locations (e->dest);
3506
 
3507
      /* Register the necessary assertions for each operand in the
3508
         conditional predicate.  */
3509
      FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3510
        need_assert |= register_edge_assert_for (op, e, last_si);
3511
    }
3512
 
3513
  /* Finally, indicate that we have found the operands in the
3514
     conditional.  */
3515
  FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3516
    SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3517
 
3518
  return need_assert;
3519
}
3520
 
3521
 
3522
/* Traverse all the statements in block BB looking for statements that
3523
   may generate useful assertions for the SSA names in their operand.
3524
   If a statement produces a useful assertion A for name N_i, then the
3525
   list of assertions already generated for N_i is scanned to
3526
   determine if A is actually needed.
3527
 
3528
   If N_i already had the assertion A at a location dominating the
3529
   current location, then nothing needs to be done.  Otherwise, the
3530
   new location for A is recorded instead.
3531
 
3532
   1- For every statement S in BB, all the variables used by S are
3533
      added to bitmap FOUND_IN_SUBGRAPH.
3534
 
3535
   2- If statement S uses an operand N in a way that exposes a known
3536
      value range for N, then if N was not already generated by an
3537
      ASSERT_EXPR, create a new assert location for N.  For instance,
3538
      if N is a pointer and the statement dereferences it, we can
3539
      assume that N is not NULL.
3540
 
3541
   3- COND_EXPRs are a special case of #2.  We can derive range
3542
      information from the predicate but need to insert different
3543
      ASSERT_EXPRs for each of the sub-graphs rooted at the
3544
      conditional block.  If the last statement of BB is a conditional
3545
      expression of the form 'X op Y', then
3546
 
3547
      a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
3548
 
3549
      b) If the conditional is the only entry point to the sub-graph
3550
         corresponding to the THEN_CLAUSE, recurse into it.  On
3551
         return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
3552
         an ASSERT_EXPR is added for the corresponding variable.
3553
 
3554
      c) Repeat step (b) on the ELSE_CLAUSE.
3555
 
3556
      d) Mark X and Y in FOUND_IN_SUBGRAPH.
3557
 
3558
      For instance,
3559
 
3560
            if (a == 9)
3561
              b = a;
3562
            else
3563
              b = c + 1;
3564
 
3565
      In this case, an assertion on the THEN clause is useful to
3566
      determine that 'a' is always 9 on that edge.  However, an assertion
3567
      on the ELSE clause would be unnecessary.
3568
 
3569
   4- If BB does not end in a conditional expression, then we recurse
3570
      into BB's dominator children.
3571
 
3572
   At the end of the recursive traversal, every SSA name will have a
3573
   list of locations where ASSERT_EXPRs should be added.  When a new
3574
   location for name N is found, it is registered by calling
3575
   register_new_assert_for.  That function keeps track of all the
3576
   registered assertions to prevent adding unnecessary assertions.
3577
   For instance, if a pointer P_4 is dereferenced more than once in a
3578
   dominator tree, only the location dominating all the dereference of
3579
   P_4 will receive an ASSERT_EXPR.
3580
 
3581
   If this function returns true, then it means that there are names
3582
   for which we need to generate ASSERT_EXPRs.  Those assertions are
3583
   inserted by process_assert_insertions.
3584
 
3585
   TODO.  Handle SWITCH_EXPR.  */
3586
 
3587
static bool
3588
find_assert_locations (basic_block bb)
3589
{
3590
  block_stmt_iterator si;
3591
  tree last, phi;
3592
  bool need_assert;
3593
  basic_block son;
3594
 
3595
  if (TEST_BIT (blocks_visited, bb->index))
3596
    return false;
3597
 
3598
  SET_BIT (blocks_visited, bb->index);
3599
 
3600
  need_assert = false;
3601
 
3602
  /* Traverse all PHI nodes in BB marking used operands.  */
3603
  for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3604
    {
3605
      use_operand_p arg_p;
3606
      ssa_op_iter i;
3607
 
3608
      FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
3609
        {
3610
          tree arg = USE_FROM_PTR (arg_p);
3611
          if (TREE_CODE (arg) == SSA_NAME)
3612
            {
3613
              gcc_assert (is_gimple_reg (PHI_RESULT (phi)));
3614
              SET_BIT (found_in_subgraph, SSA_NAME_VERSION (arg));
3615
            }
3616
        }
3617
    }
3618
 
3619
  /* Traverse all the statements in BB marking used names and looking
3620
     for statements that may infer assertions for their used operands.  */
3621
  last = NULL_TREE;
3622
  for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3623
    {
3624
      tree stmt, op;
3625
      ssa_op_iter i;
3626
 
3627
      stmt = bsi_stmt (si);
3628
 
3629
      /* See if we can derive an assertion for any of STMT's operands.  */
3630
      FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
3631
        {
3632
          tree value;
3633
          enum tree_code comp_code;
3634
 
3635
          /* Mark OP in bitmap FOUND_IN_SUBGRAPH.  If STMT is inside
3636
             the sub-graph of a conditional block, when we return from
3637
             this recursive walk, our parent will use the
3638
             FOUND_IN_SUBGRAPH bitset to determine if one of the
3639
             operands it was looking for was present in the sub-graph.  */
3640
          SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3641
 
3642
          /* If OP is used in such a way that we can infer a value
3643
             range for it, and we don't find a previous assertion for
3644
             it, create a new assertion location node for OP.  */
3645
          if (infer_value_range (stmt, op, &comp_code, &value))
3646
            {
3647
              /* If we are able to infer a nonzero value range for OP,
3648
                 then walk backwards through the use-def chain to see if OP
3649
                 was set via a typecast.
3650
 
3651
                 If so, then we can also infer a nonzero value range
3652
                 for the operand of the NOP_EXPR.  */
3653
              if (comp_code == NE_EXPR && integer_zerop (value))
3654
                {
3655
                  tree t = op;
3656
                  tree def_stmt = SSA_NAME_DEF_STMT (t);
3657
 
3658
                  while (TREE_CODE (def_stmt) == MODIFY_EXPR
3659
                         && TREE_CODE (TREE_OPERAND (def_stmt, 1)) == NOP_EXPR
3660
                         && TREE_CODE (TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0)) == SSA_NAME
3661
                         && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0))))
3662
                    {
3663
                      t = TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0);
3664
                      def_stmt = SSA_NAME_DEF_STMT (t);
3665
 
3666
                      /* Note we want to register the assert for the
3667
                         operand of the NOP_EXPR after SI, not after the
3668
                         conversion.  */
3669
                      if (! has_single_use (t))
3670
                        {
3671
                          register_new_assert_for (t, comp_code, value,
3672
                                                   bb, NULL, si);
3673
                          need_assert = true;
3674
                        }
3675
                    }
3676
                }
3677
 
3678
              /* If OP is used only once, namely in this STMT, don't
3679
                 bother creating an ASSERT_EXPR for it.  Such an
3680
                 ASSERT_EXPR would do nothing but increase compile time.  */
3681
              if (!has_single_use (op))
3682
                {
3683
                  register_new_assert_for (op, comp_code, value, bb, NULL, si);
3684
                  need_assert = true;
3685
                }
3686
            }
3687
        }
3688
 
3689
      /* Remember the last statement of the block.  */
3690
      last = stmt;
3691
    }
3692
 
3693
  /* If BB's last statement is a conditional expression
3694
     involving integer operands, recurse into each of the sub-graphs
3695
     rooted at BB to determine if we need to add ASSERT_EXPRs.  */
3696
  if (last
3697
      && TREE_CODE (last) == COND_EXPR
3698
      && !fp_predicate (COND_EXPR_COND (last))
3699
      && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
3700
    need_assert |= find_conditional_asserts (bb);
3701
 
3702
  /* Recurse into the dominator children of BB.  */
3703
  for (son = first_dom_son (CDI_DOMINATORS, bb);
3704
       son;
3705
       son = next_dom_son (CDI_DOMINATORS, son))
3706
    need_assert |= find_assert_locations (son);
3707
 
3708
  return need_assert;
3709
}
3710
 
3711
 
3712
/* Create an ASSERT_EXPR for NAME and insert it in the location
3713
   indicated by LOC.  Return true if we made any edge insertions.  */
3714
 
3715
static bool
3716
process_assert_insertions_for (tree name, assert_locus_t loc)
3717
{
3718
  /* Build the comparison expression NAME_i COMP_CODE VAL.  */
3719
  tree stmt, cond, assert_expr;
3720
  edge_iterator ei;
3721
  edge e;
3722
 
3723
  cond = build2 (loc->comp_code, boolean_type_node, name, loc->val);
3724
  assert_expr = build_assert_expr_for (cond, name);
3725
 
3726
  if (loc->e)
3727
    {
3728
      /* We have been asked to insert the assertion on an edge.  This
3729
         is used only by COND_EXPR and SWITCH_EXPR assertions.  */
3730
#if defined ENABLE_CHECKING
3731
      gcc_assert (TREE_CODE (bsi_stmt (loc->si)) == COND_EXPR
3732
          || TREE_CODE (bsi_stmt (loc->si)) == SWITCH_EXPR);
3733
#endif
3734
 
3735
      bsi_insert_on_edge (loc->e, assert_expr);
3736
      return true;
3737
    }
3738
 
3739
  /* Otherwise, we can insert right after LOC->SI iff the
3740
     statement must not be the last statement in the block.  */
3741
  stmt = bsi_stmt (loc->si);
3742
  if (!stmt_ends_bb_p (stmt))
3743
    {
3744
      bsi_insert_after (&loc->si, assert_expr, BSI_SAME_STMT);
3745
      return false;
3746
    }
3747
 
3748
  /* If STMT must be the last statement in BB, we can only insert new
3749
     assertions on the non-abnormal edge out of BB.  Note that since
3750
     STMT is not control flow, there may only be one non-abnormal edge
3751
     out of BB.  */
3752
  FOR_EACH_EDGE (e, ei, loc->bb->succs)
3753
    if (!(e->flags & EDGE_ABNORMAL))
3754
      {
3755
        bsi_insert_on_edge (e, assert_expr);
3756
        return true;
3757
      }
3758
 
3759
  gcc_unreachable ();
3760
}
3761
 
3762
 
3763
/* Process all the insertions registered for every name N_i registered
3764
   in NEED_ASSERT_FOR.  The list of assertions to be inserted are
3765
   found in ASSERTS_FOR[i].  */
3766
 
3767
static void
3768
process_assert_insertions (void)
3769
{
3770
  unsigned i;
3771
  bitmap_iterator bi;
3772
  bool update_edges_p = false;
3773
  int num_asserts = 0;
3774
 
3775
  if (dump_file && (dump_flags & TDF_DETAILS))
3776
    dump_all_asserts (dump_file);
3777
 
3778
  EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
3779
    {
3780
      assert_locus_t loc = asserts_for[i];
3781
      gcc_assert (loc);
3782
 
3783
      while (loc)
3784
        {
3785
          assert_locus_t next = loc->next;
3786
          update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
3787
          free (loc);
3788
          loc = next;
3789
          num_asserts++;
3790
        }
3791
    }
3792
 
3793
  if (update_edges_p)
3794
    bsi_commit_edge_inserts ();
3795
 
3796
  if (dump_file && (dump_flags & TDF_STATS))
3797
    fprintf (dump_file, "\nNumber of ASSERT_EXPR expressions inserted: %d\n\n",
3798
             num_asserts);
3799
}
3800
 
3801
 
3802
/* Traverse the flowgraph looking for conditional jumps to insert range
3803
   expressions.  These range expressions are meant to provide information
3804
   to optimizations that need to reason in terms of value ranges.  They
3805
   will not be expanded into RTL.  For instance, given:
3806
 
3807
   x = ...
3808
   y = ...
3809
   if (x < y)
3810
     y = x - 2;
3811
   else
3812
     x = y + 3;
3813
 
3814
   this pass will transform the code into:
3815
 
3816
   x = ...
3817
   y = ...
3818
   if (x < y)
3819
    {
3820
      x = ASSERT_EXPR <x, x < y>
3821
      y = x - 2
3822
    }
3823
   else
3824
    {
3825
      y = ASSERT_EXPR <y, x <= y>
3826
      x = y + 3
3827
    }
3828
 
3829
   The idea is that once copy and constant propagation have run, other
3830
   optimizations will be able to determine what ranges of values can 'x'
3831
   take in different paths of the code, simply by checking the reaching
3832
   definition of 'x'.  */
3833
 
3834
static void
3835
insert_range_assertions (void)
3836
{
3837
  edge e;
3838
  edge_iterator ei;
3839
  bool update_ssa_p;
3840
 
3841
  found_in_subgraph = sbitmap_alloc (num_ssa_names);
3842
  sbitmap_zero (found_in_subgraph);
3843
 
3844
  blocks_visited = sbitmap_alloc (last_basic_block);
3845
  sbitmap_zero (blocks_visited);
3846
 
3847
  need_assert_for = BITMAP_ALLOC (NULL);
3848
  asserts_for = XNEWVEC (assert_locus_t, num_ssa_names);
3849
  memset (asserts_for, 0, num_ssa_names * sizeof (assert_locus_t));
3850
 
3851
  calculate_dominance_info (CDI_DOMINATORS);
3852
 
3853
  update_ssa_p = false;
3854
  FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3855
    if (find_assert_locations (e->dest))
3856
      update_ssa_p = true;
3857
 
3858
  if (update_ssa_p)
3859
    {
3860
      process_assert_insertions ();
3861
      update_ssa (TODO_update_ssa_no_phi);
3862
    }
3863
 
3864
  if (dump_file && (dump_flags & TDF_DETAILS))
3865
    {
3866
      fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
3867
      dump_function_to_file (current_function_decl, dump_file, dump_flags);
3868
    }
3869
 
3870
  sbitmap_free (found_in_subgraph);
3871
  free (asserts_for);
3872
  BITMAP_FREE (need_assert_for);
3873
}
3874
 
3875
 
3876
/* Convert range assertion expressions into the implied copies and
3877
   copy propagate away the copies.  Doing the trivial copy propagation
3878
   here avoids the need to run the full copy propagation pass after
3879
   VRP.
3880
 
3881
   FIXME, this will eventually lead to copy propagation removing the
3882
   names that had useful range information attached to them.  For
3883
   instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
3884
   then N_i will have the range [3, +INF].
3885
 
3886
   However, by converting the assertion into the implied copy
3887
   operation N_i = N_j, we will then copy-propagate N_j into the uses
3888
   of N_i and lose the range information.  We may want to hold on to
3889
   ASSERT_EXPRs a little while longer as the ranges could be used in
3890
   things like jump threading.
3891
 
3892
   The problem with keeping ASSERT_EXPRs around is that passes after
3893
   VRP need to handle them appropriately.
3894
 
3895
   Another approach would be to make the range information a first
3896
   class property of the SSA_NAME so that it can be queried from
3897
   any pass.  This is made somewhat more complex by the need for
3898
   multiple ranges to be associated with one SSA_NAME.  */
3899
 
3900
static void
3901
remove_range_assertions (void)
3902
{
3903
  basic_block bb;
3904
  block_stmt_iterator si;
3905
 
3906
  /* Note that the BSI iterator bump happens at the bottom of the
3907
     loop and no bump is necessary if we're removing the statement
3908
     referenced by the current BSI.  */
3909
  FOR_EACH_BB (bb)
3910
    for (si = bsi_start (bb); !bsi_end_p (si);)
3911
      {
3912
        tree stmt = bsi_stmt (si);
3913
        tree use_stmt;
3914
 
3915
        if (TREE_CODE (stmt) == MODIFY_EXPR
3916
            && TREE_CODE (TREE_OPERAND (stmt, 1)) == ASSERT_EXPR)
3917
          {
3918
            tree rhs = TREE_OPERAND (stmt, 1), var;
3919
            tree cond = fold (ASSERT_EXPR_COND (rhs));
3920
            use_operand_p use_p;
3921
            imm_use_iterator iter;
3922
 
3923
            gcc_assert (cond != boolean_false_node);
3924
 
3925
            /* Propagate the RHS into every use of the LHS.  */
3926
            var = ASSERT_EXPR_VAR (rhs);
3927
            FOR_EACH_IMM_USE_STMT (use_stmt, iter, TREE_OPERAND (stmt, 0))
3928
              FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3929
                {
3930
                  SET_USE (use_p, var);
3931
                  gcc_assert (TREE_CODE (var) == SSA_NAME);
3932
                }
3933
 
3934
            /* And finally, remove the copy, it is not needed.  */
3935
            bsi_remove (&si, true);
3936
          }
3937
        else
3938
          bsi_next (&si);
3939
      }
3940
 
3941
  sbitmap_free (blocks_visited);
3942
}
3943
 
3944
 
3945
/* Return true if STMT is interesting for VRP.  */
3946
 
3947
static bool
3948
stmt_interesting_for_vrp (tree stmt)
3949
{
3950
  if (TREE_CODE (stmt) == PHI_NODE
3951
      && is_gimple_reg (PHI_RESULT (stmt))
3952
      && (INTEGRAL_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))
3953
          || POINTER_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))))
3954
    return true;
3955
  else if (TREE_CODE (stmt) == MODIFY_EXPR)
3956
    {
3957
      tree lhs = TREE_OPERAND (stmt, 0);
3958
      tree rhs = TREE_OPERAND (stmt, 1);
3959
 
3960
      /* In general, assignments with virtual operands are not useful
3961
         for deriving ranges, with the obvious exception of calls to
3962
         builtin functions.  */
3963
      if (TREE_CODE (lhs) == SSA_NAME
3964
          && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
3965
              || POINTER_TYPE_P (TREE_TYPE (lhs)))
3966
          && ((TREE_CODE (rhs) == CALL_EXPR
3967
               && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
3968
               && DECL_P (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))
3969
               && DECL_IS_BUILTIN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)))
3970
              || ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS)))
3971
        return true;
3972
    }
3973
  else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
3974
    return true;
3975
 
3976
  return false;
3977
}
3978
 
3979
 
3980
/* Initialize local data structures for VRP.  */
3981
 
3982
static void
3983
vrp_initialize (void)
3984
{
3985
  basic_block bb;
3986
 
3987
  vr_value = XNEWVEC (value_range_t *, num_ssa_names);
3988
  memset (vr_value, 0, num_ssa_names * sizeof (value_range_t *));
3989
 
3990
  FOR_EACH_BB (bb)
3991
    {
3992
      block_stmt_iterator si;
3993
      tree phi;
3994
 
3995
      for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3996
        {
3997
          if (!stmt_interesting_for_vrp (phi))
3998
            {
3999
              tree lhs = PHI_RESULT (phi);
4000
              set_value_range_to_varying (get_value_range (lhs));
4001
              DONT_SIMULATE_AGAIN (phi) = true;
4002
            }
4003
          else
4004
            DONT_SIMULATE_AGAIN (phi) = false;
4005
        }
4006
 
4007
      for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
4008
        {
4009
          tree stmt = bsi_stmt (si);
4010
 
4011
          if (!stmt_interesting_for_vrp (stmt))
4012
            {
4013
              ssa_op_iter i;
4014
              tree def;
4015
              FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
4016
                set_value_range_to_varying (get_value_range (def));
4017
              DONT_SIMULATE_AGAIN (stmt) = true;
4018
            }
4019
          else
4020
            {
4021
              DONT_SIMULATE_AGAIN (stmt) = false;
4022
            }
4023
        }
4024
    }
4025
}
4026
 
4027
 
4028
/* Visit assignment STMT.  If it produces an interesting range, record
4029
   the SSA name in *OUTPUT_P.  */
4030
 
4031
static enum ssa_prop_result
4032
vrp_visit_assignment (tree stmt, tree *output_p)
4033
{
4034
  tree lhs, rhs, def;
4035
  ssa_op_iter iter;
4036
 
4037
  lhs = TREE_OPERAND (stmt, 0);
4038
  rhs = TREE_OPERAND (stmt, 1);
4039
 
4040
  /* We only keep track of ranges in integral and pointer types.  */
4041
  if (TREE_CODE (lhs) == SSA_NAME
4042
      && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
4043
           /* It is valid to have NULL MIN/MAX values on a type.  See
4044
              build_range_type.  */
4045
           && TYPE_MIN_VALUE (TREE_TYPE (lhs))
4046
           && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
4047
          || POINTER_TYPE_P (TREE_TYPE (lhs))))
4048
    {
4049
      struct loop *l;
4050
      value_range_t new_vr = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
4051
 
4052
      extract_range_from_expr (&new_vr, rhs);
4053
 
4054
      /* If STMT is inside a loop, we may be able to know something
4055
         else about the range of LHS by examining scalar evolution
4056
         information.  */
4057
      if (current_loops && (l = loop_containing_stmt (stmt)))
4058
        adjust_range_with_scev (&new_vr, l, stmt, lhs);
4059
 
4060
      if (update_value_range (lhs, &new_vr))
4061
        {
4062
          *output_p = lhs;
4063
 
4064
          if (dump_file && (dump_flags & TDF_DETAILS))
4065
            {
4066
              fprintf (dump_file, "Found new range for ");
4067
              print_generic_expr (dump_file, lhs, 0);
4068
              fprintf (dump_file, ": ");
4069
              dump_value_range (dump_file, &new_vr);
4070
              fprintf (dump_file, "\n\n");
4071
            }
4072
 
4073
          if (new_vr.type == VR_VARYING)
4074
            return SSA_PROP_VARYING;
4075
 
4076
          return SSA_PROP_INTERESTING;
4077
        }
4078
 
4079
      return SSA_PROP_NOT_INTERESTING;
4080
    }
4081
 
4082
  /* Every other statement produces no useful ranges.  */
4083
  FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
4084
    set_value_range_to_varying (get_value_range (def));
4085
 
4086
  return SSA_PROP_VARYING;
4087
}
4088
 
4089
 
4090
/* Compare all the value ranges for names equivalent to VAR with VAL
4091
   using comparison code COMP.  Return the same value returned by
4092
   compare_range_with_value, including the setting of
4093
   *STRICT_OVERFLOW_P.  */
4094
 
4095
static tree
4096
compare_name_with_value (enum tree_code comp, tree var, tree val,
4097
                         bool *strict_overflow_p)
4098
{
4099
  bitmap_iterator bi;
4100
  unsigned i;
4101
  bitmap e;
4102
  tree retval, t;
4103
  int used_strict_overflow;
4104
 
4105
  t = retval = NULL_TREE;
4106
 
4107
  /* Get the set of equivalences for VAR.  */
4108
  e = get_value_range (var)->equiv;
4109
 
4110
  /* Add VAR to its own set of equivalences so that VAR's value range
4111
     is processed by this loop (otherwise, we would have to replicate
4112
     the body of the loop just to check VAR's value range).  */
4113
  bitmap_set_bit (e, SSA_NAME_VERSION (var));
4114
 
4115
  /* Start at -1.  Set it to 0 if we do a comparison without relying
4116
     on overflow, or 1 if all comparisons rely on overflow.  */
4117
  used_strict_overflow = -1;
4118
 
4119
  EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
4120
    {
4121
      bool sop;
4122
 
4123
      value_range_t equiv_vr = *(vr_value[i]);
4124
 
4125
      /* If name N_i does not have a valid range, use N_i as its own
4126
         range.  This allows us to compare against names that may
4127
         have N_i in their ranges.  */
4128
      if (equiv_vr.type == VR_VARYING || equiv_vr.type == VR_UNDEFINED)
4129
        {
4130
          equiv_vr.type = VR_RANGE;
4131
          equiv_vr.min = ssa_name (i);
4132
          equiv_vr.max = ssa_name (i);
4133
        }
4134
 
4135
      sop = false;
4136
      t = compare_range_with_value (comp, &equiv_vr, val, &sop);
4137
      if (t)
4138
        {
4139
          /* If we get different answers from different members
4140
             of the equivalence set this check must be in a dead
4141
             code region.  Folding it to a trap representation
4142
             would be correct here.  For now just return don't-know.  */
4143
          if (retval != NULL
4144
              && t != retval)
4145
            {
4146
              retval = NULL_TREE;
4147
              break;
4148
            }
4149
          retval = t;
4150
 
4151
          if (!sop)
4152
            used_strict_overflow = 0;
4153
          else if (used_strict_overflow < 0)
4154
            used_strict_overflow = 1;
4155
        }
4156
    }
4157
 
4158
  /* Remove VAR from its own equivalence set.  */
4159
  bitmap_clear_bit (e, SSA_NAME_VERSION (var));
4160
 
4161
  if (retval)
4162
    {
4163
      if (used_strict_overflow > 0)
4164
        *strict_overflow_p = true;
4165
      return retval;
4166
    }
4167
 
4168
  /* We couldn't find a non-NULL value for the predicate.  */
4169
  return NULL_TREE;
4170
}
4171
 
4172
 
4173
/* Given a comparison code COMP and names N1 and N2, compare all the
4174
   ranges equivalent to N1 against all the ranges equivalent to N2
4175
   to determine the value of N1 COMP N2.  Return the same value
4176
   returned by compare_ranges.  Set *STRICT_OVERFLOW_P to indicate
4177
   whether we relied on an overflow infinity in the comparison.  */
4178
 
4179
 
4180
static tree
4181
compare_names (enum tree_code comp, tree n1, tree n2,
4182
               bool *strict_overflow_p)
4183
{
4184
  tree t, retval;
4185
  bitmap e1, e2;
4186
  bitmap_iterator bi1, bi2;
4187
  unsigned i1, i2;
4188
  int used_strict_overflow;
4189
 
4190
  /* Compare the ranges of every name equivalent to N1 against the
4191
     ranges of every name equivalent to N2.  */
4192
  e1 = get_value_range (n1)->equiv;
4193
  e2 = get_value_range (n2)->equiv;
4194
 
4195
  /* Add N1 and N2 to their own set of equivalences to avoid
4196
     duplicating the body of the loop just to check N1 and N2
4197
     ranges.  */
4198
  bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
4199
  bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
4200
 
4201
  /* If the equivalence sets have a common intersection, then the two
4202
     names can be compared without checking their ranges.  */
4203
  if (bitmap_intersect_p (e1, e2))
4204
    {
4205
      bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4206
      bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4207
 
4208
      return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
4209
             ? boolean_true_node
4210
             : boolean_false_node;
4211
    }
4212
 
4213
  /* Start at -1.  Set it to 0 if we do a comparison without relying
4214
     on overflow, or 1 if all comparisons rely on overflow.  */
4215
  used_strict_overflow = -1;
4216
 
4217
  /* Otherwise, compare all the equivalent ranges.  First, add N1 and
4218
     N2 to their own set of equivalences to avoid duplicating the body
4219
     of the loop just to check N1 and N2 ranges.  */
4220
  EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
4221
    {
4222
      value_range_t vr1 = *(vr_value[i1]);
4223
 
4224
      /* If the range is VARYING or UNDEFINED, use the name itself.  */
4225
      if (vr1.type == VR_VARYING || vr1.type == VR_UNDEFINED)
4226
        {
4227
          vr1.type = VR_RANGE;
4228
          vr1.min = ssa_name (i1);
4229
          vr1.max = ssa_name (i1);
4230
        }
4231
 
4232
      t = retval = NULL_TREE;
4233
      EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
4234
        {
4235
          bool sop = false;
4236
 
4237
          value_range_t vr2 = *(vr_value[i2]);
4238
 
4239
          if (vr2.type == VR_VARYING || vr2.type == VR_UNDEFINED)
4240
            {
4241
              vr2.type = VR_RANGE;
4242
              vr2.min = ssa_name (i2);
4243
              vr2.max = ssa_name (i2);
4244
            }
4245
 
4246
          t = compare_ranges (comp, &vr1, &vr2, &sop);
4247
          if (t)
4248
            {
4249
              /* If we get different answers from different members
4250
                 of the equivalence set this check must be in a dead
4251
                 code region.  Folding it to a trap representation
4252
                 would be correct here.  For now just return don't-know.  */
4253
              if (retval != NULL
4254
                  && t != retval)
4255
                {
4256
                  bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4257
                  bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4258
                  return NULL_TREE;
4259
                }
4260
              retval = t;
4261
 
4262
              if (!sop)
4263
                used_strict_overflow = 0;
4264
              else if (used_strict_overflow < 0)
4265
                used_strict_overflow = 1;
4266
            }
4267
        }
4268
 
4269
      if (retval)
4270
        {
4271
          bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4272
          bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4273
          if (used_strict_overflow > 0)
4274
            *strict_overflow_p = true;
4275
          return retval;
4276
        }
4277
    }
4278
 
4279
  /* None of the equivalent ranges are useful in computing this
4280
     comparison.  */
4281
  bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4282
  bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4283
  return NULL_TREE;
4284
}
4285
 
4286
 
4287
/* Given a conditional predicate COND, try to determine if COND yields
4288
   true or false based on the value ranges of its operands.  Return
4289
   BOOLEAN_TRUE_NODE if the conditional always evaluates to true,
4290
   BOOLEAN_FALSE_NODE if the conditional always evaluates to false, and,
4291
   NULL if the conditional cannot be evaluated at compile time.
4292
 
4293
   If USE_EQUIV_P is true, the ranges of all the names equivalent with
4294
   the operands in COND are used when trying to compute its value.
4295
   This is only used during final substitution.  During propagation,
4296
   we only check the range of each variable and not its equivalents.
4297
 
4298
   Set *STRICT_OVERFLOW_P to indicate whether we relied on an overflow
4299
   infinity to produce the result.  */
4300
 
4301
static tree
4302
vrp_evaluate_conditional_warnv (tree cond, bool use_equiv_p,
4303
                                bool *strict_overflow_p)
4304
{
4305
  gcc_assert (TREE_CODE (cond) == SSA_NAME
4306
              || TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison);
4307
 
4308
  if (TREE_CODE (cond) == SSA_NAME)
4309
    {
4310
      value_range_t *vr;
4311
      tree retval;
4312
 
4313
      if (use_equiv_p)
4314
        retval = compare_name_with_value (NE_EXPR, cond, boolean_false_node,
4315
                                          strict_overflow_p);
4316
      else
4317
        {
4318
          value_range_t *vr = get_value_range (cond);
4319
          retval = compare_range_with_value (NE_EXPR, vr, boolean_false_node,
4320
                                             strict_overflow_p);
4321
        }
4322
 
4323
      /* If COND has a known boolean range, return it.  */
4324
      if (retval)
4325
        return retval;
4326
 
4327
      /* Otherwise, if COND has a symbolic range of exactly one value,
4328
         return it.  */
4329
      vr = get_value_range (cond);
4330
      if (vr->type == VR_RANGE && vr->min == vr->max)
4331
        return vr->min;
4332
    }
4333
  else
4334
    {
4335
      tree op0 = TREE_OPERAND (cond, 0);
4336
      tree op1 = TREE_OPERAND (cond, 1);
4337
 
4338
      /* We only deal with integral and pointer types.  */
4339
      if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
4340
          && !POINTER_TYPE_P (TREE_TYPE (op0)))
4341
        return NULL_TREE;
4342
 
4343
      if (use_equiv_p)
4344
        {
4345
          if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
4346
            return compare_names (TREE_CODE (cond), op0, op1,
4347
                                  strict_overflow_p);
4348
          else if (TREE_CODE (op0) == SSA_NAME)
4349
            return compare_name_with_value (TREE_CODE (cond), op0, op1,
4350
                                            strict_overflow_p);
4351
          else if (TREE_CODE (op1) == SSA_NAME)
4352
            return (compare_name_with_value
4353
                    (swap_tree_comparison (TREE_CODE (cond)), op1, op0,
4354
                     strict_overflow_p));
4355
        }
4356
      else
4357
        {
4358
          value_range_t *vr0, *vr1;
4359
 
4360
          vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
4361
          vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
4362
 
4363
          if (vr0 && vr1)
4364
            return compare_ranges (TREE_CODE (cond), vr0, vr1,
4365
                                   strict_overflow_p);
4366
          else if (vr0 && vr1 == NULL)
4367
            return compare_range_with_value (TREE_CODE (cond), vr0, op1,
4368
                                             strict_overflow_p);
4369
          else if (vr0 == NULL && vr1)
4370
            return (compare_range_with_value
4371
                    (swap_tree_comparison (TREE_CODE (cond)), vr1, op0,
4372
                     strict_overflow_p));
4373
        }
4374
    }
4375
 
4376
  /* Anything else cannot be computed statically.  */
4377
  return NULL_TREE;
4378
}
4379
 
4380
/* Given COND within STMT, try to simplify it based on value range
4381
   information.  Return NULL if the conditional can not be evaluated.
4382
   The ranges of all the names equivalent with the operands in COND
4383
   will be used when trying to compute the value.  If the result is
4384
   based on undefined signed overflow, issue a warning if
4385
   appropriate.  */
4386
 
4387
tree
4388
vrp_evaluate_conditional (tree cond, tree stmt)
4389
{
4390
  bool sop;
4391
  tree ret;
4392
 
4393
  sop = false;
4394
  ret = vrp_evaluate_conditional_warnv (cond, true, &sop);
4395
 
4396
  if (ret && sop)
4397
    {
4398
      enum warn_strict_overflow_code wc;
4399
      const char* warnmsg;
4400
 
4401
      if (is_gimple_min_invariant (ret))
4402
        {
4403
          wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
4404
          warnmsg = G_("assuming signed overflow does not occur when "
4405
                       "simplifying conditional to constant");
4406
        }
4407
      else
4408
        {
4409
          wc = WARN_STRICT_OVERFLOW_COMPARISON;
4410
          warnmsg = G_("assuming signed overflow does not occur when "
4411
                       "simplifying conditional");
4412
        }
4413
 
4414
      if (issue_strict_overflow_warning (wc))
4415
        {
4416
          location_t locus;
4417
 
4418
          if (!EXPR_HAS_LOCATION (stmt))
4419
            locus = input_location;
4420
          else
4421
            locus = EXPR_LOCATION (stmt);
4422
          warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
4423
        }
4424
    }
4425
 
4426
  return ret;
4427
}
4428
 
4429
 
4430
/* Visit conditional statement STMT.  If we can determine which edge
4431
   will be taken out of STMT's basic block, record it in
4432
   *TAKEN_EDGE_P and return SSA_PROP_INTERESTING.  Otherwise, return
4433
   SSA_PROP_VARYING.  */
4434
 
4435
static enum ssa_prop_result
4436
vrp_visit_cond_stmt (tree stmt, edge *taken_edge_p)
4437
{
4438
  tree cond, val;
4439
  bool sop;
4440
 
4441
  *taken_edge_p = NULL;
4442
 
4443
  /* FIXME.  Handle SWITCH_EXPRs.  But first, the assert pass needs to
4444
     add ASSERT_EXPRs for them.  */
4445
  if (TREE_CODE (stmt) == SWITCH_EXPR)
4446
    return SSA_PROP_VARYING;
4447
 
4448
  cond = COND_EXPR_COND (stmt);
4449
 
4450
  if (dump_file && (dump_flags & TDF_DETAILS))
4451
    {
4452
      tree use;
4453
      ssa_op_iter i;
4454
 
4455
      fprintf (dump_file, "\nVisiting conditional with predicate: ");
4456
      print_generic_expr (dump_file, cond, 0);
4457
      fprintf (dump_file, "\nWith known ranges\n");
4458
 
4459
      FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
4460
        {
4461
          fprintf (dump_file, "\t");
4462
          print_generic_expr (dump_file, use, 0);
4463
          fprintf (dump_file, ": ");
4464
          dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
4465
        }
4466
 
4467
      fprintf (dump_file, "\n");
4468
    }
4469
 
4470
  /* Compute the value of the predicate COND by checking the known
4471
     ranges of each of its operands.
4472
 
4473
     Note that we cannot evaluate all the equivalent ranges here
4474
     because those ranges may not yet be final and with the current
4475
     propagation strategy, we cannot determine when the value ranges
4476
     of the names in the equivalence set have changed.
4477
 
4478
     For instance, given the following code fragment
4479
 
4480
        i_5 = PHI <8, i_13>
4481
        ...
4482
        i_14 = ASSERT_EXPR <i_5, i_5 != 0>
4483
        if (i_14 == 1)
4484
          ...
4485
 
4486
     Assume that on the first visit to i_14, i_5 has the temporary
4487
     range [8, 8] because the second argument to the PHI function is
4488
     not yet executable.  We derive the range ~[0, 0] for i_14 and the
4489
     equivalence set { i_5 }.  So, when we visit 'if (i_14 == 1)' for
4490
     the first time, since i_14 is equivalent to the range [8, 8], we
4491
     determine that the predicate is always false.
4492
 
4493
     On the next round of propagation, i_13 is determined to be
4494
     VARYING, which causes i_5 to drop down to VARYING.  So, another
4495
     visit to i_14 is scheduled.  In this second visit, we compute the
4496
     exact same range and equivalence set for i_14, namely ~[0, 0] and
4497
     { i_5 }.  But we did not have the previous range for i_5
4498
     registered, so vrp_visit_assignment thinks that the range for
4499
     i_14 has not changed.  Therefore, the predicate 'if (i_14 == 1)'
4500
     is not visited again, which stops propagation from visiting
4501
     statements in the THEN clause of that if().
4502
 
4503
     To properly fix this we would need to keep the previous range
4504
     value for the names in the equivalence set.  This way we would've
4505
     discovered that from one visit to the other i_5 changed from
4506
     range [8, 8] to VR_VARYING.
4507
 
4508
     However, fixing this apparent limitation may not be worth the
4509
     additional checking.  Testing on several code bases (GCC, DLV,
4510
     MICO, TRAMP3D and SPEC2000) showed that doing this results in
4511
     4 more predicates folded in SPEC.  */
4512
  sop = false;
4513
  val = vrp_evaluate_conditional_warnv (cond, false, &sop);
4514
  if (val)
4515
    {
4516
      if (!sop)
4517
        *taken_edge_p = find_taken_edge (bb_for_stmt (stmt), val);
4518
      else
4519
        {
4520
          if (dump_file && (dump_flags & TDF_DETAILS))
4521
            fprintf (dump_file,
4522
                     "\nIgnoring predicate evaluation because "
4523
                     "it assumes that signed overflow is undefined");
4524
          val = NULL_TREE;
4525
        }
4526
    }
4527
 
4528
  if (dump_file && (dump_flags & TDF_DETAILS))
4529
    {
4530
      fprintf (dump_file, "\nPredicate evaluates to: ");
4531
      if (val == NULL_TREE)
4532
        fprintf (dump_file, "DON'T KNOW\n");
4533
      else
4534
        print_generic_stmt (dump_file, val, 0);
4535
    }
4536
 
4537
  return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
4538
}
4539
 
4540
 
4541
/* Evaluate statement STMT.  If the statement produces a useful range,
4542
   return SSA_PROP_INTERESTING and record the SSA name with the
4543
   interesting range into *OUTPUT_P.
4544
 
4545
   If STMT is a conditional branch and we can determine its truth
4546
   value, the taken edge is recorded in *TAKEN_EDGE_P.
4547
 
4548
   If STMT produces a varying value, return SSA_PROP_VARYING.  */
4549
 
4550
static enum ssa_prop_result
4551
vrp_visit_stmt (tree stmt, edge *taken_edge_p, tree *output_p)
4552
{
4553
  tree def;
4554
  ssa_op_iter iter;
4555
  stmt_ann_t ann;
4556
 
4557
  if (dump_file && (dump_flags & TDF_DETAILS))
4558
    {
4559
      fprintf (dump_file, "\nVisiting statement:\n");
4560
      print_generic_stmt (dump_file, stmt, dump_flags);
4561
      fprintf (dump_file, "\n");
4562
    }
4563
 
4564
  ann = stmt_ann (stmt);
4565
  if (TREE_CODE (stmt) == MODIFY_EXPR)
4566
    {
4567
      tree rhs = TREE_OPERAND (stmt, 1);
4568
 
4569
      /* In general, assignments with virtual operands are not useful
4570
         for deriving ranges, with the obvious exception of calls to
4571
         builtin functions.  */
4572
      if ((TREE_CODE (rhs) == CALL_EXPR
4573
           && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
4574
           && DECL_P (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))
4575
           && DECL_IS_BUILTIN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)))
4576
          || ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
4577
        return vrp_visit_assignment (stmt, output_p);
4578
    }
4579
  else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
4580
    return vrp_visit_cond_stmt (stmt, taken_edge_p);
4581
 
4582
  /* All other statements produce nothing of interest for VRP, so mark
4583
     their outputs varying and prevent further simulation.  */
4584
  FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
4585
    set_value_range_to_varying (get_value_range (def));
4586
 
4587
  return SSA_PROP_VARYING;
4588
}
4589
 
4590
 
4591
/* Meet operation for value ranges.  Given two value ranges VR0 and
4592
   VR1, store in VR0 the result of meeting VR0 and VR1.
4593
 
4594
   The meeting rules are as follows:
4595
 
4596
   1- If VR0 and VR1 have an empty intersection, set VR0 to VR_VARYING.
4597
 
4598
   2- If VR0 and VR1 have a non-empty intersection, set VR0 to the
4599
      union of VR0 and VR1.  */
4600
 
4601
static void
4602
vrp_meet (value_range_t *vr0, value_range_t *vr1)
4603
{
4604
  if (vr0->type == VR_UNDEFINED)
4605
    {
4606
      copy_value_range (vr0, vr1);
4607
      return;
4608
    }
4609
 
4610
  if (vr1->type == VR_UNDEFINED)
4611
    {
4612
      /* Nothing to do.  VR0 already has the resulting range.  */
4613
      return;
4614
    }
4615
 
4616
  if (vr0->type == VR_VARYING)
4617
    {
4618
      /* Nothing to do.  VR0 already has the resulting range.  */
4619
      return;
4620
    }
4621
 
4622
  if (vr1->type == VR_VARYING)
4623
    {
4624
      set_value_range_to_varying (vr0);
4625
      return;
4626
    }
4627
 
4628
  if (vr0->type == VR_RANGE && vr1->type == VR_RANGE)
4629
    {
4630
      /* If VR0 and VR1 have a non-empty intersection, compute the
4631
         union of both ranges.  */
4632
      if (value_ranges_intersect_p (vr0, vr1))
4633
        {
4634
          int cmp;
4635
          tree min, max;
4636
 
4637
          /* The lower limit of the new range is the minimum of the
4638
             two ranges.  If they cannot be compared, the result is
4639
             VARYING.  */
4640
          cmp = compare_values (vr0->min, vr1->min);
4641
          if (cmp == 0 || cmp == 1)
4642
            min = vr1->min;
4643
          else if (cmp == -1)
4644
            min = vr0->min;
4645
          else
4646
            {
4647
              set_value_range_to_varying (vr0);
4648
              return;
4649
            }
4650
 
4651
          /* Similarly, the upper limit of the new range is the
4652
             maximum of the two ranges.  If they cannot be compared,
4653
             the result is VARYING.  */
4654
          cmp = compare_values (vr0->max, vr1->max);
4655
          if (cmp == 0 || cmp == -1)
4656
            max = vr1->max;
4657
          else if (cmp == 1)
4658
            max = vr0->max;
4659
          else
4660
            {
4661
              set_value_range_to_varying (vr0);
4662
              return;
4663
            }
4664
 
4665
          /* Check for useless ranges.  */
4666
          if (INTEGRAL_TYPE_P (TREE_TYPE (min))
4667
              && ((vrp_val_is_min (min) || is_overflow_infinity (min))
4668
                  && (vrp_val_is_max (max) || is_overflow_infinity (max))))
4669
            {
4670
              set_value_range_to_varying (vr0);
4671
              return;
4672
            }
4673
 
4674
          /* The resulting set of equivalences is the intersection of
4675
             the two sets.  */
4676
          if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4677
            bitmap_and_into (vr0->equiv, vr1->equiv);
4678
          else if (vr0->equiv && !vr1->equiv)
4679
            bitmap_clear (vr0->equiv);
4680
 
4681
          set_value_range (vr0, vr0->type, min, max, vr0->equiv);
4682
        }
4683
      else
4684
        goto no_meet;
4685
    }
4686
  else if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4687
    {
4688
      /* Two anti-ranges meet only if they are both identical.  */
4689
      if (compare_values (vr0->min, vr1->min) == 0
4690
          && compare_values (vr0->max, vr1->max) == 0
4691
          && compare_values (vr0->min, vr0->max) == 0)
4692
        {
4693
          /* The resulting set of equivalences is the intersection of
4694
             the two sets.  */
4695
          if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4696
            bitmap_and_into (vr0->equiv, vr1->equiv);
4697
          else if (vr0->equiv && !vr1->equiv)
4698
            bitmap_clear (vr0->equiv);
4699
        }
4700
      else
4701
        goto no_meet;
4702
    }
4703
  else if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4704
    {
4705
      /* A numeric range [VAL1, VAL2] and an anti-range ~[VAL3, VAL4]
4706
         meet only if the ranges have an empty intersection.  The
4707
         result of the meet operation is the anti-range.  */
4708
      if (!symbolic_range_p (vr0)
4709
          && !symbolic_range_p (vr1)
4710
          && !value_ranges_intersect_p (vr0, vr1))
4711
        {
4712
          /* Copy most of VR1 into VR0.  Don't copy VR1's equivalence
4713
             set.  We need to compute the intersection of the two
4714
             equivalence sets.  */
4715
          if (vr1->type == VR_ANTI_RANGE)
4716
            set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr0->equiv);
4717
 
4718
          /* The resulting set of equivalences is the intersection of
4719
             the two sets.  */
4720
          if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4721
            bitmap_and_into (vr0->equiv, vr1->equiv);
4722
          else if (vr0->equiv && !vr1->equiv)
4723
            bitmap_clear (vr0->equiv);
4724
        }
4725
      else
4726
        goto no_meet;
4727
    }
4728
  else
4729
    gcc_unreachable ();
4730
 
4731
  return;
4732
 
4733
no_meet:
4734
  /* The two range VR0 and VR1 do not meet.  Before giving up and
4735
     setting the result to VARYING, see if we can at least derive a
4736
     useful anti-range.  FIXME, all this nonsense about distinguishing
4737
     anti-ranges from ranges is necessary because of the odd
4738
     semantics of range_includes_zero_p and friends.  */
4739
  if (!symbolic_range_p (vr0)
4740
      && ((vr0->type == VR_RANGE && !range_includes_zero_p (vr0))
4741
          || (vr0->type == VR_ANTI_RANGE && range_includes_zero_p (vr0)))
4742
      && !symbolic_range_p (vr1)
4743
      && ((vr1->type == VR_RANGE && !range_includes_zero_p (vr1))
4744
          || (vr1->type == VR_ANTI_RANGE && range_includes_zero_p (vr1))))
4745
    {
4746
      set_value_range_to_nonnull (vr0, TREE_TYPE (vr0->min));
4747
 
4748
      /* Since this meet operation did not result from the meeting of
4749
         two equivalent names, VR0 cannot have any equivalences.  */
4750
      if (vr0->equiv)
4751
        bitmap_clear (vr0->equiv);
4752
    }
4753
  else
4754
    set_value_range_to_varying (vr0);
4755
}
4756
 
4757
 
4758
/* Visit all arguments for PHI node PHI that flow through executable
4759
   edges.  If a valid value range can be derived from all the incoming
4760
   value ranges, set a new range for the LHS of PHI.  */
4761
 
4762
static enum ssa_prop_result
4763
vrp_visit_phi_node (tree phi)
4764
{
4765
  int i;
4766
  tree lhs = PHI_RESULT (phi);
4767
  value_range_t *lhs_vr = get_value_range (lhs);
4768
  value_range_t vr_result = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
4769
 
4770
  copy_value_range (&vr_result, lhs_vr);
4771
 
4772
  if (dump_file && (dump_flags & TDF_DETAILS))
4773
    {
4774
      fprintf (dump_file, "\nVisiting PHI node: ");
4775
      print_generic_expr (dump_file, phi, dump_flags);
4776
    }
4777
 
4778
  for (i = 0; i < PHI_NUM_ARGS (phi); i++)
4779
    {
4780
      edge e = PHI_ARG_EDGE (phi, i);
4781
 
4782
      if (dump_file && (dump_flags & TDF_DETAILS))
4783
        {
4784
          fprintf (dump_file,
4785
              "\n    Argument #%d (%d -> %d %sexecutable)\n",
4786
              i, e->src->index, e->dest->index,
4787
              (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
4788
        }
4789
 
4790
      if (e->flags & EDGE_EXECUTABLE)
4791
        {
4792
          tree arg = PHI_ARG_DEF (phi, i);
4793
          value_range_t vr_arg;
4794
 
4795
          if (TREE_CODE (arg) == SSA_NAME)
4796
            vr_arg = *(get_value_range (arg));
4797
          else
4798
            {
4799
              if (is_overflow_infinity (arg))
4800
                {
4801
                  arg = copy_node (arg);
4802
                  TREE_OVERFLOW (arg) = 0;
4803
                }
4804
 
4805
              vr_arg.type = VR_RANGE;
4806
              vr_arg.min = arg;
4807
              vr_arg.max = arg;
4808
              vr_arg.equiv = NULL;
4809
            }
4810
 
4811
          if (dump_file && (dump_flags & TDF_DETAILS))
4812
            {
4813
              fprintf (dump_file, "\t");
4814
              print_generic_expr (dump_file, arg, dump_flags);
4815
              fprintf (dump_file, "\n\tValue: ");
4816
              dump_value_range (dump_file, &vr_arg);
4817
              fprintf (dump_file, "\n");
4818
            }
4819
 
4820
          vrp_meet (&vr_result, &vr_arg);
4821
 
4822
          if (vr_result.type == VR_VARYING)
4823
            break;
4824
        }
4825
    }
4826
 
4827
  if (vr_result.type == VR_VARYING)
4828
    goto varying;
4829
 
4830
  /* To prevent infinite iterations in the algorithm, derive ranges
4831
     when the new value is slightly bigger or smaller than the
4832
     previous one.  */
4833
  if (lhs_vr->type == VR_RANGE && vr_result.type == VR_RANGE)
4834
    {
4835
      if (!POINTER_TYPE_P (TREE_TYPE (lhs)))
4836
        {
4837
          int cmp_min = compare_values (lhs_vr->min, vr_result.min);
4838
          int cmp_max = compare_values (lhs_vr->max, vr_result.max);
4839
 
4840
          /* If the new minimum is smaller or larger than the previous
4841
             one, go all the way to -INF.  In the first case, to avoid
4842
             iterating millions of times to reach -INF, and in the
4843
             other case to avoid infinite bouncing between different
4844
             minimums.  */
4845
          if (cmp_min > 0 || cmp_min < 0)
4846
            {
4847
              /* If we will end up with a (-INF, +INF) range, set it
4848
                 to VARYING.  */
4849
              if (vrp_val_is_max (vr_result.max))
4850
                goto varying;
4851
 
4852
              if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
4853
                  || !vrp_var_may_overflow (lhs, phi))
4854
                vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
4855
              else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
4856
                vr_result.min =
4857
                  negative_overflow_infinity (TREE_TYPE (vr_result.min));
4858
              else
4859
                goto varying;
4860
            }
4861
 
4862
          /* Similarly, if the new maximum is smaller or larger than
4863
             the previous one, go all the way to +INF.  */
4864
          if (cmp_max < 0 || cmp_max > 0)
4865
            {
4866
              /* If we will end up with a (-INF, +INF) range, set it
4867
                 to VARYING.  */
4868
              if (vrp_val_is_min (vr_result.min))
4869
                goto varying;
4870
 
4871
              if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
4872
                  || !vrp_var_may_overflow (lhs, phi))
4873
                vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
4874
              else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
4875
                vr_result.max =
4876
                  positive_overflow_infinity (TREE_TYPE (vr_result.max));
4877
              else
4878
                goto varying;
4879
            }
4880
        }
4881
    }
4882
 
4883
  /* If the new range is different than the previous value, keep
4884
     iterating.  */
4885
  if (update_value_range (lhs, &vr_result))
4886
    return SSA_PROP_INTERESTING;
4887
 
4888
  /* Nothing changed, don't add outgoing edges.  */
4889
  return SSA_PROP_NOT_INTERESTING;
4890
 
4891
  /* No match found.  Set the LHS to VARYING.  */
4892
varying:
4893
  set_value_range_to_varying (lhs_vr);
4894
  return SSA_PROP_VARYING;
4895
}
4896
 
4897
/* Simplify a division or modulo operator to a right shift or
4898
   bitwise and if the first operand is unsigned or is greater
4899
   than zero and the second operand is an exact power of two.  */
4900
 
4901
static void
4902
simplify_div_or_mod_using_ranges (tree stmt, tree rhs, enum tree_code rhs_code)
4903
{
4904
  tree val = NULL;
4905
  tree op = TREE_OPERAND (rhs, 0);
4906
  value_range_t *vr = get_value_range (TREE_OPERAND (rhs, 0));
4907
 
4908
  if (TYPE_UNSIGNED (TREE_TYPE (op)))
4909
    {
4910
      val = integer_one_node;
4911
    }
4912
  else
4913
    {
4914
      bool sop = false;
4915
 
4916
      val = compare_range_with_value (GT_EXPR, vr, integer_zero_node, &sop);
4917
 
4918
      if (val
4919
          && sop
4920
          && integer_onep (val)
4921
          && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
4922
        {
4923
          location_t locus;
4924
 
4925
          if (!EXPR_HAS_LOCATION (stmt))
4926
            locus = input_location;
4927
          else
4928
            locus = EXPR_LOCATION (stmt);
4929
          warning (OPT_Wstrict_overflow,
4930
                   ("%Hassuming signed overflow does not occur when "
4931
                    "simplifying / or %% to >> or &"),
4932
                   &locus);
4933
        }
4934
    }
4935
 
4936
  if (val && integer_onep (val))
4937
    {
4938
      tree t;
4939
      tree op0 = TREE_OPERAND (rhs, 0);
4940
      tree op1 = TREE_OPERAND (rhs, 1);
4941
 
4942
      if (rhs_code == TRUNC_DIV_EXPR)
4943
        {
4944
          t = build_int_cst (NULL_TREE, tree_log2 (op1));
4945
          t = build2 (RSHIFT_EXPR, TREE_TYPE (op0), op0, t);
4946
        }
4947
      else
4948
        {
4949
          t = build_int_cst (TREE_TYPE (op1), 1);
4950
          t = int_const_binop (MINUS_EXPR, op1, t, 0);
4951
          t = fold_convert (TREE_TYPE (op0), t);
4952
          t = build2 (BIT_AND_EXPR, TREE_TYPE (op0), op0, t);
4953
        }
4954
 
4955
      TREE_OPERAND (stmt, 1) = t;
4956
      update_stmt (stmt);
4957
    }
4958
}
4959
 
4960
/* If the operand to an ABS_EXPR is >= 0, then eliminate the
4961
   ABS_EXPR.  If the operand is <= 0, then simplify the
4962
   ABS_EXPR into a NEGATE_EXPR.  */
4963
 
4964
static void
4965
simplify_abs_using_ranges (tree stmt, tree rhs)
4966
{
4967
  tree val = NULL;
4968
  tree op = TREE_OPERAND (rhs, 0);
4969
  tree type = TREE_TYPE (op);
4970
  value_range_t *vr = get_value_range (TREE_OPERAND (rhs, 0));
4971
 
4972
  if (TYPE_UNSIGNED (type))
4973
    {
4974
      val = integer_zero_node;
4975
    }
4976
  else if (vr)
4977
    {
4978
      bool sop = false;
4979
 
4980
      val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
4981
      if (!val)
4982
        {
4983
          sop = false;
4984
          val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
4985
                                          &sop);
4986
 
4987
          if (val)
4988
            {
4989
              if (integer_zerop (val))
4990
                val = integer_one_node;
4991
              else if (integer_onep (val))
4992
                val = integer_zero_node;
4993
            }
4994
        }
4995
 
4996
      if (val
4997
          && (integer_onep (val) || integer_zerop (val)))
4998
        {
4999
          tree t;
5000
 
5001
          if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
5002
            {
5003
              location_t locus;
5004
 
5005
              if (!EXPR_HAS_LOCATION (stmt))
5006
                locus = input_location;
5007
              else
5008
                locus = EXPR_LOCATION (stmt);
5009
              warning (OPT_Wstrict_overflow,
5010
                       ("%Hassuming signed overflow does not occur when "
5011
                        "simplifying abs (X) to X or -X"),
5012
                       &locus);
5013
            }
5014
 
5015
          if (integer_onep (val))
5016
            t = build1 (NEGATE_EXPR, TREE_TYPE (op), op);
5017
          else
5018
            t = op;
5019
 
5020
          TREE_OPERAND (stmt, 1) = t;
5021
          update_stmt (stmt);
5022
        }
5023
    }
5024
}
5025
 
5026
/* We are comparing trees OP0 and OP1 using COND_CODE.  OP0 has
5027
   a known value range VR.
5028
 
5029
   If there is one and only one value which will satisfy the
5030
   conditional, then return that value.  Else return NULL.  */
5031
 
5032
static tree
5033
test_for_singularity (enum tree_code cond_code, tree op0,
5034
                      tree op1, value_range_t *vr)
5035
{
5036
  tree min = NULL;
5037
  tree max = NULL;
5038
 
5039
  /* Extract minimum/maximum values which satisfy the
5040
     the conditional as it was written.  */
5041
  if (cond_code == LE_EXPR || cond_code == LT_EXPR)
5042
    {
5043
      /* This should not be negative infinity; there is no overflow
5044
         here.  */
5045
      min = TYPE_MIN_VALUE (TREE_TYPE (op0));
5046
 
5047
      max = op1;
5048
      if (cond_code == LT_EXPR && !is_overflow_infinity (max))
5049
        {
5050
          tree one = build_int_cst (TREE_TYPE (op0), 1);
5051
          max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
5052
          if (EXPR_P (max))
5053
            TREE_NO_WARNING (max) = 1;
5054
        }
5055
    }
5056
  else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
5057
    {
5058
      /* This should not be positive infinity; there is no overflow
5059
         here.  */
5060
      max = TYPE_MAX_VALUE (TREE_TYPE (op0));
5061
 
5062
      min = op1;
5063
      if (cond_code == GT_EXPR && !is_overflow_infinity (min))
5064
        {
5065
          tree one = build_int_cst (TREE_TYPE (op0), 1);
5066
          min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
5067
          if (EXPR_P (min))
5068
            TREE_NO_WARNING (min) = 1;
5069
        }
5070
    }
5071
 
5072
  /* Now refine the minimum and maximum values using any
5073
     value range information we have for op0.  */
5074
  if (min && max)
5075
    {
5076
      if (compare_values (vr->min, min) == -1)
5077
        min = min;
5078
      else
5079
        min = vr->min;
5080
      if (compare_values (vr->max, max) == 1)
5081
        max = max;
5082
      else
5083
        max = vr->max;
5084
 
5085
      /* If the new min/max values have converged to a single value,
5086
         then there is only one value which can satisfy the condition,
5087
         return that value.  */
5088
      if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
5089
        return min;
5090
    }
5091
  return NULL;
5092
}
5093
 
5094
/* Simplify a conditional using a relational operator to an equality
5095
   test if the range information indicates only one value can satisfy
5096
   the original conditional.  */
5097
 
5098
static void
5099
simplify_cond_using_ranges (tree stmt)
5100
{
5101
  tree cond = COND_EXPR_COND (stmt);
5102
  tree op0 = TREE_OPERAND (cond, 0);
5103
  tree op1 = TREE_OPERAND (cond, 1);
5104
  enum tree_code cond_code = TREE_CODE (cond);
5105
 
5106
  if (cond_code != NE_EXPR
5107
      && cond_code != EQ_EXPR
5108
      && TREE_CODE (op0) == SSA_NAME
5109
      && INTEGRAL_TYPE_P (TREE_TYPE (op0))
5110
      && is_gimple_min_invariant (op1))
5111
    {
5112
      value_range_t *vr = get_value_range (op0);
5113
 
5114
      /* If we have range information for OP0, then we might be
5115
         able to simplify this conditional. */
5116
      if (vr->type == VR_RANGE)
5117
        {
5118
          tree new = test_for_singularity (cond_code, op0, op1, vr);
5119
 
5120
          if (new)
5121
            {
5122
              if (dump_file)
5123
                {
5124
                  fprintf (dump_file, "Simplified relational ");
5125
                  print_generic_expr (dump_file, cond, 0);
5126
                  fprintf (dump_file, " into ");
5127
                }
5128
 
5129
              COND_EXPR_COND (stmt)
5130
                = build2 (EQ_EXPR, boolean_type_node, op0, new);
5131
              update_stmt (stmt);
5132
 
5133
              if (dump_file)
5134
                {
5135
                  print_generic_expr (dump_file, COND_EXPR_COND (stmt), 0);
5136
                  fprintf (dump_file, "\n");
5137
                }
5138
              return;
5139
 
5140
            }
5141
 
5142
          /* Try again after inverting the condition.  We only deal
5143
             with integral types here, so no need to worry about
5144
             issues with inverting FP comparisons.  */
5145
          cond_code = invert_tree_comparison (cond_code, false);
5146
          new = test_for_singularity (cond_code, op0, op1, vr);
5147
 
5148
          if (new)
5149
            {
5150
              if (dump_file)
5151
                {
5152
                  fprintf (dump_file, "Simplified relational ");
5153
                  print_generic_expr (dump_file, cond, 0);
5154
                  fprintf (dump_file, " into ");
5155
                }
5156
 
5157
              COND_EXPR_COND (stmt)
5158
                = build2 (NE_EXPR, boolean_type_node, op0, new);
5159
              update_stmt (stmt);
5160
 
5161
              if (dump_file)
5162
                {
5163
                  print_generic_expr (dump_file, COND_EXPR_COND (stmt), 0);
5164
                  fprintf (dump_file, "\n");
5165
                }
5166
              return;
5167
 
5168
            }
5169
        }
5170
    }
5171
}
5172
 
5173
/* Simplify STMT using ranges if possible.  */
5174
 
5175
void
5176
simplify_stmt_using_ranges (tree stmt)
5177
{
5178
  if (TREE_CODE (stmt) == MODIFY_EXPR)
5179
    {
5180
      tree rhs = TREE_OPERAND (stmt, 1);
5181
      enum tree_code rhs_code = TREE_CODE (rhs);
5182
 
5183
      /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
5184
         and BIT_AND_EXPR respectively if the first operand is greater
5185
         than zero and the second operand is an exact power of two.  */
5186
      if ((rhs_code == TRUNC_DIV_EXPR || rhs_code == TRUNC_MOD_EXPR)
5187
          && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
5188
          && integer_pow2p (TREE_OPERAND (rhs, 1)))
5189
        simplify_div_or_mod_using_ranges (stmt, rhs, rhs_code);
5190
 
5191
      /* Transform ABS (X) into X or -X as appropriate.  */
5192
      if (rhs_code == ABS_EXPR
5193
          && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
5194
          && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0))))
5195
        simplify_abs_using_ranges (stmt, rhs);
5196
    }
5197
  else if (TREE_CODE (stmt) == COND_EXPR
5198
           && COMPARISON_CLASS_P (COND_EXPR_COND (stmt)))
5199
    {
5200
      simplify_cond_using_ranges (stmt);
5201
    }
5202
}
5203
 
5204
/* Stack of dest,src equivalency pairs that need to be restored after
5205
   each attempt to thread a block's incoming edge to an outgoing edge.
5206
 
5207
   A NULL entry is used to mark the end of pairs which need to be
5208
   restored.  */
5209
static VEC(tree,heap) *stack;
5210
 
5211
/* A trivial wrapper so that we can present the generic jump threading
5212
   code with a simple API for simplifying statements.  STMT is the
5213
   statement we want to simplify, WITHIN_STMT provides the location
5214
   for any overflow warnings.  */
5215
 
5216
static tree
5217
simplify_stmt_for_jump_threading (tree stmt, tree within_stmt)
5218
{
5219
  /* We only use VRP information to simplify conditionals.  This is
5220
     overly conservative, but it's unclear if doing more would be
5221
     worth the compile time cost.  */
5222
  if (TREE_CODE (stmt) != COND_EXPR)
5223
    return NULL;
5224
 
5225
  return vrp_evaluate_conditional (COND_EXPR_COND (stmt), within_stmt);
5226
}
5227
 
5228
/* Blocks which have more than one predecessor and more than
5229
   one successor present jump threading opportunities.  ie,
5230
   when the block is reached from a specific predecessor, we
5231
   may be able to determine which of the outgoing edges will
5232
   be traversed.  When this optimization applies, we are able
5233
   to avoid conditionals at runtime and we may expose secondary
5234
   optimization opportunities.
5235
 
5236
   This routine is effectively a driver for the generic jump
5237
   threading code.  It basically just presents the generic code
5238
   with edges that may be suitable for jump threading.
5239
 
5240
   Unlike DOM, we do not iterate VRP if jump threading was successful.
5241
   While iterating may expose new opportunities for VRP, it is expected
5242
   those opportunities would be very limited and the compile time cost
5243
   to expose those opportunities would be significant.
5244
 
5245
   As jump threading opportunities are discovered, they are registered
5246
   for later realization.  */
5247
 
5248
static void
5249
identify_jump_threads (void)
5250
{
5251
  basic_block bb;
5252
  tree dummy;
5253
 
5254
  /* Ugh.  When substituting values earlier in this pass we can
5255
     wipe the dominance information.  So rebuild the dominator
5256
     information as we need it within the jump threading code.  */
5257
  calculate_dominance_info (CDI_DOMINATORS);
5258
 
5259
  /* We do not allow VRP information to be used for jump threading
5260
     across a back edge in the CFG.  Otherwise it becomes too
5261
     difficult to avoid eliminating loop exit tests.  Of course
5262
     EDGE_DFS_BACK is not accurate at this time so we have to
5263
     recompute it.  */
5264
  mark_dfs_back_edges ();
5265
 
5266
  /* Allocate our unwinder stack to unwind any temporary equivalences
5267
     that might be recorded.  */
5268
  stack = VEC_alloc (tree, heap, 20);
5269
 
5270
  /* To avoid lots of silly node creation, we create a single
5271
     conditional and just modify it in-place when attempting to
5272
     thread jumps.  */
5273
  dummy = build2 (EQ_EXPR, boolean_type_node, NULL, NULL);
5274
  dummy = build3 (COND_EXPR, void_type_node, dummy, NULL, NULL);
5275
 
5276
  /* Walk through all the blocks finding those which present a
5277
     potential jump threading opportunity.  We could set this up
5278
     as a dominator walker and record data during the walk, but
5279
     I doubt it's worth the effort for the classes of jump
5280
     threading opportunities we are trying to identify at this
5281
     point in compilation.  */
5282
  FOR_EACH_BB (bb)
5283
    {
5284
      tree last, cond;
5285
 
5286
      /* If the generic jump threading code does not find this block
5287
         interesting, then there is nothing to do.  */
5288
      if (! potentially_threadable_block (bb))
5289
        continue;
5290
 
5291
      /* We only care about blocks ending in a COND_EXPR.  While there
5292
         may be some value in handling SWITCH_EXPR here, I doubt it's
5293
         terribly important.  */
5294
      last = bsi_stmt (bsi_last (bb));
5295
      if (TREE_CODE (last) != COND_EXPR)
5296
        continue;
5297
 
5298
      /* We're basically looking for any kind of conditional with
5299
         integral type arguments.  */
5300
      cond = COND_EXPR_COND (last);
5301
      if ((TREE_CODE (cond) == SSA_NAME
5302
           && INTEGRAL_TYPE_P (TREE_TYPE (cond)))
5303
          || (COMPARISON_CLASS_P (cond)
5304
              && TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME
5305
              && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 0)))
5306
              && (TREE_CODE (TREE_OPERAND (cond, 1)) == SSA_NAME
5307
                  || is_gimple_min_invariant (TREE_OPERAND (cond, 1)))
5308
              && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 1)))))
5309
        {
5310
          edge_iterator ei;
5311
          edge e;
5312
 
5313
          /* We've got a block with multiple predecessors and multiple
5314
             successors which also ends in a suitable conditional.  For
5315
             each predecessor, see if we can thread it to a specific
5316
             successor.  */
5317
          FOR_EACH_EDGE (e, ei, bb->preds)
5318
            {
5319
              /* Do not thread across back edges or abnormal edges
5320
                 in the CFG.  */
5321
              if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
5322
                continue;
5323
 
5324
              thread_across_edge (dummy, e, true,
5325
                                  &stack,
5326
                                  simplify_stmt_for_jump_threading);
5327
            }
5328
        }
5329
    }
5330
 
5331
  /* We do not actually update the CFG or SSA graphs at this point as
5332
     ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
5333
     handle ASSERT_EXPRs gracefully.  */
5334
}
5335
 
5336
/* We identified all the jump threading opportunities earlier, but could
5337
   not transform the CFG at that time.  This routine transforms the
5338
   CFG and arranges for the dominator tree to be rebuilt if necessary.
5339
 
5340
   Note the SSA graph update will occur during the normal TODO
5341
   processing by the pass manager.  */
5342
static void
5343
finalize_jump_threads (void)
5344
{
5345
  bool cfg_altered = false;
5346
  cfg_altered = thread_through_all_blocks ();
5347
 
5348
  /* If we threaded jumps, then we need to recompute the dominance
5349
     information, to safely do that we must clean up the CFG first.  */
5350
  if (cfg_altered)
5351
    {
5352
      free_dominance_info (CDI_DOMINATORS);
5353
      cleanup_tree_cfg ();
5354
      calculate_dominance_info (CDI_DOMINATORS);
5355
    }
5356
  VEC_free (tree, heap, stack);
5357
}
5358
 
5359
 
5360
/* Traverse all the blocks folding conditionals with known ranges.  */
5361
 
5362
static void
5363
vrp_finalize (void)
5364
{
5365
  size_t i;
5366
  prop_value_t *single_val_range;
5367
  bool do_value_subst_p;
5368
 
5369
  if (dump_file)
5370
    {
5371
      fprintf (dump_file, "\nValue ranges after VRP:\n\n");
5372
      dump_all_value_ranges (dump_file);
5373
      fprintf (dump_file, "\n");
5374
    }
5375
 
5376
  /* We may have ended with ranges that have exactly one value.  Those
5377
     values can be substituted as any other copy/const propagated
5378
     value using substitute_and_fold.  */
5379
  single_val_range = XNEWVEC (prop_value_t, num_ssa_names);
5380
  memset (single_val_range, 0, num_ssa_names * sizeof (*single_val_range));
5381
 
5382
  do_value_subst_p = false;
5383
  for (i = 0; i < num_ssa_names; i++)
5384
    if (vr_value[i]
5385
        && vr_value[i]->type == VR_RANGE
5386
        && vr_value[i]->min == vr_value[i]->max)
5387
      {
5388
        single_val_range[i].value = vr_value[i]->min;
5389
        do_value_subst_p = true;
5390
      }
5391
 
5392
  if (!do_value_subst_p)
5393
    {
5394
      /* We found no single-valued ranges, don't waste time trying to
5395
         do single value substitution in substitute_and_fold.  */
5396
      free (single_val_range);
5397
      single_val_range = NULL;
5398
    }
5399
 
5400
  substitute_and_fold (single_val_range, true);
5401
 
5402
  /* We must identify jump threading opportunities before we release
5403
     the datastructures built by VRP.  */
5404
  identify_jump_threads ();
5405
 
5406
  /* Free allocated memory.  */
5407
  for (i = 0; i < num_ssa_names; i++)
5408
    if (vr_value[i])
5409
      {
5410
        BITMAP_FREE (vr_value[i]->equiv);
5411
        free (vr_value[i]);
5412
      }
5413
 
5414
  free (single_val_range);
5415
  free (vr_value);
5416
 
5417
  /* So that we can distinguish between VRP data being available
5418
     and not available.  */
5419
  vr_value = NULL;
5420
}
5421
 
5422
 
5423
/* Main entry point to VRP (Value Range Propagation).  This pass is
5424
   loosely based on J. R. C. Patterson, ``Accurate Static Branch
5425
   Prediction by Value Range Propagation,'' in SIGPLAN Conference on
5426
   Programming Language Design and Implementation, pp. 67-78, 1995.
5427
   Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
5428
 
5429
   This is essentially an SSA-CCP pass modified to deal with ranges
5430
   instead of constants.
5431
 
5432
   While propagating ranges, we may find that two or more SSA name
5433
   have equivalent, though distinct ranges.  For instance,
5434
 
5435
     1  x_9 = p_3->a;
5436
     2  p_4 = ASSERT_EXPR <p_3, p_3 != 0>
5437
     3  if (p_4 == q_2)
5438
     4    p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
5439
     5  endif
5440
     6  if (q_2)
5441
 
5442
   In the code above, pointer p_5 has range [q_2, q_2], but from the
5443
   code we can also determine that p_5 cannot be NULL and, if q_2 had
5444
   a non-varying range, p_5's range should also be compatible with it.
5445
 
5446
   These equivalences are created by two expressions: ASSERT_EXPR and
5447
   copy operations.  Since p_5 is an assertion on p_4, and p_4 was the
5448
   result of another assertion, then we can use the fact that p_5 and
5449
   p_4 are equivalent when evaluating p_5's range.
5450
 
5451
   Together with value ranges, we also propagate these equivalences
5452
   between names so that we can take advantage of information from
5453
   multiple ranges when doing final replacement.  Note that this
5454
   equivalency relation is transitive but not symmetric.
5455
 
5456
   In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
5457
   cannot assert that q_2 is equivalent to p_5 because q_2 may be used
5458
   in contexts where that assertion does not hold (e.g., in line 6).
5459
 
5460
   TODO, the main difference between this pass and Patterson's is that
5461
   we do not propagate edge probabilities.  We only compute whether
5462
   edges can be taken or not.  That is, instead of having a spectrum
5463
   of jump probabilities between 0 and 1, we only deal with 0, 1 and
5464
   DON'T KNOW.  In the future, it may be worthwhile to propagate
5465
   probabilities to aid branch prediction.  */
5466
 
5467
static unsigned int
5468
execute_vrp (void)
5469
{
5470
  insert_range_assertions ();
5471
 
5472
  current_loops = loop_optimizer_init (LOOPS_NORMAL);
5473
  if (current_loops)
5474
    scev_initialize (current_loops);
5475
 
5476
  vrp_initialize ();
5477
  ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
5478
  vrp_finalize ();
5479
 
5480
  if (current_loops)
5481
    {
5482
      scev_finalize ();
5483
      loop_optimizer_finalize (current_loops);
5484
      current_loops = NULL;
5485
    }
5486
 
5487
  /* ASSERT_EXPRs must be removed before finalizing jump threads
5488
     as finalizing jump threads calls the CFG cleanup code which
5489
     does not properly handle ASSERT_EXPRs.  */
5490
  remove_range_assertions ();
5491
 
5492
  /* If we exposed any new variables, go ahead and put them into
5493
     SSA form now, before we handle jump threading.  This simplifies
5494
     interactions between rewriting of _DECL nodes into SSA form
5495
     and rewriting SSA_NAME nodes into SSA form after block
5496
     duplication and CFG manipulation.  */
5497
  update_ssa (TODO_update_ssa);
5498
 
5499
  finalize_jump_threads ();
5500
  return 0;
5501
}
5502
 
5503
static bool
5504
gate_vrp (void)
5505
{
5506
  return flag_tree_vrp != 0;
5507
}
5508
 
5509
struct tree_opt_pass pass_vrp =
5510
{
5511
  "vrp",                                /* name */
5512
  gate_vrp,                             /* gate */
5513
  execute_vrp,                          /* execute */
5514
  NULL,                                 /* sub */
5515
  NULL,                                 /* next */
5516
  0,                                     /* static_pass_number */
5517
  TV_TREE_VRP,                          /* tv_id */
5518
  PROP_ssa | PROP_alias,                /* properties_required */
5519
  0,                                     /* properties_provided */
5520
  PROP_smt_usage,                       /* properties_destroyed */
5521
  0,                                     /* todo_flags_start */
5522
  TODO_cleanup_cfg
5523
    | TODO_ggc_collect
5524
    | TODO_verify_ssa
5525
    | TODO_dump_func
5526
    | TODO_update_ssa
5527
    | TODO_update_smt_usage,                    /* todo_flags_finish */
5528
 
5529
};

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.