From patchwork Wed Aug 28 13:48:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1977898 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=p7TNME+h; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=KNxys6XI; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=p7TNME+h; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=KNxys6XI; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wv5Nw1MS3z1yXd for ; Wed, 28 Aug 2024 23:49:04 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 65A1F3860764 for ; Wed, 28 Aug 2024 13:49:02 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by sourceware.org (Postfix) with ESMTPS id DB9823858D39 for ; Wed, 28 Aug 2024 13:48:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB9823858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DB9823858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724852919; cv=none; b=DyYcR0n3cV4qUXXwQ8ArHDF334/Uwp/aMyF5WdELifRf2HhZdjZqC0ooOh2QA8YAzbzJ3lyOHsJo8X8IRagx1VJ74xxy/lim7gNvl+hdVggq6eRf+k81QW3CTTWnpq4PTrNVBjV4UiW5TmYBRqr6jNUym1Z2wjdIwpsi1cNluIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724852919; c=relaxed/simple; bh=Grg6AVhn2UJFP06Kq4dpxXjz82QL2hDYNN67F5tVi9g=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=kpjZ/qRuxyI1oAF2zlCwxd/aiVxYL1vbMRq4T07Ew3c9fW2K1sTkUMCtc0trBNsgh4QOM7Swb63JZ/UDRPJYazHRRDlf0/MUoiY/ha8lx91FfeLNJoJy4BZbcSmETbADixeKkslY9EZXyPiCtQsRHz+8Mont6zTVX/Ia3w3LT0A= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from murzim.nue2.suse.org (unknown [10.168.4.243]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id C14121FC24 for ; Wed, 28 Aug 2024 13:48:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724852915; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=mKY5B5AfqSQS33whcTD10UXftDf4VXUpL934JiJTh28=; b=p7TNME+hm7PW/nDvkiXMAFM+5FGo77LyJzxxlN33eynlPzgQlTrx/CX+n22WTq3BvEpKQN lYKs9Jdnv3RADeVzk6AFCUW9TI6AqSgyt3hChoskl/yBTAcn9hoxtz6exFSCnpygiCzBSA Pk7fDToQLhLYSfhWBMUyfs0N0A4nN8c= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724852915; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=mKY5B5AfqSQS33whcTD10UXftDf4VXUpL934JiJTh28=; b=KNxys6XID+64bQvarQtYQsYyEg+1eVxAMPWzy8O1kJuYZEaFqQh4JZkgOhnJY7rkui0vdv /QUYO5g9VhNxmMBQ== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724852915; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=mKY5B5AfqSQS33whcTD10UXftDf4VXUpL934JiJTh28=; b=p7TNME+hm7PW/nDvkiXMAFM+5FGo77LyJzxxlN33eynlPzgQlTrx/CX+n22WTq3BvEpKQN lYKs9Jdnv3RADeVzk6AFCUW9TI6AqSgyt3hChoskl/yBTAcn9hoxtz6exFSCnpygiCzBSA Pk7fDToQLhLYSfhWBMUyfs0N0A4nN8c= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724852915; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=mKY5B5AfqSQS33whcTD10UXftDf4VXUpL934JiJTh28=; b=KNxys6XID+64bQvarQtYQsYyEg+1eVxAMPWzy8O1kJuYZEaFqQh4JZkgOhnJY7rkui0vdv /QUYO5g9VhNxmMBQ== Date: Wed, 28 Aug 2024 15:48:35 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/116514 - handle pointer difference in bit-CCP MIME-Version: 1.0 X-Spam-Score: -1.13 X-Spamd-Result: default: False [-1.13 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MISSING_MID(2.50)[]; NEURAL_HAM_LONG(-0.33)[-0.327]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; RCPT_COUNT_ONE(0.00)[1]; RCVD_COUNT_ZERO(0.00)[0]; ARC_NA(0.00)[]; MISSING_XM_UA(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_DN_NONE(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[] X-Spam-Level: X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SPF_HELO_NONE, SPF_PASS, 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: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Message-Id: <20240828134902.65A1F3860764@sourceware.org> When evaluating the difference of two aligned pointers in CCP we fail to handle the EXACT_DIV_EXPR by the element size that occurs. The testcase then also exercises modulo to test alignment but modulo by a power-of-two isn't handled either. Re-bootstrap and regtest running on x86_64-unknown-linux-gnu after an adjustment. I'd appreciate a second eye. Thanks, Richard. PR tree-optimization/116514 * tree-ssa-ccp.cc (bit_value_binop): Handle EXACT_DIV_EXPR like TRUNC_DIV_EXPR. Handle exact division of a signed value by a power-of-two like a shift. Handle unsigned division by a power-of-two like a shift. Handle unsigned TRUNC_MOD_EXPR by power-of-two, handle signed TRUNC_MOD_EXPR by power-of-two if the result is zero. * gcc.dg/tree-ssa/ssa-ccp-44.c: New testcase. --- gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-44.c | 13 +++++++++++ gcc/tree-ssa-ccp.cc | 27 +++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-44.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-44.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-44.c new file mode 100644 index 00000000000..f1f09bfb117 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-44.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-ccp1" } */ + +int +test(int* a, int* b) +{ + __INTPTR_TYPE__ delta = (int*)__builtin_assume_aligned(b, 32) + - (int*)__builtin_assume_aligned(a, 32); + __INTPTR_TYPE__ x = delta % 8; + return (x == 0); +} + +/* { dg-final { scan-tree-dump "return 1;" "ccp1" } } */ diff --git a/gcc/tree-ssa-ccp.cc b/gcc/tree-ssa-ccp.cc index 44711018e0e..8cd45545763 100644 --- a/gcc/tree-ssa-ccp.cc +++ b/gcc/tree-ssa-ccp.cc @@ -1921,6 +1921,27 @@ bit_value_binop (enum tree_code code, signop sgn, int width, { widest_int r1max = r1val | r1mask; widest_int r2max = r2val | r2mask; + if (r2mask == 0) + { + widest_int shift = wi::exact_log2 (r2val); + if (shift != -1) + { + // Handle modulo by a power of 2 as a bitwise and. + widest_int tem_val, tem_mask; + bit_value_binop (BIT_AND_EXPR, sgn, width, &tem_val, &tem_mask, + r1type_sgn, r1type_precision, r1val, r1mask, + r2type_sgn, r2type_precision, + r2val - 1, r2mask); + if (sgn == UNSIGNED + || !wi::neg_p (r1max) + || (tem_mask == 0 && tem_val == 0)) + { + *val = tem_val; + *mask = tem_mask; + return; + } + } + } if (sgn == UNSIGNED || (!wi::neg_p (r1max) && !wi::neg_p (r2max))) { @@ -1949,11 +1970,15 @@ bit_value_binop (enum tree_code code, signop sgn, int width, } break; + case EXACT_DIV_EXPR: case TRUNC_DIV_EXPR: { widest_int r1max = r1val | r1mask; widest_int r2max = r2val | r2mask; - if (r2mask == 0 && !wi::neg_p (r1max)) + if (r2mask == 0 + && (code == EXACT_DIV_EXPR + || sgn == UNSIGNED + || !wi::neg_p (r1max))) { widest_int shift = wi::exact_log2 (r2val); if (shift != -1)