@@ -1864,6 +1864,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(if (ptr_difference_const (@0, @1, &diff))
{ build_int_cst_type (type, diff); }))))
+/* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
+(simplify
+ (convert (pointer_diff @0 INTEGER_CST@1))
+ (if (POINTER_TYPE_P (type))
+ { build_fold_addr_expr_with_type
+ (build2 (MEM_REF, char_type_node, @0,
+ wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
+ type); }))
+
/* If arg0 is derived from the address of an object or function, we may
be able to fold this expression using the object or function's
alignment. */
new file mode 100644
@@ -0,0 +1,26 @@
+/* { dg-do run } */
+
+struct list { struct list *n; };
+
+struct obj {
+ int n;
+ struct list l;
+} _o;
+
+struct list _l = { .n = &_o.l };
+
+int main(int argc, char *argv[])
+{
+ struct obj *o = &_o;
+ _o.l.n = &_l;
+ while (&o->l != &_l)
+ /* Note the following is invoking undefined behavior but in
+ this kind of "obvious" cases we don't want to break things
+ unnecessarily and thus we avoid analyzing o as pointing
+ to nothing via the undefined pointer subtraction. Instead
+ we canonicalize the pointer subtraction followed by the
+ pointer conversion to pointer offsetting. */
+ o = ((struct obj *)((const char *)(o->l.n)
+ - (const char *)&((struct obj *)0)->l));
+ return 0;
+}