From patchwork Sat Dec 10 09:18:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 704704 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 3tbNpL01YZz9vFT for ; Sat, 10 Dec 2016 20:19:20 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="FU9RL1yy"; 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:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=s5aw/F9+BmUKAZGkR No4pIBIIZ/7tTRPxHmaR4KyTjIGvOxBC08jtfMkLZmbx5KikY7lAAXQS12zp0M04 OZhEfn4nuhn09VBYb+ByEIJ3SBJmS9R+ACk+4uzhdXctZxKREl+1iMFb/0BUddtr odEGHsgAUoqTCZfazQq3Bv9SGM= 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:references:mime-version :content-type:in-reply-to; s=default; bh=CWMgD7DwGa0EwYeIWqXD/0p lpuw=; b=FU9RL1yyX0pEfj9s+NE5yrl+5xVs+qk7x/iuzfJ4j4vQCqmmDXF+yqt KbY2btiVYilPk7f/teaICV/+SzR8mH73EhW8zyqwGInsQEU6B9x2UvO3PfGIzZwQ u6ucmGPNwKANeKRmK2vE0DREB50Wufdpp7iDqi9JD51C85lSxQls= Received: (qmail 123920 invoked by alias); 10 Dec 2016 09:19:10 -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 123907 invoked by uid 89); 10 Dec 2016 09:19:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.9 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=bonzini, COND_EXPR, cond_expr, D*inria.fr 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; Sat, 10 Dec 2016 09:18:59 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (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 B2B1361B91; Sat, 10 Dec 2016 09:18:57 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-204-100.brq.redhat.com [10.40.204.100]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uBA9Itoe020813 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Sat, 10 Dec 2016 04:18:57 -0500 Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id uBA9IrdI008173; Sat, 10 Dec 2016 10:18:54 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id uBA9IpVF007046; Sat, 10 Dec 2016 10:18:51 +0100 Date: Sat, 10 Dec 2016 10:18:51 +0100 From: Jakub Jelinek To: Paolo Bonzini Cc: Richard Biener , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] Fix A < 0 ? C : 0 optimization (PR tree-optimization/78720) Message-ID: <20161210091851.GN3541@tucnak.redhat.com> Reply-To: Jakub Jelinek References: <20161209192604.GG3541@tucnak.redhat.com> <491c4203-01ee-21bd-8e5c-c90862312dd2@gnu.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <491c4203-01ee-21bd-8e5c-c90862312dd2@gnu.org> User-Agent: Mutt/1.5.24 (2015-08-30) X-IsSubscribed: yes On Sat, Dec 10, 2016 at 10:05:50AM +0100, Paolo Bonzini wrote: > > > On 09/12/2016 20:26, Jakub Jelinek wrote: > > + tree ctype = TREE_TYPE (@1); > > + } > > + (if (shift >= 0) > > + (bit_and > > + (convert (rshift @0 { build_int_cst (integer_type_node, shift); })) > > + @1) > > + (bit_and > > + (lshift (convert:ctype @0) { build_int_cst (integer_type_node, -shift); }) > > + @1))))) > > The ":ctype" shouldn't be needed because the COND_EXPR already has @1's > type. Note I'm actually bootstrapping/regtesting today following patch instead, where it clearly isn't needed. But for the above case with the shift, it isn't clear for me how it would work without :ctype - how the algorithm of figuring out what type to convert to works. Because the first operand of lshift isn't used next to @1. 2016-12-09 Jakub Jelinek Marc Glisse PR tree-optimization/78720 * match.pd (A < 0 ? C : 0): Only optimize for signed A. If shift is negative, first convert to @1's type and then lshift it by -shift. * gcc.c-torture/execute/pr78720.c: New test. Jakub --- gcc/match.pd.jj 2016-12-09 10:19:10.909735559 +0100 +++ gcc/match.pd 2016-12-10 09:21:26.260516596 +0100 @@ -2768,17 +2768,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (ncmp (convert:stype @0) { build_zero_cst (stype); }))))) /* If we have A < 0 ? C : 0 where C is a power of 2, convert - this into a right shift followed by ANDing with C. */ + this into a right shift or sign extension followed by ANDing with C. */ (simplify (cond (lt @0 integer_zerop) integer_pow2p@1 integer_zerop) - (with { + (if (!TYPE_UNSIGNED (TREE_TYPE (@0))) + (with { int shift = element_precision (@0) - wi::exact_log2 (@1) - 1; - } - (bit_and - (convert (rshift @0 { build_int_cst (integer_type_node, shift); })) - @1))) + } + (if (shift >= 0) + (bit_and + (convert (rshift @0 { build_int_cst (integer_type_node, shift); })) + @1) + /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure + sign extension followed by AND with C will achieve the effect. */ + (bit_and (convert @0) @1))))) /* When the addresses are not directly of decls compare base and offset. This implements some remaining parts of fold_comparison address --- gcc/testsuite/gcc.c-torture/execute/pr78720.c.jj 2016-12-10 09:18:43.386574179 +0100 +++ gcc/testsuite/gcc.c-torture/execute/pr78720.c 2016-12-10 09:18:43.386574179 +0100 @@ -0,0 +1,29 @@ +/* PR tree-optimization/78720 */ + +__attribute__((noinline, noclone)) long int +foo (signed char x) +{ + return x < 0 ? 0x80000L : 0L; +} + +__attribute__((noinline, noclone)) long int +bar (signed char x) +{ + return x < 0 ? 0x80L : 0L; +} + +__attribute__((noinline, noclone)) long int +baz (signed char x) +{ + return x < 0 ? 0x20L : 0L; +} + +int +main () +{ + if (foo (-1) != 0x80000L || bar (-1) != 0x80L || baz (-1) != 0x20L + || foo (0) != 0L || bar (0) != 0L || baz (0) != 0L + || foo (31) != 0L || bar (31) != 0L || baz (31) != 0L) + __builtin_abort (); + return 0; +}