From patchwork Sat May 25 17:11:22 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Glisse X-Patchwork-Id: 246351 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 3D4902C008F for ; Sun, 26 May 2013 03:11:33 +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:date :from:to:cc:subject:message-id:mime-version:content-type :content-id; q=dns; s=default; b=RvhZsfT9nd1bKPErpaepecZqMGw0GiI iYBQ7HUlSIvGVXo27p6KcmTykabCbRTSx9DbtiRFvrspjkIeh7KZWFcc12BbgwUx Ld7OZHIpQJdqEZ4gQlzJuS7OrhyIaPmNzjOCCdr4SKXt1fMejb7e7vQJJSu3LjRC aMV3WVgRheM4= 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:mime-version:content-type :content-id; s=default; bh=tVzfChURki4/cxxYmNkXO9mS7YQ=; b=GEms7 kBhGDfck387YqmwBAqNdhYEJ0uoDxOMGXpmryFsOgZjuRQ4mncqU6Tg860wzBg+L 9WIl7V3AT19ESys71wuSLqhZplRlm+ljU5kT3kUrmAvHxAz0YeLuQwNLnYgTEuDD 3bK4mNuOjEy9LCASLayJaO3RJcUEgctkVNTsFQ= Received: (qmail 15548 invoked by alias); 25 May 2013 17:11:27 -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 15534 invoked by uid 89); 25 May 2013 17:11:26 -0000 X-Spam-SWARE-Status: No, score=-5.3 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD autolearn=ham version=3.3.1 Received: from mail2-relais-roc.national.inria.fr (HELO mail2-relais-roc.national.inria.fr) (192.134.164.83) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Sat, 25 May 2013 17:11:25 +0000 Received: from stedding.saclay.inria.fr ([193.55.250.194]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 25 May 2013 19:11:23 +0200 Received: from glisse (helo=localhost) by stedding.saclay.inria.fr with local-esmtp (Exim 4.80) (envelope-from ) id 1UgI02-0003LQ-Jw; Sat, 25 May 2013 19:11:22 +0200 Date: Sat, 25 May 2013 19:11:22 +0200 (CEST) From: Marc Glisse To: gcc-patches@gcc.gnu.org cc: joseph@codesourcery.com Subject: Simplify (double)i != 0 Message-ID: User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-ID: X-Virus-Found: No Hello, this patch only handles the simple case where the constant is 0, I'll keep the PR open for the more general case. Note that if we split flag_trapping_math into no|weak|strict, the test here would be !=strict, since we only remove trapping operations. Passes bootstrap+testsuite on x86_64-linux-gnu. 2013-05-27 Marc Glisse PR tree-optimization/57371 gcc/ * fold-const.c (fold_comparison): Fold comparison of a FLOAT_EXPR with 0. gcc/testsuite/ * gcc.dg/pr57371-1.c: New testcase. * gcc.dg/pr57371-2.c: Likewise. * gcc.dg/pr57371-3.c: Likewise. Index: fold-const.c =================================================================== --- fold-const.c (revision 199323) +++ fold-const.c (working copy) @@ -9362,20 +9362,38 @@ fold_comparison (location_t loc, enum tr } /* Fold comparisons against infinity. */ if (REAL_VALUE_ISINF (cst) && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))) { tem = fold_inf_compare (loc, code, type, arg0, arg1); if (tem != NULL_TREE) return tem; } + + /* (double)i CMP 0 is just i CMP 0. See PR 57371 for how this + can be extended to non-zero constants. */ + if (TREE_CODE (arg0) == FLOAT_EXPR && real_zerop (arg1)) + { + tree inner = TREE_OPERAND (arg0, 0); + tree itype = TREE_TYPE (inner); + tree ftype = TREE_TYPE (arg0); + /* If ftype cannot represent exactly all values of itype, + we may have an inexact exception. If the conversion from + itype to ftype may overflow (unsigned __int128 to float), + we may have an overflow exception. */ + if (!flag_trapping_math + || (unsigned) significand_size (TYPE_MODE (ftype)) + >= element_precision (itype) - !TYPE_UNSIGNED (itype)) + return fold_build2_loc (loc, code, type, inner, + build_zero_cst (itype)); + } } /* If this is a comparison of a real constant with a PLUS_EXPR or a MINUS_EXPR of a real constant, we can convert it into a comparison with a revised real constant as long as no overflow occurs when unsafe_math_optimizations are enabled. */ if (flag_unsafe_math_optimizations && TREE_CODE (arg1) == REAL_CST && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) Index: testsuite/gcc.dg/pr57371-3.c =================================================================== --- testsuite/gcc.dg/pr57371-3.c (revision 0) +++ testsuite/gcc.dg/pr57371-3.c (revision 0) @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-optimized -ftrapping-math" } */ + +int f (unsigned long long x) +{ + float y = x; + return y <= 0; +} + +/* { dg-final { scan-tree-dump "float" "optimized" } } */ +/* { dg-final { cleanup-tree-dump "optimized" } } */ Property changes on: testsuite/gcc.dg/pr57371-3.c ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision URL Added: svn:eol-style + native Index: testsuite/gcc.dg/pr57371-1.c =================================================================== --- testsuite/gcc.dg/pr57371-1.c (revision 0) +++ testsuite/gcc.dg/pr57371-1.c (revision 0) @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-optimized -ftrapping-math" } */ + +int f (char x) +{ + long double y = x; + return y <= 0; +} + +/* { dg-final { scan-tree-dump-not "double" "optimized" } } */ +/* { dg-final { cleanup-tree-dump "optimized" } } */ Property changes on: testsuite/gcc.dg/pr57371-1.c ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision URL Added: svn:eol-style + native Index: testsuite/gcc.dg/pr57371-2.c =================================================================== --- testsuite/gcc.dg/pr57371-2.c (revision 0) +++ testsuite/gcc.dg/pr57371-2.c (revision 0) @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-optimized -fno-trapping-math" } */ + +int f (unsigned long long x) +{ + float y = x; + return y <= 0; +} + +/* { dg-final { scan-tree-dump-not "float" "optimized" } } */ +/* { dg-final { cleanup-tree-dump "optimized" } } */