From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16164 invoked by alias); 6 May 2011 10:44:34 -0000 Received: (qmail 16047 invoked by uid 22791); 6 May 2011 10:44:33 -0000 X-SWARE-Spam-Status: No, hits=-2.0 required=5.0 tests=AWL,BAYES_00,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from nikam.ms.mff.cuni.cz (HELO nikam.ms.mff.cuni.cz) (195.113.20.16) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 06 May 2011 10:44:18 +0000 Received: from localhost (occam.kam.mff.cuni.cz [195.113.17.166]) by nikam.ms.mff.cuni.cz (Postfix) with ESMTP id 3F44D9AC4B2 for ; Fri, 6 May 2011 12:44:17 +0200 (CEST) Received: by localhost (Postfix, from userid 29025) id 3AEFA564185; Fri, 6 May 2011 12:44:17 +0200 (CEST) Date: Fri, 06 May 2011 10:47:00 -0000 From: Zdenek Dvorak To: gcc-patches@gcc.gnu.org Subject: [patch] PR 48837 Message-ID: <20110506104417.GA8664@kam.mff.cuni.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.18 (2008-05-17) Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org X-SW-Source: 2011-05/txt/msg00502.txt.bz2 Hi, when accumulator transformation is performed on a function like foo(a) { if (a > 0) return 1 + foo (a - 1) return bla(); } this becomes foo(a) { int tmp = 0; while (a > 0) tm = 1 + tmp; return tmp + bla(); } Before, bla was a tail-call, but after the optimization, it is not (since an addition has to be performed after the result of bla is known). However, we used to mark bla as tail-call, leading to a misscompilation later. Fixed by not marking tail-calls when the transformation is performed. Bootstrapped and regtested on i686. Zdenek PR tree-optimization/48837 * tree-tailcall.c (tree_optimize_tail_calls_1): Do not mark tailcalls when accumulator transformation is performed. * gcc.dg/pr48837.c: New testcase. Index: tree-tailcall.c =================================================================== --- tree-tailcall.c (revision 173354) +++ tree-tailcall.c (working copy) @@ -1021,6 +1021,14 @@ tree_optimize_tail_calls_1 (bool opt_tailcalls) integer_one_node); } + if (a_acc || m_acc) + { + /* When the tail call elimination using accumulators is performed, + statements adding the accumulated value are inserted at all exits. + This turns all other tail calls to non-tail ones. */ + opt_tailcalls = false; + } + for (; tailcalls; tailcalls = next) { next = tailcalls->next; Index: testsuite/gcc.dg/pr48837.c =================================================================== --- testsuite/gcc.dg/pr48837.c (revision 0) +++ testsuite/gcc.dg/pr48837.c (revision 0) @@ -0,0 +1,30 @@ +/* PR tree-optimization/48837 */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +void abort (void); + +__attribute__((noinline)) +int baz(void) +{ + return 1; +} + +inline const int *bar(const int *a, const int *b) +{ + return *a ? a : b; +} + +int foo(int a, int b) +{ + return a || b ? baz() : foo(*bar(&a, &b), 1) + foo(1, 0); +} + +int main(void) +{ + if (foo(0, 0) != 2) + abort(); + + return 0; +} +