summaryrefslogtreecommitdiff
path: root/gcc/match.pd
diff options
context:
space:
mode:
authorRichard Biener <rguenther@suse.de>2020-03-11 15:34:47 +0100
committerRichard Biener <rguenther@suse.de>2020-03-11 15:35:51 +0100
commitcb99630f254aaec6591e0a200b79905b31d24eb3 (patch)
treedc528f98350f3bac9c438420b4d2032bdbd7ca2d /gcc/match.pd
parentd564c5e254df744a470a658690753dc193a4fa78 (diff)
fold undefined pointer offsetting
This avoids breaking the old broken pointer offsetting via (T)(ptr - ((T)0)->x) which should have used offsetof. Breakage was exposed by the introduction of POINTER_DIFF_EXPR and making PTA not considering that producing a pointer. The mitigation for simple cases is to canonicalize _2 = _1 - 8B; o_9 = (struct obj *) _2; to o_9 = &MEM[_1 + -8B]; eliding one statement and the offending pointer subtraction. 2020-03-11 Richard Biener <rguenther@suse.de> * match.pd ((T *)(ptr - ptr-cst) -> &MEM[ptr + -ptr-cst]): New pattern. * gcc.dg/torture/20200311-1.c: New testcase.
Diffstat (limited to 'gcc/match.pd')
-rw-r--r--gcc/match.pd9
1 files changed, 9 insertions, 0 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index 19df0c404a4..9cb37740f1e 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -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. */