toolchain/gcc: replace revert with upstream fix
[openwrt/staging/jow.git] / toolchain / gcc / patches / 11.2.0 / 001-v11.3.0-ranger-Fix-up-fold_using_range-range_of_address-PR10.patch
1 From a6219e8e0719b14f474b0dcaa7bde2f4e57474f9 Mon Sep 17 00:00:00 2001
2 From: Jakub Jelinek <jakub@redhat.com>
3 Date: Wed, 17 Nov 2021 13:45:53 +0100
4 Subject: [PATCH] ranger: Fix up fold_using_range::range_of_address [PR103255]
5
6 If on &base->member the offset isn't constant or isn't zero and
7 -fdelete-null-pointer-checks and not -fwrapv-pointer and base has a range
8 that doesn't include NULL, we return the range of the base.
9 Usually it isn't a big deal, because for most pointers we just use
10 varying, range_zero and range_nonzero ranges and nothing beyond that,
11 but if a pointer is initialized from a constant, we actually track the
12 exact range and in that case this causes miscompilation.
13 As discussed on IRC, I think doing something like:
14 offset_int off2;
15 if (off_cst && off.is_constant (&off2))
16 {
17 tree cst = wide_int_to_tree (sizetype, off2 / BITS_PER_UNIT);
18 // adjust range r with POINTER_PLUS_EXPR cst
19 if (!range_includes_zero_p (&r))
20 return true;
21 }
22 // Fallback
23 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
24 return true;
25 could work, given that most of the pointer ranges are just the simple ones
26 perhaps it is too much for little benefit.
27
28 2021-11-17 Jakub Jelinek <jakub@redhat.com>
29
30 PR tree-optimization/103255
31 * gimple-range.cc (fold_using_range::range_of_address): Return
32 range_nonzero rather than unadjusted base's range. Formatting fixes.
33
34 * gcc.c-torture/execute/pr103255.c: New test.
35
36 (cherry picked from commit c39cb6bf835ca12e590eaa6f90222e51be207c50)
37 ---
38 gcc/gimple-range.cc | 16 +++++---
39 .../gcc.c-torture/execute/pr103255.c | 41 +++++++++++++++++++
40 2 files changed, 52 insertions(+), 5 deletions(-)
41 create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr103255.c
42
43 --- a/gcc/gimple-range.cc
44 +++ b/gcc/gimple-range.cc
45 @@ -491,14 +491,20 @@ gimple_ranger::range_of_address (irange
46 }
47 /* If &X->a is equal to X, the range of X is the result. */
48 if (off_cst && known_eq (off, 0))
49 - return true;
50 + return true;
51 else if (flag_delete_null_pointer_checks
52 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
53 {
54 - /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
55 - allow going from non-NULL pointer to NULL. */
56 - if(!range_includes_zero_p (&r))
57 - return true;
58 + /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
59 + allow going from non-NULL pointer to NULL. */
60 + if (!range_includes_zero_p (&r))
61 + {
62 + /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
63 + using POINTER_PLUS_EXPR if off_cst and just fall back to
64 + this. */
65 + r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
66 + return true;
67 + }
68 }
69 /* If MEM_REF has a "positive" offset, consider it non-NULL
70 always, for -fdelete-null-pointer-checks also "negative"
71 --- /dev/null
72 +++ b/gcc/testsuite/gcc.c-torture/execute/pr103255.c
73 @@ -0,0 +1,41 @@
74 +/* PR tree-optimization/103255 */
75 +
76 +struct H
77 +{
78 + unsigned a;
79 + unsigned b;
80 + unsigned c;
81 +};
82 +
83 +#if __SIZEOF_POINTER__ >= 4
84 +#define ADDR 0x400000
85 +#else
86 +#define ADDR 0x4000
87 +#endif
88 +#define OFF 0x20
89 +
90 +int
91 +main ()
92 +{
93 + struct H *h = 0;
94 + unsigned long o;
95 + volatile int t = 1;
96 +
97 + for (o = OFF; o <= OFF; o += 0x1000)
98 + {
99 + struct H *u;
100 + u = (struct H *) (ADDR + o);
101 + if (t)
102 + {
103 + h = u;
104 + break;
105 + }
106 + }
107 +
108 + if (h == 0)
109 + return 0;
110 + unsigned *tt = &h->b;
111 + if ((__SIZE_TYPE__) tt != (ADDR + OFF + __builtin_offsetof (struct H, b)))
112 + __builtin_abort ();
113 + return 0;
114 +}