From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 4DB9B3858D20; Sat, 10 Feb 2024 10:28:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4DB9B3858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707560926; bh=7IFHxdMR8L2M3wHOEQH0n/Hy14VOtOkQb8sye9IHhIo=; h=From:To:Subject:Date:From; b=XeD4hE+QpgKyYxVjaqO67ShkVBNOVUF86lUK+JGggrxhfUQNAj9/pJw0XK9gOn0qH uoKVOttkdP4lbEqkZk0xt9ssET6/NV93uJI5Vm/rXp4RyQzK5BeVfBBVnMKNVvQFEV g+0YVAQpZNGbLULWie2EuTAcgK+vwkNqs7ZUlAkw= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-8910] gimple-low: Fix up handling of volatile automatic vars in assume attribute [PR110754] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 8427290f33d95bc173d37637fbec044b2c067f14 X-Git-Newrev: 39920447f876128ff7942a9cd931021800865894 Message-Id: <20240210102846.4DB9B3858D20@sourceware.org> Date: Sat, 10 Feb 2024 10:28:46 +0000 (GMT) List-Id: https://gcc.gnu.org/g:39920447f876128ff7942a9cd931021800865894 commit r14-8910-g39920447f876128ff7942a9cd931021800865894 Author: Jakub Jelinek Date: Sat Feb 10 11:28:00 2024 +0100 gimple-low: Fix up handling of volatile automatic vars in assume attribute [PR110754] As the following testcases show, the gimple-low outlining of assume magic functions handled volatile automatic vars (including parameters/results) like non-volatile ones except it copied volatile to the new PARM_DECL, which has the undesirable effect that a load from the volatile var is passed to IFN_ASSUME and so there is a side-effect there even when side-effects of the assume attribute shouldn't be evaluated. The following patch fixes that by passing address of the volatile variables/parameters/results instead and doing loads or stores from it or to it where it was originally accessed in the assume attribute expression. 2024-02-10 Jakub Jelinek PR middle-end/110754 * gimple-low.cc (assumption_copy_decl): For TREE_THIS_VOLATILE decls create PARM_DECL with pointer to original type, set TREE_READONLY and keep TREE_THIS_VOLATILE, TREE_ADDRESSABLE, DECL_NOT_GIMPLE_REG_P and DECL_BY_REFERENCE cleared. (adjust_assumption_stmt_op): For remapped TREE_THIS_VOLATILE decls wrap PARM_DECL into a simple TREE_THIS_NO_TRAP MEM_REF. (lower_assumption): For TREE_THIS_VOLATILE vars pass ADDR_EXPR of the var as argument. * gcc.dg/attr-assume-6.c: New test. * g++.dg/cpp23/attr-assume12.C: New test. Diff: --- gcc/gimple-low.cc | 27 ++++++++++++++++++++++----- gcc/testsuite/g++.dg/cpp23/attr-assume12.C | 14 ++++++++++++++ gcc/testsuite/gcc.dg/attr-assume-6.c | 14 ++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/gcc/gimple-low.cc b/gcc/gimple-low.cc index e19fc2cce9d4..e03719887050 100644 --- a/gcc/gimple-low.cc +++ b/gcc/gimple-low.cc @@ -374,15 +374,22 @@ assumption_copy_decl (tree decl, copy_body_data *id) gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL); + if (TREE_THIS_VOLATILE (decl)) + type = build_pointer_type (type); tree copy = build_decl (DECL_SOURCE_LOCATION (decl), PARM_DECL, DECL_NAME (decl), type); if (DECL_PT_UID_SET_P (decl)) SET_DECL_PT_UID (copy, DECL_PT_UID (decl)); - TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl); - TREE_READONLY (copy) = TREE_READONLY (decl); - TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl); - DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (decl); - DECL_BY_REFERENCE (copy) = DECL_BY_REFERENCE (decl); + TREE_THIS_VOLATILE (copy) = 0; + if (TREE_THIS_VOLATILE (decl)) + TREE_READONLY (copy) = 1; + else + { + TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl); + TREE_READONLY (copy) = TREE_READONLY (decl); + DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (decl); + DECL_BY_REFERENCE (copy) = DECL_BY_REFERENCE (decl); + } DECL_ARG_TYPE (copy) = type; ((lower_assumption_data *) id)->decls.safe_push (decl); return copy_decl_for_dup_finish (id, decl, copy); @@ -466,6 +473,11 @@ adjust_assumption_stmt_op (tree *tp, int *, void *datap) case PARM_DECL: case RESULT_DECL: *tp = remap_decl (t, &data->id); + if (TREE_THIS_VOLATILE (t) && *tp != t) + { + *tp = build_simple_mem_ref (*tp); + TREE_THIS_NOTRAP (*tp) = 1; + } break; default: break; @@ -600,6 +612,11 @@ lower_assumption (gimple_stmt_iterator *gsi, struct lower_data *data) /* Remaining arguments will be the variables/parameters mentioned in the condition. */ vargs[i - sz] = lad.decls[i - 1]; + if (TREE_THIS_VOLATILE (lad.decls[i - 1])) + { + TREE_ADDRESSABLE (lad.decls[i - 1]) = 1; + vargs[i - sz] = build_fold_addr_expr (lad.decls[i - 1]); + } /* If they have gimple types, we might need to regimplify them to make the IFN_ASSUME call valid. */ if (is_gimple_reg_type (TREE_TYPE (vargs[i - sz])) diff --git a/gcc/testsuite/g++.dg/cpp23/attr-assume12.C b/gcc/testsuite/g++.dg/cpp23/attr-assume12.C new file mode 100644 index 000000000000..c955cd1d1e83 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/attr-assume12.C @@ -0,0 +1,14 @@ +// PR middle-end/110754 +// { dg-do compile { target c++11 } } +// { dg-options "-O2 -fdump-tree-optimized" } +// { dg-final { scan-tree-dump-times "a ={v} x" 1 "optimized" } } +// { dg-final { scan-tree-dump-not "={v} a" "optimized" } } +// { dg-final { scan-tree-dump-times "return 0;" 1 "optimized" } } + +int +foo (int x) +{ + volatile int a = x; + [[gnu::assume (x == (a & 0))]]; + return x; +} diff --git a/gcc/testsuite/gcc.dg/attr-assume-6.c b/gcc/testsuite/gcc.dg/attr-assume-6.c new file mode 100644 index 000000000000..346845f1600f --- /dev/null +++ b/gcc/testsuite/gcc.dg/attr-assume-6.c @@ -0,0 +1,14 @@ +/* PR middle-end/110754 */ +/* { dg-do compile } */ +/* { dg-options "-std=c23 -O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-times "a ={v} x" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-not "={v} a" "optimized" } } */ +/* { dg-final { scan-tree-dump-times "return 0;" 1 "optimized" } } */ + +int +foo (int x) +{ + volatile int a = x; + [[gnu::assume (x == (a & 0))]]; + return x; +}