From patchwork Tue Feb 13 17:51:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 873078 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-473185-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="G26lyPZM"; 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 3zgqq45yZgz9s82 for ; Wed, 14 Feb 2018 04:51:43 +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=rwMDQhcxHa6U6euiTCRDFSs88ZnB5 +ELR4VWsqLoWs2IJpmgEp6xLavH1EOyCWBP1itPzU5HJ2J+Th89fH9P9/KXSbLx3 xFlrURRtqV1cxXUqOL+qlww+GPADAoEUzVMq6EHlZfGcUMlvDOtpJBTs+B4S2De6 zOYN2u6ip5QW7s= 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=0pqyUmHsej4hqGqvrCggGrwlwEg=; b=G26 lyPZMsTHB8296mhbzaUgb2aQgmJiR51b01kkWLQX1MJaqasXfOUcY75t4dkUzaZd 3DkHN/z/3GtNayEWSJzXvxWs6M42CirUhb6Sl97HRQFKoAqs39hprzmOajmhzxAG +oUvl85BCNbECT/98AVPNUyUUqna5P6NZJsYrUUM= Received: (qmail 27517 invoked by alias); 13 Feb 2018 17:51:36 -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 27508 invoked by uid 89); 13 Feb 2018 17:51:36 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-12.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 13 Feb 2018 17:51:34 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CDFDD4022909; Tue, 13 Feb 2018 17:51:32 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-204-85.brq.redhat.com [10.40.204.85]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8BDB221411B6; Tue, 13 Feb 2018 17:51:32 +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 w1DHpUGl023739; Tue, 13 Feb 2018 18:51:30 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id w1DHpT9q023738; Tue, 13 Feb 2018 18:51:29 +0100 Date: Tue, 13 Feb 2018 18:51:29 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] FIx endless match.pd recursion on cst1 + cst2 + cst3 (PR tree-optimization/84334) Message-ID: <20180213175129.GS5867@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.1 (2017-09-22) X-IsSubscribed: yes Hi! On the following testcase, we recurse infinitely, because we have float re-association enabled, but also rounding-math, so we try to optimize (cst1 + cst2) + cst3 as (cst2 + cst3) + cst1 but (cst2 + cst3) doesn't simplify and we try again and optimize it as (cst3 + cst1) + cst2 and then (cst1 + cst2) + cst3 and so on forever. If @0 is not a CONSTANT_CLASS_P, there is not a problem, if it is, the code just checks if we can actually simplify the operation between cst2 and cst3 into a constant. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2018-02-13 Jakub Jelinek PR tree-optimization/84334 * match.pd ((A +- CST1) +- CST2 -> A + CST3): If A is also a CONSTANT_CLASS_P, only optimize if we can fold the operation between CST1 and CST2 into a constant. * gcc.dg/pr84334.c: New test. Jakub --- gcc/match.pd.jj 2018-02-13 09:33:31.000000000 +0100 +++ gcc/match.pd 2018-02-13 12:14:08.108314686 +0100 @@ -1733,9 +1733,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) CONSTANT_CLASS_P@2) /* If one of the types wraps, use that one. */ (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) - (if (outer_op == PLUS_EXPR) - (plus (view_convert @0) (inner_op @2 (view_convert @1))) - (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))) + /* If all 3 captures are CONSTANT_CLASS_P, only optimize if we + can simplify @2 with @1 into a constant, otherwise we might recurse + forever. */ + (if (CONSTANT_CLASS_P (@0)) + (with { tree cst = fold_unary (VIEW_CONVERT_EXPR, type, @1); + if (cst && CONSTANT_CLASS_P (cst)) + cst = const_binop (outer_op == PLUS_EXPR + ? inner_op : neg_inner_op, type, + @2, cst); } + (if (cst) + (outer_op (view_convert @0) { cst; }))) + (if (outer_op == PLUS_EXPR) + (plus (view_convert @0) (inner_op @2 (view_convert @1))) + (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))) (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) (if (outer_op == PLUS_EXPR) --- gcc/testsuite/gcc.dg/pr84334.c.jj 2018-02-13 12:18:12.765463667 +0100 +++ gcc/testsuite/gcc.dg/pr84334.c 2018-02-13 11:36:56.019632428 +0100 @@ -0,0 +1,12 @@ +/* PR tree-optimization/84334 */ +/* { dg-do compile } */ +/* { dg-options "-Ofast -frounding-math" } */ + +float +foo (void) +{ + float a = 9.999999974752427078783512115478515625e-7f; + float b = 1.999999994950485415756702423095703125e-6f; + float c = 4.999999873689375817775726318359375e-6f; + return a + b + c; +}