From patchwork Sun Aug 22 14:50:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1519438 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=nextmovesoftware.com header.i=@nextmovesoftware.com header.a=rsa-sha256 header.s=default header.b=PQVs2dIG; dkim-atps=neutral Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Gsywp1MRNz9sPf for ; Mon, 23 Aug 2021 00:50:49 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AA6143853826 for ; Sun, 22 Aug 2021 14:50:46 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id AFDC33853C01 for ; Sun, 22 Aug 2021 14:50:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AFDC33853C01 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=aMHwToHFdDB2R3aPqe4JMOtGzbsa3H23XgprfBXA/A0=; b=PQVs2dIGaRtVvlR9eg+ZTKIhCx uCQnR0k2etlKFK0xS9ZF2/Eyz7BgsscWnPVzhSkFgUEhII6XeQHO/IZE6ey85uOxR9abXKvp3txFf l7xkQwLkOcLEYhoGfKf7xVpFcyk+X1n3mUj+wflUBqbhhdayMlgJTIvprhzIF0hQrJxzk3saabggO /24kw6ulClSHZ8UQqm/3lf112ph22YTsPFi0Q0m67zXaW+FVbcsWuDE/KFD3Zz21YF5R8mZsNPfOh p6B6EoYWcG8w4Sj7gc3QIieKGuuVriTQ78EN5hKmL0HlJtHH5/DibXwu1MaNldluXsJCk7M/jflRy HjqDJiPw==; Received: from [185.62.158.67] (port=54550 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1mHonp-0004uO-74 for gcc-patches@gcc.gnu.org; Sun, 22 Aug 2021 10:50:25 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] Improved handling of division/modulus in bit CCP. Date: Sun, 22 Aug 2021 15:50:23 +0100 Message-ID: <004701d79765$0a351120$1e9f3360$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdeXZIn47RCTQSEhROuUAoV43NnK1A== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch implements support for TRUNC_MOD_EXPR and TRUNC_DIV_EXPR in tree-ssa's bit CCP pass. This is mostly for completeness, as the VRP pass already provides better bounds for these operations, but seeing mask values of all_ones in my debugging/instrumentation logs seemed overly pessimistic. With this patch, the expression X%10 has a nonzero bits of 0x0f (for unsigned X), likewise (X&1)/3 has a known value of zero, and (X&3)/3 has a nonzero bits mask of 0x1. This patch has been tested on x86_64-pc-linux-gnu with "make bootstrap" and "make -k check" with no new failures. Ok for mainline? 2021-08-22 Roger Sayle gcc/ChangeLog * tree-ssa-ccp.c (bit_value_binop) [TRUNC_MOD_EXPR, TRUNC_DIV_EXPR]: Provide bounds for unsigned (and signed with non-negative operands) division and modulus. Roger diff --git a/gcc/tree-ssa-ccp.c b/gcc/tree-ssa-ccp.c index 1a63ae5..1a94aeb 100644 --- a/gcc/tree-ssa-ccp.c +++ b/gcc/tree-ssa-ccp.c @@ -1736,6 +1736,68 @@ bit_value_binop (enum tree_code code, signop sgn, int width, break; } + case TRUNC_MOD_EXPR: + { + widest_int r1max = r1val | r1mask; + widest_int r2max = r2val | r2mask; + if (sgn == UNSIGNED + || (!wi::neg_p (r1max) && !wi::neg_p (r2max))) + { + /* Confirm R2 has some bits set, to avoid division by zero. */ + widest_int r2min = wi::bit_and_not (r2val, r2mask); + if (r2min != 0) + { + /* R1 % R2 is R1 if R1 is always less than R2. */ + if (wi::ltu_p (r1max, r2min)) + { + *mask = r1mask; + *val = r1val; + } + else + { + /* R1 % R2 is always less than the maximum of R2. */ + unsigned int lzcount = wi::clz (r2max); + unsigned int bits = wi::get_precision (r2max) - lzcount; + if (r2max == wi::lshift (1, bits)) + bits--; + *mask = wi::mask (bits, false); + *val = 0; + } + } + } + } + break; + + case TRUNC_DIV_EXPR: + { + widest_int r1max = r1val | r1mask; + widest_int r2max = r2val | r2mask; + if (sgn == UNSIGNED + || (!wi::neg_p (r1max) && !wi::neg_p (r2max))) + { + /* Confirm R2 has some bits set, to avoid division by zero. */ + widest_int r2min = wi::bit_and_not (r2val, r2mask); + if (r2min != 0) + { + /* R1 / R2 is zero if R1 is always less than R2. */ + if (wi::ltu_p (r1max, r2min)) + { + *mask = 0; + *val = 0; + } + else + { + widest_int upper = wi::udiv_trunc (r1max, r2min); + unsigned int lzcount = wi::clz (upper); + unsigned int bits = wi::get_precision (upper) - lzcount; + *mask = wi::mask (bits, false); + *val = 0; + } + } + } + } + break; + default:; } }