From patchwork Sat Sep 7 16:42:20 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Glisse X-Patchwork-Id: 273385 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 "www.sourceware.org", Issuer "StartCom Class 1 Primary Intermediate Server CA" (not verified)) by ozlabs.org (Postfix) with ESMTPS id F09BC2C011D for ; Sun, 8 Sep 2013 02:42:32 +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:subject:in-reply-to:message-id:references:mime-version :content-type; q=dns; s=default; b=Zeth4gv5ydkHXk/vL4DMAqzsopxHW zMQf5A9+RQVkFPIiE/rwZuk+zLulxnDC3hm9awkGq9WhM1yJ8LcTS9F4lWZbqQsk hqjJPRPz/k5RWbu6gEHZAvQ6rAVHZyyXlzr1nK7J1QJ7QHNC2hg8C40WXWS1bPkD P3j0gZ5zmSwc/0= 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:subject:in-reply-to:message-id:references:mime-version :content-type; s=default; bh=WzNA0V+kyTJ6YZHDMSdAP5ZGRLc=; b=of5 DC9004SJC0Ua1u3+agNhlO6P4wWt37MIK4JsnS8g0xXx3OMlVhkI5nvpWb8QS6i6 80BvhQZHxv+oAT2Ffw3QNe0MGczLUtaGBCCozyveVTVRvWuEyGfl7gek9INOJMDV 1YjLgAiLc0+sDPe22tUUm+uIEAp3IpR4TiunV9C0= Received: (qmail 30654 invoked by alias); 7 Sep 2013 16:42:26 -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 30631 invoked by uid 89); 7 Sep 2013 16:42:25 -0000 Received: from mail2-relais-roc.national.inria.fr (HELO mail2-relais-roc.national.inria.fr) (192.134.164.83) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (CAMELLIA256-SHA encrypted) ESMTPS; Sat, 07 Sep 2013 16:42:25 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL, BAYES_00, KHOP_DYNAMIC, KHOP_THREADED, RCVD_IN_PBL, RCVD_IN_RP_RNBL, RCVD_IN_SORBS_DUL autolearn=no version=3.3.2 X-HELO: mail2-relais-roc.national.inria.fr Received: from ip-27.net-81-220-32.lyon.rev.numericable.fr (HELO laptop-mg.local) ([81.220.32.27]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 07 Sep 2013 18:42:20 +0200 Date: Sat, 7 Sep 2013 18:42:20 +0200 (CEST) From: Marc Glisse To: gcc-patches@gcc.gnu.org Subject: Re: operator new returns nonzero In-Reply-To: Message-ID: References: User-Agent: Alpine 2.10 (DEB 1266 2009-07-14) MIME-Version: 1.0 On Sat, 7 Sep 2013, Marc Glisse wrote: > this patch teaches the compiler that operator new, when it can throw, isn't > allowed to return a null pointer. Note that it doesn't work for placement new > (that one may have to be handled in the front-end or the inliner), Placement new is a completely different thing. For one, it is nothrow (so the test doesn't apply). But mostly, it is a condition on the argument more than the result. Using the nonnull attribute would make sense, but the compiler doesn't seem clever enough to use that information. The easiest seems to be in the library: Though I am not sure what the policy is for this kind of thing. And that's assuming it is indeed undefined to pass a null pointer to it, I don't have a good reference for that. > and it will not work on user-defined operator new if they are inlined. But then if that operator new is inlined, it may already contain a line like if(p==0)throw(); which lets the compiler know all it needs. > I guess it > would be possible to teach the inliner to insert an assert_expr or something > when inlining such a function, but that's not for now. The code I removed > from vrp_visit_stmt was duplicated in stmt_interesting_for_vrp and thus > useless. > > I ran the c,c++ testsuite on a non-bootstrap compiler. I'll do more proper > testing when trunk bootstraps again. > > 2013-09-07 Marc Glisse > > PR c++/19476 > gcc/ > * fold-const.c (tree_expr_nonzero_warnv_p): Handle operator new. > * tree-vrp.c (gimple_stmt_nonzero_warnv_p, stmt_interesting_for_vrp): > Likewise. > (vrp_visit_stmt): Remove duplicated code. > > gcc/testsuite/ > * g++.dg/tree-ssa/pr19476-1.C: New file. > * g++.dg/tree-ssa/pr19476-2.C: Likewise. --- o/new 2013-09-07 11:11:17.388756320 +0200 +++ i/new 2013-09-07 18:00:32.204797291 +0200 @@ -144,9 +144,9 @@ // Default placement versions of operator new. inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT -{ return __p; } +{ if (!__p) __builtin_unreachable(); return __p; } inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT -{ return __p; } +{ if (!__p) __builtin_unreachable(); return __p; } // Default placement versions of operator delete. inline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPT { }