From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8039 invoked by alias); 3 Feb 2010 13:01:34 -0000 Received: (qmail 7966 invoked by uid 22791); 3 Feb 2010 13:01:32 -0000 X-SWARE-Spam-Status: No, hits=-7.7 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI X-Spam-Check-By: sourceware.org Received: from cantor.suse.de (HELO mx1.suse.de) (195.135.220.2) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 03 Feb 2010 13:01:28 +0000 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.221.2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.suse.de (Postfix) with ESMTP id 0198490975 for ; Wed, 3 Feb 2010 14:01:26 +0100 (CET) Date: Wed, 03 Feb 2010 13:01:00 -0000 From: Richard Guenther To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix PR42944 Message-ID: User-Agent: Alpine 2.00 (LNX 1167 2008-08-23) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII 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: 2010-02/txt/msg00113.txt.bz2 We're a bit overeager in assuming that malloc/calloc do not clobber memory. Because glibc appearantly chooses to set errno. Fixed as follows, bootstrapped and tested on x86_64-unknown-linux-gnu. The testcase will show whether there are any systems that have a plain errno declaration. Committed to trunk. Richard. 2010-02-03 Richard Guenther PR tree-optimization/42944 * tree-ssa-alias.c (ref_maybe_used_by_call_p_1): Handle calloc. (call_may_clobber_ref_p_1): Likewise. Properly handle malloc and calloc clobbering errno. * gcc.dg/errno-1.c: New testcase. Index: gcc/tree-ssa-alias.c =================================================================== *** gcc/tree-ssa-alias.c (revision 156463) --- gcc/tree-ssa-alias.c (working copy) *************** ref_maybe_used_by_call_p_1 (gimple call, *** 963,968 **** --- 963,969 ---- /* The following builtins do not read from memory. */ case BUILT_IN_FREE: case BUILT_IN_MALLOC: + case BUILT_IN_CALLOC: case BUILT_IN_MEMSET: case BUILT_IN_FREXP: case BUILT_IN_FREXPF: *************** call_may_clobber_ref_p_1 (gimple call, a *** 1190,1195 **** --- 1191,1211 ---- /* Allocating memory does not have any side-effects apart from being the definition point for the pointer. */ case BUILT_IN_MALLOC: + case BUILT_IN_CALLOC: + /* Unix98 specifies that errno is set on allocation failure. + Until we properly can track the errno location assume it + is not a plain decl but anonymous storage in a different + translation unit. */ + if (flag_errno_math) + { + struct ptr_info_def *pi; + if (DECL_P (base)) + return false; + if (INDIRECT_REF_P (base) + && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME + && (pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0)))) + return pi->pt.anything || pi->pt.nonlocal; + } return false; /* Freeing memory kills the pointed-to memory. More importantly the call has to serve as a barrier for moving loads and stores Index: gcc/testsuite/gcc.dg/errno-1.c =================================================================== *** gcc/testsuite/gcc.dg/errno-1.c (revision 0) --- gcc/testsuite/gcc.dg/errno-1.c (revision 0) *************** *** 0 **** --- 1,17 ---- + /* { dg-do compile } */ + /* { dg-options "-O2" } */ + + #include + #include + + int main() + { + void *p; + errno = 0; + p = malloc (-1); + if (errno != 0) + do_not_optimize_away (); + return 0; + } + + /* { dg-final { scan-assembler "do_not_optimize_away" } } */