From patchwork Fri Sep 21 08:04:29 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Lyon X-Patchwork-Id: 185602 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]) by ozlabs.org (Postfix) with SMTP id EE31E2C007E for ; Fri, 21 Sep 2012 18:04:55 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1348819496; h=Comment: DomainKey-Signature:Received:Received:Received:Received: MIME-Version:Received:Received:In-Reply-To:References:Date: Message-ID:Subject:From:To:Cc:Content-Type:Mailing-List: Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:Sender:Delivered-To; bh=G7jpVdltbccRXclE5a4v+Mrk8c8=; b=xlECCKETt+Qqcs6OoLCEGTUcLcRY3tpRHhmPn/hg5qigwOueLfKSA5JaOdYaxA jz2cXHjuRRPj657d390lqR2yXOc3FqblWMy0ccAQDtJEhm02qdsMeS1Res1kQoa3 49Zp7tH4h/osB0A1pmRlrzcNrS8ZbSVUefTcE5/s46bdg= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:MIME-Version:Received:Received:In-Reply-To:References:Date:Message-ID:Subject:From:To:Cc:Content-Type:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=f8UGK374ZAUcdfVZuCJxTXtCgEn+jIi73V3s9PHQQ0Z6Oqq/if4KTQg8xQfre9 Yi8SUJhhQ9O7NG4rgSiSj4UI3ecEJM6etkw/k5tmT9wNO0S1eEpinCl8fg26+WQH edKPHVbub7Wi3ysICgjvYVcYhz6X5W5MeHkdZADhF7mL8=; Received: (qmail 6871 invoked by alias); 21 Sep 2012 08:04:48 -0000 Received: (qmail 6855 invoked by uid 22791); 21 Sep 2012 08:04:45 -0000 X-SWARE-Spam-Status: No, hits=-2.9 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, KHOP_THREADED, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, TW_FN, TW_QE X-Spam-Check-By: sourceware.org Received: from mail-qc0-f175.google.com (HELO mail-qc0-f175.google.com) (209.85.216.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 21 Sep 2012 08:04:30 +0000 Received: by qcad10 with SMTP id d10so2706650qca.20 for ; Fri, 21 Sep 2012 01:04:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:x-gm-message-state; bh=0fxVpEsO1QYnx+YqvDS1Ht4ntsQfrOaybkmpHwkN1MY=; b=ahfiJiph15bH6/DR4O0iFvIR2mgwzWXVkRi06pXACLkHGg2Fe8ckTcuVsehRXJF0OQ hp4swfBhSKWGpvD9zVfSApsoCXW+w0MrhkHbaHZyCENeM1MNA5WXEW25HXUqaZuRjjei 80XO9ORq85/03BRqtqYdqkImQOoR4Ixrv3j0xdkSOg5URtKye3rBf7G3NXG5ausZ89GM jk4cqjaxOQ7SfJpNy3z/Zyowg4UtIBP36h36/UIYEadCwvuDez452v+PC/ZAtiZSVXWd F0OYK7cv5Zg29mFD2IM7kgxVCFCYAuHzDY3dMUfwyDKdVdpOWvs84FVy18B+lse0hkqS AzXg== MIME-Version: 1.0 Received: by 10.224.193.69 with SMTP id dt5mr10356920qab.2.1348214669563; Fri, 21 Sep 2012 01:04:29 -0700 (PDT) Received: by 10.49.105.168 with HTTP; Fri, 21 Sep 2012 01:04:29 -0700 (PDT) In-Reply-To: References: <149796024.SZgQOFZzjV@polaris> Date: Fri, 21 Sep 2012 10:04:29 +0200 Message-ID: Subject: Re: [Patch] catch builtin_bswap16 construct From: Christophe Lyon To: Eric Botcazou Cc: gcc-patches@gcc.gnu.org X-Gm-Message-State: ALoCoQkw6f0sOJx2CxELJCmeUIGUBxaBbk2Y4f5h9GtavQgWPOBhUB1wmuPVghKfljX3LC3s4c7+ X-IsSubscribed: yes 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 On 20 September 2012 09:55, Christophe Lyon wrote: > On 20 September 2012 09:12, Eric Botcazou wrote: >>> The attached patch catches C constructs: >>> (A << 8) | (A >> 8) >>> where A is unsigned 16 bits >>> and maps them to builtin_bswap16(A) which can provide more efficient >>> implementations on some targets. >> >> This belongs in tree-ssa-math-opts.c:execute_optimize_bswap instead. >> > OK I'll have a look at that. Actually I modified fold-const.c because > it's here that the similar 32 bits pattern is turned into a rotate. > >> When I implemented __builtin_bswap16, I didn't add this because I thought this >> would be overkill since the RTL combiner should be able to catch the pattern. >> Have you investigated on this front? But I don't have a strong opinion. >> > No I didn't. As I said above, I looked at where the 32 bits pattern > was handled and added the 16 bits one. > > Christophe. Here is a new patch, modifying tree-ssa-math-opts.c as you suggested. It's indeed simpler :-) Validated with qemu-arm on target arm-none-linux-gnueabi. OK? Christophe. 2012-09-21 Christophe Lyon gcc/ * tree-ssa-math-opts.c (bswap_stats): Add found_16bit field. (execute_optimize_bswap): Add support for builtin_bswap16. gcc/testsuite/ * gcc.target/arm/builtin-bswap16-1.c: New testcase. diff --git a/gcc/tree-ssa-math-opts.c b/gcc/tree-ssa-math-opts.c index c3392fb..340f0df 100644 --- a/gcc/tree-ssa-math-opts.c +++ b/gcc/tree-ssa-math-opts.c @@ -154,6 +154,9 @@ static struct static struct { + /* Number of hand-written 16-bit bswaps found. */ + int found_16bit; + /* Number of hand-written 32-bit bswaps found. */ int found_32bit; @@ -1792,9 +1795,9 @@ static unsigned int execute_optimize_bswap (void) { basic_block bb; - bool bswap32_p, bswap64_p; + bool bswap16_p, bswap32_p, bswap64_p; bool changed = false; - tree bswap32_type = NULL_TREE, bswap64_type = NULL_TREE; + tree bswap16_type = NULL_TREE, bswap32_type = NULL_TREE, bswap64_type = NULL_TREE; if (BITS_PER_UNIT != 8) return 0; @@ -1802,17 +1805,25 @@ execute_optimize_bswap (void) if (sizeof (HOST_WIDEST_INT) < 8) return 0; + bswap16_p = (builtin_decl_explicit_p (BUILT_IN_BSWAP16) + && optab_handler (bswap_optab, HImode) != CODE_FOR_nothing); bswap32_p = (builtin_decl_explicit_p (BUILT_IN_BSWAP32) && optab_handler (bswap_optab, SImode) != CODE_FOR_nothing); bswap64_p = (builtin_decl_explicit_p (BUILT_IN_BSWAP64) && (optab_handler (bswap_optab, DImode) != CODE_FOR_nothing || (bswap32_p && word_mode == SImode))); - if (!bswap32_p && !bswap64_p) + if (!bswap16_p && !bswap32_p && !bswap64_p) return 0; /* Determine the argument type of the builtins. The code later on assumes that the return and argument type are the same. */ + if (bswap16_p) + { + tree fndecl = builtin_decl_explicit (BUILT_IN_BSWAP16); + bswap16_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); + } + if (bswap32_p) { tree fndecl = builtin_decl_explicit (BUILT_IN_BSWAP32); @@ -1852,6 +1863,13 @@ execute_optimize_bswap (void) switch (type_size) { + case 16: + if (bswap16_p) + { + fndecl = builtin_decl_explicit (BUILT_IN_BSWAP16); + bswap_type = bswap16_type; + } + break; case 32: if (bswap32_p) { @@ -1879,7 +1897,9 @@ execute_optimize_bswap (void) continue; changed = true; - if (type_size == 32) + if (type_size == 16) + bswap_stats.found_16bit++; + else if (type_size == 32) bswap_stats.found_32bit++; else bswap_stats.found_64bit++; @@ -1924,6 +1944,8 @@ execute_optimize_bswap (void) } } + statistics_counter_event (cfun, "16-bit bswap implementations found", + bswap_stats.found_16bit); statistics_counter_event (cfun, "32-bit bswap implementations found", bswap_stats.found_32bit); statistics_counter_event (cfun, "64-bit bswap implementations found", diff --git a/gcc/testsuite/gcc.target/arm/builtin-bswap16-1.c b/gcc/testsuite/gcc.target/arm/builtin-bswap16-1.c new file mode 100644 index 0000000..6920f00 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/builtin-bswap16-1.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ +/* { dg-require-effective-target arm_arch_v6_ok } */ +/* { dg-add-options arm_arch_v6 } */ +/* { dg-final { scan-assembler-not "orr\[ \t\]" } } */ + +unsigned short swapu16_1 (unsigned short x) +{ + return (x << 8) | (x >> 8); +} + +unsigned short swapu16_2 (unsigned short x) +{ + return (x >> 8) | (x << 8); +}