diff options
author | Jakub Jelinek <jakub@redhat.com> | 2021-10-28 20:10:15 +0200 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2021-10-28 20:10:15 +0200 |
commit | 6123b998b185572abac7d7224b34f03955bb91a2 (patch) | |
tree | 37da95c6b820de59031915d527b40bb48263829e /gcc/fold-const.c | |
parent | d123daec0c237533cf974334d98bc6d357d4273e (diff) | |
download | gcc-6123b998b185572abac7d7224b34f03955bb91a2.zip gcc-6123b998b185572abac7d7224b34f03955bb91a2.tar.gz gcc-6123b998b185572abac7d7224b34f03955bb91a2.tar.bz2 |
match.pd: Optimize MIN_EXPR <addr1, addr2> etc. addr1 < addr2 would be simplified [PR102951]
This patch outlines the decision whether address comparison can be folded
or not from the match.pd simple comparison simplification and uses it
both there and in a new minmax simplification, such that we fold e.g.
MAX (&a[2], &a[1]) etc.
Some of the Wstringop-overflow-62.c changes might look weird, but that
seems to be mainly due to gimple_fold_builtin_memset not bothering to
copy over location, will fix that incrementally.
2021-10-28 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/102951
* fold-const.h (address_compare): Declare.
* fold-const.c (address_compare): New function.
* match.pd (cmp (convert1?@2 addr@0) (convert2? addr@1)): Use
address_compare helper.
(minmax cmp (convert1?@2 addr@0) (convert2?@3 addr@1)): New
simplification.
* gcc.dg/tree-ssa/pr102951.c: New test.
* gcc.dg/Wstringop-overflow-62.c: Adjust expected diagnostics.
Diffstat (limited to 'gcc/fold-const.c')
-rw-r--r-- | gcc/fold-const.c | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/gcc/fold-const.c b/gcc/fold-const.c index c7daf871..54f91f0 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -16492,6 +16492,132 @@ tree_nonzero_bits (const_tree t) return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t))); } +/* Helper function for address compare simplifications in match.pd. + OP0 and OP1 are ADDR_EXPR operands being compared by CODE. + BASE0, BASE1, OFF0 and OFF1 are set by the function. + GENERIC is true if GENERIC folding and false for GIMPLE folding. + Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1}, + 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1, + and 2 if unknown. */ + +int +address_compare (tree_code code, tree type, tree op0, tree op1, + tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1, + bool generic) +{ + gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR); + gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR); + base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0); + base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1); + if (base0 && TREE_CODE (base0) == MEM_REF) + { + off0 += mem_ref_offset (base0).force_shwi (); + base0 = TREE_OPERAND (base0, 0); + } + if (base1 && TREE_CODE (base1) == MEM_REF) + { + off1 += mem_ref_offset (base1).force_shwi (); + base1 = TREE_OPERAND (base1, 0); + } + if (base0 == NULL_TREE || base1 == NULL_TREE) + return 2; + + int equal = 2; + /* Punt in GENERIC on variables with value expressions; + the value expressions might point to fields/elements + of other vars etc. */ + if (generic + && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0)) + || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1)))) + return 2; + else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1)) + { + symtab_node *node0 = symtab_node::get_create (base0); + symtab_node *node1 = symtab_node::get_create (base1); + equal = node0->equal_address_to (node1); + } + else if ((DECL_P (base0) + || TREE_CODE (base0) == SSA_NAME + || TREE_CODE (base0) == STRING_CST) + && (DECL_P (base1) + || TREE_CODE (base1) == SSA_NAME + || TREE_CODE (base1) == STRING_CST)) + equal = (base0 == base1); + if (equal == 1) + { + if (code == EQ_EXPR + || code == NE_EXPR + /* If the offsets are equal we can ignore overflow. */ + || known_eq (off0, off1) + || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)) + /* Or if we compare using pointers to decls or strings. */ + || (POINTER_TYPE_P (type) + && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))) + return 1; + return 2; + } + if (equal != 0) + return equal; + if (code != EQ_EXPR && code != NE_EXPR) + return 2; + + HOST_WIDE_INT ioff0 = -1, ioff1 = -1; + off0.is_constant (&ioff0); + off1.is_constant (&ioff1); + if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST) + || (TREE_CODE (base0) == STRING_CST && DECL_P (base1)) + || (TREE_CODE (base0) == STRING_CST + && TREE_CODE (base1) == STRING_CST + && ioff0 >= 0 && ioff1 >= 0 + && ioff0 < TREE_STRING_LENGTH (base0) + && ioff1 < TREE_STRING_LENGTH (base1) + /* This is a too conservative test that the STRING_CSTs + will not end up being string-merged. */ + && strncmp (TREE_STRING_POINTER (base0) + ioff0, + TREE_STRING_POINTER (base1) + ioff1, + MIN (TREE_STRING_LENGTH (base0) - ioff0, + TREE_STRING_LENGTH (base1) - ioff1)) != 0)) + ; + else if (!DECL_P (base0) || !DECL_P (base1)) + return 2; + /* If this is a pointer comparison, ignore for now even + valid equalities where one pointer is the offset zero + of one object and the other to one past end of another one. */ + else if (!INTEGRAL_TYPE_P (type)) + ; + /* Assume that automatic variables can't be adjacent to global + variables. */ + else if (is_global_var (base0) != is_global_var (base1)) + ; + else + { + tree sz0 = DECL_SIZE_UNIT (base0); + tree sz1 = DECL_SIZE_UNIT (base1); + /* If sizes are unknown, e.g. VLA or not representable, punt. */ + if (!tree_fits_poly_int64_p (sz0) || !tree_fits_poly_int64_p (sz1)) + return 2; + + poly_int64 size0 = tree_to_poly_int64 (sz0); + poly_int64 size1 = tree_to_poly_int64 (sz1); + /* If one offset is pointing (or could be) to the beginning of one + object and the other is pointing to one past the last byte of the + other object, punt. */ + if (maybe_eq (off0, 0) && maybe_eq (off1, size1)) + equal = 2; + else if (maybe_eq (off1, 0) && maybe_eq (off0, size0)) + equal = 2; + /* If both offsets are the same, there are some cases we know that are + ok. Either if we know they aren't zero, or if we know both sizes + are no zero. */ + if (equal == 2 + && known_eq (off0, off1) + && (known_ne (off0, 0) + || (known_ne (size0, 0) && known_ne (size1, 0)))) + equal = 0; + } + return equal; +} + #if CHECKING_P namespace selftest { |