From patchwork Fri Apr 27 10:01:01 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 155454 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 734CAB6F9D for ; Fri, 27 Apr 2012 20:01:25 +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=1336125686; h=Comment: DomainKey-Signature:Received:Received:Received:Received:From:To: Cc:Subject:Date:Message-Id:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=iNYYeTEd7lF078HpJO96COz8Ci4=; b=vov2Hm5gVu4YmO4 /YPqRDeShe5wA2CWMHodGV5LV3K3yMForlOGG2RrumypfSbNfWmkBfVVEf/veCDN CY87C9mqSkefPNY8gT6QrmGizJJHigFOSnHSApJyLHhdqQl5usQMBV+p3Tf7PzTV qUF0Tzw4zYcmBqKpFbDFmtAnzIZ4= 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:From:To:Cc:Subject:Date:Message-Id:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=nOtrFUTTGVG19FngJWARlp8XQuzoVtvJptweTgnMmcP2isLV3t7paYD+QZ0YxL uAbwA4scqNLRBH/ZEP2Odpfub0FwZMXi7t0d2IumH8ehwVajXjkmc/M4v5mmHROy ygzH+6TShQwhQQ/d6tVVKKlLcgfzEenILaQG+bILWFnG0=; Received: (qmail 10417 invoked by alias); 27 Apr 2012 10:01:21 -0000 Received: (qmail 10407 invoked by uid 22791); 27 Apr 2012 10:01:20 -0000 X-SWARE-Spam-Status: No, hits=-6.3 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_HI, TW_EQ, TW_TM, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from fencepost.gnu.org (HELO fencepost.gnu.org) (208.118.235.10) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 27 Apr 2012 10:01:06 +0000 Received: from bonzini by fencepost.gnu.org with local (Exim 4.71) (envelope-from ) id 1SNhz6-0003X9-7m; Fri, 27 Apr 2012 06:01:04 -0400 From: Paolo Bonzini To: gcc-patches@gcc.gnu.org Cc: rguenther@suse.de, ubizjak@gmail.com Subject: [PATCH] teach phi-opt to produce -(a COND b) Date: Fri, 27 Apr 2012 12:01:01 +0200 Message-Id: <1335520861-29252-1-git-send-email-bonzini@gnu.org> 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 This patch teaches phiopt to look at phis whose arguments are -1 and 0, and produce negated setcc statements. Bootstrapped/regtested x86_64-pc-linux-gnu, together with the patch for pr53138. Ok for mainline? Paolo 2012-04-27 Paolo Bonzini * tree-ssa-phiopt.c (conditional_replacement): Replace PHIs whose arguments are -1 and 0, by negating the result of the conditional. 2012-04-27 Paolo Bonzini * gcc.c-torture/execute/20120427-2.c: New testcase. * gcc.dg/tree-ssa/phi-opt-10.c: New testcase. Index: tree-ssa-phiopt.c =================================================================== --- tree-ssa-phiopt.c (revisione 186859) +++ tree-ssa-phiopt.c (copia locale) @@ -536,17 +536,21 @@ gimple_stmt_iterator gsi; edge true_edge, false_edge; tree new_var, new_var2; + bool neg; /* FIXME: Gimplification of complex type is too hard for now. */ if (TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE || TREE_CODE (TREE_TYPE (arg1)) == COMPLEX_TYPE) return false; - /* The PHI arguments have the constants 0 and 1, then convert - it to the conditional. */ + /* The PHI arguments have the constants 0 and 1, or 0 and -1, then + convert it to the conditional. */ if ((integer_zerop (arg0) && integer_onep (arg1)) || (integer_zerop (arg1) && integer_onep (arg0))) - ; + neg = false; + else if ((integer_zerop (arg0) && integer_all_onesp (arg1)) + || (integer_zerop (arg1) && integer_all_onesp (arg0))) + neg = true; else return false; @@ -558,7 +562,7 @@ falls through into BB. There is a single PHI node at the join point (BB) and its arguments - are constants (0, 1). + are constants (0, 1) or (0, -1). So, given the condition COND, and the two PHI arguments, we can rewrite this PHI into non-branching code: @@ -585,12 +589,20 @@ edge so that we know when to invert the condition below. */ extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge); if ((e0 == true_edge && integer_zerop (arg0)) - || (e0 == false_edge && integer_onep (arg0)) + || (e0 == false_edge && !integer_zerop (arg0)) || (e1 == true_edge && integer_zerop (arg1)) - || (e1 == false_edge && integer_onep (arg1))) + || (e1 == false_edge && !integer_zerop (arg1))) cond = fold_build1_loc (gimple_location (stmt), - TRUTH_NOT_EXPR, TREE_TYPE (cond), cond); + TRUTH_NOT_EXPR, TREE_TYPE (cond), cond); + if (neg) + { + cond = fold_convert_loc (gimple_location (stmt), + TREE_TYPE (result), cond); + cond = fold_build1_loc (gimple_location (stmt), + NEGATE_EXPR, TREE_TYPE (cond), cond); + } + /* Insert our new statements at the end of conditional block before the COND_STMT. */ gsi = gsi_for_stmt (stmt); Index: testsuite/gcc.c-torture/execute/20120427-2.c =================================================================== --- testsuite/gcc.c-torture/execute/20120427-2.c (revisione 0) +++ testsuite/gcc.c-torture/execute/20120427-2.c (revisione 0) @@ -0,0 +1,38 @@ +typedef struct sreal +{ + unsigned sig; /* Significant. */ + int exp; /* Exponent. */ +} sreal; + +sreal_compare (sreal *a, sreal *b) +{ + if (a->exp > b->exp) + return 1; + if (a->exp < b->exp) + return -1; + if (a->sig > b->sig) + return 1; + if (a->sig < b->sig) + return -1; + return 0; +} + +sreal a[] = { + { 0, 0 }, + { 1, 0 }, + { 0, 1 }, + { 1, 1 } +}; + +int main() +{ + int i, j; + for (i = 0; i <= 3; i++) { + for (j = 0; j < 3; j++) { + if (i < j && sreal_compare(&a[i], &a[j]) != -1) abort(); + if (i == j && sreal_compare(&a[i], &a[j]) != 0) abort(); + if (i > j && sreal_compare(&a[i], &a[j]) != 1) abort(); + } + } + return 0; +} Index: testsuite/gcc.dg/tree-ssa/phi-opt-10.c =================================================================== --- testsuite/gcc.dg/tree-ssa/phi-opt-10.c (revisione 0) +++ testsuite/gcc.dg/tree-ssa/phi-opt-10.c (revisione 0) @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ + +int nem1_phi (unsigned long a) { return a ? -1 : 0; } +int eqm1_phi (unsigned long a) { return a ? 0 : -1; } + +int spaceship1 (long a) { return a > 0 ? 1 : a < 0 ? -1 : 0; } +int spaceship2 (long a) { return a > 0 ? 1 : a == 0 ? 0 : -1; } + +/* { dg-final { scan-tree-dump-times " = -D" 4 "optimized"} } */ +/* { dg-final { cleanup-tree-dump "optimized" } } */