commit 557c126f9fbdcde256f134d4ed34ff305387fd41 Author: Jakub Jelinek Date: Wed Sep 14 11:36:36 2022 Disallow pointer operands for |, ^ and partly & [PR106878] This is a backport of 645ef01a463f15fc230e2155719c7a12cec89acf without the changes to verify_gimple_assign_binary from the original patch. The original cover letter for the patch is as follows: My change to match.pd (that added the two simplifications this patch touches) results in more |/^/& assignments with pointer arguments, but since r12-1608 we reject pointer operands for BIT_NOT_EXPR. Disallowing them for BIT_NOT_EXPR and allowing for BIT_{IOR,XOR,AND}_EXPR leads to a match.pd maintainance nightmare (see one of the patches in the PR), so either we want to allow pointer operand on BIT_NOT_EXPR (but then we run into issues e.g. with the ranger which expects it can emulate BIT_NOT_EXPR ~X as - 1 - X which doesn't work for pointers which don't support MINUS_EXPR), or the following patch disallows pointer arguments for all of BIT_{IOR,XOR,AND}_EXPR with the exception of BIT_AND_EXPR with INTEGER_CST last operand (for simpler pointer realignment). I had to tweak one reassoc optimization and the two match.pd simplifications. 2022-09-14 Jakub Jelinek PR tree-optimization/106878 * match.pd ((type) X op CST -> (type) (X op ((type-x) CST)), (type) (((type2) X) op Y) -> (X op (type) Y)): Punt for POINTER_TYPE_P or OFFSET_TYPE. * tree-ssa-reassoc.cc (optimize_range_tests_cmp_bitwise): For pointers cast them to pointer sized integers first. * gcc.c-torture/compile/pr106878.c: New test. diff --git a/gcc/match.pd b/gcc/match.pd index a9aae484b2b..cbae09dfb28 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1663,6 +1663,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && (int_fits_type_p (@1, TREE_TYPE (@0)) || tree_nop_conversion_p (TREE_TYPE (@0), type))) || types_match (@0, @1)) + && !POINTER_TYPE_P (TREE_TYPE (@0)) + && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE /* ??? This transform conflicts with fold-const.cc doing Convert (T)(x & c) into (T)x & (T)c, if c is an integer constants (if x has signed type, the sign bit cannot be set @@ -1699,7 +1701,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (GIMPLE && TREE_CODE (@1) != INTEGER_CST && tree_nop_conversion_p (type, TREE_TYPE (@2)) - && types_match (type, @0)) + && types_match (type, @0) + && !POINTER_TYPE_P (TREE_TYPE (@0)) + && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE) (bitop @0 (convert @1))))) (for bitop (bit_and bit_ior) diff --git a/gcc/testsuite/gcc.c-torture/compile/pr106878.c b/gcc/testsuite/gcc.c-torture/compile/pr106878.c new file mode 100644 index 00000000000..c8457189437 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr106878.c @@ -0,0 +1,15 @@ +/* PR tree-optimization/106878 */ + +typedef __INTPTR_TYPE__ intptr_t; +typedef __UINTPTR_TYPE__ uintptr_t; +int a; + +int +foo (const int *c) +{ + uintptr_t d = ((intptr_t) c | (intptr_t) &a) & 65535 << 16; + intptr_t e = (intptr_t) c; + if (d != (e & 65535 << 16)) + return 1; + return 0; +} diff --git a/gcc/tree-ssa-reassoc.cc b/gcc/tree-ssa-reassoc.cc index e3d521e3267..6baef476471 100644 --- a/gcc/tree-ssa-reassoc.cc +++ b/gcc/tree-ssa-reassoc.cc @@ -3617,10 +3617,14 @@ optimize_range_tests_cmp_bitwise (enum tree_code opcode, int first, int length, tree type2 = NULL_TREE; bool strict_overflow_p = false; candidates.truncate (0); + if (POINTER_TYPE_P (type1)) + type1 = pointer_sized_int_node; for (j = i; j; j = chains[j - 1]) { tree type = TREE_TYPE (ranges[j - 1].exp); strict_overflow_p |= ranges[j - 1].strict_overflow_p; + if (POINTER_TYPE_P (type)) + type = pointer_sized_int_node; if ((b % 4) == 3) { /* For the signed < 0 cases, the types should be @@ -3651,6 +3655,8 @@ optimize_range_tests_cmp_bitwise (enum tree_code opcode, int first, int length, tree type = TREE_TYPE (ranges[j - 1].exp); if (j == k) continue; + if (POINTER_TYPE_P (type)) + type = pointer_sized_int_node; if ((b % 4) == 3) { if (!useless_type_conversion_p (type1, type)) @@ -3680,7 +3686,7 @@ optimize_range_tests_cmp_bitwise (enum tree_code opcode, int first, int length, op = r->exp; continue; } - if (id == l) + if (id == l || POINTER_TYPE_P (TREE_TYPE (op))) { code = (b % 4) == 3 ? BIT_NOT_EXPR : NOP_EXPR; g = gimple_build_assign (make_ssa_name (type1), code, op); @@ -3704,6 +3710,14 @@ optimize_range_tests_cmp_bitwise (enum tree_code opcode, int first, int length, gimple_seq_add_stmt_without_update (&seq, g); op = gimple_assign_lhs (g); } + type1 = TREE_TYPE (ranges[k - 1].exp); + if (POINTER_TYPE_P (type1)) + { + gimple *g + = gimple_build_assign (make_ssa_name (type1), NOP_EXPR, op); + gimple_seq_add_stmt_without_update (&seq, g); + op = gimple_assign_lhs (g); + } candidates.pop (); if (update_range_test (&ranges[k - 1], NULL, candidates.address (), candidates.length (), opcode, ops, op,