From patchwork Thu Jun 25 15:38:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1317008 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=E9nVUebG; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49t41c0Trmz9sRW for ; Fri, 26 Jun 2020 01:39:00 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DD8A3388700F; Thu, 25 Jun 2020 15:38:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD8A3388700F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1593099537; bh=WSpfo6kSp2+egBmnp2wX8frFqKnEjbdVrONTkm57h/Q=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=E9nVUebG0IvfemF3jTT2/Ggbxi+0EhLAGoxPHi2qD4MIVDZmDF5H6bjhjAmGaPbei ee0tuG12bhjL3Hcp72DZ+IuXT6/AaW02Qwolz/jXK0KtQgNM/7TzCNDtwimSHt12IL 1pEUKa0jDlrwd2etVgNL/qvEK8KhHAj99Rw3/Gwg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [IPv6:2001:67c:2050::465:202]) by sourceware.org (Postfix) with ESMTPS id 4B31D3858D34 for ; Thu, 25 Jun 2020 15:38:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 4B31D3858D34 Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:105:465:1:1:0]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 49t41V1rF2zQlJf; Thu, 25 Jun 2020 17:38:54 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter06.heinlein-hosting.de (spamfilter06.heinlein-hosting.de [80.241.56.125]) (amavisd-new, port 10030) with ESMTP id cfN3bpECrKcY; Thu, 25 Jun 2020 17:38:51 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed] d: Don't set DECL_INITIAL if initializer is 'void'. Date: Thu, 25 Jun 2020 17:38:19 +0200 Message-Id: <20200625153828.2304469-2-ibuclaw@gdcproject.org> In-Reply-To: <20200625153828.2304469-1-ibuclaw@gdcproject.org> References: <20200625153828.2304469-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-MBO-SPAM-Probability: 1 X-Rspamd-Score: 0.29 / 15.00 / 15.00 X-Rspamd-Queue-Id: 286C817FB X-Rspamd-UID: 9d8436 X-Spam-Status: No, score=-16.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Iain Buclaw via Gcc-patches From: Iain Buclaw Reply-To: Iain Buclaw Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hi, This patch fixes a logic bug in the code-gen for variable declarations. Declarations initialized with `= void` were being default initialized. That is not really the intent, and misses the small optimization that should have been gained from using void initializations. Bootstrapped and regression tested on x86_64-linux-gnu, and committed to mainline. Regards Iain. --- gcc/d/ChangeLog: * decl.cc (DeclVisitor::visit (VarDeclaration *)): Don't set DECL_INITIAL if initializer is 'void'. gcc/testsuite/ChangeLog: * gdc.dg/init1.d: New test. --- gcc/d/decl.cc | 11 ++++++++--- gcc/testsuite/gdc.dg/init1.d | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/init1.d diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc index ea6614fb714..77144fe11c8 100644 --- a/gcc/d/decl.cc +++ b/gcc/d/decl.cc @@ -697,13 +697,18 @@ public: return; } - if (d->_init && !d->_init->isVoidInitializer ()) + if (d->_init) { - Expression *e = initializerToExpression (d->_init, d->type); - DECL_INITIAL (decl) = build_expr (e, true); + /* Use the explicit initializer, this includes `void`. */ + if (!d->_init->isVoidInitializer ()) + { + Expression *e = initializerToExpression (d->_init, d->type); + DECL_INITIAL (decl) = build_expr (e, true); + } } else { + /* Use default initializer for the type. */ if (TypeStruct *ts = d->type->isTypeStruct ()) DECL_INITIAL (decl) = layout_struct_initializer (ts->sym); else diff --git a/gcc/testsuite/gdc.dg/init1.d b/gcc/testsuite/gdc.dg/init1.d new file mode 100644 index 00000000000..679ad15460e --- /dev/null +++ b/gcc/testsuite/gdc.dg/init1.d @@ -0,0 +1,9 @@ +// { dg-do run { target hw } } +// { dg-options "-fno-druntime" } +// 'a' should not be default initialized to -1. +static char a = void; + +extern (C) void main() +{ + assert(a == 0); +}