From patchwork Thu Feb 28 18:27:48 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 224131 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 43EB62C0294 for ; Fri, 1 Mar 2013 05:28:10 +1100 (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=1362680891; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Date:From:To:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:User-Agent:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=v/alY0u0TxKWuBfMbuE2qFWkhzc=; b=yTNYXUJ/9L58CEf cfYWGqC8FGDGwcUDDtk6J9Y9MyrvON45vyO5PG7FEb+bUpbnKO8ExCms4Dj3lFVk tLbeRXYlBQ8RQWPTxDhPQKXSwtxxLV36lwzE550lWTUeVFr+Z0qd2/6x5RDMe8gm al4HwyHzZwGeKpWpXPB76F7iZBXU= 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:Received:Date:From:To:Subject:Message-ID:MIME-Version:Content-Type:Content-Disposition:User-Agent:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=RbQXbos90XMeTZdBkASRHVzdBb/MvWJVjUNd9xEa0BW9r8QUrL4ORMDrQd+paR MhdVDAbJ7aQaSluasbwBpge2u5XJI4ZrX3SNy0PvEsS4M72zdI2ajUbKZ3ayF4lJ l5czHK1azwKx4Hd0MnuWOZU22N4QmszInmp9XqiUYp89M=; Received: (qmail 27399 invoked by alias); 28 Feb 2013 18:28:02 -0000 Received: (qmail 27387 invoked by uid 22791); 28 Feb 2013 18:28:01 -0000 X-SWARE-Spam-Status: No, hits=-6.7 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, KHOP_SPAMHAUS_DROP, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, RP_MATCHES_RCVD, SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 28 Feb 2013 18:27:53 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r1SIRq7w008204 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 28 Feb 2013 13:27:52 -0500 Received: from redhat.com (ovpn-116-20.ams2.redhat.com [10.36.116.20]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r1SIRmtj013369 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO) for ; Thu, 28 Feb 2013 13:27:51 -0500 Date: Thu, 28 Feb 2013 19:27:48 +0100 From: Marek Polacek To: GCC Patches Subject: [PATCH] Fix PR56478 Message-ID: <20130228182748.GD15445@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) 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 The following testcase ICEd because we were trying to negate "-9223372036854775808", which results in UB, since it's LLONG_MAX + 1. Then we got a SIGFPE on -LLONG_MIN / -1 So fixed by doing the arithmetics on trees, and in case we got an overflow, we bail out. The hunk probability = (double) REG_BR_PROB_BASE * compare_count / loop_count; in there should be probably handled in the same way. But I'll handle that separately. In the first hunk, I did s/int/HOST_WIDE_INT/, because HWI is what tree_low_cst returns. For why that could be a problem, see the Jakub's comment in the PR. Regtested/bootstrapped on x86_64-linux, ok for trunk? 2013-02-28 Marek Polacek PR tree-optimization/56478 * predict.c (is_comparison_with_loop_invariant_p): Change the type of step to HOST_WIDE_INT. (predict_iv_comparison): Do the computation on trees, return in case of an overflow. * gcc.dg/torture/pr56478.c: New test. Marek --- gcc/predict.c.mp 2013-02-28 17:26:47.950247877 +0100 +++ gcc/predict.c 2013-02-28 17:26:56.855275792 +0100 @@ -1034,7 +1034,7 @@ is_comparison_with_loop_invariant_p (gim tree op0, op1, bound, base; affine_iv iv0, iv1; enum tree_code code; - int step; + HOST_WIDE_INT step; code = gimple_cond_code (stmt); *loop_invariant = NULL; @@ -1224,11 +1224,29 @@ predict_iv_comparison (struct loop *loop && host_integerp (compare_base, 0)) { int probability; + tree step_var, loop_count_var; HOST_WIDE_INT compare_count; HOST_WIDE_INT loop_bound = tree_low_cst (loop_bound_var, 0); HOST_WIDE_INT compare_bound = tree_low_cst (compare_var, 0); HOST_WIDE_INT base = tree_low_cst (compare_base, 0); - HOST_WIDE_INT loop_count = (loop_bound - base) / compare_step; + + /* We want to do the arithmetics on trees (and punt in + case of an overflow). */ + step_var = build_int_cst (NULL_TREE, compare_step); + gcc_assert (TREE_CODE (step_var) == INTEGER_CST); + + /* Compute (loop_bound - base) / compare_step. */ + loop_count_var + = int_const_binop (TRUNC_DIV_EXPR, + int_const_binop (MINUS_EXPR, + loop_bound_var, + compare_base), + step_var); + + if (TREE_OVERFLOW_P (loop_count_var)) + return; + + HOST_WIDE_INT loop_count = tree_low_cst (loop_count_var, 0); if ((compare_step > 0) ^ (compare_code == LT_EXPR || compare_code == LE_EXPR)) --- gcc/testsuite/gcc.dg/torture/pr56478.c.mp 2013-02-28 17:32:26.430308968 +0100 +++ gcc/testsuite/gcc.dg/torture/pr56478.c 2013-02-28 18:15:41.641379179 +0100 @@ -0,0 +1,12 @@ +/* PR tree-optimization/56478 */ +/* { dg-do compile } */ + +int a; + +void +foo () +{ + int b; + for (b = 0;; b++) + a = 0 < -__LONG_LONG_MAX__ - 1 - b ? : 0; +}