From patchwork Wed May 18 08:38:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kugan Vivekanandarajah X-Patchwork-Id: 623442 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.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3r8nfl1jx6z9vKg for ; Wed, 18 May 2016 18:38:53 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=PQUGGA9C; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; q=dns; s=default; b=KU+aqYJg95ScuOdkcP HnWZMTZcW3kgMl7zBJkHLqKJEHirvjwZ3CAV5lCAR5k4YWS1RQsEcVw6BqCKF9Uh lSOUK8AxDqZNWhlp4+yGj0OcpBS3s7h3cd22UjyjwgpYn+FjTkj6E6qjKe8lXDr9 3qFXVKEMlfe8Th6hhsCbZFltA= 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 :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; s=default; bh=eUeJz74orfSJilueb132yIOe 8u8=; b=PQUGGA9Cjt1VN9EU/coFIWQ0Pxx+trCDiJxJHA+XSMFZ8JG2WiQ/JHli gBE8l/HqHlb+lYfdrQYMoGt2mzcXhHg3fsSt9nt96A/TQmLB7fKhWDHUkDKwRR0W DHvqScHEzEp5l8PFftsgyfayUAvXVeBgLk2P9y6WQT9YN2EydxE= Received: (qmail 70023 invoked by alias); 18 May 2016 08:38:44 -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 69886 invoked by uid 89); 18 May 2016 08:38:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=reassociate_bb, UD:tree-ssa-reassoc.c X-HELO: mail-qg0-f45.google.com Received: from mail-qg0-f45.google.com (HELO mail-qg0-f45.google.com) (209.85.192.45) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Wed, 18 May 2016 08:38:32 +0000 Received: by mail-qg0-f45.google.com with SMTP id f92so21847291qgf.0 for ; Wed, 18 May 2016 01:38:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=fDvtLHoXbjT37LMUyb+r2UMKY+bx8He8c0Hd4uWlef8=; b=cFS5WU+ujOT6Wq1WSBZNUZqTspCQBxADkKf3NuRFiign2rNeNwzTOUBi8lr79s82qA Zy93W6N1+EQQY6CtebLF4HUwj8devawSfHjAlbaB4PkR1I22GP+R8V2hIcl3BJlbnt9N Rwmb+6Q/kg2xYTy/06Y+MQJcV+nvEIy+Mi3GdBIBeLqAEsVgFN0Tp8bm+GEhgtWBYcni +m996i2y9JqQDO8B08+qqKXKtsfv38hl45TI8naMMcm1+TSlsMCR/hyuUp8jNZvtvdUF joRKY12U1v5RVgPd90RHfq3+sZEEhth3JKDFkHCW3NZwuog5ypnB9d+5Uu2zZVMTALwQ Hufg== X-Gm-Message-State: AOPr4FWOkCj2rkI9lAFyWF4xHJB5di3hWsK6BbDuKmOC+iKSvniSoaEpIRqDmKVmHiLvo80MKJxxZNyQWHe+gBL0 MIME-Version: 1.0 X-Received: by 10.140.223.10 with SMTP id t10mr6427365qhb.96.1463560710332; Wed, 18 May 2016 01:38:30 -0700 (PDT) Received: by 10.200.42.71 with HTTP; Wed, 18 May 2016 01:38:30 -0700 (PDT) In-Reply-To: References: <56CFC02F.2070801@linaro.org> <56D42341.5090607@linaro.org> <5718B5B1.8030809@linaro.org> <571B7448.4080005@linaro.org> <572A96AE.10701@linaro.org> Date: Wed, 18 May 2016 18:38:30 +1000 Message-ID: Subject: Re: [RFC][PATCH][PR40921] Convert x + (-y * z * z) into x - y * z * z From: Kugan Vivekanandarajah To: Richard Biener Cc: "gcc-patches@gcc.gnu.org" X-IsSubscribed: yes >>> Please move the whole thing under the else { } case of the ops.length >>> == 0, ops.length == 1 test chain >>> as you did for the actual emit of the negate. >>> >> >> I see your point. However, when we remove the (-1) from the ops list, that >> intern can result in ops.length becoming 1. Therefore, I moved the the >> following if (negate_result), outside the condition. > > Ah, indeed. But now you have to care for ops.length () == 0 and thus > the unconditonally ops.last () may now trap. So I suggest to > do Done. > Yes - the patch is ok with the above suggested change. While testing on an arm variant, vector types are not handled. Therefore, I had to change: + || ((TREE_CODE (last->op) == REAL_CST) + && real_equal (&TREE_REAL_CST (last->op), &dconstm1)) to + || real_minus_onep (last->op)) Is this Still OK. Bootstrap and regression testing on ARM, AARCH64 and x86-64 didn’t have any new regressions. Thanks, Kugan diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr40921.c b/gcc/testsuite/gcc.dg/tree-ssa/pr40921.c index e69de29..3a5a23a 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/pr40921.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr40921.c @@ -0,0 +1,26 @@ + +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized -ffast-math" } */ + +unsigned int foo (unsigned int x, unsigned int y, unsigned int z) +{ + return x + (-y * z * z); +} + +float bar (float x, float y, float z) +{ + return x + (-y * z * z); +} + +float bar2 (float x, float y, float z) +{ + return x + (-y * z * z * 5.0f); +} + +float bar3 (float x, float y, float z) +{ + return x + (-y * x * -z); +} + + +/* { dg-final { scan-tree-dump-times "_* = -y_" 0 "optimized" } } */ diff --git a/gcc/tree-ssa-reassoc.c b/gcc/tree-ssa-reassoc.c index 7408977..b54e3eb 100644 --- a/gcc/tree-ssa-reassoc.c +++ b/gcc/tree-ssa-reassoc.c @@ -4252,6 +4252,45 @@ acceptable_pow_call (gimple *stmt, tree *base, HOST_WIDE_INT *exponent) return true; } +/* Try to derive and add operand entry for OP to *OPS. Return false if + unsuccessful. */ + +static bool +try_special_add_to_ops (vec *ops, + enum tree_code code, + tree op, gimple* def_stmt) +{ + tree base = NULL_TREE; + HOST_WIDE_INT exponent = 0; + + if (TREE_CODE (op) != SSA_NAME) + return false; + + if (code == MULT_EXPR + && acceptable_pow_call (def_stmt, &base, &exponent)) + { + add_repeat_to_ops_vec (ops, base, exponent); + gimple_set_visited (def_stmt, true); + return true; + } + else if (code == MULT_EXPR + && is_gimple_assign (def_stmt) + && gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR + && !HONOR_SNANS (TREE_TYPE (op)) + && (!HONOR_SIGNED_ZEROS (TREE_TYPE (op)) + || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (op)))) + { + tree rhs1 = gimple_assign_rhs1 (def_stmt); + tree cst = build_minus_one_cst (TREE_TYPE (op)); + add_to_ops_vec (ops, rhs1); + add_to_ops_vec (ops, cst); + gimple_set_visited (def_stmt, true); + return true; + } + + return false; +} + /* Recursively linearize a binary expression that is the RHS of STMT. Place the operands of the expression tree in the vector named OPS. */ @@ -4266,8 +4305,6 @@ linearize_expr_tree (vec *ops, gimple *stmt, bool binrhsisreassoc = false; enum tree_code rhscode = gimple_assign_rhs_code (stmt); struct loop *loop = loop_containing_stmt (stmt); - tree base = NULL_TREE; - HOST_WIDE_INT exponent = 0; if (set_visited) gimple_set_visited (stmt, true); @@ -4303,24 +4340,10 @@ linearize_expr_tree (vec *ops, gimple *stmt, if (!binrhsisreassoc) { - if (rhscode == MULT_EXPR - && TREE_CODE (binrhs) == SSA_NAME - && acceptable_pow_call (binrhsdef, &base, &exponent)) - { - add_repeat_to_ops_vec (ops, base, exponent); - gimple_set_visited (binrhsdef, true); - } - else + if (!try_special_add_to_ops (ops, rhscode, binrhs, binrhsdef)) add_to_ops_vec (ops, binrhs); - if (rhscode == MULT_EXPR - && TREE_CODE (binlhs) == SSA_NAME - && acceptable_pow_call (binlhsdef, &base, &exponent)) - { - add_repeat_to_ops_vec (ops, base, exponent); - gimple_set_visited (binlhsdef, true); - } - else + if (!try_special_add_to_ops (ops, rhscode, binlhs, binlhsdef)) add_to_ops_vec (ops, binlhs); return; @@ -4360,14 +4383,7 @@ linearize_expr_tree (vec *ops, gimple *stmt, linearize_expr_tree (ops, SSA_NAME_DEF_STMT (binlhs), is_associative, set_visited); - if (rhscode == MULT_EXPR - && TREE_CODE (binrhs) == SSA_NAME - && acceptable_pow_call (SSA_NAME_DEF_STMT (binrhs), &base, &exponent)) - { - add_repeat_to_ops_vec (ops, base, exponent); - gimple_set_visited (SSA_NAME_DEF_STMT (binrhs), true); - } - else + if (!try_special_add_to_ops (ops, rhscode, binrhs, binrhsdef)) add_to_ops_vec (ops, binrhs); } @@ -5127,6 +5143,24 @@ reassociate_bb (basic_block bb) powi_result = attempt_builtin_powi (stmt, &ops); } + operand_entry *last; + bool negate_result = false; + if (ops.length () > 1 + && rhs_code == MULT_EXPR) + { + last = ops.last (); + if (((TREE_CODE (last->op) == INTEGER_CST + && integer_minus_onep (last->op)) + || real_minus_onep (last->op)) + && !HONOR_SNANS (TREE_TYPE (lhs)) + && (!HONOR_SIGNED_ZEROS (TREE_TYPE (lhs)) + || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (lhs)))) + { + ops.pop (); + negate_result = true; + } + } + /* If the operand vector is now empty, all operands were consumed by the __builtin_powi optimization. */ if (ops.length () == 0) @@ -5189,6 +5223,18 @@ reassociate_bb (basic_block bb) gsi_insert_after (&gsi, mul_stmt, GSI_NEW_STMT); } } + + if (negate_result) + { + stmt = SSA_NAME_DEF_STMT (lhs); + tree tmp = make_ssa_name (TREE_TYPE (lhs)); + gimple_set_lhs (stmt, tmp); + gassign *neg_stmt = gimple_build_assign (lhs, NEGATE_EXPR, + tmp); + gimple_stmt_iterator gsi = gsi_for_stmt (stmt); + gsi_insert_after (&gsi, neg_stmt, GSI_NEW_STMT); + update_stmt (stmt); + } } } }