From patchwork Tue Nov 8 12:53:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 17022 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2684932wru; Tue, 8 Nov 2022 04:54:37 -0800 (PST) X-Google-Smtp-Source: AMsMyM4YtvTt1dXhHi7qCNECoZG7fyC4LvgVwGCqxlriAqitx5fncpMjhZFBdvL8GopSrYF9zJI+ X-Received: by 2002:a17:906:9bd4:b0:7ad:2d86:418c with SMTP id de20-20020a1709069bd400b007ad2d86418cmr53600922ejc.380.1667912076918; Tue, 08 Nov 2022 04:54:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667912076; cv=none; d=google.com; s=arc-20160816; b=BMN7u4CRIdABv6OIlUxgF89ypWVXqscZjvgiVOPEJbgCssBfO0NeQ6h/bh6KIk1628 0aPpOLBmJ0kGJuVbvYUr7TRfo/BqarKPovrf3eMp10P57t64t3Pq7Qc8yLvSk2Kv8NoY eCuQ3XyZwaEyY+1L8YSWHvKWBLqhsu4GakDiYE5UHmWIRKss86dOv7wPVkagyfkyXp0N WMYmJwExgLhuYbEI/UVoMjKM/7N3XqOlAoaQENrHeH8fEginU8FnMg7dLZGIHK7OAUZ/ Sx6C935K13isdyALIoCgMiqPBVGdLs+eCEfc4fr7upCfn+D3uR1ct7Op+FndOTBK+/AM 3x0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:message-id :mime-version:subject:cc:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=UM7owphI3lOTENpp3ZiSX+U1WRhvl2GOrjvBUGawdFg=; b=C6rcp74tgyc0UDtCJAdyFaPfPA6lKFk0RFv48+SDeExzAUX1S95eRXNAgz7UObiHmK 4NjmF1P+NJnGCieAV2D+nfK8li9rzwANxIzSWA+B+hpw11duK3drQ7OgRe1XpW+0f7mv zXW9Qsx/ezuOa8bp537fpCu8+CK/nvrsDvmU9Z6miK/CrZ0hWJngr2aIio3m9/2RH5QJ QjKiJ8OH3G56usjav2iEnOBQr8SOgRqz6mIAvTVBBbuHwLM+v16nL4u7MJWmro+VACF2 J4I8aokTbs9FRU/KIm53qwRLH6XplsKvoU3iy+4oTbhlH1Z1moMvaXU7qy5zXxMXAlMR VKbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=H8ayXxt+; 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=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id jg26-20020a170907971a00b0079d5da30399si13029670ejc.427.2022.11.08.04.54.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 04:54:36 -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=@gcc.gnu.org header.s=default header.b=H8ayXxt+; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 01C543858417 for ; Tue, 8 Nov 2022 12:54:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 01C543858417 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667912076; bh=UM7owphI3lOTENpp3ZiSX+U1WRhvl2GOrjvBUGawdFg=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=H8ayXxt+fwM10qqe51lKgkubS9OpZYNnYxUu1/f4W//BO5oQcQvPzkP1t+JhWsnUV 1Nv6mGyIoPW2JlM1OGUQbXwHzonw903t1KetgWd/dIVj6dYgJzMV0kT9XVNs5G6Ph1 m1g3iVfe6o0F6pHywsmR18fdEXexnUOSE98xXTyQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 057213858C53 for ; Tue, 8 Nov 2022 12:53:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 057213858C53 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id D80E821B46; Tue, 8 Nov 2022 12:53:48 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id BFC2213398; Tue, 8 Nov 2022 12:53:48 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id V/6ZLVxRamMQbgAAMHmgww (envelope-from ); Tue, 08 Nov 2022 12:53:48 +0000 Date: Tue, 8 Nov 2022 13:53:48 +0100 (CET) To: gcc-patches@gcc.gnu.org cc: Jakub Jelinek Subject: [PATCH][v2] tree-optimization/107389 - honor __builtin_assume_alignment at -O0 MIME-Version: 1.0 Message-Id: <20221108125348.BFC2213398@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_NUMSUBJECT, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748932574272380057?= X-GMAIL-MSGID: =?utf-8?q?1748932574272380057?= The following makes sure to set alignment information on the LHS of __builtin_assume_alignment calls even when not optimizing so uses as arguments to builtin functions like memcpy or __atomic_load_n can be reflected at RTL expansion time. Bootstrap and regtest running on x86_64-unknown-linux-gnu, OK? Thanks, Richard. PR tree-optimization/107389 * gimple-low.cc (lower_builtin_assume_aligned): New. (lower_stmt): Call it. * gcc.dg/pr107389.c: New testcase. --- gcc/gimple-low.cc | 41 +++++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr107389.c | 13 +++++++++++ 2 files changed, 54 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr107389.c diff --git a/gcc/gimple-low.cc b/gcc/gimple-low.cc index 512aa9feada..f9bcb772163 100644 --- a/gcc/gimple-low.cc +++ b/gcc/gimple-low.cc @@ -84,6 +84,7 @@ static void lower_try_catch (gimple_stmt_iterator *, struct lower_data *); static void lower_gimple_return (gimple_stmt_iterator *, struct lower_data *); static void lower_builtin_setjmp (gimple_stmt_iterator *); static void lower_builtin_posix_memalign (gimple_stmt_iterator *); +static void lower_builtin_assume_aligned (gimple_stmt_iterator *); /* Lower the body of current_function_decl from High GIMPLE into Low @@ -768,6 +769,13 @@ lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data) lower_builtin_posix_memalign (gsi); return; } + else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_ASSUME_ALIGNED + && !optimize) + { + lower_builtin_assume_aligned (gsi); + data->cannot_fallthru = false; + return; + } } if (decl && (flags_from_decl_or_type (decl) & ECF_NORETURN)) @@ -1310,6 +1318,39 @@ lower_builtin_posix_memalign (gimple_stmt_iterator *gsi) gsi_insert_after (gsi, stmt, GSI_NEW_STMT); gsi_insert_after (gsi, gimple_build_label (noalign_label), GSI_NEW_STMT); } + +/* Lower calls to __builtin_assume_aligned when not optimizing. */ + +static void +lower_builtin_assume_aligned (gimple_stmt_iterator *gsi) +{ + gcall *call = as_a (gsi_stmt (*gsi)); + + tree lhs = gimple_call_lhs (call); + if (!lhs || !POINTER_TYPE_P (TREE_TYPE (lhs))) + return; + + tree align = gimple_call_arg (call, 1); + tree misalign = (gimple_call_num_args (call) > 2 + ? gimple_call_arg (call, 2) : NULL_TREE); + if (!tree_fits_uhwi_p (align) + || (misalign && !tree_fits_uhwi_p (misalign))) + return; + + unsigned aligni = TREE_INT_CST_LOW (align); + unsigned misaligni = misalign ? TREE_INT_CST_LOW (misalign) : 0; + if (aligni <= 1 + || (aligni & (aligni - 1)) != 0 + || (misaligni & ~(aligni - 1)) != 0) + return; + + /* For lowering we simply transfer alignment information to the + result and leave the call otherwise unchanged, it will be elided + at RTL expansion time. */ + ptr_info_def *pi = get_ptr_info (lhs); + set_ptr_info_alignment (pi, aligni, misaligni); + gsi_next (gsi); +} /* Record the variables in VARS into function FN. */ diff --git a/gcc/testsuite/gcc.dg/pr107389.c b/gcc/testsuite/gcc.dg/pr107389.c new file mode 100644 index 00000000000..deb63380704 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr107389.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-fdump-tree-optimized-alias" } */ + +unsigned foo (void *p) +{ + unsigned i; + __builtin_memcpy (&i, __builtin_assume_aligned (p, 4), sizeof (unsigned)); + return i; +} + +/* Even when not optimizing we should have alignment info on the temporary + feeding the memcpy. */ +/* { dg-final { scan-tree-dump "ALIGN = 4" "optimized" } } */