From patchwork Wed Jun 19 04:08:37 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Law X-Patchwork-Id: 252487 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 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 7E6CE2C0303 for ; Wed, 19 Jun 2013 14:08:49 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:subject:content-type; q= dns; s=default; b=ppS6mkPz5JoLNiN2E74GGPI4g1WW/NBFafbRKPRIi6KqJ9 kQ2lW4vCMZEwA4uOCFIDtWDJWSm+jwxY1y5MkJgS7UqMdN0L5T0vpwvg1i30Dkmj ZmGtCeQEalrjwqKsa9TW/gN0aHhAiJ1Bi+tbOz41MvBCpjJGE8I41EfoKM+NY= 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 :message-id:date:from:mime-version:to:subject:content-type; s= default; bh=GXLh9LTw5zmZhIfMDqW5kp2ww64=; b=HUDG/oBy1TVe6glyvc+M C9qoUdKF4Ph8wh5Kp/4mtvQsYQe2e/dnHcLgqTGyjDWj26yzDURadLAVZA/tawjT lt3O2Jn04yz2KnT34Qg+MsZ06bHdgBC0V0WKb9jhQBSp9K239rhVglEKOaeR7Eoy zeUzkbCGDWWZ7F1wCYV7mB4= Received: (qmail 25163 invoked by alias); 19 Jun 2013 04:08:42 -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 25152 invoked by uid 89); 19 Jun 2013 04:08:42 -0000 X-Spam-SWARE-Status: No, score=-7.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_W, RCVD_IN_HOSTKARMA_WL, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS, TW_TM autolearn=ham version=3.3.1 Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Wed, 19 Jun 2013 04:08:41 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r5J48dfR012667 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 19 Jun 2013 00:08:39 -0400 Received: from stumpy.slc.redhat.com (ovpn-113-62.phx2.redhat.com [10.3.113.62]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r5J48cI8029982 for ; Wed, 19 Jun 2013 00:08:39 -0400 Message-ID: <51C12EC5.709@redhat.com> Date: Tue, 18 Jun 2013 22:08:37 -0600 From: Jeff Law User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130612 Thunderbird/17.0.6 MIME-Version: 1.0 To: gcc-patches Subject: [PATCH] Improve folding of bitwise ops feeding conditionals for single bit types X-Virus-Found: No The notable changes since the last version: First, it should properly handle signed single bit types, though I haven't tested it with real code. Second, the transformation is only applied when the result is used in a conditional. Thus it's much less likely to pessimize targets with and-not instructions as it's highly likely we'll eliminate two gimple statements rather than just one. Other comments (such as not needing to retrieve gsi_stmt) were also addressed. Testcase was renamed, but is otherwise unchanged. Bootstrapped and regression tested on x86_64-unknown-linux-gnu. OK for the trunk? * tree-ssa-forwprop.c (simplify_bitwise_binary_boolean): New function. (simplify_bitwise_binary): Use it to simpify certain binary ops on booleans. * gcc.dg/tree-ssa/forwprop-28.c: New test. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/forwprop-28.c b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-28.c new file mode 100644 index 0000000..2c42065 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-28.c @@ -0,0 +1,76 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-forwprop1" } */ + +extern char * frob (void); +extern _Bool testit(void); + +test (int code) +{ + char * temp = frob();; + int rotate = (code == 22); + if (temp == 0 && !rotate) + oof(); +} + +test_2 (int code) +{ + char * temp = frob(); + int rotate = (code == 22); + if (!rotate && temp == 0) + oof(); +} + + +test_3 (int code) +{ + char * temp = frob(); + int rotate = (code == 22); + if (!rotate || temp == 0) + oof(); +} + + +test_4 (int code) +{ + char * temp = frob(); + int rotate = (code == 22); + if (temp == 0 || !rotate) + oof(); +} + + +test_5 (int code) +{ + _Bool temp = testit();; + _Bool rotate = (code == 22); + if (temp == 0 && !rotate) + oof(); +} + +test_6 (int code) +{ + _Bool temp = testit(); + _Bool rotate = (code == 22); + if (!rotate && temp == 0) + oof(); +} + + +test_7 (int code) +{ + _Bool temp = testit(); + _Bool rotate = (code == 22); + if (!rotate || temp == 0) + oof(); +} + + +test_8 (int code) +{ + _Bool temp = testit(); + _Bool rotate = (code == 22); + if (temp == 0 || !rotate) + oof(); +} + +/* { dg-final { scan-tree-dump-times "Replaced" 8 "forwprop1"} } */ diff --git a/gcc/tree-ssa-forwprop.c b/gcc/tree-ssa-forwprop.c index c6a7eaf..29a0bb7 100644 --- a/gcc/tree-ssa-forwprop.c +++ b/gcc/tree-ssa-forwprop.c @@ -1870,6 +1870,52 @@ hoist_conversion_for_bitop_p (tree to, tree from) return false; } +/* GSI points to a statement of the form + + result = OP0 CODE OP1 + + Where OP0 and OP1 are single bit SSA_NAMEs and CODE is either + BIT_AND_EXPR or BIT_IOR_EXPR. + + If OP0 is fed by a bitwise negation of another single bit SSA_NAME, + then we can simplify the two statements into a single LT_EXPR or LE_EXPR + when code is BIT_AND_EXPR and BIT_IOR_EXPR respectively. + + If a simplification is mode, return TRUE, else return FALSE. */ +static bool +simplify_bitwise_binary_boolean (gimple_stmt_iterator *gsi, + enum tree_code code, + tree op0, tree op1) +{ + gimple op0_def_stmt = SSA_NAME_DEF_STMT (op0); + + if (!is_gimple_assign (op0_def_stmt) + || (gimple_assign_rhs_code (op0_def_stmt) != BIT_NOT_EXPR)) + return false; + + tree x = gimple_assign_rhs1 (op0_def_stmt); + if (TREE_CODE (x) == SSA_NAME + && INTEGRAL_TYPE_P (TREE_TYPE (x)) + && TYPE_PRECISION (TREE_TYPE (x)) == 1 + && TYPE_UNSIGNED (TREE_TYPE (x)) == TYPE_UNSIGNED (TREE_TYPE (op1))) + { + enum tree_code newcode; + + gimple stmt = gsi_stmt (*gsi); + gimple_assign_set_rhs1 (stmt, x); + gimple_assign_set_rhs2 (stmt, op1); + if (code == BIT_AND_EXPR) + newcode = TYPE_UNSIGNED (TREE_TYPE (x)) ? LT_EXPR : GT_EXPR; + else + newcode = TYPE_UNSIGNED (TREE_TYPE (x)) ? LE_EXPR : GE_EXPR; + gimple_assign_set_rhs_code (stmt, newcode); + update_stmt (stmt); + return true; + } + return false; + +} + /* Simplify bitwise binary operations. Return true if a transformation applied, otherwise return false. */ @@ -2117,8 +2163,44 @@ simplify_bitwise_binary (gimple_stmt_iterator *gsi) return true; } } - } + /* If arg1 and arg2 are booleans (or any single bit type) + then try to simplify: + + (~X & Y) -> X < Y + (X & ~Y) -> Y < X + (~X | Y) -> X <= Y + (X | ~Y) -> Y <= X + + But only do this if our result feeds into a comparison as + this transformation is not always a win, particularly on + targets with and-not instructions. */ + if (TREE_CODE (arg1) == SSA_NAME + && TREE_CODE (arg2) == SSA_NAME + && INTEGRAL_TYPE_P (TREE_TYPE (arg1)) + && TYPE_PRECISION (TREE_TYPE (arg1)) == 1 + && TYPE_PRECISION (TREE_TYPE (arg2)) == 1 + && (TYPE_UNSIGNED (TREE_TYPE (arg1)) + == TYPE_UNSIGNED (TREE_TYPE (arg2)))) + { + use_operand_p use_p; + gimple use_stmt; + + if (single_imm_use (gimple_assign_lhs (stmt), &use_p, &use_stmt)) + { + if (gimple_code (use_stmt) == GIMPLE_COND + && gimple_cond_lhs (use_stmt) == gimple_assign_lhs (stmt) + && integer_zerop (gimple_cond_rhs (use_stmt)) + && gimple_cond_code (use_stmt) == NE_EXPR) + { + if (simplify_bitwise_binary_boolean (gsi, code, arg1, arg2)) + return true; + if (simplify_bitwise_binary_boolean (gsi, code, arg2, arg1)) + return true; + } + } + } + } return false; }