From patchwork Sat Feb 10 09:46:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 199238 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp1382432dyd; Sat, 10 Feb 2024 01:46:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IHKWMmVhtRam7BR7eSFYL2B5H+VVUBM0HfsJ/9otaFbrbPedgQyUFtCu0RvVFfm3xxXCcsa X-Received: by 2002:ac8:5f8b:0:b0:42b:f984:a026 with SMTP id j11-20020ac85f8b000000b0042bf984a026mr2390436qta.65.1707558406596; Sat, 10 Feb 2024 01:46:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707558406; cv=pass; d=google.com; s=arc-20160816; b=Ka+scHJzgdn8nctCLJzz/lg71x3wO+9j9CmQYo35bS9q9IMrdDJu+FdPoYl5nYkIF2 Mxn5l47f0c8rPBQE5/sTIJV+vJzC635l53KfHLOX2HCELcAnXJpLO+HsNh3gqTHeYp0e 6svBfFNTXyEvTrdV+q82egx4DbDMYoavYmSlVSUR1mC9OCuMk3yXCdLl7RltbzfKFcZ0 LQqvDChE+S2Zkd6INQHFwHoZYC2rMcGjB+y5kbbUOVCU3x7XdaCiyRdrtbphZvry7oW9 BWmykqILZKuXbpQB74oulYYDJxu5+ZheHio8h+Q6Zz5fIb3lfQ56G1Q6lFkENGjF+fGX 4+Bw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=+362R2ge4WDJny1/N7a7rG/ouzS/GRWjpvdv3MshB6A=; fh=bPZW5Civg9rXLiIRV0s8Ybv4JbfKiXJq8rbFtAmlsPs=; b=bOIxWy4CfoSJd29vvsJ5INQh9UWUbB2TJLxjhW4Z2bmm41oG/3SrLcunPBT1Yy1yqg xk4gVmExlNiMj9AN49102GURpPh4pMZtri6TF6eWryBm+4LpLl/LOGN/7qsXZXGjFKQ2 89takjDjf7z9L4gAOrAB4CaQo2Uks/gjn0lzHnFPG6V1VIhDu1hRAW/gMzNR0IPYNNw1 NlSLYM2IJJLHtao3Jm7rTx6ar98SYB0mKOAgn60k7KZD9GoTyuYhpGH17wovWheDkzRU WiuWDXWKnYUSIAU3Z1PlgkJ5ybBMkHNytiKv3thVkBKDVZsg9GPPdYcGY/FX6SiSB0Qa pCEA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="hn5GHuh/"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCX+hC229ZUi77vjKLutrGuyc7qs88EMGFBt1lucZs51zw3LeNAoNoghD+qS6NRqBXXFBQKQqWs6qbnaOfspb0pZUlIVEA== Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id h5-20020ac87d45000000b0042c4b08836bsi3847923qtb.301.2024.02.10.01.46.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Feb 2024 01:46:46 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="hn5GHuh/"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4E9B83858C98 for ; Sat, 10 Feb 2024 09:46:46 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 77E473858D20 for ; Sat, 10 Feb 2024 09:46:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 77E473858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 77E473858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707558373; cv=none; b=l9aRs3pdjw/m8CRsHLnkNobghpo3rHPrmtk8n1P+S3hNWzSADaDYRdHbOwejeVC8crGV6nbA1jUk0y+uSbH0lAfj490vBF2siIheuDBboKHebDg5AWmfUd3eccUfLB5iqNmmqvb01A3Sflze84ld6sCSPhbRen/U67kt+x4Usro= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707558373; c=relaxed/simple; bh=ojiIByL0DyLxsrkEn/WR+OEW+1NuWO2tNqR1tk0NkVo=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=XANjDyYe41VhjyXqY43TvDu3Fm0iM/NZ7fMv/8n3kX66ThU31Nj/dZw7LEjIXmI6ghE95LQWIl9UuasA3gzcG/Wg6c5l2fRebdm2BitgHjDxyODJNHyv9tSfLBXvCkzXm/Zi9qxFbPVwSX44hqqPDyA5LUMYqmBHUN2WnsfK1E8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707558371; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=+362R2ge4WDJny1/N7a7rG/ouzS/GRWjpvdv3MshB6A=; b=hn5GHuh/zS3M9hfhWda3mJHW5nXFy0Kqzy14qJkW493SLvJrVw/OLGhSYT1YJPPPkho/jo nN464hXViqTKWAmZBM8QjgfQrT+QzGE//w7cafMoYRzkNjxC5cKN1leivgNxJ78OUhINLW 9UQmUlIP1lhuR6njZ7NOu05/YY5qqaM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-610-ushZEDcwNzGWdh60AuY4YQ-1; Sat, 10 Feb 2024 04:46:08 -0500 X-MC-Unique: ushZEDcwNzGWdh60AuY4YQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B11A584AF84; Sat, 10 Feb 2024 09:46:07 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6E64E2166B31; Sat, 10 Feb 2024 09:46:07 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 41A9k4ns4023427 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 10 Feb 2024 10:46:05 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41A9k3HB4023426; Sat, 10 Feb 2024 10:46:03 +0100 Date: Sat, 10 Feb 2024 10:46:03 +0100 From: Jakub Jelinek To: Richard Biener , Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] gimple-low: Fix up handling of volatile automatic vars in assume attribute [PR110754] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790504763299988778 X-GMAIL-MSGID: 1790504763299988778 Hi! 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. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? I guess we want to backport it to 13 eventually too. 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. Jakub --- gcc/gimple-low.cc.jj 2024-02-01 09:16:51.265388345 +0100 +++ gcc/gimple-low.cc 2024-02-09 15:29:16.003289765 +0100 @@ -374,15 +374,22 @@ assumption_copy_decl (tree decl, copy_bo 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 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 * /* 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])) --- gcc/testsuite/gcc.dg/attr-assume-6.c.jj 2024-02-09 15:32:49.384325442 +0100 +++ gcc/testsuite/gcc.dg/attr-assume-6.c 2024-02-09 15:48:27.612288853 +0100 @@ -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; +} --- gcc/testsuite/g++.dg/cpp23/attr-assume12.C.jj 2024-02-09 15:51:21.100878070 +0100 +++ gcc/testsuite/g++.dg/cpp23/attr-assume12.C 2024-02-09 15:51:51.863450599 +0100 @@ -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; +}