From patchwork Mon Aug 10 08:36:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1342753 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=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com 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=N3u6y4OZ; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4BQ8Sb1P5pz9sT6 for ; Mon, 10 Aug 2020 18:36:13 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 39B303851C06; Mon, 10 Aug 2020 08:36:10 +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 062583857C53 for ; Mon, 10 Aug 2020 08:36:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 062583857C53 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=roger@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:Cc:To:From:Sender:Reply-To: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=Lse3S21C9n5XoWj7OoOBsy7E8fRd6zx0SObOBrl5eS0=; b=N3u6y4OZoKzOL/p/VPheYRD7sB ONP7Ca24Bv5WUEKzCzwL/u8JDbV3rpUuldIUqQKHHntjvf6l5X5BgqLOb70o452RJcXzjdiM0qKOS qaQdisSOpZ0DweqFfUU8FAFQ/i/g0YIpfX4sbx+a0Uil4ow4XMjRrb8WN1jkkVZgtKkKwRRT6zbXF D4IMaGcdrfGjm8QBOq5fh5+omRriOg4rrKwnciNF9sfU6NgbUickoctXaGfet7oCGxUh9MRke7v7R gxl+IKnz6nuTYgVPupuUptHqJUaDqQ8eKbdX5prw53QF18fIx6OqQixHxxbTJxDH5tRy0oaH8s45Z QJNlrbqg==; Received: from host86-137-89-56.range86-137.btcentralplus.com ([86.137.89.56]:59019 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1k53Hr-0007t2-B2; Mon, 10 Aug 2020 04:36:07 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] i386: Improve code generation of smin(x,0) with -m32. Date: Mon, 10 Aug 2020 09:36:06 +0100 Message-ID: <001501d66ef1$4b230bd0$e1692370$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdZu7wUtvYXj9nkbTmq69uq6lUv4oA== 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=-11.1 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.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Cc: 'Uros Bizjak' Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" To make amends for the recent (temporary) testsuite failure of my new gcc.target/i386/minmax-9.c when compiled with -m32, this patch improves the -m32 code we generate for the examples in that test case. Currently, smax(x,0) generates the very cool implementation: smax0: movl 4(%esp), %eax cltd notl %edx andl %edx, %eax ret But mysteriously, the related smin(x,0) uses a longer conditional move sequence: smin0: movl 4(%esp), %eax xorl %edx, %edx testl %eax, %eax setg %dl subl $1, %edx andl %edx, %eax ret With this patch, we now generate the much nicer: smin0: movl 4(%esp), %eax cltd andl %edx, %eax ret The trick is to expand smin(x,0) as "x < 0 ? x : 0" instead of the current "x <= 0 ? x : 0", as the former can take advantage of sign_bit_mask operations. The benefits can also be seen in QImode, where previously: smin0: movl 4(%esp), %edx testb %dl, %dl setg %al subl $1, %eax andl %edx, %eax ret now becomes: smin0: movl 4(%esp), %edx movl %edx, %eax sarb $7, %al andl %edx, %eax ret This patch has been tested on x86_64-pc-linux-gnu with a "make bootstrap" and "make -k check" with no new failures. Please let me know if I need to tweak the dejagnu directives; no other i386.exp test appears to explicitly specify -m32, but as a compile test, it's not unreasonable to check things on x86_64. OK for mainline? 2020-08-10 Roger Sayle gcc/ChangeLog * config/i386/i386-expand.c (ix86_expand_int_movcc): Expand signed MIN_EXPR against zero as "x < 0 ? x : 0" instead of "x <= 0 ? x : 0" to enable sign_bit_compare_p optimizations. gcc/testsuite/ChangeLog * gcc.target/i386/minmax-12.c: New test. Thanks in advance, Roger --- Roger Sayle NextMove Software Cambridge, UK diff --git a/gcc/testsuite/gcc.target/i386/minmax-12.c b/gcc/testsuite/gcc.target/i386/minmax-12.c new file mode 100644 index 0000000..bcb3af0 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/minmax-12.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-m32 -O2 -march=i386 -mtune=generic" } */ + +#define min(a,b) (((a) < (b))? (a) : (b)) + +int foo(int x) +{ + return min(x,0); +} + +signed char bar(signed char x) +{ + return min(x,0); +} + +/* { dg-final { scan-assembler "cltd" } } */ +/* { dg-final { scan-assembler "sarb" } } */ diff --git a/gcc/config/i386/i386-expand.c b/gcc/config/i386/i386-expand.c index e194214..46fbab4 100644 --- a/gcc/config/i386/i386-expand.c +++ b/gcc/config/i386/i386-expand.c @@ -3305,7 +3305,17 @@ ix86_expand_int_movcc (rtx operands[]) { var = operands[2]; if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx) - operands[2] = constm1_rtx, op = and_optab; + { + /* For smin (x, 0), expand as "x < 0 ? x : 0" instead of + "x <= 0 ? x : 0" to enable sign_bit_compare_p. */ + if (code == LE && op1 == const0_rtx && rtx_equal_p (op0, var)) + operands[1] = simplify_gen_relational (LT, VOIDmode, + GET_MODE (op0), + op0, const0_rtx); + + operands[2] = constm1_rtx; + op = and_optab; + } else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx) operands[2] = const0_rtx, op = ior_optab; else