From patchwork Thu Oct 27 06:28:19 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chung-Lin Tang X-Patchwork-Id: 122071 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 413C51007D8 for ; Thu, 27 Oct 2011 17:28:33 +1100 (EST) Received: (qmail 8919 invoked by alias); 27 Oct 2011 06:28:30 -0000 Received: (qmail 8910 invoked by uid 22791); 27 Oct 2011 06:28:29 -0000 X-SWARE-Spam-Status: No, hits=-1.1 required=5.0 tests=AWL, BAYES_00, FROM_12LTRDOM, TW_NV X-Spam-Check-By: sourceware.org Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 27 Oct 2011 06:28:14 +0000 Received: from svr-orw-exc-10.mgc.mentorg.com ([147.34.98.58]) by relay1.mentorg.com with esmtp id 1RJJRm-0004gm-0k from ChungLin_Tang@mentor.com for gcc-patches@gcc.gnu.org; Wed, 26 Oct 2011 23:28:14 -0700 Received: from SVR-ORW-FEM-05.mgc.mentorg.com ([147.34.97.43]) by SVR-ORW-EXC-10.mgc.mentorg.com with Microsoft SMTPSVC(6.0.3790.4675); Wed, 26 Oct 2011 23:27:31 -0700 Received: from [0.0.0.0] (147.34.91.1) by svr-orw-fem-05.mgc.mentorg.com (147.34.97.43) with Microsoft SMTP Server id 14.1.289.1; Wed, 26 Oct 2011 23:28:13 -0700 Message-ID: <4EA8FA03.10805@codesourcery.com> Date: Thu, 27 Oct 2011 14:28:19 +0800 From: Chung-Lin Tang User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 MIME-Version: 1.0 To: gcc-patches Subject: [PATCH] PR49720, infinite recursion in RTX simplification X-IsSubscribed: yes 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 Hi, this patch is for a specific case in RTX simplification where infinite recursion ensues, causing the out-of-stack segfault in PR49720. Tracking back the origin of this bug, the exact revision causing this was rev.92429, back in 2004. The clause in simplify-rtx.c:simplify_relational_operation_1(), transforming X + cst1 == cst2 into X == cst2 - cst1 can cause an oscillating recursive cycle like this: A + B == C <===> C - B == A When all A, B, and C are CONSTANT_P, but not simplifiable (in the PR testcase, they are all SYMBOL_REFs). The switching of XEXP(op0,1) to the other side of the equation, plus commutative canonicalizing then completes the cycle. This can be solved by using simplify_binary_relation() and returning NULL_RTX when simplification of the constant PLUS/MINUS fails, but I'm not sure that further working on X == CST2-CST1 will never yield more optimization, so the patch here just tries to plug the recursion hole for the exact condition. Bootstrapped and tested on i686 and x86_64 without regressions. Is this okay for trunk? Thanks, Chung-Lin 2011-10-27 Chung-Lin Tang PR rtl-optimization/49720 * simplify-rtx.c (simplify_relational_operation_1): Detect infinite recursion condition in "(eq/ne (plus x cst1) cst2) simplifies to (eq/ne x (cst2 - cst1))" case. testsuite/ * g++.dg/torture/pr49720.C: New test. Index: trunk/gcc/simplify-rtx.c =================================================================== --- trunk/gcc/simplify-rtx.c (revision 180421) +++ trunk/gcc/simplify-rtx.c (working copy) @@ -4352,10 +4352,20 @@ { rtx x = XEXP (op0, 0); rtx c = XEXP (op0, 1); + enum rtx_code invcode = op0code == PLUS ? MINUS : PLUS; + rtx tem = simplify_gen_binary (invcode, cmp_mode, op1, c); - c = simplify_gen_binary (op0code == PLUS ? MINUS : PLUS, - cmp_mode, op1, c); - return simplify_gen_relational (code, mode, cmp_mode, x, c); + /* Detect an infinite recursive condition, where we oscillate at this + simplification case between: + A + B == C <---> C - B == A, + where A, B, and C are all constants with non-simplifiable expressions, + usually SYMBOL_REFs. */ + if (GET_CODE (tem) == invcode + && CONSTANT_P (x) + && rtx_equal_p (c, XEXP (tem, 1))) + return NULL_RTX; + + return simplify_gen_relational (code, mode, cmp_mode, x, tem); } /* (ne:SI (zero_extract:SI FOO (const_int 1) BAR) (const_int 0))) is Index: trunk/gcc/testsuite/g++.dg/torture/pr49720.C =================================================================== --- trunk/gcc/testsuite/g++.dg/torture/pr49720.C (revision 0) +++ trunk/gcc/testsuite/g++.dg/torture/pr49720.C (revision 0) @@ -0,0 +1,8 @@ +/* { dg-do compile } */ + +extern char t_start[], t_end[], t_size[]; +bool foo (void) +{ + long size = reinterpret_cast(t_size); + return (size == t_end - t_start); +}