From patchwork Tue Feb 7 21:23:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 725352 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vHy4S3Wv0z9s4s for ; Wed, 8 Feb 2017 08:23:18 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="cSGNi7rF"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=xmBsRDXrct3nTOe6hvmOQz+fNNRFS GCQS41oIAFNh2i0JTqSGUqSG6RP4ZrFwy1Td6BrLEXG8fdS+ZYLZmmDcpYBNtqrn rg1Et8WJXC4Y4Iehnl5824NvGzYdmWokrIo55YdOgLRpox+B2TQaNFc5jPO9YEwi 0oT3QVa4X88tFU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; s=default; bh=Mb7UGIAYwtyRUYQWfx0TcGcfONg=; b=cSG Ni7rF0uKbF3+tKEQH/nEo4YZ+7Jwdu+9EfzWmVUA6L8Trq75hIE2/dtk5GELKTE3 zU2Id7w2VLGmBrspYt/Mor9GyYJ0YseaJd07mZiu03kdo50vbtFGYnMFNiCDnQ8I vtNmUBHXEQ9di06tk21MumepAM5lm21WcESeD6NU= Received: (qmail 70869 invoked by alias); 7 Feb 2017 21:23:11 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 70856 invoked by uid 89); 7 Feb 2017 21:23:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=replaced X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 07 Feb 2017 21:23:08 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 801947FB6F; Tue, 7 Feb 2017 21:23:08 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-116-76.ams2.redhat.com [10.36.116.76]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v17LN691021321 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Tue, 7 Feb 2017 16:23:07 -0500 Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id v17LN4M2007911; Tue, 7 Feb 2017 22:23:04 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id v17LN2ws007910; Tue, 7 Feb 2017 22:23:02 +0100 Date: Tue, 7 Feb 2017 22:23:02 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Improve x % y to x VRP optimization (PR tree-optimization/79408) Message-ID: <20170207212302.GW1849@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.7.1 (2016-10-04) X-IsSubscribed: yes Hi! While looking at the RTL problem when combiner optimizes away (x & 0xfffe) % 0xffff into just (x & 0xfffe), I've looked at VRP which optimizes that case well (-O2 only, the PR is -O1), but discovered that it isn't generic enough, we actually don't need op1 to be constant in this case, a range whose (positive) minimum satisfies it can be handled the same. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk (or shall I defer it for GCC8)? 2017-02-07 Jakub Jelinek PR tree-optimization/79408 * tree-vrp.c (simplify_div_or_mod_using_ranges): If op1 is not constant, but SSA_NAME with a known integer range, use the minimum of that range instead of op1 to determine if modulo can be replaced with its first operand. * gcc.dg/tree-ssa/pr79408.c: New test. Jakub --- gcc/tree-vrp.c.jj 2017-01-30 19:10:50.000000000 +0100 +++ gcc/tree-vrp.c 2017-02-07 12:33:32.721482609 +0100 @@ -9241,17 +9241,25 @@ simplify_div_or_mod_using_ranges (gimple tree val = NULL; tree op0 = gimple_assign_rhs1 (stmt); tree op1 = gimple_assign_rhs2 (stmt); + tree op1min = op1; value_range *vr = get_value_range (op0); if (rhs_code == TRUNC_MOD_EXPR - && TREE_CODE (op1) == INTEGER_CST - && tree_int_cst_sgn (op1) == 1 + && TREE_CODE (op1) == SSA_NAME) + { + value_range *vr1 = get_value_range (op1); + if (range_int_cst_p (vr1)) + op1min = vr1->min; + } + if (rhs_code == TRUNC_MOD_EXPR + && TREE_CODE (op1min) == INTEGER_CST + && tree_int_cst_sgn (op1min) == 1 && range_int_cst_p (vr) - && tree_int_cst_lt (vr->max, op1)) + && tree_int_cst_lt (vr->max, op1min)) { if (TYPE_UNSIGNED (TREE_TYPE (op0)) || tree_int_cst_sgn (vr->min) >= 0 - || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1), - vr->min)) + || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), + op1min), vr->min)) { /* If op0 already has the range op0 % op1 has, --- gcc/testsuite/gcc.dg/tree-ssa/pr79408.c.jj 2017-02-07 16:30:42.439774777 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr79408.c 2017-02-07 16:32:37.653282317 +0100 @@ -0,0 +1,40 @@ +/* PR tree-optimization/79408 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +void link_error (void); + +void +foo (unsigned int x, unsigned int y) +{ + if (x > 7312) + return; + if (y <= 7312) + return; + if (x % y != x) + link_error (); +} + +void +bar (int x, int y) +{ + if (x > 7312 || x < 0) + return; + if (y <= 7312) + return; + if (x % y != x) + link_error (); +} + +void +baz (int x, int y) +{ + if (x > 7312 || x < -7312) + return; + if (y <= 7312) + return; + if (x % y != x) + link_error (); +} + +/* { dg-final { scan-tree-dump-times "link_error" 0 "optimized"} } */