From patchwork Sun Feb 24 11:58:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1047486 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-496951-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="xYLTjaP+"; dkim-atps=neutral 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 446k9t1gb8z9sB3 for ; Sun, 24 Feb 2019 22:58:21 +1100 (AEDT) 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=Sb0JCQkNsrGETxT5xQQk3rw6PN5Mz /ODPjgIyerCRuZpQzRi//goVrYKhAOsDpLfIhhVr39fvgCix7JHEXEmzVXZ8E6n1 JY12H9x5vLoqaP+6IZJ0gAcB9eN6UpSTqHuSFAZU4NdNceQrY5MfnBMzDjxXQNxe 6by+rh9U7TCtLw= 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=UjYmQcMkfDinFYrf0F8nXEIXYjQ=; b=xYL TjaP+3rb6/FKvmu2/VvWAAdSx/J6wPpojBaA3E4Txp4ZQGX89bYUBlBiBoQyVgbg rZP+/wQCL4jkbBfaIJFZu0gAYq0XDJfbdKQKiGxuRf1Z3m8nXPyg4jfaeaVGFgX4 wjZDpEDhLx0bcMmMidkEp/klzmF02dNIVPzeZwlE= Received: (qmail 63180 invoked by alias); 24 Feb 2019 11:58:13 -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 63145 invoked by uid 89); 24 Feb 2019 11:58:13 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-10.9 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=type_precision, TYPE_PRECISION 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; Sun, 24 Feb 2019 11:58:12 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CA70D30832C4; Sun, 24 Feb 2019 11:58:10 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-117-64.ams2.redhat.com [10.36.117.64]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5D8FB5C290; Sun, 24 Feb 2019 11:58:10 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id x1OBw7fY013633; Sun, 24 Feb 2019 12:58:08 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id x1OBw5WT013632; Sun, 24 Feb 2019 12:58:05 +0100 Date: Sun, 24 Feb 2019 12:58:05 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Handle __builtin_bswap{16, 32, 64} in bitwise ccp (PR tree-optimization/89475) Message-ID: <20190224115805.GQ7611@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-IsSubscribed: yes Hi! These builtins are perfect candidates for bitwise ccp, the bytes are preserved, just byte-swapped. Noticed this while wondering why we haven't optimized the f9 function in another PR, bswap64 zero extended from 32-bits, later casted to 32-bit unsigned int is 0. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for GCC10? 2019-02-23 Jakub Jelinek PR tree-optimization/89475 * tree-ssa-ccp.c (evaluate_stmt): Handle BUILT_IN_BSWAP{16,32,64} calls. * gcc.dg/tree-ssa/pr89475.c: New test. Jakub --- gcc/tree-ssa-ccp.c.jj 2019-01-01 12:37:17.078976247 +0100 +++ gcc/tree-ssa-ccp.c 2019-02-23 22:35:23.888343273 +0100 @@ -1960,6 +1960,35 @@ evaluate_stmt (gimple *stmt) break; } + case BUILT_IN_BSWAP16: + case BUILT_IN_BSWAP32: + case BUILT_IN_BSWAP64: + val = get_value_for_expr (gimple_call_arg (stmt, 0), true); + if (val.lattice_val == UNDEFINED) + break; + else if (val.lattice_val == CONSTANT + && val.value + && TREE_CODE (val.value) == INTEGER_CST) + { + tree type = TREE_TYPE (gimple_call_lhs (stmt)); + int prec = TYPE_PRECISION (type); + wide_int wval = wi::to_wide (val.value); + val.value + = wide_int_to_tree (type, + wide_int::from (wval, prec, + UNSIGNED).bswap ()); + val.mask + = widest_int::from (wide_int::from (val.mask, prec, + UNSIGNED).bswap (), + UNSIGNED); + if (wi::sext (val.mask, prec) != -1) + break; + } + val.lattice_val = VARYING; + val.value = NULL_TREE; + val.mask = -1; + break; + default:; } } --- gcc/testsuite/gcc.dg/tree-ssa/pr89475.c.jj 2019-02-23 18:58:23.035845645 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr89475.c 2019-02-23 18:59:38.462607598 +0100 @@ -0,0 +1,104 @@ +/* PR tree-optimization/89475 */ +/* { dg-do compile { target { ilp32 || lp64 } } } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-not "link_error " "optimized" } } */ + +void link_error (void); + +unsigned short +f0 (unsigned short x) +{ + x &= 0xaa55; + x = __builtin_bswap16 (x); + if (x & 0xaa55) + link_error (); + return x; +} + +unsigned short +f1 (unsigned short x) +{ + x &= 0x55aa; + x = __builtin_bswap16 (x); + if (x & 0x55aa) + link_error (); + return x; +} + +unsigned int +f2 (unsigned int x) +{ + x &= 0x55aa5aa5U; + x = __builtin_bswap32 (x); + if (x & 0x5aa555aaU) + link_error (); + return x; +} + +unsigned long long int +f3 (unsigned long long int x) +{ + x &= 0x55aa5aa544cc2211ULL; + x = __builtin_bswap64 (x); + if (x & 0xeedd33bb5aa555aaULL) + link_error (); + return x; +} + +unsigned short +f4 (unsigned short x) +{ + x = __builtin_bswap32 (x); + if (x != 0) + link_error (); + return x; +} + +unsigned int +f5 (unsigned int x) +{ + x = __builtin_bswap64 (x); + if (x != 0) + link_error (); + return x; +} + +unsigned short +f6 (unsigned short x) +{ + x |= 0xaa55; + x = __builtin_bswap16 (x); + if ((x | 0xaa55) != 0xffff) + link_error (); + return x; +} + +unsigned short +f7 (unsigned short x) +{ + x |= 0x55aa; + x = __builtin_bswap16 (x); + if ((x | 0x55aa) != 0xffff) + link_error (); + return x; +} + +unsigned int +f8 (unsigned int x) +{ + x |= 0x55aa5aa5U; + x = __builtin_bswap32 (x); + if ((x | 0x5aa555aaU) != 0xffffffffU) + link_error (); + return x; +} + +unsigned long long int +f9 (unsigned long long int x) +{ + x |= 0x55aa5aa544cc2211ULL; + x = __builtin_bswap64 (x); + if ((x | 0xeedd33bb5aa555aaULL) != 0xffffffffffffffffULL) + link_error (); + return x; +}